Skip to content

[Darwin][ASan][Test] Create a noinlined wrapper function for reliable suppression in test. #131247

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

Conversation

padriff
Copy link
Contributor

@padriff padriff commented Mar 14, 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.

Also necessary to disable leaks to pass on Apple Silicon.

rdar://144800068

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Mar 14, 2025

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

Author: Paddy McDonald (padriff)

Changes

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.

Also necessary to disable leaks to pass on Apple Silicon.

rdar://144800068


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

1 Files Affected:

  • (modified) compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp (+10-5)
diff --git a/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp b/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp
index 966f21346e4fe..50dfd1e2e332b 100644
--- a/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp
+++ b/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp
@@ -3,8 +3,8 @@
 // RUN: not %run %t 2>&1 | FileCheck --check-prefix=CHECK-CRASH %s
 
 // Check that suppressing a function name works within a no-fork sandbox
-// RUN: echo "interceptor_via_fun:CFStringCreateWithBytes" > %t.supp
-// RUN: %env_asan_opts=suppressions='"%t.supp"' \
+// RUN: echo "interceptor_via_fun:createCFString" > %t.supp
+// RUN: %env_asan_opts=suppressions='"%t.supp"':verbostiy=1:  \
 // RUN:   sandbox-exec -p '(version 1)(allow default)(deny process-fork)' \
 // RUN:   %run %t 2>&1 | FileCheck --check-prefix=CHECK-IGNORE %s
 
@@ -13,12 +13,17 @@
 
 #include <CoreFoundation/CoreFoundation.h>
 
