1
//! \file
2
/*
3
**  Copyright (C) - Triton
4
**
5
**  This program is under the terms of the Apache License 2.0.
6
*/
7

8
#include <triton/pythonBindings.hpp>
9
#include <triton/pythonObjects.hpp>
10
#include <triton/pythonUtils.hpp>
11
#include <triton/pythonXFunctions.hpp>
12
#include <triton/exceptions.hpp>
13
#include <triton/bitsVector.hpp>
14
#include <triton/immediate.hpp>
15
#include <triton/memoryAccess.hpp>
16
#include <triton/register.hpp>
17

18

19

20
/*! \page py_triton_page Python bindings of libTriton
21
    \brief [**python api**] All information about libTriton's Python API.
22
    \anchor triton
23

24
\section triton_py_api Python API - Methods and namespaces of the triton module
25
<hr>
26

27
This project works using a `TritonContext` which contains all the required internal state
28
to simulate your instructions. You may also find [helpers](https://github.com/JonathanSalwan/Triton/tree/master/src/examples/python)
29
to wrap more generic concepts.
30

31
\subsection triton_py_api_classes Classes
32

33
- \ref py_AstContext_page
34
- \ref py_AstNode_page
35
- \ref py_BitsVector_page
36
- \ref py_Immediate_page
37
- \ref py_Instruction_page
38
- \ref py_MemoryAccess_page
39
- \ref py_PathConstraint_page
40
- \ref py_Register_page
41
- \ref py_SolverModel_page
42
- \ref py_SymbolicExpression_page
43
- \ref py_SymbolicVariable_page
44
- \ref py_TritonContext_page
45

46

47
\subsection triton_py_api_namespaces Namespaces
48

49
- \ref py_ARCH_page
50
- \ref py_AST_NODE_page
51
- \ref py_AST_REPRESENTATION_page
52
- \ref py_CALLBACK_page
53
- \ref py_CONDITION_page
54
- \ref py_CPUSIZE_page
55
- \ref py_EXTEND_page
56
- \ref py_MODE_page
57
- \ref py_OPCODE_page
58
- \ref py_OPERAND_page
59
- \ref py_PREFIX_page
60
- \ref py_REG_page
61
- \ref py_SHIFT_page
62
- \ref py_SYMBOLIC_page
63
- \ref py_SYSCALL_page
64
- \ref py_VERSION_page
65

66
*/
67

68

69

