cdlopen.c 12 KB

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