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 | |
Argyrios Kyrtzidis | 23ade50 | 2011-02-15 22:55:20 +0000 | [diff] [blame] | 16 | #include "ClangSACheckers.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 23 | using namespace ento; |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 24 | |
| 25 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 26 | class PointerSubChecker |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 27 | : public Checker< check::PreStmt<BinaryOperator> > { |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 28 | mutable llvm::OwningPtr<BuiltinBug> BT; |
| 29 | |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 30 | public: |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 31 | void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const; |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 32 | }; |
| 33 | } |
| 34 | |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 35 | void PointerSubChecker::checkPreStmt(const BinaryOperator *B, |
| 36 | CheckerContext &C) const { |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 37 | // When doing pointer subtraction, if the two pointers do not point to the |
| 38 | // same memory chunk, emit a warning. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 39 | if (B->getOpcode() != BO_Sub) |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 40 | return; |
| 41 | |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 42 | const ProgramState *state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 43 | const LocationContext *LCtx = C.getLocationContext(); |
| 44 | SVal LV = state->getSVal(B->getLHS(), LCtx); |
| 45 | SVal RV = state->getSVal(B->getRHS(), LCtx); |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 46 | |
| 47 | const MemRegion *LR = LV.getAsRegion(); |
| 48 | const MemRegion *RR = RV.getAsRegion(); |
| 49 | |
Zhongxing Xu | adca271 | 2009-11-10 02:37:53 +0000 | [diff] [blame] | 50 | if (!(LR && RR)) |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 51 | return; |
| 52 | |
Zhongxing Xu | adca271 | 2009-11-10 02:37:53 +0000 | [diff] [blame] | 53 | const MemRegion *BaseLR = LR->getBaseRegion(); |
| 54 | const MemRegion *BaseRR = RR->getBaseRegion(); |
| 55 | |
| 56 | if (BaseLR == BaseRR) |
| 57 | return; |
| 58 | |
| 59 | // Allow arithmetic on different symbolic regions. |
| 60 | if (isa<SymbolicRegion>(BaseLR) || isa<SymbolicRegion>(BaseRR)) |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 61 | return; |
| 62 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 63 | if (ExplodedNode *N = C.addTransition()) { |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 64 | if (!BT) |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 65 | BT.reset(new BuiltinBug("Pointer subtraction", |
Zhongxing Xu | dfed7a1 | 2009-11-09 07:29:39 +0000 | [diff] [blame] | 66 | "Subtraction of two pointers that do not point to " |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 67 | "the same memory chunk may cause incorrect result.")); |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 68 | BugReport *R = new BugReport(*BT, BT->getDescription(), N); |
Zhongxing Xu | 3ce2dc3 | 2009-11-09 05:34:10 +0000 | [diff] [blame] | 69 | R->addRange(B->getSourceRange()); |
| 70 | C.EmitReport(R); |
| 71 | } |
| 72 | } |
| 73 | |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 74 | void ento::registerPointerSubChecker(CheckerManager &mgr) { |
Argyrios Kyrtzidis | 983326f | 2011-02-23 01:05:36 +0000 | [diff] [blame] | 75 | mgr.registerChecker<PointerSubChecker>(); |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 76 | } |