Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 8f21e45

Browse files
Treehugger RobotGerrit Code Review
authored andcommitted
Merge "Decrease logspam of stack MTE remapping." into main
2 parents 86b88c1 + f7c5e68 commit 8f21e45

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

libc/bionic/libc_init_dynamic.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ extern "C" {
6161
};
6262

6363
void memtag_stack_dlopen_callback() {
64-
async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "remapping stacks as PROT_MTE");
65-
__pthread_internal_remap_stack_with_mte();
64+
if (__pthread_internal_remap_stack_with_mte()) {
65+
async_safe_format_log(ANDROID_LOG_DEBUG, "libc", "remapped stacks as PROT_MTE");
66+
}
6667
}
6768

6869
// Use an initializer so __libc_sysinfo will have a fallback implementation

libc/bionic/pthread_internal.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,21 +240,21 @@ __LIBC_HIDDEN__ void* __allocate_stack_mte_ringbuffer(size_t n, pthread_internal
240240
return reinterpret_cast<void*>(aligned_allocation | ((1ULL << n) << 56ULL));
241241
}
242242

243-
void __pthread_internal_remap_stack_with_mte() {
243+
bool __pthread_internal_remap_stack_with_mte() {
244244
#if defined(__aarch64__)
245245
ScopedWriteLock creation_locker(&g_thread_creation_lock);
246246
ScopedReadLock list_locker(&g_thread_list_lock);
247247
// If process already uses memtag-stack ABI, we don't need to do anything.
248-
if (__libc_memtag_stack_abi) return;
248+
if (__libc_memtag_stack_abi) return false;
249249
__libc_memtag_stack_abi = true;
250250

251251
for (pthread_internal_t* t = g_thread_list; t != nullptr; t = t->next) {
252252
if (t->terminating) continue;
253253
t->bionic_tcb->tls_slot(TLS_SLOT_STACK_MTE) =
254254
__allocate_stack_mte_ringbuffer(0, t->is_main() ? nullptr : t);
255255
}
256-
if (!atomic_load(&__libc_globals->memtag)) return;
257-
if (atomic_exchange(&__libc_memtag_stack, true)) return;
256+
if (!atomic_load(&__libc_globals->memtag)) return false;
257+
if (atomic_exchange(&__libc_memtag_stack, true)) return false;
258258
uintptr_t lo, hi;
259259
__find_main_stack_limits(&lo, &hi);
260260

@@ -269,7 +269,10 @@ void __pthread_internal_remap_stack_with_mte() {
269269
async_safe_fatal("error: failed to set PROT_MTE on thread: %d", t->tid);
270270
}
271271
}
272-
#endif
272+
return true;
273+
#else
274+
return false;
275+
#endif // defined(__aarch64__)
273276
}
274277

275278
bool android_run_on_all_threads(bool (*func)(void*), void* arg) {

libc/bionic/pthread_internal.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,9 @@ __LIBC_HIDDEN__ extern void __bionic_atfork_run_prepare();
273273
__LIBC_HIDDEN__ extern void __bionic_atfork_run_child();
274274
__LIBC_HIDDEN__ extern void __bionic_atfork_run_parent();
275275

276-
// Re-map all threads and successively launched threads with PROT_MTE.
277-
__LIBC_HIDDEN__ void __pthread_internal_remap_stack_with_mte();
276+
// Re-map all threads and successively launched threads with PROT_MTE. Returns 'true' if remapping
277+
// took place, 'false' on error or if the stacks were already remapped in the past.
278+
__LIBC_HIDDEN__ bool __pthread_internal_remap_stack_with_mte();
278279

279280
extern "C" bool android_run_on_all_threads(bool (*func)(void*), void* arg);
280281

0 commit comments

Comments
 (0)