This is an archive of the discontinued LLVM Phabricator instance.

[lldb/python] Fix (some) dangling pointers in our glue code
ClosedPublic

Authored by labath on Dec 17 2021, 2:06 AM.

Details

Summary

This starts to fix the other half of the lifetime problems in this code

  • dangling references. SB objects created on the stack will go away

when the function returns, which is a problem if the python code they
were meant for stashes a reference to them somewhere. Most of the time
this goes by unnoticed, as the code rarely has a reason to store these,
but in case it does, we shouldn't respond by crashing.

This patch fixes the management for a couple of SB objects (Debugger,
Frame, Thread). The SB objects are now created on the heap, and
their ownership is immediately passed on to SWIG, which will ensure they
are destroyed when the last python reference goes away. I will handle
the other objects in separate patches.

I include one test which demonstrates the lifetime issue for SBDebugger.
Strictly speaking, one should create a test case for each of these
objects and each of the contexts they are being used. That would require
figuring out how to persist (and later access) each of these objects.
Some of those may involve a lot of hoop-jumping (we can run python code
from within a frame-format string). I don't think that is
necessary/worth it since the new wrapper functions make it very hard to
get this wrong.

Diff Detail

Event Timeline

labath requested review of this revision.Dec 17 2021, 2:06 AM
labath created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptDec 17 2021, 2:06 AM
mib accepted this revision.Dec 17 2021, 10:32 AM

Thanks for fixing this! LGTM beside a small nit.

lldb/test/API/commands/command/script/TestCommandScript.py
172

persistence.py doesn't seem to have a use_debugger function

This revision is now accepted and ready to land.Dec 17 2021, 10:32 AM

Thanks. In case anyone's wondering, the reason for that stray command is because I originally wanted to test this via use_debugger command. However, that turned out to not work because the SBDebugger object for the use_debuggers own argument was created at the exact same spot on the stack as the argument to the save_debugger command. Do, the test passed even when it shouldn't have. I have now removed the leftover command.