rsleigh 0.4.2

SLEIGH (.slaspec) parser and Rust decoder/P-code emitter codegen — Ghidra-compatible disassembly in pure Rust
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
use std::ops::Range;

use crate::execution::{Binary, DynamicValueType, VariableId};
use crate::semantic::execution::{
    Assignment as FinalAssignment, AssignmentOp as FinalAssignmentOp,
    AssignmentWrite as FinalAssignmentType, AssignmentWriteVariable as FinalAssignmentValueWrite,
};
use crate::semantic::inner::execution::TableExportType;
use crate::semantic::inner::{Sleigh, SolverStatus};
use crate::{
    AttachVarnodeId, BitrangeId, ExecutionError, NumberNonZeroUnsigned, NumberUnsigned, Span,
    TableId, VarSizeError, VarnodeId,
};

use super::{
    len, Execution, Expr, ExprBinaryOp, ExprElement, ExprNumber, ExprUnaryOp, ExprValue, FieldSize,
    FieldSizeMut, FieldSizeTableExport, FieldSizeUnmutable, MemoryLocation, Unary,
};

#[derive(Clone, Debug)]
pub struct Assignment {
    pub location: Span,
    pub var_location: Span,
    pub var: AssignmentWrite,
    pub right: Expr,
}

#[derive(Clone, Debug)]
pub enum AssignmentWrite {
    Variable {
        value: AssignmentWriteVariable,
        op: Option<AssignmentOp>,
    },
    Memory {
        mem: MemoryLocation,
        addr: Expr,
    },
    // write to memory based on the table export
    TableExport {
        table_id: TableId,
        op: Option<AssignmentOp>,
    },
    // table export, if a reference, the size is just a sugestion,
    // each write could have a diferent size.
    // should only be created if the write have diferent size from the export
    TableReferenceExport {
        table_id: TableId,
        size: FieldSize,
    },
}
impl AssignmentWrite {
    fn convert(self) -> FinalAssignmentType {
        match self {
            AssignmentWrite::Variable { value, op } => FinalAssignmentType::Variable {
                value: value.convert(),
                op: op.map(AssignmentOp::convert),
            },
            AssignmentWrite::Memory { mem, addr } => FinalAssignmentType::Memory {
                mem: mem.convert(),
                addr: addr.convert(),
            },
            AssignmentWrite::TableExport { table_id, op } => FinalAssignmentType::TableExport {
                table_id,
                op: op.map(AssignmentOp::convert),
                size: None,
            },
            AssignmentWrite::TableReferenceExport { table_id, size } => {
                FinalAssignmentType::TableExport {
                    table_id,
                    op: None,
                    size: size.possible_value(),
                }
            }
        }
    }
}

#[derive(Copy, Clone, Debug)]
pub enum AssignmentWriteVariable {
    Varnode(VarnodeId),
    Bitrange(BitrangeId),
    DynVarnode {
        value_id: DynamicValueType,
        attach_id: AttachVarnodeId,
    },
    Local {
        id: VariableId,
        creation: bool,
    },
}
impl AssignmentWriteVariable {
    fn convert(self) -> FinalAssignmentValueWrite {
        match self {
            AssignmentWriteVariable::Varnode(var) => FinalAssignmentValueWrite::Varnode(var),
            AssignmentWriteVariable::Bitrange(bit) => FinalAssignmentValueWrite::Bitrange(bit),
            AssignmentWriteVariable::DynVarnode {
                value_id,
                attach_id,
            } => FinalAssignmentValueWrite::DynVarnode {
                value_id,
                attach_id,
            },
            AssignmentWriteVariable::Local { id, creation: _ } => {
                FinalAssignmentValueWrite::Variable(id)
            }
        }
    }
}

