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/arch.h>
27 #include <lttng/ust-libc-wrapper.h>
29 #include "common/macros.h"
30 #include "common/align.h"
32 #define LTTNG_UST_TRACEPOINT_HIDDEN_DEFINITION
33 #define LTTNG_UST_TRACEPOINT_PROVIDER_HIDDEN_DEFINITION
35 #define LTTNG_UST_TRACEPOINT_DEFINE
36 #define LTTNG_UST_TRACEPOINT_CREATE_PROBES
37 #define LTTNG_UST_TP_IP_PARAM ip
40 #define STATIC_CALLOC_LEN 4096
41 static char static_calloc_buf
[STATIC_CALLOC_LEN
];
42 static unsigned long static_calloc_buf_offset
;
44 struct alloc_functions
{
45 void *(*calloc
)(size_t nmemb
, size_t size
);
46 void *(*malloc
)(size_t size
);
47 void (*free
)(void *ptr
);
48 void *(*realloc
)(void *ptr
, size_t size
);
49 void *(*memalign
)(size_t alignment
, size_t size
);
50 int (*posix_memalign
)(void **memptr
, size_t alignment
, size_t size
);
54 struct alloc_functions cur_alloc
;
57 * Make sure our own use of the LTS compat layer will not cause infinite
58 * recursion by calling calloc.
62 void *static_calloc(size_t nmemb
, size_t size
);
65 * pthread mutex replacement for URCU tls compat layer.
67 static int ust_malloc_lock
;
70 void ust_malloc_spin_lock(pthread_mutex_t
*lock
)
71 __attribute__((unused
));
73 void ust_malloc_spin_lock(pthread_mutex_t
*lock
__attribute__((unused
)))
76 * The memory barrier within cmpxchg takes care of ordering
77 * memory accesses with respect to the start of the critical
80 while (uatomic_cmpxchg(&ust_malloc_lock
, 0, 1) != 0)
85 void ust_malloc_spin_unlock(pthread_mutex_t
*lock
)
86 __attribute__((unused
));
88 void ust_malloc_spin_unlock(pthread_mutex_t
*lock
__attribute__((unused
)))
91 * Ensure memory accesses within the critical section do not
95 uatomic_set(&ust_malloc_lock
, 0);
99 * Use initial-exec TLS model for the malloc_nesting nesting guard
100 * variable to ensure that the glibc implementation of the TLS access
101 * don't trigger infinite recursion by calling the memory allocator
102 * wrapper functions, which could happen with global-dynamic.
104 static __thread
__attribute__((tls_model("initial-exec"))) int malloc_nesting
;
107 * Static allocator to use when initially executing dlsym(). It keeps a
108 * size_t value of each object size prior to the object.
111 void *static_calloc_aligned(size_t nmemb
, size_t size
, size_t alignment
)
113 size_t prev_offset
, new_offset
, res_offset
, aligned_offset
;
115 if (nmemb
* size
== 0) {
120 * Protect static_calloc_buf_offset from concurrent updates
121 * using a cmpxchg loop rather than a mutex to remove a
122 * dependency on pthread. This will minimize the risk of bad
123 * interaction between mutex and malloc instrumentation.
125 res_offset
= CMM_LOAD_SHARED(static_calloc_buf_offset
);
127 prev_offset
= res_offset
;
128 aligned_offset
= LTTNG_UST_ALIGN(prev_offset
+ sizeof(size_t), alignment
);
129 new_offset
= aligned_offset
+ nmemb
* size
;
130 if (new_offset
> sizeof(static_calloc_buf
)) {
133 } while ((res_offset
= uatomic_cmpxchg(&static_calloc_buf_offset
,
134 prev_offset
, new_offset
)) != prev_offset
);
135 *(size_t *) &static_calloc_buf
[aligned_offset
- sizeof(size_t)] = size
;
136 return &static_calloc_buf
[aligned_offset
];
140 void *static_calloc(size_t nmemb
, size_t size
)
144 retval
= static_calloc_aligned(nmemb
, size
, 1);
149 void *static_malloc(size_t size
)
153 retval
= static_calloc_aligned(1, size
, 1);
158 void static_free(void *ptr
__attribute__((unused
)))
164 void *static_realloc(void *ptr
, size_t size
)
166 size_t *old_size
= NULL
;
175 old_size
= (size_t *) ptr
- 1;
176 if (size
<= *old_size
) {
177 /* We can re-use the old entry. */
183 /* We need to expand. Don't free previous memory location. */
184 retval
= static_calloc_aligned(1, size
, 1);
187 memcpy(retval
, ptr
, *old_size
);
193 void *static_memalign(size_t alignment
, size_t size
)
197 retval
= static_calloc_aligned(1, size
, alignment
);
202 int static_posix_memalign(void **memptr
, size_t alignment
, size_t size
)
206 /* Check for power of 2, larger than void *. */
207 if (alignment
& (alignment
- 1)
208 || alignment
< sizeof(void *)
212 ptr
= static_calloc_aligned(1, size
, alignment
);
219 void setup_static_allocator(void)
221 assert(cur_alloc
.calloc
== NULL
);
222 cur_alloc
.calloc
= static_calloc
;
223 assert(cur_alloc
.malloc
== NULL
);
224 cur_alloc
.malloc
= static_malloc
;
225 assert(cur_alloc
.free
== NULL
);
226 cur_alloc
.free
= static_free
;
227 assert(cur_alloc
.realloc
== NULL
);
228 cur_alloc
.realloc
= static_realloc
;
229 assert(cur_alloc
.memalign
== NULL
);
230 cur_alloc
.memalign
= static_memalign
;
231 assert(cur_alloc
.posix_memalign
== NULL
);
232 cur_alloc
.posix_memalign
= static_posix_memalign
;
236 void lookup_all_symbols(void)
238 struct alloc_functions af
;
241 * Temporarily redirect allocation functions to
242 * static_calloc_aligned, and free function to static_free
243 * (no-op), until the dlsym lookup has completed.
245 setup_static_allocator();
247 /* Perform the actual lookups */
248 af
.calloc
= dlsym(RTLD_NEXT
, "calloc");
249 af
.malloc
= dlsym(RTLD_NEXT
, "malloc");
250 af
.free
= dlsym(RTLD_NEXT
, "free");
251 af
.realloc
= dlsym(RTLD_NEXT
, "realloc");
252 af
.memalign
= dlsym(RTLD_NEXT
, "memalign");
253 af
.posix_memalign
= dlsym(RTLD_NEXT
, "posix_memalign");
255 /* Populate the new allocator functions */
256 memcpy(&cur_alloc
, &af
, sizeof(cur_alloc
));
259 void *malloc(size_t size
)
264 if (cur_alloc
.malloc
== NULL
) {
265 lookup_all_symbols();
266 if (cur_alloc
.malloc
== NULL
) {
267 fprintf(stderr
, "mallocwrap: unable to find malloc\n");
271 retval
= cur_alloc
.malloc(size
);
272 if (malloc_nesting
== 1) {
273 lttng_ust_tracepoint(lttng_ust_libc
, malloc
,
274 size
, retval
, LTTNG_UST_CALLER_IP());
284 * Check whether the memory was allocated with
285 * static_calloc_align, in which case there is nothing to free.
287 if (caa_unlikely((char *)ptr
>= static_calloc_buf
&&
288 (char *)ptr
< static_calloc_buf
+ STATIC_CALLOC_LEN
)) {
292 if (malloc_nesting
== 1) {
293 lttng_ust_tracepoint(lttng_ust_libc
, free
,
294 ptr
, LTTNG_UST_CALLER_IP());
297 if (cur_alloc
.free
== NULL
) {
298 lookup_all_symbols();
299 if (cur_alloc
.free
== NULL
) {
300 fprintf(stderr
, "mallocwrap: unable to find free\n");
309 void *calloc(size_t nmemb
, size_t size
)
314 if (cur_alloc
.calloc
== NULL
) {
315 lookup_all_symbols();
316 if (cur_alloc
.calloc
== NULL
) {
317 fprintf(stderr
, "callocwrap: unable to find calloc\n");
321 retval
= cur_alloc
.calloc(nmemb
, size
);
322 if (malloc_nesting
== 1) {
323 lttng_ust_tracepoint(lttng_ust_libc
, calloc
,
324 nmemb
, size
, retval
, LTTNG_UST_CALLER_IP());
330 void *realloc(void *ptr
, size_t size
)
336 * Check whether the memory was allocated with
337 * static_calloc_align, in which case there is nothing
338 * to free, and we need to copy the old data.
340 if (caa_unlikely((char *)ptr
>= static_calloc_buf
&&
341 (char *)ptr
< static_calloc_buf
+ STATIC_CALLOC_LEN
)) {
344 old_size
= (size_t *) ptr
- 1;
345 if (cur_alloc
.calloc
== NULL
) {
346 lookup_all_symbols();
347 if (cur_alloc
.calloc
== NULL
) {
348 fprintf(stderr
, "reallocwrap: unable to find calloc\n");
352 retval
= cur_alloc
.calloc(1, size
);
354 memcpy(retval
, ptr
, *old_size
);
357 * Mimic that a NULL pointer has been received, so
358 * memory allocation analysis based on the trace don't
359 * get confused by the address from the static
366 if (cur_alloc
.realloc
== NULL
) {
367 lookup_all_symbols();
368 if (cur_alloc
.realloc
== NULL
) {
369 fprintf(stderr
, "reallocwrap: unable to find realloc\n");
373 retval
= cur_alloc
.realloc(ptr
, size
);
375 if (malloc_nesting
== 1) {
376 lttng_ust_tracepoint(lttng_ust_libc
, realloc
,
377 ptr
, size
, retval
, LTTNG_UST_CALLER_IP());
383 void *memalign(size_t alignment
, size_t size
)
388 if (cur_alloc
.memalign
== NULL
) {
389 lookup_all_symbols();
390 if (cur_alloc
.memalign
== NULL
) {
391 fprintf(stderr
, "memalignwrap: unable to find memalign\n");
395 retval
= cur_alloc
.memalign(alignment
, size
);
396 if (malloc_nesting
== 1) {
397 lttng_ust_tracepoint(lttng_ust_libc
, memalign
,
398 alignment
, size
, retval
,
399 LTTNG_UST_CALLER_IP());
405 int posix_memalign(void **memptr
, size_t alignment
, size_t size
)
410 if (cur_alloc
.posix_memalign
== NULL
) {
411 lookup_all_symbols();
412 if (cur_alloc
.posix_memalign
== NULL
) {
413 fprintf(stderr
, "posix_memalignwrap: unable to find posix_memalign\n");
417 retval
= cur_alloc
.posix_memalign(memptr
, alignment
, size
);
418 if (malloc_nesting
== 1) {
419 lttng_ust_tracepoint(lttng_ust_libc
, posix_memalign
,
420 *memptr
, alignment
, size
,
421 retval
, LTTNG_UST_CALLER_IP());
427 void lttng_ust_libc_wrapper_malloc_ctor(void)
429 /* Initialization already done */
430 if (cur_alloc
.calloc
) {
434 * Ensure the allocator is in place before the process becomes
437 lookup_all_symbols();