This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Fix [some] leaks in python bindings
ClosedPublic

Authored by labath on Nov 19 2021, 9:50 AM.

Details

Summary

Using an lldb_private object in the bindings involves three steps

  • wrapping the object in it's lldb::SB variant
  • using swig to convert/wrap that to a PyObject
  • wrapping *that* in a lldb_private::python::PythonObject

Our SBTypeToSWIGWrapper was only handling the middle part. This doesn't
just result in increased boilerplate in the callers, but is also a
functionality problem, as it's very hard to get the lifetime of of all
of these objects right. Most of the callers are creating the SB object
(step 1) on the stack, which means that we end up with dangling python
objects after the function terminates. Most of the time this isn't a
problem, because the python code does not need to persist the objects.
However, there are legitimate cases where they can do it (and even if
the use case is not completely legitimate, crashing is not the best
response to that).

For this reason, some of our code creates the SB object on the heap, but
it has another problem -- it never gets cleaned up.

This patch begins to add a new function (ToSWIGWrapper), which does all
of the three steps, while properly taking care of ownership. In the
first step, I have converted most of the leaky code (except for
SBStructuredData, which needs a bit more work).

Diff Detail

Event Timeline

labath requested review of this revision.Nov 19 2021, 9:50 AM
labath created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptNov 19 2021, 9:50 AM
labath added inline comments.Nov 19 2021, 9:55 AM
lldb/bindings/python/python-swigsafecast.swig
61–67

I have two of these, because one of the functions wants to modify the SB object before passing it to python. It's not a super common use case, but I think there will be a couple more of these. I have also considered having a function returning a (PythonObject, SBValue*) pair, but it seemed more complicated.

JDevlieghere accepted this revision.Nov 19 2021, 10:35 AM

Nice cleanup. LGTM.

This revision is now accepted and ready to land.Nov 19 2021, 10:35 AM
This revision was automatically updated to reflect the committed changes.