Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 1 | //=== PointerSubChecker.cpp - Pointer subtraction 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 PointerSubChecker, a builtin checker that checks for |
| 11 | // pointer subtractions on two pointers pointing to different memory chunks. |
| 12 | // This check corresponds to CWE-469. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +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 | 3ce2dc3 | 2009-11-09 05:34:10 +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 | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 22 | |
| 23 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 24 | class PointerSubChecker |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 25 | : public CheckerVisitor<PointerSubChecker> { |
| 26 | BuiltinBug *BT; |
| 27 | public: |
| 28 | PointerSubChecker() : BT(0) {} |
| 29 | static void *getTag(); |
| 30 | void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B); |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | void *PointerSubChecker::getTag() { |
| 35 | static int x; |
| 36 | return &x; |
| 37 | } |
| 38 | |
| 39 | void PointerSubChecker::PreVisitBinaryOperator(CheckerContext &C, |
| 40 | const BinaryOperator *B) { |
| 41 | // When doing pointer subtraction, if the two pointers do not point to the |
| 42 | // same memory chunk, emit a warning. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 43 | if (B->getOpcode() != BO_Sub) |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 44 | return; |
| 45 | |
| 46 | const GRState *state = C.getState(); |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 47 | SVal LV = state->getSVal(B->getLHS()); |
| 48 | SVal RV = state->getSVal(B->getRHS()); |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 49 | |
| 50 | const MemRegion *LR = LV.getAsRegion(); |
| 51 | const MemRegion *RR = RV.getAsRegion(); |
| 52 | |
Zhongxing Xu | adca271 | 2009-11-10 02:37:53 +0000 | [diff] [blame] | 53 | if (!(LR && RR)) |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 54 | return; |
| 55 | |
Zhongxing Xu | adca271 | 2009-11-10 02:37:53 +0000 | [diff] [blame] | 56 | const MemRegion *BaseLR = LR->getBaseRegion(); |
| 57 | const MemRegion *BaseRR = RR->getBaseRegion(); |
| 58 | |
| 59 | if (BaseLR == BaseRR) |
| 60 | return; |
| 61 | |
| 62 | // Allow arithmetic on different symbolic regions. |
| 63 | if (isa<SymbolicRegion>(BaseLR) || isa<SymbolicRegion>(BaseRR)) |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 64 | return; |
| 65 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 66 | if (ExplodedNode *N = C.generateNode()) { |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 67 | if (!BT) |
| 68 | BT = new BuiltinBug("Pointer subtraction", |
Zhongxing Xu | dfed7a1 | 2009-11-09 07:29:39 +0000 | [diff] [blame] | 69 | "Subtraction of two pointers that do not point to " |
| 70 | "the same memory chunk may cause incorrect result."); |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 71 | RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N); |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 72 | R->addRange(B->getSourceRange()); |
| 73 | C.EmitReport(R); |
| 74 | } |
| 75 | } |
| 76 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame^] | 77 | void GR::RegisterPointerSubChecker(GRExprEngine &Eng) { |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 78 | Eng.registerCheck(new PointerSubChecker()); |
| 79 | } |