2 * SPDX-License-Identifier: LGPL-2.1-or-later
4 * Copyright (C) 2009 Pierre-Marc Fournier
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Do _not_ define _LGPL_SOURCE because we don't want to create a
10 * circular dependency loop between this malloc wrapper, liburcu and
14 /* Has to be included first to override dlfcn.h */
15 #include <common/compat/dlfcn.h>
17 #include <sys/types.h>
22 #include <urcu/system.h>
23 #include <urcu/uatomic.h>
24 #include <urcu/compiler.h>
25 #include <urcu/tls-compat.h>
26 #include <urcu/arch.h>
28 #include <lttng/ust-libc-wrapper.h>
30 #include "common/macros.h"
31 #include "common/align.h"
33 #define LTTNG_UST_TRACEPOINT_DEFINE
34 #define LTTNG_UST_TRACEPOINT_CREATE_PROBES
35 #define TP_IP_PARAM ip
38 #define STATIC_CALLOC_LEN 4096
39 static char static_calloc_buf
[STATIC_CALLOC_LEN
];
40 static unsigned long static_calloc_buf_offset
;
42 struct alloc_functions
{
43 void *(*calloc
)(size_t nmemb
, size_t size
);
44 void *(*malloc
)(size_t size
);
45 void (*free
)(void *ptr
);
46 void *(*realloc
)(void *ptr
, size_t size
);
47 void *(*memalign
)(size_t alignment
, size_t size
);
48 int (*posix_memalign
)(void **memptr
, size_t alignment
, size_t size
);
52 struct alloc_functions cur_alloc
;
55 * Make sure our own use of the LTS compat layer will not cause infinite
56 * recursion by calling calloc.
60 void *static_calloc(size_t nmemb
, size_t size
);
63 * pthread mutex replacement for URCU tls compat layer.
65 static int ust_malloc_lock
;
68 void ust_malloc_spin_lock(pthread_mutex_t
*lock
)
69 __attribute__((unused
));
71 void ust_malloc_spin_lock(pthread_mutex_t
*lock
__attribute__((unused
)))
74 * The memory barrier within cmpxchg takes care of ordering
75 * memory accesses with respect to the start of the critical
78 while (uatomic_cmpxchg(&ust_malloc_lock
, 0, 1) != 0)
83 void ust_malloc_spin_unlock(pthread_mutex_t
*lock
)
84 __attribute__((unused
));
86 void ust_malloc_spin_unlock(pthread_mutex_t
*lock
__attribute__((unused
)))
89 * Ensure memory accesses within the critical section do not
93 uatomic_set(&ust_malloc_lock
, 0);
96 #define calloc static_calloc
97 #define pthread_mutex_lock ust_malloc_spin_lock
98 #define pthread_mutex_unlock ust_malloc_spin_unlock
99 static DEFINE_URCU_TLS(int, malloc_nesting
);
100 #undef pthread_mutex_unlock
101 #undef pthread_mutex_lock
105 * Static allocator to use when initially executing dlsym(). It keeps a
106 * size_t value of each object size prior to the object.
109 void *static_calloc_aligned(size_t nmemb
, size_t size
, size_t alignment
)
111 size_t prev_offset
, new_offset
, res_offset
, aligned_offset
;
113 if (nmemb
* size
== 0) {
118 * Protect static_calloc_buf_offset from concurrent updates
119 * using a cmpxchg loop rather than a mutex to remove a
120 * dependency on pthread. This will minimize the risk of bad
121 * interaction between mutex and malloc instrumentation.
123 res_offset
= CMM_LOAD_SHARED(static_calloc_buf_offset
);
125 prev_offset
= res_offset
;
126 aligned_offset
= LTTNG_UST_ALIGN(prev_offset
+ sizeof(size_t), alignment
);
127 new_offset
= aligned_offset
+ nmemb
* size
;
128 if (new_offset
> sizeof(static_calloc_buf
)) {
131 } while ((res_offset
= uatomic_cmpxchg(&static_calloc_buf_offset
,
132 prev_offset
, new_offset
)) != prev_offset
);
133 *(size_t *) &static_calloc_buf
[aligned_offset
- sizeof(size_t)] = size
;
134 return &static_calloc_buf
[aligned_offset
];
138 void *static_calloc(size_t nmemb
, size_t size
)
142 retval
= static_calloc_aligned(nmemb
, size
, 1);
147 void *static_malloc(size_t size
)
151 retval
= static_calloc_aligned(1, size
, 1);
156 void static_free(void *ptr
__attribute__((unused
)))
162 void *static_realloc(void *ptr
, size_t size
)
164 size_t *old_size
= NULL
;
173 old_size
= (size_t *) ptr
- 1;
174 if (size
<= *old_size
) {
175 /* We can re-use the old entry. */
181 /* We need to expand. Don't free previous memory location. */
182 retval
= static_calloc_aligned(1, size
, 1);
185 memcpy(retval
, ptr
, *old_size
);
191 void *static_memalign(size_t alignment
, size_t size
)
195 retval
= static_calloc_aligned(1, size
, alignment
);
200 int static_posix_memalign(void **memptr
, size_t alignment
, size_t size
)
204 /* Check for power of 2, larger than void *. */
205 if (alignment
& (alignment
- 1)
206 || alignment
< sizeof(void *)
210 ptr
= static_calloc_aligned(1, size
, alignment
);
217 void setup_static_allocator(void)
219 assert(cur_alloc
.calloc
== NULL
);
220 cur_alloc
.calloc
= static_calloc
;
221 assert(cur_alloc
.malloc
== NULL
);
222 cur_alloc
.malloc
= static_malloc
;
223 assert(cur_alloc
.free
== NULL
);
224 cur_alloc
.free
= static_free
;
225 assert(cur_alloc
.realloc
== NULL
);
226 cur_alloc
.realloc
= static_realloc
;
227 assert(cur_alloc
.memalign
== NULL
);
228 cur_alloc
.memalign
= static_memalign
;
229 assert(cur_alloc
.posix_memalign
== NULL
);
230 cur_alloc
.posix_memalign
= static_posix_memalign
;
234 void lookup_all_symbols(void)
236 struct alloc_functions af
;
239 * Temporarily redirect allocation functions to
240 * static_calloc_aligned, and free function to static_free
241 * (no-op), until the dlsym lookup has completed.
243 setup_static_allocator();
245 /* Perform the actual lookups */
246 af
.calloc
= dlsym(RTLD_NEXT
, "calloc");
247 af
.malloc
= dlsym(RTLD_NEXT
, "malloc");
248 af
.free
= dlsym(RTLD_NEXT
, "free");
249 af
.realloc
= dlsym(RTLD_NEXT
, "realloc");
250 af
.memalign
= dlsym(RTLD_NEXT
, "memalign");
251 af
.posix_memalign
= dlsym(RTLD_NEXT
, "posix_memalign");
253 /* Populate the new allocator functions */
254 memcpy(&cur_alloc
, &af
, sizeof(cur_alloc
));
257 void *malloc(size_t size
)
261 URCU_TLS(malloc_nesting
)++;
262 if (cur_alloc
.malloc
== NULL
) {
263 lookup_all_symbols();
264 if (cur_alloc
.malloc
== NULL
) {
265 fprintf(stderr
, "mallocwrap: unable to find malloc\n");
269 retval
= cur_alloc
.malloc(size
);
270 if (URCU_TLS(malloc_nesting
) == 1) {
271 lttng_ust_tracepoint(lttng_ust_libc
, malloc
,
272 size
, retval
, LTTNG_UST_CALLER_IP());
274 URCU_TLS(malloc_nesting
)--;
280 URCU_TLS(malloc_nesting
)++;
282 * Check whether the memory was allocated with
283 * static_calloc_align, in which case there is nothing to free.
285 if (caa_unlikely((char *)ptr
>= static_calloc_buf
&&
286 (char *)ptr
< static_calloc_buf
+ STATIC_CALLOC_LEN
)) {
290 if (URCU_TLS(malloc_nesting
) == 1) {
291 lttng_ust_tracepoint(lttng_ust_libc
, free
,
292 ptr
, LTTNG_UST_CALLER_IP());
295 if (cur_alloc
.free
== NULL
) {
296 lookup_all_symbols();
297 if (cur_alloc
.free
== NULL
) {
298 fprintf(stderr
, "mallocwrap: unable to find free\n");
304 URCU_TLS(malloc_nesting
)--;
307 void *calloc(size_t nmemb
, size_t size
)
311 URCU_TLS(malloc_nesting
)++;
312 if (cur_alloc
.calloc
== NULL
) {
313 lookup_all_symbols();
314 if (cur_alloc
.calloc
== NULL
) {
315 fprintf(stderr
, "callocwrap: unable to find calloc\n");
319 retval
= cur_alloc
.calloc(nmemb
, size
);
320 if (URCU_TLS(malloc_nesting
) == 1) {
321 lttng_ust_tracepoint(lttng_ust_libc
, calloc
,
322 nmemb
, size
, retval
, LTTNG_UST_CALLER_IP());
324 URCU_TLS(malloc_nesting
)--;
328 void *realloc(void *ptr
, size_t size
)
332 URCU_TLS(malloc_nesting
)++;
334 * Check whether the memory was allocated with
335 * static_calloc_align, in which case there is nothing
336 * to free, and we need to copy the old data.
338 if (caa_unlikely((char *)ptr
>= static_calloc_buf
&&
339 (char *)ptr
< static_calloc_buf
+ STATIC_CALLOC_LEN
)) {
342 old_size
= (size_t *) ptr
- 1;
343 if (cur_alloc
.calloc
== NULL
) {
344 lookup_all_symbols();
345 if (cur_alloc
.calloc
== NULL
) {
346 fprintf(stderr
, "reallocwrap: unable to find calloc\n");
350 retval
= cur_alloc
.calloc(1, size
);
352 memcpy(retval
, ptr
, *old_size
);
355 * Mimick that a NULL pointer has been received, so
356 * memory allocation analysis based on the trace don't
357 * get confused by the address from the static
364 if (cur_alloc
.realloc
== NULL
) {
365 lookup_all_symbols();
366 if (cur_alloc
.realloc
== NULL
) {
367 fprintf(stderr
, "reallocwrap: unable to find realloc\n");
371 retval
= cur_alloc
.realloc(ptr
, size
);
373 if (URCU_TLS(malloc_nesting
) == 1) {
374 lttng_ust_tracepoint(lttng_ust_libc
, realloc
,
375 ptr
, size
, retval
, LTTNG_UST_CALLER_IP());
377 URCU_TLS(malloc_nesting
)--;
381 void *memalign(size_t alignment
, size_t size
)
385 URCU_TLS(malloc_nesting
)++;
386 if (cur_alloc
.memalign
== NULL
) {
387 lookup_all_symbols();
388 if (cur_alloc
.memalign
== NULL
) {
389 fprintf(stderr
, "memalignwrap: unable to find memalign\n");
393 retval
= cur_alloc
.memalign(alignment
, size
);
394 if (URCU_TLS(malloc_nesting
) == 1) {
395 lttng_ust_tracepoint(lttng_ust_libc
, memalign
,
396 alignment
, size
, retval
,
397 LTTNG_UST_CALLER_IP());
399 URCU_TLS(malloc_nesting
)--;
403 int posix_memalign(void **memptr
, size_t alignment
, size_t size
)
407 URCU_TLS(malloc_nesting
)++;
408 if (cur_alloc
.posix_memalign
== NULL
) {
409 lookup_all_symbols();
410 if (cur_alloc
.posix_memalign
== NULL
) {
411 fprintf(stderr
, "posix_memalignwrap: unable to find posix_memalign\n");
415 retval
= cur_alloc
.posix_memalign(memptr
, alignment
, size
);
416 if (URCU_TLS(malloc_nesting
) == 1) {
417 lttng_ust_tracepoint(lttng_ust_libc
, posix_memalign
,
418 *memptr
, alignment
, size
,
419 retval
, LTTNG_UST_CALLER_IP());
421 URCU_TLS(malloc_nesting
)--;
426 void lttng_ust_fixup_malloc_nesting_tls(void)
428 asm volatile ("" : : "m" (URCU_TLS(malloc_nesting
)));
431 void lttng_ust_libc_wrapper_malloc_ctor(void)
433 /* Initialization already done */
434 if (cur_alloc
.calloc
) {
437 lttng_ust_fixup_malloc_nesting_tls();
439 * Ensure the allocator is in place before the process becomes
442 lookup_all_symbols();