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
//! Reassociation pass.
//!
//! This pass reorders operations to enable better constant folding. For example:
//!
//! ```text
//! (x + 5) + 3  →  x + (5 + 3)  →  x + 8
//! ```
//!
//! # Transformed Patterns
//!
//! ## Associative operations (`add`, `mul`, `and`, `or`, `xor`)
//!
//! For these operations, constants combine using the same operation:
//! - `(x + c1) + c2` → `x + (c1 + c2)`
//! - `(x * c1) * c2` → `x * (c1 * c2)`
//! - `(x ^ c1) ^ c2` → `x ^ (c1 ^ c2)` (common in obfuscation)
//!
//! ## Subtraction chains
//!
//! Subtraction is not associative, but constants combine with addition:
//! - `(x - c1) - c2` → `x - (c1 + c2)`
//!
//! ## Shift chains
//!
//! Shift amounts combine with addition:
//! - `(x << c1) << c2` → `x << (c1 + c2)`
//! - `(x >> c1) >> c2` → `x >> (c1 + c2)` (preserves signedness)
//!
//! # Implementation Strategy
//!
//! The pass works in three phases:
//! 1. Find constants and their defining instructions
//! 2. Identify operations where one operand is the result of another same-op with a constant
//! 3. Combine the constants and rewrite to `x op combined_const`
//!
//! The SCCP pass will then fold the combined constants in the next iteration.

use std::{collections::HashMap, sync::Arc};

use crate::{
    analysis::{ConstValue, DefUseIndex, SsaFunction, SsaOp, SsaVarId},
    compiler::{pass::SsaPass, CompilerContext, EventKind, EventLog},
    metadata::{token::Token, typesystem::PointerSize},
    CilObject, Result,
};

/// Reassociation pass that reorders operations to enable constant folding.
pub struct ReassociationPass;

impl Default for ReassociationPass {
    fn default() -> Self {
        Self::new()
    }
}

/// A candidate for reassociation.
#[derive(Debug)]
struct ReassociationCandidate {
    /// Block containing the outer operation
    block_idx: usize,
    /// Instruction index of the outer operation
    instr_idx: usize,
    /// The destination variable
    dest: SsaVarId,
    /// The non-constant operand (x in `(x op c1) op c2`)
    base_var: SsaVarId,
    /// The first constant variable (c1)
    const1_var: SsaVarId,
    /// The second constant variable (c2)
    const2_var: SsaVarId,
    /// The first constant value
    const1_value: ConstValue,
    /// The second constant value
    const2_value: ConstValue,
    /// Block and instruction of the inner operation (to mark for removal)
    inner_block: usize,
    inner_instr: usize,
    inner_dest: SsaVarId,
    /// The type of operation
    op_kind: OpKind,
}

/// The kind of operation that can be reassociated.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OpKind {
    /// Addition: constants combine with add
    Add,
    /// Subtraction: constants combine with add
    Sub,
    /// Multiplication: constants combine with mul
    Mul,
    /// Bitwise AND: constants combine with and
    And,
    /// Bitwise OR: constants combine with or
    Or,
    /// Bitwise XOR: constants combine with xor
    Xor,
    /// Shift left: shift amounts combine with add
    Shl,
    /// Shift right: shift amounts combine with add (preserves signedness)
    Shr { unsigned: bool },
}

impl OpKind {
    /// Combines two constants for reassociation.
    ///
    /// For associative operations (Add, Mul, And, Or, Xor), the combining
    /// operation is the same as the main operation.
    ///
    /// For non-associative operations that still benefit from reassociation:
    /// - Sub: `(x - c1) - c2` → `x - (c1 + c2)` (combine with add)
    /// - Shl/Shr: `(x << c1) << c2` → `x << (c1 + c2)` (combine with add)
    fn combine(
        self,
        c1: &ConstValue,
        c2: &ConstValue,
        ptr_size: PointerSize,
    ) -> Option<ConstValue> {
        match self {
            // Associative: combine with same operation
            OpKind::Add | OpKind::Sub | OpKind::Shl | OpKind::Shr { .. } => c1.add(c2, ptr_size),
            OpKind::Mul => c1.mul(c2, ptr_size),
            OpKind::And => c1.bitwise_and(c2, ptr_size),
            OpKind::Or => c1.bitwise_or(c2, ptr_size),
            OpKind::Xor => c1.bitwise_xor(c2, ptr_size),
        }
    }

    /// Returns a description of the operation.
    fn name(self) -> &'static str {
        match self {
            OpKind::Add => "add",
            OpKind::Sub => "sub",
            OpKind::Mul => "mul",
            OpKind::And => "and",
            OpKind::Or => "or",
            OpKind::Xor => "xor",
            OpKind::Shl => "shl",
            OpKind::Shr { unsigned: false } => "shr",
            OpKind::Shr { unsigned: true } => "shr.un",
        }
    }

    /// Returns the name of the combining operation for logging.
    fn combine_name(self) -> &'static str {
        match self {
            // Associative: combine with same operation
            OpKind::Add | OpKind::Mul | OpKind::And | OpKind::Or | OpKind::Xor => self.name(),
            // Non-associative: combine with addition
            OpKind::Sub | OpKind::Shl | OpKind::Shr { .. } => "add",
        }
    }

    /// Returns true if this operation is commutative.
    ///
    /// For non-commutative operations, the constant must be on the right side.
    const fn is_commutative(self) -> bool {
        match self {
            OpKind::Add | OpKind::Mul | OpKind::And | OpKind::Or | OpKind::Xor => true,
            OpKind::Sub | OpKind::Shl | OpKind::Shr { .. } => false,
        }
    }
}

impl ReassociationPass {
    /// Creates a new reassociation pass.
    #[must_use]
    pub fn new() -> Self {
        Self
    }

    /// Gets the OpKind if the operation can be reassociated.
    ///
    /// Returns (kind, dest, left_operand, right_operand).
    /// For shift operations, left is the value being shifted, right is the amount.
    fn get_op_kind(op: &SsaOp) -> Option<(OpKind, SsaVarId, SsaVarId, SsaVarId)> {
        match op {
            SsaOp::Add { dest, left, right } => Some((OpKind::Add, *dest, *left, *right)),
            SsaOp::Sub { dest, left, right } => Some((OpKind::Sub, *dest, *left, *right)),
            SsaOp::Mul { dest, left, right } => Some((OpKind::Mul, *dest, *left, *right)),
            SsaOp::And { dest, left, right } => Some((OpKind::And, *dest, *left, *right)),
            SsaOp::Or { dest, left, right } => Some((OpKind::Or, *dest, *left, *right)),
            SsaOp::Xor { dest, left, right } => Some((OpKind::Xor, *dest, *left, *right)),
            SsaOp::Shl {
                dest,
                value,
                amount,
            } => Some((OpKind::Shl, *dest, *value, *amount)),
            SsaOp::Shr {
                dest,
                value,
                amount,
                unsigned,
            } => Some((
                OpKind::Shr {
                    unsigned: *unsigned,
                },
                *dest,
                *value,
                *amount,
            )),
            _ => None,
        }
    }

    /// Creates a new operation with the given operands.
    ///
    /// For shift operations, `left` is the value being shifted and `right` is the amount.
    fn make_op(kind: OpKind, dest: SsaVarId, left: SsaVarId, right: SsaVarId) -> SsaOp {
        match kind {
            OpKind::Add => SsaOp::Add { dest, left, right },
            OpKind::Sub => SsaOp::Sub { dest, left, right },
            OpKind::Mul => SsaOp::Mul { dest, left, right },
            OpKind::And => SsaOp::And { dest, left, right },
            OpKind::Or => SsaOp::Or { dest, left, right },
            OpKind::Xor => SsaOp::Xor { dest, left, right },
            OpKind::Shl => SsaOp::Shl {
                dest,
                value: left,
                amount: right,
            },
            OpKind::Shr { unsigned } => SsaOp::Shr {
                dest,
                value: left,
                amount: right,
                unsigned,
            },
        }
    }

