Skip to content

Commit e57a989

Browse files
[3.15] gh-135893: Lazily initialize the libffi allocator in ctypes (GH-161844) (GH-161975)
(cherry picked from commit 9b633f5) Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
1 parent d5b42ce commit e57a989

5 files changed

Lines changed: 46 additions & 12 deletions

File tree

‎Lib/test/test_ctypes/test_callbacks.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from ctypes.util import find_library
1515
from test import support
1616
from test.support import import_helper
17+
from test.support import script_helper
1718
_ctypes_test = import_helper.import_module("_ctypes_test")
1819

1920

@@ -343,6 +344,19 @@ def cb():
343344

344345
self.assertEqual(_testfunc_callback_int_to_longlong(cb), -1)
345346

347+
def test_import_does_not_open_fds(self):
348+
# gh-135893: Importing ctypes must not allocate a libffi closure,
349+
# which can leave a file descriptor open for the rest of the
350+
# process' lifetime.
351+
code = """if 1:
352+
from test.support import os_helper
353+
before = os_helper.fd_count()
354+
import ctypes
355+
after = os_helper.fd_count()
356+
assert before == after
357+
"""
358+
script_helper.assert_python_ok("-c", code)
359+
346360

347361
if __name__ == '__main__':
348362
unittest.main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a regression where importing :mod:`ctypes` could leave an extra file
2+
descriptor open on some Linux systems, or hang in x86-64 macOS applications
3+
signed with the hardened runtime.

‎Modules/_ctypes/_ctypes.c‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6427,18 +6427,6 @@ _ctypes_add_objects(PyObject *mod)
64276427
static int
64286428
_ctypes_mod_exec(PyObject *mod)
64296429
{
6430-
// See https://github.com/python/cpython/issues/128485
6431-
// This allocates some memory and then frees it to ensure that the
6432-
// the dlmalloc allocator initializes itself to avoid data races
6433-
// in free-threading.
6434-
void *codeloc = NULL;
6435-
void *ptr = Py_ffi_closure_alloc(sizeof(void *), &codeloc);
6436-
if (ptr == NULL) {
6437-
PyErr_NoMemory();
6438-
return -1;
6439-
}
6440-
Py_ffi_closure_free(ptr);
6441-
64426430
_ctypes_init_fielddesc();
64436431

64446432
ctypes_state *st = get_module_state(mod);

‎Modules/_ctypes/callbacks.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#endif
1010

1111
#include "pycore_call.h" // _PyObject_CallNoArgs()
12+
#include "pycore_lock.h" // _PyOnceFlag_CallOnce()
1213
#include "pycore_runtime.h" // _Py_ID()
1314

1415
#include <ffi.h>
@@ -379,6 +380,27 @@ static CThunkObject* CThunkObject_new(ctypes_state *st, Py_ssize_t nargs)
379380
return p;
380381
}
381382

383+
static _PyOnceFlag closure_alloc_init_once = {0};
384+
385+
// This is done lazily with a once flag rather than in the module exec function,
386+
// because allocating a closure can have some side effects that affect the whole
387+
// process (gh-135893).
388+
static int
389+
closure_alloc_init(void *Py_UNUSED(arg))
390+
{
391+
// See https://github.com/python/cpython/issues/128485
392+
// This allocates some memory and then frees it to ensure that libffi's
393+
// dlmalloc allocator initializes itself exactly once, avoiding data races
394+
// between threads that don't share a GIL.
395+
void *codeloc = NULL;
396+
void *ptr = Py_ffi_closure_alloc(sizeof(void *), &codeloc);
397+
if (ptr == NULL) {
398+
return -1;
399+
}
400+
Py_ffi_closure_free(ptr);
401+
return 0;
402+
}
403+
382404
CThunkObject *_ctypes_alloc_callback(ctypes_state *st,
383405
PyObject *callable,
384406
PyObject *converters,
@@ -398,6 +420,12 @@ CThunkObject *_ctypes_alloc_callback(ctypes_state *st,
398420

399421
assert(CThunk_CheckExact(st, (PyObject *)p));
400422

423+
if (_PyOnceFlag_CallOnce(&closure_alloc_init_once,
424+
closure_alloc_init, NULL) < 0) {
425+
PyErr_NoMemory();
426+
goto error;
427+
}
428+
401429
p->pcl_write = Py_ffi_closure_alloc(sizeof(ffi_closure), &p->pcl_exec);
402430
if (p->pcl_write == NULL) {
403431
PyErr_NoMemory();

‎Tools/c-analyzer/cpython/ignored.tsv‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Modules/posixmodule.c os_pipe_impl pipe2_works_atomic -
2626
## guards around resource init
2727
Python/thread_pthread.h PyThread__init_thread lib_initialized -
2828
Modules/_struct.c - endian_tables_init_once -
29+
Modules/_ctypes/callbacks.c - closure_alloc_init_once -
2930

3031
##-----------------------
3132
## other values (not Python-specific)

0 commit comments

Comments
 (0)