aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-evidence / src / main.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 | //! aienos-evidence CLI. |
| 17 | //! |
| 18 | //! aienos-evidence capture --model PATH --repo NAME=PATH... [--endpoint URL] |
| 19 | //! [--samples N] [--warmup N] [--max-tokens N] |
| 20 | //! [--prompt TEXT] [--out PATH] [--allow-dirty] |
| 21 | //! aienos-evidence verify [BUNDLE] |
| 22 | //! aienos-evidence verify-efi IMAGE... |
| 23 | //! aienos-evidence verify-rollback PRE.json POST.json |
| 24 | //! aienos-evidence models [--endpoint URL] |
| 25 | |
| 26 | use aienos_evidence::{capture, http, pe, rollback, verify}; |
| 27 | use std::path::PathBuf; |
| 28 | use std::process::exit; |
| 29 | |
| 30 | const DEFAULT_BUNDLE: &str = "evidence/config_a_reference_bundle.json"; |
| 31 | const DEFAULT_ENDPOINT: &str = "http://127.0.0.1:18094/v1"; |
| 32 | const USAGE: &str = "usage: |
| 33 | aienos-evidence capture --model PATH --repo NAME=PATH... [--endpoint URL] [--samples N] |
| 34 | [--warmup N] [--max-tokens N] [--prompt TEXT] [--out PATH] [--allow-dirty] |
| 35 | aienos-evidence verify [BUNDLE] |
| 36 | aienos-evidence verify-efi IMAGE... |
| 37 | aienos-evidence verify-rollback PRE.json POST.json |
| 38 | aienos-evidence models [--endpoint URL]"; |
| 39 | |
| 40 | fn fail(msg: impl std::fmt::Display) -> ! { |
| 41 | eprintln!("{msg}"); |
| 42 | exit(1) |
| 43 | } |
| 44 | |
| 45 | struct Args { |
| 46 | flags: Vec<(String, String)>, |
| 47 | switches: Vec<String>, |
| 48 | positional: Vec<String>, |
| 49 | } |
| 50 | |
| 51 | fn parse(args: &[String], switches: &[&str]) -> Args { |
| 52 | let mut a = Args { |
| 53 | flags: vec![], |
| 54 | switches: vec![], |
| 55 | positional: vec![], |
| 56 | }; |
| 57 | let mut i = 0; |
| 58 | while i < args.len() { |
| 59 | let arg = &args[i]; |
| 60 | if switches.contains(&arg.as_str()) { |
| 61 | a.switches.push(arg.clone()); |
| 62 | } else if arg.starts_with("--") { |
| 63 | let value = args |
| 64 | .get(i + 1) |
| 65 | .unwrap_or_else(|| fail(format!("{arg} needs a value\n{USAGE}"))); |
| 66 | a.flags.push((arg.clone(), value.clone())); |
| 67 | i += 1; |
| 68 | } else { |
| 69 | a.positional.push(arg.clone()); |
| 70 | } |
| 71 | i += 1; |
| 72 | } |
| 73 | a |
| 74 | } |
| 75 | |
| 76 | impl Args { |
| 77 | fn get(&self, name: &str) -> Option<&str> { |
| 78 | self.flags |
| 79 | .iter() |
| 80 | .rev() |
| 81 | .find(|(k, _)| k == name) |
| 82 | .map(|(_, v)| v.as_str()) |
| 83 | } |
| 84 | |
| 85 | fn all(&self, name: &str) -> Vec<&str> { |
| 86 | self.flags |
| 87 | .iter() |
| 88 | .filter(|(k, _)| k == name) |
| 89 | .map(|(_, v)| v.as_str()) |
| 90 | .collect() |
| 91 | } |
| 92 | |
| 93 | fn num<T: std::str::FromStr>(&self, name: &str, default: T) -> T { |
| 94 | self.get(name) |
| 95 | .map(|v| { |
| 96 | v.parse() |
| 97 | .unwrap_or_else(|_| fail(format!("{name}: not a number: {v}"))) |
| 98 | }) |
| 99 | .unwrap_or(default) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn cmd_capture(args: &[String]) { |
| 104 | let a = parse(args, &["--allow-dirty"]); |
| 105 | let model = a |
| 106 | .get("--model") |
| 107 | .map(PathBuf::from) |
| 108 | .or_else(|| std::env::var_os("AIENOS_BASELINE_MODEL").map(PathBuf::from)) |
| 109 | .unwrap_or_else(|| fail(format!("--model is required\n{USAGE}"))); |
| 110 | let repos: Vec<(String, PathBuf)> = a |
| 111 | .all("--repo") |
| 112 | .into_iter() |
| 113 | .map(|r| { |
| 114 | let (name, path) = r |
| 115 | .split_once('=') |
| 116 | .unwrap_or_else(|| fail(format!("--repo expects NAME=PATH, got {r}"))); |
| 117 | (name.to_string(), PathBuf::from(path)) |
| 118 | }) |
| 119 | .collect(); |
| 120 | if repos.is_empty() { |
| 121 | fail(format!( |
| 122 | "at least one --repo NAME=PATH is required\n{USAGE}" |
| 123 | )); |
| 124 | } |
| 125 | let opts = capture::Options { |
| 126 | model, |
| 127 | endpoint: a.get("--endpoint").unwrap_or(DEFAULT_ENDPOINT).to_string(), |
| 128 | prompt: a |
| 129 | .get("--prompt") |
| 130 | .unwrap_or(capture::DEFAULT_PROMPT) |
| 131 | .to_string(), |
| 132 | max_tokens: a.num("--max-tokens", 256), |
| 133 | samples: a.num("--samples", 5), |
| 134 | warmup: a.num("--warmup", 1), |
| 135 | repos, |
| 136 | allow_dirty: a.switches.iter().any(|s| s == "--allow-dirty"), |
| 137 | }; |
| 138 | let bundle = capture::capture(&opts).unwrap_or_else(|e| fail(format!("capture failed: {e}"))); |
| 139 | let out = PathBuf::from(a.get("--out").unwrap_or(DEFAULT_BUNDLE)); |
| 140 | if let Some(dir) = out.parent().filter(|d| !d.as_os_str().is_empty()) { |
| 141 | std::fs::create_dir_all(dir).unwrap_or_else(|e| fail(e)); |
| 142 | } |
| 143 | let text = serde_json::to_string_pretty(&bundle).unwrap_or_else(|e| fail(e)) + "\n"; |
| 144 | std::fs::write(&out, text).unwrap_or_else(|e| fail(format!("{}: {e}", out.display()))); |
| 145 | let samples = bundle["reference_workload"]["samples"] |
| 146 | .as_array() |
| 147 | .map_or(0, Vec::len); |
| 148 | println!( |
| 149 | "Captured Config A: {} ({samples} measured samples)", |
| 150 | out.display() |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | fn cmd_verify(args: &[String]) { |
| 155 | let path = args.first().map(String::as_str).unwrap_or(DEFAULT_BUNDLE); |
| 156 | let text = std::fs::read_to_string(path) |
| 157 | .unwrap_or_else(|e| fail(format!("Evidence unavailable: {e}"))); |
| 158 | let bundle: serde_json::Value = |
| 159 | serde_json::from_str(&text).unwrap_or_else(|e| fail(format!("Evidence unreadable: {e}"))); |
| 160 | let report = verify::check(&bundle); |
| 161 | if !report.errors.is_empty() { |
| 162 | for e in &report.errors { |
| 163 | println!("FAIL: {e}"); |
| 164 | } |
| 165 | exit(1); |
| 166 | } |
| 167 | let samples = &bundle["reference_workload"]["samples"]; |
| 168 | let rates: Vec<f64> = samples |
| 169 | .as_array() |
| 170 | .into_iter() |
| 171 | .flatten() |
| 172 | .filter_map(|s| s["decode_tokens_per_second"].as_f64()) |
| 173 | .collect(); |
| 174 | let mean = rates.iter().sum::<f64>() / rates.len().max(1) as f64; |
| 175 | println!( |
| 176 | "Config A capture verified for {}: {} samples, mean decode {mean:.1} tokens/s", |
| 177 | bundle["config_identity"]["host"].as_str().unwrap_or("?"), |
| 178 | rates.len() |
| 179 | ); |
| 180 | if report.m0_open.is_empty() { |
| 181 | println!("M0 closable: yes"); |
| 182 | } else { |
| 183 | println!("M0 closable: no ({})", report.m0_open.join("; ")); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | fn cmd_verify_efi(args: &[String]) { |
| 188 | if args.is_empty() { |
| 189 | fail(USAGE); |
| 190 | } |
| 191 | for path in args { |
| 192 | let data = std::fs::read(path) |
| 193 | .unwrap_or_else(|e| fail(format!("UEFI image verification failed: {path}: {e}"))); |
| 194 | match pe::verify_efi_application(&data) { |
| 195 | Ok(()) => println!("AArch64 PE32+ EFI application verified: {path}"), |
| 196 | Err(e) => fail(format!("UEFI image verification failed: {path}: {e}")), |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /// Native-boot rollback verdict. Exits 0 PASS, 1 FAIL, 3 BLOCKED, 2 when a |
| 202 | /// capture cannot be read or parsed (stdout, as the former verifier printed). |
| 203 | fn cmd_verify_rollback(args: &[String]) { |
| 204 | // Usage errors exit 2, never 1, so they cannot be mistaken for a FAIL verdict. |
| 205 | let [pre, post] = args else { |
| 206 | eprintln!("{USAGE}"); |
| 207 | exit(rollback::EXIT_UNPARSEABLE) |
| 208 | }; |
| 209 | let load = |path: &str| { |
| 210 | rollback::load(path).unwrap_or_else(|e| { |
| 211 | println!("ERROR: Cannot parse {path}: {e}"); |
| 212 | exit(rollback::EXIT_UNPARSEABLE) |
| 213 | }) |
| 214 | }; |
| 215 | let (pre, post) = (load(pre), load(post)); |
| 216 | let (report, verdict) = rollback::verify(&pre, &post); |
| 217 | print!("{report}"); |
| 218 | exit(verdict.exit_code()); |
| 219 | } |
| 220 | |
| 221 | fn cmd_models(args: &[String]) { |
| 222 | let a = parse(args, &[]); |
| 223 | let url = a.get("--endpoint").unwrap_or(DEFAULT_ENDPOINT); |
| 224 | let ep = http::Endpoint::parse(url).unwrap_or_else(|e| fail(e)); |
| 225 | for id in http::model_ids(&ep).unwrap_or_else(|e| fail(format!("{url}: {e}"))) { |
| 226 | println!("{id}"); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | fn main() { |
| 231 | let args: Vec<String> = std::env::args().skip(1).collect(); |
| 232 | let rest = args.get(1..).unwrap_or(&[]); |
| 233 | match args.first().map(String::as_str) { |
| 234 | Some("capture") => cmd_capture(rest), |
| 235 | Some("verify") => cmd_verify(rest), |
| 236 | Some("verify-efi") => cmd_verify_efi(rest), |
| 237 | Some("verify-rollback") => cmd_verify_rollback(rest), |
| 238 | Some("models") => cmd_models(rest), |
| 239 | _ => fail(USAGE), |
| 240 | } |
| 241 | } |