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
//! CIL emulation engine for .NET bytecode execution.
//!
//! This module provides a controlled execution environment for .NET CIL (Common
//! Intermediate Language) bytecode. The emulation engine is essential for
//! deobfuscation as many obfuscators rely on runtime computation of values,
//! dynamic string decryption, and control flow obfuscation that can only be
//! resolved through execution.
//!
//! # Architecture
//!
//! The emulation engine is organized into several sub-modules:
//!
//! - Runtime value representation with type safety and symbolic tracking
//! - Memory model including evaluation stack, locals, and managed heap
//! - Core interpreter and execution controller
//! - Hook system for intercepting method calls and providing custom behavior
//! - Process model for coordinating emulation
//! - Capture context for collecting results
//!
//! # Key Components
//!
//! ## Process Model
//! - [`crate::emulation::EmulationProcess`] - Central emulation process coordinating all components
//! - [`crate::emulation::ProcessBuilder`] - Fluent API for configuring emulation processes
//! - [`crate::emulation::EmulationConfig`] - Comprehensive configuration with presets
//!
//! ## Value System
//! - [`crate::emulation::EmValue`] - Runtime value representation for all CIL types
//! - [`crate::emulation::SymbolicValue`] - Tracks unknown/unresolved values during partial emulation
//! - [`crate::emulation::HeapRef`] - Reference to heap-allocated objects
//!
//! ## Memory Model
//! - [`crate::emulation::EvaluationStack`] - CIL evaluation stack with overflow protection
//! - [`crate::emulation::LocalVariables`] - Local variable storage with type-aware initialization
//! - [`crate::emulation::ManagedHeap`] - Simulated managed heap with memory limits
//! - [`crate::emulation::AddressSpace`] - Process-wide memory management
//!
//! ## Execution Engine
//! - [`crate::emulation::Interpreter`] - Core instruction interpreter
//! - [`crate::emulation::EmulationController`] - High-level execution control with limits
//! - [`crate::emulation::StepResult`] - Result of executing a single instruction
//!
//! ## Hook System
//! - [`crate::emulation::HookManager`] - Registry for method interception hooks
//! - [`crate::emulation::Hook`] - Builder for creating method hooks
//! - [`crate::emulation::HookContext`] - Information passed to hook handlers
//!
//! ## Result Capture
//! - [`crate::emulation::CaptureContext`] - Automatic result collection
//! - [`crate::emulation::CapturedAssembly`] - Captured Assembly.Load data
//! - [`crate::emulation::CapturedString`] - Captured decrypted strings
//!
//! ## Loading
//! - [`crate::emulation::PeLoader`] - PE image loader for memory mapping
//! - [`crate::emulation::DataLoader`] - Raw data loader
//!
//! # Usage Examples
//!
//! ## Process-Based Emulation
//!
//! ```rust,ignore
//! use dotscope::emulation::ProcessBuilder;
//! use dotscope::CilObject;
//!
//! let assembly = CilObject::from_path("sample.exe")?;
//! let pe_bytes = std::fs::read("sample.exe")?;
//!
//! let mut process = ProcessBuilder::new()
//! .assembly(assembly)
//! .map_pe_image(&pe_bytes, "sample.exe")
//! .for_extraction()
//! .capture_assemblies()
//! .build()?;
//!
//! // Get captured assemblies after execution
//! for (idx, asm) in process.captured_assemblies().iter().enumerate() {
//! std::fs::write(format!("extracted_{}.dll", idx), &asm.data)?;
//! }
//! ```
//!
//! ## String Decryption Pattern
//!
//! ```rust,ignore
//! use dotscope::emulation::ProcessBuilder;
//!
//! let process = ProcessBuilder::new()
//! .assembly(assembly)
//! .for_analysis()
//! .capture_strings()
//! .build()?;
//!
//! // After emulation
//! for string in process.captured_strings() {
//! println!("Decrypted: {}", string.value);
//! }
//! ```
//!
//! # Integration with Analysis
//!
//! The emulation engine integrates with the analysis infrastructure:
//!
//! | Component | Usage |
//! |-----------|-------|
//! | CFG | Structured control flow during emulation |
//! | SSA | Value tracking and constant propagation |
//! | Data Flow | Identifying which values need emulation |
//! | Call Graph | Deciding method inlining/hooking |
//!
//! # Execution Limits
//!
//! The emulator enforces several limits to prevent runaway execution:
//!
//! - **Instruction limit**: Maximum instructions to execute
//! - **Call depth limit**: Maximum call stack depth
//! - **Memory limit**: Maximum heap allocation
//! - **Timeout**: Wall-clock time limit
//!
//! # Thread Safety
//!
//! The emulation types are designed for single-threaded use within an analysis
//! context. The [`crate::emulation::EmulationProcess`] and related types are `Send` but not
//! `Sync`, as emulation inherently involves mutable state.
// Re-export primary types from value module
pub use ;
// Re-export primary types from memory module
pub use ;
// Re-export primary types from engine module
pub use ;
// Re-export primary types from process module
pub use ;
// Re-export primary types from exception module
pub use ;
// Re-export primary types from thread module
pub use ;
// Re-export primary types from runtime module
pub use ;
// Re-export primary types from capture module
pub use ;
// Re-export primary types from loader module
pub use ;
// Re-export primary types from fakeobjects module
pub use ;