about summary refs log tree commit diff stats
path: root/miasm2/ir/translators/z3_ir.py
blob: 6f0b1aef90c7d949a8da39513b72b4214e10e0c7 (plain) (blame)
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
import logging
import operator

import z3

from miasm2.core.asmbloc import asm_label
from miasm2.ir.translators.translator import Translator

log = logging.getLogger("translator_z3")
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter("%(levelname)-5s: %(message)s"))
log.addHandler(console_handler)
log.setLevel(logging.WARNING)

class Z3Mem(object):
    """Memory abstration for TranslatorZ3. Memory elements are only accessed,
    never written. To give a concrete value for a given memory cell in a solver,
    add "mem32.get(address, size) == <value>" constraints to your equation.
    The endianness of memory accesses is handled accordingly to the "endianness"
    attribute.

    Note: Will have one memory space for each addressing size used.
    For example, if memory is accessed via 32 bits values and 16 bits values,
    these access will not occur in the same address space.
    """

    def __init__(self, endianness="<", name="mem"):
        """Initializes a Z3Mem object with a given @name and @endianness.
        @endianness: Endianness of memory representation. '<' for little endian,
            '>' for big endian.
        @name: name of memory Arrays generated. They will be named
            name+str(address size) (for example mem32, mem16...).
        """
        if endianness not in ['<', '>']:
            raise ValueError("Endianness should be '>' (big) or '<' (little)")
        self.endianness = endianness
        self.mems = {} # Address size -> memory z3.Array
        self.name = name

    def get_mem_array(self, size):
        """Returns a z3 Array used internally to represent memory for addresses
        of size @size.
        @size: integer, size in bit of addresses in the memory to get.
        Return a z3 Array: BitVecSort(size) -> BitVecSort(8).
        """
        try:
            mem = self.mems[size]
        except KeyError:
            # Lazy instanciation
            self.mems[size] = z3.Array(self.name + str(size),
                                        z3.BitVecSort(size),
                                        z3.BitVecSort(8))
            mem = self.mems[size]
        return mem

    def __getitem__(self, addr):
        """One byte memory access. Different address sizes with the same value
        will result in different memory accesses.
        @addr: a z3 BitVec, the address to read.
        Return a z3 BitVec of size 8 bits representing a memory access.
        """
        size = addr.size()
        mem = self.get_mem_array(size)
        return mem[addr]

    def get(self, addr, size):
        """ Memory access at address @addr of size @size.
        @addr: a z3 BitVec, the address to read.
        @size: int, size of the read in bits.
        Return a z3 BitVec of size @size representing a memory access.
        """
        original_size = size
        if original_size % 8 != 0:
            # Size not aligned on 8bits -> read more than size and extract after
            size = ((original_size / 8) + 1) * 8
        res = self[addr]
        if self.is_little_endian():
            for i in xrange(1, size/8):
                res = z3.Concat(self[addr+i], res)
        else:
            for i in xrange(1, size/8):
                res = z3.Concat(res, self[addr+i])
        if size == original_size:
            return res
        else:
            # Size not aligned, extract right sized result
            return z3.Extract(original_size-1, 0, res)

    def is_little_endian(self):
        """True if this memory is little endian."""
        return self.endianness == "<"

    def is_big_endian(self):
        """True if this memory is big endian."""
        return not self.is_little_endian()


class TranslatorZ3(Translator):
    """Translate a Miasm expression to an equivalent z3 python binding
    expression. Memory is abstracted via z3.Array (see Z3Mem).
    The result of from_expr will be a z3 Expr.

    If you want to interract with the memory abstraction after the translation,
    you can instanciate your own Z3Mem, that will be equivalent to the one
    used by TranslatorZ3.
    """

    # Implemented language
    __LANG__ = "z3"
    # Operations translation
    trivial_ops = ["+", "-", "/", "%", "&", "^", "|", "*", "<<"]
    _cache = None
    _mem = None

    @classmethod
    def from_ExprInt(cls, expr):
        return z3.BitVecVal(expr.arg.arg, expr.size)

    @classmethod
    def from_ExprId(cls, expr):
        if isinstance(expr.name, asm_label) and expr.name.offset is not None:
            return z3.BitVecVal(expr.name.offset, expr.size)
        else:
            return z3.BitVec(str(expr), expr.size)

    @classmethod
    def from_ExprMem(cls, expr):
        addr = cls.from_expr(expr.arg)
        return cls._mem.get(addr, expr.size)

    @classmethod
    def from_ExprSlice(cls, expr):
        res = cls.from_expr(expr.arg)
        res = z3.Extract(expr.stop-1, expr.start, res)
        return res

    @classmethod
    def from_ExprCompose(cls, expr):
        res = None
        args = sorted(expr.args, key=operator.itemgetter(2)) # sort by start off
        for subexpr, start, stop in args:
            sube = cls.from_expr(subexpr)
            e = z3.Extract(stop-start-1, 0, sube)
            if res:
                res = z3.Concat(e, res)
            else:
                res = e
        return res

    @classmethod
    def from_ExprCond(cls, expr):
        cond = cls.from_expr(expr.cond)
        src1 = cls.from_expr(expr.src1)
        src2 = cls.from_expr(expr.src2)
        return z3.If(cond != 0, src1, src2)

    @classmethod
    def from_ExprOp(cls, expr):
        args = map(cls.from_expr, expr.args)
        res = args[0]
        if len(args) > 1:
            for arg in args[1:]:
                if expr.op in cls.trivial_ops:
                    res = eval("res %s arg" % expr.op)
                elif expr.op == ">>":
                    res = z3.LShR(res, arg)
                elif expr.op == "a>>":
                    res = res >> arg
                elif expr.op == "a<<":
                    res = res << arg
                else:
                    raise NotImplementedError("Unsupported OP yet: %s" % expr.op)
        elif expr.op == 'parity':
            arg = z3.Extract(7, 0, res)
            res = z3.BitVecVal(1, 1)
            for i in xrange(8):
                res = res ^ z3.Extract(i, i, arg)
        elif expr.op == '-':
            res = -res
        else:
            raise NotImplementedError("Unsupported OP yet: %s" % expr.op)
        return res

    @classmethod
    def from_ExprAff(cls, expr):
        src = cls.from_expr(expr.src)
        dst = cls.from_expr(expr.dst)
        return (src == dst)

    @classmethod
    def from_expr(cls, expr, endianness="<"):
        # This mess is just to handle cache and Z3Mem instance management
        # Might be improved in the future
        del_cache = False
        del_mem = False
        if cls._cache is None:
            cls._cache = {}
            del_cache = True
        if cls._mem is None:
            cls._mem = Z3Mem(endianness)
            del_mem = True

        try:
            if expr in cls._cache:
                return cls._cache[expr]
            else:
                ret = super(TranslatorZ3, cls).from_expr(expr)
                cls._cache[expr] = ret
                return ret
        finally:
            # Clean cache and Z3Mem if this call is the root call
            if del_cache:
                cls._cache = None
            if del_mem:
                cls._mem = None

# Register the class
Translator.register(TranslatorZ3)