univdreams 0.4.0

Byte-identical binary <-> source round-tripping: binary formats, multi-arch decode/encode, a .ud decompiler/compiler, and an x86 emulator with Win16/Win32/Linux hosts.
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
485
486
//! `CoreFoundation.dll` (Apple's CF port for Windows) stubs.
//!
//! Apple's QuickTime 7.7.9 for Windows ships CF as part of the
//! Apple Application Support installer. qts/qtcf reach it via
//! `LoadLibraryA("CoreFoundation.dll")` + `GetProcAddress` —
//! it's never a static import. Real CF's DllMain on Windows
//! drags in libdispatch and ICU, and that chain crashes our
//! vanilla PE loader (write-protect violations between
//! libdispatch and libicuuc, then a final ret-to-stack-address
//! out of CF's `_DllMainCRTStartup` after ICU's string init).
//!
//! Rather than emulate that whole framework convention we
//! host-stub the small surface qtcf actually calls. The PE
//! loader skips loading CF (via [`crate::emulator::win32::is_host_stub_dll`])
//! and the synthetic handle pre-seeded in
//! [`crate::emulator::Sandbox::new`]'s `state.modules` lets
//! `GetModuleHandleA("CoreFoundation.dll")` return non-NULL.
//! `GetProcAddress` then resolves names through this stub set.
//!
//! Reference: Apple's open-source CF (libraries `kCFAllocator*`,
//! `CFString*`, `CFArray*`, `CFDictionary*`, `CFNumber*`,
//! `CFRunLoop*`, `CFType*`) —
//! `https://opensource.apple.com/source/CF/`.

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

/// Magic word that lives at the head of every synthetic CF
/// allocation. Lets a future stub check "is this pointer a
/// CFTypeRef we minted?" without keeping a side table.
const CF_TAG: u32 = 0x4346_5453; // "CFTS"

/// Synthetic [`CFTypeID`] values handed back by `CFGetTypeID`.
/// Apple's headers don't promise these are stable across
/// releases, so any unique non-zero integer suffices.
const CF_TYPE_ID_STRING: u32 = 0x0001;
const CF_TYPE_ID_NUMBER: u32 = 0x0002;
const CF_TYPE_ID_BOOLEAN: u32 = 0x0003;
const CF_TYPE_ID_DICTIONARY: u32 = 0x0004;
const CF_TYPE_ID_ARRAY: u32 = 0x0005;
const CF_TYPE_ID_DATE: u32 = 0x0006;
const CF_TYPE_ID_DATA: u32 = 0x0007;
const CF_TYPE_ID_URL: u32 = 0x0008;
const CF_TYPE_ID_BUNDLE: u32 = 0x0009;
const CF_TYPE_ID_UUID: u32 = 0x000A;

