blob: ba931f16889d35534d7e0c14d81d05033d5a855a [file] [log] [blame]
Anna Zaks1c215d02011-12-05 18:58:01 +00001//== TaintTesterChecker.cpp ----------------------------------- -*- C++ -*--=//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Anna Zaks1c215d02011-12-05 18:58:01 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This checker can be used for testing how taint data is propagated.
10//
11//===----------------------------------------------------------------------===//
Kristof Umann76a21502018-12-15 16:23:51 +000012#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000013#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Anna Zaks1c215d02011-12-05 18:58:01 +000014#include "clang/StaticAnalyzer/Core/Checker.h"
15#include "clang/StaticAnalyzer/Core/CheckerManager.h"
16#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Anna Zaks1c215d02011-12-05 18:58:01 +000017
18using namespace clang;
19using namespace ento;
20
21namespace {
22class TaintTesterChecker : public Checker< check::PostStmt<Expr> > {
23
Ahmed Charlesb8984322014-03-07 20:03:18 +000024 mutable std::unique_ptr<BugType> BT;
Anna Zaks1c215d02011-12-05 18:58:01 +000025 void initBugType() const;
26
27 /// Given a pointer argument, get the symbol of the value it contains
28 /// (points to).
29 SymbolRef getPointedToSymbol(CheckerContext &C,
30 const Expr* Arg,
31 bool IssueWarning = true) const;
32
33public:
34 void checkPostStmt(const Expr *E, CheckerContext &C) const;
35};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000036}
Anna Zaks1c215d02011-12-05 18:58:01 +000037
38inline void TaintTesterChecker::initBugType() const {
39 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000040 BT.reset(new BugType(this, "Tainted data", "General"));
Anna Zaks1c215d02011-12-05 18:58:01 +000041}
42
43void TaintTesterChecker::checkPostStmt(const Expr *E,
44 CheckerContext &C) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +000045 ProgramStateRef State = C.getState();
Anna Zaks1c215d02011-12-05 18:58:01 +000046 if (!State)
47 return;
48
Ted Kremenek632e3b72012-01-06 22:09:28 +000049 if (State->isTainted(E, C.getLocationContext())) {
Devin Coughline39bd402015-09-16 22:03:05 +000050 if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
Anna Zaks1c215d02011-12-05 18:58:01 +000051 initBugType();
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000052 auto report = llvm::make_unique<BugReport>(*BT, "tainted",N);
Anna Zaks1c215d02011-12-05 18:58:01 +000053 report->addRange(E->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000054 C.emitReport(std::move(report));
Anna Zaks1c215d02011-12-05 18:58:01 +000055 }
56 }
57}
58
59void ento::registerTaintTesterChecker(CheckerManager &mgr) {
60 mgr.registerChecker<TaintTesterChecker>();
61}
Kristof Umann058a7a42019-01-26 14:23:08 +000062
63bool ento::shouldRegisterTaintTesterChecker(const LangOptions &LO) {
64 return true;
65}