blob: 059203fca730eedbab10e9b002890a8e79964a2d [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 {
Ted Kremenek3a0678e2015-09-08 03:50:52 +000026class FixedAddressChecker
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000027 : public Checker< check::PreStmt<BinaryOperator> > {
Ahmed Charlesb8984322014-03-07 20:03:18 +000028 mutable std::unique_ptr<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
George Karpenkovd703ec92018-01-17 20:27:29 +000047 SVal RV = C.getSVal(B->getRHS());
Zhongxing Xu6c306c82009-11-09 06:52:44 +000048
49 if (!RV.isConstant() || RV.isZeroConstant())
50 return;
51
Devin Coughline39bd402015-09-16 22:03:05 +000052 if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
Zhongxing Xu6c306c82009-11-09 06:52:44 +000053 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000054 BT.reset(
55 new BuiltinBug(this, "Use fixed address",
56 "Using a fixed address is not portable because that "
57 "address will probably not be valid in all "
58 "environments or platforms."));
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000059 auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
Zhongxing Xu6c306c82009-11-09 06:52:44 +000060 R->addRange(B->getRHS()->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000061 C.emitReport(std::move(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}