Skip to content

Commit

Permalink
mte: make h_malloc_disable_memory_tagging() thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
muhomorr committed Oct 29, 2023
1 parent 225f0b0 commit 2fe0e3f
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions h_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1181,13 +1181,14 @@ static inline void enforce_init(void) {
}
}

static struct mutex init_lock = MUTEX_INITIALIZER;

COLD static void init_slow_path(void) {
static struct mutex lock = MUTEX_INITIALIZER;

mutex_lock(&lock);
mutex_lock(&init_lock);

if (unlikely(is_init())) {
mutex_unlock(&lock);
mutex_unlock(&init_lock);
return;
}

Expand Down Expand Up @@ -1278,7 +1279,7 @@ COLD static void init_slow_path(void) {
}
memory_set_name(&ro, sizeof(ro), "malloc read-only after init");

mutex_unlock(&lock);
mutex_unlock(&init_lock);

// may allocate, so wait until the allocator is initialized to avoid deadlocking
if (unlikely(pthread_atfork(full_lock, full_unlock, post_fork_child))) {
Expand Down Expand Up @@ -2157,6 +2158,7 @@ COLD EXPORT int h_malloc_set_state(UNUSED void *state) {
#ifdef __ANDROID__
COLD EXPORT void h_malloc_disable_memory_tagging(void) {
#ifdef HAS_ARM_MTE
mutex_lock(&init_lock);
if (!ro.is_memtag_disabled) {
if (is_init()) {
if (unlikely(memory_protect_rw(&ro, sizeof(ro)))) {
Expand All @@ -2171,6 +2173,7 @@ COLD EXPORT void h_malloc_disable_memory_tagging(void) {
ro.is_memtag_disabled = true;
}
}
mutex_unlock(&init_lock);
#endif
}
#endif

0 comments on commit 2fe0e3f

Please sign in to comment.