clang  6.0.0svn
ArrayBoundChecker.cpp
Go to the documentation of this file.
1 //== ArrayBoundChecker.cpp ------------------------------*- 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 file defines ArrayBoundChecker, which is a path-sensitive check
11 // which looks for an out-of-bound array element access.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "ClangSACheckers.h"
21 
22 using namespace clang;
23 using namespace ento;
24 
25 namespace {
26 class ArrayBoundChecker :
27  public Checker<check::Location> {
28  mutable std::unique_ptr<BuiltinBug> BT;
29 
30 public:
31  void checkLocation(SVal l, bool isLoad, const Stmt* S,
32  CheckerContext &C) const;
33 };
34 }
35 
36 void ArrayBoundChecker::checkLocation(SVal l, bool isLoad, const Stmt* LoadS,
37  CheckerContext &C) const {
38  // Check for out of bound array element access.
39  const MemRegion *R = l.getAsRegion();
40  if (!R)
41  return;
42 
43  const ElementRegion *ER = dyn_cast<ElementRegion>(R);
44  if (!ER)
45  return;
46 
47  // Get the index of the accessed element.
49 
50  // Zero index is always in bound, this also passes ElementRegions created for
51  // pointer casts.
52  if (Idx.isZeroConstant())
53  return;
54 
56 
57  // Get the size of the array.
58  DefinedOrUnknownSVal NumElements
60  ER->getValueType());
61 
62  ProgramStateRef StInBound = state->assumeInBound(Idx, NumElements, true);
63  ProgramStateRef StOutBound = state->assumeInBound(Idx, NumElements, false);
64  if (StOutBound && !StInBound) {
65  ExplodedNode *N = C.generateErrorNode(StOutBound);
66  if (!N)
67  return;
68 
69  if (!BT)
70  BT.reset(new BuiltinBug(
71  this, "Out-of-bound array access",
72  "Access out-of-bound array element (buffer overflow)"));
73 
74  // FIXME: It would be nice to eventually make this diagnostic more clear,
75  // e.g., by referencing the original declaration or by saying *why* this
76  // reference is outside the range.
77 
78  // Generate a report for this bug.
79  auto report = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
80 
81  report->addRange(LoadS->getSourceRange());
82  C.emitReport(std::move(report));
83  return;
84  }
85 
86  // Array bound check succeeded. From this point forward the array bound
87  // should always succeed.
88  C.addTransition(StInBound);
89 }
90 
91 void ento::registerArrayBoundChecker(CheckerManager &mgr) {
92  mgr.registerChecker<ArrayBoundChecker>();
93 }
virtual DefinedOrUnknownSVal getSizeInElements(ProgramStateRef state, const MemRegion *region, QualType EleTy)
Definition: Store.h:129
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:79
ExplodedNode * generateErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
Stmt - This represents one statement.
Definition: Stmt.h:66
NonLoc getIndex() const
Definition: MemRegion.h:1085
ExplodedNode * addTransition(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generates a new transition in the program state graph (ExplodedGraph).
const MemRegion * getSuperRegion() const
Definition: MemRegion.h:430
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
CHECKER * registerChecker()
Used to register checkers.
StoreManager & getStoreManager()
const MemRegion * getAsRegion() const
Definition: SVals.cpp:140
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:63
Dataflow Directional Tag Classes.
bool isZeroConstant() const
Definition: SVals.cpp:219
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition: SVals.h:92
const ProgramStateRef & getState() const
QualType getValueType() const override
Definition: MemRegion.h:1087
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:265
ElementRegin is used to represent both array elements and casts.
Definition: MemRegion.h:1066