dotscope 0.6.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
//! Emulation configuration types.
//!
//! This module provides comprehensive configuration for the emulation engine,
//! allowing fine-grained control over execution limits, memory settings,
//! stub behavior, and tracing options.
//!
//! # Overview
//!
//! Configuration is organized into several interconnected structures:
//!
//! - [`EmulationConfig`] - Top-level configuration container
//! - [`EmulationLimits`] - Execution limits (instructions, call depth, memory)
//! - [`MemoryConfig`] - Memory subsystem settings
//! - [`StubConfig`] - Method stub behavior
//! - [`TracingConfig`] - Logging and tracing options
//! - [`CaptureConfig`] - Runtime data capture settings
//!
//! # Configuration Presets
//!
//! For common use cases, [`EmulationConfig`] provides preset configurations:
//!
//! - [`EmulationConfig::extraction()`] - Optimized for unpacking/decrypting assemblies
//! - [`EmulationConfig::analysis()`] - Optimized for static analysis
//! - [`EmulationConfig::full()`] - Complete emulation with all features
//! - [`EmulationConfig::minimal()`] - Lightweight constant folding
//!
//! # Example
//!
//! ```rust,no_run
//! use dotscope::emulation::{EmulationConfig, EmulationLimits};
//!
//! // Use a preset
//! let config = EmulationConfig::extraction();
//!
//! // Or customize
//! let config = EmulationConfig {
//!     limits: EmulationLimits::new()
//!         .with_max_instructions(10_000_000)
//!         .with_timeout_ms(30_000),
//!     symbolic_tracking: true,
//!     ..Default::default()
//! };
//! ```

use std::{ops::Range, path::PathBuf};

use crate::metadata::typesystem::PointerSize;

/// Comprehensive emulation configuration with fine-grained control.
///
/// `EmulationConfig` is the top-level configuration container that controls
/// all aspects of .NET emulation behavior. It aggregates sub-configurations
/// for limits, memory, stubs, and tracing.
///
/// # Default Configuration
///
/// The default configuration provides reasonable settings for general use:
/// - 10 million instruction limit
/// - 1000 call depth limit
/// - 256 MB heap limit
/// - Threading and exceptions enabled
/// - All stubs enabled in non-strict mode
///
/// # Presets
///
/// Use the preset methods for common scenarios:
/// - [`extraction()`](Self::extraction) - For unpacking protected assemblies
/// - [`analysis()`](Self::analysis) - For static analysis
/// - [`full()`](Self::full) - For complete emulation
/// - [`minimal()`](Self::minimal) - For simple constant folding
///
/// # Example
///
/// ```rust,no_run
/// use dotscope::emulation::EmulationConfig;
///
/// // Default configuration
/// let config = EmulationConfig::default();
///
/// // Preset for extraction
/// let config = EmulationConfig::extraction();
///
/// // Custom configuration
/// let config = EmulationConfig {
///     symbolic_tracking: true,
///     threading_enabled: false,
///     ..EmulationConfig::analysis()
/// };
/// ```
#[derive(Clone, Debug)]
pub struct EmulationConfig {
    /// Execution limits controlling resource usage.
    ///
    /// Includes maximum instructions, call depth, heap size, and timeout.
    pub limits: EmulationLimits,

    /// Behavior when encountering methods without stubs.
    ///
    /// Determines whether to return symbolic values, fail, use defaults,
    /// or skip the call entirely.
    pub unknown_method: UnknownMethodBehavior,

    /// Whether to track symbolic values for data flow analysis.
    ///
    /// When enabled, operations on unknown values produce symbolic
    /// expressions that can be analyzed for patterns.
    pub symbolic_tracking: bool,

    /// Whether to enable multi-threading support.
    ///
    /// When enabled, the emulator can handle Thread.Start and
    /// related operations. Disable for deterministic single-threaded
    /// execution.
    pub threading_enabled: bool,

    /// Thread scheduling quantum in instructions.
    ///
    /// Number of instructions to execute per thread before
    /// switching to the next ready thread.
    pub thread_quantum: usize,