impl Assignment {
    pub fn new(var_location: Span, var: AssignmentWrite, location: Span, right: Expr) -> Self {
        // TODO table export can't be writen if is value or const
        Self {
            var_location,
            var,
            location,
            right,
        }
    }
    pub fn solve(
        &mut self,
        sleigh: &Sleigh,
        execution: &Execution,
        solved: &mut impl SolverStatus,
    ) -> Result<(), Box<ExecutionError>> {
        self.right.solve(sleigh, execution, solved)?;

        // change the table mem write size
        if hack_solve_table_reference_export_size(self, sleigh, execution) {
            solved.i_did_a_thing();
        }

        // solve simple expr that don't follow many rules
        if hack_solve_simple_bin_ands(self, sleigh, execution)? {
            solved.i_did_a_thing();
        }

        // add auto truncate in simple bitand expr
        if hack_auto_fix_bitrange_with_bitand(self, sleigh, execution) {
            solved.i_did_a_thing();
        }

        // add extra information for the variable creation
        if hack_extra_var_info_creation(self, sleigh, execution) {
            solved.i_did_a_thing();
        }

        // identify the implicity truncation for varnodes
        if hack_auto_zext_right_side(self, sleigh, execution) {
            solved.i_did_a_thing();
        }

        // identify the implicity truncation for left size if one byte varnode
        if hack_1_byte_varnode_assign_to_bit(self, sleigh, execution) {
            solved.i_did_a_thing();
        }

        // add auto truncate in mem deref
        if hack_auto_trunkate_right_side(self, sleigh, execution) {
            solved.i_did_a_thing();
        }

        if hack_force_solve_usercall(self, sleigh, execution)? {
            solved.i_did_a_thing();
        }

        // TODO check left size can be truncated correctly

        // left and right sizes are the same
        let modified = {
            let (mut left, mut right) = self.left_and_right_size_mut(sleigh, execution);
            len::a_receive_b(&mut *left, &mut *right)
        };
        if modified.ok_or_else(|| VarSizeError::AssignmentSides {
            left: self.left_size(sleigh, execution),
            right: self.right.size(sleigh, execution),
            location: self.location.clone(),
            backtrace: format!("{}:{}", file!(), line!()),
        })? {
            solved.i_did_a_thing()
        }

        match &mut self.var {
            AssignmentWrite::Variable { value, op } => {
                if let Some(op) = op {
                    if op.output_size().is_undefined() {
                        solved.iam_not_finished(&self.var_location, file!(), line!())
                    }
                }
                match value {
                    AssignmentWriteVariable::Local { id, creation: _ } => {
                        let var = execution.variable(*id);
                        if var.size.get().is_undefined() {
                            solved.iam_not_finished(&self.var_location, file!(), line!())
                        }
                    }
                    AssignmentWriteVariable::Varnode(_)
                    | AssignmentWriteVariable::Bitrange(_)
                    | AssignmentWriteVariable::DynVarnode { .. } => {}
                }
            }
            AssignmentWrite::Memory { mem, addr } => {
                if mem.size.is_undefined() {
                    solved.iam_not_finished(&self.var_location, file!(), line!())
                }
                addr.solve(sleigh, execution, solved)?;
            }
            AssignmentWrite::TableExport { table_id: _, op } => {
                if let Some(op) = op {
                    if op.output_size().is_undefined() {
                        solved.iam_not_finished(&self.var_location, file!(), line!())
                    }
                }
            }
            AssignmentWrite::TableReferenceExport { table_id: _, size } => {
                if size.is_undefined() {
                    solved.iam_not_finished(&self.var_location, file!(), line!())
                }
            }
        }

        Ok(())
    }

    pub fn left_size(&self, sleigh: &Sleigh, execution: &Execution) -> FieldSize {
        match &self.var {
            AssignmentWrite::TableExport { table_id, op: None } => {
                let table = sleigh.table(*table_id);
                let table_export = *table.export.borrow();
                *table_export.unwrap().size().unwrap()
            }
            AssignmentWrite::Memory { mem, .. } => mem.size,
            AssignmentWrite::Variable { op: Some(op), .. }
            | AssignmentWrite::TableExport { op: Some(op), .. } => op.output_size(),
            AssignmentWrite::Variable {
                op: None,
                value: AssignmentWriteVariable::Varnode(var),
            } => FieldSize::new_bytes(sleigh.varnode(*var).len_bytes),
            AssignmentWrite::Variable {
                op: None,
                value: AssignmentWriteVariable::Bitrange(bit),
            } => FieldSize::new_bits(sleigh.bitrange(*bit).bits.len()),
            AssignmentWrite::Variable {
                op: None,
                value: AssignmentWriteVariable::DynVarnode { attach_id, .. },
            } => FieldSize::new_bytes(sleigh.attach_varnodes_len_bytes(*attach_id)),
            AssignmentWrite::Variable {
                op: None,
                value: AssignmentWriteVariable::Local { id, creation: _ },
            } => execution.variable(*id).size.get(),
            AssignmentWrite::TableReferenceExport { size, .. } => *size,
        }
    }

