Skip to content

Commit

Permalink
Add a hybrid example related to threads
Browse files Browse the repository at this point in the history
Check whether we can set DDCs for spawned threads individually, and
independent from the main program thread.
  • Loading branch information
0152la committed Apr 10, 2024
1 parent 8772cc0 commit 3057bbe
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
73 changes: 73 additions & 0 deletions hybrid/threads_ddc.c
Original file line number Diff line number Diff line change
@@ -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 <assert.h>
#include <math.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#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;
}
1 change: 1 addition & 0 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ elif [ "$1" = "morello-hybrid" ]; then
run OK compare_platforms compare_platforms_overflow
run OK hybrid/ddc_compartment_switching ddc_compartment_switching
run OK hybrid basic_ddc
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 syscall-restrict syscall-restrict
Expand Down

0 comments on commit 3057bbe

Please sign in to comment.