| 1 |
/* ffi.dlopen() interface with dlopen()/dlsym()/dlclose() */ |
| 2 |
|
| 3 |
static void *cdlopen_fetch(PyObject *libname, void *libhandle, |
| 4 |
const char *symbol) |
| 5 |
{ |
| 6 |
void *address; |
| 7 |
|
| 8 |
if (libhandle == NULL) { |
| 9 |
PyErr_Format(FFIError, "library '%s' has been closed", |
| 10 |
PyUnicode_AsUTF8(libname)); |
| 11 |
return NULL; |
| 12 |
} |
| 13 |
|
| 14 |
dlerror(); /* clear error condition */ |
| 15 |
address = dlsym(libhandle, symbol); |
| 16 |
if (address == NULL) { |
| 17 |
const char *error = dlerror(); |
| 18 |
PyErr_Format(FFIError, "symbol '%s' not found in library '%s': %s", |
| 19 |
symbol, PyUnicode_AsUTF8(libname), error); |
| 20 |
} |
| 21 |
return address; |
| 22 |
} |
| 23 |
|
| 24 |
static void cdlopen_close_ignore_errors(void *libhandle) |
| 25 |
{ |
| 26 |
if (libhandle != NULL) |
| 27 |
dlclose(libhandle); |
| 28 |
} |
| 29 |
|
| 30 |
static int cdlopen_close(PyObject *libname, void *libhandle) |
| 31 |
{ |
| 32 |
if (libhandle != NULL && dlclose(libhandle) != 0) { |
| 33 |
const char *error = dlerror(); |
| 34 |
PyErr_Format(FFIError, "closing library '%s': %s", |
| 35 |
PyUnicode_AsUTF8(libname), error); |
| 36 |
return -1; |
| 37 |
} |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
static PyObject *ffi_dlopen(PyObject *self, PyObject *args) |
| 42 |
{ |
| 43 |
const char *modname; |
| 44 |
PyObject *temp, *result = NULL; |
| 45 |
void *handle; |
| 46 |
int auto_close; |
| 47 |
|
| 48 |
handle = b_do_dlopen(args, &modname, &temp, &auto_close); |
| 49 |
if (handle != NULL) |
| 50 |
{ |
| 51 |
result = (PyObject *)lib_internal_new((FFIObject *)self, |
| 52 |
modname, handle, auto_close); |
| 53 |
} |
| 54 |
Py_XDECREF(temp); |
| 55 |
return result; |
| 56 |
} |
| 57 |
|
| 58 |
static PyObject *ffi_dlclose(PyObject *self, PyObject *args) |
| 59 |
{ |
| 60 |
LibObject *lib; |
| 61 |
void *libhandle; |
| 62 |
if (!PyArg_ParseTuple(args, "O!", &Lib_Type, &lib)) |
| 63 |
return NULL; |
| 64 |
|
| 65 |
libhandle = lib->l_libhandle; |
| 66 |
if (libhandle != NULL) |
| 67 |
{ |
| 68 |
lib->l_libhandle = NULL; |
| 69 |
|
| 70 |
/* Clear the dict to force further accesses to do cdlopen_fetch() |
| 71 |
again, and fail because the library was closed. */ |
| 72 |
PyDict_Clear(lib->l_dict); |
| 73 |
|
| 74 |
if (cdlopen_close(lib->l_libname, libhandle) < 0) |
| 75 |
return NULL; |
| 76 |
} |
| 77 |
Py_INCREF(Py_None); |
| 78 |
return Py_None; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
static Py_ssize_t cdl_4bytes(char *src) |
| 83 |
{ |
| 84 |
/* read 4 bytes in big-endian order; return it as a signed integer */ |
| 85 |
signed char *ssrc = (signed char *)src; |
| 86 |
unsigned char *usrc = (unsigned char *)src; |
| 87 |
return (ssrc[0] << 24) | (usrc[1] << 16) | (usrc[2] << 8) | usrc[3]; |
| 88 |
} |
| 89 |
|
| 90 |
static _cffi_opcode_t cdl_opcode(char *src) |
| 91 |
{ |
| 92 |
return (_cffi_opcode_t)cdl_4bytes(src); |
| 93 |
} |
| 94 |
|
| 95 |
typedef struct { |
| 96 |
unsigned long long value; |
| 97 |
int neg; |
| 98 |
} cdl_intconst_t; |
| 99 |
|
| 100 |
static int _cdl_realize_global_int(struct _cffi_getconst_s *gc) |
| 101 |
{ |
| 102 |
/* The 'address' field of 'struct _cffi_global_s' is set to point |
| 103 |
to this function in case ffiobj_init() sees constant integers. |
| 104 |
This fishes around after the 'ctx->globals' array, which is |
| 105 |
initialized to contain another array, this time of |
| 106 |
'cdl_intconst_t' structures. We get the nth one and it tells |
| 107 |
us what to return. |
| 108 |
*/ |
| 109 |
cdl_intconst_t *ic; |
| 110 |
ic = (cdl_intconst_t *)(gc->ctx->globals + gc->ctx->num_globals); |
| 111 |
ic += gc->gindex; |
| 112 |
gc->value = ic->value; |
| 113 |
return ic->neg; |
| 114 |
} |
| 115 |
|
| 116 |
static int ffiobj_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 117 |
{ |
| 118 |
FFIObject *ffi; |
| 119 |
static char *keywords[] = {"module_name", "_version", "_types", |
| 120 |
"_globals", "_struct_unions", "_enums", |
| 121 |
"_typenames", "_includes", NULL}; |
| 122 |
char *ffiname = "?", *types = NULL, *building = NULL; |
| 123 |
Py_ssize_t version = -1; |
| 124 |
Py_ssize_t types_len = 0; |
| 125 |
PyObject *globals = NULL, *struct_unions = NULL, *enums = NULL; |
| 126 |
PyObject *typenames = NULL, *includes = NULL; |
| 127 |
|
| 128 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 129 |
"|sns#O!O!O!O!O!:FFI", keywords, |
| 130 |
&ffiname, &version, &types, &types_len, |
| 131 |
&PyTuple_Type, &globals, |
| 132 |
&PyTuple_Type, &struct_unions, |
| 133 |
&PyTuple_Type, &enums, |
| 134 |
&PyTuple_Type, &typenames, |
| 135 |
&PyTuple_Type, &includes)) |
| 136 |
return -1; |
| 137 |
|
| 138 |
ffi = (FFIObject *)self; |
| 139 |
if (ffi->ctx_is_nonempty) { |
| 140 |
PyErr_SetString(PyExc_ValueError, |
| 141 |
"cannot call FFI.__init__() more than once"); |
| 142 |
return -1; |
| 143 |
} |
| 144 |
ffi->ctx_is_nonempty = 1; |
| 145 |
|
| 146 |
if (version == -1 && types_len == 0) |
| 147 |
return 0; |
| 148 |
if (version < CFFI_VERSION_MIN || version > CFFI_VERSION_MAX) { |
| 149 |
PyErr_Format(PyExc_ImportError, |
| 150 |
"cffi out-of-line Python module '%s' has unknown " |
| 151 |
"version %p", ffiname, (void *)version); |
| 152 |
return -1; |
| 153 |
} |
| 154 |
|
| 155 |
if (types_len > 0) { |
| 156 |
/* unpack a string of 4-byte entries into an array of _cffi_opcode_t */ |
| 157 |
_cffi_opcode_t *ntypes; |
| 158 |
Py_ssize_t i, n = types_len / 4; |
| 159 |
|
| 160 |
building = PyMem_Malloc(n * sizeof(_cffi_opcode_t)); |
| 161 |
if (building == NULL) |
| 162 |
goto error; |
| 163 |
ntypes = (_cffi_opcode_t *)building; |
| 164 |
|
| 165 |
for (i = 0; i < n; i++) { |
| 166 |
ntypes[i] = cdl_opcode(types); |
| 167 |
types += 4; |
| 168 |
} |
| 169 |
ffi->types_builder.ctx.types = ntypes; |
| 170 |
ffi->types_builder.ctx.num_types = n; |
| 171 |
building = NULL; |
| 172 |
} |
| 173 |
|
| 174 |
if (globals != NULL) { |
| 175 |
/* unpack a tuple alternating strings and ints, each two together |
| 176 |
describing one global_s entry with no specified address or size. |
| 177 |
The int is only used with integer constants. */ |
| 178 |
struct _cffi_global_s *nglobs; |
| 179 |
cdl_intconst_t *nintconsts; |
| 180 |
Py_ssize_t i, n = PyTuple_GET_SIZE(globals) / 2; |
| 181 |
|
| 182 |
i = n * (sizeof(struct _cffi_global_s) + sizeof(cdl_intconst_t)); |
| 183 |
building = PyMem_Malloc(i); |
| 184 |
if (building == NULL) |
| 185 |
goto error; |
| 186 |
memset(building, 0, i); |
| 187 |
nglobs = (struct _cffi_global_s *)building; |
| 188 |
nintconsts = (cdl_intconst_t *)(nglobs + n); |
| 189 |
|
| 190 |
for (i = 0; i < n; i++) { |
| 191 |
char *g = PyBytes_AS_STRING(PyTuple_GET_ITEM(globals, i * 2)); |
| 192 |
nglobs[i].type_op = cdl_opcode(g); g += 4; |
| 193 |
nglobs[i].name = g; |
| 194 |
if (_CFFI_GETOP(nglobs[i].type_op) == _CFFI_OP_CONSTANT_INT || |
| 195 |
_CFFI_GETOP(nglobs[i].type_op) == _CFFI_OP_ENUM) { |
| 196 |
PyObject *o = PyTuple_GET_ITEM(globals, i * 2 + 1); |
| 197 |
nglobs[i].address = &_cdl_realize_global_int; |
| 198 |
{ |
| 199 |
nintconsts[i].neg = PyObject_RichCompareBool(o, Py_False, |
| 200 |
Py_LE); |
| 201 |
nintconsts[i].value = PyLong_AsUnsignedLongLongMask(o); |
| 202 |
if (PyErr_Occurred()) |
| 203 |
goto error; |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
ffi->types_builder.ctx.globals = nglobs; |
| 208 |
ffi->types_builder.ctx.num_globals = n; |
| 209 |
building = NULL; |
| 210 |
} |
| 211 |
|
| 212 |
if (struct_unions != NULL) { |
| 213 |
/* unpack a tuple of struct/unions, each described as a sub-tuple; |
| 214 |
the item 0 of each sub-tuple describes the struct/union, and |
| 215 |
the items 1..N-1 describe the fields, if any */ |
| 216 |
struct _cffi_struct_union_s *nstructs; |
| 217 |
struct _cffi_field_s *nfields; |
| 218 |
Py_ssize_t i, n = PyTuple_GET_SIZE(struct_unions); |
| 219 |
Py_ssize_t nf = 0; /* total number of fields */ |
| 220 |
|
| 221 |
for (i = 0; i < n; i++) { |
| 222 |
nf += PyTuple_GET_SIZE(PyTuple_GET_ITEM(struct_unions, i)) - 1; |
| 223 |
} |
| 224 |
i = (n * sizeof(struct _cffi_struct_union_s) + |
| 225 |
nf * sizeof(struct _cffi_field_s)); |
| 226 |
building = PyMem_Malloc(i); |
| 227 |
if (building == NULL) |
| 228 |
goto error; |
| 229 |
memset(building, 0, i); |
| 230 |
nstructs = (struct _cffi_struct_union_s *)building; |
| 231 |
nfields = (struct _cffi_field_s *)(nstructs + n); |
| 232 |
nf = 0; |
| 233 |
|
| 234 |
for (i = 0; i < n; i++) { |
| 235 |
/* 'desc' is the tuple of strings (desc_struct, desc_field_1, ..) */ |
| 236 |
PyObject *desc = PyTuple_GET_ITEM(struct_unions, i); |
| 237 |
Py_ssize_t j, nf1 = PyTuple_GET_SIZE(desc) - 1; |
| 238 |
char *s = PyBytes_AS_STRING(PyTuple_GET_ITEM(desc, 0)); |
| 239 |
/* 's' is the first string, describing the struct/union */ |
| 240 |
nstructs[i].type_index = cdl_4bytes(s); s += 4; |
| 241 |
nstructs[i].flags = cdl_4bytes(s); s += 4; |
| 242 |
nstructs[i].name = s; |
| 243 |
if (nstructs[i].flags & (_CFFI_F_OPAQUE | _CFFI_F_EXTERNAL)) { |
| 244 |
nstructs[i].size = (size_t)-1; |
| 245 |
nstructs[i].alignment = -1; |
| 246 |
nstructs[i].first_field_index = -1; |
| 247 |
nstructs[i].num_fields = 0; |
| 248 |
assert(nf1 == 0); |
| 249 |
} |
| 250 |
else { |
| 251 |
nstructs[i].size = (size_t)-2; |
| 252 |
nstructs[i].alignment = -2; |
| 253 |
nstructs[i].first_field_index = nf; |
| 254 |
nstructs[i].num_fields = nf1; |
| 255 |
} |
| 256 |
for (j = 0; j < nf1; j++) { |
| 257 |
char *f = PyBytes_AS_STRING(PyTuple_GET_ITEM(desc, j + 1)); |
| 258 |
/* 'f' is one of the other strings beyond the first one, |
| 259 |
describing one field each */ |
| 260 |
nfields[nf].field_type_op = cdl_opcode(f); f += 4; |
| 261 |
nfields[nf].field_offset = (size_t)-1; |
| 262 |
if (_CFFI_GETOP(nfields[nf].field_type_op) != _CFFI_OP_NOOP) { |
| 263 |
nfields[nf].field_size = cdl_4bytes(f); f += 4; |
| 264 |
} |
| 265 |
else { |
| 266 |
nfields[nf].field_size = (size_t)-1; |
| 267 |
} |
| 268 |
nfields[nf].name = f; |
| 269 |
nf++; |
| 270 |
} |
| 271 |
} |
| 272 |
ffi->types_builder.ctx.struct_unions = nstructs; |
| 273 |
ffi->types_builder.ctx.fields = nfields; |
| 274 |
ffi->types_builder.ctx.num_struct_unions = n; |
| 275 |
building = NULL; |
| 276 |
} |
| 277 |
|
| 278 |
if (enums != NULL) { |
| 279 |
/* unpack a tuple of strings, each of which describes one enum_s |
| 280 |
entry */ |
| 281 |
struct _cffi_enum_s *nenums; |
| 282 |
Py_ssize_t i, n = PyTuple_GET_SIZE(enums); |
| 283 |
|
| 284 |
i = n * sizeof(struct _cffi_enum_s); |
| 285 |
building = PyMem_Malloc(i); |
| 286 |
if (building == NULL) |
| 287 |
goto error; |
| 288 |
memset(building, 0, i); |
| 289 |
nenums = (struct _cffi_enum_s *)building; |
| 290 |
|
| 291 |
for (i = 0; i < n; i++) { |
| 292 |
char *e = PyBytes_AS_STRING(PyTuple_GET_ITEM(enums, i)); |
| 293 |
/* 'e' is a string describing the enum */ |
| 294 |
nenums[i].type_index = cdl_4bytes(e); e += 4; |
| 295 |
nenums[i].type_prim = cdl_4bytes(e); e += 4; |
| 296 |
nenums[i].name = e; e += strlen(e) + 1; |
| 297 |
nenums[i].enumerators = e; |
| 298 |
} |
| 299 |
ffi->types_builder.ctx.enums = nenums; |
| 300 |
ffi->types_builder.ctx.num_enums = n; |
| 301 |
building = NULL; |
| 302 |
} |
| 303 |
|
| 304 |
if (typenames != NULL) { |
| 305 |
/* unpack a tuple of strings, each of which describes one typename_s |
| 306 |
entry */ |
| 307 |
struct _cffi_typename_s *ntypenames; |
| 308 |
Py_ssize_t i, n = PyTuple_GET_SIZE(typenames); |
| 309 |
|
| 310 |
i = n * sizeof(struct _cffi_typename_s); |
| 311 |
building = PyMem_Malloc(i); |
| 312 |
if (building == NULL) |
| 313 |
goto error; |
| 314 |
memset(building, 0, i); |
| 315 |
ntypenames = (struct _cffi_typename_s *)building; |
| 316 |
|
| 317 |
for (i = 0; i < n; i++) { |
| 318 |
char *t = PyBytes_AS_STRING(PyTuple_GET_ITEM(typenames, i)); |
| 319 |
/* 't' is a string describing the typename */ |
| 320 |
ntypenames[i].type_index = cdl_4bytes(t); t += 4; |
| 321 |
ntypenames[i].name = t; |
| 322 |
} |
| 323 |
ffi->types_builder.ctx.typenames = ntypenames; |
| 324 |
ffi->types_builder.ctx.num_typenames = n; |
| 325 |
building = NULL; |
| 326 |
} |
| 327 |
|
| 328 |
if (includes != NULL) { |
| 329 |
PyObject *included_libs; |
| 330 |
|
| 331 |
included_libs = PyTuple_New(PyTuple_GET_SIZE(includes)); |
| 332 |
if (included_libs == NULL) |
| 333 |
return -1; |
| 334 |
|
| 335 |
Py_INCREF(includes); |
| 336 |
ffi->types_builder.included_ffis = includes; |
| 337 |
ffi->types_builder.included_libs = included_libs; |
| 338 |
} |
| 339 |
|
| 340 |
/* Above, we took directly some "char *" strings out of the strings, |
| 341 |
typically from somewhere inside tuples. Keep them alive by |
| 342 |
incref'ing the whole input arguments. */ |
| 343 |
Py_INCREF(args); |
| 344 |
Py_XINCREF(kwds); |
| 345 |
ffi->types_builder._keepalive1 = args; |
| 346 |
ffi->types_builder._keepalive2 = kwds; |
| 347 |
return 0; |
| 348 |
|
| 349 |
error: |
| 350 |
if (building != NULL) |
| 351 |
PyMem_Free(building); |
| 352 |
if (!PyErr_Occurred()) |
| 353 |
PyErr_NoMemory(); |
| 354 |
return -1; |
| 355 |
} |