35 std::string TritonToZ3::getStringValue(
const z3::expr& expr) {
36 return Z3_get_numeral_string(this->
context, expr);
41 std::unordered_map<triton::ast::SharedAbstractNode, z3::expr> results;
45 for (
auto&& n : nodes) {
46 results.insert(std::make_pair(n, this->do_convert(n, &results)));
49 return results.at(node);
58 std::vector<z3::expr> children;
59 for (
auto&& n : node->getChildren()) {
60 children.emplace_back(results->at(n));
63 switch (node->getType()) {
66 auto size = triton::ast::getInteger<triton::uint32>(node->getChildren()[0]);
67 auto isort = this->
context.bv_sort(size);
68 auto value = this->
context.bv_val(0, 8);
69 return to_expr(this->
context, Z3_mk_const_array(this->
context, isort, value));
73 auto bvsize = node->getBitvectorSize();
74 auto retval = to_expr(this->
context, Z3_mk_bvand(this->
context, children[0], this->
context.bv_val(0xff, bvsize)));
79 to_expr(this->
context, Z3_mk_bvlshr(this->
context, children[0], this->
context.bv_val(index, bvsize))),
80 this->context.bv_val(0xff, bvsize)
84 return to_expr(this->
context, retval);
88 return to_expr(this->
context, Z3_mk_bvadd(this->
context, children[0], children[1]));
91 return to_expr(this->
context, Z3_mk_bvand(this->
context, children[0], children[1]));
94 return to_expr(this->
context, Z3_mk_bvashr(this->
context, children[0], children[1]));
97 return to_expr(this->
context, Z3_mk_bvlshr(this->
context, children[0], children[1]));
100 return to_expr(this->
context, Z3_mk_bvmul(this->
context, children[0], children[1]));
103 return to_expr(this->
context, Z3_mk_bvnand(this->
context, children[0], children[1]));
106 return to_expr(this->
context, Z3_mk_bvneg(this->
context, children[0]));
109 return to_expr(this->
context, Z3_mk_bvnor(this->
context, children[0], children[1]));
112 return to_expr(this->
context, Z3_mk_bvnot(this->
context, children[0]));
115 return to_expr(this->
context, Z3_mk_bvor(this->
context, children[0], children[1]));
118 triton::uint32 rot = triton::ast::getInteger<triton::uint32>(node->getChildren()[1]);
119 return to_expr(this->
context, Z3_mk_rotate_left(this->
context, rot, children[0]));
123 triton::uint32 rot = triton::ast::getInteger<triton::uint32>(node->getChildren()[1]);
124 return to_expr(this->
context, Z3_mk_rotate_right(this->
context, rot, children[0]));
128 return to_expr(this->
context, Z3_mk_bvsdiv(this->
context, children[0], children[1]));
131 return to_expr(this->
context, Z3_mk_bvsge(this->
context, children[0], children[1]));
134 return to_expr(this->
context, Z3_mk_bvsgt(this->
context, children[0], children[1]));
137 return to_expr(this->
context, Z3_mk_bvshl(this->
context, children[0], children[1]));
140 return to_expr(this->
context, Z3_mk_bvsle(this->
context, children[0], children[1]));
143 return to_expr(this->
context, Z3_mk_bvslt(this->
context, children[0], children[1]));
146 return to_expr(this->
context, Z3_mk_bvsmod(this->
context, children[0], children[1]));
149 return to_expr(this->
context, Z3_mk_bvsrem(this->
context, children[0], children[1]));
152 return to_expr(this->
context, Z3_mk_bvsub(this->
context, children[0], children[1]));
155 return to_expr(this->
context, Z3_mk_bvudiv(this->
context, children[0], children[1]));
158 return to_expr(this->
context, Z3_mk_bvuge(this->
context, children[0], children[1]));
161 return to_expr(this->
context, Z3_mk_bvugt(this->
context, children[0], children[1]));
164 return to_expr(this->
context, Z3_mk_bvule(this->
context, children[0], children[1]));
167 return to_expr(this->
context, Z3_mk_bvult(this->
context, children[0], children[1]));
170 return to_expr(this->
context, Z3_mk_bvurem(this->
context, children[0], children[1]));
173 return to_expr(this->
context, Z3_mk_bvxnor(this->
context, children[0], children[1]));
176 return to_expr(this->
context, Z3_mk_bvxor(this->
context, children[0], children[1]));
179 return this->
context.bv_val(this->getStringValue(children[0]).c_str(), children[1].get_numeral_uint());
182 z3::expr currentValue = children[0];
183 z3::expr nextValue(this->
context);
187 nextValue = children[idx];
188 currentValue = to_expr(this->
context, Z3_mk_concat(this->
context, currentValue, nextValue));
195 z3::expr op1 = children[0];
196 z3::expr op2 = children[1];
197 Z3_ast ops[] = {op1, op2};
199 return to_expr(this->
context, Z3_mk_distinct(this->
context, 2, ops));
203 return to_expr(this->
context, Z3_mk_eq(this->
context, children[0], children[1]));
206 return to_expr(this->
context, Z3_mk_extract(this->
context, children[0].get_numeral_uint(), children[1].get_numeral_uint(), children[2]));
210 Z3_app* vars =
new Z3_app[size];
213 vars[i] = children[i];
216 z3::expr e = to_expr(this->
context, Z3_mk_forall_const(this->
context, 0, size, vars, 0, 0, children[size]));
223 z3::expr op1 = children[0];
224 z3::expr op2 = children[1];
226 return to_expr(this->
context, Z3_mk_iff(this->
context, op1, op2));
230 std::string value(triton::ast::getInteger<std::string>(node));
231 return this->
context.int_val(value.c_str());
235 z3::expr op1 = children[0];
236 z3::expr op2 = children[1];
237 z3::expr op3 = children[2];
239 return to_expr(this->
context, Z3_mk_ite(this->
context, op1, op2, op3));
243 z3::expr currentValue = children[0];
244 z3::expr nextValue(this->
context);
247 nextValue = children[idx];
248 Z3_ast ops[] = {currentValue, nextValue};
249 currentValue = to_expr(this->
context, Z3_mk_and(this->
context, 2, ops));
263 z3::expr value = children[0];
268 z3::expr currentValue = children[0];
269 z3::expr nextValue(this->
context);
272 nextValue = children[idx];
273 Z3_ast ops[] = {currentValue, nextValue};
274 currentValue = to_expr(this->
context, Z3_mk_or(this->
context, 2, ops));
281 z3::expr currentValue = children[0];
282 z3::expr nextValue(this->
context);
285 nextValue = children[idx];
286 currentValue = to_expr(this->
context, Z3_mk_xor(this->
context, currentValue, nextValue));
301 return results->at(this->
symbols[value]);
305 return to_expr(this->
context, Z3_mk_select(this->
context, children[0], children[1]));
308 return to_expr(this->
context, Z3_mk_store(this->
context, children[0], children[1], children[2]));
311 return to_expr(this->
context, Z3_mk_sign_ext(this->
context, children[0].get_numeral_uint(), children[1]));
317 this->
variables[symVar->getName()] = symVar;
326 return this->
context.bv_const(symVar->getName().c_str(), symVar->getSize());
330 return to_expr(this->
context, Z3_mk_zero_ext(this->
context, children[0].get_numeral_uint(), children[1]));
TRITON_EXPORT std::vector< SharedAbstractNode > & getChildren(void)
Returns the children of the node.
std::unordered_map< std::string, triton::engines::symbolic::SharedSymbolicVariable > variables
The set of symbolic variables contained in the expression.
TRITON_EXPORT ~TritonToZ3()
Destructor.
TRITON_EXPORT TritonToZ3(bool eval=true)
Constructor.
std::unordered_map< std::string, triton::ast::SharedAbstractNode > symbols
The map of symbols. E.g: (let (symbols expr1) expr2)
z3::context context
The z3's context.
TRITON_EXPORT z3::expr convert(const triton::ast::SharedAbstractNode &node)
Converts to Z3's AST.
The exception class used by all AST lifting (e.g z3 <-> triton).
std::vector< SharedAbstractNode > childrenExtraction(const SharedAbstractNode &node, bool unroll, bool revert)
Returns node and all its children of an AST sorted topologically. If unroll is true,...
std::shared_ptr< triton::ast::AbstractNode > SharedAbstractNode
Shared Abstract Node.
constexpr triton::uint32 byte
byte size in bit
std::shared_ptr< triton::engines::symbolic::SymbolicVariable > SharedSymbolicVariable
Shared Symbolic variable.
std::uint32_t uint32
unisgned 32-bits
std::string toString(const T &obj)
Converts an object to a string.