Debian Codegraph @

vendor/Heap-Layers/wrappers/macwrapper.cpp

raw
1 // -*- C++ -*-
2
3 /**
4 * @file macwrapper.cpp
5 * @brief Replaces malloc family on Macs with custom versions.
6 * @author Emery Berger <http://www.emeryberger.com>
7 * @note Copyright (C) 2010-2018 by Emery Berger, University of Massachusetts Amherst.
8 */
9
10 #ifndef __APPLE__
11 #error "This file is for use on Mac OS only."
12 #endif
13
14 #include <AvailabilityMacros.h>
15
16 #include <cstdlib>
17 using namespace std;
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <malloc/malloc.h>
23 #include <errno.h>
24
25 #include <unistd.h>
26
27 /*
28 To use this library,
29 you only need to define the following allocation functions:
30
31 - xxmalloc
32 - xxfree
33 - xxmemalign
34 - xxmalloc_usable_size
35 - xxmalloc_lock
36 - xxmalloc_unlock
37
38 See the extern "C" block below for function prototypes and more
39 details. YOU SHOULD NOT NEED TO MODIFY ANY OF THE CODE HERE TO
40 SUPPORT ANY ALLOCATOR.
41
42
43 LIMITATIONS:
44
45 - This wrapper also assumes that there is some way to lock all the
46 heaps used by a given allocator; however, such support is only
47 required by programs that also call fork(). In case your program
48 does not, the lock and unlock calls given below can be no-ops.
49
50 */
51
52
53 #include <assert.h>
54
55 extern "C" {
56
57 void * xxmalloc (size_t);
58 void xxfree (void *);
59 void * xxmemalign(size_t, size_t);
60
61 /// Pending widespread support for sized deallocation.
62 /// void xxfree_sized (void *, size_t);
63
64 // Takes a pointer and returns how much space it holds.
65 size_t xxmalloc_usable_size (void *);
66
67 // Locks the heap(s), used prior to any invocation of fork().
68 void xxmalloc_lock ();
69
70 // Unlocks the heap(s), after fork().
71 void xxmalloc_unlock ();
72
73 }
74
75 #include "macinterpose.h"
76
77 //////////
78 //////////
79
80 // All replacement functions get the prefix "replace_".
81
82 extern "C" {
83
84 void * replace_malloc (size_t sz) {
85 void * ptr = xxmalloc(sz);
86 return ptr;
87 }
88
89 #if 0 // Disabled pending wider support for sized deallocation.
90 void replace_free_sized (void * ptr, size_t sz) {
91 xxfree_sized (ptr, sz);
92 }
93 #endif
94
95 size_t replace_malloc_usable_size (void * ptr) {
96 if (ptr == nullptr) {
97 return 0;
98 }
99 auto objSize = xxmalloc_usable_size (ptr);
100 return objSize;
101 }
102
103 void replace_free (void * ptr) {
104 xxfree (ptr);
105 }
106
107 size_t replace_malloc_good_size (size_t sz) {
108 auto * ptr = xxmalloc(sz);
109 auto objSize = xxmalloc_usable_size(ptr);
110 xxfree(ptr);
111 return objSize;
112 }
113
114 static void * _extended_realloc (void * ptr, size_t sz, bool isReallocf)
115 {
116 // NULL ptr = malloc.
117 if (ptr == NULL) {
118 return xxmalloc(sz);
119 }
120
121 // 0 size = free. We return a small object. This behavior is
122 // apparently required under Mac OS X and optional under POSIX.
123 if (sz == 0) {
124 xxfree(ptr);
125 return xxmalloc(1);
126 }
127
128 auto objSize = xxmalloc_usable_size(ptr);
129
130 // Custom logic here to ensure we only do a logarithmic number of
131 // reallocations (with a constant space overhead).
132
133 // Don't change size if the object is shrinking by less than half.
134 if ((objSize / 2 < sz) && (sz <= objSize)) {
135 // Do nothing.
136 return ptr;
137 }
138 #if 0
139 // If the object is growing by less than 2X, double it.
140 if ((objSize < sz) && (sz < objSize * 2)) {
141 sz = objSize * 2;
142 }
143 #endif
144
145 auto * buf = xxmalloc((size_t) sz);
146
147 if (buf != NULL) {
148 // Successful malloc.
149 // Copy the contents of the original object
150 // up to the size of the new block.
151 auto minSize = (objSize < sz) ? objSize : sz;
152 memcpy (buf, ptr, minSize);
153 xxfree(ptr);
154 } else {
155 if (isReallocf) {
156 // Free the old block if the new allocation failed.
157 // Specific behavior for Mac OS X reallocf().
158 xxfree(ptr);
159 }
160 }
161
162 // Return a pointer to the new one.
163 return buf;
164 }
165
166 void * replace_realloc (void * ptr, size_t sz) {
167 return _extended_realloc (ptr, sz, false);
168 }
169
170 void * replace_reallocf (void * ptr, size_t sz) {
171 return _extended_realloc (ptr, sz, true);
172 }
173
174 void * replace_calloc (size_t elsize, size_t nelems) {
175 auto n = nelems * elsize;
176 if (elsize && (nelems != n / elsize)) {
177 return nullptr;
178 }
179 auto * ptr = xxmalloc(n);
180 if (ptr) {
181 memset (ptr, 0, n);
182 }
183 return ptr;
184 }
185
186 char * replace_strdup (const char * s)
187 {
188 char * newString = NULL;
189 if (s != NULL) {
190 auto len = strlen(s) + 1UL;
191 if ((newString = (char *) replace_malloc(len))) {
192 memcpy (newString, s, len);
193 }
194 }
195 return newString;
196 }
197
198 void * replace_memalign (size_t alignment, size_t size)
199 {
200 return xxmemalign(alignment, size);
201 #if 0
202 // Check for non power-of-two alignment, or mistake in size.
203 if (alignment < alignof(max_align_t)) {
204 alignment = alignof(max_align_t);
205 }
206 // Round up to next power of two.
207 if (alignment & (alignment - 1)) {
208 size_t a = alignof(max_align_t);
209 while (a < alignment) {
210 a <<= 1;
211 }
212 alignment = a;
213 }
214 // Try to just allocate an object of the requested size.
215 // If it happens to be aligned properly, just return it.
216 auto * ptr = replace_malloc(size);
217 if (((size_t) ptr & ~(alignment - 1)) == (size_t) ptr) {
218 // It is already aligned just fine; return it.
219 return ptr;
220 }
221 // It was not aligned as requested: free the object.
222 replace_free(ptr);
223
224 // Force size to be a multiple of alignment.
225 if (alignment < size) {
226 size = size + alignment - (size % alignment);
227 } else {
228 size = alignment;
229 }
230
231 ptr = replace_malloc(size);
232 // If the underlying malloc has "natural" alignment, this will work.
233 if (((size_t) ptr & ~(alignment - 1)) == (size_t) ptr) {
234 // It is already aligned just fine; return it.
235 return ptr;
236 }
237 // It was not aligned as requested: free the object.
238 replace_free(ptr);
239
240 // Now get a big chunk of memory and align the object within it.
241 // NOTE: this assumes that the underlying allocator will be able
242 // to free the aligned object, or ignore the free request.
243 auto * buf = replace_malloc(2 * alignment + size);
244 auto * alignedPtr = (void *) (((size_t) buf + alignment - 1) & ~(alignment - 1));
245 return alignedPtr;
246 #endif
247 }
248
249 #if 0
250 void * replace_aligned_alloc (size_t alignment, size_t size) {
251 // Per the man page: "The function aligned_alloc() is the same as
252 // memalign(), except for the added restriction that size should be
253 // a multiple of alignment." Rather than check and potentially fail,
254 // we just enforce this by rounding up the size, if necessary.
255 size = size + alignment - (size % alignment);
256 return replace_memalign (alignment, size);
257 }
258 #endif
259
260 int replace_posix_memalign(void **memptr, size_t alignment, size_t size)
261 {
262 // Check for non power-of-two alignment.
263 if ((alignment == 0) ||
264 (alignment & (alignment - 1)))
265 {
266 return EINVAL;
267 }
268 auto * ptr = replace_memalign (alignment, size);
269 if (!ptr) {
270 return ENOMEM;
271 } else {
272 *memptr = ptr;
273 return 0;
274 }
275 }
276
277 void * replace_valloc (size_t sz)
278 {
279 // Equivalent to memalign(pagesize, sz).
280 void * ptr = replace_memalign (PAGE_SIZE, sz);
281 return ptr;
282 }
283
284 void replace_vfree(void * ptr)
285 {
286 replace_free(ptr);
287 }
288
289 }
290
291
292 /////////
293 /////////
294
295 extern "C" {
296 // operator new
297 void * _Znwm (unsigned long);
298 void * _Znam (unsigned long);
299
300 // operator delete
301 void _ZdlPv (void *);
302 void _ZdaPv (void *);
303
304 // nothrow variants
305 // operator new nothrow
306 void * _ZnwmRKSt9nothrow_t ();
307 void * _ZnamRKSt9nothrow_t ();
308 // operator delete nothrow
309 void _ZdaPvRKSt9nothrow_t (void *);
310 void _ZdlPvRKSt9nothrow_t (void *);
311
312 void _malloc_fork_prepare ();
313 void _malloc_fork_parent ();
314 void _malloc_fork_child ();
315 }
316
317 static const char * theOneTrueZoneName = "DefaultMallocZone";
318
319 static bool initializeZone(malloc_zone_t& zone);
320
321 malloc_zone_t * getDefaultZone() {
322 static malloc_zone_t theDefaultZone;
323 static bool initialized = initializeZone(theDefaultZone);
324 (void) initialized;
325 return &theDefaultZone;
326 }
327
328 extern "C" {
329
330 malloc_zone_t * replace_malloc_create_zone(vm_size_t,
331 unsigned)
332 {
333 // auto zone = (malloc_zone_t *) replace_malloc(sizeof(malloc_zone_t));
334 return getDefaultZone();
335 // return nullptr; // zone;
336 }
337
338 malloc_zone_t * replace_malloc_default_zone () {
339 return getDefaultZone();
340 }
341
342 malloc_zone_t * replace_malloc_default_purgeable_zone() {
343 return getDefaultZone();
344 }
345
346 void replace_malloc_destroy_zone (malloc_zone_t *) {
347 // Do nothing.
348 }
349
350 kern_return_t replace_malloc_get_all_zones (task_t,
351 memory_reader_t,
352 vm_address_t **addresses,
353 unsigned *count) {
354 *addresses = 0;
355 *count = 0;
356 return KERN_SUCCESS;
357 }
358
359 const char * replace_malloc_get_zone_name(malloc_zone_t * z) {
360 return z->zone_name;
361 }
362
363 void replace_malloc_printf(const char *, ...) {
364 }
365
366 size_t replace_internal_malloc_zone_size (malloc_zone_t *, const void * ptr) {
367 return replace_malloc_usable_size((void *) ptr);
368 }
369
370 int replace_malloc_jumpstart(int) {
371 return 1;
372 }
373
374 void replace_malloc_set_zone_name(malloc_zone_t *, const char *) {
375 // do nothing.
376 }
377
378 unsigned replace_malloc_zone_batch_malloc(malloc_zone_t *,
379 size_t sz,
380 void ** results,
381 unsigned num_requested)
382 {
383 for (auto i = 0U; i < num_requested; i++) {
384 results[i] = replace_malloc(sz);
385 if (results[i] == nullptr) {
386 return i;
387 }
388 }
389 return num_requested;
390 }
391
392 void replace_malloc_zone_batch_free(malloc_zone_t *,
393 void ** to_be_freed,
394 unsigned num)
395 {
396 for (auto i = 0U; i < num; i++) {
397 replace_free(to_be_freed[i]);
398 }
399 }
400
401 void * replace_malloc_zone_calloc (malloc_zone_t *, size_t n, size_t size) {
402 return replace_calloc (n, size);
403 }
404
405 bool replace_malloc_zone_check(malloc_zone_t *) {
406 // Just return true for all zones.
407 return true;
408 }
409
410 void replace_malloc_zone_free (malloc_zone_t *, void * ptr) {
411 replace_free(ptr);
412 }
413
414 void replace_malloc_zone_free_definite_size (malloc_zone_t *, void * ptr, size_t) {
415 replace_free(ptr);
416 }
417
418 malloc_zone_t * replace_malloc_zone_from_ptr (const void *) {
419 return replace_malloc_default_zone();
420 }
421
422 void replace_malloc_zone_log(malloc_zone_t *, void *) {
423 // Do nothing.
424 }
425
426 void * replace_malloc_zone_malloc (malloc_zone_t *, size_t size) {
427 return replace_malloc (size);
428 }
429
430 void replace_malloc_zone_print(malloc_zone_t *, bool) {
431 // Do nothing.
432 }
433
434 void replace_malloc_zone_print_ptr_info(void *) {
435 }
436
437 void * replace_malloc_zone_realloc (malloc_zone_t *, void * ptr, size_t size) {
438 return replace_realloc (ptr, size);
439 }
440
441 void replace_malloc_zone_register (malloc_zone_t *) {
442 }
443
444 void * replace_malloc_zone_memalign (malloc_zone_t *, size_t alignment, size_t size) {
445 return replace_memalign (alignment, size);
446 }
447
448 void replace_malloc_zone_unregister (malloc_zone_t *) {
449 }
450
451 void * replace_malloc_zone_valloc (malloc_zone_t *, size_t size) {
452 return replace_valloc (size);
453 }
454
455 void replace__malloc_fork_child() {
456 /* Called in the child process after a fork() to resume normal operation. In the MTASK case we also have to change memory inheritance so that the child does not share memory with the parent. */
457 xxmalloc_unlock();
458 }
459
460 void replace__malloc_fork_parent() {
461 /* Called in the parent process after a fork() to resume normal operation. */
462 xxmalloc_unlock();
463 }
464
465 void replace__malloc_fork_prepare() {
466 /* Prepare the malloc module for a fork by insuring that no thread is in a malloc critical section */
467 xxmalloc_lock();
468 }
469
470 }
471
472 extern "C" void vfree (void *);
473 extern "C" int malloc_jumpstart (int);
474
475 // Now interpose everything.
476
477 #ifndef HL_REPLACE_MALLOC_OPS
478 #define HL_REPLACE_MALLOC_OPS 1
479 #endif
480
481 #define HL_REPLACE_ZONES 1
482
483
484 #if HL_REPLACE_MALLOC_OPS
485
486 MAC_INTERPOSE(replace__malloc_fork_child, _malloc_fork_child);
487 MAC_INTERPOSE(replace__malloc_fork_parent, _malloc_fork_parent);
488 MAC_INTERPOSE(replace__malloc_fork_prepare, _malloc_fork_prepare);
489 //MAC_INTERPOSE(replace_aligned_alloc, aligned_alloc);
490 MAC_INTERPOSE(replace_calloc, calloc);
491 MAC_INTERPOSE(xxfree, _ZdaPv);
492 MAC_INTERPOSE(xxfree, _ZdaPvRKSt9nothrow_t);
493 MAC_INTERPOSE(xxfree, _ZdlPv);
494 MAC_INTERPOSE(xxfree, _ZdlPvRKSt9nothrow_t);
495 MAC_INTERPOSE(xxfree, free);
496 MAC_INTERPOSE(xxfree, vfree);
497 MAC_INTERPOSE(xxmalloc, _Znam);
498 MAC_INTERPOSE(xxmalloc, _ZnamRKSt9nothrow_t);
499 MAC_INTERPOSE(xxmalloc, _Znwm);
500 MAC_INTERPOSE(xxmalloc, _ZnwmRKSt9nothrow_t);
501 MAC_INTERPOSE(xxmalloc, malloc);
502 #if HL_REPLACE_ZONES
503 MAC_INTERPOSE(replace_malloc_create_zone, malloc_create_zone);
504 MAC_INTERPOSE(replace_malloc_default_purgeable_zone, malloc_default_purgeable_zone);
505 MAC_INTERPOSE(replace_malloc_default_zone, malloc_default_zone);
506 MAC_INTERPOSE(replace_malloc_destroy_zone, malloc_destroy_zone);
507 MAC_INTERPOSE(replace_malloc_get_all_zones, malloc_get_all_zones);
508 MAC_INTERPOSE(replace_malloc_get_zone_name, malloc_get_zone_name);
509 #endif
510 MAC_INTERPOSE(replace_malloc_good_size, malloc_good_size);
511 #if HL_REPLACE_ZONES
512 MAC_INTERPOSE(replace_malloc_jumpstart, malloc_jumpstart);
513 #endif
514 MAC_INTERPOSE(replace_malloc_printf, malloc_printf);
515 MAC_INTERPOSE(replace_malloc_set_zone_name, malloc_set_zone_name);
516 MAC_INTERPOSE(xxmalloc_usable_size, malloc_size);
517 #if HL_REPLACE_ZONES
518 MAC_INTERPOSE(replace_malloc_zone_batch_free, malloc_zone_batch_free);
519 MAC_INTERPOSE(replace_malloc_zone_batch_malloc, malloc_zone_batch_malloc);
520 MAC_INTERPOSE(replace_malloc_zone_calloc, malloc_zone_calloc);
521 MAC_INTERPOSE(replace_malloc_zone_check, malloc_zone_check);
522 MAC_INTERPOSE(replace_malloc_zone_free, malloc_zone_free);
523 MAC_INTERPOSE(replace_malloc_zone_from_ptr, malloc_zone_from_ptr);
524 MAC_INTERPOSE(replace_malloc_zone_log, malloc_zone_log);
525 MAC_INTERPOSE(replace_malloc_zone_malloc, malloc_zone_malloc);
526 MAC_INTERPOSE(replace_malloc_zone_memalign, malloc_zone_memalign);
527 MAC_INTERPOSE(replace_malloc_zone_print, malloc_zone_print);
528 MAC_INTERPOSE(replace_malloc_zone_print_ptr_info, malloc_zone_print_ptr_info);
529 MAC_INTERPOSE(replace_malloc_zone_realloc, malloc_zone_realloc);
530 MAC_INTERPOSE(replace_malloc_zone_register, malloc_zone_register);
531 MAC_INTERPOSE(replace_malloc_zone_unregister, malloc_zone_unregister);
532 MAC_INTERPOSE(replace_malloc_zone_valloc, malloc_zone_valloc);
533 #endif
534 MAC_INTERPOSE(replace_posix_memalign, posix_memalign);
535 MAC_INTERPOSE(replace_realloc, realloc);
536 MAC_INTERPOSE(replace_reallocf, reallocf);
537 MAC_INTERPOSE(replace_strdup, strdup);
538 MAC_INTERPOSE(replace_valloc, valloc);
539
540 #endif
541
542
543 // Force initialization of the default zone.
544
545 #if HL_REPLACE_ZONES
546 static bool initializeZone(malloc_zone_t& zone) {
547 zone.size = replace_internal_malloc_zone_size;
548 zone.malloc = replace_malloc_zone_malloc;
549 zone.calloc = replace_malloc_zone_calloc;
550 zone.valloc = replace_malloc_zone_valloc;
551 zone.free = replace_malloc_zone_free;
552 zone.realloc = replace_malloc_zone_realloc;
553 zone.destroy = replace_malloc_destroy_zone;
554 zone.zone_name = theOneTrueZoneName;
555 zone.batch_malloc = replace_malloc_zone_batch_malloc;
556 zone.batch_free = replace_malloc_zone_batch_free;
557 zone.introspect = NULL;
558 zone.version = 8;
559 zone.memalign = replace_malloc_zone_memalign;
560 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 && !defined(__POWERPC__)
561 zone.free_definite_size = replace_malloc_zone_free_definite_size;
562 zone.pressure_relief = NULL;
563 #endif
564 return true;
565 }
566 #else
567 static bool initializeZone(malloc_zone_t& zone) { return true; }
568 #endif
569