From 577bfbf31e46d92871a8c8181c41cffd86d2e5ae Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 02:53:27 -0800 Subject: [PATCH 01/13] chore(quest): claim quest/m2/wasm/refuse.md Quest-Claim: 1c58d2663fb2fab1 From 5de37302a4529a1ac671a89d039e8c41e7e66823 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 03:41:45 -0800 Subject: [PATCH 02/13] feat(ffi): refuse at export what the web target's host lacks Checks the generated crate for wasm32-wasip1 under the driver and walks every function its exports reach through each crate's MIR, to each import and C function, refusing the call in the crate's own code that reaches an import the web binding does not implement. Co-Authored-By: Claude Opus 5.5 --- driver/src/cargo.rs | 8 +- ffi/Cargo.lock | 125 +++++ ffi/Cargo.toml | 1 + ffi/fixtures/lacks/Cargo.toml | 22 + ffi/fixtures/lacks/expected.txt | 15 + ffi/fixtures/lacks/ffi.toml | 1 + ffi/fixtures/lacks/src/lib.rs | 138 ++++++ ffi/src/cli.rs | 51 ++- ffi/src/driver.rs | 6 +- ffi/src/main.rs | 3 + ffi/src/wasm.rs | 788 ++++++++++++++++++++++++++++++++ ffi/src/web.rs | 17 + ffi/tests/fixtures.rs | 62 +++ flake.nix | 13 +- ts/src/cli.rs | 1 + 15 files changed, 1239 insertions(+), 12 deletions(-) create mode 100644 ffi/fixtures/lacks/Cargo.toml create mode 100644 ffi/fixtures/lacks/expected.txt create mode 100644 ffi/fixtures/lacks/ffi.toml create mode 100644 ffi/fixtures/lacks/src/lib.rs create mode 100644 ffi/src/wasm.rs diff --git a/driver/src/cargo.rs b/driver/src/cargo.rs index e68a0b23a..c79ca1557 100644 --- a/driver/src/cargo.rs +++ b/driver/src/cargo.rs @@ -135,6 +135,9 @@ pub struct Check<'a> { pub args: &'a [String], /// The environment cargo runs in, which the wrapper reads its job from. pub env: &'a [(&'a str, &'a OsStr)], + /// Where cargo builds, absolute, if not [`target_dir`]'s for the manifest: another + /// package's, whose crates this check then shares. + pub target_dir: Option<&'a Path>, } impl Check<'_> { @@ -142,7 +145,10 @@ impl Check<'_> { /// stale, so the package always recompiles. pub fn run(&self) -> Result { let exe = std::env::current_exe().map_err(|err| format!("finding this executable: {err}"))?; - let target_dir = target_dir(self.tool, Some(self.manifest))?; + let target_dir = match self.target_dir { + Some(dir) => dir.to_path_buf(), + None => target_dir(self.tool, Some(self.manifest))?, + }; let package_dir = self.manifest.parent().expect("a manifest path has a parent"); let cargo = |subcommand: &str| { let mut cmd = cargo(); diff --git a/ffi/Cargo.lock b/ffi/Cargo.lock index 434df6316..7af119daf 100644 --- a/ffi/Cargo.lock +++ b/ffi/Cargo.lock @@ -25,6 +25,18 @@ dependencies = [ "url", ] +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "bumpalo" +version = "3.20.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87a0cf66fd1eb2fb7b822cb78370d82303750809bc6ba855f3f58e517d3238db" + [[package]] name = "bytes" version = "1.12.1" @@ -93,6 +105,30 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + [[package]] name = "generator" version = "0.8.12" @@ -282,6 +318,24 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "js-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dc09d2154098bbf4ef54eab6b117a3c24cbbffe94046b16ee6d6d4510584d3a" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "lacks-fixture" +version = "0.0.0" +dependencies = [ + "web-sys", +] + [[package]] name = "lazy_static" version = "1.5.1" @@ -431,6 +485,7 @@ dependencies = [ "serde", "serde_json", "toml", + "wasmparser", ] [[package]] @@ -552,6 +607,12 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + [[package]] name = "smallvec" version = "1.16.4" @@ -768,6 +829,70 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasm-bindgen" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0181c14ddd6c7b8b0b0132a5f1ea2f32be0d893cd3ea97b6e3b28886b8a3d1c" +dependencies = [ + "cfg-if", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e931a02a39abdbf66966fc35902b5ed58344750a70885bed4ebb3d8eaf28ed9" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ca4b54280e455bd831e33a6b8e734baf432fe63fbc35c42ac14ce67a043a53a" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 3.0.9", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b1f9c3798725016c6ee5f2f57869ec0831e8bbc0ab0310a86e787e97f41389e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "wasmparser" +version = "0.263.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f5fa0a054ae153606df820eb0a84213e28cda41bbe04ba2628e71b6ba100fcb" +dependencies = [ + "bitflags", + "indexmap", +] + +[[package]] +name = "web-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7d179596ff5ec0f6de4db64d54011bde6289cc49603d3a6e4ce3f329207ab88" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "winapi-util" version = "0.1.11" diff --git a/ffi/Cargo.toml b/ffi/Cargo.toml index 47cdf090d..6ee07cb45 100644 --- a/ffi/Cargo.toml +++ b/ffi/Cargo.toml @@ -26,6 +26,7 @@ rust2-driver = { path = "../driver" } serde = { version = "1.0.228", features = ["derive"] } serde_json = "1.0.154" toml = "1.1.8" +wasmparser = { version = "0.263.0", default-features = false, features = ["std"] } [dev-dependencies] loom = "0.7.3" diff --git a/ffi/fixtures/lacks/Cargo.toml b/ffi/fixtures/lacks/Cargo.toml new file mode 100644 index 000000000..feccf6c3e --- /dev/null +++ b/ffi/fixtures/lacks/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "lacks-fixture" +description = "Exports whose wasm32-wasip1 module would import what the web target's host lacks, each refused at the call that reaches it" +version = "0.0.0" +edition = "2024" +license = "MIT OR Apache-2.0" +publish = false + +# The export's prefix. +[lib] +name = "lacks" + +[dependencies] +web-sys = { version = "0.3.107", features = ["console"] } + +[lints] +workspace = true + +[features] +# A call of a C function no library defines, which fails the module's link, so a build +# without it can compare the module's imports with what the export refused. +undefined = [] diff --git a/ffi/fixtures/lacks/expected.txt b/ffi/fixtures/lacks/expected.txt new file mode 100644 index 000000000..b8104920b --- /dev/null +++ b/ffi/fixtures/lacks/expected.txt @@ -0,0 +1,15 @@ +fixtures/lacks/src/lib.rs:104:11: rust2-ffi: this calls `undefined`, which no library wasm32-wasip1's module links defines; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:116:11: rust2-ffi: the module imports `page.alert`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:121:27: rust2-ffi: `>::into` reaches `__wbindgen_describe`, `__wbindgen_describe_generic_import`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:121:2: rust2-ffi: `web_sys::features::gen_console::console::log_1` reaches `__wbg_log_ff86bbb34c295e21`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:121:39: rust2-ffi: the drop of `wasm_bindgen::JsValue` reaches `__wbindgen_object_drop_ref`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:14:2: rust2-ffi: `std::thread::sleep` reaches `wasi_snapshot_preview1.poll_oneoff`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:18:1: rust2-ffi: the drop of `std::net::TcpListener` reaches `wasi_snapshot_preview1.fd_close`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:29:25: rust2-ffi: the drop of `std::result::Result<(std::net::TcpStream, std::net::SocketAddr), std::io::Error>` reaches `wasi_snapshot_preview1.fd_close`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:29:3: rust2-ffi: `std::net::TcpListener::accept` reaches `wasi_snapshot_preview1.sock_accept`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:41:3: rust2-ffi: `std::fs::read_to_string` reaches `wasi_snapshot_preview1.fd_close`, `wasi_snapshot_preview1.fd_fdstat_get`, `wasi_snapshot_preview1.fd_filestat_get`, `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.fd_read`, `wasi_snapshot_preview1.path_open`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:52:2: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:62:2: rust2-ffi: `std::fs::exists` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:85:11: rust2-ffi: `std::fs::remove_file` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_unlink_file`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:91:2: rust2-ffi: `std::fs::read_to_string` reaches `wasi_snapshot_preview1.fd_close`, `wasi_snapshot_preview1.fd_fdstat_get`, `wasi_snapshot_preview1.fd_filestat_get`, `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.fd_read`, `wasi_snapshot_preview1.path_open`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:9:2: rust2-ffi: `std::fs::read_to_string` reaches `wasi_snapshot_preview1.fd_close`, `wasi_snapshot_preview1.fd_fdstat_get`, `wasi_snapshot_preview1.fd_filestat_get`, `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.fd_read`, `wasi_snapshot_preview1.path_open`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config diff --git a/ffi/fixtures/lacks/ffi.toml b/ffi/fixtures/lacks/ffi.toml new file mode 100644 index 000000000..7720948f9 --- /dev/null +++ b/ffi/fixtures/lacks/ffi.toml @@ -0,0 +1 @@ +exclude = [] diff --git a/ffi/fixtures/lacks/src/lib.rs b/ffi/fixtures/lacks/src/lib.rs new file mode 100644 index 000000000..bec3cb66e --- /dev/null +++ b/ffi/fixtures/lacks/src/lib.rs @@ -0,0 +1,138 @@ +//! Exports reaching what the web target's host lacks, each by another route the walk +//! follows, and exports reaching only what it has, which export. + +use std::os::fd::FromRawFd; +use std::time::{Duration, UNIX_EPOCH}; + +/// A file read: the file system, which a web page has none of. +pub fn read(path: String) -> Option { + std::fs::read_to_string(path).ok() +} + +/// A sleep, which a web page's one thread cannot do. +pub fn nap(ms: u64) { + std::thread::sleep(Duration::from_millis(ms)); +} + +/// A socket accepting a connection. +pub struct Listener(std::net::TcpListener); + +impl Listener { + /// The listener a host passed as `fd`. + pub fn adopt(fd: i32) -> Listener { + // SAFETY: the fixture is only exported, never run. + Listener(unsafe { std::net::TcpListener::from_raw_fd(fd) }) + } + + /// Whether a connection came. + pub fn accept(&self) -> bool { + self.0.accept().is_ok() + } +} + +trait Source { + fn load(&self) -> String; +} + +struct Disk; + +impl Source for Disk { + fn load(&self) -> String { + std::fs::read_to_string("disk").unwrap_or_default() + } +} + +/// A file read through `dyn`, refused in the implementation that reads. +pub fn loaded() -> String { + let source: Box = Box::new(Disk); + source.load() +} + +fn exists() -> bool { + std::fs::metadata("pointer").is_ok() +} + +/// A file read through a function pointer. +pub fn pointed() -> bool { + let f: fn() -> bool = exists; + f() +} + +fn listed() -> bool { + std::fs::exists("listed").unwrap_or(false) +} + +static HANDLERS: [fn() -> bool; 1] = [listed]; + +/// A file read through a static's table of functions. +pub fn handled() -> bool { + HANDLERS[0]() +} + +/// A file removed when its handle drops. +pub struct Temp { + path: String, +} + +impl Temp { + pub fn new(path: String) -> Temp { + Temp { path } + } +} + +impl Drop for Temp { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.path); + } +} + +/// A file read once the future is polled. +pub async fn fetch(path: String) -> Option { + std::fs::read_to_string(path).ok() +} + +#[cfg(feature = "undefined")] +unsafe extern "C" { + /// A C function no library the module links defines. + fn undefined(); +} + +/// A call of what nothing defines. +#[cfg(feature = "undefined")] +pub fn linked() { + // SAFETY: the fixture is only exported, never run. + unsafe { undefined() } +} + +#[link(wasm_import_module = "page")] +unsafe extern "C" { + /// A function of a module the binding does not implement. + fn alert(); +} + +/// A call of what the module imports from elsewhere. +pub fn shout() { + // SAFETY: the fixture is only exported, never run. + unsafe { alert() } +} + +/// A browser API through web-sys, whose wasm-bindgen panics on WASI rather than import it. +pub fn log(text: String) { + web_sys::console::log_1(&text.into()); +} + +/// The real-time clock, which the host has. +pub fn now() -> u64 { + let since = std::time::SystemTime::now().duration_since(UNIX_EPOCH); + since.map(|d| d.as_secs()).unwrap_or(0) +} + +/// Randomness, which the host has: a map's keys are seeded from it. +pub fn seeded() -> usize { + std::collections::HashMap::::new().capacity() +} + +/// Output and the environment, which the host has. +pub fn greet() { + println!("hello from {:?}", std::env::var("USER").ok()); +} diff --git a/ffi/src/cli.rs b/ffi/src/cli.rs index 71f741d1f..481d37804 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -93,6 +93,12 @@ fn try_run(args: &[String]) -> Result { let roots = std::env::join_paths(std::iter::once(workspace).chain(cargo_home)) .map_err(|err| format!("joining the roots of spans: {err}"))?; let baseline = own.baseline.unwrap_or_default(); + let sysroot = crate::wasm::sysroot()?; + // A target with a host checks std's MIR too, from the sysroot or else from source. + let cargo_args = |target: &str, args: Vec| match crate::wasm::host(target).is_some() && sysroot.is_none() { + true => [args, vec![crate::wasm::STD.to_string()]].concat(), + false => args, + }; let check = |target: &str, output: (&str, &std::ffi::OsStr), merge: &std::ffi::OsStr, baseline: &Path| { Check { tool: "rust2-ffi", @@ -101,8 +107,12 @@ fn try_run(args: &[String]) -> Result { root: None, // Always named, so cargo's config never picks another: the exports are found on it. target: Some(target), - args: &args.cargo, + args: &cargo_args(target, args.cargo.clone()), + target_dir: None, env: &[ + // The wrapper's answer to cargo's query names the sysroot, which cargo would + // otherwise cache by the executables alone. + ("CARGO_CACHE_RUSTC_INFO", "0".as_ref()), (config::env::CONFIG, args.config.as_os_str()), (config::env::PACKAGE, line.as_ref()), (config::env::PATCH, patch.as_ref()), @@ -137,10 +147,41 @@ fn try_run(args: &[String]) -> Result { std::fs::remove_dir_all(&stage).map_err(|err| format!("removing {}: {err}", stage.display()))?; } let code = code?; - if output.0 == config::env::OUT && code == ExitCode::SUCCESS { - lock(&checked, &output.1, &picks)?; - } - Ok(code) + if output.0 != config::env::OUT || code != ExitCode::SUCCESS { + return Ok(code); + } + lock(&checked, &output.1, &picks)?; + // The web binding is written when the export serves its target, whose host lacks what + // the generated crate may reach: walked in the facade's target directory, which holds + // every crate it builds with already. + if !output.1.join("web").join("index.ts").exists() { + return Ok(code); + } + let glue = format!("{}-ffi", facade.name); + let own: Vec = (checked.metadata.packages.iter()) + .filter(|p| p.source.is_none()) + .flat_map(|p| p.targets.iter().filter(|t| t.kind.iter().any(|k| k.contains("lib")))) + .map(|t| t.name.replace('-', "_")) + .collect(); + let own = own.join(","); + let target_dir = rust2_driver::cargo::target_dir("rust2-ffi", Some(&args.manifest))?; + let mut cargo = picks.resolving().args(); + cargo.extend(["--features".to_string(), "web".to_string()]); + Check { + tool: "rust2-ffi", + manifest: &output.1.join("Cargo.toml"), + package: &glue, + root: None, + target: Some(crate::web::TARGET), + args: &cargo_args(crate::web::TARGET, cargo), + target_dir: Some(&target_dir), + env: &[ + ("CARGO_CACHE_RUSTC_INFO", "0".as_ref()), + (crate::wasm::env::PACKAGE, glue.as_ref()), + (crate::wasm::env::OWN, own.as_ref()), + ], + } + .run() } /// Write the generated crate's lockfile: the check's, with what the crate adds of its own diff --git a/ffi/src/driver.rs b/ffi/src/driver.rs index eee9cbfc5..b3249d782 100644 --- a/ffi/src/driver.rs +++ b/ffi/src/driver.rs @@ -37,7 +37,11 @@ pub fn run(args: Vec) -> ExitCode { } }; } - rust2_driver::rustc::run("rust2-ffi", args, |args, _| { + rust2_driver::rustc::run("rust2-ffi", args, |args, added| { + crate::wasm::flags(args, added)?; + if let Some(walk) = crate::wasm::Walk::for_args(args)? { + return Ok(Some(Box::new(walk))); + } Ok(Export::for_args(args)?.map(|job| Box::new(job) as rust2_driver::rustc::Job)) }) } diff --git a/ffi/src/main.rs b/ffi/src/main.rs index bf5f8a9ad..e637fc4c1 100644 --- a/ffi/src/main.rs +++ b/ffi/src/main.rs @@ -21,6 +21,8 @@ extern crate rustc_middle; extern crate rustc_parse; extern crate rustc_session; extern crate rustc_span; +extern crate rustc_structures; +extern crate rustc_symbol_mangling; extern crate rustc_trait_selection; mod abi; @@ -40,6 +42,7 @@ mod lock; mod merge; mod python; mod swift; +mod wasm; mod web; use std::process::ExitCode; diff --git a/ffi/src/wasm.rs b/ffi/src/wasm.rs new file mode 100644 index 000000000..b73ee3970 --- /dev/null +++ b/ffi/src/wasm.rs @@ -0,0 +1,788 @@ +//! What a wasm32 target's host lacks, refused at export at the call that reaches it. +//! +//! A target's binding implements its module's imports with its host's APIs, and refuses at +//! `load` a module importing anything else. So once the crate is generated, the export +//! checks it for the target, as `cargo build` would compile it, and walks every function +//! the module's exports reach: through std, `dyn`, function pointers, statics, and drop +//! glue, as rustc's monomorphization collector does, but into every crate's MIR rather than +//! stopping at another crate's code. Each walk ends at a function the module imports, or +//! one it links from C, whose imports a link of that function alone shows. What reaches an +//! import the host lacks is refused at the call in the crate's own code nearest it, so a +//! new std needs no change here: the imports, not a list of std's paths, decide. + +use std::path::{Path, PathBuf}; +use std::process::Command; + +use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet}; +use rustc_driver::{Callbacks, Compilation}; +use rustc_structures::NativeLibKind; +use rustc_hir::def::DefKind; +use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; +use rustc_interface::interface::{Compiler, Config}; +use rustc_middle::middle::exported_symbols::ExportedSymbol; +use rustc_middle::mir::interpret::{AllocId, GlobalAlloc}; +use rustc_middle::mono::{CollectionMode, MonoItem}; +use rustc_middle::ty::{self, Instance, InstanceKind, TyCtxt}; +use rustc_span::{DUMMY_SP, Span}; + +/// A target whose binding implements its module's imports, so the export refuses what its +/// host lacks. +pub struct Host { + pub target: &'static str, + /// What a refusal calls the target. + name: &'static str, + /// Whether the binding implements the function `name` the module imports from `module`. + implements: fn(&str, &str) -> bool, +} + +/// Each target the export knows the host of. +const HOSTS: &[Host] = &[Host { + target: crate::web::TARGET, + name: "the web target", + implements: |module, name| match module { + // What JavaScript implements, which the glue imports and its binding provides. + "rust2" => true, + "wasi_snapshot_preview1" => crate::web::wasi().contains(&name), + _ => false, + }, +}]; + +/// The host of `target`, if the export knows it. +pub fn host(target: &str) -> Option<&'static Host> { + HOSTS.iter().find(|host| host.target == target) +} + +/// The rustc flags of every crate compiled for a target with a host: all of its MIR, which +/// `cargo check` encodes for generic and inline functions alone, with each call where the +/// source makes it, as inlining would not keep it. +const MIR: [&str; 2] = ["-Zalways-encode-mir", "-Zinline-mir=no"]; + +/// The std crates `-Zbuild-std` checks for a target with a host when no sysroot is set, +/// which flake.nix's sysroot holds too. +pub const STD: &str = "-Zbuild-std=core,alloc,std,panic_abort"; + +/// A sysroot of std checked with its MIR for each target with a host, which flake.nix +/// builds once per toolchain beside rust2-ts's, if set: absolute. +pub fn sysroot() -> Result, String> { + let dir = match std::env::var(SYSROOT) { + Ok(dir) if !dir.is_empty() => dir, + Err(std::env::VarError::NotUnicode(_)) => return Err(format!("{SYSROOT} is not UTF-8")), + _ => return Ok(None), + }; + let dir = std::path::absolute(&dir) + .map_err(|err| format!("resolving {SYSROOT}: {err}"))? + .into_os_string() + .into_string() + .expect("a UTF-8 path resolves to a UTF-8 path"); + for host in HOSTS { + let lib = Path::new(&dir).join("lib/rustlib").join(host.target).join("lib"); + if !lib.is_dir() { + return Err(format!( + "{SYSROOT} is {dir}, which holds no std for {} in {}", + host.target, + lib.display() + )); + } + } + Ok(Some(dir)) +} + +/// The variable naming [`sysroot`]: rust2-ts's, whose sysroot holds these targets' std too. +const SYSROOT: &str = "RUST2_TS_SYSROOT"; + +/// Add to rustc's `args` what a crate for a target with a host compiles with: its MIR, and +/// std's from [`sysroot`]. Cargo's query of the compiler, whose answer it hashes into every +/// unit's fingerprint, names the flags and the sysroot, so a target directory keeps +/// nothing compiled without them. +pub fn flags(args: &[String], added: &mut Vec) -> Result<(), String> { + let sysroot = sysroot()?; + if args[1..] == ["-vV"] { + println!("rust2-ffi-mir: {} {}", MIR.join(" "), sysroot.as_deref().unwrap_or(STD)); + } + // The target's crates only: build scripts and proc macros run on the host, with its std. + if args.windows(2).any(|w| w[0] == "--target" && host(&w[1]).is_some()) { + added.extend(MIR.map(str::to_string)); + if let Some(sysroot) = sysroot { + added.extend(["--sysroot".to_string(), sysroot]); + } + } + Ok(()) +} + +/// What the CLI hands the wrapper for the check of the generated crate: its package, and +/// the crates of the exported workspace's own packages, which a refusal points into. +pub mod env { + /// The generated crate's package, whose library the wrapper walks. + pub const PACKAGE: &str = "RUST2_FFI_WALK"; + /// The library crate names of every package of the export's own source, not a + /// registry's or git's, comma-separated. + pub const OWN: &str = "RUST2_FFI_OWN"; +} + +/// The walk of the generated crate's exports, after rustc checks it. +pub struct Walk { + host: &'static Host, + own: FxHashSet, +} + +impl Walk { + /// The job for this rustc invocation, if it compiles the generated crate for a target + /// with a host. + pub fn for_args(args: &[String]) -> Result, String> { + let Ok(package) = std::env::var(env::PACKAGE) else { + return Ok(None); + }; + // Its library, which builds the module and a static library, not its build script. + let ours = std::env::var("CARGO_PKG_NAME").is_ok_and(|name| name == package); + let lib = (args.windows(2)).any(|w| w[0] == "--crate-type" && matches!(w[1].as_str(), "cdylib" | "staticlib")); + if !ours || !lib { + return Ok(None); + } + let target = args.windows(2).find(|w| w[0] == "--target").map(|w| w[1].as_str()); + let Some(host) = target.and_then(host) else { + return Err(format!("{package} is checked for {target:?}, whose host the export does not know")); + }; + let own = std::env::var(env::OWN).map_err(|_| format!("{} is unset", env::OWN))?; + Ok(Some(Self { + host, + own: own.split(',').filter(|s| !s.is_empty()).map(str::to_string).collect(), + })) + } +} + +impl Callbacks for Walk { + fn config(&mut self, config: &mut Config) { + // The collector then follows a call into any crate, and to a foreign function, + // rather than stopping where another crate's code would be linked. + config.override_queries = Some(|_, providers| { + providers.hooks.should_codegen_locally = |_, _| true; + }); + } + + fn after_analysis<'tcx>(&mut self, _compiler: &Compiler, tcx: TyCtxt<'tcx>) -> Compilation { + if tcx.dcx().has_errors().is_none() { + let own = |krate: CrateNum| krate != LOCAL_CRATE && self.own.contains(tcx.crate_name(krate).as_str()); + let graph = Graph::walk(tcx); + let lacks = Lacks::find(tcx, &graph, self.host); + report(tcx, &graph, &lacks, &own, self.host); + } + match tcx.dcx().has_errors() { + None => Compilation::Continue, + Some(_) => Compilation::Stop, + } + } +} + +/// A function or static the module holds. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +enum Node<'tcx> { + Fn(Instance<'tcx>), + Static(DefId), +} + +impl<'tcx> Node<'tcx> { + fn def_id(self) -> DefId { + match self { + Node::Fn(instance) => instance.def_id(), + Node::Static(def_id) => def_id, + } + } +} + +/// Where a walk ends: a function the module imports, or one it links from C. +enum Leaf { + Import { module: String, name: String }, + C(String), + /// A browser API wasm-bindgen would import, which on a WASI target it compiles to a + /// function that panics instead. + Bindgen(String), +} + +/// Every function and static the generated crate's exports reach, and what each uses. +struct Graph<'tcx> { + nodes: FxIndexSet>, + /// What each node uses, by index, and where. + edges: Vec>, + /// The export each node was first reached from. + origin: Vec, + /// The nodes the walk ends at. + leaves: FxHashMap, +} + +impl<'tcx> Graph<'tcx> { + fn walk(tcx: TyCtxt<'tcx>) -> Self { + let mut graph = Graph { + nodes: FxIndexSet::default(), + edges: Vec::new(), + origin: Vec::new(), + leaves: FxHashMap::default(), + }; + // The module's exports: each function the crate gives a symbol of its own. + for def_id in tcx.hir_crate_items(()).definitions() { + let def_id = def_id.to_def_id(); + if matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) + && !tcx.generics_of(def_id).requires_monomorphization(tcx) + && tcx.codegen_fn_attrs(def_id).contains_extern_indicator() + { + let at = graph.add(Node::Fn(Instance::mono(tcx, def_id)), None); + graph.origin[at] = at; + } + } + let symbols = Symbols::new(tcx); + // wasm-bindgen names each import's stand-in after the import: `__wbg_` for a browser + // API, `__wbindgen_` for its own. + let bindgen = tcx.crates(()).iter().any(|&krate| tcx.crate_name(krate).as_str() == "wasm_bindgen"); + let stub = |def_id: DefId| { + let name = tcx.item_name(def_id); + (bindgen && (name.as_str().starts_with("__wbg_") || name.as_str().starts_with("__wbindgen_"))) + .then(|| name.to_string()) + }; + let mut next = 0; + while next < graph.nodes.len() { + let at = next; + next += 1; + let node = graph.nodes[at]; + let def_id = node.def_id(); + if tcx.is_foreign_item(def_id) { + match symbols.resolve(tcx, node) { + Resolved::Rust(def_id) => { + let to = match tcx.def_kind(def_id) { + DefKind::Static { .. } => Node::Static(def_id), + _ => Node::Fn(Instance::mono(tcx, def_id)), + }; + graph.add(to, Some((at, DUMMY_SP))); + } + Resolved::Leaf(leaf) => { + graph.leaves.insert(at, leaf); + } + Resolved::Nothing => {} + } + continue; + } + match node { + Node::Fn(instance) => { + if let InstanceKind::Item(def_id) = instance.def + && matches!(tcx.def_kind(def_id), DefKind::Fn) + && let Some(name) = stub(def_id) + { + graph.leaves.insert(at, Leaf::Bindgen(name)); + continue; + } + if let InstanceKind::Item(def_id) = instance.def { + // A constructor's body only builds its value. + if matches!(tcx.def_kind(def_id), DefKind::Ctor(..)) { + continue; + } + if !tcx.is_mir_available(def_id) { + tcx.dcx().span_err( + tcx.def_span(def_id), + format!( + "rust2-ffi: `{}` of crate `{}` has no MIR to follow, which every crate a target with a host compiles with; this is a bug in rust2-ffi", + tcx.def_path_str(def_id), + tcx.crate_name(def_id.krate) + ), + ); + continue; + } + } + let Ok((used, _)) = tcx.items_of_instance((instance, CollectionMode::UsedItems)) else { + tcx.dcx().span_err( + tcx.def_span(def_id), + format!("rust2-ffi: `{instance}` does not normalize; this is a bug in rust2-ffi"), + ); + continue; + }; + for item in used { + let to = match item.node { + MonoItem::Fn(instance) => Node::Fn(instance), + MonoItem::Static(def_id) => Node::Static(def_id), + MonoItem::GlobalAsm(_) => continue, + }; + graph.add(to, Some((at, item.span))); + } + } + Node::Static(def_id) => { + // What its value points to: functions, vtables' methods, and other statics. + if let Ok(alloc) = tcx.eval_static_initializer(def_id) { + let mut seen = FxHashSet::default(); + for &prov in alloc.inner().provenance().ptrs().values() { + graph.alloc(tcx, prov.alloc_id(), at, &mut seen); + } + } + } + } + } + graph + } + + /// The node for `node`, added if new and used by `from` at a span if given. + fn add(&mut self, node: Node<'tcx>, from: Option<(usize, Span)>) -> usize { + let (at, new) = self.nodes.insert_full(node); + if new { + self.edges.push(Vec::new()); + self.origin.push(from.map_or(at, |(from, _)| self.origin[from])); + } + if let Some((from, span)) = from { + self.edges[from].push((at, span)); + } + at + } + + /// What the allocation `id` points to, used by `from`, as the collector scans a static. + fn alloc(&mut self, tcx: TyCtxt<'tcx>, id: AllocId, from: usize, seen: &mut FxHashSet) { + if !seen.insert(id) { + return; + } + match tcx.global_alloc(id) { + GlobalAlloc::Static(def_id) => { + self.add(Node::Static(def_id), Some((from, DUMMY_SP))); + } + GlobalAlloc::Memory(alloc) => { + for &prov in alloc.inner().provenance().ptrs().values() { + self.alloc(tcx, prov.alloc_id(), from, seen); + } + } + GlobalAlloc::Function { instance } => { + self.add(Node::Fn(instance), Some((from, DUMMY_SP))); + } + GlobalAlloc::VTable(ty, dyn_ty) => { + let principal = (dyn_ty.principal()).map(|principal| tcx.instantiate_bound_regions_with_erased(principal)); + self.alloc(tcx, tcx.vtable_allocation((ty, principal)), from, seen); + } + GlobalAlloc::TypeId { .. } => {} + } + } +} + +/// What a foreign item's symbol is. +enum Resolved { + /// A Rust function or static some crate defines under it. + Rust(DefId), + Leaf(Leaf), + /// One the link makes that uses nothing, as the allocator's marker static. + Nothing, +} + +/// The Rust items each crate defines under a symbol of their own, which a foreign item of +/// another crate may name. +struct Symbols { + defined: FxHashMap, +} + +impl Symbols { + fn new(tcx: TyCtxt<'_>) -> Self { + let mut defined = FxHashMap::default(); + for krate in std::iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) { + let symbols = match krate { + LOCAL_CRATE => &[][..], + _ => tcx.exported_non_generic_symbols(krate), + }; + for (symbol, _) in symbols { + let ExportedSymbol::NonGeneric(def_id) = *symbol else { + continue; + }; + if !tcx.def_kind(def_id).has_codegen_attrs() { + continue; + } + let attrs = tcx.codegen_fn_attrs(def_id); + if attrs.contains_extern_indicator() { + defined.insert(tcx.symbol_name(Instance::mono(tcx, def_id)).name.to_string(), def_id); + } + // An implementation of an externally implementable item, under the item's name. + for &(foreign, ..) in &attrs.foreign_item_symbol_aliases { + defined.insert(tcx.symbol_name(Instance::mono(tcx, foreign)).name.to_string(), def_id); + } + } + } + Self { defined } + } + + fn resolve<'tcx>(&self, tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Resolved { + let def_id = node.def_id(); + if let Some(module) = tcx.wasm_import_module_map(def_id.krate).get(&def_id) { + // Imported by its link name, under a symbol rustc mangles apart from other modules'. + let name = tcx.codegen_fn_attrs(def_id).symbol_name.unwrap_or_else(|| tcx.item_name(def_id)); + return Resolved::Leaf(Leaf::Import { + module: module.clone(), + name: name.to_string(), + }); + } + let name = tcx.symbol_name(Instance::mono(tcx, def_id)).name.to_string(); + if let Some(&def_id) = self.defined.get(&name) { + return Resolved::Rust(def_id); + } + // The allocator shim the link makes: each `__rust_*` function std's default calls, + // when no `#[global_allocator]` defines it, and the marker it reads. + use rustc_ast::expand::allocator::{ + ALLOC_ERROR_HANDLER, ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE, default_fn_name, global_fn_name, + }; + let mangle = |name: &str| rustc_symbol_mangling::mangle_internal_symbol(tcx, name); + if name == mangle(NO_ALLOC_SHIM_IS_UNSTABLE) { + return Resolved::Nothing; + } + let methods = ALLOCATOR_METHODS.iter().map(|m| m.name).chain([ALLOC_ERROR_HANDLER]); + for method in methods { + if name == mangle(&global_fn_name(method)) + && let Some(&def_id) = self.defined.get(&mangle(&default_fn_name(method))) + { + return Resolved::Rust(def_id); + } + } + Resolved::Leaf(Leaf::C(name)) + } +} + +/// What each leaf needs that the module cannot have. +struct Lacks { + /// Each leaf's, empty for one the host has all of. + by_leaf: FxHashMap>, +} + +/// What the module cannot have. +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] +enum Lack { + /// An import the host lacks, as `module.name`. + Import(String), + /// A C function or static no library the module links defines, which fails its link. + Undefined(String), + /// A browser API wasm-bindgen panics on rather than import, by its stand-in's name. + Bindgen(String), +} + +impl Lacks { + fn find(tcx: TyCtxt<'_>, graph: &Graph<'_>, host: &Host) -> Self { + let mut by_leaf = FxHashMap::default(); + let mut c = Vec::new(); + for (&at, leaf) in &graph.leaves { + match leaf { + Leaf::Import { module, name } => { + let lacks = match (host.implements)(module, name) { + true => Vec::new(), + false => vec![Lack::Import(format!("{module}.{name}"))], + }; + by_leaf.insert(at, lacks); + } + Leaf::C(name) => c.push((at, name.as_str())), + Leaf::Bindgen(name) => { + by_leaf.insert(at, vec![Lack::Bindgen(name.clone())]); + } + } + } + if !c.is_empty() { + match Linker::new(tcx, host) { + Ok(linker) => { + for (at, name) in c { + match linker.imports(name) { + Ok(Some(imports)) => { + let lacks = (imports.into_iter()) + .filter(|(module, name)| !(host.implements)(module, name)) + .map(|(module, name)| Lack::Import(format!("{module}.{name}"))) + .collect(); + by_leaf.insert(at, lacks); + } + Ok(None) => { + by_leaf.insert(at, vec![Lack::Undefined(name.to_string())]); + } + Err(err) => { + tcx.dcx().err(format!("rust2-ffi: {err}")); + } + } + } + } + Err(err) => { + tcx.dcx().err(format!("rust2-ffi: {err}")); + } + } + } + Self { by_leaf } + } +} + +/// A link of one C function alone, as the target's linker would make the module, to see +/// what it imports: rustc's own `rust-lld`, over the target's C library and each static +/// library a crate links. +struct Linker { + lld: PathBuf, + libs: Vec, + dir: PathBuf, +} + +impl Linker { + fn new(tcx: TyCtxt<'_>, host: &Host) -> Result { + // The toolchain's, whatever sysroot the MIR came from. + let sysroot = &tcx.sess.opts.sysroot.default; + let lld = rustc_session::filesearch::make_target_bin_path(sysroot, rustc_session::config::host_tuple()) + .join("rust-lld"); + let own = sysroot.join("lib/rustlib").join(host.target).join("lib/self-contained"); + let mut libs = vec![own.join("libc.a")]; + let search: Vec = (tcx.sess.target_filesearch()) + .search_paths(rustc_session::search_paths::PathKind::Native) + .map(|path| path.dir.to_path_buf()) + .chain([own]) + .collect(); + for krate in std::iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) { + // A library of C's: wasm32 links no other kind, and a `wasm_import_module` is none. + let libs_of = tcx.native_libraries(krate).iter(); + for lib in libs_of.filter(|lib| matches!(lib.kind, NativeLibKind::Static { .. } | NativeLibKind::Unspecified)) { + let name = lib.name; + let file = format!("lib{name}.a"); + let Some(found) = search.iter().map(|dir| dir.join(&file)).find(|path| path.is_file()) else { + return Err(format!( + "`{}` links the C library `{name}`, which is in no directory the check searches", + tcx.crate_name(krate) + )); + }; + if !libs.contains(&found) { + libs.push(found); + } + } + } + let dir = tcx.sess.io.temps_dir.clone().or_else(|| tcx.sess.io.output_dir.clone()); + let dir = dir.ok_or("the check has no output directory to link in")?; + Ok(Self { lld, libs, dir }) + } + + /// What the module imports for the C function or static `name`, each by module and + /// name, or none if no library defines it. + fn imports(&self, name: &str) -> Result>, String> { + let out = self.dir.join(format!("rust2-ffi-{}-{name}.wasm", std::process::id())); + // What the C library needs of the module's own code, which compiler-builtins and std + // define, it may leave undefined: the module's link finds it. + let output = Command::new(&self.lld) + .args(["-flavor", "wasm", "--no-entry", "--allow-undefined"]) + .arg(format!("--export-if-defined={name}")) + .arg("-o") + .arg(&out) + .args(&self.libs) + .output() + .map_err(|err| format!("running {}: {err}", self.lld.display()))?; + if !output.status.success() { + return Err(format!( + "linking `{name}` to see what it imports: {}", + String::from_utf8_lossy(&output.stderr) + )); + } + let wasm = std::fs::read(&out).map_err(|err| format!("reading {}: {err}", out.display())); + let _ = std::fs::remove_file(&out); + let (imports, exports) = linked(&wasm?).map_err(|err| format!("reading the link of `{name}`: {err}"))?; + if !exports.iter().any(|export| export == name) { + return Ok(None); + } + Ok(Some(imports.into_iter().filter(|(module, _)| module != "env").collect())) + } +} + +/// A wasm module's function imports, by module and name, and its exports: a function's, or +/// a static's global. +fn linked(wasm: &[u8]) -> wasmparser::Result<(Vec<(String, String)>, Vec)> { + let (mut imports, mut exports) = (Vec::new(), Vec::new()); + for payload in wasmparser::Parser::new(0).parse_all(wasm) { + match payload? { + wasmparser::Payload::ImportSection(section) => { + for import in section.into_imports() { + let import = import?; + if let wasmparser::TypeRef::Func(_) = import.ty { + imports.push((import.module.to_string(), import.name.to_string())); + } + } + } + wasmparser::Payload::ExportSection(section) => { + for export in section { + exports.push(export?.name.to_string()); + } + } + _ => {} + } + } + Ok((imports, exports)) +} + +/// Refuse each call in the crate's own code that reaches an import the host lacks without +/// passing through that code again, or else the export whose item does. What the generated +/// crate reaches through std alone is the binding's to implement, refused once as a bug. +fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn Fn(CrateNum) -> bool, host: &Host) { + let len = graph.nodes.len(); + let krate = |at: usize| match graph.nodes[at] { + // A type's drop is its crate's, as its `Drop` impl is. + Node::Fn(Instance { + def: InstanceKind::Shim(ty::ShimKind::DropGlue(_, Some(ty))), + .. + }) if let ty::Adt(adt, _) = ty.kind() => adt.did().krate, + node => node.def_id().krate, + }; + // A leaf is where the module leaves Rust, whoever declares it. + let mine = |at: usize| !graph.leaves.contains_key(&at) && own(krate(at)); + let glue = |at: usize| !graph.leaves.contains_key(&at) && krate(at) == LOCAL_CRATE; + let lacking = |at: usize| lacks.by_leaf.get(&at).is_some_and(|lacking| !lacking.is_empty()); + // What reaches a lacking leaf without passing through the crate's own code or the + // generated crate's, and the next node on the way, found backwards from the leaves. + let mut users = vec![Vec::new(); len]; + for (from, edges) in graph.edges.iter().enumerate() { + for &(to, _) in edges { + users[to].push(from); + } + } + let mut toward: Vec> = vec![None; len]; + let mut tainted: Vec = (0..len).map(lacking).collect(); + let mut queue: std::collections::VecDeque = (0..len).filter(|&at| tainted[at]).collect(); + while let Some(at) = queue.pop_front() { + for &user in &users[at] { + if !tainted[user] && !mine(user) && !glue(user) { + tainted[user] = true; + toward[user] = Some(at); + queue.push_back(user); + } + } + } + // Each call into that, from the crate's own code or from the generated crate's, by + // where it is refused, with the node it calls and the export it serves. + let mut refused: FxIndexMap<(Span, usize), usize> = FxIndexMap::default(); + let mut runtime: FxIndexSet = FxIndexSet::default(); + for (from, edges) in graph.edges.iter().enumerate() { + if !mine(from) && !glue(from) { + continue; + } + for &(to, span) in edges { + if !tainted[to] || mine(to) { + continue; + } + // A shim's own spans are the compiler's: a type's drop is refused at the type. + let shim = !matches!(graph.nodes[from], Node::Fn(Instance { def: InstanceKind::Item(_), .. })); + let span = match glue(from) { + false if span.is_dummy() || shim => item_span(tcx, graph.nodes[from]), + false => span, + // What the generated crate reaches through std alone is its runtime's. + true if tcx.lookup_stability(krate(to).as_def_id()).is_some() => { + runtime.insert(to); + continue; + } + // Its call names the export's item, whose own span says where. + true => item_span(tcx, graph.nodes[to]), + }; + refused.entry((span, to)).or_insert(graph.origin[from]); + } + } + let path = |to: usize| { + let mut path = vec![to]; + while let Some(next) = toward[*path.last().expect("a path has a node")] { + path.push(next); + } + path + }; + // Everything `to` reaches the same way that the module cannot have, said. + let reached = |to: usize| { + let mut seen = FxHashSet::default(); + let mut stack = vec![to]; + let mut reached = Vec::new(); + while let Some(at) = stack.pop() { + if !seen.insert(at) { + continue; + } + reached.extend(lacks.by_leaf.get(&at).into_iter().flatten().cloned()); + stack.extend((graph.edges[at].iter()).map(|&(next, _)| next).filter(|&next| tainted[next])); + } + reached.sort(); + reached.dedup(); + let (mut imports, mut undefined, mut bindgen) = (Vec::new(), Vec::new(), Vec::new()); + for lack in &reached { + match lack { + Lack::Import(name) => imports.push(name.as_str()), + Lack::Undefined(name) => undefined.push(name.as_str()), + Lack::Bindgen(name) => bindgen.push(name.as_str()), + } + } + let mut said = Vec::new(); + if !imports.is_empty() { + said.push(format!("`{}`, which {}'s host lacks", imports.join("`, `"), host.name)); + } + if !undefined.is_empty() { + said.push(format!( + "`{}`, which no library {}'s module links defines", + undefined.join("`, `"), + host.target + )); + } + if !bindgen.is_empty() { + said.push(format!( + "`{}`, which wasm-bindgen panics on in {} rather than import from JavaScript", + bindgen.join("`, `"), + host.target + )); + } + said.join(", and ") + }; + let through = |path: &[usize]| { + let named: Vec = path.iter().filter_map(|&at| describe(tcx, graph, at)).collect(); + format!("through {}", named.join(", then ")) + }; + let mut bugs = FxHashSet::default(); + for to in runtime { + let path = path(to); + if bugs.insert(*path.last().expect("a path has a node")) { + tcx.dcx() + .struct_err(format!( + "rust2-ffi: the generated crate reaches {} through std; this is a bug in rust2-ffi", + reached(to), + )) + .with_note(through(&path)) + .emit(); + } + } + for ((span, to), export) in refused { + let path = path(to); + let what = match graph.leaves.get(&to) { + Some(Leaf::Import { .. }) => "the module imports".to_string(), + Some(Leaf::C(_) | Leaf::Bindgen(_)) => "this calls".to_string(), + None => format!( + "{} reaches", + describe(tcx, graph, to).unwrap_or_else(|| "this".to_string()) + ), + }; + let mut err = tcx.dcx().struct_span_err( + span, + format!( + "rust2-ffi: {what} {}; leave it out of {}'s build with a `cfg`, or exclude its export in the config", + reached(to), + host.target + ), + ); + if path.len() > 1 { + err = err.with_note(through(&path)); + } + err.with_note(format!( + "the module exports it through `{}`", + tcx.item_name(graph.nodes[export].def_id()) + )) + .emit(); + } +} + +/// Where a refusal from the generated crate points: the item it calls, or the type it +/// drops. +fn item_span<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Span { + if let Node::Fn(instance) = node + && let InstanceKind::Shim(ty::ShimKind::DropGlue(_, Some(ty))) = instance.def + && let ty::Adt(adt, _) = ty.kind() + { + return tcx.def_span(adt.did()); + } + tcx.def_span(node.def_id()) +} + +/// A node as a refusal names it, or none for a shim the compiler makes between two calls. +fn describe<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, at: usize) -> Option { + match graph.leaves.get(&at) { + Some(Leaf::Import { module, name }) => return Some(format!("the import `{module}.{name}`")), + Some(Leaf::C(name)) => return Some(format!("`{name}`, from C")), + Some(Leaf::Bindgen(name)) => return Some(format!("wasm-bindgen's `{name}`")), + None => {} + } + match graph.nodes[at] { + Node::Fn(instance) => match instance.def { + InstanceKind::Item(def_id) => Some(format!("`{}`", tcx.def_path_str(def_id))), + InstanceKind::Shim(ty::ShimKind::DropGlue(_, Some(ty))) => Some(format!("the drop of `{ty}`")), + _ => None, + }, + Node::Static(def_id) => Some(format!("the static `{}`", tcx.def_path_str(def_id))), + } +} diff --git a/ffi/src/web.rs b/ffi/src/web.rs index 09e6b8b3e..8568d2e8b 100644 --- a/ffi/src/web.rs +++ b/ffi/src/web.rs @@ -40,6 +40,23 @@ pub const GLUE_RUNTIME: &str = include_str!("../runtime/web.rs"); /// the binding implements with web APIs. pub const TARGET: &str = "wasm32-wasip1"; +/// The WASI functions the runtime implements with web APIs: each method of its `wasi` +/// object, so the export refuses what `load` would by the same list. +pub fn wasi() -> &'static [&'static str] { + static WASI: std::sync::OnceLock> = std::sync::OnceLock::new(); + WASI.get_or_init(|| { + let start = RUNTIME.find("\nconst wasi = {\n").expect("rt.ts declares its WASI functions"); + let body = &RUNTIME[start..]; + let body = &body[..body.find("\n};\n").expect("rt.ts closes its WASI functions")]; + // A method at the object's own level: one tab, its name, then its parameters. + (body.lines()) + .filter_map(|line| line.strip_prefix('\t')) + .filter_map(|line| line.split_once('(').map(|(name, _)| name)) + .filter(|name| !name.is_empty() && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')) + .collect() + }) +} + /// What the generator writes: each TypeScript file by its name under `web/`, and the Rust /// glue the crate's `web` feature builds. pub struct Output { diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 325e0e4d4..ff1f0d347 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -2440,6 +2440,68 @@ fn reject() { assert_eq!(errors.join("\n") + "\n", expected); } +/// What the web target's host lacks, refused at the call in the crate's own code nearest +/// it, by every route the walk follows. Without the call of an undefined C function, whose +/// module would not link, the module builds anyway: what it imports that the binding does +/// not implement is exactly what the refusals name. +#[test] +fn lacks() { + let dir = scratch().join("lacks"); + let output = export("lacks", &dir.join("undefined"), &[WEB], &["--features", "undefined"]); + assert!(!output.status.success(), "exporting lacks succeeded"); + let prefix = format!("{}/", manifest_dir().display()); + let refused: Vec = errors(&output).iter().map(|e| e.replace(&prefix, "")).collect(); + let expected = manifest_dir().join("fixtures/lacks/expected.txt"); + if std::env::var_os("UPDATE_EXPECT").is_some() { + std::fs::write(&expected, refused.join("\n") + "\n").unwrap(); + } else { + assert_eq!(refused.join("\n") + "\n", std::fs::read_to_string(&expected).unwrap()); + } + + let out = dir.join("out"); + let output = export("lacks", &out, &[WEB], &[]); + assert!(!output.status.success(), "exporting lacks succeeded"); + // Each `module.name` a refusal names. + let named: std::collections::BTreeSet = (errors(&output).iter()) + .flat_map(|error| error.split('`').skip(1).step_by(2).map(str::to_string).collect::>()) + .filter(|token| token.split_once('.').is_some_and(|(m, n)| !m.is_empty() && !n.contains(['.', ' ', ':']))) + .collect(); + let target = dir.join("glue"); + run(cargo() + .args(["build", "--quiet", "--locked", "--manifest-path"]) + .arg(out.join("Cargo.toml")) + .arg("--target-dir") + .arg(&target) + .args(["--target", WEB, "--features", "web"])); + let wasm = std::fs::read(target.join(WEB).join("debug").join("lacks.wasm")).unwrap(); + // The WASI functions the runtime implements, each a method of its `wasi` object. + let runtime = std::fs::read_to_string(manifest_dir().join("runtime/rt.ts")).unwrap(); + let wasi = &runtime[runtime.find("\nconst wasi = {\n").unwrap()..]; + let wasi = &wasi[..wasi.find("\n};\n").unwrap()]; + let implemented: Vec<&str> = (wasi.lines()) + .filter_map(|line| line.strip_prefix('\t')?.split_once('(').map(|(name, _)| name)) + .filter(|name| name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')) + .collect(); + let mut lacking = std::collections::BTreeSet::new(); + for payload in wasmparser::Parser::new(0).parse_all(&wasm) { + if let wasmparser::Payload::ImportSection(section) = payload.unwrap() { + for import in section.into_imports() { + let import = import.unwrap(); + let has = match import.module { + "rust2" => true, + "wasi_snapshot_preview1" => implemented.contains(&import.name), + _ => false, + }; + if !has { + lacking.insert(format!("{}.{}", import.module, import.name)); + } + } + } + } + assert!(!lacking.is_empty(), "the module imports nothing the host lacks"); + assert_eq!(lacking, named, "the module's imports the host lacks, and what the export refused"); +} + /// Each of rust2-ffi's errors the export printed, sorted: at its span, then each of its /// notes at its own. fn errors(output: &Output) -> Vec { diff --git a/flake.nix b/flake.nix index 5f12f3c6e..585aa7120 100644 --- a/flake.nix +++ b/flake.nix @@ -98,7 +98,8 @@ quest-cli = quest.packages.${system}.default; # std checked once per toolchain for the TypeScript driver's target, with the # profile translation reads its MIR at, as the sysroot rust2-ts takes instead of - # checking std with -Zbuild-std in every target directory. Offline, from the + # checking std with -Zbuild-std in every target directory, and for the web + # binding's, whose MIR rust2-ffi walks for what the web lacks. Offline, from the # crates rust-src vendors. rust2-ts-std = pkgs.stdenv.mkDerivation { name = "rust2-ts-std"; @@ -110,15 +111,17 @@ printf '[package]\nname = "sysroot"\nversion = "0.0.0"\nedition = "2024"\n' > Cargo.toml touch src/lib.rs # The crates ts/src/cli.rs's STD names. - cargo check --offline --target wasm32-unknown-unknown -Zbuild-std=core,alloc,std,panic_abort \ + cargo check --offline --target wasm32-unknown-unknown --target wasm32-wasip1 -Zbuild-std=core,alloc,std,panic_abort \ --config "source.crates-io.replace-with = 'vendored'" \ --config "source.vendored.directory = '${tsToolchain}/lib/rustlib/src/rust/library/vendor'" \ --config ${./ts/src/profile.toml} ''; installPhase = '' - lib=$out/lib/rustlib/wasm32-unknown-unknown/lib - mkdir -p $lib - find target/wasm32-unknown-unknown -name 'lib*.rmeta' ! -name 'libsysroot-*' -exec cp -t $lib {} + + for target in wasm32-unknown-unknown wasm32-wasip1; do + lib=$out/lib/rustlib/$target/lib + mkdir -p $lib + find target/$target -name 'lib*.rmeta' ! -name 'libsysroot-*' -exec cp -t $lib {} + + done ''; }; # The driver links its nightly's rustc libraries, and runs cargo from it too. diff --git a/ts/src/cli.rs b/ts/src/cli.rs index b98f634db..6492d7875 100644 --- a/ts/src/cli.rs +++ b/ts/src/cli.rs @@ -69,6 +69,7 @@ fn try_run(args: &[String]) -> Result { target: Some(TARGET), args: &cargo, env: &env, + target_dir: None, } .run() } From 0dc269dc47b9a6409f5345f38f53fcd7b2ac4347 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 03:52:39 -0800 Subject: [PATCH 03/13] style(ffi): fmt, sort, and clippy for the export refusal Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/lacks/Cargo.toml | 10 +++--- ffi/src/wasm.rs | 61 ++++++++++++++++++++++++++--------- ffi/src/web.rs | 4 ++- ffi/tests/fixtures.rs | 20 ++++++++++-- 4 files changed, 70 insertions(+), 25 deletions(-) diff --git a/ffi/fixtures/lacks/Cargo.toml b/ffi/fixtures/lacks/Cargo.toml index feccf6c3e..46f20c890 100644 --- a/ffi/fixtures/lacks/Cargo.toml +++ b/ffi/fixtures/lacks/Cargo.toml @@ -10,13 +10,13 @@ publish = false [lib] name = "lacks" +[features] +# A call of a C function no library defines, which fails the module's link, so a build +# without it can compare the module's imports with what the export refused. +undefined = [] + [dependencies] web-sys = { version = "0.3.107", features = ["console"] } [lints] workspace = true - -[features] -# A call of a C function no library defines, which fails the module's link, so a build -# without it can compare the module's imports with what the export refused. -undefined = [] diff --git a/ffi/src/wasm.rs b/ffi/src/wasm.rs index b73ee3970..1043bf80d 100644 --- a/ffi/src/wasm.rs +++ b/ffi/src/wasm.rs @@ -15,7 +15,6 @@ use std::process::Command; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet}; use rustc_driver::{Callbacks, Compilation}; -use rustc_structures::NativeLibKind; use rustc_hir::def::DefKind; use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; use rustc_interface::interface::{Compiler, Config}; @@ -24,6 +23,7 @@ use rustc_middle::mir::interpret::{AllocId, GlobalAlloc}; use rustc_middle::mono::{CollectionMode, MonoItem}; use rustc_middle::ty::{self, Instance, InstanceKind, TyCtxt}; use rustc_span::{DUMMY_SP, Span}; +use rustc_structures::NativeLibKind; /// A target whose binding implements its module's imports, so the export refuses what its /// host lacks. @@ -140,7 +140,9 @@ impl Walk { } let target = args.windows(2).find(|w| w[0] == "--target").map(|w| w[1].as_str()); let Some(host) = target.and_then(host) else { - return Err(format!("{package} is checked for {target:?}, whose host the export does not know")); + return Err(format!( + "{package} is checked for {target:?}, whose host the export does not know" + )); }; let own = std::env::var(env::OWN).map_err(|_| format!("{} is unset", env::OWN))?; Ok(Some(Self { @@ -191,7 +193,10 @@ impl<'tcx> Node<'tcx> { /// Where a walk ends: a function the module imports, or one it links from C. enum Leaf { - Import { module: String, name: String }, + Import { + module: String, + name: String, + }, C(String), /// A browser API wasm-bindgen would import, which on a WASI target it compiles to a /// function that panics instead. @@ -231,7 +236,10 @@ impl<'tcx> Graph<'tcx> { let symbols = Symbols::new(tcx); // wasm-bindgen names each import's stand-in after the import: `__wbg_` for a browser // API, `__wbindgen_` for its own. - let bindgen = tcx.crates(()).iter().any(|&krate| tcx.crate_name(krate).as_str() == "wasm_bindgen"); + let bindgen = tcx + .crates(()) + .iter() + .any(|&krate| tcx.crate_name(krate).as_str() == "wasm_bindgen"); let stub = |def_id: DefId| { let name = tcx.item_name(def_id); (bindgen && (name.as_str().starts_with("__wbg_") || name.as_str().starts_with("__wbindgen_"))) @@ -346,7 +354,8 @@ impl<'tcx> Graph<'tcx> { self.add(Node::Fn(instance), Some((from, DUMMY_SP))); } GlobalAlloc::VTable(ty, dyn_ty) => { - let principal = (dyn_ty.principal()).map(|principal| tcx.instantiate_bound_regions_with_erased(principal)); + let principal = + (dyn_ty.principal()).map(|principal| tcx.instantiate_bound_regions_with_erased(principal)); self.alloc(tcx, tcx.vtable_allocation((ty, principal)), from, seen); } GlobalAlloc::TypeId { .. } => {} @@ -401,7 +410,10 @@ impl Symbols { let def_id = node.def_id(); if let Some(module) = tcx.wasm_import_module_map(def_id.krate).get(&def_id) { // Imported by its link name, under a symbol rustc mangles apart from other modules'. - let name = tcx.codegen_fn_attrs(def_id).symbol_name.unwrap_or_else(|| tcx.item_name(def_id)); + let name = tcx + .codegen_fn_attrs(def_id) + .symbol_name + .unwrap_or_else(|| tcx.item_name(def_id)); return Resolved::Leaf(Leaf::Import { module: module.clone(), name: name.to_string(), @@ -523,7 +535,9 @@ impl Linker { for krate in std::iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) { // A library of C's: wasm32 links no other kind, and a `wasm_import_module` is none. let libs_of = tcx.native_libraries(krate).iter(); - for lib in libs_of.filter(|lib| matches!(lib.kind, NativeLibKind::Static { .. } | NativeLibKind::Unspecified)) { + for lib in + libs_of.filter(|lib| matches!(lib.kind, NativeLibKind::Static { .. } | NativeLibKind::Unspecified)) + { let name = lib.name; let file = format!("lib{name}.a"); let Some(found) = search.iter().map(|dir| dir.join(&file)).find(|path| path.is_file()) else { @@ -544,7 +558,7 @@ impl Linker { /// What the module imports for the C function or static `name`, each by module and /// name, or none if no library defines it. - fn imports(&self, name: &str) -> Result>, String> { + fn imports(&self, name: &str) -> Result>, String> { let out = self.dir.join(format!("rust2-ffi-{}-{name}.wasm", std::process::id())); // What the C library needs of the module's own code, which compiler-builtins and std // define, it may leave undefined: the module's link finds it. @@ -568,13 +582,18 @@ impl Linker { if !exports.iter().any(|export| export == name) { return Ok(None); } - Ok(Some(imports.into_iter().filter(|(module, _)| module != "env").collect())) + Ok(Some( + imports.into_iter().filter(|(module, _)| module != "env").collect(), + )) } } +/// A function a module imports, by module and name. +type Import = (String, String); + /// A wasm module's function imports, by module and name, and its exports: a function's, or /// a static's global. -fn linked(wasm: &[u8]) -> wasmparser::Result<(Vec<(String, String)>, Vec)> { +fn linked(wasm: &[u8]) -> wasmparser::Result<(Vec, Vec)> { let (mut imports, mut exports) = (Vec::new(), Vec::new()); for payload in wasmparser::Parser::new(0).parse_all(wasm) { match payload? { @@ -647,7 +666,13 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn continue; } // A shim's own spans are the compiler's: a type's drop is refused at the type. - let shim = !matches!(graph.nodes[from], Node::Fn(Instance { def: InstanceKind::Item(_), .. })); + let shim = !matches!( + graph.nodes[from], + Node::Fn(Instance { + def: InstanceKind::Item(_), + .. + }) + ); let span = match glue(from) { false if span.is_dummy() || shim => item_span(tcx, graph.nodes[from]), false => span, @@ -679,7 +704,11 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn continue; } reached.extend(lacks.by_leaf.get(&at).into_iter().flatten().cloned()); - stack.extend((graph.edges[at].iter()).map(|&(next, _)| next).filter(|&next| tainted[next])); + stack.extend( + (graph.edges[at].iter()) + .map(|&(next, _)| next) + .filter(|&next| tainted[next]), + ); } reached.sort(); reached.dedup(); @@ -750,10 +779,10 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn err = err.with_note(through(&path)); } err.with_note(format!( - "the module exports it through `{}`", - tcx.item_name(graph.nodes[export].def_id()) - )) - .emit(); + "the module exports it through `{}`", + tcx.item_name(graph.nodes[export].def_id()) + )) + .emit(); } } diff --git a/ffi/src/web.rs b/ffi/src/web.rs index 8568d2e8b..877bd4991 100644 --- a/ffi/src/web.rs +++ b/ffi/src/web.rs @@ -45,7 +45,9 @@ pub const TARGET: &str = "wasm32-wasip1"; pub fn wasi() -> &'static [&'static str] { static WASI: std::sync::OnceLock> = std::sync::OnceLock::new(); WASI.get_or_init(|| { - let start = RUNTIME.find("\nconst wasi = {\n").expect("rt.ts declares its WASI functions"); + let start = RUNTIME + .find("\nconst wasi = {\n") + .expect("rt.ts declares its WASI functions"); let body = &RUNTIME[start..]; let body = &body[..body.find("\n};\n").expect("rt.ts closes its WASI functions")]; // A method at the object's own level: one tab, its name, then its parameters. diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index ff1f0d347..ae636788a 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -2463,8 +2463,19 @@ fn lacks() { assert!(!output.status.success(), "exporting lacks succeeded"); // Each `module.name` a refusal names. let named: std::collections::BTreeSet = (errors(&output).iter()) - .flat_map(|error| error.split('`').skip(1).step_by(2).map(str::to_string).collect::>()) - .filter(|token| token.split_once('.').is_some_and(|(m, n)| !m.is_empty() && !n.contains(['.', ' ', ':']))) + .flat_map(|error| { + error + .split('`') + .skip(1) + .step_by(2) + .map(str::to_string) + .collect::>() + }) + .filter(|token| { + token + .split_once('.') + .is_some_and(|(m, n)| !m.is_empty() && !n.contains(['.', ' ', ':'])) + }) .collect(); let target = dir.join("glue"); run(cargo() @@ -2499,7 +2510,10 @@ fn lacks() { } } assert!(!lacking.is_empty(), "the module imports nothing the host lacks"); - assert_eq!(lacking, named, "the module's imports the host lacks, and what the export refused"); + assert_eq!( + lacking, named, + "the module's imports the host lacks, and what the export refused" + ); } /// Each of rust2-ffi's errors the export printed, sorted: at its span, then each of its From 22028d89fe75cc468880bb3ed39211a85bb43024 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 04:00:13 -0800 Subject: [PATCH 04/13] docs(ffi): document the export refusal and finish its quest The web section describes the refusal, what it cannot refuse, and its cost, measured on moq's stand-in. quest/m3/wasi.md says what a WASI target adds to it. Co-Authored-By: Claude Opus 5.5 --- bench/ffi/README.md | 20 ++++++++++++++++++++ ffi/README.md | 20 ++++++++++++++++---- quest/m2/wasm/README.md | 7 ++++--- quest/m2/wasm/refuse.md | 41 ----------------------------------------- quest/m3/wasi.md | 9 ++++++--- 5 files changed, 46 insertions(+), 51 deletions(-) delete mode 100644 quest/m2/wasm/refuse.md diff --git a/bench/ffi/README.md b/bench/ffi/README.md index cb8563413..9f61a8426 100644 --- a/bench/ffi/README.md +++ b/bench/ffi/README.md @@ -651,6 +651,26 @@ profile), Dart 3.13.0. C and Dart are from the same run, for scale; wall ns per An object return pays the handle's `FinalizationRegistry` entry, as Go's and Dart's pay their collectors'. Firefox runs every row at about 2 to 3 times Node's. +### The export's build cost + +The web export [refuses what its host lacks](../../ffi/README.md#typescript-web) by +walking MIR, so it builds every crate with `-Zalways-encode-mir`, takes std's from the +dev shell's MIR sysroot, and checks the glue crate a second time to walk it. Measured +2026-11-17 on the same host, the stand-in's export from an emptied target through +[`measure.py`](measure.py), at rust2 `3b2e79f0` (signatures alone) and `0dc269dc` (with +the refusal), 3 rounds each, alternating, at a load average of 24 to 31; medians: + +| export | wall s | CPU s | peak RSS | +|---|---|---|---| +| signatures alone | 27.1 | 76.4 | 856 MiB | +| with the refusal | 45.7 | 87.8 | 1,773 MiB | + +The refusal adds about 11 CPU seconds, and its walk of the stand-in's 299,455 reachable +instances takes 6.5 s of them. Wall time ranged 24 to 60 s and 39 to 69 s between rounds +at this load, so its gap is rough; the walk's check runs after moq's crates, with nothing +beside it. Peak memory doubles, steady across rounds. A native export reads no MIR and +pays none of it. + ### Lending returned bytes Bytes a call returns are an [`rt.Bytes`](../../ffi/README.md#typescript-web): up to 64 B a copy, and diff --git a/ffi/README.md b/ffi/README.md index 65c689f75..bd75d600e 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -1319,10 +1319,22 @@ target there. API for it, so nothing of WASI ships. Its clocks are `performance.now`, from `performance.timeOrigin` for the real-time one; its randomness `crypto.getRandomValues`; its stdout and stderr `console.log` and `console.error`, a line at a time; its - environment and arguments empty; and its exit an abort. `load` refuses a module importing - anything else. Measured on the api fixture, stripped, with LTO at `opt-level = "s"`, that - std costs 24 KB over `wasm32-unknown-unknown`'s (3%), 9.5 KB gzipped, and the glue 2 KB - more; `wasm32-unknown-unknown` has no clock, so `Instant::now` panics there. + environment and arguments empty; and its exit an abort. What else the module would + import, such as the file system, sockets, or another module's function, is refused at + export, at the span of each call in the crate's own code that reaches it, naming the + imports it reaches and the path down to them. The export walks MIR from each exported + function through every call, `dyn` implementation, function pointer, static, and drop it + can reach, std's included, so a new std needs no change here: every crate builds with + `-Zalways-encode-mir`, std's from the dev shell's MIR sysroot or else `-Zbuild-std`, + which doubles the export's peak memory, as + [measured](../bench/ffi/README.md#the-exports-build-cost); a native export reads no MIR. A C symbol no linked library defines, which would fail the module's link, is + refused the same way, as is a call into wasm-bindgen, whose imports on `wasm32-wasip1` + compile to stubs that panic. `thread::spawn` and a socket's `connect` and `bind` import + nothing there and return an error, as Rust's own build for the target does, so they are + not refused. `load` still refuses a module importing anything else. Measured on the api fixture, stripped, with LTO at + `opt-level = "s"`, that std costs 24 KB over `wasm32-unknown-unknown`'s (3%), 9.5 KB + gzipped, and the glue 2 KB more; `wasm32-unknown-unknown` has no clock, so + `Instant::now` panics there. - **The API** is rust2-ts's shape: each type is a module of its own, `web/.ts`, which exports the type by its name and its functions, each taking `self` first, so a caller writes `import * as Counter from "./web/Counter.ts"` and `Counter.get(c)`, and a bundler diff --git a/quest/m2/wasm/README.md b/quest/m2/wasm/README.md index a2082368f..34e383dc0 100644 --- a/quest/m2/wasm/README.md +++ b/quest/m2/wasm/README.md @@ -65,9 +65,10 @@ Decided with the maintainer on 2026-11-12, in the quest audit: crate with no source change. Rejected: the binding's classes for both, as the other languages' bindings have; a surface per route. - Open, with no owner yet: the gate's own deliverable, moq-net running in a browser - through rust2-wasm, needs a transport adapter over WebTransport, and - [the refusal](/quest/m2/wasm/refuse.md) refuses a crate that imports browser APIs - through wasm-bindgen. moq's tree plans none of it. Plan it with moq's tree before the gate's + through rust2-wasm, needs a transport adapter over WebTransport, and the export + [refuses](/ffi/README.md#typescript-web) a crate that calls browser APIs through + wasm-bindgen, whose imports on `wasm32-wasip1` compile to stubs that panic rather than + to `__wbindgen_placeholder__` imports. moq's tree plans none of it. Plan it with moq's tree before the gate's last child lands. Decided with the maintainer on 2026-11-16, on diff --git a/quest/m2/wasm/refuse.md b/quest/m2/wasm/refuse.md deleted file mode 100644 index 15c92b9be..000000000 --- a/quest/m2/wasm/refuse.md +++ /dev/null @@ -1,41 +0,0 @@ -# [L] Refuse at export what a wasm32 target lacks - -## Goal - -rust2-ffi refuses at export, at the span of the call that reaches it, what a wasm32 target's -host has no API for. On [the web target](/ffi/README.md#typescript-web), that is the file -system, sockets, and a process: today the module imports the WASI function std calls for -each, and `load` refuses the module, naming the import, which is loud but at load, not at -export. A crate importing browser APIs through wasm-bindgen (web-sys, js-sys) is refused -at the import's span too, until a consumer needs both: today its module imports -`__wbindgen_placeholder__`, which `load` refuses. [The WASI target](/quest/m3/wasi.md) -refuses what its host lacks the same way, such as threads and sockets on `wasm32-wasip1`. - -## Plan - -Split out of the web target's quest in -[#851](https://github.com/kixel-ai/rust2/pull/851), as its Plan recommended once the -exporter's MIR reading was sized: it is most of the work, and WASI needs the same. - -- It needs every reachable body's MIR, which the exporter does not read: it runs `cargo - check` of the crate under its rustc driver (`driver/`, shared with rust2-ts) and reads - signatures alone. rust2-ts reads MIR through its profile, `-Zalways-encode-mir` on every - crate ([`ts/src/profile.toml`](/ts/src/profile.toml), set by - [`ts/src/cli.rs`](/ts/src/cli.rs)), and std's from its own MIR sysroot (the flake's - `RUST2_TS_SYSROOT`, else `-Zbuild-std`). Recommended: the exporter reuses both for its - wasm32 targets alone, so the native export costs nothing more. -- Recommended: derive what is refused from the module's imports, not a list of std paths. - Walk the calls reachable from each export's instance, through `dyn` and function pointers - to every implementation the crate can reach, to a foreign item imported from - `wasi_snapshot_preview1` that the target's binding does not implement, or from - `__wbindgen_placeholder__`, and refuse at the first call in the crate's own code, naming - the path down to the import. A new std then needs no change here. -- Report the export's time on moq's stand-in with and without the MIR, as the benchmark's - build cost rows do, since every wasm32 export pays it. - -Public API: none; an export that reaches what its target lacks fails where its module's -`load` failed. - -## Related - -- [The WASI target](/quest/m3/wasi.md) - refuses what a WASI host lacks the same way diff --git a/quest/m3/wasi.md b/quest/m3/wasi.md index 11529e6aa..29ff3da08 100644 --- a/quest/m3/wasi.md +++ b/quest/m3/wasi.md @@ -32,8 +32,12 @@ hosts that are WASI runtimes. Planned in m3 before a consumer asks, as checked at run time, a lent closure, a future that loses nothing on cancel, and bytes lent without a copy. - What a WASI host lacks is refused at export, at the span of the call that reaches it, - as on every target: threads on `wasm32-wasip1`, and sockets there too. - [The refusal](/quest/m2/wasm/refuse.md) builds it for both wasm32 targets. + as [the web target](/ffi/README.md#typescript-web) refuses its own: this target adds its + host's entry to `ffi/src/wasm.rs`'s `HOSTS`, the WASI imports that host implements, and + the walk and its MIR build need nothing more. What imports nothing, it cannot refuse: + on `wasm32-wasip1`, `thread::spawn` and a socket's `connect` and `bind` return an error + without calling the host, as Rust's own build for the target does, while a socket a host + passes in imports `sock_accept` and `sock_shutdown`, refused where the host lacks them. Public API: the name of the WASI target and what it writes, if it lands. @@ -43,5 +47,4 @@ Public API: the name of the WASI target and what it writes, if it lands. ## Related -- [Refusing what a wasm32 target lacks](/quest/m2/wasm/refuse.md) - the export-time refusal this target shares with the web's - [moq in pure Go](/quest/m4/go/README.md) - parked until a consumer needs pure Go, and to weigh a WASI runtime first when one does From cc4a7dc36871ab39bd97d7f3a9d943f5c7f47e63 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 04:43:55 -0800 Subject: [PATCH 05/13] fix(ffi): walk what checked crates define, load-time statics, and C's callbacks A crate cargo check compiles records no exported symbols, so the walk read every crate's definitions instead: a foreign item naming another checked crate's #[no_mangle] item now resolves to it. The walk also roots each .init_array static, follows what C calls back into Rust, refuses what C needs that nothing defines, and refuses a rust2 import the generated crate does not declare. The glue's check runs from the facade's directory and with its profile. Co-Authored-By: Claude Opus 5.5 --- driver/src/cargo.rs | 5 +- ffi/README.md | 26 ++-- ffi/fixtures/lacks/build.rs | 33 ++++ ffi/fixtures/lacks/expected.txt | 12 +- ffi/fixtures/lacks/src/lib.rs | 53 +++++++ ffi/fixtures/lacks/src/needs.ll | 17 +++ ffi/src/cli.rs | 34 +++++ ffi/src/main.rs | 1 + ffi/src/wasm.rs | 260 ++++++++++++++++++++++---------- ffi/tests/fixtures.rs | 6 +- ts/src/cli.rs | 1 + 11 files changed, 350 insertions(+), 98 deletions(-) create mode 100644 ffi/fixtures/lacks/build.rs create mode 100644 ffi/fixtures/lacks/src/needs.ll diff --git a/driver/src/cargo.rs b/driver/src/cargo.rs index c79ca1557..940aa4097 100644 --- a/driver/src/cargo.rs +++ b/driver/src/cargo.rs @@ -138,6 +138,9 @@ pub struct Check<'a> { /// Where cargo builds, absolute, if not [`target_dir`]'s for the manifest: another /// package's, whose crates this check then shares. pub target_dir: Option<&'a Path>, + /// Where cargo runs, which it reads its config and a `--config` path from, absolute, if + /// not the package's directory: another package's, whose config this check then shares. + pub dir: Option<&'a Path>, } impl Check<'_> { @@ -149,7 +152,7 @@ impl Check<'_> { Some(dir) => dir.to_path_buf(), None => target_dir(self.tool, Some(self.manifest))?, }; - let package_dir = self.manifest.parent().expect("a manifest path has a parent"); + let package_dir = (self.dir).unwrap_or_else(|| self.manifest.parent().expect("a manifest path has a parent")); let cargo = |subcommand: &str| { let mut cmd = cargo(); // Cargo reads its config from where it runs, so run where the package is. diff --git a/ffi/README.md b/ffi/README.md index bd75d600e..5f1d225e8 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -1323,18 +1323,20 @@ target there. import, such as the file system, sockets, or another module's function, is refused at export, at the span of each call in the crate's own code that reaches it, naming the imports it reaches and the path down to them. The export walks MIR from each exported - function through every call, `dyn` implementation, function pointer, static, and drop it - can reach, std's included, so a new std needs no change here: every crate builds with - `-Zalways-encode-mir`, std's from the dev shell's MIR sysroot or else `-Zbuild-std`, - which doubles the export's peak memory, as - [measured](../bench/ffi/README.md#the-exports-build-cost); a native export reads no MIR. A C symbol no linked library defines, which would fail the module's link, is - refused the same way, as is a call into wasm-bindgen, whose imports on `wasm32-wasip1` - compile to stubs that panic. `thread::spawn` and a socket's `connect` and `bind` import - nothing there and return an error, as Rust's own build for the target does, so they are - not refused. `load` still refuses a module importing anything else. Measured on the api fixture, stripped, with LTO at - `opt-level = "s"`, that std costs 24 KB over `wasm32-unknown-unknown`'s (3%), 9.5 KB - gzipped, and the glue 2 KB more; `wasm32-unknown-unknown` has no clock, so - `Instant::now` panics there. + function, and each one a crate's `.init_array` static runs when the module loads, + through every call, `dyn` implementation, function pointer, static, and drop it can + reach, std's included, and into C and whatever Rust the C calls back, so a new std needs + no change here: every crate builds with `-Zalways-encode-mir`, std's from the dev + shell's MIR sysroot or else `-Zbuild-std`, which doubles the export's peak memory, as + [measured](../bench/ffi/README.md#the-exports-build-cost); a native export reads no MIR. + A C symbol no linked library defines, which would fail the module's link, is refused the + same way, as is a call into wasm-bindgen, whose imports on `wasm32-wasip1` compile to + stubs that panic. `thread::spawn` and a socket's `connect` and `bind` import nothing + there and return an error, as Rust's own build for the target does, so they are not + refused. `load` still refuses a module importing anything else. Measured on the api + fixture, stripped, with LTO at `opt-level = "s"`, that std costs 24 KB over + `wasm32-unknown-unknown`'s (3%), 9.5 KB gzipped, and the glue 2 KB more; + `wasm32-unknown-unknown` has no clock, so `Instant::now` panics there. - **The API** is rust2-ts's shape: each type is a module of its own, `web/.ts`, which exports the type by its name and its functions, each taking `self` first, so a caller writes `import * as Counter from "./web/Counter.ts"` and `Counter.get(c)`, and a bundler diff --git a/ffi/fixtures/lacks/build.rs b/ffi/fixtures/lacks/build.rs new file mode 100644 index 000000000..255677fba --- /dev/null +++ b/ffi/fixtures/lacks/build.rs @@ -0,0 +1,33 @@ +//! Compile `src/needs.ll` into `libneeds.a` for a wasm32 target, with the toolchain's own +//! llc and llvm-ar, which its `llvm-tools` component ships. + +use std::path::PathBuf; +use std::process::Command; + +fn main() { + println!("cargo::rerun-if-changed=src/needs.ll"); + if std::env::var("CARGO_CFG_TARGET_ARCH").unwrap() != "wasm32" { + return; + } + let rustc = std::env::var("RUSTC").unwrap(); + let sysroot = Command::new(rustc).args(["--print", "sysroot"]).output().unwrap(); + assert!(sysroot.status.success(), "rustc --print sysroot failed"); + let sysroot = String::from_utf8(sysroot.stdout).unwrap(); + let bin = PathBuf::from(sysroot.trim()) + .join("lib/rustlib") + .join(std::env::var("HOST").unwrap()) + .join("bin"); + let out = PathBuf::from(std::env::var("OUT_DIR").unwrap()); + let object = out.join("needs.o"); + let run = |cmd: &mut Command| { + let status = cmd.status().unwrap(); + assert!(status.success(), "{cmd:?} failed"); + }; + run(Command::new(bin.join("llc")) + .args(["-mtriple=wasm32-wasip1", "-filetype=obj", "src/needs.ll", "-o"]) + .arg(&object)); + let archive = out.join("libneeds.a"); + let _ = std::fs::remove_file(&archive); + run(Command::new(bin.join("llvm-ar")).arg("rcs").arg(&archive).arg(&object)); + println!("cargo::rustc-link-search=native={}", out.display()); +} diff --git a/ffi/fixtures/lacks/expected.txt b/ffi/fixtures/lacks/expected.txt index b8104920b..9eaf7108c 100644 --- a/ffi/fixtures/lacks/expected.txt +++ b/ffi/fixtures/lacks/expected.txt @@ -1,9 +1,13 @@ fixtures/lacks/src/lib.rs:104:11: rust2-ffi: this calls `undefined`, which no library wasm32-wasip1's module links defines; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config -fixtures/lacks/src/lib.rs:116:11: rust2-ffi: the module imports `page.alert`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config -fixtures/lacks/src/lib.rs:121:27: rust2-ffi: `>::into` reaches `__wbindgen_describe`, `__wbindgen_describe_generic_import`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config -fixtures/lacks/src/lib.rs:121:2: rust2-ffi: `web_sys::features::gen_console::console::log_1` reaches `__wbg_log_ff86bbb34c295e21`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config -fixtures/lacks/src/lib.rs:121:39: rust2-ffi: the drop of `wasm_bindgen::JsValue` reaches `__wbindgen_object_drop_ref`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:122:10: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:136:11: rust2-ffi: the C function `needs_missing` reaches `missing`, which no library wasm32-wasip1's module links defines; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:145:10: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:14:2: rust2-ffi: `std::thread::sleep` reaches `wasi_snapshot_preview1.poll_oneoff`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:157:11: rust2-ffi: the module imports `rust2.rust2_spoof`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:169:11: rust2-ffi: the module imports `page.alert`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:174:27: rust2-ffi: `>::into` reaches `__wbindgen_describe`, `__wbindgen_describe_generic_import`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:174:2: rust2-ffi: `web_sys::features::gen_console::console::log_1` reaches `__wbg_log_ff86bbb34c295e21`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:174:39: rust2-ffi: the drop of `wasm_bindgen::JsValue` reaches `__wbindgen_object_drop_ref`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:18:1: rust2-ffi: the drop of `std::net::TcpListener` reaches `wasi_snapshot_preview1.fd_close`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:29:25: rust2-ffi: the drop of `std::result::Result<(std::net::TcpStream, std::net::SocketAddr), std::io::Error>` reaches `wasi_snapshot_preview1.fd_close`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:29:3: rust2-ffi: `std::net::TcpListener::accept` reaches `wasi_snapshot_preview1.sock_accept`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config diff --git a/ffi/fixtures/lacks/src/lib.rs b/ffi/fixtures/lacks/src/lib.rs index bec3cb66e..85dcf39a0 100644 --- a/ffi/fixtures/lacks/src/lib.rs +++ b/ffi/fixtures/lacks/src/lib.rs @@ -104,6 +104,59 @@ pub fn linked() { unsafe { undefined() } } +// `src/needs.ll`, which the build script compiles for wasm32 alone. +#[cfg(target_arch = "wasm32")] +#[link(name = "needs", kind = "static")] +unsafe extern "C" { + /// A C function calling `lacks_callback` back. + fn needs_callback(); + /// A C function calling one nothing defines. + #[cfg(feature = "undefined")] + fn needs_missing(); +} + +/// What the C library calls back, which reads a file. +#[cfg(target_arch = "wasm32")] +#[unsafe(no_mangle)] +extern "C" fn lacks_callback() { + let _ = std::fs::metadata("callback"); +} + +/// A call of C, which calls back into Rust. +#[cfg(target_arch = "wasm32")] +pub fn called_back() { + // SAFETY: the fixture is only exported, never run. + unsafe { needs_callback() } +} + +/// A call of C, which calls what nothing defines. +#[cfg(all(target_arch = "wasm32", feature = "undefined"))] +pub fn missed() { + // SAFETY: the fixture is only exported, never run. + unsafe { needs_missing() } +} + +/// What the module runs when it loads, which no export calls. +#[used] +#[unsafe(link_section = ".init_array")] +static STARTUP: extern "C" fn() = startup; + +extern "C" fn startup() { + let _ = std::fs::metadata("startup"); +} + +#[link(wasm_import_module = "rust2")] +unsafe extern "C" { + /// A function of the binding's own module that the binding does not implement. + fn rust2_spoof(); +} + +/// A call of what the module imports from the binding's module by a name it lacks. +pub fn spoofed() { + // SAFETY: the fixture is only exported, never run. + unsafe { rust2_spoof() } +} + #[link(wasm_import_module = "page")] unsafe extern "C" { /// A function of a module the binding does not implement. diff --git a/ffi/fixtures/lacks/src/needs.ll b/ffi/fixtures/lacks/src/needs.ll new file mode 100644 index 000000000..51a0da259 --- /dev/null +++ b/ffi/fixtures/lacks/src/needs.ll @@ -0,0 +1,17 @@ +; A C library's two functions, as LLVM IR, which the toolchain's own llc compiles for +; wasm32 with no C toolchain: one calling back into a Rust function the crate defines, and +; one calling a function nothing defines. + +declare void @lacks_callback() + +define void @needs_callback() { + call void @lacks_callback() + ret void +} + +declare void @missing() + +define void @needs_missing() { + call void @missing() + ret void +} diff --git a/ffi/src/cli.rs b/ffi/src/cli.rs index 481d37804..4c7da8595 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -109,6 +109,7 @@ fn try_run(args: &[String]) -> Result { target: Some(target), args: &cargo_args(target, args.cargo.clone()), target_dir: None, + dir: None, env: &[ // The wrapper's answer to cargo's query names the sysroot, which cargo would // otherwise cache by the executables alone. @@ -165,7 +166,10 @@ fn try_run(args: &[String]) -> Result { .collect(); let own = own.join(","); let target_dir = rust2_driver::cargo::target_dir("rust2-ffi", Some(&args.manifest))?; + // Resolved as `lock` resolved it, under the check's profile, whose `cfg`s the glue was + // generated against. let mut cargo = picks.resolving().args(); + cargo.extend(profile_args(&args.cargo)); cargo.extend(["--features".to_string(), "web".to_string()]); Check { tool: "rust2-ffi", @@ -175,6 +179,7 @@ fn try_run(args: &[String]) -> Result { target: Some(crate::web::TARGET), args: &cargo_args(crate::web::TARGET, cargo), target_dir: Some(&target_dir), + dir: Some(picks.dir), env: &[ ("CARGO_CACHE_RUSTC_INFO", "0".as_ref()), (crate::wasm::env::PACKAGE, glue.as_ref()), @@ -723,6 +728,21 @@ fn run_cargo(manifest: &Path, subcommand: &[&str], picks: &Picks<'_>) -> Result< } } +/// The cargo args that pick the profile: `--release`, `-r`, and `--profile`. +fn profile_args(cargo: &[String]) -> Vec { + let mut picked = Vec::new(); + let mut args = cargo.iter(); + while let Some(arg) = args.next() { + match arg.as_str() { + "--release" | "-r" => picked.push(arg.clone()), + "--profile" => picked.extend([Some(arg), args.next()].into_iter().flatten().cloned()), + _ if arg.starts_with("--profile=") => picked.push(arg.clone()), + _ => {} + } + } + picked +} + /// What the cargo args pick for how the check resolves its dependencies. struct Picks<'a> { /// Where cargo runs, which its config and a `--config` path are read from: the facade's. @@ -933,6 +953,20 @@ mod tests { } } + #[test] + fn reads_what_picks_the_profile() { + for (cargo, want) in [ + (&["--release", "--locked"][..], &["--release"][..]), + (&["-r"], &["-r"]), + (&["--features", "a", "--profile", "bench"], &["--profile", "bench"]), + (&["--profile=bench", "--offline"], &["--profile=bench"]), + (&["--features", "release"], &[]), + ] { + let cargo: Vec = cargo.iter().map(|s| s.to_string()).collect(); + assert_eq!(profile_args(&cargo), want, "{cargo:?}"); + } + } + #[test] fn reads_what_picks_a_resolution() { for spelling in [ diff --git a/ffi/src/main.rs b/ffi/src/main.rs index e637fc4c1..11705eba0 100644 --- a/ffi/src/main.rs +++ b/ffi/src/main.rs @@ -17,6 +17,7 @@ extern crate rustc_hir; extern crate rustc_infer; extern crate rustc_interface; extern crate rustc_lexer; +extern crate rustc_metadata; extern crate rustc_middle; extern crate rustc_parse; extern crate rustc_session; diff --git a/ffi/src/wasm.rs b/ffi/src/wasm.rs index 1043bf80d..f2d085d5c 100644 --- a/ffi/src/wasm.rs +++ b/ffi/src/wasm.rs @@ -5,8 +5,10 @@ //! checks it for the target, as `cargo build` would compile it, and walks every function //! the module's exports reach: through std, `dyn`, function pointers, statics, and drop //! glue, as rustc's monomorphization collector does, but into every crate's MIR rather than -//! stopping at another crate's code. Each walk ends at a function the module imports, or -//! one it links from C, whose imports a link of that function alone shows. What reaches an +//! stopping at another crate's code, from each export and each function the module runs +//! when it loads. Each walk ends at a function the module imports, or one it links from C, +//! whose imports a link of that function alone shows, and which goes on into each Rust +//! function the C calls back. What reaches an //! import the host lacks is refused at the call in the crate's own code nearest it, so a //! new std needs no change here: the imports, not a list of std's paths, decide. @@ -16,9 +18,9 @@ use std::process::Command; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet}; use rustc_driver::{Callbacks, Compilation}; use rustc_hir::def::DefKind; -use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE}; use rustc_interface::interface::{Compiler, Config}; -use rustc_middle::middle::exported_symbols::ExportedSymbol; +use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::mir::interpret::{AllocId, GlobalAlloc}; use rustc_middle::mono::{CollectionMode, MonoItem}; use rustc_middle::ty::{self, Instance, InstanceKind, TyCtxt}; @@ -40,13 +42,16 @@ const HOSTS: &[Host] = &[Host { target: crate::web::TARGET, name: "the web target", implements: |module, name| match module { - // What JavaScript implements, which the glue imports and its binding provides. - "rust2" => true, "wasi_snapshot_preview1" => crate::web::wasi().contains(&name), _ => false, }, }]; +/// The module a binding implements the generated crate's imports from: what JavaScript +/// implements, each beside its declaration, so another crate's import from it is one the +/// host lacks, by any name. +const GLUE_MODULE: &str = "rust2"; + /// The host of `target`, if the export knows it. pub fn host(target: &str) -> Option<&'static Host> { HOSTS.iter().find(|host| host.target == target) @@ -164,8 +169,8 @@ impl Callbacks for Walk { fn after_analysis<'tcx>(&mut self, _compiler: &Compiler, tcx: TyCtxt<'tcx>) -> Compilation { if tcx.dcx().has_errors().is_none() { let own = |krate: CrateNum| krate != LOCAL_CRATE && self.own.contains(tcx.crate_name(krate).as_str()); - let graph = Graph::walk(tcx); - let lacks = Lacks::find(tcx, &graph, self.host); + let graph = Graph::walk(tcx, self.host); + let lacks = Lacks::find(&graph, self.host); report(tcx, &graph, &lacks, &own, self.host); } match tcx.dcx().has_errors() { @@ -189,6 +194,14 @@ impl<'tcx> Node<'tcx> { Node::Static(def_id) => def_id, } } + + /// The Rust function or static `def_id` defines under a symbol of its own. + fn defined(tcx: TyCtxt<'tcx>, def_id: DefId) -> Self { + match tcx.def_kind(def_id) { + DefKind::Static { .. } => Node::Static(def_id), + _ => Node::Fn(Instance::mono(tcx, def_id)), + } + } } /// Where a walk ends: a function the module imports, or one it links from C. @@ -212,15 +225,29 @@ struct Graph<'tcx> { origin: Vec, /// The nodes the walk ends at. leaves: FxHashMap, + /// Each C leaf's link, or none for a symbol no library defines. + linked: FxHashMap>, + /// The statics the module runs a function of when it loads, which it holds whether or not + /// an export reaches them. + inits: FxIndexSet, +} + +/// What a C function's link shows: what it imports from the host, and what it calls of the +/// module's own code that no Rust crate defines either, which fails the module's link. +struct Linked { + imports: Vec, + undefined: Vec, } impl<'tcx> Graph<'tcx> { - fn walk(tcx: TyCtxt<'tcx>) -> Self { + fn walk(tcx: TyCtxt<'tcx>, host: &Host) -> Self { let mut graph = Graph { nodes: FxIndexSet::default(), edges: Vec::new(), origin: Vec::new(), leaves: FxHashMap::default(), + linked: FxHashMap::default(), + inits: FxIndexSet::default(), }; // The module's exports: each function the crate gives a symbol of its own. for def_id in tcx.hir_crate_items(()).definitions() { @@ -234,6 +261,12 @@ impl<'tcx> Graph<'tcx> { } } let symbols = Symbols::new(tcx); + for &def_id in &symbols.inits { + let at = graph.add(Node::Static(def_id), None); + graph.origin[at] = at; + graph.inits.insert(at); + } + let mut linker = None; // wasm-bindgen names each import's stand-in after the import: `__wbg_` for a browser // API, `__wbindgen_` for its own. let bindgen = tcx @@ -254,13 +287,12 @@ impl<'tcx> Graph<'tcx> { if tcx.is_foreign_item(def_id) { match symbols.resolve(tcx, node) { Resolved::Rust(def_id) => { - let to = match tcx.def_kind(def_id) { - DefKind::Static { .. } => Node::Static(def_id), - _ => Node::Fn(Instance::mono(tcx, def_id)), - }; - graph.add(to, Some((at, DUMMY_SP))); + graph.add(Node::defined(tcx, def_id), Some((at, DUMMY_SP))); } Resolved::Leaf(leaf) => { + if let Leaf::C(name) = &leaf { + graph.link(tcx, host, &symbols, &mut linker, at, name); + } graph.leaves.insert(at, leaf); } Resolved::Nothing => {} @@ -361,6 +393,49 @@ impl<'tcx> Graph<'tcx> { GlobalAlloc::TypeId { .. } => {} } } + + /// Link the C leaf `at`, named `name`, and walk on into each Rust function or static it + /// calls back, as a C library may call what a Rust crate defines for it. + fn link( + &mut self, + tcx: TyCtxt<'tcx>, + host: &Host, + symbols: &Symbols, + linker: &mut Option>, + at: usize, + name: &str, + ) { + let linker = linker.get_or_insert_with(|| { + Linker::new(tcx, host) + .inspect_err(|err| { + tcx.dcx().err(format!("rust2-ffi: {err}")); + }) + .ok() + }); + let Some(linker) = linker else { + return; + }; + match linker.imports(name) { + Ok(None) => { + self.linked.insert(at, None); + } + Ok(Some((imports, needs))) => { + let mut undefined = Vec::new(); + for need in needs { + match symbols.defined.get(&need) { + Some(&def_id) => { + self.add(Node::defined(tcx, def_id), Some((at, DUMMY_SP))); + } + None => undefined.push(need), + } + } + self.linked.insert(at, Some(Linked { imports, undefined })); + } + Err(err) => { + tcx.dcx().err(format!("rust2-ffi: {err}")); + } + } + } } /// What a foreign item's symbol is. @@ -368,29 +443,44 @@ enum Resolved { /// A Rust function or static some crate defines under it. Rust(DefId), Leaf(Leaf), - /// One the link makes that uses nothing, as the allocator's marker static. + /// One that uses nothing the host lacks: the allocator's marker static the link makes, + /// or a function the generated crate imports, which its binding implements. Nothing, } /// The Rust items each crate defines under a symbol of their own, which a foreign item of -/// another crate may name. +/// another crate, or a C library, may name; and the statics the module runs a function of +/// when it loads. struct Symbols { defined: FxHashMap, + inits: Vec, } impl Symbols { + /// Read from every definition of every crate: a crate `cargo check` compiled records no + /// symbols it exports, which only a crate compiled to code computes. fn new(tcx: TyCtxt<'_>) -> Self { - let mut defined = FxHashMap::default(); + let (mut defined, mut inits) = (FxHashMap::default(), Vec::new()); + let cstore = rustc_metadata::creader::CStore::from_tcx(tcx); + // A proc macro links nothing into the module, and its metadata holds its macros alone. + let macros: FxHashSet = cstore.all_proc_macro_def_ids(tcx).map(|def_id| def_id.krate).collect(); for krate in std::iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) { - let symbols = match krate { - LOCAL_CRATE => &[][..], - _ => tcx.exported_non_generic_symbols(krate), + let definitions: Vec = match krate { + LOCAL_CRATE => tcx.hir_crate_items(()).definitions().map(|d| d.to_def_id()).collect(), + _ if macros.contains(&krate) => continue, + _ => (0..cstore.num_def_ids_untracked(krate)) + .map(|index| DefId { + krate, + index: DefIndex::from_usize(index), + }) + .collect(), }; - for (symbol, _) in symbols { - let ExportedSymbol::NonGeneric(def_id) = *symbol else { - continue; - }; - if !tcx.def_kind(def_id).has_codegen_attrs() { + for def_id in definitions { + let kind = tcx.def_kind(def_id); + if !matches!(kind, DefKind::Fn | DefKind::AssocFn | DefKind::Static { .. }) + || tcx.is_foreign_item(def_id) + || tcx.generics_of(def_id).requires_monomorphization(tcx) + { continue; } let attrs = tcx.codegen_fn_attrs(def_id); @@ -401,14 +491,29 @@ impl Symbols { for &(foreign, ..) in &attrs.foreign_item_symbol_aliases { defined.insert(tcx.symbol_name(Instance::mono(tcx, foreign)).name.to_string(), def_id); } + // A static the link keeps in `.init_array`, each of whose functions the + // module's start calls. + if matches!(kind, DefKind::Static { .. }) + && attrs + .flags + .intersects(CodegenFnAttrFlags::USED_COMPILER | CodegenFnAttrFlags::USED_LINKER) + && attrs + .link_section + .is_some_and(|section| section.as_str().starts_with(".init_array")) + { + inits.push(def_id); + } } } - Self { defined } + Self { defined, inits } } fn resolve<'tcx>(&self, tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Resolved { let def_id = node.def_id(); if let Some(module) = tcx.wasm_import_module_map(def_id.krate).get(&def_id) { + if module == GLUE_MODULE && def_id.krate == LOCAL_CRATE { + return Resolved::Nothing; + } // Imported by its link name, under a symbol rustc mangles apart from other modules'. let name = tcx .codegen_fn_attrs(def_id) @@ -462,49 +567,26 @@ enum Lack { } impl Lacks { - fn find(tcx: TyCtxt<'_>, graph: &Graph<'_>, host: &Host) -> Self { + fn find(graph: &Graph<'_>, host: &Host) -> Self { + let import = |module: &str, name: &str| { + (!(host.implements)(module, name)).then(|| Lack::Import(format!("{module}.{name}"))) + }; let mut by_leaf = FxHashMap::default(); - let mut c = Vec::new(); for (&at, leaf) in &graph.leaves { - match leaf { - Leaf::Import { module, name } => { - let lacks = match (host.implements)(module, name) { - true => Vec::new(), - false => vec![Lack::Import(format!("{module}.{name}"))], - }; - by_leaf.insert(at, lacks); - } - Leaf::C(name) => c.push((at, name.as_str())), - Leaf::Bindgen(name) => { - by_leaf.insert(at, vec![Lack::Bindgen(name.clone())]); - } - } - } - if !c.is_empty() { - match Linker::new(tcx, host) { - Ok(linker) => { - for (at, name) in c { - match linker.imports(name) { - Ok(Some(imports)) => { - let lacks = (imports.into_iter()) - .filter(|(module, name)| !(host.implements)(module, name)) - .map(|(module, name)| Lack::Import(format!("{module}.{name}"))) - .collect(); - by_leaf.insert(at, lacks); - } - Ok(None) => { - by_leaf.insert(at, vec![Lack::Undefined(name.to_string())]); - } - Err(err) => { - tcx.dcx().err(format!("rust2-ffi: {err}")); - } - } - } - } - Err(err) => { - tcx.dcx().err(format!("rust2-ffi: {err}")); - } - } + let lacks = match leaf { + Leaf::Import { module, name } => import(module, name).into_iter().collect(), + Leaf::C(name) => match graph.linked.get(&at) { + Some(Some(linked)) => (linked.imports.iter()) + .filter_map(|(module, name)| import(module, name)) + .chain(linked.undefined.iter().cloned().map(Lack::Undefined)) + .collect(), + Some(None) => vec![Lack::Undefined(name.clone())], + // Its link failed, which is refused already. + None => Vec::new(), + }, + Leaf::Bindgen(name) => vec![Lack::Bindgen(name.clone())], + }; + by_leaf.insert(at, lacks); } Self { by_leaf } } @@ -557,11 +639,12 @@ impl Linker { } /// What the module imports for the C function or static `name`, each by module and - /// name, or none if no library defines it. - fn imports(&self, name: &str) -> Result>, String> { + /// name, and what it needs of the module's own code, each by symbol; or none if no + /// library defines it. + fn imports(&self, name: &str) -> Result, Vec)>, String> { let out = self.dir.join(format!("rust2-ffi-{}-{name}.wasm", std::process::id())); - // What the C library needs of the module's own code, which compiler-builtins and std - // define, it may leave undefined: the module's link finds it. + // What the C library needs of the module's own code, such as compiler-builtins' or a + // crate's callback, it leaves undefined, which the link imports from `env`. let output = Command::new(&self.lld) .args(["-flavor", "wasm", "--no-entry", "--allow-undefined"]) .arg(format!("--export-if-defined={name}")) @@ -582,9 +665,8 @@ impl Linker { if !exports.iter().any(|export| export == name) { return Ok(None); } - Ok(Some( - imports.into_iter().filter(|(module, _)| module != "env").collect(), - )) + let (needs, imports): (Vec, Vec) = imports.into_iter().partition(|(module, _)| module == "env"); + Ok(Some((imports, needs.into_iter().map(|(_, name)| name).collect()))) } } @@ -687,6 +769,13 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn refused.entry((span, to)).or_insert(graph.origin[from]); } } + // What another crate runs when the module loads, which no call of the crate's own code + // reaches, at that crate's static. + for &at in &graph.inits { + if tainted[at] && !mine(at) { + refused.entry((item_span(tcx, graph.nodes[at]), at)).or_insert(at); + } + } let path = |to: usize| { let mut path = vec![to]; while let Some(next) = toward[*path.last().expect("a path has a node")] { @@ -761,8 +850,18 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn let path = path(to); let what = match graph.leaves.get(&to) { Some(Leaf::Import { .. }) => "the module imports".to_string(), - Some(Leaf::C(_) | Leaf::Bindgen(_)) => "this calls".to_string(), - None => format!( + Some(Leaf::Bindgen(_)) => "this calls".to_string(), + // A C function nothing defines is what it lacks itself. + Some(Leaf::C(name)) + if lacks + .by_leaf + .get(&to) + .is_some_and(|l| l[..] == [Lack::Undefined(name.clone())]) => + { + "this calls".to_string() + } + Some(Leaf::C(name)) => format!("the C function `{name}` reaches"), + _ => format!( "{} reaches", describe(tcx, graph, to).unwrap_or_else(|| "this".to_string()) ), @@ -778,10 +877,11 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn if path.len() > 1 { err = err.with_note(through(&path)); } - err.with_note(format!( - "the module exports it through `{}`", - tcx.item_name(graph.nodes[export].def_id()) - )) + let name = tcx.item_name(graph.nodes[export].def_id()); + err.with_note(match graph.inits.contains(&export) { + true => format!("the module runs it when it loads, through `{name}`"), + false => format!("the module exports it through `{name}`"), + }) .emit(); } } diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index ae636788a..f40c53326 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -2493,13 +2493,17 @@ fn lacks() { .filter_map(|line| line.strip_prefix('\t')?.split_once('(').map(|(name, _)| name)) .filter(|name| name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')) .collect(); + // The binding's own module's functions: each the generated TypeScript names. + let binding: String = (std::fs::read_dir(out.join("web")).unwrap()) + .map(|entry| std::fs::read_to_string(entry.unwrap().path()).unwrap()) + .collect(); let mut lacking = std::collections::BTreeSet::new(); for payload in wasmparser::Parser::new(0).parse_all(&wasm) { if let wasmparser::Payload::ImportSection(section) = payload.unwrap() { for import in section.into_imports() { let import = import.unwrap(); let has = match import.module { - "rust2" => true, + "rust2" => binding.contains(import.name), "wasi_snapshot_preview1" => implemented.contains(&import.name), _ => false, }; diff --git a/ts/src/cli.rs b/ts/src/cli.rs index 6492d7875..19639ffdb 100644 --- a/ts/src/cli.rs +++ b/ts/src/cli.rs @@ -70,6 +70,7 @@ fn try_run(args: &[String]) -> Result { args: &cargo, env: &env, target_dir: None, + dir: None, } .run() } From 2776f4bd569719a875f0a0089cc97f167f0a20d9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 04:45:30 -0800 Subject: [PATCH 06/13] refactor(ffi): return a C link's result as one struct Co-Authored-By: Claude Opus 5.5 --- ffi/src/wasm.rs | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/ffi/src/wasm.rs b/ffi/src/wasm.rs index f2d085d5c..966ee96c6 100644 --- a/ffi/src/wasm.rs +++ b/ffi/src/wasm.rs @@ -233,7 +233,8 @@ struct Graph<'tcx> { } /// What a C function's link shows: what it imports from the host, and what it calls of the -/// module's own code that no Rust crate defines either, which fails the module's link. +/// module's own code, each by symbol, which once walked is what no Rust crate defines +/// either, failing the module's link. struct Linked { imports: Vec, undefined: Vec, @@ -419,17 +420,15 @@ impl<'tcx> Graph<'tcx> { Ok(None) => { self.linked.insert(at, None); } - Ok(Some((imports, needs))) => { - let mut undefined = Vec::new(); - for need in needs { - match symbols.defined.get(&need) { - Some(&def_id) => { - self.add(Node::defined(tcx, def_id), Some((at, DUMMY_SP))); - } - None => undefined.push(need), + Ok(Some(mut linked)) => { + linked.undefined.retain(|need| match symbols.defined.get(need) { + Some(&def_id) => { + self.add(Node::defined(tcx, def_id), Some((at, DUMMY_SP))); + false } - } - self.linked.insert(at, Some(Linked { imports, undefined })); + None => true, + }); + self.linked.insert(at, Some(linked)); } Err(err) => { tcx.dcx().err(format!("rust2-ffi: {err}")); @@ -638,10 +637,9 @@ impl Linker { Ok(Self { lld, libs, dir }) } - /// What the module imports for the C function or static `name`, each by module and - /// name, and what it needs of the module's own code, each by symbol; or none if no - /// library defines it. - fn imports(&self, name: &str) -> Result, Vec)>, String> { + /// What the module imports for the C function or static `name`, and what it calls of the + /// module's own code, or none if no library defines it. + fn imports(&self, name: &str) -> Result, String> { let out = self.dir.join(format!("rust2-ffi-{}-{name}.wasm", std::process::id())); // What the C library needs of the module's own code, such as compiler-builtins' or a // crate's callback, it leaves undefined, which the link imports from `env`. @@ -666,7 +664,10 @@ impl Linker { return Ok(None); } let (needs, imports): (Vec, Vec) = imports.into_iter().partition(|(module, _)| module == "env"); - Ok(Some((imports, needs.into_iter().map(|(_, name)| name).collect()))) + Ok(Some(Linked { + imports, + undefined: needs.into_iter().map(|(_, name)| name).collect(), + })) } } From 37d089fb51ced7c392b38fde84ddcb8540ea9425 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:40:57 -0800 Subject: [PATCH 07/13] fix(ffi): refuse undefined C statics, skip the web sysroot for native targets The probe link imports undefined functions and warns of undefined statics, which the walk now resolves to Rust or refuses. RUST2_TS_SYSROOT is checked only for a target with a host, a +verbatim library is found by its own name, and the docs say thread::spawn panics and the walk's refusals leave out/. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 15 +++++--- ffi/fixtures/lacks/expected.txt | 18 +++++---- ffi/fixtures/lacks/src/lib.rs | 29 +++++++++++++++ ffi/fixtures/lacks/src/needs.ll | 22 +++++++++-- ffi/src/cli.rs | 14 ++++--- ffi/src/driver.rs | 4 +- ffi/src/wasm.rs | 66 +++++++++++++++++++++++---------- quest/m3/wasi.md | 5 ++- 8 files changed, 127 insertions(+), 46 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 5f1d225e8..fa002c4d9 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -1329,12 +1329,15 @@ target there. no change here: every crate builds with `-Zalways-encode-mir`, std's from the dev shell's MIR sysroot or else `-Zbuild-std`, which doubles the export's peak memory, as [measured](../bench/ffi/README.md#the-exports-build-cost); a native export reads no MIR. - A C symbol no linked library defines, which would fail the module's link, is refused the - same way, as is a call into wasm-bindgen, whose imports on `wasm32-wasip1` compile to - stubs that panic. `thread::spawn` and a socket's `connect` and `bind` import nothing - there and return an error, as Rust's own build for the target does, so they are not - refused. `load` still refuses a module importing anything else. Measured on the api - fixture, stripped, with LTO at `opt-level = "s"`, that std costs 24 KB over + A C function or static no linked library defines, which would fail the module's link, is + refused the same way, as is a call into wasm-bindgen, whose imports on `wasm32-wasip1` + compile to stubs that panic. `thread::Builder::spawn` and a socket's `connect` and `bind` + import nothing there and return an error, as Rust's own build for the target does, so + they are not refused; `thread::spawn` panics on that error, which aborts the module. The + walk checks the generated crate, so unlike the export's other refusals it runs once + `out/` is written, and a refused export leaves that `out/` behind, failed, as a refused + `Cargo.lock` does. `load` still refuses a module importing anything else. Measured on + the api fixture, stripped, with LTO at `opt-level = "s"`, that std costs 24 KB over `wasm32-unknown-unknown`'s (3%), 9.5 KB gzipped, and the glue 2 KB more; `wasm32-unknown-unknown` has no clock, so `Instant::now` panics there. - **The API** is rust2-ts's shape: each type is a module of its own, `web/.ts`, which diff --git a/ffi/fixtures/lacks/expected.txt b/ffi/fixtures/lacks/expected.txt index 9eaf7108c..17be3ce14 100644 --- a/ffi/fixtures/lacks/expected.txt +++ b/ffi/fixtures/lacks/expected.txt @@ -1,14 +1,16 @@ fixtures/lacks/src/lib.rs:104:11: rust2-ffi: this calls `undefined`, which no library wasm32-wasip1's module links defines; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config -fixtures/lacks/src/lib.rs:122:10: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config -fixtures/lacks/src/lib.rs:136:11: rust2-ffi: the C function `needs_missing` reaches `missing`, which no library wasm32-wasip1's module links defines; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config -fixtures/lacks/src/lib.rs:145:10: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:130:10: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:137:10: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:14:2: rust2-ffi: `std::thread::sleep` reaches `wasi_snapshot_preview1.poll_oneoff`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config -fixtures/lacks/src/lib.rs:157:11: rust2-ffi: the module imports `rust2.rust2_spoof`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config -fixtures/lacks/src/lib.rs:169:11: rust2-ffi: the module imports `page.alert`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config -fixtures/lacks/src/lib.rs:174:27: rust2-ffi: `>::into` reaches `__wbindgen_describe`, `__wbindgen_describe_generic_import`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config -fixtures/lacks/src/lib.rs:174:2: rust2-ffi: `web_sys::features::gen_console::console::log_1` reaches `__wbg_log_ff86bbb34c295e21`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config -fixtures/lacks/src/lib.rs:174:39: rust2-ffi: the drop of `wasm_bindgen::JsValue` reaches `__wbindgen_object_drop_ref`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:151:11: rust2-ffi: the C function `needs_missing` reaches `missing`, which no library wasm32-wasip1's module links defines; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:158:11: rust2-ffi: the C function `needs_data` reaches `missing_data`, which no library wasm32-wasip1's module links defines; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:174:10: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:186:11: rust2-ffi: the module imports `rust2.rust2_spoof`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:18:1: rust2-ffi: the drop of `std::net::TcpListener` reaches `wasi_snapshot_preview1.fd_close`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:198:11: rust2-ffi: the module imports `page.alert`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:203:27: rust2-ffi: `>::into` reaches `__wbindgen_describe`, `__wbindgen_describe_generic_import`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:203:2: rust2-ffi: `web_sys::features::gen_console::console::log_1` reaches `__wbg_log_ff86bbb34c295e21`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:203:39: rust2-ffi: the drop of `wasm_bindgen::JsValue` reaches `__wbindgen_object_drop_ref`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:29:25: rust2-ffi: the drop of `std::result::Result<(std::net::TcpStream, std::net::SocketAddr), std::io::Error>` reaches `wasi_snapshot_preview1.fd_close`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:29:3: rust2-ffi: `std::net::TcpListener::accept` reaches `wasi_snapshot_preview1.sock_accept`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:41:3: rust2-ffi: `std::fs::read_to_string` reaches `wasi_snapshot_preview1.fd_close`, `wasi_snapshot_preview1.fd_fdstat_get`, `wasi_snapshot_preview1.fd_filestat_get`, `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.fd_read`, `wasi_snapshot_preview1.path_open`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config diff --git a/ffi/fixtures/lacks/src/lib.rs b/ffi/fixtures/lacks/src/lib.rs index 85dcf39a0..d41a5f41a 100644 --- a/ffi/fixtures/lacks/src/lib.rs +++ b/ffi/fixtures/lacks/src/lib.rs @@ -113,6 +113,21 @@ unsafe extern "C" { /// A C function calling one nothing defines. #[cfg(feature = "undefined")] fn needs_missing(); + /// A C function reading a static nothing defines. + #[cfg(feature = "undefined")] + fn needs_data() -> i32; + /// A C function calling the function `LACKS_TABLE` holds. + fn needs_table(); +} + +/// What the C library calls through, which reads a file. +#[cfg(target_arch = "wasm32")] +#[unsafe(no_mangle)] +static LACKS_TABLE: extern "C" fn() = tabled; + +#[cfg(target_arch = "wasm32")] +extern "C" fn tabled() { + let _ = std::fs::metadata("table"); } /// What the C library calls back, which reads a file. @@ -136,6 +151,20 @@ pub fn missed() { unsafe { needs_missing() } } +/// A call of C, which reads what nothing defines. +#[cfg(all(target_arch = "wasm32", feature = "undefined"))] +pub fn read_missing() -> i32 { + // SAFETY: the fixture is only exported, never run. + unsafe { needs_data() } +} + +/// A call of C, which calls through a static the crate defines. +#[cfg(target_arch = "wasm32")] +pub fn tabled_call() { + // SAFETY: the fixture is only exported, never run. + unsafe { needs_table() } +} + /// What the module runs when it loads, which no export calls. #[used] #[unsafe(link_section = ".init_array")] diff --git a/ffi/fixtures/lacks/src/needs.ll b/ffi/fixtures/lacks/src/needs.ll index 51a0da259..86c5394d5 100644 --- a/ffi/fixtures/lacks/src/needs.ll +++ b/ffi/fixtures/lacks/src/needs.ll @@ -1,6 +1,7 @@ -; A C library's two functions, as LLVM IR, which the toolchain's own llc compiles for -; wasm32 with no C toolchain: one calling back into a Rust function the crate defines, and -; one calling a function nothing defines. +; A C library's functions, as LLVM IR, which the toolchain's own llc compiles for wasm32 +; with no C toolchain: one calling back into a Rust function the crate defines, one calling +; a function nothing defines, one reading a static nothing defines, and one calling through +; a static the crate defines. declare void @lacks_callback() @@ -15,3 +16,18 @@ define void @needs_missing() { call void @missing() ret void } + +@missing_data = external global i32 + +define i32 @needs_data() { + %value = load i32, ptr @missing_data + ret i32 %value +} + +@LACKS_TABLE = external global ptr + +define void @needs_table() { + %f = load ptr, ptr @LACKS_TABLE + call void %f() + ret void +} diff --git a/ffi/src/cli.rs b/ffi/src/cli.rs index 4c7da8595..052566fd8 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -93,11 +93,13 @@ fn try_run(args: &[String]) -> Result { let roots = std::env::join_paths(std::iter::once(workspace).chain(cargo_home)) .map_err(|err| format!("joining the roots of spans: {err}"))?; let baseline = own.baseline.unwrap_or_default(); - let sysroot = crate::wasm::sysroot()?; // A target with a host checks std's MIR too, from the sysroot or else from source. - let cargo_args = |target: &str, args: Vec| match crate::wasm::host(target).is_some() && sysroot.is_none() { - true => [args, vec![crate::wasm::STD.to_string()]].concat(), - false => args, + let cargo_args = |target: &str, args: Vec| -> Result, String> { + let build_std = crate::wasm::host(target).is_some() && crate::wasm::sysroot(target)?.is_none(); + Ok(match build_std { + true => [args, vec![crate::wasm::STD.to_string()]].concat(), + false => args, + }) }; let check = |target: &str, output: (&str, &std::ffi::OsStr), merge: &std::ffi::OsStr, baseline: &Path| { Check { @@ -107,7 +109,7 @@ fn try_run(args: &[String]) -> Result { root: None, // Always named, so cargo's config never picks another: the exports are found on it. target: Some(target), - args: &cargo_args(target, args.cargo.clone()), + args: &cargo_args(target, args.cargo.clone())?, target_dir: None, dir: None, env: &[ @@ -177,7 +179,7 @@ fn try_run(args: &[String]) -> Result { package: &glue, root: None, target: Some(crate::web::TARGET), - args: &cargo_args(crate::web::TARGET, cargo), + args: &cargo_args(crate::web::TARGET, cargo)?, target_dir: Some(&target_dir), dir: Some(picks.dir), env: &[ diff --git a/ffi/src/driver.rs b/ffi/src/driver.rs index b3249d782..414b999b0 100644 --- a/ffi/src/driver.rs +++ b/ffi/src/driver.rs @@ -210,7 +210,9 @@ impl Callbacks for Export { )); } } - // Refusals still let the export go on to report each clash, but write nothing. + // Refusals still let the export go on to report each clash, but write nothing. Those + // of the CLI's later steps, the lockfile's and the web target's walk, come once the + // export is written, which they check. let target = merge::Target { names: None, package: self.facade.name.clone(), diff --git a/ffi/src/wasm.rs b/ffi/src/wasm.rs index 966ee96c6..b838eae03 100644 --- a/ffi/src/wasm.rs +++ b/ffi/src/wasm.rs @@ -68,7 +68,7 @@ pub const STD: &str = "-Zbuild-std=core,alloc,std,panic_abort"; /// A sysroot of std checked with its MIR for each target with a host, which flake.nix /// builds once per toolchain beside rust2-ts's, if set: absolute. -pub fn sysroot() -> Result, String> { +fn sysroot_dir() -> Result, String> { let dir = match std::env::var(SYSROOT) { Ok(dir) if !dir.is_empty() => dir, Err(std::env::VarError::NotUnicode(_)) => return Err(format!("{SYSROOT} is not UTF-8")), @@ -79,15 +79,22 @@ pub fn sysroot() -> Result, String> { .into_os_string() .into_string() .expect("a UTF-8 path resolves to a UTF-8 path"); - for host in HOSTS { - let lib = Path::new(&dir).join("lib/rustlib").join(host.target).join("lib"); - if !lib.is_dir() { - return Err(format!( - "{SYSROOT} is {dir}, which holds no std for {} in {}", - host.target, - lib.display() - )); - } + Ok(Some(dir)) +} + +/// The [`sysroot_dir`] a crate for `target`, which has a host, compiles with, if set, or +/// an error if it holds no std for `target`. Only a target with a host reads it, so a +/// native export runs under whatever sysroot is set. +pub fn sysroot(target: &str) -> Result, String> { + let Some(dir) = sysroot_dir()? else { + return Ok(None); + }; + let lib = Path::new(&dir).join("lib/rustlib").join(target).join("lib"); + if !lib.is_dir() { + return Err(format!( + "{SYSROOT} is {dir}, which holds no std for {target} in {}", + lib.display() + )); } Ok(Some(dir)) } @@ -100,14 +107,15 @@ const SYSROOT: &str = "RUST2_TS_SYSROOT"; /// unit's fingerprint, names the flags and the sysroot, so a target directory keeps /// nothing compiled without them. pub fn flags(args: &[String], added: &mut Vec) -> Result<(), String> { - let sysroot = sysroot()?; if args[1..] == ["-vV"] { + let sysroot = sysroot_dir()?; println!("rust2-ffi-mir: {} {}", MIR.join(" "), sysroot.as_deref().unwrap_or(STD)); } // The target's crates only: build scripts and proc macros run on the host, with its std. - if args.windows(2).any(|w| w[0] == "--target" && host(&w[1]).is_some()) { + let target = args.windows(2).find(|w| w[0] == "--target" && host(&w[1]).is_some()); + if let Some([_, target]) = target { added.extend(MIR.map(str::to_string)); - if let Some(sysroot) = sysroot { + if let Some(sysroot) = sysroot(target)? { added.extend(["--sysroot".to_string(), sysroot]); } } @@ -620,7 +628,11 @@ impl Linker { libs_of.filter(|lib| matches!(lib.kind, NativeLibKind::Static { .. } | NativeLibKind::Unspecified)) { let name = lib.name; - let file = format!("lib{name}.a"); + // `+verbatim` names the archive's file as it is. + let file = match lib.verbatim { + Some(true) => name.to_string(), + _ => format!("lib{name}.a"), + }; let Some(found) = search.iter().map(|dir| dir.join(&file)).find(|path| path.is_file()) else { return Err(format!( "`{}` links the C library `{name}`, which is in no directory the check searches", @@ -642,9 +654,17 @@ impl Linker { fn imports(&self, name: &str) -> Result, String> { let out = self.dir.join(format!("rust2-ffi-{}-{name}.wasm", std::process::id())); // What the C library needs of the module's own code, such as compiler-builtins' or a - // crate's callback, it leaves undefined, which the link imports from `env`. + // crate's callback, it leaves undefined: a function the link imports from `env`, and + // a static, which no import can stand in for, it warns of by name. let output = Command::new(&self.lld) - .args(["-flavor", "wasm", "--no-entry", "--allow-undefined"]) + .args([ + "-flavor", + "wasm", + "--no-entry", + "--no-demangle", + "--color-diagnostics=never", + ]) + .args(["--import-undefined", "--warn-unresolved-symbols"]) .arg(format!("--export-if-defined={name}")) .arg("-o") .arg(&out) @@ -664,10 +684,16 @@ impl Linker { return Ok(None); } let (needs, imports): (Vec, Vec) = imports.into_iter().partition(|(module, _)| module == "env"); - Ok(Some(Linked { - imports, - undefined: needs.into_iter().map(|(_, name)| name).collect(), - })) + let statics = (String::from_utf8_lossy(&output.stderr).lines()) + .filter_map(|line| { + line.split_once(": undefined symbol: ") + .map(|(_, name)| name.trim().to_string()) + }) + .collect::>(); + let mut undefined: Vec = needs.into_iter().map(|(_, name)| name).chain(statics).collect(); + undefined.sort(); + undefined.dedup(); + Ok(Some(Linked { imports, undefined })) } } diff --git a/quest/m3/wasi.md b/quest/m3/wasi.md index 29ff3da08..8fba9bf70 100644 --- a/quest/m3/wasi.md +++ b/quest/m3/wasi.md @@ -35,8 +35,9 @@ hosts that are WASI runtimes. Planned in m3 before a consumer asks, as as [the web target](/ffi/README.md#typescript-web) refuses its own: this target adds its host's entry to `ffi/src/wasm.rs`'s `HOSTS`, the WASI imports that host implements, and the walk and its MIR build need nothing more. What imports nothing, it cannot refuse: - on `wasm32-wasip1`, `thread::spawn` and a socket's `connect` and `bind` return an error - without calling the host, as Rust's own build for the target does, while a socket a host + on `wasm32-wasip1`, `thread::Builder::spawn` and a socket's `connect` and `bind` return + an error without calling the host, as Rust's own build for the target does, and + `thread::spawn` panics on it, while a socket a host passes in imports `sock_accept` and `sock_shutdown`, refused where the host lacks them. Public API: the name of the WASI target and what it writes, if it lands. From 72b8759295b341bd0cebea89afc27eaf3e748d23 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:09:04 -0800 Subject: [PATCH 08/13] fix(ffi): walk other crates' C exports, and link C as the module does Every other crate's #[no_mangle] item the module exports is a root, refused at its item when the module cannot load or link with it. The probe links the crates' libraries before libc, skips a library whose cfg is off, keeps the constructors of what it pulls in, and refuses a +whole-archive library. A wasm-bindgen runtime function with a symbol of its own is no stub, and an instance past the recursion limit is refused as rustc's collector refuses it. rust2-go initializes the new Check fields. Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/lacks/expected.txt | 1 + ffi/fixtures/lacks/src/lib.rs | 6 ++ ffi/src/main.rs | 2 + ffi/src/wasm.rs | 149 ++++++++++++++++++++++++++++---- go/src/cli.rs | 2 + 5 files changed, 141 insertions(+), 19 deletions(-) diff --git a/ffi/fixtures/lacks/expected.txt b/ffi/fixtures/lacks/expected.txt index 17be3ce14..a1c381f5b 100644 --- a/ffi/fixtures/lacks/expected.txt +++ b/ffi/fixtures/lacks/expected.txt @@ -11,6 +11,7 @@ fixtures/lacks/src/lib.rs:198:11: rust2-ffi: the module imports `page.alert`, wh fixtures/lacks/src/lib.rs:203:27: rust2-ffi: `>::into` reaches `__wbindgen_describe`, `__wbindgen_describe_generic_import`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:203:2: rust2-ffi: `web_sys::features::gen_console::console::log_1` reaches `__wbg_log_ff86bbb34c295e21`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:203:39: rust2-ffi: the drop of `wasm_bindgen::JsValue` reaches `__wbindgen_object_drop_ref`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:225:10: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:29:25: rust2-ffi: the drop of `std::result::Result<(std::net::TcpStream, std::net::SocketAddr), std::io::Error>` reaches `wasi_snapshot_preview1.fd_close`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:29:3: rust2-ffi: `std::net::TcpListener::accept` reaches `wasi_snapshot_preview1.sock_accept`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:41:3: rust2-ffi: `std::fs::read_to_string` reaches `wasi_snapshot_preview1.fd_close`, `wasi_snapshot_preview1.fd_fdstat_get`, `wasi_snapshot_preview1.fd_filestat_get`, `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.fd_read`, `wasi_snapshot_preview1.path_open`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config diff --git a/ffi/fixtures/lacks/src/lib.rs b/ffi/fixtures/lacks/src/lib.rs index d41a5f41a..e376c01c3 100644 --- a/ffi/fixtures/lacks/src/lib.rs +++ b/ffi/fixtures/lacks/src/lib.rs @@ -218,3 +218,9 @@ pub fn seeded() -> usize { pub fn greet() { println!("hello from {:?}", std::env::var("USER").ok()); } + +/// What the module exports as C's, which no export of the binding calls. +#[unsafe(no_mangle)] +extern "C" fn lacks_exported() { + let _ = std::fs::metadata("exported"); +} diff --git a/ffi/src/main.rs b/ffi/src/main.rs index 11705eba0..59d72949a 100644 --- a/ffi/src/main.rs +++ b/ffi/src/main.rs @@ -10,6 +10,8 @@ extern crate rustc_abi; extern crate rustc_ast; extern crate rustc_attr_ir; +extern crate rustc_attr_parsing; +extern crate rustc_crate_store; extern crate rustc_data_structures; extern crate rustc_driver; extern crate rustc_errors; diff --git a/ffi/src/wasm.rs b/ffi/src/wasm.rs index b838eae03..412c4bc05 100644 --- a/ffi/src/wasm.rs +++ b/ffi/src/wasm.rs @@ -15,6 +15,9 @@ use std::path::{Path, PathBuf}; use std::process::Command; +use rustc_attr_ir::LangItem; +use rustc_attr_parsing::eval_config_entry; +use rustc_crate_store::NativeLib; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet}; use rustc_driver::{Callbacks, Compilation}; use rustc_hir::def::DefKind; @@ -238,6 +241,14 @@ struct Graph<'tcx> { /// The statics the module runs a function of when it loads, which it holds whether or not /// an export reaches them. inits: FxIndexSet, + /// What another crate exports from the module, which it holds whether or not the + /// generated crate's exports reach it, though no binding calls it. + kept: FxIndexSet, + /// How many times each node's function is already instantiated on the way it was first + /// reached, as rustc's collector counts against the recursion limit. + depth: Vec, + /// The node each node was first reached from. + parent: Vec>, } /// What a C function's link shows: what it imports from the host, and what it calls of the @@ -257,7 +268,11 @@ impl<'tcx> Graph<'tcx> { leaves: FxHashMap::default(), linked: FxHashMap::default(), inits: FxIndexSet::default(), + kept: FxIndexSet::default(), + depth: Vec::new(), + parent: Vec::new(), }; + whole_archives(tcx, host); // The module's exports: each function the crate gives a symbol of its own. for def_id in tcx.hir_crate_items(()).definitions() { let def_id = def_id.to_def_id(); @@ -275,6 +290,12 @@ impl<'tcx> Graph<'tcx> { graph.origin[at] = at; graph.inits.insert(at); } + for &def_id in &symbols.kept { + let at = graph.add(Node::defined(tcx, def_id), None); + if graph.origin[at] == at { + graph.kept.insert(at); + } + } let mut linker = None; // wasm-bindgen names each import's stand-in after the import: `__wbg_` for a browser // API, `__wbindgen_` for its own. @@ -282,11 +303,16 @@ impl<'tcx> Graph<'tcx> { .crates(()) .iter() .any(|&krate| tcx.crate_name(krate).as_str() == "wasm_bindgen"); + // A stand-in has no symbol of its own, unlike wasm-bindgen's runtime functions the + // module exports to JavaScript, such as `__wbindgen_free`. let stub = |def_id: DefId| { let name = tcx.item_name(def_id); - (bindgen && (name.as_str().starts_with("__wbg_") || name.as_str().starts_with("__wbindgen_"))) - .then(|| name.to_string()) + (bindgen + && (name.as_str().starts_with("__wbg_") || name.as_str().starts_with("__wbindgen_")) + && !tcx.codegen_fn_attrs(def_id).contains_extern_indicator()) + .then(|| name.to_string()) }; + let limit = tcx.recursion_limit(); let mut next = 0; while next < graph.nodes.len() { let at = next; @@ -347,7 +373,24 @@ impl<'tcx> Graph<'tcx> { MonoItem::Static(def_id) => Node::Static(def_id), MonoItem::GlobalAsm(_) => continue, }; - graph.add(to, Some((at, item.span))); + let to = graph.add(to, Some((at, item.span))); + // An instance within instances of itself past the limit, as of a + // function calling itself with an ever larger type, is refused as + // rustc's collector refuses it, rather than walked without end. + let depth = graph.depth[to]; + let depth = match graph.nodes[to] { + Node::Fn(instance) if tcx.is_lang_item(instance.def_id(), LangItem::DropGlue) => depth / 4, + _ => depth, + }; + if !limit.value_within_limit(depth) { + tcx.dcx().span_fatal( + item.span, + format!( + "rust2-ffi: reached the recursion limit while instantiating `{}`", + tcx.def_path_str(graph.nodes[to].def_id()) + ), + ); + } } } Node::Static(def_id) => { @@ -370,6 +413,17 @@ impl<'tcx> Graph<'tcx> { if new { self.edges.push(Vec::new()); self.origin.push(from.map_or(at, |(from, _)| self.origin[from])); + self.parent.push(from.map(|(from, _)| from)); + // The nearest node on the way here of the same function, one deeper than it. + let mut up = from.map(|(from, _)| from); + let depth = loop { + match up { + Some(above) if self.nodes[above].def_id() == node.def_id() => break self.depth[above] + 1, + Some(above) => up = self.parent[above], + None => break 0, + } + }; + self.depth.push(depth); } if let Some((from, span)) = from { self.edges[from].push((at, span)); @@ -456,10 +510,11 @@ enum Resolved { } /// The Rust items each crate defines under a symbol of their own, which a foreign item of -/// another crate, or a C library, may name; and the statics the module runs a function of -/// when it loads. +/// another crate, or a C library, may name; those of them other crates than the generated +/// one export from the module; and the statics the module runs a function of when it loads. struct Symbols { defined: FxHashMap, + kept: Vec, inits: Vec, } @@ -467,7 +522,7 @@ impl Symbols { /// Read from every definition of every crate: a crate `cargo check` compiled records no /// symbols it exports, which only a crate compiled to code computes. fn new(tcx: TyCtxt<'_>) -> Self { - let (mut defined, mut inits) = (FxHashMap::default(), Vec::new()); + let (mut defined, mut kept, mut inits) = (FxHashMap::default(), Vec::new(), Vec::new()); let cstore = rustc_metadata::creader::CStore::from_tcx(tcx); // A proc macro links nothing into the module, and its metadata holds its macros alone. let macros: FxHashSet = cstore.all_proc_macro_def_ids(tcx).map(|def_id| def_id.krate).collect(); @@ -493,6 +548,15 @@ impl Symbols { let attrs = tcx.codegen_fn_attrs(def_id); if attrs.contains_extern_indicator() { defined.insert(tcx.symbol_name(Instance::mono(tcx, def_id)).name.to_string(), def_id); + // What another crate exports from the module as C's, as a cdylib does + // each such item of every crate but the compiler's builtins, unless std + // keeps it to itself. + if krate != LOCAL_CRATE + && !tcx.is_compiler_builtins(krate) + && !attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) + { + kept.push(def_id); + } } // An implementation of an externally implementable item, under the item's name. for &(foreign, ..) in &attrs.foreign_item_symbol_aliases { @@ -512,7 +576,7 @@ impl Symbols { } } } - Self { defined, inits } + Self { defined, kept, inits } } fn resolve<'tcx>(&self, tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Resolved { @@ -615,18 +679,17 @@ impl Linker { let lld = rustc_session::filesearch::make_target_bin_path(sysroot, rustc_session::config::host_tuple()) .join("rust-lld"); let own = sysroot.join("lib/rustlib").join(host.target).join("lib/self-contained"); - let mut libs = vec![own.join("libc.a")]; + let libc = own.join("libc.a"); + let mut libs = Vec::new(); let search: Vec = (tcx.sess.target_filesearch()) .search_paths(rustc_session::search_paths::PathKind::Native) .map(|path| path.dir.to_path_buf()) .chain([own]) .collect(); + // In the link's order, the crate's own first and the C library last, so a library + // defining one of libc's symbols defines it here as it does in the module. for krate in std::iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) { - // A library of C's: wasm32 links no other kind, and a `wasm_import_module` is none. - let libs_of = tcx.native_libraries(krate).iter(); - for lib in - libs_of.filter(|lib| matches!(lib.kind, NativeLibKind::Static { .. } | NativeLibKind::Unspecified)) - { + for lib in c_libraries(tcx, krate) { let name = lib.name; // `+verbatim` names the archive's file as it is. let file = match lib.verbatim { @@ -639,11 +702,12 @@ impl Linker { tcx.crate_name(krate) )); }; - if !libs.contains(&found) { + if found != libc && !libs.contains(&found) { libs.push(found); } } } + libs.push(libc); let dir = tcx.sess.io.temps_dir.clone().or_else(|| tcx.sess.io.output_dir.clone()); let dir = dir.ok_or("the check has no output directory to link in")?; Ok(Self { lld, libs, dir }) @@ -665,6 +729,9 @@ impl Linker { "--color-diagnostics=never", ]) .args(["--import-undefined", "--warn-unresolved-symbols"]) + // What each C object it pulls in runs when the module loads, as the module's + // start calls it. + .arg("--export-if-defined=__wasm_call_ctors") .arg(format!("--export-if-defined={name}")) .arg("-o") .arg(&out) @@ -697,6 +764,40 @@ impl Linker { } } +/// The C libraries `krate` links for the target being checked: wasm32 links no other kind, +/// and a `wasm_import_module` is none. +fn c_libraries<'a>(tcx: TyCtxt<'a>, krate: CrateNum) -> impl Iterator { + (tcx.native_libraries(krate).iter()) + .filter(|lib| matches!(lib.kind, NativeLibKind::Static { .. } | NativeLibKind::Unspecified)) + .filter(move |lib| (lib.cfg.as_ref()).is_none_or(|cfg| eval_config_entry(tcx.sess, cfg).as_bool())) +} + +/// Refuse each C library a crate links whole: the module runs or exports what it holds +/// that no call names, which the walk cannot reach. +fn whole_archives(tcx: TyCtxt<'_>, host: &Host) { + for krate in std::iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) { + for lib in c_libraries(tcx, krate) { + if let NativeLibKind::Static { + whole_archive: Some(true), + .. + } = lib.kind + { + let span = (lib.foreign_module).map_or_else(|| tcx.def_span(krate.as_def_id()), |m| tcx.def_span(m)); + tcx.dcx().span_err( + span, + format!( + "rust2-ffi: `{}` links the C library `{}` with `+whole-archive`, whose code no call names, so the export cannot check what {}'s module imports for it; link it without `+whole-archive` for {}", + tcx.crate_name(krate), + lib.name, + host.target, + host.target + ), + ); + } + } + } +} + /// A function a module imports, by module and name. type Import = (String, String); @@ -810,8 +911,9 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn } path }; - // Everything `to` reaches the same way that the module cannot have, said. - let reached = |to: usize| { + // Everything `to` reaches the same way that the module cannot have, said: only what + // fails the module's load or link unless something calls `to`. + let reached = |to: usize, called: bool| { let mut seen = FxHashSet::default(); let mut stack = vec![to]; let mut reached = Vec::new(); @@ -833,7 +935,8 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn match lack { Lack::Import(name) => imports.push(name.as_str()), Lack::Undefined(name) => undefined.push(name.as_str()), - Lack::Bindgen(name) => bindgen.push(name.as_str()), + Lack::Bindgen(name) if called => bindgen.push(name.as_str()), + Lack::Bindgen(_) => {} } } let mut said = Vec::new(); @@ -867,13 +970,21 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn tcx.dcx() .struct_err(format!( "rust2-ffi: the generated crate reaches {} through std; this is a bug in rust2-ffi", - reached(to), + reached(to, true), )) .with_note(through(&path)) .emit(); } } + // What another crate exports, which no call of the crate's own code reaches, at that + // crate's item, if the module cannot load or link with it. + for &at in &graph.kept { + if tainted[at] && !mine(at) && !reached(at, false).is_empty() { + refused.entry((item_span(tcx, graph.nodes[at]), at)).or_insert(at); + } + } for ((span, to), export) in refused { + let called = !(to == export && graph.kept.contains(&to)); let path = path(to); let what = match graph.leaves.get(&to) { Some(Leaf::Import { .. }) => "the module imports".to_string(), @@ -897,7 +1008,7 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn span, format!( "rust2-ffi: {what} {}; leave it out of {}'s build with a `cfg`, or exclude its export in the config", - reached(to), + reached(to, called), host.target ), ); diff --git a/go/src/cli.rs b/go/src/cli.rs index 2f6a5cea2..2ba43ad85 100644 --- a/go/src/cli.rs +++ b/go/src/cli.rs @@ -45,6 +45,8 @@ fn try_run(args: &[String]) -> Result { // targets, as the host's `usize` is. target: Some(rustc_session::config::host_tuple()), args: &cargo, + target_dir: None, + dir: None, env: &[ (env::PACKAGE, package.as_ref()), (env::OUT, out.as_os_str()), From a74483cce66376e182e035750cc97ad45cf4530a Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:23:49 -0800 Subject: [PATCH 09/13] fix(ffi): walk depth first, link in rustc's crate order, match stubs by form The walk counts each function's instances on the way down, as rustc's collector does, so branching polymorphic recursion reaches the limit along one path. The probe orders crate libraries as rustc's link does. A wasm-bindgen stand-in is a __wbg__ function or wasm-bindgen's own __wbindgen_*. Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/lacks/Cargo.toml | 3 + ffi/fixtures/lacks/src/lib.rs | 18 +++ ffi/src/wasm.rs | 223 ++++++++++++++++++---------------- ffi/tests/fixtures.rs | 14 +++ 4 files changed, 153 insertions(+), 105 deletions(-) diff --git a/ffi/fixtures/lacks/Cargo.toml b/ffi/fixtures/lacks/Cargo.toml index 46f20c890..02c0d718d 100644 --- a/ffi/fixtures/lacks/Cargo.toml +++ b/ffi/fixtures/lacks/Cargo.toml @@ -14,6 +14,9 @@ name = "lacks" # A call of a C function no library defines, which fails the module's link, so a build # without it can compare the module's imports with what the export refused. undefined = [] +# A function calling itself with an ever larger type, which the walk refuses at the +# recursion limit, as rustc's collector does, rather than walk without end. +recursion = [] [dependencies] web-sys = { version = "0.3.107", features = ["console"] } diff --git a/ffi/fixtures/lacks/src/lib.rs b/ffi/fixtures/lacks/src/lib.rs index e376c01c3..31e1c2646 100644 --- a/ffi/fixtures/lacks/src/lib.rs +++ b/ffi/fixtures/lacks/src/lib.rs @@ -224,3 +224,21 @@ pub fn greet() { extern "C" fn lacks_exported() { let _ = std::fs::metadata("exported"); } + +/// Polymorphic recursion two ways at each step, which a breadth-first walk would expand +/// exponentially before reaching the limit. +#[cfg(feature = "recursion")] +#[allow(unconditional_recursion)] +fn nest(deeper: bool) { + if deeper { + nest::<(T,)>(deeper) + } else { + nest::<[T; 1]>(deeper) + } +} + +/// A call of what instantiates itself without end. +#[cfg(feature = "recursion")] +pub fn nested(deeper: bool) { + nest::(deeper) +} diff --git a/ffi/src/wasm.rs b/ffi/src/wasm.rs index 412c4bc05..8cb5940fc 100644 --- a/ffi/src/wasm.rs +++ b/ffi/src/wasm.rs @@ -244,11 +244,6 @@ struct Graph<'tcx> { /// What another crate exports from the module, which it holds whether or not the /// generated crate's exports reach it, though no binding calls it. kept: FxIndexSet, - /// How many times each node's function is already instantiated on the way it was first - /// reached, as rustc's collector counts against the recursion limit. - depth: Vec, - /// The node each node was first reached from. - parent: Vec>, } /// What a C function's link shows: what it imports from the host, and what it calls of the @@ -269,8 +264,6 @@ impl<'tcx> Graph<'tcx> { linked: FxHashMap::default(), inits: FxIndexSet::default(), kept: FxIndexSet::default(), - depth: Vec::new(), - parent: Vec::new(), }; whole_archives(tcx, host); // The module's exports: each function the crate gives a symbol of its own. @@ -297,112 +290,143 @@ impl<'tcx> Graph<'tcx> { } } let mut linker = None; - // wasm-bindgen names each import's stand-in after the import: `__wbg_` for a browser - // API, `__wbindgen_` for its own. - let bindgen = tcx - .crates(()) - .iter() - .any(|&krate| tcx.crate_name(krate).as_str() == "wasm_bindgen"); - // A stand-in has no symbol of its own, unlike wasm-bindgen's runtime functions the - // module exports to JavaScript, such as `__wbindgen_free`. + // wasm-bindgen names each import's stand-in after the import: `__wbg__`, + // with 16 hex digits of hash, for a browser API its macro declares in any crate, and + // `__wbindgen_` for its own, in its own crate. A stand-in has no symbol of its + // own, unlike its runtime functions the module exports to JavaScript, such as + // `__wbindgen_free`. + let bindgen = (tcx.crates(()).iter()).find(|&&krate| tcx.crate_name(krate).as_str() == "wasm_bindgen"); let stub = |def_id: DefId| { + let bindgen = *bindgen?; let name = tcx.item_name(def_id); - (bindgen - && (name.as_str().starts_with("__wbg_") || name.as_str().starts_with("__wbindgen_")) - && !tcx.codegen_fn_attrs(def_id).contains_extern_indicator()) - .then(|| name.to_string()) + let name = name.as_str(); + let generated = name + .strip_prefix("__wbg_") + .and_then(|rest| rest.rsplit_once('_')) + .is_some_and(|(_, hash)| hash.len() == 16 && hash.bytes().all(|b| b.is_ascii_hexdigit())); + let own = def_id.krate == bindgen && name.starts_with("__wbindgen_"); + ((generated || own) && !tcx.codegen_fn_attrs(def_id).contains_extern_indicator()).then(|| name.to_string()) }; let limit = tcx.recursion_limit(); - let mut next = 0; - while next < graph.nodes.len() { - let at = next; - next += 1; + // Depth first, as rustc's collector walks, counting the instances of each function on + // the way down: one within instances of itself past the limit, as of a function + // calling itself with an ever larger type, is refused as the collector refuses it, + // rather than walked without end. + enum Step { + Enter(usize, Span), + Exit(DefId), + } + let mut stack: Vec = (0..graph.nodes.len()) + .rev() + .map(|at| Step::Enter(at, DUMMY_SP)) + .collect(); + let mut entered = FxHashSet::default(); + let mut depths: FxHashMap = FxHashMap::default(); + while let Some(step) = stack.pop() { + let (at, span) = match step { + Step::Enter(at, span) => (at, span), + Step::Exit(def_id) => { + *depths.get_mut(&def_id).expect("entered") -= 1; + continue; + } + }; + if !entered.insert(at) { + continue; + } let node = graph.nodes[at]; let def_id = node.def_id(); - if tcx.is_foreign_item(def_id) { - match symbols.resolve(tcx, node) { - Resolved::Rust(def_id) => { - graph.add(Node::defined(tcx, def_id), Some((at, DUMMY_SP))); - } - Resolved::Leaf(leaf) => { - if let Leaf::C(name) = &leaf { - graph.link(tcx, host, &symbols, &mut linker, at, name); + let depth = depths.entry(def_id).or_default(); + // Drop glue nests tightly, and rustc gives it more room. + let deep = match node { + Node::Fn(instance) if tcx.is_lang_item(instance.def_id(), LangItem::DropGlue) => *depth / 4, + _ => *depth, + }; + if !limit.value_within_limit(deep) { + let span = if span.is_dummy() { tcx.def_span(def_id) } else { span }; + tcx.dcx().span_fatal( + span, + format!( + "rust2-ffi: reached the recursion limit while instantiating `{}`", + tcx.def_path_str(def_id) + ), + ); + } + *depth += 1; + stack.push(Step::Exit(def_id)); + 'expand: { + if tcx.is_foreign_item(def_id) { + match symbols.resolve(tcx, node) { + Resolved::Rust(def_id) => { + graph.add(Node::defined(tcx, def_id), Some((at, DUMMY_SP))); + } + Resolved::Leaf(leaf) => { + if let Leaf::C(name) = &leaf { + graph.link(tcx, host, &symbols, &mut linker, at, name); + } + graph.leaves.insert(at, leaf); } - graph.leaves.insert(at, leaf); + Resolved::Nothing => {} } - Resolved::Nothing => {} + break 'expand; } - continue; - } - match node { - Node::Fn(instance) => { - if let InstanceKind::Item(def_id) = instance.def - && matches!(tcx.def_kind(def_id), DefKind::Fn) - && let Some(name) = stub(def_id) - { - graph.leaves.insert(at, Leaf::Bindgen(name)); - continue; - } - if let InstanceKind::Item(def_id) = instance.def { - // A constructor's body only builds its value. - if matches!(tcx.def_kind(def_id), DefKind::Ctor(..)) { - continue; + match node { + Node::Fn(instance) => { + if let InstanceKind::Item(def_id) = instance.def + && matches!(tcx.def_kind(def_id), DefKind::Fn) + && let Some(name) = stub(def_id) + { + graph.leaves.insert(at, Leaf::Bindgen(name)); + break 'expand; } - if !tcx.is_mir_available(def_id) { + if let InstanceKind::Item(def_id) = instance.def { + // A constructor's body only builds its value. + if matches!(tcx.def_kind(def_id), DefKind::Ctor(..)) { + break 'expand; + } + if !tcx.is_mir_available(def_id) { + tcx.dcx().span_err( + tcx.def_span(def_id), + format!( + "rust2-ffi: `{}` of crate `{}` has no MIR to follow, which every crate a target with a host compiles with; this is a bug in rust2-ffi", + tcx.def_path_str(def_id), + tcx.crate_name(def_id.krate) + ), + ); + break 'expand; + } + } + let Ok((used, _)) = tcx.items_of_instance((instance, CollectionMode::UsedItems)) else { tcx.dcx().span_err( tcx.def_span(def_id), - format!( - "rust2-ffi: `{}` of crate `{}` has no MIR to follow, which every crate a target with a host compiles with; this is a bug in rust2-ffi", - tcx.def_path_str(def_id), - tcx.crate_name(def_id.krate) - ), + format!("rust2-ffi: `{instance}` does not normalize; this is a bug in rust2-ffi"), ); - continue; - } - } - let Ok((used, _)) = tcx.items_of_instance((instance, CollectionMode::UsedItems)) else { - tcx.dcx().span_err( - tcx.def_span(def_id), - format!("rust2-ffi: `{instance}` does not normalize; this is a bug in rust2-ffi"), - ); - continue; - }; - for item in used { - let to = match item.node { - MonoItem::Fn(instance) => Node::Fn(instance), - MonoItem::Static(def_id) => Node::Static(def_id), - MonoItem::GlobalAsm(_) => continue, - }; - let to = graph.add(to, Some((at, item.span))); - // An instance within instances of itself past the limit, as of a - // function calling itself with an ever larger type, is refused as - // rustc's collector refuses it, rather than walked without end. - let depth = graph.depth[to]; - let depth = match graph.nodes[to] { - Node::Fn(instance) if tcx.is_lang_item(instance.def_id(), LangItem::DropGlue) => depth / 4, - _ => depth, + break 'expand; }; - if !limit.value_within_limit(depth) { - tcx.dcx().span_fatal( - item.span, - format!( - "rust2-ffi: reached the recursion limit while instantiating `{}`", - tcx.def_path_str(graph.nodes[to].def_id()) - ), - ); + for item in used { + let to = match item.node { + MonoItem::Fn(instance) => Node::Fn(instance), + MonoItem::Static(def_id) => Node::Static(def_id), + MonoItem::GlobalAsm(_) => continue, + }; + graph.add(to, Some((at, item.span))); } } - } - Node::Static(def_id) => { - // What its value points to: functions, vtables' methods, and other statics. - if let Ok(alloc) = tcx.eval_static_initializer(def_id) { - let mut seen = FxHashSet::default(); - for &prov in alloc.inner().provenance().ptrs().values() { - graph.alloc(tcx, prov.alloc_id(), at, &mut seen); + Node::Static(def_id) => { + // What its value points to: functions, vtables' methods, and other statics. + if let Ok(alloc) = tcx.eval_static_initializer(def_id) { + let mut seen = FxHashSet::default(); + for &prov in alloc.inner().provenance().ptrs().values() { + graph.alloc(tcx, prov.alloc_id(), at, &mut seen); + } } } } } + for &(to, span) in graph.edges[at].iter().rev() { + if !entered.contains(&to) { + stack.push(Step::Enter(to, span)); + } + } } graph } @@ -413,17 +437,6 @@ impl<'tcx> Graph<'tcx> { if new { self.edges.push(Vec::new()); self.origin.push(from.map_or(at, |(from, _)| self.origin[from])); - self.parent.push(from.map(|(from, _)| from)); - // The nearest node on the way here of the same function, one deeper than it. - let mut up = from.map(|(from, _)| from); - let depth = loop { - match up { - Some(above) if self.nodes[above].def_id() == node.def_id() => break self.depth[above] + 1, - Some(above) => up = self.parent[above], - None => break 0, - } - }; - self.depth.push(depth); } if let Some((from, span)) = from { self.edges[from].push((at, span)); @@ -688,7 +701,7 @@ impl Linker { .collect(); // In the link's order, the crate's own first and the C library last, so a library // defining one of libc's symbols defines it here as it does in the module. - for krate in std::iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) { + for krate in std::iter::once(LOCAL_CRATE).chain(tcx.postorder_cnums(()).iter().rev().copied()) { for lib in c_libraries(tcx, krate) { let name = lib.name; // `+verbatim` names the archive's file as it is. diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index f40c53326..bbb48737d 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -2440,6 +2440,20 @@ fn reject() { assert_eq!(errors.join("\n") + "\n", expected); } +/// A function instantiating itself without end, two ways at each step, refused at the +/// recursion limit as rustc's collector refuses it, rather than walked until memory runs out. +#[test] +fn lacks_recursion() { + let dir = scratch().join("lacks").join("recursion"); + let output = export("lacks", &dir, &[WEB], &["--features", "recursion"]); + assert!(!output.status.success(), "exporting lacks succeeded"); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + stderr.contains("rust2-ffi: reached the recursion limit while instantiating `lacks::nest`"), + "{stderr}" + ); +} + /// What the web target's host lacks, refused at the call in the crate's own code nearest /// it, by every route the walk follows. Without the call of an undefined C function, whose /// module would not link, the module builds anyway: what it imports that the binding does From cd818f4993878405b38508254f3d487c6f3ea896 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 14:29:06 -0800 Subject: [PATCH 10/13] fix(ffi): refuse wasm-bindgen stubs only where a binding or the module's load calls them An own crate's #[no_mangle] function no binding export calls was refused on the wasm-bindgen stand-ins it reaches, and a dependency's .init_array static with a symbol of its own was taken for an uncalled export, dropping its stand-ins. Track what the generated crate's exports and the module's start reach, and skip a refusal that says nothing. Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/lacks/src/lib.rs | 8 +++++++ ffi/src/wasm.rs | 42 ++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/ffi/fixtures/lacks/src/lib.rs b/ffi/fixtures/lacks/src/lib.rs index 31e1c2646..b96d26dae 100644 --- a/ffi/fixtures/lacks/src/lib.rs +++ b/ffi/fixtures/lacks/src/lib.rs @@ -225,6 +225,14 @@ extern "C" fn lacks_exported() { let _ = std::fs::metadata("exported"); } +/// What the module exports as C's, which no export of the binding calls, calling a browser +/// API: wasm-bindgen's panicking stand-in fails neither the module's load nor its link, so it +/// is not refused. +#[unsafe(no_mangle)] +extern "C" fn lacks_logged() { + web_sys::console::log_1(&"exported".into()); +} + /// Polymorphic recursion two ways at each step, which a breadth-first walk would expand /// exponentially before reaching the limit. #[cfg(feature = "recursion")] diff --git a/ffi/src/wasm.rs b/ffi/src/wasm.rs index 8cb5940fc..1c7241f56 100644 --- a/ffi/src/wasm.rs +++ b/ffi/src/wasm.rs @@ -283,9 +283,10 @@ impl<'tcx> Graph<'tcx> { graph.origin[at] = at; graph.inits.insert(at); } + // A static the module runs when it loads is called, whatever else exports it. for &def_id in &symbols.kept { let at = graph.add(Node::defined(tcx, def_id), None); - if graph.origin[at] == at { + if !graph.inits.contains(&at) { graph.kept.insert(at); } } @@ -876,9 +877,21 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn } } } + // What the generated crate's exports and the module's start reach, which a binding or + // the module's load calls, unlike what only another crate's export reaches. + let mut called = vec![false; len]; + let mut stack: Vec = (0..len) + .filter(|&at| graph.origin[at] == at && !graph.kept.contains(&at)) + .collect(); + while let Some(at) = stack.pop() { + if !std::mem::replace(&mut called[at], true) { + stack.extend(graph.edges[at].iter().map(|&(next, _)| next)); + } + } // Each call into that, from the crate's own code or from the generated crate's, by - // where it is refused, with the node it calls and the export it serves. - let mut refused: FxIndexMap<(Span, usize), usize> = FxIndexMap::default(); + // where it is refused, with the node it calls, the export it serves, and whether + // anything calls it. + let mut refused: FxIndexMap<(Span, usize), (usize, bool)> = FxIndexMap::default(); let mut runtime: FxIndexSet = FxIndexSet::default(); for (from, edges) in graph.edges.iter().enumerate() { if !mine(from) && !glue(from) { @@ -907,14 +920,18 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn // Its call names the export's item, whose own span says where. true => item_span(tcx, graph.nodes[to]), }; - refused.entry((span, to)).or_insert(graph.origin[from]); + // A generic function's instances share its spans, some perhaps called and some not. + let entry = refused.entry((span, to)).or_insert((graph.origin[from], called[from])); + if called[from] && !entry.1 { + *entry = (graph.origin[from], true); + } } } // What another crate runs when the module loads, which no call of the crate's own code // reaches, at that crate's static. for &at in &graph.inits { if tainted[at] && !mine(at) { - refused.entry((item_span(tcx, graph.nodes[at]), at)).or_insert(at); + refused.entry((item_span(tcx, graph.nodes[at]), at)).or_insert((at, true)); } } let path = |to: usize| { @@ -992,12 +1009,16 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn // What another crate exports, which no call of the crate's own code reaches, at that // crate's item, if the module cannot load or link with it. for &at in &graph.kept { - if tainted[at] && !mine(at) && !reached(at, false).is_empty() { - refused.entry((item_span(tcx, graph.nodes[at]), at)).or_insert(at); + if tainted[at] && !mine(at) { + refused.entry((item_span(tcx, graph.nodes[at]), at)).or_insert((at, false)); } } - for ((span, to), export) in refused { - let called = !(to == export && graph.kept.contains(&to)); + for ((span, to), (export, called)) in refused { + // What no binding calls is refused only where the module cannot load or link with it. + let said = reached(to, called); + if said.is_empty() { + continue; + } let path = path(to); let what = match graph.leaves.get(&to) { Some(Leaf::Import { .. }) => "the module imports".to_string(), @@ -1020,8 +1041,7 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn let mut err = tcx.dcx().struct_span_err( span, format!( - "rust2-ffi: {what} {}; leave it out of {}'s build with a `cfg`, or exclude its export in the config", - reached(to, called), + "rust2-ffi: {what} {said}; leave it out of {}'s build with a `cfg`, or exclude its export in the config", host.target ), ); From 848238e5619777c3f30ff1b441555d3d5755e2aa Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 14:30:13 -0800 Subject: [PATCH 11/13] style(ffi): format Co-Authored-By: Claude Opus 5.5 --- ffi/src/wasm.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ffi/src/wasm.rs b/ffi/src/wasm.rs index 1c7241f56..c237f6775 100644 --- a/ffi/src/wasm.rs +++ b/ffi/src/wasm.rs @@ -931,7 +931,9 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn // reaches, at that crate's static. for &at in &graph.inits { if tainted[at] && !mine(at) { - refused.entry((item_span(tcx, graph.nodes[at]), at)).or_insert((at, true)); + refused + .entry((item_span(tcx, graph.nodes[at]), at)) + .or_insert((at, true)); } } let path = |to: usize| { @@ -1010,7 +1012,9 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn // crate's item, if the module cannot load or link with it. for &at in &graph.kept { if tainted[at] && !mine(at) { - refused.entry((item_span(tcx, graph.nodes[at]), at)).or_insert((at, false)); + refused + .entry((item_span(tcx, graph.nodes[at]), at)) + .or_insert((at, false)); } } for ((span, to), (export, called)) in refused { From 5d9858d7c706bf1fe079969e40fd8390433da05c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 15:08:02 -0800 Subject: [PATCH 12/13] fix(ffi): count what C calls back as called A C object's constructor, which the module's start runs, may call back Rust that an uncalled export pulled in, and the probe cannot tell a constructor's call from another's. Settled by the maintainer in #861. Co-Authored-By: Claude Opus 5.5 --- ffi/fixtures/lacks/build.rs | 24 +++++++++++++++++------- ffi/fixtures/lacks/expected.txt | 3 +++ ffi/fixtures/lacks/src/ctor.ll | 15 +++++++++++++++ ffi/fixtures/lacks/src/lib.rs | 24 ++++++++++++++++++++++++ ffi/src/wasm.rs | 9 ++++++++- 5 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 ffi/fixtures/lacks/src/ctor.ll diff --git a/ffi/fixtures/lacks/build.rs b/ffi/fixtures/lacks/build.rs index 255677fba..764dfd37b 100644 --- a/ffi/fixtures/lacks/build.rs +++ b/ffi/fixtures/lacks/build.rs @@ -1,11 +1,12 @@ -//! Compile `src/needs.ll` into `libneeds.a` for a wasm32 target, with the toolchain's own -//! llc and llvm-ar, which its `llvm-tools` component ships. +//! Compile `src/needs.ll` and `src/ctor.ll` into `libneeds.a` for a wasm32 target, with the +//! toolchain's own llc and llvm-ar, which its `llvm-tools` component ships. use std::path::PathBuf; use std::process::Command; fn main() { println!("cargo::rerun-if-changed=src/needs.ll"); + println!("cargo::rerun-if-changed=src/ctor.ll"); if std::env::var("CARGO_CFG_TARGET_ARCH").unwrap() != "wasm32" { return; } @@ -18,16 +19,25 @@ fn main() { .join(std::env::var("HOST").unwrap()) .join("bin"); let out = PathBuf::from(std::env::var("OUT_DIR").unwrap()); - let object = out.join("needs.o"); let run = |cmd: &mut Command| { let status = cmd.status().unwrap(); assert!(status.success(), "{cmd:?} failed"); }; - run(Command::new(bin.join("llc")) - .args(["-mtriple=wasm32-wasip1", "-filetype=obj", "src/needs.ll", "-o"]) - .arg(&object)); let archive = out.join("libneeds.a"); let _ = std::fs::remove_file(&archive); - run(Command::new(bin.join("llvm-ar")).arg("rcs").arg(&archive).arg(&object)); + let mut ar = Command::new(bin.join("llvm-ar")); + ar.arg("rcs").arg(&archive); + // Each its own object, so a call pulls only its own object, and its constructor, into + // the link. + for name in ["needs", "ctor"] { + let object = out.join(format!("{name}.o")); + run(Command::new(bin.join("llc")) + .args(["-mtriple=wasm32-wasip1", "-filetype=obj"]) + .arg(format!("src/{name}.ll")) + .arg("-o") + .arg(&object)); + ar.arg(object); + } + run(&mut ar); println!("cargo::rustc-link-search=native={}", out.display()); } diff --git a/ffi/fixtures/lacks/expected.txt b/ffi/fixtures/lacks/expected.txt index a1c381f5b..8b2265fc1 100644 --- a/ffi/fixtures/lacks/expected.txt +++ b/ffi/fixtures/lacks/expected.txt @@ -12,6 +12,9 @@ fixtures/lacks/src/lib.rs:203:27: rust2-ffi: `>::into fixtures/lacks/src/lib.rs:203:2: rust2-ffi: `web_sys::features::gen_console::console::log_1` reaches `__wbg_log_ff86bbb34c295e21`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:203:39: rust2-ffi: the drop of `wasm_bindgen::JsValue` reaches `__wbindgen_object_drop_ref`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:225:10: rust2-ffi: `std::fs::metadata` reaches `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.path_filestat_get`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:257:27: rust2-ffi: `>::into` reaches `__wbindgen_describe`, `__wbindgen_describe_generic_import`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:257:2: rust2-ffi: `web_sys::features::gen_console::console::log_1` reaches `__wbg_log_ff86bbb34c295e21`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config +fixtures/lacks/src/lib.rs:257:48: rust2-ffi: the drop of `wasm_bindgen::JsValue` reaches `__wbindgen_object_drop_ref`, which wasm-bindgen panics on in wasm32-wasip1 rather than import from JavaScript; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:29:25: rust2-ffi: the drop of `std::result::Result<(std::net::TcpStream, std::net::SocketAddr), std::io::Error>` reaches `wasi_snapshot_preview1.fd_close`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:29:3: rust2-ffi: `std::net::TcpListener::accept` reaches `wasi_snapshot_preview1.sock_accept`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config fixtures/lacks/src/lib.rs:41:3: rust2-ffi: `std::fs::read_to_string` reaches `wasi_snapshot_preview1.fd_close`, `wasi_snapshot_preview1.fd_fdstat_get`, `wasi_snapshot_preview1.fd_filestat_get`, `wasi_snapshot_preview1.fd_prestat_dir_name`, `wasi_snapshot_preview1.fd_prestat_get`, `wasi_snapshot_preview1.fd_read`, `wasi_snapshot_preview1.path_open`, which the web target's host lacks; leave it out of wasm32-wasip1's build with a `cfg`, or exclude its export in the config diff --git a/ffi/fixtures/lacks/src/ctor.ll b/ffi/fixtures/lacks/src/ctor.ll new file mode 100644 index 000000000..b7cf51ae7 --- /dev/null +++ b/ffi/fixtures/lacks/src/ctor.ll @@ -0,0 +1,15 @@ +; A C object whose constructor calls back into a Rust function the crate defines, which the +; module's start runs once any call pulls the object into the link. + +declare void @lacks_constructed() + +define internal void @construct() { + call void @lacks_constructed() + ret void +} + +@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @construct, ptr null }] + +define void @needs_ctor() { + ret void +} diff --git a/ffi/fixtures/lacks/src/lib.rs b/ffi/fixtures/lacks/src/lib.rs index b96d26dae..981edb62b 100644 --- a/ffi/fixtures/lacks/src/lib.rs +++ b/ffi/fixtures/lacks/src/lib.rs @@ -233,6 +233,30 @@ extern "C" fn lacks_logged() { web_sys::console::log_1(&"exported".into()); } +#[cfg(target_arch = "wasm32")] +#[link(name = "needs", kind = "static")] +unsafe extern "C" { + /// A C function whose object's constructor calls `lacks_constructed` back at load. + fn needs_ctor(); +} + +/// What the module exports as C's, which no export of the binding calls, but whose call +/// pulls in a C object the module's start constructs. +#[cfg(target_arch = "wasm32")] +#[unsafe(no_mangle)] +extern "C" fn lacks_pulled() { + // SAFETY: the fixture is only exported, never run. + unsafe { needs_ctor() } +} + +/// What the C object's constructor calls back at load, calling a browser API: refused, +/// since the module's start runs it. +#[cfg(target_arch = "wasm32")] +#[unsafe(no_mangle)] +extern "C" fn lacks_constructed() { + web_sys::console::log_1(&"constructed".into()); +} + /// Polymorphic recursion two ways at each step, which a breadth-first walk would expand /// exponentially before reaching the limit. #[cfg(feature = "recursion")] diff --git a/ffi/src/wasm.rs b/ffi/src/wasm.rs index c237f6775..71be917ed 100644 --- a/ffi/src/wasm.rs +++ b/ffi/src/wasm.rs @@ -878,10 +878,17 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn } } // What the generated crate's exports and the module's start reach, which a binding or - // the module's load calls, unlike what only another crate's export reaches. + // the module's load calls, unlike what only another crate's export reaches. What a C + // library calls back counts as called: a constructor of the C objects its link pulls in + // may call it at load, and the probe cannot tell a constructor's call from another's. let mut called = vec![false; len]; let mut stack: Vec = (0..len) .filter(|&at| graph.origin[at] == at && !graph.kept.contains(&at)) + .chain( + (graph.leaves.iter()) + .filter(|(_, leaf)| matches!(leaf, Leaf::C(_))) + .flat_map(|(&at, _)| graph.edges[at].iter().map(|&(next, _)| next)), + ) .collect(); while let Some(at) = stack.pop() { if !std::mem::replace(&mut called[at], true) { From 3c997b74bf216d63e0961bd5058aa910bebfb1ea Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 18 Nov 2026 15:15:18 -0800 Subject: [PATCH 13/13] fix(ffi): refuse another crate's export C calls back at its item Co-Authored-By: Claude Opus 5.5 --- ffi/src/wasm.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/ffi/src/wasm.rs b/ffi/src/wasm.rs index 71be917ed..7371de529 100644 --- a/ffi/src/wasm.rs +++ b/ffi/src/wasm.rs @@ -881,14 +881,14 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn // the module's load calls, unlike what only another crate's export reaches. What a C // library calls back counts as called: a constructor of the C objects its link pulls in // may call it at load, and the probe cannot tell a constructor's call from another's. + let back: FxHashSet = (graph.leaves.iter()) + .filter(|(_, leaf)| matches!(leaf, Leaf::C(_))) + .flat_map(|(&at, _)| graph.edges[at].iter().map(|&(next, _)| next)) + .collect(); let mut called = vec![false; len]; let mut stack: Vec = (0..len) .filter(|&at| graph.origin[at] == at && !graph.kept.contains(&at)) - .chain( - (graph.leaves.iter()) - .filter(|(_, leaf)| matches!(leaf, Leaf::C(_))) - .flat_map(|(&at, _)| graph.edges[at].iter().map(|&(next, _)| next)), - ) + .chain(back.iter().copied()) .collect(); while let Some(at) = stack.pop() { if !std::mem::replace(&mut called[at], true) { @@ -1016,12 +1016,13 @@ fn report<'tcx>(tcx: TyCtxt<'tcx>, graph: &Graph<'tcx>, lacks: &Lacks, own: &dyn } } // What another crate exports, which no call of the crate's own code reaches, at that - // crate's item, if the module cannot load or link with it. + // crate's item, if the module cannot load or link with it, or if C calls it back, as a + // constructor may at load, where no call of the crate's own code names it. for &at in &graph.kept { if tainted[at] && !mine(at) { refused .entry((item_span(tcx, graph.nodes[at]), at)) - .or_insert((at, false)); + .or_insert((at, back.contains(&at))); } } for ((span, to), (export, called)) in refused {