Index: lib/asan/asan_allocator.cc =================================================================== --- lib/asan/asan_allocator.cc +++ lib/asan/asan_allocator.cc @@ -958,15 +958,12 @@ #if !SANITIZER_SUPPORTS_WEAK_HOOKS // Provide default (no-op) implementation of malloc hooks. -extern "C" { -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void __sanitizer_malloc_hook(void *ptr, uptr size) { +WEAK_DEF(void, __sanitizer_malloc_hook, void *ptr, uptr size) { (void)ptr; (void)size; } -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void __sanitizer_free_hook(void *ptr) { + +WEAK_DEF(void, __sanitizer_free_hook, void *ptr) { (void)ptr; } -} // extern "C" #endif Index: lib/asan/asan_flags.cc =================================================================== --- lib/asan/asan_flags.cc +++ lib/asan/asan_flags.cc @@ -186,9 +186,4 @@ } // namespace __asan -#if !SANITIZER_SUPPORTS_WEAK_HOOKS -extern "C" { -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -const char* __asan_default_options() { return ""; } -} // extern "C" -#endif +WEAK_DEF(const char*, __asan_default_options, void) { return ""; } Index: lib/asan/asan_interface_internal.h =================================================================== --- lib/asan/asan_interface_internal.h +++ lib/asan/asan_interface_internal.h @@ -164,13 +164,11 @@ SANITIZER_INTERFACE_ATTRIBUTE void __asan_set_error_report_callback(void (*callback)(const char*)); - SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE - /* OPTIONAL */ void __asan_on_error(); + WEAK_DECL(void, __asan_on_error, void); SANITIZER_INTERFACE_ATTRIBUTE void __asan_print_accumulated_stats(); - SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE - /* OPTIONAL */ const char* __asan_default_options(); + WEAK_DECL(const char*, __asan_default_options, void); SANITIZER_INTERFACE_ATTRIBUTE extern uptr __asan_shadow_memory_dynamic_address; Index: lib/asan/asan_report.cc =================================================================== --- lib/asan/asan_report.cc +++ lib/asan/asan_report.cc @@ -488,9 +488,6 @@ } } // extern "C" -#if !SANITIZER_SUPPORTS_WEAK_HOOKS // Provide default implementation of __asan_on_error that does nothing // and may be overriden by user. -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE -void __asan_on_error() {} -#endif +WEAK_DEF(void, __asan_on_error, void) {} Index: lib/asan/asan_suppressions.cc =================================================================== --- lib/asan/asan_suppressions.cc +++ lib/asan/asan_suppressions.cc @@ -31,15 +31,7 @@ kInterceptorName, kInterceptorViaFunction, kInterceptorViaLibrary, kODRViolation}; -extern "C" { -#if SANITIZER_SUPPORTS_WEAK_HOOKS -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -const char *__asan_default_suppressions(); -#else -// No week hooks, provide empty implementation. -const char *__asan_default_suppressions() { return ""; } -#endif // SANITIZER_SUPPORTS_WEAK_HOOKS -} // extern "C" +WEAK_DEF(const char *, __asan_default_suppressions, void) { return ""; } void InitializeSuppressions() { CHECK_EQ(nullptr, suppression_ctx); Index: lib/asan/asan_win.cc =================================================================== --- lib/asan/asan_win.cc +++ lib/asan/asan_win.cc @@ -43,18 +43,6 @@ __asan_init(); return __asan_shadow_memory_dynamic_address; } - -void __sanitizer_default_malloc_hook(void *ptr, uptr size) { } -void __sanitizer_default_free_hook(void *ptr) { } -const char* __asan_default_default_options() { return ""; } -const char* __asan_default_default_suppressions() { return ""; } -void __asan_default_on_error() {} - -WIN_WEAK_ALIAS(__sanitizer_malloc_hook, __sanitizer_default_malloc_hook) -WIN_WEAK_ALIAS(__sanitizer_free_hook, __sanitizer_default_free_hook) -WIN_WEAK_ALIAS(__asan_default_options, __asan_default_default_options) -WIN_WEAK_ALIAS(__asan_default_suppressions, __asan_default_default_suppressions) -WIN_WEAK_ALIAS(__asan_on_error, __asan_default_on_error) } // extern "C" // ---------------------- Windows-specific interceptors ---------------- {{{ Index: lib/sanitizer_common/sancov_flags.h =================================================================== --- lib/sanitizer_common/sancov_flags.h +++ lib/sanitizer_common/sancov_flags.h @@ -32,8 +32,7 @@ void InitializeSancovFlags(); -extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE const char* -__sancov_default_options(); +WEAK_DECL(const char*, __sancov_default_options, void); } // namespace __sancov Index: lib/sanitizer_common/sancov_flags.cc =================================================================== --- lib/sanitizer_common/sancov_flags.cc +++ lib/sanitizer_common/sancov_flags.cc @@ -15,10 +15,7 @@ #include "sanitizer_flag_parser.h" #include "sanitizer_platform.h" -#if !SANITIZER_LINUX -// other platforms do not have weak symbols out of the box. -extern "C" const char* __sancov_default_options() { return ""; } -#endif +WEAK_DEF(const char*, __sancov_default_options, void) { return ""; } using namespace __sanitizer; Index: lib/sanitizer_common/sanitizer_allocator_interface.h =================================================================== --- lib/sanitizer_common/sanitizer_allocator_interface.h +++ lib/sanitizer_common/sanitizer_allocator_interface.h @@ -33,14 +33,9 @@ void (*malloc_hook)(const void *, uptr), void (*free_hook)(const void *)); -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE - /* OPTIONAL */ void __sanitizer_malloc_hook(void *ptr, uptr size); -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE - /* OPTIONAL */ void __sanitizer_free_hook(void *ptr); - - -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE - void __sanitizer_print_memory_profile(int top_percent); +WEAK_DECL(void, __sanitizer_malloc_hook, void *ptr, uptr size); +WEAK_DECL(void, __sanitizer_free_hook, void *ptr); +WEAK_DECL(void, __sanitizer_print_memory_profile, int top_percent); } // extern "C" #endif // SANITIZER_ALLOCATOR_INTERFACE_H Index: lib/sanitizer_common/sanitizer_common.cc =================================================================== --- lib/sanitizer_common/sanitizer_common.cc +++ lib/sanitizer_common/sanitizer_common.cc @@ -505,9 +505,8 @@ return InstallMallocFreeHooks(malloc_hook, free_hook); } -#if !SANITIZER_GO && !SANITIZER_SUPPORTS_WEAK_HOOKS -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void __sanitizer_print_memory_profile(int top_percent) { +#if !SANITIZER_GO +WEAK_DEF(void, __sanitizer_print_memory_profile, int top_percent) { (void)top_percent; } #endif Index: lib/sanitizer_common/sanitizer_coverage_libcdep.cc =================================================================== --- lib/sanitizer_common/sanitizer_coverage_libcdep.cc +++ lib/sanitizer_common/sanitizer_coverage_libcdep.cc @@ -1016,27 +1016,16 @@ uptr __sanitizer_update_counter_bitset_and_clear_counters(u8 *bitset) { return coverage_data.Update8bitCounterBitsetAndClearCounters(bitset); } + // Default empty implementations (weak). Users should redefine them. -#if !SANITIZER_WINDOWS // weak does not work on Windows. -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void __sanitizer_cov_trace_cmp() {} -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void __sanitizer_cov_trace_cmp1() {} -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void __sanitizer_cov_trace_cmp2() {} -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void __sanitizer_cov_trace_cmp4() {} -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void __sanitizer_cov_trace_cmp8() {} -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void __sanitizer_cov_trace_switch() {} -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void __sanitizer_cov_trace_div4() {} -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void __sanitizer_cov_trace_div8() {} -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void __sanitizer_cov_trace_gep() {} -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void __sanitizer_cov_trace_pc_indir() {} -#endif // !SANITIZER_WINDOWS +WEAK_DEF(void, __sanitizer_cov_trace_cmp, void) {} +WEAK_DEF(void, __sanitizer_cov_trace_cmp1, void) {} +WEAK_DEF(void, __sanitizer_cov_trace_cmp2, void) {} +WEAK_DEF(void, __sanitizer_cov_trace_cmp4, void) {} +WEAK_DEF(void, __sanitizer_cov_trace_cmp8, void) {} +WEAK_DEF(void, __sanitizer_cov_trace_switch, void) {} +WEAK_DEF(void, __sanitizer_cov_trace_div4, void) {} +WEAK_DEF(void, __sanitizer_cov_trace_div8, void) {} +WEAK_DEF(void, __sanitizer_cov_trace_gep, void) {} +WEAK_DEF(void, __sanitizer_cov_trace_pc_indir, void) {} } // extern "C" Index: lib/sanitizer_common/sanitizer_coverage_libcdep_new.cc =================================================================== --- lib/sanitizer_common/sanitizer_coverage_libcdep_new.cc +++ lib/sanitizer_common/sanitizer_coverage_libcdep_new.cc @@ -156,14 +156,12 @@ return __sancov::SanitizerDumpCoverage(pcs, len); } -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void -__sanitizer_cov_trace_pc_guard(u32* guard) { +WEAK_DEF(void, __sanitizer_cov_trace_pc_guard, u32* guard) { if (!*guard) return; __sancov::pc_guard_controller.TracePcGuard(guard, GET_CALLER_PC() - 1); } -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void -__sanitizer_cov_trace_pc_guard_init(u32* start, u32* end) { +WEAK_DEF(void, __sanitizer_cov_trace_pc_guard_init, u32* start, u32* end) { if (start == end || *start) return; __sancov::pc_guard_controller.InitTracePcGuard(start, end); } Index: lib/sanitizer_common/sanitizer_interface_internal.h =================================================================== --- lib/sanitizer_common/sanitizer_interface_internal.h +++ lib/sanitizer_common/sanitizer_interface_internal.h @@ -37,14 +37,14 @@ } __sanitizer_sandbox_arguments; // Notify the tools that the sandbox is going to be turned on. - SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void - __sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments *args); + WEAK_DECL(void, __sanitizer_sandbox_on_notify, + __sanitizer_sandbox_arguments *args); // This function is called by the tool when it has just finished reporting // an error. 'error_summary' is a one-line string that summarizes // the error message. This function can be overridden by the client. - SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE - void __sanitizer_report_error_summary(const char *error_summary); + WEAK_DECL(void, __sanitizer_report_error_summary, + const char *error_summary); SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_init(); SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump(); Index: lib/sanitizer_common/sanitizer_internal_defs.h =================================================================== --- lib/sanitizer_common/sanitizer_internal_defs.h +++ lib/sanitizer_common/sanitizer_internal_defs.h @@ -14,6 +14,7 @@ #define SANITIZER_DEFS_H #include "sanitizer_platform.h" +#include "sanitizer_win_defs.h" #ifndef SANITIZER_DEBUG # define SANITIZER_DEBUG 0 @@ -22,7 +23,6 @@ // Only use SANITIZER_*ATTRIBUTE* before the function return type! #if SANITIZER_WINDOWS # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport) -// FIXME find out what we need on Windows, if anything. # define SANITIZER_WEAK_ATTRIBUTE #elif SANITIZER_GO # define SANITIZER_INTERFACE_ATTRIBUTE @@ -32,11 +32,52 @@ # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak)) #endif -#if (SANITIZER_LINUX || SANITIZER_MAC || SANITIZER_WINDOWS) && !SANITIZER_GO +//--------------------------- WEAK FUNCTIONS ---------------------------------// +// When working with weak functions, to simplify the code and make it more +// portable, when possible define a default implementation using these macros: +// +// WEAK_DEF(, , ) +// Use this macro when you need to define a weak function. For example: +// WEAK_DEF(bool, compare, int a, int b) { return a > b; } +// +// WEAK_DECL(, , ) +// Use this macro when you need to declare and export a weak function that +// includes a default implementation. For example: +// WEAK_DECL(bool, compare, int a, int b); +// +#if SANITIZER_WINDOWS +# define WEAK_DEF(ReturnType, Name, ...) \ + WIN_WEAK_EXPORT_DEF(ReturnType, Name, __VA_ARGS__) +# define WEAK_DECL(ReturnType, Name, ...) \ + WIN_WEAK_EXPORT_DECL(ReturnType, Name, __VA_ARGS__) +#else +# define WEAK_DEF(ReturnType, Name, ...) \ + extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE \ + ReturnType Name(__VA_ARGS__) +# define WEAK_DECL(ReturnType, Name, ...) \ + extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE \ + ReturnType Name(__VA_ARGS__) +#endif + +// SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that +// will evaluate to a null pointer when not defined. +#if (SANITIZER_LINUX || SANITIZER_MAC) && !SANITIZER_GO # define SANITIZER_SUPPORTS_WEAK_HOOKS 1 #else # define SANITIZER_SUPPORTS_WEAK_HOOKS 0 #endif +// For some weak hooks that will be called very often and we want to avoid the +// overhead of executing the default implementation when it is not necessary, +// we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default +// implementation for platforms that doesn't support weak symbols. For example: +// +// #if !SANITIZER_SUPPORT_WEAK_HOOKS +// WEAK_DEF(bool, compare_hook, int a, int b) { return a > b; } +// #endif +// +// And then use it as: if (compare_hook) compare_hook(a, b); +//----------------------------------------------------------------------------// + // We can use .preinit_array section on Linux to call sanitizer initialization // functions very early in the process startup (unless PIC macro is defined). Index: lib/sanitizer_common/sanitizer_printf.cc =================================================================== --- lib/sanitizer_common/sanitizer_printf.cc +++ lib/sanitizer_common/sanitizer_printf.cc @@ -214,15 +214,11 @@ } // Can be overriden in frontend. -#if SANITIZER_SUPPORTS_WEAK_HOOKS -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -void OnPrint(const char *str) { - (void)str; -} -#elif SANITIZER_GO && defined(TSAN_EXTERNAL_HOOKS) -void OnPrint(const char *str); +#if SANITIZER_GO && defined(TSAN_EXTERNAL_HOOKS) +// Implementation must be defined in frontend. +extern "C" void OnPrint(const char *str); #else -void OnPrint(const char *str) { +WEAK_DEF(void, OnPrint, const char *str) { (void)str; } #endif Index: lib/sanitizer_common/sanitizer_win.cc =================================================================== --- lib/sanitizer_common/sanitizer_win.cc +++ lib/sanitizer_common/sanitizer_win.cc @@ -940,15 +940,4 @@ } // namespace __sanitizer -#if !SANITIZER_GO -// Workaround to implement weak hooks on Windows. COFF doesn't directly support -// weak symbols, but it does support /alternatename, which is similar. If the -// user does not override the hook, we will use this default definition instead -// of null. -extern "C" void __sanitizer_print_memory_profile(int top_percent) {} - -WIN_WEAK_ALIAS(__sanitizer_print_memory_profile, - __sanitizer_default_print_memory_profile) -#endif - #endif // _WIN32 Index: lib/sanitizer_common/sanitizer_win_defs.h =================================================================== --- lib/sanitizer_common/sanitizer_win_defs.h +++ lib/sanitizer_common/sanitizer_win_defs.h @@ -29,19 +29,19 @@ // We don't have a direct equivalent of weak symbols when using MSVC, but we can // use the /alternatename directive to tell the linker to default a specific // symbol to a specific value. -// Take into account that the function will be marked as UNDEF in the symbol -// table of the resulting object file, even if we provided a default value, and -// the linker won't find the default implementation until it links with that -// object file. +// Take into account that this is a pragma directive for the linker, so it will +// be ignored by the compiler and the function will be marked as UNDEF in the +// symbol table of the resulting object file. The linker won't find the default +// implementation until it links with that object file. // So, suppose we provide a default implementation "fundef" for "fun", and this -// is compiled into the object file "test.obj". +// is compiled into the object file "test.obj" including the pragma directive. // If we have some code with references to "fun" and we link that code with // "test.obj", it will work because the linker always link object files. // But, if "test.obj" is included in a static library, like "test.lib", then the // liker will only link to "test.obj" if necessary. If we only included the // definition of "fun", it won't link to "test.obj" (from test.lib) because -// "fun" appears as UNDEF, so it doesn't resolve the symbol "fun", and this will -// result in a link error. +// "fun" appears as UNDEF, so it doesn't resolve the symbol "fun", and will +// result in a link error (the linker doesn't find the pragma directive). // So, a workaround is to force linkage with the modules that include weak // definitions, with the following macro: WIN_FORCE_LINK() @@ -55,5 +55,56 @@ #define WIN_FORCE_LINK(Name) \ __pragma(comment(linker, "/include:" WIN_SYM_PREFIX STRINGIFY(Name))) +#define WIN_EXPORT(ExportedName, Name) \ + __pragma(comment(linker, "/export:" WIN_SYM_PREFIX STRINGIFY(ExportedName) \ + "=" WIN_SYM_PREFIX STRINGIFY(Name))) + +// In Windows, can't define weak functions, but we can use WIN_WEAK_ALIAS() +// which defines an alias to a default implementation, and only works when +// linking statically. +// So, to define a weak function "fun", we define a default implementation with +// a different name "fun__def" and we create a "weak alias" fun = fun__def. +// Then, users can override it just defining "fun". +// We impose "extern "C"" because otherwise WIN_WEAK_ALIAS() will fail because +// of name mangling. + +// Dummy name for default implementation of weak function. +# define WEAK_DEFAULT_NAME(Name) Name##__def +// Name for exported implementation of weak function. +# define WEAK_EXPORT_NAME(Name) Name##__dll + +// Use this macro when you need to define and export a weak function from a +// library. For example: +// WIN_WEAK_EXPORT_DEF(bool, compare, int a, int b) { return a > b; } +# define WIN_WEAK_EXPORT_DEF(ReturnType, Name, ...) \ + WIN_WEAK_ALIAS(Name, WEAK_DEFAULT_NAME(Name)) \ + WIN_EXPORT(WEAK_EXPORT_NAME(Name), Name) \ + extern "C" ReturnType Name(__VA_ARGS__); \ + extern "C" ReturnType WEAK_DEFAULT_NAME(Name)(__VA_ARGS__) + +// Use this macro when you need to declare and export a weak function from a +// library. For example: +// WIN_WEAK_EXPORT_DECL(bool, compare, int a, int b); +# define WIN_WEAK_EXPORT_DECL(ReturnType, Name, ...) \ + extern "C" ReturnType Name(__VA_ARGS__); \ + extern "C" __declspec(dllexport) \ + ReturnType WEAK_EXPORT_NAME(Name)(__VA_ARGS__) + +// Use this macro when you need to import a weak function from a library. It +// defines a weak alias to the imported function from the dll. For example: +// WIN_WEAK_IMPORT_DEF(compare); +# define WIN_WEAK_IMPORT_DEF(Name) \ + WIN_WEAK_ALIAS(Name, WEAK_EXPORT_NAME(Name)) + +// So, for Windows we provide something similar to weak symbols in Linux, with +// some differences: +// + Always a default implementation must me provided. +// + When linking dynamically with a library (dll), weak functions are +// exported with "__dll" suffix. If some unit overrides the function +// (redefining fun()), the rest of the units (other dlls, or main executable) +// doesn't have access to it (which is different in linux). When access to the +// implementation in a different unit is required, the strong definition must +// be exported and interception can be used from the rest of the units. + #endif // SANITIZER_WINDOWS #endif // SANITIZER_WIN_DEFS_H Index: lib/ubsan/ubsan_flags.h =================================================================== --- lib/ubsan/ubsan_flags.h +++ lib/ubsan/ubsan_flags.h @@ -39,11 +39,8 @@ } // namespace __ubsan -extern "C" { // Users may provide their own implementation of __ubsan_default_options to // override the default flag values. -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -const char *__ubsan_default_options(); -} // extern "C" +WEAK_DECL(const char *, __ubsan_default_options, void); #endif // UBSAN_FLAGS_H Index: lib/ubsan/ubsan_flags.cc =================================================================== --- lib/ubsan/ubsan_flags.cc +++ lib/ubsan/ubsan_flags.cc @@ -68,19 +68,6 @@ } // namespace __ubsan -extern "C" { - -#if !SANITIZER_SUPPORTS_WEAK_HOOKS -SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE -const char *__ubsan_default_options() { return ""; } -#endif - -#if SANITIZER_WINDOWS -const char *__ubsan_default_default_options() { return ""; } - -WIN_WEAK_ALIAS(__ubsan_default_options, __ubsan_default_default_options) -#endif - -} // extern "C" +WEAK_DEF(const char *, __ubsan_default_options, void) { return ""; } #endif // CAN_SANITIZE_UB