Jacek Caban : dbghelp: Introduce read_process_memory helper.

Alexandre Julliard julliard at winehq.org
Mon Apr 6 15:53:23 CDT 2020


Module: wine
Branch: master
Commit: 26f5bfdd4d071a91a38b25c0064ed5ea48993249
URL:    https://source.winehq.org/git/wine.git/?a=commit;h=26f5bfdd4d071a91a38b25c0064ed5ea48993249

Author: Jacek Caban <jacek at codeweavers.com>
Date:   Mon Apr  6 19:41:58 2020 +0200

dbghelp: Introduce read_process_memory helper.

Signed-off-by: Jacek Caban <jacek at codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 dlls/dbghelp/cpu_x86_64.c      |  4 +---
 dlls/dbghelp/dbghelp_private.h |  5 +++++
 dlls/dbghelp/elf_module.c      | 19 ++++++-------------
 dlls/dbghelp/macho_module.c    | 16 +++++++---------
 dlls/dbghelp/minidump.c        |  8 ++------
 5 files changed, 21 insertions(+), 31 deletions(-)

diff --git a/dlls/dbghelp/cpu_x86_64.c b/dlls/dbghelp/cpu_x86_64.c
index 13de1fd0e4..c2789d0d71 100644
--- a/dlls/dbghelp/cpu_x86_64.c
+++ b/dlls/dbghelp/cpu_x86_64.c
@@ -949,9 +949,7 @@ static BOOL x86_64_fetch_minidump_module(struct dump_context* dc, unsigned index
                     /* we need to read into the other process */
                     /* rtf = (RUNTIME_FUNCTION*)(module->module.BaseOfImage + (rtf->UnwindData & ~1)); */
                 }
-                if (ReadProcessMemory(dc->process->handle,
-                                      (void*)(dc->modules[index].base + rtf->UnwindData),
-                                      &ui, sizeof(ui), NULL))
+                if (read_process_memory(dc->process, dc->modules[index].base + rtf->UnwindData, &ui, sizeof(ui)))
                     minidump_add_memory_block(dc, dc->modules[index].base + rtf->UnwindData,
                                               FIELD_OFFSET(UNWIND_INFO, UnwindCode) + ui.CountOfCodes * sizeof(UNWIND_CODE), 0);
                 rtf++;
diff --git a/dlls/dbghelp/dbghelp_private.h b/dlls/dbghelp/dbghelp_private.h
index 73b9cf9c8c..cdd074709d 100644
--- a/dlls/dbghelp/dbghelp_private.h
+++ b/dlls/dbghelp/dbghelp_private.h
@@ -421,6 +421,11 @@ struct process
     BOOL                        is_64bit;
 };
 
