Closed
Description
When I compile this C code to wasm32-wasi
:
typedef struct Vector {
int a;
int b;
} Vector;
extern int extract_a(Vector v) {
return v.a;
}
And this Rust code to wasm32-wasi
:
#[repr(C)]
struct Vector {
a: i32,
b: i32,
}
extern "C" {
fn extract_a(v: Vector) -> i32;
}
fn main() {
unsafe {
extract_a(Vector { a: 5, b: 4 });
}
}
And try link them together, I'm getting linking errors:
= note: lld: error: function signature mismatch: extract_a
>>> defined as (i32, i32) -> i32 in /home/pierre/Projets/wasm-abi-test/target/wasm32-wasi/debug/deps/wasm_abi_test.67xpw7l331r9o5x.rcgu.o
>>> defined as (i32) -> i32 in /home/pierre/Projets/wasm-abi-test/target/wasm32-wasi/debug/build/wasm-abi-test-9c49ce7f6c5ca031/out/libfoo.a(test.o)
It seems that, according to clang, passing a struct by value should inline the fields, while for Rust it should be passed by pointer.
I uploaded an example project here: https://github.com/tomaka/wasm-abi-test
It can be built with something like:
AR_wasm32_wasi=/path/to/wasi-sdk-10.0/bin/ar CC_wasm32_wasi=/path/to/wasi-sdk-10.0/bin/clang cargo run --target=wasm32-wasi
Rust version: rustc 1.44.0-nightly (38114ff 2020-03-21)
Activity
tomaka commentedon May 4, 2020
Apparently this is a known issue:
TheBlueMatt commentedon Jan 9, 2021
This is mostly tracked at rustwasm/team#291 and would be fixed by #79998.
TheBlueMatt commentedon Jan 12, 2021
This should now work for wasm32-wasi (on current git head).
programmerjake commentedon Aug 28, 2022
Note that #83763 changed the default, so only wasm32-unknown-unknown uses the mismatched ABI, all other wasm targets (including custom targets) now default to using the ABI that matches clang:
rust/compiler/rustc_target/src/spec/wasm32_unknown_unknown.rs
Lines 22 to 30 in 482a3d0
temeddix commentedon Oct 26, 2023
There are discussions going on at
wasm-bindgen
to extend the support towasm32-wasi
in addition to the currentwasm32-unknown-unknown
.In short,
wasm-bindgen
needs a consistent C ABI betweenwasm32-unknown-unknown
andwasm32-wasi
to support both of them.Can we have ABI that matches clang on
wasm32-unknown-unknown
as well? I can help with this, but would it be a hard task?bjorn3 commentedon Oct 26, 2023
The change itself would be removing
options.default_adjusted_cabi = Some(Abi::Wasm)
from the target spec and that's it. Because it breaks older wasm-bindgen versions it will probably require waiting months to years before we can actually make this change. And preferably we would introduce a future compat warning for this change. Either against older wasm-bindgen versions or against anyextern "C"
function for which changing the abi actually has any effect.59 remaining items