From b01b4405c1c8a6d6e6bfade4b790cabde2e9f4d6 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Wed, 20 Jun 2018 23:19:05 +0200
Subject: Ensured all code has been disassembled before loading data.

---
 src/analysis/disass/area.c  | 88 +++++++++++++++++++++++++++++++++++----------
 src/analysis/disass/area.h  |  2 +-
 src/analysis/disass/fetch.c |  8 ++++-
 3 files changed, 77 insertions(+), 21 deletions(-)

diff --git a/src/analysis/disass/area.c b/src/analysis/disass/area.c
index c52934f..7050c9c 100644
--- a/src/analysis/disass/area.c
+++ b/src/analysis/disass/area.c
@@ -102,7 +102,10 @@ static void update_address_as_routine(GBinFormat *, const vmpa2t *);
 static void load_data_from_mem_area(mem_area *, GProcContext *, const vmpa2t *, GtkStatusStack *, activity_id_t);
 
 /* S'assure qu'une aire contient toutes ses instructions. */
-static void fill_mem_area(mem_area *, mem_area *, size_t, GProcContext *, GtkStatusStack *, activity_id_t);
+static void fill_mem_area_with_code(mem_area *, mem_area *, size_t, GProcContext *, GtkStatusStack *, activity_id_t);
+
+/* S'assure qu'une aire contient toutes ses instructions. */
+static void fill_mem_area_with_data(mem_area *, mem_area *, size_t, GProcContext *, GtkStatusStack *, activity_id_t);
 
 /* Rassemble les instructions conservées dans une zone donnée. */
 static GArchInstruction **get_instructions_from_mem_area(const mem_area *, GArchInstruction **, size_t *);