    /// Maximum number of concurrent threads.
    ///
    /// Limits the number of threads that can be created during
    /// emulation to prevent resource exhaustion.
    pub max_threads: usize,

    /// Whether to enable exception handling.
    ///
    /// When enabled, try/catch/finally blocks are respected.
    /// Disable for simpler analysis that ignores exceptions.
    pub exception_handling: bool,

    /// Whether to allow stack unwinding on unhandled exceptions.
    ///
    /// When true, unhandled exceptions propagate up the call stack.
    /// When false, they immediately terminate emulation.
    pub allow_unwind: bool,

    /// Target pointer size for native int/uint types.
    ///
    /// Derived from the PE header (PE32 → `Bit32`, PE32+ → `Bit64`).
    /// Auto-detected by [`super::ProcessBuilder::build()`] when an assembly is loaded;
    /// defaults to `Bit64`.
    pub pointer_size: PointerSize,

    /// Memory subsystem configuration.
    ///
    /// Controls heap size, stack size, and memory tracking options.
    pub memory: MemoryConfig,

    /// Method stub configuration.
    ///
    /// Controls which categories of stubs are enabled and strict mode.
    pub stubs: StubConfig,

    /// Logging and tracing configuration.
    ///
    /// Controls what events are logged during emulation.
    pub tracing: TracingConfig,
}

/// Limits for emulation execution.
///
/// `EmulationLimits` provides safety boundaries to prevent runaway emulation
/// from consuming excessive resources. When any limit is reached, emulation
/// stops with a [`LimitReached`](crate::emulation::engine::EmulationOutcome::LimitReached)
/// outcome.
///
/// # Builder Pattern
///
/// Use the builder methods for fluent configuration:
///
/// ```rust,no_run
/// use dotscope::emulation::EmulationLimits;
///
/// let limits = EmulationLimits::new()
///     .with_max_instructions(5_000_000)
///     .with_max_call_depth(100)
///     .with_timeout_ms(30_000);
/// ```
///
/// # Default Values
///
/// | Limit | Default Value |
/// |-------|---------------|
/// | `max_instructions` | 10,000,000 |
/// | `max_call_depth` | 1,000 |
/// | `max_heap_objects` | 100,000 |
/// | `max_heap_bytes` | 256 MB |
/// | `max_unmanaged_bytes` | 64 MB |
/// | `timeout_ms` | 60,000 (1 minute) |
#[derive(Clone, Debug)]
pub struct EmulationLimits {
    /// Maximum instructions to execute.
    ///
    /// Set to 0 for unlimited execution. When exceeded, emulation
    /// stops with an instruction limit error.
    pub max_instructions: u64,

    /// Maximum call stack depth.
    ///
    /// Limits nested method calls to prevent stack overflow from
    /// infinite recursion.
    pub max_call_depth: usize,

    /// Maximum number of heap object allocations.
    ///
    /// Limits the total number of objects that can be created on
    /// the managed heap.
    pub max_heap_objects: usize,

    /// Maximum total heap size in bytes.
    ///
    /// Limits the total memory that can be allocated on the
    /// managed heap.
    pub max_heap_bytes: usize,

    /// Maximum unmanaged memory allocations in bytes.
    ///
    /// Limits memory allocated via Marshal.AllocHGlobal and
    /// similar unmanaged allocation methods.
    pub max_unmanaged_bytes: usize,

    /// Timeout in milliseconds.
    ///
    /// Set to 0 for no timeout. When exceeded, emulation stops
    /// with a timeout error.
    pub timeout_ms: u64,
}

/// Memory subsystem configuration.
///
/// Controls the emulated memory environment including heap sizing,
/// stack allocation, and memory tracking options.
///
/// # Default Values
///
/// | Setting | Default Value |
/// |---------|---------------|
/// | `initial_heap_size` | 16 MB |
/// | `max_heap_size` | 256 MB |
/// | `stack_size` | 1 MB |
/// | `address_space_bits` | 32 |
/// | `track_access` | false |
/// | `zero_init` | true |
#[derive(Clone, Debug)]
pub struct MemoryConfig {
    /// Initial heap size in bytes.
    ///
    /// The heap will be pre-allocated to this size at startup.
    pub initial_heap_size: usize,

