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-platform-linux / tests / external.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 | //! `LinuxUnifiedBuffer::from_external`: a buffer over memory another allocator owns releases |
| 17 | //! through its callback exactly once and never unmaps. |
| 18 | |
| 19 | use aien_platform::{DeviceAddress, PlatformError, UnifiedBuffer}; |
| 20 | use aien_platform_linux::{LinuxMemoryKind, LinuxUnifiedBuffer}; |
| 21 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 22 | use std::sync::Arc; |
| 23 | |
| 24 | #[test] |
| 25 | fn external_buffer_releases_once_through_its_callback() { |
| 26 | let mut backing = vec![7u8; 4096]; |
| 27 | let released = Arc::new(AtomicUsize::new(0)); |
| 28 | let r = released.clone(); |
| 29 | let ptr = backing.as_mut_ptr(); |
| 30 | // SAFETY: `backing` outlives the buffer; nothing else frees it. |
| 31 | let mut buf = unsafe { |
| 32 | LinuxUnifiedBuffer::from_external( |
| 33 | ptr, |
| 34 | backing.len(), |
| 35 | DeviceAddress(ptr as u64), |
| 36 | Box::new(move || { |
| 37 | r.fetch_add(1, Ordering::SeqCst); |
| 38 | }), |
| 39 | ) |
| 40 | } |
| 41 | .expect("external buffer"); |
| 42 | assert_eq!(buf.kind(), LinuxMemoryKind::External); |
| 43 | assert_eq!(buf.len(), 4096); |
| 44 | assert_eq!(buf.as_ptr(), ptr as *const u8); |
| 45 | assert_eq!(buf.device_address(), DeviceAddress(ptr as u64)); |
| 46 | // SAFETY: inside the 4096 bytes of `backing`. |
| 47 | unsafe { *buf.as_mut_ptr().add(10) = 9 }; |
| 48 | assert_eq!( |
| 49 | released.load(Ordering::SeqCst), |
| 50 | 0, |
| 51 | "not released while alive" |
| 52 | ); |
| 53 | drop(buf); |
| 54 | assert_eq!( |
| 55 | released.load(Ordering::SeqCst), |
| 56 | 1, |
| 57 | "released exactly once on drop" |
| 58 | ); |
| 59 | assert_eq!(backing[10], 9, "the writes went to the external memory"); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn external_buffer_refuses_null_and_empty() { |
| 64 | let mut backing = [0u8; 16]; |
| 65 | // SAFETY: a null pointer and a zero length are refused before any use. |
| 66 | let null = unsafe { |
| 67 | LinuxUnifiedBuffer::from_external( |
| 68 | std::ptr::null_mut(), |
| 69 | 16, |
| 70 | DeviceAddress(0), |
| 71 | Box::new(|| {}), |
| 72 | ) |
| 73 | }; |
| 74 | assert!(matches!(null, Err(PlatformError::InvalidLayout))); |
| 75 | let empty = unsafe { |
| 76 | LinuxUnifiedBuffer::from_external( |
| 77 | backing.as_mut_ptr(), |
| 78 | 0, |
| 79 | DeviceAddress(0), |
| 80 | Box::new(|| {}), |
| 81 | ) |
| 82 | }; |
| 83 | assert!(matches!(empty, Err(PlatformError::InvalidLayout))); |
| 84 | } |