`cabi_realloc` is referenced but never linked on `wasm32-unknown-emscripten`

Đang mở
#1,751 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
Nửa ngày
Mức phù hợp với người mới
68/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust, wasm
Lĩnh vực
build-system

Hướng nghiên cứu

Start in crates/guest-rust/build.rs, where the CARGO_CFG_TARGET_FAMILY comparison fails on unix,wasm, and then check the matching cfg in src/rt/mod.rs (maybe_link_cabi_realloc). Reproduce with cargo build --release --target wasm32-unknown-emscripten on a staticlib that depends on wit-bindgen. Done when that build links, the two checks agree on whether Emscripten is included, and the other wasm and Linux targets build as before.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

crates/guest-rust/build.rs (still the case in v0.63.0) decides whether to link the cabi_realloc shim with:

let target_family = env::var("CARGO_CFG_TARGET_FAMILY").unwrap_or(String::new());
if target_family != "wasm" {
    return;
}

Cargo joins a cfg with several values with commas. wasm32-unknown-emscripten is the one wasm target with two families, so CARGO_CFG_TARGET_FAMILY is unix,wasm there, the comparison fails, and the build script returns before linking anything.

The crate's own Rust cfgs disagree with it. In src/rt/mod.rs, maybe_link_cabi_realloc references cabi_realloc under #[cfg(all(target_family = "wasm", any(target_env = "p1", target_env = "")))], and cfg(target_family = "wasm") is true on Emscripten, whose target_env is "". So the symbol is referenced but never provided. Any static library built on wit-bindgen for Emscripten then fails its final emcc link.

Reproduction (wit-bindgen 0.63.0, Rust 1.98.0, Emscripten 6.0.12):

# Cargo.toml
[lib]
crate-type = ["staticlib"]
[dependencies]
wit-bindgen = "=0.63.0"
// src/lib.rs, with wit/w.wit = `package repro:w; world w { export greet: func(name: string) -> string; }`
wit_bindgen::generate!({ world: "w", path: "wit" });
struct C;
impl Guest for C { fn greet(name: String) -> String { format!("hi {name}") } }
export!(C);
#[unsafe(no_mangle)]
pub extern "C" fn answer() -> i32 { 42 }
cargo build --release --target wasm32-unknown-emscripten
emcc main.c target/wasm32-unknown-emscripten/release/libwbrepro.a -o m.js
# wasm-ld: error: …wit_bindgen….rcgu.o: undefined symbol: cabi_realloc

(main.c calls answer().)

Fix: read the family as a list.

if !target_family.split(',').any(|f| f == "wasm") {
    return;
}

With this patched in through [patch.crates-io], the reproduction links and runs under node. Builds for wasm32-unknown-unknown, wasm32-wasip2 and x86_64-unknown-linux-gnu are unaffected; Emscripten is the only target whose family list differs.

The check dates from #851, where it reads as "any wasm target", with a panic for wasm architectures other than wasm32. I found nothing suggesting Emscripten was meant to be excluded. If it was, the cfgs in rt/mod.rs would need to exclude it too, so as it stands the two halves disagree either way.

Context: we hit this building a C ABI whose types come from wit-bindgen-generated bindings, for hosts that compile to the browser with Emscripten. We work around it with a #[no_mangle] extern "C" fn cabi_realloc on Emscripten that forwards to wit_bindgen::rt::cabi_realloc.

Ngôn ngữ chính
Rust
Star
1.5k
Fork
306
Merge trung bình
1 ngày 4 giờ
Pull request đã merge (30 ngày)
21

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

  • Không có Dockerfile hay tệp Docker Compose
  • Không có mẫu pull request
  • Không có hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của bytecodealliance/wit-bindgen

Tất cả issue của bytecodealliance/wit-bindgen

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.