1e5dd7070Spatrick //=== PointerSubChecker.cpp - Pointer subtraction checker ------*- C++ -*--===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This files defines PointerSubChecker, a builtin checker that checks for
10e5dd7070Spatrick // pointer subtractions on two pointers pointing to different memory chunks.
11e5dd7070Spatrick // This check corresponds to CWE-469.
12e5dd7070Spatrick //
13e5dd7070Spatrick //===----------------------------------------------------------------------===//
14e5dd7070Spatrick
15e5dd7070Spatrick #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
16e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
17e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/Checker.h"
18e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20e5dd7070Spatrick
21e5dd7070Spatrick using namespace clang;
22e5dd7070Spatrick using namespace ento;
23e5dd7070Spatrick
24e5dd7070Spatrick namespace {
25e5dd7070Spatrick class PointerSubChecker
26e5dd7070Spatrick : public Checker< check::PreStmt<BinaryOperator> > {
27e5dd7070Spatrick mutable std::unique_ptr<BuiltinBug> BT;
28e5dd7070Spatrick
29e5dd7070Spatrick public:
30e5dd7070Spatrick void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
31e5dd7070Spatrick };
32e5dd7070Spatrick }
33e5dd7070Spatrick
checkPreStmt(const BinaryOperator * B,CheckerContext & C) const34e5dd7070Spatrick void PointerSubChecker::checkPreStmt(const BinaryOperator *B,
35e5dd7070Spatrick CheckerContext &C) const {
36e5dd7070Spatrick // When doing pointer subtraction, if the two pointers do not point to the
37e5dd7070Spatrick // same memory chunk, emit a warning.
38e5dd7070Spatrick if (B->getOpcode() != BO_Sub)
39e5dd7070Spatrick return;
40e5dd7070Spatrick
41e5dd7070Spatrick SVal LV = C.getSVal(B->getLHS());
42e5dd7070Spatrick SVal RV = C.getSVal(B->getRHS());
43e5dd7070Spatrick
44e5dd7070Spatrick const MemRegion *LR = LV.getAsRegion();
45e5dd7070Spatrick const MemRegion *RR = RV.getAsRegion();
46e5dd7070Spatrick
47e5dd7070Spatrick if (!(LR && RR))
48e5dd7070Spatrick return;
49e5dd7070Spatrick
50e5dd7070Spatrick const MemRegion *BaseLR = LR->getBaseRegion();
51e5dd7070Spatrick const MemRegion *BaseRR = RR->getBaseRegion();
52e5dd7070Spatrick
53e5dd7070Spatrick if (BaseLR == BaseRR)
54e5dd7070Spatrick return;
55e5dd7070Spatrick
56e5dd7070Spatrick // Allow arithmetic on different symbolic regions.
57e5dd7070Spatrick if (isa<SymbolicRegion>(BaseLR) || isa<SymbolicRegion>(BaseRR))
58e5dd7070Spatrick return;
59e5dd7070Spatrick
60e5dd7070Spatrick if (ExplodedNode *N = C.generateNonFatalErrorNode()) {
61e5dd7070Spatrick if (!BT)
62e5dd7070Spatrick BT.reset(
63e5dd7070Spatrick new BuiltinBug(this, "Pointer subtraction",
64e5dd7070Spatrick "Subtraction of two pointers that do not point to "
65e5dd7070Spatrick "the same memory chunk may cause incorrect result."));
66e5dd7070Spatrick auto R =
67e5dd7070Spatrick std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N);
68e5dd7070Spatrick R->addRange(B->getSourceRange());
69e5dd7070Spatrick C.emitReport(std::move(R));
70e5dd7070Spatrick }
71e5dd7070Spatrick }
72e5dd7070Spatrick
registerPointerSubChecker(CheckerManager & mgr)73e5dd7070Spatrick void ento::registerPointerSubChecker(CheckerManager &mgr) {
74e5dd7070Spatrick mgr.registerChecker<PointerSubChecker>();
75e5dd7070Spatrick }
76e5dd7070Spatrick
shouldRegisterPointerSubChecker(const CheckerManager & mgr)77*ec727ea7Spatrick bool ento::shouldRegisterPointerSubChecker(const CheckerManager &mgr) {
78e5dd7070Spatrick return true;
79e5dd7070Spatrick }
80