Fix HWASan crashes in heapprofd initialization.
Test: atest perfetto_integrationtests on HWASan build
Bug: 210588740
Change-Id: Iddaa4a7d0dedee541f97bbf0a13958de023bdf59
diff --git a/libc/bionic/libc_init_dynamic.cpp b/libc/bionic/libc_init_dynamic.cpp
index 4625fa1..24efbf5 100644
--- a/libc/bionic/libc_init_dynamic.cpp
+++ b/libc/bionic/libc_init_dynamic.cpp
@@ -93,6 +93,15 @@
__libc_init_common();
__libc_init_scudo();
+#if __has_feature(hwaddress_sanitizer)
+ // Notify the HWASan runtime library whenever a library is loaded or unloaded
+ // so that it can update its shadow memory.
+ // This has to happen before _libc_init_malloc which might dlopen to load
+ // profiler libraries.
+ __libc_shared_globals()->load_hook = __hwasan_library_loaded;
+ __libc_shared_globals()->unload_hook = __hwasan_library_unloaded;
+#endif
+
// Hooks for various libraries to let them know that we're starting up.
__libc_globals.mutate(__libc_init_malloc);
@@ -101,13 +110,6 @@
__libc_init_fork_handler();
-#if __has_feature(hwaddress_sanitizer)
- // Notify the HWASan runtime library whenever a library is loaded or unloaded
- // so that it can update its shadow memory.
- __libc_shared_globals()->load_hook = __hwasan_library_loaded;
- __libc_shared_globals()->unload_hook = __hwasan_library_unloaded;
-#endif
-
__libc_shared_globals()->set_target_sdk_version_hook = __libc_set_target_sdk_version;
netdClientInit();