dtape getters: Return NULL if input is NULL

Found via UBSAN
This commit is contained in:
Ariel Abreu 2022-03-03 00:09:29 -05:00
parent a7e054c57e
commit 342245fa21
No known key found for this signature in database
GPG Key ID: D67AE16CCEA85B70
3 changed files with 10 additions and 1 deletions

View File

@ -18,6 +18,9 @@ struct dtape_task {
__attribute__((always_inline))
static dtape_task_t* dtape_task_for_xnu_task(task_t xnu_task) {
if (!xnu_task) {
return NULL;
}
return (dtape_task_t*)((char*)xnu_task - offsetof(dtape_task_t, xnu_task));
};

View File

@ -42,11 +42,17 @@ struct dtape_thread {
__attribute__((always_inline))
static dtape_thread_t* dtape_thread_for_xnu_thread(thread_t xnu_thread) {
if (!xnu_thread) {
return NULL;
}
return (dtape_thread_t*)((char*)xnu_thread - offsetof(dtape_thread_t, xnu_thread));
};
__attribute__((always_inline))
static dtape_task_t* dtape_task_for_thread(dtape_thread_t* thread) {
if (!thread) {
return NULL;
}
return dtape_task_for_xnu_task(thread->xnu_thread.task);
};

View File

@ -414,7 +414,7 @@ void dtape_thread_dying(dtape_thread_t* thread) {
thread_t current_thread(void) {
dtape_thread_t* thread = dtape_hooks->current_thread();
return &thread->xnu_thread;
return thread ? &thread->xnu_thread : NULL;
};
void (thread_reference)(thread_t thread) {