Skip to content

cc long double targeting wasm32-wasi links incorrectly #74393

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

Open
TjeuKayim opened this issue Jul 16, 2020 · 1 comment
Open

cc long double targeting wasm32-wasi links incorrectly #74393

TjeuKayim opened this issue Jul 16, 2020 · 1 comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-wasm Target: WASM (WebAssembly), http://webassembly.org/ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@TjeuKayim
Copy link

A Rust crate with C dependency that uses long double targeting wasm32-wasi links incorrectly, while clang can compile such programs to WebAssembly.

I tried this code:

// main.rs
fn main() {
    unsafe { print_long_double() };
}
extern "C" {
    fn print_long_double();
}
// build.rs
cc::Build::new()
    .file("src/foo.c")
    .include("src")
    .compile("foo");
// foo.c
void print_long_double() {
    srand(0);
    long double x = rand();
    printf("x = %Lf\n", x);
}
export CC_wasm32_wasi=/opt/wasi-sdk/bin/clang
export CARGO_TARGET_WASM32_WASI_LINKER=/opt/wasi-sdk/bin/clang
export RUSTFLAGS='-C target-feature=-crt-static -C link-arg=-lc-printscan-long-double'
cargo build --target wasm32-wasi
wasmtime target/wasm32-wasi/debug/long-double.wasm

I expected to see this happen: the program should run and print a random number, just like when I compile with clang.

Instead, this happened:

Error: failed to run main module `target/wasm32-wasi/debug/long-double.wasm`

Caused by:
    0: failed to instantiate "target/wasm32-wasi/debug/long-double.wasm"
    1: unknown import: `env::__floatsitf` has not been defined

Simple repo to reproduce with Docker: https://github.com/TjeuKayim/wasi-long-double

Motivation

I tried to compile rusqlite to WASI following the instructions at https://doc.rust-lang.org/nightly/nightly-rustc/rustc_target/spec/wasm32_wasi/index.html. This was one of the issues I ran into. A work-around is to pass this flag -DLONGDOUBLE_TYPE=double. The weird thing is that when I compile sqlite with clang and wasi-sdk, it compiles and runs correctly. Also, other projects manage to compile sqlite to WebAssembly, like https://wapm.io/package/sqlite#shell.

@TjeuKayim TjeuKayim added the C-bug Category: This is a bug. label Jul 16, 2020
@Alexendoo Alexendoo added A-linkage Area: linking into static, shared libraries and binaries O-wasm Target: WASM (WebAssembly), http://webassembly.org/ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 22, 2020
@nmandery
Copy link

I stumbled over a similar issue like this. Thre reason is that the f128 type are not supported by rusts compiler-builtins - see the README there.

In my case I could resolve this by linking to the libclang_rt.builtins-wasm32 of the wasi-sdk - RUSTFLAGS I used are in this Makefile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-wasm Target: WASM (WebAssembly), http://webassembly.org/ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants