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