Skip to content

Commit 3a2ecd0

Browse files
committed
Name platform inspection limits and clarify discovery functions
1 parent 177c9db commit 3a2ecd0

2 files changed

Lines changed: 97 additions & 74 deletions

File tree

‎Modules/_remote_debugging/threads.c‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
#include <sys/wait.h>
1919
#endif
2020

21+
#ifdef MS_WINDOWS
22+
enum {
23+
/* ThreadSystemThreadInformation is not named in the Windows SDK. */
24+
WIN32_THREAD_SYSTEM_INFORMATION = 40,
25+
WIN32_THREAD_RUNNING = 2,
26+
};
27+
#endif
28+
2129
/* Bound traversal of corrupted remote exception chains. */
2230
#define MAX_EXCEPTION_CHAIN_DEPTH (2 << 15)
2331

@@ -213,21 +221,19 @@ get_thread_status(RemoteUnwinderObject *unwinder, unsigned long tid) {
213221
if (thread == NULL) {
214222
return THREAD_STATE_UNKNOWN;
215223
}
216-
/* ThreadSystemThreadInformation is not named in the Windows SDK. */
217-
const THREADINFOCLASS system_thread_information = (THREADINFOCLASS)40;
218224
SYSTEM_THREAD_INFORMATION info;
219225
ULONG size = 0;
220226
NTSTATUS result = NtQueryInformationThread(
221-
thread, system_thread_information, &info, sizeof(info), &size);
227+
thread, (THREADINFOCLASS)WIN32_THREAD_SYSTEM_INFORMATION,
228+
&info, sizeof(info), &size);
222229
CloseHandle(thread);
223230
if (result != STATUS_SUCCESS || size != sizeof(info) ||
224231
(ULONG_PTR)info.ClientId.UniqueProcess != (ULONG_PTR)unwinder->handle.pid ||
225232
(ULONG_PTR)info.ClientId.UniqueThread != tid)
226233
{
227234
return THREAD_STATE_UNKNOWN;
228235
}
229-
const ULONG running = 2;
230-
return info.ThreadState == running
236+
return info.ThreadState == WIN32_THREAD_RUNNING
231237
? THREAD_STATE_RUNNING : THREAD_STATE_IDLE;
232238
#else
233239
return THREAD_STATE_UNKNOWN;

‎Python/remote_debug.h‎

