Skip to content

Commit 49af6ef

Browse files
ronortondavidchisnall
authored andcommitted
Add a variant of compartment call benchmark that actually uses some stack.
This is to check the effect of the stack low water mark. Also tests to make sure the stack is being properly cleared.
1 parent 0b12e8b commit 49af6ef

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

benchmarks/stack-usage/callee.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "callee.h"
2+
#include "../timing.h"
3+
4+
int noop_return(size_t s)
5+
{
6+
use_stack(s);
7+
return rdcycle();
8+
}
9+
10+
int noop_call(int start)
11+
{
12+
int end = rdcycle();
13+
check_stack_zeroed();
14+
return end - start;
15+
}

benchmarks/stack-usage/callee.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <compartment.h>
2+
3+
int __cheri_compartment("callee") noop_return(size_t s);
4+
int __cheri_compartment("callee") noop_call(int start);

benchmarks/stack-usage/caller.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "../timing.h"
2+
#include "callee.h"
3+
#include <compartment.h>
4+
#include <locks.hh>
5+
6+
void __cheri_compartment("caller") run()
7+
{
8+
MessageBuilder<ImplicitUARTOutput> out;
9+
out.format("#board\tstack_use\tcall\treturn\n");
10+
for (size_t s = 1; s < 0x1000; s <<= 1)
11+
{
12+
auto [callPath, returnPath] = CHERI::with_interrupts_disabled([&]() {
13+
use_stack(s);
14+
auto callPath = noop_call(rdcycle());
15+
auto returnPath = noop_return(s);
16+
returnPath = rdcycle() - returnPath;
17+
check_stack_zeroed();
18+
return std::tuple{callPath, returnPath};
19+
});
20+
out.format(__XSTRING(BOARD) "\t{}\t{}\t{}\n", s, callPath, returnPath);
21+
}
22+
}

benchmarks/stack-usage/xmake.lua

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
-- Copyright Microsoft and CHERIoT Contributors.
2+
-- SPDX-License-Identifier: MIT
3+
4+
set_project("CHERIoT stack-usage benchmark");
5+
sdkdir = "../../sdk"
6+
includes(sdkdir)
7+
set_toolchains("cheriot-clang")
8+
9+
-- Support libraries
10+
includes(path.join(sdkdir, "lib/freestanding"),
11+
path.join(sdkdir, "lib/atomic"),
12+
path.join(sdkdir, "lib/crt"))
13+
14+
option("board")
15+
set_default("sail")
16+
17+
compartment("callee")
18+
add_files("callee.cc")
19+
20+
compartment("caller")
21+
add_defines("BOARD=" .. tostring(get_config("board")))
22+
add_files("caller.cc")
23+
24+
-- Firmware image for the example.
25+
firmware("stack-usage-benchmark")
26+
add_deps("crt", "freestanding", "atomic")
27+
add_deps("caller", "callee")
28+
on_load(function(target)
29+
target:values_set("board", "$(board)")
30+
target:values_set("threads", {
31+
{
32+
compartment = "caller",
33+
priority = 1,
34+
entry_point = "run",
35+
stack_size = 0x1000,
36+
trusted_stack_frames = 3
37+
}
38+
}, {expand = false})
39+
end)

benchmarks/timing.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,28 @@ namespace
3636
CHERI::Capability stack{cspRegister};
3737
return stack.length();
3838
}
39+
40+
void use_stack(size_t s)
41+
{
42+
volatile uint8_t stack_array[s];
43+
stack_array[0] = 1;
44+
}
45+
46+
void check_stack_zeroed()
47+
{
48+
register void **cspRegister asm("csp");
49+
asm("" : "=C"(cspRegister));
50+
CHERI::Capability stack{cspRegister};
51+
CHERI::Capability<void*> stackP{stack};
52+
stackP.address() = stack.base();
53+
while(stackP.address() < stack.address())
54+
{
55+
CHERI::Capability<void> value{*stackP};
56+
if (value != NULL)
57+
{
58+
__builtin_trap();
59+
}
60+
stackP += 1;
61+
}
62+
}
3963
} // namespace

0 commit comments

Comments
 (0)