libTriton version 1.0 build 1588
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
105namespace triton {
106 namespace bindings {
107 namespace python {
108
110 void PathConstraint_dealloc(PyObject* self) {
111 std::cout << std::flush;
113 Py_TYPE(self)->tp_free((PyObject*)self);
114 }
115
116
117 static PyObject* PathConstraint_getBranchConstraints(PyObject* self, PyObject* noarg) {
118 try {
119 PyObject* ret = nullptr;
120 const auto& branches = PyPathConstraint_AsPathConstraint(self)->getBranchConstraints();
121
122 ret = xPyList_New(branches.size());
123 for (triton::usize index = 0; index != branches.size(); index++) {
124 PyObject* dict = xPyDict_New();
125 xPyDict_SetItem(dict, PyStr_FromString("isTaken"), PyBool_FromLong(std::get<0>(branches[index])));
126 xPyDict_SetItem(dict, PyStr_FromString("srcAddr"), PyLong_FromUint64(std::get<1>(branches[index])));
127 xPyDict_SetItem(dict, PyStr_FromString("dstAddr"), PyLong_FromUint64(std::get<2>(branches[index])));
128 xPyDict_SetItem(dict, PyStr_FromString("constraint"), PyAstNode(std::get<3>(branches[index])));
129 PyList_SetItem(ret, index, dict);
130 }
131
132 return ret;
133 }
134 catch (const triton::exceptions::Exception& e) {
135 return PyErr_Format(PyExc_TypeError, "%s", e.what());
136 }
137 }
138
139
140 static PyObject* PathConstraint_getComment(PyObject* self, PyObject* noarg) {
141 try {
142 return Py_BuildValue("s", PyPathConstraint_AsPathConstraint(self)->getComment().c_str());
143 }
144 catch (const triton::exceptions::Exception& e) {
145 return PyErr_Format(PyExc_TypeError, "%s", e.what());
146 }
147 }
148
149
150 static PyObject* PathConstraint_getTakenAddress(PyObject* self, PyObject* noarg) {
151 try {
152 return PyLong_FromUint64(PyPathConstraint_AsPathConstraint(self)->getTakenAddress());
153 }
154 catch (const triton::exceptions::Exception& e) {
155 return PyErr_Format(PyExc_TypeError, "%s", e.what());
156 }
157 }
158
159
160 static PyObject* PathConstraint_getTakenPredicate(PyObject* self, PyObject* noarg) {
161 try {
162 return PyAstNode(PyPathConstraint_AsPathConstraint(self)->getTakenPredicate());
163 }
164 catch (const triton::exceptions::Exception& e) {
165 return PyErr_Format(PyExc_TypeError, "%s", e.what());
166 }
167 }
168
169
170 static PyObject* PathConstraint_getThreadId(PyObject* self, PyObject* noarg) {
171 try {
172 return PyLong_FromUint32(PyPathConstraint_AsPathConstraint(self)->getThreadId());
173 }
174 catch (const triton::exceptions::Exception& e) {
175 return PyErr_Format(PyExc_TypeError, "%s", e.what());
176 }
177 }
178
179
180 static PyObject* PathConstraint_isMultipleBranches(PyObject* self, PyObject* noarg) {
181 try {
182 if (PyPathConstraint_AsPathConstraint(self)->isMultipleBranches())
183 Py_RETURN_TRUE;
184 Py_RETURN_FALSE;
185 }
186 catch (const triton::exceptions::Exception& e) {
187 return PyErr_Format(PyExc_TypeError, "%s", e.what());
188 }
189 }
190
191
192 static PyObject* PathConstraint_setComment(PyObject* self, PyObject* comment) {
193 try {
194 if (!PyStr_Check(comment))
195 return PyErr_Format(PyExc_TypeError, "PathConstraint::setComment(): Expected a string as argument.");
196 PyPathConstraint_AsPathConstraint(self)->setComment(PyStr_AsString(comment));
197 Py_INCREF(Py_None);
198 return Py_None;
199 }
200 catch (const triton::exceptions::Exception& e) {
201 return PyErr_Format(PyExc_TypeError, "%s", e.what());
202 }
203 }
204
205
207 PyMethodDef PathConstraint_callbacks[] = {
208 {"getBranchConstraints", PathConstraint_getBranchConstraints, METH_NOARGS, ""},
209 {"getComment", PathConstraint_getComment, METH_NOARGS, ""},
210 {"getTakenAddress", PathConstraint_getTakenAddress, METH_NOARGS, ""},
211 {"getTakenPredicate", PathConstraint_getTakenPredicate, METH_NOARGS, ""},
212 {"getThreadId", PathConstraint_getThreadId, METH_NOARGS, ""},
213 {"isMultipleBranches", PathConstraint_isMultipleBranches, METH_NOARGS, ""},
214 {"setComment", PathConstraint_setComment, METH_O, ""},
215 {nullptr, nullptr, 0, nullptr}
216 };
217
218
219 PyTypeObject PathConstraint_Type = {
220 PyVarObject_HEAD_INIT(&PyType_Type, 0)
221 "PathConstraint", /* tp_name */
222 sizeof(PathConstraint_Object), /* tp_basicsize */
223 0, /* tp_itemsize */
224 (destructor)PathConstraint_dealloc, /* tp_dealloc */
225 0, /* tp_print or tp_vectorcall_offset */
226 0, /* tp_getattr */
227 0, /* tp_setattr */
228 0, /* tp_compare */
229 0, /* tp_repr */
230 0, /* tp_as_number */
231 0, /* tp_as_sequence */
232 0, /* tp_as_mapping */
233 0, /* tp_hash */
234 0, /* tp_call */
235 0, /* tp_str */
236 0, /* tp_getattro */
237 0, /* tp_setattro */
238 0, /* tp_as_buffer */
239 Py_TPFLAGS_DEFAULT, /* tp_flags */
240 "PathConstraint objects", /* tp_doc */
241 0, /* tp_traverse */
242 0, /* tp_clear */
243 0, /* tp_richcompare */
244 0, /* tp_weaklistoffset */
245 0, /* tp_iter */
246 0, /* tp_iternext */
247 PathConstraint_callbacks, /* tp_methods */
248 0, /* tp_members */
249 0, /* tp_getset */
250 0, /* tp_base */
251 0, /* tp_dict */
252 0, /* tp_descr_get */
253 0, /* tp_descr_set */
254 0, /* tp_dictoffset */
255 0, /* tp_init */
256 0, /* tp_alloc */
257 0, /* tp_new */
258 0, /* tp_free */
259 0, /* tp_is_gc */
260 0, /* tp_bases */
261 0, /* tp_mro */
262 0, /* tp_cache */
263 0, /* tp_subclasses */
264 0, /* tp_weaklist */
265 0, /* tp_del */
266 #if IS_PY3
267 0, /* tp_version_tag */
268 0, /* tp_finalize */
269 #if IS_PY3_8
270 0, /* tp_vectorcall */
271 #if !IS_PY3_9
272 0, /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
273 #endif
274 #endif
275 #else
276 0 /* tp_version_tag */
277 #endif
278 };
279
280
282 PathConstraint_Object* object;
283
284 PyType_Ready(&PathConstraint_Type);
285 object = PyObject_NEW(PathConstraint_Object, &PathConstraint_Type);
286 if (object != NULL)
288
289 return (PyObject*)object;
290 }
291
292 }; /* python namespace */
293 }; /* bindings namespace */
294}; /* triton namespace */
The root class of all exceptions.
Definition: exceptions.hpp:36
TRITON_EXPORT const char * what() const
Returns the exception message.
Definition: exceptions.hpp:57
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:311
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:305
PyObject * PyAstNode(const triton::ast::SharedAbstractNode &node)
Creates the AstNode python class.
Definition: pyAstNode.cpp:988
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)