aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
GitHub aien-dev/aienux#305 (code only)
Apply it
git checkout -b patch-5ca3247c20ecb46d 792fa718dd2c05e3c5a7e13a2ff738543e5adf27 curl -fsSL https://interplane.io/drakestapleton/aienux/patches/5ca3247c20ecb46d.patch | git am
Provenance
github-import 1
aien-dev/aienux
305
30d666b8af21a4a8b13c3d80f9476377b28d837b
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":305,"url":"https://github.com/aien-dev/aienux/pull/305","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:10Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 76d73930..eeea1794 100644 --- a/.crumb +++ b/.crumb @@ -21,39 +21,39 @@ "generated": { "children": [ { - "digest": "ebc8d5c8bacd11bfcc717785e74cb4de592e0aab8f41a6f81f75f4cda158dc21", - "last_commit": "3761935", + "digest": "e4f7c9f4b54d792a3d410c4143742cd0f95b64858494d6bfa9ffc6cc9ff7687a", + "last_commit": "362cba9", "name": "crates", "purpose": "Directory crates. Purpose not yet described by a human." }, { - "digest": "3ee624a79df42f92ed58adfd80a511bc2f68dad065fd2cd8b8ce97176879fda0", - "last_commit": "3b2004b", + "digest": "4a006f504e49f1586a9591657f799ab634dd4b8256fbbed233d2bfbf541a7907", + "last_commit": "362cba9", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "e6450fb9b838f64fcc90113926436887241ae8a12ceb5523954818b3d986b281", - "last_commit": "863bef8", + "digest": "6e42346a1b795fb11cb1fe616b7e144e92002110f9de17fbfe18efab8843fde0", + "last_commit": "34d6878", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "768f7a9a329815979e95b540161c74912dddd8615f44f45c44f97db5d49fdd93", - "last_commit": "863bef8", + "digest": "9cb7696ff81e7d3e7cc312f7249aba2f0a7e7345b9ba7b80a3ab4515bbad10d9", + "last_commit": "219a061", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:eb85eae85331edd701f4123676822a283f17968b79937a88acc0fb6a6887102f", - "compiled_at": "2026-10-11T20:44:01Z", + "children_root": "sha256:1540413c18548a793b8a0ccd8641b1b4ac2dc9c0b1054dba818f7206d801e992", + "compiled_at": "2026-10-11T21:20:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:7731185c351879aa5df3887c52ce94859b52b79d2eff491da5d19fa33e916fd6", + "digest": "sha256:50df40dfa027ff79cf79a85ae9ffb9f83164f8de53001a363a35869f86f8c609", "evidence_files": 155, "evidence_root": "sha256:d59c0cef4e7dd5172923754a95b411ca54b01f35baca4587f792f1b259f1eaf7", - "files": 3065, - "generated_at_commit": "53b68c39fd800821a619436b7a723e79d5b6ea8a", - "source_tree": "sha256:3cbd2ab91fd873b9a86a2655ab003cf09d8a3270bb55cf45df0bf26436b5ab10" + "files": 3067, + "generated_at_commit": "362cba9653c8021996a21b17ead00d5d1140b371", + "source_tree": "sha256:8e0cdae2c426f575f843943b3a3d88eb7afe8a8692c43a9668218f99cd01a8b4" }, "owner_lane": "", "provenance": { diff --git a/.github/workflows/native-suites.yml b/.github/workflows/native-suites.yml index 1a44cb27..9fa36806 100644 --- a/.github/workflows/native-suites.yml +++ b/.github/workflows/native-suites.yml @@ -2,8 +2,11 @@ # only compiled these into the kernel image; nothing ran their own tests. # # Coverage map (what runs where): -# native/capability test ci.yml (ci.yml is disabled_manually on GitHub, so this does NOT run in CI today) +# native/capability test ci.yml (disabled_manually on GitHub) and, since abi-qual, the capability-abi job below # native/capability mutants this file +# native/capability test + ABI isolation this file (capability-abi job: C rule tests, +# Rust aienos-capability{,-ffi} fmt/clippy/test, and +# scripts/cap_abi_isolation_test.sh) # native/store test mutants ci.yml (ci.yml is disabled_manually on GitHub, so this does NOT run in CI today) # native/kernel test sanitize + gates ck-kernel.yml (owned by another lane) # native/net test sanitize mutants this file @@ -34,6 +37,9 @@ on: - ".github/workflows/native-suites.yml" - "scripts/qemu_native_nvme_test.sh" - "scripts/lib_gate_hold.sh" + - "scripts/cap_abi_isolation_test.sh" + - "crates/aienos-capability/**" + - "crates/aienos-capability-ffi/**" workflow_dispatch: permissions: @@ -89,6 +95,55 @@ jobs: done if grep -q 'NOT_RUN' suite.log; then echo 'NOT_RUN in log'; exit 1; fi + capability-abi: + # The native C authority (aienos_cap_*, 64-bit generation) is the + # production ABI; the Rust crates are a separate 32-bit implementation + # exported as aienos_cap32_*. This job keeps them from becoming + # interchangeable by accident and runs both sides' rule tests. + name: native/capability ABI isolation (C test, Rust tests, symbol/link checks) + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install C toolchain + run: | + sudo apt-get update + sudo apt-get install -y build-essential + cc --version | head -1 + + - uses: dtolnay/rust-toolchain@stable + with: + components: clippy, rustfmt + + - name: C rule tests (make -C native/capability test) + run: | + set -o pipefail + make -C native/capability test 2>&1 | tee cap-test.log + grep -qE 'capability: [0-9]+ checks, 0 failures' cap-test.log + grep -q 'contain-checks PASS' cap-test.log + + - name: Rust fmt + run: cargo fmt -p aienos-capability -p aienos-capability-ffi -- --check + + - name: Rust clippy + run: cargo clippy --locked -p aienos-capability -p aienos-capability-ffi --all-targets -- -D warnings + + - name: Rust tests + run: | + set -o pipefail + cargo test --locked -p aienos-capability -p aienos-capability-ffi 2>&1 | tee rust-test.log + if grep -q 'test result: FAILED' rust-test.log; then exit 1; fi + grep -q 'reclaimed_reference_stays_dead_after_restart ... ok' rust-test.log + grep -q 'layout_matches_the_cap32_contract ... ok' rust-test.log + + - name: ABI isolation (symbols, link probe, layout drift) + run: | + set -o pipefail + bash scripts/cap_abi_isolation_test.sh 2>&1 | tee abi.log + grep -qx 'AIENOS_CAP_ABI_ISOLATION: PASS' abi.log + disk-qemu: name: native/disk qemu NVMe payload runs-on: ubuntu-latest diff --git a/README.md b/README.md index 4b459de5..fdfff911 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,8 @@ The C kernel needs only gcc, binutils, make and a shell. Host tests need nothing ```bash make -C native/kernel test # C kernel host tests make -C native/kernel sanitize # same under ASan and UBSan -make -C native/capability test # capability authority +make -C native/capability test # capability authority (production ABI; docs/CAPABILITY_ABI.md) +bash scripts/cap_abi_isolation_test.sh # C and Rust capability ABIs stay separate make -C native/store test mutants # torn-write-safe Store record ``` diff --git a/crates/.crumb b/crates/.crumb index b65d0d54..546b784b 100644 --- a/crates/.crumb +++ b/crates/.crumb @@ -96,7 +96,7 @@ }, { "digest": "41559b6187a30de12fdf5ee7e749662f3576d60e4bf0e0753284c05aa64baec0", - "last_commit": "7acfbcf", + "last_commit": "3761935", "name": "aienos-boot", "purpose": "Directory crates/aienos-boot. Purpose not yet described by a human." }, @@ -107,14 +107,14 @@ "purpose": "Directory crates/aienos-c1-tree. Purpose not yet described by a human." }, { - "digest": "8a0463de3e20f4781afc41b8f7b354a29a3b819b2a88a8588b62c2ae3a29ebef", - "last_commit": "187ecdf", + "digest": "ca15c2b9ae570b0aa2c7859672de2c976d6ce046188c09b7c74b5ccbee350136", + "last_commit": "362cba9", "name": "aienos-capability", "purpose": "Directory crates/aienos-capability. Purpose not yet described by a human." }, { - "digest": "ddcab59814a675942fbea09cd734c6d9726359da319efd962a9abd1a4949e9af", - "last_commit": "e3ec8a5", + "digest": "c63ab3d5643ac3c0974eee276273a0ab80584c54926cbb62c637294b5b857718", + "last_commit": "6d17b22", "name": "aienos-capability-ffi", "purpose": "Directory crates/aienos-capability-ffi. Purpose not yet described by a human." }, @@ -149,15 +149,15 @@ "purpose": "Directory crates/aienos-store-tool. Purpose not yet described by a human." } ], - "children_root": "sha256:1334b8befdc1f899fa2171246a25c0dcb502d6ea6bb724f007e22b884f6040c3", - "compiled_at": "2026-10-07T01:17:46Z", + "children_root": "sha256:006b47340025796d925d29b5143f8c38760bb782916323e4d9070a8e408f7033", + "compiled_at": "2026-10-11T21:20:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:ebc8d5c8bacd11bfcc717785e74cb4de592e0aab8f41a6f81f75f4cda158dc21", + "digest": "sha256:e4f7c9f4b54d792a3d410c4143742cd0f95b64858494d6bfa9ffc6cc9ff7687a", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 231, - "generated_at_commit": "7acfbcf2acfbc8c505a60d0e20cebf4d5f963c00", - "source_tree": "sha256:dd6b4a3aade84dd3a50986ad97114e1338b5e3b820119356f4ffbb599e099e0b" + "generated_at_commit": "362cba9653c8021996a21b17ead00d5d1140b371", + "source_tree": "sha256:7d853b8e4348ea1aaebc7c18c71e06d2bd272911acc2e4861fd01a2589a66ad0" }, "owner_lane": "", "provenance": { diff --git a/crates/aienos-capability-ffi/.crumb b/crates/aienos-capability-ffi/.crumb index cef59d0a..ebbd11c1 100644 --- a/crates/aienos-capability-ffi/.crumb +++ b/crates/aienos-capability-ffi/.crumb @@ -13,21 +13,21 @@ "generated": { "children": [ { - "digest": "787370cdc08874f27a8e87c34a46b17011913f6ab01ea412c103129d24482953", - "last_commit": "e3ec8a5", + "digest": "1cb093121e30c7650490bef5f1b0d9d7daeba6b02576ceb70c063924375e8458", + "last_commit": "6d17b22", "name": "src", "purpose": "Directory crates/aienos-capability-ffi/src. Purpose not yet described by a human." } ], - "children_root": "sha256:3a5fc94d92942610ce31824c2157e369a5c983246a898d54728c8749a3bc31c7", - "compiled_at": "2026-10-04T13:47:18Z", + "children_root": "sha256:951f011130547622847c6176f56210f3d984788efdbbfecc80a553721dfef1bb", + "compiled_at": "2026-10-11T21:20:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:ddcab59814a675942fbea09cd734c6d9726359da319efd962a9abd1a4949e9af", + "digest": "sha256:c63ab3d5643ac3c0974eee276273a0ab80584c54926cbb62c637294b5b857718", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 2, - "generated_at_commit": "a863d5c1f5bd89c88c5f6671eecfd27b599ee512", - "source_tree": "sha256:eeae684ddb4213d1a9af41a15c6662c39f573f2182b0121620605d7bdc429009" + "generated_at_commit": "362cba9653c8021996a21b17ead00d5d1140b371", + "source_tree": "sha256:96faad31a59d21e328ed818589933b387d5487b69b7e046ee5c40ed8a9574e4e" }, "owner_lane": "", "provenance": { diff --git a/crates/aienos-capability-ffi/Cargo.toml b/crates/aienos-capability-ffi/Cargo.toml index ef56ab93..75430b7d 100644 --- a/crates/aienos-capability-ffi/Cargo.toml +++ b/crates/aienos-capability-ffi/Cargo.toml @@ -5,7 +5,7 @@ edition.workspace = true authors.workspace = true license.workspace = true repository.workspace = true -description = "Host C binding for the native AIENOS capability authority." +description = "C binding (aienos_cap32_* symbols) for the 32-bit-generation Rust capability authority. Not the native AIENOS capability ABI." [lib] crate-type = ["staticlib", "rlib"] diff --git a/crates/aienos-capability-ffi/src/.crumb b/crates/aienos-capability-ffi/src/.crumb index be3055b5..69258cfd 100644 --- a/crates/aienos-capability-ffi/src/.crumb +++ b/crates/aienos-capability-ffi/src/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T13:47:18Z", + "compiled_at": "2026-10-11T21:20:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:787370cdc08874f27a8e87c34a46b17011913f6ab01ea412c103129d24482953", + "digest": "sha256:1cb093121e30c7650490bef5f1b0d9d7daeba6b02576ceb70c063924375e8458", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 1, - "generated_at_commit": "a863d5c1f5bd89c88c5f6671eecfd27b599ee512", - "source_tree": "sha256:87b3a57ce93efc4dd9f8c51b53fe7b2b49d81716c62e7a872f1214ce1c757188" + "generated_at_commit": "362cba9653c8021996a21b17ead00d5d1140b371", + "source_tree": "sha256:334a2324f5258126e7f4e8b4fbc53f9a051c8dc53b7f9c25a9b7343292dd7c84" }, "owner_lane": "", "provenance": { diff --git a/crates/aienos-capability-ffi/src/lib.rs b/crates/aienos-capability-ffi/src/lib.rs index 5be7d247..1c411bd8 100644 --- a/crates/aienos-capability-ffi/src/lib.rs +++ b/crates/aienos-capability-ffi/src/lib.rs @@ -1,4 +1,19 @@ -//! C binding used by the reaction world and by the Linux differential oracle. +//! C binding for the 32-bit-generation Rust capability authority +//! (`aienos-capability`). +//! +//! This is NOT the native AIENOS capability ABI. The production ABI is the C +//! library in `native/capability` (`aienos_capability.h`): 64-bit +//! generations, `AienosCapRef` of 16 bytes, symbols `aienos_cap_*`. That is +//! the library omega links into the reaction world. +//! +//! This crate is a separate implementation with a different layout +//! (`AienosCapRef` of 8 bytes, 32-bit generation). So that a program built +//! against the C header can never resolve against this archive by accident, +//! every exported symbol carries its own versioned prefix, `aienos_cap32_*`, +//! and [`aienos_cap32_abi_layout`] reports the layout it was built with. +//! `scripts/test-cap-abi-isolation.sh` checks that the two archives share no +//! symbol and that a C caller of `aienos_cap_*` fails to link against this +//! one. //! //! The view pointer can validate. The admin pointer can change the table. //! They share the table, and only the admin holds the office token. @@ -11,6 +26,10 @@ use aienos_capability::{ AuthorityAdmin, AuthorityState, CapRef, Entry, Mint, ERR_IO, ERR_STATE, OK, }; +/// Capability reference in the 32-bit-generation Rust authority ABI +/// (8 bytes, align 4, generation at offset 4). NOT the native AIENOS +/// capability ABI, whose `AienosCapRef` is 16 bytes with a 64-bit +/// generation at offset 8. #[repr(C)] #[derive(Clone, Copy)] pub struct AienosCapRef { @@ -18,6 +37,8 @@ pub struct AienosCapRef { pub generation: u32, } +/// Table entry in the 32-bit-generation Rust authority ABI (64 bytes). +/// NOT the native AIENOS capability ABI (88 bytes there). #[repr(C)] #[derive(Clone, Copy)] pub struct AienosCapEntry { @@ -36,6 +57,8 @@ pub struct AienosCapEntry { pub minted_by_generation: u32, } +/// Mint request in the 32-bit-generation Rust authority ABI (48 bytes). +/// NOT the native AIENOS capability ABI (64 bytes there). #[repr(C)] #[derive(Clone, Copy)] pub struct AienosCapMint { @@ -48,6 +71,63 @@ pub struct AienosCapMint { pub authority: AienosCapRef, } +/// Version of the `aienos_cap32_*` binary contract. Bump it, and the symbol +/// prefix, if any exported layout or signature changes. +pub const AIENOS_CAP32_ABI_VERSION: u32 = 1; + +/// Width in bits of every generation field in this ABI. +pub const AIENOS_CAP32_GENERATION_BITS: u32 = 32; + +/// Number of `u32` words [`aienos_cap32_abi_layout`] writes. +pub const AIENOS_CAP32_LAYOUT_WORDS: usize = 4; + +const REF_SIZE: usize = 8; +const REF_ALIGN: usize = 4; +const REF_GENERATION_OFFSET: usize = 4; +const ENTRY_SIZE: usize = 64; +const MINT_SIZE: usize = 48; + +const _: () = assert!(core::mem::size_of::<AienosCapRef>() == REF_SIZE); +const _: () = assert!(core::mem::align_of::<AienosCapRef>() == REF_ALIGN); +const _: () = assert!(core::mem::offset_of!(AienosCapRef, cap_id) == 0); +const _: () = assert!(core::mem::offset_of!(AienosCapRef, generation) == REF_GENERATION_OFFSET); +const _: () = assert!(core::mem::size_of::<u32>() * 8 == AIENOS_CAP32_GENERATION_BITS as usize); +const _: () = assert!(core::mem::size_of::<AienosCapEntry>() == ENTRY_SIZE); +const _: () = assert!(core::mem::align_of::<AienosCapEntry>() == 8); +const _: () = assert!(core::mem::offset_of!(AienosCapEntry, generation) == 4); +const _: () = assert!(core::mem::offset_of!(AienosCapEntry, resource) == 24); +const _: () = assert!(core::mem::offset_of!(AienosCapEntry, parent_generation) == 52); +const _: () = assert!(core::mem::offset_of!(AienosCapEntry, minted_by_generation) == 60); +const _: () = assert!(core::mem::size_of::<AienosCapMint>() == MINT_SIZE); +const _: () = assert!(core::mem::offset_of!(AienosCapMint, lease_ticks) == 24); +const _: () = assert!(core::mem::offset_of!(AienosCapMint, parent) == 32); +const _: () = assert!(core::mem::offset_of!(AienosCapMint, authority) == 40); + +/// Report the layout this archive was built with, so a caller can refuse to +/// run against a different one. Writes, in order: size of `AienosCapRef`, +/// offset of its `generation`, size of that `generation` field, size of +/// `AienosCapEntry`. Returns [`AIENOS_CAP32_ABI_VERSION`], or 0 when `out` +/// is null. +/// +/// # Safety +/// `out` is null or points to [`AIENOS_CAP32_LAYOUT_WORDS`] writable `u32`s. +#[no_mangle] +pub unsafe extern "C" fn aienos_cap32_abi_layout(out: *mut u32) -> u32 { + if out.is_null() { + return 0; + } + let words: [u32; AIENOS_CAP32_LAYOUT_WORDS] = [ + REF_SIZE as u32, + REF_GENERATION_OFFSET as u32, + core::mem::size_of::<u32>() as u32, + ENTRY_SIZE as u32, + ]; + for (i, word) in words.iter().enumerate() { + unsafe { *out.add(i) = *word }; + } + AIENOS_CAP32_ABI_VERSION +} + pub struct AienosCapShared { state: Mutex<AuthorityState>, admin: Mutex<AuthorityAdmin>, @@ -128,7 +208,7 @@ fn write_ref(out: *mut AienosCapRef, cap: CapRef) { /// # Safety /// `admin_out` and `view_out` must be writable pointers. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_start( +pub unsafe extern "C" fn aienos_cap32_start( admin_out: *mut *mut AienosCapAdmin, view_out: *mut *mut AienosCapView, ) -> i32 { @@ -163,9 +243,9 @@ pub unsafe extern "C" fn aienos_cap_start( } /// # Safety -/// Both pointers came from [`aienos_cap_start`] and have not been stopped. +/// Both pointers came from [`aienos_cap32_start`] and have not been stopped. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_stop(admin: *mut AienosCapAdmin, view: *mut AienosCapView) { +pub unsafe extern "C" fn aienos_cap32_stop(admin: *mut AienosCapAdmin, view: *mut AienosCapView) { if !admin.is_null() { drop(unsafe { Box::from_raw(admin) }); } @@ -191,9 +271,9 @@ fn with_state_mut<T>(shared: &AienosCapShared, f: impl FnOnce(&mut AuthorityStat } /// # Safety -/// `admin` came from [`aienos_cap_start`]. +/// `admin` came from [`aienos_cap32_start`]. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_office( +pub unsafe extern "C" fn aienos_cap32_office( admin: *const AienosCapAdmin, out: *mut AienosCapRef, ) -> i32 { @@ -206,9 +286,9 @@ pub unsafe extern "C" fn aienos_cap_office( } /// # Safety -/// `admin` came from [`aienos_cap_start`]. `out` may be null. +/// `admin` came from [`aienos_cap32_start`]. `out` may be null. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_mint( +pub unsafe extern "C" fn aienos_cap32_mint( admin: *mut AienosCapAdmin, request: *const AienosCapMint, out: *mut AienosCapRef, @@ -230,9 +310,9 @@ pub unsafe extern "C" fn aienos_cap_mint( } /// # Safety -/// `admin` came from [`aienos_cap_start`]. +/// `admin` came from [`aienos_cap32_start`]. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_revoke( +pub unsafe extern "C" fn aienos_cap32_revoke( admin: *mut AienosCapAdmin, authority: AienosCapRef, target: AienosCapRef, @@ -248,9 +328,9 @@ pub unsafe extern "C" fn aienos_cap_revoke( } /// # Safety -/// `admin` came from [`aienos_cap_start`]. +/// `admin` came from [`aienos_cap32_start`]. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_reclaim( +pub unsafe extern "C" fn aienos_cap32_reclaim( admin: *mut AienosCapAdmin, authority: AienosCapRef, cap_id: u32, @@ -266,9 +346,9 @@ pub unsafe extern "C" fn aienos_cap_reclaim( } /// # Safety -/// `admin` came from [`aienos_cap_start`]. +/// `admin` came from [`aienos_cap32_start`]. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_advance_clock( +pub unsafe extern "C" fn aienos_cap32_advance_clock( admin: *mut AienosCapAdmin, authority: AienosCapRef, ticks: u64, @@ -284,9 +364,9 @@ pub unsafe extern "C" fn aienos_cap_advance_clock( } /// # Safety -/// `admin` came from [`aienos_cap_start`]. +/// `admin` came from [`aienos_cap32_start`]. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_bump_epoch( +pub unsafe extern "C" fn aienos_cap32_bump_epoch( admin: *mut AienosCapAdmin, authority: AienosCapRef, ) -> i32 { @@ -301,9 +381,9 @@ pub unsafe extern "C" fn aienos_cap_bump_epoch( } /// # Safety -/// `admin` came from [`aienos_cap_start`]. +/// `admin` came from [`aienos_cap32_start`]. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_kill(admin: *mut AienosCapAdmin) -> i32 { +pub unsafe extern "C" fn aienos_cap32_kill(admin: *mut AienosCapAdmin) -> i32 { if admin.is_null() { return ERR_STATE; } @@ -311,30 +391,31 @@ pub unsafe extern "C" fn aienos_cap_kill(admin: *mut AienosCapAdmin) -> i32 { OK } -/// Replace the table with the next boot generation. Outstanding references -/// fail against the new table. The office token is replaced. +/// Replace the table with the next boot generation. The new table starts +/// above every generation the old one used (fix mirrored from the native +/// authority, b1fa747), so outstanding references fail against it even +/// after reclaims. The office token is replaced. /// /// # Safety -/// `admin` came from [`aienos_cap_start`]. +/// `admin` came from [`aienos_cap32_start`]. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_restart(admin: *mut AienosCapAdmin) -> i32 { +pub unsafe extern "C" fn aienos_cap32_restart(admin: *mut AienosCapAdmin) -> i32 { if admin.is_null() { return ERR_STATE; } - let boot = match aienos_capability::take_boot_gen() { - Ok(boot) => boot, - Err(code) => return code, - }; - let state = match AuthorityState::bootstrap(boot) { - Ok(state) => state, - Err(code) => return code, - }; let mut token = [0u8; 32]; if !fill_token(&mut token) { return ERR_IO; } let shared = &unsafe { &*admin }.shared; - with_state_mut(shared, |slot| *slot = state); + let swapped = with_state_mut(shared, |slot| { + let boot = aienos_capability::take_boot_gen_above(slot.generation_floor())?; + *slot = AuthorityState::bootstrap(boot)?; + Ok::<(), i32>(()) + }); + if let Err(code) = swapped { + return code; + } let mut admin_token = shared .admin .lock() @@ -344,9 +425,9 @@ pub unsafe extern "C" fn aienos_cap_restart(admin: *mut AienosCapAdmin) -> i32 { } /// # Safety -/// `view` came from [`aienos_cap_start`]. `out` may be null. +/// `view` came from [`aienos_cap32_start`]. `out` may be null. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_validate( +pub unsafe extern "C" fn aienos_cap32_validate( view: *const AienosCapView, cap: AienosCapRef, subject: u32, @@ -370,9 +451,9 @@ pub unsafe extern "C" fn aienos_cap_validate( } /// # Safety -/// `view` came from [`aienos_cap_start`]. `out` may be null. +/// `view` came from [`aienos_cap32_start`]. `out` may be null. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_inspect( +pub unsafe extern "C" fn aienos_cap32_inspect( view: *const AienosCapView, cap: AienosCapRef, out: *mut AienosCapEntry, @@ -393,9 +474,9 @@ pub unsafe extern "C" fn aienos_cap_inspect( } /// # Safety -/// `view` came from [`aienos_cap_start`]. +/// `view` came from [`aienos_cap32_start`]. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_clock(view: *const AienosCapView) -> u64 { +pub unsafe extern "C" fn aienos_cap32_clock(view: *const AienosCapView) -> u64 { if view.is_null() { return 0; } @@ -406,9 +487,9 @@ pub unsafe extern "C" fn aienos_cap_clock(view: *const AienosCapView) -> u64 { /// change the table no matter what reference or token bytes are supplied. /// /// # Safety -/// `view` came from [`aienos_cap_start`]. `token` may be null. +/// `view` came from [`aienos_cap32_start`]. `token` may be null. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_cognition_mint( +pub unsafe extern "C" fn aienos_cap32_cognition_mint( view: *const AienosCapView, _request: *const AienosCapMint, _token: *const u8, @@ -421,9 +502,9 @@ pub unsafe extern "C" fn aienos_cap_cognition_mint( } /// # Safety -/// `view` came from [`aienos_cap_start`]. +/// `view` came from [`aienos_cap32_start`]. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_cognition_admin( +pub unsafe extern "C" fn aienos_cap32_cognition_admin( view: *const AienosCapView, _op: u32, _authority: AienosCapRef, @@ -438,7 +519,7 @@ pub unsafe extern "C" fn aienos_cap_cognition_admin( /// # Safety /// `out` may be null. When it is not, it must be writable. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_generation_advance(generation: u32, out: *mut u32) -> i32 { +pub unsafe extern "C" fn aienos_cap32_generation_advance(generation: u32, out: *mut u32) -> i32 { match aienos_capability::generation_advance(generation) { Ok(next) => { if !out.is_null() { @@ -451,9 +532,9 @@ pub unsafe extern "C" fn aienos_cap_generation_advance(generation: u32, out: *mu } /// # Safety -/// `admin` came from [`aienos_cap_start`] and is the test harness, not cognition. +/// `admin` came from [`aienos_cap32_start`] and is the test harness, not cognition. #[no_mangle] -pub unsafe extern "C" fn aienos_cap_force_generation( +pub unsafe extern "C" fn aienos_cap32_force_generation( admin: *mut AienosCapAdmin, cap_id: u32, generation: u32, @@ -467,3 +548,221 @@ pub unsafe extern "C" fn aienos_cap_force_generation( .err() .unwrap_or(OK) } + +#[cfg(test)] +mod tests { + use super::*; + use aienos_capability::{ + ERR_EXHAUSTED, ERR_REVOKED, ERR_STALE_GEN, ERR_SUBJECT, ERR_UNAUTHORIZED, RIGHT_READ, + }; + use core::ptr; + + #[test] + fn layout_matches_the_cap32_contract() { + assert_eq!(core::mem::size_of::<AienosCapRef>(), 8); + assert_eq!(core::mem::align_of::<AienosCapRef>(), 4); + assert_eq!(core::mem::offset_of!(AienosCapRef, generation), 4); + assert_eq!(core::mem::size_of::<AienosCapEntry>(), 64); + assert_eq!(core::mem::size_of::<AienosCapMint>(), 48); + let mut words = [0u32; AIENOS_CAP32_LAYOUT_WORDS]; + let version = unsafe { aienos_cap32_abi_layout(words.as_mut_ptr()) }; + assert_eq!(version, AIENOS_CAP32_ABI_VERSION); + assert_eq!(version, 1); + assert_eq!(words, [8, 4, 4, 64]); + assert_eq!(unsafe { aienos_cap32_abi_layout(ptr::null_mut()) }, 0); + // The native C ABI is 16 bytes with the generation at offset 8. A + // caller that checks this report cannot mistake one for the other. + assert_ne!(words[0], 16); + assert_ne!(words[1], 8); + } + + struct Auth { + admin: *mut AienosCapAdmin, + view: *mut AienosCapView, + } + + impl Auth { + fn start() -> Self { + let mut admin = ptr::null_mut(); + let mut view = ptr::null_mut(); + assert_eq!(unsafe { aienos_cap32_start(&mut admin, &mut view) }, OK); + Self { admin, view } + } + + fn office(&self) -> AienosCapRef { + let mut out = AienosCapRef { + cap_id: 0, + generation: 0, + }; + assert_eq!(unsafe { aienos_cap32_office(self.admin, &mut out) }, OK); + out + } + + fn mint(&self, subject: u32, resource: u64) -> AienosCapRef { + let request = AienosCapMint { + issuer: 3, + subject, + resource, + rights: RIGHT_READ, + lease_ticks: 0, + parent: AienosCapRef { + cap_id: aienos_capability::PARENT_NONE, + generation: 0, + }, + authority: self.office(), + }; + let mut out = AienosCapRef { + cap_id: 0, + generation: 0, + }; + assert_eq!( + unsafe { aienos_cap32_mint(self.admin, &request, &mut out) }, + OK + ); + out + } + + fn validate(&self, cap: AienosCapRef, subject: u32, resource: u64) -> i32 { + unsafe { + aienos_cap32_validate( + self.view, + cap, + subject, + resource, + RIGHT_READ, + ptr::null_mut(), + ) + } + } + } + + impl Drop for Auth { + fn drop(&mut self) { + unsafe { aienos_cap32_stop(self.admin, self.view) }; + } + } + + /// Mirror of the C test reclaimed_reference_stays_dead_after_restart + /// (fix b1fa747): a reference minted after one reclaim must not validate + /// again once the same slot is minted for the same subject and resource + /// after a restart. + #[test] + fn reclaimed_reference_stays_dead_after_restart() { + let a = Auth::start(); + let first = a.mint(1, 0x40); + assert_eq!( + unsafe { aienos_cap32_revoke(a.admin, a.office(), first) }, + OK + ); + assert_eq!( + unsafe { aienos_cap32_reclaim(a.admin, a.office(), first.cap_id) }, + OK + ); + let second = a.mint(1, 0x40); + assert_eq!(second.cap_id, first.cap_id); + assert_eq!(second.generation, first.generation + 1); + assert_eq!(unsafe { aienos_cap32_restart(a.admin) }, OK); + let fresh = a.mint(1, 0x40); + assert_eq!(fresh.cap_id, second.cap_id); + assert!(fresh.generation > second.generation); + assert_eq!(a.validate(second, 1, 0x40), ERR_STALE_GEN); + assert_eq!(a.validate(first, 1, 0x40), ERR_STALE_GEN); + assert_eq!(a.validate(fresh, 1, 0x40), OK); + } + + /// Mirror of the C test generation_exhaustion_fails_closed. + #[test] + fn generation_exhaustion_fails_closed() { + let mut next = 0u32; + assert_eq!( + unsafe { aienos_cap32_generation_advance(u32::MAX, &mut next) }, + ERR_EXHAUSTED + ); + assert_eq!( + unsafe { aienos_cap32_generation_advance(u32::MAX - 1, &mut next) }, + OK + ); + assert_eq!(next, u32::MAX); + let a = Auth::start(); + let cap = a.mint(1, 0x10); + assert_eq!(unsafe { aienos_cap32_revoke(a.admin, a.office(), cap) }, OK); + assert_eq!( + unsafe { aienos_cap32_force_generation(a.admin, cap.cap_id, u32::MAX) }, + OK + ); + assert_eq!( + unsafe { aienos_cap32_reclaim(a.admin, a.office(), cap.cap_id) }, + ERR_EXHAUSTED + ); + assert_eq!(a.validate(cap, 1, 0x10), ERR_STALE_GEN); + let replacement = a.mint(1, 0x11); + assert_ne!(replacement.cap_id, cap.cap_id); + assert_eq!( + unsafe { aienos_cap32_force_generation(a.admin, replacement.cap_id, 99) }, + ERR_STATE + ); + // No start above an exhausted generation exists, so restart refuses + // rather than reuse one, and the table is left as it was. + assert_eq!(unsafe { aienos_cap32_restart(a.admin) }, ERR_EXHAUSTED); + assert_eq!(a.validate(replacement, 1, 0x11), OK); + } + + #[test] + fn stale_and_unauthorized_use_is_refused() { + let a = Auth::start(); + let cap = a.mint(1, 0x20); + assert_eq!(a.validate(cap, 2, 0x20), ERR_SUBJECT); + let before = unsafe { aienos_cap32_clock(a.view) }; + let request = AienosCapMint { + issuer: 1, + subject: 1, + resource: 1, + rights: RIGHT_READ, + lease_ticks: 0, + parent: AienosCapRef { + cap_id: aienos_capability::PARENT_NONE, + generation: 0, + }, + authority: a.office(), + }; + let guess = [0u8; 32]; + assert_eq!( + unsafe { aienos_cap32_cognition_mint(a.view, &request, guess.as_ptr()) }, + ERR_UNAUTHORIZED + ); + assert_eq!( + unsafe { aienos_cap32_cognition_admin(a.view, 1, a.office(), cap) }, + ERR_UNAUTHORIZED + ); + assert_eq!(unsafe { aienos_cap32_clock(a.view) }, before); + // An ordinary reference cannot act as the office. + let mut as_office = request; + as_office.authority = cap; + let mut out = AienosCapRef { + cap_id: 0, + generation: 0, + }; + assert_eq!( + unsafe { aienos_cap32_mint(a.admin, &as_office, &mut out) }, + ERR_UNAUTHORIZED + ); + assert_eq!( + unsafe { aienos_cap32_advance_clock(a.admin, cap, 1) }, + ERR_UNAUTHORIZED + ); + assert_eq!(unsafe { aienos_cap32_revoke(a.admin, a.office(), cap) }, OK); + assert_eq!(a.validate(cap, 1, 0x20), ERR_REVOKED); + assert_eq!( + unsafe { aienos_cap32_reclaim(a.admin, a.office(), cap.cap_id) }, + OK + ); + assert_eq!(a.validate(cap, 1, 0x20), ERR_STALE_GEN); + // Null handles fail closed. + assert_eq!( + unsafe { + aienos_cap32_validate(ptr::null(), cap, 1, 0x20, RIGHT_READ, ptr::null_mut()) + }, + ERR_STATE + ); + } +} diff --git a/crates/aienos-capability/.crumb b/crates/aienos-capability/.crumb index bf98a017..c46fd152 100644 --- a/crates/aienos-capability/.crumb +++ b/crates/aienos-capability/.crumb @@ -13,21 +13,21 @@ "generated": { "children": [ { - "digest": "3e3db7622e53c339ff0383af982b4e0bf2e7bea7e091ff8d904d56331a68ee5a", - "last_commit": "187ecdf", + "digest": "9c1c32b183b9272d26de382e1327aac1da7373f79096fb3e421f795b36d17c8b", + "last_commit": "362cba9", "name": "src", "purpose": "Directory crates/aienos-capability/src. Purpose not yet described by a human." } ], - "children_root": "sha256:537644e2825d52dd152f8d50494da370526066bdb3e5f6fb6067a01edc260a93", - "compiled_at": "2026-10-04T13:47:18Z", + "children_root": "sha256:ec5b8bc5ec1d665dcfbecf32ea2408e5d3c84ec78e9d4623554ffc9a32d6970d", + "compiled_at": "2026-10-11T21:20:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:8a0463de3e20f4781afc41b8f7b354a29a3b819b2a88a8588b62c2ae3a29ebef", + "digest": "sha256:ca15c2b9ae570b0aa2c7859672de2c976d6ce046188c09b7c74b5ccbee350136", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 2, - "generated_at_commit": "a863d5c1f5bd89c88c5f6671eecfd27b599ee512", - "source_tree": "sha256:7582bb17e3282716c415dfd155b62aa4d26f69ce48ed028ab19671bbceb15da0" + "generated_at_commit": "362cba9653c8021996a21b17ead00d5d1140b371", + "source_tree": "sha256:a435f6cea762e4c709af5e49797c27aeedc468b7003993e72cf5f429dcf5e5b0" }, "owner_lane": "", "provenance": { diff --git a/crates/aienos-capability/Cargo.toml b/crates/aienos-capability/Cargo.toml index 3d94831d..ab8c992d 100644 --- a/crates/aienos-capability/Cargo.toml +++ b/crates/aienos-capability/Cargo.toml @@ -5,6 +5,6 @@ edition.workspace = true authors.workspace = true license.workspace = true repository.workspace = true -description = "Native AIENOS capability authority. The Linux mint process remains the differential oracle." +description = "Rust AIENOS capability authority with 32-bit generations; separate from the native C production ABI (see docs/CAPABILITY_ABI.md). The Linux mint process remains the differential oracle." [dependencies] diff --git a/crates/aienos-capability/src/.crumb b/crates/aienos-capability/src/.crumb index 043fdf90..c4140490 100644 --- a/crates/aienos-capability/src/.crumb +++ b/crates/aienos-capability/src/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T13:47:18Z", + "compiled_at": "2026-10-11T21:20:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:3e3db7622e53c339ff0383af982b4e0bf2e7bea7e091ff8d904d56331a68ee5a", + "digest": "sha256:9c1c32b183b9272d26de382e1327aac1da7373f79096fb3e421f795b36d17c8b", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 1, - "generated_at_commit": "a863d5c1f5bd89c88c5f6671eecfd27b599ee512", - "source_tree": "sha256:415989d80d70c9adb40bfdcb81d561e359dbe52dc3cae6c1f2795e52fcf6cb6b" + "generated_at_commit": "362cba9653c8021996a21b17ead00d5d1140b371", + "source_tree": "sha256:fe35483a6d43ccd7a9450e95bde44a95ec22e67a282447450ca4c70d1a8b32b8" }, "owner_lane": "", "provenance": { diff --git a/crates/aienos-capability/src/lib.rs b/crates/aienos-capability/src/lib.rs index ae7edece..64a44c6d 100644 --- a/crates/aienos-capability/src/lib.rs +++ b/crates/aienos-capability/src/lib.rs @@ -1,4 +1,10 @@ -//! Native capability authority. +//! Rust capability authority, 32-bit generations. +//! +//! A separate implementation of the AIENOS capability rules. The production +//! ABI is the C authority in `native/capability` (64-bit generations, +//! symbols `aienos_cap_*`); this crate and its C binding +//! `aienos-capability-ffi` (symbols `aienos_cap32_*`) are not interchangeable +//! with it. See `docs/CAPABILITY_ABI.md`. //! //! This is the AIENOS side of the host reference root. The slot rules match //! that root: a reference is an index plus a generation, a reused slot does @@ -102,14 +108,28 @@ pub const fn add_u64(a: u64, b: u64) -> Result<u64, i32> { /// Take the next boot generation. A later authority starts higher, so a /// reference from a stopped authority cannot validate against the new one. pub fn take_boot_gen() -> Result<u32, i32> { + take_boot_gen_above(0) +} + +/// Take the next boot generation, no lower than `floor`. A restart passes +/// [`AuthorityState::generation_floor`] of the table it replaces, so the new +/// table starts above every generation the old one handed out, including +/// generations a reclaim moved past the old boot generation. Fails closed +/// with [`ERR_EXHAUSTED`] rather than reuse a generation. +pub fn take_boot_gen_above(floor: u32) -> Result<u32, i32> { let mut cur = NEXT_BOOT_GEN.load(Ordering::Relaxed); loop { - if cur == 0 || cur == u32::MAX { + let start = if floor > cur { floor } else { cur }; + if start == 0 || start == u32::MAX { return Err(ERR_EXHAUSTED); } - match NEXT_BOOT_GEN.compare_exchange_weak(cur, cur + 1, Ordering::AcqRel, Ordering::Relaxed) - { - Ok(_) => return Ok(cur), + match NEXT_BOOT_GEN.compare_exchange_weak( + cur, + start + 1, + Ordering::AcqRel, + Ordering::Relaxed, + ) { + Ok(_) => return Ok(start), Err(observed) => cur = observed, } } @@ -216,6 +236,30 @@ impl AuthorityState { self.boot_gen } + /// One past the largest generation anywhere in the table, including the + /// generations recorded for parents and minters. A table restarted at or + /// above this value honours no reference this table handed out. Returns + /// `u32::MAX` when no such start exists, which + /// [`take_boot_gen_above`] refuses. + pub fn generation_floor(&self) -> u32 { + let mut top = self.boot_gen; + let mut i = 0; + while i < CAP_MAX { + let e = &self.entries[i]; + if e.generation > top { + top = e.generation; + } + if e.parent_generation > top { + top = e.parent_generation; + } + if e.minted_by_generation > top { + top = e.minted_by_generation; + } + i += 1; + } + top.saturating_add(1) + } + pub fn epoch(&self) -> u64 { self.epoch } @@ -716,6 +760,56 @@ mod tests { }) } + /// The b1fa747 defect, Rust side: a reclaim moves a slot to boot + 1, and + /// a restart at boot + 1 made the reference minted after the reclaim + /// valid again. A restart must start at or above the old table's floor. + #[test] + fn reclaimed_reference_stays_dead_after_restart() { + let mut state = boot(); + let first = root_mint(&mut state, 1, 0x40, RIGHT_READ).unwrap(); + state.revoke(office(&state), first).unwrap(); + state.reclaim(office(&state), first.cap_id).unwrap(); + let second = root_mint(&mut state, 1, 0x40, RIGHT_READ).unwrap(); + assert_eq!(second.cap_id, first.cap_id); + assert_eq!(second.generation, first.generation + 1); + // The old restart start (boot + 1) is exactly the reused generation. + assert_eq!(state.boot_gen() + 1, second.generation); + let floor = state.generation_floor(); + assert!(floor > second.generation); + let start = take_boot_gen_above(floor).unwrap(); + assert!(start >= floor); + let mut restarted = AuthorityState::bootstrap(start).unwrap(); + let fresh = root_mint(&mut restarted, 1, 0x40, RIGHT_READ).unwrap(); + assert_eq!(fresh.cap_id, second.cap_id); + assert!(fresh.generation > second.generation); + assert_eq!( + restarted.validate(second, 1, 0x40, RIGHT_READ).unwrap_err(), + ERR_STALE_GEN + ); + assert_eq!( + restarted.validate(first, 1, 0x40, RIGHT_READ).unwrap_err(), + ERR_STALE_GEN + ); + assert!(restarted.validate(fresh, 1, 0x40, RIGHT_READ).is_ok()); + } + + #[test] + fn restart_refuses_above_an_exhausted_generation() { + let mut state = boot(); + let cap = root_mint(&mut state, 1, 0x10, RIGHT_READ).unwrap(); + state.revoke(office(&state), cap).unwrap(); + state.set_generation_for_test(cap.cap_id, u32::MAX).unwrap(); + assert_eq!(state.generation_floor(), u32::MAX); + assert_eq!(take_boot_gen_above(u32::MAX).unwrap_err(), ERR_EXHAUSTED); + assert_eq!( + AuthorityState::bootstrap(u32::MAX).unwrap_err(), + ERR_EXHAUSTED + ); + let a = take_boot_gen_above(0).unwrap(); + let b = take_boot_gen_above(0).unwrap(); + assert!(b > a); + } + #[test] fn high_half_resource_is_kept() { let wide = (1u64 << 32) | 0x51; @@ -998,4 +1092,190 @@ mod tests { }); assert_eq!(too_deep.unwrap_err(), ERR_CHAIN); } + + // ---- ABI-qualification regression set (mirrors native/capability/tests) ---- + + #[test] + fn stale_reference_after_revoke_is_refused() { + let mut state = boot(); + let cap = root_mint(&mut state, 1, 0x60, RIGHT_READ).unwrap(); + assert!(state.validate(cap, 1, 0x60, RIGHT_READ).is_ok()); + state.revoke(office(&state), cap).unwrap(); + assert_eq!( + state.validate(cap, 1, 0x60, RIGHT_READ).unwrap_err(), + ERR_REVOKED + ); + assert_eq!(state.revoke(office(&state), cap).unwrap_err(), ERR_STATE); + state.reclaim(office(&state), cap.cap_id).unwrap(); + assert_eq!( + state.validate(cap, 1, 0x60, RIGHT_READ).unwrap_err(), + ERR_STALE_GEN + ); + assert_eq!(state.inspect(cap).unwrap_err(), ERR_STALE_GEN); + // A revoked minting authority cannot mint. + let minter = root_mint(&mut state, 0, RES_AUTHORITY, RIGHT_MINT).unwrap(); + state.revoke(office(&state), minter).unwrap(); + let refused = state.mint(Mint { + issuer: 3, + subject: 1, + resource: 0x61, + rights: RIGHT_READ, + lease_ticks: 0, + parent: CapRef::NONE, + authority: minter, + }); + assert_eq!(refused.unwrap_err(), ERR_REVOKED); + } + + #[test] + fn recycled_slot_does_not_honour_old_reference() { + let mut state = boot(); + let old = root_mint(&mut state, 1, 0x70, RIGHT_READ | RIGHT_DELEGATE).unwrap(); + state.revoke(office(&state), old).unwrap(); + state.reclaim(office(&state), old.cap_id).unwrap(); + let new = root_mint(&mut state, 1, 0x70, RIGHT_READ | RIGHT_DELEGATE).unwrap(); + assert_eq!(new.cap_id, old.cap_id); + assert!(new.generation > old.generation); + assert_eq!( + state.validate(old, 1, 0x70, RIGHT_READ).unwrap_err(), + ERR_STALE_GEN + ); + assert_eq!(state.inspect(old).unwrap_err(), ERR_STALE_GEN); + // The old reference cannot revoke or delegate from the new holder. + assert_eq!( + state.revoke(office(&state), old).unwrap_err(), + ERR_STALE_GEN + ); + let via_old = state.mint(Mint { + issuer: 1, + subject: 2, + resource: 0x70, + rights: RIGHT_READ, + lease_ticks: 0, + parent: old, + authority: old, + }); + assert_eq!(via_old.unwrap_err(), ERR_STALE_GEN); + assert!(state.validate(new, 1, 0x70, RIGHT_READ).is_ok()); + } + + #[test] + fn generation_exhaustion_fails_closed() { + assert_eq!(generation_advance(u32::MAX).unwrap_err(), ERR_EXHAUSTED); + assert_eq!(generation_advance(u32::MAX - 1), Ok(u32::MAX)); + let mut state = boot(); + let cap = root_mint(&mut state, 1, 0x10, RIGHT_READ).unwrap(); + state.revoke(office(&state), cap).unwrap(); + state.set_generation_for_test(cap.cap_id, u32::MAX).unwrap(); + assert_eq!( + state.reclaim(office(&state), cap.cap_id).unwrap_err(), + ERR_EXHAUSTED + ); + assert_eq!( + state.validate(cap, 1, 0x10, RIGHT_READ).unwrap_err(), + ERR_STALE_GEN + ); + let replacement = root_mint(&mut state, 1, 0x11, RIGHT_READ).unwrap(); + assert_ne!(replacement.cap_id, cap.cap_id); + // A live slot cannot be moved onto a chosen generation. + assert_eq!( + state + .set_generation_for_test(replacement.cap_id, 99) + .unwrap_err(), + ERR_STATE + ); + // Every free slot exhausted: mint refuses rather than reuse one. + for id in 0..CAP_MAX as u32 { + let _ = state.set_generation_for_test(id, u32::MAX); + } + assert_eq!( + root_mint(&mut state, 1, 0x12, RIGHT_READ).unwrap_err(), + ERR_EXHAUSTED + ); + } + + #[test] + fn unauthorized_capability_use_is_refused() { + let mut state = boot(); + let cap = root_mint(&mut state, 1, 0x80, RIGHT_READ).unwrap(); + assert_eq!( + state.validate(cap, 2, 0x80, RIGHT_READ).unwrap_err(), + ERR_SUBJECT + ); + assert_eq!( + state.validate(cap, 1, 0x81, RIGHT_READ).unwrap_err(), + ERR_RESOURCE + ); + assert_eq!( + state.validate(cap, 1, 0x80, RIGHT_WRITE).unwrap_err(), + ERR_RIGHTS + ); + let request = |authority: CapRef, rights: u32| Mint { + issuer: 3, + subject: 1, + resource: 0x82, + rights, + lease_ticks: 0, + parent: CapRef::NONE, + authority, + }; + // An ordinary capability is not an office authority. + assert_eq!( + state.mint(request(cap, RIGHT_READ)).unwrap_err(), + ERR_UNAUTHORIZED + ); + // A slot never handed out, and one out of range, authorize nothing. + let undelivered = CapRef { + cap_id: 200, + generation: state.boot_gen(), + }; + assert_eq!( + state.mint(request(undelivered, RIGHT_READ)).unwrap_err(), + ERR_UNAUTHORIZED + ); + let out_of_range = CapRef { + cap_id: CAP_MAX as u32, + generation: state.boot_gen(), + }; + assert_eq!( + state.mint(request(out_of_range, RIGHT_READ)).unwrap_err(), + ERR_UNAUTHORIZED + ); + // A minting authority cannot hand out a privilege it does not hold. + let minter = root_mint(&mut state, 0, RES_AUTHORITY, RIGHT_MINT).unwrap(); + assert!(state.mint(request(minter, RIGHT_READ)).is_ok()); + assert_eq!( + state.mint(request(minter, RIGHT_REVOKE)).unwrap_err(), + ERR_UNAUTHORIZED + ); + // Delegation must be authorized by the parent itself. + let parent = root_mint(&mut state, 1, 0x83, RIGHT_READ | RIGHT_DELEGATE).unwrap(); + let mismatched = state.mint(Mint { + issuer: 1, + subject: 2, + resource: 0x83, + rights: RIGHT_READ, + lease_ticks: 0, + parent, + authority: office(&state), + }); + assert_eq!(mismatched.unwrap_err(), ERR_UNAUTHORIZED); + // A non-office reference cannot reclaim, revoke, move the clock or epoch. + assert_eq!( + state.reclaim(cap, cap.cap_id).unwrap_err(), + ERR_UNAUTHORIZED + ); + assert_eq!(state.revoke(cap, parent).unwrap_err(), ERR_UNAUTHORIZED); + assert_eq!(state.advance_clock(cap, 1).unwrap_err(), ERR_UNAUTHORIZED); + assert_eq!(state.bump_epoch(cap).unwrap_err(), ERR_UNAUTHORIZED); + let view = AuthorityView::new(&state); + assert_eq!( + view.cognition_attempt_reclaim(office(&state), cap.cap_id), + ERR_UNAUTHORIZED + ); + assert_eq!( + view.cognition_attempt_clock(office(&state), 1), + ERR_UNAUTHORIZED + ); + } } diff --git a/docs/.crumb b/docs/.crumb index 7760fd16..5c2ff645 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -30,14 +30,14 @@ } ], "children_root": "sha256:55bcc89f5282eb2e3f4e86d882abdfb5f4ea39e427ef97ef620b2267e5701eb1", - "compiled_at": "2026-10-11T14:02:56Z", + "compiled_at": "2026-10-11T21:20:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:3ee624a79df42f92ed58adfd80a511bc2f68dad065fd2cd8b8ce97176879fda0", + "digest": "sha256:4a006f504e49f1586a9591657f799ab634dd4b8256fbbed233d2bfbf541a7907", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 68, - "generated_at_commit": "b40d23ffe805b2044cb3cd1da6f79e03db3d7517", - "source_tree": "sha256:12a74d0bb0b2c660b299e25565483847252d5409d70cbe31ed1944a994287680" + "files": 69, + "generated_at_commit": "362cba9653c8021996a21b17ead00d5d1140b371", + "source_tree": "sha256:5ae38aa8f3095ebededbd595f7e7755691c24a93c3bb67aa54ccb7329ad4b2a3" }, "owner_lane": "", "provenance": { diff --git a/docs/CAPABILITY_ABI.md b/docs/CAPABILITY_ABI.md new file mode 100644 index 00000000..ce99f107 --- /dev/null +++ b/docs/CAPABILITY_ABI.md @@ -0,0 +1,58 @@ +# Capability ABI: one production authority, one separate Rust implementation + +Ruling of the ABI qualification (abi-qual, 2026-10-11). The two capability implementations in this +repository are intentionally independent. They are not two builds of one binary contract, and they +must never be linked in place of each other. + +| | Native C authority (production ABI) | Rust crates (separate implementation) | +|---|---|---| +| Source | `native/capability/aienos_capability.{h,c}` | `crates/aienos-capability` (core, `no_std`), `crates/aienos-capability-ffi` (C binding) | +| Generation | 64 bits, `AIENOS_CAP_GENERATION_BITS` = 64 | 32 bits, `AIENOS_CAP32_GENERATION_BITS` = 32 | +| `AienosCapRef` | 16 bytes, align 8, generation at offset 8 | 8 bytes, align 4, generation at offset 4 | +| `AienosCapEntry` / `AienosCapMint` | 88 / 64 bytes | 64 / 48 bytes | +| Exported symbols | `aienos_cap_*` | `aienos_cap32_*`, plus `aienos_cap32_abi_layout` (ABI version 1) | +| Who links it | omega (`libaienos_capability.a` merged into `librx_compose.a`, header copy `src/runtime/aienos_cap.h`), `native/argus` | nothing in this repository | + +## Why the separate prefix + +Before this ruling the Rust FFI crate exported the same 17 function names as the C library with the +smaller layout. A program compiled against `aienos_capability.h` and linked against +`libaienos_capability_ffi.a` resolved every symbol and silently misread every `AienosCapRef` and +`AienosCapEntry`. With the `aienos_cap32_` prefix that program now fails to link. + +The 32-bit generation is not widened: the Rust crates keep their own contract, and a slot whose +generation reaches `u32::MAX` fails closed (`ERR_EXHAUSTED`) instead of wrapping. + +## What enforces it + +- `native/capability/aienos_capability.c`: `_Static_assert`s pin the production layout. +- `crates/aienos-capability-ffi/src/lib.rs`: `const` layout asserts and `aienos_cap32_abi_layout`, + which reports `AienosCapRef` size, generation offset, generation size and `AienosCapEntry` size. +- `scripts/cap_abi_isolation_test.sh`: builds both archives and fails unless they share no + `aienos_cap*` symbol, a C caller of `aienos_cap_*` fails to link against the Rust archive, a cap32 + caller links against it, and a drifted C header (narrowed or reordered `AienosCapRef`) is rejected. +- CI: the `capability-abi` job in `.github/workflows/native-suites.yml` runs the C rule tests, the + Rust crates' fmt, clippy and tests, and the isolation script. + +```bash +make -C native/capability test +cargo test -p aienos-capability -p aienos-capability-ffi +bash scripts/cap_abi_isolation_test.sh # ends with AIENOS_CAP_ABI_ISOLATION: PASS +``` + +## Restart fix brought to the Rust side + +The Rust authority had the restart defect the C authority fixed in b1fa747: a restart started the new +table at the old boot generation + 1, which a reclaim had already used, so a reference minted after +one reclaim validated again after the restart. `aienos_cap32_restart` now starts the new table at or +above `AuthorityState::generation_floor()` of the table it replaces (`take_boot_gen_above`), and +refuses with `ERR_EXHAUSTED` when no such start exists. + +## Known differences (not interchangeable, by design or still open) + +- The C authority also seeds a restart from `CLOCK_BOOTTIME` so a new process starts above an old one + on the same boot. The `no_std` Rust core has no clock: its starts are monotonic within one process + only. +- The C `auth_use` requires privileged operations to present an entry on `AIENOS_CAP_RES_AUTHORITY` + (issue #266, `docs/CAPABILITY_AUTHORITY_RESOURCE.md`). The Rust core does not have that check yet. + Nothing links the Rust crates today; port the check before any consumer does. diff --git a/native/.crumb b/native/.crumb index a1813020..f9359f72 100644 --- a/native/.crumb +++ b/native/.crumb @@ -61,8 +61,8 @@ "purpose": "Directory native/boot. Purpose not yet described by a human." }, { - "digest": "9a4836747ec6db7bfb75218df75936070c35e3a51470f366687e156dc6ba8d71", - "last_commit": "b84c0a6", + "digest": "aaee52694510c1aea1e16ffd54201ce8e900742974d538bbf4640ab01d239872", + "last_commit": "34d6878", "name": "capability", "purpose": "Directory native/capability. Purpose not yet described by a human." }, @@ -80,7 +80,7 @@ }, { "digest": "fdb1af443afa270d2ce93cdec1e65f2d6e3ae9e69295a919e89b4de015c95267", - "last_commit": "3f79153", + "last_commit": "863bef8", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -109,15 +109,15 @@ "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:70248a902001d0a5e9025cf96b6269987dfe327547cde585061b61ac7cee95ab", - "compiled_at": "2026-10-11T01:27:19Z", + "children_root": "sha256:cb6dc92fb6138b21f29fd64099df425f2ffb2a81072b694fb8cb75e96ba2fdfc", + "compiled_at": "2026-10-11T21:20:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:e6450fb9b838f64fcc90113926436887241ae8a12ceb5523954818b3d986b281", + "digest": "sha256:6e42346a1b795fb11cb1fe616b7e144e92002110f9de17fbfe18efab8843fde0", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", "files": 456, - "generated_at_commit": "3f791539d41d852b2b6fd36fdebaebb9f76dc2a0", - "source_tree": "sha256:88eb0e57ecadbd527ab7bb1cec80f7fd161e42dabdaf2100388156a1ace41108" + "generated_at_commit": "362cba9653c8021996a21b17ead00d5d1140b371", + "source_tree": "sha256:45c0f7ec63b02b683ac09a9ff1556a2e9f45e84ffbd11ac0cb9603a0e1ada20e" }, "owner_lane": "", "provenance": { diff --git a/native/capability/.crumb b/native/capability/.crumb index c8cd726b..cec65f50 100644 --- a/native/capability/.crumb +++ b/native/capability/.crumb @@ -14,20 +14,20 @@ "children": [ { "digest": "78115cdbda04ee0b7568424dbdba2833bd201f01118cf01464cb17653fc26aee", - "last_commit": "2529de3", + "last_commit": "b84c0a6", "name": "tests", "purpose": "Directory native/capability/tests. Purpose not yet described by a human." } ], "children_root": "sha256:0c70cfa122966e11065bca94434dcb159c75a4557cf6322bbc5256e28d49f2e5", - "compiled_at": "2026-10-06T14:20:29Z", + "compiled_at": "2026-10-11T21:20:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:9a4836747ec6db7bfb75218df75936070c35e3a51470f366687e156dc6ba8d71", + "digest": "sha256:aaee52694510c1aea1e16ffd54201ce8e900742974d538bbf4640ab01d239872", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 8, - "generated_at_commit": "2529de3fb99083fbf46e3b028806efd8b2789259", - "source_tree": "sha256:f1495160ac086d24628c729fdbe2b74985466e20ed8156db8656436753e4e859" + "generated_at_commit": "362cba9653c8021996a21b17ead00d5d1140b371", + "source_tree": "sha256:90b68117013a8044617b567a023aa2938e3695ba48153b9a7f89b263e371a14c" }, "owner_lane": "", "provenance": { diff --git a/native/capability/aienos_capability.c b/native/capability/aienos_capability.c index c4920a4e..6b10a961 100644 --- a/native/capability/aienos_capability.c +++ b/native/capability/aienos_capability.c @@ -12,11 +12,48 @@ #include <pthread.h> #include <stdatomic.h> #include <stdbool.h> +#include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> +/* Production ABI layout (LP64: aarch64, x86_64). These values are the binary + * contract omega and every other C caller compile against; a change to any + * public struct must fail the build here first. The Rust crates use a + * different, 32-bit layout under their own aienos_cap32_* symbols. */ +_Static_assert(AIENOS_CAP_GENERATION_BITS == 64, "generation width"); +_Static_assert(sizeof(((AienosCapRef *)0)->generation) * 8 == AIENOS_CAP_GENERATION_BITS, + "AienosCapRef.generation width"); +_Static_assert(sizeof(AienosCapRef) == 16, "AienosCapRef size"); +_Static_assert(_Alignof(AienosCapRef) == 8, "AienosCapRef align"); +_Static_assert(offsetof(AienosCapRef, cap_id) == 0, "AienosCapRef.cap_id offset"); +_Static_assert(offsetof(AienosCapRef, generation) == 8, "AienosCapRef.generation offset"); +_Static_assert(sizeof(AienosCapEntry) == 88, "AienosCapEntry size"); +_Static_assert(offsetof(AienosCapEntry, cap_id) == 0, "AienosCapEntry.cap_id"); +_Static_assert(offsetof(AienosCapEntry, generation) == 8, "AienosCapEntry.generation"); +_Static_assert(offsetof(AienosCapEntry, state) == 16, "AienosCapEntry.state"); +_Static_assert(offsetof(AienosCapEntry, issuer) == 20, "AienosCapEntry.issuer"); +_Static_assert(offsetof(AienosCapEntry, subject) == 24, "AienosCapEntry.subject"); +_Static_assert(offsetof(AienosCapEntry, rights) == 28, "AienosCapEntry.rights"); +_Static_assert(offsetof(AienosCapEntry, resource) == 32, "AienosCapEntry.resource"); +_Static_assert(offsetof(AienosCapEntry, epoch) == 40, "AienosCapEntry.epoch"); +_Static_assert(offsetof(AienosCapEntry, lease_expiry) == 48, "AienosCapEntry.lease_expiry"); +_Static_assert(offsetof(AienosCapEntry, parent_id) == 56, "AienosCapEntry.parent_id"); +_Static_assert(offsetof(AienosCapEntry, parent_generation) == 64, + "AienosCapEntry.parent_generation"); +_Static_assert(offsetof(AienosCapEntry, minted_by_id) == 72, "AienosCapEntry.minted_by_id"); +_Static_assert(offsetof(AienosCapEntry, minted_by_generation) == 80, + "AienosCapEntry.minted_by_generation"); +_Static_assert(sizeof(AienosCapMint) == 64, "AienosCapMint size"); +_Static_assert(offsetof(AienosCapMint, issuer) == 0, "AienosCapMint.issuer"); +_Static_assert(offsetof(AienosCapMint, subject) == 4, "AienosCapMint.subject"); +_Static_assert(offsetof(AienosCapMint, resource) == 8, "AienosCapMint.resource"); +_Static_assert(offsetof(AienosCapMint, rights) == 16, "AienosCapMint.rights"); +_Static_assert(offsetof(AienosCapMint, lease_ticks) == 24, "AienosCapMint.lease_ticks"); +_Static_assert(offsetof(AienosCapMint, parent) == 32, "AienosCapMint.parent"); +_Static_assert(offsetof(AienosCapMint, authority) == 48, "AienosCapMint.authority"); + typedef struct { uint64_t boot_gen; uint64_t epoch; diff --git a/native/capability/aienos_capability.h b/native/capability/aienos_capability.h index aa68a85c..ba03d979 100644 --- a/native/capability/aienos_capability.h +++ b/native/capability/aienos_capability.h @@ -69,6 +69,15 @@ #define AIENOS_CAP_PARENT_NONE UINT32_MAX +/* Width of every generation field in this ABI (AienosCapRef.generation, + * AienosCapEntry.generation/parent_generation/minted_by_generation). This + * library is the production capability ABI; aienos_capability.c pins the + * struct layouts below with _Static_assert. The Rust crates + * aienos-capability and aienos-capability-ffi are a separate 32-bit + * implementation that exports aienos_cap32_* symbols and is not + * interchangeable with this one. */ +#define AIENOS_CAP_GENERATION_BITS 64 + typedef struct AienosCapAdmin AienosCapAdmin; typedef struct AienosCapView AienosCapView; diff --git a/scripts/.crumb b/scripts/.crumb index 1ddff56a..cc1fa7c4 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-11T01:27:19Z", + "compiled_at": "2026-10-11T21:20:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:768f7a9a329815979e95b540161c74912dddd8615f44f45c44f97db5d49fdd93", + "digest": "sha256:9cb7696ff81e7d3e7cc312f7249aba2f0a7e7345b9ba7b80a3ab4515bbad10d9", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 79, - "generated_at_commit": "3f791539d41d852b2b6fd36fdebaebb9f76dc2a0", - "source_tree": "sha256:82320fb628dea4ad4567b44109b30092a9c59b25dec40f572b887914a943b5ea" + "files": 80, + "generated_at_commit": "362cba9653c8021996a21b17ead00d5d1140b371", + "source_tree": "sha256:65b8e19c864e095499af61a79ad42f28fe71ef034c647bfb1fee138785c1e1f6" }, "owner_lane": "", "provenance": { diff --git a/scripts/cap_abi_isolation_test.sh b/scripts/cap_abi_isolation_test.sh new file mode 100755 index 00000000..1bd6bee7 --- /dev/null +++ b/scripts/cap_abi_isolation_test.sh @@ -0,0 +1,166 @@ +#!/usr/bin/env bash +# The native C capability authority and the Rust capability crates are two +# separate implementations with incompatible layouts: +# +# native/capability (production ABI) AienosCapRef 16 bytes, u64 generation, +# symbols aienos_cap_* +# crates/aienos-capability-ffi AienosCapRef 8 bytes, u32 generation, +# symbols aienos_cap32_* +# +# This test proves they cannot be swapped by accident. It builds both archives +# and checks, with nm and the linker: +# 1 both archives define their expected entry points (non-empty, positive) +# 2 no aienos_cap* text symbol is defined by both archives +# 3 the C archive defines no aienos_cap32_*; the Rust one no aienos_cap_* +# 4 a C program compiled against aienos_capability.h links and runs against +# the C archive, and FAILS TO LINK against the Rust archive with an +# undefined reference to aienos_cap_* +# 5 control: a C caller of the cap32 ABI links against the Rust archive with +# the same link line, and aienos_cap32_abi_layout reports 8/4/4/64, v1 +# 6 the C layout asserts reject a drifted header: a narrowed generation, and +# an equal-size reorder of AienosCapRef +# Any build failure, or any check that cannot run, fails the test (exit 1). +# No Python. Needs cc, ar, nm, cargo. +# +# bash scripts/cap_abi_isolation_test.sh +set -euo pipefail + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT +CC="${CC:-cc}" +CFLAGS="-std=gnu11 -O2 -Wall -Wextra -Werror" +fails=0 + +die() { echo "cap-abi-isolation FAIL: $*" >&2; echo "AIENOS_CAP_ABI_ISOLATION: FAIL"; exit 1; } +check() { # name, then a command that must succeed + local name="$1"; shift + if "$@"; then echo "ok $name"; else echo "FAIL $name"; fails=$((fails + 1)); fi +} + +for tool in "$CC" ar nm cargo; do + command -v "$tool" > /dev/null || die "missing tool: $tool" +done + +# ---- build both archives (any failure here is a test failure) ------------- +make -s -C "$ROOT/native/capability" OUT="$WORK/c" "$WORK/c/libaienos_capability.a" \ + > "$WORK/c-build.log" 2>&1 || { tail -20 "$WORK/c-build.log"; die "native C library build"; } +C_LIB="$WORK/c/libaienos_capability.a" + +TARGET_DIR="${CARGO_TARGET_DIR:-$ROOT/target}" +(cd "$ROOT" && cargo build --locked -p aienos-capability-ffi) \ + > "$WORK/rust-build.log" 2>&1 || { tail -20 "$WORK/rust-build.log"; die "Rust staticlib build"; } +R_LIB="$TARGET_DIR/debug/libaienos_capability_ffi.a" +[ -s "$C_LIB" ] || die "C archive missing: $C_LIB" +[ -s "$R_LIB" ] || die "Rust archive missing: $R_LIB" + +# ---- symbol sets ------------------------------------------------------------ +syms() { # defined global text symbols starting with aienos_cap + nm --defined-only -g "$1" 2> /dev/null | awk '$2 == "T" {print $3}' | + grep '^aienos_cap' | sort -u || true +} +syms "$C_LIB" > "$WORK/c.syms" +syms "$R_LIB" > "$WORK/r.syms" +echo "C archive: $(wc -l < "$WORK/c.syms") aienos_cap* text symbols" +echo "Rust archive: $(wc -l < "$WORK/r.syms") aienos_cap* text symbols" + +has() { grep -qx "$2" "$1"; } +for s in aienos_cap_start aienos_cap_stop aienos_cap_mint aienos_cap_validate aienos_cap_restart; do + check "C archive defines $s" has "$WORK/c.syms" "$s" +done +for s in aienos_cap32_start aienos_cap32_stop aienos_cap32_mint aienos_cap32_validate \ + aienos_cap32_restart aienos_cap32_abi_layout; do + check "Rust archive defines $s" has "$WORK/r.syms" "$s" +done + +comm -12 "$WORK/c.syms" "$WORK/r.syms" > "$WORK/common.syms" +check "no aienos_cap* symbol defined by both archives" test ! -s "$WORK/common.syms" +[ -s "$WORK/common.syms" ] && sed 's/^/ shared: /' "$WORK/common.syms" +check "C archive defines no aienos_cap32_*" bash -c "! grep -q '^aienos_cap32_' '$WORK/c.syms'" +check "Rust archive defines no aienos_cap_*" bash -c "! grep -q '^aienos_cap_' '$WORK/r.syms'" + +# ---- link probe: a caller of the production C ABI --------------------------- +cat > "$WORK/probe.c" << 'C' +#include <stdio.h> +#include "aienos_capability.h" +int main(void) { + AienosCapAdmin *admin = NULL; + AienosCapView *view = NULL; + if (aienos_cap_start(&admin, &view) != AIENOS_CAP_OK) return 2; + AienosCapRef office; + if (aienos_cap_office(admin, &office) != AIENOS_CAP_OK) return 3; + int rc = aienos_cap_validate(view, office, 0, AIENOS_CAP_RES_AUTHORITY, + AIENOS_CAP_RIGHT_MINT, NULL); + aienos_cap_stop(admin, view); + printf("probe sizeof(AienosCapRef)=%zu validate=%d\n", sizeof(AienosCapRef), rc); + return rc == AIENOS_CAP_OK && sizeof(AienosCapRef) == 16 ? 0 : 4; +} +C +"$CC" $CFLAGS -I "$ROOT/native/capability" -c -o "$WORK/probe.o" "$WORK/probe.c" || + die "probe compile against aienos_capability.h" + +RLINK="-lpthread -ldl -lm" +check "C-ABI probe links against the C archive" \ + "$CC" -o "$WORK/probe_c" "$WORK/probe.o" "$C_LIB" -pthread +check "C-ABI probe runs against the C archive" "$WORK/probe_c" + +# shellcheck disable=SC2086 +if "$CC" -o "$WORK/probe_r" "$WORK/probe.o" "$R_LIB" $RLINK > "$WORK/probe_r.log" 2>&1; then + echo "FAIL C-ABI probe LINKED against the Rust archive (layouts would be misread)" + fails=$((fails + 1)) +elif grep -q "undefined reference to .aienos_cap_start" "$WORK/probe_r.log" && + grep -q "undefined reference to .aienos_cap_validate" "$WORK/probe_r.log"; then + echo "ok C-ABI probe fails to link against the Rust archive (undefined aienos_cap_*)" +else + echo "FAIL C-ABI probe did not link against the Rust archive, but not for the expected reason:" + tail -5 "$WORK/probe_r.log" + fails=$((fails + 1)) +fi + +# ---- control: the same link line works for a real cap32 caller ------------- +cat > "$WORK/probe32.c" << 'C' +#include <stdint.h> +#include <stdio.h> +uint32_t aienos_cap32_abi_layout(uint32_t *out); +int main(void) { + uint32_t w[4] = {0, 0, 0, 0}; + uint32_t v = aienos_cap32_abi_layout(w); + printf("cap32 abi v%u ref=%u gen_off=%u gen_size=%u entry=%u\n", v, w[0], w[1], w[2], w[3]); + return v == 1 && w[0] == 8 && w[1] == 4 && w[2] == 4 && w[3] == 64 ? 0 : 1; +} +C +"$CC" $CFLAGS -c -o "$WORK/probe32.o" "$WORK/probe32.c" || die "cap32 probe compile" +# shellcheck disable=SC2086 +check "control: cap32 caller links against the Rust archive" \ + "$CC" -o "$WORK/probe32" "$WORK/probe32.o" "$R_LIB" $RLINK +check "control: aienos_cap32_abi_layout reports v1 8/4/4/64" "$WORK/probe32" +check "control: cap32 caller does not link against the C archive" \ + bash -c "! '$CC' -o '$WORK/probe32_c' '$WORK/probe32.o' '$C_LIB' -pthread > /dev/null 2>&1" + +# ---- the C layout asserts reject a drifted header ---------------------------- +drift() { # name, perl substitution on the header; the copy must NOT compile + local name="$1" expr="$2" d="$WORK/drift-$1" + mkdir -p "$d" + cp "$ROOT/native/capability/aienos_capability.c" "$ROOT/native/capability/aienos_capability.h" "$d/" + perl -0pi -e "$expr" "$d/aienos_capability.h" + if cmp -s "$d/aienos_capability.h" "$ROOT/native/capability/aienos_capability.h"; then + echo "FAIL drift $name: substitution did not apply"; fails=$((fails + 1)); return + fi + if "$CC" $CFLAGS -c -o "$d/x.o" "$d/aienos_capability.c" > "$d/log" 2>&1; then + echo "FAIL drift $name: compiled, layout asserts did not fire"; fails=$((fails + 1)) + elif grep -q 'static assertion failed' "$d/log"; then + echo "ok drift $name rejected by _Static_assert" + else + echo "FAIL drift $name: build failed for another reason"; tail -3 "$d/log"; fails=$((fails + 1)) + fi +} +drift narrowed-generation \ + 's/typedef struct \{\n uint32_t cap_id;\n uint64_t generation;\n\} AienosCapRef;/typedef struct {\n uint32_t cap_id;\n uint32_t generation;\n} AienosCapRef;/' +drift reordered-same-size \ + 's/typedef struct \{\n uint32_t cap_id;\n uint64_t generation;\n\} AienosCapRef;/typedef struct {\n uint64_t generation;\n uint32_t cap_id;\n} AienosCapRef;/' + +if [ "$fails" -ne 0 ]; then + echo "AIENOS_CAP_ABI_ISOLATION: FAIL ($fails checks failed)" + exit 1 +fi +echo "AIENOS_CAP_ABI_ISOLATION: PASS"