diff options
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") |
