summaryrefslogtreecommitdiff
path: root/plugins/pychrysalide/arch/processor.c
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/processor.c
parenta13d12c758184449bf3305785ef33273802a761c (diff)
Relied on GObject introspection and dynamic gtypes to inherit in Python.
Diffstat (limited to 'plugins/pychrysalide/arch/processor.c')
-rw-r--r--plugins/pychrysalide/arch/processor.c359
1 files changed, 359 insertions, 0 deletions
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;