blob: 8d98ddb59cdc71e2455c97e29e419f46220b33b4 [file] [log] [blame]
Zhongxing Xu86b1e012009-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 Kyrtzidisa9215282011-02-15 22:55:20 +000016#include "ClangSACheckers.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"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Zhongxing Xu86b1e012009-11-09 05:34:10 +000021
22using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000023using namespace ento;
Zhongxing Xu86b1e012009-11-09 05:34:10 +000024
25namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000026class PointerSubChecker
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000027 : public Checker< check::PreStmt<BinaryOperator> > {
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000028 mutable llvm::OwningPtr<BuiltinBug> BT;
29
Zhongxing Xu86b1e012009-11-09 05:34:10 +000030public:
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000031 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
Zhongxing Xu86b1e012009-11-09 05:34:10 +000032};
33}
34
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000035void PointerSubChecker::checkPreStmt(const BinaryOperator *B,
36 CheckerContext &C) const {
Zhongxing Xu86b1e012009-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 McCalle3027922010-08-25 11:45:40 +000039 if (B->getOpcode() != BO_Sub)
Zhongxing Xu86b1e012009-11-09 05:34:10 +000040 return;
41
Ted Kremenek001fd5b2011-08-15 22:09:50 +000042 const ProgramState *state = C.getState();
Ted Kremenek57f09892010-02-08 16:18:51 +000043 SVal LV = state->getSVal(B->getLHS());
44 SVal RV = state->getSVal(B->getRHS());
Zhongxing Xu86b1e012009-11-09 05:34:10 +000045
46 const MemRegion *LR = LV.getAsRegion();
47 const MemRegion *RR = RV.getAsRegion();
48
Zhongxing Xu80bbc6d2009-11-10 02:37:53 +000049 if (!(LR && RR))
Zhongxing Xu86b1e012009-11-09 05:34:10 +000050 return;
51
Zhongxing Xu80bbc6d2009-11-10 02:37:53 +000052 const MemRegion *BaseLR = LR->getBaseRegion();
53 const MemRegion *BaseRR = RR->getBaseRegion();
54
55 if (BaseLR == BaseRR)
56 return;
57
58 // Allow arithmetic on different symbolic regions.
59 if (isa<SymbolicRegion>(BaseLR) || isa<SymbolicRegion>(BaseRR))
Zhongxing Xu86b1e012009-11-09 05:34:10 +000060 return;
61
Ted Kremenek750b7ac2010-12-20 21:19:09 +000062 if (ExplodedNode *N = C.generateNode()) {
Zhongxing Xu86b1e012009-11-09 05:34:10 +000063 if (!BT)
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000064 BT.reset(new BuiltinBug("Pointer subtraction",
Zhongxing Xu85000202009-11-09 07:29:39 +000065 "Subtraction of two pointers that do not point to "
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000066 "the same memory chunk may cause incorrect result."));
Benjamin Kramerf4c511b2009-11-14 12:08:24 +000067 RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
Zhongxing Xu86b1e012009-11-09 05:34:10 +000068 R->addRange(B->getSourceRange());
69 C.EmitReport(R);
70 }
71}
72
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000073void ento::registerPointerSubChecker(CheckerManager &mgr) {
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000074 mgr.registerChecker<PointerSubChecker>();
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000075}