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 / manifest.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 | //! The manifest: one line per fact, `#` starts a comment. |
| 17 | //! |
| 18 | //! ```text |
| 19 | //! abi rxc_host 2 name and contract version (informational) |
| 20 | //! header src/runtime/rxc_host_abi.h header(s) the C side is read from, relative to omega |
| 21 | //! struct RxcHostInfo 80 8 name, sizeof, alignment |
| 22 | //! field RxcHostInfo machine_id 5 32 u8 32 struct, field, offset, size, kind, element count |
| 23 | //! const RXC_HOST_ABI_VERSION 2 mirrored constant and its value |
| 24 | //! fnptr RxcHostSkillFn fn(*mut c_void, u64, *mut u64) -> i32 |
| 25 | //! fn rxc_host_open fn(*const c_char, u32, ...) -> i32 |
| 26 | //! ``` |
| 27 | //! |
| 28 | //! Kinds: `bool c_char i8 u8 i16 u16 i32 u32 i64 u64 f32 f64` (`size_t` is `u64` on the |
| 29 | //! LP64 targets this code runs on). Types in signatures use Rust spelling, canonicalised by |
| 30 | //! [`canon_type`] so `c_int`, `int` and `int32_t` all read `i32`. |
| 31 | |
| 32 | use std::collections::BTreeSet; |
| 33 | |
| 34 | #[derive(Debug, Default, Clone)] |
| 35 | pub struct Manifest { |
| 36 | pub abi: Option<(String, String)>, |
| 37 | pub headers: Vec<String>, |
| 38 | /// (name, size, align) |
| 39 | pub structs: Vec<(String, usize, usize)>, |
| 40 | /// (struct, field, offset, size, kind, count), in declaration order per struct. |
| 41 | pub fields: Vec<(String, String, usize, usize, String, usize)>, |
| 42 | pub consts: Vec<(String, i128)>, |
| 43 | pub fnptrs: Vec<(String, String)>, |
| 44 | pub fns: Vec<(String, String)>, |
| 45 | } |
| 46 | |
| 47 | impl Manifest { |
| 48 | pub fn parse(text: &str) -> Result<Manifest, String> { |
| 49 | let mut m = Manifest::default(); |
| 50 | for (i, raw) in text.lines().enumerate() { |
| 51 | let line = raw.split('#').next().unwrap_or("").trim(); |
| 52 | if line.is_empty() { |
| 53 | continue; |
| 54 | } |
| 55 | let bad = |why: &str| format!("manifest line {}: {why}: {raw}", i + 1); |
| 56 | let w: Vec<&str> = line.split_whitespace().collect(); |
| 57 | let num = |s: &str| s.parse::<usize>().map_err(|_| bad("not a number")); |
| 58 | match w[0] { |
| 59 | "abi" if w.len() == 3 => m.abi = Some((w[1].into(), w[2].into())), |
| 60 | "header" if w.len() == 2 => m.headers.push(w[1].into()), |
| 61 | "struct" if w.len() == 4 => m.structs.push((w[1].into(), num(w[2])?, num(w[3])?)), |
| 62 | "field" if w.len() == 7 => m.fields.push(( |
| 63 | w[1].into(), |
| 64 | w[2].into(), |
| 65 | num(w[3])?, |
| 66 | num(w[4])?, |
| 67 | w[5].into(), |
| 68 | num(w[6])?, |
| 69 | )), |
| 70 | "const" if w.len() == 3 => m.consts.push(( |
| 71 | w[1].into(), |
| 72 | w[2].parse::<i128>().map_err(|_| bad("not an integer"))?, |
| 73 | )), |
| 74 | "fnptr" | "fn" if w.len() >= 3 => { |
| 75 | let rest = line[w[0].len()..].trim_start()[w[1].len()..].trim(); |
| 76 | let sig = canon_sig(rest).ok_or_else(|| bad("bad signature"))?; |
| 77 | if w[0] == "fn" { |
| 78 | m.fns.push((w[1].into(), sig)); |
| 79 | } else { |
| 80 | m.fnptrs.push((w[1].into(), sig)); |
| 81 | } |
| 82 | } |
| 83 | _ => return Err(bad("unknown or malformed line")), |
| 84 | } |
| 85 | } |
| 86 | let mut seen = BTreeSet::new(); |
| 87 | for (s, ..) in &m.structs { |
| 88 | if !seen.insert(s.clone()) { |
| 89 | return Err(format!("manifest: struct {s} listed twice")); |
| 90 | } |
| 91 | } |
| 92 | for (s, f, ..) in &m.fields { |
| 93 | if !seen.contains(s) { |
| 94 | return Err(format!("manifest: field {s}.{f} of an unlisted struct")); |
| 95 | } |
| 96 | } |
| 97 | Ok(m) |
| 98 | } |
| 99 | |
| 100 | /// The comparable facts (everything but `abi` and `header`), one canonical line each, |
| 101 | /// in file order. |
| 102 | pub fn fact_lines(&self) -> Vec<String> { |
| 103 | let mut v = Vec::new(); |
| 104 | for (s, size, align) in &self.structs { |
| 105 | v.push(format!("struct {s} {size} {align}")); |
| 106 | for (fs, f, off, sz, kind, n) in &self.fields { |
| 107 | if fs == s { |
| 108 | v.push(format!("field {s} {f} {off} {sz} {kind} {n}")); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | for (n, val) in &self.consts { |
| 113 | v.push(format!("const {n} {val}")); |
| 114 | } |
| 115 | for (n, sig) in &self.fnptrs { |
| 116 | v.push(format!("fnptr {n} {sig}")); |
| 117 | } |
| 118 | for (n, sig) in &self.fns { |
| 119 | v.push(format!("fn {n} {sig}")); |
| 120 | } |
| 121 | v |
| 122 | } |
| 123 | |
| 124 | pub fn field_names(&self, s: &str) -> Vec<String> { |
| 125 | self.fields |
| 126 | .iter() |
| 127 | .filter(|f| f.0 == s) |
| 128 | .map(|f| f.1.clone()) |
| 129 | .collect() |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /// Canonical spelling of one Rust-syntax type: whitespace removed, `Option<..>` (a nullable |
| 134 | /// function pointer has the same ABI as the bare one) and `std::os::raw::` paths dropped, |
| 135 | /// C-alias integers mapped to their fixed-width names, then `*const `/`*mut ` re-spaced. |
| 136 | pub fn canon_type(t: &str) -> String { |
| 137 | let mut s: String = t.chars().filter(|c| !c.is_whitespace()).collect(); |
| 138 | for p in [ |
| 139 | "std::os::raw::", |
| 140 | "core::ffi::", |
| 141 | "std::ffi::", |
| 142 | "::std::os::raw::", |
| 143 | ] { |
| 144 | s = s.replace(p, ""); |
| 145 | } |
| 146 | while let Some(inner) = s.strip_prefix("Option<").and_then(|r| r.strip_suffix('>')) { |
| 147 | s = inner.to_string(); |
| 148 | } |
| 149 | let mut out = String::new(); |
| 150 | let mut rest = s.as_str(); |
| 151 | loop { |
| 152 | if let Some(r) = rest.strip_prefix("*const") { |
| 153 | out.push_str("*const "); |
| 154 | rest = r; |
| 155 | } else if let Some(r) = rest.strip_prefix("*mut") { |
| 156 | out.push_str("*mut "); |
| 157 | rest = r; |
| 158 | } else { |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | let base = match rest { |
| 163 | "c_int" => "i32", |
| 164 | "c_uint" => "u32", |
| 165 | "c_long" | "c_longlong" => "i64", |
| 166 | "c_ulong" | "c_ulonglong" => "u64", |
| 167 | "c_short" => "i16", |
| 168 | "c_ushort" => "u16", |
| 169 | "c_float" => "f32", |
| 170 | "c_double" => "f64", |
| 171 | "c_schar" => "i8", |
| 172 | "c_uchar" => "u8", |
| 173 | "" | "()" => "()", |
| 174 | other => other, |
| 175 | }; |
| 176 | out.push_str(base); |
| 177 | out |
| 178 | } |
| 179 | |
| 180 | /// Canonical signature `fn(a, b) -> r` from either `fn(a, b) -> r`, `(a, b) -> r` or a |
| 181 | /// parameter list plus return type assembled by the callers. |
| 182 | pub fn canon_sig(s: &str) -> Option<String> { |
| 183 | let s = s.trim(); |
| 184 | let s = s.strip_prefix("fn").unwrap_or(s).trim(); |
| 185 | let open = s.find('(')?; |
| 186 | let mut depth = 0i32; |
| 187 | let mut close = None; |
| 188 | for (i, c) in s.char_indices().skip(open) { |
| 189 | match c { |
| 190 | '(' => depth += 1, |
| 191 | ')' => { |
| 192 | depth -= 1; |
| 193 | if depth == 0 { |
| 194 | close = Some(i); |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | _ => {} |
| 199 | } |
| 200 | } |
| 201 | let close = close?; |
| 202 | let params = split_top(&s[open + 1..close]); |
| 203 | let ret = s[close + 1..].trim(); |
| 204 | let ret = ret.strip_prefix("->").map(str::trim).unwrap_or("()"); |
| 205 | Some(make_sig(¶ms, ret)) |
| 206 | } |
| 207 | |
| 208 | pub(crate) fn make_sig<S: AsRef<str>>(params: &[S], ret: &str) -> String { |
| 209 | let ps: Vec<String> = params |
| 210 | .iter() |
| 211 | .map(|p| canon_type(p.as_ref())) |
| 212 | .filter(|p| p != "()") |
| 213 | .collect(); |
| 214 | format!("fn({}) -> {}", ps.join(", "), canon_type(ret)) |
| 215 | } |
| 216 | |
| 217 | /// Split on commas that are not inside `<>`, `()` or `[]`. |
| 218 | pub(crate) fn split_top(s: &str) -> Vec<String> { |
| 219 | let mut out = Vec::new(); |
| 220 | let mut depth = 0i32; |
| 221 | let mut cur = String::new(); |
| 222 | for c in s.chars() { |
| 223 | match c { |
| 224 | '<' | '(' | '[' => depth += 1, |
| 225 | '>' | ')' | ']' => depth -= 1, |
| 226 | ',' if depth == 0 => { |
| 227 | out.push(cur.trim().to_string()); |
| 228 | cur.clear(); |
| 229 | continue; |
| 230 | } |
| 231 | _ => {} |
| 232 | } |
| 233 | cur.push(c); |
| 234 | } |
| 235 | if !cur.trim().is_empty() { |
| 236 | out.push(cur.trim().to_string()); |
| 237 | } |
| 238 | out |
| 239 | } |
| 240 | |
| 241 | #[cfg(test)] |
| 242 | mod tests { |
| 243 | use super::*; |
| 244 | |
| 245 | #[test] |
| 246 | fn canonical_types() { |
| 247 | assert_eq!(canon_type("* const c_char"), "*const c_char"); |
| 248 | assert_eq!(canon_type("*mut *mut RxcHost"), "*mut *mut RxcHost"); |
| 249 | assert_eq!(canon_type("Option<RxcHostVerifyFn>"), "RxcHostVerifyFn"); |
| 250 | assert_eq!(canon_type("c_int"), "i32"); |
| 251 | assert_eq!(canon_type(""), "()"); |
| 252 | assert_eq!( |
| 253 | canon_sig("fn(*mut c_void,u64, * mut u64)->c_int").unwrap(), |
| 254 | "fn(*mut c_void, u64, *mut u64) -> i32" |
| 255 | ); |
| 256 | assert_eq!(canon_sig("fn()").unwrap(), "fn() -> ()"); |
| 257 | } |
| 258 | |
| 259 | #[test] |
| 260 | fn parse_round_trip_and_refusals() { |
| 261 | let t = "abi x 2\nheader a.h\nstruct S 8 4\nfield S a 0 4 u32 1\nfield S b 4 4 i32 1\nconst K -3\nfn f fn(u32) -> i32\n"; |
| 262 | let m = Manifest::parse(t).unwrap(); |
| 263 | assert_eq!(m.fact_lines().len(), 5); |
| 264 | assert_eq!(m.field_names("S"), ["a", "b"]); |
| 265 | assert!(Manifest::parse("field T a 0 4 u32 1\n").is_err()); |
| 266 | assert!(Manifest::parse("struct S 8\n").is_err()); |
| 267 | assert!(Manifest::parse("struct S 8 4\nstruct S 8 4\n").is_err()); |
| 268 | } |
| 269 | } |