diff options
author | Zhongxing Xu <xuzhongxing@gmail.com> | 2009-11-11 12:33:27 +0000 |
---|---|---|
committer | Zhongxing Xu <xuzhongxing@gmail.com> | 2009-11-11 12:33:27 +0000 |
commit | 58e689fead1490611bcd114fb707bfc08a12049e (patch) | |
tree | 49d4707ae326eb1eef3ec68f664e3f1a3b194217 /lib/Analysis | |
parent | b991f48ccff0567d581cf95e4eda1bffd5bbada3 (diff) |
Reimplement out-of-bound array access checker with the new checker interface.
Now only one test case is XFAIL'ed.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86834 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis')
-rw-r--r-- | lib/Analysis/ArrayBoundChecker.cpp | 87 | ||||
-rw-r--r-- | lib/Analysis/CMakeLists.txt | 1 | ||||
-rw-r--r-- | lib/Analysis/GRExprEngineInternalChecks.cpp | 1 | ||||
-rw-r--r-- | lib/Analysis/GRExprEngineInternalChecks.h | 1 |
4 files changed, 90 insertions, 0 deletions
diff --git a/lib/Analysis/ArrayBoundChecker.cpp b/lib/Analysis/ArrayBoundChecker.cpp new file mode 100644 index 0000000000..ae8e1149c5 --- /dev/null +++ b/lib/Analysis/ArrayBoundChecker.cpp @@ -0,0 +1,87 @@ +//== ArrayBoundChecker.cpp ------------------------------*- C++ -*--==// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines ArrayBoundChecker, which is a path-sensitive check +// which looks for an out-of-bound array element access. +// +//===----------------------------------------------------------------------===// + +#include "GRExprEngineInternalChecks.h" +#include "clang/Analysis/PathSensitive/GRExprEngine.h" +#include "clang/Analysis/PathSensitive/BugReporter.h" +#include "clang/Analysis/PathSensitive/CheckerVisitor.h" + +using namespace clang; + +namespace { +class VISIBILITY_HIDDEN ArrayBoundChecker : + public CheckerVisitor<ArrayBoundChecker> { + BuiltinBug *BT; +public: + ArrayBoundChecker() : BT(0) {} + static void *getTag(); + void VisitLocation(CheckerContext &C, const Stmt *S, SVal l); +}; +} + +void clang::RegisterArrayBoundChecker(GRExprEngine &Eng) { + Eng.registerCheck(new ArrayBoundChecker()); +} + +void *ArrayBoundChecker::getTag() { + static int x = 0; return &x; +} + +void ArrayBoundChecker::VisitLocation(CheckerContext &C, const Stmt *S, SVal l){ + // Check for out of bound array element access. + const MemRegion *R = l.getAsRegion(); + if (!R) + return; + + R = R->StripCasts(); + + const ElementRegion *ER = dyn_cast<ElementRegion>(R); + if (!ER) + return; + + // Get the index of the accessed element. + DefinedOrUnknownSVal &Idx = cast<DefinedOrUnknownSVal>(ER->getIndex()); + + const GRState *state = C.getState(); + + // Get the size of the array. + SVal NumVal = C.getStoreManager().getSizeInElements(state, + ER->getSuperRegion()); + DefinedOrUnknownSVal &NumElements = cast<DefinedOrUnknownSVal>(NumVal); + + const GRState *StInBound = state->AssumeInBound(Idx, NumElements, true); + const GRState *StOutBound = state->AssumeInBound(Idx, NumElements, false); + if (StOutBound && !StInBound) { + ExplodedNode *N = C.GenerateNode(S, StOutBound, true); + + if (!N) + return; + + if (!BT) + BT = new BuiltinBug("Out-of-bound array access", + "Access out-of-bound array element (buffer overflow)"); + + // FIXME: It would be nice to eventually make this diagnostic more clear, + // e.g., by referencing the original declaration or by saying *why* this + // reference is outside the range. + + // Generate a report for this bug. + RangedBugReport *report = + new RangedBugReport(*BT, BT->getDescription().c_str(), N); + + report->addRange(S->getSourceRange()); + + C.EmitReport(report); + } +} diff --git a/lib/Analysis/CMakeLists.txt b/lib/Analysis/CMakeLists.txt index 2d684862c3..82ef9842be 100644 --- a/lib/Analysis/CMakeLists.txt +++ b/lib/Analysis/CMakeLists.txt @@ -2,6 +2,7 @@ set(LLVM_NO_RTTI 1) add_clang_library(clangAnalysis AnalysisContext.cpp + ArrayBoundChecker.cpp AttrNonNullChecker.cpp BadCallChecker.cpp BasicConstraintManager.cpp diff --git a/lib/Analysis/GRExprEngineInternalChecks.cpp b/lib/Analysis/GRExprEngineInternalChecks.cpp index 447dfc08bf..ea508a05f7 100644 --- a/lib/Analysis/GRExprEngineInternalChecks.cpp +++ b/lib/Analysis/GRExprEngineInternalChecks.cpp @@ -414,4 +414,5 @@ void GRExprEngine::RegisterInternalChecks() { RegisterReturnPointerRangeChecker(*this); RegisterCastToStructChecker(*this); + RegisterArrayBoundChecker(*this); } diff --git a/lib/Analysis/GRExprEngineInternalChecks.h b/lib/Analysis/GRExprEngineInternalChecks.h index 22151a12ef..a0687fd10b 100644 --- a/lib/Analysis/GRExprEngineInternalChecks.h +++ b/lib/Analysis/GRExprEngineInternalChecks.h @@ -32,6 +32,7 @@ void RegisterPointerArithChecker(GRExprEngine &Eng); void RegisterFixedAddressChecker(GRExprEngine &Eng); void RegisterCastToStructChecker(GRExprEngine &Eng); void RegisterUndefinedArgChecker(GRExprEngine &Eng); +void RegisterArrayBoundChecker(GRExprEngine &Eng); } // end clang namespace #endif |