stack_canary_linux_unittest.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // 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. #include "base/stack_canary_linux.h"
  5. #include "build/build_config.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. namespace base {
  8. #if defined(LIBC_GLIBC) && \
  9. (defined(ARCH_CPU_ARM_FAMILY) || defined(ARCH_CPU_X86_FAMILY))
  10. namespace {
  11. __attribute__((noinline, optnone)) void ResetCanaryAndReturn() {
  12. // Create a buffer >=8 bytes to force the stack protector on this function,
  13. // which should work as long as -fno-stack-protector isn't passed in the
  14. // default options. We compile this file with -fstack-protector-all, but it
  15. // may be overridden with -fstack-protector or -fstack-protector-strong.
  16. [[maybe_unused]] char buffer[10];
  17. ResetStackCanaryIfPossible();
  18. }
  19. } // namespace
  20. // Essentially tests that ResetStackCanaryIfPossible() changes the
  21. // actual reference canary that is checked in the function prologue.
  22. TEST(StackCanary, ChangingStackCanaryCrashesOnReturn) {
  23. ASSERT_DEATH(ResetCanaryAndReturn(), "stack smashing");
  24. }
  25. #if !defined(NDEBUG)
  26. // Tests that the useful debug message works--specifically that on death, it
  27. // prints out the bug URL with useful information.
  28. TEST(StackCanary, ChangingStackCanaryPrintsDebugMessage) {
  29. SetStackSmashingEmitsDebugMessage();
  30. ASSERT_DEATH(ResetCanaryAndReturn(), "crbug\\.com/1206626");
  31. }
  32. #endif // !defined(NDEBUG)
  33. #endif // defined(LIBC_GLIBC) && (defined(ARCH_CPU_ARM_FAMILY) ||
  34. // defined(ARCH_CPU_X86_FAMILY))
  35. } // namespace base