blob: fe4845bd889442546165225d251498be8aee3cbe [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 Kyrtzidis23ade502011-02-15 22:55:20 +000015#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000018#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Zhongxing Xuede7eb22009-11-09 13:23:31 +000020
21using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000022using namespace ento;
Zhongxing Xuede7eb22009-11-09 13:23:31 +000023
24namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000025class PointerArithChecker
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000026 : public Checker< check::PreStmt<BinaryOperator> > {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000027 mutable OwningPtr<BuiltinBug> BT;
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000028
Zhongxing Xuede7eb22009-11-09 13:23:31 +000029public:
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000030 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
Zhongxing Xuede7eb22009-11-09 13:23:31 +000031};
32}
33
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000034void PointerArithChecker::checkPreStmt(const BinaryOperator *B,
35 CheckerContext &C) const {
John McCall2de56d12010-08-25 11:45:40 +000036 if (B->getOpcode() != BO_Sub && B->getOpcode() != BO_Add)
Zhongxing Xuede7eb22009-11-09 13:23:31 +000037 return;
38
Ted Kremenek8bef8232012-01-26 21:29:00 +000039 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-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 Xuede7eb22009-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.
51 if (isa<VarRegion>(LR) || isa<CodeTextRegion>(LR) ||
52 isa<CompoundLiteralRegion>(LR)) {
53
Anna Zaks0bd6b112011-10-26 21:06:34 +000054 if (ExplodedNode *N = C.addTransition()) {
Zhongxing Xuede7eb22009-11-09 13:23:31 +000055 if (!BT)
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000056 BT.reset(new BuiltinBug("Dangerous pointer arithmetic",
Zhongxing Xuede7eb22009-11-09 13:23:31 +000057 "Pointer arithmetic done on non-array variables "
58 "means reliance on memory layout, which is "
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000059 "dangerous."));
Anna Zakse172e8b2011-08-17 23:00:25 +000060 BugReport *R = new BugReport(*BT, BT->getDescription(), N);
Zhongxing Xuede7eb22009-11-09 13:23:31 +000061 R->addRange(B->getSourceRange());
62 C.EmitReport(R);
63 }
64 }
65}
66
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000067void ento::registerPointerArithChecker(CheckerManager &mgr) {
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000068 mgr.registerChecker<PointerArithChecker>();
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000069}