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
//! Dispatcher analysis for CFF patterns.
//!
//! The dispatcher is the central control point in flattened code that routes
//! execution to different case blocks based on the state variable value.
//!
//! # Dispatcher Types
//!
//! Different obfuscators use different dispatcher implementations:
//!
//! - **Switch-based**: Uses a `switch` instruction with optional state transform
//! - **If-else chain**: Uses cascading `if (state == X)` comparisons
//! - **Computed jump**: Uses indirect branching through a jump table
//!
//! This module provides representations for all these patterns and analysis
//! functions to classify them.

use crate::analysis::{SsaFunction, SsaOp, SsaVarId};

/// A detected CFF dispatcher with all its metadata.
///
/// This struct is the primary output of dispatcher detection and contains
/// all the information needed to unflatten the control flow.
#[derive(Debug, Clone)]
pub struct Dispatcher {
    /// Block index containing the dispatcher switch/branch.
    pub block: usize,
    /// The SSA variable used in the switch instruction.
    pub switch_var: SsaVarId,
    /// Case block targets (index = case value after transform).
    pub cases: Vec<usize>,
    /// Default/exit block.
    pub default: usize,
    /// State variable phi at the dispatcher (if identified).
    pub state_phi: Option<SsaVarId>,
    /// Transform applied to state before dispatch.
    pub transform: StateTransform,
    /// Detection confidence score (0.0 - 1.0).
    pub confidence: f64,
}

impl Dispatcher {
    /// Creates a new dispatcher with the given parameters.
    #[must_use]
    pub fn new(block: usize, switch_var: SsaVarId, cases: Vec<usize>, default: usize) -> Self {
        Self {
            block,
            switch_var,
            cases,
            default,
            state_phi: None,
            transform: StateTransform::Identity,
            confidence: 0.0,
        }
    }

    /// Sets the state phi variable
    ///
    /// # Arguments
    ///
    /// * `state_phi` - The phi node carrying the raw state value before encoding
    ///
    /// # Returns
    ///
    /// Self with the state phi set.
    #[must_use]
    pub fn with_state_phi(mut self, phi: SsaVarId) -> Self {
        self.state_phi = Some(phi);
        self
    }

    /// Sets the state transform.
    #[must_use]
    pub fn with_transform(mut self, transform: StateTransform) -> Self {
        self.transform = transform;
        self
    }

    /// Sets the confidence score
    ///
    /// # Arguments
    ///
    /// * `confidence` - Score from 0.0 to 1.0 indicating detection confidence
    ///
    /// # Returns
    ///
    /// Self with the confidence set.
    #[must_use]
    pub fn with_confidence(mut self, confidence: f64) -> Self {
        self.confidence = confidence;
        self
    }

    /// Returns the number of case blocks.
    #[must_use]
    pub fn case_count(&self) -> usize {
        self.cases.len()
    }

    /// Returns all target blocks (cases + default).
    #[must_use]
    pub fn all_targets(&self) -> Vec<usize> {
        let mut targets = self.cases.clone();
        if !targets.contains(&self.default) {
            targets.push(self.default);
        }
        targets
    }

    /// Returns the target block for a given state value.
    #[must_use]
    pub fn target_for_state(&self, state: i32) -> usize {
        // Cast to usize for indexing - transform result is always non-negative after modulo/and operations
        #[allow(clippy::cast_sign_loss)]
        let index = self.transform.apply(state) as usize;
        if index < self.cases.len() {
            self.cases[index]
        } else {
            self.default
        }
    }

    /// Converts to DispatcherInfo for compatibility.
    #[must_use]
    pub fn to_info(&self) -> DispatcherInfo {
        DispatcherInfo::Switch {
            block: self.block,
            switch_var: self.switch_var,
            cases: self.cases.clone(),
            default: self.default,
            transform: self.transform.clone(),
        }
    }
}

/// Transformation applied to state before dispatch.
///
/// Many obfuscators transform the state value before using it to select
/// a case. Common transforms include:
///
/// - `(state ^ key) % N` (XOR then modulo) - ConfuserEx pattern
/// - `state % N` (modulo) - Reduces state space to N cases
/// - `state & mask` (bitwise AND) - Masks to specific bits
/// - `state >> shift` (right shift) - Extracts upper bits
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum StateTransform {
    /// No transformation - state used directly.
    #[default]
    Identity,

    /// Modulo operation: `state % divisor`.
    Modulo(u32),

    /// XOR followed by modulo: `(state ^ xor_key) % divisor`.
    ///
    /// This is the ConfuserEx pattern. The state value is XOR'd with a
    /// constant key before the modulo operation.
    XorModulo {
        /// The XOR key applied before modulo.
        xor_key: i32,
        /// The modulo divisor.
        divisor: u32,
    },

    /// Bitwise AND: `state & mask`.
    And(u32),

    /// Right shift: `state >> amount`.
    Shr(u32),
}

