clang 8.0.0

PointerSubChecker.cpp
Go to the documentation of this file.
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
16 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
17 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21
22 using namespace clang;
23 using namespace ento;
24
25 namespace {
26 class PointerSubChecker
27 : public Checker< check::PreStmt<BinaryOperator> > {
28 mutable std::unique_ptr<BuiltinBug> BT;
29
30 public:
31 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
32 };
33 }
34
35 void PointerSubChecker::checkPreStmt(const BinaryOperator *B,
36 CheckerContext &C) const {
37 // When doing pointer subtraction, if the two pointers do not point to the
38 // same memory chunk, emit a warning.
39 if (B->getOpcode() != BO_Sub)
40 return;
41
42 SVal LV = C.getSVal(B->getLHS());
43 SVal RV = C.getSVal(B->getRHS());
44
45 const MemRegion *LR = LV.getAsRegion();
46 const MemRegion *RR = RV.getAsRegion();
47
48 if (!(LR && RR))
49 return;
50
51 const MemRegion *BaseLR = LR->getBaseRegion();
52 const MemRegion *BaseRR = RR->getBaseRegion();
53
54 if (BaseLR == BaseRR)
55 return;
56
57 // Allow arithmetic on different symbolic regions.
58 if (isa<SymbolicRegion>(BaseLR) || isa<SymbolicRegion>(BaseRR))
59 return;
60
61 if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
62 if (!BT)
63 BT.reset(
64 new BuiltinBug(this, "Pointer subtraction",
65 "Subtraction of two pointers that do not point to "
66 "the same memory chunk may cause incorrect result."));
67 auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
68 R->addRange(B->getSourceRange());
69 C.emitReport(std::move(R));
70 }
71 }
72
73 void ento::registerPointerSubChecker(CheckerManager &mgr) {
74 mgr.registerChecker<PointerSubChecker>();
75 }
ento
clang::BinaryOperator::getOpcode
Opcode getOpcode() const
Definition: Expr.h:3322
BugType.h
Checker.h
CheckerManager.h
clang::BinaryOperator
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3287
BuiltinCheckerRegistration.h
clang::BinaryOperator::getLHS
Expr * getLHS() const
Definition: Expr.h:3327
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::Stmt::getSourceRange
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:276
clang::BinaryOperator::getRHS
Expr * getRHS() const
Definition: Expr.h:3329
CheckerContext.h