blob: 2b03dbcd9c246b1172fa42e53163408844a3338f [file] [log] [blame]
Zhongxing Xuede7eb22009-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 Kyrtzidisd2592a32010-12-22 18:53:44 +000015#include "ExprEngineInternalChecks.h"
Ted Kremenek21142582010-12-23 19:38:26 +000016#include "clang/StaticAnalyzer/BugReporter/BugType.h"
17#include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h"
Zhongxing Xuede7eb22009-11-09 13:23:31 +000018
19using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000020using namespace ento;
Zhongxing Xuede7eb22009-11-09 13:23:31 +000021
22namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000023class PointerArithChecker
Zhongxing Xuede7eb22009-11-09 13:23:31 +000024 : public CheckerVisitor<PointerArithChecker> {
25 BuiltinBug *BT;
26public:
27 PointerArithChecker() : BT(0) {}
28 static void *getTag();
29 void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
30};
31}
32
33void *PointerArithChecker::getTag() {
34 static int x;
35 return &x;
36}
37
38void PointerArithChecker::PreVisitBinaryOperator(CheckerContext &C,
39 const BinaryOperator *B) {
John McCall2de56d12010-08-25 11:45:40 +000040 if (B->getOpcode() != BO_Sub && B->getOpcode() != BO_Add)
Zhongxing Xuede7eb22009-11-09 13:23:31 +000041 return;
42
43 const GRState *state = C.getState();
Ted Kremenek13976632010-02-08 16:18:51 +000044 SVal LV = state->getSVal(B->getLHS());
45 SVal RV = state->getSVal(B->getRHS());
Zhongxing Xuede7eb22009-11-09 13:23:31 +000046
47 const MemRegion *LR = LV.getAsRegion();
48
49 if (!LR || !RV.isConstant())
50 return;
51
52 // If pointer arithmetic is done on variables of non-array type, this often
53 // means behavior rely on memory organization, which is dangerous.
54 if (isa<VarRegion>(LR) || isa<CodeTextRegion>(LR) ||
55 isa<CompoundLiteralRegion>(LR)) {
56
Ted Kremenekd048c6e2010-12-20 21:19:09 +000057 if (ExplodedNode *N = C.generateNode()) {
Zhongxing Xuede7eb22009-11-09 13:23:31 +000058 if (!BT)
59 BT = new BuiltinBug("Dangerous pointer arithmetic",
60 "Pointer arithmetic done on non-array variables "
61 "means reliance on memory layout, which is "
62 "dangerous.");
Benjamin Kramerd02e2322009-11-14 12:08:24 +000063 RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
Zhongxing Xuede7eb22009-11-09 13:23:31 +000064 R->addRange(B->getSourceRange());
65 C.EmitReport(R);
66 }
67 }
68}
69
Ted Kremenek9ef65372010-12-23 07:20:52 +000070void ento::RegisterPointerArithChecker(ExprEngine &Eng) {
Zhongxing Xuede7eb22009-11-09 13:23:31 +000071 Eng.registerCheck(new PointerArithChecker());
72}