[libc-commits] [libc] [libc] Make LlvmLibcStackChkFail.Smash test compatible with asan, hwasan (PR #125763)
Roland McGrath via libc-commits
libc-commits at lists.llvm.org
Tue Feb 4 13:05:17 PST 2025
https://github.com/frobtech created https://github.com/llvm/llvm-project/pull/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.
>From 10b86b91af47a8248e5978fe93873fecf7327bf8 Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Tue, 4 Feb 2025 12:59:11 -0800
Subject: [PATCH] [libc] Make LlvmLibcStackChkFail.Smash test compatible with
asan, hwasan
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.
---
.../src/compiler/stack_chk_guard_test.cpp | 31 +++++++++++++------
1 file changed, 21 insertions(+), 10 deletions(-)
diff --git a/libc/test/src/compiler/stack_chk_guard_test.cpp b/libc/test/src/compiler/stack_chk_guard_test.cpp
index 4ec8398c9fc95dc..5d5723adcf02417 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
More information about the libc-commits
mailing list