Lines changed: 86 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,30 @@ _Py_RemoteDebug_CleanupProcHandle(proc_handle_t *handle) {
297297

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

300+
enum {
301+
MACHO_NAME_SIZE = 16,
302+
MACHO_MAX_LOAD_COMMAND_BYTES = 16 * 1024 * 1024,
303+
DYLD_MAX_IMAGE_COUNT = 65536,
304+
DYLD_SNAPSHOT_ATTEMPTS = 3,
305+
DYLD_IMAGE_RECORD_FIELDS = 3,
306+
DYLD_IMAGE_COUNT_OFFSET = sizeof(uint32_t),
307+
DYLD_IMAGE_ARRAY_OFFSET = 2 * sizeof(uint32_t),
308+
DYLD_TIMESTAMP_VERSION = 15,
309+
DYLD_TIMESTAMP_OFFSET_32 = 104,
310+
DYLD_TIMESTAMP_OFFSET_64 = 184,
311+
DYLD_INFO_PREFIX_SIZE = DYLD_TIMESTAMP_OFFSET_64 + sizeof(uint64_t),
312+
};
313+
300314
static uint32_t
301-
macho_u32(uint32_t value, int swap)
315+
macho_u32(uint32_t value, int swap_bytes)
302316
{
303-
return swap ? _Py_bswap32(value) : value;
317+
return swap_bytes ? _Py_bswap32(value) : value;
304318
}
305319

306320
static uint64_t
307-
macho_u64(uint64_t value, int swap)
321+
macho_u64(uint64_t value, int swap_bytes)
308322
{
309-
return swap ? _Py_bswap64(value) : value;
323+
return swap_bytes ? _Py_bswap64(value) : value;
310324
}
311325

312326
static uintptr_t
@@ -335,21 +349,21 @@ static uintptr_t
335349
macho_section_address(const void *image, size_t image_size,
336350
uintptr_t base, const char *name)
337351
{
338-
if (strlen(name) > 16) {
352+
if (strlen(name) > MACHO_NAME_SIZE) {
339353
return 0;
340354
}
341355
struct mach_header header;
342356
if (image_size < sizeof(header)) {
343357
return macho_invalid_image();
344358
}
345359
memcpy(&header, image, sizeof(header));
346-
int wide = header.magic == MH_MAGIC_64 || header.magic == MH_CIGAM_64;
347-
int swap = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64;
360+
int is_64_bit = header.magic == MH_MAGIC_64 || header.magic == MH_CIGAM_64;
361+
int swap_bytes = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64;
348362
size_t header_size = macho_header_size(header.magic);
349363
if (header_size == 0 || image_size < header_size) {
350364
return macho_invalid_image();
351365
}
352-
size_t commands_size = macho_u32(header.sizeofcmds, swap);
366+
size_t commands_size = macho_u32(header.sizeofcmds, swap_bytes);
353367
if (commands_size > image_size - header_size) {
354368
return macho_invalid_image();
355369
}
@@ -358,52 +372,52 @@ macho_section_address(const void *image, size_t image_size,
358372
size_t remaining = commands_size;
359373
uint64_t header_address = 0, section_address = 0;
360374
int have_header = 0, have_section = 0;
361-
uint32_t commands = macho_u32(header.ncmds, swap);
362-
for (uint32_t index = 0; index < commands; index++) {
375+
uint32_t command_count = macho_u32(header.ncmds, swap_bytes);
376+
for (uint32_t index = 0; index < command_count; index++) {
363377
struct load_command command;
364378
if (remaining < sizeof(command)) {
365379
return macho_invalid_image();
366380
}
367381
memcpy(&command, cursor, sizeof(command));
368-
uint32_t kind = macho_u32(command.cmd, swap);
369-
size_t length = macho_u32(command.cmdsize, swap);
370-
if (length < sizeof(command) || length > remaining) {
382+
uint32_t command_type = macho_u32(command.cmd, swap_bytes);
383+
size_t command_size = macho_u32(command.cmdsize, swap_bytes);
384+
if (command_size < sizeof(command) || command_size > remaining) {
371385
return macho_invalid_image();
372386
}
373-
if (kind == (wide ? LC_SEGMENT_64 : LC_SEGMENT)) {
387+
if (command_type == (is_64_bit ? LC_SEGMENT_64 : LC_SEGMENT)) {
374388
uint64_t address, file_offset, file_size;
375-
uint32_t sections;
389+
uint32_t section_count;
376390
size_t segment_size, section_size;
377391
int data_segment;
378-
if (wide) {
392+
if (is_64_bit) {
379393
struct segment_command_64 segment;
380394
segment_size = sizeof(segment);
381395
section_size = sizeof(struct section_64);
382-
if (length < segment_size) {
396+
if (command_size < segment_size) {
383397
return macho_invalid_image();
384398
}
385399
memcpy(&segment, cursor, segment_size);
386-
address = macho_u64(segment.vmaddr, swap);
387-
file_offset = macho_u64(segment.fileoff, swap);
388-
file_size = macho_u64(segment.filesize, swap);
389-
sections = macho_u32(segment.nsects, swap);
390-
data_segment = strncmp(segment.segname, "__DATA", 16) == 0;
400+
address = macho_u64(segment.vmaddr, swap_bytes);
401+
file_offset = macho_u64(segment.fileoff, swap_bytes);
402+
file_size = macho_u64(segment.filesize, swap_bytes);
403+
section_count = macho_u32(segment.nsects, swap_bytes);
404+
data_segment = strncmp(segment.segname, "__DATA", MACHO_NAME_SIZE) == 0;
391405
}
392406
else {
393407
struct segment_command segment;
394408
segment_size = sizeof(segment);
395409
section_size = sizeof(struct section);
396-
if (length < segment_size) {
410+
if (command_size < segment_size) {
397411
return macho_invalid_image();
398412
}
399413
memcpy(&segment, cursor, segment_size);
400-
address = macho_u32(segment.vmaddr, swap);
401-
file_offset = macho_u32(segment.fileoff, swap);
402-
file_size = macho_u32(segment.filesize, swap);
403-
sections = macho_u32(segment.nsects, swap);
404-
data_segment = strncmp(segment.segname, "__DATA", 16) == 0;
414+
address = macho_u32(segment.vmaddr, swap_bytes);
415+
file_offset = macho_u32(segment.fileoff, swap_bytes);
416+
file_size = macho_u32(segment.filesize, swap_bytes);
417+
section_count = macho_u32(segment.nsects, swap_bytes);
418+
data_segment = strncmp(segment.segname, "__DATA", MACHO_NAME_SIZE) == 0;
405419
}
406-
if (sections > (length - segment_size) / section_size) {
420+
if (section_count > (command_size - segment_size) / section_size) {
407421
return macho_invalid_image();
408422
}
409423
/* __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,
413427
}
414428
if (data_segment) {
415429
const unsigned char *record = cursor + segment_size;
416-
for (uint32_t item = 0; item < sections;
430+
for (uint32_t item = 0; item < section_count;
417431
item++, record += section_size)
418432
{
419-
if (strncmp((const char *)record, name, 16) != 0) {
433+
if (strncmp((const char *)record, name, MACHO_NAME_SIZE) != 0) {
420434
continue;
421435
}
422-
if (wide) {
436+
if (is_64_bit) {
423437
struct section_64 section;
424438
memcpy(&section, record, sizeof(section));
425-
section_address = macho_u64(section.addr, swap);
439+
section_address = macho_u64(section.addr, swap_bytes);
426440
}
427441
else {
428442
struct section section;
429443
memcpy(&section, record, sizeof(section));
430-
section_address = macho_u32(section.addr, swap);
444+
section_address = macho_u32(section.addr, swap_bytes);
431445
}
432446
have_section = 1;
433447
}
434448
}
435449
}
436-
cursor += length;
437-
remaining -= length;
450+
cursor += command_size;
451+
remaining -= command_size;
438452
}
439453
if (!have_section) {
440454
return 0;
@@ -459,10 +473,9 @@ search_section_in_memory(proc_handle_t *handle, uintptr_t base,
459473
if (header_size == 0) {
460474
return 0;
461475
}
462-
int swap = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64;
463-
size_t commands_size = macho_u32(header.sizeofcmds, swap);
464-
const size_t max_commands_size = 16 * 1024 * 1024;
465-
if (commands_size > max_commands_size ||
476+
int swap_bytes = header.magic == MH_CIGAM || header.magic == MH_CIGAM_64;
477+
size_t commands_size = macho_u32(header.sizeofcmds, swap_bytes);
478+
if (commands_size > MACHO_MAX_LOAD_COMMAND_BYTES ||
466479
base > UINTPTR_MAX - header_size - commands_size)
467480
{
468481
return macho_invalid_image();
@@ -499,8 +512,8 @@ pid_to_task(pid_t pid)
499512
}
500513

501514
typedef struct {
502-
uintptr_t address;
503-
uintptr_t images;
515+
uintptr_t info_address;
516+
uintptr_t array_address;
504517
uint64_t timestamp;
505518
uint32_t count;
506519
size_t pointer_size;
@@ -509,7 +522,7 @@ typedef struct {
509522
static uint64_t
510523
read_dyld_pointer(const void *source, size_t pointer_size)
511524
{
512-
if (pointer_size == 4) {
525+
if (pointer_size == sizeof(uint32_t)) {
513526
uint32_t address;
514527
memcpy(&address, source, sizeof(address));
515528
return address;
@@ -520,7 +533,7 @@ read_dyld_pointer(const void *source, size_t pointer_size)
520533
}
521534

522535
static int
523-
read_dyld_image_list(proc_handle_t *handle, DyldImageList *list)
536+
read_dyld_image_metadata(proc_handle_t *handle, DyldImageList *list)
524537
{
525538
task_dyld_info_data_t info = {0};
526539
mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
@@ -551,43 +564,44 @@ read_dyld_image_list(proc_handle_t *handle, DyldImageList *list)
551564
? TASK_DYLD_ALL_IMAGE_INFO_64 : TASK_DYLD_ALL_IMAGE_INFO_32;
552565
}
553566
if (info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_64) {
554-
list->pointer_size = 8;
567+
list->pointer_size = sizeof(uint64_t);
555568
}
556569
else if (info.all_image_info_format == TASK_DYLD_ALL_IMAGE_INFO_32) {
557-
list->pointer_size = 4;
570+
list->pointer_size = sizeof(uint32_t);
558571
}
559572
else {
560573
PyErr_SetString(PyExc_RuntimeError, "Unknown dyld image-info format");
561574
return -1;
562575
}
563576

564577
/* dyld_all_image_infos uses the target's pointer width. */
565-
size_t timestamp_offset = list->pointer_size == 8 ? 184 : 104;
578+
size_t timestamp_offset = list->pointer_size == sizeof(uint64_t)
579+
? DYLD_TIMESTAMP_OFFSET_64 : DYLD_TIMESTAMP_OFFSET_32;
566580
size_t size = (size_t)Py_MIN(info.all_image_info_size,
567581
timestamp_offset + sizeof(list->timestamp));
568-
unsigned char prefix[192];
569-
if (size < 8 + list->pointer_size ||
582+
unsigned char prefix[DYLD_INFO_PREFIX_SIZE];
583+
if (size < DYLD_IMAGE_ARRAY_OFFSET + list->pointer_size ||
570584
info.all_image_info_addr > UINTPTR_MAX - size)
571585
{
572586
PyErr_SetString(PyExc_RuntimeError, "Invalid dyld image-info address");
573587
return -1;
574588
}
575-
list->address = (uintptr_t)info.all_image_info_addr;
576-
if (_Py_RemoteDebug_ReadRemoteMemory(handle, list->address, size, prefix) < 0) {
589+
list->info_address = (uintptr_t)info.all_image_info_addr;
590+
if (_Py_RemoteDebug_ReadRemoteMemory(handle, list->info_address, size, prefix) < 0) {
577591
return -1;
578592
}
579593
uint32_t version;
580594
memcpy(&version, prefix, sizeof(version));
581-
memcpy(&list->count, prefix + 4, sizeof(list->count));
582-
uint64_t images = read_dyld_pointer(prefix + 8, list->pointer_size);
595+
memcpy(&list->count, prefix + DYLD_IMAGE_COUNT_OFFSET, sizeof(list->count));
596+
uint64_t images = read_dyld_pointer(
597+
prefix + DYLD_IMAGE_ARRAY_OFFSET, list->pointer_size);
583598
if (images > UINTPTR_MAX) {
584599
PyErr_SetString(PyExc_RuntimeError, "dyld image address does not fit uintptr_t");
585600
return -1;
586601
}
587-
list->images = (uintptr_t)images;
602+
list->array_address = (uintptr_t)images;
588603
list->timestamp = 0;
589-
if (version >= 15) {
590-
/* Version 15 added infoArrayChangeTimestamp. */
604+
if (version >= DYLD_TIMESTAMP_VERSION) {
591605
if (size < timestamp_offset + sizeof(list->timestamp)) {
592606
PyErr_SetString(PyExc_RuntimeError, "Truncated dyld image information");
593607
return -1;
@@ -598,26 +612,26 @@ read_dyld_image_list(proc_handle_t *handle, DyldImageList *list)
598612
}
599613

600614
static unsigned char *
601-
copy_dyld_images(proc_handle_t *handle, DyldImageList *list)
615+
snapshot_dyld_images(proc_handle_t *handle, DyldImageList *list)
602616
{
603-
for (int attempt = 0; attempt < 3; attempt++) {
604-
if (read_dyld_image_list(handle, list) < 0) {
617+
for (int attempt = 0; attempt < DYLD_SNAPSHOT_ATTEMPTS; attempt++) {
618+
if (read_dyld_image_metadata(handle, list) < 0) {
605619
if (_Py_RemoteDebug_IsFatalReadError()) {
606620
return NULL;
607621
}
608622
PyErr_Clear();
609623
continue;
610624
}
611625
/* dyld clears infoArray while it changes the image list. */
612-
if (list->images == 0) {
626+
if (list->array_address == 0) {
613627
continue;
614628
}
615629
if (list->count == 0) {
616630
return NULL;
617631
}
618-
size_t record_size = 3 * list->pointer_size;
619-
if (list->count > 65536 ||
620-
list->images > UINTPTR_MAX - list->count * record_size)
632+
size_t record_size = DYLD_IMAGE_RECORD_FIELDS * list->pointer_size;
633+
if (list->count > DYLD_MAX_IMAGE_COUNT ||
634+
list->array_address > UINTPTR_MAX - list->count * record_size)
621635
{
622636
PyErr_SetString(PyExc_RuntimeError, "Invalid dyld image array");
623637
return NULL;
@@ -629,10 +643,12 @@ copy_dyld_images(proc_handle_t *handle, DyldImageList *list)
629643
return NULL;
630644
}
631645
DyldImageList after;
632-
if (_Py_RemoteDebug_ReadRemoteMemory(handle, list->images, size, images) == 0 &&
633-
read_dyld_image_list(handle, &after) == 0 &&
634-
after.address == list->address && after.images == list->images &&
635-
after.count == list->count && after.timestamp == list->timestamp &&
646+
if (_Py_RemoteDebug_ReadRemoteMemory(handle, list->array_address, size, images) == 0 &&
647+
read_dyld_image_metadata(handle, &after) == 0 &&
648+
after.info_address == list->info_address &&
649+
after.array_address == list->array_address &&
650+
after.count == list->count &&
651+
after.timestamp == list->timestamp &&
636652
after.pointer_size == list->pointer_size)
637653
{
638654
return images;
@@ -700,7 +716,7 @@ search_loaded_images_for_section(proc_handle_t *handle, const char *section,
700716
section_validator_t validator)
701717
{
702718
DyldImageList list;
703-
unsigned char *images = copy_dyld_images(handle, &list);
719+
unsigned char *images = snapshot_dyld_images(handle, &list);
704720
if (images == NULL) {
705721
if (_Py_RemoteDebug_IsFatalReadError()) {
706722
return 0;
@@ -710,9 +726,10 @@ search_loaded_images_for_section(proc_handle_t *handle, const char *section,
710726
return search_memory_regions_for_section(handle, section, validator);
711727
}
712728
uintptr_t result = 0;
729+
size_t record_size = DYLD_IMAGE_RECORD_FIELDS * list.pointer_size;
713730
for (uint32_t index = 0; index < list.count; index++) {
714731
uint64_t address = read_dyld_pointer(
715-
images + index * (3 * list.pointer_size), list.pointer_size);
732+
images + index * record_size, list.pointer_size);
716733
if (address == 0 || address > UINTPTR_MAX) {
717734
continue;
718735
}

0 commit comments

Comments
 (0)