libTriton version 1.0 build 1592
Loading...
Searching...
No Matches
pyMemoryAccess.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
11#include <triton/coreUtils.hpp>
12#include <triton/exceptions.hpp>
14
15#include <iostream>
16
17
18
19/* setup doctest context
20
21>>> from __future__ import print_function
22>>> from triton import TritonContext, ARCH, Instruction, MemoryAccess
23>>> ctxt = TritonContext()
24>>> ctxt.setArchitecture(ARCH.X86_64)
25
26>>> inst = Instruction(b"\x8A\xA4\x4A\x00\x01\x00\x00")
27>>> inst.setAddress(0x40000)
28
29*/
30
153namespace triton {
154 namespace bindings {
155 namespace python {
156
158 void MemoryAccess_dealloc(PyObject* self) {
159 std::cout << std::flush;
161 Py_TYPE(self)->tp_free((PyObject*)self);
162 }
163
164
165 static PyObject* MemoryAccess_getAddress(PyObject* self, PyObject* noarg) {
166 try {
167 return PyLong_FromUint64(PyMemoryAccess_AsMemoryAccess(self)->getAddress());
168 }
169 catch (const triton::exceptions::Exception& e) {
170 return PyErr_Format(PyExc_TypeError, "%s", e.what());
171 }
172 }
173
174
175 static PyObject* MemoryAccess_getLeaAst(PyObject* self, PyObject* noarg) {
176 try {
177 if (PyMemoryAccess_AsMemoryAccess(self)->getLeaAst() == nullptr) {
178 Py_INCREF(Py_None);
179 return Py_None;
180 }
181 return PyAstNode(PyMemoryAccess_AsMemoryAccess(self)->getLeaAst());
182 }
183 catch (const triton::exceptions::Exception& e) {
184 return PyErr_Format(PyExc_TypeError, "%s", e.what());
185 }
186 }
187
188
189 static PyObject* MemoryAccess_getBaseRegister(PyObject* self, PyObject* noarg) {
190 try {
191 triton::arch::Register reg(PyMemoryAccess_AsMemoryAccess(self)->getBaseRegister());
192 return PyRegister(reg);
193 }
194 catch (const triton::exceptions::Exception& e) {
195 return PyErr_Format(PyExc_TypeError, "%s", e.what());
196 }
197 }
198
199
200 static PyObject* MemoryAccess_getBitSize(PyObject* self, PyObject* noarg) {
201 try {
202 return PyLong_FromUint32(PyMemoryAccess_AsMemoryAccess(self)->getBitSize());
203 }
204 catch (const triton::exceptions::Exception& e) {
205 return PyErr_Format(PyExc_TypeError, "%s", e.what());
206 }
207 }
208
209
210 static PyObject* MemoryAccess_getBitvector(PyObject* self, PyObject* noarg) {
211 try {
212 return PyBitsVector(*PyMemoryAccess_AsMemoryAccess(self));
213 }
214 catch (const triton::exceptions::Exception& e) {
215 return PyErr_Format(PyExc_TypeError, "%s", e.what());
216 }
217 }
218
219
220 static PyObject* MemoryAccess_getDisplacement(PyObject* self, PyObject* noarg) {
221 try {
222 triton::arch::Immediate imm(PyMemoryAccess_AsMemoryAccess(self)->getDisplacement());
223 return PyImmediate(imm);
224 }
225 catch (const triton::exceptions::Exception& e) {
226 return PyErr_Format(PyExc_TypeError, "%s", e.what());
227 }
228 }
229
230
231 static PyObject* MemoryAccess_getIndexRegister(PyObject* self, PyObject* noarg) {
232 try {
233 triton::arch::Register reg(PyMemoryAccess_AsMemoryAccess(self)->getIndexRegister());
234 return PyRegister(reg);
235 }
236 catch (const triton::exceptions::Exception& e) {
237 return PyErr_Format(PyExc_TypeError, "%s", e.what());
238 }
239 }
240
241
242 static PyObject* MemoryAccess_getScale(PyObject* self, PyObject* noarg) {
243 try {
245 return PyImmediate(imm);
246 }
247 catch (const triton::exceptions::Exception& e) {
248 return PyErr_Format(PyExc_TypeError, "%s", e.what());
249 }
250 }
251
252
253 static PyObject* MemoryAccess_getSegmentRegister(PyObject* self, PyObject* noarg) {
254 try {
255 triton::arch::Register reg(PyMemoryAccess_AsMemoryAccess(self)->getSegmentRegister());
256 return PyRegister(reg);
257 }
258 catch (const triton::exceptions::Exception& e) {
259 return PyErr_Format(PyExc_TypeError, "%s", e.what());
260 }
261 }
262
263
264 static PyObject* MemoryAccess_getSize(PyObject* self, PyObject* noarg) {
265 try {
266 return PyLong_FromUint32(PyMemoryAccess_AsMemoryAccess(self)->getSize());
267 }
268 catch (const triton::exceptions::Exception& e) {
269 return PyErr_Format(PyExc_TypeError, "%s", e.what());
270 }
271 }
272
273
274 static PyObject* MemoryAccess_getType(PyObject* self, PyObject* noarg) {
275 try {
276 return PyLong_FromUint32(PyMemoryAccess_AsMemoryAccess(self)->getType());
277 }
278 catch (const triton::exceptions::Exception& e) {
279 return PyErr_Format(PyExc_TypeError, "%s", e.what());
280 }
281 }
282
283
284 static PyObject* MemoryAccess_isOverlapWith(PyObject* self, PyObject* mem2) {
285 try {
287
288 if (!PyMemoryAccess_Check(mem2))
289 return PyErr_Format(PyExc_TypeError, "MemoryAccess::isOverlapWith(): Expected a MemoryAccess as argument.");
290
293 Py_RETURN_TRUE;
294 Py_RETURN_FALSE;
295 }
296 catch (const triton::exceptions::Exception& e) {
297 return PyErr_Format(PyExc_TypeError, "%s", e.what());
298 }
299 }
300
301
302 static PyObject* MemoryAccess_setBaseRegister(PyObject* self, PyObject* reg) {
303 try {
305
306 if (!PyRegister_Check(reg))
307 return PyErr_Format(PyExc_TypeError, "MemoryAccess::setBaseRegister(): Expected a Register as argument.");
308
311 Py_INCREF(Py_None);
312 return Py_None;
313 }
314 catch (const triton::exceptions::Exception& e) {
315 return PyErr_Format(PyExc_TypeError, "%s", e.what());
316 }
317 }
318
319
320 static PyObject* MemoryAccess_setDisplacement(PyObject* self, PyObject* imm) {
321 try {
323
324 if (!PyImmediate_Check(imm))
325 return PyErr_Format(PyExc_TypeError, "MemoryAccess::setDisplacement(): Expected an Immediate as argument.");
326
329 Py_INCREF(Py_None);
330 return Py_None;
331 }
332 catch (const triton::exceptions::Exception& e) {
333 return PyErr_Format(PyExc_TypeError, "%s", e.what());
334 }
335 }
336
337
338 static PyObject* MemoryAccess_setIndexRegister(PyObject* self, PyObject* reg) {
339 try {
341
342 if (!PyRegister_Check(reg))
343 return PyErr_Format(PyExc_TypeError, "MemoryAccess::setIndexRegister(): Expected a Register as argument.");
344
347 Py_INCREF(Py_None);
348 return Py_None;
349 }
350 catch (const triton::exceptions::Exception& e) {
351 return PyErr_Format(PyExc_TypeError, "%s", e.what());
352 }
353 }
354
355
356 static PyObject* MemoryAccess_setScale(PyObject* self, PyObject* imm) {
357 try {
359
360 if (!PyImmediate_Check(imm))
361 return PyErr_Format(PyExc_TypeError, "MemoryAccess::setScale(): Expected an Immediate as argument.");
362
365 Py_INCREF(Py_None);
366 return Py_None;
367 }
368 catch (const triton::exceptions::Exception& e) {
369 return PyErr_Format(PyExc_TypeError, "%s", e.what());
370 }
371 }
372
373
374 static PyObject* MemoryAccess_setSegmentRegister(PyObject* self, PyObject* reg) {
375 try {
377
378 if (!PyRegister_Check(reg))
379 return PyErr_Format(PyExc_TypeError, "MemoryAccess::setSegmentRegister(): Expected a Register as argument.");
380
383 Py_INCREF(Py_None);
384 return Py_None;
385 }
386 catch (const triton::exceptions::Exception& e) {
387 return PyErr_Format(PyExc_TypeError, "%s", e.what());
388 }
389 }
390
391
392 #if !defined(IS_PY3_8) || !IS_PY3_8
393 static int MemoryAccess_print(PyObject* self, void* io, int s) {
394 std::cout << PyMemoryAccess_AsMemoryAccess(self);
395 return 0;
396 }
397 #endif
398
399
400 static PyObject* MemoryAccess_str(PyObject* self) {
401 try {
402 return PyStr_FromFormat("%s", triton::utils::toString(PyMemoryAccess_AsMemoryAccess(self)).c_str());
403 }
404 catch (const triton::exceptions::Exception& e) {
405 return PyErr_Format(PyExc_TypeError, "%s", e.what());
406 }
407 }
408
409
411 PyMethodDef MemoryAccess_callbacks[] = {
412 {"getAddress", MemoryAccess_getAddress, METH_NOARGS, ""},
413 {"getBaseRegister", MemoryAccess_getBaseRegister, METH_NOARGS, ""},
414 {"getBitSize", MemoryAccess_getBitSize, METH_NOARGS, ""},
415 {"getBitvector", MemoryAccess_getBitvector, METH_NOARGS, ""},
416 {"getDisplacement", MemoryAccess_getDisplacement, METH_NOARGS, ""},
417 {"getIndexRegister", MemoryAccess_getIndexRegister, METH_NOARGS, ""},
418 {"getLeaAst", MemoryAccess_getLeaAst, METH_NOARGS, ""},
419 {"getScale", MemoryAccess_getScale, METH_NOARGS, ""},
420 {"getSegmentRegister", MemoryAccess_getSegmentRegister, METH_NOARGS, ""},
421 {"getSize", MemoryAccess_getSize, METH_NOARGS, ""},
422 {"getType", MemoryAccess_getType, METH_NOARGS, ""},
423 {"isOverlapWith", MemoryAccess_isOverlapWith, METH_O, ""},
424 {"setBaseRegister", MemoryAccess_setBaseRegister, METH_O, ""},
425 {"setDisplacement", MemoryAccess_setDisplacement, METH_O, ""},
426 {"setIndexRegister", MemoryAccess_setIndexRegister, METH_O, ""},
427 {"setScale", MemoryAccess_setScale, METH_O, ""},
428 {"setSegmentRegister", MemoryAccess_setSegmentRegister, METH_O, ""},
429 {nullptr, nullptr, 0, nullptr}
430 };
431
432
433 PyTypeObject MemoryAccess_Type = {
434 PyVarObject_HEAD_INIT(&PyType_Type, 0)
435 "MemoryAccess", /* tp_name */
436 sizeof(MemoryAccess_Object), /* tp_basicsize */
437 0, /* tp_itemsize */
438 (destructor)MemoryAccess_dealloc, /* tp_dealloc */
439 #if IS_PY3_8
440 0, /* tp_vectorcall_offset */
441 #else
442 (printfunc)MemoryAccess_print, /* tp_print */
443 #endif
444 0, /* tp_getattr */
445 0, /* tp_setattr */
446 0, /* tp_compare */
447 (reprfunc)MemoryAccess_str, /* tp_repr */
448 0, /* tp_as_number */
449 0, /* tp_as_sequence */
450 0, /* tp_as_mapping */
451 0, /* tp_hash */
452 0, /* tp_call */
453 (reprfunc)MemoryAccess_str, /* tp_str */
454 0, /* tp_getattro */
455 0, /* tp_setattro */
456 0, /* tp_as_buffer */
457 Py_TPFLAGS_DEFAULT, /* tp_flags */
458 "MemoryAccess objects", /* tp_doc */
459 0, /* tp_traverse */
460 0, /* tp_clear */
461 0, /* tp_richcompare */
462 0, /* tp_weaklistoffset */
463 0, /* tp_iter */
464 0, /* tp_iternext */
465 MemoryAccess_callbacks, /* tp_methods */
466 0, /* tp_members */
467 0, /* tp_getset */
468 0, /* tp_base */
469 0, /* tp_dict */
470 0, /* tp_descr_get */
471 0, /* tp_descr_set */
472 0, /* tp_dictoffset */
473 0, /* tp_init */
474 0, /* tp_alloc */
475 0, /* tp_new */
476 0, /* tp_free */
477 0, /* tp_is_gc */
478 0, /* tp_bases */
479 0, /* tp_mro */
480 0, /* tp_cache */
481 0, /* tp_subclasses */
482 0, /* tp_weaklist */
483 0, /* tp_del */
484 #if IS_PY3
485 0, /* tp_version_tag */
486 0, /* tp_finalize */
487 #if IS_PY3_8
488 0, /* tp_vectorcall */
489 #if !IS_PY3_9
490 0, /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
491 #endif
492 #endif
493 #else
494 0 /* tp_version_tag */
495 #endif
496 };
497
498
500 MemoryAccess_Object* object;
501
502 PyType_Ready(&MemoryAccess_Type);
503 object = PyObject_NEW(MemoryAccess_Object, &MemoryAccess_Type);
504 if (object != NULL)
505 object->mem = new triton::arch::MemoryAccess(mem);
506
507 return (PyObject*)object;
508 }
509
510 }; /* python namespace */
511 }; /* bindings namespace */
512}; /* triton namespace */
This class is used to represent an immediate.
Definition immediate.hpp:37
This class is used to represent a memory access.
TRITON_EXPORT bool isOverlapWith(const MemoryAccess &other) const
Returns true if other and self overlap.
TRITON_EXPORT void setDisplacement(const triton::arch::Immediate &displacement)
LEA - Sets the displacement operand.
TRITON_EXPORT void setScale(const triton::arch::Immediate &scale)
LEA - Sets the scale operand.
TRITON_EXPORT void setIndexRegister(const triton::arch::Register &index)
LEA - Sets the index register 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.
This class is used when an instruction has a register operand.
Definition register.hpp:44
The root class of all exceptions.
TRITON_EXPORT const char * what() const
Returns the exception message.
PyObject * PyImmediate(const triton::arch::Immediate &imm)
Creates the Immediate python class.
PyTypeObject MemoryAccess_Type
pyMemory type.
PyObject * PyLong_FromUint64(triton::uint64 value)
Returns a pyObject from a triton::uint64.
Definition utils.cpp:328
PyObject * PyRegister(const triton::arch::Register &reg)
Creates the Register python class.
PyObject * PyMemoryAccess(const triton::arch::MemoryAccess &mem)
Creates the Memory python class.
PyObject * PyLong_FromUint32(triton::uint32 value)
Returns a pyObject from a triton::uint32.
Definition utils.cpp:322
PyObject * PyAstNode(const triton::ast::SharedAbstractNode &node)
Creates the AstNode python class.
std::string toString(const T &obj)
Converts an object to a string.
Definition coreUtils.hpp:38
PyMethodDef MemoryAccess_callbacks[]
MemoryAccess methods.
void MemoryAccess_dealloc(PyObject *self)
MemoryAccess destructor.
The Triton namespace.
#define PyImmediate_Check(v)
#define PyRegister_AsRegister(v)
#define PyMemoryAccess_Check(v)
#define PyImmediate_AsImmediate(v)
#define PyMemoryAccess_AsMemoryAccess(v)
#define PyRegister_Check(v)