void initRegNamespace(PyObject* registersDict) { PyDict_Clear(registersDict); PyObject* x86RegistersDict = xPyDict_New(); PyObject* x86RegistersDictClass = xPyClass_New(nullptr, x86RegistersDict, xPyString_FromString("X86")); xPyDict_SetItemString(registersDict, "X86", x86RegistersDictClass); // Init X86 REG namespace #define REG_SPEC(UPPER_NAME, LOWER_NAME, X86_64_UPPER, X86_64_LOWER, X86_64_PARENT, X86_UPPER, X86_LOWER, X86_PARENT, X86_AVAIL) \ if (X86_AVAIL) \ xPyDict_SetItemString(x86RegistersDict, #UPPER_NAME, PyLong_FromUint32(triton::arch::ID_REG_##UPPER_NAME)); // Use REG not available in capstone as normal register #define REG_SPEC_NO_CAPSTONE REG_SPEC #include "triton/x86.spec" PyObject* x8664RegistersDict = xPyDict_New(); PyObject* x8664RegistersDictClass = xPyClass_New(nullptr, x8664RegistersDict, xPyString_FromString("X86_64")); xPyDict_SetItemString(registersDict, "X86_64", x8664RegistersDictClass); // Init X86_64 REG namespace #define REG_SPEC(UPPER_NAME, LOWER_NAME, X86_64_UPPER, X86_64_LOWER, X86_64_PARENT, X86_UPPER, X86_LOWER, X86_PARENT, X86_AVAIL) \ xPyDict_SetItemString(x8664RegistersDict, #UPPER_NAME, PyLong_FromUint32(triton::arch::ID_REG_##UPPER_NAME)); // Use REG not available in capstone as normal register #define REG_SPEC_NO_CAPSTONE REG_SPEC #include "triton/x86.spec" }
void initSyscallEnv(PyObject *idSyscallClassDict) { PyDict_SetItemString(idSyscallClassDict, "STANDARD_INVALID", PyInt_FromLong(SYSCALL_STANDARD_INVALID)); PyDict_SetItemString(idSyscallClassDict, "STANDARD_IA32_LINUX", PyInt_FromLong(SYSCALL_STANDARD_IA32_LINUX)); PyDict_SetItemString(idSyscallClassDict, "STANDARD_IA32E_LINUX", PyInt_FromLong(SYSCALL_STANDARD_IA32E_LINUX)); PyDict_SetItemString(idSyscallClassDict, "STANDARD_IA32_MAC", PyInt_FromLong(SYSCALL_STANDARD_IA32_MAC)); PyDict_SetItemString(idSyscallClassDict, "STANDARD_IA32E_MAC", PyInt_FromLong(SYSCALL_STANDARD_IA32E_MAC)); PyDict_SetItemString(idSyscallClassDict, "STANDARD_IA32_WINDOWS_FAST", PyInt_FromLong(SYSCALL_STANDARD_IA32_WINDOWS_FAST)); PyDict_SetItemString(idSyscallClassDict, "STANDARD_IA32E_WINDOWS_FAST", PyInt_FromLong(SYSCALL_STANDARD_IA32E_WINDOWS_FAST)); PyDict_SetItemString(idSyscallClassDict, "STANDARD_IA32_WINDOWS_ALT", PyInt_FromLong(SYSCALL_STANDARD_IA32_WINDOWS_ALT)); PyDict_SetItemString(idSyscallClassDict, "STANDARD_WOW64", PyInt_FromLong(SYSCALL_STANDARD_WOW64)); PyDict_SetItemString(idSyscallClassDict, "STANDARD_WINDOWS_INT", PyInt_FromLong(SYSCALL_STANDARD_WINDOWS_INT)); // LINUX64 --------------------- /* Create the IDREF.SYSCALL.LINUX_64 class */ PyObject *idLinux64ClassName = xPyString_FromString("LINUX_64"); PyObject *idLinux64ClassDict = xPyDict_New(); /* Add registers ref into IDREF.OPCODE_CATEGORY class */ initLinux64Env(idLinux64ClassDict); /* Create the OPCODE_CATEGORY class */ PyObject *idLinux64Class = xPyClass_New(nullptr, idLinux64ClassDict, idLinux64ClassName); // OPCODE_CATEGORY --------------------- PyDict_SetItemString(idSyscallClassDict, "LINUX_64", idLinux64Class); }
PyObject *PyOperand(TritonOperand operand) { PyObject *dictOperandClass = xPyDict_New(); PyDict_SetItemString(dictOperandClass, "baseReg", PyLong_FromLong(operand.getBaseReg())); PyDict_SetItemString(dictOperandClass, "displacement", PyLong_FromLong(operand.getDisplacement())); PyDict_SetItemString(dictOperandClass, "indexReg", PyLong_FromLong(operand.getIndexReg())); PyDict_SetItemString(dictOperandClass, "memoryScale", PyLong_FromLong(operand.getMemoryScale())); PyDict_SetItemString(dictOperandClass, "size", PyLong_FromLong(operand.getSize())); PyDict_SetItemString(dictOperandClass, "type", PyLong_FromLong(operand.getType())); PyDict_SetItemString(dictOperandClass, "value", PyLong_FromLong(operand.getValue())); /* Create the Operand class */ PyObject *operandClassName = xPyString_FromString("Operand"); PyObject *operandClass = xPyClass_New(nullptr, dictOperandClass, operandClassName); Py_DECREF(dictOperandClass); Py_DECREF(operandClassName); Py_INCREF(operandClass); return operandClass; }
void initBindings(void) { Py_Initialize(); PyObject *tritonModule = Py_InitModule("triton", tritonCallbacks); if (tritonModule == nullptr) { std::cerr << "Failed to initialize the triton bindings" << std::endl; PyErr_Print(); exit(1); } PyObject *smt2libModule = Py_InitModule("smt2lib", smt2libCallbacks); if (smt2libModule == nullptr) { std::cerr << "Failed to initialize the smt2lib bindings" << std::endl; PyErr_Print(); exit(1); } /* Create the IDREF class */ PyObject *idRefClassName = xPyString_FromString("IDREF"); PyObject *idRefClassDict = xPyDict_New(); // REG --------------------- /* Create the IDREF.REG class */ PyObject *idRegClassName = xPyString_FromString("REG"); PyObject *idRegClassDict = xPyDict_New(); /* Add registers ref into IDREF.REG class */ initRegEnv(idRegClassDict); /* Create the REG class */ PyObject *idRegClass = xPyClass_New(nullptr, idRegClassDict, idRegClassName); // REG --------------------- // FLAG --------------------- /* Create the IDREF.FLAG class */ PyObject *idFlagClassName = xPyString_FromString("FLAG"); PyObject *idFlagClassDict = xPyDict_New(); /* Add flags ref into IDREF.FLAG class */ initFlagEnv(idFlagClassDict); /* Create the FLAG class */ PyObject *idFlagClass = xPyClass_New(nullptr, idFlagClassDict, idFlagClassName); // FLAG --------------------- // MISC --------------------- /* Create the IDREF.MISC class */ PyObject *idMiscClassName = xPyString_FromString("MISC"); PyObject *idMiscClassDict = xPyDict_New(); /* Add registers ref into IDREF.MISC class */ initMiscEnv(idMiscClassDict); /* Create the MISC class */ PyObject *idMiscClass = xPyClass_New(nullptr, idMiscClassDict, idMiscClassName); // MISC --------------------- // OPCODE --------------------- /* Create the IDREF.OPCODE class */ PyObject *idOpcodeClassName = xPyString_FromString("OPCODE"); PyObject *idOpcodeClassDict = xPyDict_New(); /* Add registers ref into IDREF.OPCODE class */ initOpcodeEnv(idOpcodeClassDict); /* Create the OPCODE class */ PyObject *idOpcodeClass = xPyClass_New(nullptr, idOpcodeClassDict, idOpcodeClassName); // OPCODE --------------------- // OPCODE_CATEGORY --------------------- /* Create the IDREF.OPCODE_CATEGORY class */ PyObject *idOpcodeCategoryClassName = xPyString_FromString("OPCODE_CATEGORY"); PyObject *idOpcodeCategoryClassDict = xPyDict_New(); /* Add registers ref into IDREF.OPCODE_CATEGORY class */ initOpcodeCategoryEnv(idOpcodeCategoryClassDict); /* Create the OPCODE_CATEGORY class */ PyObject *idOpcodeCategoryClass = xPyClass_New(nullptr, idOpcodeCategoryClassDict, idOpcodeCategoryClassName); // OPCODE_CATEGORY --------------------- // OPERAND --------------------- /* Create the IDREF.OPERAND class */ PyObject *idOperandClassName = xPyString_FromString("OPERAND"); PyObject *idOperandClassDict = xPyDict_New(); /* Add registers ref into IDREF.OPERAND class */ initOperandEnv(idOperandClassDict); /* Create the OPCODE class */ PyObject *idOperandClass = xPyClass_New(nullptr, idOperandClassDict, idOperandClassName); // OPERAND --------------------- // CALLBACK --------------------- /* Create the IDREF.CALLBACK class */ PyObject *idCallbackClassName = xPyString_FromString("CALLBACK"); PyObject *idCallbackClassDict = xPyDict_New(); /* Add registers ref into IDREF.CALLBACK class */ initCallbackEnv(idCallbackClassDict); /* Create the CALLBACK class */ PyObject *idCallbackClass = xPyClass_New(nullptr, idCallbackClassDict, idCallbackClassName); // CALLBACK --------------------- // SYMVAR --------------------- /* Create the IDREF.SYMVAR class */ PyObject *idSymVarClassName = xPyString_FromString("SYMVAR"); PyObject *idSymVarClassDict = xPyDict_New(); /* Add registers ref into IDREF.SYMVAR class */ initSymVarEnv(idSymVarClassDict); /* Create the SYMVAR class */ PyObject *idSymVarClass = xPyClass_New(nullptr, idSymVarClassDict, idSymVarClassName); // SYMVAR --------------------- // SYSCALL --------------------- /* Create the IDREF.SYSCALL class */ PyObject *idSyscallClassName = xPyString_FromString("SYSCALL"); PyObject *idSyscallClassDict = xPyDict_New(); /* Add registers ref into IDREF.SYSCALL class */ initSyscallEnv(idSyscallClassDict); /* Create the SYSCALL class */ PyObject *idSyscallClass = xPyClass_New(nullptr, idSyscallClassDict, idSyscallClassName); // SYSCALL --------------------- // VERSION --------------------- /* Create the IDREF.VERSION class */ PyObject *idVersionClassName = xPyString_FromString("VERSION"); PyObject *idVersionClassDict = xPyDict_New(); /* Add registers ref into IDREF.VERSION class */ initVersionEnv(idVersionClassDict); /* Create the VERSION class */ PyObject *idVersionClass = xPyClass_New(nullptr, idVersionClassDict, idVersionClassName); // VERSION --------------------- /* Add all classes into IDREF */ PyDict_SetItemString(idRefClassDict, "CALLBACK", idCallbackClass); PyDict_SetItemString(idRefClassDict, "FLAG", idFlagClass); PyDict_SetItemString(idRefClassDict, "MISC", idMiscClass); PyDict_SetItemString(idRefClassDict, "OPCODE", idOpcodeClass); PyDict_SetItemString(idRefClassDict, "OPCODE_CATEGORY", idOpcodeCategoryClass); PyDict_SetItemString(idRefClassDict, "OPERAND", idOperandClass); PyDict_SetItemString(idRefClassDict, "REG", idRegClass); PyDict_SetItemString(idRefClassDict, "SYMVAR", idSymVarClass); PyDict_SetItemString(idRefClassDict, "SYSCALL", idSyscallClass); PyDict_SetItemString(idRefClassDict, "VERSION", idVersionClass); /* Create the IDREF class */ PyObject *idRefClass = xPyClass_New(nullptr, idRefClassDict, idRefClassName); /* add all classes and constants into the triton module */ PyModule_AddObject(tritonModule, "IDREF", idRefClass); }
void initBindings(void) { Py_Initialize(); PyObject *tritonModule = Py_InitModule("triton", tritonCallbacks); if (tritonModule == nullptr) { std::cerr << "Failed to initialize the triton bindings" << std::endl; PyErr_Print(); exit(1); } PyObject *smt2libModule = Py_InitModule("smt2lib", smt2libCallbacks); if (smt2libModule == nullptr) { std::cerr << "Failed to initialize the smt2lib bindings" << std::endl; PyErr_Print(); exit(1); } /* Create the IDREF class */ PyObject *idRefClassName = xPyString_FromString("IDREF"); PyObject *idRefClassDict = xPyDict_New(); // REG --------------------- /* Create the IDREF.REG class */ PyObject *idRegClassName = xPyString_FromString("REG"); PyObject *idRegClassDict = xPyDict_New(); /* Add registers ref into IDREF.REG class */ initRegEnv(idRegClassDict); /* Create the REG class */ PyObject *idRegClass = xPyClass_New(nullptr, idRegClassDict, idRegClassName); // REG --------------------- // FLAG --------------------- /* Create the IDREF.FLAG class */ PyObject *idFlagClassName = xPyString_FromString("FLAG"); PyObject *idFlagClassDict = xPyDict_New(); /* Add flags ref into IDREF.FLAG class */ initFlagEnv(idFlagClassDict); /* Create the FLAG class */ PyObject *idFlagClass = xPyClass_New(nullptr, idFlagClassDict, idFlagClassName); // FLAG --------------------- // OPCODE --------------------- /* Create the IDREF.OPCODE class */ PyObject *idOpcodeClassName = xPyString_FromString("OPCODE"); PyObject *idOpcodeClassDict = xPyDict_New(); /* Add registers ref into IDREF.OPCODE class */ initOpcodeEnv(idOpcodeClassDict); /* Create the OPCODE class */ PyObject *idOpcodeClass = xPyClass_New(nullptr, idOpcodeClassDict, idOpcodeClassName); // OPCODE --------------------- // OPCODE_CATEGORY --------------------- /* Create the IDREF.OPCODE_CATEGORY class */ PyObject *idOpcodeCategoryClassName = xPyString_FromString("OPCODE_CATEGORY"); PyObject *idOpcodeCategoryClassDict = xPyDict_New(); /* Add registers ref into IDREF.OPCODE_CATEGORY class */ initOpcodeCategoryEnv(idOpcodeCategoryClassDict); /* Create the OPCODE_CATEGORY class */ PyObject *idOpcodeCategoryClass = xPyClass_New(nullptr, idOpcodeCategoryClassDict, idOpcodeCategoryClassName); // OPCODE_CATEGORY --------------------- // OPERAND --------------------- /* Create the IDREF.OPERAND class */ PyObject *idOperandClassName = xPyString_FromString("OPERAND"); PyObject *idOperandClassDict = xPyDict_New(); /* Add registers ref into IDREF.OPERAND class */ initOperandEnv(idOperandClassDict); /* Create the OPCODE class */ PyObject *idOperandClass = xPyClass_New(nullptr, idOperandClassDict, idOperandClassName); // OPERAND --------------------- // CALLBACK --------------------- /* Create the IDREF.CALLBACK class */ PyObject *idCallbackClassName = xPyString_FromString("CALLBACK"); PyObject *idCallbackClassDict = xPyDict_New(); /* Add registers ref into IDREF.CALLBACK class */ initCallbackEnv(idCallbackClassDict); /* Create the CALLBACK class */ PyObject *idCallbackClass = xPyClass_New(nullptr, idCallbackClassDict, idCallbackClassName); // CALLBACK --------------------- // SYSCALL --------------------- /* Create the IDREF.SYSCALL class */ PyObject *idSyscallClassName = xPyString_FromString("SYSCALL"); PyObject *idSyscallClassDict = xPyDict_New(); /* Add registers ref into IDREF.SYSCALL class */ initSyscallEnv(idSyscallClassDict); /* Create the SYSCALL class */ PyObject *idSyscallClass = xPyClass_New(nullptr, idSyscallClassDict, idSyscallClassName); // SYSCALL --------------------- /* Add REG, FLAG, OPCODE, OPCODE_CATEGORY, OPERAND into IDREF */ PyDict_SetItemString(idRefClassDict, "CALLBACK", idCallbackClass); PyDict_SetItemString(idRefClassDict, "FLAG", idFlagClass); PyDict_SetItemString(idRefClassDict, "OPCODE", idOpcodeClass); PyDict_SetItemString(idRefClassDict, "OPCODE_CATEGORY", idOpcodeCategoryClass); PyDict_SetItemString(idRefClassDict, "OPERAND", idOperandClass); PyDict_SetItemString(idRefClassDict, "REG", idRegClass); PyDict_SetItemString(idRefClassDict, "SYSCALL", idSyscallClass); /* Create the IDREF class */ PyObject *idRefClass = xPyClass_New(nullptr, idRefClassDict, idRefClassName); /* add all classes and constants into the triton module */ PyModule_AddObject(tritonModule, "IDREF", idRefClass); PyModule_AddObject(tritonModule, "UNSET", Py_BuildValue("k", UNSET)); // Py_BuildValue for unsigned long }