sample C cheri program hybrid cap

This commit is contained in:
2026-03-19 12:26:29 +00:00
parent 6dcad25669
commit 34e84f2926
6 changed files with 86 additions and 35 deletions

View File

@@ -0,0 +1,18 @@
#include <cheriintrin.h>
#include <stdint.h>
int main(void) {
void *__capability csp;
// Set address
csp = (void *__capability)0x80001000;
// Set bounds
csp = cheri_bounds_set(csp, 8);
// Increment offset
// csp = cheri_offset_increment(csp, 10);
uint64_t val = *(uint64_t *__capability)csp;
return 0;
}

View File

@@ -0,0 +1,40 @@
.section .text
.globl _start
.align 4
_start:
# cspecialr c1, pcc
# Use csetaddr to build a stack capability
# li t0, 0x80001000 # fixed top-of-stack address
# csetaddr csp, ct0, t0
# bound it to 8 bytes (size of dword)
# li t2, 8
# csetbounds csp, csp, t2
# li t5, 4
# cincoffsetimm csp, csp, 10
# Read bounds
# cgetbase t3, csp
# ld t0, 0(csp)
# derive capability from PCC
# csetaddr c1, ct0, t0
cspecialr ca0, pcc # Get the root data capability
li t0, 0x80002000 # Set top of stack address
csetaddr csp, ca0, t0 # Set address
li t1, -2048 # 2KB size
csetbounds csp, csp, t1 # Restrict bounds so main can't wander
# Call main
# sw ra, 12(sp)
call main
# a0 contains return value
mv t1, a0
# la t0, tohost
# Signal success
la t0, 0
li t1, 1
sd t1, 0(t0)

View File

@@ -1,8 +1,14 @@
# Send assembler file to remote machine to run
scp cheri.S home:/home/akilan/cheri/output/sdk/bin/
scp Cprograms/start.S home:/home/akilan/cheri/output/sdk/bin/
scp Cprograms/main.c home:/home/akilan/cheri/output/sdk/bin/
# Run compiled instruction remotely
ssh home 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=l64pc128 -nostdlib -nostartfiles -Wl,-Ttext=0x80000000 -o testC cheri.S'
# ssh home 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=l64pc128 -nostdlib -nostartfiles -Wl,-Ttext=0x80000000 -o testC start.S main.c'
ssh home 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -nostdlib -nostartfiles -Wl,-Ttext=0x80000000 -o testC start.S main.c'
# Disassembly ouput
ssh home 'cd /home/akilan/cheri/output/sdk/bin/ && ./llvm-objdump -d testC'
# Copy file back for testing
scp home:/home/akilan/cheri/output/sdk/bin/testC .

View File

@@ -14,12 +14,6 @@ _start:
# li t5, 4
cincoffsetimm csp, csp, 10
# seal the bounded capability
# cspecialr ct1, 4
# li t2, 12
# csetaddr ct1, ct1, t2
# ccseal c2, csp, t2
# Read bounds
cgetbase t3, csp
ld t0, 0(csp)
@@ -30,30 +24,3 @@ _start:
li t1, 1
csd t1, 0(c1)
halt:
wfi
j halt
.globl exit
exit:
j exit
.section .bss
.align 16
stack_bottom:
.space 2048
stack_top: # address = 0x80001000 (for csetaddr)
# --------------------------------------------------
# Data section (NO .bss)
# --------------------------------------------------
# .section .data
# .align 3
# .globl tohost
# .globl fromhost
# tohost:
# .dword 0
#fromhost:
# .dword 0

20
Tests/isa/cheri.c Normal file
View File

@@ -0,0 +1,20 @@
#include <cheriintrin.h>
#include <stdint.h>
int main() {
void *__capability csp;
// Set address
csp = (void *__capability)0x80001000;
// Set bounds
csp = cheri_bounds_set(csp, 8);
// Increment offset
csp = cheri_offset_increment(csp, 10);
uint64_t val = *(uint64_t *__capability)csp;
while (1) {}
return 0;
}

Binary file not shown.