2 * Copyright (C) 2009 Pierre-Marc Fournier
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <lttng/ust-dlfcn.h>
23 #include <sys/types.h>
26 #include <urcu/system.h>
27 #include <urcu/uatomic.h>
28 #include <urcu/compiler.h>
29 #include <urcu/tls-compat.h>
30 #include <urcu/arch.h>
31 #include <lttng/align.h>
34 #define TRACEPOINT_DEFINE
35 #define TRACEPOINT_CREATE_PROBES
36 #define TP_IP_PARAM ip
39 #define STATIC_CALLOC_LEN 4096
40 static char static_calloc_buf
[STATIC_CALLOC_LEN
];
41 static unsigned long static_calloc_buf_offset
;
43 struct alloc_functions
{
44 void *(*calloc
)(size_t nmemb
, size_t size
);
45 void *(*malloc
)(size_t size
);
46 void (*free
)(void *ptr
);
47 void *(*realloc
)(void *ptr
, size_t size
);
48 void *(*memalign
)(size_t alignment
, size_t size
);
49 int (*posix_memalign
)(void **memptr
, size_t alignment
, size_t size
);
53 struct alloc_functions cur_alloc
;
56 * Make sure our own use of the LTS compat layer will not cause infinite
57 * recursion by calling calloc.
61 void *static_calloc(size_t nmemb
, size_t size
);
64 * pthread mutex replacement for URCU tls compat layer.
66 static int ust_malloc_lock
;
68 static __attribute__((unused
))
69 void ust_malloc_spin_lock(pthread_mutex_t
*lock
)
72 * The memory barrier within cmpxchg takes care of ordering
73 * memory accesses with respect to the start of the critical
76 while (uatomic_cmpxchg(&ust_malloc_lock
, 0, 1) != 0)
80 static __attribute__((unused
))
81 void ust_malloc_spin_unlock(pthread_mutex_t
*lock
)
84 * Ensure memory accesses within the critical section do not
88 uatomic_set(&ust_malloc_lock
, 0);
91 #define calloc static_calloc
92 #define pthread_mutex_lock ust_malloc_spin_lock
93 #define pthread_mutex_unlock ust_malloc_spin_unlock
94 static DEFINE_URCU_TLS(int, malloc_nesting
);
95 #undef ust_malloc_spin_unlock
96 #undef ust_malloc_spin_lock
100 * Static allocator to use when initially executing dlsym(). It keeps a
101 * size_t value of each object size prior to the object.
104 void *static_calloc_aligned(size_t nmemb
, size_t size
, size_t alignment
)
106 size_t prev_offset
, new_offset
, res_offset
, aligned_offset
;
108 if (nmemb
* size
== 0) {
113 * Protect static_calloc_buf_offset from concurrent updates
114 * using a cmpxchg loop rather than a mutex to remove a
115 * dependency on pthread. This will minimize the risk of bad
116 * interaction between mutex and malloc instrumentation.
118 res_offset
= CMM_LOAD_SHARED(static_calloc_buf_offset
);
120 prev_offset
= res_offset
;
121 aligned_offset
= ALIGN(prev_offset
+ sizeof(size_t), alignment
);
122 new_offset
= aligned_offset
+ nmemb
* size
;
123 if (new_offset
> sizeof(static_calloc_buf
)) {
126 } while ((res_offset
= uatomic_cmpxchg(&static_calloc_buf_offset
,
127 prev_offset
, new_offset
)) != prev_offset
);
128 *(size_t *) &static_calloc_buf
[aligned_offset
- sizeof(size_t)] = size
;
129 return &static_calloc_buf
[aligned_offset
];
133 void *static_calloc(size_t nmemb
, size_t size
)
137 retval
= static_calloc_aligned(nmemb
, size
, 1);
142 void *static_malloc(size_t size
)
146 retval
= static_calloc_aligned(1, size
, 1);
151 void static_free(void *ptr
)
157 void *static_realloc(void *ptr
, size_t size
)
159 size_t *old_size
= NULL
;
168 old_size
= (size_t *) ptr
- 1;
169 if (size
<= *old_size
) {
170 /* We can re-use the old entry. */
176 /* We need to expand. Don't free previous memory location. */
177 retval
= static_calloc_aligned(1, size
, 1);
180 memcpy(retval
, ptr
, *old_size
);
186 void *static_memalign(size_t alignment
, size_t size
)
190 retval
= static_calloc_aligned(1, size
, alignment
);
195 int static_posix_memalign(void **memptr
, size_t alignment
, size_t size
)
199 /* Check for power of 2, larger than void *. */
200 if (alignment
& (alignment
- 1)
201 || alignment
< sizeof(void *)
205 ptr
= static_calloc_aligned(1, size
, alignment
);
212 void setup_static_allocator(void)
214 assert(cur_alloc
.calloc
== NULL
);
215 cur_alloc
.calloc
= static_calloc
;
216 assert(cur_alloc
.malloc
== NULL
);
217 cur_alloc
.malloc
= static_malloc
;
218 assert(cur_alloc
.free
== NULL
);
219 cur_alloc
.free
= static_free
;
220 assert(cur_alloc
.realloc
== NULL
);
221 cur_alloc
.realloc
= static_realloc
;
222 assert(cur_alloc
.memalign
== NULL
);
223 cur_alloc
.memalign
= static_memalign
;
224 assert(cur_alloc
.posix_memalign
== NULL
);
225 cur_alloc
.posix_memalign
= static_posix_memalign
;
229 void lookup_all_symbols(void)
231 struct alloc_functions af
;
234 * Temporarily redirect allocation functions to
235 * static_calloc_aligned, and free function to static_free
236 * (no-op), until the dlsym lookup has completed.
238 setup_static_allocator();
240 /* Perform the actual lookups */
241 af
.calloc
= dlsym(RTLD_NEXT
, "calloc");
242 af
.malloc
= dlsym(RTLD_NEXT
, "malloc");
243 af
.free
= dlsym(RTLD_NEXT
, "free");
244 af
.realloc
= dlsym(RTLD_NEXT
, "realloc");
245 af
.memalign
= dlsym(RTLD_NEXT
, "memalign");
246 af
.posix_memalign
= dlsym(RTLD_NEXT
, "posix_memalign");
248 /* Populate the new allocator functions */
249 memcpy(&cur_alloc
, &af
, sizeof(cur_alloc
));
252 void *malloc(size_t size
)
256 URCU_TLS(malloc_nesting
)++;
257 if (cur_alloc
.malloc
== NULL
) {
258 lookup_all_symbols();
259 if (cur_alloc
.malloc
== NULL
) {
260 fprintf(stderr
, "mallocwrap: unable to find malloc\n");
264 retval
= cur_alloc
.malloc(size
);
265 if (URCU_TLS(malloc_nesting
) == 1) {
266 tracepoint(lttng_ust_libc
, malloc
,
267 size
, retval
, LTTNG_UST_CALLER_IP());
269 URCU_TLS(malloc_nesting
)--;
275 URCU_TLS(malloc_nesting
)++;
277 * Check whether the memory was allocated with
278 * static_calloc_align, in which case there is nothing to free.
280 if (caa_unlikely((char *)ptr
>= static_calloc_buf
&&
281 (char *)ptr
< static_calloc_buf
+ STATIC_CALLOC_LEN
)) {
285 if (URCU_TLS(malloc_nesting
) == 1) {
286 tracepoint(lttng_ust_libc
, free
,
287 ptr
, LTTNG_UST_CALLER_IP());
290 if (cur_alloc
.free
== NULL
) {
291 lookup_all_symbols();
292 if (cur_alloc
.free
== NULL
) {
293 fprintf(stderr
, "mallocwrap: unable to find free\n");
299 URCU_TLS(malloc_nesting
)--;
302 void *calloc(size_t nmemb
, size_t size
)
306 URCU_TLS(malloc_nesting
)++;
307 if (cur_alloc
.calloc
== NULL
) {
308 lookup_all_symbols();
309 if (cur_alloc
.calloc
== NULL
) {
310 fprintf(stderr
, "callocwrap: unable to find calloc\n");
314 retval
= cur_alloc
.calloc(nmemb
, size
);
315 if (URCU_TLS(malloc_nesting
) == 1) {
316 tracepoint(lttng_ust_libc
, calloc
,
317 nmemb
, size
, retval
, LTTNG_UST_CALLER_IP());
319 URCU_TLS(malloc_nesting
)--;
323 void *realloc(void *ptr
, size_t size
)
327 URCU_TLS(malloc_nesting
)++;
329 * Check whether the memory was allocated with
330 * static_calloc_align, in which case there is nothing
331 * to free, and we need to copy the old data.
333 if (caa_unlikely((char *)ptr
>= static_calloc_buf
&&
334 (char *)ptr
< static_calloc_buf
+ STATIC_CALLOC_LEN
)) {
337 old_size
= (size_t *) ptr
- 1;
338 if (cur_alloc
.calloc
== NULL
) {
339 lookup_all_symbols();
340 if (cur_alloc
.calloc
== NULL
) {
341 fprintf(stderr
, "reallocwrap: unable to find calloc\n");
345 retval
= cur_alloc
.calloc(1, size
);
347 memcpy(retval
, ptr
, *old_size
);
350 * Mimick that a NULL pointer has been received, so
351 * memory allocation analysis based on the trace don't
352 * get confused by the address from the static
359 if (cur_alloc
.realloc
== NULL
) {
360 lookup_all_symbols();
361 if (cur_alloc
.realloc
== NULL
) {
362 fprintf(stderr
, "reallocwrap: unable to find realloc\n");
366 retval
= cur_alloc
.realloc(ptr
, size
);
368 if (URCU_TLS(malloc_nesting
) == 1) {
369 tracepoint(lttng_ust_libc
, realloc
,
370 ptr
, size
, retval
, LTTNG_UST_CALLER_IP());
372 URCU_TLS(malloc_nesting
)--;
376 void *memalign(size_t alignment
, size_t size
)
380 URCU_TLS(malloc_nesting
)++;
381 if (cur_alloc
.memalign
== NULL
) {
382 lookup_all_symbols();
383 if (cur_alloc
.memalign
== NULL
) {
384 fprintf(stderr
, "memalignwrap: unable to find memalign\n");
388 retval
= cur_alloc
.memalign(alignment
, size
);
389 if (URCU_TLS(malloc_nesting
) == 1) {
390 tracepoint(lttng_ust_libc
, memalign
,
391 alignment
, size
, retval
,
392 LTTNG_UST_CALLER_IP());
394 URCU_TLS(malloc_nesting
)--;
398 int posix_memalign(void **memptr
, size_t alignment
, size_t size
)
402 URCU_TLS(malloc_nesting
)++;
403 if (cur_alloc
.posix_memalign
== NULL
) {
404 lookup_all_symbols();
405 if (cur_alloc
.posix_memalign
== NULL
) {
406 fprintf(stderr
, "posix_memalignwrap: unable to find posix_memalign\n");
410 retval
= cur_alloc
.posix_memalign(memptr
, alignment
, size
);
411 if (URCU_TLS(malloc_nesting
) == 1) {
412 tracepoint(lttng_ust_libc
, posix_memalign
,
413 *memptr
, alignment
, size
,
414 retval
, LTTNG_UST_CALLER_IP());
416 URCU_TLS(malloc_nesting
)--;
420 __attribute__((constructor
))
421 void lttng_ust_malloc_wrapper_init(void)
423 /* Initialization already done */
424 if (cur_alloc
.calloc
) {
428 * Ensure the allocator is in place before the process becomes
431 lookup_all_symbols();