Add an API for per-process disabling memory initialization.
Introduce an android_mallopt(M_DISABLE_MEMORY_MITIGATIONS) API call
that may be used to disable zero- or pattern-init on non-MTE hardware,
or memory tagging on MTE hardware. The intent is that this function
may be called at any time, including when there are multiple threads
running.
Disabling zero- or pattern-init is quite trivial, we just need to set
a global variable to 0 via a Scudo API call (although there will be
some separate work required on the Scudo side to make this operation
thread-safe).
It is a bit more tricky to disable MTE across a process, because
the kernel does not provide an API for disabling tag checking in all
threads in a process, only per-thread. We need to send a signal to each
of the process's threads with a handler that issues the required prctl
call, and lock thread creation for the duration of the API call to
avoid races between thread enumeration and calls to pthread_create().
Bug: 135772972
Change-Id: I81ece86ace916eb6b435ab516cd431ec4b48a3bf
diff --git a/libc/bionic/pthread_exit.cpp b/libc/bionic/pthread_exit.cpp
index 81dab57..bde95ec 100644
--- a/libc/bionic/pthread_exit.cpp
+++ b/libc/bionic/pthread_exit.cpp
@@ -35,6 +35,7 @@
#include "private/bionic_constants.h"
#include "private/bionic_defs.h"
+#include "private/ScopedRWLock.h"
#include "private/ScopedSignalBlocker.h"
#include "pthread_internal.h"
@@ -103,9 +104,18 @@
!atomic_compare_exchange_weak(&thread->join_state, &old_state, THREAD_EXITED_NOT_JOINED)) {
}
- // We don't want to take a signal after unmapping the stack, the shadow call
- // stack, or dynamic TLS memory.
- ScopedSignalBlocker ssb;
+ // android_run_on_all_threads() needs to see signals blocked atomically with setting the
+ // terminating flag, so take the creation lock while doing these operations.
+ {
+ ScopedReadLock locker(&g_thread_creation_lock);
+ atomic_store(&thread->terminating, true);
+
+ // We don't want to take a signal after unmapping the stack, the shadow call stack, or dynamic
+ // TLS memory.
+ sigset64_t set;
+ sigfillset64(&set);
+ __rt_sigprocmask(SIG_BLOCK, &set, nullptr, sizeof(sigset64_t));
+ }
#ifdef __aarch64__
// Free the shadow call stack and guard pages.