| 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_dict.h" // _PyDict_SizeOf() |
| 13 |
#include <ffi.h> |
| 14 |
#ifdef MS_WIN32 |
| 15 |
# include <malloc.h> |
| 16 |
#endif |
| 17 |
#include "ctypes.h" |
| 18 |
|
| 19 |
/* This file relates to StgInfo -- type-specific information for ctypes. |
| 20 |
* See ctypes.h for details. |
| 21 |
*/ |
| 22 |
|
| 23 |
int |
| 24 |
PyCStgInfo_clone(StgInfo *dst_info, StgInfo *src_info) |
| 25 |
{ |
| 26 |
Py_ssize_t size; |
| 27 |
|
| 28 |
ctype_free_stginfo_members(dst_info); |
| 29 |
|
| 30 |
memcpy(dst_info, src_info, sizeof(StgInfo)); |
| 31 |
#ifdef Py_GIL_DISABLED |
| 32 |
dst_info->mutex = (PyMutex){0}; |
| 33 |
#endif |
| 34 |
dst_info->dict_final = 0; |
| 35 |
|
| 36 |
Py_XINCREF(dst_info->proto); |
| 37 |
Py_XINCREF(dst_info->argtypes); |
| 38 |
Py_XINCREF(dst_info->converters); |
| 39 |
Py_XINCREF(dst_info->restype); |
| 40 |
Py_XINCREF(dst_info->checker); |
| 41 |
Py_XINCREF(dst_info->module); |
| 42 |
dst_info->pointer_type = NULL; // the cache cannot be shared |
| 43 |
|
| 44 |
if (src_info->format) { |
| 45 |
dst_info->format = PyMem_Malloc(strlen(src_info->format) + 1); |
| 46 |
if (dst_info->format == NULL) { |
| 47 |
PyErr_NoMemory(); |
| 48 |
return -1; |
| 49 |
} |
| 50 |
strcpy(dst_info->format, src_info->format); |
| 51 |
} |
| 52 |
if (src_info->shape) { |
| 53 |
dst_info->shape = PyMem_Malloc(sizeof(Py_ssize_t) * src_info->ndim); |
| 54 |
if (dst_info->shape == NULL) { |
| 55 |
PyErr_NoMemory(); |
| 56 |
return -1; |
| 57 |
} |
| 58 |
memcpy(dst_info->shape, src_info->shape, |
| 59 |
sizeof(Py_ssize_t) * src_info->ndim); |
| 60 |
} |
| 61 |
|
| 62 |
if (src_info->ffi_type_pointer.elements == NULL) |
| 63 |
return 0; |
| 64 |
size = sizeof(ffi_type *) * (src_info->length + 1); |
| 65 |
dst_info->ffi_type_pointer.elements = PyMem_Malloc(size); |
| 66 |
if (dst_info->ffi_type_pointer.elements == NULL) { |
| 67 |
PyErr_NoMemory(); |
| 68 |
return -1; |
| 69 |
} |
| 70 |
memcpy(dst_info->ffi_type_pointer.elements, |
| 71 |
src_info->ffi_type_pointer.elements, |
| 72 |
size); |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
/* descr is the descriptor for a field marked as anonymous. Get all the |
| 77 |
_fields_ descriptors from descr->proto, create new descriptors with offset |
| 78 |
and index adjusted, and stuff them into type. |
| 79 |
*/ |
| 80 |
static int |
| 81 |
MakeFields(PyObject *type, CFieldObject *descr, |
| 82 |
Py_ssize_t index, Py_ssize_t offset) |
| 83 |
{ |
| 84 |
Py_ssize_t i; |
| 85 |
PyObject *fields; |
| 86 |
PyObject *fieldlist; |
| 87 |
|
| 88 |
fields = PyObject_GetAttrString(descr->proto, "_fields_"); |
| 89 |
if (fields == NULL) |
| 90 |
return -1; |
| 91 |
fieldlist = PySequence_Fast(fields, "_fields_ must be a sequence"); |
| 92 |
Py_DECREF(fields); |
| 93 |
if (fieldlist == NULL) |
| 94 |
return -1; |
| 95 |
|
| 96 |
ctypes_state *st = get_module_state_by_class(Py_TYPE(descr)); |
| 97 |
PyTypeObject *cfield_tp = st->PyCField_Type; |
| 98 |
for (i = 0; i < PySequence_Fast_GET_SIZE(fieldlist); ++i) { |
| 99 |
PyObject *pair = PySequence_Fast_GET_ITEM(fieldlist, i); /* borrowed */ |
| 100 |
PyObject *fname, *ftype, *bits; |
| 101 |
CFieldObject *fdescr; |
| 102 |
CFieldObject *new_descr; |
| 103 |
/* Convert to PyArg_UnpackTuple... */ |
| 104 |
if (!PyArg_ParseTuple(pair, "OO|O", &fname, &ftype, &bits)) { |
| 105 |
Py_DECREF(fieldlist); |
| 106 |
return -1; |
| 107 |
} |
| 108 |
fdescr = (CFieldObject *)PyObject_GetAttr(descr->proto, fname); |
| 109 |
if (fdescr == NULL) { |
| 110 |
Py_DECREF(fieldlist); |
| 111 |
return -1; |
| 112 |
} |
| 113 |
if (!Py_IS_TYPE(fdescr, cfield_tp)) { |
| 114 |
PyErr_SetString(PyExc_TypeError, "unexpected type"); |
| 115 |
Py_DECREF(fdescr); |
| 116 |
Py_DECREF(fieldlist); |
| 117 |
return -1; |
| 118 |
} |
| 119 |
if (fdescr->anonymous) { |
| 120 |
int rc = MakeFields(type, fdescr, |
| 121 |
index + fdescr->index, |
| 122 |
offset + fdescr->byte_offset); |
| 123 |
Py_DECREF(fdescr); |
| 124 |
if (rc == -1) { |
| 125 |
Py_DECREF(fieldlist); |
| 126 |
return -1; |
| 127 |
} |
| 128 |
continue; |
| 129 |
} |
| 130 |
new_descr = (CFieldObject *)cfield_tp->tp_alloc(cfield_tp, 0); |
| 131 |
if (new_descr == NULL) { |
| 132 |
Py_DECREF(fdescr); |
| 133 |
Py_DECREF(fieldlist); |
| 134 |
return -1; |
| 135 |
} |
| 136 |
assert(Py_IS_TYPE(new_descr, cfield_tp)); |
| 137 |
new_descr->byte_size = fdescr->byte_size; |
| 138 |
new_descr->byte_offset = fdescr->byte_offset + offset; |
| 139 |
new_descr->bitfield_size = fdescr->bitfield_size; |
| 140 |
new_descr->bit_offset = fdescr->bit_offset; |
| 141 |
new_descr->index = fdescr->index + index; |
| 142 |
new_descr->proto = Py_XNewRef(fdescr->proto); |
| 143 |
new_descr->getfunc = fdescr->getfunc; |
| 144 |
new_descr->setfunc = fdescr->setfunc; |
| 145 |
new_descr->name = Py_NewRef(fdescr->name); |
| 146 |
new_descr->anonymous = fdescr->anonymous; |
| 147 |
|
| 148 |
Py_DECREF(fdescr); |
| 149 |
|
| 150 |
if (-1 == PyObject_SetAttr(type, fname, (PyObject *)new_descr)) { |
| 151 |
Py_DECREF(fieldlist); |
| 152 |
Py_DECREF(new_descr); |
| 153 |
return -1; |
| 154 |
} |
| 155 |
Py_DECREF(new_descr); |
| 156 |
} |
| 157 |
Py_DECREF(fieldlist); |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
/* Iterate over the names in the type's _anonymous_ attribute, if present, |
| 162 |
*/ |
| 163 |
static int |
| 164 |
MakeAnonFields(PyObject *type) |
| 165 |
{ |
| 166 |
PyObject *anon; |
| 167 |
PyObject *anon_names; |
| 168 |
Py_ssize_t i; |
| 169 |
|
| 170 |
if (PyObject_GetOptionalAttr(type, &_Py_ID(_anonymous_), &anon) < 0) { |
| 171 |
return -1; |
| 172 |
} |
| 173 |
if (anon == NULL) { |
| 174 |
return 0; |
| 175 |
} |
| 176 |
anon_names = PySequence_Fast(anon, "_anonymous_ must be a sequence"); |
| 177 |
Py_DECREF(anon); |
| 178 |
if (anon_names == NULL) |
| 179 |
return -1; |
| 180 |
|
| 181 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); |
| 182 |
PyTypeObject *cfield_tp = st->PyCField_Type; |
| 183 |
for (i = 0; i < PySequence_Fast_GET_SIZE(anon_names); ++i) { |
| 184 |
PyObject *fname = PySequence_Fast_GET_ITEM(anon_names, i); /* borrowed */ |
| 185 |
CFieldObject *descr = (CFieldObject *)PyObject_GetAttr(type, fname); |
| 186 |
if (descr == NULL) { |
| 187 |
Py_DECREF(anon_names); |
| 188 |
return -1; |
| 189 |
} |
| 190 |
if (!Py_IS_TYPE(descr, cfield_tp)) { |
| 191 |
PyErr_Format(PyExc_AttributeError, |
| 192 |
"'%U' is specified in _anonymous_ but not in " |
| 193 |
"_fields_", |
| 194 |
fname); |
| 195 |
Py_DECREF(anon_names); |
| 196 |
Py_DECREF(descr); |
| 197 |
return -1; |
| 198 |
} |
| 199 |
descr->anonymous = 1; |
| 200 |
|
| 201 |
/* descr is in the field descriptor. */ |
| 202 |
if (-1 == MakeFields(type, (CFieldObject *)descr, |
| 203 |
((CFieldObject *)descr)->index, |
| 204 |
((CFieldObject *)descr)->byte_offset)) { |
| 205 |
Py_DECREF(descr); |
| 206 |
Py_DECREF(anon_names); |
| 207 |
return -1; |
| 208 |
} |
| 209 |
Py_DECREF(descr); |
| 210 |
} |
| 211 |
|
| 212 |
Py_DECREF(anon_names); |
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
int |
| 218 |
_replace_array_elements(ctypes_state *st, PyObject *layout_fields, |
| 219 |
Py_ssize_t ffi_ofs, StgInfo *baseinfo, StgInfo *stginfo); |
| 220 |
|
| 221 |
/* |
| 222 |
Retrieve the (optional) _pack_ attribute from a type, the _fields_ attribute, |
| 223 |
and initialize StgInfo. Used for Structure and Union subclasses. |
| 224 |
*/ |
| 225 |
int |
| 226 |
PyCStructUnionType_update_stginfo(PyObject *type, PyObject *fields, int isStruct) |
| 227 |
{ |
| 228 |
Py_ssize_t ffi_ofs; |
| 229 |
int arrays_seen = 0; |
| 230 |
|
| 231 |
int retval = -1; |
| 232 |
// The following are NULL or hold strong references. |
| 233 |
// They're cleared on error. |
| 234 |
PyObject *layout_func = NULL; |
| 235 |
PyObject *kwnames = NULL; |
| 236 |
PyObject *align_obj = NULL; |
| 237 |
PyObject *size_obj = NULL; |
| 238 |
PyObject *layout_fields_obj = NULL; |
| 239 |
PyObject *layout_fields = NULL; |
| 240 |
PyObject *layout = NULL; |
| 241 |
PyObject *format_spec_obj = NULL; |
| 242 |
|
| 243 |
if (fields == NULL) { |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); |
| 248 |
StgInfo *stginfo; |
| 249 |
if (PyStgInfo_FromType(st, type, &stginfo) < 0) { |
| 250 |
return -1; |
| 251 |
} |
| 252 |
if (!stginfo) { |
| 253 |
PyErr_SetString(PyExc_TypeError, |
| 254 |
"ctypes state is not initialized"); |
| 255 |
return -1; |
| 256 |
} |
| 257 |
PyObject *base = (PyObject *)((PyTypeObject *)type)->tp_base; |
| 258 |
StgInfo *baseinfo; |
| 259 |
if (PyStgInfo_FromType(st, base, &baseinfo) < 0) { |
| 260 |
return -1; |
| 261 |
} |
| 262 |
|
| 263 |
STGINFO_LOCK(stginfo); |
| 264 |
/* If this structure/union is already marked final we cannot assign |
| 265 |
_fields_ anymore. */ |
| 266 |
if (stginfo_get_dict_final(stginfo) == 1) {/* is final ? */ |
| 267 |
PyErr_SetString(PyExc_AttributeError, |
| 268 |
"_fields_ is final"); |
| 269 |
goto error; |
| 270 |
} |
| 271 |
|
| 272 |
layout_func = PyImport_ImportModuleAttrString("ctypes._layout", "get_layout"); |
| 273 |
if (!layout_func) { |
| 274 |
goto error; |
| 275 |
} |
| 276 |
kwnames = PyTuple_Pack( |
| 277 |
2, |
| 278 |
&_Py_ID(is_struct), |
| 279 |
&_Py_ID(base)); |
| 280 |
if (!kwnames) { |
| 281 |
goto error; |
| 282 |
} |
| 283 |
layout = PyObject_Vectorcall( |
| 284 |
layout_func, |
| 285 |
1 + (PyObject*[]){ |
| 286 |
NULL, |
| 287 |
/* positional args */ |
| 288 |
type, |
| 289 |
fields, |
| 290 |
/* keyword args */ |
| 291 |
isStruct ? Py_True : Py_False, |
| 292 |
baseinfo ? base : Py_None}, |
| 293 |
2 | PY_VECTORCALL_ARGUMENTS_OFFSET, |
| 294 |
kwnames); |
| 295 |
Py_CLEAR(kwnames); |
| 296 |
Py_CLEAR(layout_func); |
| 297 |
fields = NULL; // a borrowed reference we won't be using again |
| 298 |
if (!layout) { |
| 299 |
goto error; |
| 300 |
} |
| 301 |
|
| 302 |
align_obj = PyObject_GetAttr(layout, &_Py_ID(align)); |
| 303 |
if (!align_obj) { |
| 304 |
goto error; |
| 305 |
} |
| 306 |
Py_ssize_t total_align = PyLong_AsSsize_t(align_obj); |
| 307 |
Py_CLEAR(align_obj); |
| 308 |
if (total_align < 0) { |
| 309 |
if (!PyErr_Occurred()) { |
| 310 |
PyErr_SetString(PyExc_ValueError, |
| 311 |
"align must be a non-negative integer"); |
| 312 |
} |
| 313 |
goto error; |
| 314 |
} |
| 315 |
|
| 316 |
size_obj = PyObject_GetAttr(layout, &_Py_ID(size)); |
| 317 |
if (!size_obj) { |
| 318 |
goto error; |
| 319 |
} |
| 320 |
Py_ssize_t total_size = PyLong_AsSsize_t(size_obj); |
| 321 |
Py_CLEAR(size_obj); |
| 322 |
if (total_size < 0) { |
| 323 |
if (!PyErr_Occurred()) { |
| 324 |
PyErr_SetString(PyExc_ValueError, |
| 325 |
"size must be a non-negative integer"); |
| 326 |
} |
| 327 |
goto error; |
| 328 |
} |
| 329 |
|
| 330 |
format_spec_obj = PyObject_GetAttr(layout, &_Py_ID(format_spec)); |
| 331 |
if (!format_spec_obj) { |
| 332 |
goto error; |
| 333 |
} |
| 334 |
Py_ssize_t format_spec_size; |
| 335 |
const char *format_spec = PyUnicode_AsUTF8AndSize(format_spec_obj, |
| 336 |
&format_spec_size); |
| 337 |
if (!format_spec) { |
| 338 |
goto error; |
| 339 |
} |
| 340 |
|
| 341 |
if (stginfo->format) { |
| 342 |
PyMem_Free(stginfo->format); |
| 343 |
stginfo->format = NULL; |
| 344 |
} |
| 345 |
stginfo->format = PyMem_Malloc(format_spec_size + 1); |
| 346 |
if (!stginfo->format) { |
| 347 |
PyErr_NoMemory(); |
| 348 |
goto error; |
| 349 |
} |
| 350 |
memcpy(stginfo->format, format_spec, format_spec_size + 1); |
| 351 |
|
| 352 |
layout_fields_obj = PyObject_GetAttr(layout, &_Py_ID(fields)); |
| 353 |
if (!layout_fields_obj) { |
| 354 |
goto error; |
| 355 |
} |
| 356 |
layout_fields = PySequence_Tuple(layout_fields_obj); |
| 357 |
if (!layout_fields) { |
| 358 |
goto error; |
| 359 |
} |
| 360 |
Py_CLEAR(layout_fields_obj); |
| 361 |
Py_CLEAR(layout); |
| 362 |
|
| 363 |
Py_ssize_t len = PyTuple_GET_SIZE(layout_fields); |
| 364 |
|
| 365 |
if (stginfo->ffi_type_pointer.elements) { |
| 366 |
PyMem_Free(stginfo->ffi_type_pointer.elements); |
| 367 |
stginfo->ffi_type_pointer.elements = NULL; |
| 368 |
} |
| 369 |
|
| 370 |
if (baseinfo) { |
| 371 |
stginfo->ffi_type_pointer.type = FFI_TYPE_STRUCT; |
| 372 |
stginfo->ffi_type_pointer.elements = PyMem_New(ffi_type *, baseinfo->length + len + 1); |
| 373 |
if (stginfo->ffi_type_pointer.elements == NULL) { |
| 374 |
PyErr_NoMemory(); |
| 375 |
goto error; |
| 376 |
} |
| 377 |
memset(stginfo->ffi_type_pointer.elements, 0, |
| 378 |
sizeof(ffi_type *) * (baseinfo->length + len + 1)); |
| 379 |
if (baseinfo->length > 0) { |
| 380 |
memcpy(stginfo->ffi_type_pointer.elements, |
| 381 |
baseinfo->ffi_type_pointer.elements, |
| 382 |
sizeof(ffi_type *) * (baseinfo->length)); |
| 383 |
} |
| 384 |
ffi_ofs = baseinfo->length; |
| 385 |
} else { |
| 386 |
stginfo->ffi_type_pointer.type = FFI_TYPE_STRUCT; |
| 387 |
stginfo->ffi_type_pointer.elements = PyMem_New(ffi_type *, len + 1); |
| 388 |
if (stginfo->ffi_type_pointer.elements == NULL) { |
| 389 |
PyErr_NoMemory(); |
| 390 |
goto error; |
| 391 |
} |
| 392 |
memset(stginfo->ffi_type_pointer.elements, 0, |
| 393 |
sizeof(ffi_type *) * (len + 1)); |
| 394 |
ffi_ofs = 0; |
| 395 |
} |
| 396 |
|
| 397 |
for (Py_ssize_t i = 0; i < len; ++i) { |
| 398 |
PyObject *prop_obj = PyTuple_GET_ITEM(layout_fields, i); |
| 399 |
assert(prop_obj); |
| 400 |
if (!PyType_IsSubtype(Py_TYPE(prop_obj), st->PyCField_Type)) { |
| 401 |
PyErr_Format(PyExc_TypeError, |
| 402 |
"fields must be of type CField, got %T", prop_obj); |
| 403 |
goto error; |
| 404 |
|
| 405 |
} |
| 406 |
CFieldObject *prop = (CFieldObject *)prop_obj; // borrow from prop_obj |
| 407 |
|
| 408 |
if (prop->index != i) { |
| 409 |
PyErr_Format(PyExc_ValueError, |
| 410 |
"field %R index mismatch (expected %zd, got %zd)", |
| 411 |
prop->name, i, prop->index); |
| 412 |
goto error; |
| 413 |
} |
| 414 |
|
| 415 |
if (PyCArrayTypeObject_Check(st, prop->proto)) { |
| 416 |
arrays_seen = 1; |
| 417 |
} |
| 418 |
|
| 419 |
StgInfo *info; |
| 420 |
if (PyStgInfo_FromType(st, prop->proto, &info) < 0) { |
| 421 |
goto error; |
| 422 |
} |
| 423 |
assert(info); |
| 424 |
STGINFO_LOCK(info); |
| 425 |
stginfo->ffi_type_pointer.elements[ffi_ofs + i] = &info->ffi_type_pointer; |
| 426 |
if (info->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER)) |
| 427 |
stginfo->flags |= TYPEFLAG_HASPOINTER; |
| 428 |
|
| 429 |
stginfo_set_dict_final_lock_held(info); /* mark field type final */ |
| 430 |
STGINFO_UNLOCK(); |
| 431 |
if (-1 == PyObject_SetAttr(type, prop->name, prop_obj)) { |
| 432 |
goto error; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
stginfo->ffi_type_pointer.alignment = Py_SAFE_DOWNCAST(total_align, |
| 437 |
Py_ssize_t, |
| 438 |
unsigned short); |
| 439 |
stginfo->ffi_type_pointer.size = total_size; |
| 440 |
|
| 441 |
stginfo->size = total_size; |
| 442 |
stginfo->align = total_align; |
| 443 |
stginfo->length = ffi_ofs + len; |
| 444 |
|
| 445 |
/* |
| 446 |
* The value of MAX_STRUCT_SIZE depends on the platform Python is running on. |
| 447 |
*/ |
| 448 |
#if defined(__aarch64__) || defined(__arm__) || defined(_M_ARM64) || defined(__sparc__) |
| 449 |
# define MAX_STRUCT_SIZE 32 |
| 450 |
#elif defined(__powerpc64__) |
| 451 |
# define MAX_STRUCT_SIZE 64 |
| 452 |
#else |
| 453 |
# define MAX_STRUCT_SIZE 16 |
| 454 |
#endif |
| 455 |
|
| 456 |
if (arrays_seen && (total_size <= MAX_STRUCT_SIZE)) { |
| 457 |
if (_replace_array_elements(st, layout_fields, ffi_ofs, baseinfo, stginfo) < 0) { |
| 458 |
goto error; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
/* We did check that this flag was NOT set above, it must not |
| 463 |
have been set until now. */ |
| 464 |
if (stginfo_get_dict_final(stginfo) == 1) { |
| 465 |
PyErr_SetString(PyExc_AttributeError, |
| 466 |
"Structure or union cannot contain itself"); |
| 467 |
goto error; |
| 468 |
} |
| 469 |
stginfo_set_dict_final_lock_held(stginfo); |
| 470 |
|
| 471 |
retval = MakeAnonFields(type); |
| 472 |
error:; |
| 473 |
Py_XDECREF(layout_func); |
| 474 |
Py_XDECREF(kwnames); |
| 475 |
Py_XDECREF(align_obj); |
| 476 |
Py_XDECREF(size_obj); |
| 477 |
Py_XDECREF(layout_fields_obj); |
| 478 |
Py_XDECREF(layout_fields); |
| 479 |
Py_XDECREF(layout); |
| 480 |
Py_XDECREF(format_spec_obj); |
| 481 |
STGINFO_UNLOCK(); |
| 482 |
return retval; |
| 483 |
} |
| 484 |
|
| 485 |
/* |
| 486 |
Replace array elements at stginfo->ffi_type_pointer.elements. |
| 487 |
Return -1 if error occurred. |
| 488 |
*/ |
| 489 |
int |
| 490 |
_replace_array_elements(ctypes_state *st, PyObject *layout_fields, |
| 491 |
Py_ssize_t ffi_ofs, StgInfo *baseinfo, StgInfo *stginfo) |
| 492 |
{ |
| 493 |
/* |
| 494 |
* See bpo-22273 and gh-110190. Arrays are normally treated as |
| 495 |
* pointers, which is fine when an array name is being passed as |
| 496 |
* parameter, but not when passing structures by value that contain |
| 497 |
* arrays. |
| 498 |
* Small structures passed by value are passed in registers, and in |
| 499 |
* order to do this, libffi needs to know the true type of the array |
| 500 |
* members of structs. Treating them as pointers breaks things. |
| 501 |
* |
| 502 |
* Small structures have different sizes depending on the platform |
| 503 |
* where Python is running on: |
| 504 |
* |
| 505 |
* * x86-64: 16 bytes or less |
| 506 |
* * Arm platforms (both 32 and 64 bit): 32 bytes or less |
| 507 |
* * PowerPC 64 Little Endian: 64 bytes or less |
| 508 |
* |
| 509 |
* In that case, there can't be more than 16, 32 or 64 elements after |
| 510 |
* unrolling arrays, as we (will) disallow bitfields. |
| 511 |
* So we can collect the true ffi_type values in a fixed-size local |
| 512 |
* array on the stack and, if any arrays were seen, replace the |
| 513 |
* ffi_type_pointer.elements with a more accurate set, to allow |
| 514 |
* libffi to marshal them into registers correctly. |
| 515 |
* It means one more loop over the fields, but if we got here, |
| 516 |
* the structure is small, so there aren't too many of those. |
| 517 |
* |
| 518 |
* Although the passing in registers is specific to the above |
| 519 |
* platforms, the array-in-struct vs. pointer problem is general. |
| 520 |
* But we restrict the type transformation to small structs |
| 521 |
* nonetheless. |
| 522 |
* |
| 523 |
* Note that although a union may be small in terms of memory usage, it |
| 524 |
* could contain many overlapping declarations of arrays, e.g. |
| 525 |
* |
| 526 |
* union { |
| 527 |
* unsigned int_8 foo [16]; |
| 528 |
* unsigned uint_8 bar [16]; |
| 529 |
* unsigned int_16 baz[8]; |
| 530 |
* unsigned uint_16 bozz[8]; |
| 531 |
* unsigned int_32 fizz[4]; |
| 532 |
* unsigned uint_32 buzz[4]; |
| 533 |
* } |
| 534 |
* |
| 535 |
* which is still only 16 bytes in size. We need to convert this into |
| 536 |
* the following equivalent for libffi: |
| 537 |
* |
| 538 |
* union { |
| 539 |
* struct { int_8 e1; int_8 e2; ... int_8 e_16; } f1; |
| 540 |
* struct { uint_8 e1; uint_8 e2; ... uint_8 e_16; } f2; |
| 541 |
* struct { int_16 e1; int_16 e2; ... int_16 e_8; } f3; |
| 542 |
* struct { uint_16 e1; uint_16 e2; ... uint_16 e_8; } f4; |
| 543 |
* struct { int_32 e1; int_32 e2; ... int_32 e_4; } f5; |
| 544 |
* struct { uint_32 e1; uint_32 e2; ... uint_32 e_4; } f6; |
| 545 |
* } |
| 546 |
* |
| 547 |
* The same principle applies for a struct 32 or 64 bytes in size. |
| 548 |
* |
| 549 |
* So the struct/union needs setting up as follows: all non-array |
| 550 |
* elements copied across as is, and all array elements replaced with |
| 551 |
* an equivalent struct which has as many fields as the array has |
| 552 |
* elements, plus one NULL pointer. |
| 553 |
*/ |
| 554 |
|
| 555 |
Py_ssize_t num_ffi_type_pointers = 0; /* for the dummy fields */ |
| 556 |
Py_ssize_t num_ffi_types = 0; /* for the dummy structures */ |
| 557 |
size_t alloc_size; /* total bytes to allocate */ |
| 558 |
void *type_block = NULL; /* to hold all the type information needed */ |
| 559 |
ffi_type **element_types; /* of this struct/union */ |
| 560 |
ffi_type **dummy_types; /* of the dummy struct elements */ |
| 561 |
ffi_type *structs; /* point to struct aliases of arrays */ |
| 562 |
Py_ssize_t element_index; /* index into element_types for this */ |
| 563 |
Py_ssize_t dummy_index = 0; /* index into dummy field pointers */ |
| 564 |
Py_ssize_t struct_index = 0; /* index into dummy structs */ |
| 565 |
|
| 566 |
Py_ssize_t len = PyTuple_GET_SIZE(layout_fields); |
| 567 |
|
| 568 |
/* first pass to see how much memory to allocate */ |
| 569 |
for (Py_ssize_t i = 0; i < len; ++i) { |
| 570 |
PyObject *prop_obj = PyTuple_GET_ITEM(layout_fields, i); // borrowed |
| 571 |
assert(prop_obj); |
| 572 |
assert(PyType_IsSubtype(Py_TYPE(prop_obj), st->PyCField_Type)); |
| 573 |
CFieldObject *prop = (CFieldObject *)prop_obj; // borrowed |
| 574 |
|
| 575 |
StgInfo *info; |
| 576 |
if (PyStgInfo_FromType(st, prop->proto, &info) < 0) { |
| 577 |
goto error; |
| 578 |
} |
| 579 |
assert(info); |
| 580 |
|
| 581 |
if (!PyCArrayTypeObject_Check(st, prop->proto)) { |
| 582 |
/* Not an array. Just need an ffi_type pointer. */ |
| 583 |
num_ffi_type_pointers++; |
| 584 |
} |
| 585 |
else { |
| 586 |
/* It's an array. */ |
| 587 |
Py_ssize_t length = info->length; |
| 588 |
|
| 589 |
StgInfo *einfo; |
| 590 |
if (PyStgInfo_FromType(st, info->proto, &einfo) < 0) { |
| 591 |
goto error; |
| 592 |
} |
| 593 |
if (einfo == NULL) { |
| 594 |
PyErr_Format(PyExc_TypeError, |
| 595 |
"second item in _fields_ tuple (index %zd) must be a C type", |
| 596 |
i); |
| 597 |
goto error; |
| 598 |
} |
| 599 |
/* |
| 600 |
* We need one extra ffi_type to hold the struct, and one |
| 601 |
* ffi_type pointer per array element + one for a NULL to |
| 602 |
* mark the end. |
| 603 |
*/ |
| 604 |
num_ffi_types++; |
| 605 |
num_ffi_type_pointers += length + 1; |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
/* |
| 610 |
* At this point, we know we need storage for some ffi_types and some |
| 611 |
* ffi_type pointers. We'll allocate these in one block. |
| 612 |
* There are three sub-blocks of information: the ffi_type pointers to |
| 613 |
* this structure/union's elements, the ffi_type_pointers to the |
| 614 |
* dummy fields standing in for array elements, and the |
| 615 |
* ffi_types representing the dummy structures. |
| 616 |
*/ |
| 617 |
alloc_size = (ffi_ofs + 1 + len + num_ffi_type_pointers) * sizeof(ffi_type *) + |
| 618 |
num_ffi_types * sizeof(ffi_type); |
| 619 |
type_block = PyMem_Malloc(alloc_size); |
| 620 |
|
| 621 |
if (type_block == NULL) { |
| 622 |
PyErr_NoMemory(); |
| 623 |
goto error; |
| 624 |
} |
| 625 |
/* |
| 626 |
* the first block takes up ffi_ofs + len + 1 which is the pointers * |
| 627 |
* for this struct/union. The second block takes up |
| 628 |
* num_ffi_type_pointers, so the sum of these is ffi_ofs + len + 1 + |
| 629 |
* num_ffi_type_pointers as allocated above. The last bit is the |
| 630 |
* num_ffi_types structs. |
| 631 |
*/ |
| 632 |
element_types = (ffi_type **) type_block; |
| 633 |
dummy_types = &element_types[ffi_ofs + len + 1]; |
| 634 |
structs = (ffi_type *) &dummy_types[num_ffi_type_pointers]; |
| 635 |
|
| 636 |
if (num_ffi_types > 0) { |
| 637 |
memset(structs, 0, num_ffi_types * sizeof(ffi_type)); |
| 638 |
} |
| 639 |
if (ffi_ofs && (baseinfo != NULL)) { |
| 640 |
memcpy(element_types, |
| 641 |
baseinfo->ffi_type_pointer.elements, |
| 642 |
ffi_ofs * sizeof(ffi_type *)); |
| 643 |
} |
| 644 |
element_index = ffi_ofs; |
| 645 |
|
| 646 |
/* second pass to actually set the type pointers */ |
| 647 |
for (Py_ssize_t i = 0; i < len; ++i) { |
| 648 |
PyObject *prop_obj = PyTuple_GET_ITEM(layout_fields, i); // borrowed |
| 649 |
assert(prop_obj); |
| 650 |
assert(PyType_IsSubtype(Py_TYPE(prop_obj), st->PyCField_Type)); |
| 651 |
CFieldObject *prop = (CFieldObject *)prop_obj; // borrowed |
| 652 |
|
| 653 |
StgInfo *info; |
| 654 |
if (PyStgInfo_FromType(st, prop->proto, &info) < 0) { |
| 655 |
goto error; |
| 656 |
} |
| 657 |
assert(info); |
| 658 |
|
| 659 |
assert(element_index < (ffi_ofs + len)); /* will be used below */ |
| 660 |
if (!PyCArrayTypeObject_Check(st, prop->proto)) { |
| 661 |
/* Not an array. Just copy over the element ffi_type. */ |
| 662 |
element_types[element_index++] = &info->ffi_type_pointer; |
| 663 |
} |
| 664 |
else { |
| 665 |
Py_ssize_t length = info->length; |
| 666 |
StgInfo *einfo; |
| 667 |
if (PyStgInfo_FromType(st, info->proto, &einfo) < 0) { |
| 668 |
goto error; |
| 669 |
} |
| 670 |
if (einfo == NULL) { |
| 671 |
PyErr_Format(PyExc_TypeError, |
| 672 |
"second item in _fields_ tuple (index %zd) must be a C type", |
| 673 |
i); |
| 674 |
goto error; |
| 675 |
} |
| 676 |
element_types[element_index++] = &structs[struct_index]; |
| 677 |
structs[struct_index].size = length * einfo->ffi_type_pointer.size; |
| 678 |
structs[struct_index].alignment = einfo->ffi_type_pointer.alignment; |
| 679 |
structs[struct_index].type = FFI_TYPE_STRUCT; |
| 680 |
structs[struct_index].elements = &dummy_types[dummy_index]; |
| 681 |
++struct_index; |
| 682 |
/* Copy over the element's type, length times. */ |
| 683 |
while (length > 0) { |
| 684 |
assert(dummy_index < (num_ffi_type_pointers)); |
| 685 |
dummy_types[dummy_index++] = &einfo->ffi_type_pointer; |
| 686 |
length--; |
| 687 |
} |
| 688 |
assert(dummy_index < (num_ffi_type_pointers)); |
| 689 |
dummy_types[dummy_index++] = NULL; |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
element_types[element_index] = NULL; |
| 694 |
/* |
| 695 |
* Replace the old elements with the new, taking into account |
| 696 |
* base class elements where necessary. |
| 697 |
*/ |
| 698 |
assert(stginfo->ffi_type_pointer.elements); |
| 699 |
PyMem_Free(stginfo->ffi_type_pointer.elements); |
| 700 |
stginfo->ffi_type_pointer.elements = element_types; |
| 701 |
|
| 702 |
return 0; |
| 703 |
|
| 704 |
error: |
| 705 |
PyMem_Free(type_block); |
| 706 |
return -1; |
| 707 |
} |