dotscope 0.7.0

A high-performance, cross-platform framework for analyzing and reverse engineering .NET PE executables
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
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
//! `System.Runtime.CompilerServices.RuntimeHelpers` and `System.Object` method hooks.
//!
//! This module provides hook implementations for `RuntimeHelpers` and `Object` methods
//! commonly used by obfuscators for array initialization, hash code computation,
//! equality checks, and runtime support operations.
//!
//! # Overview
//!
//! `RuntimeHelpers` provides low-level runtime support methods. The most important
//! for deobfuscation is `InitializeArray`, which is used by the C# compiler to
//! efficiently initialize arrays from embedded data.
//!
//! # Emulated .NET Methods
//!
//! | Method | Description |
//! |--------|-------------|
//! | `InitializeArray(Array, RuntimeFieldHandle)` | Copies FieldRVA data to array |
//! | `GetHashCode(object)` | Returns identity hash code |
//! | `Equals(object, object)` | Reference equality check |
//! | `GetObjectValue(object)` | Returns object (boxes value types) |
//! | `RunClassConstructor(RuntimeTypeHandle)` | Runs static constructor (no-op) |
//! | `RunModuleConstructor(ModuleHandle)` | Runs module constructor (no-op) |
//! | `Object.Equals(object)` | Instance reference/value equality |
//! | `Object.Equals(object, object)` | Static reference/value equality |
//! | `Object.MemberwiseClone()` | Shallow clone heap object |
//! | `ValueType.Equals(object)` | Field-by-field value type equality |
//! | `GC.Collect()` | No-op (GC not emulated) |
//! | `GC.SuppressFinalize(object)` | No-op (GC not emulated) |
//! | `GC.KeepAlive(object)` | No-op (GC not emulated) |
//! | `GC.WaitForPendingFinalizers()` | No-op (GC not emulated) |
//!
//! # Deobfuscation Use Cases
//!
//! ## Static Array Initialization
//!
//! The C# compiler uses `InitializeArray` for static array literals:
//!
//! ```csharp
//! // This C# code:
//! static byte[] data = { 0x01, 0x02, 0x03 };
//!
//! // Compiles to IL like:
//! // ldsfld byte[] ::data
//! // ldtoken RuntimeFieldHandle (points to embedded data)
//! // call RuntimeHelpers.InitializeArray(Array, RuntimeFieldHandle)
//! ```
//!
//! The hook reads the actual embedded data from the PE file's FieldRVA table.
//!
//! ## ConfuserEx String Encryption
//!
//! ConfuserEx stores encrypted string data in static arrays initialized via
//! `InitializeArray`. This hook extracts the encrypted data for analysis.
//!
//! # Implementation Notes
//!
//! - `InitializeArray` reads from the actual PE file via `thread.assembly()`
//! - Hash codes are based on heap reference IDs for consistency
//! - Constructor runners are no-ops (we don't track static initialization)

use crate::{
    emulation::{
        memory::HeapObject,
        runtime::hook::{Hook, HookContext, HookManager, PreHookResult},
        thread::EmulationThread,
        EmValue,
    },
    metadata::{
        tables::FieldRvaRaw,
        typesystem::{CilFlavor, PointerSize},
    },
    Result,
};

