cxxabi.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Debug.h>
  27. #include <AK/Format.h>
  28. #include <assert.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <sys/internals.h>
  32. #include <sys/mman.h>
  33. #include <unistd.h>
  34. extern "C" {
  35. struct AtExitEntry {
  36. AtExitFunction method { nullptr };
  37. void* parameter { nullptr };
  38. void* dso_handle { nullptr };
  39. bool has_been_called { false };
  40. };
  41. static constexpr size_t max_atexit_entry_count = PAGE_SIZE / sizeof(AtExitEntry);
  42. static AtExitEntry* atexit_entries;
  43. static size_t atexit_entry_count = 0;
  44. static void lock_atexit_handlers()
  45. {
  46. if (mprotect(atexit_entries, PAGE_SIZE, PROT_READ) < 0) {
  47. perror("lock_atexit_handlers");
  48. _exit(1);
  49. }
  50. }
  51. static void unlock_atexit_handlers()
  52. {
  53. if (mprotect(atexit_entries, PAGE_SIZE, PROT_READ | PROT_WRITE) < 0) {
  54. perror("unlock_atexit_handlers");
  55. _exit(1);
  56. }
  57. }
  58. int __cxa_atexit(AtExitFunction exit_function, void* parameter, void* dso_handle)
  59. {
  60. if (atexit_entry_count >= max_atexit_entry_count)
  61. return -1;
  62. if (!atexit_entries) {
  63. atexit_entries = (AtExitEntry*)mmap(nullptr, PAGE_SIZE, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  64. if (atexit_entries == MAP_FAILED) {
  65. perror("__cxa_atexit mmap");
  66. _exit(1);
  67. }
  68. }
  69. unlock_atexit_handlers();
  70. atexit_entries[atexit_entry_count++] = { exit_function, parameter, dso_handle, false };
  71. lock_atexit_handlers();
  72. return 0;
  73. }
  74. void __cxa_finalize(void* dso_handle)
  75. {
  76. // From the itanium abi, https://itanium-cxx-abi.github.io/cxx-abi/abi.html#dso-dtor-runtime-api
  77. //
  78. // When __cxa_finalize(d) is called, it should walk the termination function list, calling each in turn
  79. // if d matches __dso_handle for the termination function entry. If d == NULL, it should call all of them.
  80. // Multiple calls to __cxa_finalize shall not result in calling termination function entries multiple times;
  81. // the implementation may either remove entries or mark them finished.
  82. ssize_t entry_index = atexit_entry_count;
  83. dbgln_if(GLOBAL_DTORS_DEBUG, "__cxa_finalize: {} entries in the finalizer list", entry_index);
  84. while (--entry_index >= 0) {
  85. auto& exit_entry = atexit_entries[entry_index];
  86. bool needs_calling = !exit_entry.has_been_called && (!dso_handle || dso_handle == exit_entry.dso_handle);
  87. if (needs_calling) {
  88. dbgln_if(GLOBAL_DTORS_DEBUG, "__cxa_finalize: calling entry[{}] {:p}({:p}) dso: {:p}", entry_index, exit_entry.method, exit_entry.parameter, exit_entry.dso_handle);
  89. unlock_atexit_handlers();
  90. exit_entry.has_been_called = true;
  91. lock_atexit_handlers();
  92. exit_entry.method(exit_entry.parameter);
  93. }
  94. }
  95. }
  96. [[noreturn]] void __cxa_pure_virtual()
  97. {
  98. VERIFY_NOT_REACHED();
  99. }
  100. } // extern "C"