blob: 031ca44b602eb5ebc0166e33d34f608c75bba58e [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 {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000022class FixedAddressChecker
Zhongxing Xub10a7c22009-11-09 06:52:44 +000023 : 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
Ted Kremenek19d67b52009-11-23 22:22:01 +000056 if (ExplodedNode *N = C.GenerateNode()) {
Zhongxing Xub10a7c22009-11-09 06:52:44 +000057 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.");
Benjamin Kramerd02e2322009-11-14 12:08:24 +000062 RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
Zhongxing Xub10a7c22009-11-09 06:52:44 +000063 R->addRange(B->getRHS()->getSourceRange());
64 C.EmitReport(R);
65 }
66}
67
68void clang::RegisterFixedAddressChecker(GRExprEngine &Eng) {
69 Eng.registerCheck(new FixedAddressChecker());
70}