    /// Finds reassociation candidates.
    fn find_candidates(
        ssa: &SsaFunction,
        constants: &HashMap<SsaVarId, ConstValue>,
        index: &DefUseIndex,
        uses: &HashMap<SsaVarId, usize>,
    ) -> Vec<ReassociationCandidate> {
        let mut candidates = Vec::new();

        for (block_idx, instr_idx, instr) in ssa.iter_instructions() {
            if let Some(candidate) =
                Self::check_reassociation(instr.op(), block_idx, instr_idx, constants, index, uses)
            {
                candidates.push(candidate);
            }
        }

        candidates
    }

    /// Checks if an operation can be reassociated.
    fn check_reassociation(
        op: &SsaOp,
        block_idx: usize,
        instr_idx: usize,
        constants: &HashMap<SsaVarId, ConstValue>,
        index: &DefUseIndex,
        uses: &HashMap<SsaVarId, usize>,
    ) -> Option<ReassociationCandidate> {
        // Get the outer operation's kind and operands
        let (outer_kind, dest, outer_left, outer_right) = Self::get_op_kind(op)?;

        // Try: (inner_result op c2) where inner_result = (x op c1)
        // Check right operand is a constant
        let c2_value = constants.get(&outer_right)?;

        // Check left operand is the result of a same-kind operation
        // Use DefUseIndex to get (block, instruction, operation) in one call
        let (inner_block, inner_instr, inner_op) = index.full_definition(outer_left)?;
        let (inner_kind, inner_dest, inner_left, inner_right) = Self::get_op_kind(inner_op)?;

        // Must be the same operation kind
        if inner_kind != outer_kind {
            return None;
        }

        // The inner result should only be used once (by this outer operation)
        // Otherwise we'd create extra computation
        let inner_uses = uses.get(&inner_dest).copied().unwrap_or(0);
        if inner_uses > 1 {
            return None;
        }

        // Try to find a constant in the inner operation
        // Case 1: inner_right is a constant (works for all operations)
        // Pattern: (x op c1) op c2 → x op (c1 combine c2)
        if let Some(c1_value) = constants.get(&inner_right) {
            return Some(ReassociationCandidate {
                block_idx,
                instr_idx,
                dest,
                base_var: inner_left,
                const1_var: inner_right,
                const2_var: outer_right,
                const1_value: c1_value.clone(),
                const2_value: c2_value.clone(),
                inner_block,
                inner_instr,
                inner_dest,
                op_kind: outer_kind,
            });
        }

        // Case 2: inner_left is a constant (only for commutative ops)
        // Pattern: (c1 op x) op c2 → x op (c1 combine c2)
        // This doesn't work for sub/shl/shr: (c1 - x) - c2 ≠ x - (c1 + c2)
        if outer_kind.is_commutative() {
            if let Some(c1_value) = constants.get(&inner_left) {
                return Some(ReassociationCandidate {
                    block_idx,
                    instr_idx,
                    dest,
                    base_var: inner_right,
                    const1_var: inner_left,
                    const2_var: outer_right,
                    const1_value: c1_value.clone(),
                    const2_value: c2_value.clone(),
                    inner_block,
                    inner_instr,
                    inner_dest,
                    op_kind: outer_kind,
                });
            }
        }

        None
    }

