Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: implement trivial destructors for static PyObject unique pointers to avoid invalid memory access on exit #28

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/clp_ffi_py/PyObjectUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,37 @@ class PyObjectDeleter {
void operator()(PyObjectType* ptr) { Py_XDECREF(reinterpret_cast<PyObject*>(ptr)); }
};

/**
* An empty deleter that has an empty implementation to ensure object trivial
* destruction.
* @tparam PyObjectType
*/
template <typename PyObjectType>
class PyObjectTrivialDeleter {
public:
void operator()(PyObjectType* ptr) {}
};

/**
* A type of smart pointer that maintains a reference to a Python object for the
* duration of its lifetime.
* @tparam PyObjectType
*/
template <typename PyObjectType>
using PyObjectPtr = std::unique_ptr<PyObjectType, PyObjectDeleter<PyObjectType>>;

/**
* A smart pointer to be used for raw Python object pointers that have static
* storage duration. It holds a reference of the underlying Python object.
* Compared to PyObjectPtr, when destructed, this smart pointer does not
* decrease the reference count of the contained Python object. Since this
* pointer has static storage duration, it's possible that the Python
* interpreter exits before this pointer's destructor is called; attempting to
* decrement the reference count (maintained by the interpreter) in this
* situation would lead to undefined behaviour.
* @tparam PyObjectType
*/
template <typename PyObjectType>
using PyObjectStaticPtr = std::unique_ptr<PyObjectType, PyObjectTrivialDeleter<PyObjectType>>;
} // namespace clp_ffi_py
#endif // CLP_FFI_PY_PY_OBJECT_PTR_HPP
4 changes: 2 additions & 2 deletions src/clp_ffi_py/Py_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
namespace clp_ffi_py {
namespace {
constexpr char const* const cPyFuncNameGetFormattedTimestamp{"get_formatted_timestamp"};
PyObjectPtr<PyObject> Py_func_get_formatted_timestamp;
PyObjectStaticPtr<PyObject> Py_func_get_formatted_timestamp{nullptr};

constexpr char const* const cPyFuncNameGetTimezoneFromTimezoneId{"get_timezone_from_timezone_id"};
PyObjectPtr<PyObject> Py_func_get_timezone_from_timezone_id;
PyObjectStaticPtr<PyObject> Py_func_get_timezone_from_timezone_id{nullptr};

/**
* Wrapper of PyObject_CallObject.
Expand Down
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ PyType_Spec PyDecoder_type_spec{
};
} // namespace

PyObjectPtr<PyTypeObject> PyDecoder::m_py_type{nullptr};
PyObjectStaticPtr<PyTypeObject> PyDecoder::m_py_type{nullptr};

auto PyDecoder::module_level_init(PyObject* py_module) -> bool {
static_assert(std::is_trivially_destructible<PyDecoder>());
Expand Down
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyDecoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PyDecoder {
private:
PyObject_HEAD;

static PyObjectPtr<PyTypeObject> m_py_type;
static PyObjectStaticPtr<PyTypeObject> m_py_type;
};
} // namespace clp_ffi_py::ir::native

Expand Down
4 changes: 2 additions & 2 deletions src/clp_ffi_py/ir/native/PyDecoderBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ auto PyDecoderBuffer::test_streaming(uint32_t seed) -> PyObject* {
);
}

PyObjectPtr<PyTypeObject> PyDecoderBuffer::m_py_type{nullptr};
PyObjectPtr<PyObject> PyDecoderBuffer::m_py_incomplete_stream_error{nullptr};
PyObjectStaticPtr<PyTypeObject> PyDecoderBuffer::m_py_type{nullptr};
PyObjectStaticPtr<PyObject> PyDecoderBuffer::m_py_incomplete_stream_error{nullptr};

auto PyDecoderBuffer::get_py_type() -> PyTypeObject* {
return m_py_type.get();
Expand Down
4 changes: 2 additions & 2 deletions src/clp_ffi_py/ir/native/PyDecoderBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ class PyDecoderBuffer {
size_t m_num_decoded_message;
bool m_py_buffer_protocol_enabled;

static PyObjectPtr<PyTypeObject> m_py_type;
static PyObjectPtr<PyObject> m_py_incomplete_stream_error;
static PyObjectStaticPtr<PyTypeObject> m_py_type;
static PyObjectStaticPtr<PyObject> m_py_incomplete_stream_error;
};
} // namespace clp_ffi_py::ir::native
#endif
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyFourByteEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ PyType_Spec PyFourByteEncoder_type_spec{
};
} // namespace

PyObjectPtr<PyTypeObject> PyFourByteEncoder::m_py_type{nullptr};
PyObjectStaticPtr<PyTypeObject> PyFourByteEncoder::m_py_type{nullptr};

auto PyFourByteEncoder::module_level_init(PyObject* py_module) -> bool {
static_assert(std::is_trivially_destructible<PyFourByteEncoder>());
Expand Down
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyFourByteEncoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PyFourByteEncoder {
private:
PyObject_HEAD;

static PyObjectPtr<PyTypeObject> m_py_type;
static PyObjectStaticPtr<PyTypeObject> m_py_type;
};
} // namespace clp_ffi_py::ir::native
#endif
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyLogEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ auto PyLogEvent::init(
return true;
}

PyObjectPtr<PyTypeObject> PyLogEvent::m_py_type{nullptr};
PyObjectStaticPtr<PyTypeObject> PyLogEvent::m_py_type{nullptr};

auto PyLogEvent::get_py_type() -> PyTypeObject* {
return m_py_type.get();
Expand Down
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyLogEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class PyLogEvent {
LogEvent* m_log_event;
PyMetadata* m_py_metadata;

static PyObjectPtr<PyTypeObject> m_py_type;
static PyObjectStaticPtr<PyTypeObject> m_py_type;
};
} // namespace clp_ffi_py::ir::native
#endif // CLP_FFI_PY_PY_LOG_EVENT_HPP
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ auto PyMetadata::init_py_timezone() -> bool {
return true;
}

PyObjectPtr<PyTypeObject> PyMetadata::m_py_type{nullptr};
PyObjectStaticPtr<PyTypeObject> PyMetadata::m_py_type{nullptr};

auto PyMetadata::get_py_type() -> PyTypeObject* {
return m_py_type.get();
Expand Down
2 changes: 1 addition & 1 deletion src/clp_ffi_py/ir/native/PyMetadata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class PyMetadata {
Metadata* m_metadata;
PyObject* m_py_timezone;

static PyObjectPtr<PyTypeObject> m_py_type;
static PyObjectStaticPtr<PyTypeObject> m_py_type;
};
} // namespace clp_ffi_py::ir::native
#endif // CLP_FFI_PY_PY_METADATA_HPP
4 changes: 2 additions & 2 deletions src/clp_ffi_py/ir/native/PyQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,8 @@ auto PyQuery::init(
return true;
}

PyObjectPtr<PyTypeObject> PyQuery::m_py_type{nullptr};
PyObjectPtr<PyObject> PyQuery::m_py_wildcard_query_type{nullptr};
PyObjectStaticPtr<PyTypeObject> PyQuery::m_py_type{nullptr};
PyObjectStaticPtr<PyObject> PyQuery::m_py_wildcard_query_type{nullptr};

auto PyQuery::get_py_type() -> PyTypeObject* {
return m_py_type.get();
Expand Down
4 changes: 2 additions & 2 deletions src/clp_ffi_py/ir/native/PyQuery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class PyQuery {
PyObject_HEAD;
Query* m_query;

static PyObjectPtr<PyTypeObject> m_py_type;
static PyObjectPtr<PyObject> m_py_wildcard_query_type;
static PyObjectStaticPtr<PyTypeObject> m_py_type;
static PyObjectStaticPtr<PyObject> m_py_wildcard_query_type;
};
} // namespace clp_ffi_py::ir::native
#endif