[compiler-rt] 90418dc - [lsan] Invoke hooks on realloc
Jin Xin Ng via llvm-commits
llvm-commits at lists.llvm.org
Tue May 23 09:44:11 PDT 2023
Author: Jin Xin Ng
Date: 2023-05-23T16:42:07Z
New Revision: 90418dc95ec1cd5aeb844d93f055b94759e2c408
URL: https://github.com/llvm/llvm-project/commit/90418dc95ec1cd5aeb844d93f055b94759e2c408
DIFF: https://github.com/llvm/llvm-project/commit/90418dc95ec1cd5aeb844d93f055b94759e2c408.diff
LOG: [lsan] Invoke hooks on realloc
Previously lsan would not invoke hooks on reallocations.
An accompanying regression test is included in sanitizer_common.
This change also moves hook calls to a location where subsequent
calls (via an external caller) to __sanitizer_get_allocated_size
via hooks will return a valid size.
This allows a faster version of __sanitizer_get_allocated_size
to be implemented, which can skip checks.
Test to ensure RunFreeHooks' call order will come with
__sanitizer_get_allocated_size_fast
Differential Revision: https://reviews.llvm.org/D151175
Added:
Modified:
compiler-rt/lib/lsan/lsan_allocator.cpp
compiler-rt/test/sanitizer_common/TestCases/malloc_hook.cpp
Removed:
################################################################################
diff --git a/compiler-rt/lib/lsan/lsan_allocator.cpp b/compiler-rt/lib/lsan/lsan_allocator.cpp
index 8dfd4aa434943..045bf76ae0885 100644
--- a/compiler-rt/lib/lsan/lsan_allocator.cpp
+++ b/compiler-rt/lib/lsan/lsan_allocator.cpp
@@ -65,12 +65,14 @@ static void RegisterAllocation(const StackTrace &stack, void *p, uptr size) {
m->stack_trace_id = StackDepotPut(stack);
m->requested_size = size;
atomic_store(reinterpret_cast<atomic_uint8_t *>(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<atomic_uint8_t *>(m), 0, memory_order_relaxed);
}
@@ -104,7 +106,6 @@ void *Allocate(const StackTrace &stack, uptr size, uptr alignment,
if (cleared && allocator.FromPrimary(p))
memset(p, 0, size);
RegisterAllocation(stack, p, size);
- RunMallocHooks(p, size);
return p;
}
@@ -119,7 +120,6 @@ static void *Calloc(uptr nmemb, uptr size, const StackTrace &stack) {
}
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
index 123910818abda..076f5fc77a4e4 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/malloc_hook.cpp
+++ b/compiler-rt/test/sanitizer_common/TestCases/malloc_hook.cpp
@@ -21,7 +21,7 @@ const volatile void *global_ptr;
// 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 @@ void FreeHook2(const volatile void *ptr) { WRITE("FH2\n"); }
// 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<int *>(malloc(sizeof(int)));
}
int main() {
@@ -57,9 +57,17 @@ int main() {
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<int *>(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;
More information about the llvm-commits
mailing list