Skip to content

compile-extension: Rust (cc-rs) C code misses -fwasm-exceptions/SUPPORT_LONGJMP and ZVAL_LONG64 flags, so the .so fails to load (bad export type for '__THREW__') #4377

Open

Description

@buvinghausen

@php-wasm/compile-extension builds Rust (ext-php-rs) extensions that fail to load in the JSPI runtimes with bad export type for '__THREW__', and with a 32-bit zend_long.

In packages/php-wasm/compile-extension/scripts/build-in-docker.sh (trunk e1634b3), the flags the script applies to the extension's C code —

BASE_CFLAGS="-DZEND_ENABLE_ZVAL_LONG64 -D__x86_64__ -fPIC -O${OPTIMIZE}"   # line 31
ASYNC_FLAGS="-sSUPPORT_LONGJMP=wasm -fwasm-exceptions -sJSPI"             # line 39

— don't reach the C that cargo compiles through cc-rs (ext-php-rs's wrapper.c), which gets only:

export CFLAGS_wasm32_unknown_emscripten="-fPIC ${CFLAGS_wasm32_unknown_emscripten:-}"   # line 54

So wrapper.c is compiled with JS-based setjmp/longjmp (hence __THREW__ at load) and with a 32-bit zend_long, while PHP itself has a 64-bit one. Adding the same four flags there (and to CXXFLAGS_wasm32_unknown_emscripten) fixes it:

export CFLAGS_wasm32_unknown_emscripten="-fPIC -DZEND_ENABLE_ZVAL_LONG64 -D__x86_64__ -sSUPPORT_LONGJMP=wasm -fwasm-exceptions ${CFLAGS_wasm32_unknown_emscripten:-}"

With that, an ext-php-rs extension built on stable Rust 1.99 loads into the stock @php-wasm/node / @php-wasm/web 3.1.56 runtimes (PHP 8.5, JSPI) and runs under node and in headless Chromium.

Two README notes for the same tool: it says Rust extensions need nightly and -Zbuild-std, but stable Rust's prebuilt wasm32-unknown-emscripten std links and loads fine; and it reads as if the Rust build runs inside the image, which has no rustc or libclang (bindgen needs both).

Happy to send a PR if useful.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions