|
| 1 | +// Check that the compiler toolchain (rustc) that we distribute is not using newer glibc |
| 2 | +// symbols than a specified minimum. |
| 3 | +// This test should only be executed on an extracted dist archive or in a dist-* CI job. |
| 4 | + |
| 5 | +//@ only-dist |
| 6 | +//@ only-x86_64-unknown-linux-gnu |
| 7 | +//@ ignore-cross-compile |
| 8 | + |
| 9 | +use std::path::{Path,PathBuf}; |
| 10 | + |
| 11 | +use run_make_support::{cmd, llvm_objdump, regex, rustc_path}; |
| 12 | + |
| 13 | +fnmain(){ |
| 14 | +// This is the maximum glibc version *supported* by the x86_64-unknown-linux-gnu target. |
| 15 | +// All glibc symbols used in the compiler must be lower or equal than this version. |
| 16 | +let max_supported = (2,17,99); |
| 17 | + |
| 18 | +let rustc = PathBuf::from(rustc_path()); |
| 19 | +// Check symbols directly in rustc |
| 20 | +check_symbols(&rustc, max_supported); |
| 21 | + |
| 22 | +// Find dynamic libraries referenced by rustc that come from our lib directory |
| 23 | +let lib_path = rustc.parent().unwrap().parent().unwrap().join("lib"); |
| 24 | +let dynamic_libs = find_dynamic_libs(&rustc) |
| 25 | +.into_iter() |
| 26 | +.filter_map(|path| path.canonicalize().ok()) |
| 27 | +.filter(|lib| lib.starts_with(&lib_path)) |
| 28 | +.collect::<Vec<_>>(); |
| 29 | +for lib in dynamic_libs { |
| 30 | +check_symbols(&lib, max_supported); |
| 31 | +} |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Debug,Ord,PartialOrd,Eq,PartialEq)] |
| 35 | +structGlibcSymbol{ |
| 36 | +name:String, |
| 37 | +version:(u32,u32,u32), |
| 38 | +} |
| 39 | + |
| 40 | +fnfind_dynamic_libs(path:&Path) -> Vec<PathBuf>{ |
| 41 | +cmd("ldd") |
| 42 | +.arg(path) |
| 43 | +.run() |
| 44 | +.stdout_utf8() |
| 45 | +.lines() |
| 46 | +.filter_map(|line| { |
| 47 | +let line = line.trim(); |
| 48 | +letSome((_, line)) = line.split_once(" => ")else{ |
| 49 | +returnNone; |
| 50 | +}; |
| 51 | + line.split_ascii_whitespace().next().map(|path| PathBuf::from(path)) |
| 52 | +}) |
| 53 | +.collect() |
| 54 | +} |
| 55 | + |
| 56 | +fncheck_symbols(file:&Path,max_supported:(u32,u32,u32)){ |
| 57 | +println!("Checking {}", file.display()); |
| 58 | +letmut invalid:Vec<GlibcSymbol> = get_glibc_symbols(file) |
| 59 | +.into_iter() |
| 60 | +.filter(|symbol| symbol.version > max_supported) |
| 61 | +.collect(); |
| 62 | +if !invalid.is_empty(){ |
| 63 | + invalid.sort(); |
| 64 | +panic!( |
| 65 | +"Found invalid glibc symbols in {}:\n{}", |
| 66 | + file.display(), |
| 67 | + invalid |
| 68 | +.into_iter() |
| 69 | +.map(|symbol| format!( |
| 70 | +"{} ({:?} higher than max allowed {:?})", |
| 71 | + symbol.name, symbol.version, max_supported |
| 72 | +)) |
| 73 | +.collect::<Vec<_>>() |
| 74 | +.join("\n") |
| 75 | +) |
| 76 | +} |
| 77 | +} |
| 78 | + |
| 79 | +fnget_glibc_symbols(file:&Path) -> Vec<GlibcSymbol>{ |
| 80 | +let regex = regex::Regex::new(r#"GLIBC_(\d)+\.(\d+)(:?\.(\d+))?"#).unwrap(); |
| 81 | + |
| 82 | +// Uses llvm-objdump, because implementing this using the `object` crate is quite complicated. |
| 83 | +llvm_objdump() |
| 84 | +.arg("-T") |
| 85 | +.arg(file) |
| 86 | +.run() |
| 87 | +.stdout_utf8() |
| 88 | +.lines() |
| 89 | +.filter_map(|line| { |
| 90 | +// Example line |
| 91 | +// 0000000000000000 DF *UND* 0000000000000000 (GLIBC_2.2.5) sbrk |
| 92 | +letmut parts = line.split(" ").collect::<Vec<_>>().into_iter().rev(); |
| 93 | +letSome(name) = parts.next()else{ |
| 94 | +returnNone; |
| 95 | +}; |
| 96 | +letSome(lib) = parts.next()else{ |
| 97 | +returnNone; |
| 98 | +}; |
| 99 | +letSome(version) = regex.captures(lib)else{ |
| 100 | +returnNone; |
| 101 | +}; |
| 102 | +let major = version.get(1).and_then(|m| m.as_str().parse().ok()).unwrap_or(0); |
| 103 | +let minor = version.get(2).and_then(|m| m.as_str().parse().ok()).unwrap_or(0); |
| 104 | +let patch = version.get(3).and_then(|m| m.as_str().parse().ok()).unwrap_or(0); |
| 105 | +Some(GlibcSymbol{version:(major, minor, patch),name: name.to_string()}) |
| 106 | +}) |
| 107 | +.collect() |
| 108 | +} |
0 commit comments