    pub fn left_and_right_size_mut<'a>(
        &'a mut self,
        sleigh: &'a Sleigh,
        execution: &'a Execution,
    ) -> (Box<dyn FieldSizeMut + 'a>, Box<dyn FieldSizeMut + 'a>) {
        let left = match &mut self.var {
            AssignmentWrite::TableReferenceExport { size, .. } => Box::new(size),
            AssignmentWrite::Variable { op: Some(op), .. }
            | AssignmentWrite::TableExport { op: Some(op), .. } => op.output_size_mut(),
            AssignmentWrite::Memory { mem, .. } => Box::new(&mut mem.size),
            AssignmentWrite::Variable {
                op: None,
                value: AssignmentWriteVariable::Varnode(var),
            } => Box::new(FieldSizeUnmutable(FieldSize::new_bytes(
                sleigh.varnode(*var).len_bytes,
            ))),
            AssignmentWrite::Variable {
                op: None,
                value: AssignmentWriteVariable::Bitrange(bit),
            } => Box::new(FieldSizeUnmutable(FieldSize::new_bits(
                sleigh.bitrange(*bit).bits.len(),
            ))),
            AssignmentWrite::Variable {
                op: None,
                value: AssignmentWriteVariable::DynVarnode { attach_id, .. },
            } => Box::new(FieldSizeUnmutable(FieldSize::new_bytes(
                sleigh.attach_varnodes_len_bytes(*attach_id),
            ))),
            AssignmentWrite::Variable {
                op: None,
                value: AssignmentWriteVariable::Local { id, creation: _ },
            } => Box::new(&execution.variable(*id).size),
            AssignmentWrite::TableExport { table_id, op: None } => {
                let table = sleigh.table(*table_id);
                Box::new(FieldSizeTableExport(&table.export))
            }
        };
        let right = self.right.size_mut(sleigh, execution);
        (left, right)
    }

    pub fn convert(self) -> FinalAssignment {
        FinalAssignment {
            location: self.location,
            var: self.var.convert(),
            right: self.right.convert(),
        }
    }

    fn swap_right(&mut self, mut new_right: impl FnMut(Expr) -> Expr) {
        // dummy value for temporary use
        let mut swap_right = Expr::Value(ExprElement::Value {
            location: Span::File(crate::FileSpan {
                start: crate::FileLocation {
                    file: std::rc::Rc::from(std::path::Path::new("")),
                    line: 0,
                    column: 0,
                },
                end_line: 0,
                end_column: 0,
            }),
            value: ExprValue::Int(ExprNumber {
                size: FieldSize::default(),
                number: crate::Number::Positive(0),
            }),
        });
        core::mem::swap(&mut self.right, &mut swap_right);
        self.right = new_right(swap_right);
    }
}

#[derive(Clone, Debug)]
pub struct MacroParamAssignment {
    pub var: VariableId,
    pub right: Expr,
}

impl MacroParamAssignment {
    pub fn new(var: VariableId, right: Expr) -> Self {
        Self { var, right }
    }
    pub fn solve(
        &mut self,
        sleigh: &Sleigh,
        execution: &Execution,
        solved: &mut impl SolverStatus,
    ) -> Result<(), Box<ExecutionError>> {
        self.right.solve(sleigh, execution, solved)?;

        let var = execution.variable(self.var);
        let result =
            len::a_equivalent_b(&mut &var.size, &mut *self.right.size_mut(sleigh, execution));
        let did_something = result.ok_or_else(|| VarSizeError::AssignmentSides {
            left: var.size.get(),
            right: self.right.size(sleigh, execution),
            location: self.right.src().clone(),
            backtrace: format!("{}:{}", file!(), line!()),
        })?;

        if did_something {
            solved.i_did_a_thing();
        }

        Ok(())
    }

    pub fn convert(self) -> FinalAssignment {
        FinalAssignment {
            location: self.right.src().clone(),
            var: FinalAssignmentType::Variable {
                value: FinalAssignmentValueWrite::Variable(self.var),
                op: None,
            },
            right: self.right.convert(),
        }
    }
}

#[derive(Clone, Debug)]
pub enum AssignmentOp {
    TakeLsb(NumberNonZeroUnsigned),
    TrunkLsb {
        bytes: NumberUnsigned,
        output_size: FieldSize,
    },
    BitRange(Range<NumberUnsigned>),
}

