27
27
* - _aligned_offset_recalloc()
28
28
*/
29
29
30
+ #ifndef _WIN32
31
+ #define _GNU_SOURCE // for RTLD_NEXT
32
+ #include <dlfcn.h>
33
+ #undef _GNU_SOURCE
34
+ #endif /* _WIN32 */
35
+
30
36
#if (defined PROXY_LIB_USES_JEMALLOC_POOL )
31
37
#include <umf/pools/pool_jemalloc.h>
32
38
#define umfPoolManagerOps umfJemallocPoolOps
48
54
#include "base_alloc_linear.h"
49
55
#include "proxy_lib.h"
50
56
#include "utils_common.h"
57
+ #include "utils_load_library.h"
51
58
#include "utils_log.h"
52
59
53
60
#ifdef _WIN32 /* Windows ***************************************/
@@ -94,6 +101,24 @@ void utils_init_once(UTIL_ONCE_FLAG *flag, void (*onceCb)(void));
94
101
* of a UMF pool to allocate memory needed by an application. It should be freed
95
102
* by an application.
96
103
*/
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 */
97
122
98
123
static UTIL_ONCE_FLAG Base_alloc_leak_initialized = UTIL_ONCE_FLAG_INIT ;
99
124
static umf_ba_linear_pool_t * Base_alloc_leak = NULL ;
@@ -107,34 +132,97 @@ static __TLS int was_called_from_umfPool = 0;
107
132
/*** The constructor and destructor of the proxy library *********************/
108
133
/*****************************************************************************/
109
134
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
+ if (!str_threshold ) {
139
+ return 0 ;
140
+ }
141
+
142
+ // move to the beginning of the number
143
+ str_threshold += strlen ("size.threshold=" );
144
+ // find ';' at the end
145
+ char * end = strstr (str_threshold , ";" );
146
+ if (end ) {
147
+ // replace ';' with '\0' to mark end of the string
148
+ * end = '\0' ;
149
+ }
150
+
151
+ size_t int_threshold = (size_t )atoi (str_threshold );
152
+ LOG_DEBUG ("Size_threshold_value = (char *) %s, (int) %zu" , str_threshold ,
153
+ int_threshold );
154
+
155
+ return int_threshold ;
156
+ }
157
+
158
+ static int get_system_allocator_symbols (void ) {
159
+ * ((void * * )(& System_aligned_alloc )) =
160
+ utils_get_symbol_addr (RTLD_NEXT , "aligned_alloc" , NULL );
161
+ * ((void * * )(& System_calloc )) =
162
+ utils_get_symbol_addr (RTLD_NEXT , "calloc" , NULL );
163
+ * ((void * * )(& System_free )) = utils_get_symbol_addr (RTLD_NEXT , "free" , NULL );
164
+ * ((void * * )(& System_malloc )) =
165
+ utils_get_symbol_addr (RTLD_NEXT , "malloc" , NULL );
166
+ * ((void * * )(& System_malloc_usable_size )) =
167
+ utils_get_symbol_addr (RTLD_NEXT , "malloc_usable_size" , NULL );
168
+ * ((void * * )(& System_realloc )) =
169
+ utils_get_symbol_addr (RTLD_NEXT , "realloc" , NULL );
170
+
171
+ if (System_aligned_alloc && System_calloc && System_free && System_malloc &&
172
+ System_malloc_usable_size && System_realloc ) {
173
+ return 0 ;
174
+ }
175
+
176
+ * ((void * * )(& System_aligned_alloc )) = NULL ;
177
+ * ((void * * )(& System_calloc )) = NULL ;
178
+ * ((void * * )(& System_free )) = NULL ;
179
+ * ((void * * )(& System_malloc )) = NULL ;
180
+ * ((void * * )(& System_malloc_usable_size )) = NULL ;
181
+ * ((void * * )(& System_realloc )) = NULL ;
182
+
183
+ return -1 ;
184
+ }
185
+ #endif /* _WIN32 */
186
+
110
187
void proxy_lib_create_common (void ) {
111
188
utils_log_init ();
112
189
umf_os_memory_provider_params_t os_params =
113
190
umfOsMemoryProviderParamsDefault ();
114
191
umf_result_t umf_result ;
115
192
116
193
#ifndef _WIN32
117
- char shm_name [NAME_MAX ];
194
+ size_t _threshold = get_size_threshold ();
195
+ if (_threshold > 0 ) {
196
+ if (get_system_allocator_symbols ()) {
197
+ LOG_ERR ("initialization of the system allocator failed!" );
198
+ exit (-1 );
199
+ }
200
+
201
+ Size_threshold_value = _threshold ;
202
+ LOG_INFO ("system allocator initialized, size threshold value = %zu" ,
203
+ Size_threshold_value );
204
+ }
118
205
119
206
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" );
207
+ LOG_INFO ("proxy_lib: using the MAP_SHARED visibility mode with the "
208
+ "file descriptor duplication" );
122
209
os_params .visibility = UMF_MEM_MAP_SHARED ;
123
210
os_params .shm_name = NULL ;
124
211
125
212
} else if (utils_env_var_has_str ("UMF_PROXY" ,
126
213
"page.disposition=shared-shm" )) {
127
214
os_params .visibility = UMF_MEM_MAP_SHARED ;
128
215
216
+ char shm_name [NAME_MAX ];
129
217
memset (shm_name , 0 , NAME_MAX );
130
218
sprintf (shm_name , "umf_proxy_lib_shm_pid_%i" , utils_getpid ());
131
219
os_params .shm_name = shm_name ;
132
220
133
- LOG_DEBUG ("proxy_lib: using the MAP_SHARED visibility mode with the "
134
- "named shared memory: %s" ,
135
- os_params .shm_name );
221
+ LOG_INFO ("proxy_lib: using the MAP_SHARED visibility mode with the "
222
+ "named shared memory: %s" ,
223
+ os_params .shm_name );
136
224
}
137
- #endif
225
+ #endif /* _WIN32 */
138
226
139
227
umf_result = umfMemoryProviderCreate (umfOsMemoryProviderOps (), & os_params ,
140
228
& OS_memory_provider );
@@ -149,16 +237,18 @@ void proxy_lib_create_common(void) {
149
237
LOG_ERR ("creating UMF pool manager failed" );
150
238
exit (-1 );
151
239
}
240
+
152
241
// The UMF pool has just been created (Proxy_pool != NULL). Stop using
153
242
// the linear allocator and start using the UMF pool allocator from now on.
243
+ LOG_DEBUG ("proxy library initialized" );
154
244
}
155
245
156
246
void proxy_lib_destroy_common (void ) {
157
247
if (utils_is_running_in_proxy_lib ()) {
158
248
// We cannot destroy 'Base_alloc_leak' nor 'Proxy_pool' nor 'OS_memory_provider',
159
249
// because it could lead to use-after-free in the program's unloader
160
250
// (for example _dl_fini() on Linux).
161
- return ;
251
+ goto fini_proxy_lib_destroy_common ;
162
252
}
163
253
164
254
umf_memory_pool_handle_t pool = Proxy_pool ;
@@ -168,6 +258,10 @@ void proxy_lib_destroy_common(void) {
168
258
umf_memory_provider_handle_t provider = OS_memory_provider ;
169
259
OS_memory_provider = NULL ;
170
260
umfMemoryProviderDestroy (provider );
261
+ LOG_DEBUG ("proxy library destroyed" );
262
+
263
+ fini_proxy_lib_destroy_common :
264
+ LOG_DEBUG ("proxy library finalized" );
171
265
}
172
266
173
267
/*****************************************************************************/
@@ -246,6 +340,12 @@ static inline size_t ba_leak_pool_contains_pointer(void *ptr) {
246
340
/*****************************************************************************/
247
341
248
342
void * malloc (size_t size ) {
343
+ #ifndef _WIN32
344
+ if (size < Size_threshold_value ) {
345
+ return System_malloc (size );
346
+ }
347
+ #endif /* _WIN32 */
348
+
249
349
if (!was_called_from_umfPool && Proxy_pool ) {
250
350
was_called_from_umfPool = 1 ;
251
351
void * ptr = umfPoolMalloc (Proxy_pool , size );
@@ -257,6 +357,12 @@ void *malloc(size_t size) {
257
357
}
258
358
259
359
void * calloc (size_t nmemb , size_t size ) {
360
+ #ifndef _WIN32
361
+ if ((nmemb * size ) < Size_threshold_value ) {
362
+ return System_calloc (nmemb , size );
363
+ }
364
+ #endif /* _WIN32 */
365
+
260
366
if (!was_called_from_umfPool && Proxy_pool ) {
261
367
was_called_from_umfPool = 1 ;
262
368
void * ptr = umfPoolCalloc (Proxy_pool , nmemb , size );
@@ -276,15 +382,22 @@ void free(void *ptr) {
276
382
return ;
277
383
}
278
384
279
- if (Proxy_pool ) {
385
+ if (Proxy_pool && ( umfPoolByPtr ( ptr ) == Proxy_pool ) ) {
280
386
if (umfPoolFree (Proxy_pool , ptr ) != UMF_RESULT_SUCCESS ) {
281
387
LOG_ERR ("umfPoolFree() failed" );
282
- assert (0 );
283
388
}
284
389
return ;
285
390
}
286
391
287
- assert (0 );
392
+ #ifndef _WIN32
393
+ if (Size_threshold_value ) {
394
+ System_free (ptr );
395
+ return ;
396
+ }
397
+ #endif /* _WIN32 */
398
+
399
+ LOG_ERR ("free() failed: %p" , ptr );
400
+
288
401
return ;
289
402
}
290
403
@@ -303,18 +416,31 @@ void *realloc(void *ptr, size_t size) {
303
416
return ba_leak_realloc (ptr , size , leak_pool_contains_pointer );
304
417
}
305
418
306
- if (Proxy_pool ) {
419
+ if (Proxy_pool && ( umfPoolByPtr ( ptr ) == Proxy_pool ) ) {
307
420
was_called_from_umfPool = 1 ;
308
421
void * new_ptr = umfPoolRealloc (Proxy_pool , ptr , size );
309
422
was_called_from_umfPool = 0 ;
310
423
return new_ptr ;
311
424
}
312
425
313
- assert (0 );
426
+ #ifndef _WIN32
427
+ if (Size_threshold_value ) {
428
+ return System_realloc (ptr , size );
429
+ }
430
+ #endif /* _WIN32 */
431
+
432
+ LOG_ERR ("realloc() failed: %p" , ptr );
433
+
314
434
return NULL ;
315
435
}
316
436
317
437
void * aligned_alloc (size_t alignment , size_t size ) {
438
+ #ifndef _WIN32
439
+ if (size < Size_threshold_value ) {
440
+ return System_aligned_alloc (alignment , size );
441
+ }
442
+ #endif /* _WIN32 */
443
+
318
444
if (!was_called_from_umfPool && Proxy_pool ) {
319
445
was_called_from_umfPool = 1 ;
320
446
void * ptr = umfPoolAlignedMalloc (Proxy_pool , size , alignment );
@@ -330,19 +456,30 @@ size_t _msize(void *ptr) {
330
456
#else
331
457
size_t malloc_usable_size (void * ptr ) {
332
458
#endif
333
-
334
- // a check to verify we are running the proxy library
459
+ // a check to verify if we are running the proxy library
335
460
if (ptr == (void * )0x01 ) {
336
461
return 0xDEADBEEF ;
337
462
}
338
463
339
- if (!was_called_from_umfPool && Proxy_pool ) {
464
+ if (ba_leak_pool_contains_pointer (ptr )) {
465
+ return 0 ; // unsupported in case of the ba_leak allocator
466
+ }
467
+
468
+ if (Proxy_pool && (umfPoolByPtr (ptr ) == Proxy_pool )) {
340
469
was_called_from_umfPool = 1 ;
341
470
size_t size = umfPoolMallocUsableSize (Proxy_pool , ptr );
342
471
was_called_from_umfPool = 0 ;
343
472
return size ;
344
473
}
345
474
475
+ #ifndef _WIN32
476
+ if (Size_threshold_value ) {
477
+ return System_malloc_usable_size (ptr );
478
+ }
479
+ #endif /* _WIN32 */
480
+
481
+ LOG_ERR ("malloc_usable_size() failed: %p" , ptr );
482
+
346
483
return 0 ; // unsupported in this case
347
484
}
348
485
0 commit comments