| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,9 +37,6 @@ PyAPI_FUNC(uintptr_t) _Py_RemoteDebug_GetAsyncioDebugAddress(proc_handle_t *hand | |||
| 37 | 37 | // Read memory from a remote process | |
| 38 | 38 | PyAPI_FUNC(int) _Py_RemoteDebug_ReadRemoteMemory(proc_handle_t *handle, uintptr_t remote_address, size_t len, void* dst); | |
| 39 | 39 | ||
| 40 | - // Write memory to a remote process | ||
| 41 | - PyAPI_FUNC(int) _Py_RemoteDebug_WriteRemoteMemory(proc_handle_t *handle, uintptr_t remote_address, size_t len, const void* src); | ||
| 42 | - | ||
| 43 | 40 | // Read debug offsets from a remote process | |
| 44 | 41 | PyAPI_FUNC(int) _Py_RemoteDebug_ReadDebugOffsets(proc_handle_t *handle, uintptr_t *runtime_start_address, _Py_DebugOffsets* debug_offsets); | |
| 45 | 42 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -80,7 +80,62 @@ read_memory(proc_handle_t *handle, uint64_t remote_address, size_t len, void* ds | |||
| 80 | 80 | static int | |
| 81 | 81 | write_memory(proc_handle_t *handle, uintptr_t remote_address, size_t len, const void* src) | |
| 82 | 82 | { | |
| 83 | - return _Py_RemoteDebug_WriteRemoteMemory(handle, remote_address, len, src); | ||
| 83 | + #ifdef MS_WINDOWS | ||
| 84 | + SIZE_T written = 0; | ||
| 85 | + SIZE_T result = 0; | ||
| 86 | + do { | ||
| 87 | + if (!WriteProcessMemory(handle->hProcess, (LPVOID)(remote_address + result), (const char*)src + result, len - result, &written)) { | ||
| 88 | + PyErr_SetFromWindowsErr(0); | ||
| 89 | + return -1; | ||
| 90 | + } | ||
| 91 | + result += written; | ||
| 92 | + } while (result < len); | ||
| 93 | + return 0; | ||
| 94 | + #elif defined(__linux__) && HAVE_PROCESS_VM_READV | ||
| 95 | + struct iovec local[1]; | ||
| 96 | + struct iovec remote[1]; | ||
| 97 | + Py_ssize_t result = 0; | ||
| 98 | + Py_ssize_t written = 0; | ||
| 99 | + | ||
| 100 | + do { | ||
| 101 | + local[0].iov_base = (void*)((char*)src + result); | ||
| 102 | + local[0].iov_len = len - result; | ||
| 103 | + remote[0].iov_base = (void*)((char*)remote_address + result); | ||
| 104 | + remote[0].iov_len = len - result; | ||
| 105 | + | ||
| 106 | + written = process_vm_writev(handle->pid, local, 1, remote, 1, 0); | ||
| 107 | + if (written < 0) { | ||
| 108 | + PyErr_SetFromErrno(PyExc_OSError); | ||
| 109 | + return -1; | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + result += written; | ||
| 113 | + } while ((size_t)written != local[0].iov_len); | ||
| 114 | + return 0; | ||
| 115 | + #elif defined(__APPLE__) && TARGET_OS_OSX | ||
| 116 | + kern_return_t kr = mach_vm_write( | ||
| 117 | + pid_to_task(handle->pid), | ||
| 118 | + (mach_vm_address_t)remote_address, | ||
| 119 | + (vm_offset_t)src, | ||
| 120 | + (mach_msg_type_number_t)len); | ||
| 121 | + | ||
| 122 | + if (kr != KERN_SUCCESS) { | ||
| 123 | + switch (kr) { | ||
| 124 | + case KERN_PROTECTION_FAILURE: | ||
| 125 | + PyErr_SetString(PyExc_PermissionError, "Not enough permissions to write memory"); | ||
| 126 | + break; | ||
| 127 | + case KERN_INVALID_ARGUMENT: | ||
| 128 | + PyErr_SetString(PyExc_PermissionError, "Invalid argument to mach_vm_write"); | ||
| 129 | + break; | ||
| 130 | + default: | ||
| 131 | + PyErr_Format(PyExc_RuntimeError, "Unknown error writing memory: %d", (int)kr); | ||
| 132 | + } | ||
| 133 | + return -1; | ||
| 134 | + } | ||
| 135 | + return 0; | ||
| 136 | + #else | ||
| 137 | + Py_UNREACHABLE(); | ||
| 138 | + #endif | ||
| 84 | 139 | } | |
| 85 | 140 | ||
| 86 | 141 | static int | |
@@ -679,68 +734,6 @@ _Py_RemoteDebug_ReadRemoteMemory(proc_handle_t *handle, uintptr_t remote_address | |||
| 679 | 734 | #endif | |
| 680 | 735 | } | |
| 681 | 736 | ||
| 682 | - // Platform-independent memory write function | ||
| 683 | - int | ||
| 684 | - _Py_RemoteDebug_WriteRemoteMemory(proc_handle_t *handle, uintptr_t remote_address, size_t len, const void* src) | ||
| 685 | - { | ||
| 686 | - #ifdef MS_WINDOWS | ||
| 687 | - SIZE_T written = 0; | ||
| 688 | - SIZE_T result = 0; | ||
| 689 | - do { | ||
| 690 | - if (!WriteProcessMemory(handle->hProcess, (LPVOID)(remote_address + result), (const char*)src + result, len - result, &written)) { | ||
| 691 | - PyErr_SetFromWindowsErr(0); | ||
| 692 | - return -1; | ||
| 693 | - } | ||
| 694 | - result += written; | ||
| 695 | - } while (result < len); | ||
| 696 | - return 0; | ||
| 697 | - #elif defined(__linux__) && HAVE_PROCESS_VM_READV | ||
| 698 | - struct iovec local[1]; | ||
| 699 | - struct iovec remote[1]; | ||
| 700 | - Py_ssize_t result = 0; | ||
| 701 | - Py_ssize_t written = 0; | ||
| 702 | - | ||
| 703 | - do { | ||
| 704 | - local[0].iov_base = (void*)((char*)src + result); | ||
| 705 | - local[0].iov_len = len - result; | ||
| 706 | - remote[0].iov_base = (void*)((char*)remote_address + result); | ||
| 707 | - remote[0].iov_len = len - result; | ||
| 708 | - | ||
| 709 | - written = process_vm_writev(handle->pid, local, 1, remote, 1, 0); | ||
| 710 | - if (written < 0) { | ||
| 711 | - PyErr_SetFromErrno(PyExc_OSError); | ||
| 712 | - return -1; | ||
| 713 | - } | ||
| 714 | - | ||
| 715 | - result += written; | ||
| 716 | - } while ((size_t)written != local[0].iov_len); | ||
| 717 | - return 0; | ||
| 718 | - #elif defined(__APPLE__) && TARGET_OS_OSX | ||
| 719 | - kern_return_t kr = mach_vm_write( | ||
| 720 | - pid_to_task(handle->pid), | ||
| 721 | - (mach_vm_address_t)remote_address, | ||
| 722 | - (vm_offset_t)src, | ||
| 723 | - (mach_msg_type_number_t)len); | ||
| 724 | - | ||
| 725 | - if (kr != KERN_SUCCESS) { | ||
| 726 | - switch (kr) { | ||
| 727 | - case KERN_PROTECTION_FAILURE: | ||
| 728 | - PyErr_SetString(PyExc_PermissionError, "Not enough permissions to write memory"); | ||
| 729 | - break; | ||
| 730 | - case KERN_INVALID_ARGUMENT: | ||
| 731 | - PyErr_SetString(PyExc_PermissionError, "Invalid argument to mach_vm_write"); | ||
| 732 | - break; | ||
| 733 | - default: | ||
| 734 | - PyErr_Format(PyExc_RuntimeError, "Unknown error writing memory: %d", (int)kr); | ||
| 735 | - } | ||
| 736 | - return -1; | ||
| 737 | - } | ||
| 738 | - return 0; | ||
| 739 | - #else | ||
| 740 | - Py_UNREACHABLE(); | ||
| 741 | - #endif | ||
| 742 | - } | ||
| 743 | - | ||
| 744 | 737 | static int | |
| 745 | 738 | is_prerelease_version(uint64_t version) | |
| 746 | 739 | { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments