blob: f28fe9a5379e3b51c3f7e8b9dec00d636ecea99a [file] [log] [blame]
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +00001//=== PointerSubChecker.cpp - Pointer subtraction 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 PointerSubChecker, a builtin checker that checks for
11// pointer subtractions on two pointers pointing to different memory chunks.
12// This check corresponds to CWE-469.
13//
14//===----------------------------------------------------------------------===//
15
Argyrios Kyrtzidis23ade502011-02-15 22:55:20 +000016#include "ClangSACheckers.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
19#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerVisitor.h"
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000020
21using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000022using namespace ento;
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000023
24namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000025class PointerSubChecker
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000026 : public CheckerVisitor<PointerSubChecker> {
27 BuiltinBug *BT;
28public:
29 PointerSubChecker() : BT(0) {}
30 static void *getTag();
31 void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
32};
33}
34
35void *PointerSubChecker::getTag() {
36 static int x;
37 return &x;
38}
39
40void PointerSubChecker::PreVisitBinaryOperator(CheckerContext &C,
41 const BinaryOperator *B) {
42 // When doing pointer subtraction, if the two pointers do not point to the
43 // same memory chunk, emit a warning.
John McCall2de56d12010-08-25 11:45:40 +000044 if (B->getOpcode() != BO_Sub)
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000045 return;
46
47 const GRState *state = C.getState();
Ted Kremenek13976632010-02-08 16:18:51 +000048 SVal LV = state->getSVal(B->getLHS());
49 SVal RV = state->getSVal(B->getRHS());
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000050
51 const MemRegion *LR = LV.getAsRegion();
52 const MemRegion *RR = RV.getAsRegion();
53
Zhongxing Xuadca2712009-11-10 02:37:53 +000054 if (!(LR && RR))
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000055 return;
56
Zhongxing Xuadca2712009-11-10 02:37:53 +000057 const MemRegion *BaseLR = LR->getBaseRegion();
58 const MemRegion *BaseRR = RR->getBaseRegion();
59
60 if (BaseLR == BaseRR)
61 return;
62
63 // Allow arithmetic on different symbolic regions.
64 if (isa<SymbolicRegion>(BaseLR) || isa<SymbolicRegion>(BaseRR))
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000065 return;
66
Ted Kremenekd048c6e2010-12-20 21:19:09 +000067 if (ExplodedNode *N = C.generateNode()) {
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000068 if (!BT)
69 BT = new BuiltinBug("Pointer subtraction",
Zhongxing Xudfed7a12009-11-09 07:29:39 +000070 "Subtraction of two pointers that do not point to "
71 "the same memory chunk may cause incorrect result.");
Benjamin Kramerd02e2322009-11-14 12:08:24 +000072 RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000073 R->addRange(B->getSourceRange());
74 C.EmitReport(R);
75 }
76}
77
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000078static void RegisterPointerSubChecker(ExprEngine &Eng) {
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000079 Eng.registerCheck(new PointerSubChecker());
80}
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000081
82void ento::registerPointerSubChecker(CheckerManager &mgr) {
83 mgr.addCheckerRegisterFunction(RegisterPointerSubChecker);
84}