Skip to content

Commit 24d44ff

Browse files
[libc] __stack_chk_fail post submit test failures (#75962)
Use a size smaller than the smallest supported page size so that we don't clobber over any guard pages, which may result in a segfault before __stack_chk_fail can be called. Also, move __stack_chk_fail outside of our namespace.
1 parent 1e91f32 commit 24d44ff

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

libc/src/compiler/__stack_chk_fail.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
#ifndef LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H
1010
#define LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H
1111

12-
namespace LIBC_NAMESPACE {
13-
12+
// The compiler will emit calls implicitly to a non-namespaced version.
13+
// TODO: can we additionally provide a namespaced alias so that tests can
14+
// explicitly call the namespaced variant rather than the non-namespaced
15+
// definition?
16+
extern "C" {
1417
[[noreturn]] void __stack_chk_fail();
15-
16-
} // namespace LIBC_NAMESPACE
18+
} // extern "C"
1719

1820
#endif // LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H

libc/src/compiler/generic/__stack_chk_fail.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
#include "src/__support/OSUtil/io.h"
1111
#include "src/stdlib/abort.h"
1212

13-
namespace LIBC_NAMESPACE {
13+
extern "C" {
1414

15-
LLVM_LIBC_FUNCTION(void, __stack_chk_fail, (void)) {
15+
void __stack_chk_fail(void) {
1616
LIBC_NAMESPACE::write_to_stderr("stack smashing detected");
1717
LIBC_NAMESPACE::abort();
1818
}
1919

20-
} // namespace LIBC_NAMESPACE
20+
} // extern "C"

libc/test/integration/src/unistd/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ if((${LIBC_TARGET_OS} STREQUAL "linux") AND (${LIBC_TARGET_ARCHITECTURE_IS_X86})
4545
libc.include.signal
4646
libc.include.sys_wait
4747
libc.include.unistd
48+
libc.src.compiler.__stack_chk_fail
4849
libc.src.pthread.pthread_atfork
4950
libc.src.signal.raise
5051
libc.src.sys.wait.wait

libc/test/src/compiler/stack_chk_guard_test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@
1212
#include "test/UnitTest/Test.h"
1313

1414
TEST(LlvmLibcStackChkFail, Death) {
15-
EXPECT_DEATH([] { LIBC_NAMESPACE::__stack_chk_fail(); },
16-
WITH_SIGNAL(SIGABRT));
15+
EXPECT_DEATH([] { __stack_chk_fail(); }, WITH_SIGNAL(SIGABRT));
1716
}
1817

1918
TEST(LlvmLibcStackChkFail, Smash) {
2019
EXPECT_DEATH(
2120
[] {
2221
int arr[20];
23-
LIBC_NAMESPACE::memset(arr, 0xAA, 9001);
22+
LIBC_NAMESPACE::memset(arr, 0xAA, 2001);
2423
},
2524
WITH_SIGNAL(SIGABRT));
2625
}

0 commit comments

Comments
 (0)