summaryrefslogtreecommitdiffstats
path: root/clang/lib/GR/Checkers/FixedAddressChecker.cpp
diff options
context:
space:
mode:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>2010-12-22 18:52:56 +0000
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>2010-12-22 18:52:56 +0000
commita700e976b658860418bc145ec0bdacd4f1db3264 (patch)
tree98372f89a5bce20f6a1edd4fc354dd579b7477dc /clang/lib/GR/Checkers/FixedAddressChecker.cpp
parent2ff5ab1516e48c2fff0138f953d887b5e695214b (diff)
downloadbcm5719-llvm-a700e976b658860418bc145ec0bdacd4f1db3264.tar.gz
bcm5719-llvm-a700e976b658860418bc145ec0bdacd4f1db3264.zip
[analyzer] Refactoring: Move checkers into lib/GR/Checkers and their own library, libclangGRCheckers
llvm-svn: 122422
Diffstat (limited to 'clang/lib/GR/Checkers/FixedAddressChecker.cpp')
-rw-r--r--clang/lib/GR/Checkers/FixedAddressChecker.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/clang/lib/GR/Checkers/FixedAddressChecker.cpp b/clang/lib/GR/Checkers/FixedAddressChecker.cpp
new file mode 100644
index 00000000000..ede6b555d48
--- /dev/null
+++ b/clang/lib/GR/Checkers/FixedAddressChecker.cpp
@@ -0,0 +1,71 @@
+//=== FixedAddressChecker.cpp - Fixed address usage checker ----*- C++ -*--===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This files defines FixedAddressChecker, a builtin checker that checks for
+// assignment of a fixed address to a pointer.
+// This check corresponds to CWE-587.
+//
+//===----------------------------------------------------------------------===//
+
+#include "GRExprEngineInternalChecks.h"
+#include "clang/GR/BugReporter/BugType.h"
+#include "clang/GR/PathSensitive/CheckerVisitor.h"
+
+using namespace clang;
+
+namespace {
+class FixedAddressChecker
+ : public CheckerVisitor<FixedAddressChecker> {
+ BuiltinBug *BT;
+public:
+ FixedAddressChecker() : BT(0) {}
+ static void *getTag();
+ void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
+};
+}
+
+void *FixedAddressChecker::getTag() {
+ static int x;
+ return &x;
+}
+
+void FixedAddressChecker::PreVisitBinaryOperator(CheckerContext &C,
+ const BinaryOperator *B) {
+ // Using a fixed address is not portable because that address will probably
+ // not be valid in all environments or platforms.
+
+ if (B->getOpcode() != BO_Assign)
+ return;
+
+ QualType T = B->getType();
+ if (!T->isPointerType())
+ return;
+
+ const GRState *state = C.getState();
+
+ SVal RV = state->getSVal(B->getRHS());
+
+ if (!RV.isConstant() || RV.isZeroConstant())
+ return;
+
+ if (ExplodedNode *N = C.generateNode()) {
+ if (!BT)
+ BT = new BuiltinBug("Use fixed address",
+ "Using a fixed address is not portable because that "
+ "address will probably not be valid in all "
+ "environments or platforms.");
+ RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
+ R->addRange(B->getRHS()->getSourceRange());
+ C.EmitReport(R);
+ }
+}
+
+void clang::RegisterFixedAddressChecker(GRExprEngine &Eng) {
+ Eng.registerCheck(new FixedAddressChecker());
+}
OpenPOWER on IntegriCloud