cdlopen.c 13 KB

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