diff --git a/compiler-rt/lib/lsan/lsan_allocator.cpp b/compiler-rt/lib/lsan/lsan_allocator.cpp --- a/compiler-rt/lib/lsan/lsan_allocator.cpp +++ b/compiler-rt/lib/lsan/lsan_allocator.cpp @@ -65,12 +65,14 @@ m->stack_trace_id = StackDepotPut(stack); m->requested_size = size; atomic_store(reinterpret_cast(m), 1, memory_order_relaxed); + RunMallocHooks(p, size); } static void RegisterDeallocation(void *p) { if (!p) return; ChunkMetadata *m = Metadata(p); CHECK(m); + RunFreeHooks(p); atomic_store(reinterpret_cast(m), 0, memory_order_relaxed); } @@ -104,7 +106,6 @@ if (cleared && allocator.FromPrimary(p)) memset(p, 0, size); RegisterAllocation(stack, p, size); - RunMallocHooks(p, size); return p; } @@ -119,7 +120,6 @@ } void Deallocate(void *p) { - RunFreeHooks(p); RegisterDeallocation(p); allocator.Deallocate(GetAllocatorCache(), p); } diff --git a/compiler-rt/test/sanitizer_common/TestCases/malloc_hook.cpp b/compiler-rt/test/sanitizer_common/TestCases/malloc_hook.cpp --- a/compiler-rt/test/sanitizer_common/TestCases/malloc_hook.cpp +++ b/compiler-rt/test/sanitizer_common/TestCases/malloc_hook.cpp @@ -21,7 +21,7 @@ // Note: avoid calling functions that allocate memory in malloc/free // to avoid infinite recursion. void __sanitizer_malloc_hook(const volatile void *ptr, size_t sz) { - if (__sanitizer_get_ownership(ptr) && sz == 4) { + if (__sanitizer_get_ownership(ptr) && sz == sizeof(int)) { WRITE("MallocHook\n"); global_ptr = ptr; } @@ -42,7 +42,7 @@ // TLS shadow for function parameters before calling operator // new and, eventually, user-provided hook. __attribute__((noinline)) void allocate(int *unused1, int *unused2) { - x = new int; + x = reinterpret_cast(malloc(sizeof(int))); } int main() { @@ -57,9 +57,17 @@ if (global_ptr != (void*)x) { _exit(1); } - *x = 0; - delete x; - // CHECK: FreeHook + + // Check that realloc invokes hooks + // We realloc to 128 here to avoid potential oversizing by allocators + // making this a no-op. + x = reinterpret_cast(realloc((int *)x, sizeof(int) * 128)); + // CHECK-DAG: FreeHook{{[[:space:]].*}}FH1{{[[:space:]].*}}FH2 + // CHECK-DAG: MH1{{[[:space:]].*}}MH2 + + x[0] = 0; + x[127] = -1; + free((void *)x); // CHECK: FH1 // CHECK: FH2 return 0;