Skip to content

Memory access violation when calling cpdef'ed method of extension type #2823

@cgohlke

Description

@cgohlke

During testing of scikit-image master with Cython 0.29.4 on Windows, I get a reproducible segfault in the scikit-image's _mcp Cython extension module as mentioned at scikit-image/scikit-image#3696 (comment).

The segfault happens when calling the cpdef goal_reached method of the cdef class MCP (Cython extension type) at https://github.com/scikit-image/scikit-image/blob/master/skimage/graph/_mcp.pyx#L541.

The segfault disappears when overriding the cpdef method from Python or when adding a __dict__ to the cdef class.

The segfault is in the generated code

obj_dict_version = likely(Py_TYPE(((PyObject *)__pyx_v_self))->tp_dictoffset) ? __PYX_GET_DICT_VERSION(_PyObject_GetDictPtr(((PyObject *)__pyx_v_self))) : 0;

where __pyx_v_self is a valid python object, tp_dictoffset seems reasonable (1936), but _PyObject_GetDictPtr(((PyObject *)__pyx_v_self)) points to an out of bounds address such that __PYX_GET_DICT_VERSION either segfaults or returns an invalid or random value.

It seems to me that either the cdef class should have a zero tp_dictoffset, or allocate memory for a __dict__.

Here's the top of the call stack:

>	_mcp.cp37-win_amd64.pyd!__pyx_f_7skimage_5graph_4_mcp_3MCP_goal_reached(__pyx_obj_7skimage_5graph_4_mcp_MCP * __pyx_v_self=0x000001a23db99858, __int64 __pyx_v_index=9, double __pyx_v_cumcost=1.0000000000000000, int __pyx_skip_dispatch=0) Line 7580	C
 	_mcp.cp37-win_amd64.pyd!__pyx_pf_7skimage_5graph_4_mcp_3MCP_6find_costs(__pyx_obj_7skimage_5graph_4_mcp_MCP * __pyx_v_self=0x000001a23db99858, _object * __pyx_v_starts=0x000001a203e47d08, _object * __pyx_v_ends=0x00007ff9b3067ce0, _object * __pyx_v_find_all_ends=0x00007ff9b305c950, _object * __pyx_v_max_coverage=0x000001a2420fb870, _object * __pyx_v_max_cumulative_cost=0x00007ff9b3067ce0, _object * __pyx_v_max_cost=0x00007ff9b3067ce0) Line 8683	C
 	_mcp.cp37-win_amd64.pyd!__pyx_pw_7skimage_5graph_4_mcp_3MCP_7find_costs(_object * __pyx_v_self=0x000001a23db99858, _object * __pyx_args=0x000001a203e9c160, _object * __pyx_kwds=0x0000000000000000) Line 7869	C
 	python37.dll!_PyMethodDef_RawFastCallKeywords(PyMethodDef * method, _object * self=0x000001a23db99858, _object * const * args=0x000001a23db97fc0, __int64 nargs=1, _object * kwnames=0x0000000000000000) Line 690	C
 	[Inline Frame] python37.dll!_PyMethodDescr_FastCallKeywords(_object *) Line 288	C

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions