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
use crate::analysis::ksets::KSetValue;
use crate::analysis::strided_intervals::StridedInterval;
use crate::error::*;
use crate::ir;
use std::cmp::{Ordering, PartialOrd};
use std::collections::BTreeSet;
use std::fmt;

const DEFAULT_K: usize = 4;
const MAX_K: usize = 128;

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct KSet {
    k: usize,
    value: KSetValue,
}

impl KSet {
    pub fn new(k: usize, value: KSetValue) -> KSet {
        KSet { k: k, value: value }
    }

    pub fn new_top(bits: usize) -> KSet {
        KSet {
            k: DEFAULT_K,
            value: KSetValue::Top(bits),
        }
    }

    pub fn from_constant(constant: ir::Constant) -> KSet {
        let mut hashset = BTreeSet::new();
        hashset.insert(constant);

        KSet {
            k: DEFAULT_K,
            value: KSetValue::Value(hashset),
        }
    }

    pub fn from_strided_interval(strided_interval: &StridedInterval) -> KSet {
        if strided_interval.interval().lo().is_top()
            || strided_interval.interval().hi().is_top()
            || strided_interval.bits() > 64
        {
            return KSet::new_top(strided_interval.bits());
        }

        let lo = strided_interval
            .interval()
            .lo()
            .value()
            .unwrap()
            .value_u64()
            .unwrap();
        let hi = strided_interval
            .interval()
            .hi()
            .value()
            .unwrap()
            .value_u64()
            .unwrap();

        let stride = strided_interval.stride();
        let stride = if stride == 0 { 1 } else { stride };
        let k = (hi - lo) as usize / stride;
        if k > MAX_K {
            return KSet::new_top(strided_interval.bits());
        }

        let mut hashset = BTreeSet::new();
        let mut v = lo;
        while v <= hi {
            hashset.insert(ir::Constant::new(v, strided_interval.bits()));
            v += strided_interval.stride() as u64;
        }

        KSet::new(k as usize, KSetValue::Value(hashset))
    }

    pub fn k(&self) -> usize {
        self.k
    }
    pub fn value(&self) -> &KSetValue {
        &self.value
    }
    pub fn bits(&self) -> usize {
        self.value().bits()
    }

    pub fn join(&self, other: &KSet) -> KSet {
        let k = self.k().max(other.k());

        KSet::new(k, self.value().join(other.value())).set_top_if_above_k()
    }

    fn set_top_if_above_k(self) -> KSet {
        if self
            .value()
            .value()
            .map(|value| value.len() > self.k())
            .unwrap_or(false)
        {
            KSet::new(self.k(), KSetValue::Top(self.bits()))
        } else {
            self
        }
    }

    fn binop<F>(&self, rhs: &KSet, f: F) -> Result<KSet>
    where
        F: Fn(&KSetValue, &KSetValue) -> Result<KSetValue>,
    {
        let value = f(self.value(), rhs.value())?;
        Ok(KSet::new(self.k(), value).set_top_if_above_k())
    }

    fn extop<F>(&self, bits: usize, f: F) -> Result<KSet>
    where
        F: Fn(&KSetValue, usize) -> Result<KSetValue>,
    {
        let value = f(self.value(), bits)?;
        Ok(KSet::new(self.k(), value).set_top_if_above_k())
    }

