blob: e30e3d0ce1ca4d9e758b266f3f59c334e880ddef [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
16#include "clang/Analysis/PathSensitive/CheckerVisitor.h"
17#include "GRExprEngineInternalChecks.h"
18
19using namespace clang;
20
21namespace {
22class VISIBILITY_HIDDEN FixedAddressChecker
23 : public CheckerVisitor<FixedAddressChecker> {
24 BuiltinBug *BT;
25public:
26 FixedAddressChecker() : BT(0) {}
27 static void *getTag();
28 void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
29};
30}
31
32void *FixedAddressChecker::getTag() {
33 static int x;
34 return &x;
35}
36
37void FixedAddressChecker::PreVisitBinaryOperator(CheckerContext &C,
38 const BinaryOperator *B) {
39 // Using a fixed address is not portable because that address will probably
40 // not be valid in all environments or platforms.
41
42 if (B->getOpcode() != BinaryOperator::Assign)
43 return;
44
45 QualType T = B->getType();
46 if (!T->isPointerType())
47 return;
48
49 const GRState *state = C.getState();
50
51 SVal RV = state->getSVal(B->getRHS());
52
53 if (!RV.isConstant() || RV.isZeroConstant())
54 return;
55
56 if (ExplodedNode *N = C.GenerateNode(B)) {
57 if (!BT)
58 BT = new BuiltinBug("Use fixed address",
Zhongxing Xudfed7a12009-11-09 07:29:39 +000059 "Using a fixed address is not portable because that "
60 "address will probably not be valid in all "
61 "environments or platforms.");
Zhongxing Xub10a7c22009-11-09 06:52:44 +000062 RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription().c_str(),
63 N);
64 R->addRange(B->getRHS()->getSourceRange());
65 C.EmitReport(R);
66 }
67}
68
69void clang::RegisterFixedAddressChecker(GRExprEngine &Eng) {
70 Eng.registerCheck(new FixedAddressChecker());
71}