    /// Maximum heap size in bytes.
    ///
    /// The heap can grow up to this limit. Allocations beyond
    /// this limit will fail.
    pub max_heap_size: usize,

    /// Default stack size per thread in bytes.
    ///
    /// Each thread receives this amount of stack space for
    /// local variables and call frames.
    pub stack_size: usize,

    /// Address space size in bits.
    ///
    /// Typically 32 for 32-bit processes (4 GB address space)
    /// or 64 for 64-bit processes.
    pub address_space_bits: u8,

    /// Whether to track memory access patterns.
    ///
    /// When enabled, reads and writes are recorded for analysis.
    /// This adds overhead but enables access pattern analysis.
    pub track_access: bool,

    /// Whether to zero-initialize allocations.
    ///
    /// When true, all allocated memory is initialized to zero.
    /// Matches .NET behavior for managed allocations.
    pub zero_init: bool,
}

/// Hook registration configuration.
///
/// Controls which categories of method hooks are registered and how
/// missing hooks are handled. Hooks provide implementations for
/// BCL (Base Class Library) methods that cannot be directly emulated.
///
/// # Hook Categories
///
/// Hooks are organized by functionality:
///
/// - **BCL**: Core types like String, Array, Object
/// - **P/Invoke**: Platform invoke / native interop
/// - **Reflection**: Type inspection and dynamic invocation
/// - **Crypto**: Cryptographic operations (AES, SHA, RSA)
/// - **I/O**: File and stream operations
/// - **Threading**: Thread and synchronization primitives
///
/// # Strict Mode
///
/// When `strict_mode` is enabled, calling a method without a hook
/// causes emulation to fail. When disabled, missing hooks return
/// symbolic or default values based on [`UnknownMethodBehavior`].
///
/// # Default Values
///
/// All hook categories are enabled by default with strict mode disabled.
#[derive(Clone, Debug)]
pub struct StubConfig {
    /// Enable Base Class Library hooks.
    ///
    /// Registers hooks for core types like String, Array,
    /// Object, and basic operations.
    pub bcl_stubs: bool,

    /// Enable P/Invoke hooks.
    ///
    /// Registers hooks for common platform invoke calls
    /// like kernel32 and user32 functions.
    pub pinvoke_stubs: bool,

    /// Enable reflection hooks.
    ///
    /// Registers hooks for Type, MethodInfo, and related
    /// reflection APIs.
    pub reflection_stubs: bool,

    /// Enable cryptographic hooks.
    ///
    /// Registers hooks for AES, SHA, MD5, RSA, and other
    /// cryptographic operations.
    pub crypto_stubs: bool,

    /// Enable I/O hooks.
    ///
    /// Registers hooks for Stream, File, BinaryReader,
    /// and related I/O types.
    pub io_stubs: bool,

    /// Enable threading hooks.
    ///
    /// Registers hooks for Thread, Monitor, Mutex, and
    /// related synchronization types.
    pub threading_stubs: bool,

    /// Enable strict mode.
    ///
    /// When true, calling a method without a registered hook causes
    /// emulation to fail immediately. When false, the behavior is
    /// determined by [`UnknownMethodBehavior`].
    pub strict_mode: bool,
}

/// Tracing and logging configuration.
///
/// Controls what events are logged during emulation for debugging
/// and analysis purposes. Enabling tracing adds overhead but provides
/// visibility into emulation behavior.
///
/// # Performance Impact
///
/// Tracing has varying performance impact:
///
/// - `trace_exceptions`: Low (exceptions are rare)
/// - `trace_calls`: Medium (one event per call)
/// - `trace_hooks`: Medium (one event per hook call)
/// - `trace_heap`: High (one event per allocation)
/// - `trace_instructions`: Very High (one event per instruction)
///
/// # File-Based Tracing
///
/// When `output_path` is set, trace events are written to the specified file
/// instead of being stored in memory. This is useful for analyzing long-running
/// emulations where memory-based tracing would be impractical.
///
/// # Default Values
///
/// Only exception tracing is enabled by default to minimize overhead
/// while capturing error information.
#[derive(Clone, Debug)]
pub struct TracingConfig {
    /// Log individual instruction execution.
    ///
    /// Records each CIL instruction as it executes, including:
    /// - Method token and IL offset
    /// - Opcode mnemonic and operands
    /// - Stack state before/after execution
    ///
    /// Very high overhead but useful for detailed debugging.
    pub trace_instructions: bool,

