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

Offen
#1,751 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
Ein halber Tag
Anfängerfreundlichkeit
68/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
rust, wasm
Bereich
build-system

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
Rust
Sterne
1.5k
Forks
306
Ø Merge
1 T. 4 Std.
Gemergte PRs (30 T.)
21

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

  • Kein Dockerfile und keine Docker-Compose-Datei
  • Keine Pull-Request-Vorlage
  • Kein Beitragsleitfaden

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus bytecodealliance/wit-bindgen

Alle Issues in bytecodealliance/wit-bindgen

Ähnliche Issues

Weitere Issues zu Rust

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.