keyhog-scanner 0.5.73

keyhog-scanner: high-performance SIMD-accelerated secret detection engine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
//! Lazy CUDA, Metal, and WGPU acquisition behind the scanner GPU boundary.

use crate::hw_probe::ScanBackend;
use std::sync::{Arc, OnceLock};

pub(crate) struct AcquiredGpuPeer {
    pub(crate) backend: Arc<dyn vyre::VyreBackend>,
    pub(crate) device_identity: Option<String>,
    pub(crate) is_software: bool,
    // Populated and read only by the GPU dispatch path. A build without the
    // `gpu` feature can never construct a peer, so these facets would be dead
    // storage in every portable binary.
    #[cfg(feature = "gpu")]
    pub(crate) resident_timed_dispatch_supported: bool,
    /// PCI vendor id where the backend exposes one; 0 means unknown.
    #[cfg(feature = "gpu")]
    pub(crate) adapter_vendor: u32,
    /// PCI device id where the backend exposes one; 0 means unknown.
    #[cfg(feature = "gpu")]
    pub(crate) adapter_device: u32,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct GpuBackendAvailability {
    pub cuda: bool,
    pub metal: bool,
    pub wgpu: bool,
}

impl GpuBackendAvailability {
    #[must_use]
    pub const fn any(self) -> bool {
        self.cuda || self.metal || self.wgpu
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct GpuBackendAcquisitionFailure {
    pub backend: &'static str,
    pub diagnostic: String,
}

pub(crate) struct GpuBackendPeers {
    cuda: OnceLock<Result<AcquiredGpuPeer, String>>,
    metal: OnceLock<Result<AcquiredGpuPeer, String>>,
    wgpu: OnceLock<Result<AcquiredGpuPeer, String>>,
    pub(crate) cuda_available: bool,
    pub(crate) metal_available: bool,
    pub(crate) wgpu_available: bool,
    pub(crate) cuda_device_identity: Option<String>,
    pub(crate) cuda_runtime_identity: Option<String>,
    pub(crate) metal_device_identity: Option<String>,
    pub(crate) metal_runtime_identity: Option<String>,
    pub(crate) wgpu_device_identity: Option<String>,
    pub(crate) wgpu_runtime_identity: Option<String>,
    pub(crate) wgpu_is_software: bool,
}

pub(crate) struct SelectedGpuPeer {
    backend: ScanBackend,
    acquisition: OnceLock<Result<AcquiredGpuPeer, String>>,
    pub(crate) available: bool,
    pub(crate) device_identity: Option<String>,
    pub(crate) runtime_identity: Option<String>,
    pub(crate) is_software: bool,
    initialization_error: Option<String>,
}

impl SelectedGpuPeer {
    pub(crate) fn new(backend: ScanBackend) -> Self {
        debug_assert!(backend.is_gpu());
        Self {
            backend,
            acquisition: OnceLock::new(),
            available: false,
            device_identity: None,
            runtime_identity: None,
            is_software: false,
            initialization_error: None,
        }
    }

    #[cfg(feature = "gpu")]
    pub(crate) fn mark_available(
        &mut self,
        device_identity: String,
        runtime_identity: Option<String>,
        is_software: bool,
    ) {
        self.available = true;
        self.device_identity = Some(device_identity);
        self.runtime_identity = runtime_identity;
        self.is_software = is_software;
        self.initialization_error = None;
    }

    pub(crate) fn mark_unavailable(&mut self, diagnostic: String) {
        self.available = false;
        self.initialization_error = Some(diagnostic);
    }

    pub(crate) fn backend(&self) -> ScanBackend {
        self.backend
    }

    pub(crate) fn get(&self, backend: ScanBackend) -> Option<&Arc<dyn vyre::VyreBackend>> {
        if backend != self.backend {
            return None;
        }
        let result = lazy_acquire(self.available, &self.acquisition, || acquire_peer(backend))?;
        match result {
            Ok(peer) => Some(&peer.backend),
            Err(error) => {
                tracing::error!(
                    target: "keyhog::routing",
                    ?backend,
                    diagnostic = %error,
                    "selected GPU backend acquisition failed"
                );
                None
            }
        }
    }

    pub(crate) fn initialized(&self, backend: ScanBackend) -> Option<&AcquiredGpuPeer> {
        if backend != self.backend {
            return None;
        }
        // LAW10: acquisition errors are retained by initialization_error and logged by get; this status accessor returns only successful peers.
        self.acquisition.get()?.as_ref().ok()
    }

    pub(crate) fn initialization_error(&self, backend: ScanBackend) -> Option<&str> {
        if backend != self.backend {
            return None;
        }
        self.acquisition
            .get()
            .and_then(|result| result.as_ref().err().map(String::as_str))
            .or(self.initialization_error.as_deref())
    }

    #[cfg(feature = "gpu")]
    pub(crate) fn resident_timed_dispatch_supported(&self, backend: ScanBackend) -> bool {
        self.initialized(backend)
            .is_some_and(|peer| peer.resident_timed_dispatch_supported)
    }
}

impl Default for GpuBackendPeers {
    fn default() -> Self {
        Self {
            cuda: OnceLock::new(),
            metal: OnceLock::new(),
            wgpu: OnceLock::new(),
            cuda_available: false,
            metal_available: false,
            wgpu_available: false,
            cuda_device_identity: None,
            metal_device_identity: None,
            metal_runtime_identity: None,
            cuda_runtime_identity: None,
            wgpu_device_identity: None,
            wgpu_runtime_identity: None,
            wgpu_is_software: false,
        }
    }
}

pub(super) fn lazy_acquire<T, E>(
    available: bool,
    slot: &OnceLock<Result<T, E>>,
    acquire: impl FnOnce() -> Result<T, E>,
) -> Option<&Result<T, E>> {
    if !available {
        return None;
    }
    Some(slot.get_or_init(acquire))
}

fn acquire_peer(backend: ScanBackend) -> Result<AcquiredGpuPeer, String> {
    match backend {
        ScanBackend::GpuCuda => acquire_cuda_peer(),
        ScanBackend::GpuMetal => acquire_metal_peer(),
        ScanBackend::GpuWgpu => acquire_wgpu_peer(),
        _ => Err(format!("{} is not a GPU backend", backend.label())),
    }
}

impl GpuBackendPeers {
    pub(crate) fn get(&self, backend: ScanBackend) -> Option<&Arc<dyn vyre::VyreBackend>> {
        let result = match backend {
            ScanBackend::GpuCuda => {
                lazy_acquire(self.cuda_available, &self.cuda, acquire_cuda_peer)
            }
            ScanBackend::GpuMetal => {
                lazy_acquire(self.metal_available, &self.metal, acquire_metal_peer)
            }
            ScanBackend::GpuWgpu => {
                lazy_acquire(self.wgpu_available, &self.wgpu, acquire_wgpu_peer)
            }
            _ => None,
        }?;
        match result {
            Ok(peer) => Some(&peer.backend),
            Err(error) => {
                tracing::error!(
                    target: "keyhog::routing",
                    ?backend,
                    diagnostic = %error,
                    "selected GPU backend acquisition failed"
                );
                None
            }
        }
    }

    pub(crate) fn initialized(&self, backend: ScanBackend) -> Option<&AcquiredGpuPeer> {
        let result = match backend {
            ScanBackend::GpuCuda => self.cuda.get(),
            ScanBackend::GpuMetal => self.metal.get(),
            ScanBackend::GpuWgpu => self.wgpu.get(),
            _ => None,
        }?;
        // LAW10: diagnostics retain the complete error in the slot; this status
        // accessor distinguishes initialized success without consuming it.
        result.as_ref().ok() // LAW10: status projection only; initialization_error retains the typed diagnostic and execution logs it before refusing this backend.
    }

    #[cfg(feature = "gpu")]
    pub(crate) fn resident_timed_dispatch_supported(&self, backend: ScanBackend) -> bool {
        self.initialized(backend)
            .is_some_and(|peer| peer.resident_timed_dispatch_supported)
    }

    pub(crate) fn initialization_error(&self, backend: ScanBackend) -> Option<&str> {
        match backend {
            ScanBackend::GpuCuda => self.cuda.get(),
            ScanBackend::GpuMetal => self.metal.get(),
            ScanBackend::GpuWgpu => self.wgpu.get(),
            _ => None,
        }
        .and_then(|result| result.as_ref().err().map(String::as_str))
    }

    pub(crate) fn availability(&self) -> GpuBackendAvailability {
        GpuBackendAvailability {
            cuda: self.cuda_available,
            metal: self.metal_available,
            wgpu: self.wgpu_available,
        }
    }
}

#[cfg(all(feature = "gpu", target_os = "linux"))]
pub(super) fn run_cuda_after_preflight<T>(
    preflight: impl FnOnce() -> Result<(), String>,
    acquire: impl FnOnce() -> Result<T, String>,
    operation: &'static str,
) -> Result<T, String> {
    preflight()?;
    std::panic::catch_unwind(std::panic::AssertUnwindSafe(acquire)).map_err(|panic| {
        format!(
            "CUDA {operation} panicked: {}. Fix: repair the CUDA driver/runtime{}",
            crate::error::panic_payload_detail(panic),
            if operation == "backend acquisition" {
                " or select another calibrated backend"
            } else {
                " before enabling this backend"
            }
        )
    })?
}

#[cfg(all(feature = "gpu", target_os = "linux"))]
fn acquire_cuda_peer() -> Result<AcquiredGpuPeer, String> {
    let backend = run_cuda_after_preflight(
        ensure_cuda_driver_library_loadable,
        || {
            let cuda = vyre_driver_cuda::backend::CudaBackend::acquire()?;
            let boxed: Box<dyn vyre::VyreBackend> =
                Box::new(vyre_driver_cuda::CudaBackendRegistration::new(cuda));
            Ok::<Arc<dyn vyre::VyreBackend>, String>(Arc::from(boxed))
        },
        "backend acquisition",
    )?;
    tracing::info!(target: "keyhog::routing", "selected CUDA peer backend acquired");
    Ok(AcquiredGpuPeer {
        backend,
        device_identity: None,
        is_software: false,
        resident_timed_dispatch_supported: true,
        // CUDA implies an NVIDIA device; the CUDA caps API does not expose a
        // PCI device id, so the device facet stays 0 (unknown).
        adapter_vendor: 0x10de,
        adapter_device: 0,
    })
}

#[cfg(not(all(feature = "gpu", target_os = "linux")))]
fn acquire_cuda_peer() -> Result<AcquiredGpuPeer, String> {
    Err("CUDA peer is not compiled for this platform".to_string())
}

#[cfg(all(feature = "gpu", target_os = "macos"))]
fn acquire_metal_peer() -> Result<AcquiredGpuPeer, String> {
    let backend = std::panic::catch_unwind(std::panic::AssertUnwindSafe(
        vyre_driver_metal::acquire,
    ))
    .map_err(|panic| {
        format!(
            "Metal backend acquisition panicked: {}. Fix: repair Metal.framework or select another calibrated backend",
            crate::error::panic_payload_detail(panic)
        )
    })?
    .map_err(|error| error.to_string())?;
    let device_identity = crate::gpu::gpu_adapter_probe()
        .map(|probe| probe.device_identity.clone())
        .ok_or_else(|| {
            "native Metal peer acquired but its exact adapter identity is unavailable".to_string()
        })?;
    tracing::info!(target: "keyhog::routing", "selected native Metal peer backend acquired");
    Ok(AcquiredGpuPeer {
        backend: Arc::from(backend),
        device_identity: Some(device_identity),
        is_software: false,
        resident_timed_dispatch_supported: false,
        // Metal implies an Apple device; the Metal acquisition API does not
        // expose a PCI device id, so the device facet stays 0 (unknown).
        adapter_vendor: 0x106b,
        adapter_device: 0,
    })
}

#[cfg(not(all(feature = "gpu", target_os = "macos")))]
fn acquire_metal_peer() -> Result<AcquiredGpuPeer, String> {
    Err("native Metal peer is not compiled for this platform".to_string())
}

#[cfg(feature = "gpu")]
fn wgpu_resident_timed_dispatch_supported(features: wgpu::Features) -> bool {
    features
        .contains(wgpu::Features::TIMESTAMP_QUERY | wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS)
}

#[cfg(feature = "gpu")]
fn acquire_wgpu_peer() -> Result<AcquiredGpuPeer, String> {
    let backend = std::panic::catch_unwind(std::panic::AssertUnwindSafe(
        vyre_driver_wgpu::WgpuBackend::shared,
    ))
    .map_err(|panic| {
        format!(
            "WGPU backend acquisition panicked: {}. Fix: repair the graphics driver/runtime or select another calibrated backend",
            crate::error::panic_payload_detail(panic)
        )
    })?
    .map_err(|error| error.to_string())?;
    let info = backend.adapter_info();
    let device_identity =
        crate::gpu::gpu_adapter_device_identity(info, backend.device_limits().max_buffer_size);
    let is_software = crate::gpu::is_software_adapter(info);
    let resident_timed_dispatch_supported =
        wgpu_resident_timed_dispatch_supported(backend.device_queue().0.features());
    tracing::info!(
        target: "keyhog::routing",
        device_identity,
        "selected WGPU peer backend acquired"
    );
    let adapter_vendor = info.vendor;
    let adapter_device = info.device;
    let backend: Arc<dyn vyre::VyreBackend> = backend;
    Ok(AcquiredGpuPeer {
        backend,
        device_identity: Some(device_identity),
        is_software,
        resident_timed_dispatch_supported,
        adapter_vendor,
        adapter_device,
    })
}

#[cfg(not(feature = "gpu"))]
fn acquire_wgpu_peer() -> Result<AcquiredGpuPeer, String> {
    Err("WGPU peer is not compiled in this build".to_string())
}

#[cfg(all(feature = "gpu", target_os = "linux"))]
pub(crate) fn load_dynamic_library(name: &std::ffi::CStr) -> Result<(), String> {
    // SAFETY: `name` is NUL-terminated for `dlopen`; a successful handle is
    // closed before returning, and `dlerror` remains valid until the next
    // dynamic-loader call on this thread.
    unsafe {
        let handle = libc::dlopen(name.as_ptr(), libc::RTLD_NOW | libc::RTLD_LOCAL);
        if handle.is_null() {
            let error = libc::dlerror();
            let detail = if error.is_null() {
                "unknown dynamic-loader error".to_owned()
            } else {
                std::ffi::CStr::from_ptr(error)
                    .to_string_lossy()
                    .into_owned()
            };
            return Err(format!("{}: {detail}", name.to_string_lossy()));
        }
        libc::dlclose(handle);
    }
    Ok(())
}

#[cfg(all(feature = "gpu", target_os = "linux"))]
fn ensure_cuda_driver_library_loadable() -> Result<(), String> {
    let first_error = match load_dynamic_library(c"libcuda.so.1") {
        Ok(()) => return Ok(()),
        Err(error) => error,
    };
    let second_error = match load_dynamic_library(c"libcuda.so") {
        Ok(()) => return Ok(()),
        Err(error) => error,
    };
    Err(format!(
        "CUDA driver library is unavailable ({second_error}; first attempt: {first_error}). Fix: install or expose the NVIDIA driver libcuda.so before enabling this backend"
    ))
}

#[cfg(all(feature = "gpu", target_os = "linux"))]
pub(crate) fn probe_cuda_peer() -> Result<vyre_driver_cuda::device::CudaDeviceCaps, String> {
    run_cuda_after_preflight(
        ensure_cuda_driver_library_loadable,
        || vyre_driver_cuda::device::CudaDeviceCaps::probe(0).map_err(|error| error.to_string()),
        "device probe",
    )
}

#[cfg(feature = "gpu")]
pub fn enumerate_gpu_device_census() -> Result<crate::gpu::device_set::GpuAdapterCensus, String> {
    let mut failures = Vec::new();
    let mut exposures = match crate::gpu::adapter_probe::probe_wgpu_device_exposures() {
        Ok(exposures) => exposures,
        Err(reason) => {
            failures.push(crate::gpu::device_set::GpuApiCensusFailure {
                api: crate::gpu::device_set::GpuApi::Wgpu,
                reason,
            });
            Vec::new()
        }
    };
    #[cfg(all(feature = "gpu", target_os = "linux"))]
    match run_cuda_after_preflight(
        ensure_cuda_driver_library_loadable,
        vyre_driver_cuda::device::CudaDeviceCaps::probe_all,
        "device census",
    ) {
        Ok(caps) => {
            exposures
                .try_reserve(caps.len())
                .map_err(|error| format!("CUDA device census reserve failed: {error}"))?;
            let runtime_identity = cuda_runtime_identity();
            for caps in caps {
                let topology = cuda_pci_bus_id(caps.ordinal).ok().and_then(|pci_bus_id| {
                    crate::gpu::adapter_probe::linux_nvidia_pci_identity(&pci_bus_id)
                });
                let (physical_identity, topology_identity, device_id, os_driver) = topology
                    .unwrap_or_else(|| {
                        let fallback = format!("cuda:ordinal={}", caps.ordinal);
                        (fallback.clone(), fallback, 0, "unavailable".to_string())
                    });
                let mut ineligible_reason = None;
                if physical_identity.starts_with("cuda:ordinal=") {
                    ineligible_reason =
                        Some("stable CUDA PCI topology identity is unavailable".to_string());
                }
                if runtime_identity.is_err() {
                    ineligible_reason =
                        Some("exact CUDA driver/runtime identity is unavailable".to_string());
                }
                exposures.push(crate::gpu::device_set::GpuDeviceExposure {
                    api: crate::gpu::device_set::GpuApi::Cuda,
                    api_ordinal: caps.ordinal,
                    physical_identity,
                    topology_identity,
                    name: caps.name,
                    vendor_id: 0x10de,
                    device_id,
                    driver_identity: format!(
                        "os-driver={os_driver};{}",
                        runtime_identity.as_deref().unwrap_or("runtime-unavailable")
                    ),
                    runtime_identity: format!(
                        "vyre-cuda={};{}",
                        env!("KEYHOG_VYRE_CUDA_VERSION"),
                        runtime_identity.as_deref().unwrap_or("runtime-unavailable")
                    ),
                    capacity_bytes: caps.total_memory,
                    is_software: false,
                    is_display_only: false,
                    ineligible_reason,
                });
            }
        }
        Err(reason) => failures.push(crate::gpu::device_set::GpuApiCensusFailure {
            api: crate::gpu::device_set::GpuApi::Cuda,
            reason,
        }),
    }
    let mut census = crate::gpu::device_set::deduplicate_gpu_exposures(exposures)?;
    census.failures = failures;
    Ok(census)
}

#[cfg(all(feature = "gpu", target_os = "linux"))]
fn cuda_runtime_identity() -> Result<String, String> {
    let version = std::fs::read_to_string("/proc/driver/nvidia/version")
        .map_err(|error| format!("cannot read NVIDIA driver identity: {error}"))?;
    let normalized = version.split_whitespace().collect::<Vec<_>>().join(" ");
    if normalized.is_empty() {
        Err("NVIDIA driver identity is empty".to_string())
    } else {
        Ok(format!("nvidia-kernel:{normalized}"))
    }
}

#[cfg(all(feature = "gpu", target_os = "linux"))]
fn cuda_pci_bus_id(ordinal: usize) -> Result<String, String> {
    type CuInit = unsafe extern "C" fn(u32) -> i32;
    type CuDeviceGet = unsafe extern "C" fn(*mut i32, i32) -> i32;
    type CuDeviceGetPciBusId = unsafe extern "C" fn(*mut std::ffi::c_char, i32, i32) -> i32;

    struct DriverHandle(*mut std::ffi::c_void);
    impl Drop for DriverHandle {
        fn drop(&mut self) {
            // SAFETY: the handle came from a successful `dlopen` in this function.
            unsafe {
                libc::dlclose(self.0);
            }
        }
    }

    unsafe fn symbol<T: Copy>(
        handle: *mut std::ffi::c_void,
        name: &std::ffi::CStr,
    ) -> Result<T, String> {
        // SAFETY: `handle` is live and `name` is NUL-terminated.
        let address = unsafe { libc::dlsym(handle, name.as_ptr()) };
        if address.is_null() {
            return Err(format!(
                "CUDA driver symbol {} is unavailable",
                name.to_string_lossy()
            ));
        }
        if std::mem::size_of::<T>() != std::mem::size_of_val(&address) {
            return Err(
                "CUDA driver function pointer has an unsupported representation".to_string(),
            );
        }
        // SAFETY: CUDA's stable driver ABI defines each requested symbol with `T`.
        Ok(unsafe { std::mem::transmute_copy(&address) })
    }

    let mut first_error = None;
    let handle = [c"libcuda.so.1", c"libcuda.so"]
        .into_iter()
        .find_map(|name| {
            // SAFETY: each static C string is NUL-terminated.
            let handle = unsafe { libc::dlopen(name.as_ptr(), libc::RTLD_NOW | libc::RTLD_LOCAL) };
            if handle.is_null() {
                if first_error.is_none() {
                    first_error = Some(name.to_string_lossy().into_owned());
                }
                None
            } else {
                Some(DriverHandle(handle))
            }
        })
        .ok_or_else(|| {
            format!(
                "CUDA driver library is unavailable while resolving PCI identity{}",
                first_error
                    .as_deref()
                    .map(|name| format!(" (first attempted {name})"))
                    .unwrap_or_default()
            )
        })?;
    // SAFETY: the symbols are resolved from the live CUDA driver handle and are
    // called with their documented driver-API signatures.
    unsafe {
        let cu_init: CuInit = symbol(handle.0, c"cuInit")?;
        let cu_device_get: CuDeviceGet = symbol(handle.0, c"cuDeviceGet")?;
        let cu_device_get_pci_bus_id: CuDeviceGetPciBusId =
            symbol(handle.0, c"cuDeviceGetPCIBusId")?;
        let init_status = cu_init(0);
        if init_status != 0 {
            return Err(format!(
                "CUDA driver initialization failed with status {init_status}"
            ));
        }
        let ordinal =
            i32::try_from(ordinal).map_err(|_| "CUDA device ordinal exceeds i32".to_string())?;
        let mut device = 0i32;
        let device_status = cu_device_get(&mut device, ordinal);
        if device_status != 0 {
            return Err(format!(
                "CUDA device {ordinal} lookup failed with status {device_status}"
            ));
        }
        let mut pci_bus_id = [0 as std::ffi::c_char; 32];
        let pci_status =
            cu_device_get_pci_bus_id(pci_bus_id.as_mut_ptr(), pci_bus_id.len() as i32, device);
        if pci_status != 0 {
            return Err(format!(
                "CUDA device {ordinal} PCI identity failed with status {pci_status}"
            ));
        }
        let pci_bus_id = std::ffi::CStr::from_ptr(pci_bus_id.as_ptr())
            .to_str()
            .map_err(|_| format!("CUDA device {ordinal} PCI identity is not UTF-8"))?
            .trim();
        if pci_bus_id.is_empty() {
            return Err(format!("CUDA device {ordinal} PCI identity is empty"));
        }
        Ok(pci_bus_id.to_string())
    }
}

#[cfg(feature = "gpu")]
/// Acquire every device in an already authenticated ordered route. Acquisition
/// is all-or-nothing; one required device failure drops successful siblings.
pub fn acquire_ordered_gpu_device_set(
    route: &crate::gpu::device_set::OrderedGpuDeviceRoute,
) -> Result<AcquiredGpuDeviceSet, String> {
    let live = enumerate_gpu_device_census()?;
    route.validate_live_set(&live)?;
    let mut devices = Vec::new();
    devices
        .try_reserve_exact(route.devices.len())
        .map_err(|error| format!("ordered GPU backend set reserve failed: {error}"))?;
    for (position, device) in route.devices.iter().enumerate() {
        let (backend, resident_timed_dispatch_supported) =
            acquire_peer_ordinal(device.api, device.api_ordinal).map_err(|error| {
                format!(
                    "required GPU device {position} acquisition failed: {error}; the ordered device-set route is invalid"
                )
            })?;
        devices.push(AcquiredGpuDevice {
            backend,
            resident_timed_dispatch_supported,
            resident_literal: std::sync::Mutex::new(
                super::resident_evidence::GpuResidentLiteralSlot::Empty,
            ),
        });
    }
    let post_acquisition = enumerate_gpu_device_census()
        .map_err(|error| format!("post-acquisition GPU census failed: {error}"))?;
    route.validate_live_set(&post_acquisition).map_err(|error| {
        format!(
            "ordered GPU device set changed during acquisition: {error}; all acquired devices were released"
        )
    })?;
    Ok(AcquiredGpuDeviceSet {
        device_set_identity_digest: route.device_set_identity_digest(),
        dispatch: std::sync::Mutex::new(()),
        devices,
    })
}

#[cfg(feature = "gpu")]
struct AcquiredGpuDevice {
    backend: Arc<dyn vyre::VyreBackend>,
    resident_timed_dispatch_supported: bool,
    resident_literal: std::sync::Mutex<super::resident_evidence::GpuResidentLiteralSlot>,
}

#[cfg(feature = "gpu")]
pub struct AcquiredGpuDeviceSet {
    device_set_identity_digest: String,
    dispatch: std::sync::Mutex<()>,
    devices: Vec<AcquiredGpuDevice>,
}

#[cfg(feature = "gpu")]
impl std::fmt::Debug for AcquiredGpuDeviceSet {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("AcquiredGpuDeviceSet")
            .field(
                "device_set_identity_digest",
                &self.device_set_identity_digest,
            )
            .field("devices", &self.devices.len())
            .finish()
    }
}

#[cfg(feature = "gpu")]
impl AcquiredGpuDeviceSet {
    #[must_use]
    pub fn len(&self) -> usize {
        self.devices.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.devices.is_empty()
    }

