Skip to content

Commit d5d20bf

Browse files
committed
tests: check wasm compiler_builtins object architecture
Add a run-make test that checks the `.o` members in the prebuilt `libcompiler_builtins` archive for `wasm32-wasip1` are wasm objects. This guards against building wasm compiler-rt fallbacks with the host C toolchain.
1 parent e95e732 commit d5d20bf

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

  • tests/run-make/wasm-compiler-builtins-object-arch
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/132802>.
2+
//!
3+
//! The prebuilt `libcompiler_builtins` rlib bundled in the wasm sysroot must
4+
//! contain wasm object files — never host ELF/Mach-O/COFF. Bootstrap could
5+
//! previously pick the host C toolchain for compiler-rt fallbacks on wasm
6+
//! targets and silently embed host objects into the wasm sysroot
7+
//! (fixed in rust-lang/rust#137457).
8+
9+
//@ only-wasm32
10+
11+
use run_make_support::object::read::Object;
12+
use run_make_support::object::read::archive::ArchiveFile;
13+
use run_make_support::object::{self, Architecture};
14+
use run_make_support::{has_extension, has_prefix, rfs, rustc, shallow_find_files};
15+
16+
fn main() {
17+
let libdir = rustc().print("target-libdir").run().stdout_utf8();
18+
let libdir = libdir.trim();
19+
20+
let rlibs = shallow_find_files(libdir, |path| {
21+
has_prefix(path, "libcompiler_builtins") && has_extension(path, "rlib")
22+
});
23+
assert!(!rlibs.is_empty(), "no libcompiler_builtins rlib found in {libdir}");
24+
25+
let data = rfs::read(&rlibs[0]);
26+
let archive = ArchiveFile::parse(&*data).unwrap();
27+
28+
let mut checked = 0usize;
29+
for member in archive.members() {
30+
let member = member.unwrap();
31+
let name = std::str::from_utf8(member.name()).unwrap_or("<invalid-utf8>");
32+
let obj_data = member.data(&*data).unwrap();
33+
let Ok(obj) = object::File::parse(obj_data) else {
34+
continue;
35+
};
36+
let arch = obj.architecture();
37+
assert!(
38+
matches!(arch, Architecture::Wasm32 | Architecture::Wasm64),
39+
"object `{name}` in compiler_builtins rlib has architecture {arch:?}, \
40+
expected wasm — see rust-lang/rust#132802",
41+
);
42+
checked += 1;
43+
}
44+
45+
assert!(
46+
checked > 0,
47+
"no object members found in compiler_builtins rlib at {} — \
48+
archive should always contain object files",
49+
rlibs[0].display(),
50+
);
51+
}

0 commit comments

Comments
 (0)