[compiler-rt] [compiler-rt][memprof] adding free_sized/free_aligned_sized intercept… (PR #154011)
David CARLIER via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 19 16:01:42 PDT 2025
https://github.com/devnexen updated https://github.com/llvm/llvm-project/pull/154011
>From 4658451a9fc63664857e90839ef0c7e361ed56b4 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Sun, 17 Aug 2025 12:18:27 +0100
Subject: [PATCH] [compiler-rt][memprof] adding free_sized/free_aligned_sized
interceptions.
---
.../lib/memprof/memprof_malloc_linux.cpp | 18 ++++++++++++++++
.../test/memprof/TestCases/free_sized.cpp | 21 +++++++++++++++++++
2 files changed, 39 insertions(+)
create mode 100644 compiler-rt/test/memprof/TestCases/free_sized.cpp
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/free_sized.cpp b/compiler-rt/test/memprof/TestCases/free_sized.cpp
new file mode 100644
index 0000000000000..88cea822ceb6e
--- /dev/null
+++ b/compiler-rt/test/memprof/TestCases/free_sized.cpp
@@ -0,0 +1,21 @@
+// RUN: %clangxx_memprof %s -o %t
+
+// RUN: %env_memprof_opts=print_text=true:log_path=stdout %run %t | FileCheck %s
+
+#include <stdlib.h>
+#include <unistd.h>
+
+extern "C" {
+ void free_sized(void *ptr, size_t size);
+ void free_aligned_sized(void *ptr, size_t alignement, size_t size);
+}
+
+int main() {
+ void *p = aligned_alloc(16, 32);
+ free_aligned_sized(p, 16, 32);
+ p = malloc(10);
+ free_sized(p, 10);
+ return 0;
+}
+// CHECK: Memory allocation stack id
+// CHECK: Memory allocation stack id
More information about the llvm-commits
mailing list