blob: d33c977826a5bc507a1d0be14543f95b7916b1f3 [file] [log] [blame]
Anna Zaks1c215d02011-12-05 18:58:01 +00001//== TaintTesterChecker.cpp ----------------------------------- -*- C++ -*--=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This checker can be used for testing how taint data is propagated.
11//
12//===----------------------------------------------------------------------===//
13#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Anna Zaks1c215d02011-12-05 18:58:01 +000015#include "clang/StaticAnalyzer/Core/Checker.h"
16#include "clang/StaticAnalyzer/Core/CheckerManager.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Anna Zaks1c215d02011-12-05 18:58:01 +000018
19using namespace clang;
20using namespace ento;
21
22namespace {
23class TaintTesterChecker : public Checker< check::PostStmt<Expr> > {
24
Ahmed Charlesb8984322014-03-07 20:03:18 +000025 mutable std::unique_ptr<BugType> BT;
Anna Zaks1c215d02011-12-05 18:58:01 +000026 void initBugType() const;
27
28 /// Given a pointer argument, get the symbol of the value it contains
29 /// (points to).
30 SymbolRef getPointedToSymbol(CheckerContext &C,
31 const Expr* Arg,
32 bool IssueWarning = true) const;
33
34public:
35 void checkPostStmt(const Expr *E, CheckerContext &C) const;
36};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000037}
Anna Zaks1c215d02011-12-05 18:58:01 +000038
39inline void TaintTesterChecker::initBugType() const {
40 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000041 BT.reset(new BugType(this, "Tainted data", "General"));
Anna Zaks1c215d02011-12-05 18:58:01 +000042}
43
44void TaintTesterChecker::checkPostStmt(const Expr *E,
45 CheckerContext &C) const {
Ted Kremenek49b1e382012-01-26 21:29:00 +000046 ProgramStateRef State = C.getState();
Anna Zaks1c215d02011-12-05 18:58:01 +000047 if (!State)
48 return;
49
Ted Kremenek632e3b72012-01-06 22:09:28 +000050 if (State->isTainted(E, C.getLocationContext())) {
Anna Zaks1c215d02011-12-05 18:58:01 +000051 if (ExplodedNode *N = C.addTransition()) {
52 initBugType();
53 BugReport *report = new BugReport(*BT, "tainted",N);
54 report->addRange(E->getSourceRange());
Jordan Rosee10d5a72012-11-02 01:53:40 +000055 C.emitReport(report);
Anna Zaks1c215d02011-12-05 18:58:01 +000056 }
57 }
58}
59
60void ento::registerTaintTesterChecker(CheckerManager &mgr) {
61 mgr.registerChecker<TaintTesterChecker>();
62}