Debian Codegraph @

Modules/_ctypes/callbacks.c

raw
1 #ifndef Py_BUILD_CORE_BUILTIN
2 # define Py_BUILD_CORE_MODULE 1
3 #endif
4
5 #include "Python.h"
6 // windows.h must be included before pycore internal headers
7 #ifdef MS_WIN32
8 # include <windows.h>
9 #endif
10
11 #include "pycore_call.h" // _PyObject_CallNoArgs()
12 #include "pycore_runtime.h" // _Py_ID()
13
14 #ifdef MS_WIN32
15 # include <malloc.h>
16 #endif
17
18 #include <ffi.h>
19 #include "ctypes.h"
20
21 #ifdef HAVE_ALLOCA_H
22 /* AIX needs alloca.h for alloca() */
23 #include <alloca.h>
24 #endif
25
26 /**************************************************************/
27
28 static int
29 CThunkObject_traverse(PyObject *myself, visitproc visit, void *arg)
30 {
31 CThunkObject *self = (CThunkObject *)myself;
32 Py_VISIT(Py_TYPE(self));
33 Py_VISIT(self->converters);
34 Py_VISIT(self->callable);
35 Py_VISIT(self->restype);
36 return 0;
37 }
38
39 static int
40 CThunkObject_clear(PyObject *myself)
41 {
42 CThunkObject *self = (CThunkObject *)myself;
43 Py_CLEAR(self->converters);
44 Py_CLEAR(self->callable);
45 Py_CLEAR(self->restype);
46 return 0;
47 }
48
49 static void
50 CThunkObject_dealloc(PyObject *myself)
51 {
52 CThunkObject *self = (CThunkObject *)myself;
53 PyTypeObject *tp = Py_TYPE(myself);
54 PyObject_GC_UnTrack(self);
55 (void)CThunkObject_clear(myself);
56 if (self->pcl_write) {
57 Py_ffi_closure_free(self->pcl_write);
58 }
59 PyObject_GC_Del(self);
60 Py_DECREF(tp);
61 }
62
63 static PyType_Slot cthunk_slots[] = {
64 {Py_tp_doc, (void *)PyDoc_STR("CThunkObject")},
65 {Py_tp_dealloc, CThunkObject_dealloc},
66 {Py_tp_traverse, CThunkObject_traverse},
67 {Py_tp_clear, CThunkObject_clear},
68 {0, NULL},
69 };
70
71 PyType_Spec cthunk_spec = {
72 .name = "_ctypes.CThunkObject",
73 .basicsize = sizeof(CThunkObject),
74 .itemsize = sizeof(ffi_type),
75 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
76 Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
77 .slots = cthunk_slots,
78 };
79
80 /**************************************************************/
81
82 #ifdef MS_WIN32
83 /*
84 * We must call AddRef() on non-NULL COM pointers we receive as arguments
85 * to callback functions - these functions are COM method implementations.
86 * The Python instances we create have a __del__ method which calls Release().
87 *
88 * The presence of a class attribute named '_needs_com_addref_' triggers this
89 * behaviour. It would also be possible to call the AddRef() Python method,
90 * after checking for PyObject_IsTrue(), but this would probably be somewhat
91 * slower.
92 */
93 static int
94 TryAddRef(PyObject *cnv, CDataObject *obj)
95 {
96 IUnknown *punk;
97 PyObject *attrdict = _PyType_GetDict((PyTypeObject *)cnv);
98 if (!attrdict) {
99 return 0;
100 }
101 int r = PyDict_Contains(attrdict, &_Py_ID(_needs_com_addref_));
102 if (r <= 0) {
103 return r;
104 }
105
106 punk = *(IUnknown **)obj->b_ptr;
107 if (punk)
108 punk->lpVtbl->AddRef(punk);
109 return 0;
110 }
111 #endif
112
113 /******************************************************************************
114 *
115 * Call the python object with all arguments
116 *
117 */
118
119 // BEWARE: The GIL needs to be held throughout the function
120 static void _CallPythonObject(ctypes_state *st,
121 void *mem,
122 ffi_type *restype,
123 SETFUNC setfunc,
124 PyObject *callable,
125 PyObject *converters,
126 int flags,
127 void **pArgs)
128 {
129 PyObject *result = NULL;
130 Py_ssize_t i = 0, j = 0, nargs = 0;
131 PyObject *error_object = NULL;
132 int *space;
133
134 assert(PyTuple_Check(converters));
135 nargs = PyTuple_GET_SIZE(converters);
136 assert(nargs <= CTYPES_MAX_ARGCOUNT);
137 PyObject **args = alloca(nargs * sizeof(PyObject *));
138 PyObject **cnvs = PySequence_Fast_ITEMS(converters);
139 for (i = 0; i < nargs; i++) {
140 PyObject *cnv = cnvs[i]; // borrowed ref
141
142 StgInfo *info;
143 if (PyStgInfo_FromType(st, cnv, &info) < 0) {
144 goto Error;
145 }
146
147 if (info && info->getfunc && !_ctypes_simple_instance(st, cnv)) {
148 PyObject *v = info->getfunc(*pArgs, info->size);
149 if (!v) {
150 goto Error;
151 }
152 args[i] = v;
153 /* XXX XXX XX
154 We have the problem that c_byte or c_short have info->size of
155 1 resp. 4, but these parameters are pushed as sizeof(int) bytes.
156 BTW, the same problem occurs when they are pushed as parameters
157 */
158 }
159 else if (info) {
160 /* Hm, shouldn't we use PyCData_AtAddress() or something like that instead? */
161 CDataObject *obj = (CDataObject *)_PyObject_CallNoArgs(cnv);
162 if (!obj) {
163 goto Error;
164 }
165 if (!CDataObject_Check(st, obj)) {
166 PyErr_Format(PyExc_TypeError,
167 "%R returned unexpected result of type %T", cnv, obj);
168 Py_DECREF(obj);
169 goto Error;
170 }
171 memcpy(obj->b_ptr, *pArgs, info->size);
172 args[i] = (PyObject *)obj;
173 #ifdef MS_WIN32
174 if (TryAddRef(cnv, obj) < 0) {
175 goto Error;
176 }
177 #endif
178 } else {
179 PyErr_Format(PyExc_TypeError,
180 "cannot build parameter of type %R", cnv);
181 goto Error;
182 }
183 /* XXX error handling! */
184 pArgs++;
185 }
186
187 if (flags & (FUNCFLAG_USE_ERRNO | FUNCFLAG_USE_LASTERROR)) {
188 error_object = _ctypes_get_errobj(st, &space);
189 if (error_object == NULL) {
190 PyErr_FormatUnraisable(
191 "Exception ignored while setting error for "
192 "ctypes callback function %R",
193 callable);
194 goto Done;
195 }
196 if (flags & FUNCFLAG_USE_ERRNO) {
197 int temp = space[0];
198 space[0] = errno;
199 errno = temp;
200 }
201 #ifdef MS_WIN32
202 if (flags & FUNCFLAG_USE_LASTERROR) {
203 int temp = space[1];
204 space[1] = GetLastError();
205 SetLastError(temp);
206 }
207 #endif
208 }
209
210 result = PyObject_Vectorcall(callable, args, nargs, NULL);
211 if (result == NULL) {
212 PyErr_FormatUnraisable("Exception ignored while "
213 "calling ctypes callback function %R",
214 callable);
215 }
216
217 #ifdef MS_WIN32
218 if (flags & FUNCFLAG_USE_LASTERROR) {
219 int temp = space[1];
220 space[1] = GetLastError();
221 SetLastError(temp);
222 }
223 #endif
224 if (flags & FUNCFLAG_USE_ERRNO) {
225 int temp = space[0];
226 space[0] = errno;
227 errno = temp;
228 }
229 Py_XDECREF(error_object);
230
231 if (restype != &ffi_type_void && result) {
232 assert(setfunc);
233
234 #ifdef WORDS_BIGENDIAN
235 /* See the corresponding code in _ctypes_callproc():
236 in callproc.c, around line 1219. */
237 if (restype->type != FFI_TYPE_FLOAT && restype->size < sizeof(ffi_arg)) {
238 mem = (char *)mem + sizeof(ffi_arg) - restype->size;
239 }
240 #endif
241
242 /* keep is an object we have to keep alive so that the result
243 stays valid. If there is no such object, the setfunc will
244 have returned Py_None.
245
246 If there is such an object, we have no choice than to keep
247 it alive forever - but a refcount and/or memory leak will
248 be the result. EXCEPT when restype is py_object - Python
249 itself knows how to manage the refcount of these objects.
250 */
251 PyObject *keep = setfunc(mem, result, restype->size);
252
253 if (keep == NULL) {
254 /* Could not convert callback result. */
255 PyErr_FormatUnraisable(
256 "Exception ignored while converting result "
257 "of ctypes callback function %R",
258 callable);
259 }
260 else if (setfunc != _ctypes_get_fielddesc("O")->setfunc) {
261 if (keep == Py_None) {
262 /* Nothing to keep */
263 Py_DECREF(keep);
264 }
265 else if (PyErr_WarnEx(PyExc_RuntimeWarning,
266 "memory leak in callback function.",
267 1) == -1) {
268 PyErr_FormatUnraisable(
269 "Exception ignored while converting result "
270 "of ctypes callback function %R",
271 callable);
272 }
273 }
274 }
275
276 Py_XDECREF(result);
277
278 Done:
279 for (j = 0; j < i; j++) {
280 Py_DECREF(args[j]);
281 }
282 return;
283
284 Error:
285 PyErr_FormatUnraisable(
286 "Exception ignored while creating argument %zd for "
287 "ctypes callback function %R",
288 i, callable);
289 goto Done;
290 }
291
292 static void closure_fcn(ffi_cif *cif,
293 void *resp,
294 void **args,
295 void *userdata)
296 {
297 PyGILState_STATE state = PyGILState_Ensure();
298
299 CThunkObject *p = (CThunkObject *)userdata;
300 ctypes_state *st = get_module_state_by_class(Py_TYPE(p));
301
302 _CallPythonObject(st,
303 resp,
304 p->ffi_restype,
305 p->setfunc,
306 p->callable,
307 p->converters,
308 p->flags,
309 args);
310
311 PyGILState_Release(state);
312 }
313
314 static CThunkObject* CThunkObject_new(ctypes_state *st, Py_ssize_t nargs)
315 {
316 CThunkObject *p;
317 Py_ssize_t i;
318
319 p = PyObject_GC_NewVar(CThunkObject, st->PyCThunk_Type, nargs);
320 if (p == NULL) {
321 return NULL;
322 }
323
324 p->pcl_write = NULL;
325 p->pcl_exec = NULL;
326 memset(&p->cif, 0, sizeof(p->cif));
327 p->flags = 0;
328 p->converters = NULL;
329 p->callable = NULL;
330 p->restype = NULL;
331 p->setfunc = NULL;
332 p->ffi_restype = NULL;
333
334 for (i = 0; i < nargs + 1; ++i)
335 p->atypes[i] = NULL;
336 PyObject_GC_Track((PyObject *)p);
337 return p;
338 }
339
340 CThunkObject *_ctypes_alloc_callback(ctypes_state *st,
341 PyObject *callable,
342 PyObject *converters,
343 PyObject *restype,
344 int flags)
345 {
346 int result;
347 CThunkObject *p;
348 Py_ssize_t nargs, i;
349 ffi_abi cc;
350
351 assert(PyTuple_Check(converters));
352 nargs = PyTuple_GET_SIZE(converters);
353 p = CThunkObject_new(st, nargs);
354 if (p == NULL)
355 return NULL;
356
357 assert(CThunk_CheckExact(st, (PyObject *)p));
358
359 p->pcl_write = Py_ffi_closure_alloc(sizeof(ffi_closure), &p->pcl_exec);
360 if (p->pcl_write == NULL) {
361 PyErr_NoMemory();
362 goto error;
363 }
364
365 p->flags = flags;
366 PyObject **cnvs = PySequence_Fast_ITEMS(converters);
367 for (i = 0; i < nargs; ++i) {
368 PyObject *cnv = cnvs[i]; // borrowed ref
369 p->atypes[i] = _ctypes_get_ffi_type(st, cnv);
370 }
371 p->atypes[i] = NULL;
372
373 p->restype = Py_NewRef(restype);
374 if (restype == Py_None) {
375 p->setfunc = NULL;
376 p->ffi_restype = &ffi_type_void;
377 } else {
378 StgInfo *info;
379 if (PyStgInfo_FromType(st, restype, &info) < 0) {
380 goto error;
381 }
382
383 if (info == NULL || info->setfunc == NULL) {
384 PyErr_SetString(PyExc_TypeError,
385 "invalid result type for callback function");
386 goto error;
387 }
388 p->setfunc = info->setfunc;
389 p->ffi_restype = &info->ffi_type_pointer;
390 }
391
392 cc = FFI_DEFAULT_ABI;
393 #if defined(MS_WIN32) && !defined(_WIN32_WCE) && !defined(MS_WIN64) && !defined(_M_ARM)
394 if ((flags & FUNCFLAG_CDECL) == 0)
395 cc = FFI_STDCALL;
396 #endif
397 result = ffi_prep_cif(&p->cif, cc,
398 Py_SAFE_DOWNCAST(nargs, Py_ssize_t, int),
399 p->ffi_restype,
400 &p->atypes[0]);
401 if (result != FFI_OK) {
402 PyErr_Format(PyExc_RuntimeError,
403 "ffi_prep_cif failed with %d", result);
404 goto error;
405 }
406
407
408 #if HAVE_FFI_PREP_CLOSURE_LOC
409 # ifdef USING_APPLE_OS_LIBFFI
410 # ifdef HAVE_BUILTIN_AVAILABLE
411 # define HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)
412 # else
413 # define HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME (ffi_prep_closure_loc != NULL)
414 # endif
415 # else
416 # define HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME 1
417 # endif
418 if (HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME) {
419 result = ffi_prep_closure_loc(p->pcl_write, &p->cif, closure_fcn,
420 p,
421 p->pcl_exec);
422 } else
423 #endif
424 {
425 #if defined(USING_APPLE_OS_LIBFFI) && defined(__arm64__)
426 PyErr_Format(PyExc_NotImplementedError, "ffi_prep_closure_loc() is missing");
427 goto error;
428 #else
429 // GH-85272, GH-23327, GH-100540: On macOS,
430 // HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME is checked at runtime because the
431 // symbol might not be available at runtime when targeting macOS 10.14
432 // or earlier. Even if ffi_prep_closure_loc() is called in practice,
433 // the deprecated ffi_prep_closure() code path is needed if
434 // HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME is false.
435 //
436 // On non-macOS platforms, even if HAVE_FFI_PREP_CLOSURE_LOC_RUNTIME is
437 // defined as 1 and ffi_prep_closure_loc() is used in practice, this
438 // code path is still compiled and emits a compiler warning. The
439 // deprecated code path is likely to be removed by a simple
440 // optimization pass.
441 //
442 // Ignore the compiler warning on the ffi_prep_closure() deprecation,
443 // rather than using complex #if/#else code paths for the different
444 // platforms.
445 _Py_COMP_DIAG_PUSH
446 _Py_COMP_DIAG_IGNORE_DEPR_DECLS
447 result = ffi_prep_closure(p->pcl_write, &p->cif, closure_fcn, p);
448 _Py_COMP_DIAG_POP
449 #endif
450 }
451
452 if (result != FFI_OK) {
453 PyErr_Format(PyExc_RuntimeError,
454 "ffi_prep_closure failed with %d", result);
455 goto error;
456 }
457
458 p->converters = Py_NewRef(converters);
459 p->callable = Py_NewRef(callable);
460 return p;
461
462 error:
463 Py_XDECREF(p);
464 return NULL;
465 }
466
467 #ifdef MS_WIN32
468
469 static void LoadPython(void)
470 {
471 if (!Py_IsInitialized()) {
472 Py_Initialize();
473 }
474 }
475
476 /******************************************************************/
477
478 long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
479 {
480 PyObject *func, *result;
481 long retval;
482
483 func = PyImport_ImportModuleAttrString("ctypes", "DllGetClassObject");
484 if (!func) {
485 /* There has been a warning before about this already */
486 goto error;
487 }
488
489 {
490 PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid);
491 if (py_rclsid == NULL) {
492 Py_DECREF(func);
493 goto error;
494 }
495 PyObject *py_riid = PyLong_FromVoidPtr((void *)riid);
496 if (py_riid == NULL) {
497 Py_DECREF(func);
498 Py_DECREF(py_rclsid);
499 goto error;
500 }
501 PyObject *py_ppv = PyLong_FromVoidPtr(ppv);
502 if (py_ppv == NULL) {
503 Py_DECREF(py_rclsid);
504 Py_DECREF(py_riid);
505 Py_DECREF(func);
506 goto error;
507 }
508 result = PyObject_CallFunctionObjArgs(func,
509 py_rclsid,
510 py_riid,
511 py_ppv,
512 NULL);
513 Py_DECREF(py_rclsid);
514 Py_DECREF(py_riid);
515 Py_DECREF(py_ppv);
516 }
517 Py_DECREF(func);
518 if (!result) {
519 goto error;
520 }
521
522 retval = PyLong_AsLong(result);
523 if (PyErr_Occurred()) {
524 Py_DECREF(result);
525 goto error;
526 }
527 Py_DECREF(result);
528 return retval;
529
530 error:
531 PyErr_FormatUnraisable("Exception ignored while calling "
532 "ctypes.DllGetClassObject");
533 return E_FAIL;
534 }
535
536 STDAPI DllGetClassObject(REFCLSID rclsid,
537 REFIID riid,
538 LPVOID *ppv)
539 {
540 long result;
541 PyGILState_STATE state;
542
543 LoadPython();
544 state = PyGILState_Ensure();
545 result = Call_GetClassObject(rclsid, riid, ppv);
546 PyGILState_Release(state);
547 return result;
548 }
549
550 long Call_CanUnloadNow(void)
551 {
552 PyObject *func = PyImport_ImportModuleAttrString("ctypes",
553 "DllCanUnloadNow");
554 if (!func) {
555 goto error;
556 }
557
558 PyObject *result = _PyObject_CallNoArgs(func);
559 Py_DECREF(func);
560 if (!result) {
561 goto error;
562 }
563
564 long retval = PyLong_AsLong(result);
565 if (PyErr_Occurred()) {
566 Py_DECREF(result);
567 goto error;
568 }
569 Py_DECREF(result);
570 return retval;
571
572 error:
573 PyErr_FormatUnraisable("Exception ignored while calling "
574 "ctypes.DllCanUnloadNow");
575 return E_FAIL;
576 }
577
578 /*
579 DllRegisterServer and DllUnregisterServer still missing
580 */
581
582 STDAPI DllCanUnloadNow(void)
583 {
584 long result;
585 PyGILState_STATE state = PyGILState_Ensure();
586 result = Call_CanUnloadNow();
587 PyGILState_Release(state);
588 return result;
589 }
590
591 #ifndef Py_NO_ENABLE_SHARED
592 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvRes)
593 {
594 switch(fdwReason) {
595 case DLL_PROCESS_ATTACH:
596 DisableThreadLibraryCalls(hinstDLL);
597 break;
598 }
599 return TRUE;
600 }
601 #endif
602
603 #endif
604
605 /*
606 Local Variables:
607 compile-command: "cd .. && python setup.py -q build_ext"
608 End:
609 */