Skip to content

Commit

Permalink
Do not destroy tracker in umfTearDown() under the proxy library
Browse files Browse the repository at this point in the history
Signed-off-by: Lukasz Dorau <[email protected]>
  • Loading branch information
ldorau committed Nov 12, 2024
1 parent e66c199 commit 93b8be2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/base_alloc/base_alloc_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ static void umf_ba_create_global(void) {

size_t smallestSize = BASE_ALLOC.ac_sizes[0];
BASE_ALLOC.smallest_ac_size_log2 = log2Utils(smallestSize);

LOG_DEBUG("UMF base allocator created");
}

// returns index of the allocation class for a given size
Expand Down
20 changes: 20 additions & 0 deletions src/libumf.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base_alloc_global.h"
#include "memspace_internal.h"
#include "provider_tracking.h"
#include "utils_common.h"
#include "utils_log.h"
#if !defined(UMF_NO_HWLOC)
#include "topology.h"
Expand All @@ -25,6 +26,11 @@ int umfInit(void) {
if (utils_fetch_and_add64(&umfRefCount, 1) == 0) {
utils_log_init();
TRACKER = umfMemoryTrackerCreate();
LOG_DEBUG("UMF tracker created");
}

if (TRACKER) {
LOG_DEBUG("UMF library initialized");
}

return (TRACKER) ? 0 : -1;
Expand All @@ -39,12 +45,26 @@ void umfTearDown(void) {
umfMemspaceLowestLatencyDestroy();
umfDestroyTopology();
#endif

if (utils_is_running_in_proxy_lib_with_size_threshold()) {
// We cannot destroy the TRACKER nor the base allocator
// when we are running in the proxy library with a size threshold,
// because it could lead to calling the system free() with an invalid pointer
// and a segfault as a result.
goto fini_umfTearDown;
}

// make sure TRACKER is not used after being destroyed
umf_memory_tracker_handle_t t = TRACKER;
TRACKER = NULL;
umfMemoryTrackerDestroy(t);
LOG_DEBUG("UMF tracker destroyed");

umf_ba_destroy_global();
LOG_DEBUG("UMF base allocator destroyed");

fini_umfTearDown:
LOG_DEBUG("UMF library finalized");
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils/utils_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ static inline int utils_is_running_in_proxy_lib(void) {
return utils_env_var_get_str("LD_PRELOAD", "libumf_proxy.so") ? 1 : 0;
}

// check if we are running in the proxy library with a size threshold
static inline int utils_is_running_in_proxy_lib_with_size_threshold(void) {
return (utils_env_var_get_str("LD_PRELOAD", "libumf_proxy.so") &&
utils_env_var_get_str("UMF_PROXY", "size.threshold="))
? 1
: 0;
}

// utils_parse_var - Parses var for a prefix,
// optionally identifying a following argument.
// Parameters:
Expand Down

0 comments on commit 93b8be2

Please sign in to comment.