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 { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 22 | class FixedAddressChecker |
Zhongxing Xu | b10a7c2 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 56 | if (ExplodedNode *N = C.GenerateNode()) { |
Zhongxing Xu | b10a7c2 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 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."); |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 62 | RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N); |
Zhongxing Xu | b10a7c2 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 63 | R->addRange(B->getRHS()->getSourceRange()); |
| 64 | C.EmitReport(R); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void clang::RegisterFixedAddressChecker(GRExprEngine &Eng) { |
| 69 | Eng.registerCheck(new FixedAddressChecker()); |
| 70 | } |