-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Accessing foreign global variables under wasm32-unknown-unknown target #60825
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
Comments
The online tool you linked is based on LLVM 5, which is very old, so it's more likely that the code compiled by C++ there is incorrect. In fact |
@CryZe Interesting. Thanks for reply. What do you mean by |
I'm running into the same issue. Imported global appear to be broken currently. |
Noticed same issue. @CryZe in example above there are no pointers involved. |
Sure but that sounds like you want some intrinsics via std::arch and not statics?! Like what is even supposed to happen here if you take a reference and then dereference it? Unless that is supposed to be Undefined Behavior. But can we even make it Undefined Behavior? Pretty sure getting a reference and dereferencing a static is not considered UB by Rust, so it can't be made UB when compiling to wasm?! (Or can it?) Unless Rust adopts what C++ seems to be doing, which is always treating the static like a pointer into linear memory and dereferencing it. This seems fairly reasonable too. All wasm globals will be pointers then though, never actual integers or floating point numbers. Although if they are pointers, that would mean that the environment somehow needs to "allocate" the actual variable into linear memory, which seems hard to do if you don't want to hardcode that for a specific wasm file into the environment. |
@CryZe C++ behaviour seems to be the closest to correct one here. The end result has to be that |
Custom globals are not supported by the WebAssembly target and I believe C/C++ have since been updated to reflect this as well. There's currently no plans to support this, and I'd recommend opening an issue on https://github.com/webassembly/tool-conventions to track how linear-memory languages like Rust/C/C++ will support this if desired. |
When trying to compile a code that uses foreign global variables under
wasm32-unknown-unknown
it appears that wrong code is generated. My use case is that I want to export a variablefoo
from the host, so Rust program compiled into wasm can use value of this variable.I tried this code:
Compiled with:
Produces following WASM code:
Rust appears to treat variable
extern "C" static foo: i32;
as variable with local linkage without emitting "import global" opcodes.This should be the direct translation into C++ code. This also declares a variable with external linkage:
Compiled into WASM:
Note the
(import "env" "foo" (global $foo i32))
: This is what C++ compiler emits when it encounters a variable declared with an external linkage. SourceI believe current behaviour of Rust compiler is incorrect and wrong WASM code is generated for variables with external C linkage. Perhaps there is a special syntax for emitting
(import "env" "foo" (global $foo i32))
? Right now the only workaround seems to be using functions instead of variables to communicate between host and the wasm program which works perfectly fine. Please let me know if I missed something and there is an easy way to achieve this.Meta
rustc --version --verbose
:I've also tried on stable, and other versions of rust nightly.
The text was updated successfully, but these errors were encountered: