interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-abi-conformance / tests / c_side.rs

229 lines · 7277 bytes Raw Blame History
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
21use aien_abi_conformance::{check_c, CCheck, Manifest};
22use std::path::PathBuf;
23use std::process::Command;
24
25const MANIFEST: &str = "\
26abi demo 2
27header demo.h
28struct DemoInfo 24 8
29field DemoInfo version 0 4 u32 1
30field DemoInfo rc 4 4 i32 1
31field DemoInfo name 8 4 c_char 4
32field DemoInfo ok 12 1 bool 1
33field DemoInfo total 16 8 u64 1
34const DEMO_ABI_VERSION 2
35const DEMO_E_ARG -1
36fnptr DemoCb fn(*mut c_void, u64) -> i32
37fn demo_open fn(*const c_char, u32, DemoCb, *mut DemoInfo) -> i32
38fn demo_close fn(*mut c_void) -> ()
39";
40
41const HEADER: &str = "\
42#include <stdint.h>
43#include <stdbool.h>
44#define DEMO_ABI_VERSION 2u
45enum { DEMO_OK = 0, DEMO_E_ARG = -1 };
46typedef int (*DemoCb)(void *ctx, uint64_t x);
47typedef struct {
48 uint32_t version;
49 int32_t rc;
50 char name[4];
51 bool ok;
52 uint64_t total;
53} DemoInfo;
54int demo_open(const char *dir, uint32_t n, DemoCb cb, DemoInfo *out);
55void demo_close(void *h);
56";
57
58const SOURCE: &str = "\
59#include \"demo.h\"
60int demo_open(const char *dir, uint32_t n, DemoCb cb, DemoInfo *out) { (void)dir; (void)n; (void)cb; (void)out; return 0; }
61void demo_close(void *h) { (void)h; }
62";
63
64struct Case {
65 dir: PathBuf,
66}
67
68impl 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
107impl Drop for Case {
108 fn drop(&mut self) {
109 let _ = std::fs::remove_dir_all(&self.dir);
110 }
111}
112
113fn 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]
124fn 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]
134fn 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]
146fn 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]
153fn 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]
160fn 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]
167fn 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]
178fn 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]
185fn 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]
192fn 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]
201fn 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]
208fn 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]
225fn 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}