70
namespace triton {
71
  namespace bindings {
72
    namespace python {
73

74 1
      static PyObject* triton_Immediate(PyObject* self, PyObject* args) {
75 1
        PyObject* value = nullptr;
76 1
        PyObject* size  = nullptr;
77

78
        /* Extract arguments */
79 1
        if (PyArg_ParseTuple(args, "|OO", &value, &size) == false) {
80 0
          return PyErr_Format(PyExc_TypeError, "Immediate(): Invalid constructor.");
81
        }
82

83
        /* Check if the first arg is a integer */
84 1
        if (value == nullptr || (!PyLong_Check(value) && !PyInt_Check(value)))
85 0
          return PyErr_Format(PyExc_TypeError, "Immediate(): Expects an integer as first argument.");
86

87
        /* Check if the second arg is a integer */
88 1
        if (size == nullptr || (!PyLong_Check(size) && !PyInt_Check(size)))
89 0
          return PyErr_Format(PyExc_TypeError, "Immediate(): Expects an integer as second argument.");
90

91
        try {
92 1
          triton::arch::Immediate imm(PyLong_AsUint64(value), PyLong_AsUint32(size));
93 1
          return PyImmediate(imm);
94
        }
95 0
        catch (const triton::exceptions::Exception& e) {
96 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
97
        }
98
      }
99

100

101 1
      static PyObject* triton_Instruction(PyObject* self, PyObject* args) {
102 1
        PyObject* arg1 = nullptr;
103 1
        PyObject* arg2 = nullptr;
104

105
        /* Extract arguments */
106 1
        if (PyArg_ParseTuple(args, "|OO", &arg1, &arg2) == false) {
107 0
          return PyErr_Format(PyExc_TypeError, "Instruction(): Invalid constructor.");
108
        }
109

110
        try {
111
          /* Instruction() */
112 1
          if (arg1 == nullptr)
113 1
            return PyInstruction();
114

115
          /* Instruction(opcode) */
116 1
          else if (arg1 && PyBytes_Check(arg1) && arg2 == nullptr) {
117 1
            triton::uint8* opc  = reinterpret_cast<triton::uint8*>(PyBytes_AsString(arg1));
118 1
            triton::uint32 size = static_cast<triton::uint32>(PyBytes_Size(arg1));
119 1
            return PyInstruction(opc, size);
120
          }
121

122
          /* Instruction(address, opcode) */
123 1
          else if (arg1 && arg2 && (PyLong_Check(arg1) || PyInt_Check(arg1)) && PyBytes_Check(arg2)) {
124 1
            triton::uint64 addr = PyLong_AsUint64(arg1);
125 1
            triton::uint8* opc  = reinterpret_cast<triton::uint8*>(PyBytes_AsString(arg2));
126 1
            triton::uint32 size = static_cast<triton::uint32>(PyBytes_Size(arg2));
127 1
            return PyInstruction(addr, opc, size);
128
          }
129

130
          else {
131 0
            return PyErr_Format(PyExc_TypeError, "Instruction(): Expects bytes as first argument or an integer as first and bytes as second argument.");
132
          }
133
        }
134 0
        catch (const triton::exceptions::Exception& e) {
135 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
136
        }
137
      }
138

139

140 1
      static PyObject* triton_MemoryAccess(PyObject* self, PyObject* args) {
141 1
        PyObject* address       = nullptr;
142 1
        PyObject* size          = nullptr;
143

144
        /* Extract arguments */
145 1
        if (PyArg_ParseTuple(args, "|OO", &address, &size) == false) {
146 0
          return PyErr_Format(PyExc_TypeError, "MemoryAccess(): Invalid constructor.");
147
        }
148

149
        /* Check if the first arg is a integer */
150 1
        if (address == nullptr || (!PyLong_Check(address) && !PyInt_Check(address)))
151 0
          return PyErr_Format(PyExc_TypeError, "MemoryAccess(): Expects an integer as first argument.");
152

153
        /* Check if the second arg is a integer */
154 1
        if (size == nullptr || (!PyLong_Check(size) && !PyInt_Check(size)))
155 0
          return PyErr_Format(PyExc_TypeError, "MemoryAccess(): Expects an integer as second argument.");
156

157
        try {
158 1
          triton::arch::MemoryAccess mem(PyLong_AsUint64(address), PyLong_AsUint32(size));
159 1
          return PyMemoryAccess(mem);
160
        }
161 0
        catch (const triton::exceptions::Exception& e) {
162 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
163
        }
164
      }
165

166

167 1
      static PyObject* triton_TritonContext(PyObject* self, PyObject* args) {
168 1
        PyObject* arch = nullptr;
169

170
        /* Extract arguments */
171 1
        if (PyArg_ParseTuple(args, "|O", &arch) == false) {
172 0
          return PyErr_Format(PyExc_TypeError, "TritonContext(): Invalid constructor.");
173
        }
174

175
        try {
176 1
          if (arch == nullptr) {
177 1
            return PyTritonContext();
178
          }
179 1
          if (arch == nullptr && (!PyLong_Check(arch) && !PyInt_Check(arch))) {
180 0
            return PyErr_Format(PyExc_TypeError, "TritonContext(): Invalid type of argument.");
181
          }
182 1
          return PyTritonContext(static_cast<triton::arch::architecture_e>(PyLong_AsUint32(arch)));
183
        }
184 0
        catch (const triton::exceptions::Exception& e) {
185 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
186
        }
187
      }
188

189

190
      PyMethodDef tritonCallbacks[] = {
191
        {"Immediate",       (PyCFunction)triton_Immediate,        METH_VARARGS,   ""},
192
        {"Instruction",     (PyCFunction)triton_Instruction,      METH_VARARGS,   ""},
193
        {"MemoryAccess",    (PyCFunction)triton_MemoryAccess,     METH_VARARGS,   ""},
194
        {"TritonContext",   (PyCFunction)triton_TritonContext,    METH_VARARGS,   ""},
195
        {nullptr,           nullptr,                              0,              nullptr}
196
      };
197

198
    }; /* python namespace */
199
  }; /* bindings namespace */
200 1
}; /* triton namespace */

Read our documentation on viewing source code .

Loading