impl AssignmentOp {
    pub fn output_size_mut(&mut self) -> Box<dyn FieldSizeMut + '_> {
        match self {
            AssignmentOp::TakeLsb(bytes) => {
                Box::new(len::FieldSizeUnmutable(FieldSize::new_bytes(*bytes)))
            }
            AssignmentOp::TrunkLsb {
                bytes: _,
                output_size,
            } => Box::new(output_size),
            AssignmentOp::BitRange(bits) => Box::new(len::FieldSizeUnmutable(FieldSize::new_bits(
                (bits.end - bits.start).try_into().unwrap(),
            ))),
        }
    }
    pub fn output_size(&self) -> FieldSize {
        match self {
            AssignmentOp::TakeLsb(bytes) => FieldSize::new_bytes(*bytes),
            AssignmentOp::TrunkLsb {
                bytes: _,
                output_size,
            } => *output_size,
            AssignmentOp::BitRange(bits) => {
                FieldSize::new_bits((bits.end - bits.start).try_into().unwrap())
            }
        }
    }
    pub fn convert(self) -> FinalAssignmentOp {
        match self {
            AssignmentOp::TakeLsb(x) => FinalAssignmentOp::TakeLsb(x),
            AssignmentOp::TrunkLsb {
                bytes,
                output_size: _,
            } => FinalAssignmentOp::TrunkLsb(bytes),
            AssignmentOp::BitRange(x) => FinalAssignmentOp::BitRange(x),
        }
    }
}

// HACK solve table export reference size
// eg: Mem = temp:1; # Mem is a ref to 4 bytes in Ram
fn hack_solve_table_reference_export_size(
    ass: &mut Assignment,
    sleigh: &Sleigh,
    execution: &Execution,
) -> bool {
    // left need to be a table
    let AssignmentWrite::TableExport { table_id, op: None } = &ass.var else {
        return false;
    };
    // table need to export a reference
    let table = sleigh.table(*table_id);
    let table_export = *table.export.borrow();
    let Some(TableExportType::Reference {
        len: left_size,
        space: _,
        also_values: _,
    }) = table_export
    else {
        return false;
    };
    // table with a defined export size
    let Some(left_size) = left_size.final_value() else {
        return false;
    };

    // right need to have a defined size
    let Some(right_size) = ass.right.size(sleigh, execution).final_value() else {
        return false;
    };

    // only if right and left size differ
    if left_size == right_size {
        return false;
    }

    // write the number of bytes of the right side
    ass.var = AssignmentWrite::TableReferenceExport {
        table_id: *table_id,
        size: FieldSize::new_bits(right_size),
    };
    true
}

