Skip to content

[libc] Make LlvmLibcStackChkFail.Smash test compatible with asan, hwasan #125763

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 2 commits into from
Feb 4, 2025

Conversation

frobtech
Copy link
Contributor

@frobtech frobtech commented Feb 4, 2025

Previously this test was entirely disabled under asan, but not
hwasan. Instead of disabling the test, make the test compatible
with both asan and hwasan by disabling sanitizers only on the
subroutine that does the stack-smashing.

Previously this test was entirely disabled under asan, but not
hwasan.  Instead of disabling the test, make the test compatible
with both asan and hwasan by disabling sanitizers only on the
subroutine that does the stack-smashing.
@frobtech
Copy link
Contributor Author

frobtech commented Feb 4, 2025

This makes it possible to start using this test in Fuchsia, where we support hwasan builds.

@frobtech frobtech marked this pull request as ready for review February 4, 2025 21:06
@llvmbot llvmbot added the libc label Feb 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 4, 2025

@llvm/pr-subscribers-libc

Author: Roland McGrath (frobtech)

Changes

Previously this test was entirely disabled under asan, but not
hwasan. Instead of disabling the test, make the test compatible
with both asan and hwasan by disabling sanitizers only on the
subroutine that does the stack-smashing.


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

1 Files Affected:

  • (modified) libc/test/src/compiler/stack_chk_guard_test.cpp (+21-10)
diff --git a/libc/test/src/compiler/stack_chk_guard_test.cpp b/libc/test/src/compiler/stack_chk_guard_test.cpp
index 4ec8398c9fc95d..5d5723adcf0241 100644
--- a/libc/test/src/compiler/stack_chk_guard_test.cpp
+++ b/libc/test/src/compiler/stack_chk_guard_test.cpp
@@ -12,19 +12,30 @@
 #include "src/string/memset.h"
 #include "test/UnitTest/Test.h"
 
+namespace {
+
 TEST(LlvmLibcStackChkFail, Death) {
   EXPECT_DEATH([] { __stack_chk_fail(); }, WITH_SIGNAL(SIGABRT));
 }
 
-// Disable the test when asan is enabled so that it doesn't immediately fail
-// after the memset, but before the stack canary is re-checked.
-#ifndef LIBC_HAS_ADDRESS_SANITIZER
+// When https://github.com/llvm/llvm-project/issues/125760 is fixed,
+// this can use the `gnu::` spelling unconditionally.
+#ifdef __clang__
+#define SANITIZER_ATTR_NS clang
+#else
+#define SANITIZER_ATTR_NS gnu
+#endif
+
+// Disable sanitizers such as asan and hwasan that would catch the buffer
+// overrun before it clobbered the stack canary word.  Function attributes
+// can't be applied to lambdas before C++23, so this has to be separate.
+[[SANITIZER_ATTR_NS::no_sanitize("all")]] void smash_stack() {
+  int arr[20];
+  LIBC_NAMESPACE::memset(arr, 0xAA, 2001);
+}
+
 TEST(LlvmLibcStackChkFail, Smash) {
-  EXPECT_DEATH(
-      [] {
-        int arr[20];
-        LIBC_NAMESPACE::memset(arr, 0xAA, 2001);
-      },
-      WITH_SIGNAL(SIGABRT));
+  EXPECT_DEATH(smash_stack, WITH_SIGNAL(SIGABRT));
 }
-#endif // LIBC_HAS_ADDRESS_SANITIZER
+
+} // namespace

Copy link
Contributor

@Caslyn Caslyn left a comment

Choose a reason for hiding this comment

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

LGTM pending current comment

@frobtech frobtech merged commit 1e7624c into llvm:main Feb 4, 2025
12 of 13 checks passed
@frobtech frobtech deleted the p/libc-stack_chk_fail-test branch February 4, 2025 21:42
@nickdesaulniers
Copy link
Member

nickdesaulniers commented Feb 4, 2025

Looks like the test is now failing in postsubmit for fullbuild mode with ASAN enabled.

https://lab.llvm.org/buildbot/#/builders/171/builds/15242/steps/4/logs/stdio

Mind reverting unless it's straightforward to fix forward relatively quickly?

frobtech added a commit that referenced this pull request Feb 5, 2025
…san, hwasan" (#125785)

Reverts #125763

This causes failures in asan. More thought is needed.
github-actions bot pushed a commit to arm/arm-toolchain that referenced this pull request Feb 5, 2025
…ible with asan, hwasan" (#125785)

Reverts llvm/llvm-project#125763

This causes failures in asan. More thought is needed.
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…san (llvm#125763)

Previously this test was entirely disabled under asan, but not
hwasan.  Instead of disabling the test, make the test compatible
with both asan and hwasan by disabling sanitizers only on the
subroutine that does the stack-smashing.
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…san, hwasan" (llvm#125785)

Reverts llvm#125763

This causes failures in asan. More thought is needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants