clang 10.0.0git

ArrayBoundChecker.cpp
Go to the documentation of this file.
1 //== ArrayBoundChecker.cpp ------------------------------*- C++ -*--==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines ArrayBoundChecker, which is a path-sensitive check
10 // which looks for an out-of-bound array element access.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
16 #include "clang/StaticAnalyzer/Core/Checker.h"
17 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
20
21 using namespace clang;
22 using namespace ento;
23
24 namespace {
25 class ArrayBoundChecker :
26 public Checker<check::Location> {
27 mutable std::unique_ptr<BuiltinBug> BT;
28
29 public:
30 void checkLocation(SVal l, bool isLoad, const Stmt* S,
31 CheckerContext &C) const;
32 };
33 }
34
35 void ArrayBoundChecker::checkLocation(SVal l, bool isLoad, const Stmt* LoadS,
36 CheckerContext &C) const {
37 // Check for out of bound array element access.
38 const MemRegion *R = l.getAsRegion();
39 if (!R)
40 return;
41
42 const ElementRegion *ER = dyn_cast<ElementRegion>(R);
43 if (!ER)
44 return;
45
46 // Get the index of the accessed element.
47 DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
48
49 // Zero index is always in bound, this also passes ElementRegions created for
50 // pointer casts.
51 if (Idx.isZeroConstant())
52 return;
53
54 ProgramStateRef state = C.getState();
55
56 // Get the size of the array.
57 DefinedOrUnknownSVal NumElements
58 = C.getStoreManager().getSizeInElements(state, ER->getSuperRegion(),
59 ER->getValueType());
60
61 ProgramStateRef StInBound = state->assumeInBound(Idx, NumElements, true);
62 ProgramStateRef StOutBound = state->assumeInBound(Idx, NumElements, false);
63 if (StOutBound && !StInBound) {
64 ExplodedNode *N = C.generateErrorNode(StOutBound);
65 if (!N)
66 return;
67
68 if (!BT)
69 BT.reset(new BuiltinBug(
70 this, "Out-of-bound array access",
71 "Access out-of-bound array element (buffer overflow)"));
72
73 // FIXME: It would be nice to eventually make this diagnostic more clear,
74 // e.g., by referencing the original declaration or by saying *why* this
75 // reference is outside the range.
76
77 // Generate a report for this bug.
78 auto report =
79 std::make_unique<PathSensitiveBugReport>(*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 }
94
95 bool ento::shouldRegisterArrayBoundChecker(const LangOptions &LO) {
96 return true;
97 }
ento
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
clang::ento::ProgramStateRef
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
Definition: ProgramState_Fwd.h:37
BugType.h
ExprEngine.h
Checker.h
CheckerManager.h
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:53
clang::CodeGen::state
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
Definition: CGOpenMPRuntime.h:802
BuiltinCheckerRegistration.h
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
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:263
CheckerContext.h