xref: /llvm-project/libc/test/src/compiler/stack_chk_guard_test.cpp (revision 1896ee38898a73ea9c2894e848884c8999884ab1)
1315a5cceSNick Desaulniers //===-- Unittests for __stack_chk_fail ------------------------------------===//
2315a5cceSNick Desaulniers //
3315a5cceSNick Desaulniers // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4315a5cceSNick Desaulniers // See https://llvm.org/LICENSE.txt for license information.
5315a5cceSNick Desaulniers // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6315a5cceSNick Desaulniers //
7315a5cceSNick Desaulniers //===----------------------------------------------------------------------===//
8315a5cceSNick Desaulniers 
9*1896ee38Slntue #include "hdr/signal_macros.h"
1022764918SNick Desaulniers #include "src/__support/macros/sanitizer.h"
11315a5cceSNick Desaulniers #include "src/compiler/__stack_chk_fail.h"
12315a5cceSNick Desaulniers #include "src/string/memset.h"
13315a5cceSNick Desaulniers #include "test/UnitTest/Test.h"
14315a5cceSNick Desaulniers 
15315a5cceSNick Desaulniers TEST(LlvmLibcStackChkFail, Death) {
1624d44ff4SNick Desaulniers   EXPECT_DEATH([] { __stack_chk_fail(); }, WITH_SIGNAL(SIGABRT));
17315a5cceSNick Desaulniers }
18315a5cceSNick Desaulniers 
1922764918SNick Desaulniers // Disable the test when asan is enabled so that it doesn't immediately fail
2022764918SNick Desaulniers // after the memset, but before the stack canary is re-checked.
21*1896ee38Slntue #ifndef LIBC_HAS_ADDRESS_SANITIZER
2222764918SNick Desaulniers TEST(LlvmLibcStackChkFail, Smash) {
2322764918SNick Desaulniers   EXPECT_DEATH(
2422764918SNick Desaulniers       [] {
25315a5cceSNick Desaulniers         int arr[20];
2624d44ff4SNick Desaulniers         LIBC_NAMESPACE::memset(arr, 0xAA, 2001);
2722764918SNick Desaulniers       },
2822764918SNick Desaulniers       WITH_SIGNAL(SIGABRT));
29b3d024c6SNick Desaulniers }
30*1896ee38Slntue #endif // LIBC_HAS_ADDRESS_SANITIZER
31