libTriton  version 0.9 build 1502
init.cpp
Go to the documentation of this file.
1 /*
3 ** Copyright (C) - Triton
4 **
5 ** This program is under the terms of the Apache License 2.0.
6 */
7 
8 /* libTriton */
9 #include <triton/pythonUtils.hpp>
11 
12 #if defined(__unix__) || defined(__APPLE__)
13  #include <dlfcn.h>
14 #endif
15 
16 #ifdef __STDC_LIB_EXT1__
17 #define __STDC_WANT_LIB_EXT1__
18 #endif
19 #include <cstdio>
20 
21 #ifndef __STDC_LIB_EXT1__
23 int fopen_s(FILE** fd, const char* fn, const char* flags) {
24  *fd = fopen(fn, flags);
25  if(*fd == 0)
26  return -1;
27  else
28  return 0;
29 }
30 #endif
31 
32 #include <iostream>
33 #include <stdexcept>
34 #include <string>
35 
36 /* pintool */
37 #include "bindings.hpp"
38 
39 
40 
118 namespace tracer {
119  namespace pintool {
120 
121  namespace options {
122  PyObject* callbackAfter = nullptr;
123  PyObject* callbackBefore = nullptr;
124  PyObject* callbackBeforeIRProc = nullptr;
125  PyObject* callbackFini = nullptr;
126  PyObject* callbackImageLoad = nullptr;
127  PyObject* callbackSignals = nullptr;
128  PyObject* callbackSyscallEntry = nullptr;
129  PyObject* callbackSyscallExit = nullptr;
131  char* startAnalysisFromSymbol = nullptr;
132  std::vector<const char*> imageBlacklist;
133  std::vector<const char*> imageWhitelist;
134  std::map<const char*, PyObject*> callbackRoutineEntry;
135  std::map<const char*, PyObject*> callbackRoutineExit;
136  std::set<triton::__uint> startAnalysisFromAddress;
137  std::set<triton::__uint> startAnalysisFromOffset;
138  std::set<triton::__uint> stopAnalysisFromAddress;
139  std::set<triton::__uint> stopAnalysisFromOffset;
141  };
142 
143 
144  #if IS_PY3
145  static struct PyModuleDef pintoolModuleDef = {
146  PyModuleDef_HEAD_INIT,
147  "pintool",
148  NULL,
149  -1,
151  NULL, /* m_slots */
152  NULL, /* m_traverse */
153  NULL, /* m_clear */
154  NULL, /* m_free */
155  };
156  #endif
157 
158 
159  #if IS_PY3
160  PyObject* initpintool(void) {
161  PyObject* pintoolModule = PyModule_Create(&tracer::pintool::pintoolModuleDef);
162  #else
163  void initpintool(void) {
164  PyObject* pintoolModule = Py_InitModule("pintool", tracer::pintool::pintoolCallbacks);
165  #endif
166 
167  if (pintoolModule == nullptr) {
168  std::cerr << "Failed to initialize the pintool bindings" << std::endl;
169  PyErr_Print();
170  exit(1);
171  }
172 
173  /* ======================= */
174 
175  /* Create the INSERT_POINT class */
176  PyObject *idCallbackClassName = triton::bindings::python::xPyString_FromString("INSERT_POINT");
177  PyObject *idCallbackClassDict = triton::bindings::python::xPyDict_New();
178 
179  /* Add callback ref into the INSERT_POINT class namespace */
190 
191  /* Create the INSERT_POINT class */
192  PyObject *idCallbackClass = triton::bindings::python::xPyClass_New(nullptr, idCallbackClassDict, idCallbackClassName);
193 
194  /* ======================= */
195 
196  /* Create the STANDARD class */
197  PyObject *idStandardClassName = triton::bindings::python::xPyString_FromString("STANDARD");
198  PyObject *idStandardClassDict = triton::bindings::python::xPyDict_New();
199 
200  /* Add callback ref into the STANDARD class namespace */
201  triton::bindings::python::xPyDict_SetItemString(idStandardClassDict, "STANDARD_INVALID", triton::bindings::python::PyLong_FromUint32(LEVEL_CORE::SYSCALL_STANDARD_INVALID));
202  triton::bindings::python::xPyDict_SetItemString(idStandardClassDict, "STANDARD_IA32_LINUX", triton::bindings::python::PyLong_FromUint32(LEVEL_CORE::SYSCALL_STANDARD_IA32_LINUX));
203  triton::bindings::python::xPyDict_SetItemString(idStandardClassDict, "STANDARD_IA32E_LINUX", triton::bindings::python::PyLong_FromUint32(LEVEL_CORE::SYSCALL_STANDARD_IA32E_LINUX));
204  triton::bindings::python::xPyDict_SetItemString(idStandardClassDict, "STANDARD_IA32_MAC", triton::bindings::python::PyLong_FromUint32(LEVEL_CORE::SYSCALL_STANDARD_IA32_MAC));
205  triton::bindings::python::xPyDict_SetItemString(idStandardClassDict, "STANDARD_IA32E_MAC", triton::bindings::python::PyLong_FromUint32(LEVEL_CORE::SYSCALL_STANDARD_IA32E_MAC));
206  triton::bindings::python::xPyDict_SetItemString(idStandardClassDict, "STANDARD_IA32_WINDOWS_FAST", triton::bindings::python::PyLong_FromUint32(LEVEL_CORE::SYSCALL_STANDARD_IA32_WINDOWS_FAST));
207  triton::bindings::python::xPyDict_SetItemString(idStandardClassDict, "STANDARD_IA32E_WINDOWS_FAST", triton::bindings::python::PyLong_FromUint32(LEVEL_CORE::SYSCALL_STANDARD_IA32E_WINDOWS_FAST));
208  triton::bindings::python::xPyDict_SetItemString(idStandardClassDict, "STANDARD_IA32_WINDOWS_ALT", triton::bindings::python::PyLong_FromUint32(LEVEL_CORE::SYSCALL_STANDARD_IA32_WINDOWS_ALT));
209  triton::bindings::python::xPyDict_SetItemString(idStandardClassDict, "STANDARD_WOW64", triton::bindings::python::PyLong_FromUint32(LEVEL_CORE::SYSCALL_STANDARD_WOW64));
210  triton::bindings::python::xPyDict_SetItemString(idStandardClassDict, "STANDARD_WINDOWS_INT", triton::bindings::python::PyLong_FromUint32(LEVEL_CORE::SYSCALL_STANDARD_WINDOWS_INT));
211 
212  /* Create the STANDARD class */
213  PyObject *idStandardClass = triton::bindings::python::xPyClass_New(nullptr, idStandardClassDict, idStandardClassName);
214 
215  /* ======================= */
216 
217  /* Add namespace into the pintool module */
218  PyModule_AddObject(pintoolModule, "INSERT_POINT", idCallbackClass);
219  PyModule_AddObject(pintoolModule, "STANDARD", idStandardClass);
220 
221  #if IS_PY3
222  return pintoolModule;
223  #endif
224  }
225 
226 
227  bool execScript(const char *fileName) {
228  #if defined(__unix__) || defined(__APPLE__)
229  /* On some Linux distro, we must load libpython to successfully load all others modules. See issue #276. */
230  void* handle = dlopen(PYTHON_LIBRARIES, RTLD_LAZY | RTLD_GLOBAL);
231  if (!handle)
232  throw std::runtime_error("tracer::pintool::execScript(): Cannot load the Python library.");
233  #endif
234 
235  FILE* fd = nullptr;
236  auto err = fopen_s(&fd, fileName, "r");
237  if (err != 0)
238  throw std::runtime_error("tracer::pintool::execScript(): Script file can't be found.");
239 
240  PyRun_SimpleFile(fd, fileName);
241 
242  fclose(fd);
243  return true;
244  }
245 
246 
247  void initPythonArgs(int argc, char* argv[]) {
248  #if IS_PY3
249  /* https://stackoverflow.com/a/54891187 */
250  wchar_t** _argv = static_cast<wchar_t**>(PyMem_Malloc(sizeof(wchar_t*) * argc));
251  for (int i = 0; i < argc; i++) {
252  wchar_t* arg = static_cast<wchar_t*>(Py_DecodeLocale(argv[i], NULL));
253  _argv[i] = arg;
254  }
255  /* Setup argc and argv */
256  PySys_SetArgv(argc, _argv);
257  #else
258  PySys_SetArgv(argc, argv);
259  #endif
260  }
261 
262  };
263 };
264 
triton::uint32 targetThreadId
TID focused during the JIT.
Definition: init.cpp:140
std::vector< const char * > imageWhitelist
An image white list.
Definition: init.cpp:133
PyObject * callbackFini
Callback called at the end of the execution.
Definition: init.cpp:125
std::set< triton::__uint > startAnalysisFromOffset
Start analysis from an offset.
Definition: init.cpp:137
PyObject * callbackSyscallEntry
Callback called before the syscall processing.
Definition: init.cpp:128
std::map< const char *, PyObject * > callbackRoutineExit
Callback callled after routine processing.
Definition: init.cpp:135
PyObject * callbackBefore
Callback called before the instruction processing.
Definition: init.cpp:123
PyObject * callbackAfter
Callback called after the instruction processing.
Definition: init.cpp:122
std::set< triton::__uint > stopAnalysisFromAddress
Stop analysis from address.
Definition: init.cpp:138
std::map< const char *, PyObject * > callbackRoutineEntry
Callback called before routine processing.
Definition: init.cpp:134
bool startAnalysisFromEntry
Start analysis from the entry point.
Definition: init.cpp:130
PyObject * callbackSyscallExit
Callback called after the syscall processing.
Definition: init.cpp:129
char * startAnalysisFromSymbol
Start analysis from a symbol.
Definition: init.cpp:131
std::set< triton::__uint > startAnalysisFromAddress
Start analysis from a symbol.
Definition: init.cpp:136
PyObject * callbackBeforeIRProc
Callback called before the IR processing.
Definition: init.cpp:124
PyObject * callbackImageLoad
Callback called when an image is loaded.
Definition: init.cpp:126
std::set< triton::__uint > stopAnalysisFromOffset
Stop analysis from an offset.
Definition: init.cpp:139
std::vector< const char * > imageBlacklist
An image black list.
Definition: init.cpp:132
PyObject * callbackSignals
Callback called when a signal occurs.
Definition: init.cpp:127
@ CB_IMAGE_LOAD
When an image is loaded.
Definition: bindings.hpp:85
@ CB_FINI
At the end of the execution.
Definition: bindings.hpp:79
@ CB_SIGNALS
When a signal occurs.
Definition: bindings.hpp:82
@ CB_AFTER
After the instruction processing.
Definition: bindings.hpp:76
@ CB_ROUTINE_EXIT
After the routine processing.
Definition: bindings.hpp:81
@ CB_BEFORE
Before the instruction processing.
Definition: bindings.hpp:77
@ CB_SYSCALL_ENTRY
Before the syscall processing.
Definition: bindings.hpp:83
@ CB_BEFORE_SYMPROC
Before the IR processing.
Definition: bindings.hpp:78
@ CB_SYSCALL_EXIT
After the syscall processing.
Definition: bindings.hpp:84
@ CB_ROUTINE_ENTRY
Before the routine processing.
Definition: bindings.hpp:80
void initPythonArgs(int argc, char *argv[])
The initialization of the pintool python arguments.
Definition: init.cpp:247
PyMethodDef pintoolCallbacks[]
Python callbacks of the pintool module.
Definition: bindings.cpp:579
bool execScript(const char *fileName)
The python script which will be executed by Pin.
Definition: init.cpp:227
void initpintool(void)
The initialization of the pintool python 2 module.
Definition: init.cpp:163
PyObject * xPyClass_New(PyObject *b, PyObject *d, PyObject *n)
Creates a PyClass and raises an exception if it fails. dict is copied in Py3 ! All references are dec...
PyObject * xPyString_FromString(const char *v)
Creates a PyString and raises an exception if it fails.
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:297
int xPyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
Same as PyDict_SetItemString but decrements reference on object.
std::uint32_t uint32
unisgned 32-bits
Definition: tritonTypes.hpp:31
The Tracer namespace.
Definition: api.cpp:12
int fopen_s(FILE **fd, const char *fn, const char *flags)
Secure open as fopen is deprecated on windows but fopen_s is not standard.
Definition: init.cpp:23