Skip to content

Commit

Permalink
[lldb/python] Fix (some) dangling pointers in our glue code
Browse files Browse the repository at this point in the history
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.

Differential Revision: https://reviews.llvm.org/D115925
  • Loading branch information
labath committed Dec 20, 2021
1 parent 3b2fd94 commit 7406d23
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 101 deletions.
27 changes: 15 additions & 12 deletions lldb/bindings/python/python-swigsafecast.swig
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@ PyObject *SBTypeToSWIGWrapper(lldb::SBEvent &event_sb) {
return SWIG_NewPointerObj(&event_sb, SWIGTYPE_p_lldb__SBEvent, 0);
}

PyObject *SBTypeToSWIGWrapper(lldb::SBThread &thread_sb) {
return SWIG_NewPointerObj(&thread_sb, SWIGTYPE_p_lldb__SBThread, 0);
}

PyObject *SBTypeToSWIGWrapper(lldb::SBFrame &frame_sb) {
return SWIG_NewPointerObj(&frame_sb, SWIGTYPE_p_lldb__SBFrame, 0);
}

PyObject *SBTypeToSWIGWrapper(lldb::SBDebugger &debugger_sb) {
return SWIG_NewPointerObj(&debugger_sb, SWIGTYPE_p_lldb__SBDebugger, 0);
}

PyObject *SBTypeToSWIGWrapper(lldb::SBWatchpoint &watchpoint_sb) {
return SWIG_NewPointerObj(&watchpoint_sb, SWIGTYPE_p_lldb__SBWatchpoint, 0);
}
Expand Down Expand Up @@ -89,5 +77,20 @@ PythonObject ToSWIGWrapper(const StructuredDataImpl &data_impl) {
return ToSWIGWrapper(std::make_unique<lldb::SBStructuredData>(data_impl));
}

PythonObject ToSWIGWrapper(lldb::ThreadSP thread_sp) {
return ToSWIGHelper(new lldb::SBThread(std::move(thread_sp)),
SWIGTYPE_p_lldb__SBThread);
}

PythonObject ToSWIGWrapper(lldb::StackFrameSP frame_sp) {
return ToSWIGHelper(new lldb::SBFrame(std::move(frame_sp)),
SWIGTYPE_p_lldb__SBFrame);
}

PythonObject ToSWIGWrapper(lldb::DebuggerSP debugger_sp) {
return ToSWIGHelper(new lldb::SBDebugger(std::move(debugger_sp)),
SWIGTYPE_p_lldb__SBDebugger);
}

} // namespace python
} // namespace lldb_private
69 changes: 24 additions & 45 deletions lldb/bindings/python/python-wrapper.swig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ llvm::Expected<bool> lldb_private::LLDBSwigPythonBreakpointCallbackFunction(
const lldb_private::StructuredDataImpl &args_impl) {
using namespace llvm;

lldb::SBFrame sb_frame(frame_sp);
lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);

PyErr_Cleaner py_err_cleaner(true);
Expand All @@ -38,7 +37,7 @@ llvm::Expected<bool> lldb_private::LLDBSwigPythonBreakpointCallbackFunction(
else
return arg_info.takeError();

PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame));
PythonObject frame_arg = ToSWIGWrapper(frame_sp);
PythonObject bp_loc_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_bp_loc));