    /// Log method calls and returns.
    ///
    /// Records method entry and exit with arguments and return values.
    pub trace_calls: bool,

    /// Log heap operations.
    ///
    /// Records object allocations, array creations, and garbage
    /// collection events.
    pub trace_heap: bool,

    /// Log exception handling.
    ///
    /// Records exception throws, catches, and finally block execution.
    /// Enabled by default.
    pub trace_exceptions: bool,

    /// Log stub invocations.
    ///
    /// Records when method stubs are called and their return values.
    pub trace_stubs: bool,

    /// Log array element access operations.
    ///
    /// Records stelem/ldelem operations with index and value information.
    /// Very high overhead but critical for debugging DynCipher state machines
    /// and key derivation routines.
    pub trace_array_ops: bool,

    /// Maximum trace entries to keep in memory.
    ///
    /// Set to 0 for unlimited. When exceeded, oldest entries are
    /// discarded to prevent memory exhaustion. Ignored when
    /// `output_path` is set (file-based tracing is streaming).
    pub max_trace_entries: usize,

    /// Output file path for trace events.
    ///
    /// When set, trace events are written to this file instead of
    /// being stored in memory. The file is created/truncated when
    /// emulation starts. Events are written as they occur for
    /// real-time analysis.
    ///
    /// Format: One JSON object per line (JSONL/NDJSON format).
    pub output_path: Option<PathBuf>,

    /// Context prefix for trace events.
    ///
    /// When set, this prefix is included in each trace event's JSON output
    /// as a "context" field. This helps distinguish events from different
    /// emulation contexts when sharing a trace file.
    ///
    /// Common prefixes:
    /// - "warmup" for static constructor initialization
    /// - "decryption-N" for string decryption calls (N = call index)
    /// - "anti-tamper" for anti-tamper processing
    pub context_prefix: Option<String>,
}

/// Behavior when encountering methods without registered hooks.
///
/// When the emulator encounters a method call for which no hook is
/// registered, this setting determines what happens.
///
/// # Choosing a Behavior
///
/// - Use [`Emulate`](Self::Emulate) to execute the method's CIL bytecode
///   (default, works for user-defined methods with bodies)
/// - Use [`Symbolic`](Self::Symbolic) for data flow analysis where you want
///   to track how unknown values propagate
/// - Use [`Fail`](Self::Fail) for strict emulation where all methods must
///   have hook implementations
/// - Use [`Default`](Self::Default) for extraction scenarios where return
///   values often don't matter
/// - Use [`Skip`](Self::Skip) for simple constant folding where you want
///   to ignore irrelevant calls
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum UnknownMethodBehavior {
    /// Attempt to emulate the method's CIL bytecode.
    ///
    /// The emulator will try to execute the method's actual bytecode.
    /// This works for user-defined methods with bodies but will fall back
    /// to symbolic values for methods without bodies (e.g., P/Invoke).
    ///
    /// Best for: User-defined methods, full emulation scenarios.
    #[default]
    Emulate,

    /// Return a symbolic value representing the unknown result.
    ///
    /// The symbolic value can be tracked through subsequent operations
    /// to analyze data flow patterns. Best for static analysis scenarios.
    Symbolic,

    /// Fail with an error immediately.
    ///
    /// Stops emulation with an error indicating the missing method.
    /// Use for strict emulation where all methods must be handled.
    Fail,

    /// Return a default value based on the return type.
    ///
    /// Returns null for reference types, 0 for integers, 0.0 for floats,
    /// false for booleans. Useful for extraction where return values
    /// often don't affect the target behavior.
    Default,

    /// Skip the call entirely (treat as no-op).
    ///
    /// The call is ignored and execution continues. No value is pushed
    /// for methods with return values (may cause stack imbalance if
    /// the return value is used).
    Skip,
}

