blob: 51f57a20f3ebab86f32fa23503069b41ffffe780 [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 Kyrtzidisec8605f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000018#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000021
22using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000023using namespace ento;
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000024
25namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000026class PointerSubChecker
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000027 : public Checker< check::PreStmt<BinaryOperator> > {
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000028 mutable llvm::OwningPtr<BuiltinBug> BT;
29
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000030public:
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000031 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000032};
33}
34
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000035void PointerSubChecker::checkPreStmt(const BinaryOperator *B,
36 CheckerContext &C) const {
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000037 // When doing pointer subtraction, if the two pointers do not point to the
38 // same memory chunk, emit a warning.
John McCall2de56d12010-08-25 11:45:40 +000039 if (B->getOpcode() != BO_Sub)
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000040 return;
41
Ted Kremenek18c66fd2011-08-15 22:09:50 +000042 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +000043 const LocationContext *LCtx = C.getLocationContext();
44 SVal LV = state->getSVal(B->getLHS(), LCtx);
45 SVal RV = state->getSVal(B->getRHS(), LCtx);
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000046
47 const MemRegion *LR = LV.getAsRegion();
48 const MemRegion *RR = RV.getAsRegion();
49
Zhongxing Xuadca2712009-11-10 02:37:53 +000050 if (!(LR && RR))
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000051 return;
52
Zhongxing Xuadca2712009-11-10 02:37:53 +000053 const MemRegion *BaseLR = LR->getBaseRegion();
54 const MemRegion *BaseRR = RR->getBaseRegion();
55
56 if (BaseLR == BaseRR)
57 return;
58
59 // Allow arithmetic on different symbolic regions.
60 if (isa<SymbolicRegion>(BaseLR) || isa<SymbolicRegion>(BaseRR))
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000061 return;
62
Anna Zaks0bd6b112011-10-26 21:06:34 +000063 if (ExplodedNode *N = C.addTransition()) {
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000064 if (!BT)
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000065 BT.reset(new BuiltinBug("Pointer subtraction",
Zhongxing Xudfed7a12009-11-09 07:29:39 +000066 "Subtraction of two pointers that do not point to "
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000067 "the same memory chunk may cause incorrect result."));
Anna Zakse172e8b2011-08-17 23:00:25 +000068 BugReport *R = new BugReport(*BT, BT->getDescription(), N);
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000069 R->addRange(B->getSourceRange());
70 C.EmitReport(R);
71 }
72}
73
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000074void ento::registerPointerSubChecker(CheckerManager &mgr) {
Argyrios Kyrtzidis983326f2011-02-23 01:05:36 +000075 mgr.registerChecker<PointerSubChecker>();
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000076}