diff options
| author | Peter S. Housel <housel@acm.org> | 2021-07-16 00:42:28 +0200 |
|---|---|---|
| committer | Raphael Isemann <teemperor@gmail.com> | 2021-07-16 00:45:22 +0200 |
| commit | 2e7ec447cc7eab89a72413ad91a897049f551c56 (patch) | |
| tree | 754a7a521322950c8a6488ca6281671658e9aedd /lldb/test/API/python_api/process/TestProcessAPI.py | |
| parent | bba8a76b8736fcf005ebbd0a4fb789a22eadf9ba (diff) | |
[lldb] Add AllocateMemory/DeallocateMemory to the SBProcess API
This change adds AllocateMemory and DeallocateMemory methods to the SBProcess
API, so that clients can allocate and deallocate memory blocks within the
process being debugged (for storing JIT-compiled code or other uses).
(I am developing a debugger + REPL using the API; it will need to store
JIT-compiled code within the target.)
Reviewed By: clayborg, jingham
Differential Revision: https://reviews.llvm.org/D105389
Diffstat (limited to 'lldb/test/API/python_api/process/TestProcessAPI.py')
| -rw-r--r-- | lldb/test/API/python_api/process/TestProcessAPI.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lldb/test/API/python_api/process/TestProcessAPI.py b/lldb/test/API/python_api/process/TestProcessAPI.py index b7efc7e4affa..790d794890d6 100644 --- a/lldb/test/API/python_api/process/TestProcessAPI.py +++ b/lldb/test/API/python_api/process/TestProcessAPI.py @@ -398,3 +398,58 @@ class ProcessAPITestCase(TestBase): "Process effective group ID is invalid") process_info.GetParentProcessID() + + def test_allocate_deallocate_memory(self): + """Test Python SBProcess.AllocateMemory() and SBProcess.DeallocateMemory() APIs.""" + self.build() + (target, process, main_thread, main_breakpoint) = lldbutil.run_to_source_breakpoint( + self, "// Set break point at this line", lldb.SBFileSpec("main.cpp")) + + # Allocate a block of memory in the target process + error = lldb.SBError() + addr = process.AllocateMemory(16384, lldb.ePermissionsReadable, error) + if not error.Success() or addr == lldb.LLDB_INVALID_ADDRESS: + self.fail("SBProcess.AllocateMemory() failed") + + # Now use WriteMemory() API to write 'a' into the allocated + # memory. Note that the debugger can do this even though the + # block is not set writable. + result = process.WriteMemory(addr, 'a', error) + if not error.Success() or result != 1: + self.fail("SBProcess.WriteMemory() failed") + + # Read from the memory location. This time it should be 'a'. + # Due to the typemap magic (see lldb.swig), we pass in 1 to ReadMemory and + # expect to get a Python string as the result object! + content = process.ReadMemory(addr, 1, error) + if not error.Success(): + self.fail("SBProcess.ReadMemory() failed") + if self.TraceOn(): + print("memory content:", content) + + self.expect( + content, + "Result from SBProcess.ReadMemory() matches our expected output: 'a'", + exe=False, + startstr=b'a') + + # Verify that the process itself can read the allocated memory + frame = main_thread.GetFrameAtIndex(0) + val = frame.EvaluateExpression( + "test_read(reinterpret_cast<char *>({:#x}))".format(addr)) + self.expect(val.GetValue(), + "Result of test_read() matches expected output 'a'", + exe=False, + startstr="'a'") + + # Verify that the process cannot write into the block + val = frame.EvaluateExpression( + "test_write(reinterpret_cast<char *>({:#x}), 'b')".format(addr)) + if val.GetError().Success(): + self.fail( + "test_write() to allocated memory without write permission unexpectedly succeeded") + + # Deallocate the memory + error = process.DeallocateMemory(addr) + if not error.Success(): + self.fail("SBProcess.DeallocateMemory() failed") |
