Skip to content

Commit 266e90f

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 266e90f

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

  • tests/run-make/wasm-compiler-builtins-object-arch
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
if name.ends_with(".rmeta") || name.ends_with(".rmeta-link") {
33+
continue;
34+
}
35+
let obj_data = member.data(&*data).unwrap();
36+
let obj = object::File::parse(obj_data).unwrap_or_else(|e| {
37+
panic!("failed to parse member `{name}` in compiler_builtins rlib: {e}")
38+
});
39+
let arch = obj.architecture();
40+
assert!(
41+
matches!(arch, Architecture::Wasm32 | Architecture::Wasm64),
42+
"object `{name}` in compiler_builtins rlib has architecture {arch:?}, \
43+
expected wasm — see rust-lang/rust#132802",
44+
);
45+
checked += 1;
46+
}
47+
48+
assert!(
49+
checked > 0,
50+
"no object members found in compiler_builtins rlib at {} — \
51+
archive should always contain object files",
52+
rlibs[0].display(),
53+
);
54+
}

0 commit comments

Comments
 (0)