blob: 77e9876d47905205ff3110af74025ad7f0897f26 [file] [log] [blame]
Zhongxing Xub10a7c22009-11-09 06:52:44 +00001//=== FixedAddressChecker.cpp - Fixed address usage checker ----*- C++ -*--===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This files defines FixedAddressChecker, a builtin checker that checks for
11// assignment of a fixed address to a pointer.
12// This check corresponds to CWE-587.
13//
14//===----------------------------------------------------------------------===//
15
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000016#include "ExprEngineInternalChecks.h"
Ted Kremenek21142582010-12-23 19:38:26 +000017#include "clang/StaticAnalyzer/BugReporter/BugType.h"
18#include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h"
Zhongxing Xub10a7c22009-11-09 06:52:44 +000019
20using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000021using namespace ento;
Zhongxing Xub10a7c22009-11-09 06:52:44 +000022
23namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000024class FixedAddressChecker
Zhongxing Xub10a7c22009-11-09 06:52:44 +000025 : public CheckerVisitor<FixedAddressChecker> {
26 BuiltinBug *BT;
27public:
28 FixedAddressChecker() : BT(0) {}
29 static void *getTag();
30 void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
31};
32}
33
34void *FixedAddressChecker::getTag() {
35 static int x;
36 return &x;
37}
38
39void FixedAddressChecker::PreVisitBinaryOperator(CheckerContext &C,
40 const BinaryOperator *B) {
41 // Using a fixed address is not portable because that address will probably
42 // not be valid in all environments or platforms.
43
John McCall2de56d12010-08-25 11:45:40 +000044 if (B->getOpcode() != BO_Assign)
Zhongxing Xub10a7c22009-11-09 06:52:44 +000045 return;
46
47 QualType T = B->getType();
48 if (!T->isPointerType())
49 return;
50
51 const GRState *state = C.getState();
52
Ted Kremenek13976632010-02-08 16:18:51 +000053 SVal RV = state->getSVal(B->getRHS());
Zhongxing Xub10a7c22009-11-09 06:52:44 +000054
55 if (!RV.isConstant() || RV.isZeroConstant())
56 return;
57
Ted Kremenekd048c6e2010-12-20 21:19:09 +000058 if (ExplodedNode *N = C.generateNode()) {
Zhongxing Xub10a7c22009-11-09 06:52:44 +000059 if (!BT)
60 BT = new BuiltinBug("Use fixed address",
Zhongxing Xudfed7a12009-11-09 07:29:39 +000061 "Using a fixed address is not portable because that "
62 "address will probably not be valid in all "
63 "environments or platforms.");
Benjamin Kramerd02e2322009-11-14 12:08:24 +000064 RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
Zhongxing Xub10a7c22009-11-09 06:52:44 +000065 R->addRange(B->getRHS()->getSourceRange());
66 C.EmitReport(R);
67 }
68}
69
Ted Kremenek9ef65372010-12-23 07:20:52 +000070void ento::RegisterFixedAddressChecker(ExprEngine &Eng) {
Zhongxing Xub10a7c22009-11-09 06:52:44 +000071 Eng.registerCheck(new FixedAddressChecker());
72}