libTriton version 1.0 build 1592
Loading...
Searching...
No Matches
astContext.hpp
Go to the documentation of this file.
1
2/*
3** Copyright (C) - Triton
4**
5** This program is under the terms of the Apache License 2.0.
6*/
7
8#ifndef TRITON_AST_CONTEXT_H
9#define TRITON_AST_CONTEXT_H
10
11#include <deque>
12#include <list>
13#include <memory>
14#include <unordered_map>
15#include <vector>
16
17#include <triton/ast.hpp>
19#include <triton/dllexport.hpp>
20#include <triton/exceptions.hpp>
21#include <triton/modes.hpp>
22
23
24
26namespace triton {
32 /* Forward declarations */
33 namespace engines {
34 namespace symbolic {
35 class SymbolicExpression;
36 using SharedSymbolicExpression = std::shared_ptr<triton::engines::symbolic::SymbolicExpression>;
37
38 class SymbolicVariable;
39 using SharedSymbolicVariable = std::shared_ptr<triton::engines::symbolic::SymbolicVariable>;
40 };
41 };
42
44 namespace ast {
52
53 class AstContext : public std::enable_shared_from_this<AstContext> {
54 private:
57
60
62 std::unordered_map<std::string, std::pair<triton::ast::WeakAbstractNode, triton::uint512>> valueMapping;
63
65 std::deque<SharedAbstractNode> nodes;
66
68 SharedAbstractNode simplify_concat(std::vector<SharedAbstractNode> exprs);
69
71 SharedAbstractNode simplify_extract(triton::uint32 high, triton::uint32 low, const SharedAbstractNode& expr);
72
73 public:
75 TRITON_EXPORT AstContext(const triton::modes::SharedModes& modes);
76
78 TRITON_EXPORT ~AstContext();
79
81 TRITON_EXPORT AstContext& operator=(const AstContext& other);
82
84 TRITON_EXPORT SharedAbstractNode collect(const SharedAbstractNode& node);
85
87 TRITON_EXPORT void garbage(void);
88
90 TRITON_EXPORT SharedAbstractNode array(triton::uint32 addrSize);
91
93 TRITON_EXPORT SharedAbstractNode assert_(const SharedAbstractNode& expr);
94
96 TRITON_EXPORT SharedAbstractNode bswap(const SharedAbstractNode& expr);
97
99 TRITON_EXPORT SharedAbstractNode bv(const triton::uint512& value, triton::uint32 size);
100
102 TRITON_EXPORT SharedAbstractNode bvadd(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
103
105 TRITON_EXPORT SharedAbstractNode bvand(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
106
108 TRITON_EXPORT SharedAbstractNode bvashr(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
109
111 TRITON_EXPORT SharedAbstractNode bvfalse(void);
112
114 TRITON_EXPORT SharedAbstractNode bvlshr(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
115
117 TRITON_EXPORT SharedAbstractNode bvmul(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
118
120 TRITON_EXPORT SharedAbstractNode bvnand(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
121
123 TRITON_EXPORT SharedAbstractNode bvneg(const SharedAbstractNode& expr);
124
126 TRITON_EXPORT SharedAbstractNode bvnor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
127
129 TRITON_EXPORT SharedAbstractNode bvnot(const SharedAbstractNode& expr);
130
132 TRITON_EXPORT SharedAbstractNode bvor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
133
135 TRITON_EXPORT SharedAbstractNode bvrol(const SharedAbstractNode& expr, triton::uint32 rot);
136
138 TRITON_EXPORT SharedAbstractNode bvrol(const SharedAbstractNode& expr, const SharedAbstractNode& rot);
139
141 TRITON_EXPORT SharedAbstractNode bvror(const SharedAbstractNode& expr, triton::uint32 rot);
142
144 TRITON_EXPORT SharedAbstractNode bvror(const SharedAbstractNode& expr, const SharedAbstractNode& rot);
145
147 TRITON_EXPORT SharedAbstractNode bvsdiv(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
148
150 TRITON_EXPORT SharedAbstractNode bvsge(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
151
153 TRITON_EXPORT SharedAbstractNode bvsgt(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
154
156 TRITON_EXPORT SharedAbstractNode bvshl(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
157
159 TRITON_EXPORT SharedAbstractNode bvsle(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
160
162 TRITON_EXPORT SharedAbstractNode bvslt(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
163
165 TRITON_EXPORT SharedAbstractNode bvsmod(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
166
168 TRITON_EXPORT SharedAbstractNode bvsrem(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
169
171 TRITON_EXPORT SharedAbstractNode bvsub(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
172
174 TRITON_EXPORT SharedAbstractNode bvtrue(void);
175
177 TRITON_EXPORT SharedAbstractNode bvudiv(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
178
180 TRITON_EXPORT SharedAbstractNode bvuge(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
181
183 TRITON_EXPORT SharedAbstractNode bvugt(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
184
186 TRITON_EXPORT SharedAbstractNode bvule(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
187
189 TRITON_EXPORT SharedAbstractNode bvult(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
190
192 TRITON_EXPORT SharedAbstractNode bvurem(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
193
195 TRITON_EXPORT SharedAbstractNode bvxnor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
196
198 TRITON_EXPORT SharedAbstractNode bvxor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
199
201 template <typename T> SharedAbstractNode compound(const T& exprs) {
202 SharedAbstractNode node = std::make_shared<CompoundNode>(exprs, this->shared_from_this());
203 if (node == nullptr)
204 throw triton::exceptions::Ast("Node builders - Not enough memory");
205 node->init();
206 return this->collect(node);
207 }
208
210 TRITON_EXPORT SharedAbstractNode concat(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
211
213 template <typename T> SharedAbstractNode concat(const T& exprs) {
214 /* Do not concat if there is only one element */
215 if (exprs.size() == 1) {
216 return exprs.front();
217 }
218
219 /* Allocate node */
220 SharedAbstractNode node = std::make_shared<ConcatNode>(exprs, this->shared_from_this());
221 if (node == nullptr)
222 throw triton::exceptions::Ast("Node builders - Not enough memory");
223 node->init();
224
225 if (this->modes->isModeEnabled(triton::modes::CONSTANT_FOLDING)) {
226 if (node->isSymbolized() == false) {
227 return this->bv(node->evaluate(), node->getBitvectorSize());
228 }
229 }
230
231 if (this->modes->isModeEnabled(triton::modes::AST_OPTIMIZATIONS)) {
232 /* Optimization: concatenate extractions in one if possible */
233 auto n = this->simplify_concat(std::vector<SharedAbstractNode>(exprs.begin(), exprs.end()));
234 if (n) {
235 return n;
236 }
237 }
238
239 return this->collect(node);
240 }
241
243 TRITON_EXPORT SharedAbstractNode declare(const SharedAbstractNode& var);
244
246 TRITON_EXPORT SharedAbstractNode distinct(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
247
249 TRITON_EXPORT SharedAbstractNode equal(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
250
252 TRITON_EXPORT SharedAbstractNode extract(triton::uint32 high, triton::uint32 low, const SharedAbstractNode& expr);
253
255 template <typename T> SharedAbstractNode forall(const T& vars, const SharedAbstractNode& body) {
256 SharedAbstractNode node = std::make_shared<ForallNode>(vars, body);
257 if (node == nullptr)
258 throw triton::exceptions::Ast("Node builders - Not enough memory");
259 node->init();
260 return this->collect(node);
261 }
262
264 TRITON_EXPORT SharedAbstractNode iff(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
265
267 TRITON_EXPORT SharedAbstractNode integer(const triton::uint512& value);
268
270 TRITON_EXPORT SharedAbstractNode ite(const SharedAbstractNode& ifExpr, const SharedAbstractNode& thenExpr, const SharedAbstractNode& elseExpr);
271
273 TRITON_EXPORT SharedAbstractNode land(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
274
276 template <typename T> SharedAbstractNode land(const T& exprs) {
277 SharedAbstractNode node = std::make_shared<LandNode>(exprs, this->shared_from_this());
278 if (node == nullptr)
279 throw triton::exceptions::Ast("Node builders - Not enough memory");
280 node->init();
281 return this->collect(node);
282 }
283
285 TRITON_EXPORT SharedAbstractNode let(std::string alias, const SharedAbstractNode& expr2, const SharedAbstractNode& expr3);
286
288 TRITON_EXPORT SharedAbstractNode lnot(const SharedAbstractNode& expr);
289
291 TRITON_EXPORT SharedAbstractNode lor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
292
294 template <typename T> SharedAbstractNode lor(const T& exprs) {
295 SharedAbstractNode node = std::make_shared<LorNode>(exprs, this->shared_from_this());
296 if (node == nullptr)
297 throw triton::exceptions::Ast("Node builders - Not enough memory");
298 node->init();
299 return this->collect(node);
300 }
301
303 TRITON_EXPORT SharedAbstractNode lxor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
304
306 template <typename T> SharedAbstractNode lxor(const T& exprs) {
307 SharedAbstractNode node = std::make_shared<LxorNode>(exprs, this->shared_from_this());
308 if (node == nullptr)
309 throw triton::exceptions::Ast("Node builders - Not enough memory");
310 node->init();
311 return this->collect(node);
312 }
313
316
319
321 TRITON_EXPORT SharedAbstractNode select(const SharedAbstractNode& array, const SharedAbstractNode& index);
322
324 TRITON_EXPORT SharedAbstractNode store(const SharedAbstractNode& array, triton::usize index, const SharedAbstractNode& expr);
325
327 TRITON_EXPORT SharedAbstractNode store(const SharedAbstractNode& array, const SharedAbstractNode& index, const SharedAbstractNode& expr);
328
330 TRITON_EXPORT SharedAbstractNode string(std::string value);
331
333 TRITON_EXPORT SharedAbstractNode sx(triton::uint32 sizeExt, const SharedAbstractNode& expr);
334
337
339 TRITON_EXPORT SharedAbstractNode zx(triton::uint32 sizeExt, const SharedAbstractNode& expr);
340
342 TRITON_EXPORT void initVariable(const std::string& name, const triton::uint512& value, const SharedAbstractNode& node);
343
345 TRITON_EXPORT void updateVariable(const std::string& name, const triton::uint512& value);
346
348 SharedAbstractNode getVariableNode(const std::string& name);
349
351 TRITON_EXPORT triton::uint16 getArraySize(void) const;
352
354 TRITON_EXPORT const triton::uint512& getVariableValue(const std::string& name) const;
355
358
361
363 TRITON_EXPORT std::ostream& print(std::ostream& stream, AbstractNode* node);
364 };
365
367 using SharedAstContext = std::shared_ptr<triton::ast::AstContext>;
368
370 };
372};
373
374#endif /* TRITON_AST_CONTEXT_H */
Abstract node.
Definition ast.hpp:68
AST Context - Used as AST builder.
TRITON_EXPORT void setRepresentationMode(triton::ast::representations::mode_e mode)
Sets the representation mode for this astContext.
TRITON_EXPORT triton::uint16 getArraySize(void) const
Returns the address space used for the ABV logic.
TRITON_EXPORT SharedAbstractNode bvsgt(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsgt node builder.
TRITON_EXPORT SharedAbstractNode bvmul(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvmul node builder.
TRITON_EXPORT SharedAbstractNode bvnand(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvnand node builder.
TRITON_EXPORT SharedAbstractNode bvsub(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsub node builder.
TRITON_EXPORT SharedAbstractNode distinct(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - distinct node builder.
TRITON_EXPORT SharedAbstractNode land(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - land node builder.
TRITON_EXPORT void garbage(void)
Garbage unused nodes.
SharedAbstractNode concat(const T &exprs)
AST C++ API - concat node builder.
TRITON_EXPORT SharedAbstractNode bvuge(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvuge node builder.
TRITON_EXPORT SharedAbstractNode bvshl(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvshl node builder.
SharedAbstractNode compound(const T &exprs)
AST C++ API - compound node builder.
TRITON_EXPORT SharedAbstractNode iff(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - iff node builder.
SharedAbstractNode land(const T &exprs)
AST C++ API - land node builder.
TRITON_EXPORT SharedAbstractNode bvfalse(void)
AST C++ API - bvfalse node builder.
TRITON_EXPORT SharedAbstractNode bvxnor(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvxnor node builder.
TRITON_EXPORT SharedAbstractNode bvor(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvor node builder.
TRITON_EXPORT SharedAbstractNode bvrol(const SharedAbstractNode &expr, triton::uint32 rot)
AST C++ API - bvrol node builder.
TRITON_EXPORT ~AstContext()
Destructor.
TRITON_EXPORT AstContext & operator=(const AstContext &other)
Operator.
SharedAbstractNode lor(const T &exprs)
AST C++ API - lor node builder.
TRITON_EXPORT AstContext(const triton::modes::SharedModes &modes)
Constructor.
TRITON_EXPORT SharedAbstractNode bvule(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvule node builder.
TRITON_EXPORT SharedAbstractNode store(const SharedAbstractNode &array, triton::usize index, const SharedAbstractNode &expr)
AST C++ API - store node builder.
TRITON_EXPORT SharedAbstractNode concat(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - concat node builder.
SharedAbstractNode forall(const T &vars, const SharedAbstractNode &body)
AST C++ API - forall node builder.
TRITON_EXPORT SharedAbstractNode bvand(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvand node builder.
TRITON_EXPORT SharedAbstractNode bvudiv(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvudiv node builder.
TRITON_EXPORT SharedAbstractNode string(std::string value)
AST C++ API - string node builder.
SharedAbstractNode lxor(const T &exprs)
AST C++ API - lxor node builder.
TRITON_EXPORT SharedAbstractNode bvxor(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvxor node builder.
TRITON_EXPORT SharedAbstractNode reference(const triton::engines::symbolic::SharedSymbolicExpression &expr)
AST C++ API - reference node builder.
TRITON_EXPORT SharedAbstractNode zx(triton::uint32 sizeExt, const SharedAbstractNode &expr)
AST C++ API - zx node builder.
TRITON_EXPORT SharedAbstractNode bswap(const SharedAbstractNode &expr)
AST C++ API - bswap node builder.
TRITON_EXPORT SharedAbstractNode sx(triton::uint32 sizeExt, const SharedAbstractNode &expr)
AST C++ API - sx node builder.
TRITON_EXPORT SharedAbstractNode bvsge(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsge node builder.
TRITON_EXPORT SharedAbstractNode bvsrem(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsrem node builder.
TRITON_EXPORT SharedAbstractNode bvtrue(void)
AST C++ API - bvtrue node builder.
TRITON_EXPORT SharedAbstractNode bv(const triton::uint512 &value, triton::uint32 size)
AST C++ API - bv node builder.
TRITON_EXPORT SharedAbstractNode bvult(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvult node builder.
TRITON_EXPORT SharedAbstractNode bvashr(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvashr node builder.
TRITON_EXPORT SharedAbstractNode bvsdiv(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsdiv node builder.
TRITON_EXPORT triton::ast::representations::mode_e getRepresentationMode(void) const
Gets the representations mode of this astContext.
SharedAbstractNode getVariableNode(const std::string &name)
Gets a variable node from its name.
TRITON_EXPORT SharedAbstractNode lor(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - lor node builder.
TRITON_EXPORT SharedAbstractNode bvnor(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvnor node builder.
TRITON_EXPORT SharedAbstractNode bvsle(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsle node builder.
TRITON_EXPORT SharedAbstractNode lxor(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - lxor node builder.
TRITON_EXPORT SharedAbstractNode variable(const triton::engines::symbolic::SharedSymbolicVariable &symVar)
AST C++ API - variable node builder.
TRITON_EXPORT SharedAbstractNode bvlshr(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvlshr node builder.
TRITON_EXPORT std::ostream & print(std::ostream &stream, AbstractNode *node)
Prints the node according to the current representation mode.
TRITON_EXPORT void updateVariable(const std::string &name, const triton::uint512 &value)
Updates a variable value in this context.
TRITON_EXPORT SharedAbstractNode ite(const SharedAbstractNode &ifExpr, const SharedAbstractNode &thenExpr, const SharedAbstractNode &elseExpr)
AST C++ API - ite node builder.
TRITON_EXPORT SharedAbstractNode assert_(const SharedAbstractNode &expr)
AST C++ API - assert node builder.
TRITON_EXPORT SharedAbstractNode lnot(const SharedAbstractNode &expr)
AST C++ API - lnot node builder.
TRITON_EXPORT SharedAbstractNode select(const SharedAbstractNode &array, triton::usize index)
AST C++ API - select node builder.
TRITON_EXPORT SharedAbstractNode bvneg(const SharedAbstractNode &expr)
AST C++ API - bvneg node builder.
TRITON_EXPORT SharedAbstractNode collect(const SharedAbstractNode &node)
Collect new nodes.
TRITON_EXPORT const triton::uint512 & getVariableValue(const std::string &name) const
Gets a variable value from its name.
TRITON_EXPORT SharedAbstractNode bvugt(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvugt node builder.
TRITON_EXPORT SharedAbstractNode bvslt(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvslt node builder.
TRITON_EXPORT SharedAbstractNode equal(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - equal node builder.
TRITON_EXPORT SharedAbstractNode integer(const triton::uint512 &value)
AST C++ API - integer node builder.
TRITON_EXPORT SharedAbstractNode bvror(const SharedAbstractNode &expr, triton::uint32 rot)
AST C++ API - bvror node builder.
TRITON_EXPORT SharedAbstractNode array(triton::uint32 addrSize)
AST C++ API - array node builder.
TRITON_EXPORT SharedAbstractNode bvadd(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvadd node builder.
TRITON_EXPORT SharedAbstractNode let(std::string alias, const SharedAbstractNode &expr2, const SharedAbstractNode &expr3)
AST C++ API - let node builder.
TRITON_EXPORT void initVariable(const std::string &name, const triton::uint512 &value, const SharedAbstractNode &node)
Initializes a variable in the context.
TRITON_EXPORT SharedAbstractNode extract(triton::uint32 high, triton::uint32 low, const SharedAbstractNode &expr)
AST C++ API - extract node builder.
TRITON_EXPORT SharedAbstractNode bvsmod(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsmod node builder.
TRITON_EXPORT SharedAbstractNode bvurem(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvurem node builder.
TRITON_EXPORT SharedAbstractNode bvnot(const SharedAbstractNode &expr)
AST C++ API - bvnot node builder.
TRITON_EXPORT SharedAbstractNode declare(const SharedAbstractNode &var)
AST C++ API - declare node builder.
The exception class used by all AST nodes.
std::shared_ptr< triton::ast::AbstractNode > SharedAbstractNode
Shared Abstract Node.
Definition ast.hpp:59
std::shared_ptr< triton::ast::AstContext > SharedAstContext
Shared AST context.
Definition ast.hpp:65
std::shared_ptr< triton::modes::Modes > SharedModes
Shared Modes.
Definition modes.hpp:66
@ CONSTANT_FOLDING
[symbolic] Perform a constant folding optimization of sub ASTs which do not contain symbolic variable...
@ AST_OPTIMIZATIONS
[AST] Classical arithmetic optimisations to reduce the depth of the trees.
mode_e
All types of representation mode.
Definition astEnums.hpp:98
std::shared_ptr< triton::engines::symbolic::SymbolicExpression > SharedSymbolicExpression
Shared Symbolic Expression.
Definition ast.hpp:40
std::shared_ptr< triton::engines::symbolic::SymbolicVariable > SharedSymbolicVariable
Shared Symbolic variable.
Definition ast.hpp:43
std::uint16_t uint16
unisgned 16-bits
std::size_t usize
unsigned MAX_INT 32 or 64 bits according to the CPU.
std::uint32_t uint32
unisgned 32-bits
The Triton namespace.