Index: compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc =================================================================== --- compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc +++ compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc @@ -24,7 +24,7 @@ // ASan own interface functions. #define INTERFACE_FUNCTION(Name) INTERCEPT_SANITIZER_FUNCTION(Name) -#define INTERFACE_WEAK_FUNCTION(Name) +#define INTERFACE_WEAK_FUNCTION(Name) INTERCEPT_SANITIZER_WEAK_FUNCTION(Name) #include "asan_interface.inc" // Memory allocation functions. Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_win_dll_thunk.cc =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_win_dll_thunk.cc +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_win_dll_thunk.cc @@ -16,6 +16,6 @@ #include "sanitizer_win_dll_thunk.h" // Sanitizer Coverage interface functions. #define INTERFACE_FUNCTION(Name) INTERCEPT_SANITIZER_FUNCTION(Name) -#define INTERFACE_WEAK_FUNCTION(Name) +#define INTERFACE_WEAK_FUNCTION(Name) INTERCEPT_SANITIZER_WEAK_FUNCTION(Name) #include "sanitizer_coverage_interface.inc" #endif // SANITIZER_DLL_THUNK Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_win_dll_thunk.h =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_win_dll_thunk.h +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_win_dll_thunk.h @@ -19,6 +19,9 @@ uptr dllThunkGetRealAddrOrDie(const char *name); int dllThunkIntercept(const char* main_function, uptr dll_function); + +int dllThunkInterceptWhenPossible(const char* main_function, + const char* default_function, uptr dll_function); } extern "C" int __dll_thunk_init(); @@ -34,6 +37,17 @@ __declspec(allocate(".DLLTH$M")) int (*__dll_thunk_##dll_function)() = \ intercept_##dll_function; +// Try to override dll_function with main_function from main executable. +// If main_function is not present, override dll_function with default_function. +#define INTERCEPT_WHEN_POSSIBLE(main_function, default_function, dll_function) \ + static int intercept_##dll_function() { \ + return __sanitizer::dllThunkInterceptWhenPossible(main_function, \ + default_function, (__sanitizer::uptr)dll_function); \ + } \ + __pragma(section(".DLLTH$M", long, read)) \ + __declspec(allocate(".DLLTH$M")) int (*__dll_thunk_##dll_function)() = \ + intercept_##dll_function; + // -------------------- Function interception macros ------------------------ // // Special case of hooks -- ASan own interface functions. Those are only called // after __asan_init, thus an empty implementation is sufficient. @@ -44,6 +58,17 @@ } \ INTERCEPT_OR_DIE(#name, name) +// Special case of hooks -- Weak functions, could be redefined in the main +// executable, but that is not necessary, so we shouldn't die if we can not find +// a reference. Instead, when the function is not present in the main executable +// we consider the default impl provided by asan library. +#define INTERCEPT_SANITIZER_WEAK_FUNCTION(name) \ + extern "C" __declspec(noinline) void name() { \ + volatile int prevent_icf = (__LINE__ << 8); (void)prevent_icf; \ + __debugbreak(); \ + } \ + INTERCEPT_WHEN_POSSIBLE(#name, STRINGIFY(WEAK_EXPORT_NAME(name)), name) + // We can't define our own version of strlen etc. because that would lead to // link-time or even type mismatch errors. Instead, we can declare a function // just to be able to get its address. Me may miss the first few calls to the Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_win_dll_thunk.cc =================================================================== --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_win_dll_thunk.cc +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_win_dll_thunk.cc @@ -37,11 +37,22 @@ abort(); return 0; } + +int dllThunkInterceptWhenPossible(const char* main_function, + const char* default_function, uptr dll_function) { + uptr wrapper = __interception::InternalGetProcAddress( + (void *)GetModuleHandleA(0), main_function); + if (!wrapper) + wrapper = dllThunkGetRealAddrOrDie(default_function); + if (!__interception::OverrideFunction(dll_function, wrapper, 0)) + abort(); + return 0; +} } // namespace __sanitizer // Include Sanitizer Common interface. #define INTERFACE_FUNCTION(Name) INTERCEPT_SANITIZER_FUNCTION(Name) -#define INTERFACE_WEAK_FUNCTION(Name) +#define INTERFACE_WEAK_FUNCTION(Name) INTERCEPT_SANITIZER_WEAK_FUNCTION(Name) #include "sanitizer_common_interface.inc" #pragma section(".DLLTH$A", read) // NOLINT Index: compiler-rt/trunk/lib/ubsan/ubsan_win_dll_thunk.cc =================================================================== --- compiler-rt/trunk/lib/ubsan/ubsan_win_dll_thunk.cc +++ compiler-rt/trunk/lib/ubsan/ubsan_win_dll_thunk.cc @@ -16,6 +16,6 @@ #include "sanitizer_common/sanitizer_win_dll_thunk.h" // Ubsan interface functions. #define INTERFACE_FUNCTION(Name) INTERCEPT_SANITIZER_FUNCTION(Name) -#define INTERFACE_WEAK_FUNCTION(Name) +#define INTERFACE_WEAK_FUNCTION(Name) INTERCEPT_SANITIZER_WEAK_FUNCTION(Name) #include "ubsan_interface.inc" #endif // SANITIZER_DLL_THUNK