libTriton  version 0.9 build 1504
symbolicExpression.cpp
Go to the documentation of this file.
1 /*
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>
16 #include <triton/symbolicEnums.hpp>
18 #include <triton/tritonTypes.hpp>
19 
20 
21 
22 namespace triton {
23  namespace engines {
24  namespace symbolic {
25 
27  : originMemory(),
28  originRegister() {
29  this->ast = node;
30  this->comment = comment;
31  this->id = id;
32  this->isTainted = false;
33  this->type = type;
34  }
35 
36 
38  this->ast = other.ast;
39  this->comment = other.comment;
40  this->id = other.id;
41  this->isTainted = other.isTainted;
42  this->originMemory = other.originMemory;
43  this->originRegister = other.originRegister;
44  this->type = other.type;
45  }
46 
47 
49  this->ast = other.ast;
50  this->comment = other.comment;
51  this->id = other.id;
52  this->isTainted = other.isTainted;
53  this->originMemory = other.originMemory;
54  this->originRegister = other.originRegister;
55  this->type = other.type;
56  return *this;
57  }
58 
59 
61  if (this->ast == nullptr)
62  throw triton::exceptions::SymbolicExpression("SymbolicExpression::getAst(): No AST defined.");
63  return this->ast;
64  }
65 
66 
68  if (this->ast == nullptr)
69  throw triton::exceptions::SymbolicExpression("SymbolicExpression::getNewAst(): No AST defined.");
70  return triton::ast::newInstance(this->ast.get());
71  }
72 
73 
74  const std::string& SymbolicExpression::getComment(void) const {
75  return this->comment;
76  }
77 
78 
80  return this->id;
81  }
82 
83 
84  std::string SymbolicExpression::getFormattedId(void) const {
85  if (this->ast == nullptr)
86  throw triton::exceptions::SymbolicExpression("SymbolicExpression::getFormattedId(): No AST defined.");
87 
88  if (ast->getContext()->getRepresentationMode() == triton::ast::representations::SMT_REPRESENTATION)
89  return "ref!" + std::to_string(this->id);
90 
91  else if (ast->getContext()->getRepresentationMode() == triton::ast::representations::PYTHON_REPRESENTATION)
92  return "ref_" + std::to_string(this->id);
93 
94  else
95  throw triton::exceptions::SymbolicExpression("SymbolicExpression::getFormattedId(): Invalid AST representation mode.");
96  }
97 
98 
99  std::string SymbolicExpression::getFormattedComment(void) const {
100  if (this->ast == nullptr)
101  throw triton::exceptions::SymbolicExpression("SymbolicExpression::getFormattedComment(): No AST defined.");
102 
103  if (this->getComment().empty())
104  return "";
105 
106  else if (ast->getContext()->getRepresentationMode() == triton::ast::representations::SMT_REPRESENTATION)
107  return "; " + this->getComment();
108 
109  else if (ast->getContext()->getRepresentationMode() == triton::ast::representations::PYTHON_REPRESENTATION)
110  return "# " + this->getComment();
111 
112  else
113  throw triton::exceptions::SymbolicExpression("SymbolicExpression::getFormattedComment(): Invalid AST representation mode.");
114  }
115 
116 
118  std::ostringstream stream;
119 
120  if (this->ast == nullptr)
121  throw triton::exceptions::SymbolicExpression("SymbolicExpression::getFormattedExpression(): No AST defined.");
122 
123  else if (ast->getContext()->getRepresentationMode() == triton::ast::representations::SMT_REPRESENTATION) {
124  stream << "(define-fun " << this->getFormattedId() << " () (_ BitVec " << std::dec << this->getAst()->getBitvectorSize() << ") " << this->getAst() << ")";
125  if (!this->getComment().empty())
126  stream << " " << this->getFormattedComment();
127  return stream.str();
128  }
129 
130  else if (ast->getContext()->getRepresentationMode() == triton::ast::representations::PYTHON_REPRESENTATION) {
131  stream << this->getFormattedId() << " = " << this->getAst();
132  if (!this->getComment().empty())
133  stream << " " << this->getFormattedComment();
134  return stream.str();
135  }
136 
137  else
138  throw triton::exceptions::SymbolicExpression("SymbolicExpression::getFormattedExpression(): Invalid AST representation mode.");
139  }
140 
141 
143  return this->type;
144  }
145 
146 
148  return this->originMemory;
149  }
150 
151 
153  return this->originRegister;
154  }
155 
156 
159 
160  /* If node is the same as the old one, just do not set the ast. */
161  if (node == old)
162  return;
163 
164  if (old) {
165  /* Link old parents with the new node */
166  for (auto sp : old->getParents()) {
167  node->setParent(sp.get());
168  }
169  }
170 
171  /* Set the new ast */
172  this->ast = node;
173 
174  /* Do not init parents if the new node has same properties that the old one */
175  if (!old || !old->canReplaceNodeWithoutUpdate(ast)) {
176  this->ast->initParents();
177  }
178  }
179 
180 
181  void SymbolicExpression::setComment(const std::string& comment) {
182  this->comment = comment;
183  }
184 
185 
187  this->type = type;
188  }
189 
190 
192  this->originMemory = mem;
193  }
194 
195 
197  this->originRegister = reg;
198  }
199 
200 
203  }
204 
205 
206  bool SymbolicExpression::isMemory(void) const {
208  }
209 
210 
212  if (this->ast == nullptr)
213  return false;
214  return this->ast->isSymbolized();
215  }
216 
217 
218  std::ostream& operator<<(std::ostream& stream, const SymbolicExpression& symExpr) {
219  stream << symExpr.getFormattedExpression();
220  return stream;
221  }
222 
223 
224  std::ostream& operator<<(std::ostream& stream, const SymbolicExpression* symExpr) {
225  stream << *symExpr;
226  return stream;
227  }
228 
229  }; /* symbolic namespace */
230  }; /* engines namespace */
231 }; /*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::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 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::REG, REG_INVALID otherwise...
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::MEM,...
TRITON_EXPORT void setComment(const std::string &comment)
Sets a comment to the symbolic expression.
TRITON_EXPORT bool isSymbolized(void) const
Returns true if the expression contains a symbolic variable.
triton::arch::Register originRegister
The origin register if kind is equal to triton::engines::symbolic::REG, REG_INVALID otherwise.
TRITON_EXPORT void setOriginRegister(const triton::arch::Register &reg)
Sets the origin register.
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 kind 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.
std::string comment
The comment of the symbolic expression.
TRITON_EXPORT SymbolicExpression & operator=(const SymbolicExpression &other)
Operator.
The exception class used by symbolic expressions.
Definition: exceptions.hpp:89
std::shared_ptr< triton::ast::AbstractNode > SharedAbstractNode
Shared Abstract Node.
Definition: ast.hpp:57
SharedAbstractNode newInstance(AbstractNode *node, bool unroll)
AST C++ API - Duplicates the AST.
Definition: ast.cpp:3156
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.
Definition: tritonTypes.hpp:67
The Triton namespace.
Definition: api.cpp:227