TestStackSmash.cpp 932 B

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Format.h>
  7. #include <LibTest/TestCase.h>
  8. // Note: Needs to be 'noinline' so stack canary isn't optimized out.
  9. static void __attribute__((noinline)) smasher(char* string)
  10. {
  11. #pragma GCC diagnostic push
  12. #pragma GCC diagnostic ignored "-Warray-bounds"
  13. for (int i = 0; i < 256; i++) {
  14. string[i] = 'A';
  15. }
  16. #pragma GCC diagnostic pop
  17. }
  18. // Note: Needs to be 'noinline' so stack canary isn't optimized out.
  19. static void __attribute__((noinline)) stack_to_smash()
  20. {
  21. char string[8] = {};
  22. smasher(string);
  23. }
  24. TEST_CASE(stack_smash)
  25. {
  26. EXPECT_CRASH("Smash the stack and trigger __stack_chk_fail", [] {
  27. outln("[+] Starting the stack smash...");
  28. stack_to_smash();
  29. outln("[+] Stack smash wasn't detected!");
  30. return Test::Crash::Failure::DidNotCrash;
  31. });
  32. }