    /// Applies the reassociation transformations.
    fn apply_reassociations(
        ssa: &mut SsaFunction,
        candidates: Vec<ReassociationCandidate>,
        method_token: Token,
        changes: &mut EventLog,
        ptr_size: PointerSize,
    ) {
        for candidate in candidates {
            // Combine the constants
            let Some(combined) = candidate.op_kind.combine(
                &candidate.const1_value,
                &candidate.const2_value,
                ptr_size,
            ) else {
                continue;
            };

            // Update the first constant definition to the combined value
            if let Some(block) = ssa.block_mut(candidate.inner_block) {
                // Find and update the const1 definition
                for instr in block.instructions_mut() {
                    if let SsaOp::Const { dest, value: _ } = instr.op() {
                        if *dest == candidate.const1_var {
                            instr.set_op(SsaOp::Const {
                                dest: *dest,
                                value: combined.clone(),
                            });
                            break;
                        }
                    }
                }

                // Update the inner operation to just use base_var and the combined constant
                let inner_instr = &mut block.instructions_mut()[candidate.inner_instr];
                inner_instr.set_op(Self::make_op(
                    candidate.op_kind,
                    candidate.inner_dest,
                    candidate.base_var,
                    candidate.const1_var,
                ));
            }

            // Replace the outer operation with a Copy from the inner result
            if let Some(block) = ssa.block_mut(candidate.block_idx) {
                let outer_instr = &mut block.instructions_mut()[candidate.instr_idx];
                outer_instr.set_op(SsaOp::Copy {
                    dest: candidate.dest,
                    src: candidate.inner_dest,
                });
            }

            changes
                .record(EventKind::ConstantFolded)
                .at(method_token, candidate.instr_idx)
                .message(format!(
                    "reassociate: (x {} c1) {} c2 → x {} (c1 {} c2)",
                    candidate.op_kind.name(),
                    candidate.op_kind.name(),
                    candidate.op_kind.name(),
                    candidate.op_kind.combine_name()
                ));
        }
    }
}

impl SsaPass for ReassociationPass {
    fn name(&self) -> &'static str {
        "reassociation"
    }

    fn description(&self) -> &'static str {
        "Reorder operations to enable constant folding (add, sub, mul, and, or, xor, shl, shr)"
    }

    fn run_on_method(
        &self,
        ssa: &mut SsaFunction,
        method_token: Token,
        ctx: &CompilerContext,
        assembly: &Arc<CilObject>,
    ) -> Result<bool> {
        let ptr_size = PointerSize::from_pe(assembly.file().pe().is_64bit);
        let mut changes = EventLog::new();

        // Gather information
        let constants = ssa.find_constants();
        let index = DefUseIndex::build_with_ops(ssa);
        let uses = ssa.count_uses();

        // Find and apply reassociations
        let candidates = Self::find_candidates(ssa, &constants, &index, &uses);
        Self::apply_reassociations(ssa, candidates, method_token, &mut changes, ptr_size);

        let changed = !changes.is_empty();
        if changed {
            ctx.events.merge(&changes);
        }
        Ok(changed)
    }
}

#[cfg(test)]
mod tests {
    use crate::{analysis::ConstValue, metadata::typesystem::PointerSize};

    use super::OpKind;

    #[test]
    fn test_op_kind_combine_add() {
        let c1 = ConstValue::I32(5);
        let c2 = ConstValue::I32(3);
        let result = OpKind::Add.combine(&c1, &c2, PointerSize::Bit64);
        assert_eq!(result, Some(ConstValue::I32(8)));
    }

    #[test]
    fn test_op_kind_combine_xor() {
        let c1 = ConstValue::I32(0xF0);
        let c2 = ConstValue::I32(0x0F);
        let result = OpKind::Xor.combine(&c1, &c2, PointerSize::Bit64);
        assert_eq!(result, Some(ConstValue::I32(0xFF)));
    }

    #[test]
    fn test_op_kind_combine_mul() {
        let c1 = ConstValue::I32(7);
        let c2 = ConstValue::I32(11);
        let result = OpKind::Mul.combine(&c1, &c2, PointerSize::Bit64);
        assert_eq!(result, Some(ConstValue::I32(77)));
    }

    #[test]
    fn test_op_kind_combine_and() {
        let c1 = ConstValue::I32(0xFF);
        let c2 = ConstValue::I32(0x0F);
        let result = OpKind::And.combine(&c1, &c2, PointerSize::Bit64);
        assert_eq!(result, Some(ConstValue::I32(0x0F)));
    }

