Skip to content

Commit b07b8b3

Browse files
committed
add more synchronization for enabling/disabling memory tagging
1 parent 12860f7 commit b07b8b3

1 file changed

Lines changed: 13 additions & 18 deletions

File tree

h_malloc.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,14 +1232,13 @@ static inline void enforce_init(void) {
12321232
}
12331233
}
12341234

1235-
static struct mutex init_lock = MUTEX_INITIALIZER;
1236-
12371235
COLD static void init_slow_path(void) {
1236+
static struct mutex lock = MUTEX_INITIALIZER;
12381237

1239-
mutex_lock(&init_lock);
1238+
mutex_lock(&lock);
12401239

12411240
if (unlikely(is_init())) {
1242-
mutex_unlock(&init_lock);
1241+
mutex_unlock(&lock);
12431242
return;
12441243
}
12451244

@@ -1324,7 +1323,7 @@ COLD static void init_slow_path(void) {
13241323
}
13251324
memory_set_name(&ro, sizeof(ro), "malloc read-only after init");
13261325

1327-
mutex_unlock(&init_lock);
1326+
mutex_unlock(&lock);
13281327

13291328
// may allocate, so wait until the allocator is initialized to avoid deadlocking
13301329
if (unlikely(pthread_atfork(full_lock, full_unlock, post_fork_child))) {
@@ -2214,22 +2213,18 @@ COLD EXPORT int h_malloc_set_state(UNUSED void *state) {
22142213
#ifdef __ANDROID__
22152214
COLD EXPORT void h_malloc_disable_memory_tagging(void) {
22162215
#ifdef HAS_ARM_MTE
2217-
mutex_lock(&init_lock);
2216+
init();
2217+
full_lock();
22182218
if (!ro.is_memtag_disabled) {
2219-
if (is_init()) {
2220-
if (unlikely(memory_protect_rw(&ro, sizeof(ro)))) {
2221-
fatal_error("failed to unprotect allocator data");
2222-
}
2223-
ro.is_memtag_disabled = true;
2224-
if (unlikely(memory_protect_ro(&ro, sizeof(ro)))) {
2225-
fatal_error("failed to protect allocator data");
2226-
}
2227-
} else {
2228-
// bionic calls this function very early in some cases
2229-
ro.is_memtag_disabled = true;
2219+
if (unlikely(memory_protect_rw(&ro, sizeof(ro)))) {
2220+
fatal_error("failed to unprotect allocator data");
2221+
}
2222+
ro.is_memtag_disabled = true;
2223+
if (unlikely(memory_protect_ro(&ro, sizeof(ro)))) {
2224+
fatal_error("failed to protect allocator data");
22302225
}
22312226
}
2232-
mutex_unlock(&init_lock);
2227+
full_unlock();
22332228
#endif
22342229
}
22352230
#endif

0 commit comments

Comments
 (0)