libTriton  version 0.9 build 1502
memoryAccess.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 <triton/cpuSize.hpp>
9 #include <triton/exceptions.hpp>
10 #include <triton/memoryAccess.hpp>
11 
12 
13 
14 namespace triton {
15  namespace arch {
16 
18  this->address = 0;
19  this->leaAst = nullptr;
20  this->pcRelative = 0;
21  }
22 
23 
25  : MemoryAccess() {
26  this->address = address;
27 
28  if (size == 0)
29  throw triton::exceptions::MemoryAccess("MemoryAccess::MemoryAccess(): size cannot be zero.");
30 
31  if (size != triton::size::byte &&
32  size != triton::size::word &&
33  size != triton::size::dword &&
34  size != triton::size::qword &&
35  size != triton::size::dqword &&
36  size != triton::size::qqword &&
37  size != triton::size::dqqword)
38  throw triton::exceptions::MemoryAccess("MemoryAccess::MemoryAccess(): size must be aligned.");
39 
40  this->setBits(((size * triton::bitsize::byte) - 1), 0);
41  }
42 
43 
45  : BitsVector(other) {
46  this->copy(other);
47  }
48 
49 
51  return this->address;
52  }
53 
54 
56  return this->leaAst;
57  }
58 
59 
61  return this->pcRelative;
62  }
63 
64 
66  return this->getVectorSize();
67  }
68 
69 
71  return this->getVectorSize() / triton::bitsize::byte;
72  }
73 
74 
76  return triton::arch::OP_MEM;
77  }
78 
79 
81  return this->segmentReg;
82  }
83 
84 
86  return this->baseReg;
87  }
88 
89 
91  return this->indexReg;
92  }
93 
94 
96  return this->displacement;
97  }
98 
99 
101  return this->scale;
102  }
103 
104 
106  return this->segmentReg;
107  }
108 
109 
111  return this->baseReg;
112  }
113 
114 
116  return this->indexReg;
117  }
118 
119 
121  return this->displacement;
122  }
123 
124 
126  return this->scale;
127  }
128 
129 
130  bool MemoryAccess::isOverlapWith(const MemoryAccess& other) const {
131  if (this->getAddress() <= other.getAddress() && other.getAddress() < (this->getAddress() + this->getSize())) return true;
132  if (other.getAddress() <= this->getAddress() && this->getAddress() < (other.getAddress() + other.getSize())) return true;
133  return false;
134  }
135 
136 
138  this->address = addr;
139  }
140 
141 
143  this->pcRelative = addr;
144  }
145 
146 
148  this->segmentReg = segment;
149  }
150 
151 
153  this->baseReg = base;
154  }
155 
156 
158  this->indexReg = index;
159  }
160 
161 
163  this->displacement = displacement;
164  }
165 
166 
168  this->scale = scale;
169  }
170 
171 
173  this->leaAst = ast;
174  }
175 
176 
178  BitsVector::operator=(other);
179  this->copy(other);
180  return *this;
181  }
182 
183 
184  void MemoryAccess::copy(const MemoryAccess& other) {
185  this->address = other.address;
186  this->baseReg = other.baseReg;
187  this->displacement = other.displacement;
188  this->indexReg = other.indexReg;
189  this->leaAst = other.leaAst;
190  this->pcRelative = other.pcRelative;
191  this->scale = other.scale;
192  this->segmentReg = other.segmentReg;
193  }
194 
195 
196  std::ostream& operator<<(std::ostream& stream, const MemoryAccess& mem) {
197  stream << "[@0x"
198  << std::hex << mem.getAddress()
199  << "]:"
200  << std::dec << mem.getBitSize()
201  << " bv["
202  << mem.getHigh()
203  << ".."
204  << mem.getLow()
205  << "]";
206  return stream;
207  }
208 
209 
210  std::ostream& operator<<(std::ostream& stream, const MemoryAccess* mem) {
211  stream << *mem;
212  return stream;
213  }
214 
215 
216  bool operator==(const MemoryAccess& mem1, const MemoryAccess& mem2) {
217  if (mem1.getAddress() != mem2.getAddress())
218  return false;
219  if (mem1.getSize() != mem2.getSize())
220  return false;
221  if (mem1.getConstBaseRegister() != mem2.getConstBaseRegister())
222  return false;
223  if (mem1.getConstIndexRegister() != mem2.getConstIndexRegister())
224  return false;
225  if (mem1.getConstScale() != mem2.getConstScale())
226  return false;
227  if (mem1.getConstDisplacement() != mem2.getConstDisplacement())
228  return false;
230  return false;
231  if (mem1.getPcRelative() != mem2.getPcRelative())
232  return false;
233  return true;
234  }
235 
236 
237  bool operator!=(const MemoryAccess& mem1, const MemoryAccess& mem2) {
238  return !(mem1 == mem2);
239  }
240 
241 
242  bool operator<(const MemoryAccess& mem1, const MemoryAccess& mem2) {
243  triton::uint64 seed1 = 0;
244  triton::uint64 seed2 = 0;
245 
246  /*
247  * Golden ratio 32-bits -> 0x9e3779b9
248  * Golden ratio 64-bits -> 0x9e3779b97f4a7c13
249  */
250  seed1 ^= mem1.getAddress() + 0x9e3779b97f4a7c13 + (seed1 << 6) + (seed1 >> 2);
251  seed1 ^= mem1.getSize() + 0x9e3779b97f4a7c13 + (seed1 << 6) + (seed1 >> 2);
252 
253  seed2 ^= mem2.getAddress() + 0x9e3779b97f4a7c13 + (seed2 << 6) + (seed2 >> 2);
254  seed2 ^= mem2.getSize() + 0x9e3779b97f4a7c13 + (seed2 << 6) + (seed2 >> 2);
255 
256  return (seed1 < seed2);
257  }
258 
259  }; /* arch namespace */
260 }; /* triton namespace */
This class is used to deal with registers and memory as bits vector.
Definition: bitsVector.hpp:36
TRITON_EXPORT triton::uint32 getHigh(void) const
Returns the highest bit.
Definition: bitsVector.cpp:34
TRITON_EXPORT triton::uint32 getVectorSize(void) const
Returns the size in bits of the vector.
Definition: bitsVector.cpp:44
TRITON_EXPORT triton::uint32 getLow(void) const
Returns the lower bit.
Definition: bitsVector.cpp:39
TRITON_EXPORT void setBits(triton::uint32 high, triton::uint32 low)
Sets the bits (high, low) position.
Definition: bitsVector.cpp:78
TRITON_EXPORT BitsVector & operator=(const BitsVector &other)
Copy a BitsVector.
Definition: bitsVector.cpp:96
This class is used to represent an immediate.
Definition: immediate.hpp:37
This class is used to represent a memory access.
triton::arch::Register baseReg
LEA - If the operand has a base register, this attribute is filled.
TRITON_EXPORT bool isOverlapWith(const MemoryAccess &other) const
Returns true if other and self overlap.
triton::uint64 address
The memory' address.
TRITON_EXPORT triton::uint64 getPcRelative(void) const
LEA - Gets pc relative.
TRITON_EXPORT triton::ast::SharedAbstractNode getLeaAst(void) const
Returns the AST of the memory access (LEA).
TRITON_EXPORT void setDisplacement(const triton::arch::Immediate &displacement)
LEA - Sets the displacement operand.
triton::arch::Immediate displacement
LEA - If the operand has a displacement, this attribute is filled.
triton::uint64 pcRelative
Contains the pc relative if it exists.
TRITON_EXPORT triton::arch::Register & getSegmentRegister(void)
LEA - Returns the segment register operand.
TRITON_EXPORT const triton::arch::Register & getConstBaseRegister(void) const
LEA - Returns the base register operand.
TRITON_EXPORT void setLeaAst(const triton::ast::SharedAbstractNode &ast)
Sets the AST of the memory access (LEA).
TRITON_EXPORT const triton::arch::Register & getConstSegmentRegister(void) const
LEA - Returns the segment register operand.
TRITON_EXPORT triton::uint32 getBitSize(void) const
Returns the size (in bits) of the memory vector.
TRITON_EXPORT triton::arch::operand_e getType(void) const
Returns the type of the operand (triton::arch::OPERAND_MEMORY).
TRITON_EXPORT triton::arch::Register & getIndexRegister(void)
LEA - Returns the index register operand.
TRITON_EXPORT triton::arch::Immediate & getScale(void)
LEA - Returns the scale operand.
TRITON_EXPORT const triton::arch::Immediate & getConstDisplacement(void) const
LEA - Returns the displacement operand.
TRITON_EXPORT MemoryAccess & operator=(const MemoryAccess &other)
Copies a MemoryAccess.
TRITON_EXPORT void setScale(const triton::arch::Immediate &scale)
LEA - Sets the scale operand.
TRITON_EXPORT triton::uint64 getAddress(void) const
Returns the address of the memory.
TRITON_EXPORT void setPcRelative(triton::uint64 addr)
LEA - Sets pc relative.
TRITON_EXPORT triton::uint32 getSize(void) const
Returns the size (in bytes) of the memory vector.
TRITON_EXPORT triton::arch::Immediate & getDisplacement(void)
LEA - Returns the displacement operand.
triton::arch::Immediate scale
LEA - If the operand has a scale, this attribute is filled.
triton::arch::Register segmentReg
LEA - If the operand has a segment register, this attribute is filled.
triton::arch::Register indexReg
LEA - If the operand has an index register, this attribute is filled.
TRITON_EXPORT void setAddress(triton::uint64 addr)
Sets the address of the memory access.
TRITON_EXPORT void setIndexRegister(const triton::arch::Register &index)
LEA - Sets the index register operand.
TRITON_EXPORT triton::arch::Register & getBaseRegister(void)
LEA - Returns the base register operand.
triton::ast::SharedAbstractNode leaAst
The AST of the memory access (LEA).
TRITON_EXPORT const triton::arch::Register & getConstIndexRegister(void) const
LEA - Returns the index register operand.
TRITON_EXPORT const triton::arch::Immediate & getConstScale(void) const
LEA - Returns the scale operand.
TRITON_EXPORT void setSegmentRegister(const triton::arch::Register &segment)
LEA - Sets the segment register operand.
TRITON_EXPORT void setBaseRegister(const triton::arch::Register &base)
LEA - Sets the base register operand.
TRITON_EXPORT MemoryAccess()
Constructor.
This class is used when an instruction has a register operand.
Definition: register.hpp:44
The exception class used by memory access.
Definition: exceptions.hpp:245
std::ostream & operator<<(std::ostream &stream, const BitsVector &bv)
Displays a BitsVector.
Definition: bitsVector.cpp:103
bool operator==(const Immediate &imm1, const Immediate &imm2)
Compares two Immediate.
Definition: immediate.cpp:127
bool operator!=(const Immediate &imm1, const Immediate &imm2)
Compares two Immediate.
Definition: immediate.cpp:136
bool operator<(const Immediate &imm1, const Immediate &imm2)
Compares two Immediate (needed for std::map)
Definition: immediate.cpp:141
@ OP_MEM
memory operand
Definition: archEnums.hpp:50
std::shared_ptr< triton::ast::AbstractNode > SharedAbstractNode
Shared Abstract Node.
Definition: ast.hpp:57
constexpr triton::uint32 byte
byte size in bit
Definition: cpuSize.hpp:58
constexpr triton::uint32 dword
dword size in byte
Definition: cpuSize.hpp:34
constexpr triton::uint32 dqqword
dqqword size in byte
Definition: cpuSize.hpp:42
constexpr triton::uint32 word
word size in byte
Definition: cpuSize.hpp:32
constexpr triton::uint32 dqword
dqword size in byte
Definition: cpuSize.hpp:38
constexpr triton::uint32 byte
byte size in byte
Definition: cpuSize.hpp:30
constexpr triton::uint32 qword
qword size in byte
Definition: cpuSize.hpp:36
constexpr triton::uint32 qqword
qqword size in byte
Definition: cpuSize.hpp:40
std::uint64_t uint64
unisgned 64-bits
Definition: tritonTypes.hpp:34
std::uint32_t uint32
unisgned 32-bits
Definition: tritonTypes.hpp:31
The Triton namespace.
Definition: api.cpp:227