Emscripten: static (non-MAIN_MODULE) builds fail at startup on dynamic-linking-only JS helpers
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Bug description:
CPython's Emscripten platform code is written for the -sMAIN_MODULE dynamic-linking configuration it is built and tested in. Several EM_JS bodies reference JS helpers that only exist in that configuration. Because EM_JS bodies are emitted verbatim into the output, Emscripten's dependency tracker cannot see those references, so a static link silently emits JS calling helpers it never included and fails at runtime instead of at link time.
Four such references, each confirmed against Emscripten 6.0.9:
Python/emscripten_syscalls.c:110hooksresolveGlobalSymbol()to wrapmain()inWebAssembly.promising(). That helper is defined insrc/lib/libdylink.js, whichsrc/modules.mjs:99links in onlyif (MAIN_MODULE); the file itself#errors otherwise, and the symbol is marked__internal: true. The read sits below theif (!WebAssembly.promising) return;guard on line 106, so this only fires once a runtime exposes JSPI.Python/emscripten_trampoline.c:54and:82usewasmTableandaddFunction(). Neither is included by default;MAIN_MODULEhappens to pull both in through libdylink's own__deps.- The promising-main wrapper calls
_emscripten_exit_with_live_runtime()(emscripten_syscalls.c:128), which is likewise dropped from a static link. Python/emscripten_trampoline.c:93reads__PyRuntime, which is bound into the JS glue only becauseconfigurelists it in-sEXPORTED_FUNCTIONSfor CPython's ownpython.mjslink. An embedder linkinglibpythoninto their own application gets no such flag. Unlike the others, this one fails on every runtime, JSPI or not.
Minimal reproducer
This is the shape of (1) with no CPython involved:
#include <emscripten.h>
EM_JS(void, probe, (void), {
if (!WebAssembly.promising) return;
const orig = resolveGlobalSymbol;
})
__attribute__((constructor)) void c(void) { probe(); }
int main(void) { return 0; }
$ emcc t.c -o t.js # links clean, no diagnostic at all
$ node t.js # fine, the guard returns early
$ node --experimental-wasm-jspi t.js
ReferenceError: resolveGlobalSymbol is not defined
at probe (t.js:1513:136)
at initRuntime (t.js:540:35)
Emscripten does warn for library symbols it knows about — referencing addFunction the same way produces "addFunction is a library symbol and not included by default". resolveGlobalSymbol gets nothing, because without MAIN_MODULE libdylink.js is never loaded and the name is not in the symbol set to warn about.
Full reproduction
I built libpython3.14.a for wasm32-emscripten with --disable-wasm-dynamic-linking MODULE_BUILDTYPE=static CFLAGS=-DPY_CALL_TRAMPOLINE and linked it into a small C application that runs an interactive Python console in the browser. It aborts during startup on __PyRuntime; with that symbol exported by hand it then aborts on resolveGlobalSymbol as soon as JSPI is enabled, and works with JSPI off.
Note on the fix
EM_JS_DEPS (emscripten/em_macros.h) exists precisely to declare these, and emscripten_syscalls.c:43 already uses it for $SYSCALLS. Declaring $resolveGlobalSymbol converts the silent breakage into a link-time error: undefined symbol: $resolveGlobalSymbol, which is the behaviour you would want in a configuration that genuinely cannot support it.
Why this is not caught today
Every wasm test builds through Platforms/emscripten/__main__.py, which hard-codes --enable-wasm-dynamic-linking at line 510. web_example, web_example_pyrepl_jspi and browser_test therefore all exercise MAIN_MODULE only; the static configuration has no coverage.
Context for the report: this blocks embedding CPython in Kodi's WebAssembly port, which links statically.
CPython versions tested on:
3.14, CPython main branch
Operating systems tested on:
Other
Linked PRs
- gh-156781
- gh-156798
- gh-157238
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
檢查 Python/emscripten_syscalls.c、Python/emscripten_trampoline.c、emscripten/em_macros.h 和 Platforms/emscripten/main.py,先從現有的 EM_JS_DEPS 用法和 static-link 設定開始。重現最小的 C 範例,並檢查 web_example、web_example_pyrepl_jspi 和 browser_test 路徑;完成的標準是靜態建置不再於啟動時失敗,而是在連結期間回報不支援的參照。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- c, javascript, python, wasm
- 領域
- build-system, web-dev
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100