This exercise steps you through getting up and running with code compilation and execution for RISC-V and CHERI-RISC-V programs.
The first test program is written in conventional C, and can be compiled to RISC-V or CHERI-RISC-V targets:
- Compile print-pointer.c with a RISC-V target and a binary name of print-pointer-riscv.
print-pointer.c:
/*
* SPDX-License-Identifier: BSD-2-Clause-DARPA-SSITH-ECATS-HR0011-18-C-0016
* Copyright (c) 2020 SRI International
*/
#include <printf.h>
void
init(void)
{
printf("size of pointer: %zu\n", sizeof(void *));
/* XXX: ideally we'd use ptraddr_t below */
printf("size of address: %zu\n", sizeof(size_t));
}
void notified(void){}
-
Run the binary.
-
Compile print-pointer.c with a CHERI-RISC-V target and a binary name of print-pointer-cheri.
-
Run the binary: it should print a pointer size of 16 and address size of 8.
The second test program is written in CHERI C:
- Compile print-capability.c with a CHERI-RISC-V target and a binary name of print-capability.
/*
* SPDX-License-Identifier: BSD-2-Clause-DARPA-SSITH-ECATS-HR0011-18-C-0016
* Copyright (c) 2020 SRI International
*/
#include <printf.h>
void
init(void)
{
int i;
char *c;
void *cap_to_int = &i;
void *cap_to_cap = &c;
printf("cap to int length: %lu\n", __builtin_cheri_length_get(cap_to_int));
printf("cap to cap length: %lu\n", __builtin_cheri_length_get(cap_to_cap));
}
void notified(void){}
- Run the binary: note how the length of the capability depends on the size of the type it points to.