|
| 1 | +//! Tool used by CI to inspect compiler-builtins archives and help ensure we won't run into any |
| 2 | +//! linking errors. |
| 3 | +
|
| 4 | +use std::collections::{BTreeMap, BTreeSet}; |
| 5 | +use std::fs; |
| 6 | +use std::io::{BufRead, BufReader}; |
| 7 | +use std::path::{Path, PathBuf}; |
| 8 | +use std::process::{Command, Stdio}; |
| 9 | + |
| 10 | +use object::read::archive::{ArchiveFile, ArchiveMember}; |
| 11 | +use object::{Object, ObjectSymbol, Symbol, SymbolKind, SymbolScope, SymbolSection}; |
| 12 | +use serde_json::Value; |
| 13 | + |
| 14 | +const CHECK_LIBRARIES: &[&str] = &["compiler_builtins", "builtins_test_intrinsics"]; |
| 15 | +const CHECK_EXTENSIONS: &[Option<&str>] = &[Some("rlib"), Some("a"), Some("exe"), None]; |
| 16 | + |
| 17 | +const USAGE: &str = "Usage: |
| 18 | +
|
| 19 | + symbol-check build-and-check CARGO_ARGS ... |
| 20 | +
|
| 21 | +Cargo will get invoked with `CARGO_ARGS` and all output |
| 22 | +`compiler_builtins*.rlib` files will be checked. |
| 23 | +"; |
| 24 | + |
| 25 | +fn main() { |
| 26 | + // Create a `&str` vec so we can match on it. |
| 27 | + let args = std::env::args().collect::<Vec<_>>(); |
| 28 | + let args_ref = args.iter().map(String::as_str).collect::<Vec<_>>(); |
| 29 | + |
| 30 | + match &args_ref[1..] { |
| 31 | + ["build-and-check", rest @ ..] if !rest.is_empty() => { |
| 32 | + let paths = exec_cargo_with_args(rest); |
| 33 | + for path in paths { |
| 34 | + println!("Checking {}", path.display()); |
| 35 | + verify_no_duplicates(&path); |
| 36 | + verify_core_symbols(&path); |
| 37 | + } |
| 38 | + } |
| 39 | + _ => { |
| 40 | + println!("{USAGE}"); |
| 41 | + std::process::exit(1); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// Run `cargo build` with the provided additional arguments, collecting the list of created |
| 47 | +/// libraries. |
| 48 | +fn exec_cargo_with_args(args: &[&str]) -> Vec<PathBuf> { |
| 49 | + let mut cmd = Command::new("cargo") |
| 50 | + .arg("build") |
| 51 | + .arg("--message-format=json") |
| 52 | + .args(args) |
| 53 | + .stdout(Stdio::piped()) |
| 54 | + .spawn() |
| 55 | + .expect("failed to launch Cargo"); |
| 56 | + |
| 57 | + let stdout = cmd.stdout.take().unwrap(); |
| 58 | + let reader = BufReader::new(stdout); |
| 59 | + let mut check_files = Vec::new(); |
| 60 | + |
| 61 | + for line in reader.lines() { |
| 62 | + let line = line.expect("failed to read line"); |
| 63 | + println!("{line}"); // tee to stdout |
| 64 | + |
| 65 | + // Select only steps that create files |
| 66 | + let j: Value = serde_json::from_str(&line).expect("failed to deserialize"); |
| 67 | + if j["reason"] != "compiler-artifact" { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + // Find rlibs in the created file list that match our expected library names and |
| 72 | + // extensions. |
| 73 | + for fpath in j["filenames"].as_array().expect("filenames not an array") { |
| 74 | + let path = fpath.as_str().expect("file name not a string"); |
| 75 | + let path = PathBuf::from(path); |
| 76 | + |
| 77 | + if CHECK_EXTENSIONS.contains(&path.extension().map(|ex| ex.to_str().unwrap())) { |
| 78 | + let fname = path.file_name().unwrap().to_str().unwrap(); |
| 79 | + |
| 80 | + if CHECK_LIBRARIES.iter().any(|lib| fname.contains(lib)) { |
| 81 | + check_files.push(path); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + cmd.wait().expect("failed to wait on Cargo"); |
| 88 | + |
| 89 | + assert!(!check_files.is_empty(), "no compiler_builtins rlibs found"); |
| 90 | + println!("Collected the following rlibs to check: {check_files:#?}"); |
| 91 | + |
| 92 | + check_files |
| 93 | +} |
| 94 | + |
| 95 | +/// Information collected from `object`, for convenience. |
| 96 | +#[expect(unused)] // only for printing |
| 97 | +#[derive(Clone, Debug)] |
| 98 | +struct SymInfo { |
| 99 | + name: String, |
| 100 | + kind: SymbolKind, |
| 101 | + scope: SymbolScope, |
| 102 | + section: SymbolSection, |
| 103 | + is_undefined: bool, |
| 104 | + is_global: bool, |
| 105 | + is_local: bool, |
| 106 | + is_weak: bool, |
| 107 | + is_common: bool, |
| 108 | + address: u64, |
| 109 | + object: String, |
| 110 | +} |
| 111 | + |
| 112 | +impl SymInfo { |
| 113 | + fn new(sym: &Symbol, member: &ArchiveMember) -> Self { |
| 114 | + Self { |
| 115 | + name: sym.name().expect("missing name").to_owned(), |
| 116 | + kind: sym.kind(), |
| 117 | + scope: sym.scope(), |
| 118 | + section: sym.section(), |
| 119 | + is_undefined: sym.is_undefined(), |
| 120 | + is_global: sym.is_global(), |
| 121 | + is_local: sym.is_local(), |
| 122 | + is_weak: sym.is_weak(), |
| 123 | + is_common: sym.is_common(), |
| 124 | + address: sym.address(), |
| 125 | + object: String::from_utf8_lossy(member.name()).into_owned(), |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/// Ensure that the same global symbol isn't defined in multiple object files within an archive. |
| 131 | +/// |
| 132 | +/// Note that this will also locate cases where a symbol is weakly defined in more than one place. |
| 133 | +/// Technically there are no linker errors that will come from this, but it keeps our binary more |
| 134 | +/// straightforward and saves some distribution size. |
| 135 | +fn verify_no_duplicates(path: &Path) { |
| 136 | + let mut syms = BTreeMap::<String, SymInfo>::new(); |
| 137 | + let mut dups = Vec::new(); |
| 138 | + let mut found_any = false; |
| 139 | + |
| 140 | + for_each_symbol(path, |symbol, member| { |
| 141 | + // Only check defined globals |
| 142 | + if !symbol.is_global() || symbol.is_undefined() { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + let sym = SymInfo::new(&symbol, member); |
| 147 | + |
| 148 | + // x86-32 includes multiple copies of thunk symbols |
| 149 | + if sym.name.starts_with("__x86.get_pc_thunk") { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + // Windows has symbols for literal numeric constants, string literals, and MinGW pseudo- |
| 154 | + // relocations. These are allowed to have repeated definitions. |
| 155 | + let win_allowed_dup_pfx = ["__real@", "__xmm@", "??_C@_", ".refptr"]; |
| 156 | + if win_allowed_dup_pfx |
| 157 | + .iter() |
| 158 | + .any(|pfx| sym.name.starts_with(pfx)) |
| 159 | + { |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + match syms.get(&sym.name) { |
| 164 | + Some(existing) => { |
| 165 | + dups.push(sym); |
| 166 | + dups.push(existing.clone()); |
| 167 | + } |
| 168 | + None => { |
| 169 | + syms.insert(sym.name.clone(), sym); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + found_any = true; |
| 174 | + }); |
| 175 | + |
| 176 | + assert!(found_any, "no symbols found"); |
| 177 | + |
| 178 | + if !dups.is_empty() { |
| 179 | + dups.sort_unstable_by(|a, b| a.name.cmp(&b.name)); |
| 180 | + panic!("found duplicate symbols: {dups:#?}"); |
| 181 | + } |
| 182 | + |
| 183 | + println!(" success: no duplicate symbols found"); |
| 184 | +} |
| 185 | + |
| 186 | +/// Ensure that there are no references to symbols from `core` that aren't also (somehow) defined. |
| 187 | +fn verify_core_symbols(path: &Path) { |
| 188 | + let mut defined = BTreeSet::new(); |
| 189 | + let mut undefined = Vec::new(); |
| 190 | + let mut has_symbols = false; |
| 191 | + |
| 192 | + for_each_symbol(path, |symbol, member| { |
| 193 | + has_symbols = true; |
| 194 | + |
| 195 | + // Find only symbols from `core` |
| 196 | + if !symbol.name().unwrap().contains("_ZN4core") { |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + let sym = SymInfo::new(&symbol, member); |
| 201 | + if sym.is_undefined { |
| 202 | + undefined.push(sym); |
| 203 | + } else { |
| 204 | + defined.insert(sym.name); |
| 205 | + } |
| 206 | + }); |
| 207 | + |
| 208 | + assert!(has_symbols, "no symbols found"); |
| 209 | + |
| 210 | + // Discard any symbols that are defined somewhere in the archive |
| 211 | + undefined.retain(|sym| !defined.contains(&sym.name)); |
| 212 | + |
| 213 | + if !undefined.is_empty() { |
| 214 | + undefined.sort_unstable_by(|a, b| a.name.cmp(&b.name)); |
| 215 | + panic!("found undefined symbols from core: {undefined:#?}"); |
| 216 | + } |
| 217 | + |
| 218 | + println!(" success: no undefined references to core found"); |
| 219 | +} |
| 220 | + |
| 221 | +/// For a given archive path, do something with each symbol. |
| 222 | +fn for_each_symbol(path: &Path, mut f: impl FnMut(Symbol, &ArchiveMember)) { |
| 223 | + let data = fs::read(path).expect("reading file failed"); |
| 224 | + let archive = ArchiveFile::parse(data.as_slice()).expect("archive parse failed"); |
| 225 | + for member in archive.members() { |
| 226 | + let member = member.expect("failed to access member"); |
| 227 | + let obj_data = member.data(&*data).expect("failed to access object"); |
| 228 | + let obj = object::File::parse(obj_data).expect("failed to parse object"); |
| 229 | + obj.symbols().for_each(|sym| f(sym, &member)); |
| 230 | + } |
| 231 | +} |
0 commit comments