/// Configuration for what to capture during emulation.
///
/// `CaptureConfig` determines what runtime data is collected and stored
/// during emulation. Captured data can be retrieved after emulation
/// completes for analysis or extraction.
///
/// # Use Cases
///
/// - **Unpacking**: Enable `assemblies` to capture dynamically loaded assemblies
/// - **String Decryption**: Enable `strings` to capture decrypted strings
/// - **Behavioral Analysis**: Enable `file_operations` and `network_operations`
/// - **Memory Dumping**: Add regions to `memory_regions` to capture writes
///
/// # Example
///
/// ```rust,no_run
/// use dotscope::emulation::CaptureConfig;
///
/// let config = CaptureConfig {
///     assemblies: true,
///     strings: true,
///     ..Default::default()
/// };
/// ```
///
/// # Default Values
///
/// By default, no capture is enabled to minimize overhead.
#[derive(Clone, Debug, Default)]
pub struct CaptureConfig {
    /// Capture assemblies loaded via Assembly.Load.
    ///
    /// When enabled, byte arrays passed to Assembly.Load are captured
    /// and can be retrieved via [`super::EmulationProcess::captured_assemblies`].
    pub assemblies: bool,

    /// Capture writes to specific memory regions.
    ///
    /// Ranges are specified as `start..end` (exclusive). Writes within
    /// these ranges are captured with their data.
    pub memory_regions: Vec<Range<u64>>,

    /// Capture decrypted or constructed strings.
    ///
    /// When enabled, strings created during emulation are captured,
    /// useful for extracting obfuscated string literals.
    pub strings: bool,

    /// Capture file system operations.
    ///
    /// Records file opens, reads, writes, and closes performed
    /// during emulation.
    pub file_operations: bool,

    /// Capture network operations.
    ///
    /// Records socket operations, HTTP requests, and other network
    /// activity during emulation.
    pub network_operations: bool,
}

impl Default for EmulationConfig {
    /// Creates a default emulation configuration.
    ///
    /// See the struct documentation for default values.
    fn default() -> Self {
        Self {
            limits: EmulationLimits::default(),
            unknown_method: UnknownMethodBehavior::Symbolic,
            symbolic_tracking: false,
            threading_enabled: true,
            thread_quantum: 1000,
            max_threads: 16,
            exception_handling: true,
            allow_unwind: true,
            pointer_size: PointerSize::Bit64,
            memory: MemoryConfig::default(),
            stubs: StubConfig::default(),
            tracing: TracingConfig::default(),
        }
    }
}

impl Default for EmulationLimits {
    /// Creates default execution limits.
    ///
    /// See the struct documentation for default values.
    fn default() -> Self {
        Self {
            max_instructions: 10_000_000,
            max_call_depth: 1000,
            max_heap_objects: 100_000,
            max_heap_bytes: 256 * 1024 * 1024,     // 256 MB
            max_unmanaged_bytes: 64 * 1024 * 1024, // 64 MB
            timeout_ms: 60_000,                    // 1 minute
        }
    }
}

impl Default for MemoryConfig {
    /// Creates default memory configuration.
    ///
    /// See the struct documentation for default values.
    fn default() -> Self {
        Self {
            initial_heap_size: 16 * 1024 * 1024, // 16 MB
            max_heap_size: 256 * 1024 * 1024,    // 256 MB
            stack_size: 1024 * 1024,             // 1 MB
            address_space_bits: 32,
            track_access: false,
            zero_init: true,
        }
    }
}

impl Default for StubConfig {
    /// Creates default stub configuration with all stubs enabled.
    ///
    /// See the struct documentation for default values.
    fn default() -> Self {
        Self {
            bcl_stubs: true,
            pinvoke_stubs: true,
            reflection_stubs: true,
            crypto_stubs: true,
            io_stubs: true,
            threading_stubs: true,
            strict_mode: false,
        }
    }
}