// HACK auto solve simple assignments.
// eg: local tmp:2 = disassembly_var & xFFFF;
fn hack_solve_simple_bin_ands(
    ass: &mut Assignment,
    sleigh: &Sleigh,
    execution: &Execution,
) -> Result<bool, Box<VarSizeError>> {
    // left need to have a known size
    let Some(ass_size) = ass.left_size(sleigh, execution).final_value() else {
        return Ok(false);
    };

    fn get_simple_value_size(value: &mut ExprValue) -> Option<&mut FieldSize> {
        Some(match value {
            ExprValue::Int(var) => &mut var.size,
            ExprValue::TokenField(tf) => &mut tf.size,
            ExprValue::Context(ctx) => &mut ctx.size,
            ExprValue::Bitrange(bt) => &mut bt.size,
            ExprValue::DisVar(dis) => &mut dis.size,
            _ => return None,
        })
    }

    match &mut ass.right {
        // if a simple value, just assign the size to the value
        Expr::Value(ExprElement::Value { location: _, value }) => {
            let Some(var_size) = get_simple_value_size(value) else {
                return Ok(false);
            };

            var_size
                .update_action(|var| var.set_final_value(ass_size))
                .ok_or_else(|| {
                    Box::new(VarSizeError::AssignmentSides {
                        left: FieldSize::new_bits(ass_size),
                        right: ass.right.size(sleigh, execution),
                        location: ass.location.clone(),
                        backtrace: format!("{}:{}", file!(), line!()),
                    })
                })
        }

        // if a simple binary expr with two simple values, just set all of then to the same size
        Expr::Op(ExprBinaryOp {
            location: _,
            output_size,
            op: Binary::BitOr | Binary::BitAnd | Binary::BitXor,
            left,
            right,
        }) => {
            let (
                Expr::Value(ExprElement::Value {
                    location: _,
                    value: expr_left,
                }),
                Expr::Value(ExprElement::Value {
                    location: _,
                    value: expr_right,
                }),
            ) = (left.as_mut(), right.as_mut())
            else {
                return Ok(false);
            };

            let Some(left_size) = get_simple_value_size(expr_left) else {
                return Ok(false);
            };

            let Some(right_size) = get_simple_value_size(expr_right) else {
                return Ok(false);
            };

            let result_right = right_size.update_action(|x| x.set_final_value(ass_size));
            let result_right = result_right.ok_or_else(|| {
                Box::new(VarSizeError::AssignmentSides {
                    left: FieldSize::new_bits(ass_size),
                    right: right.size(sleigh, execution),
                    location: ass.location.clone(),
                    backtrace: format!("{}:{}", file!(), line!()),
                })
            })?;
            let result_left = left_size.update_action(|x| x.set_final_value(ass_size));
            let result_left = result_left.ok_or_else(|| {
                Box::new(VarSizeError::AssignmentSides {
                    left: FieldSize::new_bits(ass_size),
                    right: right.size(sleigh, execution),
                    location: ass.location.clone(),
                    backtrace: format!("{}:{}", file!(), line!()),
                })
            })?;
            let result_output = output_size.update_action(|x| x.set_final_value(ass_size));
            let result_output = result_output.ok_or_else(|| {
                Box::new(VarSizeError::AssignmentSides {
                    left: FieldSize::new_bits(ass_size),
                    right: ass.right.size(sleigh, execution),
                    location: ass.location.clone(),
                    backtrace: format!("{}:{}", file!(), line!()),
                })
            })?;
            Ok(result_left | result_right | result_output)
        }

        _ => Ok(false),
    }
}

fn hack_auto_fix_bitrange_with_bitand(
    ass: &mut Assignment,
    sleigh: &Sleigh,
    execution: &Execution,
) -> bool {
    // left side need to be a bitrange op
    let AssignmentWrite::Variable {
        op: Some(AssignmentOp::BitRange(bitrange)),
        value: _,
    } = &ass.var
    else {
        return false;
    };
    let range = bitrange.end - bitrange.start;

    // right side need to be bigger then the left side
    let Some(right_size) = ass.right.size(sleigh, execution).final_value() else {
        return false;
    };
    if right_size.get() <= range {
        return false;
    }

    // add the bitrange around the bitand
    ass.swap_right(|swap_right| {
        Expr::Value(ExprElement::new_op(
            swap_right.src().clone(),
            Unary::BitRange {
                range: 0..range,
                size: FieldSize::Value(range.try_into().unwrap()),
            },
            swap_right,
        ))
    });
    true
}

// HACK get extra info for the variable during it's creation
// eg: local tmp = *:1 value; # this var is always 1 byte
fn hack_extra_var_info_creation(
    ass: &mut Assignment,
    _sleigh: &Sleigh,
    execution: &Execution,
) -> bool {
    let AssignmentWrite::Variable {
        op: _,
        value: AssignmentWriteVariable::Local { id, creation: true },
    } = &ass.var
    else {
        return false;
    };

    let var = execution.variable(*id);
    if !var.size.get().is_fully_undefined() {
        return false;
    }

    match &ass.right {
        // in a deref from address, just use the size that is being deref as size
        Expr::Value(ExprElement::Op(ExprUnaryOp {
            location: _,
            op:
                Unary::Dereference(MemoryLocation {
                    space: _,
                    size,
                    location: _,
                }),
            input: _,
        })) => {
            var.size.set(*size);
            true
        }

        _ => false,
    }
}

