libTriton  version 0.9 build 1502
instruction.hpp
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 #ifndef TRITON_INSTRUCTION_H
9 #define TRITON_INSTRUCTION_H
10 
11 #include <list>
12 #include <map>
13 #include <ostream>
14 #include <set>
15 #include <sstream>
16 #include <utility>
17 #include <vector>
18 
19 #include <triton/archEnums.hpp>
20 #include <triton/ast.hpp>
21 #include <triton/dllexport.hpp>
22 #include <triton/memoryAccess.hpp>
24 #include <triton/register.hpp>
26 #include <triton/tritonTypes.hpp>
27 
28 
29 
31 namespace triton {
38  namespace arch {
48  class Instruction {
49  protected:
52 
55 
57  std::stringstream disassembly;
58 
61 
64 
67 
70 
73 
75  std::set<std::pair<triton::arch::MemoryAccess, triton::ast::SharedAbstractNode>> loadAccess;
76 
78  std::set<std::pair<triton::arch::MemoryAccess, triton::ast::SharedAbstractNode>> storeAccess;
79 
81  std::set<std::pair<triton::arch::Register, triton::ast::SharedAbstractNode>> readRegisters;
82 
84  std::set<std::pair<triton::arch::Register, triton::ast::SharedAbstractNode>> writtenRegisters;
85 
87  std::set<std::pair<triton::arch::Immediate, triton::ast::SharedAbstractNode>> readImmediates;
88 
90  std::set<triton::arch::Register> undefinedRegisters;
91 
93  bool branch;
94 
97 
100 
102  bool tainted;
103 
105  bool writeBack;
106 
109 
111  bool thumb;
112 
113  private:
115  void copy(const Instruction& other);
116 
117  public:
119  std::vector<triton::arch::OperandWrapper> operands;
120 
122  std::vector<triton::engines::symbolic::SharedSymbolicExpression> symbolicExpressions;
123 
125  TRITON_EXPORT Instruction();
126 
128  TRITON_EXPORT Instruction(const triton::uint8* opcode, triton::uint32 opSize);
129 
131  TRITON_EXPORT Instruction(triton::uint64 addr, const triton::uint8* opcode, triton::uint32 opSize);
132 
134  TRITON_EXPORT Instruction(const Instruction& other);
135 
137  TRITON_EXPORT Instruction& operator=(const Instruction& other);
138 
140  TRITON_EXPORT ~Instruction();
141 
143  TRITON_EXPORT triton::uint32 getThreadId(void) const;
144 
146  TRITON_EXPORT void setThreadId(triton::uint32 tid);
147 
149  TRITON_EXPORT triton::uint64 getAddress(void) const;
150 
152  TRITON_EXPORT triton::uint64 getNextAddress(void) const;
153 
155  TRITON_EXPORT void setAddress(triton::uint64 addr);
156 
158  TRITON_EXPORT std::string getDisassembly(void) const;
159 
161  TRITON_EXPORT const triton::uint8* getOpcode(void) const;
162 
164  TRITON_EXPORT triton::uint32 getType(void) const;
165 
167  TRITON_EXPORT triton::arch::x86::prefix_e getPrefix(void) const;
168 
170  TRITON_EXPORT triton::arch::arm::condition_e getCodeCondition(void) const;
171 
173  TRITON_EXPORT std::set<std::pair<triton::arch::MemoryAccess, triton::ast::SharedAbstractNode>>& getLoadAccess(void);
174 
176  TRITON_EXPORT std::set<std::pair<triton::arch::MemoryAccess, triton::ast::SharedAbstractNode>>& getStoreAccess(void);
177 
179  TRITON_EXPORT std::set<std::pair<triton::arch::Register, triton::ast::SharedAbstractNode>>& getReadRegisters(void);
180 
182  TRITON_EXPORT std::set<std::pair<triton::arch::Register, triton::ast::SharedAbstractNode>>& getWrittenRegisters(void);
183 
185  TRITON_EXPORT std::set<std::pair<triton::arch::Immediate, triton::ast::SharedAbstractNode>>& getReadImmediates(void);
186 
188  TRITON_EXPORT std::set<triton::arch::Register>& getUndefinedRegisters(void);
189 
191  TRITON_EXPORT void setOpcode(const triton::uint8* opcode, triton::uint32 size);
192 
194  TRITON_EXPORT triton::uint32 getSize(void) const;
195 
197  TRITON_EXPORT void setLoadAccess(const triton::arch::MemoryAccess& mem, const triton::ast::SharedAbstractNode& node);
198 
200  TRITON_EXPORT void removeLoadAccess(const triton::arch::MemoryAccess& mem);
201 
203  TRITON_EXPORT void setStoreAccess(const triton::arch::MemoryAccess& mem, const triton::ast::SharedAbstractNode& node);
204 
206  TRITON_EXPORT void removeStoreAccess(const triton::arch::MemoryAccess& mem);
207 
209  TRITON_EXPORT void setReadRegister(const triton::arch::Register& reg, const triton::ast::SharedAbstractNode& node);
210 
212  TRITON_EXPORT void removeReadRegister(const triton::arch::Register& reg);
213 
215  TRITON_EXPORT void setWrittenRegister(const triton::arch::Register& reg, const triton::ast::SharedAbstractNode& node);
216 
218  TRITON_EXPORT void removeWrittenRegister(const triton::arch::Register& reg);
219 
221  TRITON_EXPORT void setReadImmediate(const triton::arch::Immediate& imm, const triton::ast::SharedAbstractNode& node);
222 
224  TRITON_EXPORT void removeReadImmediate(const triton::arch::Immediate& imm);
225 
227  TRITON_EXPORT void setUndefinedRegister(const triton::arch::Register& reg);
228 
230  TRITON_EXPORT void removeUndefinedRegister(const triton::arch::Register& reg);
231 
233  TRITON_EXPORT void setSize(triton::uint32 size);
234 
236  TRITON_EXPORT void setType(triton::uint32 type);
237 
239  TRITON_EXPORT void setPrefix(triton::arch::x86::prefix_e prefix);
240 
243 
245  TRITON_EXPORT void setDisassembly(const std::string& str);
246 
248  TRITON_EXPORT void setTaint(bool state);
249 
251  TRITON_EXPORT void setTaint(void);
252 
254  TRITON_EXPORT void setWriteBack(bool state);
255 
257  TRITON_EXPORT void setUpdateFlag(bool state);
258 
260  TRITON_EXPORT void setThumb(bool state);
261 
264 
266  TRITON_EXPORT bool isBranch(void) const;
267 
269  TRITON_EXPORT bool isControlFlow(void) const;
270 
272  TRITON_EXPORT bool isConditionTaken(void) const;
273 
275  TRITON_EXPORT bool isTainted(void) const;
276 
278  TRITON_EXPORT bool isSymbolized(void) const;
279 
281  TRITON_EXPORT bool isMemoryRead(void) const;
282 
284  TRITON_EXPORT bool isMemoryWrite(void) const;
285 
287  TRITON_EXPORT bool isWriteTo(const triton::arch::OperandWrapper& target) const;
288 
290  TRITON_EXPORT bool isReadFrom(const triton::arch::OperandWrapper& target) const;
291 
293  TRITON_EXPORT bool isPrefixed(void) const;
294 
296  TRITON_EXPORT bool isWriteBack(void) const;
297 
299  TRITON_EXPORT bool isUpdateFlag(void) const;
300 
302  TRITON_EXPORT bool isThumb(void) const;
303 
305  TRITON_EXPORT void setBranch(bool flag);
306 
308  TRITON_EXPORT void setControlFlow(bool flag);
309 
311  TRITON_EXPORT void setConditionTaken(bool flag);
312 
314  TRITON_EXPORT void clear(void);
315  };
316 
318  TRITON_EXPORT std::ostream& operator<<(std::ostream& stream, const Instruction& inst);
319 
321  TRITON_EXPORT std::ostream& operator<<(std::ostream& stream, const Instruction* inst);
322 
324  };
326 };
327 
328 #endif /* TRITON_INSTRUCTION_H */
This class is used to represent an immediate.
Definition: immediate.hpp:37
This class is used when to represent an instruction.
Definition: instruction.hpp:48
TRITON_EXPORT void setLoadAccess(const triton::arch::MemoryAccess &mem, const triton::ast::SharedAbstractNode &node)
Sets a load access.
TRITON_EXPORT std::set< std::pair< triton::arch::Register, triton::ast::SharedAbstractNode > > & getReadRegisters(void)
Returns the list of all implicit and explicit register (flags includes) inputs (read)
TRITON_EXPORT Instruction & operator=(const Instruction &other)
Copies an Instruction.
Definition: instruction.cpp:64
TRITON_EXPORT void setUpdateFlag(bool state)
Sets the updateFlag of the instruction.
TRITON_EXPORT triton::uint32 getSize(void) const
Returns the size of the instruction.
TRITON_EXPORT void setDisassembly(const std::string &str)
Sets the disassembly of the instruction.
std::set< std::pair< triton::arch::Immediate, triton::ast::SharedAbstractNode > > readImmediates
Implicit and explicit immediate inputs (read). This field is set at the semantics level.
Definition: instruction.hpp:87
triton::uint8 opcode[32]
The opcode of the instruction.
Definition: instruction.hpp:60
TRITON_EXPORT bool isWriteBack(void) const
Returns true if the instruction performs a write back. Mainly used for AArch64 instructions like LDR.
TRITON_EXPORT void removeLoadAccess(const triton::arch::MemoryAccess &mem)
Removes a load access.
TRITON_EXPORT void setThumb(bool state)
Sets the Thumb mode of the instruction.
TRITON_EXPORT void setConditionTaken(bool flag)
Sets flag to define if the condition is taken or not.
std::stringstream disassembly
The disassembly of the instruction. This field is set at the disassembly level.
Definition: instruction.hpp:57
TRITON_EXPORT bool isWriteTo(const triton::arch::OperandWrapper &target) const
Returns whether the instruction writes the specified operand.
TRITON_EXPORT void setWrittenRegister(const triton::arch::Register &reg, const triton::ast::SharedAbstractNode &node)
Sets a written register.
TRITON_EXPORT const triton::uint8 * getOpcode(void) const
Returns the opcode of the instruction.
triton::uint32 size
The size of the instruction.
Definition: instruction.hpp:63
triton::arch::x86::prefix_e prefix
The prefix of the instruction. This field is set at the disassembly level. Mainly used for X86.
Definition: instruction.hpp:69
bool thumb
True if this is a Thumb instruction. (Note that isThumb() in Arm32Cpu is used to determine the curren...
std::set< std::pair< triton::arch::MemoryAccess, triton::ast::SharedAbstractNode > > storeAccess
Implicit and explicit store access (write). This field is set at the semantics level.
Definition: instruction.hpp:78
triton::arch::arm::condition_e codeCondition
The code condition of the instruction. This field is set at the disassembly level....
Definition: instruction.hpp:72
TRITON_EXPORT bool isThumb(void) const
Returns true if it is a Thumb instruction.
TRITON_EXPORT std::set< std::pair< triton::arch::MemoryAccess, triton::ast::SharedAbstractNode > > & getStoreAccess(void)
Returns the list of all implicit and explicit store access.
TRITON_EXPORT void setType(triton::uint32 type)
Sets the type of the instruction.
TRITON_EXPORT void setPrefix(triton::arch::x86::prefix_e prefix)
Sets the prefix of the instruction (mainly for X86).
TRITON_EXPORT void setAddress(triton::uint64 addr)
Sets the address of the instruction.
TRITON_EXPORT std::set< std::pair< triton::arch::MemoryAccess, triton::ast::SharedAbstractNode > > & getLoadAccess(void)
Returns the list of all implicit and explicit load access.
TRITON_EXPORT void clear(void)
Clears all instruction information.
TRITON_EXPORT void setTaint(void)
Sets the taint of the instruction based on its expressions.
bool writeBack
True if this instruction performs a write back. Mainly used for AArch64 instruction like LDR.
TRITON_EXPORT void setStoreAccess(const triton::arch::MemoryAccess &mem, const triton::ast::SharedAbstractNode &node)
Sets a store access.
TRITON_EXPORT triton::uint32 getType(void) const
Returns the type of the instruction.
TRITON_EXPORT void removeReadRegister(const triton::arch::Register &reg)
Removes a read register.
TRITON_EXPORT bool isConditionTaken(void) const
Returns true if the condition is taken (e.g x86: jcc, cmovcc, setcc, ...).
TRITON_EXPORT bool isReadFrom(const triton::arch::OperandWrapper &target) const
Returns whether the instruction reads the specified operand.
TRITON_EXPORT bool isTainted(void) const
Returns true if at least one of its expressions is tainted.
std::set< std::pair< triton::arch::MemoryAccess, triton::ast::SharedAbstractNode > > loadAccess
Implicit and explicit load access (read). This field is set at the semantics level.
Definition: instruction.hpp:75
triton::uint32 type
The type of the instruction. This field is set at the disassembly level.
Definition: instruction.hpp:66
TRITON_EXPORT std::set< triton::arch::Register > & getUndefinedRegisters(void)
Returns the list of all implicit and explicit undefined registers.
TRITON_EXPORT void setWriteBack(bool state)
Sets the writeBack flag of the instruction.
TRITON_EXPORT triton::uint64 getAddress(void) const
Returns the address of the instruction.
TRITON_EXPORT bool isSymbolized(void) const
Returns true if at least one of its expressions contains a symbolic variable.
TRITON_EXPORT bool isMemoryRead(void) const
Returns true if the instruction contains an expression which reads the memory.
TRITON_EXPORT void removeUndefinedRegister(const triton::arch::Register &reg)
Removes an undefined register.
bool controlFlow
True if this instruction changes the control flow. This field is set at the disassembly level.
Definition: instruction.hpp:96
TRITON_EXPORT bool isMemoryWrite(void) const
Returns true if the instruction contains an expression which writes into the memory.
TRITON_EXPORT void setUndefinedRegister(const triton::arch::Register &reg)
Sets an undefined register.
TRITON_EXPORT triton::uint32 getThreadId(void) const
Returns the thread id of the instruction.
Definition: instruction.cpp:99
TRITON_EXPORT void removeReadImmediate(const triton::arch::Immediate &imm)
Removes a read immediate.
TRITON_EXPORT bool isBranch(void) const
Returns true if this instruction is a branch.
TRITON_EXPORT bool isPrefixed(void) const
Returns true if the instruction has a prefix (mainly for X86).
TRITON_EXPORT void setBranch(bool flag)
Sets flag to define this instruction as branch or not.
TRITON_EXPORT void setSize(triton::uint32 size)
Sets the size of the instruction.
TRITON_EXPORT Instruction()
Constructor.
Definition: instruction.cpp:19
TRITON_EXPORT void setCodeCondition(triton::arch::arm::condition_e codeCondition)
Sets the code condition of the instruction (mainly for AArch64).
TRITON_EXPORT std::set< std::pair< triton::arch::Register, triton::ast::SharedAbstractNode > > & getWrittenRegisters(void)
Returns the list of all implicit and explicit register (flags includes) outputs (write)
std::vector< triton::arch::OperandWrapper > operands
A list of operands.
bool tainted
True if this instruction is tainted. This field is set at the semantics level.
TRITON_EXPORT void removeWrittenRegister(const triton::arch::Register &reg)
Removes a written register.
std::set< triton::arch::Register > undefinedRegisters
Implicit and explicit undefined registers. This field is set at the semantics level.
Definition: instruction.hpp:90
TRITON_EXPORT void setReadImmediate(const triton::arch::Immediate &imm, const triton::ast::SharedAbstractNode &node)
Sets a read immediate.
std::set< std::pair< triton::arch::Register, triton::ast::SharedAbstractNode > > readRegisters
Implicit and explicit register inputs (read). This field is set at the semantics level.
Definition: instruction.hpp:81
TRITON_EXPORT void setControlFlow(bool flag)
Sets flag to define this instruction changes the control flow or not.
TRITON_EXPORT bool isUpdateFlag(void) const
Returns true if the instruction updates flags. Mainly used for AArch64 instructions like ADDS.
bool updateFlag
True if this instruction updartes flags. Mainly used for AArch64 instruction like ADDS.
TRITON_EXPORT triton::arch::x86::prefix_e getPrefix(void) const
Returns the prefix of the instruction (mainly for X86).
TRITON_EXPORT ~Instruction()
Destructor.
Definition: instruction.cpp:53
std::set< std::pair< triton::arch::Register, triton::ast::SharedAbstractNode > > writtenRegisters
Implicit and explicit register outputs (write). This field is set at the semantics level.
Definition: instruction.hpp:84
TRITON_EXPORT void setOpcode(const triton::uint8 *opcode, triton::uint32 size)
Sets the opcode of the instruction.
std::vector< triton::engines::symbolic::SharedSymbolicExpression > symbolicExpressions
The semantics set of the instruction.
TRITON_EXPORT std::set< std::pair< triton::arch::Immediate, triton::ast::SharedAbstractNode > > & getReadImmediates(void)
Returns the list of all implicit and explicit immediate inputs (read)
TRITON_EXPORT void setReadRegister(const triton::arch::Register &reg, const triton::ast::SharedAbstractNode &node)
Sets a read register.
TRITON_EXPORT const triton::engines::symbolic::SharedSymbolicExpression & addSymbolicExpression(const triton::engines::symbolic::SharedSymbolicExpression &expr)
Adds a symbolic expression.
triton::uint64 address
The address of the instruction.
Definition: instruction.hpp:54
TRITON_EXPORT void removeStoreAccess(const triton::arch::MemoryAccess &mem)
Removes a store access.
TRITON_EXPORT void setThreadId(triton::uint32 tid)
Sets the thread id of the instruction.
bool conditionTaken
True if the condition is taken (i.g x86: jcc, cmocc, setcc, ...). This field is set at the semantics ...
Definition: instruction.hpp:99
triton::uint32 tid
The thread id of the instruction.
Definition: instruction.hpp:51
TRITON_EXPORT triton::arch::arm::condition_e getCodeCondition(void) const
Returns the code codition of the instruction (mainly for AArch64).
TRITON_EXPORT std::string getDisassembly(void) const
Returns the disassembly of the instruction.
bool branch
True if this instruction is a branch. This field is set at the disassembly level.
Definition: instruction.hpp:93
TRITON_EXPORT triton::uint64 getNextAddress(void) const
Returns the next address of the instruction.
TRITON_EXPORT bool isControlFlow(void) const
Returns true if this instruction changes the control flow (e.g x86: JMP, JCC, CALL,...
This class is used to represent a memory access.
This class is used as operand wrapper.
This class is used when an instruction has a register operand.
Definition: register.hpp:44
std::ostream & operator<<(std::ostream &stream, const BitsVector &bv)
Displays a BitsVector.
Definition: bitsVector.cpp:103
condition_e
Types of condition.
Definition: archEnums.hpp:144
std::shared_ptr< triton::ast::AbstractNode > SharedAbstractNode
Shared Abstract Node.
Definition: ast.hpp:57
constexpr triton::uint32 flag
flag size in bit
Definition: cpuSize.hpp:56
std::shared_ptr< triton::engines::symbolic::SymbolicExpression > SharedSymbolicExpression
Shared Symbolic Expression.
Definition: ast.hpp:38
std::uint64_t uint64
unisgned 64-bits
Definition: tritonTypes.hpp:34
std::uint32_t uint32
unisgned 32-bits
Definition: tritonTypes.hpp:31
std::uint8_t uint8
unisgned 8-bits
Definition: tritonTypes.hpp:25
prefix_e
Types of prefix.
Definition: archEnums.hpp:91
The Triton namespace.
Definition: api.cpp:227