Skip to content

gh-127945: fix thread safety and add lock held assertions to paramfunc in ctypes #132473

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

Merged
merged 2 commits into from
Apr 14, 2025
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
50 changes: 21 additions & 29 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,14 @@ PyType_Spec pyctype_type_spec = {
.slots = ctype_type_slots,
};

/*
PyCStructType_Type - a meta type/class. Creating a new class using this one as
__metaclass__ will call the constructor StructUnionType_new.
It initializes the C accessible fields somehow.
*/

static PyCArgObject *
StructUnionType_paramfunc_lock_held(ctypes_state *st, CDataObject *self)
StructUnionType_paramfunc(ctypes_state *st, CDataObject *self)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
PyCArgObject *parg;
Expand Down Expand Up @@ -649,20 +655,6 @@ StructUnionType_paramfunc_lock_held(ctypes_state *st, CDataObject *self)
return parg;
}

/*
PyCStructType_Type - a meta type/class. Creating a new class using this one as
__metaclass__ will call the constructor StructUnionType_new.
It initializes the C accessible fields somehow.
*/
static PyCArgObject *
StructUnionType_paramfunc(ctypes_state *st, CDataObject *self)
{
PyCArgObject *res;
Py_BEGIN_CRITICAL_SECTION(self);
res = StructUnionType_paramfunc_lock_held(st, self);
Py_END_CRITICAL_SECTION();
return res;
}

static int
StructUnionType_init(PyObject *self, PyObject *args, PyObject *kwds, int isStruct)
Expand Down Expand Up @@ -1213,6 +1205,7 @@ PyCPointerType_SetProto(ctypes_state *st, StgInfo *stginfo, PyObject *proto)
static PyCArgObject *
PyCPointerType_paramfunc(ctypes_state *st, CDataObject *self)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
PyCArgObject *parg;

parg = PyCArgObject_new(st);
Expand All @@ -1222,7 +1215,7 @@ PyCPointerType_paramfunc(ctypes_state *st, CDataObject *self)
parg->tag = 'P';
parg->pffi_type = &ffi_type_pointer;
parg->obj = Py_NewRef(self);
parg->value.p = locked_deref(self);
parg->value.p = *(void **)self->b_ptr;
return parg;
}

Expand Down Expand Up @@ -1665,6 +1658,7 @@ add_getset(PyTypeObject *type, PyGetSetDef *gsp)
static PyCArgObject *
PyCArrayType_paramfunc(ctypes_state *st, CDataObject *self)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
PyCArgObject *p = PyCArgObject_new(st);
if (p == NULL)
return NULL;
Expand Down Expand Up @@ -2159,7 +2153,9 @@ c_void_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
parg->tag = 'Z';
parg->obj = Py_NewRef(value);
/* Remember: b_ptr points to where the pointer is stored! */
parg->value.p = locked_deref((CDataObject *)value);
Py_BEGIN_CRITICAL_SECTION(value);
parg->value.p = *(void **)_CDataObject_CAST(value)->b_ptr;
Py_END_CRITICAL_SECTION();
return (PyObject *)parg;
}
}
Expand Down Expand Up @@ -2241,7 +2237,7 @@ static PyObject *CreateSwappedType(ctypes_state *st, PyTypeObject *type,
}

static PyCArgObject *
PyCSimpleType_paramfunc_lock_held(ctypes_state *st, CDataObject *self)
PyCSimpleType_paramfunc(ctypes_state *st, CDataObject *self)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
const char *fmt;
Expand Down Expand Up @@ -2270,15 +2266,6 @@ PyCSimpleType_paramfunc_lock_held(ctypes_state *st, CDataObject *self)
return parg;
}

static PyCArgObject *
PyCSimpleType_paramfunc(ctypes_state *st, CDataObject *self)
{
PyCArgObject *res;
Py_BEGIN_CRITICAL_SECTION(self);
res = PyCSimpleType_paramfunc_lock_held(st, self);
Py_END_CRITICAL_SECTION();
return res;
}

static int
PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds)
Expand Down Expand Up @@ -2766,6 +2753,7 @@ make_funcptrtype_dict(ctypes_state *st, PyObject *attrdict, StgInfo *stginfo)
static PyCArgObject *
PyCFuncPtrType_paramfunc(ctypes_state *st, CDataObject *self)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
PyCArgObject *parg;

parg = PyCArgObject_new(st);
Expand All @@ -2775,7 +2763,7 @@ PyCFuncPtrType_paramfunc(ctypes_state *st, CDataObject *self)
parg->tag = 'P';
parg->pffi_type = &ffi_type_pointer;
parg->obj = Py_NewRef(self);
parg->value.p = locked_deref(self);
parg->value.p = *(void **)self->b_ptr;
return parg;
}

Expand Down Expand Up @@ -5827,7 +5815,11 @@ Pointer_subscript(PyObject *myself, PyObject *item)
static int
Pointer_bool(PyObject *self)
{
return locked_deref(_CDataObject_CAST(self)) != NULL;
int res;
Py_BEGIN_CRITICAL_SECTION(self);
res = *(void **)_CDataObject_CAST(self)->b_ptr != NULL;
Py_END_CRITICAL_SECTION();
return res;
}

static PyType_Slot pycpointer_slots[] = {
Expand Down
2 changes: 2 additions & 0 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,9 @@ static int ConvParam(ctypes_state *st,
PyCArgObject *carg;
assert(info->paramfunc);
/* If it has an stginfo, it is a CDataObject */
Py_BEGIN_CRITICAL_SECTION(obj);
carg = info->paramfunc(st, (CDataObject *)obj);
Py_END_CRITICAL_SECTION();
if (carg == NULL)
return -1;
pa->ffi_type = carg->pffi_type;
Expand Down
11 changes: 0 additions & 11 deletions Modules/_ctypes/ctypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,3 @@ PyStgInfo_Init(ctypes_state *state, PyTypeObject *type)
info->initialized = 1;
return info;
}

/* Equivalent to *self->b_ptr with a lock. */
static inline void *
locked_deref(CDataObject *self)
{
void *ptr;
Py_BEGIN_CRITICAL_SECTION(self);
ptr = *(void **)self->b_ptr;
Py_END_CRITICAL_SECTION();
return ptr;
}
Loading