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
|
#include "verify.h"
#include "builtins/builtins.h"
namespace goose::verify
{
optional< z3::expr > BuildZ3ValFromConstant( const Value& val )
{
auto& c = GetZ3Context();
// TODO: what about very large int types? Can z3 represent them?
if( auto b = FromValue< bool >( val ) )
return c.bool_val( *b );
else if( auto intVal = FromValue< APSInt >( val ) )
return c.int_val( intVal->getExtValue() );
return nullopt;
}
optional< z3::expr > BuildZ3ConstantFromType( const Term& type, const string& name )
{
auto& c = GetZ3Context();
// TODO: what about very large int types? Can z3 represent them?
if( type == GetValueType< bool >() )
return c.bool_const( name.c_str() );
else if( FromValue< builtins::IntegerType >( *ValueFromIRExpr( type ) ) )
return c.int_const( name.c_str() );
return nullopt;
}
template< typename I, typename F >
optional< z3::expr > BuildZ3UnaryExpr( Builder& b, const I& instr, F&& func )
{
auto operand = BuildZ3ExprFromValue( b, instr.operand() );
if( !operand )
return nullopt;
return func( *operand );
}
template< typename I, typename F >
optional< z3::expr > BuildZ3BinExpr( Builder& b, const I& instr, F&& func )
{
auto lhs = BuildZ3ExprFromValue( b, instr.lhs() );
if( !lhs )
return nullopt;
auto rhs = BuildZ3ExprFromValue( b, instr.rhs() );
if( !rhs )
return nullopt;
return func( *lhs, *rhs );
}
template< typename T >
optional< z3::expr > BuildZ3Op( Builder& b, const T& instr )
{
return nullopt;
}
// Implemented in call.cpp
extern optional< z3::expr > BuildZ3Op( Builder& b, const Call& instr );
optional< z3::expr > BuildZ3Op( Builder& b, const CreateTemporary& instr )
{
b.setVar( instr.index(), instr.value() );
return nullopt;
}
optional< z3::expr > BuildZ3Op( Builder& b, const GetTemporary& instr )
{
const auto* expr = b.retrieveVar( instr.index(), instr.type() );
if( expr )
return *expr;
return BuildZ3ConstantFromType( instr.type(), format( "v{}", instr.index() ) );
}
optional< z3::expr > BuildZ3Op( Builder& b, const GetVar& instr )
{
const auto* expr = b.retrieveVar( instr.index(), instr.type() );
if( expr )
return *expr;
return BuildZ3ConstantFromType( instr.type(), format( "v{}", instr.index() ) );
}
optional< z3::expr > BuildZ3Op( Builder& b, const SetVar& instr )
{
b.setVar( instr.index(), instr.value() );
return nullopt;
}
// TODO: Phi. Construct an expression combining all of the incoming z3 expressions from all the possible incoming blocks.
// TODO: LoadConstStr. Build a z3 str value.
// TODO: bitwise operators. They need to work on bitvecs, so we'll need a param indicating that we want to
// build bitvecs rather than ints, and use it when building the sub expression of something that needs bitvecs.
optional< z3::expr > BuildZ3Op( Builder& b, const Not& instr )
{
// TODO this works only for bools, have to use ~ for bitvecs
return BuildZ3UnaryExpr( b, instr, []( auto&& operand ) { return !operand; } );
}
optional< z3::expr > BuildZ3Op( Builder& b, const And& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs ) -> optional< z3::expr >
{
// TODO handle non bools (use & for bitvecs)
if( !lhs.is_bool() || !rhs.is_bool() )
return nullopt;
return lhs && rhs;
} );
}
optional< z3::expr > BuildZ3Op( Builder& b, const Or& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs ) -> optional< z3::expr >
{
// TODO handle non bools (use | for bitvecs)
if( !lhs.is_bool() || !rhs.is_bool() )
return nullopt;
return lhs || rhs;
} );
}
// TODO: Xor, Shl, LShr, AShr
optional< z3::expr > BuildZ3Op( Builder& b, const Add& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs ) { return lhs + rhs; } );
}
optional< z3::expr > BuildZ3Op( Builder& b, const Sub& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs ) { return lhs - rhs; } );
}
optional< z3::expr > BuildZ3Op( Builder& b, const Mul& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs ) { return lhs * rhs; } );
}
// TODO: UDiv (check how unsigneds need to be handled. I've seen somewhere saying to use bitvectors,
// but the api have unsigned methods for nums? Maybe in current versions of z3 we don't need to use bitvectors for unsigned?)
optional< z3::expr > BuildZ3Op( Builder& b, const SDiv& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs ) { return lhs / rhs; } );
}
// TODO: URem
optional< z3::expr > BuildZ3Op( Builder& b, const SRem& instr )
{
// TODO: need to use a different function for bitvectors
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs ) { return z3::rem( lhs, rhs ); } );
}
optional< z3::expr > BuildZ3Op( Builder& b, const Eq& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs ) { return lhs == rhs; } );
}
optional< z3::expr > BuildZ3Op( Builder& b, const Neq& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs ) { return lhs != rhs; } );
}
// TODO: UGT, UGE, ULT, ULE
optional< z3::expr > BuildZ3Op( Builder& b, const SGT& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs ) { return lhs > rhs; } );
}
optional< z3::expr > BuildZ3Op( Builder& b, const SGE& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs ) { return lhs >= rhs; } );
}
optional< z3::expr > BuildZ3Op( Builder& b, const SLT& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs ) { return lhs < rhs; } );
}
optional< z3::expr > BuildZ3Op( Builder& b, const SLE& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs ) { return lhs <= rhs; } );
}
optional< z3::expr > BuildZ3Op( Builder& b, const Assert& instr )
{
// TODO: refactor the return value here to be able to indicate failure versus "nothing to do"
auto cond = BuildZ3ExprFromValue( b, instr.cond() );
if( cond )
b.checkAssertion( *cond, instr.cond().locationId() );
return nullopt;
}
optional< z3::expr > BuildZ3Op( Builder& b, const Placeholder& instr )
{
const auto* expr = b.retrievePlaceholder( instr.name() );
if( expr )
return *expr;
return BuildZ3ConstantFromType( instr.type(), format( "p{}", instr.name() ) );
}
optional< z3::expr > BuildZ3Op( Builder& b, const llr::Instruction& instr )
{
return visit( [&]( auto&& e )
{
return BuildZ3Op( b, e );
}, instr.content() );
}
optional< z3::expr > BuildZ3ExprFromValue( Builder& b, const Value& val )
{
if( val.isPoison() )
return nullopt;
if( val.isConstant() )
return BuildZ3ValFromConstant( val );
|
|
>
|
>
>
>
|
>
>
>
|
>
|
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
<
|
<
|
>
>
>
|
>
>
>
|
>
|
|
<
|
|
>
>
|
|
|
<
|
|
>
>
|
|
|
|
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
#include "verify.h"
#include "builtins/builtins.h"
namespace goose::verify
{
optional< Z3Val > BuildZ3ValFromConstant( const Value& val )
{
auto& c = GetZ3Context();
auto type = *ValueFromIRExpr( val.type() );
// TODO: what about very large int types? Can z3 represent them?
if( auto b = FromValue< bool >( val ) )
return Z3Val{ c.bool_val( *b ), move( type ) };
else if( auto intVal = FromValue< APSInt >( val ) )
{
auto intType = FromValue< builtins::IntegerType >( type );
if( intType->m_signed )
return Z3Val{ c.int_val( intVal->getExtValue() ), move( type ) };
return Z3Val{ c.bv_val( intVal->getExtValue(), intType->m_numBits ), move( type ) };
}
return nullopt;
}
optional< Z3Val > BuildZ3ConstantFromType( const Term& type, const string& name )
{
auto& c = GetZ3Context();
auto typeVal = *ValueFromIRExpr( type );
// TODO: what about very large int types? Can z3 represent them?
if( type == GetValueType< bool >() )
return Z3Val{ c.bool_const( name.c_str() ), move( typeVal ) };
else if( auto intType = FromValue< builtins::IntegerType >( *ValueFromIRExpr( type ) ) )
{
if( intType->m_signed )
return Z3Val{ c.int_const( name.c_str() ), move( typeVal ) };
return Z3Val{ c.bv_const( name.c_str(), intType->m_numBits ), move( typeVal ) };
}
return nullopt;
}
z3::expr GetAsBitVec( const z3::expr& expr, const Value& type )
{
if( expr.is_bv() )
return expr;
assert( expr.is_int() );
auto intType = FromValue< builtins::IntegerType >( type );
assert( intType );
return z3::int2bv( intType->m_numBits, expr );
}
z3::expr GetAsBitVec( const Z3Val& zv )
{
return GetAsBitVec( zv.expr, zv.type );
}
template< typename I, typename F >
optional< Z3Val > BuildZ3UnaryExpr( Builder& b, const I& instr, F&& func )
{
auto operand = BuildZ3ExprFromValue( b, instr.operand() );
if( !operand )
return nullopt;
return Z3Val{ func( operand->expr ), operand->type };
}
template< typename I, typename F >
optional< Z3Val > BuildZ3BinExpr( Builder& b, const I& instr, F&& func )
{
auto lhs = BuildZ3ExprFromValue( b, instr.lhs() );
if( !lhs )
return nullopt;
auto rhs = BuildZ3ExprFromValue( b, instr.rhs() );
if( !rhs )
return nullopt;
if( lhs->expr.get_sort().sort_kind() == rhs->expr.get_sort().sort_kind() )
return Z3Val{ func( lhs->expr, rhs->expr, lhs->type ), lhs->type };
// If we are trying to do an operation on a mix of bitvec and int,
// convert the int to a bitvec first.
if( lhs->expr.is_bv() )
{
assert( rhs->expr.is_int() );
return Z3Val{ func( lhs->expr, GetAsBitVec( *rhs ), lhs->type ), lhs->type };
}
else
{
assert( lhs->expr.is_int() );
return Z3Val{ func( GetAsBitVec( *lhs ), rhs->expr, lhs->type ), lhs->type };
}
return nullopt;
}
template< typename T >
optional< Z3Val > BuildZ3Op( Builder& b, const T& instr )
{
return nullopt;
}
// Implemented in call.cpp
extern optional< Z3Val > BuildZ3Op( Builder& b, const Call& instr );
optional< Z3Val > BuildZ3Op( Builder& b, const CreateTemporary& instr )
{
b.setVar( instr.index(), instr.value() );
return nullopt;
}
optional< Z3Val > BuildZ3Op( Builder& b, const GetTemporary& instr )
{
const auto* expr = b.retrieveVar( instr.index(), instr.type() );
if( expr )
return Z3Val{ *expr, *ValueFromIRExpr( instr.type() ) };
return BuildZ3ConstantFromType( instr.type(), format( "v{}", instr.index() ) );
}
optional< Z3Val > BuildZ3Op( Builder& b, const GetVar& instr )
{
const auto* expr = b.retrieveVar( instr.index(), instr.type() );
if( expr )
return Z3Val{ *expr, *ValueFromIRExpr( instr.type() ) };
return BuildZ3ConstantFromType( instr.type(), format( "v{}", instr.index() ) );
}
optional< Z3Val > BuildZ3Op( Builder& b, const SetVar& instr )
{
b.setVar( instr.index(), instr.value() );
return nullopt;
}
// TODO: Phi. Construct an expression combining all of the incoming z3 expressions from all the possible incoming blocks.
// TODO: LoadConstStr. Build a z3 str value.
// TODO: bitwise operators. They need to convert their operands to bitvec first as needed.
optional< Z3Val > BuildZ3Op( Builder& b, const Not& instr )
{
return BuildZ3UnaryExpr( b, instr, []( auto&& operand )
{
if( operand.is_bool() )
return !operand;
assert( operand.is_bv() );
return ~operand;
} );
}
optional< Z3Val > BuildZ3Op( Builder& b, const And& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type )
{
if( lhs.is_bool() && rhs.is_bool() )
return lhs && rhs;
auto lhsBV = GetAsBitVec( lhs, type );
auto rhsBV = GetAsBitVec( rhs, type );
return lhsBV & rhsBV;
} );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Or& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type )
{
if( lhs.is_bool() && rhs.is_bool() )
return lhs || rhs;
auto lhsBV = GetAsBitVec( lhs, type );
auto rhsBV = GetAsBitVec( rhs, type );
return lhsBV | rhsBV;
} );
}
// TODO: Xor, Shl, LShr, AShr
optional< Z3Val > BuildZ3Op( Builder& b, const Add& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs + rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Sub& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs - rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Mul& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs * rhs; } );
}
// TODO: UDiv
optional< Z3Val > BuildZ3Op( Builder& b, const SDiv& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs / rhs; } );
}
// TODO: URem
optional< Z3Val > BuildZ3Op( Builder& b, const SRem& instr )
{
// TODO: need to use a different function for bitvectors
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return z3::rem( lhs, rhs ); } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Eq& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs == rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Neq& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs != rhs; } );
}
// TODO: UGT, UGE, ULT, ULE
optional< Z3Val > BuildZ3Op( Builder& b, const SGT& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs > rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const SGE& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs >= rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const SLT& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs < rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const SLE& instr )
{
return BuildZ3BinExpr( b, instr, []( auto&& lhs, auto&& rhs, auto&& type ) { return lhs <= rhs; } );
}
optional< Z3Val > BuildZ3Op( Builder& b, const Assert& instr )
{
// TODO: refactor the return value here to be able to indicate failure versus "nothing to do"
auto cond = BuildZ3ExprFromValue( b, instr.cond() );
if( cond )
b.checkAssertion( cond->expr, instr.cond().locationId() );
return nullopt;
}
optional< Z3Val > BuildZ3Op( Builder& b, const Placeholder& instr )
{
const auto* expr = b.retrievePlaceholder( instr.name() );
if( expr )
return Z3Val{ *expr, *ValueFromIRExpr( instr.type() ) };
return BuildZ3ConstantFromType( instr.type(), format( "p{}", instr.name() ) );
}
optional< Z3Val > BuildZ3Op( Builder& b, const llr::Instruction& instr )
{
return visit( [&]( auto&& e )
{
return BuildZ3Op( b, e );
}, instr.content() );
}
optional< Z3Val > BuildZ3ExprFromValue( Builder& b, const Value& val )
{
if( val.isPoison() )
return nullopt;
if( val.isConstant() )
return BuildZ3ValFromConstant( val );
|