Line data Source code
1 : /*
2 : Samba Unix SMB/CIFS implementation.
3 :
4 : Samba trivial allocation library - new interface
5 :
6 : NOTE: Please read talloc_guide.txt for full documentation
7 :
8 : Copyright (C) Andrew Tridgell 2004
9 : Copyright (C) Stefan Metzmacher 2006
10 :
11 : ** NOTE! The following LGPL license applies to the talloc
12 : ** library. This does NOT imply that all of Samba is released
13 : ** under the LGPL
14 :
15 : This library is free software; you can redistribute it and/or
16 : modify it under the terms of the GNU Lesser General Public
17 : License as published by the Free Software Foundation; either
18 : version 3 of the License, or (at your option) any later version.
19 :
20 : This library is distributed in the hope that it will be useful,
21 : but WITHOUT ANY WARRANTY; without even the implied warranty of
22 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 : Lesser General Public License for more details.
24 :
25 : You should have received a copy of the GNU Lesser General Public
26 : License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 : */
28 :
29 : /*
30 : inspired by http://swapped.cc/halloc/
31 : */
32 :
33 : #include "replace.h"
34 : #include "talloc.h"
35 :
36 : #ifdef HAVE_SYS_AUXV_H
37 : #include <sys/auxv.h>
38 : #endif
39 :
40 : #if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
41 : #error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
42 : #endif
43 :
44 : #if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
45 : #error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
46 : #endif
47 :
48 : /* Special macros that are no-ops except when run under Valgrind on
49 : * x86. They've moved a little bit from valgrind 1.0.4 to 1.9.4 */
50 : #ifdef HAVE_VALGRIND_MEMCHECK_H
51 : /* memcheck.h includes valgrind.h */
52 : #include <valgrind/memcheck.h>
53 : #elif defined(HAVE_VALGRIND_H)
54 : #include <valgrind.h>
55 : #endif
56 :
57 : #define MAX_TALLOC_SIZE 0x10000000
58 :
59 : #define TALLOC_FLAG_FREE 0x01
60 : #define TALLOC_FLAG_LOOP 0x02
61 : #define TALLOC_FLAG_POOL 0x04 /* This is a talloc pool */
62 : #define TALLOC_FLAG_POOLMEM 0x08 /* This is allocated in a pool */
63 :
64 : /*
65 : * Bits above this are random, used to make it harder to fake talloc
66 : * headers during an attack. Try not to change this without good reason.
67 : */
68 : #define TALLOC_FLAG_MASK 0x0F
69 :
70 : #define TALLOC_MAGIC_REFERENCE ((const char *)1)
71 :
72 : #define TALLOC_MAGIC_BASE 0xe814ec70
73 : #define TALLOC_MAGIC_NON_RANDOM ( \
74 : ~TALLOC_FLAG_MASK & ( \
75 : TALLOC_MAGIC_BASE + \
76 : (TALLOC_BUILD_VERSION_MAJOR << 24) + \
77 : (TALLOC_BUILD_VERSION_MINOR << 16) + \
78 : (TALLOC_BUILD_VERSION_RELEASE << 8)))
79 : static unsigned int talloc_magic = TALLOC_MAGIC_NON_RANDOM;
80 :
81 : /* by default we abort when given a bad pointer (such as when talloc_free() is called
82 : on a pointer that came from malloc() */
83 : #ifndef TALLOC_ABORT
84 : #define TALLOC_ABORT(reason) abort()
85 : #endif
86 :
87 : #ifndef discard_const_p
88 : #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
89 : # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
90 : #else
91 : # define discard_const_p(type, ptr) ((type *)(ptr))
92 : #endif
93 : #endif
94 :
95 : /* these macros gain us a few percent of speed on gcc */
96 : #if (__GNUC__ >= 3)
97 : /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
98 : as its first argument */
99 : #ifndef likely
100 : #define likely(x) __builtin_expect(!!(x), 1)
101 : #endif
102 : #ifndef unlikely
103 : #define unlikely(x) __builtin_expect(!!(x), 0)
104 : #endif
105 : #else
106 : #ifndef likely
107 : #define likely(x) (x)
108 : #endif
109 : #ifndef unlikely
110 : #define unlikely(x) (x)
111 : #endif
112 : #endif
113 :
114 : /* this null_context is only used if talloc_enable_leak_report() or
115 : talloc_enable_leak_report_full() is called, otherwise it remains
116 : NULL
117 : */
118 : static void *null_context;
119 : static bool talloc_report_null;
120 : static bool talloc_report_null_full;
121 : static void *autofree_context;
122 :
123 : static void talloc_setup_atexit(void);
124 :
125 : /* used to enable fill of memory on free, which can be useful for
126 : * catching use after free errors when valgrind is too slow
127 : */
128 : static struct {
129 : bool initialised;
130 : bool enabled;
131 : uint8_t fill_value;
132 : } talloc_fill;
133 :
134 : #define TALLOC_FILL_ENV "TALLOC_FREE_FILL"
135 :
136 : /*
137 : * do not wipe the header, to allow the
138 : * double-free logic to still work
139 : */
140 : #define TC_INVALIDATE_FULL_FILL_CHUNK(_tc) do { \
141 : if (unlikely(talloc_fill.enabled)) { \
142 : size_t _flen = (_tc)->size; \
143 : char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
144 : memset(_fptr, talloc_fill.fill_value, _flen); \
145 : } \
146 : } while (0)
147 :
148 : #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
149 : /* Mark the whole chunk as not accessible */
150 : #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { \
151 : size_t _flen = TC_HDR_SIZE + (_tc)->size; \
152 : char *_fptr = (char *)(_tc); \
153 : VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
154 : } while(0)
155 : #else
156 : #define TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc) do { } while (0)
157 : #endif
158 :
159 : #define TC_INVALIDATE_FULL_CHUNK(_tc) do { \
160 : TC_INVALIDATE_FULL_FILL_CHUNK(_tc); \
161 : TC_INVALIDATE_FULL_VALGRIND_CHUNK(_tc); \
162 : } while (0)
163 :
164 : #define TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
165 : if (unlikely(talloc_fill.enabled)) { \
166 : size_t _flen = (_tc)->size - (_new_size); \
167 : char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
168 : _fptr += (_new_size); \
169 : memset(_fptr, talloc_fill.fill_value, _flen); \
170 : } \
171 : } while (0)
172 :
173 : #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
174 : /* Mark the unused bytes not accessible */
175 : #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
176 : size_t _flen = (_tc)->size - (_new_size); \
177 : char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
178 : _fptr += (_new_size); \
179 : VALGRIND_MAKE_MEM_NOACCESS(_fptr, _flen); \
180 : } while (0)
181 : #else
182 : #define TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
183 : #endif
184 :
185 : #define TC_INVALIDATE_SHRINK_CHUNK(_tc, _new_size) do { \
186 : TC_INVALIDATE_SHRINK_FILL_CHUNK(_tc, _new_size); \
187 : TC_INVALIDATE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
188 : } while (0)
189 :
190 : #define TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size) do { \
191 : if (unlikely(talloc_fill.enabled)) { \
192 : size_t _flen = (_tc)->size - (_new_size); \
193 : char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
194 : _fptr += (_new_size); \
195 : memset(_fptr, talloc_fill.fill_value, _flen); \
196 : } \
197 : } while (0)
198 :
199 : #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
200 : /* Mark the unused bytes as undefined */
201 : #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { \
202 : size_t _flen = (_tc)->size - (_new_size); \
203 : char *_fptr = (char *)TC_PTR_FROM_CHUNK(_tc); \
204 : _fptr += (_new_size); \
205 : VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
206 : } while (0)
207 : #else
208 : #define TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
209 : #endif
210 :
211 : #define TC_UNDEFINE_SHRINK_CHUNK(_tc, _new_size) do { \
212 : TC_UNDEFINE_SHRINK_FILL_CHUNK(_tc, _new_size); \
213 : TC_UNDEFINE_SHRINK_VALGRIND_CHUNK(_tc, _new_size); \
214 : } while (0)
215 :
216 : #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
217 : /* Mark the new bytes as undefined */
218 : #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { \
219 : size_t _old_used = TC_HDR_SIZE + (_tc)->size; \
220 : size_t _new_used = TC_HDR_SIZE + (_new_size); \
221 : size_t _flen = _new_used - _old_used; \
222 : char *_fptr = _old_used + (char *)(_tc); \
223 : VALGRIND_MAKE_MEM_UNDEFINED(_fptr, _flen); \
224 : } while (0)
225 : #else
226 : #define TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size) do { } while (0)
227 : #endif
228 :
229 : #define TC_UNDEFINE_GROW_CHUNK(_tc, _new_size) do { \
230 : TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
231 : } while (0)
232 :
233 : struct talloc_reference_handle {
234 : struct talloc_reference_handle *next, *prev;
235 : void *ptr;
236 : const char *location;
237 : };
238 :
239 : struct talloc_memlimit {
240 : struct talloc_chunk *parent;
241 : struct talloc_memlimit *upper;
242 : size_t max_size;
243 : size_t cur_size;
244 : };
245 :
246 : static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size);
247 : static inline void talloc_memlimit_grow(struct talloc_memlimit *limit,
248 : size_t size);
249 : static inline void talloc_memlimit_shrink(struct talloc_memlimit *limit,
250 : size_t size);
251 : static inline void tc_memlimit_update_on_free(struct talloc_chunk *tc);
252 :
253 : static inline void _tc_set_name_const(struct talloc_chunk *tc,
254 : const char *name);
255 : static struct talloc_chunk *_vasprintf_tc(const void *t,
256 : const char *fmt,
257 : va_list ap);
258 :
259 : typedef int (*talloc_destructor_t)(void *);
260 :
261 : struct talloc_pool_hdr;
262 :
263 : struct talloc_chunk {
264 : /*
265 : * flags includes the talloc magic, which is randomised to
266 : * make overwrite attacks harder
267 : */
268 : unsigned flags;
269 :
270 : /*
271 : * If you have a logical tree like:
272 : *
273 : * <parent>
274 : * / | \
275 : * / | \
276 : * / | \
277 : * <child 1> <child 2> <child 3>
278 : *
279 : * The actual talloc tree is:
280 : *
281 : * <parent>
282 : * |
283 : * <child 1> - <child 2> - <child 3>
284 : *
285 : * The children are linked with next/prev pointers, and
286 : * child 1 is linked to the parent with parent/child
287 : * pointers.
288 : */
289 :
290 : struct talloc_chunk *next, *prev;
291 : struct talloc_chunk *parent, *child;
292 : struct talloc_reference_handle *refs;
293 : talloc_destructor_t destructor;
294 : const char *name;
295 : size_t size;
296 :
297 : /*
298 : * limit semantics:
299 : * if 'limit' is set it means all *new* children of the context will
300 : * be limited to a total aggregate size ox max_size for memory
301 : * allocations.
302 : * cur_size is used to keep track of the current use
303 : */
304 : struct talloc_memlimit *limit;
305 :
306 : /*
307 : * For members of a pool (i.e. TALLOC_FLAG_POOLMEM is set), "pool"
308 : * is a pointer to the struct talloc_chunk of the pool that it was
309 : * allocated from. This way children can quickly find the pool to chew
310 : * from.
311 : */
312 : struct talloc_pool_hdr *pool;
313 : };
314 :
315 : union talloc_chunk_cast_u {
316 : uint8_t *ptr;
317 : struct talloc_chunk *chunk;
318 : };
319 :
320 : /* 16 byte alignment seems to keep everyone happy */
321 : #define TC_ALIGN16(s) (((s)+15)&~15)
322 : #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
323 : #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
324 :
325 0 : _PUBLIC_ int talloc_version_major(void)
326 : {
327 0 : return TALLOC_VERSION_MAJOR;
328 : }
329 :
330 0 : _PUBLIC_ int talloc_version_minor(void)
331 : {
332 0 : return TALLOC_VERSION_MINOR;
333 : }
334 :
335 2 : _PUBLIC_ int talloc_test_get_magic(void)
336 : {
337 2 : return talloc_magic;
338 : }
339 :
340 43084093335 : static inline void _talloc_chunk_set_free(struct talloc_chunk *tc,
341 : const char *location)
342 : {
343 : /*
344 : * Mark this memory as free, and also over-stamp the talloc
345 : * magic with the old-style magic.
346 : *
347 : * Why? This tries to avoid a memory read use-after-free from
348 : * disclosing our talloc magic, which would then allow an
349 : * attacker to prepare a valid header and so run a destructor.
350 : *
351 : */
352 43084093335 : tc->flags = TALLOC_MAGIC_NON_RANDOM | TALLOC_FLAG_FREE
353 43084093335 : | (tc->flags & TALLOC_FLAG_MASK);
354 :
355 : /* we mark the freed memory with where we called the free
356 : * from. This means on a double free error we can report where
357 : * the first free came from
358 : */
359 43084093335 : if (location) {
360 40395615717 : tc->name = location;
361 : }
362 41088110993 : }
363 :
364 2688477618 : static inline void _talloc_chunk_set_not_free(struct talloc_chunk *tc)
365 : {
366 : /*
367 : * Mark this memory as not free.
368 : *
369 : * Why? This is memory either in a pool (and so available for
370 : * talloc's re-use or after the realloc(). We need to mark
371 : * the memory as free() before any realloc() call as we can't
372 : * write to the memory after that.
373 : *
374 : * We put back the normal magic instead of the 'not random'
375 : * magic.
376 : */
377 :
378 2688477618 : tc->flags = talloc_magic |
379 2688332826 : ((tc->flags & TALLOC_FLAG_MASK) & ~TALLOC_FLAG_FREE);
380 2621665994 : }
381 :
382 : static void (*talloc_log_fn)(const char *message);
383 :
384 171973 : _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
385 : {
386 171973 : talloc_log_fn = log_fn;
387 171973 : }
388 :
389 : #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
390 : #define CONSTRUCTOR __attribute__((constructor))
391 : #elif defined(HAVE_PRAGMA_INIT)
392 : #define CONSTRUCTOR
393 : #pragma init (talloc_lib_init)
394 : #endif
395 : #if defined(HAVE_CONSTRUCTOR_ATTRIBUTE) || defined(HAVE_PRAGMA_INIT)
396 : void talloc_lib_init(void) CONSTRUCTOR;
397 94588 : void talloc_lib_init(void)
398 : {
399 2143 : uint32_t random_value;
400 : #if defined(HAVE_GETAUXVAL) && defined(AT_RANDOM)
401 2143 : uint8_t *p;
402 : /*
403 : * Use the kernel-provided random values used for
404 : * ASLR. This won't change per-exec, which is ideal for us
405 : */
406 94588 : p = (uint8_t *) getauxval(AT_RANDOM);
407 94588 : if (p) {
408 : /*
409 : * We get 16 bytes from getauxval. By calling rand(),
410 : * a totally insecure PRNG, but one that will
411 : * deterministically have a different value when called
412 : * twice, we ensure that if two talloc-like libraries
413 : * are somehow loaded in the same address space, that
414 : * because we choose different bytes, we will keep the
415 : * protection against collision of multiple talloc
416 : * libs.
417 : *
418 : * This protection is important because the effects of
419 : * passing a talloc pointer from one to the other may
420 : * be very hard to determine.
421 : */
422 94588 : int offset = rand() % (16 - sizeof(random_value));
423 94588 : memcpy(&random_value, p + offset, sizeof(random_value));
424 : } else
425 : #endif
426 : {
427 : /*
428 : * Otherwise, hope the location we are loaded in
429 : * memory is randomised by someone else
430 : */
431 0 : random_value = ((uintptr_t)talloc_lib_init & 0xFFFFFFFF);
432 : }
433 94588 : talloc_magic = random_value & ~TALLOC_FLAG_MASK;
434 94588 : }
435 : #else
436 : #warning "No __attribute__((constructor)) support found on this platform, additional talloc security measures not available"
437 : #endif
438 :
439 22 : static void talloc_lib_atexit(void)
440 : {
441 22 : TALLOC_FREE(autofree_context);
442 :
443 22 : if (talloc_total_size(null_context) == 0) {
444 1 : return;
445 : }
446 :
447 20 : if (talloc_report_null_full) {
448 0 : talloc_report_full(null_context, stderr);
449 20 : } else if (talloc_report_null) {
450 20 : talloc_report(null_context, stderr);
451 : }
452 : }
453 :
454 26 : static void talloc_setup_atexit(void)
455 : {
456 2 : static bool done;
457 :
458 26 : if (done) {
459 3 : return;
460 : }
461 :
462 22 : atexit(talloc_lib_atexit);
463 22 : done = true;
464 : }
465 :
466 : static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
467 6 : static void talloc_log(const char *fmt, ...)
468 : {
469 0 : va_list ap;
470 0 : char *message;
471 :
472 6 : if (!talloc_log_fn) {
473 0 : return;
474 : }
475 :
476 6 : va_start(ap, fmt);
477 6 : message = talloc_vasprintf(NULL, fmt, ap);
478 6 : va_end(ap);
479 :
480 6 : talloc_log_fn(message);
481 6 : talloc_free(message);
482 : }
483 :
484 0 : static void talloc_log_stderr(const char *message)
485 : {
486 0 : fprintf(stderr, "%s", message);
487 0 : }
488 :
489 0 : _PUBLIC_ void talloc_set_log_stderr(void)
490 : {
491 0 : talloc_set_log_fn(talloc_log_stderr);
492 0 : }
493 :
494 : static void (*talloc_abort_fn)(const char *reason);
495 :
496 75263 : _PUBLIC_ void talloc_set_abort_fn(void (*abort_fn)(const char *reason))
497 : {
498 75263 : talloc_abort_fn = abort_fn;
499 75263 : }
500 :
501 0 : static void talloc_abort(const char *reason)
502 : {
503 0 : talloc_log("%s\n", reason);
504 :
505 0 : if (!talloc_abort_fn) {
506 0 : TALLOC_ABORT(reason);
507 : }
508 :
509 0 : talloc_abort_fn(reason);
510 0 : }
511 :
512 0 : static void talloc_abort_access_after_free(void)
513 : {
514 0 : talloc_abort("Bad talloc magic value - access after free");
515 0 : }
516 :
517 0 : static void talloc_abort_unknown_value(void)
518 : {
519 0 : talloc_abort("Bad talloc magic value - unknown value");
520 0 : }
521 :
522 : /* panic if we get a bad magic value */
523 96118587373 : static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
524 : {
525 96118587373 : const char *pp = (const char *)ptr;
526 96118587373 : struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
527 96118587373 : if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~TALLOC_FLAG_MASK)) != talloc_magic)) {
528 0 : if ((tc->flags & (TALLOC_FLAG_FREE | ~TALLOC_FLAG_MASK))
529 : == (TALLOC_MAGIC_NON_RANDOM | TALLOC_FLAG_FREE)) {
530 0 : talloc_log("talloc: access after free error - first free may be at %s\n", tc->name);
531 0 : talloc_abort_access_after_free();
532 0 : return NULL;
533 : }
534 :
535 0 : talloc_abort_unknown_value();
536 0 : return NULL;
537 : }
538 92721395917 : return tc;
539 : }
540 :
541 : /* hook into the front of the list */
542 : #define _TLIST_ADD(list, p) \
543 : do { \
544 : if (!(list)) { \
545 : (list) = (p); \
546 : (p)->next = (p)->prev = NULL; \
547 : } else { \
548 : (list)->prev = (p); \
549 : (p)->next = (list); \
550 : (p)->prev = NULL; \
551 : (list) = (p); \
552 : }\
553 : } while (0)
554 :
555 : /* remove an element from a list - element doesn't have to be in list. */
556 : #define _TLIST_REMOVE(list, p) \
557 : do { \
558 : if ((p) == (list)) { \
559 : (list) = (p)->next; \
560 : if (list) (list)->prev = NULL; \
561 : } else { \
562 : if ((p)->prev) (p)->prev->next = (p)->next; \
563 : if ((p)->next) (p)->next->prev = (p)->prev; \
564 : } \
565 : if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
566 : } while (0)
567 :
568 :
569 : /*
570 : return the parent chunk of a pointer
571 : */
572 1707387536 : static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
573 : {
574 49555311 : struct talloc_chunk *tc;
575 :
576 1665733878 : if (unlikely(ptr == NULL)) {
577 572765 : return NULL;
578 : }
579 :
580 1706795213 : tc = talloc_chunk_from_ptr(ptr);
581 5478152177 : while (tc->prev) tc=tc->prev;
582 :
583 1665161113 : return tc->parent;
584 : }
585 :
586 845037491 : _PUBLIC_ void *talloc_parent(const void *ptr)
587 : {
588 845037491 : struct talloc_chunk *tc = talloc_parent_chunk(ptr);
589 845017933 : return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
590 : }
591 :
592 : /*
593 : find parents name
594 : */
595 0 : _PUBLIC_ const char *talloc_parent_name(const void *ptr)
596 : {
597 0 : struct talloc_chunk *tc = talloc_parent_chunk(ptr);
598 0 : return tc? tc->name : NULL;
599 : }
600 :
601 : /*
602 : A pool carries an in-pool object count count in the first 16 bytes.
603 : bytes. This is done to support talloc_steal() to a parent outside of the
604 : pool. The count includes the pool itself, so a talloc_free() on a pool will
605 : only destroy the pool if the count has dropped to zero. A talloc_free() of a
606 : pool member will reduce the count, and eventually also call free(3) on the
607 : pool memory.
608 :
609 : The object count is not put into "struct talloc_chunk" because it is only
610 : relevant for talloc pools and the alignment to 16 bytes would increase the
611 : memory footprint of each talloc chunk by those 16 bytes.
612 : */
613 :
614 : struct talloc_pool_hdr {
615 : void *end;
616 : unsigned int object_count;
617 : size_t poolsize;
618 : };
619 :
620 : union talloc_pool_hdr_cast_u {
621 : uint8_t *ptr;
622 : struct talloc_pool_hdr *hdr;
623 : };
624 :
625 : #define TP_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_pool_hdr))
626 :
627 1546686885 : static inline struct talloc_pool_hdr *talloc_pool_from_chunk(struct talloc_chunk *c)
628 : {
629 1547826840 : union talloc_chunk_cast_u tcc = { .chunk = c };
630 1547826840 : union talloc_pool_hdr_cast_u tphc = { tcc.ptr - TP_HDR_SIZE };
631 1546686885 : return tphc.hdr;
632 : }
633 :
634 2949361137 : static inline struct talloc_chunk *talloc_chunk_from_pool(struct talloc_pool_hdr *h)
635 : {
636 2949530674 : union talloc_pool_hdr_cast_u tphc = { .hdr = h };
637 3385868149 : union talloc_chunk_cast_u tcc = { .ptr = tphc.ptr + TP_HDR_SIZE };
638 2949361137 : return tcc.chunk;
639 : }
640 :
641 1965467229 : static inline void *tc_pool_end(struct talloc_pool_hdr *pool_hdr)
642 : {
643 1965467229 : struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
644 1965467229 : return (char *)tc + TC_HDR_SIZE + pool_hdr->poolsize;
645 : }
646 :
647 1964878602 : static inline size_t tc_pool_space_left(struct talloc_pool_hdr *pool_hdr)
648 : {
649 1964878602 : return (char *)tc_pool_end(pool_hdr) - (char *)pool_hdr->end;
650 : }
651 :
652 : /* If tc is inside a pool, this gives the next neighbour. */
653 1432522458 : static inline void *tc_next_chunk(struct talloc_chunk *tc)
654 : {
655 1432522458 : return (char *)tc + TC_ALIGN16(TC_HDR_SIZE + tc->size);
656 : }
657 :
658 337023428 : static inline void *tc_pool_first_chunk(struct talloc_pool_hdr *pool_hdr)
659 : {
660 337023428 : struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
661 337023428 : return tc_next_chunk(tc);
662 : }
663 :
664 : /* Mark the whole remaining pool as not accessible */
665 612051323 : static inline void tc_invalidate_pool(struct talloc_pool_hdr *pool_hdr)
666 : {
667 612051323 : size_t flen = tc_pool_space_left(pool_hdr);
668 :
669 612051323 : if (unlikely(talloc_fill.enabled)) {
670 18 : memset(pool_hdr->end, talloc_fill.fill_value, flen);
671 : }
672 :
673 : #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_NOACCESS)
674 : VALGRIND_MAKE_MEM_NOACCESS(pool_hdr->end, flen);
675 : #endif
676 612051323 : }
677 :
678 : /*
679 : Allocate from a pool
680 : */
681 :
682 39732983530 : static inline struct talloc_chunk *tc_alloc_pool(struct talloc_chunk *parent,
683 : size_t size, size_t prefix_len)
684 : {
685 39732983530 : struct talloc_pool_hdr *pool_hdr = NULL;
686 1658877920 : union talloc_chunk_cast_u tcc;
687 1658877920 : size_t space_left;
688 1658877920 : struct talloc_chunk *result;
689 1658877920 : size_t chunk_size;
690 :
691 39732983530 : if (parent == NULL) {
692 0 : return NULL;
693 : }
694 :
695 39732983530 : if (parent->flags & TALLOC_FLAG_POOL) {
696 734353736 : pool_hdr = talloc_pool_from_chunk(parent);
697 : }
698 38998629794 : else if (parent->flags & TALLOC_FLAG_POOLMEM) {
699 607172652 : pool_hdr = parent->pool;
700 : }
701 :
702 38336289186 : if (pool_hdr == NULL) {
703 36994762798 : return NULL;
704 : }
705 :
706 1341526388 : space_left = tc_pool_space_left(pool_hdr);
707 :
708 : /*
709 : * Align size to 16 bytes
710 : */
711 1341526388 : chunk_size = TC_ALIGN16(size + prefix_len);
712 :
713 1341526388 : if (space_left < chunk_size) {
714 253193106 : return NULL;
715 : }
716 :
717 1086875156 : tcc = (union talloc_chunk_cast_u) {
718 1086875156 : .ptr = ((uint8_t *)pool_hdr->end) + prefix_len
719 : };
720 1086875156 : result = tcc.chunk;
721 :
722 : #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
723 : VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, chunk_size);
724 : #endif
725 :
726 1086875156 : pool_hdr->end = (void *)((char *)pool_hdr->end + chunk_size);
727 :
728 1086875156 : result->flags = talloc_magic | TALLOC_FLAG_POOLMEM;
729 1086875156 : result->pool = pool_hdr;
730 :
731 1086875156 : pool_hdr->object_count++;
732 :
733 1086875156 : return result;
734 : }
735 :
736 : /*
737 : Allocate a bit of memory as a child of an existing pointer
738 : */
739 40025574029 : static inline void *__talloc_with_prefix(const void *context,
740 : size_t size,
741 : size_t prefix_len,
742 : struct talloc_chunk **tc_ret)
743 : {
744 40025574029 : struct talloc_chunk *tc = NULL;
745 40025574029 : struct talloc_memlimit *limit = NULL;
746 40025574029 : size_t total_len = TC_HDR_SIZE + size + prefix_len;
747 40025574029 : struct talloc_chunk *parent = NULL;
748 :
749 40025574029 : if (unlikely(context == NULL)) {
750 434288296 : context = null_context;
751 : }
752 :
753 40025574029 : if (unlikely(size >= MAX_TALLOC_SIZE)) {
754 1 : return NULL;
755 : }
756 :
757 40025574027 : if (unlikely(total_len < TC_HDR_SIZE)) {
758 0 : return NULL;
759 : }
760 :
761 40025574027 : if (likely(context != NULL)) {
762 39730165608 : parent = talloc_chunk_from_ptr(context);
763 :
764 39730165604 : if (parent->limit != NULL) {
765 50 : limit = parent->limit;
766 : }
767 :
768 39730165604 : tc = tc_alloc_pool(parent, TC_HDR_SIZE+size, prefix_len);
769 : }
770 :
771 40025574025 : if (tc == NULL) {
772 38939051946 : uint8_t *ptr = NULL;
773 : union talloc_chunk_cast_u tcc;
774 :
775 : /*
776 : * Only do the memlimit check/update on actual allocation.
777 : */
778 38939051934 : if (!talloc_memlimit_check(limit, total_len)) {
779 12 : errno = ENOMEM;
780 12 : return NULL;
781 : }
782 :
783 38939051922 : ptr = malloc(total_len);
784 38939051922 : if (unlikely(ptr == NULL)) {
785 0 : return NULL;
786 : }
787 38939051922 : tcc = (union talloc_chunk_cast_u) { .ptr = ptr + prefix_len };
788 38939051922 : tc = tcc.chunk;
789 38939051922 : tc->flags = talloc_magic;
790 38939051922 : tc->pool = NULL;
791 :
792 40631009945 : talloc_memlimit_grow(limit, total_len);
793 : }
794 :
795 40025574013 : tc->limit = limit;
796 40025574013 : tc->size = size;
797 40025574013 : tc->destructor = NULL;
798 40025574013 : tc->child = NULL;
799 40025574013 : tc->name = NULL;
800 40025574013 : tc->refs = NULL;
801 :
802 40025574013 : if (likely(context != NULL)) {
803 39730165594 : if (parent->child) {
804 28569256708 : parent->child->parent = NULL;
805 28569256708 : tc->next = parent->child;
806 28569256708 : tc->next->prev = tc;
807 : } else {
808 11160908886 : tc->next = NULL;
809 : }
810 39730165594 : tc->parent = parent;
811 39730165594 : tc->prev = NULL;
812 39730165594 : parent->child = tc;
813 : } else {
814 295408419 : tc->next = tc->prev = tc->parent = NULL;
815 : }
816 :
817 40025574013 : *tc_ret = tc;
818 40025574013 : return TC_PTR_FROM_CHUNK(tc);
819 : }
820 :
821 39750545649 : static inline void *__talloc(const void *context,
822 : size_t size,
823 : struct talloc_chunk **tc)
824 : {
825 39750545649 : return __talloc_with_prefix(context, size, 0, tc);
826 : }
827 :
828 : /*
829 : * Create a talloc pool
830 : */
831 :
832 275028380 : static inline void *_talloc_pool(const void *context, size_t size)
833 : {
834 275028380 : struct talloc_chunk *tc = NULL;
835 1196165 : struct talloc_pool_hdr *pool_hdr;
836 1196165 : void *result;
837 :
838 275028380 : result = __talloc_with_prefix(context, size, TP_HDR_SIZE, &tc);
839 :
840 275028380 : if (unlikely(result == NULL)) {
841 0 : return NULL;
842 : }
843 :
844 275028380 : pool_hdr = talloc_pool_from_chunk(tc);
845 :
846 275028380 : tc->flags |= TALLOC_FLAG_POOL;
847 275028380 : tc->size = 0;
848 :
849 275028380 : pool_hdr->object_count = 1;
850 275028380 : pool_hdr->end = result;
851 275028380 : pool_hdr->poolsize = size;
852 :
853 275028380 : tc_invalidate_pool(pool_hdr);
854 :
855 275028380 : return result;
856 : }
857 :
858 4873446 : _PUBLIC_ void *talloc_pool(const void *context, size_t size)
859 : {
860 4873446 : return _talloc_pool(context, size);
861 : }
862 :
863 : /*
864 : * Create a talloc pool correctly sized for a basic size plus
865 : * a number of subobjects whose total size is given. Essentially
866 : * a custom allocator for talloc to reduce fragmentation.
867 : */
868 :
869 270154934 : _PUBLIC_ void *_talloc_pooled_object(const void *ctx,
870 : size_t type_size,
871 : const char *type_name,
872 : unsigned num_subobjects,
873 : size_t total_subobjects_size)
874 : {
875 1139954 : size_t poolsize, subobjects_slack, tmp;
876 1139954 : struct talloc_chunk *tc;
877 1139954 : struct talloc_pool_hdr *pool_hdr;
878 1139954 : void *ret;
879 :
880 270154934 : poolsize = type_size + total_subobjects_size;
881 :
882 270154934 : if ((poolsize < type_size) || (poolsize < total_subobjects_size)) {
883 0 : goto overflow;
884 : }
885 :
886 270154934 : if (num_subobjects == UINT_MAX) {
887 0 : goto overflow;
888 : }
889 270154934 : num_subobjects += 1; /* the object body itself */
890 :
891 : /*
892 : * Alignment can increase the pool size by at most 15 bytes per object
893 : * plus alignment for the object itself
894 : */
895 270154934 : subobjects_slack = (TC_HDR_SIZE + TP_HDR_SIZE + 15) * num_subobjects;
896 270154934 : if (subobjects_slack < num_subobjects) {
897 0 : goto overflow;
898 : }
899 :
900 270154934 : tmp = poolsize + subobjects_slack;
901 270154934 : if ((tmp < poolsize) || (tmp < subobjects_slack)) {
902 0 : goto overflow;
903 : }
904 270154934 : poolsize = tmp;
905 :
906 270154934 : ret = _talloc_pool(ctx, poolsize);
907 270154934 : if (ret == NULL) {
908 0 : return NULL;
909 : }
910 :
911 270154934 : tc = talloc_chunk_from_ptr(ret);
912 270154934 : tc->size = type_size;
913 :
914 270154934 : pool_hdr = talloc_pool_from_chunk(tc);
915 :
916 : #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
917 : VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, type_size);
918 : #endif
919 :
920 270154934 : pool_hdr->end = ((char *)pool_hdr->end + TC_ALIGN16(type_size));
921 :
922 270154934 : _tc_set_name_const(tc, type_name);
923 270154934 : return ret;
924 :
925 1139954 : overflow:
926 0 : return NULL;
927 : }
928 :
929 : /*
930 : setup a destructor to be called on free of a pointer
931 : the destructor should return 0 on success, or -1 on failure.
932 : if the destructor fails then the free is failed, and the memory can
933 : be continued to be used
934 : */
935 2511004756 : _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
936 : {
937 2511004756 : struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
938 2511004741 : tc->destructor = destructor;
939 2511004741 : }
940 :
941 : /*
942 : increase the reference count on a piece of memory.
943 : */
944 10 : _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
945 : {
946 10 : if (unlikely(!talloc_reference(null_context, ptr))) {
947 0 : return -1;
948 : }
949 5 : return 0;
950 : }
951 :
952 : /*
953 : helper for talloc_reference()
954 :
955 : this is referenced by a function pointer and should not be inline
956 : */
957 311207762 : static int talloc_reference_destructor(struct talloc_reference_handle *handle)
958 : {
959 311207762 : struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
960 311207762 : _TLIST_REMOVE(ptr_tc->refs, handle);
961 311207762 : return 0;
962 : }
963 :
964 : /*
965 : more efficient way to add a name to a pointer - the name must point to a
966 : true string constant
967 : */
968 47116076925 : static inline void _tc_set_name_const(struct talloc_chunk *tc,
969 : const char *name)
970 : {
971 45277847627 : tc->name = name;
972 45281222958 : }
973 :
974 : /*
975 : internal talloc_named_const()
976 : */
977 28360916700 : static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
978 : {
979 1268571650 : void *ptr;
980 28360916700 : struct talloc_chunk *tc = NULL;
981 :
982 29629488348 : ptr = __talloc(context, size, &tc);
983 28360916698 : if (unlikely(ptr == NULL)) {
984 6 : return NULL;
985 : }
986 :
987 28360916686 : _tc_set_name_const(tc, name);
988 :
989 28131795909 : return ptr;
990 : }
991 :
992 : /*
993 : make a secondary reference to a pointer, hanging off the given context.
994 : the pointer remains valid until both the original caller and this given
995 : context are freed.
996 :
997 : the major use for this is when two different structures need to reference the
998 : same underlying data, and you want to be able to free the two instances separately,
999 : and in either order
1000 : */
1001 312295729 : _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const char *location)
1002 : {
1003 23066993 : struct talloc_chunk *tc;
1004 23066993 : struct talloc_reference_handle *handle;
1005 312295729 : if (unlikely(ptr == NULL)) return NULL;
1006 :
1007 311427328 : tc = talloc_chunk_from_ptr(ptr);
1008 311427328 : handle = (struct talloc_reference_handle *)_talloc_named_const(context,
1009 : sizeof(struct talloc_reference_handle),
1010 : TALLOC_MAGIC_REFERENCE);
1011 311427328 : if (unlikely(handle == NULL)) return NULL;
1012 :
1013 : /* note that we hang the destructor off the handle, not the
1014 : main context as that allows the caller to still setup their
1015 : own destructor on the context if they want to */
1016 311427328 : talloc_set_destructor(handle, talloc_reference_destructor);
1017 311427328 : handle->ptr = discard_const_p(void, ptr);
1018 311427328 : handle->location = location;
1019 311427328 : _TLIST_ADD(tc->refs, handle);
1020 288384284 : return handle->ptr;
1021 : }
1022 :
1023 : static void *_talloc_steal_internal(const void *new_ctx, const void *ptr);
1024 :
1025 1069844855 : static inline void _tc_free_poolmem(struct talloc_chunk *tc,
1026 : const char *location)
1027 : {
1028 260365512 : struct talloc_pool_hdr *pool;
1029 260365512 : struct talloc_chunk *pool_tc;
1030 260365512 : void *next_tc;
1031 :
1032 1069844876 : pool = tc->pool;
1033 1069844876 : pool_tc = talloc_chunk_from_pool(pool);
1034 1069844876 : next_tc = tc_next_chunk(tc);
1035 :
1036 1069844876 : _talloc_chunk_set_free(tc, location);
1037 :
1038 1069844876 : TC_INVALIDATE_FULL_CHUNK(tc);
1039 :
1040 1069844876 : if (unlikely(pool->object_count == 0)) {
1041 0 : talloc_abort("Pool object count zero!");
1042 0 : return;
1043 : }
1044 :
1045 1069844876 : pool->object_count--;
1046 :
1047 1069844876 : if (unlikely(pool->object_count == 1
1048 : && !(pool_tc->flags & TALLOC_FLAG_FREE))) {
1049 : /*
1050 : * if there is just one object left in the pool
1051 : * and pool->flags does not have TALLOC_FLAG_FREE,
1052 : * it means this is the pool itself and
1053 : * the rest is available for new objects
1054 : * again.
1055 : */
1056 336434801 : pool->end = tc_pool_first_chunk(pool);
1057 336434801 : tc_invalidate_pool(pool);
1058 336434801 : return;
1059 : }
1060 :
1061 733410075 : if (unlikely(pool->object_count == 0)) {
1062 : /*
1063 : * we mark the freed memory with where we called the free
1064 : * from. This means on a double free error we can report where
1065 : * the first free came from
1066 : */
1067 38891093 : pool_tc->name = location;
1068 :
1069 38891093 : if (pool_tc->flags & TALLOC_FLAG_POOLMEM) {
1070 29369 : _tc_free_poolmem(pool_tc, location);
1071 : } else {
1072 : /*
1073 : * The tc_memlimit_update_on_free()
1074 : * call takes into account the
1075 : * prefix TP_HDR_SIZE allocated before
1076 : * the pool talloc_chunk.
1077 : */
1078 38861703 : tc_memlimit_update_on_free(pool_tc);
1079 38861703 : TC_INVALIDATE_FULL_CHUNK(pool_tc);
1080 38861703 : free(pool);
1081 : }
1082 38891072 : return;
1083 : }
1084 :
1085 694518982 : if (pool->end == next_tc) {
1086 : /*
1087 : * if pool->pool still points to end of
1088 : * 'tc' (which is stored in the 'next_tc' variable),
1089 : * we can reclaim the memory of 'tc'.
1090 : */
1091 409395597 : pool->end = tc;
1092 409395597 : return;
1093 : }
1094 :
1095 : /*
1096 : * Do nothing. The memory is just "wasted", waiting for the pool
1097 : * itself to be freed.
1098 : */
1099 : }
1100 :
1101 : static inline void _tc_free_children_internal(struct talloc_chunk *tc,
1102 : void *ptr,
1103 : const char *location);
1104 :
1105 : static inline int _talloc_free_internal(void *ptr, const char *location);
1106 :
1107 : /*
1108 : internal free call that takes a struct talloc_chunk *.
1109 : */
1110 39737898078 : static inline int _tc_free_internal(struct talloc_chunk *tc,
1111 : const char *location)
1112 : {
1113 1678288067 : void *ptr_to_free;
1114 39737898078 : void *ptr = TC_PTR_FROM_CHUNK(tc);
1115 :
1116 39737898078 : if (unlikely(tc->refs)) {
1117 2805157 : int is_child;
1118 : /* check if this is a reference from a child or
1119 : * grandchild back to it's parent or grandparent
1120 : *
1121 : * in that case we need to remove the reference and
1122 : * call another instance of talloc_free() on the current
1123 : * pointer.
1124 : */
1125 25238546 : is_child = talloc_is_parent(tc->refs, ptr);
1126 25238546 : _talloc_free_internal(tc->refs, location);
1127 25238546 : if (is_child) {
1128 56245 : return _talloc_free_internal(ptr, location);
1129 : }
1130 22379420 : return -1;
1131 : }
1132 :
1133 39712659532 : if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
1134 : /* we have a free loop - stop looping */
1135 363 : return 0;
1136 : }
1137 :
1138 39712659168 : if (unlikely(tc->destructor)) {
1139 2095986649 : talloc_destructor_t d = tc->destructor;
1140 :
1141 : /*
1142 : * Protect the destructor against some overwrite
1143 : * attacks, by explicitly checking it has the right
1144 : * magic here.
1145 : */
1146 2095986649 : if (talloc_chunk_from_ptr(ptr) != tc) {
1147 : /*
1148 : * This can't actually happen, the
1149 : * call itself will panic.
1150 : */
1151 0 : TALLOC_ABORT("talloc_chunk_from_ptr failed!");
1152 : }
1153 :
1154 2095986649 : if (d == (talloc_destructor_t)-1) {
1155 619176 : return -1;
1156 : }
1157 2095367370 : tc->destructor = (talloc_destructor_t)-1;
1158 2095367370 : if (d(ptr) == -1) {
1159 : /*
1160 : * Only replace the destructor pointer if
1161 : * calling the destructor didn't modify it.
1162 : */
1163 386269048 : if (tc->destructor == (talloc_destructor_t)-1) {
1164 386267366 : tc->destructor = d;
1165 : }
1166 386269048 : return -1;
1167 : }
1168 1709098322 : tc->destructor = NULL;
1169 : }
1170 :
1171 39325770841 : if (tc->parent) {
1172 35921285547 : _TLIST_REMOVE(tc->parent->child, tc);
1173 35921285547 : if (tc->parent->child) {
1174 24614804705 : tc->parent->child->parent = tc->parent;
1175 : }
1176 : } else {
1177 3404485294 : if (tc->prev) tc->prev->next = tc->next;
1178 3404485294 : if (tc->next) tc->next->prev = tc->prev;
1179 3404485294 : tc->prev = tc->next = NULL;
1180 : }
1181 :
1182 39325770841 : tc->flags |= TALLOC_FLAG_LOOP;
1183 :
1184 39325770841 : _tc_free_children_internal(tc, ptr, location);
1185 :
1186 39325770841 : _talloc_chunk_set_free(tc, location);
1187 :
1188 39325770841 : if (tc->flags & TALLOC_FLAG_POOL) {
1189 1056817 : struct talloc_pool_hdr *pool;
1190 :
1191 268289788 : pool = talloc_pool_from_chunk(tc);
1192 :
1193 268289788 : if (unlikely(pool->object_count == 0)) {
1194 0 : talloc_abort("Pool object count zero!");
1195 0 : return 0;
1196 : }
1197 :
1198 268289788 : pool->object_count--;
1199 :
1200 268289788 : if (likely(pool->object_count != 0)) {
1201 38665516 : return 0;
1202 : }
1203 :
1204 : /*
1205 : * With object_count==0, a pool becomes a normal piece of
1206 : * memory to free. If it's allocated inside a pool, it needs
1207 : * to be freed as poolmem, else it needs to be just freed.
1208 : */
1209 228567455 : ptr_to_free = pool;
1210 : } else {
1211 37389732664 : ptr_to_free = tc;
1212 : }
1213 :
1214 39286859191 : if (tc->flags & TALLOC_FLAG_POOLMEM) {
1215 1066997566 : _tc_free_poolmem(tc, location);
1216 1066997566 : return 0;
1217 : }
1218 :
1219 38219861625 : tc_memlimit_update_on_free(tc);
1220 :
1221 38219861625 : TC_INVALIDATE_FULL_CHUNK(tc);
1222 38219861625 : free(ptr_to_free);
1223 38219861625 : return 0;
1224 : }
1225 :
1226 : /*
1227 : internal talloc_free call
1228 : */
1229 8394515139 : static inline int _talloc_free_internal(void *ptr, const char *location)
1230 : {
1231 342033838 : struct talloc_chunk *tc;
1232 :
1233 8394515139 : if (unlikely(ptr == NULL)) {
1234 0 : return -1;
1235 : }
1236 :
1237 : /* possibly initialised the talloc fill value */
1238 8394515139 : if (unlikely(!talloc_fill.initialised)) {
1239 85966 : const char *fill = getenv(TALLOC_FILL_ENV);
1240 85966 : if (fill != NULL) {
1241 2 : talloc_fill.enabled = true;
1242 2 : talloc_fill.fill_value = strtoul(fill, NULL, 0);
1243 : }
1244 85966 : talloc_fill.initialised = true;
1245 : }
1246 :
1247 8394515139 : tc = talloc_chunk_from_ptr(ptr);
1248 8394515139 : return _tc_free_internal(tc, location);
1249 : }
1250 :
1251 : static inline size_t _talloc_total_limit_size(const void *ptr,
1252 : struct talloc_memlimit *old_limit,
1253 : struct talloc_memlimit *new_limit);
1254 :
1255 : /*
1256 : move a lump of memory from one talloc context to another returning the
1257 : ptr on success, or NULL if it could not be transferred.
1258 : passing NULL as ptr will always return NULL with no side effects.
1259 : */
1260 2620128991 : static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
1261 : {
1262 75577253 : struct talloc_chunk *tc, *new_tc;
1263 2620128991 : size_t ctx_size = 0;
1264 :
1265 2620128991 : if (unlikely(!ptr)) {
1266 0 : return NULL;
1267 : }
1268 :
1269 2620128991 : if (unlikely(new_ctx == NULL)) {
1270 38312148 : new_ctx = null_context;
1271 : }
1272 :
1273 2620128991 : tc = talloc_chunk_from_ptr(ptr);
1274 :
1275 2620128991 : if (tc->limit != NULL) {
1276 :
1277 6 : ctx_size = _talloc_total_limit_size(ptr, NULL, NULL);
1278 :
1279 : /* Decrement the memory limit from the source .. */
1280 6 : talloc_memlimit_shrink(tc->limit->upper, ctx_size);
1281 :
1282 4 : if (tc->limit->parent == tc) {
1283 0 : tc->limit->upper = NULL;
1284 : } else {
1285 4 : tc->limit = NULL;
1286 : }
1287 : }
1288 :
1289 2620128991 : if (unlikely(new_ctx == NULL)) {
1290 37802851 : if (tc->parent) {
1291 37797511 : _TLIST_REMOVE(tc->parent->child, tc);
1292 37797511 : if (tc->parent->child) {
1293 212858 : tc->parent->child->parent = tc->parent;
1294 : }
1295 : } else {
1296 5340 : if (tc->prev) tc->prev->next = tc->next;
1297 5340 : if (tc->next) tc->next->prev = tc->prev;
1298 : }
1299 :
1300 37802851 : tc->parent = tc->next = tc->prev = NULL;
1301 37802851 : return discard_const_p(void, ptr);
1302 : }
1303 :
1304 2582326140 : new_tc = talloc_chunk_from_ptr(new_ctx);
1305 :
1306 2582326140 : if (unlikely(tc == new_tc || tc->parent == new_tc)) {
1307 63977380 : return discard_const_p(void, ptr);
1308 : }
1309 :
1310 2514675943 : if (tc->parent) {
1311 2008309850 : _TLIST_REMOVE(tc->parent->child, tc);
1312 2008309850 : if (tc->parent->child) {
1313 482151893 : tc->parent->child->parent = tc->parent;
1314 : }
1315 : } else {
1316 506366093 : if (tc->prev) tc->prev->next = tc->next;
1317 506366093 : if (tc->next) tc->next->prev = tc->prev;
1318 506366093 : tc->prev = tc->next = NULL;
1319 : }
1320 :
1321 2514675943 : tc->parent = new_tc;
1322 2514675943 : if (new_tc->child) new_tc->child->parent = NULL;
1323 2514675943 : _TLIST_ADD(new_tc->child, tc);
1324 :
1325 2514675943 : if (tc->limit || new_tc->limit) {
1326 6 : ctx_size = _talloc_total_limit_size(ptr, tc->limit,
1327 : new_tc->limit);
1328 : /* .. and increment it in the destination. */
1329 4 : if (new_tc->limit) {
1330 75577255 : talloc_memlimit_grow(new_tc->limit, ctx_size);
1331 : }
1332 : }
1333 :
1334 2445096181 : return discard_const_p(void, ptr);
1335 : }
1336 :
1337 : /*
1338 : move a lump of memory from one talloc context to another returning the
1339 : ptr on success, or NULL if it could not be transferred.
1340 : passing NULL as ptr will always return NULL with no side effects.
1341 : */
1342 4188628667 : _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const char *location)
1343 : {
1344 123826553 : struct talloc_chunk *tc;
1345 :
1346 4188628667 : if (unlikely(ptr == NULL)) {
1347 1899124109 : return NULL;
1348 : }
1349 :
1350 2231675636 : tc = talloc_chunk_from_ptr(ptr);
1351 :
1352 2231675636 : if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
1353 0 : struct talloc_reference_handle *h;
1354 :
1355 0 : talloc_log("WARNING: talloc_steal with references at %s\n",
1356 : location);
1357 :
1358 0 : for (h=tc->refs; h; h=h->next) {
1359 0 : talloc_log("\treference at %s\n",
1360 : h->location);
1361 : }
1362 : }
1363 :
1364 : #if 0
1365 : /* this test is probably too expensive to have on in the
1366 : normal build, but it useful for debugging */
1367 : if (talloc_is_parent(new_ctx, ptr)) {
1368 : talloc_log("WARNING: stealing into talloc child at %s\n", location);
1369 : }
1370 : #endif
1371 :
1372 2231675636 : return _talloc_steal_internal(new_ctx, ptr);
1373 : }
1374 :
1375 : /*
1376 : this is like a talloc_steal(), but you must supply the old
1377 : parent. This resolves the ambiguity in a talloc_steal() which is
1378 : called on a context that has more than one parent (via references)
1379 :
1380 : The old parent can be either a reference or a parent
1381 : */
1382 9925792 : _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, const void *ptr)
1383 : {
1384 182100 : struct talloc_chunk *tc;
1385 182100 : struct talloc_reference_handle *h;
1386 :
1387 9925792 : if (unlikely(ptr == NULL)) {
1388 5850062 : return NULL;
1389 : }
1390 :
1391 3935220 : if (old_parent == talloc_parent(ptr)) {
1392 3760097 : return _talloc_steal_internal(new_parent, ptr);
1393 : }
1394 :
1395 175123 : tc = talloc_chunk_from_ptr(ptr);
1396 175123 : for (h=tc->refs;h;h=h->next) {
1397 0 : if (talloc_parent(h) == old_parent) {
1398 0 : if (_talloc_steal_internal(new_parent, h) != h) {
1399 0 : return NULL;
1400 : }
1401 0 : return discard_const_p(void, ptr);
1402 : }
1403 : }
1404 :
1405 : /* it wasn't a parent */
1406 172890 : return NULL;
1407 : }
1408 :
1409 : /*
1410 : remove a secondary reference to a pointer. This undo's what
1411 : talloc_reference() has done. The context and pointer arguments
1412 : must match those given to a talloc_reference()
1413 : */
1414 92910469 : static inline int talloc_unreference(const void *context, const void *ptr)
1415 : {
1416 92910469 : struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1417 7902268 : struct talloc_reference_handle *h;
1418 :
1419 92910469 : if (unlikely(context == NULL)) {
1420 84166242 : context = null_context;
1421 : }
1422 :
1423 95260302 : for (h=tc->refs;h;h=h->next) {
1424 4694934 : struct talloc_chunk *p = talloc_parent_chunk(h);
1425 4694934 : if (p == NULL) {
1426 0 : if (context == NULL) break;
1427 4694934 : } else if (TC_PTR_FROM_CHUNK(p) == context) {
1428 2131159 : break;
1429 : }
1430 : }
1431 92910469 : if (h == NULL) {
1432 82877042 : return -1;
1433 : }
1434 :
1435 2345101 : return _talloc_free_internal(h, __location__);
1436 : }
1437 :
1438 : /*
1439 : remove a specific parent context from a pointer. This is a more
1440 : controlled variant of talloc_free()
1441 : */
1442 :
1443 : /* coverity[ -tainted_data_sink : arg-1 ] */
1444 93248592 : _PUBLIC_ int talloc_unlink(const void *context, void *ptr)
1445 : {
1446 7850053 : struct talloc_chunk *tc_p, *new_p, *tc_c;
1447 7850053 : void *new_parent;
1448 :
1449 93248592 : if (ptr == NULL) {
1450 2489810 : return -1;
1451 : }
1452 :
1453 90597670 : if (context == NULL) {
1454 84737598 : context = null_context;
1455 : }
1456 :
1457 90597670 : if (talloc_unreference(context, ptr) == 0) {
1458 31687 : return 0;
1459 : }
1460 :
1461 90565368 : if (context != NULL) {
1462 6399126 : tc_c = talloc_chunk_from_ptr(context);
1463 : } else {
1464 76710871 : tc_c = NULL;
1465 : }
1466 90565368 : if (tc_c != talloc_parent_chunk(ptr)) {
1467 179963 : return -1;
1468 : }
1469 :
1470 90384072 : tc_p = talloc_chunk_from_ptr(ptr);
1471 :
1472 90384072 : if (tc_p->refs == NULL) {
1473 88071273 : return _talloc_free_internal(ptr, __location__);
1474 : }
1475 :
1476 2312799 : new_p = talloc_parent_chunk(tc_p->refs);
1477 2312799 : if (new_p) {
1478 2312799 : new_parent = TC_PTR_FROM_CHUNK(new_p);
1479 : } else {
1480 0 : new_parent = NULL;
1481 : }
1482 :
1483 2312799 : if (talloc_unreference(new_parent, ptr) != 0) {
1484 0 : return -1;
1485 : }
1486 :
1487 2312799 : _talloc_steal_internal(new_parent, ptr);
1488 :
1489 2312799 : return 0;
1490 : }
1491 :
1492 : /*
1493 : add a name to an existing pointer - va_list version
1494 : */
1495 : static inline const char *tc_set_name_v(struct talloc_chunk *tc,
1496 : const char *fmt,
1497 : va_list ap) PRINTF_ATTRIBUTE(2,0);
1498 :
1499 28190931 : static inline const char *tc_set_name_v(struct talloc_chunk *tc,
1500 : const char *fmt,
1501 : va_list ap)
1502 : {
1503 32706216 : struct talloc_chunk *name_tc = _vasprintf_tc(TC_PTR_FROM_CHUNK(tc),
1504 : fmt,
1505 : ap);
1506 28190931 : if (likely(name_tc)) {
1507 28190931 : tc->name = TC_PTR_FROM_CHUNK(name_tc);
1508 28190931 : _tc_set_name_const(name_tc, ".name");
1509 : } else {
1510 0 : tc->name = NULL;
1511 : }
1512 28190931 : return tc->name;
1513 : }
1514 :
1515 : /*
1516 : add a name to an existing pointer
1517 : */
1518 16988742 : _PUBLIC_ const char *talloc_set_name(const void *ptr, const char *fmt, ...)
1519 : {
1520 16988742 : struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1521 291936 : const char *name;
1522 291936 : va_list ap;
1523 16988742 : va_start(ap, fmt);
1524 16988742 : name = tc_set_name_v(tc, fmt, ap);
1525 16988742 : va_end(ap);
1526 16988742 : return name;
1527 : }
1528 :
1529 :
1530 : /*
1531 : create a named talloc pointer. Any talloc pointer can be named, and
1532 : talloc_named() operates just like talloc() except that it allows you
1533 : to name the pointer.
1534 : */
1535 9191242 : _PUBLIC_ void *talloc_named(const void *context, size_t size, const char *fmt, ...)
1536 : {
1537 4132438 : va_list ap;
1538 4132438 : void *ptr;
1539 4132438 : const char *name;
1540 9191242 : struct talloc_chunk *tc = NULL;
1541 :
1542 9191242 : ptr = __talloc(context, size, &tc);
1543 9191242 : if (unlikely(ptr == NULL)) return NULL;
1544 :
1545 9191242 : va_start(ap, fmt);
1546 9191242 : name = tc_set_name_v(tc, fmt, ap);
1547 9191242 : va_end(ap);
1548 :
1549 9191242 : if (unlikely(name == NULL)) {
1550 0 : _talloc_free_internal(ptr, __location__);
1551 0 : return NULL;
1552 : }
1553 :
1554 5058804 : return ptr;
1555 : }
1556 :
1557 : /*
1558 : return the name of a talloc ptr, or "UNNAMED"
1559 : */
1560 12559044733 : static inline const char *__talloc_get_name(const void *ptr)
1561 : {
1562 12930701438 : struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
1563 12559044588 : if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
1564 0 : return ".reference";
1565 : }
1566 12559044588 : if (likely(tc->name)) {
1567 12559044557 : return tc->name;
1568 : }
1569 43 : return "UNNAMED";
1570 : }
1571 :
1572 3771298 : _PUBLIC_ const char *talloc_get_name(const void *ptr)
1573 : {
1574 3771298 : return __talloc_get_name(ptr);
1575 : }
1576 :
1577 : /*
1578 : check if a pointer has the given name. If it does, return the pointer,
1579 : otherwise return NULL
1580 : */
1581 8812155298 : _PUBLIC_ void *talloc_check_name(const void *ptr, const char *name)
1582 : {
1583 271625733 : const char *pname;
1584 8812155298 : if (unlikely(ptr == NULL)) return NULL;
1585 8797060690 : pname = __talloc_get_name(ptr);
1586 8797060690 : if (likely(pname == name || strcmp(pname, name) == 0)) {
1587 8761911386 : return discard_const_p(void, ptr);
1588 : }
1589 34889551 : return NULL;
1590 : }
1591 :
1592 0 : static void talloc_abort_type_mismatch(const char *location,
1593 : const char *name,
1594 : const char *expected)
1595 : {
1596 0 : const char *reason;
1597 :
1598 0 : reason = talloc_asprintf(NULL,
1599 : "%s: Type mismatch: name[%s] expected[%s]",
1600 : location,
1601 : name?name:"NULL",
1602 : expected);
1603 0 : if (!reason) {
1604 0 : reason = "Type mismatch";
1605 : }
1606 :
1607 0 : talloc_abort(reason);
1608 0 : }
1609 :
1610 3758211902 : _PUBLIC_ void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location)
1611 : {
1612 100811588 : const char *pname;
1613 :
1614 3758211902 : if (unlikely(ptr == NULL)) {
1615 0 : talloc_abort_type_mismatch(location, NULL, name);
1616 0 : return NULL;
1617 : }
1618 :
1619 3758211902 : pname = __talloc_get_name(ptr);
1620 3758211578 : if (likely(pname == name || strcmp(pname, name) == 0)) {
1621 3657399998 : return discard_const_p(void, ptr);
1622 : }
1623 :
1624 0 : talloc_abort_type_mismatch(location, pname, name);
1625 0 : return NULL;
1626 : }
1627 :
1628 : /*
1629 : this is for compatibility with older versions of talloc
1630 : */
1631 2010947 : _PUBLIC_ void *talloc_init(const char *fmt, ...)
1632 : {
1633 90911 : va_list ap;
1634 90911 : void *ptr;
1635 90911 : const char *name;
1636 2010947 : struct talloc_chunk *tc = NULL;
1637 :
1638 2010947 : ptr = __talloc(NULL, 0, &tc);
1639 2010947 : if (unlikely(ptr == NULL)) return NULL;
1640 :
1641 2010947 : va_start(ap, fmt);
1642 2010947 : name = tc_set_name_v(tc, fmt, ap);
1643 2010947 : va_end(ap);
1644 :
1645 2010947 : if (unlikely(name == NULL)) {
1646 0 : _talloc_free_internal(ptr, __location__);
1647 0 : return NULL;
1648 : }
1649 :
1650 1920036 : return ptr;
1651 : }
1652 :
1653 39913319443 : static inline void _tc_free_children_internal(struct talloc_chunk *tc,
1654 : void *ptr,
1655 : const char *location)
1656 : {
1657 80751668288 : while (tc->child) {
1658 : /* we need to work out who will own an abandoned child
1659 : if it cannot be freed. In priority order, the first
1660 : choice is owner of any remaining reference to this
1661 : pointer, the second choice is our parent, and the
1662 : final choice is the null context. */
1663 31343382939 : void *child = TC_PTR_FROM_CHUNK(tc->child);
1664 31343382939 : const void *new_parent = null_context;
1665 31343382939 : if (unlikely(tc->child->refs)) {
1666 25196643 : struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
1667 25196643 : if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1668 : }
1669 31343382939 : if (unlikely(_tc_free_internal(tc->child, location) == -1)) {
1670 391709097 : if (talloc_parent_chunk(child) != tc) {
1671 : /*
1672 : * Destructor already reparented this child.
1673 : * No further reparenting needed.
1674 : */
1675 1682 : continue;
1676 : }
1677 382380459 : if (new_parent == null_context) {
1678 357198160 : struct talloc_chunk *p = talloc_parent_chunk(ptr);
1679 357198160 : if (p) new_parent = TC_PTR_FROM_CHUNK(p);
1680 : }
1681 382380459 : _talloc_steal_internal(new_parent, child);
1682 : }
1683 : }
1684 39913319443 : }
1685 :
1686 : /*
1687 : this is a replacement for the Samba3 talloc_destroy_pool functionality. It
1688 : should probably not be used in new code. It's in here to keep the talloc
1689 : code consistent across Samba 3 and 4.
1690 : */
1691 587548602 : _PUBLIC_ void talloc_free_children(void *ptr)
1692 : {
1693 587548602 : struct talloc_chunk *tc_name = NULL;
1694 2599130 : struct talloc_chunk *tc;
1695 :
1696 587548602 : if (unlikely(ptr == NULL)) {
1697 0 : return;
1698 : }
1699 :
1700 587548602 : tc = talloc_chunk_from_ptr(ptr);
1701 :
1702 : /* we do not want to free the context name if it is a child .. */
1703 587548602 : if (likely(tc->child)) {
1704 566022780 : for (tc_name = tc->child; tc_name; tc_name = tc_name->next) {
1705 311238670 : if (tc->name == TC_PTR_FROM_CHUNK(tc_name)) break;
1706 : }
1707 254784417 : if (tc_name) {
1708 307 : _TLIST_REMOVE(tc->child, tc_name);
1709 307 : if (tc->child) {
1710 307 : tc->child->parent = tc;
1711 : }
1712 : }
1713 : }
1714 :
1715 587548602 : _tc_free_children_internal(tc, ptr, __location__);
1716 :
1717 : /* .. so we put it back after all other children have been freed */
1718 587548602 : if (tc_name) {
1719 307 : if (tc->child) {
1720 0 : tc->child->parent = NULL;
1721 : }
1722 307 : tc_name->parent = tc;
1723 307 : _TLIST_ADD(tc->child, tc_name);
1724 : }
1725 : }
1726 :
1727 : /*
1728 : Allocate a bit of memory as a child of an existing pointer
1729 : */
1730 0 : _PUBLIC_ void *_talloc(const void *context, size_t size)
1731 : {
1732 0 : struct talloc_chunk *tc;
1733 0 : return __talloc(context, size, &tc);
1734 : }
1735 :
1736 : /*
1737 : externally callable talloc_set_name_const()
1738 : */
1739 3785675431 : _PUBLIC_ void talloc_set_name_const(const void *ptr, const char *name)
1740 : {
1741 3785675431 : _tc_set_name_const(talloc_chunk_from_ptr(ptr), name);
1742 3785675431 : }
1743 :
1744 : /*
1745 : create a named talloc pointer. Any talloc pointer can be named, and
1746 : talloc_named() operates just like talloc() except that it allows you
1747 : to name the pointer.
1748 : */
1749 4341051677 : _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *name)
1750 : {
1751 4341051677 : return _talloc_named_const(context, size, name);
1752 : }
1753 :
1754 : /*
1755 : free a talloc pointer. This also frees all child pointers of this
1756 : pointer recursively
1757 :
1758 : return 0 if the memory is actually freed, otherwise -1. The memory
1759 : will not be freed if the ref_count is > 1 or the destructor (if
1760 : any) returns non-zero
1761 : */
1762 8514879785 : _PUBLIC_ int _talloc_free(void *ptr, const char *location)
1763 : {
1764 338608371 : struct talloc_chunk *tc;
1765 :
1766 8514879785 : if (unlikely(ptr == NULL)) {
1767 228685204 : return -1;
1768 : }
1769 :
1770 8279142430 : tc = talloc_chunk_from_ptr(ptr);
1771 :
1772 8279142430 : if (unlikely(tc->refs != NULL)) {
1773 17423 : struct talloc_reference_handle *h;
1774 :
1775 338456 : if (talloc_parent(ptr) == null_context && tc->refs->next == NULL) {
1776 : /* in this case we do know which parent should
1777 : get this pointer, as there is really only
1778 : one parent */
1779 338454 : return talloc_unlink(null_context, ptr);
1780 : }
1781 :
1782 2 : talloc_log("ERROR: talloc_free with references at %s\n",
1783 : location);
1784 :
1785 6 : for (h=tc->refs; h; h=h->next) {
1786 4 : talloc_log("\treference at %s\n",
1787 : h->location);
1788 : }
1789 2 : return -1;
1790 : }
1791 :
1792 8278803974 : return _talloc_free_internal(ptr, location);
1793 : }
1794 :
1795 :
1796 :
1797 : /*
1798 : A talloc version of realloc. The context argument is only used if
1799 : ptr is NULL
1800 : */
1801 5441438237 : _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
1802 : {
1803 132517703 : struct talloc_chunk *tc;
1804 132517703 : void *new_ptr;
1805 5441438237 : bool malloced = false;
1806 5441438237 : struct talloc_pool_hdr *pool_hdr = NULL;
1807 5441438237 : size_t old_size = 0;
1808 5441438237 : size_t new_size = 0;
1809 :
1810 : /* size zero is equivalent to free() */
1811 5441438237 : if (unlikely(size == 0)) {
1812 163184 : talloc_unlink(context, ptr);
1813 163184 : return NULL;
1814 : }
1815 :
1816 5441275053 : if (unlikely(size >= MAX_TALLOC_SIZE)) {
1817 1 : return NULL;
1818 : }
1819 :
1820 : /* realloc(NULL) is equivalent to malloc() */
1821 5441275051 : if (ptr == NULL) {
1822 836780809 : return _talloc_named_const(context, size, name);
1823 : }
1824 :
1825 4627721345 : tc = talloc_chunk_from_ptr(ptr);
1826 :
1827 : /* don't allow realloc on referenced pointers */
1828 4627721345 : if (unlikely(tc->refs)) {
1829 1 : return NULL;
1830 : }
1831 :
1832 : /* don't let anybody try to realloc a talloc_pool */
1833 4627721343 : if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
1834 0 : return NULL;
1835 : }
1836 :
1837 : /* handle realloc inside a talloc_pool */
1838 4627721343 : if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
1839 15222714 : pool_hdr = tc->pool;
1840 : }
1841 :
1842 : /* don't shrink if we have less than 1k to gain */
1843 4627721343 : if (size < tc->size && tc->limit == NULL) {
1844 1074910108 : if (pool_hdr) {
1845 1618053 : void *next_tc = tc_next_chunk(tc);
1846 1618053 : TC_INVALIDATE_SHRINK_CHUNK(tc, size);
1847 1618053 : tc->size = size;
1848 1618053 : if (next_tc == pool_hdr->end) {
1849 : /* note: tc->size has changed, so this works */
1850 1617199 : pool_hdr->end = tc_next_chunk(tc);
1851 : }
1852 1618053 : return ptr;
1853 1073292055 : } else if ((tc->size - size) < 1024) {
1854 : /*
1855 : * if we call TC_INVALIDATE_SHRINK_CHUNK() here
1856 : * we would need to call TC_UNDEFINE_GROW_CHUNK()
1857 : * after each realloc call, which slows down
1858 : * testing a lot :-(.
1859 : *
1860 : * That is why we only mark memory as undefined here.
1861 : */
1862 1067517125 : TC_UNDEFINE_SHRINK_CHUNK(tc, size);
1863 :
1864 : /* do not shrink if we have less than 1k to gain */
1865 1067517125 : tc->size = size;
1866 1067517125 : return ptr;
1867 : }
1868 3552811235 : } else if (tc->size == size) {
1869 : /*
1870 : * do not change the pointer if it is exactly
1871 : * the same size.
1872 : */
1873 849628982 : return ptr;
1874 : }
1875 :
1876 : /*
1877 : * by resetting magic we catch users of the old memory
1878 : *
1879 : * We mark this memory as free, and also over-stamp the talloc
1880 : * magic with the old-style magic.
1881 : *
1882 : * Why? This tries to avoid a memory read use-after-free from
1883 : * disclosing our talloc magic, which would then allow an
1884 : * attacker to prepare a valid header and so run a destructor.
1885 : *
1886 : * What else? We have to re-stamp back a valid normal magic
1887 : * on this memory once realloc() is done, as it will have done
1888 : * a memcpy() into the new valid memory. We can't do this in
1889 : * reverse as that would be a real use-after-free.
1890 : */
1891 2688477618 : _talloc_chunk_set_free(tc, NULL);
1892 :
1893 2688477618 : if (pool_hdr) {
1894 169537 : struct talloc_chunk *pool_tc;
1895 13532632 : void *next_tc = tc_next_chunk(tc);
1896 13532632 : size_t old_chunk_size = TC_ALIGN16(TC_HDR_SIZE + tc->size);
1897 13532632 : size_t new_chunk_size = TC_ALIGN16(TC_HDR_SIZE + size);
1898 169537 : size_t space_needed;
1899 169537 : size_t space_left;
1900 13532632 : unsigned int chunk_count = pool_hdr->object_count;
1901 :
1902 13532632 : pool_tc = talloc_chunk_from_pool(pool_hdr);
1903 13532632 : if (!(pool_tc->flags & TALLOC_FLAG_FREE)) {
1904 12939523 : chunk_count -= 1;
1905 : }
1906 :
1907 13532632 : if (chunk_count == 1) {
1908 : /*
1909 : * optimize for the case where 'tc' is the only
1910 : * chunk in the pool.
1911 : */
1912 588627 : char *start = tc_pool_first_chunk(pool_hdr);
1913 588627 : space_needed = new_chunk_size;
1914 588627 : space_left = (char *)tc_pool_end(pool_hdr) - start;
1915 :
1916 588627 : if (space_left >= space_needed) {
1917 588142 : size_t old_used = TC_HDR_SIZE + tc->size;
1918 588142 : size_t new_used = TC_HDR_SIZE + size;
1919 588142 : new_ptr = start;
1920 :
1921 : #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
1922 : {
1923 : /*
1924 : * The area from
1925 : * start -> tc may have
1926 : * been freed and thus been marked as
1927 : * VALGRIND_MEM_NOACCESS. Set it to
1928 : * VALGRIND_MEM_UNDEFINED so we can
1929 : * copy into it without valgrind errors.
1930 : * We can't just mark
1931 : * new_ptr -> new_ptr + old_used
1932 : * as this may overlap on top of tc,
1933 : * (which is why we use memmove, not
1934 : * memcpy below) hence the MIN.
1935 : */
1936 : size_t undef_len = MIN((((char *)tc) - ((char *)new_ptr)),old_used);
1937 : VALGRIND_MAKE_MEM_UNDEFINED(new_ptr, undef_len);
1938 : }
1939 : #endif
1940 :
1941 588142 : memmove(new_ptr, tc, old_used);
1942 :
1943 588142 : tc = (struct talloc_chunk *)new_ptr;
1944 9 : TC_UNDEFINE_GROW_CHUNK(tc, size);
1945 :
1946 : /*
1947 : * first we do not align the pool pointer
1948 : * because we want to invalidate the padding
1949 : * too.
1950 : */
1951 588142 : pool_hdr->end = new_used + (char *)new_ptr;
1952 588142 : tc_invalidate_pool(pool_hdr);
1953 :
1954 : /* now the aligned pointer */
1955 588142 : pool_hdr->end = new_chunk_size + (char *)new_ptr;
1956 588142 : goto got_new_ptr;
1957 : }
1958 :
1959 478 : next_tc = NULL;
1960 : }
1961 :
1962 12944490 : if (new_chunk_size == old_chunk_size) {
1963 16377 : TC_UNDEFINE_GROW_CHUNK(tc, size);
1964 1240294 : _talloc_chunk_set_not_free(tc);
1965 1240294 : tc->size = size;
1966 1240294 : return ptr;
1967 : }
1968 :
1969 11704196 : if (next_tc == pool_hdr->end) {
1970 : /*
1971 : * optimize for the case where 'tc' is the last
1972 : * chunk in the pool.
1973 : */
1974 11300891 : space_needed = new_chunk_size - old_chunk_size;
1975 11300891 : space_left = tc_pool_space_left(pool_hdr);
1976 :
1977 11300891 : if (space_left >= space_needed) {
1978 128414 : TC_UNDEFINE_GROW_CHUNK(tc, size);
1979 8886270 : _talloc_chunk_set_not_free(tc);
1980 8886270 : tc->size = size;
1981 8886270 : pool_hdr->end = tc_next_chunk(tc);
1982 8886270 : return ptr;
1983 : }
1984 : }
1985 :
1986 2817926 : new_ptr = tc_alloc_pool(tc, size + TC_HDR_SIZE, 0);
1987 :
1988 2817926 : if (new_ptr == NULL) {
1989 : /*
1990 : * Couldn't allocate from pool (pool size
1991 : * counts as already allocated for memlimit
1992 : * purposes). We must check memory limit
1993 : * before any real malloc.
1994 : */
1995 2464859 : if (tc->limit) {
1996 : /*
1997 : * Note we're doing an extra malloc,
1998 : * on top of the pool size, so account
1999 : * for size only, not the difference
2000 : * between old and new size.
2001 : */
2002 8 : if (!talloc_memlimit_check(tc->limit, size)) {
2003 6 : _talloc_chunk_set_not_free(tc);
2004 6 : errno = ENOMEM;
2005 6 : return NULL;
2006 : }
2007 : }
2008 2464853 : new_ptr = malloc(TC_HDR_SIZE+size);
2009 2464853 : malloced = true;
2010 2464853 : new_size = size;
2011 : }
2012 :
2013 2817920 : if (new_ptr) {
2014 2817920 : memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
2015 :
2016 2817920 : _tc_free_poolmem(tc, __location__ "_talloc_realloc");
2017 : }
2018 : }
2019 : else {
2020 : /* We're doing realloc here, so record the difference. */
2021 2674944986 : old_size = tc->size;
2022 2674944986 : new_size = size;
2023 : /*
2024 : * We must check memory limit
2025 : * before any real realloc.
2026 : */
2027 2674944986 : if (tc->limit && (size > old_size)) {
2028 2 : if (!talloc_memlimit_check(tc->limit,
2029 : (size - old_size))) {
2030 2 : _talloc_chunk_set_not_free(tc);
2031 2 : errno = ENOMEM;
2032 2 : return NULL;
2033 : }
2034 : }
2035 2674944984 : new_ptr = realloc(tc, size + TC_HDR_SIZE);
2036 : }
2037 2678351046 : got_new_ptr:
2038 :
2039 2678351046 : if (unlikely(!new_ptr)) {
2040 : /*
2041 : * Ok, this is a strange spot. We have to put back
2042 : * the old talloc_magic and any flags, except the
2043 : * TALLOC_FLAG_FREE as this was not free'ed by the
2044 : * realloc() call after all
2045 : */
2046 0 : _talloc_chunk_set_not_free(tc);
2047 0 : return NULL;
2048 : }
2049 :
2050 : /*
2051 : * tc is now the new value from realloc(), the old memory we
2052 : * can't access any more and was preemptively marked as
2053 : * TALLOC_FLAG_FREE before the call. Now we mark it as not
2054 : * free again
2055 : */
2056 2678351046 : tc = (struct talloc_chunk *)new_ptr;
2057 2678351046 : _talloc_chunk_set_not_free(tc);
2058 2678351046 : if (malloced) {
2059 2464853 : tc->flags &= ~TALLOC_FLAG_POOLMEM;
2060 : }
2061 2678351046 : if (tc->parent) {
2062 1979304898 : tc->parent->child = tc;
2063 : }
2064 2678351046 : if (tc->child) {
2065 1472471961 : tc->child->parent = tc;
2066 : }
2067 :
2068 2678351046 : if (tc->prev) {
2069 692986306 : tc->prev->next = tc;
2070 : }
2071 2678351046 : if (tc->next) {
2072 1711300061 : tc->next->prev = tc;
2073 : }
2074 :
2075 2678351046 : if (new_size > old_size) {
2076 2738301734 : talloc_memlimit_grow(tc->limit, new_size - old_size);
2077 6716141 : } else if (new_size < old_size) {
2078 72441761 : talloc_memlimit_shrink(tc->limit, old_size - new_size);
2079 : }
2080 :
2081 2678351046 : tc->size = size;
2082 2678351046 : _tc_set_name_const(tc, name);
2083 :
2084 2678351046 : return TC_PTR_FROM_CHUNK(tc);
2085 : }
2086 :
2087 : /*
2088 : a wrapper around talloc_steal() for situations where you are moving a pointer
2089 : between two structures, and want the old pointer to be set to NULL
2090 : */
2091 317041305 : _PUBLIC_ void *_talloc_move(const void *new_ctx, const void *_pptr)
2092 : {
2093 317041305 : const void **pptr = discard_const_p(const void *,_pptr);
2094 317041305 : void *ret = talloc_steal(new_ctx, discard_const_p(void, *pptr));
2095 317041305 : (*pptr) = NULL;
2096 317041305 : return ret;
2097 : }
2098 :
2099 : enum talloc_mem_count_type {
2100 : TOTAL_MEM_SIZE,
2101 : TOTAL_MEM_BLOCKS,
2102 : TOTAL_MEM_LIMIT,
2103 : };
2104 :
2105 730867535 : static inline size_t _talloc_total_mem_internal(const void *ptr,
2106 : enum talloc_mem_count_type type,
2107 : struct talloc_memlimit *old_limit,
2108 : struct talloc_memlimit *new_limit)
2109 : {
2110 730867535 : size_t total = 0;
2111 3311024 : struct talloc_chunk *c, *tc;
2112 :
2113 730867535 : if (ptr == NULL) {
2114 34 : ptr = null_context;
2115 : }
2116 730867535 : if (ptr == NULL) {
2117 1 : return 0;
2118 : }
2119 :
2120 730867532 : tc = talloc_chunk_from_ptr(ptr);
2121 :
2122 730867532 : if (old_limit || new_limit) {
2123 14 : if (tc->limit && tc->limit->upper == old_limit) {
2124 0 : tc->limit->upper = new_limit;
2125 : }
2126 : }
2127 :
2128 : /* optimize in the memlimits case */
2129 730867532 : if (type == TOTAL_MEM_LIMIT &&
2130 18 : tc->limit != NULL &&
2131 7 : tc->limit != old_limit &&
2132 4 : tc->limit->parent == tc) {
2133 0 : return tc->limit->cur_size;
2134 : }
2135 :
2136 730867532 : if (tc->flags & TALLOC_FLAG_LOOP) {
2137 138950623 : return 0;
2138 : }
2139 :
2140 591228606 : tc->flags |= TALLOC_FLAG_LOOP;
2141 :
2142 591228606 : if (old_limit || new_limit) {
2143 14 : if (old_limit == tc->limit) {
2144 14 : tc->limit = new_limit;
2145 : }
2146 : }
2147 :
2148 591228606 : switch (type) {
2149 3677757 : case TOTAL_MEM_SIZE:
2150 3677757 : if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
2151 3677717 : total = tc->size;
2152 : }
2153 3655404 : break;
2154 587550831 : case TOTAL_MEM_BLOCKS:
2155 587550831 : total++;
2156 587550831 : break;
2157 18 : case TOTAL_MEM_LIMIT:
2158 18 : if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
2159 : /*
2160 : * Don't count memory allocated from a pool
2161 : * when calculating limits. Only count the
2162 : * pool itself.
2163 : */
2164 18 : if (!(tc->flags & TALLOC_FLAG_POOLMEM)) {
2165 16 : if (tc->flags & TALLOC_FLAG_POOL) {
2166 : /*
2167 : * If this is a pool, the allocated
2168 : * size is in the pool header, and
2169 : * remember to add in the prefix
2170 : * length.
2171 : */
2172 1 : struct talloc_pool_hdr *pool_hdr
2173 2 : = talloc_pool_from_chunk(tc);
2174 2 : total = pool_hdr->poolsize +
2175 : TC_HDR_SIZE +
2176 : TP_HDR_SIZE;
2177 : } else {
2178 14 : total = tc->size + TC_HDR_SIZE;
2179 : }
2180 : }
2181 : }
2182 9 : break;
2183 : }
2184 594194288 : for (c = tc->child; c; c = c->next) {
2185 2965682 : total += _talloc_total_mem_internal(TC_PTR_FROM_CHUNK(c), type,
2186 : old_limit, new_limit);
2187 : }
2188 :
2189 591228606 : tc->flags &= ~TALLOC_FLAG_LOOP;
2190 :
2191 591228606 : return total;
2192 : }
2193 :
2194 : /*
2195 : return the total size of a talloc pool (subtree)
2196 : */
2197 713521 : _PUBLIC_ size_t talloc_total_size(const void *ptr)
2198 : {
2199 713521 : return _talloc_total_mem_internal(ptr, TOTAL_MEM_SIZE, NULL, NULL);
2200 : }
2201 :
2202 : /*
2203 : return the total number of blocks in a talloc pool (subtree)
2204 : */
2205 727188314 : _PUBLIC_ size_t talloc_total_blocks(const void *ptr)
2206 : {
2207 727188314 : return _talloc_total_mem_internal(ptr, TOTAL_MEM_BLOCKS, NULL, NULL);
2208 : }
2209 :
2210 : /*
2211 : return the number of external references to a pointer
2212 : */
2213 32020 : _PUBLIC_ size_t talloc_reference_count(const void *ptr)
2214 : {
2215 32020 : struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
2216 336 : struct talloc_reference_handle *h;
2217 32020 : size_t ret = 0;
2218 :
2219 32077 : for (h=tc->refs;h;h=h->next) {
2220 57 : ret++;
2221 : }
2222 32020 : return ret;
2223 : }
2224 :
2225 : /*
2226 : report on memory usage by all children of a pointer, giving a full tree view
2227 : */
2228 4059 : _PUBLIC_ void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
2229 : void (*callback)(const void *ptr,
2230 : int depth, int max_depth,
2231 : int is_ref,
2232 : void *private_data),
2233 : void *private_data)
2234 : {
2235 422 : struct talloc_chunk *c, *tc;
2236 :
2237 4059 : if (ptr == NULL) {
2238 3161 : ptr = null_context;
2239 : }
2240 4059 : if (ptr == NULL) return;
2241 :
2242 906 : tc = talloc_chunk_from_ptr(ptr);
2243 :
2244 906 : if (tc->flags & TALLOC_FLAG_LOOP) {
2245 0 : return;
2246 : }
2247 :
2248 906 : callback(ptr, depth, max_depth, 0, private_data);
2249 :
2250 906 : if (max_depth >= 0 && depth >= max_depth) {
2251 142 : return;
2252 : }
2253 :
2254 662 : tc->flags |= TALLOC_FLAG_LOOP;
2255 1391 : for (c=tc->child;c;c=c->next) {
2256 729 : if (c->name == TALLOC_MAGIC_REFERENCE) {
2257 16 : struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
2258 16 : callback(h->ptr, depth + 1, max_depth, 1, private_data);
2259 : } else {
2260 713 : talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
2261 : }
2262 : }
2263 662 : tc->flags &= ~TALLOC_FLAG_LOOP;
2264 : }
2265 :
2266 922 : static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
2267 : {
2268 922 : const char *name = __talloc_get_name(ptr);
2269 430 : struct talloc_chunk *tc;
2270 922 : FILE *f = (FILE *)_f;
2271 :
2272 922 : if (is_ref) {
2273 16 : fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
2274 16 : return;
2275 : }
2276 :
2277 906 : tc = talloc_chunk_from_ptr(ptr);
2278 906 : if (tc->limit && tc->limit->parent == tc) {
2279 564 : fprintf(f, "%*s%-30s is a memlimit context"
2280 : " (max_size = %lu bytes, cur_size = %lu bytes)\n",
2281 : depth*4, "",
2282 : name,
2283 142 : (unsigned long)tc->limit->max_size,
2284 142 : (unsigned long)tc->limit->cur_size);
2285 : }
2286 :
2287 906 : if (depth == 0) {
2288 193 : fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
2289 : (max_depth < 0 ? "full " :""), name,
2290 193 : (unsigned long)talloc_total_size(ptr),
2291 193 : (unsigned long)talloc_total_blocks(ptr));
2292 193 : return;
2293 : }
2294 :
2295 713 : fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
2296 : depth*4, "",
2297 : name,
2298 713 : (unsigned long)talloc_total_size(ptr),
2299 713 : (unsigned long)talloc_total_blocks(ptr),
2300 713 : (int)talloc_reference_count(ptr), ptr);
2301 :
2302 : #if 0
2303 : fprintf(f, "content: ");
2304 : if (talloc_total_size(ptr)) {
2305 : int tot = talloc_total_size(ptr);
2306 : int i;
2307 :
2308 : for (i = 0; i < tot; i++) {
2309 : if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
2310 : fprintf(f, "%c", ((char *)ptr)[i]);
2311 : } else {
2312 : fprintf(f, "~%02x", ((char *)ptr)[i]);
2313 : }
2314 : }
2315 : }
2316 : fprintf(f, "\n");
2317 : #endif
2318 : }
2319 :
2320 : /*
2321 : report on memory usage by all children of a pointer, giving a full tree view
2322 : */
2323 3346 : _PUBLIC_ void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
2324 : {
2325 3346 : if (f) {
2326 3346 : talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
2327 3346 : fflush(f);
2328 : }
2329 3346 : }
2330 :
2331 : /*
2332 : report on memory usage by all children of a pointer, giving a full tree view
2333 : */
2334 165 : _PUBLIC_ void talloc_report_full(const void *ptr, FILE *f)
2335 : {
2336 165 : talloc_report_depth_file(ptr, 0, -1, f);
2337 165 : }
2338 :
2339 : /*
2340 : report on memory usage by all children of a pointer
2341 : */
2342 3181 : _PUBLIC_ void talloc_report(const void *ptr, FILE *f)
2343 : {
2344 3181 : talloc_report_depth_file(ptr, 0, 1, f);
2345 3181 : }
2346 :
2347 : /*
2348 : enable tracking of the NULL context
2349 : */
2350 49998 : _PUBLIC_ void talloc_enable_null_tracking(void)
2351 : {
2352 49998 : if (null_context == NULL) {
2353 49982 : null_context = _talloc_named_const(NULL, 0, "null_context");
2354 49982 : if (autofree_context != NULL) {
2355 0 : talloc_reparent(NULL, null_context, autofree_context);
2356 : }
2357 : }
2358 49998 : }
2359 :
2360 : /*
2361 : enable tracking of the NULL context, not moving the autofree context
2362 : into the NULL context. This is needed for the talloc testsuite
2363 : */
2364 74 : _PUBLIC_ void talloc_enable_null_tracking_no_autofree(void)
2365 : {
2366 74 : if (null_context == NULL) {
2367 108 : null_context = _talloc_named_const(NULL, 0, "null_context");
2368 : }
2369 74 : }
2370 :
2371 : /*
2372 : disable tracking of the NULL context
2373 : */
2374 80 : _PUBLIC_ void talloc_disable_null_tracking(void)
2375 : {
2376 80 : if (null_context != NULL) {
2377 : /* we have to move any children onto the real NULL
2378 : context */
2379 36 : struct talloc_chunk *tc, *tc2;
2380 72 : tc = talloc_chunk_from_ptr(null_context);
2381 72 : for (tc2 = tc->child; tc2; tc2=tc2->next) {
2382 0 : if (tc2->parent == tc) tc2->parent = NULL;
2383 0 : if (tc2->prev == tc) tc2->prev = NULL;
2384 : }
2385 72 : for (tc2 = tc->next; tc2; tc2=tc2->next) {
2386 0 : if (tc2->parent == tc) tc2->parent = NULL;
2387 0 : if (tc2->prev == tc) tc2->prev = NULL;
2388 : }
2389 72 : tc->child = NULL;
2390 72 : tc->next = NULL;
2391 : }
2392 80 : talloc_free(null_context);
2393 80 : null_context = NULL;
2394 80 : }
2395 :
2396 : /*
2397 : enable leak reporting on exit
2398 : */
2399 22 : _PUBLIC_ void talloc_enable_leak_report(void)
2400 : {
2401 22 : talloc_enable_null_tracking();
2402 22 : talloc_report_null = true;
2403 22 : talloc_setup_atexit();
2404 22 : }
2405 :
2406 : /*
2407 : enable full leak reporting on exit
2408 : */
2409 2 : _PUBLIC_ void talloc_enable_leak_report_full(void)
2410 : {
2411 2 : talloc_enable_null_tracking();
2412 2 : talloc_report_null_full = true;
2413 2 : talloc_setup_atexit();
2414 2 : }
2415 :
2416 : /*
2417 : talloc and zero memory.
2418 : */
2419 8155544099 : _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
2420 : {
2421 8155544099 : void *p = _talloc_named_const(ctx, size, name);
2422 :
2423 8155544099 : if (p) {
2424 8155544099 : memset(p, '\0', size);
2425 : }
2426 :
2427 8155544099 : return p;
2428 : }
2429 :
2430 : /*
2431 : memdup with a talloc.
2432 : */
2433 6790906712 : _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
2434 : {
2435 6790906712 : void *newp = NULL;
2436 :
2437 6790906712 : if (likely(size > 0) && unlikely(p == NULL)) {
2438 0 : return NULL;
2439 : }
2440 :
2441 6790906712 : newp = _talloc_named_const(t, size, name);
2442 6790906712 : if (likely(newp != NULL) && likely(size > 0)) {
2443 6789243654 : memcpy(newp, p, size);
2444 : }
2445 :
2446 6432034742 : return newp;
2447 : }
2448 :
2449 10718273647 : static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
2450 : {
2451 396476403 : char *ret;
2452 10718273647 : struct talloc_chunk *tc = NULL;
2453 :
2454 10718273647 : ret = (char *)__talloc(t, len + 1, &tc);
2455 10718273647 : if (unlikely(!ret)) return NULL;
2456 :
2457 10718273645 : memcpy(ret, p, len);
2458 10718273645 : ret[len] = 0;
2459 :
2460 10718273645 : _tc_set_name_const(tc, ret);
2461 10718273645 : return ret;
2462 : }
2463 :
2464 : /*
2465 : strdup with a talloc
2466 : */
2467 9071071197 : _PUBLIC_ char *talloc_strdup(const void *t, const char *p)
2468 : {
2469 9071071197 : if (unlikely(!p)) return NULL;
2470 9070459082 : return __talloc_strlendup(t, p, strlen(p));
2471 : }
2472 :
2473 : /*
2474 : strndup with a talloc
2475 : */
2476 1647814633 : _PUBLIC_ char *talloc_strndup(const void *t, const char *p, size_t n)
2477 : {
2478 1647814633 : if (unlikely(!p)) return NULL;
2479 1647814565 : return __talloc_strlendup(t, p, strnlen(p, n));
2480 : }
2481 :
2482 340977953 : static inline char *__talloc_strlendup_append(char *s, size_t slen,
2483 : const char *a, size_t alen)
2484 : {
2485 11389631 : char *ret;
2486 :
2487 340977953 : ret = talloc_realloc(NULL, s, char, slen + alen + 1);
2488 340977953 : if (unlikely(!ret)) return NULL;
2489 :
2490 : /* append the string and the trailing \0 */
2491 340977953 : memcpy(&ret[slen], a, alen);
2492 340977953 : ret[slen+alen] = 0;
2493 :
2494 340977953 : _tc_set_name_const(talloc_chunk_from_ptr(ret), ret);
2495 340977953 : return ret;
2496 : }
2497 :
2498 : /*
2499 : * Appends at the end of the string.
2500 : */
2501 226194 : _PUBLIC_ char *talloc_strdup_append(char *s, const char *a)
2502 : {
2503 226194 : if (unlikely(!s)) {
2504 0 : return talloc_strdup(NULL, a);
2505 : }
2506 :
2507 226194 : if (unlikely(!a)) {
2508 0 : return s;
2509 : }
2510 :
2511 226194 : return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
2512 : }
2513 :
2514 : /*
2515 : * Appends at the end of the talloc'ed buffer,
2516 : * not the end of the string.
2517 : */
2518 340751725 : _PUBLIC_ char *talloc_strdup_append_buffer(char *s, const char *a)
2519 : {
2520 11389544 : size_t slen;
2521 :
2522 340751725 : if (unlikely(!s)) {
2523 0 : return talloc_strdup(NULL, a);
2524 : }
2525 :
2526 340751725 : if (unlikely(!a)) {
2527 0 : return s;
2528 : }
2529 :
2530 340751725 : slen = talloc_get_size(s);
2531 340751725 : if (likely(slen > 0)) {
2532 340751725 : slen--;
2533 : }
2534 :
2535 340751725 : return __talloc_strlendup_append(s, slen, a, strlen(a));
2536 : }
2537 :
2538 : /*
2539 : * Appends at the end of the string.
2540 : */
2541 32 : _PUBLIC_ char *talloc_strndup_append(char *s, const char *a, size_t n)
2542 : {
2543 32 : if (unlikely(!s)) {
2544 0 : return talloc_strndup(NULL, a, n);
2545 : }
2546 :
2547 32 : if (unlikely(!a)) {
2548 0 : return s;
2549 : }
2550 :
2551 32 : return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
2552 : }
2553 :
2554 : /*
2555 : * Appends at the end of the talloc'ed buffer,
2556 : * not the end of the string.
2557 : */
2558 2 : _PUBLIC_ char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
2559 : {
2560 0 : size_t slen;
2561 :
2562 2 : if (unlikely(!s)) {
2563 0 : return talloc_strndup(NULL, a, n);
2564 : }
2565 :
2566 2 : if (unlikely(!a)) {
2567 0 : return s;
2568 : }
2569 :
2570 2 : slen = talloc_get_size(s);
2571 2 : if (likely(slen > 0)) {
2572 2 : slen--;
2573 : }
2574 :
2575 2 : return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
2576 : }
2577 :
2578 : #ifndef HAVE_VA_COPY
2579 : #ifdef HAVE___VA_COPY
2580 : #define va_copy(dest, src) __va_copy(dest, src)
2581 : #else
2582 : #define va_copy(dest, src) (dest) = (src)
2583 : #endif
2584 : #endif
2585 :
2586 : static struct talloc_chunk *_vasprintf_tc(const void *t,
2587 : const char *fmt,
2588 : va_list ap) PRINTF_ATTRIBUTE(2,0);
2589 :
2590 660153113 : static struct talloc_chunk *_vasprintf_tc(const void *t,
2591 : const char *fmt,
2592 : va_list ap)
2593 : {
2594 21490465 : int vlen;
2595 21490465 : size_t len;
2596 21490465 : char *ret;
2597 21490465 : va_list ap2;
2598 660153113 : struct talloc_chunk *tc = NULL;
2599 21490465 : char buf[1024];
2600 :
2601 660153113 : va_copy(ap2, ap);
2602 660153113 : vlen = vsnprintf(buf, sizeof(buf), fmt, ap2);
2603 660153113 : va_end(ap2);
2604 660153113 : if (unlikely(vlen < 0)) {
2605 0 : return NULL;
2606 : }
2607 660153113 : len = vlen;
2608 660153113 : if (unlikely(len + 1 < len)) {
2609 0 : return NULL;
2610 : }
2611 :
2612 660153113 : ret = (char *)__talloc(t, len+1, &tc);
2613 660153113 : if (unlikely(!ret)) return NULL;
2614 :
2615 660153113 : if (len < sizeof(buf)) {
2616 660152849 : memcpy(ret, buf, len+1);
2617 : } else {
2618 264 : va_copy(ap2, ap);
2619 264 : vsnprintf(ret, len+1, fmt, ap2);
2620 264 : va_end(ap2);
2621 : }
2622 :
2623 660153113 : _tc_set_name_const(tc, ret);
2624 660153113 : return tc;
2625 : }
2626 :
2627 631962182 : _PUBLIC_ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
2628 : {
2629 631962182 : struct talloc_chunk *tc = _vasprintf_tc(t, fmt, ap);
2630 631962182 : if (tc == NULL) {
2631 0 : return NULL;
2632 : }
2633 631962182 : return TC_PTR_FROM_CHUNK(tc);
2634 : }
2635 :
2636 :
2637 : /*
2638 : Perform string formatting, and return a pointer to newly allocated
2639 : memory holding the result, inside a memory pool.
2640 : */
2641 381936665 : _PUBLIC_ char *talloc_asprintf(const void *t, const char *fmt, ...)
2642 : {
2643 8226452 : va_list ap;
2644 8226452 : char *ret;
2645 :
2646 381936665 : va_start(ap, fmt);
2647 381936665 : ret = talloc_vasprintf(t, fmt, ap);
2648 381936665 : va_end(ap);
2649 381936665 : return ret;
2650 : }
2651 :
2652 : static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2653 : const char *fmt, va_list ap)
2654 : PRINTF_ATTRIBUTE(3,0);
2655 :
2656 273383186 : static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
2657 : const char *fmt, va_list ap)
2658 : {
2659 6762585 : ssize_t alen;
2660 6762585 : va_list ap2;
2661 6762585 : char c;
2662 :
2663 273383186 : va_copy(ap2, ap);
2664 : /* this call looks strange, but it makes it work on older solaris boxes */
2665 273383186 : alen = vsnprintf(&c, 1, fmt, ap2);
2666 273383186 : va_end(ap2);
2667 :
2668 273383186 : if (alen <= 0) {
2669 : /* Either the vsnprintf failed or the format resulted in
2670 : * no characters being formatted. In the former case, we
2671 : * ought to return NULL, in the latter we ought to return
2672 : * the original string. Most current callers of this
2673 : * function expect it to never return NULL.
2674 : */
2675 0 : return s;
2676 : }
2677 :
2678 273383186 : s = talloc_realloc(NULL, s, char, slen + alen + 1);
2679 273383186 : if (!s) return NULL;
2680 :
2681 273383186 : vsnprintf(s + slen, alen + 1, fmt, ap);
2682 :
2683 273383186 : _tc_set_name_const(talloc_chunk_from_ptr(s), s);
2684 273383186 : return s;
2685 : }
2686 :
2687 : /**
2688 : * Realloc @p s to append the formatted result of @p fmt and @p ap,
2689 : * and return @p s, which may have moved. Good for gradually
2690 : * accumulating output into a string buffer. Appends at the end
2691 : * of the string.
2692 : **/
2693 6175373 : _PUBLIC_ char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
2694 : {
2695 6175373 : if (unlikely(!s)) {
2696 8 : return talloc_vasprintf(NULL, fmt, ap);
2697 : }
2698 :
2699 6175365 : return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
2700 : }
2701 :
2702 : /**
2703 : * Realloc @p s to append the formatted result of @p fmt and @p ap,
2704 : * and return @p s, which may have moved. Always appends at the
2705 : * end of the talloc'ed buffer, not the end of the string.
2706 : **/
2707 267208499 : _PUBLIC_ char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
2708 : {
2709 6694471 : size_t slen;
2710 :
2711 267208499 : if (unlikely(!s)) {
2712 678 : return talloc_vasprintf(NULL, fmt, ap);
2713 : }
2714 :
2715 267207821 : slen = talloc_get_size(s);
2716 267207821 : if (likely(slen > 0)) {
2717 267207821 : slen--;
2718 : }
2719 :
2720 267207821 : return __talloc_vaslenprintf_append(s, slen, fmt, ap);
2721 : }
2722 :
2723 : /*
2724 : Realloc @p s to append the formatted result of @p fmt and return @p
2725 : s, which may have moved. Good for gradually accumulating output
2726 : into a string buffer.
2727 : */
2728 1593270 : _PUBLIC_ char *talloc_asprintf_append(char *s, const char *fmt, ...)
2729 : {
2730 26949 : va_list ap;
2731 :
2732 1593270 : va_start(ap, fmt);
2733 1593270 : s = talloc_vasprintf_append(s, fmt, ap);
2734 1593270 : va_end(ap);
2735 1593270 : return s;
2736 : }
2737 :
2738 : /*
2739 : Realloc @p s to append the formatted result of @p fmt and return @p
2740 : s, which may have moved. Good for gradually accumulating output
2741 : into a buffer.
2742 : */
2743 140425776 : _PUBLIC_ char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
2744 : {
2745 4363550 : va_list ap;
2746 :
2747 140425776 : va_start(ap, fmt);
2748 140425776 : s = talloc_vasprintf_append_buffer(s, fmt, ap);
2749 140425776 : va_end(ap);
2750 140425776 : return s;
2751 : }
2752 :
2753 88346158 : _PUBLIC_ void talloc_asprintf_addbuf(char **ps, const char *fmt, ...)
2754 : {
2755 797415 : va_list ap;
2756 88346158 : char *s = *ps;
2757 88346158 : char *t = NULL;
2758 :
2759 88346158 : if (s == NULL) {
2760 0 : return;
2761 : }
2762 :
2763 88346158 : va_start(ap, fmt);
2764 88346158 : t = talloc_vasprintf_append_buffer(s, fmt, ap);
2765 88346158 : va_end(ap);
2766 :
2767 88346158 : if (t == NULL) {
2768 : /* signal failure to the next caller */
2769 0 : TALLOC_FREE(s);
2770 0 : *ps = NULL;
2771 : } else {
2772 88346158 : *ps = t;
2773 : }
2774 : }
2775 :
2776 : /*
2777 : alloc an array, checking for integer overflow in the array size
2778 : */
2779 7948383127 : _PUBLIC_ void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2780 : {
2781 7948383127 : if (count >= MAX_TALLOC_SIZE/el_size) {
2782 3 : return NULL;
2783 : }
2784 8160637190 : return _talloc_named_const(ctx, el_size * count, name);
2785 : }
2786 :
2787 : /*
2788 : alloc an zero array, checking for integer overflow in the array size
2789 : */
2790 1014175187 : _PUBLIC_ void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
2791 : {
2792 1014175187 : if (count >= MAX_TALLOC_SIZE/el_size) {
2793 0 : return NULL;
2794 : }
2795 1014175187 : return _talloc_zero(ctx, el_size * count, name);
2796 : }
2797 :
2798 : /*
2799 : realloc an array, checking for integer overflow in the array size
2800 : */
2801 5441208571 : _PUBLIC_ void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
2802 : {
2803 5441208571 : if (count >= MAX_TALLOC_SIZE/el_size) {
2804 1 : return NULL;
2805 : }
2806 5441208569 : return _talloc_realloc(ctx, ptr, el_size * count, name);
2807 : }
2808 :
2809 : /*
2810 : a function version of talloc_realloc(), so it can be passed as a function pointer
2811 : to libraries that want a realloc function (a realloc function encapsulates
2812 : all the basic capabilities of an allocation library, which is why this is useful)
2813 : */
2814 6 : _PUBLIC_ void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
2815 : {
2816 6 : return _talloc_realloc(context, ptr, size, NULL);
2817 : }
2818 :
2819 :
2820 2 : static int talloc_autofree_destructor(void *ptr)
2821 : {
2822 2 : autofree_context = NULL;
2823 2 : return 0;
2824 : }
2825 :
2826 : /*
2827 : return a context which will be auto-freed on exit
2828 : this is useful for reducing the noise in leak reports
2829 : */
2830 2 : _PUBLIC_ void *talloc_autofree_context(void)
2831 : {
2832 2 : if (autofree_context == NULL) {
2833 2 : autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
2834 2 : talloc_set_destructor(autofree_context, talloc_autofree_destructor);
2835 2 : talloc_setup_atexit();
2836 : }
2837 2 : return autofree_context;
2838 : }
2839 :
2840 1950297095 : _PUBLIC_ size_t talloc_get_size(const void *context)
2841 : {
2842 46480795 : struct talloc_chunk *tc;
2843 :
2844 1950297095 : if (context == NULL) {
2845 13415561 : return 0;
2846 : }
2847 :
2848 1936712673 : tc = talloc_chunk_from_ptr(context);
2849 :
2850 1936712673 : return tc->size;
2851 : }
2852 :
2853 : /*
2854 : find a parent of this context that has the given name, if any
2855 : */
2856 0 : _PUBLIC_ void *talloc_find_parent_byname(const void *context, const char *name)
2857 : {
2858 0 : struct talloc_chunk *tc;
2859 :
2860 0 : if (context == NULL) {
2861 0 : return NULL;
2862 : }
2863 :
2864 0 : tc = talloc_chunk_from_ptr(context);
2865 0 : while (tc) {
2866 0 : if (tc->name && strcmp(tc->name, name) == 0) {
2867 0 : return TC_PTR_FROM_CHUNK(tc);
2868 : }
2869 0 : while (tc && tc->prev) tc = tc->prev;
2870 0 : if (tc) {
2871 0 : tc = tc->parent;
2872 : }
2873 : }
2874 0 : return NULL;
2875 : }
2876 :
2877 : /*
2878 : show the parentage of a context
2879 : */
2880 0 : _PUBLIC_ void talloc_show_parents(const void *context, FILE *file)
2881 : {
2882 0 : struct talloc_chunk *tc;
2883 :
2884 0 : if (context == NULL) {
2885 0 : fprintf(file, "talloc no parents for NULL\n");
2886 0 : return;
2887 : }
2888 :
2889 0 : tc = talloc_chunk_from_ptr(context);
2890 0 : fprintf(file, "talloc parents of '%s'\n", __talloc_get_name(context));
2891 0 : while (tc) {
2892 0 : fprintf(file, "\t'%s'\n", __talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
2893 0 : while (tc && tc->prev) tc = tc->prev;
2894 0 : if (tc) {
2895 0 : tc = tc->parent;
2896 : }
2897 : }
2898 0 : fflush(file);
2899 : }
2900 :
2901 : /*
2902 : return 1 if ptr is a parent of context
2903 : */
2904 25238546 : static int _talloc_is_parent(const void *context, const void *ptr, int depth)
2905 : {
2906 2805157 : struct talloc_chunk *tc;
2907 :
2908 25238546 : if (context == NULL) {
2909 0 : return 0;
2910 : }
2911 :
2912 25238546 : tc = talloc_chunk_from_ptr(context);
2913 111913936 : while (tc) {
2914 83333853 : if (depth <= 0) {
2915 6 : return 0;
2916 : }
2917 83333847 : if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
2918 323046685 : while (tc && tc->prev) tc = tc->prev;
2919 83277602 : if (tc) {
2920 83277602 : tc = tc->parent;
2921 83277602 : depth--;
2922 : }
2923 : }
2924 22379414 : return 0;
2925 : }
2926 :
2927 : /*
2928 : return 1 if ptr is a parent of context
2929 : */
2930 25238546 : _PUBLIC_ int talloc_is_parent(const void *context, const void *ptr)
2931 : {
2932 25238546 : return _talloc_is_parent(context, ptr, TALLOC_MAX_DEPTH);
2933 : }
2934 :
2935 : /*
2936 : return the total size of memory used by this context and all children
2937 : */
2938 18 : static inline size_t _talloc_total_limit_size(const void *ptr,
2939 : struct talloc_memlimit *old_limit,
2940 : struct talloc_memlimit *new_limit)
2941 : {
2942 13 : return _talloc_total_mem_internal(ptr, TOTAL_MEM_LIMIT,
2943 : old_limit, new_limit);
2944 : }
2945 :
2946 37507811542 : static inline bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size)
2947 : {
2948 1 : struct talloc_memlimit *l;
2949 :
2950 38939051970 : for (l = limit; l != NULL; l = l->upper) {
2951 46 : if (l->max_size != 0 &&
2952 46 : ((l->max_size <= l->cur_size) ||
2953 36 : (l->max_size - l->cur_size < size))) {
2954 10 : return false;
2955 : }
2956 : }
2957 :
2958 37507811531 : return true;
2959 : }
2960 :
2961 : /*
2962 : Update memory limits when freeing a talloc_chunk.
2963 : */
2964 38258723328 : static void tc_memlimit_update_on_free(struct talloc_chunk *tc)
2965 : {
2966 1408464363 : size_t limit_shrink_size;
2967 :
2968 38258723328 : if (!tc->limit) {
2969 36850258955 : return;
2970 : }
2971 :
2972 : /*
2973 : * Pool entries don't count. Only the pools
2974 : * themselves are counted as part of the memory
2975 : * limits. Note that this also takes care of
2976 : * nested pools which have both flags
2977 : * TALLOC_FLAG_POOLMEM|TALLOC_FLAG_POOL set.
2978 : */
2979 20 : if (tc->flags & TALLOC_FLAG_POOLMEM) {
2980 0 : return;
2981 : }
2982 :
2983 : /*
2984 : * If we are part of a memory limited context hierarchy
2985 : * we need to subtract the memory used from the counters
2986 : */
2987 :
2988 20 : limit_shrink_size = tc->size+TC_HDR_SIZE;
2989 :
2990 : /*
2991 : * If we're deallocating a pool, take into
2992 : * account the prefix size added for the pool.
2993 : */
2994 :
2995 20 : if (tc->flags & TALLOC_FLAG_POOL) {
2996 2 : limit_shrink_size += TP_HDR_SIZE;
2997 : }
2998 :
2999 30 : talloc_memlimit_shrink(tc->limit, limit_shrink_size);
3000 :
3001 20 : if (tc->limit->parent == tc) {
3002 10 : free(tc->limit);
3003 : }
3004 :
3005 20 : tc->limit = NULL;
3006 : }
3007 :
3008 : /*
3009 : Increase memory limit accounting after a malloc/realloc.
3010 : */
3011 41610686829 : static void talloc_memlimit_grow(struct talloc_memlimit *limit,
3012 : size_t size)
3013 : {
3014 1497768848 : struct talloc_memlimit *l;
3015 :
3016 41610686861 : for (l = limit; l != NULL; l = l->upper) {
3017 30 : size_t new_cur_size = l->cur_size + size;
3018 30 : if (new_cur_size < l->cur_size) {
3019 0 : talloc_abort("logic error in talloc_memlimit_grow\n");
3020 0 : return;
3021 : }
3022 30 : l->cur_size = new_cur_size;
3023 : }
3024 : }
3025 :
3026 : /*
3027 : Decrease memory limit accounting after a free/realloc.
3028 : */
3029 5644403 : static void talloc_memlimit_shrink(struct talloc_memlimit *limit,
3030 : size_t size)
3031 : {
3032 130553 : struct talloc_memlimit *l;
3033 :
3034 5775000 : for (l = limit; l != NULL; l = l->upper) {
3035 44 : if (l->cur_size < size) {
3036 0 : talloc_abort("logic error in talloc_memlimit_shrink\n");
3037 0 : return;
3038 : }
3039 44 : l->cur_size = l->cur_size - size;
3040 : }
3041 : }
3042 :
3043 18 : _PUBLIC_ int talloc_set_memlimit(const void *ctx, size_t max_size)
3044 : {
3045 18 : struct talloc_chunk *tc = talloc_chunk_from_ptr(ctx);
3046 9 : struct talloc_memlimit *orig_limit;
3047 18 : struct talloc_memlimit *limit = NULL;
3048 :
3049 18 : if (tc->limit && tc->limit->parent == tc) {
3050 8 : tc->limit->max_size = max_size;
3051 8 : return 0;
3052 : }
3053 10 : orig_limit = tc->limit;
3054 :
3055 10 : limit = malloc(sizeof(struct talloc_memlimit));
3056 10 : if (limit == NULL) {
3057 0 : return 1;
3058 : }
3059 10 : limit->parent = tc;
3060 10 : limit->max_size = max_size;
3061 10 : limit->cur_size = _talloc_total_limit_size(ctx, tc->limit, limit);
3062 :
3063 10 : if (orig_limit) {
3064 6 : limit->upper = orig_limit;
3065 : } else {
3066 4 : limit->upper = NULL;
3067 : }
3068 :
3069 5 : return 0;
3070 : }
|