blob: d699deecd768d3013d4d6f48fa4a00167a89b33d [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 Kyrtzidisec8605f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Zhongxing Xub10a7c22009-11-09 06:52:44 +000021
22using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000023using namespace ento;
Zhongxing Xub10a7c22009-11-09 06:52:44 +000024
25namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000026class FixedAddressChecker
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000027 : public Checker< check::PreStmt<BinaryOperator> > {
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000028 mutable llvm::OwningPtr<BuiltinBug> BT;
29
Zhongxing Xub10a7c22009-11-09 06:52:44 +000030public:
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000031 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
Zhongxing Xub10a7c22009-11-09 06:52:44 +000032};
33}
34
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000035void FixedAddressChecker::checkPreStmt(const BinaryOperator *B,
36 CheckerContext &C) const {
Zhongxing Xub10a7c22009-11-09 06:52:44 +000037 // Using a fixed address is not portable because that address will probably
38 // not be valid in all environments or platforms.
39
John McCall2de56d12010-08-25 11:45:40 +000040 if (B->getOpcode() != BO_Assign)
Zhongxing Xub10a7c22009-11-09 06:52:44 +000041 return;
42
43 QualType T = B->getType();
44 if (!T->isPointerType())
45 return;
46
47 const GRState *state = C.getState();
48
Ted Kremenek13976632010-02-08 16:18:51 +000049 SVal RV = state->getSVal(B->getRHS());
Zhongxing Xub10a7c22009-11-09 06:52:44 +000050
51 if (!RV.isConstant() || RV.isZeroConstant())
52 return;
53
Ted Kremenekd048c6e2010-12-20 21:19:09 +000054 if (ExplodedNode *N = C.generateNode()) {
Zhongxing Xub10a7c22009-11-09 06:52:44 +000055 if (!BT)
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000056 BT.reset(new BuiltinBug("Use fixed address",
Zhongxing Xudfed7a12009-11-09 07:29:39 +000057 "Using a fixed address is not portable because that "
58 "address will probably not be valid in all "
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000059 "environments or platforms."));
Benjamin Kramerd02e2322009-11-14 12:08:24 +000060 RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
Zhongxing Xub10a7c22009-11-09 06:52:44 +000061 R->addRange(B->getRHS()->getSourceRange());
62 C.EmitReport(R);
63 }
64}
65
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000066void ento::registerFixedAddressChecker(CheckerManager &mgr) {
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000067 mgr.registerChecker<FixedAddressChecker>();
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000068}