blob: 639a4bd43cef2e5bdb4f1abe11d6f66f22a9f67b [file] [log] [blame]
Matthew Dentonbb0b03e2021-07-22 16:18:131// Copyright 2021 The Chromium Authors. All rights reserved.
2// 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
7#include "base/compiler_specific.h"
8#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 {
17__attribute__((noinline, optnone)) void ResetCanaryAndReturn() {
18 // 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.
22 char buffer[10];
23 ALLOW_UNUSED_LOCAL(buffer);
24 ResetStackCanaryIfPossible();
25}
26} // namespace
27
28// Essentially tests that ResetStackCanaryIfPossible() changes the
29// actual reference canary that is checked in the function prologue.
30TEST(StackCanary, ChangingStackCanaryCrashesOnReturn) {
31 ASSERT_DEATH(ResetCanaryAndReturn(), "stack smashing");
32}
33
34#if !defined(NDEBUG)
35// Tests that the useful debug message works--specifically that on death, it
36// prints out the bug URL with useful information.
37TEST(StackCanary, ChangingStackCanaryPrintsDebugMessage) {
38 SetStackSmashingEmitsDebugMessage();
39 ASSERT_DEATH(ResetCanaryAndReturn(), "crbug\\.com/1206626");
40}
41#endif // !defined(NDEBUG)
42
43#endif // defined(LIBC_GLIBC) && (defined(ARCH_CPU_ARM_FAMILY) ||
44 // defined(ARCH_CPU_X86_FAMILY))
45
46} // namespace base