Skip to content
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

[Darwin][ASan][Test] Create a unoptimized wrapper function in unsanitized dylib for reliable suppression in test. #131906

Merged

Conversation

padriff
Copy link
Contributor

@padriff padriff commented Mar 18, 2025

CFStringCreateWithBytes may not always appear on stack due to optimizations.

Create a wrapper function for the purposes of testing suppression files that will always appear on stack for test stability.

Test should be suppressing ASan for a function outside of sanitized code.
Update function to be extern "C" to match function decoration in original framework and avoid the leak caused by DemangleCXXABI.

rdar://144800068

…for parity with original configuration.

Test should be suppressing ASan for a function outside of sanitized code.
Update function to be extern "C" to match function decoration in original framework.

rdar://144800068
@llvmbot
Copy link
Member

llvmbot commented Mar 18, 2025

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

Author: Paddy McDonald (padriff)

Changes

Test should be suppressing ASan for a function outside of sanitized code.
Update function to be extern "C" to match function decoration in original framework.

rdar://144800068


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

1 Files Affected:

  • (modified) compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp (+20-2)
diff --git a/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp b/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp
index be0a2b1aec516..651d0c5d05b07 100644
--- a/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp
+++ b/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp
@@ -1,5 +1,9 @@
+// Compile the intermediate function to a dylib without -fsanitize to avoid
+// suppressing symbols in sanitized code.
+// RUN: %clangxx -O0 -DSHARED_LIB %s -dynamiclib -o %t.dylib -framework Foundation
+
 // Check that without suppressions, we catch the issue.
-// RUN: %clangxx_asan -O0 %s -o %t -framework Foundation
+// RUN: %clangxx_asan -O0 %s -o %t -framework Foundation %t.dylib
 // RUN: not %run %t 2>&1 | FileCheck --check-prefix=CHECK-CRASH %s
 
 // Check that suppressing a function name works within a no-fork sandbox
@@ -13,23 +17,37 @@
 
 #include <CoreFoundation/CoreFoundation.h>
 
+#if defined(SHARED_LIB)
+
+extern "C" {
 // Disable optimizations to ensure that this function appears on the stack trace so our
 // configured suppressions `interceptor_via_fun:createCFString` can take effect.
-__attribute__((noinline, disable_tail_calls)) CFStringRef
+__attribute__((disable_tail_calls)) CFStringRef
 createCFString(const unsigned char *bytes, CFIndex length) {
   return CFStringCreateWithBytes(kCFAllocatorDefault, bytes, length,
                                  kCFStringEncodingUTF8, FALSE);
 }
+}
+
+#else
+
+extern "C" {
+CFStringRef createCFString(const unsigned char *bytes, CFIndex length);
+}
 
 int main() {
   char *a = (char *)malloc(6);
   strcpy(a, "hello");
+  // Intentional out-of-bounds access that will be caught unless an ASan suppression is provided.
   CFStringRef str = createCFString((unsigned char *)a, 10); // BOOM
+  // If this is printed to stderr then the ASan suppression has worked.
   fprintf(stderr, "Ignored.\n");
   free(a);
   CFRelease(str);
 }
 
+#endif
+
 // CHECK-CRASH: AddressSanitizer: heap-buffer-overflow
 // CHECK-CRASH-NOT: Ignored.
 // CHECK-IGNORE-NOT: AddressSanitizer: heap-buffer-overflow

@padriff padriff changed the title [Darwin][ASan][Test] Move test wrapper function to unsanitized dylib for parity with original configuration. [Darwin][ASan][Test] Create a unoptimized wrapper function in unsanitized dylib for reliable suppression in test. Mar 18, 2025
Copy link
Contributor

@thetruestblue thetruestblue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable and future-proof... and follows patterns of similar tests...

Might be the most commented test in the suite now.

I'm fine with this change I suppose. It feels a bit imperfect from where the test was originally but keeps the spirit of the test and future proofs it. I like it.

My assumption is this has been tested now in a variety of OSes and looks good/confirmed working?

@padriff
Copy link
Contributor Author

padriff commented Mar 19, 2025

We still have the issue with running this and other tests on Apple silicon due to LSan being very noisy, but I've run this on Intel hardware on a number of OS versions.

@thetruestblue
Copy link
Contributor

thetruestblue commented Mar 20, 2025

We still have the issue with running this and other tests on Apple silicon due to LSan being very noisy, but I've run this on Intel hardware on a number of OS versions.

Please still test this on Apple Silicon before merging.

I merged a change that should stop running leak detection for asan tests on non-x86_64 so that shouldn't be true anymore. Please let me know if you have LSan noise on AS, since you should not.

@padriff
Copy link
Contributor Author

padriff commented Mar 22, 2025

Tested on Apple Silicon and it passes

@thetruestblue thetruestblue merged commit 4517a33 into llvm:main Mar 22, 2025
10 checks passed
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