Skip to content

Commit 573721b

Browse files
authored
[sanitizer][Fuchsia] Add callback at end of __sanitizer_startup_hook (#131886)
Sanitizers using this hook on Fuchsia can define this function to do any extra stuff at the end of the startup hook. For now this is only used by HWASan which needs to explicitly be initialized before libc extensions are intitialized.
1 parent 304c7a8 commit 573721b

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

compiler-rt/lib/asan/asan_fuchsia.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
# include "asan_thread.h"
2626
# include "lsan/lsan_common.h"
2727

28+
namespace __sanitizer {
29+
// ASan doesn't need to do anything else special in the startup hook.
30+
void EarlySanitizerInit() {}
31+
} // namespace __sanitizer
32+
2833
namespace __asan {
2934

3035
// The system already set up the shadow memory for us.

compiler-rt/lib/hwasan/hwasan_fuchsia.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@
3131
SANITIZER_INTERFACE_ATTRIBUTE
3232
THREADLOCAL uptr __hwasan_tls;
3333

34+
namespace __sanitizer {
35+
void EarlySanitizerInit() {
36+
// Setup the hwasan runtime before any `__libc_extensions_init`s are called.
37+
// This is needed because libraries which define this function (like fdio)
38+
// may be instrumented and either access `__hwasan_tls` or make runtime calls.
39+
__hwasan_init();
40+
}
41+
} // namespace __sanitizer
42+
3443
namespace __hwasan {
3544

3645
bool InitShadow() {

compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,8 @@ void __sanitizer_startup_hook(int argc, char **argv, char **envp,
547547
__sanitizer::StoredEnviron = envp;
548548
__sanitizer::MainThreadStackBase = reinterpret_cast<uintptr_t>(stack_base);
549549
__sanitizer::MainThreadStackSize = stack_size;
550+
551+
EarlySanitizerInit();
550552
}
551553

552554
void __sanitizer_set_report_path(const char *path) {

compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ struct MemoryMappingLayoutData {
3232

3333
void InitShadowBounds();
3434

35+
// Individual sanitizers can define this to explicitly run something at the end
36+
// of `__sanitizer_startup_hook`. This can be useful if a sanitizer needs to do
37+
// extra work after the common startup hook code is called and before module
38+
// ctors are invoked. For example, hwasan can explicitly call its initializing
39+
// function here so it can be set up before libc extensions are initialized.
40+
void EarlySanitizerInit();
41+
3542
} // namespace __sanitizer
3643

3744
#endif // SANITIZER_FUCHSIA

compiler-rt/lib/ubsan/ubsan_init_standalone.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
#include "ubsan_init.h"
2020
#include "ubsan_signals_standalone.h"
2121

22+
#if SANITIZER_FUCHSIA
23+
namespace __sanitizer {
24+
// UBSan doesn't need to do anything else special in the startup hook.
25+
void EarlySanitizerInit() {}
26+
} // namespace __sanitizer
27+
#endif // SANITIZER_FUCHSIA
28+
2229
namespace __ubsan {
2330

2431
class UbsanStandaloneInitializer {

0 commit comments

Comments
 (0)