_embedding.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /***** Support code for embedding *****/
  2. #ifdef __cplusplus
  3. extern "C" {
  4. #endif
  5. #if defined(_WIN32)
  6. # define CFFI_DLLEXPORT __declspec(dllexport)
  7. #elif defined(__GNUC__)
  8. # define CFFI_DLLEXPORT __attribute__((visibility("default")))
  9. #else
  10. # define CFFI_DLLEXPORT /* nothing */
  11. #endif
  12. /* There are two global variables of type _cffi_call_python_fnptr:
  13. * _cffi_call_python, which we declare just below, is the one called
  14. by ``extern "Python"`` implementations.
  15. * _cffi_call_python_org, which on CPython is actually part of the
  16. _cffi_exports[] array, is the function pointer copied from
  17. _cffi_backend.
  18. After initialization is complete, both are equal. However, the
  19. first one remains equal to &_cffi_start_and_call_python until the
  20. very end of initialization, when we are (or should be) sure that
  21. concurrent threads also see a completely initialized world, and
  22. only then is it changed.
  23. */
  24. #undef _cffi_call_python
  25. typedef void (*_cffi_call_python_fnptr)(struct _cffi_externpy_s *, char *);
  26. static void _cffi_start_and_call_python(struct _cffi_externpy_s *, char *);
  27. static _cffi_call_python_fnptr _cffi_call_python = &_cffi_start_and_call_python;
  28. #ifndef _MSC_VER
  29. /* --- Assuming a GCC not infinitely old --- */
  30. # define cffi_compare_and_swap(l,o,n) __sync_bool_compare_and_swap(l,o,n)
  31. # define cffi_write_barrier() __sync_synchronize()
  32. # if !defined(__amd64__) && !defined(__x86_64__) && \
  33. !defined(__i386__) && !defined(__i386)
  34. # define cffi_read_barrier() __sync_synchronize()
  35. # else
  36. # define cffi_read_barrier() (void)0
  37. # endif
  38. #else
  39. /* --- Windows threads version --- */
  40. # include <Windows.h>
  41. # define cffi_compare_and_swap(l,o,n) \
  42. (InterlockedCompareExchangePointer(l,n,o) == (o))
  43. # define cffi_write_barrier() InterlockedCompareExchange(&_cffi_dummy,0,0)
  44. # define cffi_read_barrier() (void)0
  45. static volatile LONG _cffi_dummy;
  46. #endif
  47. #ifdef WITH_THREAD
  48. # ifndef _MSC_VER
  49. # include <pthread.h>
  50. static pthread_mutex_t _cffi_embed_startup_lock;
  51. # else
  52. static CRITICAL_SECTION _cffi_embed_startup_lock;
  53. # endif
  54. static char _cffi_embed_startup_lock_ready = 0;
  55. #endif
  56. static void _cffi_acquire_reentrant_mutex(void)
  57. {
  58. static void *volatile lock = NULL;
  59. while (!cffi_compare_and_swap(&lock, NULL, (void *)1)) {
  60. /* should ideally do a spin loop instruction here, but
  61. hard to do it portably and doesn't really matter I
  62. think: pthread_mutex_init() should be very fast, and
  63. this is only run at start-up anyway. */
  64. }
  65. #ifdef WITH_THREAD
  66. if (!_cffi_embed_startup_lock_ready) {
  67. # ifndef _MSC_VER
  68. pthread_mutexattr_t attr;
  69. pthread_mutexattr_init(&attr);
  70. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  71. pthread_mutex_init(&_cffi_embed_startup_lock, &attr);
  72. # else
  73. InitializeCriticalSection(&_cffi_embed_startup_lock);
  74. # endif
  75. _cffi_embed_startup_lock_ready = 1;
  76. }
  77. #endif
  78. while (!cffi_compare_and_swap(&lock, (void *)1, NULL))
  79. ;
  80. #ifndef _MSC_VER
  81. pthread_mutex_lock(&_cffi_embed_startup_lock);
  82. #else
  83. EnterCriticalSection(&_cffi_embed_startup_lock);
  84. #endif
  85. }
  86. static void _cffi_release_reentrant_mutex(void)
  87. {
  88. #ifndef _MSC_VER
  89. pthread_mutex_unlock(&_cffi_embed_startup_lock);
  90. #else
  91. LeaveCriticalSection(&_cffi_embed_startup_lock);
  92. #endif
  93. }
  94. /********** CPython-specific section **********/
  95. #ifndef PYPY_VERSION
  96. #include "_cffi_errors.h"
  97. #define _cffi_call_python_org _cffi_exports[_CFFI_CPIDX]
  98. PyMODINIT_FUNC _CFFI_PYTHON_STARTUP_FUNC(void); /* forward */
  99. static void _cffi_py_initialize(void)
  100. {
  101. /* XXX use initsigs=0, which "skips initialization registration of
  102. signal handlers, which might be useful when Python is
  103. embedded" according to the Python docs. But review and think
  104. if it should be a user-controllable setting.
  105. XXX we should also give a way to write errors to a buffer
  106. instead of to stderr.
  107. XXX if importing 'site' fails, CPython (any version) calls
  108. exit(). Should we try to work around this behavior here?
  109. */
  110. Py_InitializeEx(0);
  111. }
  112. static int _cffi_initialize_python(void)
  113. {
  114. /* This initializes Python, imports _cffi_backend, and then the
  115. present .dll/.so is set up as a CPython C extension module.
  116. */
  117. int result;
  118. PyGILState_STATE state;
  119. PyObject *pycode=NULL, *global_dict=NULL, *x;
  120. PyObject *builtins;
  121. state = PyGILState_Ensure();
  122. /* Call the initxxx() function from the present module. It will
  123. create and initialize us as a CPython extension module, instead
  124. of letting the startup Python code do it---it might reimport
  125. the same .dll/.so and get maybe confused on some platforms.
  126. It might also have troubles locating the .dll/.so again for all
  127. I know.
  128. */
  129. (void)_CFFI_PYTHON_STARTUP_FUNC();
  130. if (PyErr_Occurred())
  131. goto error;
  132. /* Now run the Python code provided to ffi.embedding_init_code().
  133. */
  134. pycode = Py_CompileString(_CFFI_PYTHON_STARTUP_CODE,
  135. "<init code for '" _CFFI_MODULE_NAME "'>",
  136. Py_file_input);
  137. if (pycode == NULL)
  138. goto error;
  139. global_dict = PyDict_New();
  140. if (global_dict == NULL)
  141. goto error;
  142. builtins = PyEval_GetBuiltins();
  143. if (builtins == NULL)
  144. goto error;
  145. if (PyDict_SetItemString(global_dict, "__builtins__", builtins) < 0)
  146. goto error;
  147. x = PyEval_EvalCode(
  148. #if PY_MAJOR_VERSION < 3
  149. (PyCodeObject *)
  150. #endif
  151. pycode, global_dict, global_dict);
  152. if (x == NULL)
  153. goto error;
  154. Py_DECREF(x);
  155. /* Done! Now if we've been called from
  156. _cffi_start_and_call_python() in an ``extern "Python"``, we can
  157. only hope that the Python code did correctly set up the
  158. corresponding @ffi.def_extern() function. Otherwise, the
  159. general logic of ``extern "Python"`` functions (inside the
  160. _cffi_backend module) will find that the reference is still
  161. missing and print an error.
  162. */
  163. result = 0;
  164. done:
  165. Py_XDECREF(pycode);
  166. Py_XDECREF(global_dict);
  167. PyGILState_Release(state);
  168. return result;
  169. error:;
  170. {
  171. /* Print as much information as potentially useful.
  172. Debugging load-time failures with embedding is not fun
  173. */
  174. PyObject *ecap;
  175. PyObject *exception, *v, *tb, *f, *modules, *mod;
  176. PyErr_Fetch(&exception, &v, &tb);
  177. ecap = _cffi_start_error_capture();
  178. f = PySys_GetObject((char *)"stderr");
  179. if (f != NULL && f != Py_None) {
  180. PyFile_WriteString(
  181. "Failed to initialize the Python-CFFI embedding logic:\n\n", f);
  182. }
  183. if (exception != NULL) {
  184. PyErr_NormalizeException(&exception, &v, &tb);
  185. PyErr_Display(exception, v, tb);
  186. }
  187. Py_XDECREF(exception);
  188. Py_XDECREF(v);
  189. Py_XDECREF(tb);
  190. if (f != NULL && f != Py_None) {
  191. PyFile_WriteString("\nFrom: " _CFFI_MODULE_NAME
  192. "\ncompiled with cffi version: 1.15.0"
  193. "\n_cffi_backend module: ", f);
  194. modules = PyImport_GetModuleDict();
  195. mod = PyDict_GetItemString(modules, "_cffi_backend");
  196. if (mod == NULL) {
  197. PyFile_WriteString("not loaded", f);
  198. }
  199. else {
  200. v = PyObject_GetAttrString(mod, "__file__");
  201. PyFile_WriteObject(v, f, 0);
  202. Py_XDECREF(v);
  203. }
  204. PyFile_WriteString("\nsys.path: ", f);
  205. PyFile_WriteObject(PySys_GetObject((char *)"path"), f, 0);
  206. PyFile_WriteString("\n\n", f);
  207. }
  208. _cffi_stop_error_capture(ecap);
  209. }
  210. result = -1;
  211. goto done;
  212. }
  213. #if PY_VERSION_HEX < 0x03080000
  214. PyAPI_DATA(char *) _PyParser_TokenNames[]; /* from CPython */
  215. #endif
  216. static int _cffi_carefully_make_gil(void)
  217. {
  218. /* This does the basic initialization of Python. It can be called
  219. completely concurrently from unrelated threads. It assumes
  220. that we don't hold the GIL before (if it exists), and we don't
  221. hold it afterwards.
  222. (What it really does used to be completely different in Python 2
  223. and Python 3, with the Python 2 solution avoiding the spin-lock
  224. around the Py_InitializeEx() call. However, after recent changes
  225. to CPython 2.7 (issue #358) it no longer works. So we use the
  226. Python 3 solution everywhere.)
  227. This initializes Python by calling Py_InitializeEx().
  228. Important: this must not be called concurrently at all.
  229. So we use a global variable as a simple spin lock. This global
  230. variable must be from 'libpythonX.Y.so', not from this
  231. cffi-based extension module, because it must be shared from
  232. different cffi-based extension modules.
  233. In Python < 3.8, we choose
  234. _PyParser_TokenNames[0] as a completely arbitrary pointer value
  235. that is never written to. The default is to point to the
  236. string "ENDMARKER". We change it temporarily to point to the
  237. next character in that string. (Yes, I know it's REALLY
  238. obscure.)
  239. In Python >= 3.8, this string array is no longer writable, so
  240. instead we pick PyCapsuleType.tp_version_tag. We can't change
  241. Python < 3.8 because someone might use a mixture of cffi
  242. embedded modules, some of which were compiled before this file
  243. changed.
  244. */
  245. #ifdef WITH_THREAD
  246. # if PY_VERSION_HEX < 0x03080000
  247. char *volatile *lock = (char *volatile *)_PyParser_TokenNames;
  248. char *old_value, *locked_value;
  249. while (1) { /* spin loop */
  250. old_value = *lock;
  251. locked_value = old_value + 1;
  252. if (old_value[0] == 'E') {
  253. assert(old_value[1] == 'N');
  254. if (cffi_compare_and_swap(lock, old_value, locked_value))
  255. break;
  256. }
  257. else {
  258. assert(old_value[0] == 'N');
  259. /* should ideally do a spin loop instruction here, but
  260. hard to do it portably and doesn't really matter I
  261. think: PyEval_InitThreads() should be very fast, and
  262. this is only run at start-up anyway. */
  263. }
  264. }
  265. # else
  266. int volatile *lock = (int volatile *)&PyCapsule_Type.tp_version_tag;
  267. int old_value, locked_value;
  268. assert(!(PyCapsule_Type.tp_flags & Py_TPFLAGS_HAVE_VERSION_TAG));
  269. while (1) { /* spin loop */
  270. old_value = *lock;
  271. locked_value = -42;
  272. if (old_value == 0) {
  273. if (cffi_compare_and_swap(lock, old_value, locked_value))
  274. break;
  275. }
  276. else {
  277. assert(old_value == locked_value);
  278. /* should ideally do a spin loop instruction here, but
  279. hard to do it portably and doesn't really matter I
  280. think: PyEval_InitThreads() should be very fast, and
  281. this is only run at start-up anyway. */
  282. }
  283. }
  284. # endif
  285. #endif
  286. /* call Py_InitializeEx() */
  287. if (!Py_IsInitialized()) {
  288. _cffi_py_initialize();
  289. #if PY_VERSION_HEX < 0x03070000
  290. PyEval_InitThreads();
  291. #endif
  292. PyEval_SaveThread(); /* release the GIL */
  293. /* the returned tstate must be the one that has been stored into the
  294. autoTLSkey by _PyGILState_Init() called from Py_Initialize(). */
  295. }
  296. else {
  297. #if PY_VERSION_HEX < 0x03070000
  298. /* PyEval_InitThreads() is always a no-op from CPython 3.7 */
  299. PyGILState_STATE state = PyGILState_Ensure();
  300. PyEval_InitThreads();
  301. PyGILState_Release(state);
  302. #endif
  303. }
  304. #ifdef WITH_THREAD
  305. /* release the lock */
  306. while (!cffi_compare_and_swap(lock, locked_value, old_value))
  307. ;
  308. #endif
  309. return 0;
  310. }
  311. /********** end CPython-specific section **********/
  312. #else
  313. /********** PyPy-specific section **********/
  314. PyMODINIT_FUNC _CFFI_PYTHON_STARTUP_FUNC(const void *[]); /* forward */
  315. static struct _cffi_pypy_init_s {
  316. const char *name;
  317. void *func; /* function pointer */
  318. const char *code;
  319. } _cffi_pypy_init = {
  320. _CFFI_MODULE_NAME,
  321. _CFFI_PYTHON_STARTUP_FUNC,
  322. _CFFI_PYTHON_STARTUP_CODE,
  323. };
  324. extern int pypy_carefully_make_gil(const char *);
  325. extern int pypy_init_embedded_cffi_module(int, struct _cffi_pypy_init_s *);
  326. static int _cffi_carefully_make_gil(void)
  327. {
  328. return pypy_carefully_make_gil(_CFFI_MODULE_NAME);
  329. }
  330. static int _cffi_initialize_python(void)
  331. {
  332. return pypy_init_embedded_cffi_module(0xB011, &_cffi_pypy_init);
  333. }
  334. /********** end PyPy-specific section **********/
  335. #endif
  336. #ifdef __GNUC__
  337. __attribute__((noinline))
  338. #endif
  339. static _cffi_call_python_fnptr _cffi_start_python(void)
  340. {
  341. /* Delicate logic to initialize Python. This function can be
  342. called multiple times concurrently, e.g. when the process calls
  343. its first ``extern "Python"`` functions in multiple threads at
  344. once. It can also be called recursively, in which case we must
  345. ignore it. We also have to consider what occurs if several
  346. different cffi-based extensions reach this code in parallel
  347. threads---it is a different copy of the code, then, and we
  348. can't have any shared global variable unless it comes from
  349. 'libpythonX.Y.so'.
  350. Idea:
  351. * _cffi_carefully_make_gil(): "carefully" call
  352. PyEval_InitThreads() (possibly with Py_InitializeEx() first).
  353. * then we use a (local) custom lock to make sure that a call to this
  354. cffi-based extension will wait if another call to the *same*
  355. extension is running the initialization in another thread.
  356. It is reentrant, so that a recursive call will not block, but
  357. only one from a different thread.
  358. * then we grab the GIL and (Python 2) we call Py_InitializeEx().
  359. At this point, concurrent calls to Py_InitializeEx() are not
  360. possible: we have the GIL.
  361. * do the rest of the specific initialization, which may
  362. temporarily release the GIL but not the custom lock.
  363. Only release the custom lock when we are done.
  364. */
  365. static char called = 0;
  366. if (_cffi_carefully_make_gil() != 0)
  367. return NULL;
  368. _cffi_acquire_reentrant_mutex();
  369. /* Here the GIL exists, but we don't have it. We're only protected
  370. from concurrency by the reentrant mutex. */
  371. /* This file only initializes the embedded module once, the first
  372. time this is called, even if there are subinterpreters. */
  373. if (!called) {
  374. called = 1; /* invoke _cffi_initialize_python() only once,
  375. but don't set '_cffi_call_python' right now,
  376. otherwise concurrent threads won't call
  377. this function at all (we need them to wait) */
  378. if (_cffi_initialize_python() == 0) {
  379. /* now initialization is finished. Switch to the fast-path. */
  380. /* We would like nobody to see the new value of
  381. '_cffi_call_python' without also seeing the rest of the
  382. data initialized. However, this is not possible. But
  383. the new value of '_cffi_call_python' is the function
  384. 'cffi_call_python()' from _cffi_backend. So: */
  385. cffi_write_barrier();
  386. /* ^^^ we put a write barrier here, and a corresponding
  387. read barrier at the start of cffi_call_python(). This
  388. ensures that after that read barrier, we see everything
  389. done here before the write barrier.
  390. */
  391. assert(_cffi_call_python_org != NULL);
  392. _cffi_call_python = (_cffi_call_python_fnptr)_cffi_call_python_org;
  393. }
  394. else {
  395. /* initialization failed. Reset this to NULL, even if it was
  396. already set to some other value. Future calls to
  397. _cffi_start_python() are still forced to occur, and will
  398. always return NULL from now on. */
  399. _cffi_call_python_org = NULL;
  400. }
  401. }
  402. _cffi_release_reentrant_mutex();
  403. return (_cffi_call_python_fnptr)_cffi_call_python_org;
  404. }
  405. static
  406. void _cffi_start_and_call_python(struct _cffi_externpy_s *externpy, char *args)
  407. {
  408. _cffi_call_python_fnptr fnptr;
  409. int current_err = errno;
  410. #ifdef _MSC_VER
  411. int current_lasterr = GetLastError();
  412. #endif
  413. fnptr = _cffi_start_python();
  414. if (fnptr == NULL) {
  415. fprintf(stderr, "function %s() called, but initialization code "
  416. "failed. Returning 0.\n", externpy->name);
  417. memset(args, 0, externpy->size_of_result);
  418. }
  419. #ifdef _MSC_VER
  420. SetLastError(current_lasterr);
  421. #endif
  422. errno = current_err;
  423. if (fnptr != NULL)
  424. fnptr(externpy, args);
  425. }
  426. /* The cffi_start_python() function makes sure Python is initialized
  427. and our cffi module is set up. It can be called manually from the
  428. user C code. The same effect is obtained automatically from any
  429. dll-exported ``extern "Python"`` function. This function returns
  430. -1 if initialization failed, 0 if all is OK. */
  431. _CFFI_UNUSED_FN
  432. static int cffi_start_python(void)
  433. {
  434. if (_cffi_call_python == &_cffi_start_and_call_python) {
  435. if (_cffi_start_python() == NULL)
  436. return -1;
  437. }
  438. cffi_read_barrier();
  439. return 0;
  440. }
  441. #undef cffi_compare_and_swap
  442. #undef cffi_write_barrier
  443. #undef cffi_read_barrier
  444. #ifdef __cplusplus
  445. }
  446. #endif