Files
nvdriver/tests/software-manager/owned.rs
LIghtJUNction 32c8728037 Migrate the workspace and kernel crates to Rust edition 2024
Mechanical, behavior-neutral: edition/resolver keys, unsafe extern blocks,
unsafe(no_mangle)/unsafe(export_name), unsafe blocks inside unsafe fn,
r#gen renames, --edition flags in Makefiles/tools, rustfmt style_edition
pinned to 2021 so formatting stays edition-independent, and clippy
collapsible_if allowed workspace-wide (let-chain churn).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-12-02 01:52:38 +08:00

242 lines
8.3 KiB
Rust

#![allow(dead_code)]
#[path = "../../src/nvidia/kernel/pf_software.rs"]
mod pf_software;
#[path = "../../src/nvidia/kernel/registry.rs"]
mod registry;
#[path = "../../src/nvidia/kernel/software_manager.rs"]
mod software_manager;
#[path = "../../src/nvidia/kernel/system_os.rs"]
mod system_os;
#[path = "../../src/nvidia/kernel/system_platform.rs"]
mod system_platform;
use nv_rs_core::hypervisor::HypervisorType;
use software_manager as manager;
use std::sync::{
atomic::{AtomicU32, Ordering},
Arc, Mutex,
};
static TEST_LOCK: Mutex<()> = Mutex::new(());
unsafe extern "C" {
fn fixture_set_alloc_fail(n: u32);
fn fixture_set_free_fail(n: u32);
fn fixture_set_process(value: bool);
fn fixture_live() -> u32;
fn fixture_held() -> bool;
}
fn clean() {
unsafe {
fixture_set_alloc_fail(0);
fixture_set_free_fail(0);
fixture_set_process(true);
}
manager::shutdown().unwrap();
assert_eq!(unsafe { fixture_live() }, 0);
}
#[test]
fn every_initial_allocation_failure_retains_partial_parents_for_retry_or_abort() {
let _serial = TEST_LOCK.lock().unwrap();
clean();
for retry in [false, true] {
for nth in 1..=3 {
unsafe {
fixture_set_alloc_fail(nth);
}
assert_eq!(manager::initialize(HypervisorType::Unknown), Err(-12));
assert_eq!(manager::with(|_| Ok(())), Err(-19));
assert_eq!(unsafe { fixture_live() }, nth - 1);
unsafe {
fixture_set_alloc_fail(0);
}
if retry {
manager::initialize(HypervisorType::Kvm).unwrap();
manager::initialize(HypervisorType::Unknown).unwrap();
assert_eq!(
manager::with(|m| Ok(m.hypervisor_type)).unwrap(),
HypervisorType::Kvm
);
assert_eq!(unsafe { fixture_live() }, 3);
}
manager::shutdown().unwrap();
manager::shutdown().unwrap();
assert_eq!(unsafe { fixture_live() }, 0);
}
}
}
#[test]
fn pending_and_live_instances_block_shutdown_but_leave_cleanup_access_available() {
let _serial = TEST_LOCK.lock().unwrap();
clean();
manager::initialize(HypervisorType::Unknown).unwrap();
let pending =
manager::with(|m| Ok(m.instances.reserve().map_err(|_| -75)?.retain_for_cleanup()))
.unwrap();
assert_eq!(pending.index(), 0);
assert_eq!(manager::shutdown(), Err(-16));
manager::with(|m| {
assert_eq!(m.instances.pending_count(), 1);
match m.instances.release_pending(pending) {
Ok(()) => Ok(()),
Err(_) => Err(-5),
}
})
.unwrap();
let live = manager::with(|m| Ok(m.instances.reserve().map_err(|_| -75)?.commit())).unwrap();
assert_eq!(manager::shutdown(), Err(-16));
manager::with(|m| match m.instances.release(live) {
Ok(()) => Ok(()),
Err(_) => Err(-5),
})
.unwrap();
manager::shutdown().unwrap();
assert_eq!(unsafe { fixture_live() }, 0);
}
#[test]
fn child_references_and_each_close_failure_retain_real_parents_until_retry() {
let _serial = TEST_LOCK.lock().unwrap();
clean();
for nth in 1..=3 {
manager::initialize(HypervisorType::Unknown).unwrap();
unsafe {
fixture_set_free_fail(nth);
}
assert_eq!(manager::shutdown(), Err(-11));
assert!(unsafe { fixture_live() } > 0);
assert_eq!(manager::with(|_| Ok(())), Err(-19));
assert_eq!(manager::initialize(HypervisorType::Unknown), Err(-16));
unsafe {
fixture_set_free_fail(0);
}
manager::shutdown().unwrap();
assert_eq!(unsafe { fixture_live() }, 0);
}
manager::initialize(HypervisorType::Unknown).unwrap();
let os = manager::with(|m| m.system_os.reference()).unwrap();
assert!(!os.is_sim_mods());
assert_eq!(os.logical_base().get(), 0x1_0000_0000);
assert_eq!(manager::shutdown(), Err(-16));
drop(os);
manager::shutdown().unwrap();
manager::initialize(HypervisorType::Unknown).unwrap();
let platform = manager::with(|m| m.platform.reference()).unwrap();
assert_eq!(manager::shutdown(), Err(-16));
drop(platform);
manager::shutdown().unwrap();
assert_eq!(unsafe { fixture_live() }, 0);
}
#[test]
fn namespaces_and_timeout_ids_are_distinct_and_never_reused_after_failure_or_reinitialize() {
let _serial = TEST_LOCK.lock().unwrap();
clean();
manager::initialize(HypervisorType::Unknown).unwrap();
let (first, key) =
manager::with(|m| Ok((m.reserve_device_range()?, m.allocate_timeout_key()?))).unwrap();
let mut burned = None;
assert_eq!(
manager::with::<()>(|m| {
burned = Some((m.reserve_device_range()?, m.allocate_timeout_key()?));
Err(-5)
}),
Err(-5)
);
manager::shutdown().unwrap();
manager::initialize(HypervisorType::Unknown).unwrap();
let (next, nextkey) =
manager::with(|m| Ok((m.reserve_device_range()?, m.allocate_timeout_key()?))).unwrap();
assert_eq!(burned.unwrap().0.get(), first.get() + 0x40000);
assert_eq!(next.get(), first.get() + 0x80000);
assert_eq!(nextkey.0, key.0 + 2);
manager::shutdown().unwrap();
}
#[test]
fn real_sleepable_mutex_serializes_callbacks_and_rejects_recursive_or_atomic_access() {
let _serial = TEST_LOCK.lock().unwrap();
clean();
manager::initialize(HypervisorType::Unknown).unwrap();
unsafe {
fixture_set_process(false);
}
assert_eq!(manager::with(|_| Ok(())), Err(-11));
assert_eq!(manager::shutdown(), Err(-11));
unsafe {
fixture_set_process(true);
}
manager::with(|_| {
assert!(unsafe { fixture_held() });
assert_eq!(manager::with(|_| Ok(())), Err(-35));
Ok(())
})
.unwrap();
let active = Arc::new(AtomicU32::new(0));
let completed = Arc::new(AtomicU32::new(0));
let mut handles = Vec::new();
for _ in 0..4 {
let active = active.clone();
let completed = completed.clone();
handles.push(std::thread::spawn(move || {
for _ in 0..16 {
manager::with(|m| {
assert!(unsafe { fixture_held() });
assert_eq!(active.fetch_add(1, Ordering::AcqRel), 0);
let _id = m.allocate_timeout_key()?;
std::thread::yield_now();
assert_eq!(active.fetch_sub(1, Ordering::AcqRel), 1);
completed.fetch_add(1, Ordering::AcqRel);
Ok(())
})
.unwrap();
}
}));
}
for h in handles {
h.join().unwrap();
}
assert_eq!(completed.load(Ordering::Acquire), 64);
manager::shutdown().unwrap();
assert_eq!(unsafe { fixture_live() }, 0);
}
fn architecture_key() -> nv_rs_core::gpu::gpu_arch::Key {
nv_rs_core::gpu::gpu_arch::Key {
architecture: 0x14,
implementation: 0,
hidrev: 0,
tegra_type: 0,
}
}
#[test]
fn actual_architecture_cache_refs_and_failed_last_free_remain_owned_until_drain() {
let _serial = TEST_LOCK.lock().unwrap();
clean();
manager::initialize(HypervisorType::Unknown).unwrap();
let external = manager::with(|m| m.architectures.get_or_create(architecture_key())).unwrap();
assert_eq!(external.logical_base().unwrap().get(), 0x1_0100_0000);
assert_eq!(manager::shutdown(), Err(-16));
// Preflight did not mutate ready state or drop cache ownership.
manager::with(|m| {
assert!(m.architectures.has_external_references()?);
Ok(())
})
.unwrap();
drop(external);
manager::shutdown().unwrap();
assert_eq!(unsafe { fixture_live() }, 0);
for nth in 1..=4 {
manager::initialize(HypervisorType::Unknown).unwrap();
manager::with(|m| {
drop(m.architectures.get_or_create(architecture_key())?);
Ok(())
})
.unwrap();
assert_eq!(unsafe { fixture_live() }, 4);
unsafe {
fixture_set_free_fail(nth);
}
assert_eq!(manager::shutdown(), Err(-11));
assert!(unsafe { fixture_live() } > 0);
unsafe {
fixture_set_free_fail(0);
}
manager::shutdown().unwrap();
assert_eq!(unsafe { fixture_live() }, 0);
}
}