-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rust 1.67.0
stopped initializing the WASI environment for exported functions on target="wasm32-wasi"
#107635
Comments
cc @sunfishcode re: apparent wasm32-wasi functionality regression |
I recognize that it worked in wasm32-wasi in earlier versions, however it was by accident. Rust isn't meant to support having programs with a Are you able to switch the crate to a cdylib, by eg. adding this to Cargo.toml, and removing the [lib]
crate-type = ["cdylib"] This configuration is not entirely stable yet, but may work as a temporary workaround. |
The workaround works, but it has some significant drawbacks for us. We have been relying on this functionality for the last 2 years. Just to give a bit more context about our use case. We expose an API to wasm modules so that they can create new instances from the same module, but use a different entry point. This requires us to allow entering the module without calling main. And this is one of our core features. The update to We have a fairly big community around our products. Just our discord consists of 800+ members and Rust is our primary language. So this change has a significant impact on us. The best outcome for us would be to allow the previous behaviour, both |
Directly reverting the PR in question would cause regressions for other users, including users who specifically need the change to not run the initializers on arbitrary exports, and who requested this change. I'm looking into other ways to fix this. @alexcrichton had the idea that we could remove all use of |
It's worth noting that while WASI is definitely expected to land in some form, it's not a finished, stable part of WebAssembly, because it's big enough as an idea to have its own WASI subproposals. If I understand correctly, WASI programs have to initialize their own environment, rather than the system function definitions being included via some dynamic linker equivalent transcluding libc-or-whatnot? And I would guess the same for other things, instead of simply having certain things happen implicitly due to the operating system's program loader? Interesting. On most systems, somehow bypassing those two components of the system could have equally annoying consequences, it just doesn't happen because it's (mostly) not possible. |
WG-prioritization assigning priority (Zulip discussion). @rustbot label -I-prioritize +P-medium |
Use `__wasilibc_get_environ()` to read the environment variable list from wasi-libc instead of using `environ`. `environ` is a global variable which effectively requires wasi-libc to initialize the environment variables eagerly, and `__wasilibc_get_environ()` is specifically designed to be an alternative that lets wasi-libc intiailize its environment variables lazily. This should have the side effect of fixing at least some of the cases of rust-lang#107635.
I've now submitted #107866 which changes Rust's use of the global |
This workaround doesn't seem to work for me. I compiled, linked and initialize a wasm blob here https://github.com/lapce/lapce/blob/master/lapce-proxy/src/plugin/wasi.rs#L360-L520 And it is trying to compile a wasm of this repo https://github.com/hbina/lapce-prettier And with some grep-foo, I can see that it is trying to initialize the environment variable eagerly? Compiling as a
Where
And Compiling as
Where
It doesn't call ctors because....the dtor is just a dummy?
but it doesn't seem to work still. Edit: Is there a tool to do this for me? Something like |
…environ, r=workingjubilee Allow wasi-libc to initialize its environment variables lazily. Use `__wasilibc_get_environ()` to read the environment variable list from wasi-libc instead of using `environ`. `environ` is a global variable which effectively requires wasi-libc to initialize the environment variables eagerly, and `__wasilibc_get_environ()` is specifically designed to be an alternative that lets wasi-libc intiailize its environment variables lazily. This should have the side effect of fixing at least some of the cases of rust-lang#107635.
@hbina Can you try again with the latest nightly? It should be carrying the partial fix mentioned, which should cover your case. |
Hi @workingjubilee I have updated my previous post to add some clarity and use |
The latest nightly now includes #107866 and lazily initializes environment variables, at least in simple examples, which I hope will fix the immediate bug with environment variables. For the more general fix, I've now submitted #108097 which switches Rust's |
The code:
would produce the following Wasm assembly on Rust
1.66.1
compiled withcargo build --target="wasm32-wasi"
:On
1.67.0
it produces this one:Notice that the call to the
$__wasm_call_ctors
function is missing in the exportedtest
. This means that when entering the Wasm instance through the exported function, it will not have a correctly set up WASI environment and calls to open files or look up environment variables will fail. The_start
function in both versions calls$__wasm_call_ctors
and works correctly.I'm not sure what commit broke this.
The only PR that is related to this seems to be this one #105405.EDIT: Or more likely, this one #105395
The text was updated successfully, but these errors were encountered: