Skip to content

Commit 91d8c56

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 91d8c56

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

  • tests/run-make/wasm-compiler-builtins-object-arch
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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::archive::ArchiveFile;
12+
use run_make_support::{has_extension, has_prefix, rfs, rustc, shallow_find_files};
13+
14+
fn main() {
15+
let libdir = rustc().print("target-libdir").run().stdout_utf8();
16+
let libdir = libdir.trim();
17+
18+
let rlibs = shallow_find_files(libdir, |path| {
19+
has_prefix(path, "libcompiler_builtins") && has_extension(path, "rlib")
20+
});
21+
assert!(!rlibs.is_empty(), "no libcompiler_builtins rlib found in {libdir}");
22+
23+
let data = rfs::read(&rlibs[0]);
24+
let archive = ArchiveFile::parse(&*data).unwrap();
25+
26+
let mut checked = 0usize;
27+
for member in archive.members() {
28+
let member = member.unwrap();
29+
let name = std::str::from_utf8(member.name()).unwrap_or("<invalid-utf8>");
30+
if !name.ends_with(".o") {
31+
continue;
32+
}
33+
let obj_data = member.data(&*data).unwrap();
34+
assert!(
35+
obj_data.starts_with(b"\0asm"),
36+
"object `{name}` in compiler_builtins rlib is not a wasm object \
37+
(first bytes: {:02x?}) — see rust-lang/rust#132802",
38+
&obj_data[..4.min(obj_data.len())]
39+
);
40+
checked += 1;
41+
}
42+
43+
assert!(
44+
checked > 0,
45+
"no .o members found in compiler_builtins rlib at {} — \
46+
archive should always contain object files",
47+
rlibs[0].display(),
48+
);
49+
}

0 commit comments

Comments
 (0)