/// Register every CoreFoundation host stub.
pub fn register(registry: &mut Registry) {
    let dll = "corefoundation.dll";

    // ---- Allocators ------------------------------------------------
    registry.register_data(dll, "kCFAllocatorDefault", 0);
    registry.register_data(dll, "kCFAllocatorSystemDefault", 0);
    registry.register_data(dll, "kCFAllocatorMalloc", 0);
    registry.register_data(dll, "kCFAllocatorMallocZone", 0);
    registry.register_data(dll, "kCFAllocatorNull", 0);
    registry.register_data(dll, "kCFAllocatorUseContext", 0);
    registry.register(
        dll,
        "CFAllocatorAllocate",
        stub_allocator_allocate as StubFn,
        0,
    );
    registry.register(dll, "CFAllocatorDeallocate", stub_zero as StubFn, 0);
    registry.register(dll, "CFAllocatorGetDefault", stub_returns_zero as StubFn, 0);
    registry.register(
        dll,
        "CFAllocatorGetTypeID",
        stub_type_id_string as StubFn,
        0,
    );

    // ---- Strings ---------------------------------------------------
    registry.register(
        dll,
        "__CFStringMakeConstantString",
        stub_make_constant_string as StubFn,
        0,
    );
    registry.register(
        dll,
        "CFStringCreateWithCString",
        stub_string_create as StubFn,
        0,
    );
    registry.register(
        dll,
        "CFStringCreateWithCStringNoCopy",
        stub_string_create as StubFn,
        0,
    );
    registry.register(
        dll,
        "CFStringCreateWithCharacters",
        stub_string_create as StubFn,
        0,
    );
    registry.register(
        dll,
        "CFStringCreateWithBytes",
        stub_string_create as StubFn,
        0,
    );
    registry.register(dll, "CFStringCreateCopy", stub_returns_arg1 as StubFn, 0);
    registry.register(dll, "CFStringGetTypeID", stub_type_id_string as StubFn, 0);
    registry.register(dll, "CFStringGetLength", stub_returns_zero as StubFn, 0);
    registry.register(dll, "CFStringGetCStringPtr", stub_returns_zero as StubFn, 0);
    registry.register(
        dll,
        "CFStringGetCString",
        stub_string_get_cstring as StubFn,
        0,
    );
    registry.register(dll, "CFStringCompare", stub_returns_zero as StubFn, 0);

    // ---- Numbers / Booleans ---------------------------------------
    registry.register_data(dll, "kCFBooleanTrue", 0);
    registry.register_data(dll, "kCFBooleanFalse", 0);
    registry.register(dll, "CFNumberCreate", stub_string_create as StubFn, 0);
    registry.register(dll, "CFNumberGetTypeID", stub_type_id_number as StubFn, 0);
    registry.register(dll, "CFBooleanGetTypeID", stub_type_id_boolean as StubFn, 0);
    registry.register(dll, "CFNumberGetValue", stub_zero as StubFn, 0);

    // ---- Dictionary / Array ---------------------------------------
    registry.register_data(dll, "kCFTypeDictionaryKeyCallBacks", 0);
    registry.register_data(dll, "kCFTypeDictionaryValueCallBacks", 0);
    registry.register_data(dll, "kCFCopyStringDictionaryKeyCallBacks", 0);
    registry.register_data(dll, "kCFTypeArrayCallBacks", 0);
    registry.register(dll, "CFDictionaryCreate", stub_string_create as StubFn, 0);
    registry.register(
        dll,
        "CFDictionaryCreateMutable",
        stub_string_create as StubFn,
        0,
    );
    registry.register(dll, "CFDictionaryGetValue", stub_returns_zero as StubFn, 0);
    registry.register(dll, "CFDictionarySetValue", stub_zero as StubFn, 0);
    registry.register(dll, "CFDictionaryAddValue", stub_zero as StubFn, 0);
    registry.register(
        dll,
        "CFDictionaryGetTypeID",
        stub_type_id_dictionary as StubFn,
        0,
    );
    registry.register(dll, "CFArrayCreate", stub_string_create as StubFn, 0);
    registry.register(dll, "CFArrayCreateMutable", stub_string_create as StubFn, 0);
    registry.register(dll, "CFArrayAppendValue", stub_zero as StubFn, 0);
    registry.register(dll, "CFArrayGetCount", stub_returns_zero as StubFn, 0);
    registry.register(
        dll,
        "CFArrayGetValueAtIndex",
        stub_returns_zero as StubFn,
        0,
    );
    registry.register(dll, "CFArrayGetTypeID", stub_type_id_array as StubFn, 0);

    // ---- Data ------------------------------------------------------
    registry.register(dll, "CFDataCreate", stub_string_create as StubFn, 0);
    registry.register(
        dll,
        "CFDataCreateWithBytesNoCopy",
        stub_string_create as StubFn,
        0,
    );
    registry.register(dll, "CFDataGetBytePtr", stub_returns_zero as StubFn, 0);
    registry.register(dll, "CFDataGetLength", stub_returns_zero as StubFn, 0);
    registry.register(dll, "CFDataGetTypeID", stub_type_id_data as StubFn, 0);

    // ---- Date ------------------------------------------------------
    registry.register(dll, "CFDateCreate", stub_string_create as StubFn, 0);
    registry.register(dll, "CFDateGetTypeID", stub_type_id_date as StubFn, 0);

    // ---- URL -------------------------------------------------------
    registry.register(
        dll,
        "CFURLCreateWithString",
        stub_string_create as StubFn,
        0,
    );
    registry.register(
        dll,
        "CFURLCreateWithFileSystemPath",
        stub_string_create as StubFn,
        0,
    );
    registry.register(dll, "CFURLGetTypeID", stub_type_id_url as StubFn, 0);

    // ---- UUID / Bundle --------------------------------------------
    registry.register(dll, "CFUUIDCreate", stub_string_create as StubFn, 0);
    registry.register(dll, "CFUUIDGetTypeID", stub_type_id_uuid as StubFn, 0);
    registry.register(dll, "CFBundleCreate", stub_string_create as StubFn, 0);
    registry.register(dll, "CFBundleGetMainBundle", stub_returns_zero as StubFn, 0);
    registry.register(
        dll,
        "CFBundleGetBundleWithIdentifier",
        stub_returns_zero as StubFn,
        0,
    );
    registry.register(dll, "CFBundleCopyBundleURL", stub_returns_zero as StubFn, 0);
    registry.register(dll, "CFBundleGetTypeID", stub_type_id_bundle as StubFn, 0);

    // ---- Generic CFType -------------------------------------------
    registry.register(dll, "CFRetain", stub_returns_arg0 as StubFn, 0);
    registry.register(dll, "CFRelease", stub_zero as StubFn, 0);
    registry.register(dll, "CFGetRetainCount", stub_returns_one as StubFn, 0);
    registry.register(dll, "CFGetTypeID", stub_type_id_string as StubFn, 0);
    registry.register(
        dll,
        "CFCopyTypeIDDescription",
        stub_string_create as StubFn,
        0,
    );
    registry.register(dll, "CFEqual", stub_cf_equal as StubFn, 0);
    registry.register(dll, "CFHash", stub_returns_zero as StubFn, 0);

    // ---- RunLoop (no-op) ------------------------------------------
    registry.register(dll, "CFRunLoopGetCurrent", stub_returns_one as StubFn, 0);
    registry.register(dll, "CFRunLoopGetMain", stub_returns_one as StubFn, 0);
    registry.register(dll, "CFRunLoopRun", stub_zero as StubFn, 0);
    registry.register(dll, "CFRunLoopStop", stub_zero as StubFn, 0);
    registry.register(dll, "CFRunLoopRunInMode", stub_returns_one as StubFn, 0);

    // ---- Preferences (return NULL — no plist) ---------------------
    registry.register(
        dll,
        "CFPreferencesCopyAppValue",
        stub_returns_zero as StubFn,
        0,
    );
    registry.register(dll, "CFPreferencesSetAppValue", stub_zero as StubFn, 0);
    registry.register(dll, "CFPreferencesAppSynchronize", stub_zero as StubFn, 0);

    // ---- Locale / Timezone (return NULL — formatter takes default) -
    registry.register(dll, "CFLocaleCopyCurrent", stub_returns_zero as StubFn, 0);
    registry.register(dll, "CFTimeZoneCopySystem", stub_returns_zero as StubFn, 0);

    // ---- Notification center (no-op) ------------------------------
    registry.register(
        dll,
        "CFNotificationCenterGetLocalCenter",
        stub_returns_one as StubFn,
        0,
    );
    registry.register(
        dll,
        "CFNotificationCenterAddObserver",
        stub_zero as StubFn,
        0,
    );
    registry.register(
        dll,
        "CFNotificationCenterRemoveObserver",
        stub_zero as StubFn,
        0,
    );
    registry.register(
        dll,
        "CFNotificationCenterPostNotification",
        stub_zero as StubFn,
        0,
    );
}

