Skip to content

Commit 0508fda

Browse files
committed
[lldb] Extend SWIG SBProcess interface with WriteMemoryAsCString method
This patch tries to address an interoperability issue when writing python string into the process memory. Since the python string is not null-terminated, it would still be written to memory however, when trying to read it again with `SBProcess::ReadCStringFromMemory`, the memory read would fail, since the read string doens't contain a null-terminator, and therefore is not a valid C string. To address that, this patch extends the `SBProcess` SWIG interface to expose a new `WriteMemoryAsCString` method that is only exposed to the SWIG target language. That method checks that the buffer to write is null-terminated and otherwise, it appends a null byte at the end of it. Signed-off-by: Med Ismail Bennani <[email protected]>
1 parent ef46a32 commit 0508fda

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

lldb/test/API/python_api/process/TestProcessAPI.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,32 @@ def test_write_memory(self):
212212

213213
self.assertEqual(cstring, message)
214214

215+
# Get the SBValue for the global variable 'my_cstring'.
216+
val = frame.FindValue("my_cstring", lldb.eValueTypeVariableGlobal)
217+
self.DebugSBValue(val)
218+
219+
addr = val.AddressOf().GetValueAsUnsigned()
220+
221+
# Write an empty string to memory
222+
bytes_written = process.WriteMemoryAsCString(addr, "", error)
223+
self.assertEqual(bytes_written, 0)
224+
if not error.Success():
225+
self.fail("SBProcess.WriteMemoryAsCString() failed")
226+
227+
message = "Hello!"
228+
bytes_written = process.WriteMemoryAsCString(addr, message, error)
229+
self.assertEqual(bytes_written, len(message) + 1)
230+
if not error.Success():
231+
self.fail("SBProcess.WriteMemoryAsCString() failed")
232+
233+
cstring = process.ReadCStringFromMemory(
234+
val.AddressOf().GetValueAsUnsigned(), 256, error)
235+
if not error.Success():
236+
self.fail("SBProcess.ReadCStringFromMemory() failed")
237+
238+
self.assertEqual(cstring, message)
239+
240+
215241
def test_access_my_int(self):
216242
"""Test access 'my_int' using Python SBProcess.GetByteOrder() and other APIs."""
217243
self.build()

0 commit comments

Comments
 (0)