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_SOLVER_page
63
- \ref py_SYMBOLIC_page
64
- \ref py_SYSCALL_page
65
- \ref py_VERSION_page
66

67
*/
68

69

70

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

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

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

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

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

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

101

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

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

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

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

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

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

140

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

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

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

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

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

167

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

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

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

190

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

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

Read our documentation on viewing source code .

Loading