Skip to content

Commit 10b86b9

Browse files
committed
[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.
1 parent c8ca486 commit 10b86b9

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

libc/test/src/compiler/stack_chk_guard_test.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,30 @@
1212
#include "src/string/memset.h"
1313
#include "test/UnitTest/Test.h"
1414

15+
namespace {
16+
1517
TEST(LlvmLibcStackChkFail, Death) {
1618
EXPECT_DEATH([] { __stack_chk_fail(); }, WITH_SIGNAL(SIGABRT));
1719
}
1820

19-
// Disable the test when asan is enabled so that it doesn't immediately fail
20-
// after the memset, but before the stack canary is re-checked.
21-
#ifndef LIBC_HAS_ADDRESS_SANITIZER
21+
// When https://github.com/llvm/llvm-project/issues/125760 is fixed,
22+
// this can use the `gnu::` spelling unconditionally.
23+
#ifdef __clang__
24+
#define SANITIZER_ATTR_NS clang
25+
#else
26+
#define SANITIZER_ATTR_NS gnu
27+
#endif
28+
29+
// Disable sanitizers such as asan and hwasan that would catch the buffer
30+
// overrun before it clobbered the stack canary word. Function attributes
31+
// can't be applied to lambdas before C++23, so this has to be separate.
32+
[[SANITIZER_ATTR_NS::no_sanitize("all")]] void smash_stack() {
33+
int arr[20];
34+
LIBC_NAMESPACE::memset(arr, 0xAA, 2001);
35+
}
36+
2237
TEST(LlvmLibcStackChkFail, Smash) {
23-
EXPECT_DEATH(
24-
[] {
25-
int arr[20];
26-
LIBC_NAMESPACE::memset(arr, 0xAA, 2001);
27-
},
28-
WITH_SIGNAL(SIGABRT));
38+
EXPECT_DEATH(smash_stack, WITH_SIGNAL(SIGABRT));
2939
}
30-
#endif // LIBC_HAS_ADDRESS_SANITIZER
40+
41+
} // namespace

0 commit comments

Comments
 (0)