Zhongxing Xu | ede7eb2 | 2009-11-09 13:23:31 +0000 | [diff] [blame] | 1 | //=== PointerArithChecker.cpp - Pointer arithmetic 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 PointerArithChecker, a builtin checker that checks for |
| 11 | // pointer arithmetic on locations other than array elements. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 15 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
Zhongxing Xu | ede7eb2 | 2009-11-09 13:23:31 +0000 | [diff] [blame] | 16 | #include "GRExprEngineInternalChecks.h" |
| 17 | |
| 18 | using namespace clang; |
| 19 | |
| 20 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 21 | class PointerArithChecker |
Zhongxing Xu | ede7eb2 | 2009-11-09 13:23:31 +0000 | [diff] [blame] | 22 | : public CheckerVisitor<PointerArithChecker> { |
| 23 | BuiltinBug *BT; |
| 24 | public: |
| 25 | PointerArithChecker() : BT(0) {} |
| 26 | static void *getTag(); |
| 27 | void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B); |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | void *PointerArithChecker::getTag() { |
| 32 | static int x; |
| 33 | return &x; |
| 34 | } |
| 35 | |
| 36 | void PointerArithChecker::PreVisitBinaryOperator(CheckerContext &C, |
| 37 | const BinaryOperator *B) { |
| 38 | if (B->getOpcode() != BinaryOperator::Sub && |
| 39 | B->getOpcode() != BinaryOperator::Add) |
| 40 | return; |
| 41 | |
| 42 | const GRState *state = C.getState(); |
Zhongxing Xu | 6f8c430 | 2010-02-08 09:30:02 +0000 | [diff] [blame] | 43 | SVal LV = state->getExprVal(B->getLHS()); |
| 44 | SVal RV = state->getExprVal(B->getRHS()); |
Zhongxing Xu | ede7eb2 | 2009-11-09 13:23:31 +0000 | [diff] [blame] | 45 | |
| 46 | const MemRegion *LR = LV.getAsRegion(); |
| 47 | |
| 48 | if (!LR || !RV.isConstant()) |
| 49 | return; |
| 50 | |
| 51 | // If pointer arithmetic is done on variables of non-array type, this often |
| 52 | // means behavior rely on memory organization, which is dangerous. |
| 53 | if (isa<VarRegion>(LR) || isa<CodeTextRegion>(LR) || |
| 54 | isa<CompoundLiteralRegion>(LR)) { |
| 55 | |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 56 | if (ExplodedNode *N = C.GenerateNode()) { |
Zhongxing Xu | ede7eb2 | 2009-11-09 13:23:31 +0000 | [diff] [blame] | 57 | if (!BT) |
| 58 | BT = new BuiltinBug("Dangerous pointer arithmetic", |
| 59 | "Pointer arithmetic done on non-array variables " |
| 60 | "means reliance on memory layout, which is " |
| 61 | "dangerous."); |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 62 | RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N); |
Zhongxing Xu | ede7eb2 | 2009-11-09 13:23:31 +0000 | [diff] [blame] | 63 | R->addRange(B->getSourceRange()); |
| 64 | C.EmitReport(R); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void clang::RegisterPointerArithChecker(GRExprEngine &Eng) { |
| 70 | Eng.registerCheck(new PointerArithChecker()); |
| 71 | } |