/* Chrysalide - Outil d'analyse de fichiers binaires * instruction.c - équivalent Python du fichier "arch/instruction.h" * * Copyright (C) 2012-2017 Cyrille Bagard * * This file is part of Chrysalide. * * Chrysalide is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Chrysalide is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "instruction.h" #include #include #include #include "vmpa.h" #include "../access.h" #include "../helpers.h" #include "../glibext/linegen.h" /* ------------------- DEFINITION DES LIAISONS ENTRE INSTRUCTIONS ------------------- */ /* --------------------- INSTRUCTIONS D'ARCHITECTURES EN PYTHON --------------------- */ /* Fournit l'identifiant unique pour un ensemble d'instructions. */ static PyObject *py_arch_instruction_get_unique_id(PyObject *, void *); /* Fournit la place mémoire d'une instruction. */ static PyObject *py_arch_instruction_get_range(PyObject *, void *); /* Définit la localisation d'une instruction. */ static int py_arch_instruction_set_range(PyObject *, PyObject *, void *); /* Fournit le nom humain de l'instruction manipulée. */ static PyObject *py_arch_instruction_get_keyword(PyObject *, void *); /* Fournit tous les opérandes d'une instruction. */ static PyObject *py_arch_instruction_get_operands(PyObject *, void *); /* Définit les constantes pour les instructions. */ static bool py_arch_instruction_define_constants(PyTypeObject *); /* ---------------------------------------------------------------------------------- */ /* DEFINITION DES LIAISONS ENTRE INSTRUCTIONS */ /* ---------------------------------------------------------------------------------- */ /* Fournit les origines d'une instruction donnée. */ static PyObject *py_arch_instruction_get_sources(PyObject *, void *); /* Fournit les destinations d'une instruction donnée. */ static PyObject *py_arch_instruction_get_destinations(PyObject *, void *); /****************************************************************************** * * * Paramètres : self = instruction d'architecture à manipuler. * * unused = adresse non utilisée ici. * * * * Description : Fournit les origines d'une instruction donnée. * * * * Retour : Nombre de ces origines. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_instruction_get_sources(PyObject *self, void *unused) { PyObject *result; /* Instance à retourner */ GArchInstruction *instr; /* Version native */ const instr_link_t *source; /* Origine des liens */ size_t count; /* Nombre de liens présents */ size_t i; /* Boucle de parcours */ PyObject *linked; /* Source de lien Python */ PyObject *type; /* Nature du lien en Python */ #ifndef NDEBUG int ret; /* Bilan d'une écriture d'arg. */ #endif instr = G_ARCH_INSTRUCTION(pygobject_get(self)); g_arch_instruction_lock_src(instr); count = g_arch_instruction_count_sources(instr); result = PyTuple_New(count); for (i = 0; i < count; i++) { source = g_arch_instruction_get_source(instr, i); linked = pygobject_new(G_OBJECT(source->linked)); type = PyLong_FromLong(source->type); #ifndef NDEBUG ret = PyTuple_SetItem(result, i, Py_BuildValue("(OO)", linked, type)); assert(ret == 0); #else PyTuple_SetItem(result, i, Py_BuildValue("(OO)", linked, type)); #endif unref_instr_link(source); } g_arch_instruction_unlock_src(instr); return result; } /****************************************************************************** * * * Paramètres : self = instruction d'architecture à manipuler. * * unused = adresse non utilisée ici. * * * * Description : Fournit les destinations d'une instruction donnée. * * * * Retour : Nombre de ces destinations. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_instruction_get_destinations(PyObject *self, void *unused) { PyObject *result; /* Instance à retourner */ GArchInstruction *instr; /* Version native */ const instr_link_t *dest; /* Destination des liens */ size_t count; /* Nombre de liens présents */ size_t i; /* Boucle de parcours */ PyObject *linked; /* Destination de lien Python */ PyObject *type; /* Nature du lien en Python */ #ifndef NDEBUG int ret; /* Bilan d'une écriture d'arg. */ #endif instr = G_ARCH_INSTRUCTION(pygobject_get(self)); g_arch_instruction_lock_dest(instr); count = g_arch_instruction_count_destinations(instr); result = PyTuple_New(count); for (i = 0; i < count; i++) { dest = g_arch_instruction_get_destination(instr, i); linked = pygobject_new(G_OBJECT(dest->linked)); type = PyLong_FromLong(dest->type); #ifndef NDEBUG ret = PyTuple_SetItem(result, i, Py_BuildValue("(OO)", linked, type)); assert(ret == 0); #else PyTuple_SetItem(result, i, Py_BuildValue("(OO)", linked, type)); #endif unref_instr_link(dest); } g_arch_instruction_unlock_dest(instr); return result; } /****************************************************************************** * * * Paramètres : self = classe représentant une instruction. * * closure = adresse non utilisée ici. * * * * Description : Fournit l'identifiant unique pour un ensemble d'instructions.* * * * Retour : Identifiant unique par type d'instruction et architecture. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_instruction_get_unique_id(PyObject *self, void *closure) { PyObject *result; /* Conversion à retourner */ GArchInstruction *instr; /* Version native */ itid_t uid; /* Identifiant unique associé */ instr = G_ARCH_INSTRUCTION(pygobject_get(self)); uid = g_arch_instruction_get_unique_id(instr); result = PyLong_FromUnsignedLong(uid); return result; } /****************************************************************************** * * * Paramètres : self = classe représentant une instruction. * * closure = adresse non utilisée ici. * * * * Description : Fournit la place mémoire d'une instruction. * * * * Retour : Valeur associée à la propriété consultée. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_instruction_get_range(PyObject *self, void *closure) { PyObject *result; /* Conversion à retourner */ GArchInstruction *instr; /* Version native */ const mrange_t *range; /* Espace mémoire à exporter */ instr = G_ARCH_INSTRUCTION(pygobject_get(self)); range = g_arch_instruction_get_range(instr); result = build_from_internal_mrange(range); return result; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * value = valeur fournie à intégrer ou prendre en compte. * * closure = adresse non utilisée ici. * * * * Description : Définit la localisation d'une instruction. * * * * Retour : Bilan de l'opération pour Python. * * * * Remarques : - * * * ******************************************************************************/ static int py_arch_instruction_set_range(PyObject *self, PyObject *value, void *closure) { int ret; /* Bilan d'analyse */ mrange_t *range; /* Espace mémoire à manipuler */ GArchInstruction *instr; /* Version native */ ret = PyObject_IsInstance(value, (PyObject *)get_python_mrange_type()); if (!ret) return -1; range = get_internal_mrange(value); instr = G_ARCH_INSTRUCTION(pygobject_get(self)); g_arch_instruction_set_range(instr, range); return 0; } /****************************************************************************** * * * Paramètres : self = classe représentant une instruction. * * unused = adresse non utilisée ici. * * * * Description : Fournit le nom humain de l'instruction manipulée. * * * * Retour : Valeur associée à la propriété consultée. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_instruction_get_keyword(PyObject *self, void *unused) { PyObject *result; /* Trouvailles à retourner */ GArchInstruction *instr; /* Version native */ const char *kw; /* Valeur récupérée */ instr = G_ARCH_INSTRUCTION(pygobject_get(self)); kw = g_arch_instruction_get_keyword(instr); result = PyUnicode_FromString(kw); return result; } /****************************************************************************** * * * Paramètres : self = objet représentant une instruction. * * unused = adresse non utilisée ici. * * * * Description : Fournit tous les opérandes d'une instruction. * * * * Retour : Valeur associée à la propriété consultée. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_instruction_get_operands(PyObject *self, void *unused) { PyObject *result; /* Instance à retourner */ GArchInstruction *instr; /* Version native */ size_t count; /* Nombre d'opérandes présents */ size_t i; /* Boucle de parcours */ GArchOperand *operand; /* Opérande à manipuler */ PyObject *opobj; /* Version Python */ #ifndef NDEBUG int ret; /* Bilan d'une écriture d'arg. */ #endif instr = G_ARCH_INSTRUCTION(pygobject_get(self)); g_arch_instruction_lock_operands(instr); count = _g_arch_instruction_count_operands(instr); result = PyTuple_New(count); for (i = 0; i < count; i++) { operand = _g_arch_instruction_get_operand(instr, i); opobj = pygobject_new(G_OBJECT(operand)); #ifndef NDEBUG ret = PyTuple_SetItem(result, i, Py_BuildValue("O", opobj)); assert(ret == 0); #else PyTuple_SetItem(result, i, Py_BuildValue("O", opobj)); #endif g_object_unref(G_OBJECT(operand)); } g_arch_instruction_unlock_operands(instr); return result; } /****************************************************************************** * * * Paramètres : obj_type = type dont le dictionnaire est à compléter. * * * * Description : Définit les constantes pour les instructions. * * * * Retour : true en cas de succès de l'opération, false sinon. * * * * Remarques : - * * * ******************************************************************************/ static bool py_arch_instruction_define_constants(PyTypeObject *obj_type) { bool result; /* Bilan à retourner */ result = true; result &= PyDict_AddIntMacro(obj_type, ILT_EXEC_FLOW); result &= PyDict_AddIntMacro(obj_type, ILT_JUMP); result &= PyDict_AddIntMacro(obj_type, ILT_CASE_JUMP); result &= PyDict_AddIntMacro(obj_type, ILT_JUMP_IF_TRUE); result &= PyDict_AddIntMacro(obj_type, ILT_JUMP_IF_FALSE); result &= PyDict_AddIntMacro(obj_type, ILT_LOOP); result &= PyDict_AddIntMacro(obj_type, ILT_CALL); result &= PyDict_AddIntMacro(obj_type, ILT_CATCH_EXCEPTION); result &= PyDict_AddIntMacro(obj_type, ILT_REF); return result; } /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * * * * Retour : Définition d'objet pour Python. * * * * Remarques : - * * * ******************************************************************************/ PyTypeObject *get_python_arch_instruction_type(void) { static PyMethodDef py_arch_instruction_methods[] = { { NULL } }; static PyGetSetDef py_arch_instruction_getseters[] = { { "uid", py_arch_instruction_get_unique_id, NULL, "Provide the unique identification number given to this kind of instruction.", NULL }, { "range", py_arch_instruction_get_range, py_arch_instruction_set_range, "Give access to the memory range covered by the current instruction.", NULL }, { "keyword", (getter)py_arch_instruction_get_keyword, (setter)NULL, "Give le name of the assembly instruction.", NULL }, { "operands", (getter)py_arch_instruction_get_operands, (setter)NULL, "Provide the list of instruction attached operands.", NULL }, { "sources", (getter)py_arch_instruction_get_sources, (setter)NULL, "Provide the instructions list driving to the current instruction." }, { "destinations", (getter)py_arch_instruction_get_destinations, (setter)NULL, "Provide the instructions list following the current instruction." }, { NULL } }; static PyTypeObject py_arch_instruction_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.arch.ArchInstruction", .tp_basicsize = sizeof(PyGObject), .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IS_ABSTRACT | Py_TPFLAGS_BASETYPE, .tp_doc = "PyChrysalide instruction for a given architecture.", .tp_methods = py_arch_instruction_methods, .tp_getset = py_arch_instruction_getseters, }; return &py_arch_instruction_type; } /****************************************************************************** * * * Paramètres : module = module dont la définition est à compléter. * * * * Description : Prend en charge l'objet 'pychrysalide.arch.ArchInstruction'. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool ensure_python_arch_instruction_is_registered(void) { PyTypeObject *type; /* Type Python 'ArchInstruc...'*/ PyObject *module; /* Module à recompléter */ PyObject *dict; /* Dictionnaire du module */ type = get_python_arch_instruction_type(); if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) { APPLY_ABSTRACT_FLAG(type); module = get_access_to_python_module("pychrysalide.arch"); dict = PyModule_GetDict(module); if (!ensure_python_line_generator_is_registered()) return false; if (!_register_class_for_pygobject(dict, G_TYPE_ARCH_INSTRUCTION, type, &PyGObject_Type, get_python_line_generator_type(), NULL)) return false; if (!py_arch_instruction_define_constants(type)) return false; } return true; }