Skip to content

Commit 220aa29

Browse files
committed
do not destroy proxy lib pool before umf lib
1 parent 13bd6dd commit 220aa29

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/utils/utils_common.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
#include <stdbool.h>
1515
#include <stddef.h>
1616
#include <stdint.h>
17+
#include <stdio.h>
18+
#include <string.h>
1719

1820
#include <umf/base.h>
1921
#include <umf/memory_provider.h>
2022

23+
#include "utils_load_library.h"
24+
2125
#ifdef __cplusplus
2226
extern "C" {
2327
#endif
@@ -79,6 +83,23 @@ static inline int utils_env_var_has_str(const char *envvar, const char *str) {
7983

8084
// check if we are running in the proxy library
8185
static inline int utils_is_running_in_proxy_lib(void) {
86+
// check if the proxy library is loaded using /proc/self/maps
87+
FILE *fp = fopen("/proc/self/maps", "r");
88+
if (!fp) {
89+
perror("Failed to open /proc/self/maps");
90+
return -1;
91+
}
92+
93+
char line[256];
94+
while (fgets(line, sizeof(line), fp)) {
95+
if (strstr(line, "libumf_proxy.so") != NULL) {
96+
fclose(fp);
97+
return 1; // Shared object is loaded
98+
}
99+
}
100+
fclose(fp);
101+
102+
// check if the proxy library is loaded using LD_PRELOAD
82103
return utils_env_var_get_str("LD_PRELOAD", "libumf_proxy.so") ? 1 : 0;
83104
}
84105

src/utils/utils_load_library.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,31 @@ void *utils_open_library(const char *filename, int userFlags) {
7171
dlopenFlags |= RTLD_GLOBAL;
7272
}
7373
if (userFlags & UMF_UTIL_OPEN_LIBRARY_NO_LOAD) {
74-
dlopenFlags |= RTLD_NOLOAD;
74+
dlopenFlags = RTLD_NOLOAD | RTLD_NOW;
7575
}
7676

7777
void *handle = dlopen(filename, dlopenFlags);
7878
if (handle == NULL) {
79+
if (userFlags & UMF_UTIL_OPEN_LIBRARY_NO_LOAD) {
80+
// if we are not loading the library, just return NULL if it is not
81+
// found
82+
LOG_DEBUG("Library %s not found, returning NULL", filename);
83+
return NULL;
84+
}
85+
7986
LOG_FATAL("dlopen(%s) failed with error: %s", filename, dlerror());
87+
return NULL;
8088
}
8189

90+
LOG_DEBUG("Opened library %s with handle %p", filename, handle);
8291
return handle;
8392
}
8493

85-
int utils_close_library(void *handle) { return dlclose(handle); }
94+
int utils_close_library(void *handle) {
95+
LOG_DEBUG("Closing library handle %p", handle);
96+
97+
return dlclose(handle);
98+
}
8699

87100
void *utils_get_symbol_addr(void *handle, const char *symbol,
88101
const char *libname) {

0 commit comments

Comments
 (0)