From 3870a681e392fbf596c9c6a61069a5abe9a5e571 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Fri, 13 Nov 2026 22:51:12 +0000 Subject: [PATCH 01/10] Rewrite Tachyon's macOS and Windows inspection code --- Lib/test/test_capi/test_remote_debug_macho.py | 112 +++++ Lib/test/test_external_inspection.py | 40 ++ Makefile.pre.in | 1 + Modules/_remote_debugging/_remote_debugging.h | 12 +- Modules/_remote_debugging/asyncio.c | 9 +- Modules/_remote_debugging/module.c | 14 - Modules/_remote_debugging/threads.c | 97 ++-- Modules/_testinternalcapi.c | 73 +++ Python/remote_debug.h | 465 +++++++----------- Python/remote_debug_macho.h | 163 ++++++ 10 files changed, 587 insertions(+), 399 deletions(-) create mode 100644 Lib/test/test_capi/test_remote_debug_macho.py create mode 100644 Python/remote_debug_macho.h diff --git a/Lib/test/test_capi/test_remote_debug_macho.py b/Lib/test/test_capi/test_remote_debug_macho.py new file mode 100644 index 00000000000000..e559cb8b095b09 --- /dev/null +++ b/Lib/test/test_capi/test_remote_debug_macho.py @@ -0,0 +1,112 @@ +"""Mach-O section lookup with synthetic executable images.""" + +import struct +import sys +import unittest + +from test.support.import_helper import import_module + +_testinternalcapi = import_module('_testinternalcapi') + + +@unittest.skipUnless(sys.platform == 'darwin', 'requires Mach-O headers') +class MachOSectionTests(unittest.TestCase): + BASE = 0x20000000 + OFFSET = 0x2345 + + def lookup(self, image, *, base=BASE, name='PyRuntime'): + return _testinternalcapi.get_macho_section_address(bytes(image), base, name) + + def image(self, *, wide=True, endian='<', data_first=False, name=b'PyRuntime'): + # The preferred VM address differs from the load address. + address = 0x100000000 if wide else 0x1000 + segment_format = endian + ('II16sQQQQiiII' if wide else 'II16sIIIIiiII') + section_format = endian + ('16s16sQQIIIIIIII' if wide else '16s16sIIIIIIIII') + segment_size = struct.calcsize(segment_format) + section_size = struct.calcsize(section_format) + kind = 0x19 if wide else 1 # LC_SEGMENT_64 / LC_SEGMENT + text = struct.pack(segment_format, kind, segment_size, b'__TEXT', + address, 0x1000, 0, 0x1000, 7, 5, 0, 0) + data = struct.pack(segment_format, kind, segment_size + section_size, + b'__DATA', address + 0x2000, 0x1000, + 0x1000, 0x1000, 7, 3, 1, 0) + fields = [name, b'__DATA', address + self.OFFSET, 0x100, + 0, 0, 0, 0, 0, 0, 0] + if wide: + fields.append(0) + data += struct.pack(section_format, *fields) + uuid = struct.pack(endian + 'II16s', 0x1b, 24, bytes(16)) + commands = data + uuid + text if data_first else text + uuid + data + header = [0xfeedfacf if wide else 0xfeedface, + 0x1000007 if wide else 7, 3, 2, 3, len(commands), 0] + if wide: + header.append(0) + return struct.pack(endian + 'I' * len(header), *header) + commands + + def test_thin_images(self): + for wide in (False, True): + for endian in ('<', '>'): + for data_first in (False, True): + with self.subTest(wide=wide, endian=endian, data_first=data_first): + image = self.image(wide=wide, endian=endian, data_first=data_first) + self.assertEqual(self.lookup(image), self.BASE + self.OFFSET) + + def test_missing_section(self): + self.assertEqual(self.lookup(self.image(), name='AsyncioDebug'), 0) + + def test_fixed_width_section_name(self): + name = b'0123456789abcdef' + self.assertEqual(self.lookup(self.image(name=name), name=name.decode()), + self.BASE + self.OFFSET) + self.assertEqual(self.lookup(self.image(name=name), name=name.decode() + 'x'), 0) + + def test_truncated_images(self): + for image in (self.image(), self.image(wide=False)): + for size in range(len(image)): + with self.subTest(length=len(image), size=size): + with self.assertRaises(RuntimeError): + self.lookup(image[:size]) + + def test_invalid_commands(self): + # The first 64-bit segment command follows the 32-byte header. + for field, value in ((16, 1000), # ncmds + (20, 0xffffffff), # sizeofcmds + (36, 0), # cmdsize + (36, 8), # shorter than segment header + (36, 0xffffffff), + (96, 0xffffffff)): # nsects + with self.subTest(field=field, value=value): + image = bytearray(self.image()) + struct.pack_into(' # include -# define MAX_NATIVE_THREADS 4096 # endif #endif @@ -104,7 +103,6 @@ typedef struct { #ifdef MS_WINDOWS #define STATUS_SUCCESS ((NTSTATUS)0x00000000L) -#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) #define STATUS_NO_MORE_ENTRIES ((NTSTATUS)0x8000001AL) typedef enum _WIN32_THREADSTATE { WIN32_THREADSTATE_INITIALIZED = 0, @@ -387,14 +385,6 @@ typedef struct { #ifdef Py_GIL_DISABLED uint32_t tlbc_generation; _Py_hashtable_t *tlbc_cache; -#endif -#ifdef __APPLE__ - uint64_t thread_id_offset; - int thread_id_offset_initialized; -#endif -#ifdef MS_WINDOWS - PVOID win_process_buffer; - ULONG win_process_buffer_size; #endif // Thread stopping state (only on platforms that support it) #ifdef Py_REMOTE_DEBUG_SUPPORTS_BLOCKING @@ -681,7 +671,7 @@ extern int find_running_frame( uintptr_t *frame ); -extern int get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid, uint64_t pthread_id); +extern int get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid); extern PyObject* unwind_stack_for_thread( RemoteUnwinderObject *unwinder, diff --git a/Modules/_remote_debugging/asyncio.c b/Modules/_remote_debugging/asyncio.c index d31f783186a2c3..9642ed347de23c 100644 --- a/Modules/_remote_debugging/asyncio.c +++ b/Modules/_remote_debugging/asyncio.c @@ -40,14 +40,7 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle) } } #elif defined(__APPLE__) && TARGET_OS_OSX - // On macOS, try libpython first, then fall back to python - address = search_map_for_section(handle, "AsyncioDebug", "libpython", - NULL); - if (address == 0 && !_Py_RemoteDebug_HasPermissionError()) { - PyErr_Clear(); - address = search_map_for_section(handle, "AsyncioDebug", "python", - NULL); - } + address = search_loaded_images_for_section(handle, "AsyncioDebug", NULL); if (address == 0) { if (!_Py_RemoteDebug_HasPermissionError()) { PyObject *exc = PyErr_GetRaisedException(); diff --git a/Modules/_remote_debugging/module.c b/Modules/_remote_debugging/module.c index 28f73a86dcdbda..3b998fd0200d6e 100644 --- a/Modules/_remote_debugging/module.c +++ b/Modules/_remote_debugging/module.c @@ -456,15 +456,6 @@ _remote_debugging_RemoteUnwinder___init___impl(RemoteUnwinderObject *self, } #endif -#if defined(__APPLE__) - self->thread_id_offset = 0; - self->thread_id_offset_initialized = 0; -#endif - -#ifdef MS_WINDOWS - self->win_process_buffer = NULL; - self->win_process_buffer_size = 0; -#endif #ifdef __linux__ self->thread_tids = NULL; self->thread_tids_capacity = 0; @@ -1347,11 +1338,6 @@ RemoteUnwinder_dealloc(PyObject *op) if (self->code_object_cache) { _Py_hashtable_destroy(self->code_object_cache); } -#ifdef MS_WINDOWS - if (self->win_process_buffer != NULL) { - PyMem_Free(self->win_process_buffer); - } -#endif #ifdef Py_GIL_DISABLED if (self->tlbc_cache) { diff --git a/Modules/_remote_debugging/threads.c b/Modules/_remote_debugging/threads.c index a3bd6c9edfb58e..6e2269aa713258 100644 --- a/Modules/_remote_debugging/threads.c +++ b/Modules/_remote_debugging/threads.c @@ -159,39 +159,17 @@ find_running_frame( * ============================================================================ */ int -get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid, uint64_t pthread_id) { +get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid) { #if defined(__APPLE__) && TARGET_OS_OSX - if (!unwinder->thread_id_offset_initialized) { - uint64_t *tids = (uint64_t *)PyMem_Malloc(MAX_NATIVE_THREADS * sizeof(uint64_t)); - if (!tids) { - // Non-fatal: thread status is best-effort - return THREAD_STATE_UNKNOWN; - } - int n = proc_pidinfo(unwinder->handle.pid, PROC_PIDLISTTHREADS, 0, tids, MAX_NATIVE_THREADS * sizeof(uint64_t)) / sizeof(uint64_t); - if (n <= 0) { - PyMem_Free(tids); - return THREAD_STATE_UNKNOWN; - } - uint64_t min_offset = UINT64_MAX; - for (int i = 0; i < n; i++) { - uint64_t offset = tids[i] - pthread_id; - if (offset < min_offset) { - min_offset = offset; - } - } - unwinder->thread_id_offset = min_offset; - unwinder->thread_id_offset_initialized = 1; - PyMem_Free(tids); - } - struct proc_threadinfo ti; - uint64_t tid_with_offset = pthread_id + unwinder->thread_id_offset; - if (proc_pidinfo(unwinder->handle.pid, PROC_PIDTHREADINFO, tid_with_offset, &ti, sizeof(ti)) != sizeof(ti)) { + struct proc_threadinfo thread; + /* This flavor accepts the native ID from pthread_threadid_np(). */ + int size = proc_pidinfo(unwinder->handle.pid, PROC_PIDTHREADID64INFO, + tid, &thread, sizeof(thread)); + if (size != sizeof(thread)) { return THREAD_STATE_UNKNOWN; } - if (ti.pth_run_state == TH_STATE_RUNNING) { - return THREAD_STATE_RUNNING; - } - return THREAD_STATE_IDLE; + return thread.pth_run_state == TH_STATE_RUNNING + ? THREAD_STATE_RUNNING : THREAD_STATE_IDLE; #elif defined(__linux__) char stat_path[256]; char buffer[2048] = ""; @@ -232,46 +210,29 @@ get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid, uint64_t pthread return THREAD_STATE_UNKNOWN; } #elif defined(MS_WINDOWS) - ULONG n; - NTSTATUS status = NtQuerySystemInformation( - SystemProcessInformation, - unwinder->win_process_buffer, - unwinder->win_process_buffer_size, - &n - ); - if (status == STATUS_INFO_LENGTH_MISMATCH) { - // Buffer was too small so we reallocate a larger one and try again. - unwinder->win_process_buffer_size = n; - PVOID new_buffer = PyMem_Realloc(unwinder->win_process_buffer, n); - if (!new_buffer) { - // Match Linux/macOS: degrade gracefully on alloc failure - return THREAD_STATE_UNKNOWN; - } - unwinder->win_process_buffer = new_buffer; - return get_thread_status(unwinder, tid, pthread_id); - } - if (status != STATUS_SUCCESS) { + if (tid > MAXDWORD) { return THREAD_STATE_UNKNOWN; } - - SYSTEM_PROCESS_INFORMATION *pi = (SYSTEM_PROCESS_INFORMATION *)unwinder->win_process_buffer; - while ((ULONG)(ULONG_PTR)pi->UniqueProcessId != unwinder->handle.pid) { - if (pi->NextEntryOffset == 0) { - // Process not found (may have exited) - return THREAD_STATE_UNKNOWN; - } - pi = (SYSTEM_PROCESS_INFORMATION *)(((BYTE *)pi) + pi->NextEntryOffset); + HANDLE thread = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)tid); + if (thread == NULL) { + return THREAD_STATE_UNKNOWN; } - - SYSTEM_THREAD_INFORMATION *ti = (SYSTEM_THREAD_INFORMATION *)((char *)pi + sizeof(SYSTEM_PROCESS_INFORMATION)); - for (size_t i = 0; i < pi->NumberOfThreads; i++, ti++) { - if (ti->ClientId.UniqueThread == (HANDLE)tid) { - return ti->ThreadState != WIN32_THREADSTATE_RUNNING ? THREAD_STATE_IDLE : THREAD_STATE_RUNNING; - } + /* ThreadSystemThreadInformation is available since Windows 10, but the + * Windows SDK does not name this THREADINFOCLASS value. */ + const THREADINFOCLASS system_thread_information = (THREADINFOCLASS)40; + SYSTEM_THREAD_INFORMATION info; + ULONG size = 0; + NTSTATUS result = NtQueryInformationThread( + thread, system_thread_information, &info, sizeof(info), &size); + CloseHandle(thread); + if (result != STATUS_SUCCESS || size != sizeof(info) || + (ULONG_PTR)info.ClientId.UniqueProcess != (ULONG_PTR)unwinder->handle.pid || + (ULONG_PTR)info.ClientId.UniqueThread != tid) + { + return THREAD_STATE_UNKNOWN; } - - // Thread not found (may have exited) - return THREAD_STATE_UNKNOWN; + return info.ThreadState == WIN32_THREADSTATE_RUNNING + ? THREAD_STATE_RUNNING : THREAD_STATE_IDLE; #else return THREAD_STATE_UNKNOWN; #endif @@ -491,12 +452,10 @@ unwind_stack_for_thread( } // Check CPU status - long pthread_id = GET_MEMBER(long, ts, unwinder->debug_offsets.thread_state.thread_id); - // Optimization: only check CPU status if needed by mode because it's expensive int cpu_status = THREAD_STATE_UNKNOWN; if (unwinder->mode == PROFILING_MODE_CPU || unwinder->mode == PROFILING_MODE_ALL) { - cpu_status = get_thread_status(unwinder, tid, pthread_id); + cpu_status = get_thread_status(unwinder, tid); } if (cpu_status == THREAD_STATE_UNKNOWN) { diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 586b687f233ce0..1e2a18f269b2c6 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -3482,7 +3482,80 @@ static PyTypeObject SelfInterruptingContextManager_Type = { }; +#ifdef __APPLE__ +# include +# if TARGET_OS_OSX +# include "pycore_debug_offsets.h" +# include "../Python/remote_debug.h" + +static PyObject * +get_macho_section_address(PyObject *self, PyObject *args) +{ + const char *image, *section; + Py_ssize_t size; + unsigned long long base; + if (!PyArg_ParseTuple(args, "y#Ks", &image, &size, &base, §ion)) { + return NULL; + } + if (base > UINTPTR_MAX) { + return PyErr_Format(PyExc_OverflowError, "base does not fit uintptr_t"); + } + uintptr_t address = macho_section_address(image, size, base, section); + if (PyErr_Occurred()) { + return NULL; + } + return PyLong_FromUnsignedLongLong(address); +} + +static PyObject * +get_dyld_image_addresses(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + proc_handle_t *handle = PyMem_Calloc(1, sizeof(*handle)); + if (handle == NULL) { + return PyErr_NoMemory(); + } + PyObject *result = NULL; + unsigned char *images = NULL; + if (_Py_RemoteDebug_InitProcHandle(handle, getpid()) < 0) { + goto done; + } + DyldImageList list; + images = copy_dyld_images(handle, &list); + if (images == NULL) { + if (!PyErr_Occurred()) { + result = PyList_New(0); + } + goto done; + } + result = PyList_New(list.count); + if (result == NULL) { + goto done; + } + for (uint32_t index = 0; index < list.count; index++) { + uint64_t address = 0; + memcpy(&address, images + index * (3 * list.pointer_size), list.pointer_size); + PyObject *item = PyLong_FromUnsignedLongLong(address); + if (item == NULL) { + Py_CLEAR(result); + goto done; + } + PyList_SET_ITEM(result, index, item); + } +done: + PyMem_Free(images); + _Py_RemoteDebug_CleanupProcHandle(handle); + PyMem_Free(handle); + return result; +} + +# endif +#endif + static PyMethodDef module_functions[] = { +#if defined(__APPLE__) && TARGET_OS_OSX + {"get_macho_section_address", get_macho_section_address, METH_VARARGS}, + {"get_dyld_image_addresses", get_dyld_image_addresses, METH_NOARGS}, +#endif {"get_configs", get_configs, METH_NOARGS}, {"get_eval_frame_stats", get_eval_frame_stats, METH_NOARGS, NULL}, {"get_recursion_depth", get_recursion_depth, METH_NOARGS}, diff --git a/Python/remote_debug.h b/Python/remote_debug.h index a4a3a010926a7b..912013270fc15f 100644 --- a/Python/remote_debug.h +++ b/Python/remote_debug.h @@ -61,9 +61,7 @@ extern "C" { #if defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX # include -# include # include -# include # include # include # include @@ -72,7 +70,6 @@ extern "C" { # include # include # include -# include #endif #ifdef MS_WINDOWS @@ -282,6 +279,11 @@ _Py_RemoteDebug_CleanupProcHandle(proc_handle_t *handle) { CloseHandle(handle->hProcess); handle->hProcess = NULL; } +#elif defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX + if (handle->task != MACH_PORT_NULL) { + mach_port_deallocate(mach_task_self(), handle->task); + handle->task = MACH_PORT_NULL; + } #elif defined(__linux__) if (handle->memfd != -1) { close(handle->memfd); @@ -294,233 +296,40 @@ _Py_RemoteDebug_CleanupProcHandle(proc_handle_t *handle) { #if defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX -static uintptr_t -return_section_address64( - const char* section, - uintptr_t base, - void* map -) { - struct mach_header_64* hdr = (struct mach_header_64*)map; - int ncmds = hdr->ncmds; - - int cmd_cnt = 0; - struct segment_command_64* cmd = map + sizeof(struct mach_header_64); - - uintptr_t vmaddr = 0; - - for (int i = 0; cmd_cnt < 2 && i < ncmds; i++) { - if (cmd->cmd == LC_SEGMENT_64 && strcmp(cmd->segname, "__TEXT") == 0) { - vmaddr = cmd->vmaddr; - } - if (cmd->cmd == LC_SEGMENT_64 && strcmp(cmd->segname, "__DATA") == 0) { - // The section address only needs the image's ASLR slide. - // VM regions need not match the segment's on-disk size. - - int nsects = cmd->nsects; - struct section_64* sec = (struct section_64*)( - (void*)cmd + sizeof(struct segment_command_64) - ); - for (int j = 0; j < nsects; j++) { - if (strcmp(sec[j].sectname, section) == 0) { - return base + sec[j].addr - vmaddr; - } - } - cmd_cnt++; - } - - cmd = (struct segment_command_64*)((void*)cmd + cmd->cmdsize); - } - - return 0; -} - -static uintptr_t -return_section_address32( - const char* section, - uintptr_t base, - void* map -) { - struct mach_header* hdr = (struct mach_header*)map; - int ncmds = hdr->ncmds; - - int cmd_cnt = 0; - struct segment_command* cmd = map + sizeof(struct mach_header); - - uintptr_t vmaddr = 0; - - for (int i = 0; cmd_cnt < 2 && i < ncmds; i++) { - if (cmd->cmd == LC_SEGMENT && strcmp(cmd->segname, "__TEXT") == 0) { - vmaddr = cmd->vmaddr; - } - if (cmd->cmd == LC_SEGMENT && strcmp(cmd->segname, "__DATA") == 0) { - // The section address only needs the image's ASLR slide. - // VM regions need not match the segment's on-disk size. - - int nsects = cmd->nsects; - struct section* sec = (struct section*)( - (void*)cmd + sizeof(struct segment_command) - ); - for (int j = 0; j < nsects; j++) { - if (strcmp(sec[j].sectname, section) == 0) { - return base + sec[j].addr - vmaddr; - } - } - cmd_cnt++; - } - - cmd = (struct segment_command*)((void*)cmd + cmd->cmdsize); - } - - return 0; -} - -static uintptr_t -return_section_address_fat( - const char* section, - uintptr_t base, - void* map -) { - struct fat_header* fat_hdr = (struct fat_header*)map; - - // Determine host CPU type for architecture selection - cpu_type_t cpu; - int is_abi64; - size_t cpu_size = sizeof(cpu), abi64_size = sizeof(is_abi64); - - if (sysctlbyname("hw.cputype", &cpu, &cpu_size, NULL, 0) != 0) { - int err = errno; - _set_debug_oserror_from_errno(err, - "Failed to determine CPU type via sysctlbyname " - "for fat binary analysis at 0x%lx: %s", - base, strerror(err)); - return 0; - } - if (sysctlbyname("hw.cpu64bit_capable", &is_abi64, &abi64_size, NULL, 0) != 0) { - int err = errno; - _set_debug_oserror_from_errno(err, - "Failed to determine CPU ABI capability via sysctlbyname " - "for fat binary analysis at 0x%lx: %s", - base, strerror(err)); - return 0; - } - - cpu |= is_abi64 * CPU_ARCH_ABI64; - - // Check endianness - int swap = fat_hdr->magic == FAT_CIGAM; - struct fat_arch* arch = (struct fat_arch*)(map + sizeof(struct fat_header)); - - // Get number of architectures in fat binary - uint32_t nfat_arch = swap ? __builtin_bswap32(fat_hdr->nfat_arch) : fat_hdr->nfat_arch; - - // Search for matching architecture - for (uint32_t i = 0; i < nfat_arch; i++) { - cpu_type_t arch_cpu = swap ? __builtin_bswap32(arch[i].cputype) : arch[i].cputype; - - if (arch_cpu == cpu) { - // Found matching architecture, now process it - uint32_t offset = swap ? __builtin_bswap32(arch[i].offset) : arch[i].offset; - struct mach_header_64* hdr = (struct mach_header_64*)(map + offset); - - // Determine which type of Mach-O it is and process accordingly - switch (hdr->magic) { - case MH_MAGIC: - case MH_CIGAM: - return return_section_address32(section, base, (void*)hdr); - - case MH_MAGIC_64: - case MH_CIGAM_64: - return return_section_address64(section, base, (void*)hdr); - - default: - PyErr_Format(PyExc_RuntimeError, - "Unknown Mach-O magic number 0x%x in fat binary architecture %u at base 0x%lx", - hdr->magic, i, base); - return 0; - } - } - } - - PyErr_Format(PyExc_RuntimeError, - "No matching architecture found for CPU type 0x%x " - "in fat binary at base 0x%lx (%u architectures examined)", - cpu, base, nfat_arch); - return 0; -} +#include "remote_debug_macho.h" static uintptr_t -search_section_in_file(const char* secname, char* path, uintptr_t base) +search_section_in_memory(proc_handle_t *handle, uintptr_t base, + const char *section) { - int fd = open(path, O_RDONLY); - if (fd == -1) { - int err = errno; - _set_debug_oserror_from_errno_with_filename(err, path, - "Cannot open binary file '%s' for section '%s' search: %s", - path, secname, strerror(err)); + struct mach_header header; + if (_Py_RemoteDebug_ReadRemoteMemory(handle, base, sizeof(header), &header) < 0) { return 0; } - - struct stat fs; - if (fstat(fd, &fs) == -1) { - int err = errno; - _set_debug_oserror_from_errno_with_filename(err, path, - "Cannot get file size for binary '%s' during section '%s' search: %s", - path, secname, strerror(err)); - close(fd); + size_t header_size = macho_header_size(header.magic); + if (header_size == 0) { return 0; } - - void* map = mmap(0, fs.st_size, PROT_READ, MAP_SHARED, fd, 0); - if (map == MAP_FAILED) { - int err = errno; - _set_debug_oserror_from_errno_with_filename(err, path, - "Cannot memory map binary file '%s' (size: %lld bytes) for section '%s' search: %s", - path, (long long)fs.st_size, secname, strerror(err)); - close(fd); + int swap = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64; + size_t commands_size = macho_u32(header.sizeofcmds, swap); + /* A corrupt remote header must not trigger an unbounded allocation. */ + const size_t max_commands_size = 16 * 1024 * 1024; + if (commands_size > max_commands_size || + base > UINTPTR_MAX - header_size - commands_size) + { + return macho_invalid_image(); + } + size_t size = header_size + commands_size; + void *image = PyMem_Malloc(size); + if (image == NULL) { + PyErr_NoMemory(); return 0; } - uintptr_t result = 0; - uint32_t magic = *(uint32_t*)map; - - switch (magic) { - case MH_MAGIC: - case MH_CIGAM: - result = return_section_address32(secname, base, map); - break; - case MH_MAGIC_64: - case MH_CIGAM_64: - result = return_section_address64(secname, base, map); - break; - case FAT_MAGIC: - case FAT_CIGAM: - result = return_section_address_fat(secname, base, map); - break; - default: - PyErr_Format(PyExc_RuntimeError, - "Unrecognized Mach-O magic number 0x%x in binary file '%s' for section '%s' search", - magic, path, secname); - break; - } - - if (munmap(map, fs.st_size) != 0) { - if (!PyErr_Occurred()) { - int err = errno; - _set_debug_oserror_from_errno_with_filename(err, path, - "Failed to unmap binary file '%s' (size: %lld bytes): %s", - path, (long long)fs.st_size, strerror(err)); - } - result = 0; - } - if (close(fd) != 0) { - if (!PyErr_Occurred()) { - int err = errno; - _set_debug_oserror_from_errno_with_filename(err, path, - "Failed to close binary file '%s': %s", - path, strerror(err)); - } - result = 0; + if (_Py_RemoteDebug_ReadRemoteMemory(handle, base, size, image) == 0) { + result = macho_section_address(image, size, base, section); } + PyMem_Free(image); return result; } @@ -542,86 +351,156 @@ pid_to_task(pid_t pid) return task; } -static uintptr_t -search_map_for_section(proc_handle_t *handle, const char* secname, const char* substr, - section_validator_t validator) { - mach_vm_address_t address = 0; - mach_vm_size_t size = 0; - mach_msg_type_number_t count = sizeof(vm_region_basic_info_data_64_t); - vm_region_basic_info_data_64_t region_info; - mach_port_t object_name; - - mach_port_t proc_ref = pid_to_task(handle->pid); - if (proc_ref == 0) { - if (!PyErr_Occurred()) { - PyErr_Format(PyExc_PermissionError, - "Cannot get task port for PID %d during section search", - handle->pid); - } - return 0; - } +typedef struct { + uintptr_t address; + uintptr_t images; + uint64_t timestamp; + uint32_t count; + size_t pointer_size; +} DyldImageList; - char map_filename[MAXPATHLEN + 1]; +static int +read_dyld_image_list(proc_handle_t *handle, DyldImageList *list) +{ + task_dyld_info_data_t info; + mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; + kern_return_t status = task_info(handle->task, TASK_DYLD_INFO, + (task_info_t)&info, &count); + if (status != KERN_SUCCESS) { + PyErr_Format(status == KERN_PROTECTION_FAILURE + ? PyExc_PermissionError : PyExc_RuntimeError, + "Cannot read dyld information for PID %d (Mach error %d)", + handle->pid, status); + return -1; + } + if (info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_64) { + list->pointer_size = 8; + } + else if (info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_32) { + list->pointer_size = 4; + } + else { + PyErr_SetString(PyExc_RuntimeError, "Unknown dyld image-info format"); + return -1; + } - kern_return_t kr; - while ((kr = mach_vm_region( - proc_ref, - &address, - &size, - VM_REGION_BASIC_INFO_64, - (vm_region_info_t)®ion_info, - &count, - &object_name)) == KERN_SUCCESS) + /* dyld_all_image_infos starts with version, count, and an array pointer. + * Decode the target layout rather than the host's pointer-bearing struct. */ + size_t timestamp_offset = list->pointer_size == 8 ? 184 : 104; + size_t size = (size_t)Py_MIN(info.all_image_info_size, + timestamp_offset + sizeof(list->timestamp)); + unsigned char prefix[192]; + if (size < 8 + list->pointer_size || + info.all_image_info_addr > UINTPTR_MAX - size) { + PyErr_SetString(PyExc_RuntimeError, "Invalid dyld image-info address"); + return -1; + } + list->address = (uintptr_t)info.all_image_info_addr; + if (_Py_RemoteDebug_ReadRemoteMemory(handle, list->address, size, prefix) < 0) { + return -1; + } + uint32_t version; + memcpy(&version, prefix, sizeof(version)); + memcpy(&list->count, prefix + 4, sizeof(list->count)); + uint64_t images = 0; + memcpy(&images, prefix + 8, list->pointer_size); + if (images > UINTPTR_MAX) { + PyErr_SetString(PyExc_RuntimeError, "dyld image address does not fit uintptr_t"); + return -1; + } + list->images = (uintptr_t)images; + list->timestamp = 0; + if (version >= 15) { + /* Version 15 added infoArrayChangeTimestamp at these ABI offsets. */ + if (size < timestamp_offset + sizeof(list->timestamp)) { + PyErr_SetString(PyExc_RuntimeError, "Truncated dyld image information"); + return -1; + } + memcpy(&list->timestamp, prefix + timestamp_offset, sizeof(list->timestamp)); + } + return 0; +} - if ((region_info.protection & VM_PROT_READ) == 0 - || (region_info.protection & VM_PROT_EXECUTE) == 0) { - address += size; +static unsigned char * +copy_dyld_images(proc_handle_t *handle, DyldImageList *list) +{ + for (int attempt = 0; attempt < 3; attempt++) { + if (read_dyld_image_list(handle, list) < 0) { + if (_Py_RemoteDebug_IsFatalReadError()) { + return NULL; + } + PyErr_Clear(); continue; } - - int path_len = proc_regionfilename( - handle->pid, address, map_filename, MAXPATHLEN); - if (path_len == 0) { - address += size; + /* dyld clears infoArray while it changes the image list. */ + if (list->images == 0) { continue; } - - char* filename = strrchr(map_filename, '/'); - if (filename != NULL) { - filename++; // Move past the '/' - } else { - filename = map_filename; // No path, use the whole string + if (list->count == 0) { + return NULL; } - - if (strncmp(filename, substr, strlen(substr)) == 0) { - PyErr_Clear(); - uintptr_t result = search_section_in_file( - secname, map_filename, address); - if (result != 0) { - if (validator == NULL || validator(handle, result)) { - return result; - } - if (_Py_RemoteDebug_HasPermissionError()) { - return 0; - } - } - else if (_Py_RemoteDebug_HasPermissionError()) { - return 0; - } + size_t record_size = 3 * list->pointer_size; + if (list->count > 65536 || + list->images > UINTPTR_MAX - list->count * record_size) + { + PyErr_SetString(PyExc_RuntimeError, "Invalid dyld image array"); + return NULL; } - - address += size; + size_t size = list->count * record_size; + unsigned char *images = PyMem_Malloc(size); + if (images == NULL) { + PyErr_NoMemory(); + return NULL; + } + DyldImageList after; + if (_Py_RemoteDebug_ReadRemoteMemory(handle, list->images, size, images) == 0 && + read_dyld_image_list(handle, &after) == 0 && + after.address == list->address && after.images == list->images && + after.count == list->count && after.timestamp == list->timestamp && + after.pointer_size == list->pointer_size) + { + return images; + } + PyMem_Free(images); + if (_Py_RemoteDebug_IsFatalReadError()) { + return NULL; + } + PyErr_Clear(); } + PyErr_SetString(PyExc_RuntimeError, "The remote dyld image list changed during inspection"); + return NULL; +} - if (kr != KERN_INVALID_ADDRESS && !PyErr_Occurred()) { - PyErr_Format(PyExc_RuntimeError, - "mach_vm_region failed while searching PID %d for section '%s' " - "(kern_return_t: %d)", - handle->pid, secname, kr); +static uintptr_t +search_loaded_images_for_section(proc_handle_t *handle, const char *section, + section_validator_t validator) +{ + DyldImageList list; + unsigned char *images = copy_dyld_images(handle, &list); + if (images == NULL) { + return 0; } - - return 0; + uintptr_t result = 0; + for (uint32_t index = 0; index < list.count; index++) { + uint64_t address = 0; + memcpy(&address, images + index * (3 * list.pointer_size), list.pointer_size); + if (address == 0 || address > UINTPTR_MAX) { + continue; + } + uintptr_t candidate = search_section_in_memory(handle, address, section); + if (candidate != 0 && (validator == NULL || validator(handle, candidate))) { + result = candidate; + break; + } + if (_Py_RemoteDebug_IsFatalReadError()) { + break; + } + /* A library can unload after the image-list snapshot. */ + PyErr_Clear(); + } + PyMem_Free(images); + return result; } #endif // (__APPLE__ && defined(TARGET_OS_OSX) && TARGET_OS_OSX) @@ -1173,22 +1052,14 @@ _Py_RemoteDebug_GetPyRuntimeAddress(proc_handle_t* handle) } } #elif defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX - // On macOS, try libpython first, then fall back to python - const char* candidates[] = {"libpython", "python", "Python", NULL}; - for (const char** candidate = candidates; *candidate; candidate++) { - PyErr_Clear(); - address = search_map_for_section(handle, "PyRuntime", *candidate, - _Py_RemoteDebug_ValidatePyRuntimeCookie); - if (address != 0 || _Py_RemoteDebug_HasPermissionError()) { - break; - } - } + address = search_loaded_images_for_section( + handle, "PyRuntime", _Py_RemoteDebug_ValidatePyRuntimeCookie); if (address == 0) { if (!_Py_RemoteDebug_HasPermissionError()) { PyObject *exc = PyErr_GetRaisedException(); PyErr_Format(PyExc_RuntimeError, "Failed to find the PyRuntime section in process %d " - "on macOS platform (tried both libpython and python)", + "on macOS platform", handle->pid); _PyErr_ChainExceptions1(exc); } diff --git a/Python/remote_debug_macho.h b/Python/remote_debug_macho.h new file mode 100644 index 00000000000000..03cd6d24f760c8 --- /dev/null +++ b/Python/remote_debug_macho.h @@ -0,0 +1,163 @@ +/* Bounded section lookup in a copy of a remote Mach-O header and commands. */ +#ifndef Py_REMOTE_DEBUG_MACHO_H +#define Py_REMOTE_DEBUG_MACHO_H + +#include +#include "internal/pycore_bitutils.h" + +static uint32_t +macho_u32(uint32_t value, int swap) +{ + return swap ? _Py_bswap32(value) : value; +} + +static uint64_t +macho_u64(uint64_t value, int swap) +{ + return swap ? _Py_bswap64(value) : value; +} + +static uintptr_t +macho_invalid_image(void) +{ + PyErr_SetString(PyExc_RuntimeError, "Invalid or truncated Mach-O image"); + return 0; +} + +static size_t +macho_header_size(uint32_t magic) +{ + switch (magic) { + case MH_MAGIC: + case MH_CIGAM: + return sizeof(struct mach_header); + case MH_MAGIC_64: + case MH_CIGAM_64: + return sizeof(struct mach_header_64); + default: + return 0; + } +} + +static uintptr_t +macho_section_address(const void *image, size_t image_size, + uintptr_t base, const char *name) +{ + if (strlen(name) > 16) { + return 0; + } + const unsigned char *bytes = image; + uint32_t magic; + if (image_size < sizeof(magic)) { + return macho_invalid_image(); + } + memcpy(&magic, bytes, sizeof(magic)); + + int wide = magic == MH_MAGIC_64 || magic == MH_CIGAM_64; + int swap = magic == MH_CIGAM || magic == MH_CIGAM_64; + size_t header_size = macho_header_size(magic); + if (header_size == 0 || image_size < header_size) { + return macho_invalid_image(); + } + struct mach_header header; + memcpy(&header, bytes, sizeof(header)); + size_t commands_size = macho_u32(header.sizeofcmds, swap); + if (commands_size > image_size - header_size) { + return macho_invalid_image(); + } + + const unsigned char *cursor = bytes + header_size; + size_t remaining = commands_size; + uint64_t header_address = 0, section_address = 0; + int have_header = 0, have_section = 0; + uint32_t commands = macho_u32(header.ncmds, swap); + for (uint32_t index = 0; index < commands; index++) { + struct load_command command; + if (remaining < sizeof(command)) { + return macho_invalid_image(); + } + memcpy(&command, cursor, sizeof(command)); + uint32_t kind = macho_u32(command.cmd, swap); + size_t length = macho_u32(command.cmdsize, swap); + if (length < sizeof(command) || length > remaining) { + return macho_invalid_image(); + } + if (kind == (wide ? LC_SEGMENT_64 : LC_SEGMENT)) { + uint64_t address, file_offset, file_size; + uint32_t sections; + size_t segment_size, section_size; + int data_segment; + if (wide) { + struct segment_command_64 segment; + segment_size = sizeof(segment); + section_size = sizeof(struct section_64); + if (length < segment_size) { + return macho_invalid_image(); + } + memcpy(&segment, cursor, segment_size); + address = macho_u64(segment.vmaddr, swap); + file_offset = macho_u64(segment.fileoff, swap); + file_size = macho_u64(segment.filesize, swap); + sections = macho_u32(segment.nsects, swap); + data_segment = strncmp(segment.segname, "__DATA", 16) == 0; + } + else { + struct segment_command segment; + segment_size = sizeof(segment); + section_size = sizeof(struct section); + if (length < segment_size) { + return macho_invalid_image(); + } + memcpy(&segment, cursor, segment_size); + address = macho_u32(segment.vmaddr, swap); + file_offset = macho_u32(segment.fileoff, swap); + file_size = macho_u32(segment.filesize, swap); + sections = macho_u32(segment.nsects, swap); + data_segment = strncmp(segment.segname, "__DATA", 16) == 0; + } + if (sections > (length - segment_size) / section_size) { + return macho_invalid_image(); + } + /* The file-backed segment at offset zero contains the header. + * __PAGEZERO has no file contents and does not define the slide. */ + if (file_offset == 0 && file_size != 0) { + header_address = address; + have_header = 1; + } + if (data_segment) { + const unsigned char *record = cursor + segment_size; + for (uint32_t item = 0; item < sections; + item++, record += section_size) + { + if (strncmp((const char *)record, name, 16) != 0) { + continue; + } + if (wide) { + struct section_64 section; + memcpy(§ion, record, sizeof(section)); + section_address = macho_u64(section.addr, swap); + } + else { + struct section section; + memcpy(§ion, record, sizeof(section)); + section_address = macho_u32(section.addr, swap); + } + have_section = 1; + } + } + } + cursor += length; + remaining -= length; + } + if (!have_section) { + return 0; + } + if (!have_header || section_address < header_address || + section_address - header_address > UINTPTR_MAX - base) + { + return macho_invalid_image(); + } + return base + (uintptr_t)(section_address - header_address); +} + +#endif /* Py_REMOTE_DEBUG_MACHO_H */ From beadba1aa518496ed8e961b1252d38b65b604bf6 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Fri, 13 Nov 2026 23:13:40 +0000 Subject: [PATCH 02/10] Preserve unsigned thread IDs and target pointer widths --- Modules/_remote_debugging/_remote_debugging.h | 10 ---------- Modules/_remote_debugging/threads.c | 8 +++++--- Modules/_testinternalcapi.c | 4 ++-- Python/remote_debug.h | 20 +++++++++++++++---- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Modules/_remote_debugging/_remote_debugging.h b/Modules/_remote_debugging/_remote_debugging.h index 94fcba54673da6..56d05ab44e8305 100644 --- a/Modules/_remote_debugging/_remote_debugging.h +++ b/Modules/_remote_debugging/_remote_debugging.h @@ -104,16 +104,6 @@ typedef struct { #ifdef MS_WINDOWS #define STATUS_SUCCESS ((NTSTATUS)0x00000000L) #define STATUS_NO_MORE_ENTRIES ((NTSTATUS)0x8000001AL) -typedef enum _WIN32_THREADSTATE { - WIN32_THREADSTATE_INITIALIZED = 0, - WIN32_THREADSTATE_READY = 1, - WIN32_THREADSTATE_RUNNING = 2, - WIN32_THREADSTATE_STANDBY = 3, - WIN32_THREADSTATE_TERMINATED = 4, - WIN32_THREADSTATE_WAITING = 5, - WIN32_THREADSTATE_TRANSITION = 6, - WIN32_THREADSTATE_UNKNOWN = 7 -} WIN32_THREADSTATE; #endif /* ============================================================================ diff --git a/Modules/_remote_debugging/threads.c b/Modules/_remote_debugging/threads.c index 6e2269aa713258..0805f17f4e7c2f 100644 --- a/Modules/_remote_debugging/threads.c +++ b/Modules/_remote_debugging/threads.c @@ -231,7 +231,8 @@ get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid) { { return THREAD_STATE_UNKNOWN; } - return info.ThreadState == WIN32_THREADSTATE_RUNNING + const ULONG running = 2; + return info.ThreadState == running ? THREAD_STATE_RUNNING : THREAD_STATE_IDLE; #else return THREAD_STATE_UNKNOWN; @@ -359,7 +360,8 @@ unwind_stack_for_thread( STATS_ADD(unwinder, memory_bytes_read, SIZEOF_INTERP_FRAME); } - long tid = GET_MEMBER(long, ts, unwinder->debug_offsets.thread_state.native_thread_id); + unsigned long tid = GET_MEMBER(unsigned long, ts, + unwinder->debug_offsets.thread_state.native_thread_id); // Calculate thread status using flags (always) int status_flags = 0; @@ -556,7 +558,7 @@ unwind_stack_for_thread( } } if (thread_id == NULL) { - thread_id = PyLong_FromLongLong(tid); + thread_id = PyLong_FromUnsignedLong(tid); if (thread_id == NULL) { set_exception_cause(unwinder, PyExc_RuntimeError, "Failed to create thread ID"); goto error; diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 1e2a18f269b2c6..5cc28ab63d4778 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -3532,8 +3532,8 @@ get_dyld_image_addresses(PyObject *self, PyObject *Py_UNUSED(ignored)) goto done; } for (uint32_t index = 0; index < list.count; index++) { - uint64_t address = 0; - memcpy(&address, images + index * (3 * list.pointer_size), list.pointer_size); + uint64_t address = read_dyld_pointer( + images + index * (3 * list.pointer_size), list.pointer_size); PyObject *item = PyLong_FromUnsignedLongLong(address); if (item == NULL) { Py_CLEAR(result); diff --git a/Python/remote_debug.h b/Python/remote_debug.h index 912013270fc15f..7145c357b375f3 100644 --- a/Python/remote_debug.h +++ b/Python/remote_debug.h @@ -359,6 +359,19 @@ typedef struct { size_t pointer_size; } DyldImageList; +static uint64_t +read_dyld_pointer(const void *source, size_t pointer_size) +{ + if (pointer_size == 4) { + uint32_t address; + memcpy(&address, source, sizeof(address)); + return address; + } + uint64_t address; + memcpy(&address, source, sizeof(address)); + return address; +} + static int read_dyld_image_list(proc_handle_t *handle, DyldImageList *list) { @@ -403,8 +416,7 @@ read_dyld_image_list(proc_handle_t *handle, DyldImageList *list) uint32_t version; memcpy(&version, prefix, sizeof(version)); memcpy(&list->count, prefix + 4, sizeof(list->count)); - uint64_t images = 0; - memcpy(&images, prefix + 8, list->pointer_size); + uint64_t images = read_dyld_pointer(prefix + 8, list->pointer_size); if (images > UINTPTR_MAX) { PyErr_SetString(PyExc_RuntimeError, "dyld image address does not fit uintptr_t"); return -1; @@ -483,8 +495,8 @@ search_loaded_images_for_section(proc_handle_t *handle, const char *section, } uintptr_t result = 0; for (uint32_t index = 0; index < list.count; index++) { - uint64_t address = 0; - memcpy(&address, images + index * (3 * list.pointer_size), list.pointer_size); + uint64_t address = read_dyld_pointer( + images + index * (3 * list.pointer_size), list.pointer_size); if (address == 0 || address > UINTPTR_MAX) { continue; } From 4edf584c6bb7d6a1240d40ed429d258075aa60bc Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Fri, 13 Nov 2026 23:22:16 +0000 Subject: [PATCH 03/10] Read target pointer width from older dyld responses --- Python/remote_debug.h | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Python/remote_debug.h b/Python/remote_debug.h index 7145c357b375f3..4abc4d44f5da1d 100644 --- a/Python/remote_debug.h +++ b/Python/remote_debug.h @@ -29,6 +29,7 @@ extern "C" { #include "pyconfig.h" #include "internal/pycore_ceval.h" +#include #ifdef __linux__ # include @@ -375,7 +376,7 @@ read_dyld_pointer(const void *source, size_t pointer_size) static int read_dyld_image_list(proc_handle_t *handle, DyldImageList *list) { - task_dyld_info_data_t info; + task_dyld_info_data_t info = {0}; mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; kern_return_t status = task_info(handle->task, TASK_DYLD_INFO, (task_info_t)&info, &count); @@ -386,6 +387,23 @@ read_dyld_image_list(proc_handle_t *handle, DyldImageList *list) handle->pid, status); return -1; } + if (count < TASK_DYLD_INFO_COUNT) { + /* Older kernels return the address and size without the format. */ + size_t legacy_size = offsetof(task_dyld_info_data_t, all_image_info_format); + if (count * sizeof(natural_t) < legacy_size) { + PyErr_SetString(PyExc_RuntimeError, "Truncated task dyld information"); + return -1; + } + struct proc_bsdinfo process; + int size = proc_pidinfo(handle->pid, PROC_PIDTBSDINFO, 0, + &process, sizeof(process)); + if (size != sizeof(process)) { + PyErr_SetString(PyExc_RuntimeError, "Cannot determine the remote process pointer size"); + return -1; + } + info.all_image_info_format = process.pbi_flags & PROC_FLAG_LP64 + ? TASK_DYLD_ALL_IMAGE_INFO_64 : TASK_DYLD_ALL_IMAGE_INFO_32; + } if (info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_64) { list->pointer_size = 8; } From 4b2bdb11318f2b55b1241dc0fa7324b014705e8c Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Fri, 13 Nov 2026 23:37:39 +0000 Subject: [PATCH 04/10] Keep runtime discovery available during dyld updates --- Lib/test/test_capi/test_remote_debug_macho.py | 6 ++ Modules/_testinternalcapi.c | 23 ++++++++ Python/remote_debug.h | 57 ++++++++++++++++++- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_capi/test_remote_debug_macho.py b/Lib/test/test_capi/test_remote_debug_macho.py index e559cb8b095b09..390ef5e89f4792 100644 --- a/Lib/test/test_capi/test_remote_debug_macho.py +++ b/Lib/test/test_capi/test_remote_debug_macho.py @@ -94,6 +94,12 @@ def test_missing_header_segment(self): @unittest.skipUnless(sys.platform == 'darwin', 'requires dyld') class DyldImageTests(unittest.TestCase): + def test_memory_region_fallback(self): + try: + _testinternalcapi.check_macho_runtime_address() + except PermissionError as exc: + self.skipTest(f"self-inspection is unavailable: {exc}") + def test_loaded_executable(self): ctypes = import_module('ctypes') get_header = ctypes.CDLL(None)._dyld_get_image_header diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 5cc28ab63d4778..8c7e604ea9ee9a 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -3507,6 +3507,28 @@ get_macho_section_address(PyObject *self, PyObject *args) return PyLong_FromUnsignedLongLong(address); } +static PyObject * +check_macho_runtime_address(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + proc_handle_t *handle = PyMem_Calloc(1, sizeof(*handle)); + if (handle == NULL) { + return PyErr_NoMemory(); + } + if (_Py_RemoteDebug_InitProcHandle(handle, getpid()) == 0) { + uintptr_t address = search_memory_regions_for_section( + handle, "PyRuntime", _Py_RemoteDebug_ValidatePyRuntimeCookie); + if (!PyErr_Occurred() && address != (uintptr_t)&_PyRuntime) { + PyErr_SetString(PyExc_AssertionError, "Memory-region lookup returned the wrong runtime"); + } + } + _Py_RemoteDebug_CleanupProcHandle(handle); + PyMem_Free(handle); + if (PyErr_Occurred()) { + return NULL; + } + Py_RETURN_NONE; +} + static PyObject * get_dyld_image_addresses(PyObject *self, PyObject *Py_UNUSED(ignored)) { @@ -3555,6 +3577,7 @@ static PyMethodDef module_functions[] = { #if defined(__APPLE__) && TARGET_OS_OSX {"get_macho_section_address", get_macho_section_address, METH_VARARGS}, {"get_dyld_image_addresses", get_dyld_image_addresses, METH_NOARGS}, + {"check_macho_runtime_address", check_macho_runtime_address, METH_NOARGS}, #endif {"get_configs", get_configs, METH_NOARGS}, {"get_eval_frame_stats", get_eval_frame_stats, METH_NOARGS, NULL}, diff --git a/Python/remote_debug.h b/Python/remote_debug.h index 4abc4d44f5da1d..801b25bd32e8fa 100644 --- a/Python/remote_debug.h +++ b/Python/remote_debug.h @@ -199,7 +199,7 @@ _Py_RemoteDebug_ValidatePyRuntimeCookie(proc_handle_t *handle, uintptr_t address } char buf[sizeof(_Py_Debug_Cookie) - 1]; if (_Py_RemoteDebug_ReadRemoteMemory(handle, address, sizeof(buf), buf) != 0) { - if (!_Py_RemoteDebug_HasPermissionError()) { + if (!_Py_RemoteDebug_IsFatalReadError()) { PyErr_Clear(); } return 0; @@ -502,6 +502,54 @@ copy_dyld_images(proc_handle_t *handle, DyldImageList *list) return NULL; } +static uintptr_t +search_memory_regions_for_section(proc_handle_t *handle, const char *section, + section_validator_t validator) +{ + mach_vm_address_t address = 0; + while (address <= UINTPTR_MAX) { + mach_vm_size_t size = 0; + vm_region_basic_info_data_64_t region; + mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64; + mach_port_t object = MACH_PORT_NULL; + kern_return_t status = mach_vm_region( + handle->task, &address, &size, VM_REGION_BASIC_INFO_64, + (vm_region_info_t)®ion, &count, &object); + if (object != MACH_PORT_NULL) { + mach_port_deallocate(mach_task_self(), object); + } + if (status == KERN_INVALID_ADDRESS) { + return 0; + } + if (status != KERN_SUCCESS) { + PyErr_Format(status == KERN_PROTECTION_FAILURE + ? PyExc_PermissionError : PyExc_RuntimeError, + "Cannot read memory regions for PID %d (Mach error %d)", + handle->pid, status); + return 0; + } + if (address > UINTPTR_MAX) { + return 0; + } + vm_prot_t required = VM_PROT_READ | VM_PROT_EXECUTE; + if ((region.protection & required) == required) { + uintptr_t candidate = search_section_in_memory(handle, address, section); + if (candidate != 0 && (validator == NULL || validator(handle, candidate))) { + return candidate; + } + if (_Py_RemoteDebug_IsFatalReadError()) { + return 0; + } + PyErr_Clear(); + } + if (size == 0 || size > UINT64_MAX - address) { + return 0; + } + address += size; + } + return 0; +} + static uintptr_t search_loaded_images_for_section(proc_handle_t *handle, const char *section, section_validator_t validator) @@ -509,7 +557,12 @@ search_loaded_images_for_section(proc_handle_t *handle, const char *section, DyldImageList list; unsigned char *images = copy_dyld_images(handle, &list); if (images == NULL) { - return 0; + if (_Py_RemoteDebug_IsFatalReadError()) { + return 0; + } + /* A loader update can outlast the snapshot retry window. */ + PyErr_Clear(); + return search_memory_regions_for_section(handle, section, validator); } uintptr_t result = 0; for (uint32_t index = 0; index < list.count; index++) { From 760914c013dc8af650155f09bdd0513602808ad4 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Sat, 14 Nov 2026 00:48:43 +0000 Subject: [PATCH 05/10] Limit the platform rewrite to the profiler implementation --- Lib/test/test_capi/test_remote_debug_macho.py | 118 ------------------ Lib/test/test_external_inspection.py | 40 ------ Modules/_testinternalcapi.c | 96 -------------- 3 files changed, 254 deletions(-) delete mode 100644 Lib/test/test_capi/test_remote_debug_macho.py diff --git a/Lib/test/test_capi/test_remote_debug_macho.py b/Lib/test/test_capi/test_remote_debug_macho.py deleted file mode 100644 index 390ef5e89f4792..00000000000000 --- a/Lib/test/test_capi/test_remote_debug_macho.py +++ /dev/null @@ -1,118 +0,0 @@ -"""Mach-O section lookup with synthetic executable images.""" - -import struct -import sys -import unittest - -from test.support.import_helper import import_module - -_testinternalcapi = import_module('_testinternalcapi') - - -@unittest.skipUnless(sys.platform == 'darwin', 'requires Mach-O headers') -class MachOSectionTests(unittest.TestCase): - BASE = 0x20000000 - OFFSET = 0x2345 - - def lookup(self, image, *, base=BASE, name='PyRuntime'): - return _testinternalcapi.get_macho_section_address(bytes(image), base, name) - - def image(self, *, wide=True, endian='<', data_first=False, name=b'PyRuntime'): - # The preferred VM address differs from the load address. - address = 0x100000000 if wide else 0x1000 - segment_format = endian + ('II16sQQQQiiII' if wide else 'II16sIIIIiiII') - section_format = endian + ('16s16sQQIIIIIIII' if wide else '16s16sIIIIIIIII') - segment_size = struct.calcsize(segment_format) - section_size = struct.calcsize(section_format) - kind = 0x19 if wide else 1 # LC_SEGMENT_64 / LC_SEGMENT - text = struct.pack(segment_format, kind, segment_size, b'__TEXT', - address, 0x1000, 0, 0x1000, 7, 5, 0, 0) - data = struct.pack(segment_format, kind, segment_size + section_size, - b'__DATA', address + 0x2000, 0x1000, - 0x1000, 0x1000, 7, 3, 1, 0) - fields = [name, b'__DATA', address + self.OFFSET, 0x100, - 0, 0, 0, 0, 0, 0, 0] - if wide: - fields.append(0) - data += struct.pack(section_format, *fields) - uuid = struct.pack(endian + 'II16s', 0x1b, 24, bytes(16)) - commands = data + uuid + text if data_first else text + uuid + data - header = [0xfeedfacf if wide else 0xfeedface, - 0x1000007 if wide else 7, 3, 2, 3, len(commands), 0] - if wide: - header.append(0) - return struct.pack(endian + 'I' * len(header), *header) + commands - - def test_thin_images(self): - for wide in (False, True): - for endian in ('<', '>'): - for data_first in (False, True): - with self.subTest(wide=wide, endian=endian, data_first=data_first): - image = self.image(wide=wide, endian=endian, data_first=data_first) - self.assertEqual(self.lookup(image), self.BASE + self.OFFSET) - - def test_missing_section(self): - self.assertEqual(self.lookup(self.image(), name='AsyncioDebug'), 0) - - def test_fixed_width_section_name(self): - name = b'0123456789abcdef' - self.assertEqual(self.lookup(self.image(name=name), name=name.decode()), - self.BASE + self.OFFSET) - self.assertEqual(self.lookup(self.image(name=name), name=name.decode() + 'x'), 0) - - def test_truncated_images(self): - for image in (self.image(), self.image(wide=False)): - for size in range(len(image)): - with self.subTest(length=len(image), size=size): - with self.assertRaises(RuntimeError): - self.lookup(image[:size]) - - def test_invalid_commands(self): - # The first 64-bit segment command follows the 32-byte header. - for field, value in ((16, 1000), # ncmds - (20, 0xffffffff), # sizeofcmds - (36, 0), # cmdsize - (36, 8), # shorter than segment header - (36, 0xffffffff), - (96, 0xffffffff)): # nsects - with self.subTest(field=field, value=value): - image = bytearray(self.image()) - struct.pack_into(' -# if TARGET_OS_OSX -# include "pycore_debug_offsets.h" -# include "../Python/remote_debug.h" - -static PyObject * -get_macho_section_address(PyObject *self, PyObject *args) -{ - const char *image, *section; - Py_ssize_t size; - unsigned long long base; - if (!PyArg_ParseTuple(args, "y#Ks", &image, &size, &base, §ion)) { - return NULL; - } - if (base > UINTPTR_MAX) { - return PyErr_Format(PyExc_OverflowError, "base does not fit uintptr_t"); - } - uintptr_t address = macho_section_address(image, size, base, section); - if (PyErr_Occurred()) { - return NULL; - } - return PyLong_FromUnsignedLongLong(address); -} - -static PyObject * -check_macho_runtime_address(PyObject *self, PyObject *Py_UNUSED(ignored)) -{ - proc_handle_t *handle = PyMem_Calloc(1, sizeof(*handle)); - if (handle == NULL) { - return PyErr_NoMemory(); - } - if (_Py_RemoteDebug_InitProcHandle(handle, getpid()) == 0) { - uintptr_t address = search_memory_regions_for_section( - handle, "PyRuntime", _Py_RemoteDebug_ValidatePyRuntimeCookie); - if (!PyErr_Occurred() && address != (uintptr_t)&_PyRuntime) { - PyErr_SetString(PyExc_AssertionError, "Memory-region lookup returned the wrong runtime"); - } - } - _Py_RemoteDebug_CleanupProcHandle(handle); - PyMem_Free(handle); - if (PyErr_Occurred()) { - return NULL; - } - Py_RETURN_NONE; -} - -static PyObject * -get_dyld_image_addresses(PyObject *self, PyObject *Py_UNUSED(ignored)) -{ - proc_handle_t *handle = PyMem_Calloc(1, sizeof(*handle)); - if (handle == NULL) { - return PyErr_NoMemory(); - } - PyObject *result = NULL; - unsigned char *images = NULL; - if (_Py_RemoteDebug_InitProcHandle(handle, getpid()) < 0) { - goto done; - } - DyldImageList list; - images = copy_dyld_images(handle, &list); - if (images == NULL) { - if (!PyErr_Occurred()) { - result = PyList_New(0); - } - goto done; - } - result = PyList_New(list.count); - if (result == NULL) { - goto done; - } - for (uint32_t index = 0; index < list.count; index++) { - uint64_t address = read_dyld_pointer( - images + index * (3 * list.pointer_size), list.pointer_size); - PyObject *item = PyLong_FromUnsignedLongLong(address); - if (item == NULL) { - Py_CLEAR(result); - goto done; - } - PyList_SET_ITEM(result, index, item); - } -done: - PyMem_Free(images); - _Py_RemoteDebug_CleanupProcHandle(handle); - PyMem_Free(handle); - return result; -} - -# endif -#endif - static PyMethodDef module_functions[] = { -#if defined(__APPLE__) && TARGET_OS_OSX - {"get_macho_section_address", get_macho_section_address, METH_VARARGS}, - {"get_dyld_image_addresses", get_dyld_image_addresses, METH_NOARGS}, - {"check_macho_runtime_address", check_macho_runtime_address, METH_NOARGS}, -#endif {"get_configs", get_configs, METH_NOARGS}, {"get_eval_frame_stats", get_eval_frame_stats, METH_NOARGS, NULL}, {"get_recursion_depth", get_recursion_depth, METH_NOARGS}, From 177c9db46ce99a2964b9901f39ebbc4cea76a8b5 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Sat, 14 Nov 2026 00:56:41 +0000 Subject: [PATCH 06/10] Keep platform inspection helpers local and simplify Mach-O decoding --- Makefile.pre.in | 1 - Modules/_remote_debugging/_remote_debugging.h | 2 - Modules/_remote_debugging/threads.c | 15 +- Python/remote_debug.h | 159 ++++++++++++++++- Python/remote_debug_macho.h | 163 ------------------ 5 files changed, 156 insertions(+), 184 deletions(-) delete mode 100644 Python/remote_debug_macho.h diff --git a/Makefile.pre.in b/Makefile.pre.in index ba0b368ea13803..1d109dac77fa90 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -1284,7 +1284,6 @@ PYTHON_HEADERS= \ $(srcdir)/Include/warnings.h \ $(srcdir)/Include/weakrefobject.h \ $(srcdir)/Python/remote_debug.h \ - $(srcdir)/Python/remote_debug_macho.h \ \ pyconfig.h \ $(PARSER_HEADERS) \ diff --git a/Modules/_remote_debugging/_remote_debugging.h b/Modules/_remote_debugging/_remote_debugging.h index 56d05ab44e8305..6080dc4e381506 100644 --- a/Modules/_remote_debugging/_remote_debugging.h +++ b/Modules/_remote_debugging/_remote_debugging.h @@ -661,8 +661,6 @@ extern int find_running_frame( uintptr_t *frame ); -extern int get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid); - extern PyObject* unwind_stack_for_thread( RemoteUnwinderObject *unwinder, uintptr_t *current_tstate, diff --git a/Modules/_remote_debugging/threads.c b/Modules/_remote_debugging/threads.c index 0805f17f4e7c2f..4aa202d08251d6 100644 --- a/Modules/_remote_debugging/threads.c +++ b/Modules/_remote_debugging/threads.c @@ -158,11 +158,10 @@ find_running_frame( * THREAD STATUS FUNCTIONS * ============================================================================ */ -int -get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid) { +static int +get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) { #if defined(__APPLE__) && TARGET_OS_OSX struct proc_threadinfo thread; - /* This flavor accepts the native ID from pthread_threadid_np(). */ int size = proc_pidinfo(unwinder->handle.pid, PROC_PIDTHREADID64INFO, tid, &thread, sizeof(thread)); if (size != sizeof(thread)) { @@ -174,7 +173,7 @@ get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid) { char stat_path[256]; char buffer[2048] = ""; - snprintf(stat_path, sizeof(stat_path), "/proc/%d/task/%" PRIu64 "/stat", unwinder->handle.pid, tid); + snprintf(stat_path, sizeof(stat_path), "/proc/%d/task/%lu/stat", unwinder->handle.pid, tid); int fd = open(stat_path, O_RDONLY); if (fd == -1) { @@ -210,15 +209,11 @@ get_thread_status(RemoteUnwinderObject *unwinder, uint64_t tid) { return THREAD_STATE_UNKNOWN; } #elif defined(MS_WINDOWS) - if (tid > MAXDWORD) { - return THREAD_STATE_UNKNOWN; - } HANDLE thread = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)tid); if (thread == NULL) { return THREAD_STATE_UNKNOWN; } - /* ThreadSystemThreadInformation is available since Windows 10, but the - * Windows SDK does not name this THREADINFOCLASS value. */ + /* ThreadSystemThreadInformation is not named in the Windows SDK. */ const THREADINFOCLASS system_thread_information = (THREADINFOCLASS)40; SYSTEM_THREAD_INFORMATION info; ULONG size = 0; @@ -453,8 +448,6 @@ unwind_stack_for_thread( status_flags |= THREAD_STATUS_HAS_EXCEPTION; } - // Check CPU status - // Optimization: only check CPU status if needed by mode because it's expensive int cpu_status = THREAD_STATE_UNKNOWN; if (unwinder->mode == PROFILING_MODE_CPU || unwinder->mode == PROFILING_MODE_ALL) { cpu_status = get_thread_status(unwinder, tid); diff --git a/Python/remote_debug.h b/Python/remote_debug.h index 801b25bd32e8fa..6196092dab4bea 100644 --- a/Python/remote_debug.h +++ b/Python/remote_debug.h @@ -29,7 +29,6 @@ extern "C" { #include "pyconfig.h" #include "internal/pycore_ceval.h" -#include #ifdef __linux__ # include @@ -61,6 +60,7 @@ extern "C" { #endif #if defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX +# include "internal/pycore_bitutils.h" # include # include # include @@ -297,7 +297,155 @@ _Py_RemoteDebug_CleanupProcHandle(proc_handle_t *handle) { #if defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX -#include "remote_debug_macho.h" +static uint32_t +macho_u32(uint32_t value, int swap) +{ + return swap ? _Py_bswap32(value) : value; +} + +static uint64_t +macho_u64(uint64_t value, int swap) +{ + return swap ? _Py_bswap64(value) : value; +} + +static uintptr_t +macho_invalid_image(void) +{ + PyErr_SetString(PyExc_RuntimeError, "Invalid or truncated Mach-O image"); + return 0; +} + +static size_t +macho_header_size(uint32_t magic) +{ + switch (magic) { + case MH_MAGIC: + case MH_CIGAM: + return sizeof(struct mach_header); + case MH_MAGIC_64: + case MH_CIGAM_64: + return sizeof(struct mach_header_64); + default: + return 0; + } +} + +static uintptr_t +macho_section_address(const void *image, size_t image_size, + uintptr_t base, const char *name) +{ + if (strlen(name) > 16) { + return 0; + } + struct mach_header header; + if (image_size < sizeof(header)) { + return macho_invalid_image(); + } + memcpy(&header, image, sizeof(header)); + int wide = header.magic == MH_MAGIC_64 || header.magic == MH_CIGAM_64; + int swap = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64; + size_t header_size = macho_header_size(header.magic); + if (header_size == 0 || image_size < header_size) { + return macho_invalid_image(); + } + size_t commands_size = macho_u32(header.sizeofcmds, swap); + if (commands_size > image_size - header_size) { + return macho_invalid_image(); + } + + const unsigned char *cursor = (const unsigned char *)image + header_size; + size_t remaining = commands_size; + uint64_t header_address = 0, section_address = 0; + int have_header = 0, have_section = 0; + uint32_t commands = macho_u32(header.ncmds, swap); + for (uint32_t index = 0; index < commands; index++) { + struct load_command command; + if (remaining < sizeof(command)) { + return macho_invalid_image(); + } + memcpy(&command, cursor, sizeof(command)); + uint32_t kind = macho_u32(command.cmd, swap); + size_t length = macho_u32(command.cmdsize, swap); + if (length < sizeof(command) || length > remaining) { + return macho_invalid_image(); + } + if (kind == (wide ? LC_SEGMENT_64 : LC_SEGMENT)) { + uint64_t address, file_offset, file_size; + uint32_t sections; + size_t segment_size, section_size; + int data_segment; + if (wide) { + struct segment_command_64 segment; + segment_size = sizeof(segment); + section_size = sizeof(struct section_64); + if (length < segment_size) { + return macho_invalid_image(); + } + memcpy(&segment, cursor, segment_size); + address = macho_u64(segment.vmaddr, swap); + file_offset = macho_u64(segment.fileoff, swap); + file_size = macho_u64(segment.filesize, swap); + sections = macho_u32(segment.nsects, swap); + data_segment = strncmp(segment.segname, "__DATA", 16) == 0; + } + else { + struct segment_command segment; + segment_size = sizeof(segment); + section_size = sizeof(struct section); + if (length < segment_size) { + return macho_invalid_image(); + } + memcpy(&segment, cursor, segment_size); + address = macho_u32(segment.vmaddr, swap); + file_offset = macho_u32(segment.fileoff, swap); + file_size = macho_u32(segment.filesize, swap); + sections = macho_u32(segment.nsects, swap); + data_segment = strncmp(segment.segname, "__DATA", 16) == 0; + } + if (sections > (length - segment_size) / section_size) { + return macho_invalid_image(); + } + /* __PAGEZERO has no file contents and does not define the slide. */ + if (file_offset == 0 && file_size != 0) { + header_address = address; + have_header = 1; + } + if (data_segment) { + const unsigned char *record = cursor + segment_size; + for (uint32_t item = 0; item < sections; + item++, record += section_size) + { + if (strncmp((const char *)record, name, 16) != 0) { + continue; + } + if (wide) { + struct section_64 section; + memcpy(§ion, record, sizeof(section)); + section_address = macho_u64(section.addr, swap); + } + else { + struct section section; + memcpy(§ion, record, sizeof(section)); + section_address = macho_u32(section.addr, swap); + } + have_section = 1; + } + } + } + cursor += length; + remaining -= length; + } + if (!have_section) { + return 0; + } + if (!have_header || section_address < header_address || + section_address - header_address > UINTPTR_MAX - base) + { + return macho_invalid_image(); + } + return base + (uintptr_t)(section_address - header_address); +} static uintptr_t 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, } int swap = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64; size_t commands_size = macho_u32(header.sizeofcmds, swap); - /* A corrupt remote header must not trigger an unbounded allocation. */ const size_t max_commands_size = 16 * 1024 * 1024; if (commands_size > max_commands_size || base > UINTPTR_MAX - header_size - commands_size) @@ -334,7 +481,6 @@ search_section_in_memory(proc_handle_t *handle, uintptr_t base, return result; } - static mach_port_t pid_to_task(pid_t pid) { @@ -415,8 +561,7 @@ read_dyld_image_list(proc_handle_t *handle, DyldImageList *list) return -1; } - /* dyld_all_image_infos starts with version, count, and an array pointer. - * Decode the target layout rather than the host's pointer-bearing struct. */ + /* dyld_all_image_infos uses the target's pointer width. */ size_t timestamp_offset = list->pointer_size == 8 ? 184 : 104; size_t size = (size_t)Py_MIN(info.all_image_info_size, timestamp_offset + sizeof(list->timestamp)); @@ -442,7 +587,7 @@ read_dyld_image_list(proc_handle_t *handle, DyldImageList *list) list->images = (uintptr_t)images; list->timestamp = 0; if (version >= 15) { - /* Version 15 added infoArrayChangeTimestamp at these ABI offsets. */ + /* Version 15 added infoArrayChangeTimestamp. */ if (size < timestamp_offset + sizeof(list->timestamp)) { PyErr_SetString(PyExc_RuntimeError, "Truncated dyld image information"); return -1; diff --git a/Python/remote_debug_macho.h b/Python/remote_debug_macho.h deleted file mode 100644 index 03cd6d24f760c8..00000000000000 --- a/Python/remote_debug_macho.h +++ /dev/null @@ -1,163 +0,0 @@ -/* Bounded section lookup in a copy of a remote Mach-O header and commands. */ -#ifndef Py_REMOTE_DEBUG_MACHO_H -#define Py_REMOTE_DEBUG_MACHO_H - -#include -#include "internal/pycore_bitutils.h" - -static uint32_t -macho_u32(uint32_t value, int swap) -{ - return swap ? _Py_bswap32(value) : value; -} - -static uint64_t -macho_u64(uint64_t value, int swap) -{ - return swap ? _Py_bswap64(value) : value; -} - -static uintptr_t -macho_invalid_image(void) -{ - PyErr_SetString(PyExc_RuntimeError, "Invalid or truncated Mach-O image"); - return 0; -} - -static size_t -macho_header_size(uint32_t magic) -{ - switch (magic) { - case MH_MAGIC: - case MH_CIGAM: - return sizeof(struct mach_header); - case MH_MAGIC_64: - case MH_CIGAM_64: - return sizeof(struct mach_header_64); - default: - return 0; - } -} - -static uintptr_t -macho_section_address(const void *image, size_t image_size, - uintptr_t base, const char *name) -{ - if (strlen(name) > 16) { - return 0; - } - const unsigned char *bytes = image; - uint32_t magic; - if (image_size < sizeof(magic)) { - return macho_invalid_image(); - } - memcpy(&magic, bytes, sizeof(magic)); - - int wide = magic == MH_MAGIC_64 || magic == MH_CIGAM_64; - int swap = magic == MH_CIGAM || magic == MH_CIGAM_64; - size_t header_size = macho_header_size(magic); - if (header_size == 0 || image_size < header_size) { - return macho_invalid_image(); - } - struct mach_header header; - memcpy(&header, bytes, sizeof(header)); - size_t commands_size = macho_u32(header.sizeofcmds, swap); - if (commands_size > image_size - header_size) { - return macho_invalid_image(); - } - - const unsigned char *cursor = bytes + header_size; - size_t remaining = commands_size; - uint64_t header_address = 0, section_address = 0; - int have_header = 0, have_section = 0; - uint32_t commands = macho_u32(header.ncmds, swap); - for (uint32_t index = 0; index < commands; index++) { - struct load_command command; - if (remaining < sizeof(command)) { - return macho_invalid_image(); - } - memcpy(&command, cursor, sizeof(command)); - uint32_t kind = macho_u32(command.cmd, swap); - size_t length = macho_u32(command.cmdsize, swap); - if (length < sizeof(command) || length > remaining) { - return macho_invalid_image(); - } - if (kind == (wide ? LC_SEGMENT_64 : LC_SEGMENT)) { - uint64_t address, file_offset, file_size; - uint32_t sections; - size_t segment_size, section_size; - int data_segment; - if (wide) { - struct segment_command_64 segment; - segment_size = sizeof(segment); - section_size = sizeof(struct section_64); - if (length < segment_size) { - return macho_invalid_image(); - } - memcpy(&segment, cursor, segment_size); - address = macho_u64(segment.vmaddr, swap); - file_offset = macho_u64(segment.fileoff, swap); - file_size = macho_u64(segment.filesize, swap); - sections = macho_u32(segment.nsects, swap); - data_segment = strncmp(segment.segname, "__DATA", 16) == 0; - } - else { - struct segment_command segment; - segment_size = sizeof(segment); - section_size = sizeof(struct section); - if (length < segment_size) { - return macho_invalid_image(); - } - memcpy(&segment, cursor, segment_size); - address = macho_u32(segment.vmaddr, swap); - file_offset = macho_u32(segment.fileoff, swap); - file_size = macho_u32(segment.filesize, swap); - sections = macho_u32(segment.nsects, swap); - data_segment = strncmp(segment.segname, "__DATA", 16) == 0; - } - if (sections > (length - segment_size) / section_size) { - return macho_invalid_image(); - } - /* The file-backed segment at offset zero contains the header. - * __PAGEZERO has no file contents and does not define the slide. */ - if (file_offset == 0 && file_size != 0) { - header_address = address; - have_header = 1; - } - if (data_segment) { - const unsigned char *record = cursor + segment_size; - for (uint32_t item = 0; item < sections; - item++, record += section_size) - { - if (strncmp((const char *)record, name, 16) != 0) { - continue; - } - if (wide) { - struct section_64 section; - memcpy(§ion, record, sizeof(section)); - section_address = macho_u64(section.addr, swap); - } - else { - struct section section; - memcpy(§ion, record, sizeof(section)); - section_address = macho_u32(section.addr, swap); - } - have_section = 1; - } - } - } - cursor += length; - remaining -= length; - } - if (!have_section) { - return 0; - } - if (!have_header || section_address < header_address || - section_address - header_address > UINTPTR_MAX - base) - { - return macho_invalid_image(); - } - return base + (uintptr_t)(section_address - header_address); -} - -#endif /* Py_REMOTE_DEBUG_MACHO_H */ From 3a2ecd009eac318b1874c1fdcd7df9797ab9be1f Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Sat, 14 Nov 2026 01:07:08 +0000 Subject: [PATCH 07/10] Name platform inspection limits and clarify discovery functions --- Modules/_remote_debugging/threads.c | 16 ++- Python/remote_debug.h | 155 +++++++++++++++------------- 2 files changed, 97 insertions(+), 74 deletions(-) diff --git a/Modules/_remote_debugging/threads.c b/Modules/_remote_debugging/threads.c index 4aa202d08251d6..66c40d875ab3b3 100644 --- a/Modules/_remote_debugging/threads.c +++ b/Modules/_remote_debugging/threads.c @@ -18,6 +18,14 @@ #include #endif +#ifdef MS_WINDOWS +enum { + /* ThreadSystemThreadInformation is not named in the Windows SDK. */ + WIN32_THREAD_SYSTEM_INFORMATION = 40, + WIN32_THREAD_RUNNING = 2, +}; +#endif + /* Bound traversal of corrupted remote exception chains. */ #define MAX_EXCEPTION_CHAIN_DEPTH (2 << 15) @@ -213,12 +221,11 @@ get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) { if (thread == NULL) { return THREAD_STATE_UNKNOWN; } - /* ThreadSystemThreadInformation is not named in the Windows SDK. */ - const THREADINFOCLASS system_thread_information = (THREADINFOCLASS)40; SYSTEM_THREAD_INFORMATION info; ULONG size = 0; NTSTATUS result = NtQueryInformationThread( - thread, system_thread_information, &info, sizeof(info), &size); + thread, (THREADINFOCLASS)WIN32_THREAD_SYSTEM_INFORMATION, + &info, sizeof(info), &size); CloseHandle(thread); if (result != STATUS_SUCCESS || size != sizeof(info) || (ULONG_PTR)info.ClientId.UniqueProcess != (ULONG_PTR)unwinder->handle.pid || @@ -226,8 +233,7 @@ get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) { { return THREAD_STATE_UNKNOWN; } - const ULONG running = 2; - return info.ThreadState == running + return info.ThreadState == WIN32_THREAD_RUNNING ? THREAD_STATE_RUNNING : THREAD_STATE_IDLE; #else return THREAD_STATE_UNKNOWN; diff --git a/Python/remote_debug.h b/Python/remote_debug.h index 6196092dab4bea..a97fa0a3a4aca2 100644 --- a/Python/remote_debug.h +++ b/Python/remote_debug.h @@ -297,16 +297,30 @@ _Py_RemoteDebug_CleanupProcHandle(proc_handle_t *handle) { #if defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX +enum { + MACHO_NAME_SIZE = 16, + MACHO_MAX_LOAD_COMMAND_BYTES = 16 * 1024 * 1024, + DYLD_MAX_IMAGE_COUNT = 65536, + DYLD_SNAPSHOT_ATTEMPTS = 3, + DYLD_IMAGE_RECORD_FIELDS = 3, + DYLD_IMAGE_COUNT_OFFSET = sizeof(uint32_t), + DYLD_IMAGE_ARRAY_OFFSET = 2 * sizeof(uint32_t), + DYLD_TIMESTAMP_VERSION = 15, + DYLD_TIMESTAMP_OFFSET_32 = 104, + DYLD_TIMESTAMP_OFFSET_64 = 184, + DYLD_INFO_PREFIX_SIZE = DYLD_TIMESTAMP_OFFSET_64 + sizeof(uint64_t), +}; + static uint32_t -macho_u32(uint32_t value, int swap) +macho_u32(uint32_t value, int swap_bytes) { - return swap ? _Py_bswap32(value) : value; + return swap_bytes ? _Py_bswap32(value) : value; } static uint64_t -macho_u64(uint64_t value, int swap) +macho_u64(uint64_t value, int swap_bytes) { - return swap ? _Py_bswap64(value) : value; + return swap_bytes ? _Py_bswap64(value) : value; } static uintptr_t @@ -335,7 +349,7 @@ static uintptr_t macho_section_address(const void *image, size_t image_size, uintptr_t base, const char *name) { - if (strlen(name) > 16) { + if (strlen(name) > MACHO_NAME_SIZE) { return 0; } struct mach_header header; @@ -343,13 +357,13 @@ macho_section_address(const void *image, size_t image_size, return macho_invalid_image(); } memcpy(&header, image, sizeof(header)); - int wide = header.magic == MH_MAGIC_64 || header.magic == MH_CIGAM_64; - int swap = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64; + int is_64_bit = header.magic == MH_MAGIC_64 || header.magic == MH_CIGAM_64; + int swap_bytes = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64; size_t header_size = macho_header_size(header.magic); if (header_size == 0 || image_size < header_size) { return macho_invalid_image(); } - size_t commands_size = macho_u32(header.sizeofcmds, swap); + size_t commands_size = macho_u32(header.sizeofcmds, swap_bytes); if (commands_size > image_size - header_size) { return macho_invalid_image(); } @@ -358,52 +372,52 @@ macho_section_address(const void *image, size_t image_size, size_t remaining = commands_size; uint64_t header_address = 0, section_address = 0; int have_header = 0, have_section = 0; - uint32_t commands = macho_u32(header.ncmds, swap); - for (uint32_t index = 0; index < commands; index++) { + uint32_t command_count = macho_u32(header.ncmds, swap_bytes); + for (uint32_t index = 0; index < command_count; index++) { struct load_command command; if (remaining < sizeof(command)) { return macho_invalid_image(); } memcpy(&command, cursor, sizeof(command)); - uint32_t kind = macho_u32(command.cmd, swap); - size_t length = macho_u32(command.cmdsize, swap); - if (length < sizeof(command) || length > remaining) { + uint32_t command_type = macho_u32(command.cmd, swap_bytes); + size_t command_size = macho_u32(command.cmdsize, swap_bytes); + if (command_size < sizeof(command) || command_size > remaining) { return macho_invalid_image(); } - if (kind == (wide ? LC_SEGMENT_64 : LC_SEGMENT)) { + if (command_type == (is_64_bit ? LC_SEGMENT_64 : LC_SEGMENT)) { uint64_t address, file_offset, file_size; - uint32_t sections; + uint32_t section_count; size_t segment_size, section_size; int data_segment; - if (wide) { + if (is_64_bit) { struct segment_command_64 segment; segment_size = sizeof(segment); section_size = sizeof(struct section_64); - if (length < segment_size) { + if (command_size < segment_size) { return macho_invalid_image(); } memcpy(&segment, cursor, segment_size); - address = macho_u64(segment.vmaddr, swap); - file_offset = macho_u64(segment.fileoff, swap); - file_size = macho_u64(segment.filesize, swap); - sections = macho_u32(segment.nsects, swap); - data_segment = strncmp(segment.segname, "__DATA", 16) == 0; + address = macho_u64(segment.vmaddr, swap_bytes); + file_offset = macho_u64(segment.fileoff, swap_bytes); + file_size = macho_u64(segment.filesize, swap_bytes); + section_count = macho_u32(segment.nsects, swap_bytes); + data_segment = strncmp(segment.segname, "__DATA", MACHO_NAME_SIZE) == 0; } else { struct segment_command segment; segment_size = sizeof(segment); section_size = sizeof(struct section); - if (length < segment_size) { + if (command_size < segment_size) { return macho_invalid_image(); } memcpy(&segment, cursor, segment_size); - address = macho_u32(segment.vmaddr, swap); - file_offset = macho_u32(segment.fileoff, swap); - file_size = macho_u32(segment.filesize, swap); - sections = macho_u32(segment.nsects, swap); - data_segment = strncmp(segment.segname, "__DATA", 16) == 0; + address = macho_u32(segment.vmaddr, swap_bytes); + file_offset = macho_u32(segment.fileoff, swap_bytes); + file_size = macho_u32(segment.filesize, swap_bytes); + section_count = macho_u32(segment.nsects, swap_bytes); + data_segment = strncmp(segment.segname, "__DATA", MACHO_NAME_SIZE) == 0; } - if (sections > (length - segment_size) / section_size) { + if (section_count > (command_size - segment_size) / section_size) { return macho_invalid_image(); } /* __PAGEZERO has no file contents and does not define the slide. */ @@ -413,28 +427,28 @@ macho_section_address(const void *image, size_t image_size, } if (data_segment) { const unsigned char *record = cursor + segment_size; - for (uint32_t item = 0; item < sections; + for (uint32_t item = 0; item < section_count; item++, record += section_size) { - if (strncmp((const char *)record, name, 16) != 0) { + if (strncmp((const char *)record, name, MACHO_NAME_SIZE) != 0) { continue; } - if (wide) { + if (is_64_bit) { struct section_64 section; memcpy(§ion, record, sizeof(section)); - section_address = macho_u64(section.addr, swap); + section_address = macho_u64(section.addr, swap_bytes); } else { struct section section; memcpy(§ion, record, sizeof(section)); - section_address = macho_u32(section.addr, swap); + section_address = macho_u32(section.addr, swap_bytes); } have_section = 1; } } } - cursor += length; - remaining -= length; + cursor += command_size; + remaining -= command_size; } if (!have_section) { return 0; @@ -459,10 +473,9 @@ search_section_in_memory(proc_handle_t *handle, uintptr_t base, if (header_size == 0) { return 0; } - int swap = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64; - size_t commands_size = macho_u32(header.sizeofcmds, swap); - const size_t max_commands_size = 16 * 1024 * 1024; - if (commands_size > max_commands_size || + int swap_bytes = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64; + size_t commands_size = macho_u32(header.sizeofcmds, swap_bytes); + if (commands_size > MACHO_MAX_LOAD_COMMAND_BYTES || base > UINTPTR_MAX - header_size - commands_size) { return macho_invalid_image(); @@ -499,8 +512,8 @@ pid_to_task(pid_t pid) } typedef struct { - uintptr_t address; - uintptr_t images; + uintptr_t info_address; + uintptr_t array_address; uint64_t timestamp; uint32_t count; size_t pointer_size; @@ -509,7 +522,7 @@ typedef struct { static uint64_t read_dyld_pointer(const void *source, size_t pointer_size) { - if (pointer_size == 4) { + if (pointer_size == sizeof(uint32_t)) { uint32_t address; memcpy(&address, source, sizeof(address)); return address; @@ -520,7 +533,7 @@ read_dyld_pointer(const void *source, size_t pointer_size) } static int -read_dyld_image_list(proc_handle_t *handle, DyldImageList *list) +read_dyld_image_metadata(proc_handle_t *handle, DyldImageList *list) { task_dyld_info_data_t info = {0}; mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; @@ -551,10 +564,10 @@ read_dyld_image_list(proc_handle_t *handle, DyldImageList *list) ? TASK_DYLD_ALL_IMAGE_INFO_64 : TASK_DYLD_ALL_IMAGE_INFO_32; } if (info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_64) { - list->pointer_size = 8; + list->pointer_size = sizeof(uint64_t); } else if (info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_32) { - list->pointer_size = 4; + list->pointer_size = sizeof(uint32_t); } else { PyErr_SetString(PyExc_RuntimeError, "Unknown dyld image-info format"); @@ -562,32 +575,33 @@ read_dyld_image_list(proc_handle_t *handle, DyldImageList *list) } /* dyld_all_image_infos uses the target's pointer width. */ - size_t timestamp_offset = list->pointer_size == 8 ? 184 : 104; + size_t timestamp_offset = list->pointer_size == sizeof(uint64_t) + ? DYLD_TIMESTAMP_OFFSET_64 : DYLD_TIMESTAMP_OFFSET_32; size_t size = (size_t)Py_MIN(info.all_image_info_size, timestamp_offset + sizeof(list->timestamp)); - unsigned char prefix[192]; - if (size < 8 + list->pointer_size || + unsigned char prefix[DYLD_INFO_PREFIX_SIZE]; + if (size < DYLD_IMAGE_ARRAY_OFFSET + list->pointer_size || info.all_image_info_addr > UINTPTR_MAX - size) { PyErr_SetString(PyExc_RuntimeError, "Invalid dyld image-info address"); return -1; } - list->address = (uintptr_t)info.all_image_info_addr; - if (_Py_RemoteDebug_ReadRemoteMemory(handle, list->address, size, prefix) < 0) { + list->info_address = (uintptr_t)info.all_image_info_addr; + if (_Py_RemoteDebug_ReadRemoteMemory(handle, list->info_address, size, prefix) < 0) { return -1; } uint32_t version; memcpy(&version, prefix, sizeof(version)); - memcpy(&list->count, prefix + 4, sizeof(list->count)); - uint64_t images = read_dyld_pointer(prefix + 8, list->pointer_size); + memcpy(&list->count, prefix + DYLD_IMAGE_COUNT_OFFSET, sizeof(list->count)); + uint64_t images = read_dyld_pointer( + prefix + DYLD_IMAGE_ARRAY_OFFSET, list->pointer_size); if (images > UINTPTR_MAX) { PyErr_SetString(PyExc_RuntimeError, "dyld image address does not fit uintptr_t"); return -1; } - list->images = (uintptr_t)images; + list->array_address = (uintptr_t)images; list->timestamp = 0; - if (version >= 15) { - /* Version 15 added infoArrayChangeTimestamp. */ + if (version >= DYLD_TIMESTAMP_VERSION) { if (size < timestamp_offset + sizeof(list->timestamp)) { PyErr_SetString(PyExc_RuntimeError, "Truncated dyld image information"); return -1; @@ -598,10 +612,10 @@ read_dyld_image_list(proc_handle_t *handle, DyldImageList *list) } static unsigned char * -copy_dyld_images(proc_handle_t *handle, DyldImageList *list) +snapshot_dyld_images(proc_handle_t *handle, DyldImageList *list) { - for (int attempt = 0; attempt < 3; attempt++) { - if (read_dyld_image_list(handle, list) < 0) { + for (int attempt = 0; attempt < DYLD_SNAPSHOT_ATTEMPTS; attempt++) { + if (read_dyld_image_metadata(handle, list) < 0) { if (_Py_RemoteDebug_IsFatalReadError()) { return NULL; } @@ -609,15 +623,15 @@ copy_dyld_images(proc_handle_t *handle, DyldImageList *list) continue; } /* dyld clears infoArray while it changes the image list. */ - if (list->images == 0) { + if (list->array_address == 0) { continue; } if (list->count == 0) { return NULL; } - size_t record_size = 3 * list->pointer_size; - if (list->count > 65536 || - list->images > UINTPTR_MAX - list->count * record_size) + size_t record_size = DYLD_IMAGE_RECORD_FIELDS * list->pointer_size; + if (list->count > DYLD_MAX_IMAGE_COUNT || + list->array_address > UINTPTR_MAX - list->count * record_size) { PyErr_SetString(PyExc_RuntimeError, "Invalid dyld image array"); return NULL; @@ -629,10 +643,12 @@ copy_dyld_images(proc_handle_t *handle, DyldImageList *list) return NULL; } DyldImageList after; - if (_Py_RemoteDebug_ReadRemoteMemory(handle, list->images, size, images) == 0 && - read_dyld_image_list(handle, &after) == 0 && - after.address == list->address && after.images == list->images && - after.count == list->count && after.timestamp == list->timestamp && + if (_Py_RemoteDebug_ReadRemoteMemory(handle, list->array_address, size, images) == 0 && + read_dyld_image_metadata(handle, &after) == 0 && + after.info_address == list->info_address && + after.array_address == list->array_address && + after.count == list->count && + after.timestamp == list->timestamp && after.pointer_size == list->pointer_size) { return images; @@ -700,7 +716,7 @@ search_loaded_images_for_section(proc_handle_t *handle, const char *section, section_validator_t validator) { DyldImageList list; - unsigned char *images = copy_dyld_images(handle, &list); + unsigned char *images = snapshot_dyld_images(handle, &list); if (images == NULL) { if (_Py_RemoteDebug_IsFatalReadError()) { return 0; @@ -710,9 +726,10 @@ search_loaded_images_for_section(proc_handle_t *handle, const char *section, return search_memory_regions_for_section(handle, section, validator); } uintptr_t result = 0; + size_t record_size = DYLD_IMAGE_RECORD_FIELDS * list.pointer_size; for (uint32_t index = 0; index < list.count; index++) { uint64_t address = read_dyld_pointer( - images + index * (3 * list.pointer_size), list.pointer_size); + images + index * record_size, list.pointer_size); if (address == 0 || address > UINTPTR_MAX) { continue; } From 896f61f4fbc04f73a729fe39e3b3d5e4a7bd8146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Sat, 14 Nov 2026 08:49:23 +0100 Subject: [PATCH 08/10] Simplify macOS runtime discovery; lazy asyncio - Search `dyld.uuidArray` first, then `infoArray`, then memory - Replace dyld snapshot retries and timestamp checks with candidate validation and a fallback to mapped images - Support only little-endian 64-bit Mach-O images: arm64 and x86_64, including Rosetta. - Single 4KiB read for headers and load commands - Remove duplicate `PyRuntime` lookups - Lazy asyncio debug initialization unless it's needed? --- Modules/_remote_debugging/module.c | 27 +- Python/remote_debug.h | 428 ++++++++++------------------- 2 files changed, 142 insertions(+), 313 deletions(-) diff --git a/Modules/_remote_debugging/module.c b/Modules/_remote_debugging/module.c index 3b998fd0200d6e..303af4a4505fea 100644 --- a/Modules/_remote_debugging/module.c +++ b/Modules/_remote_debugging/module.c @@ -384,12 +384,6 @@ _remote_debugging_RemoteUnwinder___init___impl(RemoteUnwinderObject *self, return -1; } - self->runtime_start_address = _Py_RemoteDebug_GetPyRuntimeAddress(&self->handle); - if (self->runtime_start_address == 0) { - set_exception_cause(self, PyExc_RuntimeError, "Failed to get Python runtime address"); - return -1; - } - if (_Py_RemoteDebug_ReadDebugOffsets(&self->handle, &self->runtime_start_address, &self->debug_offsets) < 0) @@ -404,20 +398,8 @@ _remote_debugging_RemoteUnwinder___init___impl(RemoteUnwinderObject *self, return -1; } - // Try to read async debug offsets, but don't fail if they're not available - self->async_debug_offsets_available = 1; - int async_debug_result = read_async_debug(self); - if (async_debug_result == PY_REMOTE_DEBUG_INVALID_ASYNC_DEBUG_OFFSETS) { - return -1; - } - if (async_debug_result < 0) { - if (_Py_RemoteDebug_HasPermissionError()) { - return -1; - } - PyErr_Clear(); - memset(&self->async_debug_offsets, 0, sizeof(self->async_debug_offsets)); - self->async_debug_offsets_available = 0; - } + self->async_debug_offsets_available = 0; + memset(&self->async_debug_offsets, 0, sizeof(self->async_debug_offsets)); if (populate_initial_state_data(all_threads, self, self->runtime_start_address, &self->interpreter_addr ,&self->tstate_addr) < 0) @@ -1392,11 +1374,6 @@ init_runtime_offsets(RuntimeOffsets *offsets, pid_t pid, int debug) set_exception_cause(offsets, PyExc_RuntimeError, "Failed to initialize process handle"); return -1; } - offsets->runtime_start_address = _Py_RemoteDebug_GetPyRuntimeAddress(&offsets->handle); - if (offsets->runtime_start_address == 0) { - set_exception_cause(offsets, PyExc_RuntimeError, "Failed to get Python runtime address"); - goto error; - } if (_Py_RemoteDebug_ReadDebugOffsets(&offsets->handle, &offsets->runtime_start_address, &offsets->debug_offsets) < 0) diff --git a/Python/remote_debug.h b/Python/remote_debug.h index a97fa0a3a4aca2..66fff0fdc6dee9 100644 --- a/Python/remote_debug.h +++ b/Python/remote_debug.h @@ -60,17 +60,13 @@ extern "C" { #endif #if defined(__APPLE__) && defined(TARGET_OS_OSX) && TARGET_OS_OSX -# include "internal/pycore_bitutils.h" -# include +# include # include # include # include # include -# include # include # include -# include -# include #endif #ifdef MS_WINDOWS @@ -301,162 +297,77 @@ enum { MACHO_NAME_SIZE = 16, MACHO_MAX_LOAD_COMMAND_BYTES = 16 * 1024 * 1024, DYLD_MAX_IMAGE_COUNT = 65536, - DYLD_SNAPSHOT_ATTEMPTS = 3, - DYLD_IMAGE_RECORD_FIELDS = 3, - DYLD_IMAGE_COUNT_OFFSET = sizeof(uint32_t), - DYLD_IMAGE_ARRAY_OFFSET = 2 * sizeof(uint32_t), - DYLD_TIMESTAMP_VERSION = 15, - DYLD_TIMESTAMP_OFFSET_32 = 104, - DYLD_TIMESTAMP_OFFSET_64 = 184, - DYLD_INFO_PREFIX_SIZE = DYLD_TIMESTAMP_OFFSET_64 + sizeof(uint64_t), }; -static uint32_t -macho_u32(uint32_t value, int swap_bytes) -{ - return swap_bytes ? _Py_bswap32(value) : value; -} - -static uint64_t -macho_u64(uint64_t value, int swap_bytes) -{ - return swap_bytes ? _Py_bswap64(value) : value; -} - -static uintptr_t -macho_invalid_image(void) -{ - PyErr_SetString(PyExc_RuntimeError, "Invalid or truncated Mach-O image"); - return 0; -} - -static size_t -macho_header_size(uint32_t magic) -{ - switch (magic) { - case MH_MAGIC: - case MH_CIGAM: - return sizeof(struct mach_header); - case MH_MAGIC_64: - case MH_CIGAM_64: - return sizeof(struct mach_header_64); - default: - return 0; - } -} +/* These 64-bit little-endian layouts work on both arm64 and x86_64 (Rosetta). */ +static_assert(sizeof(uintptr_t) == sizeof(uint64_t), "64-bit macOS required"); static uintptr_t macho_section_address(const void *image, size_t image_size, uintptr_t base, const char *name) { - if (strlen(name) > MACHO_NAME_SIZE) { - return 0; - } - struct mach_header header; + struct mach_header_64 header; if (image_size < sizeof(header)) { - return macho_invalid_image(); + return 0; } memcpy(&header, image, sizeof(header)); - int is_64_bit = header.magic == MH_MAGIC_64 || header.magic == MH_CIGAM_64; - int swap_bytes = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64; - size_t header_size = macho_header_size(header.magic); - if (header_size == 0 || image_size < header_size) { - return macho_invalid_image(); - } - size_t commands_size = macho_u32(header.sizeofcmds, swap_bytes); - if (commands_size > image_size - header_size) { - return macho_invalid_image(); + if (header.magic != MH_MAGIC_64 || + header.sizeofcmds > image_size - sizeof(header)) + { + return 0; } - const unsigned char *cursor = (const unsigned char *)image + header_size; - size_t remaining = commands_size; + const unsigned char *cursor = (const unsigned char *)image + sizeof(header); + size_t remaining = header.sizeofcmds; uint64_t header_address = 0, section_address = 0; int have_header = 0, have_section = 0; - uint32_t command_count = macho_u32(header.ncmds, swap_bytes); - for (uint32_t index = 0; index < command_count; index++) { + for (uint32_t index = 0; index < header.ncmds; index++) { struct load_command command; if (remaining < sizeof(command)) { - return macho_invalid_image(); + return 0; } memcpy(&command, cursor, sizeof(command)); - uint32_t command_type = macho_u32(command.cmd, swap_bytes); - size_t command_size = macho_u32(command.cmdsize, swap_bytes); + size_t command_size = command.cmdsize; if (command_size < sizeof(command) || command_size > remaining) { - return macho_invalid_image(); + return 0; } - if (command_type == (is_64_bit ? LC_SEGMENT_64 : LC_SEGMENT)) { - uint64_t address, file_offset, file_size; - uint32_t section_count; - size_t segment_size, section_size; - int data_segment; - if (is_64_bit) { - struct segment_command_64 segment; - segment_size = sizeof(segment); - section_size = sizeof(struct section_64); - if (command_size < segment_size) { - return macho_invalid_image(); - } - memcpy(&segment, cursor, segment_size); - address = macho_u64(segment.vmaddr, swap_bytes); - file_offset = macho_u64(segment.fileoff, swap_bytes); - file_size = macho_u64(segment.filesize, swap_bytes); - section_count = macho_u32(segment.nsects, swap_bytes); - data_segment = strncmp(segment.segname, "__DATA", MACHO_NAME_SIZE) == 0; - } - else { - struct segment_command segment; - segment_size = sizeof(segment); - section_size = sizeof(struct section); - if (command_size < segment_size) { - return macho_invalid_image(); - } - memcpy(&segment, cursor, segment_size); - address = macho_u32(segment.vmaddr, swap_bytes); - file_offset = macho_u32(segment.fileoff, swap_bytes); - file_size = macho_u32(segment.filesize, swap_bytes); - section_count = macho_u32(segment.nsects, swap_bytes); - data_segment = strncmp(segment.segname, "__DATA", MACHO_NAME_SIZE) == 0; + if (command.cmd == LC_SEGMENT_64) { + struct segment_command_64 segment; + if (command_size < sizeof(segment)) { + return 0; } - if (section_count > (command_size - segment_size) / section_size) { - return macho_invalid_image(); + memcpy(&segment, cursor, sizeof(segment)); + if (segment.nsects > + (command_size - sizeof(segment)) / sizeof(struct section_64)) + { + return 0; } /* __PAGEZERO has no file contents and does not define the slide. */ - if (file_offset == 0 && file_size != 0) { - header_address = address; + if (segment.fileoff == 0 && segment.filesize != 0) { + header_address = segment.vmaddr; have_header = 1; } - if (data_segment) { - const unsigned char *record = cursor + segment_size; - for (uint32_t item = 0; item < section_count; - item++, record += section_size) + if (strncmp(segment.segname, "__DATA", MACHO_NAME_SIZE) == 0) { + const unsigned char *record = cursor + sizeof(segment); + for (uint32_t item = 0; item < segment.nsects; + item++, record += sizeof(struct section_64)) { - if (strncmp((const char *)record, name, MACHO_NAME_SIZE) != 0) { - continue; - } - if (is_64_bit) { - struct section_64 section; - memcpy(§ion, record, sizeof(section)); - section_address = macho_u64(section.addr, swap_bytes); + struct section_64 section; + memcpy(§ion, record, sizeof(section)); + if (strncmp(section.sectname, name, MACHO_NAME_SIZE) == 0) { + section_address = section.addr; + have_section = 1; } - else { - struct section section; - memcpy(§ion, record, sizeof(section)); - section_address = macho_u32(section.addr, swap_bytes); - } - have_section = 1; } } } cursor += command_size; remaining -= command_size; } - if (!have_section) { - return 0; - } - if (!have_header || section_address < header_address || + if (!have_header || !have_section || section_address < header_address || section_address - header_address > UINTPTR_MAX - base) { - return macho_invalid_image(); + return 0; } return base + (uintptr_t)(section_address - header_address); } @@ -465,29 +376,45 @@ static uintptr_t search_section_in_memory(proc_handle_t *handle, uintptr_t base, const char *section) { - struct mach_header header; - if (_Py_RemoteDebug_ReadRemoteMemory(handle, base, sizeof(header), &header) < 0) { + /* Most images fit in one read. Stay within the smallest macOS page size. */ + unsigned char prefix[4096]; + size_t prefix_size = sizeof(prefix) - base % sizeof(prefix); + struct mach_header_64 header; + if (prefix_size < sizeof(header) || + _Py_RemoteDebug_ReadRemoteMemory(handle, base, prefix_size, prefix) < 0) + { + if (_Py_RemoteDebug_IsFatalReadError()) { + return 0; + } + PyErr_Clear(); + prefix_size = sizeof(header); + if (_Py_RemoteDebug_ReadRemoteMemory(handle, base, prefix_size, prefix) < 0) { + return 0; + } + } + memcpy(&header, prefix, sizeof(header)); + if (header.magic != MH_MAGIC_64 || + header.sizeofcmds > MACHO_MAX_LOAD_COMMAND_BYTES) + { return 0; } - size_t header_size = macho_header_size(header.magic); - if (header_size == 0) { + size_t size = sizeof(header) + header.sizeofcmds; + if (base > UINTPTR_MAX - size) { return 0; } - int swap_bytes = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64; - size_t commands_size = macho_u32(header.sizeofcmds, swap_bytes); - if (commands_size > MACHO_MAX_LOAD_COMMAND_BYTES || - base > UINTPTR_MAX - header_size - commands_size) - { - return macho_invalid_image(); + if (size <= prefix_size) { + return macho_section_address(prefix, size, base, section); } - size_t size = header_size + commands_size; - void *image = PyMem_Malloc(size); + unsigned char *image = PyMem_Malloc(size); if (image == NULL) { PyErr_NoMemory(); return 0; } + memcpy(image, prefix, prefix_size); uintptr_t result = 0; - if (_Py_RemoteDebug_ReadRemoteMemory(handle, base, size, image) == 0) { + if (_Py_RemoteDebug_ReadRemoteMemory(handle, base + prefix_size, + size - prefix_size, image + prefix_size) == 0) + { result = macho_section_address(image, size, base, section); } PyMem_Free(image); @@ -511,34 +438,13 @@ pid_to_task(pid_t pid) return task; } -typedef struct { - uintptr_t info_address; - uintptr_t array_address; - uint64_t timestamp; - uint32_t count; - size_t pointer_size; -} DyldImageList; - -static uint64_t -read_dyld_pointer(const void *source, size_t pointer_size) -{ - if (pointer_size == sizeof(uint32_t)) { - uint32_t address; - memcpy(&address, source, sizeof(address)); - return address; - } - uint64_t address; - memcpy(&address, source, sizeof(address)); - return address; -} - static int -read_dyld_image_metadata(proc_handle_t *handle, DyldImageList *list) +read_dyld_image_metadata(proc_handle_t *handle, struct dyld_all_image_infos *images) { task_dyld_info_data_t info = {0}; mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT; kern_return_t status = task_info(handle->task, TASK_DYLD_INFO, - (task_info_t)&info, &count); + (task_info_t)&info, &count); if (status != KERN_SUCCESS) { PyErr_Format(status == KERN_PROTECTION_FAILURE ? PyExc_PermissionError : PyExc_RuntimeError, @@ -546,129 +452,41 @@ read_dyld_image_metadata(proc_handle_t *handle, DyldImageList *list) handle->pid, status); return -1; } - if (count < TASK_DYLD_INFO_COUNT) { - /* Older kernels return the address and size without the format. */ - size_t legacy_size = offsetof(task_dyld_info_data_t, all_image_info_format); - if (count * sizeof(natural_t) < legacy_size) { - PyErr_SetString(PyExc_RuntimeError, "Truncated task dyld information"); - return -1; - } - struct proc_bsdinfo process; - int size = proc_pidinfo(handle->pid, PROC_PIDTBSDINFO, 0, - &process, sizeof(process)); - if (size != sizeof(process)) { - PyErr_SetString(PyExc_RuntimeError, "Cannot determine the remote process pointer size"); - return -1; - } - info.all_image_info_format = process.pbi_flags & PROC_FLAG_LP64 - ? TASK_DYLD_ALL_IMAGE_INFO_64 : TASK_DYLD_ALL_IMAGE_INFO_32; - } - if (info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_64) { - list->pointer_size = sizeof(uint64_t); - } - else if (info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_32) { - list->pointer_size = sizeof(uint32_t); - } - else { - PyErr_SetString(PyExc_RuntimeError, "Unknown dyld image-info format"); + if (count < TASK_DYLD_INFO_COUNT || + info.all_image_info_format != TASK_DYLD_ALL_IMAGE_INFO_64) + { + PyErr_SetString(PyExc_RuntimeError, "Unsupported dyld image-info format"); return -1; } - - /* dyld_all_image_infos uses the target's pointer width. */ - size_t timestamp_offset = list->pointer_size == sizeof(uint64_t) - ? DYLD_TIMESTAMP_OFFSET_64 : DYLD_TIMESTAMP_OFFSET_32; - size_t size = (size_t)Py_MIN(info.all_image_info_size, - timestamp_offset + sizeof(list->timestamp)); - unsigned char prefix[DYLD_INFO_PREFIX_SIZE]; - if (size < DYLD_IMAGE_ARRAY_OFFSET + list->pointer_size || + size_t uuid_end = offsetof(struct dyld_all_image_infos, uuidArray) + + sizeof(images->uuidArray); + size_t size = (size_t)Py_MIN(info.all_image_info_size, uuid_end); + if (size < offsetof(struct dyld_all_image_infos, infoArray) + + sizeof(images->infoArray) || info.all_image_info_addr > UINTPTR_MAX - size) { - PyErr_SetString(PyExc_RuntimeError, "Invalid dyld image-info address"); + PyErr_SetString(PyExc_RuntimeError, "Invalid dyld image information"); return -1; } - list->info_address = (uintptr_t)info.all_image_info_addr; - if (_Py_RemoteDebug_ReadRemoteMemory(handle, list->info_address, size, prefix) < 0) { - return -1; - } - uint32_t version; - memcpy(&version, prefix, sizeof(version)); - memcpy(&list->count, prefix + DYLD_IMAGE_COUNT_OFFSET, sizeof(list->count)); - uint64_t images = read_dyld_pointer( - prefix + DYLD_IMAGE_ARRAY_OFFSET, list->pointer_size); - if (images > UINTPTR_MAX) { - PyErr_SetString(PyExc_RuntimeError, "dyld image address does not fit uintptr_t"); + if (_Py_RemoteDebug_ReadRemoteMemory(handle, info.all_image_info_addr, + size, images) < 0) + { return -1; } - list->array_address = (uintptr_t)images; - list->timestamp = 0; - if (version >= DYLD_TIMESTAMP_VERSION) { - if (size < timestamp_offset + sizeof(list->timestamp)) { - PyErr_SetString(PyExc_RuntimeError, "Truncated dyld image information"); - return -1; - } - memcpy(&list->timestamp, prefix + timestamp_offset, sizeof(list->timestamp)); + /* The UUID array was added in version 8 of dyld_all_image_infos. */ + if (images->version < 8 || size < uuid_end) { + images->uuidArray = NULL; + images->uuidArrayCount = 0; } return 0; } -static unsigned char * -snapshot_dyld_images(proc_handle_t *handle, DyldImageList *list) -{ - for (int attempt = 0; attempt < DYLD_SNAPSHOT_ATTEMPTS; attempt++) { - if (read_dyld_image_metadata(handle, list) < 0) { - if (_Py_RemoteDebug_IsFatalReadError()) { - return NULL; - } - PyErr_Clear(); - continue; - } - /* dyld clears infoArray while it changes the image list. */ - if (list->array_address == 0) { - continue; - } - if (list->count == 0) { - return NULL; - } - size_t record_size = DYLD_IMAGE_RECORD_FIELDS * list->pointer_size; - if (list->count > DYLD_MAX_IMAGE_COUNT || - list->array_address > UINTPTR_MAX - list->count * record_size) - { - PyErr_SetString(PyExc_RuntimeError, "Invalid dyld image array"); - return NULL; - } - size_t size = list->count * record_size; - unsigned char *images = PyMem_Malloc(size); - if (images == NULL) { - PyErr_NoMemory(); - return NULL; - } - DyldImageList after; - if (_Py_RemoteDebug_ReadRemoteMemory(handle, list->array_address, size, images) == 0 && - read_dyld_image_metadata(handle, &after) == 0 && - after.info_address == list->info_address && - after.array_address == list->array_address && - after.count == list->count && - after.timestamp == list->timestamp && - after.pointer_size == list->pointer_size) - { - return images; - } - PyMem_Free(images); - if (_Py_RemoteDebug_IsFatalReadError()) { - return NULL; - } - PyErr_Clear(); - } - PyErr_SetString(PyExc_RuntimeError, "The remote dyld image list changed during inspection"); - return NULL; -} - static uintptr_t search_memory_regions_for_section(proc_handle_t *handle, const char *section, section_validator_t validator) { mach_vm_address_t address = 0; - while (address <= UINTPTR_MAX) { + while (1) { mach_vm_size_t size = 0; vm_region_basic_info_data_64_t region; mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64; @@ -689,9 +507,6 @@ search_memory_regions_for_section(proc_handle_t *handle, const char *section, handle->pid, status); return 0; } - if (address > UINTPTR_MAX) { - return 0; - } vm_prot_t required = VM_PROT_READ | VM_PROT_EXECUTE; if ((region.protection & required) == required) { uintptr_t candidate = search_section_in_memory(handle, address, section); @@ -708,29 +523,33 @@ search_memory_regions_for_section(proc_handle_t *handle, const char *section, } address += size; } - return 0; } static uintptr_t -search_loaded_images_for_section(proc_handle_t *handle, const char *section, - section_validator_t validator) +search_dyld_image_array(proc_handle_t *handle, uintptr_t array_address, + size_t count, size_t record_size, + const char *section, section_validator_t validator) { - DyldImageList list; - unsigned char *images = snapshot_dyld_images(handle, &list); + if (array_address == 0 || count == 0 || count > DYLD_MAX_IMAGE_COUNT || + array_address > UINTPTR_MAX - count * record_size) + { + return 0; + } + size_t size = count * record_size; + unsigned char *images = PyMem_Malloc(size); if (images == NULL) { - if (_Py_RemoteDebug_IsFatalReadError()) { - return 0; - } - /* A loader update can outlast the snapshot retry window. */ - PyErr_Clear(); - return search_memory_regions_for_section(handle, section, validator); + PyErr_NoMemory(); + return 0; } uintptr_t result = 0; - size_t record_size = DYLD_IMAGE_RECORD_FIELDS * list.pointer_size; - for (uint32_t index = 0; index < list.count; index++) { - uint64_t address = read_dyld_pointer( - images + index * record_size, list.pointer_size); - if (address == 0 || address > UINTPTR_MAX) { + if (_Py_RemoteDebug_ReadRemoteMemory(handle, array_address, size, images) < 0) { + goto done; + } + for (size_t index = 0; index < count; index++) { + /* Both dyld_image_info and dyld_uuid_info begin with imageLoadAddress. */ + uintptr_t address; + memcpy(&address, images + index * record_size, sizeof(address)); + if (address == 0) { continue; } uintptr_t candidate = search_section_in_memory(handle, address, section); @@ -741,13 +560,46 @@ search_loaded_images_for_section(proc_handle_t *handle, const char *section, if (_Py_RemoteDebug_IsFatalReadError()) { break; } - /* A library can unload after the image-list snapshot. */ + /* A library can unload after the image list is read. */ PyErr_Clear(); } +done: PyMem_Free(images); + if (!_Py_RemoteDebug_IsFatalReadError()) { + PyErr_Clear(); + } return result; } +static uintptr_t +search_loaded_images_for_section(proc_handle_t *handle, const char *section, + section_validator_t validator) +{ + struct dyld_all_image_infos images = {0}; + if (read_dyld_image_metadata(handle, &images) == 0) { + /* Python is normally outside the shared cache: try the shorter list. */ + uintptr_t result = search_dyld_image_array( + handle, (uintptr_t)images.uuidArray, images.uuidArrayCount, + sizeof(struct dyld_uuid_info), section, validator); + if (result != 0 || _Py_RemoteDebug_IsFatalReadError()) { + return result; + } + result = search_dyld_image_array( + handle, (uintptr_t)images.infoArray, images.infoArrayCount, + sizeof(struct dyld_image_info), section, validator); + if (result != 0 || _Py_RemoteDebug_IsFatalReadError()) { + return result; + } + } + if (_Py_RemoteDebug_IsFatalReadError()) { + return 0; + } + /* dyld can leave both lists unavailable while a stopped target is loading + a library. Treat copied lists as hints and fall back to mapped images. */ + PyErr_Clear(); + return search_memory_regions_for_section(handle, section, validator); +} + #endif // (__APPLE__ && defined(TARGET_OS_OSX) && TARGET_OS_OSX) #if defined(__linux__) && HAVE_PROCESS_VM_READV From f0feea370dbb342edc2c3d47bb963ad0ab7c3ff1 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Sat, 14 Nov 2026 18:00:20 +0000 Subject: [PATCH 09/10] Preserve target-exit errors during deferred asyncio discovery Check the known runtime address when optional image lookup fails, and preserve fatal exceptions through async discovery. --- Lib/test/test_external_inspection.py | 22 ++++++++++++++++++++++ Modules/_remote_debugging/asyncio.c | 26 ++++++++++++++++++-------- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_external_inspection.py b/Lib/test/test_external_inspection.py index a3f2eabacf6e88..104efaebd85c16 100644 --- a/Lib/test/test_external_inspection.py +++ b/Lib/test/test_external_inspection.py @@ -4687,6 +4687,28 @@ def test_dead_process_raises_not_degrades(self): else: self.fail("ProcessLookupError never raised for dead process") + def test_dead_process_before_first_async_read(self): + script_body = """\ + import asyncio + sock.sendall(b"ready") + sock.recv(16) + """ + with self._target_process(script_body) as (p, client_socket, _): + _wait_for_signal(client_socket, b"ready") + unwinders = [ + RemoteUnwinder(p.pid, debug=debug) + for debug in (False, True) + ] + + p.kill() + p.wait() + + for debug, unwinder in zip((False, True), unwinders): + for method in ("get_async_stack_trace", "get_all_awaited_by"): + with self.subTest(debug=debug, method=method): + with self.assertRaises(ProcessLookupError): + getattr(unwinder, method)() + @requires_remote_subprocess_debugging() class TestFrameChainLimits(RemoteInspectionTestBase): diff --git a/Modules/_remote_debugging/asyncio.c b/Modules/_remote_debugging/asyncio.c index 9642ed347de23c..11fa40eb3e5f00 100644 --- a/Modules/_remote_debugging/asyncio.c +++ b/Modules/_remote_debugging/asyncio.c @@ -22,7 +22,7 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle) address = search_windows_map_for_section(handle, "AsyncioD", L"_asyncio", NULL); if (address == 0) { - if (!_Py_RemoteDebug_HasPermissionError()) { + if (!_Py_RemoteDebug_IsFatalReadError()) { PyObject *exc = PyErr_GetRaisedException(); PyErr_SetString(PyExc_RuntimeError, "Failed to find the AsyncioDebug section in the process."); _PyErr_ChainExceptions1(exc); @@ -33,7 +33,7 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle) address = search_linux_map_for_section(handle, "AsyncioDebug", "python", NULL); if (address == 0) { - if (!_Py_RemoteDebug_HasPermissionError()) { + if (!_Py_RemoteDebug_IsFatalReadError()) { PyObject *exc = PyErr_GetRaisedException(); PyErr_SetString(PyExc_RuntimeError, "Failed to find the AsyncioDebug section in the process."); _PyErr_ChainExceptions1(exc); @@ -42,7 +42,7 @@ _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t* handle) #elif defined(__APPLE__) && TARGET_OS_OSX address = search_loaded_images_for_section(handle, "AsyncioDebug", NULL); if (address == 0) { - if (!_Py_RemoteDebug_HasPermissionError()) { + if (!_Py_RemoteDebug_IsFatalReadError()) { PyObject *exc = PyErr_GetRaisedException(); PyErr_SetString(PyExc_RuntimeError, "Failed to find the AsyncioDebug section in the process."); _PyErr_ChainExceptions1(exc); @@ -92,12 +92,22 @@ ensure_async_debug_offsets(RemoteUnwinderObject *unwinder) return -1; } if (result < 0) { - if (!_Py_RemoteDebug_HasPermissionError()) { - PyErr_Clear(); - PyErr_SetString(PyExc_RuntimeError, "AsyncioDebug section not available"); - set_exception_cause(unwinder, PyExc_RuntimeError, - "AsyncioDebug section unavailable - asyncio module may not be loaded in target process"); + if (_Py_RemoteDebug_IsFatalReadError()) { + return -1; + } + PyErr_Clear(); + /* Image discovery can fail without reporting that the target exited. + Read a known runtime address before reporting unavailable asyncio. */ + char runtime_byte; + if (_Py_RemoteDebug_ReadRemoteMemory(&unwinder->handle, + unwinder->runtime_start_address, sizeof(runtime_byte), + &runtime_byte) < 0) + { + return -1; } + PyErr_SetString(PyExc_RuntimeError, "AsyncioDebug section not available"); + set_exception_cause(unwinder, PyExc_RuntimeError, + "AsyncioDebug section unavailable - asyncio module may not be loaded in target process"); return -1; } From 40bc6524e772e8447a51376c0c10525d5e2c7e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Sat, 14 Nov 2026 20:14:35 +0100 Subject: [PATCH 10/10] Preserve PyRuntime lookup errors during offset initialization --- Python/remote_debug.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Python/remote_debug.h b/Python/remote_debug.h index 66fff0fdc6dee9..a68e4c6102e5a7 100644 --- a/Python/remote_debug.h +++ b/Python/remote_debug.h @@ -1633,7 +1633,6 @@ _Py_RemoteDebug_ReadDebugOffsets( "Failed to locate PyRuntime address for PID %d", handle->pid); } - _set_debug_exception_cause(PyExc_RuntimeError, "PyRuntime address lookup failed during debug offsets initialization"); return -1; } size_t size = sizeof(struct _Py_DebugOffsets);