llvm-project/lldb/bindings/python/python-swigsafecast.swig
Pavel Labath 7f09ab08de [lldb] Fix [some] leaks in python bindings
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).

Differential Revision: https://reviews.llvm.org/D114259
2021-11-22 15:14:52 +01:00

91 lines
3.1 KiB
Plaintext

namespace lldb_private {
namespace python {
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);
}
PyObject *
SBTypeToSWIGWrapper(lldb::SBBreakpointLocation &breakpoint_location_sb) {
return SWIG_NewPointerObj(&breakpoint_location_sb,
SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
}
PyObject *SBTypeToSWIGWrapper(lldb::SBCommandReturnObject &cmd_ret_obj_sb) {
return SWIG_NewPointerObj(&cmd_ret_obj_sb,
SWIGTYPE_p_lldb__SBCommandReturnObject, 0);
}
PyObject *SBTypeToSWIGWrapper(lldb::SBExecutionContext &ctx_sb) {
return SWIG_NewPointerObj(&ctx_sb, SWIGTYPE_p_lldb__SBExecutionContext, 0);
}
PyObject *SBTypeToSWIGWrapper(lldb::SBTypeSummaryOptions &summary_options_sb) {
return SWIG_NewPointerObj(&summary_options_sb,
SWIGTYPE_p_lldb__SBTypeSummaryOptions, 0);
}
PyObject *SBTypeToSWIGWrapper(lldb::SBStructuredData &structured_data_sb) {
return SWIG_NewPointerObj(&structured_data_sb,
SWIGTYPE_p_lldb__SBStructuredData, 0);
}
PyObject *SBTypeToSWIGWrapper(lldb::SBSymbolContext &sym_ctx_sb) {
return SWIG_NewPointerObj(&sym_ctx_sb, SWIGTYPE_p_lldb__SBSymbolContext, 0);
}
PyObject *SBTypeToSWIGWrapper(lldb::SBStream &stream_sb) {
return SWIG_NewPointerObj(&stream_sb, SWIGTYPE_p_lldb__SBStream, 0);
}
PythonObject ToSWIGHelper(void *obj, swig_type_info *info) {
return {PyRefType::Owned, SWIG_NewPointerObj(obj, info, SWIG_POINTER_OWN)};
}
PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBValue> value_sb) {
return ToSWIGHelper(value_sb.release(), SWIGTYPE_p_lldb__SBValue);
}
PythonObject ToSWIGWrapper(lldb::ValueObjectSP value_sp) {
return ToSWIGWrapper(std::make_unique<lldb::SBValue>(std::move(value_sp)));
}
PythonObject ToSWIGWrapper(lldb::TargetSP target_sp) {
return ToSWIGHelper(new lldb::SBTarget(std::move(target_sp)),
SWIGTYPE_p_lldb__SBTarget);
}
PythonObject ToSWIGWrapper(lldb::ProcessSP process_sp) {
return ToSWIGHelper(new lldb::SBProcess(std::move(process_sp)),
SWIGTYPE_p_lldb__SBProcess);
}
PythonObject ToSWIGWrapper(lldb::ThreadPlanSP thread_plan_sp) {
return ToSWIGHelper(new lldb::SBThreadPlan(std::move(thread_plan_sp)),
SWIGTYPE_p_lldb__SBThreadPlan);
}
PythonObject ToSWIGWrapper(lldb::BreakpointSP breakpoint_sp) {
return ToSWIGHelper(new lldb::SBBreakpoint(std::move(breakpoint_sp)),
SWIGTYPE_p_lldb__SBBreakpoint);
}
} // namespace python
} // namespace lldb_private