Stream: general

Topic: Return Refernces


view this post on Zulip Benjamin Fry (May 06 2021 at 15:17):

Hi, folks. for various reasons, I dropped work on my wasmtime-java integration, but am picking it back up now. Some tests that were working before have now stopped working, and it looks like something changed in the Rust -> WASM compilation in regards to mutable pointers (byte array out params). Basically, generated WASM import declarations are not generating the i32 input param for that field. Do folks know if something changed in a recent(ish) version of rustc/llvm that might have broken this? Thanks!

view this post on Zulip Benjamin Fry (May 06 2021 at 15:18):

I updated everything to current stable releases for reference...

view this post on Zulip Benjamin Fry (May 06 2021 at 15:20):

Here's an example, the response param doesn't end up in the generated wasm interface: https://github.com/bluejekyll/wasmtime-java/blob/main/tests/strings/src/lib.rs#L6

Wasmtime bindings for Java. Contribute to bluejekyll/wasmtime-java development by creating an account on GitHub.

view this post on Zulip Alex Crichton (May 06 2021 at 15:20):

You might be running into how the wasm32-wasi target had its ABI updated to match C perhaps? Do you have the signature of the function in question?

view this post on Zulip Alex Crichton (May 06 2021 at 15:21):

ah yeah that's the ABI update, the first argument is now passed indirectly on the wasi target rather than by-value as two params

view this post on Zulip Alex Crichton (May 06 2021 at 15:21):

(assuming WasmSlice is two u32 values)

view this post on Zulip Benjamin Fry (May 06 2021 at 15:21):

Yes, WasmSlice is two u32's

view this post on Zulip Benjamin Fry (May 06 2021 at 15:22):

We we can't do repr(C) type params (anymore)?

view this post on Zulip Alex Crichton (May 06 2021 at 15:23):

you can, the ABI is just different

view this post on Zulip Alex Crichton (May 06 2021 at 15:23):

clang decided that the ABI for any aggregate parameter is for it to be passed indirectly

view this post on Zulip Till Schneidereit (May 06 2021 at 15:23):

Also, this RFC is highly relevant: https://github.com/bytecodealliance/rfcs/pull/11

It'll be a bit until it's fully finalized and implemented, but not that long either

Rendered Overhaul the wasmtime crate's API to improve it along a number of vectors: Greatly improve the multithreading story in all languages (Rust, C, Go, ...), namely enabling safe usage of...

view this post on Zulip Alex Crichton (May 06 2021 at 15:24):

oh I don't think this affects the problem @Benjamin Fry is running into, I think it's about compiling Rust to wasm instead

view this post on Zulip Benjamin Fry (May 06 2021 at 15:24):

Alex Crichton said:

you can, the ABI is just different

Do you perhaps have a link to an example?

view this post on Zulip Alex Crichton (May 06 2021 at 15:24):

there's an upstream issue about this ABI detail as well -- https://github.com/WebAssembly/tool-conventions/issues/88

Currently clang will pass any structure with more than a single element indirectly (as LLVM byval). Many other ABIs pass small structures in registers, potentially improving performance. We could d...

view this post on Zulip Alex Crichton (May 06 2021 at 15:24):

the funtion you have there is translated to (import "test" "say_hello_to_java" (func (param i32 i32)))

view this post on Zulip Alex Crichton (May 06 2021 at 15:25):

where the first i32 points to memory where there's two consecutive i32 values, and the second i32 is the return pointer from the signature

view this post on Zulip Till Schneidereit (May 06 2021 at 15:25):

oh, right. The part I was referring to was this:

for various reasons, I dropped work on my wasmtime-java integration, but am picking it back up now.

view this post on Zulip Alex Crichton (May 06 2021 at 15:25):

oh heh indeed it is highly relevant for that part :)

view this post on Zulip Alex Crichton (May 06 2021 at 15:26):

similarly for your greet function that gets translated to (export "greet" (func (param i32)))

view this post on Zulip Alex Crichton (May 06 2021 at 15:26):

which means that to call the greet function you first have to allocate space for the WasmSlice struct in memory, then fill in the struct, then pass the pointer

view this post on Zulip Benjamin Fry (May 06 2021 at 15:27):

Yeah, I understand the issue now, thanks, @Alex Crichton

view this post on Zulip Benjamin Fry (May 06 2021 at 15:28):

Is the best bet to expand these to the exact ABI, and not try and do the direct inline types then?

view this post on Zulip Alex Crichton (May 06 2021 at 15:30):

depends on your use case, but the answer is probably "yes"

view this post on Zulip Benjamin Fry (May 06 2021 at 15:52):

Thanks, @Alex Crichton that was the exact issue. I really appreciate the quick feedback. I owe you a drink at next RustConf.


Last updated: Dec 23 2024 at 13:07 UTC