• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #if defined (__SVR4) && defined (__sun)
2 #   include <alloca.h>
3 #endif
4 
5 #include "pycore_moduleobject.h"  // _PyModule_GetState()
6 #include "pycore_typeobject.h"    // _PyType_GetModuleState()
7 
8 #ifndef MS_WIN32
9 #define max(a, b) ((a) > (b) ? (a) : (b))
10 #define min(a, b) ((a) < (b) ? (a) : (b))
11 
12 #define PARAMFLAG_FIN 0x1
13 #define PARAMFLAG_FOUT 0x2
14 #define PARAMFLAG_FLCID 0x4
15 #endif
16 
17 /*
18  * bpo-13097: Max number of arguments CFuncPtr._argtypes_ and
19  * _ctypes_callproc() will accept.
20  *
21  * This limit is enforced for the `alloca()` call in `_ctypes_callproc`,
22  * to avoid allocating a massive buffer on the stack.
23  */
24 #ifndef CTYPES_MAX_ARGCOUNT
25   #ifdef __EMSCRIPTEN__
26     #define CTYPES_MAX_ARGCOUNT 1000
27   #else
28     #define CTYPES_MAX_ARGCOUNT 1024
29   #endif
30 #endif
31 
32 #if defined(__has_builtin)
33 #if __has_builtin(__builtin_available)
34 #define HAVE_BUILTIN_AVAILABLE 1
35 #endif
36 #endif
37 
38 #ifdef MS_WIN32
39 #include <Unknwn.h> // for IUnknown interface
40 #endif
41 
42 typedef struct {
43     PyTypeObject *DictRemover_Type;
44     PyTypeObject *PyCArg_Type;
45     PyTypeObject *PyCField_Type;
46     PyTypeObject *PyCThunk_Type;
47     PyTypeObject *StructParam_Type;
48     PyTypeObject *PyCType_Type;
49     PyTypeObject *PyCStructType_Type;
50     PyTypeObject *UnionType_Type;
51     PyTypeObject *PyCPointerType_Type;
52     PyTypeObject *PyCArrayType_Type;
53     PyTypeObject *PyCSimpleType_Type;
54     PyTypeObject *PyCFuncPtrType_Type;
55     PyTypeObject *PyCData_Type;
56     PyTypeObject *Struct_Type;
57     PyTypeObject *Union_Type;
58     PyTypeObject *PyCArray_Type;
59     PyTypeObject *Simple_Type;
60     PyTypeObject *PyCPointer_Type;
61     PyTypeObject *PyCFuncPtr_Type;
62 #ifdef MS_WIN32
63     PyTypeObject *PyComError_Type;
64 #endif
65     /* This dict maps ctypes types to POINTER types */
66     PyObject *_ctypes_ptrtype_cache;
67     /* a callable object used for unpickling:
68        strong reference to _ctypes._unpickle() function */
69     PyObject *_unpickle;
70     PyObject *array_cache;
71     PyObject *error_object_name;  // callproc.c
72     PyObject *PyExc_ArgError;
73     PyObject *swapped_suffix;
74 } ctypes_state;
75 
76 
77 extern struct PyModuleDef _ctypesmodule;
78 
79 
80 static inline ctypes_state *
get_module_state(PyObject * module)81 get_module_state(PyObject *module)
82 {
83     void *state = _PyModule_GetState(module);
84     assert(state != NULL);
85     return (ctypes_state *)state;
86 }
87 
88 static inline ctypes_state *
get_module_state_by_class(PyTypeObject * cls)89 get_module_state_by_class(PyTypeObject *cls)
90 {
91     ctypes_state *state = (ctypes_state *)_PyType_GetModuleState(cls);
92     assert(state != NULL);
93     return state;
94 }
95 
96 static inline ctypes_state *
get_module_state_by_def(PyTypeObject * cls)97 get_module_state_by_def(PyTypeObject *cls)
98 {
99     PyObject *mod = PyType_GetModuleByDef(cls, &_ctypesmodule);
100     assert(mod != NULL);
101     return get_module_state(mod);
102 }
103 
104 
105 extern PyType_Spec carg_spec;
106 extern PyType_Spec cfield_spec;
107 extern PyType_Spec cthunk_spec;
108 
109 typedef struct tagPyCArgObject PyCArgObject;
110 typedef struct tagCDataObject CDataObject;
111 typedef PyObject *(* GETFUNC)(void *, Py_ssize_t size);
112 typedef PyObject *(* SETFUNC)(void *, PyObject *value, Py_ssize_t size);
113 typedef PyCArgObject *(* PARAMFUNC)(ctypes_state *st, CDataObject *obj);
114 
115 /* A default buffer in CDataObject, which can be used for small C types.  If
116 this buffer is too small, PyMem_Malloc will be called to create a larger one,
117 and this one is not used.
118 
119 Making CDataObject a variable size object would be a better solution, but more
120 difficult in the presence of PyCFuncPtrObject.  Maybe later.
121 */
122 union value {
123                 char c[16];
124                 short s;
125                 int i;
126                 long l;
127                 float f;
128                 double d;
129                 long long ll;
130                 long double D;
131 };
132 
133 /*
134   Hm. Are there CDataObject's which do not need the b_objects member?  In
135   this case we probably should introduce b_flags to mark it as present...  If
136   b_objects is not present/unused b_length is unneeded as well.
137 */
138 
139 struct tagCDataObject {
140     PyObject_HEAD
141     char *b_ptr;                /* pointer to memory block */
142     int  b_needsfree;           /* need _we_ free the memory? */
143     CDataObject *b_base;        /* pointer to base object or NULL */
144     Py_ssize_t b_size;          /* size of memory block in bytes */
145     Py_ssize_t b_length;        /* number of references we need */
146     Py_ssize_t b_index;         /* index of this object into base's
147                                b_object list */
148     PyObject *b_objects;        /* dictionary of references we need to keep, or Py_None */
149     union value b_value;
150 };
151 
152 typedef struct {
153     PyObject_VAR_HEAD
154     ffi_closure *pcl_write; /* the C callable, writeable */
155     void *pcl_exec;         /* the C callable, executable */
156     ffi_cif cif;
157     int flags;
158     PyObject *converters;
159     PyObject *callable;
160     PyObject *restype;
161     SETFUNC setfunc;
162     ffi_type *ffi_restype;
163     ffi_type *atypes[1];
164 } CThunkObject;
165 #define CThunk_CheckExact(st, v)        Py_IS_TYPE(v, st->PyCThunk_Type)
166 
167 typedef struct {
168     /* First part identical to tagCDataObject */
169     PyObject_HEAD
170     char *b_ptr;                /* pointer to memory block */
171     int  b_needsfree;           /* need _we_ free the memory? */
172     CDataObject *b_base;        /* pointer to base object or NULL */
173     Py_ssize_t b_size;          /* size of memory block in bytes */
174     Py_ssize_t b_length;        /* number of references we need */
175     Py_ssize_t b_index;         /* index of this object into base's
176                                    b_object list */
177     PyObject *b_objects;        /* list of references we need to keep */
178     union value b_value;
179     /* end of tagCDataObject, additional fields follow */
180 
181     CThunkObject *thunk;
182     PyObject *callable;
183 
184     /* These two fields will override the ones in the type's stginfo if
185        they are set */
186     PyObject *converters;
187     PyObject *argtypes;
188     PyObject *restype;
189     PyObject *checker;
190     PyObject *errcheck;
191 #ifdef MS_WIN32
192     int index;
193     GUID *iid;
194 #endif
195     PyObject *paramflags;
196 } PyCFuncPtrObject;
197 
198 extern int PyCStructUnionType_update_stginfo(PyObject *fields, PyObject *type, int isStruct);
199 extern int PyType_stginfo(PyTypeObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
200 extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
201 
202 
203 
204 #define CDataObject_CheckExact(st, v)  Py_IS_TYPE((v), (st)->PyCData_Type)
205 #define CDataObject_Check(st, v)       PyObject_TypeCheck((v), (st)->PyCData_Type)
206 #define _CDataObject_HasExternalBuffer(v)  ((v)->b_ptr != (char *)&(v)->b_value)
207 
208 #define PyCSimpleTypeObject_CheckExact(st, v)  Py_IS_TYPE((v), (st)->PyCSimpleType_Type)
209 #define PyCSimpleTypeObject_Check(st, v)       PyObject_TypeCheck((v), (st)->PyCSimpleType_Type)
210 
211 extern struct fielddesc *_ctypes_get_fielddesc(const char *fmt);
212 
213 
214 extern PyObject *
215 PyCField_FromDesc(ctypes_state *st, PyObject *desc, Py_ssize_t index,
216                 Py_ssize_t *pfield_size, int bitsize, int *pbitofs,
217                 Py_ssize_t *psize, Py_ssize_t *poffset, Py_ssize_t *palign,
218                 int pack, int is_big_endian);
219 
220 extern PyObject *PyCData_AtAddress(ctypes_state *st, PyObject *type, void *buf);
221 extern PyObject *PyCData_FromBytes(ctypes_state *st, PyObject *type, char *data, Py_ssize_t length);
222 
223 #define PyCArrayTypeObject_Check(st, v)   PyObject_TypeCheck((v), (st)->PyCArrayType_Type)
224 #define ArrayObject_Check(st, v)          PyObject_TypeCheck((v), (st)->PyCArray_Type)
225 #define PointerObject_Check(st, v)        PyObject_TypeCheck((v), (st)->PyCPointer_Type)
226 #define PyCPointerTypeObject_Check(st, v) PyObject_TypeCheck((v), (st)->PyCPointerType_Type)
227 #define PyCFuncPtrObject_Check(st,v)      PyObject_TypeCheck((v), (st)->PyCFuncPtr_Type)
228 #define PyCFuncPtrTypeObject_Check(st, v) PyObject_TypeCheck((v), (st)->PyCFuncPtrType_Type)
229 #define PyCStructTypeObject_Check(st, v)  PyObject_TypeCheck((v), (st)->PyCStructType_Type)
230 
231 extern PyObject *
232 PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length);
233 
234 extern PyMethodDef _ctypes_module_methods[];
235 
236 extern CThunkObject *_ctypes_alloc_callback(ctypes_state *st,
237                                            PyObject *callable,
238                                            PyObject *converters,
239                                            PyObject *restype,
240                                            int flags);
241 /* a table entry describing a predefined ctypes type */
242 struct fielddesc {
243     char code;
244     SETFUNC setfunc;
245     GETFUNC getfunc;
246     ffi_type *pffi_type; /* always statically allocated */
247     SETFUNC setfunc_swapped;
248     GETFUNC getfunc_swapped;
249 };
250 
251 typedef struct {
252     PyObject_HEAD
253     Py_ssize_t offset;
254     Py_ssize_t size;
255     Py_ssize_t index;                   /* Index into CDataObject's
256                                        object array */
257     PyObject *proto;                    /* a type or NULL */
258     GETFUNC getfunc;                    /* getter function if proto is NULL */
259     SETFUNC setfunc;                    /* setter function if proto is NULL */
260     int anonymous;
261 } CFieldObject;
262 
263 /****************************************************************
264  StgInfo
265 
266  Since Python 3.13, ctypes-specific type information is stored in the
267  corresponding type object, in a `StgInfo` struct accessed by the helpers
268  below.
269  Before that, each type's `tp_dict` was set to a dict *subclass* that included
270  the fields that are now in StgInfo. The mechanism was called "StgDict"; a few
271  references to that name might remain.
272 
273  Functions for accessing StgInfo are `static inline` for performance;
274  see later in this file.
275 
276  ****************************************************************
277 
278  StgInfo fields
279 
280  setfunc and getfunc is only set for simple data types, it is copied from the
281  corresponding fielddesc entry.  These are functions to set and get the value
282  in a memory block.
283  They should probably by used by other types as well.
284 
285  proto is only used for Pointer and Array types - it points to the item type
286  object.
287 
288  Probably all the magic ctypes methods (like from_param) should have C
289  callable wrappers in the StgInfo.  For simple data type, for example,
290  the fielddesc table could have entries for C codec from_param functions or
291  other methods as well, if a subtype overrides this method in Python at
292  construction time, or assigns to it later, tp_setattro should update the
293  StgInfo function to a generic one.
294 
295  Currently, PyCFuncPtr types have 'converters' and 'checker' entries in their
296  type dict.  They are only used to cache attributes from other entries, which
297  is wrong.
298 
299  One use case is the .value attribute that all simple types have.  But some
300  complex structures, like VARIANT, represent a single value also, and should
301  have this attribute.
302 
303  Another use case is a _check_retval_ function, which is called when a ctypes
304  type is used as return type of a function to validate and compute the return
305  value.
306 
307  Common ctypes protocol:
308 
309   - setfunc: store a python value in a memory block
310   - getfunc: convert data from a memory block into a python value
311 
312   - checkfunc: validate and convert a return value from a function call
313   - toparamfunc: convert a python value into a function argument
314 
315 *****************************************************************/
316 
317 typedef struct {
318     int initialized;
319     Py_ssize_t size;            /* number of bytes */
320     Py_ssize_t align;           /* alignment requirements */
321     Py_ssize_t length;          /* number of fields */
322     ffi_type ffi_type_pointer;
323     PyObject *proto;            /* Only for Pointer/ArrayObject */
324     SETFUNC setfunc;            /* Only for simple objects */
325     GETFUNC getfunc;            /* Only for simple objects */
326     PARAMFUNC paramfunc;
327 
328     /* Following fields only used by PyCFuncPtrType_Type instances */
329     PyObject *argtypes;         /* tuple of CDataObjects */
330     PyObject *converters;       /* tuple([t.from_param for t in argtypes]) */
331     PyObject *restype;          /* CDataObject or NULL */
332     PyObject *checker;
333     PyObject *module;
334     int flags;                  /* calling convention and such */
335 
336     /* pep3118 fields, pointers need PyMem_Free */
337     char *format;
338     int ndim;
339     Py_ssize_t *shape;
340 /*      Py_ssize_t *strides;    */ /* unused in ctypes */
341 /*      Py_ssize_t *suboffsets; */ /* unused in ctypes */
342 } StgInfo;
343 
344 extern int PyCStgInfo_clone(StgInfo *dst_info, StgInfo *src_info);
345 extern void ctype_clear_stginfo(StgInfo *info);
346 
347 typedef int(* PPROC)(void);
348 
349 PyObject *_ctypes_callproc(ctypes_state *st,
350                     PPROC pProc,
351                     PyObject *arguments,
352 #ifdef MS_WIN32
353                     IUnknown *pIUnk,
354                     GUID *iid,
355 #endif
356                     int flags,
357                     PyObject *argtypes,
358                     PyObject *restype,
359                     PyObject *checker);
360 
361 
362 #define FUNCFLAG_STDCALL 0x0
363 #define FUNCFLAG_CDECL   0x1
364 #define FUNCFLAG_HRESULT 0x2
365 #define FUNCFLAG_PYTHONAPI 0x4
366 #define FUNCFLAG_USE_ERRNO 0x8
367 #define FUNCFLAG_USE_LASTERROR 0x10
368 
369 #define TYPEFLAG_ISPOINTER 0x100
370 #define TYPEFLAG_HASPOINTER 0x200
371 #define TYPEFLAG_HASUNION 0x400
372 #define TYPEFLAG_HASBITFIELD 0x800
373 
374 #define DICTFLAG_FINAL 0x1000
375 
376 struct tagPyCArgObject {
377     PyObject_HEAD
378     ffi_type *pffi_type;
379     char tag;
380     union {
381         char c;
382         char b;
383         short h;
384         int i;
385         long l;
386         long long q;
387         long double D;
388         double d;
389         float f;
390         void *p;
391     } value;
392     PyObject *obj;
393     Py_ssize_t size; /* for the 'V' tag */
394 };
395 
396 #define PyCArg_CheckExact(st, v)        Py_IS_TYPE(v, st->PyCArg_Type)
397 extern PyCArgObject *PyCArgObject_new(ctypes_state *st);
398 
399 extern PyObject *
400 PyCData_get(ctypes_state *st, PyObject *type, GETFUNC getfunc, PyObject *src,
401           Py_ssize_t index, Py_ssize_t size, char *ptr);
402 
403 extern int
404 PyCData_set(ctypes_state *st,
405           PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
406           Py_ssize_t index, Py_ssize_t size, char *ptr);
407 
408 extern void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...);
409 
410 struct basespec {
411     CDataObject *base;
412     Py_ssize_t index;
413     char *adr;
414 };
415 
416 extern char basespec_string[];
417 
418 extern ffi_type *_ctypes_get_ffi_type(ctypes_state *st, PyObject *obj);
419 
420 extern char *_ctypes_conversion_encoding;
421 extern char *_ctypes_conversion_errors;
422 
423 
424 extern void _ctypes_free_closure(void *);
425 extern void *_ctypes_alloc_closure(void);
426 
427 extern PyObject *PyCData_FromBaseObj(ctypes_state *st, PyObject *type,
428                                      PyObject *base, Py_ssize_t index, char *adr);
429 extern char *_ctypes_alloc_format_string(const char *prefix, const char *suffix);
430 extern char *_ctypes_alloc_format_string_with_shape(int ndim,
431                                                 const Py_ssize_t *shape,
432                                                 const char *prefix, const char *suffix);
433 
434 extern int _ctypes_simple_instance(ctypes_state *st, PyObject *obj);
435 
436 PyObject *_ctypes_get_errobj(ctypes_state *st, int **pspace);
437 
438 #ifdef USING_MALLOC_CLOSURE_DOT_C
439 void Py_ffi_closure_free(void *p);
440 void *Py_ffi_closure_alloc(size_t size, void** codeloc);
441 #else
442 #define Py_ffi_closure_free ffi_closure_free
443 #define Py_ffi_closure_alloc ffi_closure_alloc
444 #endif
445 
446 
447 /****************************************************************
448  * Accessing StgInfo -- these are inlined for performance reasons.
449  */
450 
451 // `PyStgInfo_From**` functions get a PyCTypeDataObject.
452 // These return -1 on error, 0 if "not found", 1 on OK.
453 // (Currently, these do not return -1 in practice. This might change
454 // in the future.)
455 
456 //
457 // Common helper:
458 static inline int
_stginfo_from_type(ctypes_state * state,PyTypeObject * type,StgInfo ** result)459 _stginfo_from_type(ctypes_state *state, PyTypeObject *type, StgInfo **result)
460 {
461     *result = NULL;
462     if (!PyObject_IsInstance((PyObject *)type, (PyObject *)state->PyCType_Type)) {
463         // not a ctypes class.
464         return 0;
465     }
466     StgInfo *info = PyObject_GetTypeData((PyObject *)type, state->PyCType_Type);
467     assert(info != NULL);
468     if (!info->initialized) {
469         // StgInfo is not initialized. This happens in abstract classes.
470         return 0;
471     }
472     *result = info;
473     return 1;
474 }
475 // from a type:
476 static inline int
PyStgInfo_FromType(ctypes_state * state,PyObject * type,StgInfo ** result)477 PyStgInfo_FromType(ctypes_state *state, PyObject *type, StgInfo **result)
478 {
479     return _stginfo_from_type(state, (PyTypeObject *)type, result);
480 }
481 // from an instance:
482 static inline int
PyStgInfo_FromObject(ctypes_state * state,PyObject * obj,StgInfo ** result)483 PyStgInfo_FromObject(ctypes_state *state, PyObject *obj, StgInfo **result)
484 {
485     return _stginfo_from_type(state, Py_TYPE(obj), result);
486 }
487 // from either a type or an instance:
488 static inline int
PyStgInfo_FromAny(ctypes_state * state,PyObject * obj,StgInfo ** result)489 PyStgInfo_FromAny(ctypes_state *state, PyObject *obj, StgInfo **result)
490 {
491     if (PyType_Check(obj)) {
492         return _stginfo_from_type(state, (PyTypeObject *)obj, result);
493     }
494     return _stginfo_from_type(state, Py_TYPE(obj), result);
495 }
496 
497 /* A variant of PyStgInfo_FromType that doesn't need the state,
498  * so it can be called from finalization functions when the module
499  * state is torn down. Does no checks; cannot fail.
500  * This inlines the current implementation PyObject_GetTypeData,
501  * so it might break in the future.
502  */
503 static inline StgInfo *
_PyStgInfo_FromType_NoState(PyObject * type)504 _PyStgInfo_FromType_NoState(PyObject *type)
505 {
506     size_t type_basicsize =_Py_SIZE_ROUND_UP(PyType_Type.tp_basicsize,
507                                              ALIGNOF_MAX_ALIGN_T);
508     return (StgInfo *)((char *)type + type_basicsize);
509 }
510 
511 // Initialize StgInfo on a newly created type
512 static inline StgInfo *
PyStgInfo_Init(ctypes_state * state,PyTypeObject * type)513 PyStgInfo_Init(ctypes_state *state, PyTypeObject *type)
514 {
515     if (!PyObject_IsInstance((PyObject *)type, (PyObject *)state->PyCType_Type)) {
516         PyErr_Format(PyExc_SystemError,
517                      "'%s' is not a ctypes class.",
518                      type->tp_name);
519         return NULL;
520     }
521     StgInfo *info = PyObject_GetTypeData((PyObject *)type, state->PyCType_Type);
522     if (info->initialized) {
523         PyErr_Format(PyExc_SystemError,
524                      "StgInfo of '%s' is already initialized.",
525                      type->tp_name);
526         return NULL;
527     }
528     PyObject *module = PyType_GetModule(state->PyCType_Type);
529     if (!module) {
530         return NULL;
531     }
532     info->module = Py_NewRef(module);
533 
534     info->initialized = 1;
535     return info;
536 }
537