+// Use a noinline + disable_tail_calls wrapper function to suppress to stabilize test.
+__attribute__((noinline)) __attribute__((disable_tail_calls)) CFStringRef
+createCFString(const unsigned char *bytes, CFIndex length) {
+  return CFStringCreateWithBytes(kCFAllocatorDefault, bytes, length,
+                                 kCFStringEncodingUTF8, FALSE);
+}
+
 int main() {
   char *a = (char *)malloc(6);
   strcpy(a, "hello");
-  CFStringRef str =
-      CFStringCreateWithBytes(kCFAllocatorDefault, (unsigned char *)a, 10,
-                              kCFStringEncodingUTF8, FALSE);  // BOOM
+  CFStringRef str = createCFString((unsigned char *)a, 10); // BOOM
   fprintf(stderr, "Ignored.\n");
   free(a);
   CFRelease(str);

@@ -13,12 +13,17 @@

#include <CoreFoundation/CoreFoundation.h>

// Use a noinline + disable_tail_calls wrapper function to suppress to stabilize test.
__attribute__((noinline)) __attribute__((disable_tail_calls)) CFStringRef
Copy link
Contributor

@thetruestblue thetruestblue Mar 14, 2025

Choose a reason for hiding this comment

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

nit: __attribute__((noinline, disable_tail_calls))

also this feels a bit like overkill.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The two optimizations that could break the test are inlining of the function and if the function were optimized to tail call to the callee. Admittedly I don't think the current optimizer will add a tail call as the callee is external to the binary, but if we are explicitly disabling one that can break it then shouldn't we do both?

@thetruestblue
Copy link
Contributor

approved, but please address nits

… suppression in test.

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.

Also necessary to disable leaks to pass on Apple Silicon.

rdar://144800068
@padriff padriff force-pushed the eng/padriff/Fix-ASan-suppressions-sandbox branch from e7e01c3 to f3f7543 Compare March 14, 2025 05:16
Copy link
Collaborator

@yln yln left a comment

Choose a reason for hiding this comment

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

LGTM, with nits, thanks!

@thetruestblue thetruestblue merged commit 78f74f6 into llvm:main Mar 14, 2025
4 of 5 checks passed
Copy link

@padriff Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@aeubanks
Copy link
Contributor

this seems to have broken the test on x86-64 macs:

 ******************** TEST 'AddressSanitizer-x86_64-darwin :: TestCases/Darwin/suppressions-sandbox.cpp' FAILED ********************
 Exit Code: 1
 
 Command Output (stderr):
 --
 RUN: at line 2: /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm-build/Release+Asserts/./bin/clang  --driver-mode=g++ -fsanitize=address -mno-omit-leaf-frame-pointer -fno-omit-frame-pointer -fno-optimize-sibling-calls -gline-tables-only  -arch x86_64 -stdlib=libc++ -mmacosx-version-min=10.12 -isysroot /Volumes/Work/s/w/ir/cache/osx_sdk/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -mlinker-version=1115.7.3  -shared-libasan -O0 /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp -o /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm-build/Release+Asserts/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64DarwinConfig/TestCases/Darwin/Output/suppressions-sandbox.cpp.tmp -framework Foundation
 + /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm-build/Release+Asserts/./bin/clang --driver-mode=g++ -fsanitize=address -mno-omit-leaf-frame-pointer -fno-omit-frame-pointer -fno-optimize-sibling-calls -gline-tables-only -arch x86_64 -stdlib=libc++ -mmacosx-version-min=10.12 -isysroot /Volumes/Work/s/w/ir/cache/osx_sdk/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.2.sdk -mlinker-version=1115.7.3 -shared-libasan -O0 /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp -o /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm-build/Release+Asserts/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64DarwinConfig/TestCases/Darwin/Output/suppressions-sandbox.cpp.tmp -framework Foundation
 RUN: at line 3: not  /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm-build/Release+Asserts/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64DarwinConfig/TestCases/Darwin/Output/suppressions-sandbox.cpp.tmp 2>&1 | FileCheck --check-prefix=CHECK-CRASH /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp
 + not /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm-build/Release+Asserts/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64DarwinConfig/TestCases/Darwin/Output/suppressions-sandbox.cpp.tmp
 + FileCheck --check-prefix=CHECK-CRASH /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp
 RUN: at line 6: echo "interceptor_via_fun:createCFString" > /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm-build/Release+Asserts/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64DarwinConfig/TestCases/Darwin/Output/suppressions-sandbox.cpp.tmp.supp
 + echo interceptor_via_fun:createCFString
 RUN: at line 7: env ASAN_OPTIONS=abort_on_error=0:log_to_syslog=0:detect_leaks=1:suppressions='"/Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm-build/Release+Asserts/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64DarwinConfig/TestCases/Darwin/Output/suppressions-sandbox.cpp.tmp.supp"'    sandbox-exec -p '(version 1)(allow default)(deny process-fork)'     /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm-build/Release+Asserts/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64DarwinConfig/TestCases/Darwin/Output/suppressions-sandbox.cpp.tmp 2>&1 | FileCheck --check-prefix=CHECK-IGNORE /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp
 + env 'ASAN_OPTIONS=abort_on_error=0:log_to_syslog=0:detect_leaks=1:suppressions="/Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm-build/Release+Asserts/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64DarwinConfig/TestCases/Darwin/Output/suppressions-sandbox.cpp.tmp.supp"' sandbox-exec -p '(version 1)(allow default)(deny process-fork)' /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm-build/Release+Asserts/runtimes/runtimes-bins/compiler-rt/test/asan/X86_64DarwinConfig/TestCases/Darwin/Output/suppressions-sandbox.cpp.tmp
 + FileCheck --check-prefix=CHECK-IGNORE /Volumes/Work/s/w/ir/cache/builder/src/third_party/llvm/compiler-rt/test/asan/TestCases/Darwin/suppressions-sandbox.cpp
 
 --

the test logs are not very helpful though...

@aeubanks
Copy link
Contributor

also started failing at https://green.lab.llvm.org/job/llvm.org/job/clang-stage1-RA/3852/ with this PR in the blamelist, can we revert this?

aeubanks added a commit that referenced this pull request Mar 18, 2025
@aeubanks
Copy link
Contributor

I've reverted this in a65cbc4, feel free to reland once you've figured out the x86-64 issue

also I'm surprised this is necessary given the test uses -O0

@padriff
Copy link
Contributor Author

padriff commented Mar 18, 2025

@aeubanks - thanks, I was working on a fix yesterday to address this. The issue is related to the FIXME comment in DemangleCXXABI in compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_posix_libcdep.cpp.
LeakSanitizer was firing on a leak caused by demangling the symbol for the suppressed function name.

The tail call optimization that breaks the test on AS is in the external code rather than in the test code, so the -O0 doesn't impact that.

Updated PR which also moves the suppression to a non-sanitized dylib

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.

5 participants