ud-emulator 0.1.4

Pure-Rust 32-bit x86 emulator + PE runtime loader + Win32 host shims. Mirrors oxideav-vfw; intended to grow into the dynamic-analysis backend that informs decompilation (indirect-target recovery, constant-data discovery).
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
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
//! `gdi32.dll` stubs — the graphics-device-interface surface a
//! VfW-class codec DLL imports.
//!
//! Codecs typically only use the `gdi32` API in their config-dialog
//! path (which we never invoke) or in palette negotiation (which
//! we sidestep by reporting a true-color "device"). All eight
//! stubs are therefore fail-soft: they return sensible defaults
//! that let any DllMain CRT init / static-constructor pass.
//!
//! Reference: MSDN `gdi32` page-by-page; cited inline next to
//! each stub.

use super::{arg_dword, HostState, Registry, StubFn, Win32Error};
use crate::emulator::{Cpu, Mmu};
use std::collections::BTreeSet;

/// Sentinel handle for `CreateCompatibleDC` / `GetDC`. Every
/// `HDC` we hand out is the same value; `DeleteDC` /
/// `ReleaseDC` validates against `HostState::valid_hdcs` (a
/// per-sandbox `BTreeSet`).
pub const SENTINEL_HDC: u32 = 0xDEAD_C011;

/// Track which `HDC` values are currently "valid" — i.e. which
/// `CreateCompatibleDC` calls have happened without a paired
/// `DeleteDC`. Stored on the [`crate::win32::HostState`] via a
/// `Box<BTreeSet<u32>>` slot kept in `host.gdi_hdcs`.
///
/// We store this as a side table here instead of adding a field
/// to `HostState` because round-1 tests don't need the surface.
/// A test that calls `gdi32::register(...)` then `CreateCompatibleDC`
/// gets the set automatically populated.
fn gdi_hdcs_mut(state: &mut HostState) -> &mut BTreeSet<u32> {
    state.gdi_hdcs.get_or_insert_with(BTreeSet::new)
}

/// Register every gdi32 stub.
pub fn register(registry: &mut Registry) {
    // https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-bitblt
    registry.register("gdi32.dll", "BitBlt", stub_bitblt as StubFn, 9);
    // https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createcompatibledc
    registry.register(
        "gdi32.dll",
        "CreateCompatibleDC",
        stub_create_compatible_dc as StubFn,
        1,
    );
    // https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-deletedc
    registry.register("gdi32.dll", "DeleteDC", stub_delete_dc as StubFn, 1);
    // https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getdevicecaps
    registry.register(
        "gdi32.dll",
        "GetDeviceCaps",
        stub_get_device_caps as StubFn,
        2,
    );
    // https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getnearestcolor
    registry.register(
        "gdi32.dll",
        "GetNearestColor",
        stub_get_nearest_color as StubFn,
        2,
    );
    // https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getobjecta
    registry.register("gdi32.dll", "GetObjectA", stub_get_object_a as StubFn, 3);
    // https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getsystempaletteentries
    registry.register(
        "gdi32.dll",
        "GetSystemPaletteEntries",
        stub_get_system_palette_entries as StubFn,
        4,
    );
    // https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-selectobject
    registry.register("gdi32.dll", "SelectObject", stub_select_object as StubFn, 2);

    // ---- Round-47 additions: msadds32.ax PE-load surface --------
    //
    // After r46 (user32!{SetTimer, KillTimer}) the splitter
    // (`msadds32.ax`) advances its `gdi32` import-table walk to
    // `StretchDIBits` — the splitter's headless render-out path.
    // The codec sandbox never actually paints a pixel; the named
    // import just needs to resolve to a thunk before
    // `Sandbox::load` returns the [`Image`].
    //
    // https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-stretchdibits
    // int StretchDIBits(HDC hdc, int xDest, int yDest, int
    //   DestWidth, int DestHeight, int xSrc, int ySrc, int
    //   SrcWidth, int SrcHeight, const VOID *lpBits, const
    //   BITMAPINFO *lpbmi, UINT iUsage, DWORD rop) — 13 args.
    registry.register(
        "gdi32.dll",
        "StretchDIBits",
        stub_stretch_dibits as StubFn,
        13,
    );

    // ---- Codec-corpus probe addition ----------------------------
    //
    // Cinepak (`iccvid`) frees a GDI object in its config-dialog
    // teardown. The sandbox hands out no real GDI handles, so
    // `DeleteObject` is a no-op that reports success.
    // https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-deleteobject
    registry.register("gdi32.dll", "DeleteObject", stub_delete_object as StubFn, 1);
}

