110 lines
2.3 KiB
C
110 lines
2.3 KiB
C
#include <cheriintrin.h>
|
|
#include <stdint.h>
|
|
|
|
void test() {
|
|
|
|
void *__capability c1;
|
|
|
|
// Set address
|
|
c1 = (void *__capability)0x80001009;
|
|
|
|
c1 = cheri_bounds_set(c1, 8);
|
|
|
|
}
|
|
|
|
// Add delta value for TLB translation
|
|
static inline void * __capability add_delta(void * __capability cap, int offset) {
|
|
void * __capability result;
|
|
|
|
asm volatile (
|
|
"cincoffset %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 write_string_baremetal(void *__capability cap, const char* src) {
|
|
// char *__capability ptr = (char *__capability)cap;
|
|
|
|
// while (*src != '\0') {
|
|
// *ptr = *src; // This will FAULT if ptr goes outside cap's bounds
|
|
// // ptr = add_delta(ptr, 1);
|
|
// src++;
|
|
// }
|
|
// *ptr = '\0'; // Null terminate
|
|
// }
|
|
|
|
// int main(void) {
|
|
// void *__capability csp1;
|
|
// void *__capability csp2;
|
|
|
|
// // Set address
|
|
// // csp1 = (void *__capability)0x80001000;
|
|
|
|
// //void *csp1;
|
|
|
|
// // Set bounds
|
|
// csp1 = cheri_bounds_set(csp1, 40);
|
|
// // Increment offset
|
|
// // csp = cheri_offset_increment(csp, 10);
|
|
// csp1 = add_delta(csp1, 20);
|
|
|
|
// // csp2 = (void *__capability)0x80004000;
|
|
|
|
// // csp2 = cheri_bounds_set(csp2, 12);
|
|
|
|
// // csp2 = add_delta(csp2, 13);
|
|
|
|
// // uint64_t val = *(uint64_t * __capability)csp1;
|
|
// // csp2 = 'A';
|
|
|
|
// //*char csp1 = 'A';
|
|
// *(int * __capability)csp1 = 42;
|
|
|
|
|
|
// // uint64_t val = *(uint64_t *__capability)csp1;
|
|
// // test();
|
|
|
|
// return 0;
|
|
// }
|
|
|
|
int main(void) {
|
|
void *__capability csp1;
|
|
|
|
// Start from a VALID capability
|
|
void *__capability base = cheri_ddc_get();
|
|
|
|
// Set address
|
|
// csp1 = cheri_address_set(base, 0x80001000);
|
|
|
|
// Set bounds
|
|
// csp1 = cheri_bounds_set(csp1, 40);
|
|
|
|
// Add offset
|
|
// csp1 = cheri_offset_increment(csp1, 20);
|
|
|
|
// ()csp1 = 12;
|
|
// int j;
|
|
// j = i;
|
|
int a = 3;
|
|
|
|
char letters[] = {'A', 'B', 'C', 'D', 'E'};
|
|
|
|
csp1 = cheri_address_set(base, (uintptr_t)letters);
|
|
|
|
csp1 = __builtin_cheri_bounds_set(csp1, sizeof(letters));
|
|
|
|
*((char *__capability)csp1) = 'Z';
|
|
|
|
// csp1 = &letters;
|
|
|
|
// letters[1] = 'D';
|
|
|
|
|
|
|
|
return 0;
|
|
} |