diff --git a/build/Makefile.vars.morello-hybrid b/build/Makefile.vars.morello-hybrid index 99ec093..48e6db6 100644 --- a/build/Makefile.vars.morello-hybrid +++ b/build/Makefile.vars.morello-hybrid @@ -6,5 +6,5 @@ CHERIBASE ?= $(HOME)/cheri SDKBASE ?= $(CHERIBASE)/output/morello-sdk -CFLAGS := --config cheribsd-morello-hybrid.cfg $(CFLAGS) +CFLAGS := --config cheribsd-morello-hybrid.cfg $(CFLAGS) -lm -lpthread PLATFORM := morello-hybrid diff --git a/hybrid/threads_ddc.c b/hybrid/threads_ddc.c new file mode 100644 index 0000000..ecdff2b --- /dev/null +++ b/hybrid/threads_ddc.c @@ -0,0 +1,73 @@ +/*** + * This example explores setting different DDCs across threads, ensuring each + * thread has its own DDC which does not interfere with other threads. + * + * The number of threads to execute can be changed by modifying `t_count`. + ***/ + +#include +#include +#include +#include +#include +#include +#include + +#include "cheriintrin.h" + +#include "../include/common.h" +#include "./include/utils.h" + +struct t_info +{ + unsigned short count; + pthread_t tid; + void *addr_in; + void *__capability ddc_out; +}; + +void *run_thread(void *curr_t_info_ptr) +{ + struct t_info *curr_t_info = (struct t_info *) curr_t_info_ptr; + void *__capability old_ddc = read_ddc(); + void *__capability new_ddc = + cheri_address_set(cheri_ddc_get(), (intptr_t) curr_t_info->addr_in); + write_ddc(new_ddc); + // Force wait to ensure threads don't die too fast + for (size_t i = 0; i < pow(10, 6); ++i) + { + }; + curr_t_info->ddc_out = read_ddc(); + assert(cheri_address_get(curr_t_info->ddc_out) == (unsigned long) curr_t_info->addr_in); + assert(curr_t_info->ddc_out != old_ddc); + return NULL; +} + +int main() +{ + unsigned short t_count = 3; + const unsigned int to_alloc = 256; + struct t_info *t_infos = malloc(t_count * sizeof(struct t_info)); + pthread_t *tids = malloc(t_count * sizeof(pthread_t)); + + for (size_t i = 0; i < t_count; ++i) + { + t_infos[i].count = i; + t_infos[i].addr_in = malloc(to_alloc); + pthread_create(&t_infos[i].tid, NULL, &run_thread, &t_infos[i]); + } + + printf("== Main thread DDC:\n"); + pp_cap(cheri_ddc_get()); + for (size_t i = 0; i < t_count; ++i) + { + pthread_join(t_infos[i].tid, NULL); + printf("== From thread %hu:\n", t_infos[i].count); + pp_cap(t_infos[i].ddc_out); + free(t_infos[i].addr_in); + } + + free(tids); + free(t_infos); + return 0; +} diff --git a/tests/run_tests.sh b/tests/run_tests.sh index e45f1e8..5b918b8 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -88,6 +88,7 @@ elif [ "$1" = "morello-hybrid" ]; then run OK example_allocators/compartment_alloc main run OK hybrid basic_ddc run OK hybrid cap_build + run OK hybrid threads_ddc run OK hybrid/compartment_examples/inter_comp_call/base main run OK hybrid/compartment_examples/inter_comp_call/malicious_compartments inter_comp_call-secure run OK hybrid/ddc_compartment_switching ddc_compartment_switching