Zhongxing Xu | b10a7c2 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 1 | //=== FixedAddressChecker.cpp - Fixed address usage checker ----*- 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 files defines FixedAddressChecker, a builtin checker that checks for |
| 11 | // assignment of a fixed address to a pointer. |
| 12 | // This check corresponds to CWE-587. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "clang/Analysis/PathSensitive/CheckerVisitor.h" |
| 17 | #include "GRExprEngineInternalChecks.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | namespace { |
| 22 | class VISIBILITY_HIDDEN FixedAddressChecker |
| 23 | : public CheckerVisitor<FixedAddressChecker> { |
| 24 | BuiltinBug *BT; |
| 25 | public: |
| 26 | FixedAddressChecker() : BT(0) {} |
| 27 | static void *getTag(); |
| 28 | void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B); |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | void *FixedAddressChecker::getTag() { |
| 33 | static int x; |
| 34 | return &x; |
| 35 | } |
| 36 | |
| 37 | void FixedAddressChecker::PreVisitBinaryOperator(CheckerContext &C, |
| 38 | const BinaryOperator *B) { |
| 39 | // Using a fixed address is not portable because that address will probably |
| 40 | // not be valid in all environments or platforms. |
| 41 | |
| 42 | if (B->getOpcode() != BinaryOperator::Assign) |
| 43 | return; |
| 44 | |
| 45 | QualType T = B->getType(); |
| 46 | if (!T->isPointerType()) |
| 47 | return; |
| 48 | |
| 49 | const GRState *state = C.getState(); |
| 50 | |
| 51 | SVal RV = state->getSVal(B->getRHS()); |
| 52 | |
| 53 | if (!RV.isConstant() || RV.isZeroConstant()) |
| 54 | return; |
| 55 | |
| 56 | if (ExplodedNode *N = C.GenerateNode(B)) { |
| 57 | if (!BT) |
| 58 | BT = new BuiltinBug("Use fixed address", |
Zhongxing Xu | dfed7a1 | 2009-11-09 07:29:39 +0000 | [diff] [blame] | 59 | "Using a fixed address is not portable because that " |
| 60 | "address will probably not be valid in all " |
| 61 | "environments or platforms."); |
Zhongxing Xu | b10a7c2 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 62 | RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription().c_str(), |
| 63 | N); |
| 64 | R->addRange(B->getRHS()->getSourceRange()); |
| 65 | C.EmitReport(R); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void clang::RegisterFixedAddressChecker(GRExprEngine &Eng) { |
| 70 | Eng.registerCheck(new FixedAddressChecker()); |
| 71 | } |