cxxabi.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2019-2020, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/Format.h>
  8. #include <LibC/bits/pthread_integration.h>
  9. #include <assert.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <sys/internals.h>
  13. #include <sys/mman.h>
  14. #include <unistd.h>
  15. extern "C" {
  16. struct AtExitEntry {
  17. AtExitFunction method { nullptr };
  18. void* parameter { nullptr };
  19. void* dso_handle { nullptr };
  20. bool has_been_called { false };
  21. };
  22. static constexpr size_t max_atexit_entry_count = PAGE_SIZE / sizeof(AtExitEntry);
  23. static AtExitEntry* atexit_entries;
  24. static size_t atexit_entry_count = 0;
  25. static pthread_mutex_t atexit_mutex = __PTHREAD_MUTEX_INITIALIZER;
  26. static void lock_atexit_handlers()
  27. {
  28. if (mprotect(atexit_entries, PAGE_SIZE, PROT_READ) < 0) {
  29. perror("lock_atexit_handlers");
  30. _exit(1);
  31. }
  32. }
  33. static void unlock_atexit_handlers()
  34. {
  35. if (mprotect(atexit_entries, PAGE_SIZE, PROT_READ | PROT_WRITE) < 0) {
  36. perror("unlock_atexit_handlers");
  37. _exit(1);
  38. }
  39. }
  40. int __cxa_atexit(AtExitFunction exit_function, void* parameter, void* dso_handle)
  41. {
  42. __pthread_mutex_lock(&atexit_mutex);
  43. if (atexit_entry_count >= max_atexit_entry_count) {
  44. __pthread_mutex_unlock(&atexit_mutex);
  45. return -1;
  46. }
  47. if (!atexit_entries) {
  48. atexit_entries = (AtExitEntry*)mmap(nullptr, PAGE_SIZE, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  49. if (atexit_entries == MAP_FAILED) {
  50. __pthread_mutex_unlock(&atexit_mutex);
  51. perror("__cxa_atexit mmap");
  52. _exit(1);
  53. }
  54. }
  55. unlock_atexit_handlers();
  56. atexit_entries[atexit_entry_count++] = { exit_function, parameter, dso_handle, false };
  57. lock_atexit_handlers();
  58. __pthread_mutex_unlock(&atexit_mutex);
  59. return 0;
  60. }
  61. void __cxa_finalize(void* dso_handle)
  62. {
  63. // From the itanium abi, https://itanium-cxx-abi.github.io/cxx-abi/abi.html#dso-dtor-runtime-api
  64. //
  65. // When __cxa_finalize(d) is called, it should walk the termination function list, calling each in turn
  66. // if d matches __dso_handle for the termination function entry. If d == NULL, it should call all of them.
  67. // Multiple calls to __cxa_finalize shall not result in calling termination function entries multiple times;
  68. // the implementation may either remove entries or mark them finished.
  69. __pthread_mutex_lock(&atexit_mutex);
  70. ssize_t entry_index = atexit_entry_count;
  71. dbgln_if(GLOBAL_DTORS_DEBUG, "__cxa_finalize: {} entries in the finalizer list", entry_index);
  72. while (--entry_index >= 0) {
  73. auto& exit_entry = atexit_entries[entry_index];
  74. bool needs_calling = !exit_entry.has_been_called && (!dso_handle || dso_handle == exit_entry.dso_handle);
  75. if (needs_calling) {
  76. 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);
  77. unlock_atexit_handlers();
  78. exit_entry.has_been_called = true;
  79. lock_atexit_handlers();
  80. __pthread_mutex_unlock(&atexit_mutex);
  81. exit_entry.method(exit_entry.parameter);
  82. __pthread_mutex_lock(&atexit_mutex);
  83. }
  84. }
  85. __pthread_mutex_unlock(&atexit_mutex);
  86. }
  87. __attribute__((noreturn)) void __cxa_pure_virtual()
  88. {
  89. VERIFY_NOT_REACHED();
  90. }
  91. } // extern "C"