From eaf46c79d5b1db06f1f1f7da17a37ec007af2d92 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Fri, 11 May 2018 16:10:49 +0200
Subject: Implemented a singleton system for ARMv7 registers.

---
 plugins/arm/v7/core.c              |   4 +
 plugins/arm/v7/cregister.c         | 162 +++++++++++++++++++++++++++++++++----
 plugins/arm/v7/cregister.h         |  11 +++
 plugins/arm/v7/operands/reglist.c  |   5 --
 plugins/arm/v7/register.c          | 136 ++++++++++++++++++++++++++++++-
 plugins/arm/v7/register.h          |  12 ++-
 plugins/dalvik/operands/register.c |   2 +-
 plugins/dalvik/register.c          |   4 +-
 plugins/dalvik/register.h          |   5 +-
 9 files changed, 310 insertions(+), 31 deletions(-)

diff --git a/plugins/arm/v7/core.c b/plugins/arm/v7/core.c
index a2ed090..4de61a2 100644
--- a/plugins/arm/v7/core.c
+++ b/plugins/arm/v7/core.c
@@ -25,7 +25,9 @@
 
 
 #include <core/processors.h>
+#include "cregister.h"
 #include "processor.h"
+#include "register.h"
 
 
 
@@ -66,5 +68,7 @@ bool init_armv7_core(void)
 
 void exit_armv7_core(void)
 {
+    clean_armv7_cregister_cache();
+    clean_armv7_register_cache();
 
 }
diff --git a/plugins/arm/v7/cregister.c b/plugins/arm/v7/cregister.c
index 03607e2..7b39e02 100644
--- a/plugins/arm/v7/cregister.c
+++ b/plugins/arm/v7/cregister.c
@@ -31,6 +31,9 @@
 
 
 
+/* ------------------------- GESTION UNITAIRE DES REGISTRES ------------------------- */
+
+
 /* Représentation d'un registre de co-processeur ARMv7 (instance) */
 struct _GArmV7CRegister
 {
@@ -63,6 +66,23 @@ static void g_armv7_cregister_finalize(GArmV7CRegister *);
 /* Traduit un registre en version humainement lisible. */
 static void g_armv7_cregister_print(const GArmV7CRegister *, GBufferLine *, AsmSyntax);
 
+/* Crée une réprésentation de registre de co-processeur ARMv7. */
+static GArmV7CRegister *_g_armv7_cregister_new(uint8_t);
+
+
+
+/* ------------------------ GESTION SOUS FORME DE SINGLETONS ------------------------ */
+
+
+/* Conservation des registres utilisés */
+static GArmV7CRegister **_armv7_cregisters = NULL;
+static size_t _av7creg_count = 0;
+G_LOCK_DEFINE_STATIC(_av7creg_mutex);
+
+
+/* Fournit le singleton associé à un registre de co-proc. ARMv7. */
+static GArmV7CRegister *get_armv7_cregister(uint8_t);
+
 
 
 /* Indique le type défini pour une représentation d'un registre de co-processeur ARMv7. */
@@ -155,6 +175,40 @@ static void g_armv7_cregister_finalize(GArmV7CRegister *reg)
 
 /******************************************************************************
 *                                                                             *
+*  Paramètres  : reg    = registre à transcrire.                              *
+*                line   = ligne tampon où imprimer l'opérande donné.          *
+*                syntax = type de représentation demandée.                    *
+*                                                                             *
+*  Description : Traduit un registre en version humainement lisible.          *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static void g_armv7_cregister_print(const GArmV7CRegister *reg, GBufferLine *line, AsmSyntax syntax)
+{
+    char key[MAX_REGNAME_LEN];              /* Mot clef principal          */
+    size_t klen;                            /* Taille de ce mot clef       */
+
+    switch (G_ARM_REGISTER(reg)->index)
+    {
+        case 0 ... 15:
+            klen = snprintf(key, MAX_REGNAME_LEN, "c%hhu", G_ARM_REGISTER(reg)->index);
+            break;
+        default:
+            klen = snprintf(key, MAX_REGNAME_LEN, "c??");
+            break;
+    }
+
+    g_buffer_line_append_text(line, BLC_ASSEMBLY, key, klen, RTT_REGISTER, NULL);
+
+}
+
+
+/******************************************************************************
+*                                                                             *
 *  Paramètres  : index = indice du registre correspondant.                    *
 *                                                                             *
 *  Description : Crée une réprésentation de registre de co-processeur ARMv7.  *
@@ -165,7 +219,7 @@ static void g_armv7_cregister_finalize(GArmV7CRegister *reg)
 *                                                                             *
 ******************************************************************************/
 
