[compiler-rt] Draft changes to demo free_sized test (PR #154228)

Snehasish Kumar via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 18 16:51:55 PDT 2025


https://github.com/snehasish created https://github.com/llvm/llvm-project/pull/154228

None

>From 0d6dcf704ca461d5f673c2a03b317098b3f965bf Mon Sep 17 00:00:00 2001
From: Snehasish Kumar <snehasishk at google.com>
Date: Mon, 18 Aug 2025 23:51:27 +0000
Subject: [PATCH] Draft changes to demo free_sized test

---
 .../lib/memprof/memprof_malloc_linux.cpp       | 18 ++++++++++++++++++
 .../TestCases/memprof_histogram_uint8.cpp      |  4 +++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/lib/memprof/memprof_malloc_linux.cpp b/compiler-rt/lib/memprof/memprof_malloc_linux.cpp
index 2a028c7d0b489..68fe65475889a 100644
--- a/compiler-rt/lib/memprof/memprof_malloc_linux.cpp
+++ b/compiler-rt/lib/memprof/memprof_malloc_linux.cpp
@@ -50,6 +50,24 @@ INTERCEPTOR(void, cfree, void *ptr) {
 }
 #endif // SANITIZER_INTERCEPT_CFREE
 
+#if SANITIZER_INTERCEPT_FREE_SIZED
+INTERCEPTOR(void, free_sized, void *ptr, uptr size) {
+  if (DlsymAlloc::PointerIsMine(ptr))
+    return DlsymAlloc::Free(ptr);
+  GET_STACK_TRACE_FREE;
+  memprof_delete(ptr, size, 0, &stack, FROM_MALLOC);
+}
+#endif // SANITIZER_INTERCEPT_FREE_SIZED
+
+#if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
+INTERCEPTOR(void, free_aligned_sized, void *ptr, uptr alignment, uptr size) {
+  if (DlsymAlloc::PointerIsMine(ptr))
+    return DlsymAlloc::Free(ptr);
+  GET_STACK_TRACE_FREE;
+  memprof_delete(ptr, size, alignment, &stack, FROM_MALLOC);
+}
+#endif // SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
+
 INTERCEPTOR(void *, malloc, uptr size) {
   if (DlsymAlloc::Use())
     return DlsymAlloc::Allocate(size);
diff --git a/compiler-rt/test/memprof/TestCases/memprof_histogram_uint8.cpp b/compiler-rt/test/memprof/TestCases/memprof_histogram_uint8.cpp
index ef6fb33ffeeaa..5d6be314df32d 100644
--- a/compiler-rt/test/memprof/TestCases/memprof_histogram_uint8.cpp
+++ b/compiler-rt/test/memprof/TestCases/memprof_histogram_uint8.cpp
@@ -8,6 +8,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+extern "C" void free_sized(void *p, size_t size);
+
 int main() {
   // Allocate memory that will create a histogram
   char *buffer = (char *)malloc(1024);
@@ -28,7 +30,7 @@ int main() {
   }
 
   // Free the memory to trigger MIB creation with histogram
-  free(buffer);
+  free_sized(buffer, 1024);
 
   printf("Test completed successfully\n");
   return 0;



More information about the llvm-commits mailing list