impl StateTransform {
    /// Applies this transform to a state value.
    #[must_use]
    pub fn apply(&self, state: i32) -> i32 {
        match self {
            Self::Identity => state,
            Self::Modulo(n) => {
                // Use unsigned modulo for consistency with CIL rem.un
                let u_state = state.cast_unsigned();
                (u_state % n).cast_signed()
            }
            Self::XorModulo { xor_key, divisor } => {
                // ConfuserEx pattern: (state ^ key) % N
                let xored = state ^ xor_key;
                let u_xored = xored.cast_unsigned();
                (u_xored % divisor).cast_signed()
            }
            Self::And(mask) => state & (*mask).cast_signed(),
            Self::Shr(amount) => {
                // Logical right shift (treat as unsigned)
                let u_state = state.cast_unsigned();
                (u_state >> amount).cast_signed()
            }
        }
    }

    /// Returns the divisor for modulo transforms (including XorModulo).
    #[must_use]
    pub fn modulo_divisor(&self) -> Option<u32> {
        match self {
            Self::Modulo(n) => Some(*n),
            Self::XorModulo { divisor, .. } => Some(*divisor),
            _ => None,
        }
    }

    /// Returns the XOR key if this is an XorModulo transform.
    #[must_use]
    pub fn xor_key(&self) -> Option<i32> {
        match self {
            Self::XorModulo { xor_key, .. } => Some(*xor_key),
            _ => None,
        }
    }

    /// Returns whether this is the identity transform (no-op).
    #[must_use]
    pub fn is_identity(&self) -> bool {
        matches!(self, Self::Identity)
    }

    /// Returns whether this is an XorModulo transform (ConfuserEx pattern).
    #[must_use]
    pub fn is_xor_modulo(&self) -> bool {
        matches!(self, Self::XorModulo { .. })
    }
}

/// Information about a detected dispatcher.
///
/// This struct captures all the details needed to understand how the
/// dispatcher routes control flow based on state values.
#[derive(Debug, Clone)]
pub enum DispatcherInfo {
    /// Switch-based dispatcher.
    ///
    /// The most common pattern, using a CIL `switch` instruction:
    /// ```text
    /// ldloc state
    /// ldc.i4 N
    /// rem.un           ; optional transform
    /// switch (case0, case1, ..., caseN-1)
    /// br default       ; fallthrough to default
    /// ```
    Switch {
        /// Block containing the switch instruction.
        block: usize,
        /// Variable used in the switch (after any transform).
        switch_var: SsaVarId,
        /// Case targets indexed by case value.
        /// `cases[i]` is the target block for case value `i`.
        cases: Vec<usize>,
        /// Default target (when value is out of range).
        default: usize,
        /// Transform applied to state before switch.
        transform: StateTransform,
    },

    /// If-else chain dispatcher.
    ///
    /// Used by some obfuscators to avoid obvious switch patterns:
    /// ```text
    /// if (state == X) goto blockX
    /// if (state == Y) goto blockY
    /// ...
    /// goto default
    /// ```
    IfElseChain {
        /// Head block (first comparison).
        head_block: usize,
        /// State variable being compared.
        state_var: SsaVarId,
        /// Comparisons in order: (compare_value, target_block).
        comparisons: Vec<(i32, usize)>,
        /// Default target when no comparison matches.
        default: Option<usize>,
    },

    /// Computed jump dispatcher (indirect branching).
    ///
    /// Some obfuscators use indirect jumps through a computed address:
    /// ```text
    /// ldloc state
    /// ; compute jump target address
    /// calli target  ; or similar indirect jump
    /// ```
    ///
    /// This is the hardest pattern to analyze as targets may not be
    /// statically determinable.
    ComputedJump {
        /// Block containing the indirect jump.
        block: usize,
        /// Variable holding the computed jump target.
        target_var: SsaVarId,
        /// Known jump table entries (if resolvable).
        /// Maps state value to target block.
        jump_table: Vec<usize>,
        /// Base address used in jump computation (if known).
        base_address: Option<u64>,
    },
}

impl DispatcherInfo {
    /// Returns the dispatcher block.
    #[must_use]
    pub fn block(&self) -> usize {
        match self {
            Self::Switch { block, .. } | Self::ComputedJump { block, .. } => *block,
            Self::IfElseChain { head_block, .. } => *head_block,
        }
    }

