Skip to content

Commit a0c0dc1

Browse files
eupharinaresistor
authored andcommitted
[CHERI_CSA] CHERI API Modelling
1 parent 890c9d5 commit a0c0dc1

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

clang/include/clang/StaticAnalyzer/Checkers/Checkers.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,10 @@ def UncheckedLocalVarsChecker : Checker<"UncheckedLocalVarsChecker">,
17941794

17951795
let ParentPackage = CHERI in {
17961796

1797+
def CheriAPIModelling : Checker<"CheriAPIModelling">,
1798+
HelpText<"Model CheriAPI">,
1799+
Documentation<NotDocumented>;
1800+
17971801
def ProvenanceSourceChecker : Checker<"ProvenanceSource">,
17981802
HelpText<"Check expressions with ambiguous provenance source.">,
17991803
CheckerOptions<[
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}

clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ add_clang_library(clangStaticAnalyzerCheckers
2727
CheckerDocumentation.cpp
2828
CHERI/AllocationChecker.cpp
2929
CHERI/CapabilityCopyChecker.cpp
30+
CHERI/CheriAPIModelling.cpp
3031
CHERI/CHERIUtils.cpp
3132
CHERI/PointerSizeAssumptionsChecker.cpp
3233
CHERI/ProvenanceSourceChecker.cpp

0 commit comments

Comments
 (0)