Index: lib/asan/asan_interceptors.cc =================================================================== --- lib/asan/asan_interceptors.cc +++ lib/asan/asan_interceptors.cc @@ -117,13 +117,18 @@ #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \ ASAN_WRITE_RANGE(ptr, size) #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) ASAN_READ_RANGE(ptr, size) +// On FreeBSD intercepted function may be called before the references to the +// real functions are initialized. #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \ do { \ + if (SANITIZER_FREEBSD && UNLIKELY(!asan_interceptors_inited)) \ + InitializeAsanInterceptors(); \ if (asan_init_is_running) \ return REAL(func)(__VA_ARGS__); \ ctx = 0; \ (void) ctx; \ - if (SANITIZER_MAC && UNLIKELY(!asan_inited)) \ + if ((SANITIZER_MAC != 0 || SANITIZER_FREEBSD != 0) && \ + UNLIKELY(!asan_inited)) \ return REAL(func)(__VA_ARGS__); \ ENSURE_ASAN_INITED(); \ } while (false) @@ -736,9 +741,8 @@ // ---------------------- InitializeAsanInterceptors ---------------- {{{1 namespace __asan { void InitializeAsanInterceptors() { - static bool was_called_once; - CHECK(was_called_once == false); - was_called_once = true; + if (asan_interceptors_inited) return; + InitializeCommonInterceptors(); // Intercept mem* functions. @@ -827,6 +831,8 @@ InitializeWindowsInterceptors(); #endif + asan_interceptors_inited = true; + VReport(1, "AddressSanitizer: libc interceptors initialized\n"); } Index: lib/asan/asan_internal.h =================================================================== --- lib/asan/asan_internal.h +++ lib/asan/asan_internal.h @@ -123,6 +123,7 @@ extern int asan_inited; // Used to avoid infinite recursion in __asan_init(). extern bool asan_init_is_running; +extern bool asan_interceptors_inited; extern void (*death_callback)(void); // These magic values are written to shadow for better error reporting. Index: lib/asan/asan_rtl.cc =================================================================== --- lib/asan/asan_rtl.cc +++ lib/asan/asan_rtl.cc @@ -322,6 +322,7 @@ // -------------------------- Globals --------------------- {{{1 int asan_inited; bool asan_init_is_running; +bool asan_interceptors_inited; void (*death_callback)(void); #if !ASAN_FIXED_MAPPING