@@ -896,7 +899,56 @@ static void load_data_from_mem_area(mem_area *area, GProcContext *ctx, const vmp
 *                                                                             *
 ******************************************************************************/
 
-static void fill_mem_area(mem_area *area, mem_area *list, size_t count, GProcContext *ctx, GtkStatusStack *status, activity_id_t id)
+static void fill_mem_area_with_code(mem_area *area, mem_area *list, size_t count, GProcContext *ctx, GtkStatusStack *status, activity_id_t id)
+{
+    const vmpa2t *addr;                     /* Début de la zone à traiter  */
+    phys_t len;                             /* Taille de la zone à remplir */
+    phys_t i;                               /* Boucle de parcours          */
+    vmpa2t start;                           /* Adresse de départ de combles*/
+
+    if (area->is_exec)
+    {
+        addr = get_mrange_addr(&area->range);
+        len = get_mrange_length(&area->range);
+
+        for (i = 0; i < len; i++)
+        {
+            if (is_range_empty_in_mem_area(area, i, 1))
+            {
+                copy_vmpa(&start, addr);
+                advance_vmpa(&start, i);
+
+                if (get_virt_addr(&start) % area->packing_size == 0)
+                    load_code_from_mem_area(area, list, count, ctx, &start, false, status, id);
+
+            }
+
+        }
+
+    }
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : area   = aire représentant à contenu à parcourir.            *
+*                list   = liste de zones délimitant des contenus à traiter.   *
+*                count  = nombre de zones à disposition.                      *
+*                binary = représentation de binaire chargé.                   *
+*                ctx    = contexte offert en soutien à un désassemblage.      *
+*                status = barre de statut à actualiser.                       *
+*                id     = identifiant du groupe de progression à l'affichage. *
+*                                                                             *
+*  Description : S'assure qu'une aire contient toutes ses instructions.       *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static void fill_mem_area_with_data(mem_area *area, mem_area *list, size_t count, GProcContext *ctx, GtkStatusStack *status, activity_id_t id)
 {
     const vmpa2t *addr;                     /* Début de la zone à traiter  */
     phys_t len;                             /* Taille de la zone à remplir */
@@ -916,24 +968,17 @@ static void fill_mem_area(mem_area *area, mem_area *list, size_t count, GProcCon
             copy_vmpa(&start, addr);
             advance_vmpa(&start, i);
 
-            if (area->is_exec && get_virt_addr(&start) % area->packing_size == 0)
-                load_code_from_mem_area(area, list, count, ctx, &start, false, status, id);
-
-            if (is_range_empty_in_mem_area(area, i, 1))
+            if (area->is_exec && err_trigger)
             {
-                if (area->is_exec && err_trigger)
-                {
-                    g_arch_processor_add_error(area->proc, APE_DISASSEMBLY, &start,
-                                               _("Unable to disassemble code instruction"));
+                g_arch_processor_add_error(area->proc, APE_DISASSEMBLY, &start,
+                                           _("Unable to disassemble code instruction"));
 
-                    err_trigger = false;
-
-                }
-
-                load_data_from_mem_area(area, ctx, &start, status, id);
+                err_trigger = false;
 
             }
 
+            load_data_from_mem_area(area, ctx, &start, status, id);
+
         }
 
         else
@@ -1483,7 +1528,7 @@ static void insert_instr_into_mem_areas_forced(mem_area *areas, size_t count, GA
 *                                                                             *
 *  Paramètres  : areas  = liste de zones délimitant des contenus à traiter.   *
 *                count  = nombre de zones à disposition.                      *
-*                binary = représentation de binaire chargé.                   *
+*                code   = nature des instructions à utiliser pour le comble.  *
 *                ctx    = contexte offert en soutien à un désassemblage.      *
 *                status = barre de statut à actualiser.                       *
 *                id     = identifiant du groupe de progression à l'affichage. *
@@ -1496,12 +1541,17 @@ static void insert_instr_into_mem_areas_forced(mem_area *areas, size_t count, GA
 *                                                                             *
 ******************************************************************************/
 
-void ensure_all_mem_areas_are_filled(mem_area *areas, size_t count, GProcContext *ctx, GtkStatusStack *status, activity_id_t id)
+void ensure_all_mem_areas_are_filled(mem_area *areas, size_t count, bool code, GProcContext *ctx, GtkStatusStack *status, activity_id_t id)
 {
     size_t i;                               /* Boucle de parcours          */
 
-    for (i = 0; i < count; i++)
-        fill_mem_area(&areas[i], areas, count, ctx, status, id);
+    if (code)
+        for (i = 0; i < count; i++)
+            fill_mem_area_with_code(&areas[i], areas, count, ctx, status, id);
+
+    else
+        for (i = 0; i < count; i++)
+            fill_mem_area_with_data(&areas[i], areas, count, ctx, status, id);
 
 }
 
diff --git a/src/analysis/disass/area.h b/src/analysis/disass/area.h
index d1e59d9..dc84277 100644
--- a/src/analysis/disass/area.h
+++ b/src/analysis/disass/area.h
@@ -53,7 +53,7 @@ void load_code_from_mem_area(mem_area *, mem_area *, size_t, GProcContext *, con
 mem_area *find_memory_area_by_addr(mem_area *, size_t, const vmpa2t *);
 
 /* S'assure que l'ensemble des aires est entièrement décodé. */
-void ensure_all_mem_areas_are_filled(mem_area *, size_t, GProcContext *, GtkStatusStack *, activity_id_t);
+void ensure_all_mem_areas_are_filled(mem_area *, size_t, bool, GProcContext *, GtkStatusStack *, activity_id_t);
 
 
 
diff --git a/src/analysis/disass/fetch.c b/src/analysis/disass/fetch.c
index 03b3ff0..fc1c2ab 100644
--- a/src/analysis/disass/fetch.c
+++ b/src/analysis/disass/fetch.c
@@ -463,7 +463,7 @@ GArchInstruction **disassemble_binary_content(GLoadedBinary *binary, GProcContex
 
     gtk_status_stack_update_activity(status, template.id, _("Disassembling the remaining instructions..."));
 
-    ensure_all_mem_areas_are_filled(template.areas, template.count, template.ctx, status, template.id);
+    ensure_all_mem_areas_are_filled(template.areas, template.count, true, template.ctx, status, template.id);
 
     g_work_queue_wait_for_completion(queue, gid);
 
@@ -471,6 +471,12 @@ GArchInstruction **disassemble_binary_content(GLoadedBinary *binary, GProcContex
 
     g_object_set_data(G_OBJECT(template.ctx), "remaining_counter", NULL);
 
+    gtk_status_stack_update_activity(status, template.id, _("Filling holes with data..."));
+
+    ensure_all_mem_areas_are_filled(template.areas, template.count, false, template.ctx, status, template.id);
+
+    g_work_queue_wait_for_completion(queue, gid);
+
     gtk_status_stack_remove_activity(status, template.id);
 
     /**
-- 
cgit v0.11.2-87-g4458