blob: 085a991f7866ce7d6fa770a81bf1c98aabd0fb76 [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000019#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.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> > {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000028 mutable OwningPtr<BuiltinBug> BT;
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000029
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
Ted Kremenek8bef8232012-01-26 21:29:00 +000047 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +000048 SVal RV = state->getSVal(B->getRHS(), C.getLocationContext());
Zhongxing Xub10a7c22009-11-09 06:52:44 +000049
50 if (!RV.isConstant() || RV.isZeroConstant())
51 return;
52
Anna Zaks0bd6b112011-10-26 21:06:34 +000053 if (ExplodedNode *N = C.addTransition()) {
Zhongxing Xub10a7c22009-11-09 06:52:44 +000054 if (!BT)
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000055 BT.reset(new BuiltinBug("Use fixed address",
Zhongxing Xudfed7a12009-11-09 07:29:39 +000056 "Using a fixed address is not portable because that "
57 "address will probably not be valid in all "
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000058 "environments or platforms."));
Anna Zakse172e8b2011-08-17 23:00:25 +000059 BugReport *R = new BugReport(*BT, BT->getDescription(), N);
Zhongxing Xub10a7c22009-11-09 06:52:44 +000060 R->addRange(B->getRHS()->getSourceRange());
Jordan Rose785950e2012-11-02 01:53:40 +000061 C.emitReport(R);
Zhongxing Xub10a7c22009-11-09 06:52:44 +000062 }
63}
64
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000065void ento::registerFixedAddressChecker(CheckerManager &mgr) {
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000066 mgr.registerChecker<FixedAddressChecker>();
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000067}