1 //== ArrayBoundChecker.cpp ------------------------------*- C++ -*--==//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines ArrayBoundChecker, which is a path-sensitive check
10 // which looks for an out-of-bound array element access.
12 //===----------------------------------------------------------------------===//
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/DynamicExtent.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
22 using namespace clang
;
26 class ArrayBoundChecker
:
27 public Checker
<check::Location
> {
28 mutable std::unique_ptr
<BugType
> BT
;
31 void checkLocation(SVal l
, bool isLoad
, const Stmt
* S
,
32 CheckerContext
&C
) const;
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();
43 const ElementRegion
*ER
= dyn_cast
<ElementRegion
>(R
);
47 // Get the index of the accessed element.
48 DefinedOrUnknownSVal Idx
= ER
->getIndex().castAs
<DefinedOrUnknownSVal
>();
50 // Zero index is always in bound, this also passes ElementRegions created for
52 if (Idx
.isZeroConstant())
55 ProgramStateRef state
= C
.getState();
57 // Get the size of the array.
58 DefinedOrUnknownSVal ElementCount
= getDynamicElementCount(
59 state
, ER
->getSuperRegion(), C
.getSValBuilder(), ER
->getValueType());
61 ProgramStateRef StInBound
, StOutBound
;
62 std::tie(StInBound
, StOutBound
) = state
->assumeInBoundDual(Idx
, ElementCount
);
63 if (StOutBound
&& !StInBound
) {
64 ExplodedNode
*N
= C
.generateErrorNode(StOutBound
);
69 BT
.reset(new BugType(this, "Out-of-bound array access"));
71 // FIXME: It would be nice to eventually make this diagnostic more clear,
72 // e.g., by referencing the original declaration or by saying *why* this
73 // reference is outside the range.
75 // Generate a report for this bug.
76 auto report
= std::make_unique
<PathSensitiveBugReport
>(
77 *BT
, "Access out-of-bound array element (buffer overflow)", N
);
79 report
->addRange(LoadS
->getSourceRange());
80 C
.emitReport(std::move(report
));
84 // Array bound check succeeded. From this point forward the array bound
85 // should always succeed.
86 C
.addTransition(StInBound
);
89 void ento::registerArrayBoundChecker(CheckerManager
&mgr
) {
90 mgr
.registerChecker
<ArrayBoundChecker
>();
93 bool ento::shouldRegisterArrayBoundChecker(const CheckerManager
&mgr
) {