libTriton version 1.0 build 1592
Loading...
Searching...
No Matches
pyPathConstraint.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/exceptions.hpp>
13
14#include <iostream>
15
16
17
18/* setup doctest context
19
20>>> from triton import TritonContext, ARCH
21>>> ctxt = TritonContext()
22>>> ctxt.setArchitecture(ARCH.X86_64)
23>>> astCtxt = ctxt.getAstContext()
24
25*/
26
108namespace triton {
109 namespace bindings {
110 namespace python {
111
113 void PathConstraint_dealloc(PyObject* self) {
114 std::cout << std::flush;
116 Py_TYPE(self)->tp_free((PyObject*)self);
117 }
118
119
120 static PyObject* PathConstraint_getBranchConstraints(PyObject* self, PyObject* noarg) {
121 try {
122 PyObject* ret = nullptr;
123 const auto& branches = PyPathConstraint_AsPathConstraint(self)->getBranchConstraints();
124
125 ret = xPyList_New(branches.size());
126 for (triton::usize index = 0; index != branches.size(); index++) {
127 PyObject* dict = xPyDict_New();
128 xPyDict_SetItem(dict, PyStr_FromString("isTaken"), PyBool_FromLong(std::get<0>(branches[index])));
129 xPyDict_SetItem(dict, PyStr_FromString("srcAddr"), PyLong_FromUint64(std::get<1>(branches[index])));
130 xPyDict_SetItem(dict, PyStr_FromString("dstAddr"), PyLong_FromUint64(std::get<2>(branches[index])));
131 xPyDict_SetItem(dict, PyStr_FromString("constraint"), PyAstNode(std::get<3>(branches[index])));
132 PyList_SetItem(ret, index, dict);
133 }
134
135 return ret;
136 }
137 catch (const triton::exceptions::Exception& e) {
138 return PyErr_Format(PyExc_TypeError, "%s", e.what());
139 }
140 }
141
142
143 static PyObject* PathConstraint_getComment(PyObject* self, PyObject* noarg) {
144 try {
145 return Py_BuildValue("s", PyPathConstraint_AsPathConstraint(self)->getComment().c_str());
146 }
147 catch (const triton::exceptions::Exception& e) {
148 return PyErr_Format(PyExc_TypeError, "%s", e.what());
149 }
150 }
151
152
153 static PyObject* PathConstraint_getSourceAddress(PyObject* self, PyObject* noarg) {
154 try {
155 return PyLong_FromUint64(PyPathConstraint_AsPathConstraint(self)->getSourceAddress());
156 }
157 catch (const triton::exceptions::Exception& e) {
158 return PyErr_Format(PyExc_TypeError, "%s", e.what());
159 }
160 }
161
162
163 static PyObject* PathConstraint_getTakenAddress(PyObject* self, PyObject* noarg) {
164 try {
165 return PyLong_FromUint64(PyPathConstraint_AsPathConstraint(self)->getTakenAddress());
166 }
167 catch (const triton::exceptions::Exception& e) {
168 return PyErr_Format(PyExc_TypeError, "%s", e.what());
169 }
170 }
171
172
173 static PyObject* PathConstraint_getTakenPredicate(PyObject* self, PyObject* noarg) {
174 try {
175 return PyAstNode(PyPathConstraint_AsPathConstraint(self)->getTakenPredicate());
176 }
177 catch (const triton::exceptions::Exception& e) {
178 return PyErr_Format(PyExc_TypeError, "%s", e.what());
179 }
180 }
181
182
183 static PyObject* PathConstraint_getThreadId(PyObject* self, PyObject* noarg) {
184 try {
185 return PyLong_FromUint32(PyPathConstraint_AsPathConstraint(self)->getThreadId());
186 }
187 catch (const triton::exceptions::Exception& e) {
188 return PyErr_Format(PyExc_TypeError, "%s", e.what());
189 }
190 }
191
192
193 static PyObject* PathConstraint_isMultipleBranches(PyObject* self, PyObject* noarg) {
194 try {
195 if (PyPathConstraint_AsPathConstraint(self)->isMultipleBranches())
196 Py_RETURN_TRUE;
197 Py_RETURN_FALSE;
198 }
199 catch (const triton::exceptions::Exception& e) {
200 return PyErr_Format(PyExc_TypeError, "%s", e.what());
201 }
202 }
203
204
205 static PyObject* PathConstraint_setComment(PyObject* self, PyObject* comment) {
206 try {
207 if (!PyStr_Check(comment))
208 return PyErr_Format(PyExc_TypeError, "PathConstraint::setComment(): Expected a string as argument.");
209 PyPathConstraint_AsPathConstraint(self)->setComment(PyStr_AsString(comment));
210 Py_INCREF(Py_None);
211 return Py_None;
212 }
213 catch (const triton::exceptions::Exception& e) {
214 return PyErr_Format(PyExc_TypeError, "%s", e.what());
215 }
216 }
217
218
220 PyMethodDef PathConstraint_callbacks[] = {
221 {"getBranchConstraints", PathConstraint_getBranchConstraints, METH_NOARGS, ""},
222 {"getComment", PathConstraint_getComment, METH_NOARGS, ""},
223 {"getSourceAddress", PathConstraint_getSourceAddress, METH_NOARGS, ""},
224 {"getTakenAddress", PathConstraint_getTakenAddress, METH_NOARGS, ""},
225 {"getTakenPredicate", PathConstraint_getTakenPredicate, METH_NOARGS, ""},
226 {"getThreadId", PathConstraint_getThreadId, METH_NOARGS, ""},
227 {"isMultipleBranches", PathConstraint_isMultipleBranches, METH_NOARGS, ""},
228 {"setComment", PathConstraint_setComment, METH_O, ""},
229 {nullptr, nullptr, 0, nullptr}
230 };
231
232
233 PyTypeObject PathConstraint_Type = {
234 PyVarObject_HEAD_INIT(&PyType_Type, 0)
235 "PathConstraint", /* tp_name */
236 sizeof(PathConstraint_Object), /* tp_basicsize */
237 0, /* tp_itemsize */
238 (destructor)PathConstraint_dealloc, /* tp_dealloc */
239 0, /* tp_print or tp_vectorcall_offset */
240 0, /* tp_getattr */
241 0, /* tp_setattr */
242 0, /* tp_compare */
243 0, /* tp_repr */
244 0, /* tp_as_number */
245 0, /* tp_as_sequence */
246 0, /* tp_as_mapping */
247 0, /* tp_hash */
248 0, /* tp_call */
249 0, /* tp_str */
250 0, /* tp_getattro */
251 0, /* tp_setattro */
252 0, /* tp_as_buffer */
253 Py_TPFLAGS_DEFAULT, /* tp_flags */
254 "PathConstraint objects", /* tp_doc */
255 0, /* tp_traverse */
256 0, /* tp_clear */
257 0, /* tp_richcompare */
258 0, /* tp_weaklistoffset */
259 0, /* tp_iter */
260 0, /* tp_iternext */
261 PathConstraint_callbacks, /* tp_methods */
262 0, /* tp_members */
263 0, /* tp_getset */
264 0, /* tp_base */
265 0, /* tp_dict */
266 0, /* tp_descr_get */
267 0, /* tp_descr_set */
268 0, /* tp_dictoffset */
269 0, /* tp_init */
270 0, /* tp_alloc */
271 0, /* tp_new */
272 0, /* tp_free */
273 0, /* tp_is_gc */
274 0, /* tp_bases */
275 0, /* tp_mro */
276 0, /* tp_cache */
277 0, /* tp_subclasses */
278 0, /* tp_weaklist */
279 0, /* tp_del */
280 #if IS_PY3
281 0, /* tp_version_tag */
282 0, /* tp_finalize */
283 #if IS_PY3_8
284 0, /* tp_vectorcall */
285 #if !IS_PY3_9
286 0, /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
287 #endif
288 #endif
289 #else
290 0 /* tp_version_tag */
291 #endif
292 };
293
294
296 PathConstraint_Object* object;
297
298 PyType_Ready(&PathConstraint_Type);
299 object = PyObject_NEW(PathConstraint_Object, &PathConstraint_Type);
300 if (object != NULL)
302
303 return (PyObject*)object;
304 }
305
306 }; /* python namespace */
307 }; /* bindings namespace */
308}; /* triton namespace */
The root class of all exceptions.
TRITON_EXPORT const char * what() const
Returns the exception message.
PyObject * PyPathConstraint(const triton::engines::symbolic::PathConstraint &pc)
Creates the PathConstraint python class.
PyTypeObject PathConstraint_Type
pyPathConstraint type.
PyObject * xPyList_New(Py_ssize_t len)
Creates a PyList and raises an exception if it fails.
int xPyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
Same as PyDict_SetItem but decrements reference on object and key.
PyObject * PyLong_FromUint64(triton::uint64 value)
Returns a pyObject from a triton::uint64.
Definition utils.cpp:328
PyObject * xPyDict_New(void)
Creates a PyDict and raises an exception if it fails.
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::size_t usize
unsigned MAX_INT 32 or 64 bits according to the CPU.
void PathConstraint_dealloc(PyObject *self)
PathConstraint destructor.
PyMethodDef PathConstraint_callbacks[]
PathConstraint methods.
The Triton namespace.
#define PyPathConstraint_AsPathConstraint(v)