1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:27:45 +00:00

LibC: Prevent assertions in malloc/free at exit time

This is a bit sad, but, with the Allocators as static globals their
destructors were running before some user code. Which doesn't really
make much sense, as none of the members of (at least the basic one) do
any real heavy lifting or have many resources to RAII.

To avoid the problem, just mmap the memory for the global arrays of
Allocators in __malloc_init and let the Kernel collect the memory when
we're done with the process.
This commit is contained in:
Andrew Kaster 2019-12-22 02:06:50 -07:00 committed by Andreas Kling
parent 7edfdca4b2
commit 150837e7e8
2 changed files with 25 additions and 2 deletions

View file

@ -3,6 +3,8 @@
#include <stdio.h>
#include <stdlib.h>
//#define GLOBAL_DTORS_DEBUG
extern "C" {
int main(int, char**, char**);
@ -83,11 +85,18 @@ void __cxa_finalize(void* dso_handle)
int entry_index = __exit_entry_count;
#ifdef GLOBAL_DTORS_DEBUG
dbgprintf("__cxa_finalize: %d entries in the finalizer list\n", entry_index);
#endif
while (--entry_index >= 0)
{
auto& exit_entry = __exit_entries[entry_index];
bool needs_calling = !exit_entry.has_been_called && (!dso_handle || dso_handle == exit_entry.dso_handle);
if (needs_calling) {
#ifdef GLOBAL_DTORS_DEBUG
dbgprintf("__cxa_finalize: calling entry[%d] %p(%p) dso: %p\n", entry_index, exit_entry.method, exit_entry.parameter, exit_entry.dso_handle);
#endif
exit_entry.method(exit_entry.parameter);
exit_entry.has_been_called = true;
}