blob: 085a991f7866ce7d6fa770a81bf1c98aabd0fb76 [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"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000019#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xu6c306c82009-11-09 06:52:44 +000021
22using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000023using namespace ento;
Zhongxing Xu6c306c82009-11-09 06:52:44 +000024
25namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000026class FixedAddressChecker
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000027 : public Checker< check::PreStmt<BinaryOperator> > {
Dylan Noblesmithe2778992012-02-05 02:12:40 +000028 mutable OwningPtr<BuiltinBug> BT;
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000029
Zhongxing Xu6c306c82009-11-09 06:52:44 +000030public:
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000031 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
Zhongxing Xu6c306c82009-11-09 06:52:44 +000032};
33}
34
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000035void FixedAddressChecker::checkPreStmt(const BinaryOperator *B,
36 CheckerContext &C) const {
Zhongxing Xu6c306c82009-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 McCalle3027922010-08-25 11:45:40 +000040 if (B->getOpcode() != BO_Assign)
Zhongxing Xu6c306c82009-11-09 06:52:44 +000041 return;
42
43 QualType T = B->getType();
44 if (!T->isPointerType())
45 return;
46
Ted Kremenek49b1e382012-01-26 21:29:00 +000047 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +000048 SVal RV = state->getSVal(B->getRHS(), C.getLocationContext());
Zhongxing Xu6c306c82009-11-09 06:52:44 +000049
50 if (!RV.isConstant() || RV.isZeroConstant())
51 return;
52
Anna Zaksda4c8d62011-10-26 21:06:34 +000053 if (ExplodedNode *N = C.addTransition()) {
Zhongxing Xu6c306c82009-11-09 06:52:44 +000054 if (!BT)
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000055 BT.reset(new BuiltinBug("Use fixed address",
Zhongxing Xu85000202009-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 Kyrtzidisdff865d2011-02-23 01:05:36 +000058 "environments or platforms."));
Anna Zaks3a6bdf82011-08-17 23:00:25 +000059 BugReport *R = new BugReport(*BT, BT->getDescription(), N);
Zhongxing Xu6c306c82009-11-09 06:52:44 +000060 R->addRange(B->getRHS()->getSourceRange());
Jordan Rosee10d5a72012-11-02 01:53:40 +000061 C.emitReport(R);
Zhongxing Xu6c306c82009-11-09 06:52:44 +000062 }
63}
64
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000065void ento::registerFixedAddressChecker(CheckerManager &mgr) {
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000066 mgr.registerChecker<FixedAddressChecker>();
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000067}