// ---- stubs --------------------------------------------------------

/// Allocate a synthetic `CFTypeRef` from the heap arena, big
/// enough to hold the CF magic tag and a type id.
fn synth_cf_object(state: &mut HostState, mmu: &mut Mmu, type_id: u32) -> Result<u32, Win32Error> {
    let addr = state.arena_alloc(16)?;
    mmu.store32(addr, CF_TAG)
        .map_err(|t| super::trap_to_win32_local("CF synth_object", t))?;
    mmu.store32(addr.wrapping_add(4), type_id)
        .map_err(|t| super::trap_to_win32_local("CF synth_object", t))?;
    Ok(addr)
}

/// `CFStringRef __CFStringMakeConstantString(const char *cstr)`.
/// Real CF interns the string in a process-wide cache; the
/// analyser doesn't need that — return a fresh synthetic
/// CFStringRef every call. qtcf/qts treat the result as opaque.
fn stub_make_constant_string(
    _cpu: &mut Cpu,
    mmu: &mut Mmu,
    state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    synth_cf_object(state, mmu, CF_TYPE_ID_STRING)
}

/// Generic factory for the typed CF*Create family. Returns a
/// fresh synthetic CFTypeRef; callers never inspect the contents.
fn stub_string_create(
    _cpu: &mut Cpu,
    mmu: &mut Mmu,
    state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    synth_cf_object(state, mmu, CF_TYPE_ID_STRING)
}

