summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/arch
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2018-12-07 20:33:21 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2018-12-07 20:33:21 (GMT)
commitc0af4cc392a246b05d41b7b7c05bbcd8b0cfb39e (patch)
tree0d645c71c40fc4b625271d0ab1ef8e7ebab04075 /plugins/pychrysalide/arch
parenta13d12c758184449bf3305785ef33273802a761c (diff)
Relied on GObject introspection and dynamic gtypes to inherit in Python.
Diffstat (limited to 'plugins/pychrysalide/arch')
-rw-r--r--plugins/pychrysalide/arch/Makefile.am1
-rw-r--r--plugins/pychrysalide/arch/context.c163
-rw-r--r--plugins/pychrysalide/arch/context.h45
-rw-r--r--plugins/pychrysalide/arch/instruction.c45
-rw-r--r--plugins/pychrysalide/arch/instruction.h3
-rw-r--r--plugins/pychrysalide/arch/module.c2
-rw-r--r--plugins/pychrysalide/arch/processor.c359
7 files changed, 618 insertions, 0 deletions
diff --git a/plugins/pychrysalide/arch/Makefile.am b/plugins/pychrysalide/arch/Makefile.am
index 6210ed3..0145db2 100644
--- a/plugins/pychrysalide/arch/Makefile.am
+++ b/plugins/pychrysalide/arch/Makefile.am
@@ -2,6 +2,7 @@
noinst_LTLIBRARIES = libpychrysaarch.la
libpychrysaarch_la_SOURCES = \
+ context.h context.c \
feeder.h feeder.c \
immediate.h immediate.c \
instriter.h instriter.c \
diff --git a/plugins/pychrysalide/arch/context.c b/plugins/pychrysalide/arch/context.c
new file mode 100644
index 0000000..4428075
--- /dev/null
+++ b/plugins/pychrysalide/arch/context.c
@@ -0,0 +1,163 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * context.c - équivalent Python du fichier "arch/context.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
+ */
+
+
+#include "context.h"
+
+
+#include <assert.h>
+#include <pygobject.h>
+
+
+#include <arch/context.h>
+
+
+#include "../access.h"
+#include "../helpers.h"
+
+
+
+/******************************************************************************
+* *
+* Paramètres : - *
+* *
+* Description : Fournit un accès à une définition de type à diffuser. *
+* *
+* Retour : Définition d'objet pour Python. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+PyTypeObject *get_python_proc_context_type(void)
+{
+ static PyMethodDef py_proc_context_methods[] = {
+ { NULL }
+ };
+
+ static PyGetSetDef py_proc_context_getseters[] = {
+ { NULL }
+ };
+
+ static PyTypeObject py_proc_context_type = {
+
+ PyVarObject_HEAD_INIT(NULL, 0)
+
+ .tp_name = "pychrysalide.arch.ProcContext",
+ .tp_basicsize = sizeof(PyGObject),
+
+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IS_ABSTRACT | Py_TPFLAGS_BASETYPE,
+
+ .tp_doc = "PyChrysalide disassembly context.",
+
+ .tp_methods = py_proc_context_methods,
+ .tp_getset = py_proc_context_getseters,
+
+ };
+
+ return &py_proc_context_type;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : module = module dont la définition est à compléter. *
+* *
+* Description : Prend en charge l'objet 'pychrysalide.arch.ArchContext'. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool ensure_python_proc_context_is_registered(void)
+{
+ PyTypeObject *type; /* Type Python 'ArchContext' */
+ PyObject *module; /* Module à recompléter */
+ PyObject *dict; /* Dictionnaire du module */
+
+ type = get_python_proc_context_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 (!register_class_for_pygobject(dict, G_TYPE_PROC_CONTEXT, 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 contexte de désassemblage. *
+* *
+* Retour : Bilan de l'opération, voire indications supplémentaires. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+int convert_to_proc_context(PyObject *arg, void *dst)
+{
+ int result; /* Bilan à retourner */
+
+ result = PyObject_IsInstance(arg, (PyObject *)get_python_proc_context_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 disassembly context");
+ break;
+
+ case 1:
+ *((GProcContext **)dst) = G_PROC_CONTEXT(pygobject_get(arg));
+ break;
+
+ default:
+ assert(false);
+ break;
+
+ }
+
+ return result;
+
+}
diff --git a/plugins/pychrysalide/arch/context.h b/plugins/pychrysalide/arch/context.h
new file mode 100644
index 0000000..eb5f0c1
--- /dev/null
+++ b/plugins/pychrysalide/arch/context.h
@@ -0,0 +1,45 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * context.h - prototypes pour l'équivalent Python du fichier "arch/context.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_CONTEXT_H
+#define _PLUGINS_PYCHRYSALIDE_ARCH_CONTEXT_H
+
+
+#include <Python.h>
+#include <stdbool.h>
+
+
+
+/* Fournit un accès à une définition de type à diffuser. */
+PyTypeObject *get_python_proc_context_type(void);
+
+/* Prend en charge l'objet 'pychrysalide.arch.ProcContext'. */
+bool ensure_python_proc_context_is_registered(void);
+
+/* Tente de convertir en contexte de désassemblage. */
+int convert_to_proc_context(PyObject *, void *);
+
+
+
+#endif /* _PLUGINS_PYCHRYSALIDE_ARCH_CONTEXT_H */
diff --git a/plugins/pychrysalide/arch/instruction.c b/plugins/pychrysalide/arch/instruction.c
index 4202625..6399d63 100644
--- a/plugins/pychrysalide/arch/instruction.c
+++ b/plugins/pychrysalide/arch/instruction.c
@@ -568,3 +568,48 @@ bool ensure_python_arch_instruction_is_registered(void)
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 instruction d'architecture. *
+* *
+* Retour : Bilan de l'opération, voire indications supplémentaires. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+int convert_to_arch_instruction(PyObject *arg, void *dst)
+{
+ int result; /* Bilan à retourner */
+
+ result = PyObject_IsInstance(arg, (PyObject *)get_python_arch_instruction_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 arch instruction");
+ break;
+
+ case 1:
+ *((GArchInstruction **)dst) = G_ARCH_INSTRUCTION(pygobject_get(arg));
+ break;
+
+ default:
+ assert(false);
+ break;
+
+ }
+
+ return result;
+
+}
diff --git a/plugins/pychrysalide/arch/instruction.h b/plugins/pychrysalide/arch/instruction.h
index 5433222..fea329f 100644
--- a/plugins/pychrysalide/arch/instruction.h
+++ b/plugins/pychrysalide/arch/instruction.h
@@ -37,6 +37,9 @@ PyTypeObject *get_python_arch_instruction_type(void);
/* Prend en charge l'objet 'pychrysalide.arch.ArchInstruction'. */
bool ensure_python_arch_instruction_is_registered(void);
+/* Tente de convertir en instruction d'architecture. */
+int convert_to_arch_instruction(PyObject *, void *);
+
#endif /* _PLUGINS_PYCHRYSALIDE_ARCH_INSTRUCTION_H */
diff --git a/plugins/pychrysalide/arch/module.c b/plugins/pychrysalide/arch/module.c
index 8bd6df6..db514ef 100644
--- a/plugins/pychrysalide/arch/module.c
+++ b/plugins/pychrysalide/arch/module.c
@@ -32,6 +32,7 @@
#include <common/endianness.h>
+#include "context.h"
#include "feeder.h"
#include "immediate.h"
#include "instriter.h"
@@ -163,6 +164,7 @@ bool populate_arch_module(void)
result = true;
+ if (result) result = ensure_python_proc_context_is_registered();
if (result) result = ensure_python_proxy_feeder_is_registered();
if (result) result = ensure_python_imm_operand_is_registered();
if (result) result = ensure_python_instr_iterator_is_registered();
diff --git a/plugins/pychrysalide/arch/processor.c b/plugins/pychrysalide/arch/processor.c
index 7ff374b..2ecf6b8 100644
--- a/plugins/pychrysalide/arch/processor.c
+++ b/plugins/pychrysalide/arch/processor.c
@@ -30,19 +30,35 @@
#include <i18n.h>
+#include <arch/processor-int.h>
+#include "context.h"
#include "instriter.h"
#include "instruction.h"
#include "vmpa.h"
#include "../access.h"
+#include "../dt.h"
#include "../helpers.h"
+#include "../analysis/content.h"
+#include "../format/executable.h"
+/* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */
+/* Accompagne la création d'une instance dérivée en Python. */
+static PyObject *py_arch_processor_new(PyTypeObject *, PyObject *, PyObject *);
+/* Initialise la classe des descriptions de fichier binaire. */
+static void py_arch_processor_init_gclass(GArchProcessorClass *, gpointer);
+
+/* Fournit un contexte propre au processeur d'une architecture. */
+static GProcContext *py_arch_processor_get_context_wrapper(const GArchProcessor *);
+
+/* Désassemble une instruction dans un flux de données. */
+static GArchInstruction *py_arch_processor_disassemble_wrapper(const GArchProcessor *, GProcContext *, const GBinContent *, vmpa2t *, GExeFormat *);
@@ -61,6 +77,11 @@ static PyObject *py_arch_processor_get_instruction_min_size(PyObject *, void *);
/* Indique si l'architecture possède un espace virtuel ou non. */
static PyObject *py_arch_processor_has_virtual_space(PyObject *, void *);
+/* Fournit un contexte propre au processeur d'une architecture. */
+static PyObject *py_arch_processor_get_context(PyObject *, PyObject *);
+
+/* Désassemble une instruction dans un flux de données. */
+static PyObject *py_arch_processor_disassemble(PyObject *, PyObject *);
@@ -89,6 +110,7 @@ static PyObject *py_arch_processor_find_instr_by_addr(PyObject *, PyObject *);
+/* ---------------------------- DEFINITION DE PROCESSEUR ---------------------------- */
/* Définit les constantes pour les types d'erreurs. */
@@ -96,7 +118,239 @@ static bool define_python_arch_processor_constants(PyTypeObject *);
+/* ---------------------------------------------------------------------------------- */
+/* 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_processor_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+ PyObject *result; /* Objet à retourner */
+ bool abstract; /* Validation du type parent */
+ GType gtype; /* Nouveau type de processeur */
+ PyObject *sys_mod_dict; /* Dictionnaire des modules */
+ PyObject *modname; /* Nom du module du type */
+ PyObject *module; /* Module à recompléter */
+ PyObject *dict; /* Dictionnaire dudit module */
+
+ /* Validations diverses */
+
+ abstract = (type == get_python_arch_processor_type());
+
+ if (abstract)
+ {
+ result = NULL;
+ PyErr_Format(PyExc_RuntimeError, _("%s is an abstract class"), type->tp_name);
+ goto exit;
+ }
+
+ /* Mise en place d'un type dédié */
+
+ gtype = built_dynamic_type(G_TYPE_ARCH_PROCESSOR, type->tp_name,
+ (GClassInitFunc)py_arch_processor_init_gclass);
+
+ /* Enregistrement du nouveau GType dans Python */
+
+ sys_mod_dict = PyImport_GetModuleDict();
+
+ modname = PyDict_GetItemString(type->tp_dict, "__module__");
+
+ module = PyObject_GetItem(sys_mod_dict, modname);
+
+ dict = PyModule_GetDict(module);
+
+ if (!_register_class_for_pygobject(dict, gtype, type,
+ &PyGObject_Type, get_python_arch_processor_type(), NULL))
+ {
+ result = NULL;
+ goto exit;
+ }
+
+ Py_DECREF(module);
+ Py_DECREF(modname);
+
+ /* On créé, 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_processor_init_gclass(GArchProcessorClass *class, gpointer unused)
+{
+ class->get_ctx = py_arch_processor_get_context_wrapper;
+
+ class->disassemble = py_arch_processor_disassemble_wrapper;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : proc = architecture visée par la procédure. *
+* *
+* Description : Fournit un contexte propre au processeur d'une architecture. *
+* *
+* Retour : Nouveau contexte mis à disposition. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static GProcContext *py_arch_processor_get_context_wrapper(const GArchProcessor *proc)
+{
+ GProcContext *result; /* Instance à retourner */
+ PyObject *pyobj; /* Objet Python concerné */
+ PyObject *pyctx; /* Contexte en objet Python */
+ int ret; /* Bilan d'une conversion */
+ GArchProcessorClass *class; /* Classe de l'objet courant */
+ GArchProcessorClass *parent; /* Classe parente */
+
+ pyobj = pygobject_new(G_OBJECT(proc));
+
+ if (has_python_method(pyobj, "_get_context"))
+ {
+ pyctx = run_python_method(pyobj, "_get_context", NULL);
+
+ if (pyctx == NULL)
+ result = NULL;
+
+ else
+ {
+ ret = convert_to_proc_context(pyctx, &result);
+
+ if (ret == 1)
+ g_object_ref(G_OBJECT(result));
+ else
+ {
+ PyErr_Clear();
+ result = NULL;
+ }
+ Py_DECREF(pyctx);
+
+ }
+
+ }
+
+ else
+ {
+ class = G_ARCH_PROCESSOR_GET_CLASS(proc);
+ parent = g_type_class_peek_parent(class);
+
+ result = parent->get_ctx(proc);
+
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : proc = architecture visée par la procédure. *
+* ctx = contexte lié à l'exécution du processeur. *
+* content = flux de données à analyser. *
+* pos = position courante dans ce flux. [OUT] *
+* format = format du fichier contenant le code. *
+* *
+* Description : Désassemble une instruction dans un flux de données. *
+* *
+* Retour : Instruction mise en place ou NULL en cas d'échec. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static GArchInstruction *py_arch_processor_disassemble_wrapper(const GArchProcessor *proc, GProcContext *ctx, const GBinContent *content, vmpa2t *pos, GExeFormat *format)
+{
+ GArchInstruction *result; /* Instance à retourner */
+ PyObject *pyobj; /* Objet Python concerné */
+ PyObject *pypos; /* Position en objet Python */
+ PyObject *args; /* Arguments pour l'appel */
+ PyObject *pyins; /* Instruction en objet Python */
+ int ret; /* Bilan d'une conversion */
+
+ pyobj = pygobject_new(G_OBJECT(proc));
+
+ if (has_python_method(pyobj, "_disassemble"))
+ {
+ pypos = build_from_internal_vmpa(pos);
+ Py_INCREF(pypos);
+
+ args = PyTuple_New(4);
+ PyTuple_SetItem(args, 0, pygobject_new(G_OBJECT(ctx)));
+ PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(content)));
+ PyTuple_SetItem(args, 2, pypos);
+ PyTuple_SetItem(args, 3, pygobject_new(G_OBJECT(format)));
+
+ pyins = run_python_method(pyobj, "_disassemble", args);
+
+ Py_DECREF(args);
+
+ if (pyins == NULL)
+ result = NULL;
+
+ else
+ {
+ ret = convert_to_arch_instruction(pyins, &result);
+
+ if (ret == 1)
+ g_object_ref(G_OBJECT(result));
+ else
+ {
+ PyErr_Clear();
+ result = NULL;
+ }
+
+ Py_DECREF(pyins);
+
+ copy_vmpa(pos, get_internal_vmpa(pypos));
+
+ }
+
+ Py_DECREF(pypos);
+
+ }
+
+ else
+ result = NULL;
+
+ return result;
+
+}
@@ -229,7 +483,100 @@ static PyObject *py_arch_processor_has_virtual_space(PyObject *self, void *closu
}
+/******************************************************************************
+* *
+* Paramètres : self = architecture concernée par la procédure. *
+* args = instruction représentant le point de départ. *
+* *
+* Description : Fournit un contexte propre au processeur d'une architecture. *
+* *
+* Retour : Nouveau contexte mis à disposition. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+static PyObject *py_arch_processor_get_context(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Instance à retourner */
+ GArchProcessor *proc; /* Processeur manipulé */
+ GProcContext *ctx; /* Nouveau contexte en place */
+
+ proc = G_ARCH_PROCESSOR(pygobject_get(self));
+
+ ctx = g_arch_processor_get_context(proc);
+
+ if (ctx != NULL)
+ {
+ result = pygobject_new(G_OBJECT(ctx));
+ g_object_unref(G_OBJECT(ctx));
+ }
+ else
+ {
+ result = Py_None;
+ Py_INCREF(result);
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : self = architecture concernée par la procédure. *
+* args = instruction représentant le point de départ. *
+* *
+* Description : Désassemble une instruction dans un flux de données. *
+* *
+* Retour : Instruction mise en place ou None en cas d'échec. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static PyObject *py_arch_processor_disassemble(PyObject *self, PyObject *args)
+{
+ PyObject *result; /* Instance à retourner */
+ GProcContext *ctx; /* Contexte de désassemblage */
+ GBinContent *content; /* Contenu binaire à parcourir */
+ PyObject *pypos; /* Position en objet Python */
+ GExeFormat *format; /* Format de fichier associé */
+ int ret; /* Bilan de lecture des args. */
+ vmpa2t pos; /* Position d'analyse courante */
+ GArchProcessor *proc; /* Processeur manipulé */
+ GArchInstruction *instr; /* Instruction mise en place */
+
+ ret = PyArg_ParseTuple(args, "O&O&OO&",
+ convert_to_proc_context, &ctx,
+ convert_to_binary_content, &content,
+ &pypos,
+ convert_to_executable_format, &format);
+ if (!ret) return NULL;
+
+ ret = convert_any_to_vmpa(pypos, &pos);
+ if (ret != 1) return NULL;
+
+ proc = G_ARCH_PROCESSOR(pygobject_get(self));
+
+ instr = g_arch_processor_disassemble(proc, ctx, content, &pos, format);
+
+ copy_vmpa(get_internal_vmpa(pypos), &pos);
+
+ if (instr != NULL)
+ {
+ result = pygobject_new(G_OBJECT(instr));
+ g_object_unref(G_OBJECT(instr));
+ }
+ else
+ {
+ result = Py_None;
+ Py_INCREF(result);
+ }
+
+ return result;
+
+}
@@ -527,6 +874,16 @@ PyTypeObject *get_python_arch_processor_type(void)
{
static PyMethodDef py_arch_processor_methods[] = {
{
+ "get_context", py_arch_processor_get_context,
+ METH_NOARGS,
+ "get_context($self, /)\n--\n\nProvide a new disassembly context."
+ },
+ {
+ "disassemble", py_arch_processor_disassemble,
+ METH_VARARGS,
+ "disassemble($self, context, content, pos, format, /)\n--\n\nDisassemble a portion of binary content into one instruction."
+ },
+ {
"add_error", py_arch_processor_add_error,
METH_VARARGS,
"add_error($self, type, addr, desc, /)\n--\n\nExtend the list of detected disassembling errors."
@@ -581,6 +938,8 @@ PyTypeObject *get_python_arch_processor_type(void)
.tp_methods = py_arch_processor_methods,
.tp_getset = py_arch_processor_getseters,
+ .tp_new = py_arch_processor_new,
+
};
return &py_arch_processor_type;