libTriton version 1.0 build 1590
Loading...
Searching...
No Matches
symbolicExpression.cpp
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#include <iosfwd>
9#include <string>
10#include <sstream>
11
12#include <triton/ast.hpp>
13#include <triton/astContext.hpp>
15#include <triton/exceptions.hpp>
19
20
21
22namespace triton {
23 namespace engines {
24 namespace symbolic {
25
27 : originMemory(),
28 originRegister() {
29 this->ast = node;
30 this->comment = comment;
31 this->address = -1;
32 this->id = id;
33 this->isTainted = false;
34 this->type = type;
35 }
36
37
39 this->ast = other.ast;
40 this->comment = other.comment;
41 this->id = other.id;
42 this->isTainted = other.isTainted;
43 this->originMemory = other.originMemory;
44 this->originRegister = other.originRegister;
45 this->type = other.type;
46 this->address = other.address;
47 }
48
49
51 this->ast = other.ast;
52 this->comment = other.comment;
53 this->id = other.id;
54 this->isTainted = other.isTainted;
55 this->originMemory = other.originMemory;
56 this->originRegister = other.originRegister;
57 this->type = other.type;
58 this->address = other.address;
59 return *this;
60 }
61
62
64 if (this->ast == nullptr)
65 throw triton::exceptions::SymbolicExpression("SymbolicExpression::getAst(): No AST defined.");
66 return this->ast;
67 }
68
69
71 if (this->ast == nullptr)
72 throw triton::exceptions::SymbolicExpression("SymbolicExpression::getNewAst(): No AST defined.");
73 return triton::ast::newInstance(this->ast.get());
74 }
75
76
77 const std::string& SymbolicExpression::getComment(void) const {
78 return this->comment;
79 }
80
81
83 return this->id;
84 }
85
86
87 std::string SymbolicExpression::getFormattedId(void) const {
88 if (this->ast == nullptr)
89 throw triton::exceptions::SymbolicExpression("SymbolicExpression::getFormattedId(): No AST defined.");
90
91 if (ast->getContext()->getRepresentationMode() == triton::ast::representations::SMT_REPRESENTATION)
92 return "ref!" + std::to_string(this->id);
93
94 else if (ast->getContext()->getRepresentationMode() == triton::ast::representations::PYTHON_REPRESENTATION)
95 return "ref_" + std::to_string(this->id);
96
97 else if (ast->getContext()->getRepresentationMode() == triton::ast::representations::PCODE_REPRESENTATION) {
98 if (this->isMemory()) {
99 auto mem = this->getOriginMemory();
100 std::ostringstream ss;
101 ss << "@[0x" << std::hex << mem.getAddress() << ":" << std::dec << mem.getBitSize() << "]";
102 return ss.str();
103 }
104 else if (this->isRegister()) {
105 auto reg = this->getOriginRegister();
106 return reg.getName() + "_" + std::to_string(this->id);
107 }
108 else {
109 return "tmp_" + std::to_string(this->id);
110 }
111 }
112
113 else
114 throw triton::exceptions::SymbolicExpression("SymbolicExpression::getFormattedId(): Invalid AST representation mode.");
115 }
116
117
119 if (this->ast == nullptr)
120 throw triton::exceptions::SymbolicExpression("SymbolicExpression::getFormattedComment(): No AST defined.");
121
122 if (this->getComment().empty())
123 return "";
124
125 switch (ast->getContext()->getRepresentationMode()) {
128 return "; " + this->getComment();
129
131 return "# " + this->getComment();
132
133 default:
134 throw triton::exceptions::SymbolicExpression("SymbolicExpression::getFormattedComment(): Invalid AST representation mode.");
135 };
136 }
137
138
139 std::string SymbolicExpression::getBitvectorDefine(void) const {
140 std::ostringstream stream;
141 stream << "(define-fun " << this->getFormattedId() << " () (_ BitVec " << std::dec << this->getAst()->getBitvectorSize() << ") " << this->getAst() << ")";
142 return stream.str();
143 }
144
145
146 std::string SymbolicExpression::getArrayDefine(void) const {
147 std::ostringstream stream;
148
149 if (this->getAst()->getType() == triton::ast::ARRAY_NODE) {
150 stream << "(declare-fun " << this->getFormattedId() << " () (Array (_ BitVec " << std::dec << triton::ast::getIndexSize(this->getAst()) << ") (_ BitVec 8)))";
151 }
152 else {
153 stream << "(define-fun " << this->getFormattedId() << " () (Array (_ BitVec " << std::dec << triton::ast::getIndexSize(this->getAst()) << ") (_ BitVec 8)) " << this->getAst() << ")";
154 }
155
156 return stream.str();
157 }
158
159
161 std::ostringstream stream;
162
163 if (this->ast == nullptr)
164 throw triton::exceptions::SymbolicExpression("SymbolicExpression::getFormattedExpression(): No AST defined.");
165
166 switch (ast->getContext()->getRepresentationMode()) {
168 stream << (this->getAst()->isArray() ? this->getArrayDefine() : this->getBitvectorDefine());
169 break;
172 stream << this->getFormattedId() << " = " << this->getAst();
173 break;
174
175 default:
176 throw triton::exceptions::SymbolicExpression("SymbolicExpression::getFormattedExpression(): Invalid AST representation mode.");
177 }
178
179 if (!this->getComment().empty()) {
180 stream << " " << this->getFormattedComment();
181 }
182
183 return stream.str();
184 }
185
186
190
191
195
196
200
201
204
205 /* If node is the same as the old one, just do not set the ast. */
206 if (node == old)
207 return;
208
209 if (old) {
210 /* Link old parents with the new node */
211 for (auto sp : old->getParents()) {
212 node->setParent(sp.get());
213 }
214 }
215
216 /* Set the new ast */
217 this->ast = node;
218
219 /* Do not init parents if the new node has same properties that the old one */
220 if (!old || !old->canReplaceNodeWithoutUpdate(ast)) {
221 this->ast->initParents();
222 }
223 }
224
225
226 void SymbolicExpression::setComment(const std::string& comment) {
227 this->comment = comment;
228 }
229
230
232 this->address = address;
233 }
234
235
237 return this->address;
238 }
239
240
244
245
249
250
254
255
259
260
264
265
267 if (this->ast == nullptr)
268 return false;
269 return this->ast->isSymbolized();
270 }
271
272
273 void SymbolicExpression::writeBackDisassembly(const std::string& disassembly) {
274 this->disassembly = disassembly;
275 }
276
277
278 const std::string& SymbolicExpression::getDisassembly(void) {
279 return this->disassembly;
280 }
281
282
283 std::ostream& operator<<(std::ostream& stream, const SymbolicExpression& symExpr) {
284 stream << symExpr.getFormattedExpression();
285 return stream;
286 }
287
288
289 std::ostream& operator<<(std::ostream& stream, const SymbolicExpression* symExpr) {
290 stream << *symExpr;
291 return stream;
292 }
293
294 }; /* symbolic namespace */
295 }; /* engines namespace */
296}; /*triton namespace */
This class is used to represent a memory access.
This class is used when an instruction has a register operand.
Definition register.hpp:44
TRITON_EXPORT bool isMemory(void) const
Returns true if the symbolic expression is assigned to a memory.
TRITON_EXPORT triton::usize getId(void) const
Returns the symbolic expression id.
TRITON_EXPORT triton::uint64 getAddress(void) const
Get the address of the symbolic expression, if any.
TRITON_EXPORT const std::string & getDisassembly(void)
Gets the instruction disassembly where the symbolic expression comes from.
triton::usize id
The symbolic expression id. This id is unique.
TRITON_EXPORT SymbolicExpression(const triton::ast::SharedAbstractNode &node, triton::usize id, triton::engines::symbolic::expression_e type, const std::string &comment="")
Constructor.
TRITON_EXPORT std::string getFormattedId(void) const
Returns the id as string of the symbolic expression according the mode of the AST representation.
TRITON_EXPORT std::string getFormattedComment(void) const
Returns the comment as string of the symbolic expression according the mode of the AST representation...
TRITON_EXPORT void setAddress(triton::uint64 address)
Sets the symbolic expression address.
TRITON_EXPORT void setAst(const triton::ast::SharedAbstractNode &node)
Sets a root node.
TRITON_EXPORT void setType(triton::engines::symbolic::expression_e type)
Sets the kind of the symbolic expression.
TRITON_EXPORT const triton::ast::SharedAbstractNode & getAst(void) const
Returns the SMT AST root node of the symbolic expression. This is the semantics.
TRITON_EXPORT std::string getFormattedExpression(void) const
Returns the symbolic expression representation as string according the mode of the AST representation...
TRITON_EXPORT const triton::arch::Register & getOriginRegister(void) const
Returns the origin register if kind is equal to triton::engines::symbolic::REGISTER_EXPRESSION,...
TRITON_EXPORT const std::string & getComment(void) const
Returns the comment of the symbolic expression.
bool isTainted
True if the symbolic expression is tainted.
TRITON_EXPORT const triton::arch::MemoryAccess & getOriginMemory(void) const
Returns the origin memory access if kind is equal to triton::engines::symbolic::MEMORY_EXPRESSION,...
TRITON_EXPORT void setComment(const std::string &comment)
Sets a comment to the symbolic expression.
std::string disassembly
The instruction disassembly where the symbolic expression comes from.
TRITON_EXPORT bool isSymbolized(void) const
Returns true if the expression contains a symbolic variable.
triton::arch::Register originRegister
The origin register if type is equal to triton::engines::symbolic::REG, REG_INVALID otherwise.
TRITON_EXPORT void setOriginRegister(const triton::arch::Register &reg)
Sets the origin register.
triton::uint64 address
The address of the instruction behind the symbolic expression. -1 if not defined.
TRITON_EXPORT triton::engines::symbolic::expression_e getType(void) const
Returns the type of the symbolic expression assignment.
triton::ast::SharedAbstractNode ast
The root node (AST) of the symbolic expression.
triton::arch::MemoryAccess originMemory
The origin memory address if type is equal to triton::engines::symbolic::MEM, invalid memory otherwis...
TRITON_EXPORT triton::ast::SharedAbstractNode getNewAst(void) const
Returns a new SMT AST root node of the symbolic expression. This new instance is a duplicate of the o...
TRITON_EXPORT bool isRegister(void) const
Returns true if the symbolic expression is assigned to a register.
TRITON_EXPORT void setOriginMemory(const triton::arch::MemoryAccess &mem)
Sets the origin memory acccess.
triton::engines::symbolic::expression_e type
The type of the symbolic expression assignment.
TRITON_EXPORT void writeBackDisassembly(const std::string &disassembly)
Writes back the instruction disassembly where the symbolic expression comes from.
std::string comment
The comment of the symbolic expression.
TRITON_EXPORT SymbolicExpression & operator=(const SymbolicExpression &other)
Operator.
The exception class used by symbolic expressions.
triton::uint32 getIndexSize(const SharedAbstractNode &node)
Gets the index size of an array.
Definition ast.cpp:3755
std::shared_ptr< triton::ast::AbstractNode > SharedAbstractNode
Shared Abstract Node.
Definition ast.hpp:59
SharedAbstractNode newInstance(AbstractNode *node, bool unroll)
AST C++ API - Duplicates the AST.
Definition ast.cpp:3604
expression_e
Type of symbolic expressions.
std::ostream & operator<<(std::ostream &stream, const SymbolicExpression &symExpr)
Displays a symbolic expression.
@ REGISTER_EXPRESSION
Assigned to a register expression.
@ MEMORY_EXPRESSION
Assigned to a memory expression.
std::size_t usize
unsigned MAX_INT 32 or 64 bits according to the CPU.
std::uint64_t uint64
unisgned 64-bits
The Triton namespace.