locum 0.1.6

Captures everything a Direct3D 11 game draws in a frame, to look at, query, change and replay: d3d11.dll and dxgi.dll beside the game that watch every call on its way to the real runtime.
Documentation
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
//! The real `d3d11.dll` and `dxgi.dll`, loaded from the system folder by full path: our own `d3d11.dll` and
//! `dxgi.dll` are loaded under those names first, so a bare name gives back ours.

use core::ffi::c_void;
use std::os::windows::ffi::OsStrExt;
use std::path::PathBuf;
use std::sync::OnceLock;

use windows::Win32::Foundation::HMODULE;
use windows::Win32::System::Diagnostics::Debug::RtlCaptureStackBackTrace;
use windows::Win32::System::LibraryLoader::{
    GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, GetModuleFileNameW,
    GetModuleHandleExW, GetModuleHandleW, GetProcAddress, LOAD_LIBRARY_FLAGS, LoadLibraryExW,
};
use windows::Win32::System::SystemInformation::GetSystemDirectoryW;
use windows::core::{PCSTR, PCWSTR};

use crate::log::log;

pub struct System {
    pub folder: PathBuf,
    pub d3d11: HMODULE,
    pub dxgi: HMODULE,
}

// Module handles are process-wide and immutable once loaded.
unsafe impl Send for System {}
unsafe impl Sync for System {}

pub fn system() -> &'static System {
    static SYSTEM: OnceLock<System> = OnceLock::new();
    SYSTEM.get_or_init(|| {
        let mut buf = [0u16; 260];
        let n = unsafe { GetSystemDirectoryW(Some(&mut buf)) } as usize;
        let folder = PathBuf::from(String::from_utf16_lossy(&buf[..n]));
        // Windows gives a DLL asked for by name the module already loaded under that name. A game that loads one of
        // ours only later, by name (Unreal Engine 5 delay-loads d3d11.dll), would be given the real one loaded below,
        // and its device would never reach locum: so ours beside locum.dll go in first.
        if let Some(own) = module_of(system as *const c_void).map(module_path)
            && let Some(own) = own.parent()
        {
            for name in ["d3d11.dll", "dxgi.dll", "d3d12.dll"] {
                if own.join(name).is_file() && !same_folder(own, &folder) {
                    load(own, name);
                }
            }
        }
        let d3d11 = load(&folder, "d3d11.dll").unwrap_or_default();
        let dxgi = load(&folder, "dxgi.dll").unwrap_or_default();
        System { folder, d3d11, dxgi }
    })
}

/// The real `d3d12.dll`, loaded from the system folder the first time a D3D12 export is called (a D3D11 game never
/// gets it). NVAPI is hidden first (see [`hide_nvapi`]).
pub fn system_d3d12() -> HMODULE {
    static D3D12: OnceLock<usize> = OnceLock::new();
    HMODULE(*D3D12.get_or_init(|| {
        hide_nvapi();
        load(&system().folder, "d3d12.dll").map_or(0, |h| h.0 as usize)
    }) as *mut c_void)
}

/// Puts in locum's `nvapi64.dll`, which exports nothing, before the game loads NVAPI by name, so the game and NVIDIA
/// Aftermath find no NVAPI: its Direct3D 12 functions take the driver's own objects, and given locum's wrappers the
/// driver crashes reading their internals.
fn hide_nvapi() {
    let own = module_of(system as *const c_void)
        .map(module_path)
        .and_then(|p| p.parent().map(std::path::Path::to_path_buf));
    if let Ok(loaded) = unsafe { GetModuleHandleW(windows::core::w!("nvapi64.dll")) } {
        let path = module_path(loaded);
        if own
            .as_deref()
            .is_none_or(|own| path.parent().is_none_or(|p| !same_folder(p, own)))
        {
            log!(
                "NVAPI was loaded before Direct3D 12 ({}): its D3D12 functions get locum's wrappers",
                path.display()
            );
        }
        return;
    }
    match own {
        Some(own) if own.join("nvapi64.dll").is_file() => {
            if load(&own, "nvapi64.dll").is_some() {
                log!("NVAPI hidden: locum's nvapi64.dll stands in for it");
            }
        }
        _ => log!(
            "no nvapi64.dll beside locum.dll: a game using NVAPI's Direct3D 12 functions gets locum's wrappers"
        ),
    }
}