impl TracingConfig {
    /// Creates a tracing configuration that logs everything to a file.
    ///
    /// This enables all tracing categories and writes to the specified file path.
    /// Useful for debugging complex emulation issues.
    ///
    /// # Arguments
    ///
    /// * `path` - File path to write trace events to (created/truncated on start)
    ///
    /// # Example
    ///
    /// ```ignore
    /// let tracing = TracingConfig::full_trace("emulation.trace");
    /// let config = EmulationConfig {
    ///     tracing,
    ///     ..Default::default()
    /// };
    /// ```
    #[must_use]
    pub fn full_trace<P: Into<PathBuf>>(path: P) -> Self {
        Self {
            trace_instructions: true,
            trace_calls: true,
            trace_heap: true,
            trace_exceptions: true,
            trace_stubs: true,
            trace_array_ops: true,
            max_trace_entries: 0, // Unlimited for file-based
            output_path: Some(path.into()),
            context_prefix: None,
        }
    }

    /// Creates a tracing configuration for call-level tracing to a file.
    ///
    /// Traces method calls, exceptions, and stubs but not individual instructions.
    /// Good balance between detail and performance.
    #[must_use]
    pub fn call_trace<P: Into<PathBuf>>(path: P) -> Self {
        Self {
            trace_instructions: false,
            trace_calls: true,
            trace_heap: false,
            trace_exceptions: true,
            trace_stubs: true,
            trace_array_ops: false,
            max_trace_entries: 0,
            output_path: Some(path.into()),
            context_prefix: None,
        }
    }

    /// Sets the context prefix for trace events.
    ///
    /// # Arguments
    ///
    /// * `prefix` - The context prefix to use (e.g., "warmup", "decryption-0")
    ///
    /// # Returns
    ///
    /// Returns `self` for method chaining.
    #[must_use]
    pub fn with_context(mut self, prefix: impl Into<String>) -> Self {
        self.context_prefix = Some(prefix.into());
        self
    }

    /// Checks if any tracing is enabled.
    #[must_use]
    pub fn is_enabled(&self) -> bool {
        self.trace_instructions
            || self.trace_calls
            || self.trace_heap
            || self.trace_exceptions
            || self.trace_stubs
            || self.trace_array_ops
    }

    /// Checks if file-based tracing is configured.
    #[must_use]
    pub fn has_output_file(&self) -> bool {
        self.output_path.is_some()
    }
}

impl Default for TracingConfig {
    /// Creates default tracing configuration.
    ///
    /// Only exception tracing is enabled by default.
    fn default() -> Self {
        Self {
            trace_instructions: false,
            trace_calls: false,
            trace_heap: false,
            trace_exceptions: true,
            trace_stubs: false,
            trace_array_ops: false,
            max_trace_entries: 10_000,
            output_path: None,
            context_prefix: None,
        }
    }
}

