Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193//! Compile-time syscall-number gate.//!//! Re-derives every `abi::nr::*` constant from the kernel syscall tables in//! the pinned kernel tree (`third_party/linux/`, the gitlink-locked kernel//! source the Makefile's `kernel-provenance` target asserts) and fails the//! build on any mismatch. This moves the `kernel_source` ground-truth check//! from test time to build time: a nit binary whose syscall numbers disagree//! with the pinned kernel table cannot be built at all, even on a machine//! with no kernel tree.//!//! The check is one-way on purpose: nit ships a *closed set* of syscalls//! (enforced by `tests/audit.rs`), so the table may contain entries nit does//! not use; every name nit does use must match the table's number. The deeper//! pin against the full kernel tree (`tests/kernel_source.rs`) stays.//!//! The parsers below mirror `tests/common/mod.rs` (abi.rs) and//! `tests/kernel_source.rs` (the .tbl files) exactly; if one changes, change//! both.
use std::collections::BTreeMap;use std::path::Path;
fn main() { let manifest = Path::new(env!("CARGO_MANIFEST_DIR")); // The .tbl tables live in the pinned kernel tree (third_party/linux, the // gitlink-locked submodule). On a normal host checkout that tree is // present and is the authoritative source. In the container, though, the // 1.8G tree never enters the build context (.dockerignore excludes it); // only the ground-truth subset (third_party/linux-subset, blob-checked // against the tree by `make kernel-subset`) rides the `ADD . /src` layer, // and it carries these same two tables. So the table root prefers the full // tree and falls back to the subset -- a nit binary whose numbers drift // from the pinned table cannot be built in either environment. let x86_tbl_rel = "arch/x86/entry/syscalls/syscall_64.tbl"; let a64_tbl_rel = "scripts/syscall.tbl"; let tree = manifest.join("third_party/linux"); let subset = manifest.join("third_party/linux-subset"); let table_root = if tree.join(x86_tbl_rel).is_file() { tree } else if subset.join(x86_tbl_rel).is_file() { subset } else { tree }; for rel in [x86_tbl_rel, a64_tbl_rel] { println!("cargo:rerun-if-changed={}", table_root.join(rel).display()); } println!("cargo:rerun-if-changed=src/abi.rs");
let abi_src = std::fs::read_to_string(manifest.join("src/abi.rs")) .unwrap_or_else(|e| panic!("build gate: cannot read src/abi.rs: {e}")); let (x86, a64) = parse_abi_blocks(&abi_src); if x86.is_empty() || a64.is_empty() { panic!("build gate: parsed no nr constants from src/abi.rs (did the module move?)"); } let x86_tbl_path = table_root.join(x86_tbl_rel); let a64_tbl_path = table_root.join(a64_tbl_rel); // Fail-closed: without the kernel tree the gate cannot run, and a build // that skipped the gate is worse than no build (the precedent is // tests/kernel_source.rs's skip-loudly rule). let read_tbl = |path: &Path| -> String { std::fs::read_to_string(path).unwrap_or_else(|e| { panic!( "build gate: cannot read kernel syscall table {}: {}\n\ The kernel submodule is not initialized. Run:\n\ git submodule update --init\n\ (expected kernel pin: v7.2.4, see the Makefile's KERNEL_PIN)", path.display(), e ) }) }; let x86_tbl = parse_tbl(&read_tbl(&x86_tbl_path)); let a64_tbl = parse_tbl(&read_tbl(&a64_tbl_path));
// Collect the mismatches and fail the build with the full list: // `compile_error!` is emitted as a macro invocation string, so build the // message first and expand it once. let mut bad: Vec<String> = Vec::new(); for (name, value) in &x86 { match x86_tbl.get(name) { Some(t) if *t == *value => {} Some(t) => bad.push(format!("x86_64 {name}: abi.rs = {value}, kernel table = {t}")), None => bad.push(format!("x86_64 {name}: not in the kernel table")), } } for (name, value) in &a64 { match a64_tbl.get(name) { Some(t) if *t == *value => {} Some(t) => bad.push(format!("aarch64 {name}: abi.rs = {value}, kernel table = {t}")), None => bad.push(format!("aarch64 {name}: not in the kernel table")), } } if !bad.is_empty() { let msg = format!( "syscall numbers in src/abi.rs do not match the pinned kernel 7.2.4 tables:\n {}", bad.join("\n ") ); // A `compile_error!` with a dynamic string is not possible; the // build script's own failure is the gate (same effect: no build). eprintln!("error: {msg}"); std::process::exit(1); }}
/// Parse `src/abi.rs` into per-arch `name -> number` for the `nr` module./// Mirrors `tests/common/mod.rs::parse_abi_blocks` exactly.fn parse_abi_blocks(src: &str) -> (BTreeMap<String, u64>, BTreeMap<String, u64>) { let mut x86: BTreeMap<String, u64> = BTreeMap::new(); let mut a64: BTreeMap<String, u64> = BTreeMap::new(); let mut in_nr = false; let mut arch: Option<&'static str> = None;
for line in src.lines() { let t = line.trim(); if !in_nr { if t == "pub mod nr {" { in_nr = true; } continue; } if t == "}" { break; } if t == "#[cfg(target_arch = \"x86_64\")]" { arch = Some("x86_64"); continue; } if t == "#[cfg(target_arch = \"aarch64\")]" { arch = Some("aarch64"); continue; } if let Some(rest) = t.strip_prefix("pub const ") { let Some((name, rest2)) = rest.split_once(':') else { continue; }; let name = name.trim(); if name.is_empty() { continue; } let Some((sig, value)) = rest2.split_once(" = ") else { continue; }; if !sig.trim().ends_with("u64") { continue; } let value: u64 = match value.trim().trim_end_matches(';').trim().parse() { Ok(v) => v, Err(_) => panic!("abi.rs: unparseable value in `{t:?}`"), }; match arch { Some("x86_64") => { x86.insert(name.to_string(), value); } Some("aarch64") => { a64.insert(name.to_string(), value); } _ => {} } } } (x86, a64)}
/// Parse a kernel syscall table (`syscall_64.tbl` / `scripts/syscall.tbl`)/// into `name -> number`, keeping only the native 64-bit rows. Mirrors/// `tests/kernel_source.rs::parse_tbl` exactly.fn parse_tbl(text: &str) -> BTreeMap<String, u64> { let mut m: BTreeMap<String, u64> = BTreeMap::new(); for line in text.lines() { let line = line.trim_start(); if line.is_empty() || line.starts_with('#') { continue; } let cols: Vec<&str> = line.split_whitespace().collect(); if cols.len() < 3 { continue; } let (nr, abi_label, name) = (cols[0], cols[1], cols[2]); if abi_label != "common" && abi_label != "64" { continue; } let nr: u64 = match nr.parse() { Ok(v) => v, Err(_) => panic!("kernel .tbl: unparseable number on line: {line:?}"), }; if m.insert(name.to_string(), nr).is_some() { panic!("kernel .tbl: duplicate name {name}"); } } m}