/// `BOOL DeleteObject(HGDIOBJ ho)`. No-op returning TRUE.
fn stub_delete_object(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &Registry,
) -> Result<u32, Win32Error> {
    Ok(1)
}

/// `BOOL BitBlt(HDC, int, int, int, int, HDC, int, int, DWORD)`.
/// No-op returning TRUE. Codecs typically only use this in their
/// config-dialog path, which we never invoke.
fn stub_bitblt(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &Registry,
) -> Result<u32, Win32Error> {
    Ok(1)
}

/// `HDC CreateCompatibleDC(HDC hdc)`. Returns the sentinel HDC
/// + records it in the live-set so `DeleteDC` validates.
fn stub_create_compatible_dc(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    state: &mut HostState,
    _registry: &Registry,
) -> Result<u32, Win32Error> {
    gdi_hdcs_mut(state).insert(SENTINEL_HDC);
    Ok(SENTINEL_HDC)
}

/// `BOOL DeleteDC(HDC hdc)`. Removes from the live set; returns
/// TRUE.
fn stub_delete_dc(
    cpu: &mut Cpu,
    mmu: &mut Mmu,
    state: &mut HostState,
    _registry: &Registry,
) -> Result<u32, Win32Error> {
    let h = arg_dword(cpu, mmu, 0).map_err(|t| crate::win32::trap_to_win32_local("DeleteDC", t))?;
    gdi_hdcs_mut(state).remove(&h);
    Ok(1)
}

// `wingdi.h` GetDeviceCaps indices.
const DRIVERVERSION: u32 = 0;
const TECHNOLOGY: u32 = 2;
const HORZSIZE: u32 = 4;
const VERTSIZE: u32 = 6;
const HORZRES: u32 = 8;
const VERTRES: u32 = 10;
const BITSPIXEL: u32 = 12;
const PLANES: u32 = 14;
const NUMBRUSHES: u32 = 16;
const NUMPENS: u32 = 18;
const NUMFONTS: u32 = 22;
const NUMCOLORS: u32 = 24;
const RASTERCAPS: u32 = 38;
const LOGPIXELSX: u32 = 88;
const LOGPIXELSY: u32 = 90;
const SIZEPALETTE: u32 = 104;
const NUMRESERVED: u32 = 106;
const COLORRES: u32 = 108;

/// `int GetDeviceCaps(HDC hdc, int index)`. Returns sensible
/// "true-color VGA" defaults — see MSDN `GetDeviceCaps` for the
/// index → return-value table.
fn stub_get_device_caps(
    cpu: &mut Cpu,
    mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &Registry,
) -> Result<u32, Win32Error> {
    let _hdc = arg_dword(cpu, mmu, 0)
        .map_err(|t| crate::win32::trap_to_win32_local("GetDeviceCaps", t))?;
    let idx = arg_dword(cpu, mmu, 1)
        .map_err(|t| crate::win32::trap_to_win32_local("GetDeviceCaps", t))?;
    Ok(match idx {
        DRIVERVERSION => 0x0400,
        TECHNOLOGY => 1, // DT_RASDISPLAY
        HORZSIZE => 320,
        VERTSIZE => 240,
        HORZRES => 1024,
        VERTRES => 768,
        BITSPIXEL => 32,
        PLANES => 1,
        NUMBRUSHES => 0,
        NUMPENS => 16,
        NUMFONTS => 0,
        NUMCOLORS => 0,
        // RC_BITBLT | RC_BITMAP64 | RC_DI_BITMAP | RC_DIBTODEV |
        // RC_GDI20_OUTPUT | RC_PALETTE | RC_STRETCHBLT | RC_STRETCHDIB.
        RASTERCAPS => 0x0000_19F1,
        LOGPIXELSX => 96,
        LOGPIXELSY => 96,
        SIZEPALETTE => 0,
        NUMRESERVED => 20,
        COLORRES => 24,
        _ => 0,
    })
}

/// `COLORREF GetNearestColor(HDC hdc, COLORREF crColor)`. Return
/// the input color unchanged (we have a true-color "device").
fn stub_get_nearest_color(
    cpu: &mut Cpu,
    mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &Registry,
) -> Result<u32, Win32Error> {
    let _hdc = arg_dword(cpu, mmu, 0)
        .map_err(|t| crate::win32::trap_to_win32_local("GetNearestColor", t))?;
    let color = arg_dword(cpu, mmu, 1)
        .map_err(|t| crate::win32::trap_to_win32_local("GetNearestColor", t))?;
    Ok(color)
}

