aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-abi-conformance / src / identity.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Native archive identity (used from `build.rs`). |
| 17 | //! |
| 18 | //! `native-artifacts.lock` (repository root) records the expected sha256 of each archive built |
| 19 | //! from the pinned omega source, per target triple and C compiler, one line each: |
| 20 | //! |
| 21 | //! ```text |
| 22 | //! <archive> <omega-sha> <target> <sha256> <cc --version, first line> |
| 23 | //! ``` |
| 24 | //! |
| 25 | //! omega's archives are byte-reproducible for a given source, compiler and target (the build |
| 26 | //! writes no paths or times into them), so an entry is a real expectation: a built archive that |
| 27 | //! differs from its entry fails the build. A combination with no entry builds, and is reported |
| 28 | //! as `digest unrecorded`, which the qualification entry point does not accept as PASS. |
| 29 | //! A prebuilt archive (`*_LIB`) must come with its expected digest (`*_LIB_SHA256`). |
| 30 | |
| 31 | use std::path::Path; |
| 32 | use std::process::Command; |
| 33 | |
| 34 | pub fn sha256_file(path: &Path) -> Result<String, String> { |
| 35 | let out = Command::new("sha256sum") |
| 36 | .arg(path) |
| 37 | .output() |
| 38 | .map_err(|e| format!("cannot run sha256sum on {}: {e}", path.display()))?; |
| 39 | if !out.status.success() { |
| 40 | return Err(format!("sha256sum failed on {}", path.display())); |
| 41 | } |
| 42 | let text = String::from_utf8_lossy(&out.stdout); |
| 43 | let d = text.split_whitespace().next().unwrap_or("").to_string(); |
| 44 | if is_sha256(&d) { |
| 45 | Ok(d) |
| 46 | } else { |
| 47 | Err(format!( |
| 48 | "sha256sum printed no digest for {}", |
| 49 | path.display() |
| 50 | )) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub fn is_sha256(s: &str) -> bool { |
| 55 | s.len() == 64 && s.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) |
| 56 | } |
| 57 | |
| 58 | /// First line of `<cc> --version` (the compiler identity recorded with each digest). |
| 59 | pub fn cc_version(cc: &str) -> String { |
| 60 | Command::new(cc) |
| 61 | .arg("--version") |
| 62 | .output() |
| 63 | .ok() |
| 64 | .and_then(|o| { |
| 65 | String::from_utf8_lossy(&o.stdout) |
| 66 | .lines() |
| 67 | .next() |
| 68 | .map(|l| l.trim().to_string()) |
| 69 | }) |
| 70 | .unwrap_or_else(|| "unknown".into()) |
| 71 | } |
| 72 | |
| 73 | /// One `native-artifacts.lock` entry. |
| 74 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 75 | pub struct LockEntry { |
| 76 | pub archive: String, |
| 77 | pub omega: String, |
| 78 | pub target: String, |
| 79 | pub sha256: String, |
| 80 | pub cc: String, |
| 81 | } |
| 82 | |
| 83 | pub fn parse_lock(text: &str) -> Result<Vec<LockEntry>, String> { |
| 84 | let mut v = Vec::new(); |
| 85 | for (i, raw) in text.lines().enumerate() { |
| 86 | let line = raw.trim(); |
| 87 | if line.is_empty() || line.starts_with('#') { |
| 88 | continue; |
| 89 | } |
| 90 | let w: Vec<&str> = line.splitn(5, ' ').collect(); |
| 91 | if w.len() != 5 || !is_sha256(w[3]) || w[1].len() != 40 { |
| 92 | return Err(format!( |
| 93 | "native-artifacts.lock line {}: want `<archive> <omega-sha> <target> <sha256> <cc version>`: {raw}", |
| 94 | i + 1 |
| 95 | )); |
| 96 | } |
| 97 | v.push(LockEntry { |
| 98 | archive: w[0].into(), |
| 99 | omega: w[1].into(), |
| 100 | target: w[2].into(), |
| 101 | sha256: w[3].into(), |
| 102 | cc: w[4].trim().into(), |
| 103 | }); |
| 104 | } |
| 105 | Ok(v) |
| 106 | } |
| 107 | |
| 108 | /// The recorded digest for this archive, omega commit, target and compiler, if any. |
| 109 | pub fn expected<'a>( |
| 110 | lock: &'a [LockEntry], |
| 111 | archive: &str, |
| 112 | omega: &str, |
| 113 | target: &str, |
| 114 | cc: &str, |
| 115 | ) -> Option<&'a LockEntry> { |
| 116 | lock.iter() |
| 117 | .find(|e| e.archive == archive && e.omega == omega && e.target == target && e.cc == cc) |
| 118 | } |
| 119 | |
| 120 | /// Outcome of the digest check for an archive built from source. |
| 121 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 122 | pub enum Digest { |
| 123 | /// matches its `native-artifacts.lock` entry |
| 124 | Locked, |
| 125 | /// no entry for this (archive, omega, target, compiler) |
| 126 | Unrecorded, |
| 127 | } |
| 128 | |
| 129 | /// Compare a built archive's digest with the lock. `Err` = a recorded digest that differs. |
| 130 | pub fn check_built( |
| 131 | lock: &[LockEntry], |
| 132 | archive: &str, |
| 133 | omega: &str, |
| 134 | target: &str, |
| 135 | cc: &str, |
| 136 | actual: &str, |
| 137 | ) -> Result<Digest, String> { |
| 138 | match expected(lock, archive, omega, target, cc) { |
| 139 | Some(e) if e.sha256 == actual => Ok(Digest::Locked), |
| 140 | Some(e) => Err(format!( |
| 141 | "{archive} built from omega {omega} for {target} with `{cc}` has sha256 {actual}, native-artifacts.lock expects {}", |
| 142 | e.sha256 |
| 143 | )), |
| 144 | None => Ok(Digest::Unrecorded), |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /// A prebuilt archive must name its expected digest, and match it. |
| 149 | pub fn check_prebuilt(path: &Path, expected: Option<&str>, var: &str) -> Result<String, String> { |
| 150 | let Some(want) = expected else { |
| 151 | return Err(format!( |
| 152 | "{} is a prebuilt archive with no expected digest: set {var}=<sha256> (sha256sum {})", |
| 153 | path.display(), |
| 154 | path.display() |
| 155 | )); |
| 156 | }; |
| 157 | let want = want.trim().to_ascii_lowercase(); |
| 158 | if !is_sha256(&want) { |
| 159 | return Err(format!("{var} must be 64 lowercase hex characters")); |
| 160 | } |
| 161 | let got = sha256_file(path)?; |
| 162 | if got != want { |
| 163 | return Err(format!( |
| 164 | "{} has sha256 {got}, {var} expects {want} (wrong, rebuilt, truncated or corrupted archive)", |
| 165 | path.display() |
| 166 | )); |
| 167 | } |
| 168 | Ok(got) |
| 169 | } |
| 170 | |
| 171 | #[cfg(test)] |
| 172 | mod tests { |
| 173 | use super::*; |
| 174 | |
| 175 | const A: &str = "3eb20f5ec60b465413320000000000000000000000000000000000000000abcd"; |
| 176 | |
| 177 | #[test] |
| 178 | fn lock_lookup_and_mismatch() { |
| 179 | let o = "6c6180cf378075b61291f4565d226eba38b4decd"; |
| 180 | let text = format!("# c\nlibx.a {o} aarch64-unknown-linux-gnu {A} cc (X) 1.2\n"); |
| 181 | let l = parse_lock(&text).unwrap(); |
| 182 | let t = "aarch64-unknown-linux-gnu"; |
| 183 | assert_eq!( |
| 184 | check_built(&l, "libx.a", o, t, "cc (X) 1.2", A), |
| 185 | Ok(Digest::Locked) |
| 186 | ); |
| 187 | assert_eq!( |
| 188 | check_built(&l, "libx.a", o, t, "cc (X) 1.3", A), |
| 189 | Ok(Digest::Unrecorded) |
| 190 | ); |
| 191 | let bad = A.replace('a', "b"); |
| 192 | assert!(check_built(&l, "libx.a", o, t, "cc (X) 1.2", &bad).is_err()); |
| 193 | assert!(parse_lock("libx.a short aarch64 abc cc\n").is_err()); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn prebuilt_needs_and_checks_a_digest() { |
| 198 | let dir = std::env::temp_dir().join(format!("abi-id-{}", std::process::id())); |
| 199 | std::fs::create_dir_all(&dir).unwrap(); |
| 200 | let f = dir.join("lib.a"); |
| 201 | std::fs::write(&f, b"!<arch>\n").unwrap(); |
| 202 | let real = sha256_file(&f).unwrap(); |
| 203 | assert!(check_prebuilt(&f, None, "X_SHA256").is_err()); |
| 204 | assert_eq!(check_prebuilt(&f, Some(&real), "X_SHA256").unwrap(), real); |
| 205 | // truncated / corrupted: same name, different bytes |
| 206 | std::fs::write(&f, b"!<arch").unwrap(); |
| 207 | assert!(check_prebuilt(&f, Some(&real), "X_SHA256").is_err()); |
| 208 | assert!(check_prebuilt(&f, Some("nothex"), "X_SHA256").is_err()); |
| 209 | let _ = std::fs::remove_dir_all(&dir); |
| 210 | } |
| 211 | } |