From 3a4a977678848d7284df585480e4056d5553a4f2 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Wed, 16 Jan 2019 18:53:37 +0100 Subject: Dealt with registers and register operands in the Python bindings. --- configure.ac | 1 + plugins/pychrysalide/arch/Makefile.am | 6 +- plugins/pychrysalide/arch/module.c | 7 + plugins/pychrysalide/arch/operands/Makefile.am | 23 ++ plugins/pychrysalide/arch/operands/module.c | 97 +++++++ plugins/pychrysalide/arch/operands/module.h | 42 +++ plugins/pychrysalide/arch/operands/register.c | 330 +++++++++++++++++++++++ plugins/pychrysalide/arch/operands/register.h | 45 ++++ plugins/pychrysalide/arch/register.c | 354 +++++++++++++++++++++++++ plugins/pychrysalide/arch/register.h | 51 ++++ plugins/pychrysalide/helpers.c | 27 ++ plugins/pychrysalide/helpers.h | 3 + src/arch/operands/register.c | 5 +- 13 files changed, 986 insertions(+), 5 deletions(-) create mode 100644 plugins/pychrysalide/arch/operands/Makefile.am create mode 100644 plugins/pychrysalide/arch/operands/module.c create mode 100644 plugins/pychrysalide/arch/operands/module.h create mode 100644 plugins/pychrysalide/arch/operands/register.c create mode 100644 plugins/pychrysalide/arch/operands/register.h create mode 100644 plugins/pychrysalide/arch/register.c create mode 100644 plugins/pychrysalide/arch/register.h diff --git a/configure.ac b/configure.ac index b514ea5..9effa17 100644 --- a/configure.ac +++ b/configure.ac @@ -382,6 +382,7 @@ AC_CONFIG_FILES([Makefile plugins/pychrysalide/analysis/disass/Makefile plugins/pychrysalide/analysis/types/Makefile plugins/pychrysalide/arch/Makefile + plugins/pychrysalide/arch/operands/Makefile plugins/pychrysalide/common/Makefile plugins/pychrysalide/core/Makefile plugins/pychrysalide/debug/Makefile diff --git a/plugins/pychrysalide/arch/Makefile.am b/plugins/pychrysalide/arch/Makefile.am index 0145db2..b47bf24 100644 --- a/plugins/pychrysalide/arch/Makefile.am +++ b/plugins/pychrysalide/arch/Makefile.am @@ -11,10 +11,12 @@ libpychrysaarch_la_SOURCES = \ operand.h operand.c \ processor.h processor.c \ raw.h raw.c \ + register.h register.c \ targetableop.h targetableop.c \ vmpa.h vmpa.c -libpychrysaarch_la_LIBADD = +libpychrysaarch_la_LIBADD = \ + operands/libpychrysaarchoperands.la libpychrysaarch_la_LDFLAGS = @@ -29,4 +31,4 @@ AM_CPPFLAGS = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) $(LIBPYGOBJE AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) -SUBDIRS = +SUBDIRS = operands diff --git a/plugins/pychrysalide/arch/module.c b/plugins/pychrysalide/arch/module.c index 40b8d71..d412ddc 100644 --- a/plugins/pychrysalide/arch/module.c +++ b/plugins/pychrysalide/arch/module.c @@ -40,8 +40,10 @@ #include "operand.h" #include "processor.h" #include "raw.h" +#include "register.h" #include "targetableop.h" #include "vmpa.h" +#include "operands/module.h" #include "../helpers.h" @@ -139,6 +141,8 @@ bool add_arch_module(PyObject *super) result = (module != NULL); + if (result) result = add_arch_operands_module(module); + if (result) result = py_base_define_constants(Py_TYPE(module)); return result; @@ -172,10 +176,13 @@ bool populate_arch_module(void) if (result) result = ensure_python_arch_operand_is_registered(); if (result) result = ensure_python_arch_processor_is_registered(); if (result) result = ensure_python_raw_instruction_is_registered(); + if (result) result = ensure_python_arch_register_is_registered(); if (result) result = ensure_python_targetable_operand_is_registered(); if (result) result = ensure_python_vmpa_is_registered(); if (result) result = ensure_python_mrange_is_registered(); + if (result) result = populate_arch_operands_module(); + assert(result); return result; diff --git a/plugins/pychrysalide/arch/operands/Makefile.am b/plugins/pychrysalide/arch/operands/Makefile.am new file mode 100644 index 0000000..94da1d1 --- /dev/null +++ b/plugins/pychrysalide/arch/operands/Makefile.am @@ -0,0 +1,23 @@ + +noinst_LTLIBRARIES = libpychrysaarchoperands.la + +libpychrysaarchoperands_la_SOURCES = \ + module.h module.c \ + register.h register.c + +libpychrysaarchoperands_la_LIBADD = + +libpychrysaarchoperands_la_LDFLAGS = + + +devdir = $(includedir)/chrysalide-$(subdir) + +dev_HEADERS = $(libpychrysaarchoperands_la_SOURCES:%c=) + + +AM_CPPFLAGS = $(LIBGTK_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \ + -I$(top_srcdir)/src + +AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS) $(COMPLIANCE_FLAGS) + +SUBDIRS = diff --git a/plugins/pychrysalide/arch/operands/module.c b/plugins/pychrysalide/arch/operands/module.c new file mode 100644 index 0000000..47a806a --- /dev/null +++ b/plugins/pychrysalide/arch/operands/module.c @@ -0,0 +1,97 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * module.c - intégration du répertoire arch en tant que module + * + * Copyright (C) 2018 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 "module.h" + + +#include + + +#include "register.h" +#include "../../helpers.h" + + + +/****************************************************************************** +* * +* Paramètres : super = module dont la définition est à compléter. * +* * +* Description : Ajoute le module 'arch.operands' à un module Python. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool add_arch_operands_module(PyObject *super) +{ + bool result; /* Bilan à retourner */ + PyObject *module; /* Sous-module mis en place */ + + static PyModuleDef py_chrysalide_arch_operands_module = { + + .m_base = PyModuleDef_HEAD_INIT, + + .m_name = "pychrysalide.arch.operands", + .m_doc = "Python module for Chrysalide.arch.operands", + + .m_size = -1, + + }; + + module = build_python_module(super, &py_chrysalide_arch_operands_module); + + result = (module != NULL); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Intègre les objets du module 'arch.operands'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool populate_arch_operands_module(void) +{ + bool result; /* Bilan à retourner */ + + result = true; + + if (result) result = ensure_python_register_operand_is_registered(); + + assert(result); + + return result; + +} diff --git a/plugins/pychrysalide/arch/operands/module.h b/plugins/pychrysalide/arch/operands/module.h new file mode 100644 index 0000000..f15e314 --- /dev/null +++ b/plugins/pychrysalide/arch/operands/module.h @@ -0,0 +1,42 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * module.h - prototypes pour l'intégration du répertoire operands en tant que module + * + * Copyright (C) 2018 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 + */ + + +#ifndef _PLUGINS_PYCHRYSALIDE_ARCH_OPERANDS_MODULE_H +#define _PLUGINS_PYCHRYSALIDE_ARCH_OPERANDS_MODULE_H + + +#include +#include + + + +/* Ajoute le module 'arch.operands' à un module Python. */ +bool add_arch_operands_module(PyObject *); + +/* Intègre les objets du module 'arch.operands'. */ +bool populate_arch_operands_module(void); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_ARCH_OPERANDS_MODULE_H */ diff --git a/plugins/pychrysalide/arch/operands/register.c b/plugins/pychrysalide/arch/operands/register.c new file mode 100644 index 0000000..74b6a4f --- /dev/null +++ b/plugins/pychrysalide/arch/operands/register.c @@ -0,0 +1,330 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * register.c - équivalent Python du fichier "arch/operands/register.c" + * + * Copyright (C) 2018 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 "register.h" + + +#include + + +#include +#include +#include + + +#include "../operand.h" +#include "../register.h" +#include "../../access.h" +#include "../../helpers.h" + + + +/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ + + +/* Accompagne la création d'une instance dérivée en Python. */ +static PyObject *py_register_operand_new(PyTypeObject *, PyObject *, PyObject *); + +/* Initialise la classe des descriptions de fichier binaire. */ +static void py_register_operand_init_gclass(GRegisterOperandClass *, gpointer); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_register_operand_init(PyObject *self, PyObject *args, PyObject *kwds); + + + +/* ------------------------- REGISTRE SOUS FORME D'OPERANDE ------------------------- */ + + +/* Fournit le registre associé à l'opérande. */ +static PyObject *py_register_operand_get_register(PyObject *, void *); + + + +/* ---------------------------------------------------------------------------------- */ +/* GLUE POUR CREATION DEPUIS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : type = type du nouvel objet à mettre en place. * +* args = éventuelle liste d'arguments. * +* kwds = éventuel dictionnaire de valeurs mises à disposition. * +* * +* Description : Accompagne la création d'une instance dérivée en Python. * +* * +* Retour : Nouvel objet Python mis en place ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_register_operand_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *result; /* Objet à retourner */ + PyTypeObject *base; /* Type de base à dériver */ + bool first_time; /* Evite les multiples passages*/ + GType gtype; /* Nouveau type de processeur */ + bool status; /* Bilan d'un enregistrement */ + + /* Validations diverses */ + + base = get_python_register_operand_type(); + + if (type == base) + goto simple_way; + + /* Mise en place d'un type dédié */ + + first_time = (g_type_from_name(type->tp_name) == 0); + + gtype = built_dynamic_type(G_TYPE_REGISTER_OPERAND, type->tp_name, + (GClassInitFunc)py_register_operand_init_gclass, NULL); + + if (first_time) + { + status = register_class_for_dynamic_pygobject(gtype, type, base); + + if (!status) + { + result = NULL; + goto exit; + } + + } + + /* On crée, et on laisse ensuite la main à PyGObject_Type.tp_init() */ + + simple_way: + + result = PyType_GenericNew(type, args, kwds); + + exit: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : class = classe à initialiser. * +* unused = données non utilisées ici. * +* * +* Description : Initialise la classe des descriptions de fichier binaire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_register_operand_init_gclass(GRegisterOperandClass *class, gpointer unused) +{ + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet à initialiser (théoriquement). * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Initialise une instance sur la base du dérivé de GObject. * +* * +* Retour : 0. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_register_operand_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + GArchRegister *reg; /* Registre brut transmis */ + int ret; /* Bilan de lecture des args. */ + PyObject *new_args; /* Nouveaux arguments épurés */ + PyObject *new_kwds; /* Nouveau dictionnaire épuré */ + GRegisterOperand *operand; /* Opérande à manipuler */ + + /* Récupération des paramètres */ + + ret = PyArg_ParseTuple(args, "O&", convert_to_arch_register, ®); + if (!ret) return -1; + + /* Initialisation d'un objet GLib */ + + new_args = PyTuple_New(0); + new_kwds = PyDict_New(); + + ret = PyGObject_Type.tp_init(self, new_args, new_kwds); + + Py_DECREF(new_kwds); + Py_DECREF(new_args); + + if (ret == -1) return -1; + + /* Eléments de base */ + + operand = G_REGISTER_OPERAND(pygobject_get(self)); + + g_object_ref(G_OBJECT(reg)); + operand->reg = reg; + + return 0; + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* REGISTRE SOUS FORME D'OPERANDE */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Fournit le registre associé à l'opérande. * +* * +* Retour : Représentation interne du registre. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_register_operand_get_register(PyObject *self, void *closure) +{ + PyObject *result; /* Instance Python à retourner */ + GRegisterOperand *operand; /* Version GLib de l'opérande */ + GArchRegister *reg; /* Registre lié à l'opérande */ + + operand = G_REGISTER_OPERAND(pygobject_get(self)); + + reg = g_register_operand_get_register(operand); + + if (reg == NULL) + { + result = Py_None; + Py_INCREF(result); + } + + else + { + result = pygobject_new(G_OBJECT(reg)); + g_object_unref(reg); + } + + 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_register_operand_type(void) +{ + static PyMethodDef py_register_operand_methods[] = { + { NULL } + }; + + static PyGetSetDef py_register_operand_getseters[] = { + { + "register", py_register_operand_get_register, NULL, + "Provide the register used by the operand.", NULL + }, + { NULL } + }; + + static PyTypeObject py_register_operand_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.arch.operands.RegisterOperand", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyChrysalide instruction register operand.", + + .tp_methods = py_register_operand_methods, + .tp_getset = py_register_operand_getseters, + + .tp_init = py_register_operand_init, + .tp_new = py_register_operand_new, + + }; + + return &py_register_operand_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.arch.ArchOperand'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_register_operand_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'ArchOperand' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_register_operand_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.arch.operands"); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_REGISTER_OPERAND, type, get_python_arch_operand_type())) + return false; + + } + + return true; + +} diff --git a/plugins/pychrysalide/arch/operands/register.h b/plugins/pychrysalide/arch/operands/register.h new file mode 100644 index 0000000..e960067 --- /dev/null +++ b/plugins/pychrysalide/arch/operands/register.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * register.h - prototypes pour l'équivalent Python du fichier "arch/operands/register.h" + * + * Copyright (C) 2018 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 + */ + + +#ifndef _PLUGINS_PYCHRYSALIDE_ARCH_OPERANDS_REGISTER_H +#define _PLUGINS_PYCHRYSALIDE_ARCH_OPERANDS_REGISTER_H + + +#include +#include + + + +/* ------------------------- REGISTRE SOUS FORME D'OPERANDE ------------------------- */ + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_register_operand_type(void); + +/* Prend en charge l'objet 'pychrysalide.arch.operands.RegisterOperand'. */ +bool ensure_python_register_operand_is_registered(void); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_ARCH_OPERANDS_REGISTER_H */ diff --git a/plugins/pychrysalide/arch/register.c b/plugins/pychrysalide/arch/register.c new file mode 100644 index 0000000..0299fa6 --- /dev/null +++ b/plugins/pychrysalide/arch/register.c @@ -0,0 +1,354 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * register.c - équivalent Python du fichier "arch/register.c" + * + * Copyright (C) 2018 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 "register.h" + + +#include +#include + + +#include +#include +#include + + +#include "../access.h" +#include "../helpers.h" + + + +/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ + + +/* Accompagne la création d'une instance dérivée en Python. */ +static PyObject *py_arch_register_new(PyTypeObject *, PyObject *, PyObject *); + +/* Initialise la classe des descriptions de fichier binaire. */ +static void py_arch_register_init_gclass(GArchRegisterClass *, gpointer); + +/* Indique si le registre correspond à ebp ou similaire. */ +static bool g_arch_register_is_base_pointer_wrapper(const GArchRegister *); + + + +/* ---------------------------- PUR REGISTRE DU MATERIEL ---------------------------- */ + + +/* Indique si le registre correspond à ebp ou similaire. */ +static PyObject *py_arch_register_is_base_pointer(PyObject *, void *); + + + +/* ---------------------------------------------------------------------------------- */ +/* GLUE POUR CREATION DEPUIS PYTHON */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : type = type du nouvel objet à mettre en place. * +* args = éventuelle liste d'arguments. * +* kwds = éventuel dictionnaire de valeurs mises à disposition. * +* * +* Description : Accompagne la création d'une instance dérivée en Python. * +* * +* Retour : Nouvel objet Python mis en place ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_arch_register_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *result; /* Objet à retourner */ + PyTypeObject *base; /* Type de base à dériver */ + bool first_time; /* Evite les multiples passages*/ + GType gtype; /* Nouveau type de registre */ + bool status; /* Bilan d'un enregistrement */ + + /* Validations diverses */ + + base = get_python_arch_register_type(); + + if (type == base) + { + result = NULL; + PyErr_Format(PyExc_RuntimeError, _("%s is an abstract class"), type->tp_name); + goto exit; + } + + /* Mise en place d'un type dédié */ + + first_time = (g_type_from_name(type->tp_name) == 0); + + gtype = built_dynamic_type(G_TYPE_ARCH_REGISTER, type->tp_name, + (GClassInitFunc)py_arch_register_init_gclass, NULL); + + if (first_time) + { + status = register_class_for_dynamic_pygobject(gtype, type, base); + + if (!status) + { + result = NULL; + goto exit; + } + + } + + /* On crée, et on laisse ensuite la main à PyGObject_Type.tp_init() */ + + result = PyType_GenericNew(type, args, kwds); + + exit: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : class = classe à initialiser. * +* unused = données non utilisées ici. * +* * +* Description : Initialise la classe des descriptions de fichier binaire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void py_arch_register_init_gclass(GArchRegisterClass *class, gpointer unused) +{ + class->is_bp = g_arch_register_is_base_pointer_wrapper; + +} + + +/****************************************************************************** +* * +* Paramètres : reg = registre visé par la procédure. * +* * +* Description : Indique si le registre correspond à ebp ou similaire. * +* * +* Retour : true si la correspondance est avérée, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool g_arch_register_is_base_pointer_wrapper(const GArchRegister *reg) +{ + bool result; /* Bilan à renvoyer */ + PyObject *pyobj; /* Objet Python concerné */ + PyObject *pyret; /* Bilan de consultation */ + + pyobj = pygobject_new(G_OBJECT(reg)); + + assert(has_python_method(pyobj, "_is_base_pointer")); + + pyret = run_python_method(pyobj, "_is_base_pointer", NULL); + + result = (pyret == Py_True); + + Py_XDECREF(pyret); + + return result; + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* PUR REGISTRE DU MATERIEL */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* closure = non utilisé ici. * +* * +* Description : Indique si le registre correspond à ebp ou similaire. * +* * +* Retour : True si la correspondance est avérée, False sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_arch_register_is_base_pointer(PyObject *self, void *closure) +{ + PyObject *result; /* Instance Python à retourner */ + GArchRegister *reg; /* Registre visé */ + bool status; /* Bilan de consultation */ + + reg = G_ARCH_REGISTER(pygobject_get(self)); + + status = g_arch_register_is_base_pointer(reg); + + result = status ? Py_True : Py_False; + Py_INCREF(result); + + 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_register_type(void) +{ + static PyMethodDef py_arch_register_methods[] = { + { + "_is_base_pointer", not_yet_implemented_method, + METH_NOARGS, + "_is_base_pointer($self, /)\n--\n\nDefine an implementation to provide the relative member value." + }, + { NULL } + }; + + static PyGetSetDef py_arch_register_getseters[] = { + { + "is_base_pointer", py_arch_register_is_base_pointer, NULL, + "Tell if the register is a base pointer or not.", NULL + }, + { NULL } + }; + + static PyTypeObject py_arch_register_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.arch.ArchRegister", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IS_ABSTRACT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyChrysalide register for a given architecture.", + + .tp_methods = py_arch_register_methods, + .tp_getset = py_arch_register_getseters, + + .tp_new = py_arch_register_new, + + }; + + return &py_arch_register_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.arch.ArchRegister'. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_arch_register_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'ArchRegister' */ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_arch_register_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.arch"); + + dict = PyModule_GetDict(module); + + if (!register_class_for_pygobject(dict, G_TYPE_ARCH_REGISTER, type, &PyGObject_Type)) + return false; + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en registre d'architecture. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_arch_register(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_arch_register_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to architecture register"); + break; + + case 1: + *((GArchRegister **)dst) = G_ARCH_REGISTER(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/arch/register.h b/plugins/pychrysalide/arch/register.h new file mode 100644 index 0000000..b2d9ec0 --- /dev/null +++ b/plugins/pychrysalide/arch/register.h @@ -0,0 +1,51 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * register.h - prototypes pour l'équivalent Python du fichier "arch/register.h" + * + * Copyright (C) 2018 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 + */ + + +#ifndef _PLUGINS_PYCHRYSALIDE_ARCH_REGISTER_H +#define _PLUGINS_PYCHRYSALIDE_ARCH_REGISTER_H + + +#include +#include + + +#include + + + +/* ---------------------------- PUR REGISTRE DU MATERIEL ---------------------------- */ + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_arch_register_type(void); + +/* Prend en charge l'objet 'pychrysalide.arch.ArchRegister'. */ +bool ensure_python_arch_register_is_registered(void); + +/* Tente de convertir en registre d'architecture. */ +int convert_to_arch_register(PyObject *, void *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_ARCH_REGISTER_H */ diff --git a/plugins/pychrysalide/helpers.c b/plugins/pychrysalide/helpers.c index a930097..7e31fa7 100644 --- a/plugins/pychrysalide/helpers.c +++ b/plugins/pychrysalide/helpers.c @@ -31,6 +31,7 @@ #include +#include #include @@ -484,6 +485,32 @@ bool register_python_module_object(PyObject *module, PyTypeObject *type) /****************************************************************************** * * +* Paramètres : self = objet quelconque dont le code Python hérite. * +* args = série d'arguments si présents. * +* * +* Description : Marque l'absence d'implémentation pour une méthode donnée. * +* * +* Retour : NULL pour la levée d'exception. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyObject *not_yet_implemented_method(PyObject *self, PyObject *args) +{ + PyObject *result; /* Exception à retourner */ + + result = NULL; + + PyErr_SetString(PyExc_NotImplementedError, _("Implementated method is missing")); + + return result; + +} + + +/****************************************************************************** +* * * Paramètres : spec = définition à mettre en place dynamiquement. * * * * Description : Définit dans le tas de Python un nouveau type. * diff --git a/plugins/pychrysalide/helpers.h b/plugins/pychrysalide/helpers.h index bf95e61..ce5840b 100644 --- a/plugins/pychrysalide/helpers.h +++ b/plugins/pychrysalide/helpers.h @@ -89,6 +89,9 @@ bool register_python_module_object(PyObject *, PyTypeObject *); #define APPLY_ABSTRACT_FLAG(tp) tp->tp_new = PyBaseObject_Type.tp_new +/* Marque l'absence d'implémentation pour une méthode donnée. */ +PyObject *not_yet_implemented_method(PyObject *, PyObject *); + /* Définit dans le tas de Python un nouveau type. */ PyTypeObject *define_python_dynamic_type(const PyTypeObject *); diff --git a/src/arch/operands/register.c b/src/arch/operands/register.c index 508886b..6196991 100644 --- a/src/arch/operands/register.c +++ b/src/arch/operands/register.c @@ -140,8 +140,7 @@ static void g_register_operand_init(GRegisterOperand *operand) static void g_register_operand_dispose(GRegisterOperand *operand) { - if (operand->reg != NULL) - g_object_unref(G_OBJECT(operand->reg)); + g_clear_object(&operand->reg); G_OBJECT_CLASS(g_register_operand_parent_class)->dispose(G_OBJECT(operand)); @@ -215,7 +214,7 @@ static void g_register_operand_print(const GRegisterOperand *operand, GBufferLin * * * Paramètres : operand = opérande représentant un registre. * * * -* Description : Fournit le registre Dalvik associé à l'opérande. * +* Description : Fournit le registre associé à l'opérande. * * * * Retour : Représentation interne du registre. * * * -- cgit v0.11.2-87-g4458