Skip to content

Conversation

wujunzhuo
Copy link

No description provided.

@yamt
Copy link
Contributor

yamt commented Feb 15, 2023

can you explain how can it lead memory leak?
note that this api is for a very special purpose. bytecodealliance/wizer#8 (comment)

@TerrorJack
Copy link
Contributor

We should not complicate wasi-libc ctor logic by trying to make them idempotent (or at least not leak memory or do other weird stuff when called twice). They are never meant to be called more than once.

If the ctor is indeed called more than once, it's a misuse, due to targetting wasi command modules in old versions of wasi-sdk. wasm-ld would wrap the exports with __wasm_call_ctors and __wasm_call_dtors, so calling those exports multiple times would invoke ctors/dtors multiple times, and undefined behaviors await.

The situation has been mitigated by #328. However, users should still explicitly pass -mexec-model=reactor at link-time if they do want to call exported functions more than once.

@wujunzhuo
Copy link
Author

@yamt @TerrorJack

The memory leak phenomenon is described in the issue #389, and it's very easy to reproduce.

However, users should still explicitly pass -mexec-model=reactor at link-time if they do want to call exported functions more than once.

It seems like this bug is due to the lack of passing reactor model type in Rustc/Cargo?

@TerrorJack
Copy link
Contributor

The memory leak phenomenon is described in the issue #389

That is not a wasi-libc issue.

It seems like this bug is due to the lack of passing reactor model type in Rustc/Cargo?

Try

[target.wasm32-wasi]
rustflags = ["-C", "link-arg=-mexec-model=reactor"]

@wujunzhuo
Copy link
Author

Try

[target.wasm32-wasi]
rustflags = ["-C", "link-arg=-mexec-model=reactor"]
wujunzhuo@wujunzhuo-macbook wasm % cat ~/.cargo/config.toml 
[target.wasm32-wasi]
rustflags = ["-C", "link-arg=-mexec-model=reactor"]
wujunzhuo@wujunzhuo-macbook wasm % cargo build --release --target wasm32-wasi
   Compiling hello v0.1.0 (/Users/wujunzhuo/Desktop/hello/wasm)
error: linking with `rust-lld` failed: exit status: 1
  |
  = note: "rust-lld" "-flavor" "wasm" "--rsp-quoting=posix" "--export" "hello" "-z" "stack-size=1048576" "--stack-first" "--allow-undefined" "--fatal-warnings" "--no-demangle" "/Users/wujunzhuo/Desktop/hello/wasm/target/wasm32-wasi/release/deps/hello.hello.acc9aabb-cgu.0.rcgu.o" "/Users/wujunzhuo/Desktop/hello/wasm/target/wasm32-wasi/release/deps/hello.hello.acc9aabb-cgu.1.rcgu.o" "/Users/wujunzhuo/Desktop/hello/wasm/target/wasm32-wasi/release/deps/hello.hello.acc9aabb-cgu.2.rcgu.o" "/Users/wujunzhuo/Desktop/hello/wasm/target/wasm32-wasi/release/deps/hello.hello.acc9aabb-cgu.3.rcgu.o" "/Users/wujunzhuo/Desktop/hello/wasm/target/wasm32-wasi/release/deps/hello.hello.acc9aabb-cgu.4.rcgu.o" "/Users/wujunzhuo/Desktop/hello/wasm/target/wasm32-wasi/release/deps/hello.hello.acc9aabb-cgu.5.rcgu.o" "/Users/wujunzhuo/Desktop/hello/wasm/target/wasm32-wasi/release/deps/hello.hello.acc9aabb-cgu.6.rcgu.o" "/Users/wujunzhuo/Desktop/hello/wasm/target/wasm32-wasi/release/deps/hello.1ykrkc27kh9t6on3.rcgu.o" "-L" "/Users/wujunzhuo/Desktop/hello/wasm/target/wasm32-wasi/release/deps" "-L" "/Users/wujunzhuo/Desktop/hello/wasm/target/release/deps" "-L" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/libstd-09fa6ca09f86d164.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/libpanic_abort-c3e95cd39c57a54c.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/libwasi-fbb489748f675963.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/librustc_demangle-cd39d41079a84f6f.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/libstd_detect-a0fc91e2cfaf61dd.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/libhashbrown-a62345a7d8a42199.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/libminiz_oxide-db5f21836df1d8ae.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/libadler-9f21871e5221501a.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/librustc_std_workspace_alloc-288ed4fa2a08c0e9.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/libunwind-38fdae42d1315457.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/libcfg_if-14a890fc8e331c29.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/liblibc-1cd46bbe1fa300e9.rlib" "-l" "c" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/liballoc-1c2004caac219938.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/librustc_std_workspace_core-1fbc4eaf09b3d6fd.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/libcore-41d052a032f91e0b.rlib" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/libcompiler_builtins-61af768e1b976a69.rlib" "-L" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib" "-L" "/Users/wujunzhuo/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/wasm32-wasi/lib/self-contained" "-o" "/Users/wujunzhuo/Desktop/hello/wasm/target/wasm32-wasi/release/deps/hello.wasm" "--gc-sections" "--no-entry" "-O3" "-mexec-model=reactor"
  = note: rust-lld: error: invalid target architecture: exec-model=reactor
          

error: could not compile `hello` due to previous error

Is there any mistake?

@TerrorJack
Copy link
Contributor

Try adding linker = "clang" (or absolute path of clang. -mexec-model flag is a clang driver flag, not an lld flag)

@wujunzhuo
Copy link
Author

Try adding linker = "clang" (or absolute path of clang. -mexec-model flag is a clang driver flag, not an lld flag)

Still not work... Alright, I'll close this pr, and open a new issue in the Rust toolchain community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants