clang API Documentation

TaintTesterChecker.cpp
Go to the documentation of this file.
00001 //== TaintTesterChecker.cpp ----------------------------------- -*- C++ -*--=//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This checker can be used for testing how taint data is propagated.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 #include "ClangSACheckers.h"
00014 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
00015 #include "clang/StaticAnalyzer/Core/Checker.h"
00016 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00017 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
00018 
00019 using namespace clang;
00020 using namespace ento;
00021 
00022 namespace {
00023 class TaintTesterChecker : public Checker< check::PostStmt<Expr> > {
00024 
00025   mutable std::unique_ptr<BugType> BT;
00026   void initBugType() const;
00027 
00028   /// Given a pointer argument, get the symbol of the value it contains
00029   /// (points to).
00030   SymbolRef getPointedToSymbol(CheckerContext &C,
00031                                const Expr* Arg,
00032                                bool IssueWarning = true) const;
00033 
00034 public:
00035   void checkPostStmt(const Expr *E, CheckerContext &C) const;
00036 };
00037 }
00038 
00039 inline void TaintTesterChecker::initBugType() const {
00040   if (!BT)
00041     BT.reset(new BugType(this, "Tainted data", "General"));
00042 }
00043 
00044 void TaintTesterChecker::checkPostStmt(const Expr *E,
00045                                        CheckerContext &C) const {
00046   ProgramStateRef State = C.getState();
00047   if (!State)
00048     return;
00049 
00050   if (State->isTainted(E, C.getLocationContext())) {
00051     if (ExplodedNode *N = C.addTransition()) {
00052       initBugType();
00053       BugReport *report = new BugReport(*BT, "tainted",N);
00054       report->addRange(E->getSourceRange());
00055       C.emitReport(report);
00056     }
00057   }
00058 }
00059 
00060 void ento::registerTaintTesterChecker(CheckerManager &mgr) {
00061   mgr.registerChecker<TaintTesterChecker>();
00062 }