This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Extend SWIG SBProcess interface with WriteMemoryAsCString method
ClosedPublic

Authored by mib on Feb 16 2023, 3:25 PM.

Details

Summary

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 <medismail.bennani@gmail.com>

Diff Detail

Event Timeline

mib created this revision.Feb 16 2023, 3:25 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 16 2023, 3:25 PM
mib requested review of this revision.Feb 16 2023, 3:25 PM
bulbazord requested changes to this revision.Feb 16 2023, 3:57 PM
bulbazord added a reviewer: jingham.

I'm alright with the implementation, but I think you should add some documentation here. Also, what do you think of the name WriteMemoryAsCString? To make it clear that whatever you are passing will be written to memory as a cstring.

lldb/bindings/interface/SBProcessExtensions.i
5–9
This revision now requires changes to proceed.Feb 16 2023, 3:57 PM
mib updated this revision to Diff 498201.Feb 16 2023, 5:16 PM
mib retitled this revision from [lldb] Extend SWIG SBProcess interface with WriteCStringToMemory method to [lldb] Extend SWIG SBProcess interface with WriteMemoryAsCString method.
mib edited the summary of this revision. (Show Details)

LGTM. @jingham what do you think?

labath added a subscriber: labath.Feb 20 2023, 7:46 AM
labath added inline comments.
lldb/bindings/interface/SBProcessExtensions.i
11

I think this will fail if the string is empty ("").

Somewhat relatedly, what if this appended the nul byte unconditionally (so it kinda behaved like std::string, which is always nul-terminated, but the nul byte is not counted as the length of the string)?

I see no tests. If there were tests, one of them could be for "" and then you would know whether Pavel was right or not...

mib updated this revision to Diff 502332.Mar 3 2023, 6:52 PM

Handle empty string case and add tests

bulbazord accepted this revision.Mar 3 2023, 6:55 PM

Thanks for adding those tests.

This revision is now accepted and ready to land.Mar 3 2023, 6:55 PM