xref: /freebsd-src/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/ArrayBoundChecker.cpp (revision 647cbc5de815c5651677bf8582797f716ec7b48d)
10b57cec5SDimitry Andric //== ArrayBoundChecker.cpp ------------------------------*- C++ -*--==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines ArrayBoundChecker, which is a path-sensitive check
100b57cec5SDimitry Andric // which looks for an out-of-bound array element access.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
150b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
160b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
170b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
180b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19fe6060f1SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
200b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace clang;
230b57cec5SDimitry Andric using namespace ento;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric namespace {
260b57cec5SDimitry Andric class ArrayBoundChecker :
270b57cec5SDimitry Andric     public Checker<check::Location> {
28*647cbc5dSDimitry Andric   const BugType BT{this, "Out-of-bound array access"};
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric public:
310b57cec5SDimitry Andric   void checkLocation(SVal l, bool isLoad, const Stmt* S,
320b57cec5SDimitry Andric                      CheckerContext &C) const;
330b57cec5SDimitry Andric };
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric 
checkLocation(SVal l,bool isLoad,const Stmt * LoadS,CheckerContext & C) const360b57cec5SDimitry Andric void ArrayBoundChecker::checkLocation(SVal l, bool isLoad, const Stmt* LoadS,
370b57cec5SDimitry Andric                                       CheckerContext &C) const {
380b57cec5SDimitry Andric   // Check for out of bound array element access.
390b57cec5SDimitry Andric   const MemRegion *R = l.getAsRegion();
400b57cec5SDimitry Andric   if (!R)
410b57cec5SDimitry Andric     return;
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   const ElementRegion *ER = dyn_cast<ElementRegion>(R);
440b57cec5SDimitry Andric   if (!ER)
450b57cec5SDimitry Andric     return;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   // Get the index of the accessed element.
480b57cec5SDimitry Andric   DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   // Zero index is always in bound, this also passes ElementRegions created for
510b57cec5SDimitry Andric   // pointer casts.
520b57cec5SDimitry Andric   if (Idx.isZeroConstant())
530b57cec5SDimitry Andric     return;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   // Get the size of the array.
585ffd83dbSDimitry Andric   DefinedOrUnknownSVal ElementCount = getDynamicElementCount(
595ffd83dbSDimitry Andric       state, ER->getSuperRegion(), C.getSValBuilder(), ER->getValueType());
600b57cec5SDimitry Andric 
6181ad6265SDimitry Andric   ProgramStateRef StInBound, StOutBound;
6281ad6265SDimitry Andric   std::tie(StInBound, StOutBound) = state->assumeInBoundDual(Idx, ElementCount);
630b57cec5SDimitry Andric   if (StOutBound && !StInBound) {
640b57cec5SDimitry Andric     ExplodedNode *N = C.generateErrorNode(StOutBound);
650b57cec5SDimitry Andric     if (!N)
660b57cec5SDimitry Andric       return;
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric     // FIXME: It would be nice to eventually make this diagnostic more clear,
690b57cec5SDimitry Andric     // e.g., by referencing the original declaration or by saying *why* this
700b57cec5SDimitry Andric     // reference is outside the range.
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric     // Generate a report for this bug.
735f757f3fSDimitry Andric     auto report = std::make_unique<PathSensitiveBugReport>(
74*647cbc5dSDimitry Andric         BT, "Access out-of-bound array element (buffer overflow)", N);
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric     report->addRange(LoadS->getSourceRange());
770b57cec5SDimitry Andric     C.emitReport(std::move(report));
780b57cec5SDimitry Andric     return;
790b57cec5SDimitry Andric   }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   // Array bound check succeeded.  From this point forward the array bound
820b57cec5SDimitry Andric   // should always succeed.
830b57cec5SDimitry Andric   C.addTransition(StInBound);
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric 
registerArrayBoundChecker(CheckerManager & mgr)860b57cec5SDimitry Andric void ento::registerArrayBoundChecker(CheckerManager &mgr) {
870b57cec5SDimitry Andric   mgr.registerChecker<ArrayBoundChecker>();
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric 
shouldRegisterArrayBoundChecker(const CheckerManager & mgr)905ffd83dbSDimitry Andric bool ento::shouldRegisterArrayBoundChecker(const CheckerManager &mgr) {
910b57cec5SDimitry Andric   return true;
920b57cec5SDimitry Andric }
93