blob: 47da87f0bcc62299f15257cecac355329d976f24 [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> > {
Dylan Noblesmithe2778992012-02-05 02:12:40 +000028 mutable OwningPtr<BuiltinBug> BT;
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000029
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 Kremenek49b1e382012-01-26 21:29:00 +000042 ProgramStateRef state = C.getState();
Ted Kremenek632e3b72012-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 Xu86b1e012009-11-09 05:34:10 +000046
47 const MemRegion *LR = LV.getAsRegion();
48 const MemRegion *RR = RV.getAsRegion();
49
Zhongxing Xu80bbc6d2009-11-10 02:37:53 +000050 if (!(LR && RR))
Zhongxing Xu86b1e012009-11-09 05:34:10 +000051 return;
52
Zhongxing Xu80bbc6d2009-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 Xu86b1e012009-11-09 05:34:10 +000061 return;
62
Anna Zaksda4c8d62011-10-26 21:06:34 +000063 if (ExplodedNode *N = C.addTransition()) {
Zhongxing Xu86b1e012009-11-09 05:34:10 +000064 if (!BT)
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000065 BT.reset(new BuiltinBug("Pointer subtraction",
Zhongxing Xu85000202009-11-09 07:29:39 +000066 "Subtraction of two pointers that do not point to "
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000067 "the same memory chunk may cause incorrect result."));
Anna Zaks3a6bdf82011-08-17 23:00:25 +000068 BugReport *R = new BugReport(*BT, BT->getDescription(), N);
Zhongxing Xu86b1e012009-11-09 05:34:10 +000069 R->addRange(B->getSourceRange());
Jordan Rosee10d5a72012-11-02 01:53:40 +000070 C.emitReport(R);
Zhongxing Xu86b1e012009-11-09 05:34:10 +000071 }
72}
73
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000074void ento::registerPointerSubChecker(CheckerManager &mgr) {
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000075 mgr.registerChecker<PointerSubChecker>();
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000076}