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 / tests / c_side.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 C-side check against small synthetic headers and archives: one compatible case and one |
| 17 | //! negative case per kind of drift the mission names (equal-size reorder, width, signedness, |
| 18 | //! parameter type, ABI version constant, missing symbol, corrupted archive). |
| 19 | //! Needs `cc`, `ar` and `nm` (the same tools the native build needs). |
| 20 | |
| 21 | use aien_abi_conformance::{check_c, CCheck, Manifest}; |
| 22 | use std::path::PathBuf; |
| 23 | use std::process::Command; |
| 24 | |
| 25 | const MANIFEST: &str = "\ |
| 26 | abi demo 2 |
| 27 | header demo.h |
| 28 | struct DemoInfo 24 8 |
| 29 | field DemoInfo version 0 4 u32 1 |
| 30 | field DemoInfo rc 4 4 i32 1 |
| 31 | field DemoInfo name 8 4 c_char 4 |
| 32 | field DemoInfo ok 12 1 bool 1 |
| 33 | field DemoInfo total 16 8 u64 1 |
| 34 | const DEMO_ABI_VERSION 2 |
| 35 | const DEMO_E_ARG -1 |
| 36 | fnptr DemoCb fn(*mut c_void, u64) -> i32 |
| 37 | fn demo_open fn(*const c_char, u32, DemoCb, *mut DemoInfo) -> i32 |
| 38 | fn demo_close fn(*mut c_void) -> () |
| 39 | "; |
| 40 | |
| 41 | const HEADER: &str = "\ |
| 42 | #include <stdint.h> |
| 43 | #include <stdbool.h> |
| 44 | #define DEMO_ABI_VERSION 2u |
| 45 | enum { DEMO_OK = 0, DEMO_E_ARG = -1 }; |
| 46 | typedef int (*DemoCb)(void *ctx, uint64_t x); |
| 47 | typedef struct { |
| 48 | uint32_t version; |
| 49 | int32_t rc; |
| 50 | char name[4]; |
| 51 | bool ok; |
| 52 | uint64_t total; |
| 53 | } DemoInfo; |
| 54 | int demo_open(const char *dir, uint32_t n, DemoCb cb, DemoInfo *out); |
| 55 | void demo_close(void *h); |
| 56 | "; |
| 57 | |
| 58 | const SOURCE: &str = "\ |
| 59 | #include \"demo.h\" |
| 60 | int demo_open(const char *dir, uint32_t n, DemoCb cb, DemoInfo *out) { (void)dir; (void)n; (void)cb; (void)out; return 0; } |
| 61 | void demo_close(void *h) { (void)h; } |
| 62 | "; |
| 63 | |
| 64 | struct Case { |
| 65 | dir: PathBuf, |
| 66 | } |
| 67 | |
| 68 | impl Case { |
| 69 | fn new(tag: &str, header: &str, source: &str) -> Case { |
| 70 | let dir = std::env::temp_dir().join(format!("abi-c-{}-{tag}", std::process::id())); |
| 71 | let _ = std::fs::remove_dir_all(&dir); |
| 72 | std::fs::create_dir_all(&dir).unwrap(); |
| 73 | std::fs::write(dir.join("demo.h"), header).unwrap(); |
| 74 | std::fs::write(dir.join("demo.c"), source).unwrap(); |
| 75 | let ok = Command::new("cc") |
| 76 | .current_dir(&dir) |
| 77 | .args(["-c", "-o", "demo.o", "demo.c"]) |
| 78 | .status() |
| 79 | .unwrap() |
| 80 | .success(); |
| 81 | assert!(ok, "cc demo.c"); |
| 82 | let ok = Command::new("ar") |
| 83 | .current_dir(&dir) |
| 84 | .args(["rcs", "libdemo.a", "demo.o"]) |
| 85 | .status() |
| 86 | .unwrap() |
| 87 | .success(); |
| 88 | assert!(ok, "ar"); |
| 89 | Case { dir } |
| 90 | } |
| 91 | |
| 92 | fn check(&self, manifest: &str) -> Result<String, Vec<String>> { |
| 93 | let m = Manifest::parse(manifest).unwrap(); |
| 94 | let archive = self.dir.join("libdemo.a"); |
| 95 | let work = self.dir.join("probe"); |
| 96 | check_c(&CCheck { |
| 97 | manifest: &m, |
| 98 | root: &self.dir, |
| 99 | includes: vec![], |
| 100 | archive: Some(archive.as_path()), |
| 101 | work: work.as_path(), |
| 102 | cc: "cc".into(), |
| 103 | }) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl Drop for Case { |
| 108 | fn drop(&mut self) { |
| 109 | let _ = std::fs::remove_dir_all(&self.dir); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | fn expect_err(r: Result<String, Vec<String>>, needle: &str) { |
| 114 | match r { |
| 115 | Ok(rep) => panic!("expected a failure mentioning `{needle}`, got PASS:\n{rep}"), |
| 116 | Err(e) => assert!( |
| 117 | e.iter().any(|l| l.contains(needle)), |
| 118 | "no error mentions `{needle}`: {e:#?}" |
| 119 | ), |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn compatible_header_and_archive_pass() { |
| 125 | let c = Case::new("ok", HEADER, SOURCE); |
| 126 | let rep = c.check(MANIFEST).expect("compatible case must pass"); |
| 127 | assert!( |
| 128 | rep.contains("probe: 8 layout/constant facts match"), |
| 129 | "{rep}" |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | #[test] |
| 134 | fn equal_size_reorder_is_caught() { |
| 135 | // version and rc swapped: same size, same alignment, same total |
| 136 | let h = HEADER.replace( |
| 137 | " uint32_t version;\n int32_t rc;", |
| 138 | " int32_t rc;\n uint32_t version;", |
| 139 | ); |
| 140 | let c = Case::new("reorder", &h, SOURCE); |
| 141 | expect_err(c.check(MANIFEST), "reordered"); |
| 142 | expect_err(c.check(MANIFEST), "field DemoInfo version 4 4 u32 1"); |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn width_change_is_caught() { |
| 147 | let h = HEADER.replace("uint32_t version;", "uint64_t version;"); |
| 148 | let c = Case::new("width", &h, SOURCE); |
| 149 | expect_err(c.check(MANIFEST), "struct DemoInfo"); |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn signedness_change_is_caught() { |
| 154 | let h = HEADER.replace("int32_t rc;", "uint32_t rc;"); |
| 155 | let c = Case::new("sign", &h, SOURCE); |
| 156 | expect_err(c.check(MANIFEST), "field DemoInfo rc 4 4 u32 1"); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn bool_to_byte_change_is_caught() { |
| 161 | let h = HEADER.replace("bool ok;", "uint8_t ok;"); |
| 162 | let c = Case::new("bool", &h, SOURCE); |
| 163 | expect_err(c.check(MANIFEST), "field DemoInfo ok 12 1 u8 1"); |
| 164 | } |
| 165 | |
| 166 | #[test] |
| 167 | fn parameter_type_change_is_caught() { |
| 168 | let h = HEADER.replace( |
| 169 | "int demo_open(const char *dir, uint32_t n,", |
| 170 | "int demo_open(const char *dir, uint64_t n,", |
| 171 | ); |
| 172 | let s = SOURCE.replace("uint32_t n", "uint64_t n"); |
| 173 | let c = Case::new("param", &h, &s); |
| 174 | expect_err(c.check(MANIFEST), "fn demo_open: C signature"); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn callback_signature_change_is_caught() { |
| 179 | let h = HEADER.replace("(void *ctx, uint64_t x)", "(void *ctx, uint32_t x)"); |
| 180 | let c = Case::new("cb", &h, SOURCE); |
| 181 | expect_err(c.check(MANIFEST), "fnptr DemoCb"); |
| 182 | } |
| 183 | |
| 184 | #[test] |
| 185 | fn abi_version_mismatch_is_caught() { |
| 186 | let h = HEADER.replace("#define DEMO_ABI_VERSION 2u", "#define DEMO_ABI_VERSION 3u"); |
| 187 | let c = Case::new("ver", &h, SOURCE); |
| 188 | expect_err(c.check(MANIFEST), "const DEMO_ABI_VERSION 3"); |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn removed_field_is_caught_even_when_size_is_kept() { |
| 193 | // `ok` dropped: the struct keeps 24 bytes because of padding |
| 194 | let h = HEADER.replace(" bool ok;\n", ""); |
| 195 | let c = Case::new("drop", &h, SOURCE); |
| 196 | let r = c.check(MANIFEST); |
| 197 | expect_err(r, "reordered"); |
| 198 | } |
| 199 | |
| 200 | #[test] |
| 201 | fn missing_symbol_is_caught() { |
| 202 | let s = SOURCE.replace("void demo_close(void *h) { (void)h; }\n", ""); |
| 203 | let c = Case::new("sym", HEADER, &s); |
| 204 | expect_err(c.check(MANIFEST), "symbol demo_close: not defined"); |
| 205 | } |
| 206 | |
| 207 | #[test] |
| 208 | fn corrupted_archive_is_caught() { |
| 209 | let c = Case::new("corrupt", HEADER, SOURCE); |
| 210 | let a = c.dir.join("libdemo.a"); |
| 211 | let bytes = std::fs::read(&a).unwrap(); |
| 212 | std::fs::write(&a, &bytes[..bytes.len() / 3]).unwrap(); |
| 213 | let r = c.check(MANIFEST); |
| 214 | match r { |
| 215 | Ok(rep) => panic!("truncated archive passed: {rep}"), |
| 216 | Err(e) => assert!( |
| 217 | e.iter() |
| 218 | .any(|l| l.contains("nm failed") || l.contains("not defined")), |
| 219 | "{e:#?}" |
| 220 | ), |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn missing_header_struct_is_caught() { |
| 226 | let c = Case::new("nostruct", HEADER, SOURCE); |
| 227 | let m = format!("{MANIFEST}struct Ghost 8 8\nfield Ghost a 0 8 u64 1\n"); |
| 228 | expect_err(c.check(&m), "struct Ghost: not declared"); |
| 229 | } |