12754fe60SDimitry Andric //=== PointerSubChecker.cpp - Pointer subtraction checker ------*- C++ -*--===//
22754fe60SDimitry Andric //
32754fe60SDimitry Andric //                     The LLVM Compiler Infrastructure
42754fe60SDimitry Andric //
52754fe60SDimitry Andric // This file is distributed under the University of Illinois Open Source
62754fe60SDimitry Andric // License. See LICENSE.TXT for details.
72754fe60SDimitry Andric //
82754fe60SDimitry Andric //===----------------------------------------------------------------------===//
92754fe60SDimitry Andric //
102754fe60SDimitry Andric // This files defines PointerSubChecker, a builtin checker that checks for
112754fe60SDimitry Andric // pointer subtractions on two pointers pointing to different memory chunks.
122754fe60SDimitry Andric // This check corresponds to CWE-469.
132754fe60SDimitry Andric //
142754fe60SDimitry Andric //===----------------------------------------------------------------------===//
152754fe60SDimitry Andric 
16*b5893f02SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
17139f7f9bSDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
183b0f4066SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
192754fe60SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20dd6029ffSDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
212754fe60SDimitry Andric 
222754fe60SDimitry Andric using namespace clang;
232754fe60SDimitry Andric using namespace ento;
242754fe60SDimitry Andric 
252754fe60SDimitry Andric namespace {
262754fe60SDimitry Andric class PointerSubChecker
273b0f4066SDimitry Andric   : public Checker< check::PreStmt<BinaryOperator> > {
2859d1ed5bSDimitry Andric   mutable std::unique_ptr<BuiltinBug> BT;
29dd6029ffSDimitry Andric 
302754fe60SDimitry Andric public:
31dd6029ffSDimitry Andric   void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
322754fe60SDimitry Andric };
332754fe60SDimitry Andric }
342754fe60SDimitry Andric 
checkPreStmt(const BinaryOperator * B,CheckerContext & C) const35dd6029ffSDimitry Andric void PointerSubChecker::checkPreStmt(const BinaryOperator *B,
36dd6029ffSDimitry Andric                                      CheckerContext &C) const {
372754fe60SDimitry Andric   // When doing pointer subtraction, if the two pointers do not point to the
382754fe60SDimitry Andric   // same memory chunk, emit a warning.
392754fe60SDimitry Andric   if (B->getOpcode() != BO_Sub)
402754fe60SDimitry Andric     return;
412754fe60SDimitry Andric 
424ba319b5SDimitry Andric   SVal LV = C.getSVal(B->getLHS());
434ba319b5SDimitry Andric   SVal RV = C.getSVal(B->getRHS());
442754fe60SDimitry Andric 
452754fe60SDimitry Andric   const MemRegion *LR = LV.getAsRegion();
462754fe60SDimitry Andric   const MemRegion *RR = RV.getAsRegion();
472754fe60SDimitry Andric 
482754fe60SDimitry Andric   if (!(LR && RR))
492754fe60SDimitry Andric     return;
502754fe60SDimitry Andric 
512754fe60SDimitry Andric   const MemRegion *BaseLR = LR->getBaseRegion();
522754fe60SDimitry Andric   const MemRegion *BaseRR = RR->getBaseRegion();
532754fe60SDimitry Andric 
542754fe60SDimitry Andric   if (BaseLR == BaseRR)
552754fe60SDimitry Andric     return;
562754fe60SDimitry Andric 
572754fe60SDimitry Andric   // Allow arithmetic on different symbolic regions.
582754fe60SDimitry Andric   if (isa<SymbolicRegion>(BaseLR) || isa<SymbolicRegion>(BaseRR))
592754fe60SDimitry Andric     return;
602754fe60SDimitry Andric 
610623d748SDimitry Andric   if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
622754fe60SDimitry Andric     if (!BT)
6359d1ed5bSDimitry Andric       BT.reset(
6459d1ed5bSDimitry Andric           new BuiltinBug(this, "Pointer subtraction",
652754fe60SDimitry Andric                          "Subtraction of two pointers that do not point to "
66dd6029ffSDimitry Andric                          "the same memory chunk may cause incorrect result."));
673dac3a9bSDimitry Andric     auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
682754fe60SDimitry Andric     R->addRange(B->getSourceRange());
693dac3a9bSDimitry Andric     C.emitReport(std::move(R));
702754fe60SDimitry Andric   }
712754fe60SDimitry Andric }
722754fe60SDimitry Andric 
registerPointerSubChecker(CheckerManager & mgr)732754fe60SDimitry Andric void ento::registerPointerSubChecker(CheckerManager &mgr) {
74dd6029ffSDimitry Andric   mgr.registerChecker<PointerSubChecker>();
752754fe60SDimitry Andric }
76