/// `int GetObjectA(HGDIOBJ hgdiobj, int cbBuffer, LPVOID lpvObject)`.
/// Stub failure: returns 0. Codecs that hit this path are doing
/// palette negotiation we're not driving.
fn stub_get_object_a(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &Registry,
) -> Result<u32, Win32Error> {
    Ok(0)
}

/// `UINT GetSystemPaletteEntries(HDC hdc, UINT iStart, UINT cEntries,
/// LPPALETTEENTRY lppe)`. Stub failure: returns 0.
fn stub_get_system_palette_entries(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &Registry,
) -> Result<u32, Win32Error> {
    Ok(0)
}

/// `HGDIOBJ SelectObject(HDC hdc, HGDIOBJ hObject)`. No-op:
/// return the input `hObject` unchanged.
fn stub_select_object(
    cpu: &mut Cpu,
    mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &Registry,
) -> Result<u32, Win32Error> {
    let _hdc =
        arg_dword(cpu, mmu, 0).map_err(|t| crate::win32::trap_to_win32_local("SelectObject", t))?;
    let h =
        arg_dword(cpu, mmu, 1).map_err(|t| crate::win32::trap_to_win32_local("SelectObject", t))?;
    Ok(h)
}

/// `int StretchDIBits(HDC hdc, int xDest, int yDest, int DestWidth,
/// int DestHeight, int xSrc, int ySrc, int SrcWidth, int SrcHeight,
/// const VOID *lpBits, const BITMAPINFO *lpbmi, UINT iUsage,
/// DWORD rop)` — fail-soft.
///
/// Per MSDN the call copies colour data from a source DIB to a
/// destination rectangle and returns the number of scanlines
/// copied (`GDI_ERROR` on failure).  The codec sandbox never owns
/// a real DC and never composites a final frame — `msadds32.ax`
/// is the audio splitter and only pulls this import as part of
/// its statically-linked render-out surface, never invokes it on
/// the decode path we drive.  We therefore return the caller's
/// `DestHeight` so any "scanlines > 0 == success" probe sees a
/// satisfied contract.
fn stub_stretch_dibits(
    cpu: &mut Cpu,
    mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &Registry,
) -> Result<u32, Win32Error> {
    // We touch only the args we care about; the rest are pulled
    // through `arg_dword` so a stack-bounds trap surfaces as a
    // proper Win32Error rather than a silent under-read.
    let _hdc = arg_dword(cpu, mmu, 0)
        .map_err(|t| crate::win32::trap_to_win32_local("StretchDIBits", t))?;
    let _x_dest = arg_dword(cpu, mmu, 1)
        .map_err(|t| crate::win32::trap_to_win32_local("StretchDIBits", t))?;
    let _y_dest = arg_dword(cpu, mmu, 2)
        .map_err(|t| crate::win32::trap_to_win32_local("StretchDIBits", t))?;
    let _dest_width = arg_dword(cpu, mmu, 3)
        .map_err(|t| crate::win32::trap_to_win32_local("StretchDIBits", t))?;
    let dest_height = arg_dword(cpu, mmu, 4)
        .map_err(|t| crate::win32::trap_to_win32_local("StretchDIBits", t))?;
    let _x_src = arg_dword(cpu, mmu, 5)
        .map_err(|t| crate::win32::trap_to_win32_local("StretchDIBits", t))?;
    let _y_src = arg_dword(cpu, mmu, 6)
        .map_err(|t| crate::win32::trap_to_win32_local("StretchDIBits", t))?;
    let _src_width = arg_dword(cpu, mmu, 7)
        .map_err(|t| crate::win32::trap_to_win32_local("StretchDIBits", t))?;
    let _src_height = arg_dword(cpu, mmu, 8)
        .map_err(|t| crate::win32::trap_to_win32_local("StretchDIBits", t))?;
    let _lp_bits = arg_dword(cpu, mmu, 9)
        .map_err(|t| crate::win32::trap_to_win32_local("StretchDIBits", t))?;
    let _lpbmi = arg_dword(cpu, mmu, 10)
        .map_err(|t| crate::win32::trap_to_win32_local("StretchDIBits", t))?;
    let _i_usage = arg_dword(cpu, mmu, 11)
        .map_err(|t| crate::win32::trap_to_win32_local("StretchDIBits", t))?;
    let _rop = arg_dword(cpu, mmu, 12)
        .map_err(|t| crate::win32::trap_to_win32_local("StretchDIBits", t))?;
    // MSDN: "the return value is the number of scanlines copied".
    // Reporting the caller's `DestHeight` satisfies any
    // "scanlines > 0 == success" probe at the call site.  If the
    // caller passed 0 (degenerate), echo 0 — still a non-error
    // outcome, since `GDI_ERROR` is the explicit failure marker
    // and we never want to surface that from a fail-soft stub.
    Ok(dest_height)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::emulator::mmu::Perm;
    use crate::emulator::regs::Reg32;

    fn make_env() -> (Cpu, Mmu, Registry, HostState) {
        let mut mmu = Mmu::new();
        mmu.map(0x4000, 0x4000, Perm::R | Perm::W);
        mmu.map(0x9000, 0x1000, Perm::R | Perm::W);
        let mut cpu = Cpu::new();
        cpu.regs.set_esp(0x9F00);
        let mut registry = Registry::new();
        registry.register_kernel32();
        registry.register_gdi32();
        let state = HostState::new(0x4000, 0x8000);
        (cpu, mmu, registry, state)
    }

    fn call(
        cpu: &mut Cpu,
        mmu: &mut Mmu,
        registry: &Registry,
        state: &mut HostState,
        dll: &str,
        name: &str,
        args: &[u32],
    ) -> Result<(), crate::Error> {
        for a in args.iter().rev() {
            cpu.push32(mmu, *a)?;
        }
        cpu.push32(mmu, 0xDEAD_DEAD)?;
        cpu.regs.eip = registry.resolve(dll, name).expect("registered");
        crate::win32::dispatch_stub(cpu, mmu, registry, state)
    }

    #[test]
    fn create_compatible_dc_returns_sentinel() {
        let (mut cpu, mut mmu, registry, mut state) = make_env();
        call(
            &mut cpu,
            &mut mmu,
            &registry,
            &mut state,
            "gdi32.dll",
            "CreateCompatibleDC",
            &[0],
        )
        .unwrap();
        assert_eq!(cpu.regs.get32(Reg32::Eax), SENTINEL_HDC);
    }

    #[test]
    fn create_then_delete_dc_roundtrips() {
        let (mut cpu, mut mmu, registry, mut state) = make_env();
        call(
            &mut cpu,
            &mut mmu,
            &registry,
            &mut state,
            "gdi32.dll",
            "CreateCompatibleDC",
            &[0],
        )
        .unwrap();
        let h = cpu.regs.get32(Reg32::Eax);
        call(
            &mut cpu,
            &mut mmu,
            &registry,
            &mut state,
            "gdi32.dll",
            "DeleteDC",
            &[h],
        )
        .unwrap();
        assert_eq!(cpu.regs.get32(Reg32::Eax), 1);
    }

    #[test]
    fn get_device_caps_bitspixel_is_32() {
        let (mut cpu, mut mmu, registry, mut state) = make_env();
        call(
            &mut cpu,
            &mut mmu,
            &registry,
            &mut state,
            "gdi32.dll",
            "GetDeviceCaps",
            &[SENTINEL_HDC, BITSPIXEL],
        )
        .unwrap();
        assert_eq!(cpu.regs.get32(Reg32::Eax), 32);
    }

    #[test]
    fn get_nearest_color_is_identity() {
        let (mut cpu, mut mmu, registry, mut state) = make_env();
        call(
            &mut cpu,
            &mut mmu,
            &registry,
            &mut state,
            "gdi32.dll",
            "GetNearestColor",
            &[SENTINEL_HDC, 0x12_3456],
        )
        .unwrap();
        assert_eq!(cpu.regs.get32(Reg32::Eax), 0x12_3456);
    }

    #[test]
    fn select_object_returns_input_unchanged() {
        let (mut cpu, mut mmu, registry, mut state) = make_env();
        call(
            &mut cpu,
            &mut mmu,
            &registry,
            &mut state,
            "gdi32.dll",
            "SelectObject",
            &[SENTINEL_HDC, 0xCAFE_BABE],
        )
        .unwrap();
        assert_eq!(cpu.regs.get32(Reg32::Eax), 0xCAFE_BABE);
    }

    #[test]
    fn stretch_dibits_returns_dest_height() {
        let (mut cpu, mut mmu, registry, mut state) = make_env();
        // 13 args: hdc, xDest, yDest, DestWidth, DestHeight,
        //          xSrc, ySrc, SrcWidth, SrcHeight,
        //          lpBits, lpbmi, iUsage, rop.
        call(
            &mut cpu,
            &mut mmu,
            &registry,
            &mut state,
            "gdi32.dll",
            "StretchDIBits",
            &[
                SENTINEL_HDC,
                0,
                0,
                352,
                288,
                0,
                0,
                352,
                288,
                0,
                0,
                0,
                0x00CC_0020,
            ],
        )
        .unwrap();
        assert_eq!(
            cpu.regs.get32(Reg32::Eax),
            288,
            "StretchDIBits should echo DestHeight as the scanline count"
        );
    }
}