blob: 313fe7c43e0c505e7234928fa25ccd0554bece58 [file] [log] [blame]
Zhongxing Xuab0e27f2009-11-09 13:23:31 +00001//=== PointerArithChecker.cpp - Pointer arithmetic 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 PointerArithChecker, a builtin checker that checks for
11// pointer arithmetic on locations other than array elements.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisa9215282011-02-15 22:55:20 +000015#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xuab0e27f2009-11-09 13:23:31 +000020
21using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000022using namespace ento;
Zhongxing Xuab0e27f2009-11-09 13:23:31 +000023
24namespace {
Ted Kremenek3a0678e2015-09-08 03:50:52 +000025class PointerArithChecker
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000026 : public Checker< check::PreStmt<BinaryOperator> > {
Ahmed Charlesb8984322014-03-07 20:03:18 +000027 mutable std::unique_ptr<BuiltinBug> BT;
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000028
Zhongxing Xuab0e27f2009-11-09 13:23:31 +000029public:
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000030 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
Zhongxing Xuab0e27f2009-11-09 13:23:31 +000031};
32}
33
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000034void PointerArithChecker::checkPreStmt(const BinaryOperator *B,
35 CheckerContext &C) const {
John McCalle3027922010-08-25 11:45:40 +000036 if (B->getOpcode() != BO_Sub && B->getOpcode() != BO_Add)
Zhongxing Xuab0e27f2009-11-09 13:23:31 +000037 return;
38
Ted Kremenek49b1e382012-01-26 21:29:00 +000039 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-01-06 22:09:28 +000040 const LocationContext *LCtx = C.getLocationContext();
41 SVal LV = state->getSVal(B->getLHS(), LCtx);
42 SVal RV = state->getSVal(B->getRHS(), LCtx);
Zhongxing Xuab0e27f2009-11-09 13:23:31 +000043
44 const MemRegion *LR = LV.getAsRegion();
45
46 if (!LR || !RV.isConstant())
47 return;
48
49 // If pointer arithmetic is done on variables of non-array type, this often
50 // means behavior rely on memory organization, which is dangerous.
Ted Kremenek3a0678e2015-09-08 03:50:52 +000051 if (isa<VarRegion>(LR) || isa<CodeTextRegion>(LR) ||
Zhongxing Xuab0e27f2009-11-09 13:23:31 +000052 isa<CompoundLiteralRegion>(LR)) {
53
Anna Zaksda4c8d62011-10-26 21:06:34 +000054 if (ExplodedNode *N = C.addTransition()) {
Zhongxing Xuab0e27f2009-11-09 13:23:31 +000055 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000056 BT.reset(
57 new BuiltinBug(this, "Dangerous pointer arithmetic",
58 "Pointer arithmetic done on non-array variables "
59 "means reliance on memory layout, which is "
60 "dangerous."));
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000061 auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
Zhongxing Xuab0e27f2009-11-09 13:23:31 +000062 R->addRange(B->getSourceRange());
Aaron Ballman8d3a7a52015-06-23 13:15:32 +000063 C.emitReport(std::move(R));
Zhongxing Xuab0e27f2009-11-09 13:23:31 +000064 }
65 }
66}
67
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000068void ento::registerPointerArithChecker(CheckerManager &mgr) {
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000069 mgr.registerChecker<PointerArithChecker>();
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000070}