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/driver/src/cargo.rs b/driver/src/cargo.rs index e68a0b23a..940aa4097 100644 --- a/driver/src/cargo.rs +++ b/driver/src/cargo.rs @@ -135,6 +135,12 @@ 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>, + /// 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<'_> { @@ -142,8 +148,11 @@ 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 package_dir = self.manifest.parent().expect("a manifest path has a parent"); + 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.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/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/README.md b/ffi/README.md index 65c689f75..fa002c4d9 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -1319,10 +1319,27 @@ 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, 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 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 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/Cargo.toml b/ffi/fixtures/lacks/Cargo.toml new file mode 100644 index 000000000..02c0d718d --- /dev/null +++ b/ffi/fixtures/lacks/Cargo.toml @@ -0,0 +1,25 @@ +[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" + +[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 = [] +# 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"] } + +[lints] +workspace = true diff --git a/ffi/fixtures/lacks/build.rs b/ffi/fixtures/lacks/build.rs new file mode 100644 index 000000000..764dfd37b --- /dev/null +++ b/ffi/fixtures/lacks/build.rs @@ -0,0 +1,43 @@ +//! 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; + } + 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 run = |cmd: &mut Command| { + let status = cmd.status().unwrap(); + assert!(status.success(), "{cmd:?} failed"); + }; + let archive = out.join("libneeds.a"); + let _ = std::fs::remove_file(&archive); + 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 new file mode 100644 index 000000000..8b2265fc1 --- /dev/null +++ b/ffi/fixtures/lacks/expected.txt @@ -0,0 +1,25 @@ +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: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: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: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 +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/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 new file mode 100644 index 000000000..981edb62b --- /dev/null +++ b/ffi/fixtures/lacks/src/lib.rs @@ -0,0 +1,276 @@ +//! 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() } +} + +// `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(); + /// 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. +#[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() } +} + +/// 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")] +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. + 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()); +} + +/// 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"); +} + +/// 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()); +} + +#[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")] +#[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/fixtures/lacks/src/needs.ll b/ffi/fixtures/lacks/src/needs.ll new file mode 100644 index 000000000..86c5394d5 --- /dev/null +++ b/ffi/fixtures/lacks/src/needs.ll @@ -0,0 +1,33 @@ +; 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() + +define void @needs_callback() { + call void @lacks_callback() + ret void +} + +declare void @missing() + +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 71f741d1f..052566fd8 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -93,6 +93,14 @@ 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(); + // A target with a host checks std's MIR too, from the sysroot or else from source. + 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 { tool: "rust2-ffi", @@ -101,8 +109,13 @@ 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, + 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 +150,45 @@ 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))?; + // 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", + 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), + dir: Some(picks.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 @@ -682,6 +730,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. @@ -892,6 +955,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/driver.rs b/ffi/src/driver.rs index eee9cbfc5..414b999b0 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)) }) } @@ -206,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/main.rs b/ffi/src/main.rs index bf5f8a9ad..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; @@ -17,10 +19,13 @@ 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; extern crate rustc_span; +extern crate rustc_structures; +extern crate rustc_symbol_mangling; extern crate rustc_trait_selection; mod abi; @@ -40,6 +45,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..7371de529 --- /dev/null +++ b/ffi/src/wasm.rs @@ -0,0 +1,1100 @@ +//! 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, 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. + +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; +use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE}; +use rustc_interface::interface::{Compiler, Config}; +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}; +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. +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 { + "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) +} + +/// 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. +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")), + _ => 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"); + 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)) +} + +/// 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> { + 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. + 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(target)? { + 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, self.host); + let lacks = Lacks::find(&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, + } + } + + /// 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. +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, + /// 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 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, +} + +/// What a C function's link shows: what it imports from the host, and what it calls of the +/// 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, +} + +impl<'tcx> Graph<'tcx> { + 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(), + kept: FxIndexSet::default(), + }; + 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(); + 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); + for &def_id in &symbols.inits { + let at = graph.add(Node::Static(def_id), None); + 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.inits.contains(&at) { + graph.kept.insert(at); + } + } + let mut linker = None; + // 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); + 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(); + // 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(); + 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); + } + Resolved::Nothing => {} + } + break 'expand; + } + 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 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: `{instance}` does not normalize; this is a bug in rust2-ffi"), + ); + break 'expand; + }; + 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); + } + } + } + } + } + for &(to, span) in graph.edges[at].iter().rev() { + if !entered.contains(&to) { + stack.push(Step::Enter(to, span)); + } + } + } + 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 { .. } => {} + } + } + + /// 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(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 + } + None => true, + }); + self.linked.insert(at, Some(linked)); + } + Err(err) => { + tcx.dcx().err(format!("rust2-ffi: {err}")); + } + } + } +} + +/// What a foreign item's symbol is. +enum Resolved { + /// A Rust function or static some crate defines under it. + Rust(DefId), + Leaf(Leaf), + /// 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, 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, +} + +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 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(); + for krate in std::iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) { + 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 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); + 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 { + 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, kept, 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) + .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(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(); + for (&at, leaf) in &graph.leaves { + 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 } + } +} + +/// 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 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.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. + 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", + tcx.crate_name(krate) + )); + }; + 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 }) + } + + /// 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: 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", + "--no-demangle", + "--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) + .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); + } + let (needs, imports): (Vec, Vec) = imports.into_iter().partition(|(module, _)| module == "env"); + 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 })) + } +} + +/// 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); + +/// 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, 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); + } + } + } + // 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. 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(back.iter().copied()) + .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, 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) { + 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]), + }; + // 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, true)); + } + } + 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: 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(); + 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) if called => bindgen.push(name.as_str()), + Lack::Bindgen(_) => {} + } + } + 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, 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, 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, back.contains(&at))); + } + } + 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(), + 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()) + ), + }; + let mut err = tcx.dcx().struct_span_err( + span, + format!( + "rust2-ffi: {what} {said}; leave it out of {}'s build with a `cfg`, or exclude its export in the config", + host.target + ), + ); + if path.len() > 1 { + err = err.with_note(through(&path)); + } + 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(); + } +} + +/// 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..877bd4991 100644 --- a/ffi/src/web.rs +++ b/ffi/src/web.rs @@ -40,6 +40,25 @@ 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..bbb48737d 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -2440,6 +2440,100 @@ 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 +/// 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(); + // 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" => binding.contains(import.name), + "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/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()), 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..8fba9bf70 100644 --- a/quest/m3/wasi.md +++ b/quest/m3/wasi.md @@ -32,8 +32,13 @@ 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::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. @@ -43,5 +48,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 diff --git a/ts/src/cli.rs b/ts/src/cli.rs index b98f634db..19639ffdb 100644 --- a/ts/src/cli.rs +++ b/ts/src/cli.rs @@ -69,6 +69,8 @@ fn try_run(args: &[String]) -> Result { target: Some(TARGET), args: &cargo, env: &env, + target_dir: None, + dir: None, } .run() }