/// Whether two folders are the same (Windows paths compare without case).
fn same_folder(a: &std::path::Path, b: &std::path::Path) -> bool {
    a.to_string_lossy().eq_ignore_ascii_case(&b.to_string_lossy())
}

/// Loads `name` from `folder` by full path.
fn load(folder: &std::path::Path, name: &str) -> Option<HMODULE> {
    let path = folder.join(name);
    let wide: Vec<u16> = path.as_os_str().encode_wide().chain(Some(0)).collect();
    match unsafe { LoadLibraryExW(PCWSTR(wide.as_ptr()), None, LOAD_LIBRARY_FLAGS(0)) } {
        Ok(h) => {
            log!("loaded {} at {:p}", path.display(), h.0);
            Some(h)
        }
        Err(e) => {
            log!("FAILED to load {}: {e}", path.display());
            None
        }
    }
}

/// Loads a DLL from the system folder (once; later calls give the same module).
pub fn load_system_dll(name: &'static str) -> Option<HMODULE> {
    static LOADED: std::sync::Mutex<Vec<(&'static str, usize)>> = std::sync::Mutex::new(Vec::new());
    let mut loaded = LOADED.lock().unwrap_or_else(|e| e.into_inner());
    if let Some(&(_, h)) = loaded.iter().find(|(n, _)| *n == name) {
        return Some(HMODULE(h as *mut c_void));
    }
    let h = load(&system().folder, name)?;
    loaded.push((name, h.0 as usize));
    Some(h)
}

/// The address of `name` in `module`, as the function type `F`.
///
/// Overlays can redirect a system DLL's exports to their own code (Epic Online Services' overlay does, for
/// `D3D11CreateDevice`), and their code then calls the export they found under the bare name, which is ours: an
/// address outside `module` is therefore not used, but the export's own, read from the DLL's file.
///
/// # Safety
/// `F` must be the export's real function pointer type.
pub unsafe fn export<F: Copy>(module: HMODULE, name: &'static core::ffi::CStr) -> Option<F> {
    assert_eq!(size_of::<F>(), size_of::<usize>());
    let address = checked_export(module, Export::Name(name))?;
    Some(unsafe { core::mem::transmute_copy(&address) })
}

/// Resolves a numbered export with the same overlay-redirection check as named exports.
pub fn export_ordinal(module: HMODULE, ordinal: u16) -> Option<usize> {
    checked_export(module, Export::Ordinal(ordinal))
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Export {
    Name(&'static core::ffi::CStr),
    Ordinal(u16),
}

impl core::fmt::Display for Export {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Name(name) => write!(f, "{}", name.to_string_lossy()),
            Self::Ordinal(ordinal) => write!(f, "ordinal {ordinal}"),
        }
    }
}

fn checked_export(module: HMODULE, export: Export) -> Option<usize> {
    let name = match export {
        Export::Name(name) => PCSTR(name.as_ptr().cast()),
        Export::Ordinal(ordinal) => PCSTR(usize::from(ordinal) as *const u8),
    };
    let found = unsafe { GetProcAddress(module, name) }? as usize;
    checked_address(module, export, found)
}

fn checked_address(module: HMODULE, export: Export, found: usize) -> Option<usize> {
    let base = module.0 as usize;
    let size = unsafe { image_size(module) };
    let address = if (base..base.checked_add(size)?).contains(&found) {
        found
    } else {
        let own = own_export(module, export)?;
        static LOGGED: std::sync::Mutex<Vec<(usize, Export)>> = std::sync::Mutex::new(Vec::new());
        let mut logged = LOGGED.lock().unwrap_or_else(|e| e.into_inner());
        if !logged.contains(&(base, export)) {
            logged.push((base, export));
            let by = module_of(found as *const c_void).map(module_path);
            log!(
                "{} of {} leads to {:#x} in {}: called at its own address {:#x} instead",
                export,
                module_path(module).display(),
                found,
                by.map_or("no module".into(), |p| p.display().to_string()),
                own
            );
        }
        own
    };
    Some(address)
}

/// The size of a loaded module's image, from its PE headers.
///
/// # Safety
/// `module` must be a loaded module.
unsafe fn image_size(module: HMODULE) -> usize {
    let base = module.0 as *const u8;
    unsafe {
        let pe = base.add(base.add(0x3c).cast::<u32>().read_unaligned() as usize);
        // SizeOfImage: the optional header (after the 4-byte signature and 20-byte file header), offset 56.
        pe.add(24 + 56).cast::<u32>().read_unaligned() as usize
    }
}

/// The address of `name` in `module` as the module's file exports it (what its in-memory export table said before
/// anything changed it). None for an export forwarded to another DLL or not found.
fn own_export(module: HMODULE, export: Export) -> Option<usize> {
    let file = std::fs::read(module_path(module)).ok()?;
    let rva = pe_export_rva(&file, export)?;
    (rva != 0 && (rva as usize) < unsafe { image_size(module) })
        .then(|| (module.0 as usize).checked_add(rva as usize))?
}

/// The RVA of an exported function in a PE32+ file's bytes, or None (forwarded, absent or malformed).
fn pe_export_rva(file: &[u8], export: Export) -> Option<u32> {
    let u16_at = |o: usize| Some(u16::from_le_bytes(file.get(o..o + 2)?.try_into().ok()?));
    let u32_at = |o: usize| Some(u32::from_le_bytes(file.get(o..o + 4)?.try_into().ok()?));
    let pe = u32_at(0x3c)? as usize;
    if file.get(pe..pe + 4)? != b"PE\0\0" {
        return None;
    }
    let sections = u16_at(pe + 6)? as usize;
    let optional = pe + 24;
    let optional_size = u16_at(pe + 20)? as usize;
    if u16_at(optional)? != 0x20b {
        return None;
    }
    // Data directory 0 (exports) of a PE32+ optional header.
    let (dir, dir_size) = (u32_at(optional + 112)?, u32_at(optional + 116)?);
    let table = optional + optional_size;
    let offset = |rva: u32| -> Option<usize> {
        (0..sections).find_map(|i| {
            let s = table + i * 40;
            let (size, address, raw) = (u32_at(s + 8)?, u32_at(s + 12)?, u32_at(s + 20)?);
            (address..address.checked_add(size)?)
                .contains(&rva)
                .then(|| (rva - address + raw) as usize)
        })
    };
    let d = offset(dir)?;
    let (count, functions, names, ordinals) = (
        u32_at(d + 24)? as usize,
        u32_at(d + 28)?,
        u32_at(d + 32)?,
        u32_at(d + 36)?,
    );
    let functions = offset(functions)?;
    let index = match export {
        Export::Ordinal(ordinal) => u32::from(ordinal).checked_sub(u32_at(d + 16)?)?,
        Export::Name(name) => {
            let name = name.to_bytes();
            let (names, ordinals) = (offset(names)?, offset(ordinals)?);
            let index = (0..count).find(|&i| {
                u32_at(names + 4 * i)
                    .and_then(offset)
                    .and_then(|o| file.get(o..o + name.len() + 1))
                    .is_some_and(|s| &s[..name.len()] == name && s[name.len()] == 0)
            })?;
            u32::from(u16_at(ordinals + 2 * index)?)
        }
    };
    if index >= u32_at(d + 20)? {
        return None;
    }
    let rva = u32_at(functions + 4 * index as usize)?;
    // An address inside the export directory names another DLL's export, not code.
    (rva != 0 && !(dir..dir.checked_add(dir_size)?).contains(&rva)).then_some(rva)
}

/// The address the function calling this one will return to, i.e. its caller's code.
#[inline(never)]
pub fn caller_of_caller() -> *const c_void {
    let mut frames = [core::ptr::null_mut::<c_void>(); 1];
    // Frame 0 is this function, 1 the export that called it, 2 that export's caller.
    let n = unsafe { RtlCaptureStackBackTrace(2, &mut frames, None) };
    if n == 1 { frames[0] } else { core::ptr::null() }
}

/// The module containing `address` (no reference taken), if any.
pub fn module_of(address: *const c_void) -> Option<HMODULE> {
    let mut module = HMODULE::default();
    let flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
    unsafe { GetModuleHandleExW(flags, PCWSTR(address.cast()), &mut module) }.ok()?;
    Some(module)
}

pub fn module_path(module: HMODULE) -> PathBuf {
    let mut buf = [0u16; 1024];
    let n = unsafe { GetModuleFileNameW(Some(module), &mut buf) } as usize;
    PathBuf::from(String::from_utf16_lossy(&buf[..n]))
}

/// The runtime's own modules a game may ship beside itself: the D3D12 Agility SDK's runtime and debug layer, and
/// WARP's.
const RUNTIME_MODULES: [&str; 3] = ["d3d12core.dll", "d3d12sdklayers.dll", "d3d10warp.dll"];

/// Whether code at `address` belongs to Windows, the graphics driver or the D3D runtime rather than the game: such
/// callers (the real d3d11.dll asking dxgi for a factory, an Agility SDK D3D12Core.dll in the game's folder asking for
/// one) get the real objects, unwrapped.
pub fn is_system_caller(address: *const c_void) -> bool {
    let Some(module) = module_of(address) else {
        return false;
    };
    is_system_module(
        &module_path(module).to_string_lossy(),
        &system().folder.to_string_lossy(),
    )
}

/// Whether the module at `path` is Windows' (in the system folder `system`), the graphics driver's or the runtime's.
fn is_system_module(path: &str, system: &str) -> bool {
    let path = path.to_lowercase();
    let name = path.rsplit(['\\', '/']).next().unwrap_or_default();
    path.starts_with(&system.to_lowercase())
        || path.contains("\\driverstore\\")
        || RUNTIME_MODULES.contains(&name)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// An Agility SDK runtime in the game's folder is the runtime, not the game.
    #[test]
    fn the_runtime_beside_a_game_is_the_system() {
        let system = r"C:\Windows\system32";
        for (path, expected) in [
            (r"C:\Windows\System32\d3d11.dll", true),
            (
                r"C:\Windows\System32\DriverStore\FileRepository\nv.inf_amd64\nvwgf2umx.dll",
                true,
            ),
            (r"D:\Games\Game\D3D12\D3D12Core.dll", true),
            (r"D:\Games\Game\D3D12\d3d12SDKLayers.dll", true),
            (r"D:\Games\Game\d3d10warp.dll", true),
            (r"D:\Games\Game\Game.exe", false),
            (r"D:\Games\Game\d3d12core.dll.bak\Game.dll", false),
        ] {
            assert_eq!(is_system_module(path, system), expected, "{path}");
        }
    }

    /// The file's export table gives the address the loader gives for an unchanged system DLL.
    #[test]
    fn own_export_matches_the_loader() {
        let folder = system_folder();
        for (dll, names) in [
            (
                "d3d11.dll",
                &[c"D3D11CreateDevice", c"D3D11CreateDeviceAndSwapChain"][..],
            ),
            ("dxgi.dll", &[c"CreateDXGIFactory1", c"CreateDXGIFactory2"][..]),
        ] {
            let module = load(&folder, dll).expect("system DLL");
            for name in names {
                let loader =
                    unsafe { GetProcAddress(module, PCSTR(name.as_ptr().cast())) }.expect("export") as usize;
                assert_eq!(
                    own_export(module, Export::Name(name)),
                    Some(loader),
                    "{dll} {name:?}"
                );
            }
            assert_eq!(own_export(module, Export::Name(c"NoSuchExport")), None);
        }
    }

    #[test]
    fn ordinal_exports_recover_from_out_of_module_redirection() {
        let module = load(&system_folder(), "d3d12.dll").expect("system D3D12");
        // Every ordinal the loader knows (which ones depends on the Windows build), and none it doesn't.
        let mut found = 0;
        for ordinal in 0..=1024 {
            let export = Export::Ordinal(ordinal);
            let loader = unsafe { GetProcAddress(module, PCSTR(usize::from(ordinal) as *const u8)) }
                .map(|f| f as usize);
            assert_eq!(own_export(module, export), loader, "ordinal {ordinal}");
            assert_eq!(export_ordinal(module, ordinal), loader);
            if let Some(loader) = loader {
                found += 1;
                // Inject the address an overlay could return, without modifying a live system module.
                let foreign = ordinal_exports_recover_from_out_of_module_redirection as *const () as usize;
                assert_eq!(checked_address(module, export, foreign), Some(loader));
            }
        }
        assert!(found > 0);
        assert_eq!(own_export(module, Export::Ordinal(u16::MAX)), None);
        let name = Export::Name(c"D3D12CreateDevice");
        assert_eq!(checked_address(module, name, 1), own_export(module, name));
    }

    fn system_folder() -> PathBuf {
        let mut buf = [0u16; 260];
        let n = unsafe { GetSystemDirectoryW(Some(&mut buf)) } as usize;
        PathBuf::from(String::from_utf16_lossy(&buf[..n]))
    }
}