|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include <string.h> |
| 14 | + |
| 15 | +#include "heap.h" |
| 16 | + |
| 17 | +/* The heap metadata buffer is interpreted as an array of 8-byte pairs. The |
| 18 | + * first pair contains metadata describing the buffer itself: max valid index |
| 19 | + * (e.g. size of the buffer) and next index (e.g. write cursor/position). Each |
| 20 | + * subsequent pair describes the address and length of a heap entry in the |
| 21 | + * remote process. A 4KiB page provides sufficient space for the header and |
| 22 | + * 255 (address, length) pairs. |
| 23 | + * |
| 24 | + * ------------ |
| 25 | + * | uint64_t | max valid index (e.g. sizeof(buffer) / sizeof(uint64_t)) |
| 26 | + * ------------ |
| 27 | + * | uint64_t | next free index (starts at 2) |
| 28 | + * ------------ |
| 29 | + * | uint64_t | heap item 1 address |
| 30 | + * ------------ |
| 31 | + * | uint64_t | heap item 1 size |
| 32 | + * ------------ |
| 33 | + * | uint64_t | heap item 2 address |
| 34 | + * ------------ |
| 35 | + * | uint64_t | heap item 2 size |
| 36 | + * ------------ |
| 37 | + * | uint64_t | ... |
| 38 | + * ------------ |
| 39 | + * | uint64_t | ... |
| 40 | + * ------------ |
| 41 | + * | uint64_t | heap item N address |
| 42 | + * ------------ |
| 43 | + * | uint64_t | heap item N size |
| 44 | + * ------------ |
| 45 | + */ |
| 46 | + |
| 47 | +#if !__has_builtin(__builtin_debugtrap) |
| 48 | +#error("compiler support for __builtin_debugtrap is required") |
| 49 | +#endif |
| 50 | + |
| 51 | +#define MAX_VALID_IDX 0 |
| 52 | +#define NEXT_FREE_IDX 1 |
| 53 | +#define HEADER_SIZE 2 |
| 54 | +#define ENTRY_SIZE 2 |
| 55 | + |
| 56 | +// Callback for malloc_iterate. Because this function is meant to be copied to |
| 57 | +// a different process for execution, it must not make any function calls to |
| 58 | +// ensure compiles to simple, position-independent code. It is implemented in C |
| 59 | +// for readability/maintainability. It is placed in its own code section to |
| 60 | +// simplify calculating its size. |
| 61 | +__attribute__((noinline, used, section("heap_iterator"))) |
| 62 | +static void heap_iterate_callback(unsigned long base, unsigned long size, void *arg) { |
| 63 | + volatile uint64_t *data = (uint64_t*)arg; |
| 64 | + while (data[NEXT_FREE_IDX] >= data[MAX_VALID_IDX]) { |
| 65 | + // SIGTRAP indicates the buffer is full and needs to be drained before more |
| 66 | + // entries can be written. |
| 67 | + __builtin_debugtrap(); |
| 68 | + |
| 69 | + // After the SIGTRAP, the signal handler advances the instruction pointer |
| 70 | + // (PC) to the next instruction. Inserting a nop instruction here ensures |
| 71 | + // the CPU has a clear, executable instruction to process, which avoids |
| 72 | + // potential speculative execution or pipeline issues that could arise if |
| 73 | + // the next instruction were a control transfer like a branch or jump. |
| 74 | + __asm__ __volatile__("nop"); |
| 75 | + } |
| 76 | + data[data[NEXT_FREE_IDX]++] = base; |
| 77 | + data[data[NEXT_FREE_IDX]++] = size; |
| 78 | +} |
| 79 | + |
| 80 | +// The linker implicitly defines __start- and __stop- prefixed symbols that mark |
| 81 | +// the start and end of user defined sections. |
| 82 | +extern char __stop_heap_iterator[]; |
| 83 | + |
| 84 | +void* heap_iterate_callback_start() { |
| 85 | + return (void*)heap_iterate_callback; |
| 86 | +} |
| 87 | + |
| 88 | +size_t heap_iterate_callback_len() { |
| 89 | + return (uintptr_t)__stop_heap_iterator - (uintptr_t)heap_iterate_callback; |
| 90 | +} |
| 91 | + |
| 92 | +bool heap_iterate_metadata_init(void* data, size_t len) { |
| 93 | + uint64_t *metadata = data; |
| 94 | + const uint64_t max_entries = len / sizeof(uint64_t); |
| 95 | + if (max_entries < HEADER_SIZE + ENTRY_SIZE) |
| 96 | + return false; |
| 97 | + |
| 98 | + memset(data, 0, len); |
| 99 | + metadata[MAX_VALID_IDX] = max_entries; |
| 100 | + metadata[NEXT_FREE_IDX] = HEADER_SIZE; |
| 101 | + return true; |
| 102 | +} |
| 103 | + |
| 104 | +bool heap_iterate_metadata_process( |
| 105 | + void* data, size_t len, void* callback_context, heap_iterate_entry_callback_t callback) { |
| 106 | + uint64_t *metadata = data; |
| 107 | + const uint64_t max_entries = len / sizeof(uint64_t); |
| 108 | + const uint64_t end_index = metadata[NEXT_FREE_IDX]; |
| 109 | + |
| 110 | + if (metadata[MAX_VALID_IDX] != max_entries || end_index > max_entries) |
| 111 | + return false; |
| 112 | + |
| 113 | + for (size_t i = HEADER_SIZE; i < end_index; i += ENTRY_SIZE) { |
| 114 | + const uint64_t base = metadata[i]; |
| 115 | + const uint64_t size = metadata[i + 1]; |
| 116 | + callback(callback_context, base, size); |
| 117 | + } |
| 118 | + |
| 119 | + return true; |
| 120 | +} |
0 commit comments