blob: fe628a2512b27b991a2c3a5726076a15c0eba37b [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 Kyrtzidis23ade502011-02-15 22:55:20 +000016#include "ClangSACheckers.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerVisitor.h"
Zhongxing Xub10a7c22009-11-09 06:52:44 +000020
21using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000022using namespace ento;
Zhongxing Xub10a7c22009-11-09 06:52:44 +000023
24namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000025class FixedAddressChecker
Zhongxing Xub10a7c22009-11-09 06:52:44 +000026 : public CheckerVisitor<FixedAddressChecker> {
27 BuiltinBug *BT;
28public:
29 FixedAddressChecker() : BT(0) {}
30 static void *getTag();
31 void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
32};
33}
34
35void *FixedAddressChecker::getTag() {
36 static int x;
37 return &x;
38}
39
40void FixedAddressChecker::PreVisitBinaryOperator(CheckerContext &C,
41 const BinaryOperator *B) {
42 // Using a fixed address is not portable because that address will probably
43 // not be valid in all environments or platforms.
44
John McCall2de56d12010-08-25 11:45:40 +000045 if (B->getOpcode() != BO_Assign)
Zhongxing Xub10a7c22009-11-09 06:52:44 +000046 return;
47
48 QualType T = B->getType();
49 if (!T->isPointerType())
50 return;
51
52 const GRState *state = C.getState();
53
Ted Kremenek13976632010-02-08 16:18:51 +000054 SVal RV = state->getSVal(B->getRHS());
Zhongxing Xub10a7c22009-11-09 06:52:44 +000055
56 if (!RV.isConstant() || RV.isZeroConstant())
57 return;
58
Ted Kremenekd048c6e2010-12-20 21:19:09 +000059 if (ExplodedNode *N = C.generateNode()) {
Zhongxing Xub10a7c22009-11-09 06:52:44 +000060 if (!BT)
61 BT = new BuiltinBug("Use fixed address",
Zhongxing Xudfed7a12009-11-09 07:29:39 +000062 "Using a fixed address is not portable because that "
63 "address will probably not be valid in all "
64 "environments or platforms.");
Benjamin Kramerd02e2322009-11-14 12:08:24 +000065 RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
Zhongxing Xub10a7c22009-11-09 06:52:44 +000066 R->addRange(B->getRHS()->getSourceRange());
67 C.EmitReport(R);
68 }
69}
70
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000071static void RegisterFixedAddressChecker(ExprEngine &Eng) {
Zhongxing Xub10a7c22009-11-09 06:52:44 +000072 Eng.registerCheck(new FixedAddressChecker());
73}
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000074
75void ento::registerFixedAddressChecker(CheckerManager &mgr) {
76 mgr.addCheckerRegisterFunction(RegisterFixedAddressChecker);
77}