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 | |
Zhongxing Xu | b10a7c2 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 16 | #include "GRExprEngineInternalChecks.h" |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 17 | #include "clang/Checker/BugReporter/BugType.h" |
| 18 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
Zhongxing Xu | b10a7c2 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 23 | class FixedAddressChecker |
Zhongxing Xu | b10a7c2 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 24 | : public CheckerVisitor<FixedAddressChecker> { |
| 25 | BuiltinBug *BT; |
| 26 | public: |
| 27 | FixedAddressChecker() : BT(0) {} |
| 28 | static void *getTag(); |
| 29 | void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B); |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | void *FixedAddressChecker::getTag() { |
| 34 | static int x; |
| 35 | return &x; |
| 36 | } |
| 37 | |
| 38 | void FixedAddressChecker::PreVisitBinaryOperator(CheckerContext &C, |
| 39 | const BinaryOperator *B) { |
| 40 | // Using a fixed address is not portable because that address will probably |
| 41 | // not be valid in all environments or platforms. |
| 42 | |
| 43 | if (B->getOpcode() != BinaryOperator::Assign) |
| 44 | return; |
| 45 | |
| 46 | QualType T = B->getType(); |
| 47 | if (!T->isPointerType()) |
| 48 | return; |
| 49 | |
| 50 | const GRState *state = C.getState(); |
| 51 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 52 | SVal RV = state->getSVal(B->getRHS()); |
Zhongxing Xu | b10a7c2 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 53 | |
| 54 | if (!RV.isConstant() || RV.isZeroConstant()) |
| 55 | return; |
| 56 | |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 57 | if (ExplodedNode *N = C.GenerateNode()) { |
Zhongxing Xu | b10a7c2 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 58 | if (!BT) |
| 59 | BT = new BuiltinBug("Use fixed address", |
Zhongxing Xu | dfed7a1 | 2009-11-09 07:29:39 +0000 | [diff] [blame] | 60 | "Using a fixed address is not portable because that " |
| 61 | "address will probably not be valid in all " |
| 62 | "environments or platforms."); |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 63 | RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N); |
Zhongxing Xu | b10a7c2 | 2009-11-09 06:52:44 +0000 | [diff] [blame] | 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 | } |