    #[test]
    fn test_op_kind_combine_or() {
        let c1 = ConstValue::I32(0xF0);
        let c2 = ConstValue::I32(0x0F);
        let result = OpKind::Or.combine(&c1, &c2, PointerSize::Bit64);
        assert_eq!(result, Some(ConstValue::I32(0xFF)));
    }

    #[test]
    fn test_op_kind_combine_sub() {
        // (x - 5) - 3 → x - (5 + 3) → x - 8
        let c1 = ConstValue::I32(5);
        let c2 = ConstValue::I32(3);
        let result = OpKind::Sub.combine(&c1, &c2, PointerSize::Bit64);
        assert_eq!(result, Some(ConstValue::I32(8)));
    }

    #[test]
    fn test_op_kind_combine_shl() {
        // (x << 2) << 3 → x << (2 + 3) → x << 5
        let c1 = ConstValue::I32(2);
        let c2 = ConstValue::I32(3);
        let result = OpKind::Shl.combine(&c1, &c2, PointerSize::Bit64);
        assert_eq!(result, Some(ConstValue::I32(5)));
    }

    #[test]
    fn test_op_kind_combine_shr() {
        // (x >> 4) >> 2 → x >> (4 + 2) → x >> 6
        let c1 = ConstValue::I32(4);
        let c2 = ConstValue::I32(2);
        let result = OpKind::Shr { unsigned: false }.combine(&c1, &c2, PointerSize::Bit64);
        assert_eq!(result, Some(ConstValue::I32(6)));
    }

    #[test]
    fn test_op_kind_combine_shr_unsigned() {
        // (x >>> 4) >>> 2 → x >>> (4 + 2) → x >>> 6
        let c1 = ConstValue::I32(4);
        let c2 = ConstValue::I32(2);
        let result = OpKind::Shr { unsigned: true }.combine(&c1, &c2, PointerSize::Bit64);
        assert_eq!(result, Some(ConstValue::I32(6)));
    }

    #[test]
    fn test_op_kind_is_commutative() {
        assert!(OpKind::Add.is_commutative());
        assert!(OpKind::Mul.is_commutative());
        assert!(OpKind::And.is_commutative());
        assert!(OpKind::Or.is_commutative());
        assert!(OpKind::Xor.is_commutative());
        assert!(!OpKind::Sub.is_commutative());
        assert!(!OpKind::Shl.is_commutative());
        assert!(!OpKind::Shr { unsigned: false }.is_commutative());
        assert!(!OpKind::Shr { unsigned: true }.is_commutative());
    }

    #[test]
    fn test_op_kind_combine_name() {
        // Associative operations combine with themselves
        assert_eq!(OpKind::Add.combine_name(), "add");
        assert_eq!(OpKind::Mul.combine_name(), "mul");
        assert_eq!(OpKind::And.combine_name(), "and");
        assert_eq!(OpKind::Or.combine_name(), "or");
        assert_eq!(OpKind::Xor.combine_name(), "xor");
        // Non-associative operations combine with add
        assert_eq!(OpKind::Sub.combine_name(), "add");
        assert_eq!(OpKind::Shl.combine_name(), "add");
        assert_eq!(OpKind::Shr { unsigned: false }.combine_name(), "add");
        assert_eq!(OpKind::Shr { unsigned: true }.combine_name(), "add");
    }

    #[test]
    fn test_op_kind_name() {
        assert_eq!(OpKind::Add.name(), "add");
        assert_eq!(OpKind::Sub.name(), "sub");
        assert_eq!(OpKind::Mul.name(), "mul");
        assert_eq!(OpKind::And.name(), "and");
        assert_eq!(OpKind::Or.name(), "or");
        assert_eq!(OpKind::Xor.name(), "xor");
        assert_eq!(OpKind::Shl.name(), "shl");
        assert_eq!(OpKind::Shr { unsigned: false }.name(), "shr");
        assert_eq!(OpKind::Shr { unsigned: true }.name(), "shr.un");
    }
}