xref: /aosp_15_r20/external/llvm-libc/test/src/compiler/stack_chk_guard_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for __stack_chk_fail ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "hdr/signal_macros.h"
10 #include "src/__support/macros/sanitizer.h"
11 #include "src/compiler/__stack_chk_fail.h"
12 #include "src/string/memset.h"
13 #include "test/UnitTest/Test.h"
14 
TEST(LlvmLibcStackChkFail,Death)15 TEST(LlvmLibcStackChkFail, Death) {
16   EXPECT_DEATH([] { __stack_chk_fail(); }, WITH_SIGNAL(SIGABRT));
17 }
18 
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
TEST(LlvmLibcStackChkFail,Smash)22 TEST(LlvmLibcStackChkFail, Smash) {
23   EXPECT_DEATH(
24       [] {
25         int arr[20];
26         LIBC_NAMESPACE::memset(arr, 0xAA, 2001);
27       },
28       WITH_SIGNAL(SIGABRT));
29 }
30 #endif // LIBC_HAS_ADDRESS_SANITIZER
31