    /// Returns the number of cases (excluding default).
    #[must_use]
    pub fn case_count(&self) -> usize {
        match self {
            Self::Switch { cases, .. } => cases.len(),
            Self::IfElseChain { comparisons, .. } => comparisons.len(),
            Self::ComputedJump { jump_table, .. } => jump_table.len(),
        }
    }

    /// Returns the target block for a given case value.
    ///
    /// For switch dispatchers, this applies the transform and indexes into cases.
    /// For if-else chains, this searches the comparison list.
    /// For computed jumps, this indexes directly into the jump table.
    #[must_use]
    pub fn target_for_case(&self, case_value: i32) -> Option<usize> {
        match self {
            Self::Switch {
                cases,
                default,
                transform,
                ..
            } => {
                // Cast to usize for indexing - transform result is always non-negative after modulo/and operations
                #[allow(clippy::cast_sign_loss)]
                let index = transform.apply(case_value) as usize;
                if index < cases.len() {
                    Some(cases[index])
                } else {
                    Some(*default)
                }
            }
            Self::IfElseChain {
                comparisons,
                default,
                ..
            } => {
                for (cmp_val, target) in comparisons {
                    if *cmp_val == case_value {
                        return Some(*target);
                    }
                }
                *default
            }
            Self::ComputedJump { jump_table, .. } => {
                // Cast to usize for indexing - case_value is validated to be non-negative by caller
                #[allow(clippy::cast_sign_loss)]
                let index = case_value as usize;
                jump_table.get(index).copied()
            }
        }
    }

    /// Returns all possible target blocks.
    #[must_use]
    pub fn all_targets(&self) -> Vec<usize> {
        match self {
            Self::Switch { cases, default, .. } => {
                let mut targets: Vec<usize> = cases.clone();
                if !targets.contains(default) {
                    targets.push(*default);
                }
                targets
            }
            Self::IfElseChain {
                comparisons,
                default,
                ..
            } => {
                let mut targets: Vec<usize> = comparisons.iter().map(|(_, t)| *t).collect();
                if let Some(def) = default {
                    if !targets.contains(def) {
                        targets.push(*def);
                    }
                }
                targets
            }
            Self::ComputedJump { jump_table, .. } => jump_table.clone(),
        }
    }

    /// Returns the state transform applied before dispatch.
    #[must_use]
    pub fn transform(&self) -> StateTransform {
        match self {
            Self::Switch { transform, .. } => transform.clone(),
            Self::IfElseChain { .. } | Self::ComputedJump { .. } => StateTransform::Identity,
        }
    }

    /// Returns the variable used for dispatch decisions.
    #[must_use]
    pub fn dispatch_var(&self) -> SsaVarId {
        match self {
            Self::Switch { switch_var, .. } => *switch_var,
            Self::IfElseChain { state_var, .. } => *state_var,
            Self::ComputedJump { target_var, .. } => *target_var,
        }
    }

    /// Returns true if this is a computed jump dispatcher.
    #[must_use]
    pub fn is_computed_jump(&self) -> bool {
        matches!(self, Self::ComputedJump { .. })
    }

    /// Returns the base address for computed jump dispatchers.
    #[must_use]
    pub fn base_address(&self) -> Option<u64> {
        match self {
            Self::ComputedJump { base_address, .. } => *base_address,
            _ => None,
        }
    }
}

/// Analyzes a block to determine if it's a switch-based dispatcher.
///
/// Looks for the pattern:
/// ```text
/// [optional: ldloc state; ldc.i4 N; rem.un -> transformed_var]
/// switch(var) -> targets, default
/// ```
///
/// # Arguments
///
/// * `ssa` - The SSA function containing the block
/// * `block_idx` - Index of the block to analyze
///
/// # Returns
///
/// `Some(DispatcherInfo::Switch { .. })` if the block contains a switch-based
/// dispatcher pattern, `None` otherwise.
pub fn analyze_switch_dispatcher(ssa: &SsaFunction, block_idx: usize) -> Option<DispatcherInfo> {
    let block = ssa.block(block_idx)?;

    // Find the switch instruction (terminator)
    let switch_instr = block
        .instructions()
        .iter()
        .rev()
        .find(|i| matches!(i.op(), SsaOp::Switch { .. }))?;

    let (switch_var, targets, default) = match switch_instr.op() {
        SsaOp::Switch {
            value,
            targets,
            default,
        } => (*value, targets.clone(), *default),
        _ => return None,
    };

    // Determine if there's a transform applied to the switch variable
    let transform = analyze_switch_transform(ssa, switch_var);

    Some(DispatcherInfo::Switch {
        block: block_idx,
        switch_var,
        cases: targets,
        default,
        transform,
    })
}

