blob: 3bdaeab9b703884ca9635302a8705420e5fb15f6 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2021 The Chromium Authors
Matthew Dentonbb0b03e2021-07-22 16:18:132// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/stack_canary_linux.h"
6
Jose Dapena Paz7cc1b1d42023-11-08 18:37:287#include "base/compiler_specific.h"
Matthew Dentonbb0b03e2021-07-22 16:18:138#include "build/build_config.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace base {
12
13#if defined(LIBC_GLIBC) && \
14 (defined(ARCH_CPU_ARM_FAMILY) || defined(ARCH_CPU_X86_FAMILY))
15
16namespace {
Jose Dapena Paz7cc1b1d42023-11-08 18:37:2817NOINLINE NOOPT void ResetCanaryAndReturn() {
Matthew Dentonbb0b03e2021-07-22 16:18:1318 // Create a buffer >=8 bytes to force the stack protector on this function,
19 // which should work as long as -fno-stack-protector isn't passed in the
20 // default options. We compile this file with -fstack-protector-all, but it
21 // may be overridden with -fstack-protector or -fstack-protector-strong.
Avi Drissmandea32052022-01-13 21:31:1822 [[maybe_unused]] char buffer[10];
Matthew Dentonbb0b03e2021-07-22 16:18:1323 ResetStackCanaryIfPossible();
24}
25} // namespace
26
27// Essentially tests that ResetStackCanaryIfPossible() changes the
28// actual reference canary that is checked in the function prologue.
29TEST(StackCanary, ChangingStackCanaryCrashesOnReturn) {
30 ASSERT_DEATH(ResetCanaryAndReturn(), "stack smashing");
31}
32
33#if !defined(NDEBUG)
34// Tests that the useful debug message works--specifically that on death, it
35// prints out the bug URL with useful information.
36TEST(StackCanary, ChangingStackCanaryPrintsDebugMessage) {
37 SetStackSmashingEmitsDebugMessage();
38 ASSERT_DEATH(ResetCanaryAndReturn(), "crbug\\.com/1206626");
39}