/// Preset configurations for common use cases.
impl EmulationConfig {
    /// Creates a configuration optimized for extracting packed/encrypted assemblies.
    ///
    /// This preset is designed for unpacking scenarios where the goal is to
    /// run the unpacker/decryptor and capture the resulting assembly.
    ///
    /// # Settings
    ///
    /// - **Instruction limit**: 50 million (high to allow complex unpacking)
    /// - **Unknown methods**: Return default values (unpacker may call irrelevant BCL methods)
    /// - **Threading**: Enabled (some unpackers use threads)
    /// - **Exceptions**: Enabled (unpackers may use try/catch)
    /// - **Strict mode**: Disabled (tolerate missing stubs)
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use dotscope::emulation::{EmulationConfig, ProcessBuilder};
    /// use dotscope::CilObject;
    ///
    /// # fn main() -> Result<(), dotscope::Error> {
    /// # let packed_assembly = CilObject::from_path("sample.exe")?;
    /// let config = EmulationConfig::extraction();
    /// let process = ProcessBuilder::new()
    ///     .assembly(packed_assembly)
    ///     .config(config)
    ///     .capture_assemblies()
    ///     .build()?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn extraction() -> Self {
        Self {
            limits: EmulationLimits {
                max_instructions: 50_000_000,
                ..Default::default()
            },
            unknown_method: UnknownMethodBehavior::Default,
            symbolic_tracking: false,
            threading_enabled: true,
            exception_handling: true,
            stubs: StubConfig {
                strict_mode: false,
                ..Default::default()
            },
            ..Default::default()
        }
    }

    /// Creates a configuration optimized for static analysis and constant propagation.
    ///
    /// This preset is designed for analyzing code behavior without full execution,
    /// tracking symbolic values to understand data flow.
    ///
    /// # Settings
    ///
    /// - **Instruction limit**: 1 million (moderate for analysis)
    /// - **Unknown methods**: Return symbolic values (track data flow)
    /// - **Symbolic tracking**: Enabled
    /// - **Threading**: Disabled (for determinism)
    /// - **Exceptions**: Disabled (simplify control flow)
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use dotscope::emulation::EmulationConfig;
    ///
    /// let config = EmulationConfig::analysis();
    /// // Use for constant propagation, dead code detection, etc.
    /// ```
    #[must_use]
    pub fn analysis() -> Self {
        Self {
            limits: EmulationLimits {
                max_instructions: 1_000_000,
                ..Default::default()
            },
            unknown_method: UnknownMethodBehavior::Symbolic,
            symbolic_tracking: true,
            threading_enabled: false,
            exception_handling: false,
            stubs: StubConfig {
                strict_mode: false,
                ..Default::default()
            },
            ..Default::default()
        }
    }

    /// Creates a configuration for complete emulation with all features enabled.
    ///
    /// This preset provides the most accurate emulation at the cost of requiring
    /// stubs for all called methods.
    ///
    /// # Settings
    ///
    /// - **Instruction limit**: 100 million (very high for complex programs)
    /// - **Timeout**: 5 minutes
    /// - **Unknown methods**: Fail (strict - all methods must have stubs)
    /// - **Threading**: Enabled
    /// - **Exceptions**: Enabled
    /// - **Strict mode**: Enabled
    ///
    /// # Warning
    ///
    /// This mode will fail if any called method lacks a stub. Only use when
    /// you have comprehensive stub coverage.
    #[must_use]
    pub fn full() -> Self {
        Self {
            limits: EmulationLimits {
                max_instructions: 100_000_000,
                timeout_ms: 300_000, // 5 minutes
                ..Default::default()
            },
            unknown_method: UnknownMethodBehavior::Fail,
            symbolic_tracking: false,
            threading_enabled: true,
            exception_handling: true,
            stubs: StubConfig {
                strict_mode: true,
                ..Default::default()
            },
            tracing: TracingConfig {
                trace_exceptions: true,
                ..Default::default()
            },
            ..Default::default()
        }
    }

    /// Creates a minimal configuration for simple constant folding.
    ///
    /// This preset is designed for lightweight analysis scenarios like
    /// evaluating simple expressions or folding constants.
    ///
    /// # Settings
    ///
    /// - **Instruction limit**: 10,000 (very low)
    /// - **Call depth**: 10 (shallow)
    /// - **Unknown methods**: Skip (ignore irrelevant calls)
    /// - **Threading**: Disabled
    /// - **Exceptions**: Disabled
    /// - **Stubs**: Only BCL enabled
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use dotscope::emulation::EmulationConfig;
    ///
    /// let config = EmulationConfig::minimal();
    /// // Use for simple constant evaluation
    /// ```
    #[must_use]
    pub fn minimal() -> Self {
        Self {
            limits: EmulationLimits {
                max_instructions: 10_000,
                max_call_depth: 10,
                ..Default::default()
            },
            unknown_method: UnknownMethodBehavior::Skip,
            symbolic_tracking: false,
            threading_enabled: false,
            exception_handling: false,
            stubs: StubConfig {
                bcl_stubs: true,
                pinvoke_stubs: false,
                reflection_stubs: false,
                crypto_stubs: false,
                io_stubs: false,
                threading_stubs: false,
                strict_mode: false,
            },
            ..Default::default()
        }
    }
}