/// Analyzes the definition of a switch variable to detect transforms.
fn analyze_switch_transform(ssa: &SsaFunction, switch_var: SsaVarId) -> StateTransform {
    let Some(def) = ssa.get_definition(switch_var) else {
        return StateTransform::Identity;
    };

    match def {
        // Remainder (modulo): state % N or (state ^ key) % N
        SsaOp::Rem {
            left,
            right,
            unsigned: true,
            ..
        } => {
            let Some(SsaOp::Const { value, .. }) = ssa.get_definition(*right) else {
                return StateTransform::Identity;
            };
            let Some(divisor) = value.as_i32() else {
                return StateTransform::Identity;
            };

            // Check if left operand is the result of an XOR operation (ConfuserEx pattern)
            // The pattern is: switch_var = (state ^ XOR_KEY) % N
            if let Some(xor_key) = find_xor_key(ssa, *left) {
                return StateTransform::XorModulo {
                    xor_key,
                    divisor: divisor.cast_unsigned(),
                };
            }

            StateTransform::Modulo(divisor.cast_unsigned())
        }

        // Bitwise AND: state & mask
        SsaOp::And { right, .. } => {
            if let Some(SsaOp::Const { value, .. }) = ssa.get_definition(*right) {
                if let Some(mask) = value.as_i32() {
                    return StateTransform::And(mask.cast_unsigned());
                }
            }
            StateTransform::Identity
        }

        // Right shift: state >> amount
        SsaOp::Shr { amount, .. } => {
            if let Some(SsaOp::Const { value, .. }) = ssa.get_definition(*amount) {
                if let Some(shift) = value.as_i32() {
                    return StateTransform::Shr(shift.cast_unsigned());
                }
            }
            StateTransform::Identity
        }

        _ => StateTransform::Identity,
    }
}

