1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/stack_canary_linux.h"
7 #include "build/build_config.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 #if defined(LIBC_GLIBC) && \
13 (defined(ARCH_CPU_ARM_FAMILY) || defined(ARCH_CPU_X86_FAMILY))
16 __attribute__((noinline, optnone)) void ResetCanaryAndReturn() {
17 // Create a buffer >=8 bytes to force the stack protector on this function,
18 // which should work as long as -fno-stack-protector isn't passed in the
19 // default options. We compile this file with -fstack-protector-all, but it
20 // may be overridden with -fstack-protector or -fstack-protector-strong.
21 [[maybe_unused]] char buffer[10];
22 ResetStackCanaryIfPossible();
26 // Essentially tests that ResetStackCanaryIfPossible() changes the
27 // actual reference canary that is checked in the function prologue.
28 TEST(StackCanary, ChangingStackCanaryCrashesOnReturn) {
29 ASSERT_DEATH(ResetCanaryAndReturn(), "stack smashing");
33 // Tests that the useful debug message works--specifically that on death, it
34 // prints out the bug URL with useful information.
35 TEST(StackCanary, ChangingStackCanaryPrintsDebugMessage) {
36 SetStackSmashingEmitsDebugMessage();
37 ASSERT_DEATH(ResetCanaryAndReturn(), "crbug\\.com/1206626");
39 #endif // !defined(NDEBUG)
41 #endif // defined(LIBC_GLIBC) && (defined(ARCH_CPU_ARM_FAMILY) ||
42 // defined(ARCH_CPU_X86_FAMILY))