[hwasan] Tweak process and thread initialization.
Make sure that TLS_SLOT_TSAN is always available and correctly set up in
HWASan-instrumented functions by setting up the tls register and running hwasan
initialization (__hwasan_init in the main thread and __hwasan_thread_enter in
secondary) early enough.
This is needed to accomodate a change in HWASan: https://reviews.llvm.org/D52249
Bug: 112438058
Test: boot with SANITIZE_TARGET=hwaddress, run bionic-tests
Change-Id: Icd909a4ea0da6c6c1095522bcc28debef5f2c63d
diff --git a/libc/bionic/libc_init_static.cpp b/libc/bionic/libc_init_static.cpp
index 55506a3..38a04f8 100644
--- a/libc/bionic/libc_init_static.cpp
+++ b/libc/bionic/libc_init_static.cpp
@@ -127,20 +127,25 @@
exit(slingshot(args.argc, args.argv, args.envp));
}
-#if __has_feature(hwaddress_sanitizer)
+extern "C" void __hwasan_init();
+
__attribute__((no_sanitize("hwaddress")))
-#endif
__noreturn void __libc_init(void* raw_args,
void (*onexit)(void) __unused,
int (*slingshot)(int, char**, char**),
structors_array_t const * const structors) {
#if __has_feature(hwaddress_sanitizer)
- __hwasan_shadow_init();
+ // Install main thread TLS early. It will be initialized later in __libc_init_main_thread. For now
+ // all we need is access to TLS_SLOT_TSAN.
+ pthread_internal_t* main_thread = __get_main_thread();
+ __set_tls(main_thread->tls);
+ // Initialize HWASan. This sets up TLS_SLOT_TSAN, among other things.
+ __hwasan_init();
+ // We are ready to run HWASan-instrumented code, proceed with libc initialization...
#endif
__real_libc_init(raw_args, onexit, slingshot, structors);
}
-
static uint32_t g_target_sdk_version{__ANDROID_API__};
extern "C" uint32_t android_get_application_target_sdk_version() {