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
//! Unified constant resolution for SSA variables.
//!
//! The [`ValueResolver`] composes [`ConstEvaluator`], [`PhiAnalyzer`], and optionally
//! [`SsaEvaluator`] into a single reusable entry point for demand-driven constant
//! resolution. It replaces ad-hoc tracing logic (like the former `trace_to_constant`)
//! with a three-tier fallback strategy:
//!
//! 1. **ConstEvaluator** — handles all instruction-defined ops (arithmetic, bitwise,
//!    comparisons, conversions) with caching and cycle detection.
//! 2. **PhiAnalyzer** — checks whether all PHI operands resolve to the same constant.
//! 3. **SsaEvaluator** (path-aware fallback) — uses `resolve_with_trace` for
//!    instruction-defined vars that `ConstEvaluator` couldn't fold (e.g., XOR with
//!    a Call operand).
//!
//! # Example
//!
//! ```rust,ignore
//! use dotscope::analysis::{ValueResolver, SsaFunction};
//! use dotscope::metadata::typesystem::PointerSize;
//!
//! let mut resolver = ValueResolver::new(&ssa, PointerSize::Bit64)
//!     .with_path_aware_fallback();
//! resolver.load_known_values(ctx, method_token);
//!
//! if let Some(value) = resolver.resolve(some_var) {
//!     println!("Resolved to: {:?}", value);
//! }
//! ```

use crate::{
    analysis::ssa::{ConstEvaluator, ConstValue, PhiAnalyzer, SsaEvaluator, SsaFunction, SsaVarId},
    metadata::typesystem::PointerSize,
};

#[cfg(feature = "compiler")]
use crate::{compiler::CompilerContext, metadata::token::Token};

/// Demand-driven constant resolver composing multiple analysis components.
///
/// Provides a unified API for resolving SSA variables to constant values,
/// combining the strengths of [`ConstEvaluator`] (instruction folding),
/// [`PhiAnalyzer`] (uniform PHI detection), and optionally [`SsaEvaluator`]
/// (path-aware tracing for variables that pure constant folding can't handle).
pub struct ValueResolver<'a> {
    ssa: &'a SsaFunction,
    evaluator: ConstEvaluator<'a>,
    phi: PhiAnalyzer<'a>,
    resolve_phis: bool,
    path_aware_fallback: bool,
    ptr_size: PointerSize,
}

impl<'a> ValueResolver<'a> {
    /// Creates a new resolver with PHI resolution enabled and path-aware fallback disabled.
    #[must_use]
    pub fn new(ssa: &'a SsaFunction, ptr_size: PointerSize) -> Self {
        Self {
            ssa,
            evaluator: ConstEvaluator::new(ssa, ptr_size),
            phi: PhiAnalyzer::new(ssa),
            resolve_phis: true,
            path_aware_fallback: false,
            ptr_size,
        }
    }

    /// Enables the path-aware fallback via [`SsaEvaluator`].
    ///
    /// When enabled, variables that `ConstEvaluator` can't fold (e.g., XOR where
    /// one operand comes from a Call) will be attempted via `resolve_with_trace`.
    #[must_use]
    pub fn with_path_aware_fallback(mut self) -> Self {
        self.path_aware_fallback = true;
        self
    }

    /// Bulk-injects all known values for a method from the [`CompilerContext`].
    ///
    /// This loads values discovered by earlier passes (e.g., constant propagation)
    /// into the inner [`ConstEvaluator`] so they're available during resolution.
    #[cfg(feature = "compiler")]
    pub fn load_known_values(&mut self, ctx: &CompilerContext, method_token: Token) {
        ctx.for_each_known_value(method_token, |var, val| {
            self.evaluator.set_known(var, val.clone());
        });
    }

    /// Injects a single known value into the resolver.
    pub fn set_known(&mut self, var: SsaVarId, value: ConstValue) {
        self.evaluator.set_known(var, value);
    }

