Skip to content

release/19.x: [lsan] Fix free(NULL) interception during initialization #121100

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

Merged
merged 3 commits into from
Jan 13, 2025

Conversation

llvmbot
Copy link
Member

@llvmbot llvmbot commented Dec 25, 2024

Backport 1797174 ae0ed3d d9ed8b0

Requested by: @nikic

@llvmbot
Copy link
Member Author

llvmbot commented Dec 25, 2024

@tmiasko @vitalybuka @vitalybuka What do you think about merging this PR to the release branch?

@llvmbot
Copy link
Member Author

llvmbot commented Dec 25, 2024

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

Author: None (llvmbot)

Changes

Backport 1797174 ae0ed3d d9ed8b0

Requested by: @nikic


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

2 Files Affected:

  • (modified) compiler-rt/lib/lsan/lsan_interceptors.cpp (+2)
  • (added) compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c (+60)
diff --git a/compiler-rt/lib/lsan/lsan_interceptors.cpp b/compiler-rt/lib/lsan/lsan_interceptors.cpp
index b569c337e97641..efbf2fdfb0ab3f 100644
--- a/compiler-rt/lib/lsan/lsan_interceptors.cpp
+++ b/compiler-rt/lib/lsan/lsan_interceptors.cpp
@@ -77,6 +77,8 @@ INTERCEPTOR(void*, malloc, uptr size) {
 }
 
 INTERCEPTOR(void, free, void *p) {
+  if (UNLIKELY(!p))
+    return;
   if (DlsymAlloc::PointerIsMine(p))
     return DlsymAlloc::Free(p);
   ENSURE_LSAN_INITED;
diff --git a/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c b/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c
new file mode 100644
index 00000000000000..7b5b9cf34a90f9
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/dlsym_alloc.c
@@ -0,0 +1,60 @@
+// RUN: %clang -O0 %s -o %t && %run %t
+
+// FIXME: TSAN does not use DlsymAlloc.
+// UNSUPPORTED: tsan
+// FIXME: investigate why this fails on macos
+// UNSUPPORTED: darwin
+
+#include <stdlib.h>
+
+const char *test() __attribute__((disable_sanitizer_instrumentation)) {
+  void *volatile p = malloc(3);
+  p = realloc(p, 7);
+  free(p);
+
+  p = calloc(3, 7);
+  free(p);
+
+  free(NULL);
+
+  return "";
+}
+
+const char *__asan_default_options()
+    __attribute__((disable_sanitizer_instrumentation)) {
+  return test();
+}
+const char *__hwasan_default_options()
+    __attribute__((disable_sanitizer_instrumentation)) {
+  return test();
+}
+const char *__lsan_default_options()
+    __attribute__((disable_sanitizer_instrumentation)) {
+  return test();
+}
+const char *__memprof_default_options()
+    __attribute__((disable_sanitizer_instrumentation)) {
+  return test();
+}
+const char *__msan_default_options()
+    __attribute__((disable_sanitizer_instrumentation)) {
+  return test();
+}
+const char *__nsan_default_options()
+    __attribute__((disable_sanitizer_instrumentation)) {
+  return test();
+}
+const char *__rtsan_default_options()
+    __attribute__((disable_sanitizer_instrumentation)) {
+  return test();
+}
+const char *__tsan_default_options()
+    __attribute__((disable_sanitizer_instrumentation)) {
+  return test();
+}
+const char *__ubsan_default_options()
+    __attribute__((disable_sanitizer_instrumentation)) {
+  return test();
+}
+
+int main(int argc, char **argv) { return 0; }

@nikic nikic changed the title release/19.x: [test][compiler-rt] Mark dlsym_alloc.c as unsupported on macos (#108439) release/19.x: [lsan] Fix free(NULL) interception during initialization Dec 25, 2024
@tru tru merged commit 07b4f63 into llvm:release/19.x Jan 13, 2025
4 of 6 checks passed
vitalybuka and others added 3 commits January 13, 2025 11:53
Almost all sanitizers already support the test.
* Tsan does not use DlsymAlloc yet.
* Lsan will support with llvm#106912.

memprof,rtsan,nsan are not tested as part of
sanitizer_common, but we should keep them here to
show up when it happen.

---------

Co-authored-by: Xiaofeng Tian <[email protected]>
(cherry picked from commit 1797174)
Previously an attempt to free a null pointer during initialization would
fail on ENSURE_LSAN_INITED assertion (since a null pointer is not owned
by DlsymAlloc).

(cherry picked from commit ae0ed3d)
Copy link

@nikic (or anyone else). If you would like to add a note about this fix in the release notes (completely optional). Please reply to this comment with a one or two sentence description of the fix. When you are done, please add the release:note label to this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

5 participants