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
// Panopticon - A libre program analysis library for machine code
// Copyright (C) 2014-2018  The Panopticon Developers
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

use std::num::Wrapping;
use std::u64;

use quickcheck::{Arbitrary,Gen};

use {NameRef,Result,SegmentRef};

/// A variable with known size.
#[derive(Clone,Copy,PartialEq,Eq,PartialOrd,Ord,Debug,Hash)]
pub struct Variable {
    /// Name of the variable.
    pub name: NameRef,
    /// Size of the variable value in bits. Never zero.
    pub bits: usize,
}

impl Variable {
    /// Crates a new variable. Fails if `bits` zero.
    pub fn new(name: NameRef, bits: usize) -> Result<Variable> {
        if bits == 0 { return Err("Variable can't have size 0".into()); }

        Ok(Variable{
            name: name,
            bits: bits
        })
    }
}

/// A constant value.
#[derive(Clone,Copy,PartialEq,Eq,PartialOrd,Ord,Debug,Hash)]
pub struct Constant {
    /// Value
    pub value: u64,
    /// Size of the value in bits. Never zero.
    pub bits: usize,
}

impl Constant {
    /// Create a new constant. Fails if `bits` is zero.
    pub fn new(value: u64, bits: usize) -> Result<Constant> {
        if bits == 0 { return Err("Variable can't have size 0".into()); }

        Ok(Constant{
            value: value,
            bits: bits
        })
    }

    /// Returns a bit mask covering all bits of the constant. Returns `u64::MAX` if `bits` > 64.
    pub fn mask(&self) -> Wrapping<u64> {
        Wrapping(if self.bits < 64 { (1u64 << self.bits) - 1 } else { u64::MAX })
    }
}

impl Into<Wrapping<u64>> for Constant {
    fn into(self) -> Wrapping<u64> {
        Wrapping(self.value) & self.mask()
    }
}

/// A RREIL value.
#[derive(Clone,Copy,PartialEq,Eq,PartialOrd,Ord,Debug,Hash)]
pub enum Value {
    /// An undefined value of unknown size.
    Undefined,
    /// A RREIL variable, possibly in SSA form.
    Variable(Variable),
    /// A constant value.
    Constant(Constant),
}

impl Value {
    /// Creates a new constant value. Fails if `bits` is zero.
    pub fn val(val: u64, bits: usize) -> Result<Value> {
        Ok(Value::Constant(Constant::new(val,bits)?))
    }

    /// Creates a new variable. Fails if `bits` is zero.
    pub fn var(name: NameRef, bits: usize) -> Result<Value> {
        Ok(Value::Variable(Variable::new(name,bits)?))
    }

    /// Creates an undefined value.
    pub fn undef() -> Value {
        Value::Undefined
    }

    /// Returns the size of the value in bits or `None` if it is the undefined value.
    pub fn bits(&self) -> Option<usize> {
        match self {
            &Value::Variable(Variable{ bits,.. }) => Some(bits),
            &Value::Constant(Constant{ bits,.. }) => Some(bits),
            &Value::Undefined => None,
        }
    }


}

impl From<Variable> for Value {
    fn from(v: Variable) -> Value {
        Value::Variable(v)
    }
}

impl From<Constant> for Value {
    fn from(v: Constant) -> Value {
        Value::Constant(v)
    }
}

/// A RREIL memory segment identifier.
#[derive(Clone,Copy,PartialEq,Eq,Debug,Hash)]
pub struct Segment {
    /// Segment name.
    pub name: SegmentRef,
}

impl Arbitrary for Variable {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        Variable {
            name: NameRef::arbitrary(g),
            bits: 1 << g.gen_range(0, 11),
        }
    }
}

impl Arbitrary for Constant {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        Constant{
            value: g.gen(),
            bits: 1 << g.gen_range(0, 11),
        }
    }
}

impl Arbitrary for Segment {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        Segment{ name: SegmentRef::new(g.gen_range(0,100)) }
    }
}

impl Arbitrary for Value {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        match g.gen_range(0, 2) {
            0 => Value::Undefined,
            1 => Value::Variable(Variable::arbitrary(g)),
            2 => Value::Constant(Constant::arbitrary(g)),
            _ => unreachable!(),
        }
    }
}