summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorChristian Heimes <christian@python.org>2022-03-30 19:28:33 (GMT)
committerGitHub <noreply@github.com>2022-03-30 19:28:33 (GMT)
commit581c4434de62d9d36392f10e65866c081fb18d71 (patch)
tree8c7f01b8826e7f11bbe9a344612bb0187e2e4e8e /Python
parent795c00b91cbc208969302e9e16a269c2049af3e9 (diff)
downloadcpython-581c4434de62d9d36392f10e65866c081fb18d71.zip
cpython-581c4434de62d9d36392f10e65866c081fb18d71.tar.gz
cpython-581c4434de62d9d36392f10e65866c081fb18d71.tar.bz2
bpo-47162: Add call trampoline to mitigate bad fpcasts on Emscripten (GH-32189)
Diffstat (limited to 'Python')
-rw-r--r--Python/import.c12
-rw-r--r--Python/importdl.c6
-rw-r--r--Python/importdl.h8
3 files changed, 20 insertions, 6 deletions
diff --git a/Python/import.c b/Python/import.c
index 982ec8c..4b6d6d1 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -527,7 +527,7 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
else {
if (def->m_base.m_init == NULL)
return NULL;
- mod = def->m_base.m_init();
+ mod = _PyImport_InitFunc_TrampolineCall(def->m_base.m_init);
if (mod == NULL)
return NULL;
if (PyObject_SetItem(modules, name, mod) == -1) {
@@ -958,6 +958,13 @@ PyImport_GetImporter(PyObject *path)
return get_path_importer(tstate, path_importer_cache, path_hooks, path);
}
+#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
+#include <emscripten.h>
+EM_JS(PyObject*, _PyImport_InitFunc_TrampolineCall, (PyModInitFunction func), {
+ return wasmTable.get(func)();
+});
+#endif // __EMSCRIPTEN__ && PY_CALL_TRAMPOLINE
+
static PyObject*
create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
{
@@ -973,8 +980,7 @@ create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
/* Cannot re-init internal module ("sys" or "builtins") */
return PyImport_AddModuleObject(name);
}
-
- mod = (*p->initfunc)();
+ mod = _PyImport_InitFunc_TrampolineCall(*p->initfunc);
if (mod == NULL) {
return NULL;
}
diff --git a/Python/importdl.c b/Python/importdl.c
index f66c601..870ae27 100644
--- a/Python/importdl.c
+++ b/Python/importdl.c
@@ -102,7 +102,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
const char *oldcontext;
dl_funcptr exportfunc;
PyModuleDef *def;
- PyObject *(*p0)(void);
+ PyModInitFunction p0;
name_unicode = PyObject_GetAttrString(spec, "name");
if (name_unicode == NULL) {
@@ -157,7 +157,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
goto error;
}
- p0 = (PyObject *(*)(void))exportfunc;
+ p0 = (PyModInitFunction)exportfunc;
/* Package context is needed for single-phase init */
oldcontext = _Py_PackageContext;
@@ -166,7 +166,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
_Py_PackageContext = oldcontext;
goto error;
}
- m = p0();
+ m = _PyImport_InitFunc_TrampolineCall(p0);
_Py_PackageContext = oldcontext;
if (m == NULL) {
diff --git a/Python/importdl.h b/Python/importdl.h
index 9847652..26d18b6 100644
--- a/Python/importdl.h
+++ b/Python/importdl.h
@@ -10,6 +10,14 @@ extern const char *_PyImport_DynLoadFiletab[];
extern PyObject *_PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *);
+typedef PyObject *(*PyModInitFunction)(void);
+
+#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
+extern PyObject *_PyImport_InitFunc_TrampolineCall(PyModInitFunction func);
+#else
+#define _PyImport_InitFunc_TrampolineCall(func) (func)()
+#endif
+
/* Max length of module suffix searched for -- accommodates "module.slb" */
#define MAXSUFFIXSIZE 12