/// `void *CFAllocatorAllocate(CFAllocatorRef, CFIndex size, CFOptionFlags)`.
/// Delegate to the heap arena.
fn stub_allocator_allocate(
    cpu: &mut Cpu,
    mmu: &mut Mmu,
    state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    let _alloc =
        arg_dword(cpu, mmu, 0).map_err(|t| super::trap_to_win32_local("CFAllocatorAllocate", t))?;
    let size =
        arg_dword(cpu, mmu, 1).map_err(|t| super::trap_to_win32_local("CFAllocatorAllocate", t))?;
    if size == 0 {
        return Ok(0);
    }
    let addr = state.arena_alloc(size)?;
    Ok(addr)
}

/// `Boolean CFStringGetCString(CFStringRef, char *buffer, CFIndex max, CFStringEncoding)`.
/// Write an empty C string into the buffer and return TRUE so
/// callers fall through to the "got a name" path with `strlen` 0.
fn stub_string_get_cstring(
    cpu: &mut Cpu,
    mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    let buf =
        arg_dword(cpu, mmu, 1).map_err(|t| super::trap_to_win32_local("CFStringGetCString", t))?;
    if buf != 0 {
        mmu.store8(buf, 0)
            .map_err(|t| super::trap_to_win32_local("CFStringGetCString", t))?;
    }
    Ok(1)
}

/// `Boolean CFEqual(CFTypeRef a, CFTypeRef b)`. Pointer equality
/// suffices for the synthetic objects we hand out.
fn stub_cf_equal(
    cpu: &mut Cpu,
    mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    let a = arg_dword(cpu, mmu, 0).map_err(|t| super::trap_to_win32_local("CFEqual", t))?;
    let b = arg_dword(cpu, mmu, 1).map_err(|t| super::trap_to_win32_local("CFEqual", t))?;
    Ok(if a == b { 1 } else { 0 })
}

// ---- generic helpers ----------------------------------------------

fn stub_zero(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    Ok(0)
}

fn stub_returns_zero(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    Ok(0)
}

fn stub_returns_one(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    Ok(1)
}

fn stub_returns_arg0(
    cpu: &mut Cpu,
    mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    arg_dword(cpu, mmu, 0).map_err(|t| super::trap_to_win32_local("CF stub", t))
}

fn stub_returns_arg1(
    cpu: &mut Cpu,
    mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    arg_dword(cpu, mmu, 1).map_err(|t| super::trap_to_win32_local("CF stub", t))
}

fn stub_type_id_string(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    Ok(CF_TYPE_ID_STRING)
}

fn stub_type_id_number(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    Ok(CF_TYPE_ID_NUMBER)
}

fn stub_type_id_boolean(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    Ok(CF_TYPE_ID_BOOLEAN)
}

fn stub_type_id_dictionary(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    Ok(CF_TYPE_ID_DICTIONARY)
}

fn stub_type_id_array(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    Ok(CF_TYPE_ID_ARRAY)
}

fn stub_type_id_date(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    Ok(CF_TYPE_ID_DATE)
}

fn stub_type_id_data(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    Ok(CF_TYPE_ID_DATA)
}

fn stub_type_id_url(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    Ok(CF_TYPE_ID_URL)
}

fn stub_type_id_uuid(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    Ok(CF_TYPE_ID_UUID)
}

fn stub_type_id_bundle(
    _cpu: &mut Cpu,
    _mmu: &mut Mmu,
    _state: &mut HostState,
    _registry: &mut Registry,
) -> Result<u32, Win32Error> {
    Ok(CF_TYPE_ID_BUNDLE)
}