    pub fn device_set_identity_digest(&self) -> &str {
        &self.device_set_identity_digest
    }

    pub(crate) fn lock_complete_dispatch(&self) -> Result<std::sync::MutexGuard<'_, ()>, String> {
        self.dispatch
            .lock()
            .map_err(|_| "ordered GPU dispatch lock is unavailable after an internal panic".into())
    }

    #[must_use]
    pub fn backend(&self, position: usize) -> Option<&Arc<dyn vyre::VyreBackend>> {
        self.devices.get(position).map(|device| &device.backend)
    }

    #[must_use]
    pub(crate) fn resident_timed_dispatch_supported(&self, position: usize) -> Option<bool> {
        self.devices
            .get(position)
            .map(|device| device.resident_timed_dispatch_supported)
    }

    #[must_use]
    pub(crate) fn resident_literal(
        &self,
        position: usize,
    ) -> Option<&std::sync::Mutex<super::resident_evidence::GpuResidentLiteralSlot>> {
        self.devices
            .get(position)
            .map(|device| &device.resident_literal)
    }
}

#[cfg(feature = "gpu")]
fn acquire_peer_ordinal(
    api: crate::gpu::device_set::GpuApi,
    ordinal: usize,
) -> Result<(Arc<dyn vyre::VyreBackend>, bool), String> {
    match api {
        crate::gpu::device_set::GpuApi::Wgpu => {
            let backend = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                vyre_driver_wgpu::WgpuBackend::acquire_adapter(ordinal)
            }))
            .map_err(|panic| {
                format!(
                    "WGPU adapter {ordinal} acquisition panicked: {}",
                    crate::error::panic_payload_detail(panic)
                )
            })?
            .map_err(|error| error.to_string())?;
            let resident_timed_dispatch_supported =
                wgpu_resident_timed_dispatch_supported(backend.device_queue().0.features());
            Ok((Arc::new(backend), resident_timed_dispatch_supported))
        }
        crate::gpu::device_set::GpuApi::Cuda => {
            #[cfg(all(feature = "gpu", target_os = "linux"))]
            {
                let backend = run_cuda_after_preflight(
                    ensure_cuda_driver_library_loadable,
                    || {
                        vyre_driver_cuda::backend::CudaBackend::acquire_ordinal(ordinal).map(
                            |cuda| {
                                Arc::new(vyre_driver_cuda::CudaBackendRegistration::new(cuda))
                                    as Arc<dyn vyre::VyreBackend>
                            },
                        )
                    },
                    "ordered device acquisition",
                )?;
                Ok((backend, true))
            }
            #[cfg(not(all(feature = "gpu", target_os = "linux")))]
            {
                let _ = ordinal;
                Err("CUDA ordered-device acquisition is unavailable on this platform".to_string())
            }
        }
        crate::gpu::device_set::GpuApi::Metal => {
            if ordinal != 0 {
                return Err(format!(
                    "Metal adapter ordinal {ordinal} is not enumerable through the current VYRE API"
                ));
            }
            acquire_metal_peer().map(|peer| (peer.backend, peer.resident_timed_dispatch_supported))
        }
    }
}

#[cfg(all(test, feature = "gpu"))]
#[path = "../../../tests/unit/gpu_backend_acquisition.rs"]
mod tests;