blob: b2ef4ffd43296954baf1b21e4148beb4fb9527ac [file] [log] [blame]
Zhongxing Xu6c306c82009-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 Kyrtzidisa9215282011-02-15 22:55:20 +000016#include "ClangSACheckers.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerVisitor.h"
Zhongxing Xu6c306c82009-11-09 06:52:44 +000019
20using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000021using namespace ento;
Zhongxing Xu6c306c82009-11-09 06:52:44 +000022
23namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000024class FixedAddressChecker
Zhongxing Xu6c306c82009-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 McCalle3027922010-08-25 11:45:40 +000044 if (B->getOpcode() != BO_Assign)
Zhongxing Xu6c306c82009-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 Kremenek57f09892010-02-08 16:18:51 +000053 SVal RV = state->getSVal(B->getRHS());
Zhongxing Xu6c306c82009-11-09 06:52:44 +000054
55 if (!RV.isConstant() || RV.isZeroConstant())
56 return;
57
Ted Kremenek750b7ac2010-12-20 21:19:09 +000058 if (ExplodedNode *N = C.generateNode()) {
Zhongxing Xu6c306c82009-11-09 06:52:44 +000059 if (!BT)
60 BT = new BuiltinBug("Use fixed address",
Zhongxing Xu85000202009-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 Kramerf4c511b2009-11-14 12:08:24 +000064 RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
Zhongxing Xu6c306c82009-11-09 06:52:44 +000065 R->addRange(B->getRHS()->getSourceRange());
66 C.EmitReport(R);
67 }
68}
69
Argyrios Kyrtzidisa9215282011-02-15 22:55:20 +000070void ento::registerFixedAddressChecker(ExprEngine &Eng) {
Zhongxing Xu6c306c82009-11-09 06:52:44 +000071 Eng.registerCheck(new FixedAddressChecker());
72}