// HACK truncate the right side if left side is smaller
// eg mem: R0 = *:3 ptr; # R0 is 1 byte
fn hack_auto_trunkate_right_side(
    ass: &mut Assignment,
    sleigh: &Sleigh,
    execution: &Execution,
) -> bool {
    // left need to have a known size
    let Some(left_size) = ass.left_size(sleigh, execution).final_value() else {
        return false;
    };

    // right need to have a known size
    let right_size = ass.right.size(sleigh, execution);
    let Some(right_size) = right_size.final_value() else {
        return false;
    };

    // only if left side is smaller then the right side
    if left_size >= right_size {
        return false;
    }

    match &mut ass.right {
        // if right side is deref and left side is byte sized, just adjust the mem read-size
        Expr::Value(ExprElement::Op(ExprUnaryOp {
            location: _,
            op: Unary::Dereference(MemoryLocation { size, .. }),
            input: _,
        })) if left_size.get() % 8 == 0 => {
            *size = FieldSize::new_bits(left_size);
        }

        // otherwise just add a implici truncation to the right
        _ => ass.swap_right(|swap_right| {
            Expr::Value(ExprElement::new_op(
                swap_right.src().clone(),
                Unary::BitRange {
                    range: 0..left_size.get(),
                    size: FieldSize::Value(left_size),
                },
                swap_right,
            ))
        }),
    }
    true
}

// HACK: sometimes when assigning a value smaller then the size of the left side,
// eg one bit registers are declare as one
// byte instead of bitranges (eg flags on X86 like ZF), and assigned
// binary values to it, when that happen, it's unclear if zext or a left
// hand truncation happen, I'll stick with left hand operation for now
fn hack_auto_zext_right_side(ass: &mut Assignment, sleigh: &Sleigh, execution: &Execution) -> bool {
    // left hand need to have a known size
    let left_size = match &mut ass.var {
        // can be a varnode
        AssignmentWrite::Variable {
            value: AssignmentWriteVariable::Varnode(id),
            op: None,
        } => {
            let var = sleigh.varnode(*id);
            let len = var.len_bytes.get() * 8;
            len.try_into().unwrap()
        }
        // local variable
        AssignmentWrite::Variable {
            value: AssignmentWriteVariable::Local { id, creation: _ },
            op: None,
        } => {
            let var = execution.variable(*id);
            let Some(bits) = var.size.get().final_value() else {
                return false;
            };
            bits
        }
        // or mem write
        AssignmentWrite::Memory { mem, addr: _ } => {
            let Some(size) = mem.size.final_value() else {
                return false;
            };
            size
        }
        _ => return false,
    };

    // right side need to be smaller then the right side
    let Some(right_size) = ass.right.size(sleigh, execution).max_bits() else {
        return false;
    };
    if left_size <= right_size {
        return false;
    }

    // add a zext to the right side
    ass.swap_right(|swap_right| {
        Expr::new_value(ExprElement::new_op(
            swap_right.src().clone(),
            Unary::Zext(FieldSize::new_bits(left_size)),
            swap_right,
        ))
    });

    true
}

// HACK some times, single bit register will be declared as single byte register.
// but in the execution body the value will be assign to a single bit variable
fn hack_1_byte_varnode_assign_to_bit(
    ass: &mut Assignment,
    sleigh: &Sleigh,
    execution: &Execution,
) -> bool {
    // right hand need to be one byte sized
    if ass.right.size(sleigh, execution).final_value() != Some(8.try_into().unwrap()) {
        return false;
    }

    // left hand need to be one bit len
    if ass.left_size(sleigh, execution).final_value() != Some(1.try_into().unwrap()) {
        return false;
    }

    // truncate the right size to one bit
    ass.swap_right(|swap_right| {
        Expr::Value(ExprElement::new_op(
            swap_right.src().clone(),
            Unary::BitRange {
                range: 0..1,
                size: FieldSize::Value(1.try_into().unwrap()),
            },
            swap_right,
        ))
    });

    true
}

// HACK force solving simple usercall
// eg YmmReg1 = vpblendd_avx2( vexVVVV_YmmReg, YmmReg2_m256, imm8:1 ); # vpblendd_avx2 is pcodeop
fn hack_force_solve_usercall(
    ass: &mut Assignment,
    sleigh: &Sleigh,
    execution: &Execution,
) -> Result<bool, Box<VarSizeError>> {
    // if right is a simple user_call
    let Expr::Value(ExprElement::UserCall(usercall)) = &ass.right else {
        return Ok(false);
    };
    let location = usercall.location.clone();
    // user the same size on both sides
    let (mut left_size, mut right_size) = ass.left_and_right_size_mut(sleigh, execution);
    len::a_equivalent_b(&mut *left_size, &mut *right_size).ok_or_else(|| {
        Box::new(VarSizeError::AssignmentSides {
            left: left_size.get(),
            right: right_size.get(),
            location: location.clone(),
            backtrace: format!("{}:{}", file!(), line!()),
        })
    })
}