+static inline BOOL read_process_memory(const struct process *process, UINT64 addr, void *buf, size_t size)
+{
+    return ReadProcessMemory(process->handle, (void*)(UINT_PTR)addr, buf, size, NULL);
+}
+
 struct line_info
 {
     ULONG_PTR                   is_first : 1,
diff --git a/dlls/dbghelp/elf_module.c b/dlls/dbghelp/elf_module.c
index 23cb8a54b7..96c7484cef 100644
--- a/dlls/dbghelp/elf_module.c
+++ b/dlls/dbghelp/elf_module.c
@@ -1486,9 +1486,7 @@ static BOOL elf_enum_modules_internal(const struct process* pcs,
             UINT64 l_next, l_prev;
         } lm;
 
-        if (!pcs->dbg_hdr_addr ||
-            !ReadProcessMemory(pcs->handle, (void*)pcs->dbg_hdr_addr,
-                               &dbg_hdr, sizeof(dbg_hdr), NULL))
+        if (!pcs->dbg_hdr_addr || !read_process_memory(pcs, pcs->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
             return FALSE;
 
         /* Now walk the linked list.  In all known ELF implementations,
@@ -1498,12 +1496,11 @@ static BOOL elf_enum_modules_internal(const struct process* pcs,
          */
         for (lm_addr = dbg_hdr.r_map; lm_addr; lm_addr = lm.l_next)
         {
-            if (!ReadProcessMemory(pcs->handle, (void*)lm_addr, &lm, sizeof(lm), NULL))
+            if (!read_process_memory(pcs, lm_addr, &lm, sizeof(lm)))
                 return FALSE;
 
             if (lm.l_prev && /* skip first entry, normally debuggee itself */
-                lm.l_name &&
-                ReadProcessMemory(pcs->handle, (void*)(ULONG_PTR)lm.l_name, bufstr, sizeof(bufstr), NULL))
+                lm.l_name && read_process_memory(pcs, lm.l_name, bufstr, sizeof(bufstr)))
             {
                 bufstr[sizeof(bufstr) - 1] = '\0';
                 MultiByteToWideChar(CP_UNIXCP, 0, bufstr, -1, bufstrW, ARRAY_SIZE(bufstrW));
@@ -1531,9 +1528,7 @@ static BOOL elf_enum_modules_internal(const struct process* pcs,
             UINT32 l_next, l_prev;
         } lm;
 
-        if (!pcs->dbg_hdr_addr ||
-            !ReadProcessMemory(pcs->handle, (void*)pcs->dbg_hdr_addr,
-                           &dbg_hdr, sizeof(dbg_hdr), NULL))
+        if (!pcs->dbg_hdr_addr || !read_process_memory(pcs, pcs->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
             return FALSE;
 
         /* Now walk the linked list.  In all known ELF implementations,
@@ -1543,13 +1538,11 @@ static BOOL elf_enum_modules_internal(const struct process* pcs,
          */
         for (lm_addr = dbg_hdr.r_map; lm_addr; lm_addr = lm.l_next)
         {
-            if (!ReadProcessMemory(pcs->handle, (void*)lm_addr, &lm, sizeof(lm), NULL))
+            if (!read_process_memory(pcs, lm_addr, &lm, sizeof(lm)))
                 return FALSE;
 
             if (lm.l_prev && /* skip first entry, normally debuggee itself */
-                lm.l_name &&
-                ReadProcessMemory(pcs->handle, (void *)(DWORD_PTR)lm.l_name,
-                                  bufstr, sizeof(bufstr), NULL))
+                lm.l_name && read_process_memory(pcs, lm.l_name, bufstr, sizeof(bufstr)))
             {
                 bufstr[sizeof(bufstr) - 1] = '\0';
                 MultiByteToWideChar(CP_UNIXCP, 0, bufstr, -1, bufstrW, ARRAY_SIZE(bufstrW));
diff --git a/dlls/dbghelp/macho_module.c b/dlls/dbghelp/macho_module.c
index ff0072d5a9..8b32a190d8 100644
--- a/dlls/dbghelp/macho_module.c
+++ b/dlls/dbghelp/macho_module.c
@@ -1349,7 +1349,7 @@ static BOOL image_uses_split_segs(struct process* process, ULONG_PTR load_addr)
         UINT32 target_magic = (process->is_64bit) ? MACHO_MH_MAGIC_64 : MACHO_MH_MAGIC_32;
         struct macho_header header;
 
-        if (ReadProcessMemory(process->handle, (void*)load_addr, &header, FIELD_OFFSET(struct macho_header, reserved), NULL) &&
+        if (read_process_memory(process, load_addr, &header, FIELD_OFFSET(struct macho_header, reserved)) &&
             header.magic == target_magic && header.cputype == target_cpu &&
             header.flags & MACHO_DYLD_IN_SHARED_CACHE)
         {
@@ -1629,8 +1629,7 @@ static BOOL macho_enum_modules_internal(const struct process* pcs,
     else
         len = sizeof(image_infos.infos32);
     if (!pcs->dbg_hdr_addr ||
-        !ReadProcessMemory(pcs->handle, (void*)pcs->dbg_hdr_addr,
-                           &image_infos, len, NULL))
+        !read_process_memory(pcs, pcs->dbg_hdr_addr, &image_infos, len))
         goto done;
     if (!pcs->is_64bit)
     {
@@ -1649,8 +1648,7 @@ static BOOL macho_enum_modules_internal(const struct process* pcs,
     len *= image_infos.infos64.infoArrayCount;
     info_array = HeapAlloc(GetProcessHeap(), 0, len);
     if (!info_array ||
-        !ReadProcessMemory(pcs->handle, (void*)image_infos.infos64.infoArray,
-                           info_array, len, NULL))
+        !read_process_memory(pcs, image_infos.infos64.infoArray, info_array, len))
         goto done;
     TRACE("... read image infos\n");
 
@@ -1666,7 +1664,7 @@ static BOOL macho_enum_modules_internal(const struct process* pcs,
             info.imageFilePath = info32->imageFilePath;
         }
         if (info.imageFilePath &&
-            ReadProcessMemory(pcs->handle, (void*)info.imageFilePath, bufstr, sizeof(bufstr), NULL))
+            read_process_memory(pcs, info.imageFilePath, bufstr, sizeof(bufstr)))
         {
             bufstr[sizeof(bufstr) - 1] = '\0';
             TRACE("[%d] image file %s\n", i, debugstr_a(bufstr));
@@ -1850,7 +1848,7 @@ static BOOL macho_search_loader(struct process* pcs, struct macho_info* macho_in
         len = sizeof(image_infos.infos64);
     else
         len = sizeof(image_infos.infos32);
-    if (ReadProcessMemory(pcs->handle, (void*)pcs->dbg_hdr_addr, &image_infos, len, NULL))
+    if (read_process_memory(pcs, pcs->dbg_hdr_addr, &image_infos, len))
     {
         if (pcs->is_64bit)
             len = sizeof(image_info.info64);
@@ -1862,7 +1860,7 @@ static BOOL macho_search_loader(struct process* pcs, struct macho_info* macho_in
             len = sizeof(image_info.info32);
         }
         if (image_infos.infos64.infoArray && image_infos.infos64.infoArrayCount &&
-            ReadProcessMemory(pcs->handle, (void*)image_infos.infos64.infoArray, &image_info, len, NULL))
+            read_process_memory(pcs, image_infos.infos64.infoArray, &image_info, len))
         {
             if (!pcs->is_64bit)
             {
@@ -1872,7 +1870,7 @@ static BOOL macho_search_loader(struct process* pcs, struct macho_info* macho_in
             }
             for (len = sizeof(path); image_info.info64.imageFilePath && len > 0; len /= 2)
             {
-                if (ReadProcessMemory(pcs->handle, (void*)image_info.info64.imageFilePath, path, len, NULL))
+                if (read_process_memory(pcs, image_info.info64.imageFilePath, path, len))
                 {
                     path[len - 1] = 0;
                     got_path = TRUE;
diff --git a/dlls/dbghelp/minidump.c b/dlls/dbghelp/minidump.c
index f934aab83c..cdee1a8b2e 100644
--- a/dlls/dbghelp/minidump.c
+++ b/dlls/dbghelp/minidump.c
@@ -832,9 +832,7 @@ static unsigned         dump_memory_info(struct dump_context* dc)
         for (pos = 0; pos < dc->mem[i].size; pos += sizeof(tmp))
         {
             len = min(dc->mem[i].size - pos, sizeof(tmp));
-            if (ReadProcessMemory(dc->process->handle,
-                                  (void*)(DWORD_PTR)(dc->mem[i].base + pos),
-                                  tmp, len, NULL))
+            if (read_process_memory(dc->process, dc->mem[i].base + pos, tmp, len))
                 WriteFile(dc->hFile, tmp, len, &written, NULL);
         }
         dc->rva += mdMem.Memory.DataSize;
@@ -890,9 +888,7 @@ static unsigned         dump_memory64_info(struct dump_context* dc)
         for (pos = 0; pos < dc->mem64[i].size; pos += sizeof(tmp))
         {
             len = min(dc->mem64[i].size - pos, sizeof(tmp));
-            if (ReadProcessMemory(dc->process->handle,
-                                  (void*)(ULONG_PTR)(dc->mem64[i].base + pos),
-                                  tmp, len, NULL))
+            if (read_process_memory(dc->process, dc->mem64[i].base + pos, tmp, len))
                 WriteFile(dc->hFile, tmp, len, &written, NULL);
         }
         filepos.QuadPart += mdMem64.DataSize;




More information about the wine-cvs mailing list