/// Registers all `RuntimeHelpers` method hooks with the given hook manager.
///
/// # Arguments
///
/// * `manager` - The [`HookManager`] to register hooks with
///
/// # Registered Hooks
///
/// - `RuntimeHelpers.InitializeArray` - Copy FieldRVA data to array
/// - `RuntimeHelpers.GetHashCode` - Identity hash code
/// - `RuntimeHelpers.Equals` - Reference equality
/// - `RuntimeHelpers.GetObjectValue` - Return object (box value types)
/// - `RuntimeHelpers.RunClassConstructor` - No-op
/// - `RuntimeHelpers.RunModuleConstructor` - No-op
/// - `Object.Equals` - Reference/value equality (instance and static)
/// - `Object.MemberwiseClone` - Shallow clone heap object
/// - `ValueType.Equals` - Field-by-field value type equality
/// - `GC.Collect` - No-op
/// - `GC.SuppressFinalize` - No-op
/// - `GC.KeepAlive` - No-op
/// - `GC.WaitForPendingFinalizers` - No-op
pub fn register(manager: &HookManager) -> Result<()> {
    manager.register(
        Hook::new("System.Runtime.CompilerServices.RuntimeHelpers.InitializeArray")
            .match_name(
                "System.Runtime.CompilerServices",
                "RuntimeHelpers",
                "InitializeArray",
            )
            .pre(runtime_helpers_initialize_array_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode")
            .match_name(
                "System.Runtime.CompilerServices",
                "RuntimeHelpers",
                "GetHashCode",
            )
            .pre(runtime_helpers_get_hash_code_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.RuntimeHelpers.Equals")
            .match_name(
                "System.Runtime.CompilerServices",
                "RuntimeHelpers",
                "Equals",
            )
            .pre(runtime_helpers_equals_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue")
            .match_name(
                "System.Runtime.CompilerServices",
                "RuntimeHelpers",
                "GetObjectValue",
            )
            .pre(runtime_helpers_get_object_value_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor")
            .match_name(
                "System.Runtime.CompilerServices",
                "RuntimeHelpers",
                "RunClassConstructor",
            )
            .pre(runtime_helpers_run_class_constructor_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.RuntimeHelpers.RunModuleConstructor")
            .match_name(
                "System.Runtime.CompilerServices",
                "RuntimeHelpers",
                "RunModuleConstructor",
            )
            .pre(runtime_helpers_run_module_constructor_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.RuntimeHelpers.PrepareDelegate")
            .match_name(
                "System.Runtime.CompilerServices",
                "RuntimeHelpers",
                "PrepareDelegate",
            )
            .pre(runtime_helpers_prepare_noop_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod")
            .match_name(
                "System.Runtime.CompilerServices",
                "RuntimeHelpers",
                "PrepareMethod",
            )
            .pre(runtime_helpers_prepare_noop_pre),
    )?;

    // System.Object.Equals - handles both instance and static overloads.
    // Critical for ConfuserEx anti-tamper: Assembly.GetExecutingAssembly().Equals(...)
    manager.register(
        Hook::new("System.Object.Equals")
            .match_name("System", "Object", "Equals")
            .pre(object_equals_pre),
    )?;

    // System.Object..ctor() - base constructor, no-op.
    // Every .NET type's constructor calls base Object::.ctor() which has no side effects.
    manager.register(
        Hook::new("System.Object..ctor")
            .match_name("System", "Object", ".ctor")
            .pre(object_ctor_pre),
    )?;

    // System.Object.ToString() — type-aware string conversion
    manager.register(
        Hook::new("System.Object.ToString")
            .match_name("System", "Object", "ToString")
            .pre(object_to_string_pre),
    )?;

    // System.Object.GetHashCode() — identity-based hash code
    manager.register(
        Hook::new("System.Object.GetHashCode")
            .match_name("System", "Object", "GetHashCode")
            .pre(object_get_hash_code_pre),
    )?;

    // System.Object.MemberwiseClone() — shallow clone
    manager.register(
        Hook::new("System.Object.MemberwiseClone")
            .match_name("System", "Object", "MemberwiseClone")
            .pre(object_memberwise_clone_pre),
    )?;

    // System.ValueType.Equals(object) — value type field-by-field equality
    manager.register(
        Hook::new("System.ValueType.Equals")
            .match_name("System", "ValueType", "Equals")
            .pre(valuetype_equals_pre),
    )?;

    // GC stubs — no-op in the emulator
    manager.register(
        Hook::new("System.GC.Collect")
            .match_name("System", "GC", "Collect")
            .pre(gc_noop_pre),
    )?;
    manager.register(
        Hook::new("System.GC.SuppressFinalize")
            .match_name("System", "GC", "SuppressFinalize")
            .pre(gc_noop_pre),
    )?;
    manager.register(
        Hook::new("System.GC.KeepAlive")
            .match_name("System", "GC", "KeepAlive")
            .pre(gc_noop_pre),
    )?;
    manager.register(
        Hook::new("System.GC.WaitForPendingFinalizers")
            .match_name("System", "GC", "WaitForPendingFinalizers")
            .pre(gc_noop_pre),
    )?;
    manager.register(
        Hook::new("System.GC.get_MaxGeneration")
            .match_name("System", "GC", "get_MaxGeneration")
            .pre(gc_get_max_generation_pre),
    )?;

    // System.Runtime.CompilerServices.Unsafe stubs
    manager.register(
        Hook::new("System.Runtime.CompilerServices.Unsafe.As")
            .match_name("System.Runtime.CompilerServices", "Unsafe", "As")
            .pre(unsafe_as_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.Unsafe.SizeOf")
            .match_name("System.Runtime.CompilerServices", "Unsafe", "SizeOf")
            .pre(unsafe_sizeof_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.Unsafe.Add")
            .match_name("System.Runtime.CompilerServices", "Unsafe", "Add")
            .pre(unsafe_passthrough_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.Unsafe.AddByteOffset")
            .match_name("System.Runtime.CompilerServices", "Unsafe", "AddByteOffset")
            .pre(unsafe_passthrough_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.Unsafe.ReadUnaligned")
            .match_name("System.Runtime.CompilerServices", "Unsafe", "ReadUnaligned")
            .pre(unsafe_read_unaligned_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.Unsafe.WriteUnaligned")
            .match_name(
                "System.Runtime.CompilerServices",
                "Unsafe",
                "WriteUnaligned",
            )
            .pre(unsafe_write_noop_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.Unsafe.AsRef")
            .match_name("System.Runtime.CompilerServices", "Unsafe", "AsRef")
            .pre(unsafe_as_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.Unsafe.AsPointer")
            .match_name("System.Runtime.CompilerServices", "Unsafe", "AsPointer")
            .pre(unsafe_passthrough_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.Unsafe.ByteOffset")
            .match_name("System.Runtime.CompilerServices", "Unsafe", "ByteOffset")
            .pre(unsafe_byte_offset_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.Unsafe.AreSame")
            .match_name("System.Runtime.CompilerServices", "Unsafe", "AreSame")
            .pre(unsafe_are_same_pre),
    )?;

    manager.register(
        Hook::new("System.Runtime.CompilerServices.Unsafe.IsNullRef")
            .match_name("System.Runtime.CompilerServices", "Unsafe", "IsNullRef")
            .pre(unsafe_is_null_ref_pre),
    )?;

    Ok(())
}

/// Hook for `System.Runtime.CompilerServices.RuntimeHelpers.InitializeArray` method.
///
/// Provides a fast way to initialize an array from data stored in a module.
///
/// # Handled Overloads
///
/// - `RuntimeHelpers.InitializeArray(Array, RuntimeFieldHandle) -> void`
///
/// # Parameters
///
/// - `array`: The array to be initialized
/// - `fldHandle`: A field handle that specifies the location of the data for the array
fn runtime_helpers_initialize_array_pre(
    ctx: &HookContext<'_>,
    thread: &mut EmulationThread,
) -> PreHookResult {
    // RuntimeHelpers.InitializeArray(Array array, RuntimeFieldHandle fldHandle)
    // args[0] = array (ObjectRef)
    // args[1] = field handle (NativeInt/I32 containing token value)

    if ctx.args.len() < 2 {
        return PreHookResult::Bypass(None);
    }

    // Get the array reference
    let array_ref = match &ctx.args[0] {
        EmValue::ObjectRef(r) => *r,
        _ => return PreHookResult::Bypass(None),
    };

    // Get the field token from the RuntimeFieldHandle
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    let field_token = match &ctx.args[1] {
        EmValue::I32(v) => (*v).cast_unsigned(),
        EmValue::NativeInt(v) | EmValue::I64(v) => *v as u32,
        _ => return PreHookResult::Bypass(None),
    };

    // Need assembly access to read FieldRVA data
    let Some(assembly) = thread.assembly() else {
        return PreHookResult::Bypass(None);
    };

    // Look up the RVA for this field token in the FieldRVA table
    let Some(tables) = assembly.tables() else {
        return PreHookResult::Bypass(None);
    };

    let Some(fieldrva_table) = tables.table::<FieldRvaRaw>() else {
        return PreHookResult::Bypass(None);
    };

    // Find the RVA for this field token
    let mut rva: Option<u32> = None;
    for row in fieldrva_table {
        // Convert field index to full token (table 0x04 = Field)
        let row_token = row.field | 0x0400_0000;
        if row_token == field_token && row.rva > 0 {
            rva = Some(row.rva);
            break;
        }
    }

    let Some(rva) = rva else {
        // Field not found in RVA table - may not have embedded data
        return PreHookResult::Bypass(None);
    };

    // Convert RVA to file offset and read data
    let file = assembly.file();
    let Ok(file_offset) = file.rva_to_offset(rva as usize) else {
        return PreHookResult::Bypass(None);
    };

    let pe_data = file.data();
    if file_offset >= pe_data.len() {
        return PreHookResult::Bypass(None);
    }

    // Get the array length and element type
    let array_len = thread.heap().get_array_length(array_ref).unwrap_or(0);
    if array_len == 0 {
        return PreHookResult::Bypass(None);
    }

    let element_type = thread
        .heap()
        .get_array_element_type(array_ref)
        .unwrap_or(CilFlavor::U1);

    // Calculate element size in bytes
    let Some(element_size) = element_type.element_size(ctx.pointer_size) else {
        return PreHookResult::Bypass(None);
    };

    // Calculate total bytes to read
    let total_bytes = array_len * element_size;
    let bytes_to_read = total_bytes.min(pe_data.len() - file_offset);

    // Read the bytes from the PE file
    let data = &pe_data[file_offset..file_offset + bytes_to_read];

    // Set each element in the array based on element type
    for i in 0..array_len {
        let byte_offset = i * element_size;
        if byte_offset + element_size > data.len() {
            break;
        }

        let value = match element_type {
            CilFlavor::Boolean | CilFlavor::I1 | CilFlavor::U1 => {
                EmValue::I32(i32::from(data[byte_offset]))
            }
            CilFlavor::Char | CilFlavor::I2 | CilFlavor::U2 => {
                let bytes = [data[byte_offset], data[byte_offset + 1]];
                EmValue::I32(i32::from(i16::from_le_bytes(bytes)))
            }
            CilFlavor::I4 | CilFlavor::U4 => {
                let bytes = [
                    data[byte_offset],
                    data[byte_offset + 1],
                    data[byte_offset + 2],
                    data[byte_offset + 3],
                ];
                EmValue::I32(i32::from_le_bytes(bytes))
            }
            CilFlavor::R4 => {
                let bytes = [
                    data[byte_offset],
                    data[byte_offset + 1],
                    data[byte_offset + 2],
                    data[byte_offset + 3],
                ];
                EmValue::F32(f32::from_le_bytes(bytes))
            }
            CilFlavor::I8 | CilFlavor::U8 => {
                let bytes = [
                    data[byte_offset],
                    data[byte_offset + 1],
                    data[byte_offset + 2],
                    data[byte_offset + 3],
                    data[byte_offset + 4],
                    data[byte_offset + 5],
                    data[byte_offset + 6],
                    data[byte_offset + 7],
                ];
                EmValue::I64(i64::from_le_bytes(bytes))
            }
            CilFlavor::R8 => {
                let bytes = [
                    data[byte_offset],
                    data[byte_offset + 1],
                    data[byte_offset + 2],
                    data[byte_offset + 3],
                    data[byte_offset + 4],
                    data[byte_offset + 5],
                    data[byte_offset + 6],
                    data[byte_offset + 7],
                ];
                EmValue::F64(f64::from_le_bytes(bytes))
            }
            // For other types, treat as bytes
            _ => EmValue::I32(i32::from(data[byte_offset])),
        };

        try_hook!(thread.heap_mut().set_array_element(array_ref, i, value));
    }

    PreHookResult::Bypass(None)
}

/// Hook for `System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode` method.
///
/// Returns a hash code for an object based on its identity.
///
/// # Handled Overloads
///
/// - `RuntimeHelpers.GetHashCode(Object) -> Int32`
///
/// # Parameters
///
/// - `o`: An object
fn runtime_helpers_get_hash_code_pre(
    ctx: &HookContext<'_>,
    _thread: &mut EmulationThread,
) -> PreHookResult {
    // Return a hash based on the object reference ID
    let result = if let Some(EmValue::ObjectRef(r)) = ctx.args.first() {
        #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
        let hash = r.id() as i32;
        EmValue::I32(hash)
    } else {
        EmValue::I32(0)
    };
    PreHookResult::Bypass(Some(result))
}

/// Hook for `System.Runtime.CompilerServices.RuntimeHelpers.Equals` method.
///
/// Determines whether the specified object instances are the same instance.
///
/// # Handled Overloads
///
/// - `RuntimeHelpers.Equals(Object, Object) -> Boolean`
///
/// # Parameters
///
/// - `o1`: The first object to compare
/// - `o2`: The second object to compare
fn runtime_helpers_equals_pre(
    ctx: &HookContext<'_>,
    _thread: &mut EmulationThread,
) -> PreHookResult {
    if ctx.args.len() < 2 {
        return PreHookResult::Bypass(Some(EmValue::I32(0))); // false
    }

    // Reference equality
    let equal = match (&ctx.args[0], &ctx.args[1]) {
        (EmValue::ObjectRef(a), EmValue::ObjectRef(b)) => a.id() == b.id(),
        (EmValue::Null, EmValue::Null) => true,
        _ => false,
    };

    PreHookResult::Bypass(Some(EmValue::I32(i32::from(equal))))
}

/// Hook for `System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue` method.
///
/// Boxes a value type. For reference types, returns the object unchanged.
///
/// # Handled Overloads
///
/// - `RuntimeHelpers.GetObjectValue(Object) -> Object`
///
/// # Parameters
///
/// - `obj`: The value type to be boxed (or reference type to return)
fn runtime_helpers_get_object_value_pre(
    ctx: &HookContext<'_>,
    _thread: &mut EmulationThread,
) -> PreHookResult {
    // For reference types, return as-is
    // For value types, this would box them (we just return as-is)
    let result = if let Some(arg) = ctx.args.first() {
        arg.clone()
    } else {
        EmValue::Null
    };
    PreHookResult::Bypass(Some(result))
}

/// Hook for `System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor` method.
///
/// Runs a class constructor (static constructor).
///
/// # Handled Overloads
///
/// - `RuntimeHelpers.RunClassConstructor(RuntimeTypeHandle) -> void`
///
/// # Parameters
///
/// - `type`: A type handle that specifies the class constructor to run
fn runtime_helpers_run_class_constructor_pre(
    _ctx: &HookContext<'_>,
    _thread: &mut EmulationThread,
) -> PreHookResult {
    // We don't actually run the static constructor
    PreHookResult::Bypass(None)
}

/// Hook for `System.Runtime.CompilerServices.RuntimeHelpers.RunModuleConstructor` method.
///
/// Runs a module constructor.
///
/// # Handled Overloads
///
/// - `RuntimeHelpers.RunModuleConstructor(ModuleHandle) -> void`
///
/// # Parameters
///
/// - `module`: A handle that specifies the module constructor to run
fn runtime_helpers_run_module_constructor_pre(
    _ctx: &HookContext<'_>,
    _thread: &mut EmulationThread,
) -> PreHookResult {
    PreHookResult::Bypass(None)
}

/// Hook for `RuntimeHelpers.PrepareDelegate` and `RuntimeHelpers.PrepareMethod`.
///
/// These are JIT compilation hints that request early compilation of delegates
/// and methods. In emulation we interpret IL directly, so pre-compilation is
/// unnecessary. Implemented as a void no-op.
fn runtime_helpers_prepare_noop_pre(
    _ctx: &HookContext<'_>,
    _thread: &mut EmulationThread,
) -> PreHookResult {
    PreHookResult::Bypass(None)
}

/// Hook for `System.Object.Equals` method.
///
/// Handles both the instance and static overloads:
///
/// - `object.Equals(object) -> bool` (instance, virtual)
/// - `Object.Equals(object, object) -> bool` (static)
///
/// Uses reference equality for object references and value equality for
/// primitives. This is critical for ConfuserEx anti-tamper checks which call
/// `Assembly.GetExecutingAssembly().Equals(Assembly.GetCallingAssembly())`.
/// Without this hook, the call returns a Symbolic value, and depending on
/// how ControlFlow protection restructures branches, the anti-tamper check
/// may incorrectly fail, causing decryptors to return null.
fn object_equals_pre(ctx: &HookContext<'_>, _thread: &mut EmulationThread) -> PreHookResult {
    // Instance overload: this.Equals(other)
    if let Some(this) = ctx.this {
        let other = if ctx.args.is_empty() {
            &EmValue::Null
        } else {
            &ctx.args[0]
        };
        let equal = this.clr_equals(other);
        return PreHookResult::Bypass(Some(EmValue::I32(i32::from(equal))));
    }

    // Static overload: Object.Equals(a, b)
    if ctx.args.len() >= 2 {
        let equal = ctx.args[0].clr_equals(&ctx.args[1]);
        return PreHookResult::Bypass(Some(EmValue::I32(i32::from(equal))));
    }

    PreHookResult::Bypass(Some(EmValue::I32(0)))
}

/// Hook for `System.Object::.ctor()`.
///
/// The base `Object` constructor is a no-op in the CLR. Every type's constructor
/// chain ultimately calls this, but it performs no initialization.
fn object_ctor_pre(_ctx: &HookContext<'_>, _thread: &mut EmulationThread) -> PreHookResult {
    PreHookResult::Bypass(None)
}

/// Hook for `Object.ToString()`.
///
/// Returns a string representation of the object. Type-aware:
/// - String objects → return self
/// - Boxed integers → numeric string
/// - ReflectionType → type's full name
/// - Other objects → type name or "System.Object"
#[allow(clippy::cast_sign_loss)]
fn object_to_string_pre(ctx: &HookContext<'_>, thread: &mut EmulationThread) -> PreHookResult {
    let text = match ctx.this {
        Some(EmValue::ObjectRef(href)) => {
            match thread.heap().get(*href) {
                Ok(HeapObject::String(_)) => {
                    // String.ToString() returns itself
                    return PreHookResult::Bypass(Some(EmValue::ObjectRef(*href)));
                }
                Ok(HeapObject::BoxedValue { value, .. }) => match &*value {
                    EmValue::I32(v) => v.to_string(),
                    EmValue::I64(v) => v.to_string(),
                    EmValue::F32(v) => v.to_string(),
                    EmValue::F64(v) => v.to_string(),
                    EmValue::Bool(b) => if *b { "True" } else { "False" }.to_string(),
                    EmValue::Char(c) => c.to_string(),
                    _ => "System.Object".to_string(),
                },
                Ok(HeapObject::ReflectionType { type_token, .. }) => {
                    // Return the type's full name if available
                    if let Some(asm) = thread.assembly().cloned() {
                        if let Some(cil_type) = asm.types().resolve(&type_token) {
                            if cil_type.namespace.is_empty() {
                                cil_type.name.clone()
                            } else {
                                format!("{}.{}", cil_type.namespace, cil_type.name)
                            }
                        } else {
                            format!("Type(0x{:08X})", type_token.value())
                        }
                    } else {
                        "System.Object".to_string()
                    }
                }
                Ok(HeapObject::Object { type_token, .. }) => {
                    // Try to get the type name from metadata
                    if let Some(asm) = thread.assembly().cloned() {
                        if let Some(cil_type) = asm.types().resolve(&type_token) {
                            if cil_type.namespace.is_empty() {
                                cil_type.name.clone()
                            } else {
                                format!("{}.{}", cil_type.namespace, cil_type.name)
                            }
                        } else {
                            "System.Object".to_string()
                        }
                    } else {
                        "System.Object".to_string()
                    }
                }
                _ => "System.Object".to_string(),
            }
        }
        Some(EmValue::I32(v)) => v.to_string(),
        Some(EmValue::I64(v)) => v.to_string(),
        Some(EmValue::Bool(b)) => if *b { "True" } else { "False" }.to_string(),
        Some(EmValue::Char(c)) => c.to_string(),
        _ => "System.Object".to_string(),
    };

    match thread.heap_mut().alloc_string(&text) {
        Ok(str_ref) => PreHookResult::Bypass(Some(EmValue::ObjectRef(str_ref))),
        Err(e) => PreHookResult::Error(format!("heap allocation failed: {e}")),
    }
}

/// Hook for `Object.GetHashCode()`.
///
/// Returns a deterministic hash code based on object identity (heap reference ID)
/// or the underlying value for value types.
fn object_get_hash_code_pre(ctx: &HookContext<'_>, _thread: &mut EmulationThread) -> PreHookResult {
    #[allow(clippy::cast_possible_truncation)]
    let hash = match ctx.this {
        Some(EmValue::ObjectRef(href)) => href.id() as i32,
        Some(EmValue::I32(v)) => *v,
        Some(EmValue::I64(v)) => *v as i32,
        Some(EmValue::Bool(b)) => i32::from(*b),
        Some(EmValue::Char(c)) => *c as i32,
        _ => 0,
    };
    PreHookResult::Bypass(Some(EmValue::I32(hash)))
}

/// Hook for `System.Object.MemberwiseClone()`.
///
/// Creates a shallow copy of the current object. Allocates a new heap object
/// with the same type token and copies all field values.
///
/// # Handled Overloads
///
/// - `Object.MemberwiseClone() -> Object` (protected, instance)
fn object_memberwise_clone_pre(
    ctx: &HookContext<'_>,
    thread: &mut EmulationThread,
) -> PreHookResult {
    let Some(EmValue::ObjectRef(src_ref)) = ctx.this else {
        return PreHookResult::Bypass(Some(EmValue::Null));
    };

    let src_obj = match thread.heap().get(*src_ref) {
        Ok(obj) => obj,
        Err(_) => return PreHookResult::Bypass(Some(EmValue::Null)),
    };

    match src_obj {
        HeapObject::Object {
            type_token, fields, ..
        } => {
            // Allocate a new object with the same type
            let new_ref = match thread.heap_mut().alloc_object(type_token) {
                Ok(r) => r,
                Err(_) => return PreHookResult::Bypass(Some(EmValue::Null)),
            };
            // Copy all fields
            for (field_token, value) in &fields {
                try_hook!(thread
                    .heap_mut()
                    .set_field(new_ref, *field_token, value.clone()));
            }
            PreHookResult::Bypass(Some(EmValue::ObjectRef(new_ref)))
        }
        HeapObject::BoxedValue {
            type_token, value, ..
        } => {
            // Clone the boxed value
            match thread.heap_mut().alloc_boxed(type_token, (*value).clone()) {
                Ok(new_ref) => PreHookResult::Bypass(Some(EmValue::ObjectRef(new_ref))),
                Err(_) => PreHookResult::Bypass(Some(EmValue::Null)),
            }
        }
        _ => {
            // For other heap object types, return the original reference
            // (strings are immutable, arrays would need element-by-element copy)
            PreHookResult::Bypass(Some(EmValue::ObjectRef(*src_ref)))
        }
    }
}

/// Hook for `System.ValueType.Equals(object)`.
///
/// Compares two value types field-by-field. If both `this` and the argument are
/// boxed values with the same type token, compares their inner values. Otherwise
/// falls back to reference equality.
///
/// # Handled Overloads
///
/// - `ValueType.Equals(object) -> bool` (virtual, instance)
fn valuetype_equals_pre(ctx: &HookContext<'_>, thread: &mut EmulationThread) -> PreHookResult {
    let Some(this_val) = ctx.this else {
        return PreHookResult::Bypass(Some(EmValue::I32(0)));
    };

    let other = if ctx.args.is_empty() {
        &EmValue::Null
    } else {
        &ctx.args[0]
    };

    // Fast path: direct EmValue comparison (unboxed value types on the stack)
    if this_val.clr_equals(other) {
        return PreHookResult::Bypass(Some(EmValue::I32(1)));
    }

    // Boxed value comparison: compare inner values if same type
    let (this_ref, other_ref) = match (this_val, other) {
        (EmValue::ObjectRef(a), EmValue::ObjectRef(b)) => (*a, *b),
        _ => return PreHookResult::Bypass(Some(EmValue::I32(0))),
    };

    let this_obj = match thread.heap().get(this_ref) {
        Ok(obj) => obj,
        Err(_) => return PreHookResult::Bypass(Some(EmValue::I32(0))),
    };

    let other_obj = match thread.heap().get(other_ref) {
        Ok(obj) => obj,
        Err(_) => return PreHookResult::Bypass(Some(EmValue::I32(0))),
    };

    let equal = match (&this_obj, &other_obj) {
        (
            HeapObject::BoxedValue {
                type_token: t1,
                value: v1,
                ..
            },
            HeapObject::BoxedValue {
                type_token: t2,
                value: v2,
                ..
            },
        ) => {
            // Same type token and same inner value
            t1 == t2 && v1.clr_equals(v2)
        }
        (
            HeapObject::Object {
                type_token: t1,
                fields: f1,
                ..
            },
            HeapObject::Object {
                type_token: t2,
                fields: f2,
                ..
            },
        ) => {
            // Same type and all fields equal
            t1 == t2
                && f1.len() == f2.len()
                && f1
                    .iter()
                    .all(|(k, v)| f2.get(k).is_some_and(|v2| v.clr_equals(v2)))
        }
        _ => false,
    };

    PreHookResult::Bypass(Some(EmValue::I32(i32::from(equal))))
}

/// No-op hook for GC methods (`GC.Collect()`, `GC.SuppressFinalize()`, `GC.KeepAlive()`,
/// `GC.WaitForPendingFinalizers()`).
///
/// Garbage collection is not emulated — these are safe to skip entirely.
fn gc_noop_pre(_ctx: &HookContext<'_>, _thread: &mut EmulationThread) -> PreHookResult {
    PreHookResult::Bypass(None)
}

/// Hook for `GC.get_MaxGeneration`.
///
/// .NET has 3 generations (0, 1, 2), so `MaxGeneration` returns 2.
fn gc_get_max_generation_pre(
    _ctx: &HookContext<'_>,
    _thread: &mut EmulationThread,
) -> PreHookResult {
    PreHookResult::Bypass(Some(EmValue::I32(2)))
}

/// Hook for `Unsafe.As<T>(object)` / `Unsafe.AsRef<T>(ref byte)`.
///
/// Reinterpret cast is a no-op in the emulator — just pass the value through.
fn unsafe_as_pre(ctx: &HookContext<'_>, _thread: &mut EmulationThread) -> PreHookResult {
    let result = ctx.args.first().cloned().unwrap_or(EmValue::Null);
    PreHookResult::Bypass(Some(result))
}

/// Hook for `Unsafe.SizeOf<T>()`.
///
/// Returns a reasonable default size. Without generic instantiation info we
/// fall back to pointer size (8 on 64-bit, 4 on 32-bit). The return type
/// hint from the generic parameter is not available at hook level, so we
/// use a conservative default.
fn unsafe_sizeof_pre(ctx: &HookContext<'_>, _thread: &mut EmulationThread) -> PreHookResult {
    let size = match ctx.pointer_size {
        PointerSize::Bit64 => 8,
        PointerSize::Bit32 => 4,
    };
    PreHookResult::Bypass(Some(EmValue::I32(size)))
}

/// Hook for `Unsafe.Add<T>(ref T, int)` / `Unsafe.AddByteOffset` / `Unsafe.AsPointer`.
///
/// Stub: returns the first argument unchanged.
fn unsafe_passthrough_pre(ctx: &HookContext<'_>, _thread: &mut EmulationThread) -> PreHookResult {
    let result = ctx.args.first().cloned().unwrap_or(EmValue::Null);
    PreHookResult::Bypass(Some(result))
}

/// Hook for `Unsafe.ReadUnaligned<T>(ref byte)`.
///
/// Stub: returns 0. Without actual memory layout we cannot read raw bytes.
fn unsafe_read_unaligned_pre(
    _ctx: &HookContext<'_>,
    _thread: &mut EmulationThread,
) -> PreHookResult {
    PreHookResult::Bypass(Some(EmValue::I32(0)))
}

/// Hook for `Unsafe.WriteUnaligned<T>(ref byte, T)`.
///
/// No-op stub — raw memory writes are not supported in the emulator.
fn unsafe_write_noop_pre(_ctx: &HookContext<'_>, _thread: &mut EmulationThread) -> PreHookResult {
    PreHookResult::Bypass(None)
}

/// Hook for `Unsafe.ByteOffset<T>(ref T, ref T)`.
///
/// Stub: returns 0 (cannot compute real pointer differences in emulation).
fn unsafe_byte_offset_pre(_ctx: &HookContext<'_>, _thread: &mut EmulationThread) -> PreHookResult {
    PreHookResult::Bypass(Some(EmValue::NativeInt(0)))
}

/// Hook for `Unsafe.AreSame<T>(ref T, ref T)`.
///
/// Compares the two arguments for reference equality.
fn unsafe_are_same_pre(ctx: &HookContext<'_>, _thread: &mut EmulationThread) -> PreHookResult {
    if ctx.args.len() >= 2 {
        let same = ctx.args[0].clr_equals(&ctx.args[1]);
        PreHookResult::Bypass(Some(EmValue::I32(i32::from(same))))
    } else {
        PreHookResult::Bypass(Some(EmValue::I32(0)))
    }
}

/// Hook for `Unsafe.IsNullRef<T>(ref T)`.
///
/// Returns true if the argument is Null.
fn unsafe_is_null_ref_pre(ctx: &HookContext<'_>, _thread: &mut EmulationThread) -> PreHookResult {
    let is_null = matches!(ctx.args.first(), Some(EmValue::Null) | None);
    PreHookResult::Bypass(Some(EmValue::I32(i32::from(is_null))))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        emulation::{runtime::hook::HookManager, HeapRef},
        metadata::{token::Token, typesystem::PointerSize},
    };

    #[test]
    fn test_register_hooks() {
        let manager = HookManager::new();
        register(&manager).unwrap();
        assert_eq!(manager.len(), 30);
    }

    #[test]
    fn test_get_hash_code_hook() {
        let obj_ref = HeapRef::new(42);
        let args = [EmValue::ObjectRef(obj_ref)];
        let ctx = HookContext::new(
            Token::new(0x0A000001),
            "System.Runtime.CompilerServices",
            "RuntimeHelpers",
            "GetHashCode",
            PointerSize::Bit64,
        )
        .with_args(&args);

        let mut thread = crate::test::emulation::create_test_thread();
        let result = runtime_helpers_get_hash_code_pre(&ctx, &mut thread);

        match result {
            PreHookResult::Bypass(Some(EmValue::I32(v))) => assert_eq!(v, 42),
            _ => panic!("Expected Bypass with I32(42)"),
        }
    }

    #[test]
    fn test_equals_hook_same_reference() {
        let obj1 = HeapRef::new(1);
        let obj2 = HeapRef::new(1);
        let args = [EmValue::ObjectRef(obj1), EmValue::ObjectRef(obj2)];
        let ctx = HookContext::new(
            Token::new(0x0A000001),
            "System.Runtime.CompilerServices",
            "RuntimeHelpers",
            "Equals",
            PointerSize::Bit64,
        )
        .with_args(&args);

        let mut thread = crate::test::emulation::create_test_thread();
        let result = runtime_helpers_equals_pre(&ctx, &mut thread);

        match result {
            PreHookResult::Bypass(Some(EmValue::I32(v))) => assert_eq!(v, 1), // true
            _ => panic!("Expected Bypass with I32(1)"),
        }
    }

    #[test]
    fn test_equals_hook_different_references() {
        let obj1 = HeapRef::new(1);
        let obj2 = HeapRef::new(2);
        let args = [EmValue::ObjectRef(obj1), EmValue::ObjectRef(obj2)];
        let ctx = HookContext::new(
            Token::new(0x0A000001),
            "System.Runtime.CompilerServices",
            "RuntimeHelpers",
            "Equals",
            PointerSize::Bit64,
        )
        .with_args(&args);

        let mut thread = crate::test::emulation::create_test_thread();
        let result = runtime_helpers_equals_pre(&ctx, &mut thread);

        match result {
            PreHookResult::Bypass(Some(EmValue::I32(v))) => assert_eq!(v, 0), // false
            _ => panic!("Expected Bypass with I32(0)"),
        }
    }

    #[test]
    fn test_get_object_value_hook() {
        let args = [EmValue::I32(42)];
        let ctx = HookContext::new(
            Token::new(0x0A000001),
            "System.Runtime.CompilerServices",
            "RuntimeHelpers",
            "GetObjectValue",
            PointerSize::Bit64,
        )
        .with_args(&args);

        let mut thread = crate::test::emulation::create_test_thread();
        let result = runtime_helpers_get_object_value_pre(&ctx, &mut thread);

        match result {
            PreHookResult::Bypass(Some(EmValue::I32(v))) => assert_eq!(v, 42),
            _ => panic!("Expected Bypass with I32(42)"),
        }
    }
}