Skip to content

Commit 749b9c0

Browse files
committed
Decrease logspam of stack MTE remapping.
Currently the log is called for each library that's loaded with MTE stack. For apps that are a launched with stack MTE (e.g. the CTS test apps), this causes ~40 or so loglines telling us that the stack is being remapped. Instead, make it log only once, the first time it's remapped. Bug: N/A Test: blatant visual code inspection Change-Id: If2883bc2355318b1db83f0c5ccb67050a457b52e
1 parent 1ac6bee commit 749b9c0

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)