Skip to content

Commit 12fca6c

Browse files
White-Reaper0claude
andcommitted
gh-162539: Add a pin count to ctypes objects and check it in resize()
ctypes.resize() can move or free an object's memory block while other code still uses an address inside it. Add a pin count on the object that owns the memory, helpers to pin and unpin it (atomic on the free-threaded build), and make resize() raise BufferError while the block is pinned. This is groundwork only: nothing pins yet. Follow-up commits pin the block during stores, for sub-objects, for exported buffers and for pointers. The counter fits in existing padding, so objects do not grow on 64-bit platforms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 11a14db commit 12fca6c

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

‎Modules/_ctypes/callproc.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,12 @@ _ctypes_resize_impl(PyObject *module, CDataObject *obj, Py_ssize_t size)
20902090
"Memory cannot be resized because this object doesn't own it");
20912091
return NULL;
20922092
}
2093+
if (_PyCData_IsPinned(obj)) {
2094+
// gh-162539: something still uses an address inside the block.
2095+
PyErr_SetString(PyExc_BufferError,
2096+
"Memory cannot be resized while it is in use");
2097+
return NULL;
2098+
}
20932099
if ((size_t)size <= sizeof(obj->b_value)) {
20942100
/* internal default buffer is large enough */
20952101
obj->b_size = size;

‎Modules/_ctypes/ctypes.h‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ struct tagCDataObject {
183183
PyObject_HEAD
184184
char *b_ptr; /* pointer to memory block */
185185
int b_needsfree; /* need _we_ free the memory? */
186+
int b_pins; /* reasons the memory block must not move;
187+
see _PyCData_Pin() */
186188
CDataObject *b_base; /* pointer to base object or NULL */
187189
Py_ssize_t b_size; /* size of memory block in bytes */
188190
Py_ssize_t b_length; /* number of references we need */
@@ -214,6 +216,7 @@ typedef struct {
214216
PyObject_HEAD
215217
char *b_ptr; /* pointer to memory block */
216218
int b_needsfree; /* need _we_ free the memory? */
219+
int b_pins; /* reasons the memory block must not move */
217220
CDataObject *b_base; /* pointer to base object or NULL */
218221
Py_ssize_t b_size; /* size of memory block in bytes */
219222
Py_ssize_t b_length; /* number of references we need */
@@ -252,6 +255,64 @@ extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palig
252255
#define CDataObject_Check(st, v) PyObject_TypeCheck((v), (st)->PyCData_Type)
253256
#define _CDataObject_HasExternalBuffer(v) ((v)->b_ptr != (char *)&(v)->b_value)
254257

258+
/* gh-162539: ctypes.resize() may move or free the memory block of an object
259+
that owns its memory. Anything that keeps using an address inside that
260+
block -- a store in progress, a sub-object, an exported buffer, a pointer
261+
-- must "pin" the block first and unpin it when done. resize() refuses
262+
while the block is pinned.
263+
264+
The pin count lives on the owner: the object at the end of the b_base
265+
chain whose block contains the address. A b_base link whose memory is
266+
not inside the base's block (for example the object returned by a
267+
pointer's .contents) ends the chain.
268+
269+
Every _PyCData_Pin() must be balanced by exactly one _PyCData_Unpin() on
270+
the same object, on every path including errors. */
271+
272+
static inline CDataObject *
273+
_PyCData_MemoryOwner(CDataObject *obj)
274+
{
275+
CDataObject *base = obj->b_base;
276+
while (base != NULL
277+
&& obj->b_ptr >= base->b_ptr
278+
&& obj->b_ptr + obj->b_size <= base->b_ptr + base->b_size)
279+
{
280+
obj = base;
281+
base = obj->b_base;
282+
}
283+
return obj;
284+
}
285+
286+
static inline void
287+
_PyCData_Pin(CDataObject *obj)
288+
{
289+
CDataObject *owner = _PyCData_MemoryOwner(obj);
290+
#ifdef Py_GIL_DISABLED
291+
_Py_atomic_add_int(&owner->b_pins, 1);
292+
#else
293+
owner->b_pins++;
294+
#endif
295+
}
296+
297+
static inline void
298+
_PyCData_Unpin(CDataObject *obj)
299+
{
300+
CDataObject *owner = _PyCData_MemoryOwner(obj);
301+
#ifdef Py_GIL_DISABLED
302+
int old = _Py_atomic_add_int(&owner->b_pins, -1);
303+
#else
304+
int old = owner->b_pins--;
305+
#endif
306+
assert(old > 0);
307+
(void)old;
308+
}
309+
310+
static inline int
311+
_PyCData_IsPinned(CDataObject *obj)
312+
{
313+
return FT_ATOMIC_LOAD_INT(obj->b_pins) > 0;
314+
}
315+
255316
#define PyCSimpleTypeObject_CheckExact(st, v) Py_IS_TYPE((v), (st)->PyCSimpleType_Type)
256317
#define PyCSimpleTypeObject_Check(st, v) PyObject_TypeCheck((v), (st)->PyCSimpleType_Type)
257318

0 commit comments

Comments
 (0)