libTriton version 1.0 build 1592
Loading...
Searching...
No Matches
tritonCallbacks.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
12#include <triton/basicBlock.hpp>
13#include <triton/bitsVector.hpp>
14#include <triton/exceptions.hpp>
15#include <triton/immediate.hpp>
17#include <triton/register.hpp>
18
19
20
75namespace triton {
76 namespace bindings {
77 namespace python {
78
79 static PyObject* triton_BasicBlock(PyObject* self, PyObject* args) {
80 PyObject* obj = nullptr;
81
82 /* Extract arguments */
83 if (PyArg_ParseTuple(args, "|O", &obj) == false) {
84 return PyErr_Format(PyExc_TypeError, "BasicBlock(): Invalid constructor.");
85 }
86
87 try {
88 /* Check if it's a default constructor */
89 if (obj == nullptr)
90 return PyBasicBlock();
91
92 /* Check if argument is a list of instruction */
93 else if (obj != nullptr && PyList_Check(obj)) {
95
96 for (Py_ssize_t i = 0; i < PyList_Size(obj); i++) {
97 PyObject* item = PyList_GetItem(obj, i);
98
99 if (PyInstruction_Check(item) == false)
100 return PyErr_Format(PyExc_TypeError, "BasicBlock(): All items must be an Instruction objet.");
101
102 block.add(*PyInstruction_AsInstruction(item));
103 }
104
105 return PyBasicBlock(block);
106 }
107
108 /* Otherwise, invalid constructor */
109 else {
110 return PyErr_Format(PyExc_TypeError, "BasicBlock(): Invalid constructor.");
111 }
112 }
113 catch (const triton::exceptions::Exception& e) {
114 return PyErr_Format(PyExc_TypeError, "%s", e.what());
115 }
116 }
117
118 static PyObject* triton_Immediate(PyObject* self, PyObject* args) {
119 PyObject* value = nullptr;
120 PyObject* size = nullptr;
121
122 /* Extract arguments */
123 if (PyArg_ParseTuple(args, "|OO", &value, &size) == false) {
124 return PyErr_Format(PyExc_TypeError, "Immediate(): Invalid constructor.");
125 }
126
127 /* Check if the first arg is a integer */
128 if (value == nullptr || (!PyLong_Check(value) && !PyInt_Check(value)))
129 return PyErr_Format(PyExc_TypeError, "Immediate(): Expects an integer as first argument.");
130
131 /* Check if the second arg is a integer */
132 if (size == nullptr || (!PyLong_Check(size) && !PyInt_Check(size)))
133 return PyErr_Format(PyExc_TypeError, "Immediate(): Expects an integer as second argument.");
134
135 try {
137 return PyImmediate(imm);
138 }
139 catch (const triton::exceptions::Exception& e) {
140 return PyErr_Format(PyExc_TypeError, "%s", e.what());
141 }
142 }
143
144
145 static PyObject* triton_Instruction(PyObject* self, PyObject* args) {
146 PyObject* arg1 = nullptr;
147 PyObject* arg2 = nullptr;
148
149 /* Extract arguments */
150 if (PyArg_ParseTuple(args, "|OO", &arg1, &arg2) == false) {
151 return PyErr_Format(PyExc_TypeError, "Instruction(): Invalid constructor.");
152 }
153
154 try {
155 /* Instruction() */
156 if (arg1 == nullptr)
157 return PyInstruction();
158
159 /* Instruction(opcode) */
160 else if (arg1 && PyBytes_Check(arg1) && arg2 == nullptr) {
161 triton::uint8* opc = reinterpret_cast<triton::uint8*>(PyBytes_AsString(arg1));
162 triton::uint32 size = static_cast<triton::uint32>(PyBytes_Size(arg1));
163 return PyInstruction(opc, size);
164 }
165
166 /* Instruction(address, opcode) */
167 else if (arg1 && arg2 && (PyLong_Check(arg1) || PyInt_Check(arg1)) && PyBytes_Check(arg2)) {
168 triton::uint64 addr = PyLong_AsUint64(arg1);
169 triton::uint8* opc = reinterpret_cast<triton::uint8*>(PyBytes_AsString(arg2));
170 triton::uint32 size = static_cast<triton::uint32>(PyBytes_Size(arg2));
171 return PyInstruction(addr, opc, size);
172 }
173
174 else {
175 return PyErr_Format(PyExc_TypeError, "Instruction(): Expects bytes as first argument or an integer as first and bytes as second argument.");
176 }
177 }
178 catch (const triton::exceptions::Exception& e) {
179 return PyErr_Format(PyExc_TypeError, "%s", e.what());
180 }
181 }
182
183
184 static PyObject* triton_MemoryAccess(PyObject* self, PyObject* args) {
185 PyObject* address = nullptr;
186 PyObject* size = nullptr;
187
188 /* Extract arguments */
189 if (PyArg_ParseTuple(args, "|OO", &address, &size) == false) {
190 return PyErr_Format(PyExc_TypeError, "MemoryAccess(): Invalid constructor.");
191 }
192
193 /* Check if the first arg is a integer */
194 if (address == nullptr || (!PyLong_Check(address) && !PyInt_Check(address)))
195 return PyErr_Format(PyExc_TypeError, "MemoryAccess(): Expects an integer as first argument.");
196
197 /* Check if the second arg is a integer */
198 if (size == nullptr || (!PyLong_Check(size) && !PyInt_Check(size)))
199 return PyErr_Format(PyExc_TypeError, "MemoryAccess(): Expects an integer as second argument.");
200
201 try {
203 return PyMemoryAccess(mem);
204 }
205 catch (const triton::exceptions::Exception& e) {
206 return PyErr_Format(PyExc_TypeError, "%s", e.what());
207 }
208 }
209
210
211 static PyObject* triton_TritonContext(PyObject* self, PyObject* args) {
212 PyObject* arch = nullptr;
213
214 /* Extract arguments */
215 if (PyArg_ParseTuple(args, "|O", &arch) == false) {
216 return PyErr_Format(PyExc_TypeError, "TritonContext(): Invalid constructor.");
217 }
218
219 try {
220 if (arch == nullptr) {
221 return PyTritonContext();
222 }
223 if (arch == nullptr && (!PyLong_Check(arch) && !PyInt_Check(arch))) {
224 return PyErr_Format(PyExc_TypeError, "TritonContext(): Invalid type of argument.");
225 }
227 }
228 catch (const triton::exceptions::Exception& e) {
229 return PyErr_Format(PyExc_TypeError, "%s", e.what());
230 }
231 }
232
233
234 PyMethodDef tritonCallbacks[] = {
235 {"BasicBlock", (PyCFunction)triton_BasicBlock, METH_VARARGS, ""},
236 {"Immediate", (PyCFunction)triton_Immediate, METH_VARARGS, ""},
237 {"Instruction", (PyCFunction)triton_Instruction, METH_VARARGS, ""},
238 {"MemoryAccess", (PyCFunction)triton_MemoryAccess, METH_VARARGS, ""},
239 {"TritonContext", (PyCFunction)triton_TritonContext, METH_VARARGS, ""},
240 {nullptr, nullptr, 0, nullptr}
241 };
242
243 }; /* python namespace */
244 }; /* bindings namespace */
245}; /* triton namespace */
This class is used to represent a basic block.
TRITON_EXPORT void add(const Instruction &instruction)
Add an instruction to the block.
This class is used to represent an immediate.
Definition immediate.hpp:37
This class is used to represent a memory access.
The root class of all exceptions.
TRITON_EXPORT const char * what() const
Returns the exception message.
triton::uint64 PyLong_AsUint64(PyObject *vv)
Returns a triton::uint64 from a pyObject.
Definition utils.cpp:137
PyObject * PyImmediate(const triton::arch::Immediate &imm)
Creates the Immediate python class.
PyMethodDef tritonCallbacks[]
triton python methods.
PyObject * PyBasicBlock(void)
Creates the BasicBlock python class.
PyObject * PyInstruction(void)
Creates the Instruction python class.
PyObject * PyTritonContext(void)
Creates the new TritonContext python class.
triton::uint32 PyLong_AsUint32(PyObject *vv)
Returns a triton::uint32 from a pyObject.
Definition utils.cpp:108
PyObject * PyMemoryAccess(const triton::arch::MemoryAccess &mem)
Creates the Memory python class.
std::uint64_t uint64
unisgned 64-bits
std::uint32_t uint32
unisgned 32-bits
std::uint8_t uint8
unisgned 8-bits
The Triton namespace.
#define PyInstruction_AsInstruction(v)
#define PyInstruction_Check(v)