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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions libc/test/src/compiler/stack_chk_guard_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@
#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
// 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. When
// https://github.com/llvm/llvm-project/issues/125760 is fixed, this can use
// the modern spelling [[gnu::no_sanitize(...)]] without conditionalization.
__attribute__((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