Skip to content

Commit 6d24d3e

Browse files
committed
memory_maps: abort in ia2_thread_metadata_get_for_current_thread on error instead of returning NULL
1 parent 52a87f5 commit 6d24d3e

File tree

5 files changed

+18
-25
lines changed

5 files changed

+18
-25
lines changed

runtime/libia2/ia2.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,7 @@ int protect_tls_pages(struct dl_phdr_info *info, size_t size, void *data) {
334334
exit(-1);
335335
}
336336
#if IA2_DEBUG_MEMORY
337-
if (thread_metadata) {
338-
thread_metadata->tls_addr_compartment1_first = (uintptr_t)start_round_down;
339-
}
337+
thread_metadata->tls_addr_compartment1_first = (uintptr_t)start_round_down;
340338
#endif
341339
}
342340
uint64_t after_untrusted_region_start = untrusted_stackptr_addr + 0x1000;
@@ -350,9 +348,7 @@ int protect_tls_pages(struct dl_phdr_info *info, size_t size, void *data) {
350348
exit(-1);
351349
}
352350
#if IA2_DEBUG_MEMORY
353-
if (thread_metadata) {
354-
thread_metadata->tls_addr_compartment1_second = (uintptr_t)after_untrusted_region_start;
355-
}
351+
thread_metadata->tls_addr_compartment1_second = (uintptr_t)after_untrusted_region_start;
356352
#endif
357353
}
358354
} else {
@@ -364,9 +360,7 @@ int protect_tls_pages(struct dl_phdr_info *info, size_t size, void *data) {
364360
exit(-1);
365361
}
366362
#if IA2_DEBUG_MEMORY
367-
if (thread_metadata) {
368-
thread_metadata->tls_addrs[pkey] = (uintptr_t)start_round_down;
369-
}
363+
thread_metadata->tls_addrs[pkey] = (uintptr_t)start_round_down;
370364
#endif
371365
}
372366
}

runtime/libia2/init.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ char *allocate_stack(int i) {
7979
ia2_log("allocating stack for compartment %d on thread %ld: %p..%p\n", i, (long)gettid(), stack, stack + STACK_SIZE);
8080
#if IA2_DEBUG_MEMORY
8181
struct ia2_thread_metadata *const thread_metadata = ia2_thread_metadata_get_for_current_thread();
82-
if (thread_metadata) {
83-
thread_metadata->stack_addrs[i] = (uintptr_t)stack;
84-
}
82+
thread_metadata->stack_addrs[i] = (uintptr_t)stack;
8583
#endif
8684
assert(stacks[i] == NULL); // We should only be setting this once per thread compartment right after thread creation.
8785
stacks[i] = stack;

runtime/libia2/memory_maps.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,23 @@
1515
struct ia2_thread_metadata *ia2_all_threads_metadata_get_for_current_thread(struct ia2_all_threads_metadata *const this) {
1616
const pid_t tid = gettid();
1717

18-
struct ia2_thread_metadata *metadata = NULL;
1918
if (pthread_mutex_lock(&this->lock) != 0) {
2019
perror("pthread_mutex_lock in ia2_all_threads_data_lookup failed");
21-
goto ret;
20+
abort();
21+
}
22+
23+
if (this->num_threads >= array_len(this->thread_metadata)) {
24+
fprintf(stderr, "created %zu threads, but can't store them all (max is IA2_MAX_THREADS)\n", this->num_threads);
25+
abort();
2226
}
27+
28+
struct ia2_thread_metadata *metadata = NULL;
2329
for (size_t i = 0; i < this->num_threads; i++) {
2430
if (this->tids[i] == tid) {
2531
metadata = &this->thread_metadata[i];
2632
goto unlock;
2733
}
2834
}
29-
if (this->num_threads >= array_len(this->thread_metadata)) {
30-
fprintf(stderr, "created %zu threads, but can't store them all (max is IA2_MAX_THREADS)\n", this->num_threads);
31-
goto unlock;
32-
}
3335

3436
metadata = &this->thread_metadata[this->num_threads];
3537
this->tids[this->num_threads] = tid;
@@ -38,13 +40,12 @@ struct ia2_thread_metadata *ia2_all_threads_metadata_get_for_current_thread(stru
3840
metadata->tid = tid;
3941
metadata->thread = pthread_self();
4042

41-
goto unlock;
42-
4343
unlock:
4444
if (pthread_mutex_unlock(&this->lock) != 0) {
4545
perror("pthread_mutex_unlock in ia2_all_threads_data_lookup failed");
46+
abort();
4647
}
47-
ret:
48+
4849
return metadata;
4950
}
5051

@@ -112,6 +113,7 @@ static void label_memory_map(FILE *log, uintptr_t start_addr) {
112113
const struct ia2_addr_location location = ia2_addr_location_find(start_addr);
113114
const struct ia2_thread_metadata *metadata = location.thread_metadata;
114115

116+
// If `location.name` is non-`NULL`, then `location` was found.
115117
if (location.name) {
116118
Dl_info dl_info = {0};
117119
const bool has_dl_info = dladdr((void *)metadata->start_fn, &dl_info);

runtime/libia2/memory_maps.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
/// Find the `struct ia2_thread_metadata*` for the current thread,
1717
/// adding (but not allocating) one if there isn't one yet.
18-
/// If there is no memory for more or an error, `NULL` is returned.
18+
/// If there is no memory for more or some unexpected error,
19+
/// it `abort`s. `NULL` is never returned.
1920
/// This is a purely lookup and/or additive operation,
2021
/// so the lifetime of the returned `struct ia2_thread_metadata*` is infinite,
2122
/// and since it's thread-specific,

runtime/libia2/threads.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ void *ia2_thread_begin(void *arg) {
3232

3333
#if IA2_DEBUG_MEMORY
3434
struct ia2_thread_metadata *const thread_metadata = ia2_thread_metadata_get_for_current_thread();
35-
if (thread_metadata) {
36-
thread_metadata->start_fn = fn;
37-
}
35+
thread_metadata->start_fn = fn;
3836
#endif
3937

4038
init_stacks_and_setup_tls();

0 commit comments

Comments
 (0)