/// Finds the XOR key constant if the variable is defined by an XOR operation.
///
/// The ConfuserEx pattern is: `(state ^ XOR_KEY) % N`
/// We're looking for the XOR_KEY constant in the XOR operation.
fn find_xor_key(ssa: &SsaFunction, var: SsaVarId) -> Option<i32> {
    let def = ssa.get_definition(var)?;

    match def {
        // Direct XOR: var = left ^ right
        SsaOp::Xor { left, right, .. } => {
            // Check if right operand is a constant
            if let Some(SsaOp::Const { value, .. }) = ssa.get_definition(*right) {
                if let Some(key) = value.as_i32() {
                    return Some(key);
                }
            }
            // Check if left operand is a constant (XOR is commutative)
            if let Some(SsaOp::Const { value, .. }) = ssa.get_definition(*left) {
                if let Some(key) = value.as_i32() {
                    return Some(key);
                }
            }
            None
        }

        // Copy operation: trace through
        SsaOp::Copy { src, .. } => find_xor_key(ssa, *src),

        // DUP is often used after XOR - the source might be the XOR result
        // In SSA, dup becomes a copy, so this is handled above
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        analysis::SsaVarId,
        deobfuscation::passes::unflattening::dispatcher::{DispatcherInfo, StateTransform},
    };

    #[test]
    fn test_state_transform_identity() {
        let transform = StateTransform::Identity;
        assert_eq!(transform.apply(42), 42);
        assert_eq!(transform.apply(-5), -5);
        assert!(transform.is_identity());
    }

    #[test]
    fn test_state_transform_modulo() {
        let transform = StateTransform::Modulo(7);
        assert_eq!(transform.apply(10), 3);
        assert_eq!(transform.apply(7), 0);
        assert_eq!(transform.apply(0), 0);
        assert_eq!(transform.modulo_divisor(), Some(7));
        assert!(!transform.is_identity());
    }

    #[test]
    fn test_state_transform_and() {
        let transform = StateTransform::And(0xFF);
        assert_eq!(transform.apply(0x12345678_u32 as i32), 0x78);
        assert_eq!(transform.apply(255), 255);
    }

    #[test]
    fn test_state_transform_shr() {
        let transform = StateTransform::Shr(8);
        assert_eq!(transform.apply(0x1234), 0x12);
    }

    #[test]
    fn test_state_transform_xor_modulo() {
        // ConfuserEx pattern: (state ^ xor_key) % divisor
        // Test with xor_key = -576502913 (0xDDA3001F), divisor = 7
        let transform = StateTransform::XorModulo {
            xor_key: -576502913_i32,
            divisor: 7,
        };

        // Verify it's not identity
        assert!(!transform.is_identity());
        assert!(transform.is_xor_modulo());
        assert_eq!(transform.modulo_divisor(), Some(7));
        assert_eq!(transform.xor_key(), Some(-576502913_i32));

        // Test with a sample state value
        // state = -781784372 (0xD18CAF8C)
        // xored = -781784372 ^ -576502913 = some value
        // result = (xored as u32) % 7
        let state = -781784372_i32;
        let xored = state ^ -576502913_i32;
        let expected = ((xored as u32) % 7) as i32;
        assert_eq!(transform.apply(state), expected);
    }

    #[test]
    fn test_dispatcher_info_switch() {
        let dispatcher = DispatcherInfo::Switch {
            block: 0,
            switch_var: SsaVarId::new(),
            cases: vec![1, 2, 3, 4, 5],
            default: 6,
            transform: StateTransform::Modulo(5),
        };

        assert_eq!(dispatcher.block(), 0);
        assert_eq!(dispatcher.case_count(), 5);

        // Test case routing with modulo transform
        assert_eq!(dispatcher.target_for_case(0), Some(1)); // 0 % 5 = 0 -> cases[0]
        assert_eq!(dispatcher.target_for_case(1), Some(2)); // 1 % 5 = 1 -> cases[1]
        assert_eq!(dispatcher.target_for_case(5), Some(1)); // 5 % 5 = 0 -> cases[0]
        assert_eq!(dispatcher.target_for_case(7), Some(3)); // 7 % 5 = 2 -> cases[2]
    }

    #[test]
    fn test_dispatcher_info_switch_with_xor_modulo() {
        // Test with ConfuserEx-style XorModulo transform
        // XOR key = -576502913, divisor = 7
        let dispatcher = DispatcherInfo::Switch {
            block: 1,
            switch_var: SsaVarId::new(),
            cases: vec![2, 3, 4, 5, 6, 7, 8], // 7 cases (0-6)
            default: 9,
            transform: StateTransform::XorModulo {
                xor_key: -576502913_i32,
                divisor: 7,
            },
        };

        assert_eq!(dispatcher.case_count(), 7);

        // Test case routing with XorModulo transform
        // state = -781784372 should XOR with key, then modulo 7
        let state = -781784372_i32;
        let xored = state ^ -576502913_i32;
        let expected_case_idx = ((xored as u32) % 7) as usize;
        assert_eq!(
            dispatcher.target_for_case(state),
            Some(dispatcher.all_targets()[expected_case_idx])
        );
    }

    #[test]
    fn test_dispatcher_info_if_else() {
        let dispatcher = DispatcherInfo::IfElseChain {
            head_block: 0,
            state_var: SsaVarId::new(),
            comparisons: vec![(10, 1), (20, 2), (30, 3)],
            default: Some(4),
        };

        assert_eq!(dispatcher.block(), 0);
        assert_eq!(dispatcher.case_count(), 3);
        assert_eq!(dispatcher.target_for_case(10), Some(1));
        assert_eq!(dispatcher.target_for_case(20), Some(2));
        assert_eq!(dispatcher.target_for_case(99), Some(4)); // Not found -> default
    }

    #[test]
    fn test_dispatcher_all_targets() {
        let dispatcher = DispatcherInfo::Switch {
            block: 0,
            switch_var: SsaVarId::new(),
            cases: vec![1, 2, 2, 3], // Note: 2 appears twice
            default: 4,
            transform: StateTransform::Identity,
        };

        let targets = dispatcher.all_targets();
        assert!(targets.contains(&1));
        assert!(targets.contains(&2));
        assert!(targets.contains(&3));
        assert!(targets.contains(&4));
    }

    #[test]
    fn test_dispatcher_info_computed_jump() {
        let dispatcher = DispatcherInfo::ComputedJump {
            block: 5,
            target_var: SsaVarId::new(),
            jump_table: vec![10, 20, 30, 40],
            base_address: Some(0x1000),
        };

        assert_eq!(dispatcher.block(), 5);
        assert_eq!(dispatcher.case_count(), 4);
        assert!(dispatcher.is_computed_jump());
        assert_eq!(dispatcher.base_address(), Some(0x1000));

        // Test case routing via jump table
        assert_eq!(dispatcher.target_for_case(0), Some(10));
        assert_eq!(dispatcher.target_for_case(1), Some(20));
        assert_eq!(dispatcher.target_for_case(3), Some(40));
        assert_eq!(dispatcher.target_for_case(4), None); // Out of range

        let targets = dispatcher.all_targets();
        assert_eq!(targets, vec![10, 20, 30, 40]);
    }
}