Skip to content

Commit 48d6237

Browse files
committed
PR Feedback: use __builtin_debugtrap and safe function size calculation
1 parent 7b10cc8 commit 48d6237

File tree

1 file changed

+12
-14
lines changed
  • tools/swift-inspect/Sources/AndroidCLib

1 file changed

+12
-14
lines changed

tools/swift-inspect/Sources/AndroidCLib/heap.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,43 +44,41 @@
4444
* ------------
4545
*/
4646

47+
#if !__has_builtin(__builtin_debugtrap)
48+
#error("compiler support for __builtin_debugtrap is required")
49+
#endif
50+
4751
#define MAX_VALID_IDX 0
4852
#define NEXT_FREE_IDX 1
4953
#define HEADER_SIZE 2
5054
#define ENTRY_SIZE 2
5155

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-
6056
// Callback for malloc_iterate. Because this function is meant to be copied to
6157
// a different process for execution, it must not make any function calls. It
6258
// could be written as asm, but simple C is more readable/maintainable and
6359
// 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) {
6561
volatile uint64_t *data = (uint64_t*)arg;
6662
while (data[NEXT_FREE_IDX] >= data[MAX_VALID_IDX]) {
6763
// SIGTRAP indicates the buffer is full and needs to be drained before more
6864
// entries can be written.
69-
DEBUG_BREAK();
65+
__builtin_debugtrap();
66+
asm volatile("nop");
7067
}
7168
data[data[NEXT_FREE_IDX]++] = base;
7269
data[data[NEXT_FREE_IDX]++] = size;
70+
asm volatile(".global heap_iterate_callback_end");
71+
asm volatile("heap_iterate_callback_end:");
7372
}
7473

75-
// Placeholer function to mark the end of the remote callback code.
76-
static void heap_iterate_callback_end() {}
77-
7874
void* heap_iterate_callback_start() {
7975
return (void*)heap_iterate_callback;
8076
}
8177

8278
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);
8482
}
8583

8684
bool heap_iterate_metadata_init(void* data, size_t len) {

0 commit comments

Comments
 (0)