| 1 |
#ifndef Py_EMSCRIPTEN_TRAMPOLINE_H |
| 2 |
#define Py_EMSCRIPTEN_TRAMPOLINE_H |
| 3 |
|
| 4 |
#include "pycore_typedefs.h" // _PyRuntimeState |
| 5 |
|
| 6 |
/** |
| 7 |
* C function call trampolines to mitigate bad function pointer casts. |
| 8 |
* |
| 9 |
* Section 6.3.2.3, paragraph 8 reads: |
| 10 |
* |
| 11 |
* A pointer to a function of one type may be converted to a pointer to a |
| 12 |
* function of another type and back again; the result shall compare equal to |
| 13 |
* the original pointer. If a converted pointer is used to call a function |
| 14 |
* whose type is not compatible with the pointed-to type, the behavior is |
| 15 |
* undefined. |
| 16 |
* |
| 17 |
* Typical native ABIs ignore additional arguments or fill in missing values |
| 18 |
* with 0/NULL in function pointer cast. Compilers do not show warnings when a |
| 19 |
* function pointer is explicitly casted to an incompatible type. |
| 20 |
* |
| 21 |
* Bad fpcasts are an issue in WebAssembly. WASM's indirect_call has strict |
| 22 |
* function signature checks. Argument count, types, and return type must match. |
| 23 |
* |
| 24 |
* Third party code unintentionally rely on problematic fpcasts. The call |
| 25 |
* trampoline mitigates common occurrences of bad fpcasts on Emscripten. |
| 26 |
*/ |
| 27 |
|
| 28 |
#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) |
| 29 |
|
| 30 |
PyObject* |
| 31 |
_PyEM_TrampolineCall(PyCFunctionWithKeywords func, |
| 32 |
PyObject* self, |
| 33 |
PyObject* args, |
| 34 |
PyObject* kw); |
| 35 |
|
| 36 |
#define _PyCFunction_TrampolineCall(meth, self, args) \ |
| 37 |
_PyEM_TrampolineCall(*_PyCFunctionWithKeywords_CAST(meth), (self), (args), NULL) |
| 38 |
|
| 39 |
#define _PyCFunctionWithKeywords_TrampolineCall(meth, self, args, kw) \ |
| 40 |
_PyEM_TrampolineCall((meth), (self), (args), (kw)) |
| 41 |
|
| 42 |
#define descr_set_trampoline_call(set, obj, value, closure) \ |
| 43 |
((int)_PyEM_TrampolineCall(_PyCFunctionWithKeywords_CAST(set), (obj), \ |
| 44 |
(value), (PyObject*)(closure))) |
| 45 |
|
| 46 |
#define descr_get_trampoline_call(get, obj, closure) \ |
| 47 |
_PyEM_TrampolineCall(_PyCFunctionWithKeywords_CAST(get), (obj), \ |
| 48 |
(PyObject*)(closure), NULL) |
| 49 |
|
| 50 |
|
| 51 |
#else // defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) |
| 52 |
|
| 53 |
#define _PyCFunction_TrampolineCall(meth, self, args) \ |
| 54 |
(meth)((self), (args)) |
| 55 |
|
| 56 |
#define _PyCFunctionWithKeywords_TrampolineCall(meth, self, args, kw) \ |
| 57 |
(meth)((self), (args), (kw)) |
| 58 |
|
| 59 |
#define descr_set_trampoline_call(set, obj, value, closure) \ |
| 60 |
(set)((obj), (value), (closure)) |
| 61 |
|
| 62 |
#define descr_get_trampoline_call(get, obj, closure) \ |
| 63 |
(get)((obj), (closure)) |
| 64 |
|
| 65 |
#endif // defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE) |
| 66 |
|
| 67 |
#endif // ndef Py_EMSCRIPTEN_SIGNAL_H |