From 23fdc5fd48105993cb3e8c1ff3a3384f09b008b3 Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Thu, 13 Feb 2025 01:28:34 +0500 Subject: [PATCH 1/7] Set exception in read_memory if not supported on the platform --- Modules/_testexternalinspection.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Modules/_testexternalinspection.c b/Modules/_testexternalinspection.c index 22074c81b7405f..66d9dee2d17059 100644 --- a/Modules/_testexternalinspection.c +++ b/Modules/_testexternalinspection.c @@ -481,6 +481,9 @@ read_memory(pid_t pid, uintptr_t remote_address, size_t len, void* dst) } total_bytes_read = len; #else + PyErr_SetString( + PyExc_RuntimeError, + "Memory reading is not supported on this platform"); return -1; #endif return total_bytes_read; From 5e15349906bd59f07087e17ce10617174800dc62 Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Thu, 13 Feb 2025 19:09:37 +0500 Subject: [PATCH 2/7] Add more exceptions to _testexternalinspection --- Modules/_testexternalinspection.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Modules/_testexternalinspection.c b/Modules/_testexternalinspection.c index e8f8fb4a0d1ca6..7bad442e99962d 100644 --- a/Modules/_testexternalinspection.c +++ b/Modules/_testexternalinspection.c @@ -133,6 +133,10 @@ return_section_address( cmd = (struct segment_command_64*)((void*)cmd + cmd->cmdsize); } + + // We should not be here, but if we are there, we should say about this + PyErr_SetString( + PyExc_RuntimeError, "Cannot find section address.\n"); return 0; } @@ -188,6 +192,7 @@ search_section_in_file( munmap(map, fs.st_size); if (close(fd) != 0) { + // This might hide one of the above exceptions, maybe we should chain them? PyErr_SetFromErrno(PyExc_OSError); } return result; @@ -217,7 +222,9 @@ search_map_for_section(pid_t pid, const char* secname, const char* substr) { mach_port_t proc_ref = pid_to_task(pid); if (proc_ref == 0) { - PyErr_SetString(PyExc_PermissionError, "Cannot get task for PID"); + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_PermissionError, "Cannot get task for PID"); + } return 0; } @@ -260,6 +267,8 @@ search_map_for_section(pid_t pid, const char* secname, const char* substr) { address += size; } + + PyErr_SetString(PyExc_RuntimeError, "mach_vm_region failed"); return 0; } @@ -306,6 +315,7 @@ find_map_start_address(pid_t pid, char* result_filename, const char* map) if (!match_found) { map_filename[0] = '\0'; + PyErr_SetString(PyExc_RuntimeError, "Cannot find map start address for map: %s", map); } return result_address; @@ -401,6 +411,7 @@ search_map_for_section(pid_t pid, const char* secname, const char* map) static uintptr_t search_map_for_section(pid_t pid, const char* secname, const char* map) { + PyErr_SetString(PyExc_NotImplementedError, "Not supported not this platform"); return 0; } #endif @@ -792,6 +803,9 @@ parse_coro_chain( pid, coro_address + offsets->gen_object.gi_frame_state, &gi_frame_state); + if (err) { + return -1; + } if (gi_frame_state == FRAME_SUSPENDED_YIELD_FROM) { char owner; From 67072ccac539fb5c9f68da2257c304e3ebc7fec8 Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Thu, 13 Feb 2025 19:12:00 +0500 Subject: [PATCH 3/7] Fix PyErr_Format call --- Modules/_testexternalinspection.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_testexternalinspection.c b/Modules/_testexternalinspection.c index 7bad442e99962d..d3dda1192ad981 100644 --- a/Modules/_testexternalinspection.c +++ b/Modules/_testexternalinspection.c @@ -315,7 +315,7 @@ find_map_start_address(pid_t pid, char* result_filename, const char* map) if (!match_found) { map_filename[0] = '\0'; - PyErr_SetString(PyExc_RuntimeError, "Cannot find map start address for map: %s", map); + PyErr_Format(PyExc_RuntimeError, "Cannot find map start address for map: %s", map); } return result_address; From b8c800c25bc39414e317458e0b4d230bea15ae0c Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Thu, 13 Feb 2025 19:33:43 +0500 Subject: [PATCH 4/7] Fix small typo --- Modules/_testexternalinspection.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_testexternalinspection.c b/Modules/_testexternalinspection.c index d3dda1192ad981..764b5f6b93f74a 100644 --- a/Modules/_testexternalinspection.c +++ b/Modules/_testexternalinspection.c @@ -411,7 +411,7 @@ search_map_for_section(pid_t pid, const char* secname, const char* map) static uintptr_t search_map_for_section(pid_t pid, const char* secname, const char* map) { - PyErr_SetString(PyExc_NotImplementedError, "Not supported not this platform"); + PyErr_SetString(PyExc_NotImplementedError, "Not supported on this platform"); return 0; } #endif From 6dfc0f03e0d7682f15bbfec73b8a1eed3a585cf2 Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Mon, 17 Feb 2025 22:02:47 +0500 Subject: [PATCH 5/7] Update Modules/_testexternalinspection.c Co-authored-by: Victor Stinner --- Modules/_testexternalinspection.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_testexternalinspection.c b/Modules/_testexternalinspection.c index 764b5f6b93f74a..c6774ce3a07607 100644 --- a/Modules/_testexternalinspection.c +++ b/Modules/_testexternalinspection.c @@ -268,7 +268,7 @@ search_map_for_section(pid_t pid, const char* secname, const char* substr) { address += size; } - PyErr_SetString(PyExc_RuntimeError, "mach_vm_region failed"); + PyErr_SetString(PyExc_RuntimeError, "mach_vm_region failed to find the section"); return 0; } From e9ac75bc5040b71da5bfb522ed6e0ece1dd101e7 Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Mon, 17 Feb 2025 22:50:32 +0500 Subject: [PATCH 6/7] Remove redundant check of pid_to_task result --- Modules/_testexternalinspection.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/Modules/_testexternalinspection.c b/Modules/_testexternalinspection.c index c6774ce3a07607..a36724e2de7acb 100644 --- a/Modules/_testexternalinspection.c +++ b/Modules/_testexternalinspection.c @@ -222,9 +222,6 @@ search_map_for_section(pid_t pid, const char* secname, const char* substr) { mach_port_t proc_ref = pid_to_task(pid); if (proc_ref == 0) { - if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_PermissionError, "Cannot get task for PID"); - } return 0; } From 4fde8de22647d3f6bd61e6bd0cb558e87c9655fc Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Mon, 17 Feb 2025 22:53:43 +0500 Subject: [PATCH 7/7] Fit lines to 80 columns --- Modules/_testexternalinspection.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Modules/_testexternalinspection.c b/Modules/_testexternalinspection.c index a36724e2de7acb..fcb18aeef08c39 100644 --- a/Modules/_testexternalinspection.c +++ b/Modules/_testexternalinspection.c @@ -192,7 +192,8 @@ search_section_in_file( munmap(map, fs.st_size); if (close(fd) != 0) { - // This might hide one of the above exceptions, maybe we should chain them? + // This might hide one of the above exceptions, maybe we + // should chain them? PyErr_SetFromErrno(PyExc_OSError); } return result; @@ -265,7 +266,8 @@ search_map_for_section(pid_t pid, const char* secname, const char* substr) { address += size; } - PyErr_SetString(PyExc_RuntimeError, "mach_vm_region failed to find the section"); + PyErr_SetString(PyExc_RuntimeError, + "mach_vm_region failed to find the section"); return 0; } @@ -312,7 +314,8 @@ find_map_start_address(pid_t pid, char* result_filename, const char* map) if (!match_found) { map_filename[0] = '\0'; - PyErr_Format(PyExc_RuntimeError, "Cannot find map start address for map: %s", map); + PyErr_Format(PyExc_RuntimeError, + "Cannot find map start address for map: %s", map); } return result_address; @@ -408,7 +411,8 @@ search_map_for_section(pid_t pid, const char* secname, const char* map) static uintptr_t search_map_for_section(pid_t pid, const char* secname, const char* map) { - PyErr_SetString(PyExc_NotImplementedError, "Not supported on this platform"); + PyErr_SetString(PyExc_NotImplementedError, + "Not supported on this platform"); return 0; } #endif @@ -427,7 +431,8 @@ get_py_runtime(pid_t pid) static uintptr_t get_async_debug(pid_t pid) { - uintptr_t result = search_map_for_section(pid, "AsyncioDebug", "_asyncio.cpython"); + uintptr_t result = search_map_for_section(pid, "AsyncioDebug", + "_asyncio.cpython"); if (result == 0 && !PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "Cannot find AsyncioDebug section"); }