diff options
author | Raphael Isemann <teemperor@gmail.com> | 2020-01-14 09:15:40 +0100 |
---|---|---|
committer | Raphael Isemann <teemperor@gmail.com> | 2020-01-14 09:34:32 +0100 |
commit | 61b6a4e82653e1209126404d33ad20a268f55db1 (patch) | |
tree | a7a2054959705bdfd87d931163ffb26ef70f4ee2 /lldb/packages/Python/lldbsuite/test/python_api/thread/TestThreadAPI.py | |
parent | 440ce5164f52a6b7cdf70322cc1c95656cac9aa9 (diff) | |
download | bcm5719-llvm-61b6a4e82653e1209126404d33ad20a268f55db1.tar.gz bcm5719-llvm-61b6a4e82653e1209126404d33ad20a268f55db1.zip |
[lldb] Fix that SBThread.GetStopDescription is returning strings with uninitialized memory at the end.
Summary:
`SBThread.GetStopDescription` is a curious API as it takes a buffer length as a parameter that specifies
how many bytes the buffer we pass has. Then we fill the buffer until the specified length (or the length
of the stop description string) and return the string length. If the buffer is a nullptr however, we instead
return how many bytes we would have written to the buffer so that the user can allocate a buffer with
the right size and pass that size to a subsequent `SBThread.GetStopDescription` call.
Funnily enough, it is not possible to pass a nullptr via the Python SWIG bindings, so that might be the
first API in LLDB that is not only hard to use correctly but impossible to use correctly. The only way to
call this function via Python is to throw in a large size limit that is hopefully large enough to contain the
stop description (otherwise we only get the truncated stop description).
Currently passing a size limit that is smaller than the returned stop description doesn't cause the
Python bindings to return the stop description but instead the truncated stop description + uninitialized characters
at the end of the string. The reason for this is that we return the result of `snprintf` from the method
which returns the amount of bytes that *would* have been written (which is larger than the buffer).
This causes our Python bindings to return a string that is as large as full stop description but the
buffer that has been filled is only as large as the passed in buffer size.
This patch fixes this issue by just recalculating the string length in our buffer instead of relying on the wrong
return value. We also have to do this in a new type map as the old type map is also used for all methods
with the given argument pair `char *dst, size_t dst_len` (e.g. SBProcess.GetSTDOUT`). These methods have
different semantics for these arguments and don't null-terminate the returned buffer (they instead return the
size in bytes) so we can't change the existing typemap without breaking them.
Reviewers: labath, jingham
Reviewed By: labath
Subscribers: clayborg, shafik, abidh, JDevlieghere, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D72086
Diffstat (limited to 'lldb/packages/Python/lldbsuite/test/python_api/thread/TestThreadAPI.py')
-rw-r--r-- | lldb/packages/Python/lldbsuite/test/python_api/thread/TestThreadAPI.py | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/lldb/packages/Python/lldbsuite/test/python_api/thread/TestThreadAPI.py b/lldb/packages/Python/lldbsuite/test/python_api/thread/TestThreadAPI.py index 21c32fb257f..91241385293 100644 --- a/lldb/packages/Python/lldbsuite/test/python_api/thread/TestThreadAPI.py +++ b/lldb/packages/Python/lldbsuite/test/python_api/thread/TestThreadAPI.py @@ -122,14 +122,20 @@ class ThreadAPITestCase(TestBase): self.assertTrue( thread.IsValid(), "There should be a thread stopped due to breakpoint") - #self.runCmd("process status") - - # Due to the typemap magic (see lldb.swig), we pass in an (int)length to GetStopDescription - # and expect to get a Python string as the return object! - # The 100 is just an arbitrary number specifying the buffer size. - stop_description = thread.GetStopDescription(100) - self.expect(stop_description, exe=False, - startstr='breakpoint') + + # Get the stop reason. GetStopDescription expects that we pass in the size of the description + # we expect plus an additional byte for the null terminator. + + # Test with a buffer that is exactly as large as the expected stop reason. + self.assertEqual("breakpoint 1.1", thread.GetStopDescription(len('breakpoint 1.1') + 1)) + + # Test some smaller buffer sizes. + self.assertEqual("breakpoint", thread.GetStopDescription(len('breakpoint') + 1)) + self.assertEqual("break", thread.GetStopDescription(len('break') + 1)) + self.assertEqual("b", thread.GetStopDescription(len('b') + 1)) + + # Test that we can pass in a much larger size and still get the right output. + self.assertEqual("breakpoint 1.1", thread.GetStopDescription(len('breakpoint 1.1') + 100)) def step_out_of_malloc_into_function_b(self, exe_name): """Test Python SBThread.StepOut() API to step out of a malloc call where the call site is at function b().""" |