Skip to content

Commit d8b1ec6

Browse files
committed
Update CurrentProcess.virtual_alloc to match Winprocess.virtual_alloc parameters + tests
1 parent dd9a809 commit d8b1ec6

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

tests/test_process.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,40 @@ def test_read_wstring_end_page_current_process(self):
170170
current_proc.read_memory(waddr, 0x20) # Check that Reading dumbly fails
171171
assert current_proc.read_wstring(waddr) == test_string
172172

173+
def test_virtual_alloc_current_process(self):
174+
current_proc = windows.current_process
175+
addr1 = current_proc.virtual_alloc(0x1000, prot=gdef.PAGE_EXECUTE_READWRITE)
176+
meminfo = current_proc.query_memory(addr1)
177+
assert meminfo.AllocationBase == addr1
178+
assert meminfo.AllocationProtect == gdef.PAGE_EXECUTE_READWRITE
179+
assert meminfo.Type == gdef.MEM_PRIVATE
180+
assert meminfo.State == gdef.MEM_COMMIT
181+
assert meminfo.Protect == gdef.PAGE_EXECUTE_READWRITE
182+
del meminfo
183+
184+
addr2 = current_proc.virtual_alloc(0x1000, prot=gdef.PAGE_READONLY, type=gdef.MEM_RESERVE)
185+
meminfo = current_proc.query_memory(addr2)
186+
assert meminfo.AllocationBase == addr2
187+
assert meminfo.AllocationProtect == gdef.PAGE_READONLY
188+
assert meminfo.Type == gdef.MEM_PRIVATE
189+
assert meminfo.State == gdef.MEM_RESERVE
190+
191+
def test_virtual_alloc(self, proc32_64):
192+
addr1 = proc32_64.virtual_alloc(0x1000, prot=gdef.PAGE_EXECUTE_READWRITE)
193+
meminfo = proc32_64.query_memory(addr1)
194+
assert meminfo.AllocationBase == addr1
195+
assert meminfo.AllocationProtect == gdef.PAGE_EXECUTE_READWRITE
196+
assert meminfo.Type == gdef.MEM_PRIVATE
197+
assert meminfo.State == gdef.MEM_COMMIT
198+
assert meminfo.Protect == gdef.PAGE_EXECUTE_READWRITE
199+
200+
addr2 = proc32_64.virtual_alloc(0x1000, prot=gdef.PAGE_READONLY, type=gdef.MEM_RESERVE)
201+
meminfo = proc32_64.query_memory(addr2)
202+
assert meminfo.AllocationBase == addr2
203+
assert meminfo.AllocationProtect == gdef.PAGE_READONLY
204+
assert meminfo.Type == gdef.MEM_PRIVATE
205+
assert meminfo.State == gdef.MEM_RESERVE
206+
173207
def test_query_memory(self, proc32_64):
174208
addr = proc32_64.virtual_alloc(0x2000, prot=gdef.PAGE_EXECUTE_READWRITE)
175209
proc32_64.virtual_protect(addr, 0x2000, gdef.PAGE_READONLY)

windows/winobject/process.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,13 +720,14 @@ def bitness(self):
720720
"""
721721
return ctypes.sizeof(gdef.PVOID) * 8 # byte to bits
722722

723-
def virtual_alloc(self, size, prot=PAGE_EXECUTE_READWRITE):
723+
724+
def virtual_alloc(self, size, prot=PAGE_EXECUTE_READWRITE, addr=None, type=gdef.MEM_COMMIT):
724725
"""Allocate memory in the process
725726
726727
:return: The address of the allocated memory
727728
:rtype: :class:`int`
728729
"""
729-
return winproxy.VirtualAlloc(dwSize=size, flProtect=prot)
730+
return winproxy.VirtualAlloc(lpAddress=addr, dwSize=size, flAllocationType=type, flProtect=prot)
730731

731732
def virtual_free(self, addr):
732733
"""Free memory in the process by virtual_alloc"""

0 commit comments

Comments
 (0)