Skip to main content

rapx/vm/mir_rules/call/effect/
numeric.rs

1use super::*;
2
3impl<'z3, 'tcx> VmExecutor<'z3, 'tcx> {
4    fn apply_binary_num(
5        &mut self,
6        dest: Local,
7        args: &[VmValue<'z3, 'tcx>],
8        lhs_arg: usize,
9        rhs_arg: usize,
10        f: impl Fn(&Int<'z3>, &Int<'z3>) -> Int<'z3>,
11    ) {
12        if let (Some(lhs), Some(rhs)) = (args.get(lhs_arg), args.get(rhs_arg)) {
13            let dest_ty = self.body().local_decls[dest].ty;
14            self.set_local(dest, VmValue::new(f(&lhs.z3_term, &rhs.z3_term), dest_ty));
15        }
16    }
17
18    pub(super) fn apply_numeric_effect(
19        &mut self,
20        effect: &CallEffect,
21        args: &[VmValue<'z3, 'tcx>],
22        dest: Local,
23    ) {
24        match effect {
25            CallEffect::ReturnMin { lhs_arg, rhs_arg } => {
26                // Keep min as an `ite` so path conditions can simplify it
27                // during later alignment and bounds checks.
28                self.apply_binary_num(dest, args, *lhs_arg, *rhs_arg, |lhs, rhs| {
29                    lhs.le(rhs).ite(lhs, rhs)
30                });
31            }
32            CallEffect::ReturnMax { lhs_arg, rhs_arg } => {
33                self.apply_binary_num(dest, args, *lhs_arg, *rhs_arg, |lhs, rhs| {
34                    lhs.ge(rhs).ite(lhs, rhs)
35                });
36            }
37            CallEffect::ReturnAdd { lhs_arg, rhs_arg } => {
38                self.apply_binary_num(dest, args, *lhs_arg, *rhs_arg, |lhs, rhs| {
39                    Int::add(self.z3_ctx, &[lhs, rhs])
40                });
41            }
42            CallEffect::ReturnMul { lhs_arg, rhs_arg } => {
43                self.apply_binary_num(dest, args, *lhs_arg, *rhs_arg, |lhs, rhs| {
44                    Int::mul(self.z3_ctx, &[lhs, rhs])
45                });
46            }
47            CallEffect::ReturnBoundedArithmetic {
48                lhs_arg,
49                rhs_arg,
50                op,
51            } => {
52                if let (Some(lhs), Some(rhs)) = (args.get(*lhs_arg), args.get(*rhs_arg)) {
53                    let dest_ty = self.body().local_decls[dest].ty;
54                    let Some((min, max, modulus, signed)) = self
55                        .integer_limits(dest_ty)
56                        .or_else(|| self.integer_limits(lhs.ty))
57                    else {
58                        return;
59                    };
60                    let raw = match op {
61                        BoundedArithmeticOp::SaturatingAdd | BoundedArithmeticOp::WrappingAdd => {
62                            Int::add(self.z3_ctx, &[&lhs.z3_term, &rhs.z3_term])
63                        }
64                        BoundedArithmeticOp::SaturatingSub | BoundedArithmeticOp::WrappingSub => {
65                            Int::sub(self.z3_ctx, &[&lhs.z3_term, &rhs.z3_term])
66                        }
67                    };
68                    let term = match op {
69                        BoundedArithmeticOp::SaturatingAdd | BoundedArithmeticOp::SaturatingSub => {
70                            raw.lt(&min).ite(&min, &raw.gt(&max).ite(&max, &raw))
71                        }
72                        BoundedArithmeticOp::WrappingAdd | BoundedArithmeticOp::WrappingSub => {
73                            let wrapped = raw.modulo(&modulus);
74                            if signed {
75                                wrapped
76                                    .gt(&max)
77                                    .ite(&Int::sub(self.z3_ctx, &[&wrapped, &modulus]), &wrapped)
78                            } else {
79                                wrapped
80                            }
81                        }
82                    };
83                    self.set_local(dest, VmValue::new(term, dest_ty));
84                }
85            }
86            CallEffect::ReturnWithOverflow {
87                lhs_arg,
88                rhs_arg,
89                op,
90            } => {
91                if let (Some(lhs), Some(rhs)) = (args.get(*lhs_arg), args.get(*rhs_arg)) {
92                    let term = match op {
93                        OverflowOp::Add => Int::add(self.z3_ctx, &[&lhs.z3_term, &rhs.z3_term]),
94                        OverflowOp::Sub => Int::sub(self.z3_ctx, &[&lhs.z3_term, &rhs.z3_term]),
95                        OverflowOp::Mul => Int::mul(self.z3_ctx, &[&lhs.z3_term, &rhs.z3_term]),
96                    };
97                    self.set_field_value(dest, vec![0], VmValue::new(term, lhs.ty));
98                }
99            }
100            CallEffect::ReturnNonZeroIff { arg } => {
101                if let Some(a) = args.get(*arg) {
102                    let dest_ty = self.body().local_decls[dest].ty;
103                    let zero = Int::from_u64(self.z3_ctx, 0);
104                    let term = self.fresh_int(&format!("ret_nz_iff_{}", dest.as_usize()));
105                    self.path_state.constraints
106                        .assertions
107                        .push(term._eq(&zero)._eq(&a.z3_term._eq(&zero)));
108                    self.set_local(dest, VmValue::new(term, dest_ty));
109                }
110            }
111            CallEffect::ReturnLeadingZeros { arg } => {
112                if let Some(value) = args.get(*arg) {
113                    let width = self.size_of_ty(value.ty) * 8;
114                    if width == 0
115                        || width > 128
116                        || !matches!(value.ty.kind(), TyKind::Uint(_) | TyKind::Int(_))
117                    {
118                        self.record_unsupported("ctlz operand type");
119                    } else {
120                        let zero = Int::from_u64(self.z3_ctx, 0);
121                        let width_term = Int::from_u64(self.z3_ctx, width);
122                        let one = Int::from_u64(self.z3_ctx, 1);
123                        let two = Int::from_u64(self.z3_ctx, 2);
124                        let high_bit = (1..width).fold(one.clone(), |acc, _| {
125                            Int::mul(self.z3_ctx, &[&acc, &two]).simplify()
126                        });
127                        let term = self.fresh_int(&format!("ctlz_{}", dest.as_usize()));
128                        self.path_state.constraints.assertions.push(term.ge(&zero));
129                        self.path_state.constraints.assertions.push(term.le(&width_term));
130                        self.path_state.constraints
131                            .assertions
132                            .push(value.z3_term._eq(&zero).implies(&term._eq(&width_term)));
133                        self.path_state.constraints
134                            .assertions
135                            .push(value.z3_term.lt(&high_bit).implies(&term.ge(&one)));
136                        self.path_state.constraints
137                            .assertions
138                            .push(value.z3_term.ge(&high_bit).implies(&term._eq(&zero)));
139                        self.path_state.constraints
140                            .assertions
141                            .push(value.z3_term.lt(&zero).implies(&term._eq(&zero)));
142                        let dest_ty = self.body().local_decls[dest].ty;
143                        self.set_local(dest, VmValue::new(term, dest_ty));
144                    }
145                }
146            }
147            CallEffect::ReturnTrailingZeros { arg } => {
148                if let Some(value) = args.get(*arg) {
149                    let width = self.size_of_ty(value.ty) * 8;
150                    if width == 0
151                        || width > 128
152                        || !matches!(value.ty.kind(), TyKind::Uint(_) | TyKind::Int(_))
153                    {
154                        self.record_unsupported("cttz operand type");
155                    } else {
156                        let zero = Int::from_u64(self.z3_ctx, 0);
157                        let one = Int::from_u64(self.z3_ctx, 1);
158                        let two = Int::from_u64(self.z3_ctx, 2);
159                        let width_term = Int::from_u64(self.z3_ctx, width);
160                        let term = self.fresh_int(&format!("cttz_{}", dest.as_usize()));
161                        self.path_state.constraints.assertions.push(term.ge(&zero));
162                        self.path_state.constraints.assertions.push(term.le(&width_term));
163                        self.path_state.constraints
164                            .assertions
165                            .push(value.z3_term._eq(&zero).implies(&term._eq(&width_term)));
166                        self.path_state.constraints.assertions.push(
167                            value
168                                .z3_term
169                                .modulo(&two)
170                                ._eq(&one)
171                                .implies(&term._eq(&zero)),
172                        );
173                        self.path_state.constraints.assertions.push(
174                            Bool::and(
175                                self.z3_ctx,
176                                &[
177                                    &value.z3_term.modulo(&two)._eq(&zero),
178                                    &value.z3_term._eq(&zero).not(),
179                                ],
180                            )
181                            .implies(&term.ge(&one)),
182                        );
183                        let dest_ty = self.body().local_decls[dest].ty;
184                        self.set_local(dest, VmValue::new(term, dest_ty));
185                    }
186                }
187            }
188            _ => unreachable!("non-numeric call effect"),
189        }
190    }
191}