forked from HexRaysSA/goomba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lin_conj_exprs.hpp
297 lines (263 loc) · 8.69 KB
/
lin_conj_exprs.hpp
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
/*
* Copyright (c) 2023 by Hex-Rays, [email protected]
* ALL RIGHTS RESERVED.
*
* gooMBA plugin for Hex-Rays Decompiler.
*
*/
#pragma once
#include <hexrays.hpp>
#include "linear_exprs.hpp"
#include "mcode_emu.hpp"
typedef qvector<mcode_val_t> coeff_vector_t;
typedef qvector<mcode_val_t> eval_trace_t;
const int LIN_CONJ_MAX_VARS = 16;
// represents a linear combination of conjunctions
class lin_conj_expr_t : public candidate_expr_t
{
protected:
mopvec_t mops;
coeff_vector_t coeffs;
eval_trace_t eval_trace;
public:
//-------------------------------------------------------------------------
const char *dstr() const override
{
static char buf[MAXSTR];
char *ptr = buf;
char *end = buf + sizeof(buf);
ptr += qsnprintf(ptr, end-ptr, "0x%" FMT_64 "x", coeffs[0].val);
for ( uint32 i = 1; i < coeffs.size(); i++ )
{
if ( coeffs[i].val == 0 )
continue;
ptr += qsnprintf(ptr, end-ptr, " + 0x%" FMT_64 "x(", coeffs[i].val);
ptr = print_assignment(ptr, end, i);
APPEND(ptr, end, ")");
}
return buf;
}
//-------------------------------------------------------------------------
// each boolean assignment is represented as a uint32, where the nth bit
// represents the 0/1 value of the corresponding variable
char *print_assignment(char *ptr, char *end, uint32 assn) const
{
bool first_printed = false;
for ( int i = 0; i < mops.size(); i++ )
{
if ( ((assn >> i) & 1) != 0 )
{
if ( first_printed )
APPCHAR(ptr, end, '&');
APPEND(ptr, end, mops[i].dstr());
first_printed = true;
}
}
return ptr;
}
//-------------------------------------------------------------------------
// each boolean assignment is represented as a uint32, where the nth bit
// represents the 0/1 value of the corresponding variable
void apply_assignment(uint32 assn, std::map<const mop_t, mcode_val_t> &dest)
{
// recall std::map keeps keys in sorted order
int curr_idx = 0;
for ( auto &kv : dest )
{
kv.second.val = (assn >> curr_idx) & 1;
curr_idx++;
}
}
//-------------------------------------------------------------------------
// the i'th index in output_vals contains the output value corresponding to
// the i'th assignment, where the i'th assignment is defined as in
// apply_assignment.
// the return value of this function is the corresponding coefficients in
// the linear combination of conjunctions that would yield the output
// behavior. The coefficients are ordered based on the same indexing pattern.
void compute_coeffs(coeff_vector_t &dest, const qvector<mcode_val_t> &output_vals)
{
dest = coeff_vector_t();
dest.reserve(output_vals.size());
dest.push_back(output_vals[0]); // the zero coeff = the zero assignment
// we can think of the problem as solving the linear equation Ax = y,
// where y is the output_vals and x is the desired coefficient set.
// A is defined as the binary matrix where row numbers represent
// assignments and columns represent conjunctions. See the SiMBA paper
// for more details.
// We do an additional simplification, noting that
// A_{ij} = (i & j) == j. Also, we use forward substitution since A is a
// lower-triangular matrix.
for ( uint32 i = 1; i < output_vals.size(); i++ )
{
mcode_val_t curr_coeff = output_vals[i];
for ( uint32 j = 0; j < i; j++ )
{
if ( (i & j) == j )
curr_coeff = curr_coeff - dest[j];
}
dest.push_back(curr_coeff);
}
}
//-------------------------------------------------------------------------
void recompute_coeffs()
{
compute_coeffs(coeffs, eval_trace);
}
//-------------------------------------------------------------------------
mcode_val_t evaluate(mcode_emulator_t &emu) const override
{
minsn_t *minsn = to_minsn(0);
mcode_val_t res = emu.minsn_value(*minsn);
delete minsn;
return res;
}
//-------------------------------------------------------------------------
// eliminates all variables that are not needed in the expression
void eliminate_variables()
{
for ( int i = 0; i < mops.size(); i++ )
{
if ( can_eliminate_variable(i) )
{
eliminate_variable(i);
i--; // the mop at mop[i] no longer exists
}
}
}
//-------------------------------------------------------------------------
// creates a linear combination of conjunctions based on the minsn behavior
lin_conj_expr_t(const minsn_t &insn)
{
default_zero_mcode_emu_t emu;
mcode_val_t const_term = emu.minsn_value(insn);
int nvars = emu.assigned_vals.size();
if ( nvars > LIN_CONJ_MAX_VARS )
throw "lin_conj_expr_t: too many input variables";
uint32 max_assignment = 1 << nvars;
// we have already gotten the value for the all-zeroes assignment, which is const_term
eval_trace.push_back(const_term);
eval_trace.reserve(max_assignment);
for ( uint32 assn = 1; assn < max_assignment; assn++ )
{
apply_assignment(assn, emu.assigned_vals);
mcode_val_t output_val = emu.minsn_value(insn);
eval_trace.push_back(output_val);
}
compute_coeffs(coeffs, eval_trace);
mops.reserve(emu.assigned_vals.size());
for ( const auto &kv : emu.assigned_vals )
mops.push_back(kv.first);
QASSERT(30679, coeffs.size() == (1ull << mops.size()));
}
//-------------------------------------------------------------------------
z3::expr to_smt(z3_converter_t &cvtr) const override
{
minsn_t *minsn = to_minsn(0);
z3::expr res = cvtr.minsn_to_expr(*minsn);
delete minsn;
return res;
}
//-------------------------------------------------------------------------
// converts an assignment to the corresponding conjunction. e.g.
// 0b1101 => x0 & x2 & x3
minsn_t *assn_to_minsn(uint32 assn, int size, ea_t ea) const
{
QASSERT(30680, assn != 0);
minsn_t *res = nullptr;
for ( int i = 0; i < mops.size(); i++ )
{
if ( ((assn >> i) & 1) != 0 )
{
if ( res == nullptr )
{
res = resize_mop(ea, mops[i], size, false);
}
else
{
minsn_t *new_res = new minsn_t(ea);
new_res->opcode = m_and;
new_res->l.create_from_insn(res);
minsn_t *rsz = resize_mop(ea, mops[i], size, false);
new_res->r.create_from_insn(rsz);
delete rsz;
new_res->d.size = size;
delete res;
res = new_res;
}
}
}
QASSERT(30681, res->opcode != m_ldc);
return res;
}
//-------------------------------------------------------------------------
minsn_t *to_minsn(ea_t ea) const override
{
minsn_t *res = new minsn_t(ea);
res->opcode = m_ldc;
res->l.make_number(coeffs[0].val, coeffs[0].size, ea);
res->r.zero();
res->d.size = coeffs[0].size;
for ( uint32 assn = 1; assn < coeffs.size(); assn++ )
{
auto coeff = coeffs[assn];
if ( coeff.val == 0 )
continue;
// mul = coeff * F(mops)
minsn_t mul(ea);
mul.opcode = m_mul;
mul.l.make_number(coeff.val, coeff.size);
minsn_t *F = assn_to_minsn(assn, coeff.size, ea);
mul.r.create_from_insn(F);
delete F;
mul.d.size = coeff.size;
// add = res + mul
minsn_t *add = new minsn_t(ea);
add->opcode = m_add;
add->l.create_from_insn(res);
add->r.create_from_insn(&mul);
add->d.size = coeff.size;
delete res; // mop_t::create_from_insn makes a copy of the insn
res = add;
}
return res;
}
private:
//-------------------------------------------------------------------------
// returns true if the variable can be eliminated safely
// i.e. all terms containing it have coeff = 0
bool can_eliminate_variable(int idx)
{
for ( uint32 assn = 0; assn < coeffs.size(); assn++ )
{
if ( ((assn >> idx) & 1) != 0 && coeffs[assn].val != 0 )
return false;
}
return true;
}
//-------------------------------------------------------------------------
// removes the variable from the expression
// make sure to check can_eliminate_variable before calling
void eliminate_variable(int idx)
{
coeff_vector_t new_coeffs;
eval_trace_t new_evals;
new_coeffs.reserve(coeffs.size() / 2);
new_evals.reserve(coeffs.size() / 2);
for ( uint32 assn = 0; assn < coeffs.size(); assn++ )
{
if ( ((assn >> idx) & 1) == 0 )
{
new_coeffs.push_back(coeffs[assn]);
new_evals.push_back(eval_trace[assn]);
}
else
{
QASSERT(30682, coeffs[assn].val == 0);
}
}
coeffs = new_coeffs;
eval_trace = new_evals;
mops.erase(mops.begin() + idx);
}
};