    /// Resolves a variable to a constant using a three-tier fallback strategy.
    ///
    /// 1. Try [`ConstEvaluator`] (all instruction-defined ops).
    /// 2. If PHI-defined, check for uniform constant via [`PhiAnalyzer`].
    /// 3. If path-aware fallback is enabled, try [`SsaEvaluator::resolve_with_trace`].
    pub fn resolve(&mut self, var: SsaVarId) -> Option<ConstValue> {
        // 1. Try ConstEvaluator (handles Const, arithmetic, bitwise, etc. with caching)
        if let Some(val) = self.evaluator.evaluate_var(var) {
            return Some(val);
        }

        // 2. PHI uniform constant check
        if self.resolve_phis {
            if let Some((_, phi)) = self.ssa.find_phi_defining(var) {
                let result = self.phi.uniform_constant(phi, &mut self.evaluator);
                if result.is_some() {
                    return result;
                }
            }
        }

        // 3. Path-aware fallback via SsaEvaluator (for instruction-defined vars
        //    that ConstEvaluator couldn't fold, e.g. XOR with a Call operand)
        if self.path_aware_fallback && self.ssa.get_definition(var).is_some() {
            let mut eval = SsaEvaluator::new(self.ssa, self.ptr_size);
            if let Some(resolved) = eval.resolve_with_trace(var, 15) {
                if let Some(c) = resolved.as_constant() {
                    self.evaluator.set_known(var, c.clone());
                    return Some(c.clone());
                }
            }
        }

        None
    }

    /// Resolves all variables to constants. Returns `None` if any variable can't be resolved.
    pub fn resolve_all(&mut self, vars: &[SsaVarId]) -> Option<Vec<ConstValue>> {
        let mut result = Vec::with_capacity(vars.len());
        for &var in vars {
            result.push(self.resolve(var)?);
        }
        Some(result)
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        analysis::ssa::{
            ConstValue, DefSite, PhiNode, PhiOperand, SsaBlock, SsaFunction, SsaInstruction, SsaOp,
            SsaVarId, SsaVariable, VariableOrigin,
        },
        metadata::typesystem::PointerSize,
    };

    use super::ValueResolver;

    // ── Basic constant resolution ────────────────────────────────────

