relro_test.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // relro_test.cc -- test -z relro for gold
  2. // Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include <cassert>
  18. #include <csignal>
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <exception>
  22. #include <stdint.h>
  23. #include <unistd.h>
  24. // This tests we were linked with a script. If we were linked with a
  25. // script, relro currently does not work.
  26. extern char using_script[] __attribute__ ((weak));
  27. // This code is put into a shared library linked with -z relro.
  28. // i1 and i2 are not relro variables.
  29. int i1 = 1;
  30. static int i2 = 2;
  31. // P1 is a global relro variable.
  32. int* const p1 __attribute__ ((aligned(64))) = &i1;
  33. // P2 is a local relro variable.
  34. int* const p2 __attribute__ ((aligned(64))) = &i2;
  35. // Add a TLS variable to make sure -z relro works correctly with TLS.
  36. __thread int i3 = 1;
  37. // Test symbol addresses.
  38. bool
  39. t1()
  40. {
  41. if (using_script)
  42. return true;
  43. void* i1addr = static_cast<void*>(&i1);
  44. void* i2addr = static_cast<void*>(&i2);
  45. const void* p1addr = static_cast<const void*>(&p1);
  46. const void* p2addr = static_cast<const void*>(&p2);
  47. // The relro variables should precede the non-relro variables in the
  48. // memory image.
  49. assert(i1addr > p1addr);
  50. assert(i1addr > p2addr);
  51. assert(i2addr > p1addr);
  52. assert(i2addr > p2addr);
  53. // The relro variables should not be on the same page as the
  54. // non-relro variables.
  55. const size_t page_size = getpagesize();
  56. uintptr_t i1page = reinterpret_cast<uintptr_t>(i1addr) & ~ (page_size - 1);
  57. uintptr_t i2page = reinterpret_cast<uintptr_t>(i2addr) & ~ (page_size - 1);
  58. uintptr_t p1page = reinterpret_cast<uintptr_t>(p1addr) & ~ (page_size - 1);
  59. uintptr_t p2page = reinterpret_cast<uintptr_t>(p2addr) & ~ (page_size - 1);
  60. assert(i1page != p1page);
  61. assert(i1page != p2page);
  62. assert(i2page != p1page);
  63. assert(i2page != p2page);
  64. assert(i3 == 1);
  65. return true;
  66. }
  67. // Tell terminate handler that we are throwing from a signal handler.
  68. static bool throwing;
  69. // A signal handler for SIGSEGV.
  70. extern "C"
  71. void
  72. sigsegv_handler(int)
  73. {
  74. throwing = true;
  75. throw 0;
  76. }
  77. // The original terminate handler.
  78. std::terminate_handler orig_terminate;
  79. // Throwing an exception out of a signal handler doesn't always work
  80. // reliably. When that happens the program will call terminate. We
  81. // set a terminate handler to indicate that the test probably passed.
  82. void
  83. terminate_handler()
  84. {
  85. if (!throwing)
  86. {
  87. orig_terminate();
  88. ::exit(EXIT_FAILURE);
  89. }
  90. fprintf(stderr,
  91. "relro_test: terminate called due to failure to throw through signal handler\n");
  92. fprintf(stderr, "relro_test: assuming test succeeded\n");
  93. ::exit(EXIT_SUCCESS);
  94. }
  95. // Use a separate function to throw the exception, so that we don't
  96. // need to use -fnon-call-exceptions.
  97. void f2() __attribute__ ((noinline));
  98. void
  99. f2()
  100. {
  101. int** pp1 = const_cast<int**>(&p1);
  102. *pp1 = &i2;
  103. // We shouldn't get here--the assignment to *pp1 should write to
  104. // memory which the dynamic linker marked as read-only, giving us a
  105. // SIGSEGV, causing sigsegv_handler to be invoked, to throw past us.
  106. assert(0);
  107. }
  108. // Changing a relro variable should give us a SIGSEGV.
  109. bool
  110. t2()
  111. {
  112. if (using_script)
  113. return true;
  114. signal(SIGSEGV, sigsegv_handler);
  115. orig_terminate = std::set_terminate(terminate_handler);
  116. try
  117. {
  118. f2();
  119. return false;
  120. }
  121. catch (int i)
  122. {
  123. assert(i == 0);
  124. return true;
  125. }
  126. }