/// Builder methods for [`EmulationLimits`].
impl EmulationLimits {
    /// Creates new limits with default values.
    ///
    /// Equivalent to [`EmulationLimits::default()`]. Use the `with_*` methods
    /// to customize individual limits.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use dotscope::emulation::EmulationLimits;
    ///
    /// let limits = EmulationLimits::new()
    ///     .with_max_instructions(5_000_000)
    ///     .with_timeout_ms(30_000);
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the maximum instruction count.
    ///
    /// # Arguments
    ///
    /// * `max` - Maximum instructions to execute (0 for unlimited)
    ///
    /// # Returns
    ///
    /// Returns `self` for method chaining.
    #[must_use]
    pub fn with_max_instructions(mut self, max: u64) -> Self {
        self.max_instructions = max;
        self
    }

    /// Sets the maximum call stack depth.
    ///
    /// # Arguments
    ///
    /// * `max` - Maximum nested method calls
    ///
    /// # Returns
    ///
    /// Returns `self` for method chaining.
    #[must_use]
    pub fn with_max_call_depth(mut self, max: usize) -> Self {
        self.max_call_depth = max;
        self
    }

    /// Sets the maximum number of heap objects.
    ///
    /// # Arguments
    ///
    /// * `max` - Maximum objects that can be allocated
    ///
    /// # Returns
    ///
    /// Returns `self` for method chaining.
    #[must_use]
    pub fn with_max_heap_objects(mut self, max: usize) -> Self {
        self.max_heap_objects = max;
        self
    }

    /// Sets the maximum heap size in bytes.
    ///
    /// # Arguments
    ///
    /// * `max` - Maximum total heap allocation in bytes
    ///
    /// # Returns
    ///
    /// Returns `self` for method chaining.
    #[must_use]
    pub fn with_max_heap_bytes(mut self, max: usize) -> Self {
        self.max_heap_bytes = max;
        self
    }

    /// Sets the execution timeout in milliseconds.
    ///
    /// # Arguments
    ///
    /// * `ms` - Timeout in milliseconds (0 for no timeout)
    ///
    /// # Returns
    ///
    /// Returns `self` for method chaining.
    #[must_use]
    pub fn with_timeout_ms(mut self, ms: u64) -> Self {
        self.timeout_ms = ms;
        self
    }
}

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

    #[test]
    fn test_default_config() {
        let config = EmulationConfig::default();
        assert_eq!(config.limits.max_instructions, 10_000_000);
        assert!(config.threading_enabled);
        assert!(config.exception_handling);
    }

    #[test]
    fn test_extraction_preset() {
        let config = EmulationConfig::extraction();
        assert_eq!(config.limits.max_instructions, 50_000_000);
        assert_eq!(config.unknown_method, UnknownMethodBehavior::Default);
    }

    #[test]
    fn test_analysis_preset() {
        let config = EmulationConfig::analysis();
        assert!(config.symbolic_tracking);
        assert!(!config.threading_enabled);
    }

    #[test]
    fn test_full_preset() {
        let config = EmulationConfig::full();
        assert_eq!(config.unknown_method, UnknownMethodBehavior::Fail);
        assert!(config.stubs.strict_mode);
    }

    #[test]
    fn test_minimal_preset() {
        let config = EmulationConfig::minimal();
        assert_eq!(config.limits.max_instructions, 10_000);
        assert!(!config.stubs.pinvoke_stubs);
    }

    #[test]
    fn test_limits_builder() {
        let limits = EmulationLimits::new()
            .with_max_instructions(5000)
            .with_max_call_depth(50)
            .with_timeout_ms(30_000);

        assert_eq!(limits.max_instructions, 5000);
        assert_eq!(limits.max_call_depth, 50);
        assert_eq!(limits.timeout_ms, 30_000);
    }
}