Skip to content

Destroy global base allocator in the library destructor #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/base_alloc/base_alloc_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdlib.h>

#include "base_alloc.h"
#include "base_alloc_global.h"
#include "utils_concurrency.h"

#define SIZE_BA_POOL_CHUNK 128
Expand All @@ -17,17 +18,20 @@
static umf_ba_pool_t *BA_pool = NULL;
static UTIL_ONCE_FLAG ba_is_initialized = UTIL_ONCE_FLAG_INIT;

static void umf_ba_destroy_global(void) {
assert(BA_pool);
umf_ba_destroy(BA_pool);
BA_pool = NULL;
}

static void umf_ba_create_global(void) {
assert(BA_pool == NULL);
BA_pool = umf_ba_create(SIZE_BA_POOL_CHUNK);
assert(BA_pool);
#if defined(_WIN32) && !defined(UMF_SHARED_LIBRARY)
atexit(umf_ba_destroy_global);
#endif
}

void umf_ba_destroy_global(void) {
if (BA_pool) {
atexit(umf_ba_destroy_global);
umf_ba_pool_t *pool = BA_pool;
BA_pool = NULL;
umf_ba_destroy(pool);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/base_alloc/base_alloc_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern "C" {
#endif

umf_ba_pool_t *umf_ba_get_pool(size_t size);
void umf_ba_destroy_global(void);

#ifdef __cplusplus
}
Expand Down
9 changes: 9 additions & 0 deletions src/base_alloc/base_alloc_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
#include <unistd.h>

#include "base_alloc.h"
#include "base_alloc_global.h"

// The highest possible priority (101) is used, because the constructor should be called
// as the first one and the destructor as the last one in order to avoid use-after-free.
void __attribute__((constructor(101))) umf_ba_constructor(void) {}

void __attribute__((destructor(101))) umf_ba_destructor(void) {
umf_ba_destroy_global();
}

void *ba_os_alloc(size_t size) {
return mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
Expand Down
5 changes: 4 additions & 1 deletion src/libumf_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ umf_memory_tracker_handle_t TRACKER = NULL;

static void umfCreate(void) { TRACKER = umfMemoryTrackerCreate(); }

static void umfDestroy(void) { umfMemoryTrackerDestroy(TRACKER); }
static void umfDestroy(void) {
umfMemoryTrackerDestroy(TRACKER);
umf_ba_destroy_global();
}

#if defined(UMF_SHARED_LIBRARY)
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
Expand Down