|
| 1 | +//===-- AllocationChecker.cpp - Allocation Checker -*- C++ -*--------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// Model CHERI API |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| 14 | +#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 15 | +#include "clang/StaticAnalyzer/Core/Checker.h" |
| 16 | +#include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 17 | +#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
| 18 | +#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 19 | +#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
| 20 | +#include <clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h> |
| 21 | + |
| 22 | + |
| 23 | +using namespace clang; |
| 24 | +using namespace ento; |
| 25 | + |
| 26 | + |
| 27 | +//namespace { |
| 28 | + |
| 29 | +class CheriAPIModelling : public Checker<eval::Call> { |
| 30 | +public: |
| 31 | + bool evalCall(const CallEvent &Call, CheckerContext &C) const; |
| 32 | + |
| 33 | + |
| 34 | + typedef void (CheriAPIModelling::*FnCheck)(CheckerContext &, |
| 35 | + const CallExpr *) const; |
| 36 | + CallDescriptionMap<FnCheck> Callbacks = { |
| 37 | + {{CDM::SimpleFunc, {"cheri_address_set"}, 2}, |
| 38 | + &CheriAPIModelling::evalAddrSet}, |
| 39 | + {{CDM::SimpleFunc, {"cheri_bounds_set"}, 2}, |
| 40 | + &CheriAPIModelling::evalBoundsSet}, |
| 41 | + {{CDM::SimpleFunc, {"cheri_bounds_set_exact"}, 2}, |
| 42 | + &CheriAPIModelling::evalBoundsSet}, |
| 43 | + {{CDM::SimpleFunc, {"cheri_perms_and"}, 2}, |
| 44 | + &CheriAPIModelling::evalBoundsSet}, |
| 45 | + {{CDM::SimpleFunc, {"cheri_tag_clear"}, 1}, |
| 46 | + &CheriAPIModelling::evalBoundsSet} |
| 47 | + |
| 48 | + }; |
| 49 | + |
| 50 | + void evalBoundsSet(CheckerContext &C, const CallExpr *CE) const; |
| 51 | + void evalAddrSet(CheckerContext &C, const CallExpr *CE) const; |
| 52 | +}; |
| 53 | + |
| 54 | +//} // namespace |
| 55 | + |
| 56 | +void CheriAPIModelling::evalBoundsSet(CheckerContext &C, |
| 57 | + const CallExpr *CE) const { |
| 58 | + auto State = C.getState(); |
| 59 | + SVal Cap = C.getSVal(CE->getArg(0)); |
| 60 | + State = State->BindExpr(CE, C.getLocationContext(), Cap); |
| 61 | + C.addTransition(State); |
| 62 | +} |
| 63 | + |
| 64 | +void CheriAPIModelling::evalAddrSet(CheckerContext &C, |
| 65 | + const CallExpr *CE) const { |
| 66 | + auto State = C.getState(); |
| 67 | + SVal Addr = C.getSVal(CE->getArg(1)); |
| 68 | + State = State->BindExpr(CE, C.getLocationContext(), Addr); |
| 69 | + C.addTransition(State); |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +bool CheriAPIModelling::evalCall(const CallEvent &Call, |
| 75 | + CheckerContext &C) const { |
| 76 | + const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr()); |
| 77 | + if (!CE) |
| 78 | + return false; |
| 79 | + |
| 80 | + const FnCheck *Handler = Callbacks.lookup(Call); |
| 81 | + if (!Handler) |
| 82 | + return false; |
| 83 | + |
| 84 | + (this->**Handler)(C, CE); |
| 85 | + return true; |
| 86 | +} |
| 87 | + |
| 88 | +//===----------------------------------------------------------------------===// |
| 89 | +// Checker registration. |
| 90 | +//===----------------------------------------------------------------------===// |
| 91 | + |
| 92 | +void clang::ento::registerCheriAPIModelling(CheckerManager &Mgr) { |
| 93 | + Mgr.registerChecker<CheriAPIModelling>(); |
| 94 | +} |
| 95 | + |
| 96 | +bool clang::ento::shouldRegisterCheriAPIModelling(const CheckerManager &Mgr) { |
| 97 | + return true; |
| 98 | +} |
0 commit comments