-GArmV7CRegister *g_armv7_cregister_new(uint8_t index)
+static GArmV7CRegister *_g_armv7_cregister_new(uint8_t index)
 {
     GArmV7CRegister *result;                /* Structure à retourner       */
 
@@ -180,33 +234,107 @@ GArmV7CRegister *g_armv7_cregister_new(uint8_t index)
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : reg    = registre à transcrire.                              *
-*                line   = ligne tampon où imprimer l'opérande donné.          *
-*                syntax = type de représentation demandée.                    *
+*  Paramètres  : index = indice du registre correspondant.                    *
 *                                                                             *
-*  Description : Traduit un registre en version humainement lisible.          *
+*  Description : Crée une réprésentation de registre de co-processeur ARMv7.  *
 *                                                                             *
-*  Retour      : -                                                            *
+*  Retour      : Adresse de la structure mise en place.                       *
 *                                                                             *
 *  Remarques   : -                                                            *
 *                                                                             *
 ******************************************************************************/
 
-static void g_armv7_cregister_print(const GArmV7CRegister *reg, GBufferLine *line, AsmSyntax syntax)
+GArmV7CRegister *g_armv7_cregister_new(uint8_t index)
 {
-    char key[MAX_REGNAME_LEN];              /* Mot clef principal          */
-    size_t klen;                            /* Taille de ce mot clef       */
+    GArmV7CRegister *result;                /* Structure à retourner       */
 
-    switch (G_ARM_REGISTER(reg)->index)
+    result = get_armv7_cregister(index);
+
+    return result;
+
+}
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/*                          GESTION SOUS FORME DE SINGLETONS                          */
+/* ---------------------------------------------------------------------------------- */
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : index = indice du registre correspondant.                    *
+*                                                                             *
+*  Description : Fournit le singleton associé à un registre de co-proc. ARMv7.*
+*                                                                             *
+*  Retour      : Adresse de la structure mise en place.                       *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static GArmV7CRegister *get_armv7_cregister(uint8_t index)
+{
+    GArmV7CRegister *result;                 /* Structure à retourner       */
+    size_t new_count;                       /* Nouvelle taille à considérer*/
+    size_t i;                               /* Boucle de parcours          */
+
+    G_LOCK(_av7creg_mutex);
+
+    if (index >= _av7creg_count)
     {
-        case 0 ... 15:
-            klen = snprintf(key, MAX_REGNAME_LEN, "c%hhu", G_ARM_REGISTER(reg)->index);
-            break;
-        default:
-            klen = snprintf(key, MAX_REGNAME_LEN, "c??");
-            break;
+        new_count = index + 1;
+
+        _armv7_cregisters = realloc(_armv7_cregisters, new_count * sizeof(GArmV7CRegister *));
+
+        for (i = _av7creg_count; i < new_count; i++)
+            _armv7_cregisters[i] = NULL;
+
+        _av7creg_count = new_count;
+
     }
 
-    g_buffer_line_append_text(line, BLC_ASSEMBLY, key, klen, RTT_REGISTER, NULL);
+    if (_armv7_cregisters[index] == NULL)
+        _armv7_cregisters[index] = _g_armv7_cregister_new(index);
+
+    result = _armv7_cregisters[index];
+
+    G_UNLOCK(_av7creg_mutex);
+
+    g_object_ref(G_OBJECT(result));
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : -                                                            *
+*                                                                             *
+*  Description : Vide totalement le cache des registres ARMv7.                *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+void clean_armv7_cregister_cache(void)
+{
+    size_t i;                               /* Boucle de parcours          */
+
+    G_LOCK(_av7creg_mutex);
+
+    for (i = 0; i < _av7creg_count; i++)
+        g_object_unref(G_OBJECT(_armv7_cregisters[i]));
+
+    if (_armv7_cregisters != NULL)
+        free(_armv7_cregisters);
+
+    _armv7_cregisters = NULL;
+    _av7creg_count = 0;
+
+    G_UNLOCK(_av7creg_mutex);
 
 }
diff --git a/plugins/arm/v7/cregister.h b/plugins/arm/v7/cregister.h
index 4cbea8f..857c11d 100644
--- a/plugins/arm/v7/cregister.h
+++ b/plugins/arm/v7/cregister.h
@@ -30,6 +30,9 @@
 
 
 
+/* ------------------------- GESTION UNITAIRE DES REGISTRES ------------------------- */
+
+
 #define G_TYPE_ARMV7_CREGISTER               g_armv7_cregister_get_type()
 #define G_ARMV7_CREGISTER(obj)               (G_TYPE_CHECK_INSTANCE_CAST((obj), g_armv7_cregister_get_type(), GArmV7CRegister))
 #define G_IS_ARMV7_CREGISTER(obj)            (G_TYPE_CHECK_INSTANCE_TYPE((obj), g_armv7_cregister_get_type()))
@@ -53,4 +56,12 @@ GArmV7CRegister *g_armv7_cregister_new(uint8_t);
 
 
 
+/* ------------------------ GESTION SOUS FORME DE SINGLETONS ------------------------ */
+
+
+/* Vide totalement le cache des registres ARMv7. */
+void clean_armv7_cregister_cache(void);
+
+
+
 #endif  /* _PLUGINS_ARM_V7_CREGISTER_H */
diff --git a/plugins/arm/v7/operands/reglist.c b/plugins/arm/v7/operands/reglist.c
index fb36a24..9fe9245 100644
--- a/plugins/arm/v7/operands/reglist.c
+++ b/plugins/arm/v7/operands/reglist.c
@@ -138,11 +138,6 @@ static void g_armv7_reglist_operand_init(GArmV7RegListOperand *operand)
 
 static void g_armv7_reglist_operand_dispose(GArmV7RegListOperand *operand)
 {
-    size_t i;                               /* Boucle de parcours          */
-
-    for (i = 0; i < operand->count; i++)
-        g_object_unref(G_OBJECT(operand->registers[i]));
-
     G_OBJECT_CLASS(g_armv7_reglist_operand_parent_class)->dispose(G_OBJECT(operand));
 
 }
diff --git a/plugins/arm/v7/register.c b/plugins/arm/v7/register.c
index 1b17062..c2e15b0 100644
--- a/plugins/arm/v7/register.c
+++ b/plugins/arm/v7/register.c
@@ -31,6 +31,9 @@
 
 
 
+/* ------------------------- GESTION UNITAIRE DES REGISTRES ------------------------- */
+
+
 /* Représentation d'un registre ARMv7 (instance) */
 struct _GArmV7Register
 {
@@ -65,6 +68,29 @@ static void g_armv7_register_finalize(GArmV7Register *);
 /* Traduit un registre en version humainement lisible. */
 static void g_armv7_register_print(const GArmV7Register *, GBufferLine *, AsmSyntax);
 
+/* Crée une réprésentation de registre ARMv7. */
+static GArmV7Register *_g_armv7_register_new(uint8_t);
+
+
+
+/* ------------------------ GESTION SOUS FORME DE SINGLETONS ------------------------ */
+
+
+/* Conservation des registres utilisés */
+static GArmV7Register **_armv7_registers = NULL;
+static size_t _av7reg_count = 0;
+G_LOCK_DEFINE_STATIC(_av7reg_mutex);
+
+
+/* Fournit le singleton associé à un registre ARMv7. */
+static GArmV7Register *get_armv7_register(uint8_t);
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/*                           GESTION UNITAIRE DES REGISTRES                           */
+/* ---------------------------------------------------------------------------------- */
+
 
 
 /* Indique le type défini pour une représentation d'un registre ARMv7. */
@@ -216,7 +242,7 @@ static void g_armv7_register_print(const GArmV7Register *reg, GBufferLine *line,
 *                                                                             *
 ******************************************************************************/
 
-GArmV7Register *g_armv7_register_new(uint8_t index)
+static GArmV7Register *_g_armv7_register_new(uint8_t index)
 {
     GArmV7Register *result;                 /* Structure à retourner       */
 
@@ -227,3 +253,111 @@ GArmV7Register *g_armv7_register_new(uint8_t index)
     return result;
 
 }
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : index = indice du registre correspondant.                    *
+*                                                                             *
+*  Description : Crée une réprésentation de registre ARMv7.                   *
+*                                                                             *
+*  Retour      : Adresse de la structure mise en place.                       *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+GArmV7Register *g_armv7_register_new(uint8_t index)
+{
+    GArmV7Register *result;                 /* Structure à retourner       */
+
+    result = get_armv7_register(index);
+
+    return result;
+
+}
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/*                          GESTION SOUS FORME DE SINGLETONS                          */
+/* ---------------------------------------------------------------------------------- */
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : index = indice du registre correspondant.                    *
+*                                                                             *
+*  Description : Fournit le singleton associé à un registre ARMv7.            *
+*                                                                             *
+*  Retour      : Adresse de la structure mise en place.                       *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static GArmV7Register *get_armv7_register(uint8_t index)
+{
+    GArmV7Register *result;                 /* Structure à retourner       */
+    size_t new_count;                       /* Nouvelle taille à considérer*/
+    size_t i;                               /* Boucle de parcours          */
+
+    G_LOCK(_av7reg_mutex);
+
+    if (index >= _av7reg_count)
+    {
+        new_count = index + 1;
+
+        _armv7_registers = realloc(_armv7_registers, new_count * sizeof(GArmV7Register *));
+
+        for (i = _av7reg_count; i < new_count; i++)
+            _armv7_registers[i] = NULL;
+
+        _av7reg_count = new_count;
+
+    }
+
+    if (_armv7_registers[index] == NULL)
+        _armv7_registers[index] = _g_armv7_register_new(index);
+
+    result = _armv7_registers[index];
+
+    G_UNLOCK(_av7reg_mutex);
+
+    g_object_ref(G_OBJECT(result));
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : -                                                            *
+*                                                                             *
+*  Description : Vide totalement le cache des registres ARMv7.                *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+void clean_armv7_register_cache(void)
+{
+    size_t i;                               /* Boucle de parcours          */
+
+    G_LOCK(_av7reg_mutex);
+
+    for (i = 0; i < _av7reg_count; i++)
+        g_object_unref(G_OBJECT(_armv7_registers[i]));
+
+    if (_armv7_registers != NULL)
+        free(_armv7_registers);
+
+    _armv7_registers = NULL;
+    _av7reg_count = 0;
+
+    G_UNLOCK(_av7reg_mutex);
+
+}
diff --git a/plugins/arm/v7/register.h b/plugins/arm/v7/register.h
index a5b11c1..7596bac 100644
--- a/plugins/arm/v7/register.h
+++ b/plugins/arm/v7/register.h
@@ -26,11 +26,13 @@
 
 
 #include <glib-object.h>
-#include <stdbool.h>
 #include <stdint.h>
 
 
 
+/* ------------------------- GESTION UNITAIRE DES REGISTRES ------------------------- */
+
+
 #define G_TYPE_ARMV7_REGISTER               g_armv7_register_get_type()
 #define G_ARMV7_REGISTER(obj)               (G_TYPE_CHECK_INSTANCE_CAST((obj), g_armv7_register_get_type(), GArmV7Register))
 #define G_IS_ARMV7_REGISTER(obj)            (G_TYPE_CHECK_INSTANCE_TYPE((obj), g_armv7_register_get_type()))
@@ -54,4 +56,12 @@ GArmV7Register *g_armv7_register_new(uint8_t);
 
 
 
+/* ------------------------ GESTION SOUS FORME DE SINGLETONS ------------------------ */
+
+
+/* Vide totalement le cache des registres ARMv7. */
+void clean_armv7_register_cache(void);
+
+
+
 #endif  /* _PLUGINS_ARM_V7_REGISTER_H */
diff --git a/plugins/dalvik/operands/register.c b/plugins/dalvik/operands/register.c
index 79d8756..71da97f 100644
--- a/plugins/dalvik/operands/register.c
+++ b/plugins/dalvik/operands/register.c
@@ -34,7 +34,7 @@ struct _GDalvikRegisterOperand
 {
     GArchOperand parent;                    /* Instance parente            */
 
-    const GDalvikRegister *reg;             /* Registre représenté         */
+    GDalvikRegister *reg;                   /* Registre représenté         */
     bool is_written;                        /* Changement de contenu       */
 
 };
diff --git a/plugins/dalvik/register.c b/plugins/dalvik/register.c
index 8374e75..1e59421 100644
--- a/plugins/dalvik/register.c
+++ b/plugins/dalvik/register.c
@@ -76,7 +76,7 @@ static guint g_dalvik_register_hash(const GDalvikRegister *);
 static void g_dalvik_register_print(const GDalvikRegister *, GBufferLine *, AsmSyntax);
 
 /* Crée une réprésentation de registre Dalvik. */
-GDalvikRegister *_g_dalvik_register_new(uint16_t);
+static GDalvikRegister *_g_dalvik_register_new(uint16_t);
 
 
 
@@ -261,7 +261,7 @@ static void g_dalvik_register_print(const GDalvikRegister *reg, GBufferLine *lin
 *                                                                             *
 ******************************************************************************/
 
-GDalvikRegister *_g_dalvik_register_new(uint16_t index)
+static GDalvikRegister *_g_dalvik_register_new(uint16_t index)
 {
     GDalvikRegister *result;                /* Structure à retourner       */
 
diff --git a/plugins/dalvik/register.h b/plugins/dalvik/register.h
index ec45c12..ffa44e4 100644
--- a/plugins/dalvik/register.h
+++ b/plugins/dalvik/register.h
@@ -26,10 +26,7 @@
 
 
 #include <glib-object.h>
-#include <stdbool.h>
-
-
-#include <arch/archbase.h>
+#include <stdint.h>
 
 
 
-- 
cgit v0.11.2-87-g4458