TestSigAltStack.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #ifdef __clang__
  7. # pragma clang optimize off
  8. #else
  9. # pragma GCC optimize("O0")
  10. #endif
  11. #include <LibTest/TestCase.h>
  12. #include <signal.h>
  13. #include <unistd.h>
  14. static void signal_handler(int)
  15. {
  16. // We execute this syscall in order to force the kernel to perform the syscall precondition validation which
  17. // checks that we have correctly set up the stack region to match our currently implemented protections.
  18. getuid();
  19. _exit(0);
  20. }
  21. #pragma GCC diagnostic push
  22. #pragma GCC diagnostic ignored "-Winfinite-recursion"
  23. static size_t infinite_recursion(size_t input)
  24. {
  25. return infinite_recursion(input) + 1;
  26. }
  27. #pragma GCC diagnostic pop
  28. // This test can only pass with sigaltstack correctly enabled, as otherwise the SIGSEGV signal handler itself would also fault due to the overflown stack.
  29. TEST_CASE(success_case)
  30. {
  31. static u8 alt_stack[SIGSTKSZ];
  32. stack_t ss = {
  33. .ss_sp = alt_stack,
  34. .ss_flags = 0,
  35. .ss_size = SIGSTKSZ,
  36. };
  37. auto res = sigaltstack(&ss, nullptr);
  38. EXPECT_EQ(res, 0);
  39. struct sigaction sa;
  40. sa.sa_handler = signal_handler;
  41. sa.sa_flags = SA_ONSTACK;
  42. res = sigfillset(&sa.sa_mask);
  43. EXPECT_EQ(res, 0);
  44. res = sigaction(SIGSEGV, &sa, 0);
  45. EXPECT_EQ(res, 0);
  46. (void)infinite_recursion(0);
  47. FAIL("Infinite recursion finished successfully");
  48. }