Skip to content

msan: Support free_sized and free_aligned_sized from C23 #144529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jcking
Copy link
Contributor

@jcking jcking commented Jun 17, 2025

Adds support to MSan for free_sized and free_aligned_sized from C23.

Other sanitizers will be handled with their own separate PRs.

For #144435

@vitalybuka
Copy link
Collaborator

vitalybuka commented Jun 17, 2025

I see no test changes?

@vitalybuka vitalybuka self-requested a review June 17, 2025 17:23
@vitalybuka
Copy link
Collaborator

There are test/sanitizer_common
they run with most of sanitizers, same test, it's a part of ninja check-compiler-rt
You can create a single test, but looks like you will have to land it after all fixes :)

@jcking
Copy link
Contributor Author

jcking commented Jun 17, 2025

Moving back to draft until #144604 lands.

@jcking jcking marked this pull request as draft June 17, 2025 21:15
@vitalybuka vitalybuka self-requested a review June 19, 2025 00:14
@vitalybuka
Copy link
Collaborator

Just for visibility so I don't forget to merge with approval.

@jcking jcking force-pushed the msan_free_aligned_sized branch 2 times, most recently from d54fcb1 to 300e88e Compare June 19, 2025 00:44
@jcking jcking marked this pull request as ready for review June 19, 2025 00:46
@llvmbot
Copy link
Member

llvmbot commented Jun 19, 2025

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Justin King (jcking)

Changes

Adds support to MSan for free_sized and free_aligned_sized from C23.

Other sanitizers will be handled with their own separate PRs.

For #144435


Full diff: https://github.com/llvm/llvm-project/pull/144529.diff

3 Files Affected:

  • (modified) compiler-rt/lib/msan/msan_interceptors.cpp (+31)
  • (modified) compiler-rt/test/sanitizer_common/TestCases/Linux/free_aligned_sized.c (+1-1)
  • (modified) compiler-rt/test/sanitizer_common/TestCases/Linux/free_sized.c (+1-1)
diff --git a/compiler-rt/lib/msan/msan_interceptors.cpp b/compiler-rt/lib/msan/msan_interceptors.cpp
index f94d3cb79aa00..8eda43b89432c 100644
--- a/compiler-rt/lib/msan/msan_interceptors.cpp
+++ b/compiler-rt/lib/msan/msan_interceptors.cpp
@@ -215,6 +215,35 @@ INTERCEPTOR(void, free, void *ptr) {
   MsanDeallocate(&stack, ptr);
 }
 
+#if SANITIZER_INTERCEPT_FREE_SIZED
+INTERCEPTOR(void, free_sized, void *ptr, uptr size) {
+  if (UNLIKELY(!ptr))
+    return;
+  if (DlsymAlloc::PointerIsMine(ptr))
+    return DlsymAlloc::Free(ptr);
+  GET_MALLOC_STACK_TRACE;
+  MsanDeallocate(&stack, ptr);
+}
+#  define MSAN_MAYBE_INTERCEPT_FREE_SIZED INTERCEPT_FUNCTION(free_sized)
+#else
+#  define MSAN_MAYBE_INTERCEPT_FREE_SIZED
+#endif
+
+#if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
+INTERCEPTOR(void, free_aligned_sized, void *ptr, uptr alignment, uptr size) {
+  if (UNLIKELY(!ptr))
+    return;
+  if (DlsymAlloc::PointerIsMine(ptr))
+    return DlsymAlloc::Free(ptr);
+  GET_MALLOC_STACK_TRACE;
+  MsanDeallocate(&stack, ptr);
+}
+#  define MSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED \
+    INTERCEPT_FUNCTION(free_aligned_sized)
+#else
+#  define MSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED
+#endif
+
 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
 INTERCEPTOR(void, cfree, void *ptr) {
   if (UNLIKELY(!ptr))
@@ -1775,6 +1804,8 @@ void InitializeInterceptors() {
   INTERCEPT_FUNCTION(realloc);
   INTERCEPT_FUNCTION(reallocarray);
   INTERCEPT_FUNCTION(free);
+  MSAN_MAYBE_INTERCEPT_FREE_SIZED;
+  MSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED;
   MSAN_MAYBE_INTERCEPT_CFREE;
   MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE;
   MSAN_MAYBE_INTERCEPT_MALLINFO;
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/free_aligned_sized.c b/compiler-rt/test/sanitizer_common/TestCases/Linux/free_aligned_sized.c
index e9cb6f20c5ead..7710c62368191 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Linux/free_aligned_sized.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/free_aligned_sized.c
@@ -1,5 +1,5 @@
 // RUN: %clang -std=c23 -O0 %s -o %t && %run %t
-// UNSUPPORTED: asan, hwasan, rtsan, tsan, msan, ubsan
+// UNSUPPORTED: asan, hwasan, rtsan, tsan, ubsan
 
 #include <stddef.h>
 #include <stdlib.h>
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/free_sized.c b/compiler-rt/test/sanitizer_common/TestCases/Linux/free_sized.c
index 8cdf3216e528a..9eac562fecb03 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Linux/free_sized.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/free_sized.c
@@ -1,5 +1,5 @@
 // RUN: %clang -std=c23 -O0 %s -o %t && %run %t
-// UNSUPPORTED: asan, hwasan, rtsan, tsan, msan, ubsan
+// UNSUPPORTED: asan, hwasan, rtsan, tsan, ubsan
 
 #include <stddef.h>
 #include <stdlib.h>

@jcking
Copy link
Contributor Author

jcking commented Jun 19, 2025

Moving back from non-draft and updated based on the guarded approach from LSan.

Signed-off-by: Justin King <jcking@google.com>
@jcking jcking force-pushed the msan_free_aligned_sized branch from 300e88e to 961ac58 Compare June 19, 2025 01:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants