2727 * - _aligned_offset_recalloc()
2828 */
2929
30+ #ifndef _WIN32
31+ #define _GNU_SOURCE // for RTLD_NEXT
32+ #include <dlfcn.h>
33+ #undef _GNU_SOURCE
34+ #endif /* _WIN32 */
35+
3036#if (defined PROXY_LIB_USES_JEMALLOC_POOL )
3137#include <umf/pools/pool_jemalloc.h>
3238#define umfPoolManagerOps umfJemallocPoolOps
4854#include "base_alloc_linear.h"
4955#include "proxy_lib.h"
5056#include "utils_common.h"
57+ #include "utils_load_library.h"
5158#include "utils_log.h"
5259
5360#ifdef _WIN32 /* Windows ***************************************/
@@ -94,6 +101,24 @@ void utils_init_once(UTIL_ONCE_FLAG *flag, void (*onceCb)(void));
94101 * of a UMF pool to allocate memory needed by an application. It should be freed
95102 * by an application.
96103 */
104+ #ifndef _WIN32
105+ typedef void * (* system_aligned_alloc_t )(size_t alignment , size_t size );
106+ typedef void * (* system_calloc_t )(size_t nmemb , size_t size );
107+ typedef void (* system_free_t )(void * ptr );
108+ typedef void * (* system_malloc_t )(size_t size );
109+ typedef size_t (* system_malloc_usable_size_t )(void * ptr );
110+ typedef void * (* system_realloc_t )(void * ptr , size_t size );
111+
112+ // pointers to the default system allocator's API
113+ static system_aligned_alloc_t System_aligned_alloc ;
114+ static system_calloc_t System_calloc ;
115+ static system_free_t System_free ;
116+ static system_malloc_t System_malloc ;
117+ static system_malloc_usable_size_t System_malloc_usable_size ;
118+ static system_realloc_t System_realloc ;
119+
120+ static size_t Size_threshold_value = 0 ;
121+ #endif /* _WIN32 */
97122
98123static UTIL_ONCE_FLAG Base_alloc_leak_initialized = UTIL_ONCE_FLAG_INIT ;
99124static umf_ba_linear_pool_t * Base_alloc_leak = NULL ;
@@ -107,34 +132,86 @@ static __TLS int was_called_from_umfPool = 0;
107132/*** The constructor and destructor of the proxy library *********************/
108133/*****************************************************************************/
109134
135+ #ifndef _WIN32
136+ static size_t get_size_threshold (void ) {
137+ char * str_threshold = utils_env_var_get_str ("UMF_PROXY" , "size.threshold=" );
138+ long threshold = utils_get_size_threshold (str_threshold );
139+ if (threshold < 0 ) {
140+ LOG_ERR ("incorrect size threshold: %s" , str_threshold );
141+ exit (-1 );
142+ }
143+
144+ return (size_t )threshold ;
145+ }
146+
147+ static int get_system_allocator_symbols (void ) {
148+ * ((void * * )(& System_aligned_alloc )) =
149+ utils_get_symbol_addr (RTLD_NEXT , "aligned_alloc" , NULL );
150+ * ((void * * )(& System_calloc )) =
151+ utils_get_symbol_addr (RTLD_NEXT , "calloc" , NULL );
152+ * ((void * * )(& System_free )) = utils_get_symbol_addr (RTLD_NEXT , "free" , NULL );
153+ * ((void * * )(& System_malloc )) =
154+ utils_get_symbol_addr (RTLD_NEXT , "malloc" , NULL );
155+ * ((void * * )(& System_malloc_usable_size )) =
156+ utils_get_symbol_addr (RTLD_NEXT , "malloc_usable_size" , NULL );
157+ * ((void * * )(& System_realloc )) =
158+ utils_get_symbol_addr (RTLD_NEXT , "realloc" , NULL );
159+
160+ if (System_aligned_alloc && System_calloc && System_free && System_malloc &&
161+ System_malloc_usable_size && System_realloc ) {
162+ return 0 ;
163+ }
164+
165+ * ((void * * )(& System_aligned_alloc )) = NULL ;
166+ * ((void * * )(& System_calloc )) = NULL ;
167+ * ((void * * )(& System_free )) = NULL ;
168+ * ((void * * )(& System_malloc )) = NULL ;
169+ * ((void * * )(& System_malloc_usable_size )) = NULL ;
170+ * ((void * * )(& System_realloc )) = NULL ;
171+
172+ return -1 ;
173+ }
174+ #endif /* _WIN32 */
175+
110176void proxy_lib_create_common (void ) {
111177 utils_log_init ();
112178 umf_os_memory_provider_params_t os_params =
113179 umfOsMemoryProviderParamsDefault ();
114180 umf_result_t umf_result ;
115181
116182#ifndef _WIN32
117- char shm_name [NAME_MAX ];
183+ size_t _threshold = get_size_threshold ();
184+ if (_threshold > 0 ) {
185+ if (get_system_allocator_symbols ()) {
186+ LOG_ERR ("initialization of the system allocator failed!" );
187+ exit (-1 );
188+ }
189+
190+ Size_threshold_value = _threshold ;
191+ LOG_INFO ("system allocator initialized, size threshold value = %zu" ,
192+ Size_threshold_value );
193+ }
118194
119195 if (utils_env_var_has_str ("UMF_PROXY" , "page.disposition=shared-fd" )) {
120- LOG_DEBUG ("proxy_lib: using the MAP_SHARED visibility mode with the "
121- "file descriptor duplication" );
196+ LOG_INFO ("proxy_lib: using the MAP_SHARED visibility mode with the "
197+ "file descriptor duplication" );
122198 os_params .visibility = UMF_MEM_MAP_SHARED ;
123199 os_params .shm_name = NULL ;
124200
125201 } else if (utils_env_var_has_str ("UMF_PROXY" ,
126202 "page.disposition=shared-shm" )) {
127203 os_params .visibility = UMF_MEM_MAP_SHARED ;
128204
205+ char shm_name [NAME_MAX ];
129206 memset (shm_name , 0 , NAME_MAX );
130207 sprintf (shm_name , "umf_proxy_lib_shm_pid_%i" , utils_getpid ());
131208 os_params .shm_name = shm_name ;
132209
133- LOG_DEBUG ("proxy_lib: using the MAP_SHARED visibility mode with the "
134- "named shared memory: %s" ,
135- os_params .shm_name );
210+ LOG_INFO ("proxy_lib: using the MAP_SHARED visibility mode with the "
211+ "named shared memory: %s" ,
212+ os_params .shm_name );
136213 }
137- #endif
214+ #endif /* _WIN32 */
138215
139216 umf_result = umfMemoryProviderCreate (umfOsMemoryProviderOps (), & os_params ,
140217 & OS_memory_provider );
@@ -149,16 +226,18 @@ void proxy_lib_create_common(void) {
149226 LOG_ERR ("creating UMF pool manager failed" );
150227 exit (-1 );
151228 }
229+
152230 // The UMF pool has just been created (Proxy_pool != NULL). Stop using
153231 // the linear allocator and start using the UMF pool allocator from now on.
232+ LOG_DEBUG ("proxy library initialized" );
154233}
155234
156235void proxy_lib_destroy_common (void ) {
157236 if (utils_is_running_in_proxy_lib ()) {
158237 // We cannot destroy 'Base_alloc_leak' nor 'Proxy_pool' nor 'OS_memory_provider',
159238 // because it could lead to use-after-free in the program's unloader
160239 // (for example _dl_fini() on Linux).
161- return ;
240+ goto fini_proxy_lib_destroy_common ;
162241 }
163242
164243 umf_memory_pool_handle_t pool = Proxy_pool ;
@@ -168,6 +247,10 @@ void proxy_lib_destroy_common(void) {
168247 umf_memory_provider_handle_t provider = OS_memory_provider ;
169248 OS_memory_provider = NULL ;
170249 umfMemoryProviderDestroy (provider );
250+ LOG_DEBUG ("proxy library destroyed" );
251+
252+ fini_proxy_lib_destroy_common :
253+ LOG_DEBUG ("proxy library finalized" );
171254}
172255
173256/*****************************************************************************/
@@ -246,6 +329,12 @@ static inline size_t ba_leak_pool_contains_pointer(void *ptr) {
246329/*****************************************************************************/
247330
248331void * malloc (size_t size ) {
332+ #ifndef _WIN32
333+ if (size < Size_threshold_value ) {
334+ return System_malloc (size );
335+ }
336+ #endif /* _WIN32 */
337+
249338 if (!was_called_from_umfPool && Proxy_pool ) {
250339 was_called_from_umfPool = 1 ;
251340 void * ptr = umfPoolMalloc (Proxy_pool , size );
@@ -257,6 +346,12 @@ void *malloc(size_t size) {
257346}
258347
259348void * calloc (size_t nmemb , size_t size ) {
349+ #ifndef _WIN32
350+ if ((nmemb * size ) < Size_threshold_value ) {
351+ return System_calloc (nmemb , size );
352+ }
353+ #endif /* _WIN32 */
354+
260355 if (!was_called_from_umfPool && Proxy_pool ) {
261356 was_called_from_umfPool = 1 ;
262357 void * ptr = umfPoolCalloc (Proxy_pool , nmemb , size );
@@ -276,15 +371,22 @@ void free(void *ptr) {
276371 return ;
277372 }
278373
279- if (Proxy_pool ) {
374+ if (Proxy_pool && ( umfPoolByPtr ( ptr ) == Proxy_pool ) ) {
280375 if (umfPoolFree (Proxy_pool , ptr ) != UMF_RESULT_SUCCESS ) {
281376 LOG_ERR ("umfPoolFree() failed" );
282- assert (0 );
283377 }
284378 return ;
285379 }
286380
287- assert (0 );
381+ #ifndef _WIN32
382+ if (Size_threshold_value ) {
383+ System_free (ptr );
384+ return ;
385+ }
386+ #endif /* _WIN32 */
387+
388+ LOG_ERR ("free() failed: %p" , ptr );
389+
288390 return ;
289391}
290392
@@ -303,18 +405,31 @@ void *realloc(void *ptr, size_t size) {
303405 return ba_leak_realloc (ptr , size , leak_pool_contains_pointer );
304406 }
305407
306- if (Proxy_pool ) {
408+ if (Proxy_pool && ( umfPoolByPtr ( ptr ) == Proxy_pool ) ) {
307409 was_called_from_umfPool = 1 ;
308410 void * new_ptr = umfPoolRealloc (Proxy_pool , ptr , size );
309411 was_called_from_umfPool = 0 ;
310412 return new_ptr ;
311413 }
312414
313- assert (0 );
415+ #ifndef _WIN32
416+ if (Size_threshold_value ) {
417+ return System_realloc (ptr , size );
418+ }
419+ #endif /* _WIN32 */
420+
421+ LOG_ERR ("realloc() failed: %p" , ptr );
422+
314423 return NULL ;
315424}
316425
317426void * aligned_alloc (size_t alignment , size_t size ) {
427+ #ifndef _WIN32
428+ if (size < Size_threshold_value ) {
429+ return System_aligned_alloc (alignment , size );
430+ }
431+ #endif /* _WIN32 */
432+
318433 if (!was_called_from_umfPool && Proxy_pool ) {
319434 was_called_from_umfPool = 1 ;
320435 void * ptr = umfPoolAlignedMalloc (Proxy_pool , size , alignment );
@@ -330,19 +445,30 @@ size_t _msize(void *ptr) {
330445#else
331446size_t malloc_usable_size (void * ptr ) {
332447#endif
333-
334- // a check to verify we are running the proxy library
448+ // a check to verify if we are running the proxy library
335449 if (ptr == (void * )0x01 ) {
336450 return 0xDEADBEEF ;
337451 }
338452
339- if (!was_called_from_umfPool && Proxy_pool ) {
453+ if (ba_leak_pool_contains_pointer (ptr )) {
454+ return 0 ; // unsupported in case of the ba_leak allocator
455+ }
456+
457+ if (Proxy_pool && (umfPoolByPtr (ptr ) == Proxy_pool )) {
340458 was_called_from_umfPool = 1 ;
341459 size_t size = umfPoolMallocUsableSize (Proxy_pool , ptr );
342460 was_called_from_umfPool = 0 ;
343461 return size ;
344462 }
345463
464+ #ifndef _WIN32
465+ if (Size_threshold_value ) {
466+ return System_malloc_usable_size (ptr );
467+ }
468+ #endif /* _WIN32 */
469+
470+ LOG_ERR ("malloc_usable_size() failed: %p" , ptr );
471+
346472 return 0 ; // unsupported in this case
347473}
348474
0 commit comments