sample malloc working
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
# Send assembler file to remote machine to run
|
||||
scp start.S home-1:/home/akilan/cheri/output/sdk/bin/
|
||||
scp main.c home-1:/home/akilan/cheri/output/sdk/bin/
|
||||
# scp main.c home-1:/home/akilan/cheri/output/sdk/bin/
|
||||
scp malloc.c home-1:/home/akilan/cheri/output/sdk/bin/
|
||||
|
||||
# scp cheri.S home:/home/akilan/cheri/output/sdk/bin/
|
||||
|
||||
@@ -11,6 +12,9 @@ scp main.c home-1:/home/akilan/cheri/output/sdk/bin/
|
||||
|
||||
ssh home-1 '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'
|
||||
|
||||
# Malloc test implementation
|
||||
ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -nostdlib -nostartfiles -fno-builtin-malloc -mcmodel=medany -Wl,-Ttext=0x80000000 -o testC start.S malloc.c'
|
||||
|
||||
# Disassembly ouput
|
||||
ssh home-1 'cd /home/akilan/cheri/output/sdk/bin/ && ./llvm-objdump -d testC'
|
||||
|
||||
|
||||
74
Tests/isa/CPrograms/malloc.c
Normal file
74
Tests/isa/CPrograms/malloc.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <cheriintrin.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define HEAP_SIZE 4096
|
||||
|
||||
static char heap[HEAP_SIZE];
|
||||
static size_t bump = 0;
|
||||
|
||||
// Add delta value for TLB translation
|
||||
static inline void * __capability add_delta(void * __capability cap, int offset) {
|
||||
void * __capability result;
|
||||
|
||||
asm volatile (
|
||||
"candperm %0, %1, %2"
|
||||
: "=C" (result) // Output: %0 (result)
|
||||
: "C" (cap), // Input: %1 (original cap)
|
||||
"r" (offset) // Input: %2 (offset register)
|
||||
: // No clobbered registers
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void * __capability malloc(size_t size) {
|
||||
|
||||
// Align to 8 bytes (important for capability safety)
|
||||
size = (size + 7) & ~7;
|
||||
|
||||
if (bump + size > HEAP_SIZE)
|
||||
return NULL;
|
||||
|
||||
void * __capability base = cheri_ddc_get();
|
||||
|
||||
uintptr_t addr = (uintptr_t)(heap + bump);
|
||||
|
||||
// Create capability to this region
|
||||
void * __capability cap = cheri_address_set(base, addr);
|
||||
|
||||
// Enforce bounds (this is the key CHERI feature)
|
||||
cap = cheri_bounds_set(cap, size);
|
||||
|
||||
// Hard-coded delta value
|
||||
cap = add_delta(cap, 4);
|
||||
|
||||
bump += size;
|
||||
|
||||
return cap;
|
||||
}
|
||||
|
||||
void bump_reset(void) {
|
||||
bump = 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
char * __capability a = malloc(30);
|
||||
char * __capability b = malloc(16);
|
||||
|
||||
if (!a || !b) return -1;
|
||||
|
||||
a[0] = 'A';
|
||||
a[1] = 'C';
|
||||
b[0] = 'B';
|
||||
|
||||
// This will fault (out-of-bounds)
|
||||
// a[20] = 'X';
|
||||
|
||||
if (a[1] != 'C') {
|
||||
while (1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
Tests/isa/testC
BIN
Tests/isa/testC
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Submodule libs/cheri-cap-lib updated: 6120b13246...9d7de214d7
Reference in New Issue
Block a user