auto result = [&]() -> Expected<PythonObject> {
Expand Down Expand Up @@ -71,7 +70,6 @@ llvm::Expected<bool> lldb_private::LLDBSwigPythonBreakpointCallbackFunction(
bool lldb_private::LLDBSwigPythonWatchpointCallbackFunction(
const char *python_function_name, const char *session_dictionary_name,
const lldb::StackFrameSP &frame_sp, const lldb::WatchpointSP &wp_sp) {
lldb::SBFrame sb_frame(frame_sp);
lldb::SBWatchpoint sb_wp(wp_sp);

bool stop_at_watchpoint = true;
Expand All @@ -86,7 +84,7 @@ bool lldb_private::LLDBSwigPythonWatchpointCallbackFunction(
if (!pfunc.IsAllocated())
return stop_at_watchpoint;

PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_frame));
PythonObject frame_arg = ToSWIGWrapper(frame_sp);
PythonObject wp_arg(PyRefType::Owned, SBTypeToSWIGWrapper(sb_wp));
PythonObject result = pfunc(frame_arg, wp_arg, dict);

Expand Down Expand Up @@ -194,7 +192,7 @@ void *lldb_private::LLDBSwigPythonCreateSyntheticProvider(

void *lldb_private::LLDBSwigPythonCreateCommandObject(
const char *python_class_name, const char *session_dictionary_name,
const lldb::DebuggerSP debugger_sp) {
lldb::DebuggerSP debugger_sp) {
if (python_class_name == NULL || python_class_name[0] == '\0' ||
!session_dictionary_name)
Py_RETURN_NONE;
Expand All @@ -208,9 +206,7 @@ void *lldb_private::LLDBSwigPythonCreateCommandObject(
if (!pfunc.IsAllocated())
return nullptr;

lldb::SBDebugger debugger_sb(debugger_sp);
PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
PythonObject result = pfunc(debugger_arg, dict);
PythonObject result = pfunc(ToSWIGWrapper(std::move(debugger_sp)), dict);

if (result.IsAllocated())
return result.release();
Expand Down Expand Up @@ -809,11 +805,10 @@ void *lldb_private::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *

bool lldb_private::LLDBSwigPythonCallCommand(
const char *python_function_name, const char *session_dictionary_name,
lldb::DebuggerSP &debugger, const char *args,
lldb::DebuggerSP debugger, const char *args,
lldb_private::CommandReturnObject &cmd_retobj,
lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
lldb::SBDebugger debugger_sb(debugger);
lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);

PyErr_Cleaner py_err_cleaner(true);
Expand All @@ -830,7 +825,7 @@ bool lldb_private::LLDBSwigPythonCallCommand(
llvm::consumeError(argc.takeError());
return false;
}
PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
PythonObject debugger_arg = ToSWIGWrapper(std::move(debugger));
PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
PythonObject cmd_retobj_arg(PyRefType::Owned,
SBTypeToSWIGWrapper(cmd_retobj_sb));
Expand All @@ -844,11 +839,10 @@ bool lldb_private::LLDBSwigPythonCallCommand(
}

bool lldb_private::LLDBSwigPythonCallCommandObject(
PyObject * implementor, lldb::DebuggerSP & debugger, const char *args,
PyObject *implementor, lldb::DebuggerSP debugger, const char *args,
lldb_private::CommandReturnObject &cmd_retobj,
lldb::ExecutionContextRefSP exe_ctx_ref_sp) {
lldb::SBCommandReturnObject cmd_retobj_sb(cmd_retobj);
lldb::SBDebugger debugger_sb(debugger);
lldb::SBExecutionContext exe_ctx_sb(exe_ctx_ref_sp);

PyErr_Cleaner py_err_cleaner(true);
Expand All @@ -859,12 +853,12 @@ bool lldb_private::LLDBSwigPythonCallCommandObject(
if (!pfunc.IsAllocated())
return false;

PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
PythonObject exe_ctx_arg(PyRefType::Owned, SBTypeToSWIGWrapper(exe_ctx_sb));
PythonObject cmd_retobj_arg(PyRefType::Owned,
SBTypeToSWIGWrapper(cmd_retobj_sb));

pfunc(debugger_arg, PythonString(args), exe_ctx_arg, cmd_retobj_arg);
pfunc(ToSWIGWrapper(std::move(debugger)), PythonString(args), exe_ctx_arg,
cmd_retobj_arg);

return true;
}
Expand Down Expand Up @@ -922,8 +916,7 @@ PyObject *lldb_private::LLDBSwigPython_GetRecognizedArguments(
PyObject * implementor, const lldb::StackFrameSP &frame_sp) {
static char callee_name[] = "get_recognized_arguments";

lldb::SBFrame frame_sb(frame_sp);
PyObject *arg = SBTypeToSWIGWrapper(frame_sb);
PythonObject arg = ToSWIGWrapper(frame_sp);

PythonString str(callee_name);
PyObject *result =
Expand Down Expand Up @@ -973,14 +966,12 @@ bool lldb_private::LLDBSWIGPythonRunScriptKeywordProcess(
return true;
}

bool lldb_private::LLDBSWIGPythonRunScriptKeywordThread(
llvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordThread(
const char *python_function_name, const char *session_dictionary_name,
lldb::ThreadSP &thread, std::string &output)

{
lldb::ThreadSP thread) {
if (python_function_name == NULL || python_function_name[0] == '\0' ||
!session_dictionary_name)
return false;
return llvm::None;

PyErr_Cleaner py_err_cleaner(true);

Expand All @@ -990,15 +981,11 @@ bool lldb_private::LLDBSWIGPythonRunScriptKeywordThread(
python_function_name, dict);

if (!pfunc.IsAllocated())
return false;
return llvm::None;

lldb::SBThread thread_sb(thread);
PythonObject thread_arg(PyRefType::Owned, SBTypeToSWIGWrapper(thread_sb));
auto result = pfunc(thread_arg, dict);
auto result = pfunc(ToSWIGWrapper(std::move(thread)), dict);

output = result.Str().GetString().str();

return true;
return result.Str().GetString().str();
}

bool lldb_private::LLDBSWIGPythonRunScriptKeywordTarget(
Expand Down Expand Up @@ -1026,14 +1013,12 @@ bool lldb_private::LLDBSWIGPythonRunScriptKeywordTarget(
return true;
}

bool lldb_private::LLDBSWIGPythonRunScriptKeywordFrame(
llvm::Optional<std::string> lldb_private::LLDBSWIGPythonRunScriptKeywordFrame(
const char *python_function_name, const char *session_dictionary_name,
lldb::StackFrameSP &frame, std::string &output)

{
lldb::StackFrameSP frame) {
if (python_function_name == NULL || python_function_name[0] == '\0' ||
!session_dictionary_name)
return false;
return llvm::None;

PyErr_Cleaner py_err_cleaner(true);

Expand All @@ -1043,15 +1028,11 @@ bool lldb_private::LLDBSWIGPythonRunScriptKeywordFrame(
python_function_name, dict);

if (!pfunc.IsAllocated())
return false;
return llvm::None;

lldb::SBFrame frame_sb(frame);
PythonObject frame_arg(PyRefType::Owned, SBTypeToSWIGWrapper(frame_sb));
auto result = pfunc(frame_arg, dict);
auto result = pfunc(ToSWIGWrapper(std::move(frame)), dict);

output = result.Str().GetString().str();

return true;
return result.Str().GetString().str();
}

bool lldb_private::LLDBSWIGPythonRunScriptKeywordValue(
Expand Down Expand Up @@ -1081,7 +1062,7 @@ bool lldb_private::LLDBSWIGPythonRunScriptKeywordValue(

bool lldb_private::LLDBSwigPythonCallModuleInit(
const char *python_module_name, const char *session_dictionary_name,
lldb::DebuggerSP &debugger) {
lldb::DebuggerSP debugger) {
std::string python_function_name_string = python_module_name;
python_function_name_string += ".__lldb_init_module";
const char *python_function_name = python_function_name_string.c_str();
Expand All @@ -1098,9 +1079,7 @@ bool lldb_private::LLDBSwigPythonCallModuleInit(
if (!pfunc.IsAllocated())
return true;

lldb::SBDebugger debugger_sb(debugger);
PythonObject debugger_arg(PyRefType::Owned, SBTypeToSWIGWrapper(debugger_sb));
pfunc(debugger_arg, dict);
pfunc(ToSWIGWrapper(std::move(debugger)), dict);

return true;
}
Expand Down
24 changes: 12 additions & 12 deletions lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,

void *LLDBSwigPythonCreateCommandObject(const char *python_class_name,
const char *session_dictionary_name,
const lldb::DebuggerSP debugger_sp);
lldb::DebuggerSP debugger_sp);

void *LLDBSwigPythonCreateScriptedThreadPlan(
const char *python_class_name, const char *session_dictionary_name,
Expand Down Expand Up @@ -137,18 +137,18 @@ PyObject *LLDBSwigPython_GetValueSynthProviderInstance(PyObject *implementor);

bool LLDBSwigPythonCallCommand(const char *python_function_name,
const char *session_dictionary_name,
lldb::DebuggerSP &debugger, const char *args,
lldb::DebuggerSP debugger, const char *args,
lldb_private::CommandReturnObject &cmd_retobj,
lldb::ExecutionContextRefSP exe_ctx_ref_sp);

bool LLDBSwigPythonCallCommandObject(
PyObject *implementor, lldb::DebuggerSP &debugger, const char *args,
PyObject *implementor, lldb::DebuggerSP debugger, const char *args,
lldb_private::CommandReturnObject &cmd_retobj,
lldb::ExecutionContextRefSP exe_ctx_ref_sp);

bool LLDBSwigPythonCallModuleInit(const char *python_module_name,
const char *session_dictionary_name,
lldb::DebuggerSP &debugger);
lldb::DebuggerSP debugger);

void *LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
const char *session_dictionary_name,
Expand All @@ -166,20 +166,20 @@ bool LLDBSWIGPythonRunScriptKeywordProcess(const char *python_function_name,
const lldb::ProcessSP &process,
std::string &output);

bool LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name,
const char *session_dictionary_name,
lldb::ThreadSP &thread,
std::string &output);
llvm::Optional<std::string>
LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name,
const char *session_dictionary_name,
lldb::ThreadSP thread);

bool LLDBSWIGPythonRunScriptKeywordTarget(const char *python_function_name,
const char *session_dictionary_name,
const lldb::TargetSP &target,
std::string &output);

bool LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name,
const char *session_dictionary_name,
lldb::StackFrameSP &frame,
std::string &output);
llvm::Optional<std::string>
LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name,
const char *session_dictionary_name,
lldb::StackFrameSP frame);

bool LLDBSWIGPythonRunScriptKeywordValue(const char *python_function_name,
const char *session_dictionary_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2521,7 +2521,6 @@ bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
const char *impl_function, Thread *thread, std::string &output,
Status &error) {
bool ret_val;
if (!thread) {
error.SetErrorString("no thread");
return false;
Expand All @@ -2531,16 +2530,16 @@ bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
return false;
}

{
ThreadSP thread_sp(thread->shared_from_this());
Locker py_lock(this,
Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
ret_val = LLDBSWIGPythonRunScriptKeywordThread(
impl_function, m_dictionary_name.c_str(), thread_sp, output);
if (!ret_val)
error.SetErrorString("python script evaluation failed");
Locker py_lock(this,
Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
if (llvm::Optional<std::string> result = LLDBSWIGPythonRunScriptKeywordThread(
impl_function, m_dictionary_name.c_str(),
thread->shared_from_this())) {
output = std::move(*result);
return true;
}
return ret_val;
error.SetErrorString("python script evaluation failed");
return false;
}

bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
Expand Down Expand Up @@ -2571,7 +2570,6 @@ bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
const char *impl_function, StackFrame *frame, std::string &output,
Status &error) {
bool ret_val;
if (!frame) {
error.SetErrorString("no frame");
return false;
Expand All @@ -2581,16 +2579,16 @@ bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
return false;
}

{
StackFrameSP frame_sp(frame->shared_from_this());
Locker py_lock(this,
Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
ret_val = LLDBSWIGPythonRunScriptKeywordFrame(
impl_function, m_dictionary_name.c_str(), frame_sp, output);
if (!ret_val)
error.SetErrorString("python script evaluation failed");
Locker py_lock(this,
Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
if (llvm::Optional<std::string> result = LLDBSWIGPythonRunScriptKeywordFrame(
impl_function, m_dictionary_name.c_str(),
frame->shared_from_this())) {
output = std::move(*result);
return true;
}
return ret_val;
error.SetErrorString("python script evaluation failed");
return false;
}

bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
Expand Down Expand Up @@ -2655,7 +2653,6 @@ bool ScriptInterpreterPythonImpl::LoadScriptingModule(
}

ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error;
lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this();

// Before executing Python code, lock the GIL.
Locker py_lock(this,
Expand Down Expand Up @@ -2792,7 +2789,8 @@ bool ScriptInterpreterPythonImpl::LoadScriptingModule(
// if we are here, everything worked
// call __lldb_init_module(debugger,dict)
if (!LLDBSwigPythonCallModuleInit(module_name.c_str(),
m_dictionary_name.c_str(), debugger_sp)) {
m_dictionary_name.c_str(),
m_debugger.shared_from_this())) {
error.SetErrorString("calling __lldb_init_module failed");
return false;
}
Expand Down
Loading

0 comments on commit 7406d23

Please sign in to comment.