    #[test]
    fn test_resolve_const() {
        let mut ssa = SsaFunction::new(0, 0);
        let mut block = SsaBlock::new(0);

        let var = SsaVariable::new(VariableOrigin::Stack(0), 0, DefSite::instruction(0, 0));
        let var_id = var.id();
        ssa.add_variable(var);

        block.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: var_id,
            value: ConstValue::I32(42),
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Return { value: None }));
        ssa.add_block(block);

        let mut resolver = ValueResolver::new(&ssa, PointerSize::Bit64);
        assert_eq!(resolver.resolve(var_id), Some(ConstValue::I32(42)));
    }

    #[test]
    fn test_resolve_arithmetic_chain() {
        let mut ssa = SsaFunction::new(0, 0);
        let mut block = SsaBlock::new(0);

        // v0 = 10
        let v0 = SsaVariable::new(VariableOrigin::Stack(0), 0, DefSite::instruction(0, 0));
        let v0_id = v0.id();
        ssa.add_variable(v0);

        // v1 = 3
        let v1 = SsaVariable::new(VariableOrigin::Stack(1), 0, DefSite::instruction(0, 1));
        let v1_id = v1.id();
        ssa.add_variable(v1);

        // v2 = v0 + v1
        let v2 = SsaVariable::new(VariableOrigin::Stack(2), 0, DefSite::instruction(0, 2));
        let v2_id = v2.id();
        ssa.add_variable(v2);

        block.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: v0_id,
            value: ConstValue::I32(10),
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: v1_id,
            value: ConstValue::I32(3),
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Add {
            dest: v2_id,
            left: v0_id,
            right: v1_id,
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Return { value: None }));
        ssa.add_block(block);

        let mut resolver = ValueResolver::new(&ssa, PointerSize::Bit64);
        assert_eq!(resolver.resolve(v2_id), Some(ConstValue::I32(13)));
    }

    // ── PHI uniform constant ─────────────────────────────────────────

    #[test]
    fn test_resolve_phi_uniform() {
        let mut ssa = SsaFunction::new(0, 0);

        // Block 0: v0 = 42, jump to block 2
        let v0 = SsaVariable::new(VariableOrigin::Stack(0), 0, DefSite::instruction(0, 0));
        let v0_id = v0.id();
        ssa.add_variable(v0);

        let mut block0 = SsaBlock::new(0);
        block0.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: v0_id,
            value: ConstValue::I32(42),
        }));
        block0.add_instruction(SsaInstruction::synthetic(SsaOp::Jump { target: 2 }));
        ssa.add_block(block0);

        // Block 1: v1 = 42, jump to block 2
        let v1 = SsaVariable::new(VariableOrigin::Stack(1), 0, DefSite::instruction(1, 0));
        let v1_id = v1.id();
        ssa.add_variable(v1);

        let mut block1 = SsaBlock::new(1);
        block1.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: v1_id,
            value: ConstValue::I32(42),
        }));
        block1.add_instruction(SsaInstruction::synthetic(SsaOp::Jump { target: 2 }));
        ssa.add_block(block1);

        // Block 2: phi(v0, v1) - both are 42
        let phi_result = SsaVariable::new(VariableOrigin::Local(0), 0, DefSite::phi(2));
        let phi_result_id = phi_result.id();
        ssa.add_variable(phi_result);

        let mut block2 = SsaBlock::new(2);
        let mut phi = PhiNode::new(phi_result_id, VariableOrigin::Local(0));
        phi.add_operand(PhiOperand::new(v0_id, 0));
        phi.add_operand(PhiOperand::new(v1_id, 1));
        block2.add_phi(phi);
        block2.add_instruction(SsaInstruction::synthetic(SsaOp::Return { value: None }));
        ssa.add_block(block2);

        let mut resolver = ValueResolver::new(&ssa, PointerSize::Bit64);
        assert_eq!(resolver.resolve(phi_result_id), Some(ConstValue::I32(42)));
    }

    #[test]
    fn test_resolve_phi_non_uniform() {
        let mut ssa = SsaFunction::new(0, 0);

        // v0 = 42
        let v0 = SsaVariable::new(VariableOrigin::Stack(0), 0, DefSite::instruction(0, 0));
        let v0_id = v0.id();
        ssa.add_variable(v0);

        let mut block0 = SsaBlock::new(0);
        block0.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: v0_id,
            value: ConstValue::I32(42),
        }));
        block0.add_instruction(SsaInstruction::synthetic(SsaOp::Jump { target: 2 }));
        ssa.add_block(block0);

        // v1 = 99 (different)
        let v1 = SsaVariable::new(VariableOrigin::Stack(1), 0, DefSite::instruction(1, 0));
        let v1_id = v1.id();
        ssa.add_variable(v1);

        let mut block1 = SsaBlock::new(1);
        block1.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: v1_id,
            value: ConstValue::I32(99),
        }));
        block1.add_instruction(SsaInstruction::synthetic(SsaOp::Jump { target: 2 }));
        ssa.add_block(block1);

        // phi(v0, v1) - different values
        let phi_result = SsaVariable::new(VariableOrigin::Local(0), 0, DefSite::phi(2));
        let phi_result_id = phi_result.id();
        ssa.add_variable(phi_result);

        let mut block2 = SsaBlock::new(2);
        let mut phi = PhiNode::new(phi_result_id, VariableOrigin::Local(0));
        phi.add_operand(PhiOperand::new(v0_id, 0));
        phi.add_operand(PhiOperand::new(v1_id, 1));
        block2.add_phi(phi);
        block2.add_instruction(SsaInstruction::synthetic(SsaOp::Return { value: None }));
        ssa.add_block(block2);

        let mut resolver = ValueResolver::new(&ssa, PointerSize::Bit64);
        assert_eq!(resolver.resolve(phi_result_id), None);
    }

    // ── Known values / load_known_values ─────────────────────────────

    #[test]
    fn test_set_known() {
        let ssa = SsaFunction::new(0, 0);
        let var_id = SsaVarId::new();

        let mut resolver = ValueResolver::new(&ssa, PointerSize::Bit64);
        resolver.set_known(var_id, ConstValue::I32(999));

        assert_eq!(resolver.resolve(var_id), Some(ConstValue::I32(999)));
    }

    #[test]
    #[cfg(feature = "compiler")]
    fn test_load_known_values() {
        use std::sync::Arc;

        use crate::{analysis::CallGraph, compiler::CompilerContext, metadata::token::Token};

        let ssa = SsaFunction::new(0, 0);
        let ctx = CompilerContext::new(Arc::new(CallGraph::new()));
        let method = Token::new(0x06000001);

        let var1 = SsaVarId::new();
        let var2 = SsaVarId::new();
        ctx.add_known_value(method, var1, ConstValue::I32(10));
        ctx.add_known_value(method, var2, ConstValue::I32(20));

        let mut resolver = ValueResolver::new(&ssa, PointerSize::Bit64);
        resolver.load_known_values(&ctx, method);

        assert_eq!(resolver.resolve(var1), Some(ConstValue::I32(10)));
        assert_eq!(resolver.resolve(var2), Some(ConstValue::I32(20)));
    }

    // ── resolve_all ──────────────────────────────────────────────────

    #[test]
    fn test_resolve_all_success() {
        let ssa = SsaFunction::new(0, 0);

        let var1 = SsaVarId::new();
        let var2 = SsaVarId::new();

        let mut resolver = ValueResolver::new(&ssa, PointerSize::Bit64);
        resolver.set_known(var1, ConstValue::I32(1));
        resolver.set_known(var2, ConstValue::I32(2));

        let result = resolver.resolve_all(&[var1, var2]);
        assert_eq!(result, Some(vec![ConstValue::I32(1), ConstValue::I32(2)]));
    }

    #[test]
    fn test_resolve_all_partial_failure() {
        let ssa = SsaFunction::new(0, 0);

        let var1 = SsaVarId::new();
        let var2 = SsaVarId::new();

        let mut resolver = ValueResolver::new(&ssa, PointerSize::Bit64);
        resolver.set_known(var1, ConstValue::I32(1));
        // var2 unknown

        assert_eq!(resolver.resolve_all(&[var1, var2]), None);
    }

    #[test]
    fn test_resolve_all_empty() {
        let ssa = SsaFunction::new(0, 0);
        let mut resolver = ValueResolver::new(&ssa, PointerSize::Bit64);

        assert_eq!(resolver.resolve_all(&[]), Some(vec![]));
    }

    // ── Edge cases ───────────────────────────────────────────────────

    #[test]
    fn test_resolve_unknown_var() {
        let ssa = SsaFunction::new(0, 0);
        let unknown = SsaVarId::new();

        let mut resolver = ValueResolver::new(&ssa, PointerSize::Bit64);
        assert_eq!(resolver.resolve(unknown), None);
    }

    #[test]
    fn test_resolve_xor_both_const() {
        let mut ssa = SsaFunction::new(0, 0);
        let mut block = SsaBlock::new(0);

        let v0 = SsaVariable::new(VariableOrigin::Stack(0), 0, DefSite::instruction(0, 0));
        let v0_id = v0.id();
        ssa.add_variable(v0);

        let v1 = SsaVariable::new(VariableOrigin::Stack(1), 0, DefSite::instruction(0, 1));
        let v1_id = v1.id();
        ssa.add_variable(v1);

        let v2 = SsaVariable::new(VariableOrigin::Stack(2), 0, DefSite::instruction(0, 2));
        let v2_id = v2.id();
        ssa.add_variable(v2);

        block.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: v0_id,
            value: ConstValue::I32(0xFF),
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Const {
            dest: v1_id,
            value: ConstValue::I32(0x0F),
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Xor {
            dest: v2_id,
            left: v0_id,
            right: v1_id,
        }));
        block.add_instruction(SsaInstruction::synthetic(SsaOp::Return { value: None }));
        ssa.add_block(block);

        let mut resolver = ValueResolver::new(&ssa, PointerSize::Bit64);
        assert_eq!(resolver.resolve(v2_id), Some(ConstValue::I32(0xF0)));
    }
}