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