Skip to content

Commit 177c9db

Browse files
committed
Keep platform inspection helpers local and simplify Mach-O decoding
1 parent 760914c commit 177c9db

5 files changed

Lines changed: 156 additions & 184 deletions

File tree

‎Makefile.pre.in‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,6 @@ PYTHON_HEADERS= \
12841284
$(srcdir)/Include/warnings.h \
12851285
$(srcdir)/Include/weakrefobject.h \
12861286
$(srcdir)/Python/remote_debug.h \
1287-
$(srcdir)/Python/remote_debug_macho.h \
12881287
\
12891288
pyconfig.h \
12901289
$(PARSER_HEADERS) \

‎Modules/_remote_debugging/_remote_debugging.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,6 @@ extern int find_running_frame(
661661
uintptr_t *frame
662662
);
663663

664-
extern int get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid);
665-
666664
extern PyObject* unwind_stack_for_thread(
667665
RemoteUnwinderObject *unwinder,
668666
uintptr_t *current_tstate,

‎Modules/_remote_debugging/threads.c‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,10 @@ find_running_frame(
158158
* THREAD STATUS FUNCTIONS
159159
* ============================================================================ */
160160

161-
int
162-
get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid) {
161+
static int
162+
get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) {
163163
#if defined(__APPLE__) && TARGET_OS_OSX
164164
struct proc_threadinfo thread;
165-
/* This flavor accepts the native ID from pthread_threadid_np(). */
166165
int size = proc_pidinfo(unwinder->handle.pid, PROC_PIDTHREADID64INFO,
167166
tid, &thread, sizeof(thread));
168167
if (size != sizeof(thread)) {
@@ -174,7 +173,7 @@ get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid) {
174173
char stat_path[256];
175174
char buffer[2048] = "";
176175

177-
snprintf(stat_path, sizeof(stat_path), "/proc/%d/task/%" PRIu64 "/stat", unwinder->handle.pid, tid);
176+
snprintf(stat_path, sizeof(stat_path), "/proc/%d/task/%lu/stat", unwinder->handle.pid, tid);
178177

179178
int fd = open(stat_path, O_RDONLY);
180179
if (fd == -1) {
@@ -210,15 +209,11 @@ get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid) {
210209
return THREAD_STATE_UNKNOWN;
211210
}
212211
#elif defined(MS_WINDOWS)
213-
if (tid > MAXDWORD) {
214-
return THREAD_STATE_UNKNOWN;
215-
}
216212
HANDLE thread = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)tid);
217213
if (thread == NULL) {
218214
return THREAD_STATE_UNKNOWN;
219215
}
220-
/* ThreadSystemThreadInformation is available since Windows 10, but the
221-
* Windows SDK does not name this THREADINFOCLASS value. */
216+
/* ThreadSystemThreadInformation is not named in the Windows SDK. */
222217
const THREADINFOCLASS system_thread_information = (THREADINFOCLASS)40;
223218
SYSTEM_THREAD_INFORMATION info;
224219
ULONG size = 0;
@@ -453,8 +448,6 @@ unwind_stack_for_thread(
453448
status_flags |= THREAD_STATUS_HAS_EXCEPTION;
454449
}
455450

456-
// Check CPU status
457-
// Optimization: only check CPU status if needed by mode because it's expensive
458451
int cpu_status = THREAD_STATE_UNKNOWN;
459452
if (unwinder->mode == PROFILING_MODE_CPU || unwinder->mode == PROFILING_MODE_ALL) {
460453
cpu_status = get_thread_status(unwinder, tid);

‎Python/remote_debug.h‎

Lines changed: 152 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ extern "C" {
2929

3030
#include "pyconfig.h"
3131
#include "internal/pycore_ceval.h"
32-
#include <stddef.h>
3332

3433
#ifdef __linux__
3534
# include <elf.h>
@@ -61,6 +60,7 @@ extern "C" {
6160
#endif
6261

6362
#if defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX
63+
# include "internal/pycore_bitutils.h"
6464
# include <libproc.h>
6565
# include <mach-o/loader.h>
6666
# include <mach/error.h>
@@ -297,7 +297,155 @@ _Py_RemoteDebug_CleanupProcHandle(proc_handle_t *handle) {
297297

298298
#if defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX
299299

300-
#include "remote_debug_macho.h"
300+
static uint32_t
301+
macho_u32(uint32_t value, int swap)
302+
{
303+
return swap ? _Py_bswap32(value) : value;
304+
}
305+
306+
static uint64_t
307+
macho_u64(uint64_t value, int swap)
308+
{
309+
return swap ? _Py_bswap64(value) : value;
310+
}
311+
312+
static uintptr_t
313+
macho_invalid_image(void)
314+
{
315+
PyErr_SetString(PyExc_RuntimeError, "Invalid or truncated Mach-O image");
316+
return 0;
317+
}
318+
319+
static size_t
320+
macho_header_size(uint32_t magic)
321+
{
322+
switch (magic) {
323+
case MH_MAGIC:
324+
case MH_CIGAM:
325+
return sizeof(struct mach_header);
326+
case MH_MAGIC_64:
327+
case MH_CIGAM_64:
328+
return sizeof(struct mach_header_64);
329+
default:
330+
return 0;
331+
}
332+
}
333+
334+
static uintptr_t
335+
macho_section_address(const void *image, size_t image_size,
336+
uintptr_t base, const char *name)
337+
{
338+
if (strlen(name) > 16) {
339+
return 0;
340+
}
341+
struct mach_header header;
342+
if (image_size < sizeof(header)) {
343+
return macho_invalid_image();
344+
}
345+
memcpy(&header, image, sizeof(header));
346+
int wide = header.magic == MH_MAGIC_64 || header.magic == MH_CIGAM_64;
347+
int swap = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64;
348+
size_t header_size = macho_header_size(header.magic);
349+
if (header_size == 0 || image_size < header_size) {
350+
return macho_invalid_image();
351+
}
352+
size_t commands_size = macho_u32(header.sizeofcmds, swap);
353+
if (commands_size > image_size - header_size) {
354+
return macho_invalid_image();
355+
}
356+
357+
const unsigned char *cursor = (const unsigned char *)image + header_size;
358+
size_t remaining = commands_size;
359+
uint64_t header_address = 0, section_address = 0;
360+
int have_header = 0, have_section = 0;
361+
uint32_t commands = macho_u32(header.ncmds, swap);
362+
for (uint32_t index = 0; index < commands; index++) {
363+
struct load_command command;
364+
if (remaining < sizeof(command)) {
365+
return macho_invalid_image();
366+
}
367+
memcpy(&command, cursor, sizeof(command));
368+
uint32_t kind = macho_u32(command.cmd, swap);
369+
size_t length = macho_u32(command.cmdsize, swap);
370+
if (length < sizeof(command) || length > remaining) {
371+
return macho_invalid_image();
372+
}
373+
if (kind == (wide ? LC_SEGMENT_64 : LC_SEGMENT)) {
374+
uint64_t address, file_offset, file_size;
375+
uint32_t sections;
376+
size_t segment_size, section_size;
377+
int data_segment;
378+
if (wide) {
379+
struct segment_command_64 segment;
380+
segment_size = sizeof(segment);
381+
section_size = sizeof(struct section_64);
382+
if (length < segment_size) {
383+
return macho_invalid_image();
384+
}
385+
memcpy(&segment, cursor, segment_size);
386+
address = macho_u64(segment.vmaddr, swap);
387+
file_offset = macho_u64(segment.fileoff, swap);
388+
file_size = macho_u64(segment.filesize, swap);
389+
sections = macho_u32(segment.nsects, swap);
390+
data_segment = strncmp(segment.segname, "__DATA", 16) == 0;
391+
}
392+
else {
393+
struct segment_command segment;
394+
segment_size = sizeof(segment);
395+
section_size = sizeof(struct section);
396+
if (length < segment_size) {
397+
return macho_invalid_image();
398+
}
399+
memcpy(&segment, cursor, segment_size);
400+
address = macho_u32(segment.vmaddr, swap);
401+
file_offset = macho_u32(segment.fileoff, swap);
402+
file_size = macho_u32(segment.filesize, swap);
403+
sections = macho_u32(segment.nsects, swap);
404+
data_segment = strncmp(segment.segname, "__DATA", 16) == 0;
405+
}
406+
if (sections > (length - segment_size) / section_size) {
407+
return macho_invalid_image();
408+
}
409+
/* __PAGEZERO has no file contents and does not define the slide. */
410+
if (file_offset == 0 && file_size != 0) {
411+
header_address = address;
412+
have_header = 1;
413+
}
414+
if (data_segment) {
415+
const unsigned char *record = cursor + segment_size;
416+
for (uint32_t item = 0; item < sections;
417+
item++, record += section_size)
418+
{
419+
if (strncmp((const char *)record, name, 16) != 0) {
420+
continue;
421+
}
422+
if (wide) {
423+
struct section_64 section;
424+
memcpy(&section, record, sizeof(section));
425+
section_address = macho_u64(section.addr, swap);
426+
}
427+
else {
428+
struct section section;
429+
memcpy(&section, record, sizeof(section));
430+
section_address = macho_u32(section.addr, swap);
431+
}
432+
have_section = 1;
433+
}
434+
}
435+
}
436+
cursor += length;
437+
remaining -= length;
438+
}
439+
if (!have_section) {
440+
return 0;
441+
}
442+
if (!have_header || section_address < header_address ||
443+
section_address - header_address > UINTPTR_MAX - base)
444+
{
445+
return macho_invalid_image();
446+
}
447+
return base + (uintptr_t)(section_address - header_address);
448+
}
301449

302450
static uintptr_t
303451
search_section_in_memory(proc_handle_t *handle, uintptr_t base,
@@ -313,7 +461,6 @@ search_section_in_memory(proc_handle_t *handle, uintptr_t base,
313461
}
314462
int swap = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64;
315463
size_t commands_size = macho_u32(header.sizeofcmds, swap);
316-
/* A corrupt remote header must not trigger an unbounded allocation. */
317464
const size_t max_commands_size = 16 * 1024 * 1024;
318465
if (commands_size > max_commands_size ||
319466
base > UINTPTR_MAX - header_size - commands_size)
@@ -334,7 +481,6 @@ search_section_in_memory(proc_handle_t *handle, uintptr_t base,
334481
return result;
335482
}
336483

337-
338484
static mach_port_t
339485
pid_to_task(pid_t pid)
340486
{
@@ -415,8 +561,7 @@ read_dyld_image_list(proc_handle_t *handle, DyldImageList *list)
415561
return -1;
416562
}
417563

418-
/* dyld_all_image_infos starts with version, count, and an array pointer.
419-
* Decode the target layout rather than the host's pointer-bearing struct. */
564+
/* dyld_all_image_infos uses the target's pointer width. */
420565
size_t timestamp_offset = list->pointer_size == 8 ? 184 : 104;
421566
size_t size = (size_t)Py_MIN(info.all_image_info_size,
422567
timestamp_offset + sizeof(list->timestamp));
@@ -442,7 +587,7 @@ read_dyld_image_list(proc_handle_t *handle, DyldImageList *list)
442587
list->images = (uintptr_t)images;
443588
list->timestamp = 0;
444589
if (version >= 15) {
445-
/* Version 15 added infoArrayChangeTimestamp at these ABI offsets. */
590+
/* Version 15 added infoArrayChangeTimestamp. */
446591
if (size < timestamp_offset + sizeof(list->timestamp)) {
447592
PyErr_SetString(PyExc_RuntimeError, "Truncated dyld image information");
448593
return -1;

0 commit comments

Comments
 (0)