paavohtl Claude Opus 4.6 commited on
Commit
ae122ad
·
1 Parent(s): b26d949

feat(validator): add address, struct offset, and constructor hook validation

Browse files
Files changed (1) hide show
  1. crates/openwa-validator/src/lib.rs +575 -5
crates/openwa-validator/src/lib.rs CHANGED
@@ -1,6 +1,18 @@
1
  #![allow(non_snake_case)]
2
 
3
  use std::ffi::c_void;
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  const DLL_PROCESS_ATTACH: u32 = 1;
6
 
@@ -20,11 +32,9 @@ unsafe extern "system" fn DllMain(
20
  1 // TRUE
21
  }
22
 
23
- fn run_validation() -> Result<(), Box<dyn std::error::Error>> {
24
- let _ = log_line("=== OpenWA Validator ===");
25
- let _ = log_line("DLL loaded successfully. Validation will follow.");
26
- Ok(())
27
- }
28
 
29
  fn log_line(msg: &str) -> std::io::Result<()> {
30
  use std::io::Write;
@@ -35,3 +45,563 @@ fn log_line(msg: &str) -> std::io::Result<()> {
35
  writeln!(f, "{}", msg)?;
36
  Ok(())
37
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  #![allow(non_snake_case)]
2
 
3
  use std::ffi::c_void;
4
+ use std::sync::atomic::{AtomicBool, Ordering};
5
+
6
+ use openwa_types::address::va;
7
+ use openwa_types::task::{CTask, CGameTask};
8
+ use openwa_types::ddgame::DDGame;
9
+ use openwa_types::ddgame_wrapper::DDGameWrapper;
10
+
11
+ use retour::static_detour;
12
+
13
+ // ---------------------------------------------------------------------------
14
+ // DllMain
15
+ // ---------------------------------------------------------------------------
16
 
17
  const DLL_PROCESS_ATTACH: u32 = 1;
18
 
 
32
  1 // TRUE
33
  }
34
 
35
+ // ---------------------------------------------------------------------------
36
+ // Logging
37
+ // ---------------------------------------------------------------------------
 
 
38
 
39
  fn log_line(msg: &str) -> std::io::Result<()> {
40
  use std::io::Write;
 
45
  writeln!(f, "{}", msg)?;
46
  Ok(())
47
  }
48
+
49
+ fn clear_log() -> std::io::Result<()> {
50
+ std::fs::write("OpenWA_validation.log", "")?;
51
+ Ok(())
52
+ }
53
+
54
+ // ---------------------------------------------------------------------------
55
+ // ValidationResult
56
+ // ---------------------------------------------------------------------------
57
+
58
+ struct ValidationResult {
59
+ pass: u32,
60
+ fail: u32,
61
+ }
62
+
63
+ impl ValidationResult {
64
+ fn new() -> Self {
65
+ Self { pass: 0, fail: 0 }
66
+ }
67
+
68
+ fn check(&mut self, name: &str, ok: bool, detail: &str) {
69
+ if ok {
70
+ self.pass += 1;
71
+ let _ = log_line(&format!("[PASS] {} - {}", name, detail));
72
+ } else {
73
+ self.fail += 1;
74
+ let _ = log_line(&format!("[FAIL] {} - {}", name, detail));
75
+ }
76
+ }
77
+
78
+ fn total(&self) -> u32 {
79
+ self.pass + self.fail
80
+ }
81
+
82
+ fn summary_line(&self) -> String {
83
+ format!(
84
+ "Results: {}/{} passed, {} failed",
85
+ self.pass,
86
+ self.total(),
87
+ self.fail
88
+ )
89
+ }
90
+ }
91
+
92
+ // ---------------------------------------------------------------------------
93
+ // Memory helpers
94
+ // ---------------------------------------------------------------------------
95
+
96
+ #[inline]
97
+ unsafe fn read_u32(addr: u32) -> u32 {
98
+ *(addr as *const u32)
99
+ }
100
+
101
+ #[inline]
102
+ unsafe fn read_u8(addr: u32) -> u8 {
103
+ *(addr as *const u8)
104
+ }
105
+
106
+ #[inline]
107
+ fn is_in_text(addr: u32) -> bool {
108
+ addr >= va::TEXT_START && addr <= va::TEXT_END
109
+ }
110
+
111
+ #[inline]
112
+ fn is_in_rdata(addr: u32) -> bool {
113
+ addr >= va::RDATA_START && addr < va::DATA_START
114
+ }
115
+
116
+ // ---------------------------------------------------------------------------
117
+ // Task 3: Address validation
118
+ // ---------------------------------------------------------------------------
119
+
120
+ fn validate_addresses(result: &mut ValidationResult) {
121
+ let _ = log_line("");
122
+ let _ = log_line("--- Address Validation ---");
123
+
124
+ // Vtable addresses and their names
125
+ let vtables: &[(&str, u32)] = &[
126
+ ("CTask vtable", va::CTASK_VTABLE),
127
+ ("CGameTask vtable", va::CGAMETASK_VTABLE),
128
+ ("CGameTask vtable2", va::CGAMETASK_VTABLE2),
129
+ ("DDGameWrapper vtable", va::DDGAME_WRAPPER_VTABLE),
130
+ ("GfxHandler vtable", va::GFX_HANDLER_VTABLE),
131
+ ("DisplayGfx vtable", va::DISPLAY_GFX_VTABLE),
132
+ ("PCLandscape vtable", va::PC_LANDSCAPE_VTABLE),
133
+ ("LandscapeShader vtable", va::LANDSCAPE_SHADER_VTABLE),
134
+ ("DSSound vtable", va::DS_SOUND_VTABLE),
135
+ ("TaskStateMachine vtable", va::TASK_STATE_MACHINE_VTABLE),
136
+ ("OpenGLCPU vtable", va::OPENGL_CPU_VTABLE),
137
+ ("WaterEffect vtable", va::WATER_EFFECT_VTABLE),
138
+ ];
139
+
140
+ // 1. Vtable location checks
141
+ let _ = log_line("");
142
+ let _ = log_line(" Vtable location checks (.rdata range):");
143
+ for (name, addr) in vtables {
144
+ let in_rdata = is_in_rdata(*addr);
145
+ result.check(
146
+ &format!("{} location", name),
147
+ in_rdata,
148
+ &format!("0x{:08X} {}", addr, if in_rdata { "in .rdata" } else { "NOT in .rdata" }),
149
+ );
150
+ }
151
+
152
+ // 2. Vtable content checks: first entry should be a .text pointer
153
+ let _ = log_line("");
154
+ let _ = log_line(" Vtable first-entry checks (should point to .text):");
155
+ for (name, addr) in vtables {
156
+ unsafe {
157
+ let first_entry = read_u32(*addr);
158
+ let in_text = is_in_text(first_entry);
159
+ result.check(
160
+ &format!("{} first entry", name),
161
+ in_text,
162
+ &format!(
163
+ "[0x{:08X}] = 0x{:08X} {}",
164
+ addr,
165
+ first_entry,
166
+ if in_text { "in .text" } else { "NOT in .text" }
167
+ ),
168
+ );
169
+ }
170
+ }
171
+
172
+ // 3. CTask vtable method verification
173
+ let _ = log_line("");
174
+ let _ = log_line(" CTask vtable method verification:");
175
+ let ctask_vt_methods: &[(&str, u32, u32)] = &[
176
+ ("vt0 init", 0, va::CTASK_VT0_INIT),
177
+ ("vt1 Free", 4, va::CTASK_VT1_FREE),
178
+ ("vt2 HandleMessage", 8, va::CTASK_VT2_HANDLE_MESSAGE),
179
+ ("vt3", 12, va::CTASK_VT3),
180
+ ("vt4", 16, va::CTASK_VT4),
181
+ ("vt5", 20, va::CTASK_VT5),
182
+ ("vt6", 24, va::CTASK_VT6),
183
+ ("vt7 ProcessFrame", 28, va::CTASK_VT7_PROCESS_FRAME),
184
+ ];
185
+ for (name, offset, expected) in ctask_vt_methods {
186
+ unsafe {
187
+ let actual = read_u32(va::CTASK_VTABLE + offset);
188
+ let ok = actual == *expected;
189
+ result.check(
190
+ &format!("CTask::{}", name),
191
+ ok,
192
+ &format!(
193
+ "vtable+0x{:02X}: expected 0x{:08X}, got 0x{:08X}",
194
+ offset, expected, actual
195
+ ),
196
+ );
197
+ }
198
+ }
199
+
200
+ // 4. Function prologue checks
201
+ let _ = log_line("");
202
+ let _ = log_line(" Function prologue checks:");
203
+ let valid_prologues: &[u8] = &[
204
+ 0x55, // push ebp
205
+ 0x53, // push ebx
206
+ 0x56, // push esi
207
+ 0x57, // push edi
208
+ 0x83, // sub esp, ...
209
+ 0x8B, // mov reg, ...
210
+ 0x6A, // push imm8
211
+ 0x81, // sub esp, imm32
212
+ 0xB8, // mov eax, imm32
213
+ 0x51, // push ecx
214
+ 0x52, // push edx
215
+ ];
216
+
217
+ let functions: &[(&str, u32)] = &[
218
+ ("CTask::Constructor", va::CTASK_CONSTRUCTOR),
219
+ ("CGameTask::Constructor", va::CGAMETASK_CONSTRUCTOR),
220
+ ("DDGameWrapper::Constructor", va::CONSTRUCT_DD_GAME_WRAPPER),
221
+ ("DDGame::Constructor", va::CONSTRUCT_DD_GAME),
222
+ ("CreateExplosion", va::CREATE_EXPLOSION),
223
+ ("SpawnObject", va::SPAWN_OBJECT),
224
+ ("WeaponRelease", va::WEAPON_RELEASE),
225
+ ("FireWeapon", va::FIRE_WEAPON),
226
+ ("InitWeaponTable", va::INIT_WEAPON_TABLE),
227
+ ("BlitScreen", va::BLIT_SCREEN),
228
+ ("RenderDrawingQueue", va::RENDER_DRAWING_QUEUE),
229
+ ("DrawLandscape", va::DRAW_LANDSCAPE),
230
+ ("ConstructPCLandscape", va::CONSTRUCT_PC_LANDSCAPE),
231
+ ("ConstructDSSound", va::CONSTRUCT_DS_SOUND),
232
+ ("ShowChatMessage", va::SHOW_CHAT_MESSAGE),
233
+ ("FrontendChangeScreen", va::FRONTEND_CHANGE_SCREEN),
234
+ ("WA_MallocMemset", va::WA_MALLOC_MEMSET),
235
+ ("CTask::vt0_init", va::CTASK_VT0_INIT),
236
+ ("CTask::vt1_Free", va::CTASK_VT1_FREE),
237
+ ("CTask::vt2_HandleMessage", va::CTASK_VT2_HANDLE_MESSAGE),
238
+ ("CGameTask::vt0", va::CGAMETASK_VT0),
239
+ ("CGameTask::vt1_Free", va::CGAMETASK_VT1_FREE),
240
+ ("CGameTask::vt2_HandleMessage", va::CGAMETASK_VT2_HANDLE_MESSAGE),
241
+ ];
242
+
243
+ for (name, addr) in functions {
244
+ let in_text = is_in_text(*addr);
245
+ if !in_text {
246
+ result.check(
247
+ &format!("{} prologue", name),
248
+ false,
249
+ &format!("0x{:08X} not in .text range", addr),
250
+ );
251
+ continue;
252
+ }
253
+ unsafe {
254
+ let first_byte = read_u8(*addr);
255
+ let ok = valid_prologues.contains(&first_byte);
256
+ result.check(
257
+ &format!("{} prologue", name),
258
+ ok,
259
+ &format!(
260
+ "0x{:08X}: first byte 0x{:02X} {}",
261
+ addr,
262
+ first_byte,
263
+ if ok { "valid" } else { "UNEXPECTED" }
264
+ ),
265
+ );
266
+ }
267
+ }
268
+ }
269
+
270
+ // ---------------------------------------------------------------------------
271
+ // Task 6: offset_of! validation
272
+ // ---------------------------------------------------------------------------
273
+
274
+ fn validate_struct_offsets(result: &mut ValidationResult) {
275
+ let _ = log_line("");
276
+ let _ = log_line("--- Struct Offset Validation (offset_of!) ---");
277
+
278
+ // Helper macro to reduce boilerplate
279
+ macro_rules! check_offset {
280
+ ($result:expr, $struct:ty, $field:ident, $expected:expr) => {
281
+ let actual = core::mem::offset_of!($struct, $field);
282
+ $result.check(
283
+ &format!("{}::{}", stringify!($struct), stringify!($field)),
284
+ actual == $expected,
285
+ &format!("expected 0x{:X}, got 0x{:X}", $expected, actual),
286
+ );
287
+ };
288
+ }
289
+
290
+ // CTask offsets
291
+ let _ = log_line("");
292
+ let _ = log_line(" CTask:");
293
+ check_offset!(result, CTask, vtable, 0x00);
294
+ check_offset!(result, CTask, parent, 0x04);
295
+ check_offset!(result, CTask, _unknown_08, 0x08);
296
+ check_offset!(result, CTask, class_type, 0x20);
297
+
298
+ // CGameTask offsets
299
+ let _ = log_line("");
300
+ let _ = log_line(" CGameTask:");
301
+ check_offset!(result, CGameTask, base, 0x00);
302
+ check_offset!(result, CGameTask, pos_x, 0x84);
303
+ check_offset!(result, CGameTask, pos_y, 0x88);
304
+ check_offset!(result, CGameTask, speed_x, 0x90);
305
+ check_offset!(result, CGameTask, speed_y, 0x94);
306
+ check_offset!(result, CGameTask, vtable2, 0xE8);
307
+
308
+ // DDGame offsets
309
+ let _ = log_line("");
310
+ let _ = log_line(" DDGame:");
311
+ check_offset!(result, DDGame, landscape, 0x20);
312
+ check_offset!(result, DDGame, game_state, 0x24);
313
+ check_offset!(result, DDGame, arrow_sprites, 0x38);
314
+ check_offset!(result, DDGame, arrow_gfxdirs, 0xB8);
315
+ check_offset!(result, DDGame, display_gfx, 0x138);
316
+ check_offset!(result, DDGame, task_state_machine, 0x380);
317
+ check_offset!(result, DDGame, sprite_regions, 0x46C);
318
+ check_offset!(result, DDGame, coord_list, 0x50C);
319
+
320
+ // DDGameWrapper offsets
321
+ let _ = log_line("");
322
+ let _ = log_line(" DDGameWrapper:");
323
+ check_offset!(result, DDGameWrapper, vtable, 0x00);
324
+ check_offset!(result, DDGameWrapper, ddgame, 0x488);
325
+ check_offset!(result, DDGameWrapper, gfx_handler_0, 0x4C0);
326
+ check_offset!(result, DDGameWrapper, landscape, 0x4CC);
327
+ check_offset!(result, DDGameWrapper, display, 0x4D0);
328
+ }
329
+
330
+ // ---------------------------------------------------------------------------
331
+ // Task 4: Constructor hooks
332
+ // ---------------------------------------------------------------------------
333
+
334
+ static CTASK_HOOKED: AtomicBool = AtomicBool::new(false);
335
+ static DDGAME_WRAPPER_HOOKED: AtomicBool = AtomicBool::new(false);
336
+
337
+ static_detour! {
338
+ static CTaskCtorHook: unsafe extern "thiscall" fn(u32, u32, u32) -> u32;
339
+ static DDGameWrapperCtorHook: unsafe extern "thiscall" fn(u32) -> u32;
340
+ }
341
+
342
+ fn ctask_ctor_detour(this: u32, parent: u32, class_type: u32) -> u32 {
343
+ // Call original constructor
344
+ let ret = unsafe { CTaskCtorHook.call(this, parent, class_type) };
345
+
346
+ // Only validate the first CTask construction
347
+ if CTASK_HOOKED.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst).is_ok() {
348
+ let _ = log_line("");
349
+ let _ = log_line("--- CTask Constructor Hook Triggered ---");
350
+ let _ = log_line(&format!(" this=0x{:08X}, parent=0x{:08X}, class_type={}", this, parent, class_type));
351
+
352
+ let mut result = ValidationResult::new();
353
+
354
+ unsafe {
355
+ // vtable at +0x00 should be in .rdata and point to .text
356
+ let vtable_ptr = read_u32(this);
357
+ let vt_in_rdata = is_in_rdata(vtable_ptr);
358
+ result.check(
359
+ "CTask.vtable location",
360
+ vt_in_rdata,
361
+ &format!("0x{:08X} {}", vtable_ptr, if vt_in_rdata { "in .rdata" } else { "NOT in .rdata" }),
362
+ );
363
+
364
+ if vt_in_rdata {
365
+ let first_method = read_u32(vtable_ptr);
366
+ let in_text = is_in_text(first_method);
367
+ result.check(
368
+ "CTask.vtable[0] -> .text",
369
+ in_text,
370
+ &format!("0x{:08X} {}", first_method, if in_text { "in .text" } else { "NOT in .text" }),
371
+ );
372
+ }
373
+
374
+ // field at +0x08 should be 0x10
375
+ let field_08 = read_u32(this + 0x08);
376
+ result.check(
377
+ "CTask._unknown_08 == 0x10",
378
+ field_08 == 0x10,
379
+ &format!("expected 0x10, got 0x{:X}", field_08),
380
+ );
381
+
382
+ // class_type at +0x20 should be in reasonable range (<200)
383
+ let ct = read_u32(this + 0x20);
384
+ result.check(
385
+ "CTask.class_type < 200",
386
+ ct < 200,
387
+ &format!("class_type = {} (arg was {})", ct, class_type),
388
+ );
389
+ }
390
+
391
+ let _ = log_line(&format!(" CTask hook {}", result.summary_line()));
392
+ }
393
+
394
+ ret
395
+ }
396
+
397
+ fn ddgame_wrapper_ctor_detour(this: u32) -> u32 {
398
+ // Call original constructor
399
+ let ret = unsafe { DDGameWrapperCtorHook.call(this) };
400
+
401
+ // Only validate the first DDGameWrapper construction
402
+ if DDGAME_WRAPPER_HOOKED.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst).is_ok() {
403
+ let _ = log_line("");
404
+ let _ = log_line("--- DDGameWrapper Constructor Hook Triggered ---");
405
+ let _ = log_line(&format!(" this=0x{:08X}", this));
406
+
407
+ let mut result = ValidationResult::new();
408
+
409
+ unsafe {
410
+ // vtable at +0x00 should be DDGAME_WRAPPER_VTABLE
411
+ let vtable_ptr = read_u32(this);
412
+ result.check(
413
+ "DDGameWrapper.vtable == 0x66A30C",
414
+ vtable_ptr == va::DDGAME_WRAPPER_VTABLE,
415
+ &format!("expected 0x{:08X}, got 0x{:08X}", va::DDGAME_WRAPPER_VTABLE, vtable_ptr),
416
+ );
417
+
418
+ // field at +0x4E0 should be 0xFFFFFF9C (-100)
419
+ let field_4e0 = read_u32(this + 0x4E0);
420
+ result.check(
421
+ "DDGameWrapper._field_4e0 == -100",
422
+ field_4e0 == 0xFFFFFF9C,
423
+ &format!("expected 0xFFFFFF9C, got 0x{:08X}", field_4e0),
424
+ );
425
+ }
426
+
427
+ let _ = log_line(&format!(" DDGameWrapper immediate {}", result.summary_line()));
428
+
429
+ // Spawn deferred validation thread
430
+ let wrapper_addr = this;
431
+ std::thread::spawn(move || {
432
+ // Wait 5 seconds for game initialization to complete
433
+ std::thread::sleep(std::time::Duration::from_secs(5));
434
+
435
+ let _ = log_line("");
436
+ let _ = log_line("--- DDGameWrapper Deferred Validation (5s after ctor) ---");
437
+ let _ = log_line(&format!(" wrapper=0x{:08X}", wrapper_addr));
438
+
439
+ let mut result = ValidationResult::new();
440
+
441
+ unsafe {
442
+ // ddgame pointer at +0x488 should be non-null
443
+ let ddgame_ptr = read_u32(wrapper_addr + 0x488);
444
+ let ddgame_valid = ddgame_ptr != 0;
445
+ result.check(
446
+ "DDGameWrapper.ddgame != NULL",
447
+ ddgame_valid,
448
+ &format!("0x{:08X}", ddgame_ptr),
449
+ );
450
+
451
+ if ddgame_valid {
452
+ // DDGame.landscape at +0x20 should match wrapper.landscape at +0x4CC
453
+ let ddgame_landscape = read_u32(ddgame_ptr + 0x20);
454
+ let wrapper_landscape = read_u32(wrapper_addr + 0x4CC);
455
+ result.check(
456
+ "DDGame.landscape == wrapper.landscape",
457
+ ddgame_landscape == wrapper_landscape,
458
+ &format!(
459
+ "DDGame+0x20=0x{:08X}, wrapper+0x4CC=0x{:08X}",
460
+ ddgame_landscape, wrapper_landscape
461
+ ),
462
+ );
463
+
464
+ // DDGame+0x7EFC should be 1
465
+ let field_7efc = read_u32(ddgame_ptr + 0x7EFC);
466
+ result.check(
467
+ "DDGame+0x7EFC == 1",
468
+ field_7efc == 1,
469
+ &format!("expected 1, got {}", field_7efc),
470
+ );
471
+
472
+ // Subsystem vtable checks
473
+ // display_gfx at DDGame+0x138
474
+ let display_gfx = read_u32(ddgame_ptr + 0x138);
475
+ if display_gfx != 0 {
476
+ let dgfx_vt = read_u32(display_gfx);
477
+ result.check(
478
+ "DDGame.display_gfx vtable",
479
+ dgfx_vt == va::DISPLAY_GFX_VTABLE,
480
+ &format!("expected 0x{:08X}, got 0x{:08X}", va::DISPLAY_GFX_VTABLE, dgfx_vt),
481
+ );
482
+ } else {
483
+ result.check("DDGame.display_gfx", false, "NULL pointer");
484
+ }
485
+
486
+ // task_state_machine at DDGame+0x380
487
+ let tsm = read_u32(ddgame_ptr + 0x380);
488
+ if tsm != 0 {
489
+ let tsm_vt = read_u32(tsm);
490
+ result.check(
491
+ "DDGame.task_state_machine vtable",
492
+ tsm_vt == va::TASK_STATE_MACHINE_VTABLE,
493
+ &format!("expected 0x{:08X}, got 0x{:08X}", va::TASK_STATE_MACHINE_VTABLE, tsm_vt),
494
+ );
495
+ } else {
496
+ result.check("DDGame.task_state_machine", false, "NULL pointer");
497
+ }
498
+ }
499
+
500
+ // gfx_handler_0 at wrapper+0x4C0
501
+ let gfx0 = read_u32(wrapper_addr + 0x4C0);
502
+ if gfx0 != 0 {
503
+ let gfx0_vt = read_u32(gfx0);
504
+ result.check(
505
+ "wrapper.gfx_handler_0 vtable",
506
+ gfx0_vt == va::GFX_HANDLER_VTABLE,
507
+ &format!("expected 0x{:08X}, got 0x{:08X}", va::GFX_HANDLER_VTABLE, gfx0_vt),
508
+ );
509
+ } else {
510
+ result.check("wrapper.gfx_handler_0", false, "NULL pointer");
511
+ }
512
+
513
+ // landscape at wrapper+0x4CC
514
+ let landscape = read_u32(wrapper_addr + 0x4CC);
515
+ if landscape != 0 {
516
+ let land_vt = read_u32(landscape);
517
+ result.check(
518
+ "wrapper.landscape vtable",
519
+ land_vt == va::PC_LANDSCAPE_VTABLE,
520
+ &format!("expected 0x{:08X}, got 0x{:08X}", va::PC_LANDSCAPE_VTABLE, land_vt),
521
+ );
522
+ } else {
523
+ result.check("wrapper.landscape", false, "NULL pointer");
524
+ }
525
+ }
526
+
527
+ let _ = log_line(&format!(" DDGameWrapper deferred {}", result.summary_line()));
528
+ });
529
+ }
530
+
531
+ ret
532
+ }
533
+
534
+ fn install_hooks() -> Result<(), Box<dyn std::error::Error>> {
535
+ let _ = log_line("");
536
+ let _ = log_line("--- Installing Constructor Hooks ---");
537
+
538
+ unsafe {
539
+ // CTask constructor hook
540
+ let ctask_ctor: unsafe extern "thiscall" fn(u32, u32, u32) -> u32 =
541
+ std::mem::transmute(va::CTASK_CONSTRUCTOR as usize);
542
+ CTaskCtorHook
543
+ .initialize(ctask_ctor, |a, b, c| ctask_ctor_detour(a, b, c))?
544
+ .enable()?;
545
+ let _ = log_line(&format!(
546
+ " CTask constructor hook installed at 0x{:08X}",
547
+ va::CTASK_CONSTRUCTOR
548
+ ));
549
+
550
+ // DDGameWrapper constructor hook
551
+ let ddgw_ctor: unsafe extern "thiscall" fn(u32) -> u32 =
552
+ std::mem::transmute(va::CONSTRUCT_DD_GAME_WRAPPER as usize);
553
+ DDGameWrapperCtorHook
554
+ .initialize(ddgw_ctor, |a| ddgame_wrapper_ctor_detour(a))?
555
+ .enable()?;
556
+ let _ = log_line(&format!(
557
+ " DDGameWrapper constructor hook installed at 0x{:08X}",
558
+ va::CONSTRUCT_DD_GAME_WRAPPER
559
+ ));
560
+ }
561
+
562
+ let _ = log_line(" All hooks installed successfully.");
563
+ Ok(())
564
+ }
565
+
566
+ // ---------------------------------------------------------------------------
567
+ // Main validation entry point
568
+ // ---------------------------------------------------------------------------
569
+
570
+ fn run_validation() -> Result<(), Box<dyn std::error::Error>> {
571
+ // 1. Clear log file
572
+ clear_log()?;
573
+
574
+ // 2. Log header
575
+ log_line("============================================")?;
576
+ log_line(" OpenWA Runtime Validator")?;
577
+ log_line(" Target: WA.exe 3.8.1 (Steam)")?;
578
+ log_line("============================================")?;
579
+
580
+ let mut result = ValidationResult::new();
581
+
582
+ // 3. Address validation (vtables, functions)
583
+ validate_addresses(&mut result);
584
+
585
+ // 4. Struct offset validation (offset_of! checks)
586
+ validate_struct_offsets(&mut result);
587
+
588
+ // 5. Print intermediate summary
589
+ let _ = log_line("");
590
+ let _ = log_line("--- Static Validation Summary ---");
591
+ let _ = log_line(&format!(" {}", result.summary_line()));
592
+
593
+ // 6. Install hooks (CTask, DDGameWrapper)
594
+ match install_hooks() {
595
+ Ok(()) => {}
596
+ Err(e) => {
597
+ let _ = log_line(&format!("[ERROR] Failed to install hooks: {}", e));
598
+ }
599
+ }
600
+
601
+ // 7. Log that hooks are installed
602
+ let _ = log_line("");
603
+ let _ = log_line("Validator initialized. Constructor hooks active.");
604
+ let _ = log_line("Waiting for game to construct CTask / DDGameWrapper...");
605
+
606
+ Ok(())
607
+ }