Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a hybrid example related to threads #94

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/Makefile.vars.morello-hybrid
Original file line number Diff line number Diff line change
Expand Up @@ -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
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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add an assert(curr_t_info->ddc_out == curr_t_info->addr_in) or similar here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 66126d7. Also added a check to ensure the DDC is changed.

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 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
Expand Down