|
44 | 44 | * ------------
|
45 | 45 | */
|
46 | 46 |
|
| 47 | +#if !__has_builtin(__builtin_debugtrap) |
| 48 | +#error("compiler support for __builtin_debugtrap is required") |
| 49 | +#endif |
| 50 | + |
47 | 51 | #define MAX_VALID_IDX 0
|
48 | 52 | #define NEXT_FREE_IDX 1
|
49 | 53 | #define HEADER_SIZE 2
|
50 | 54 | #define ENTRY_SIZE 2
|
51 | 55 |
|
52 |
| -#if defined(__aarch64__) || defined(__ARM64__) || defined(_M_ARM64) |
53 |
| -#define DEBUG_BREAK() asm("brk #0x0") |
54 |
| -#elif defined(_M_X64) || defined(__amd64__) || defined(__x86_64__) || defined(_M_AMD64) |
55 |
| -#define DEBUG_BREAK() asm("int3; nop") |
56 |
| -#else |
57 |
| -#error("only aarch64 and x86_64 are supported") |
58 |
| -#endif |
59 |
| - |
60 | 56 | // Callback for malloc_iterate. Because this function is meant to be copied to
|
61 | 57 | // a different process for execution, it must not make any function calls. It
|
62 | 58 | // could be written as asm, but simple C is more readable/maintainable and
|
63 | 59 | // should consistently compile to movable, position-independent code.
|
64 |
| -static void heap_iterate_callback(unsigned long base, unsigned long size, void *arg) { |
| 60 | +void heap_iterate_callback(unsigned long base, unsigned long size, void *arg) { |
65 | 61 | volatile uint64_t *data = (uint64_t*)arg;
|
66 | 62 | while (data[NEXT_FREE_IDX] >= data[MAX_VALID_IDX]) {
|
67 | 63 | // SIGTRAP indicates the buffer is full and needs to be drained before more
|
68 | 64 | // entries can be written.
|
69 |
| - DEBUG_BREAK(); |
| 65 | + __builtin_debugtrap(); |
| 66 | + asm volatile("nop"); |
70 | 67 | }
|
71 | 68 | data[data[NEXT_FREE_IDX]++] = base;
|
72 | 69 | data[data[NEXT_FREE_IDX]++] = size;
|
| 70 | + asm volatile(".global heap_iterate_callback_end"); |
| 71 | + asm volatile("heap_iterate_callback_end:"); |
73 | 72 | }
|
74 | 73 |
|
75 |
| -// Placeholer function to mark the end of the remote callback code. |
76 |
| -static void heap_iterate_callback_end() {} |
77 |
| - |
78 | 74 | void* heap_iterate_callback_start() {
|
79 | 75 | return (void*)heap_iterate_callback;
|
80 | 76 | }
|
81 | 77 |
|
82 | 78 | size_t heap_iterate_callback_len() {
|
83 |
| - return (size_t)(heap_iterate_callback_end - heap_iterate_callback); |
| 79 | + extern char heap_iterate_callback_end; |
| 80 | + return (uintptr_t)&heap_iterate_callback_end - (uintptr_t)heap_iterate_callback |
| 81 | + + sizeof(uintptr_t); |
84 | 82 | }
|
85 | 83 |
|
86 | 84 | bool heap_iterate_metadata_init(void* data, size_t len) {
|
|
0 commit comments