Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Matthew Denton | bb0b03e | 2021-07-22 16:18:13 | [diff] [blame] | 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 | |
Jose Dapena Paz | 7cc1b1d4 | 2023-11-08 18:37:28 | [diff] [blame] | 7 | #include "base/compiler_specific.h" |
Matthew Denton | bb0b03e | 2021-07-22 16:18:13 | [diff] [blame] | 8 | #include "build/build_config.h" |
| 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
| 11 | namespace base { |
| 12 | |
| 13 | #if defined(LIBC_GLIBC) && \ |
| 14 | (defined(ARCH_CPU_ARM_FAMILY) || defined(ARCH_CPU_X86_FAMILY)) |
| 15 | |
| 16 | namespace { |
Jose Dapena Paz | 7cc1b1d4 | 2023-11-08 18:37:28 | [diff] [blame] | 17 | NOINLINE NOOPT void ResetCanaryAndReturn() { |
Matthew Denton | bb0b03e | 2021-07-22 16:18:13 | [diff] [blame] | 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. |
Avi Drissman | dea3205 | 2022-01-13 21:31:18 | [diff] [blame] | 22 | [[maybe_unused]] char buffer[10]; |
Matthew Denton | bb0b03e | 2021-07-22 16:18:13 | [diff] [blame] | 23 | ResetStackCanaryIfPossible(); |
| 24 | } |
| 25 | } // namespace |
| 26 | |
| 27 | // Essentially tests that ResetStackCanaryIfPossible() changes the |
| 28 | // actual reference canary that is checked in the function prologue. |
| 29 | TEST(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. |
| 36 | TEST(StackCanary, ChangingStackCanaryPrintsDebugMessage) { |
| 37 | SetStackSmashingEmitsDebugMessage(); |
| 38 | ASSERT_DEATH(ResetCanaryAndReturn(), "crbug\\.com/1206626"); |
| 39 | } |
|
|