    pub fn add(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::add)
    }

    pub fn sub(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::sub)
    }

    pub fn mul(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::mul)
    }

    pub fn divu(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::divu)
    }

    pub fn modu(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::modu)
    }

    pub fn divs(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::divs)
    }

    pub fn mods(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::mods)
    }

    pub fn and(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::and)
    }

    pub fn or(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::or)
    }

    pub fn xor(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::xor)
    }

    pub fn shl(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::shl)
    }

    pub fn shr(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::shr)
    }

    pub fn cmpeq(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::cmpeq)
    }

    pub fn cmpneq(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::cmpneq)
    }

    pub fn cmpltu(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::cmpltu)
    }

    pub fn cmplts(&self, rhs: &KSet) -> Result<KSet> {
        self.binop(rhs, KSetValue::cmplts)
    }

    pub fn zext(&self, bits: usize) -> Result<KSet> {
        self.extop(bits, KSetValue::zext)
    }

    pub fn sext(&self, bits: usize) -> Result<KSet> {
        self.extop(bits, KSetValue::sext)
    }

    pub fn trun(&self, bits: usize) -> Result<KSet> {
        let v = self.extop(bits, KSetValue::trun)?;
        Ok(v)
    }

    pub fn ite(cond: &KSet, then: &KSet, else_: &KSet) -> Result<KSet> {
        let k = match cond.value() {
            KSetValue::Top(_) => then.k().max(else_.k()),
            KSetValue::Value(value) => {
                if value.len() == 1 {
                    if value.iter().next().unwrap().is_one() {
                        then.k()
                    } else {
                        else_.k()
                    }
                } else {
                    then.k().max(else_.k())
                }
            }
            KSetValue::Bottom(_) => then.k().max(else_.k()),
        };

        let value = KSetValue::ite(cond.value(), then.value(), else_.value())?;
        Ok(KSet::new(k, value).set_top_if_above_k())
    }
}

impl fmt::Display for KSet {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}[{}]", self.k(), self.value())
    }
}

impl ir::Value for KSet {
    fn bits(&self) -> usize {
        KSet::bits(self)
    }
}

impl PartialOrd for KSet {
    fn partial_cmp(&self, other: &KSet) -> Option<Ordering> {
        let order = self.value().partial_cmp(other.value())?;

        if self.k() < other.k() {
            if order == Ordering::Greater {
                None
            } else {
                Some(Ordering::Less)
            }
        } else if self.k() > other.k() {
            if order == Ordering::Less {
                None
            } else {
                Some(Ordering::Greater)
            }
        } else {
            Some(order)
        }
    }
}

impl<'e> Into<ir::Expression<KSet>> for &'e ir::Expression<ir::Constant> {
    fn into(self) -> ir::Expression<KSet> {
        match self {
            ir::Expression::LValue(lvalue) => match lvalue.as_ref() {
                ir::LValue::Variable(variable) => variable.clone().into(),
                ir::LValue::Dereference(dereference) => {
                    ir::Dereference::new(dereference.expression().into()).into()
                }
            },
            ir::Expression::RValue(rvalue) => match rvalue.as_ref() {
                ir::RValue::Value(constant) => {
                    ir::RValue::Value(KSet::from_constant(constant.clone())).into()
                }
                ir::RValue::Reference(reference) => {
                    ir::Reference::new(reference.expression().into(), reference.bits()).into()
                }
            },
            ir::Expression::Add(lhs, rhs) => {
                ir::Expression::add(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Sub(lhs, rhs) => {
                ir::Expression::sub(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Mul(lhs, rhs) => {
                ir::Expression::mul(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Divu(lhs, rhs) => {
                ir::Expression::divu(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Modu(lhs, rhs) => {
                ir::Expression::modu(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Divs(lhs, rhs) => {
                ir::Expression::divs(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Mods(lhs, rhs) => {
                ir::Expression::mods(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::And(lhs, rhs) => {
                ir::Expression::and(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Or(lhs, rhs) => {
                ir::Expression::or(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Xor(lhs, rhs) => {
                ir::Expression::xor(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Shl(lhs, rhs) => {
                ir::Expression::shl(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Shr(lhs, rhs) => {
                ir::Expression::shr(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Cmpeq(lhs, rhs) => {
                ir::Expression::cmpeq(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Cmpneq(lhs, rhs) => {
                ir::Expression::cmpneq(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Cmplts(lhs, rhs) => {
                ir::Expression::cmplts(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Cmpltu(lhs, rhs) => {
                ir::Expression::cmpltu(lhs.as_ref().into(), rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Zext(bits, rhs) => {
                ir::Expression::zext(*bits, rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Sext(bits, rhs) => {
                ir::Expression::sext(*bits, rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Trun(bits, rhs) => {
                ir::Expression::trun(*bits, rhs.as_ref().into()).unwrap()
            }
            ir::Expression::Ite(cond, then, else_) => ir::Expression::ite(
                cond.as_ref().into(),
                then.as_ref().into(),
                else_.as_ref().into(),
            )
            .unwrap(),
        }
    }
}