From bfd81d1f289913f4d6e09cd8d99f4aaeed98436b Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Tue, 1 Jan 2013 23:29:57 +0000 Subject: Fixed the computing of basic blocks and used them in graphic views. git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@316 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a --- ChangeLog | 24 ++++ src/analysis/block-int.h | 4 + src/analysis/block.c | 21 ++++ src/analysis/block.h | 6 +- src/analysis/blocks/flow.c | 57 +++++++++- src/analysis/blocks/flow.h | 3 + src/analysis/blocks/virtual.c | 34 +++++- src/analysis/disass/disassembler.c | 4 +- src/analysis/disass/macro.c | 218 ++++++++++++++++++++++++++++++++++--- src/analysis/routine.c | 43 ++++++++ src/analysis/routine.h | 6 + src/glibext/gcodebuffer.c | 45 ++++---- src/gtkext/gtkgraphview.c | 97 +++++------------ 13 files changed, 445 insertions(+), 117 deletions(-) diff --git a/ChangeLog b/ChangeLog index f2e81ac..9561278 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,27 @@ +13-01-02 Cyrille Bagard + + * src/analysis/block.c: + * src/analysis/block.h: + * src/analysis/block-int.h: + * src/analysis/blocks/flow.c: + * src/analysis/blocks/flow.h: + * src/analysis/blocks/virtual.c: + List all basic blocks. + + * src/analysis/disass/disassembler.c: + * src/analysis/disass/macro.c: + Fix the computing of basic blocks. + + * src/analysis/routine.c: + * src/analysis/routine.h: + Store and provide the basic blocks of routine. + + * src/glibext/gcodebuffer.c: + Fix a bug: take care of comments when looking for (last) addresses. + + * src/gtkext/gtkgraphview.c: + Use basic blocks when creating subviews. + 13-01-01 Cyrille Bagard * src/gtkext/gtkgraphview.c: diff --git a/src/analysis/block-int.h b/src/analysis/block-int.h index f89fa3c..e194ace 100644 --- a/src/analysis/block-int.h +++ b/src/analysis/block-int.h @@ -29,6 +29,9 @@ +/* Etablit la liste de tous les blocs présents. */ +typedef void (* list_all_blocks_fc) (const GInstrBlock *, GInstrBlock ***, size_t *); + /* Fournit les différents accès aux registres. */ typedef const reg_access * (* list_regs_accesses_fc) (const GInstrBlock *, size_t *); @@ -39,6 +42,7 @@ struct _GInstrBlock { GObject parent; /* A laisser en premier */ + list_all_blocks_fc list_blocks; /* Liste de tous les blocs */ list_regs_accesses_fc list_regs; /* Liste des accès registres */ }; diff --git a/src/analysis/block.c b/src/analysis/block.c index 58b1fee..3093a98 100644 --- a/src/analysis/block.c +++ b/src/analysis/block.c @@ -153,3 +153,24 @@ static void g_instr_block_finalize(GInstrBlock *block) G_OBJECT_CLASS(g_instr_block_parent_class)->finalize(G_OBJECT(block)); } + + +/****************************************************************************** +* * +* Paramètres : block = bloc d'instructions à consulter. * +* list = ensemble de blocs à compléter. [OUT] * +* count = nombre de blocs au total. [OUT] * +* * +* Description : Etablit la liste de tous les blocs présents. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void g_instr_block_list_all_blocks(const GInstrBlock *block, GInstrBlock ***list, size_t *count) +{ + block->list_blocks(block, list, count); + +} diff --git a/src/analysis/block.h b/src/analysis/block.h index 2a7fb90..9e136a3 100644 --- a/src/analysis/block.h +++ b/src/analysis/block.h @@ -82,10 +82,8 @@ typedef struct _GInstrBlockClass GInstrBlockClass; /* Indique le type défini pour un bloc d'instructions. */ GType g_instr_block_get_type(void); - - - - +/* Etablit la liste de tous les blocs présents. */ +void g_instr_block_list_all_blocks(const GInstrBlock *, GInstrBlock ***, size_t *); diff --git a/src/analysis/blocks/flow.c b/src/analysis/blocks/flow.c index 2bdd0cf..f80b7c8 100644 --- a/src/analysis/blocks/flow.c +++ b/src/analysis/blocks/flow.c @@ -71,6 +71,9 @@ static void g_flow_block_memorize_access(GFlowBlock *, GArchRegister *, RegAcces /* Note les différents accès aux registres. */ static void g_flow_block_compute_regs_access(GFlowBlock *); +/* Etablit la liste de tous les blocs présents. */ +static void g_flow_block_list_all_blocks(const GFlowBlock *, GInstrBlock ***, size_t *); + /* Fournit les différents accès aux registres. */ static const reg_access *g_flow_block_list_regs_accesses(const GFlowBlock *, size_t *); @@ -122,6 +125,7 @@ static void g_flow_block_init(GFlowBlock *block) parent = G_INSTR_BLOCK(block); + parent->list_blocks = (list_all_blocks_fc)g_flow_block_list_all_blocks; parent->list_regs = (list_regs_accesses_fc)g_flow_block_list_regs_accesses; } @@ -201,13 +205,13 @@ GInstrBlock *g_flow_block_new(GArchInstruction *instrs, GArchInstruction *first, result = g_object_new(G_TYPE_FLOW_BLOCK, NULL); - + /* g_arch_instruction_get_location(first, NULL, NULL, &addr); - //printf(" ! new block @ 0x%llx - ", addr); + printf(" ! new block @ 0x%llx - ", addr); g_arch_instruction_get_location(last, NULL, NULL, &addr); - //printf("0x%llx\n", addr); - + printf("0x%llx\n", addr); + */ @@ -228,6 +232,29 @@ GInstrBlock *g_flow_block_new(GArchInstruction *instrs, GArchInstruction *first, /****************************************************************************** * * +* Paramètres : block = bloc d'instructions à consulter. * +* list = ensemble de blocs à compléter. [OUT] * +* count = nombre de blocs au total. [OUT] * +* * +* Description : Etablit la liste de tous les blocs présents. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_flow_block_list_all_blocks(const GFlowBlock *block, GInstrBlock ***list, size_t *count) +{ + (*list) = (GInstrBlock **)realloc(*list, ++(*count) * sizeof(GInstrBlock *)); + + (*list)[*count - 1] = G_INSTR_BLOCK(block); + +} + + +/****************************************************************************** +* * * Paramètres : block = bloc d'instructions à mettre à jour. * * reg = registre visé par l'opération. * * type = type d'accès à l'opérande. * @@ -362,3 +389,25 @@ static const reg_access *g_flow_block_list_regs_accesses(const GFlowBlock *block return block->accesses; } + + +/****************************************************************************** +* * +* Paramètres : block = bloc d'instructions à consulter. * +* start = adresse de départ du bloc. [OUT] * +* end = dernière adresse du bloc. [OUT] * +* * +* Description : Fournit les adresses limites d'un bloc d'exécution. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void g_flow_block_get_boundary_addresses(const GFlowBlock *block, vmpa_t *start, vmpa_t *end) +{ + g_arch_instruction_get_location(block->first, NULL, NULL, start); + g_arch_instruction_get_location(block->last, NULL, NULL, end); + +} diff --git a/src/analysis/blocks/flow.h b/src/analysis/blocks/flow.h index cc2eb66..14f49ec 100644 --- a/src/analysis/blocks/flow.h +++ b/src/analysis/blocks/flow.h @@ -55,6 +55,9 @@ GType g_flow_block_get_type(void); /* Crée un bloc d'exécution d'instructions. */ GInstrBlock *g_flow_block_new(GArchInstruction *, GArchInstruction *, GArchInstruction *); +/* Fournit les adresses limites d'un bloc d'exécution. */ +void g_flow_block_get_boundary_addresses(const GFlowBlock *, vmpa_t *, vmpa_t *); + #endif /* _ANALYSIS_BLOCKS_FLOW_H */ diff --git a/src/analysis/blocks/virtual.c b/src/analysis/blocks/virtual.c index 113e333..c981ece 100644 --- a/src/analysis/blocks/virtual.c +++ b/src/analysis/blocks/virtual.c @@ -40,13 +40,13 @@ struct _GVirtualBlock GArchInstruction *first; /* Première instruction */ GArchInstruction *last; /* Dernière instruction */ + GInstrBlock **children; /* Sous-blocs intégrés */ + size_t children_count; /* Nombre de ces sous-blocs */ + reg_access *accesses; /* Commodités d'accès #1 */ size_t count; /* Commodités d'accès #2 */ - GInstrBlock **children; /* Sous-blocs intégrés */ - size_t children_count; /* Nombre de ces sous-blocs */ - }; /* Description d'un bloc d'exécution d'instructions (classe) */ @@ -69,6 +69,9 @@ static void g_virtual_block_dispose(GVirtualBlock *); /* Procède à la libération totale de la mémoire. */ static void g_virtual_block_finalize(GVirtualBlock *); +/* Etablit la liste de tous les blocs présents. */ +static void g_virtual_block_list_all_blocks(const GVirtualBlock *, GInstrBlock ***, size_t *); + /* Fournit les différents accès aux registres. */ static const reg_access *g_virtual_block_list_regs_accesses(const GVirtualBlock *, size_t *); @@ -120,6 +123,7 @@ static void g_virtual_block_init(GVirtualBlock *block) parent = G_INSTR_BLOCK(block); + parent->list_blocks = (list_all_blocks_fc)g_virtual_block_list_all_blocks; parent->list_regs = (list_regs_accesses_fc)g_virtual_block_list_regs_accesses; } @@ -200,6 +204,30 @@ GInstrBlock *g_virtual_block_new(void) /****************************************************************************** * * * Paramètres : block = bloc d'instructions à consulter. * +* list = ensemble de blocs à compléter. [OUT] * +* count = nombre de blocs au total. [OUT] * +* * +* Description : Etablit la liste de tous les blocs présents. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_virtual_block_list_all_blocks(const GVirtualBlock *block, GInstrBlock ***list, size_t *count) +{ + size_t i; /* Boucle de parcours */ + + for (i = 0; i < block->children_count; i++) + g_instr_block_list_all_blocks(block->children[i], list, count); + +} + + +/****************************************************************************** +* * +* Paramètres : block = bloc d'instructions à consulter. * * count = nombre de registres consignés. [OUT] * * * * Description : Fournit les différents accès aux registres. * diff --git a/src/analysis/disass/disassembler.c b/src/analysis/disass/disassembler.c index 6147f3b..e7c972d 100644 --- a/src/analysis/disass/disassembler.c +++ b/src/analysis/disass/disassembler.c @@ -257,7 +257,7 @@ static void g_delayed_disassembly_process(GDelayedDisassembly *disass, GtkExtSta run_plugins_on_binary(disass->binary, PGA_BINARY_BOUNDED, true); /* Quatrième étape */ -#if 0 + id = gtk_extended_status_bar_push(statusbar, _("Grouping routines instructions..."), true); qsort(routines, routines_count, sizeof(GBinRoutine *), (__compar_fn_t)g_binary_routine_rcompare); @@ -267,7 +267,7 @@ static void g_delayed_disassembly_process(GDelayedDisassembly *disass, GtkExtSta gtk_extended_status_bar_remove(statusbar, id); run_plugins_on_binary(disass->binary, PGA_BINARY_GROUPED, true); -#endif + /* Cinquième étape */ id = gtk_extended_status_bar_push(statusbar, _("Printing disassembled code..."), true); diff --git a/src/analysis/disass/macro.c b/src/analysis/disass/macro.c index 7fda6b9..46dc0d3 100644 --- a/src/analysis/disass/macro.c +++ b/src/analysis/disass/macro.c @@ -41,6 +41,16 @@ typedef struct _branch_info } branch_info; +/** + * Macros pour le marquage des instructions traitées. + * Dans un soucis d'optimisation, on ne traite que les instructions + * démarrant un bloc. + */ +#define MACRO_MARK_AS_PROCESSED(_instr) g_object_set_data(G_OBJECT(_instr), "macro_done", _instr) +#define MACRO_IS_PROCESSED(_instr) (g_object_get_data(G_OBJECT(_instr), "macro_done") != NULL) +#define MACRO_CLEAR_PROCESSED(_instr) g_object_set_data(G_OBJECT(_instr), "macro_done", NULL) + + /* Indique si une adresse est retenue comme point de passage. */ static bool is_addr_in_branch(const branch_info *, const vmpa_t *, bool); @@ -50,6 +60,9 @@ static void find_next_jumps(GArchInstruction *, vmpa_t, vmpa_t, branch_info *); /* Retrouve le point de ralliement entre deux branches. */ static vmpa_t compute_first_common_addr(branch_info *, branch_info *); +/* Retrouve le point de ralliement entre un groupe de branches. */ +static vmpa_t compute_first_common_addr_in_group(const branch_info *, size_t); + /* Procède à la définition de bloc regroupant des instructions. */ static GInstrBlock *build_instruction_block(GArchInstruction *, vmpa_t, vmpa_t, vmpa_t); @@ -144,6 +157,7 @@ static void find_next_jumps(GArchInstruction *instrs, vmpa_t start, vmpa_t end, { case ILT_EXEC_FLOW: case ILT_JUMP: + case ILT_CASE_JUMP: case ILT_JUMP_IF_TRUE: case ILT_JUMP_IF_FALSE: g_arch_instruction_get_location(dests[i], NULL, NULL, &addr); @@ -204,6 +218,49 @@ static vmpa_t compute_first_common_addr(branch_info *a, branch_info *b) /****************************************************************************** * * +* Paramètres : list = liste d'ensembles de jalons à parcourir. * +* count = taille de cette liste. * +* * +* Description : Retrouve le point de ralliement entre un groupe de branches. * +* * +* Retour : Adresse commune aux branches. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static vmpa_t compute_first_common_addr_in_group(const branch_info *list, size_t count) +{ + vmpa_t result; /* Adresse trouvée à retourner */ + size_t i; /* Boucle de parcours #1 */ + bool keep; /* Candidate à garder ? */ + size_t j; /* Boucle de parcours #2 */ + + /* Valeur conceptuellement impossible à renvoyer */ + result = VMPA_MAX; + + //qsort(a->jumps, a->count, sizeof(vmpa_t), (__compar_fn_t)compare_vmpa); + //qsort(b->jumps, b->count, sizeof(vmpa_t), (__compar_fn_t)compare_vmpa); + + for (i = 0; i < list[0].count && result == VMPA_MAX; i++) + { + keep = true; + + for (j = 1; j < count && keep; j++) + keep = is_addr_in_branch(&list[j], &list[0].jumps[i], false); + + if (keep) + result = list[0].jumps[i]; + + } + + return result; + +} + + +/****************************************************************************** +* * * Paramètres : instrs = ensemble des instructions d'assemblage. * * start = adresse de début du bloc. * * end = adresse de fin du bloc (exclusive). * @@ -220,6 +277,7 @@ static vmpa_t compute_first_common_addr(branch_info *a, branch_info *b) static GInstrBlock *build_instruction_block(GArchInstruction *instrs, vmpa_t start, vmpa_t end, vmpa_t stop) { GInstrBlock *result; /* Regroupement à retourner */ + branch_info main_branch; /* Flot d'exécution complet */ GArchInstruction *first; /* Première instruction */ GArchInstruction *last; /* Dernière instruction */ GArchInstruction *iter; /* Boucle de parcours */ @@ -227,17 +285,26 @@ static GInstrBlock *build_instruction_block(GArchInstruction *instrs, vmpa_t sta GArchInstruction **dests; /* Instr. visée par une autre */ InstructionLinkType *types; /* Type de lien entre lignes */ size_t dcount; /* Nombre de liens de dest. */ - size_t i; /* Boucle de parcours */ + size_t i; /* Boucle de parcours #1 */ GInstrBlock *block; /* Nouveau bloc mis en place */ + branch_info *cases_branches; /* Branches d'un aiguillage */ + size_t cases_count; /* Nombre d'aiguillages */ branch_info true_branch; /* Branche 'condition vraie' */ branch_info false_branch; /* Branche 'condition fausse' */ + branch_info *excep_branches; /* Branches d'exceptions */ + size_t excep_count; /* Nombre d'exceptions */ vmpa_t next_addr; /* Prochaine instruction visée */ + size_t j; /* Boucle de parcours #2 */ + vmpa_t stop_addr; /* Adresse de fin de bloc */ result = NULL; first = NULL; last = NULL; + memset(&main_branch, 0, sizeof(branch_info)); + find_next_jumps(instrs, start, end, &main_branch); + //printf("[+] blocking 0x%08llx -> 0x%08llx... stop @ 0x%08llx\n", start, end, stop); for (iter = g_arch_instruction_find_by_address(instrs, start, true); @@ -247,15 +314,14 @@ static GInstrBlock *build_instruction_block(GArchInstruction *instrs, vmpa_t sta g_arch_instruction_get_location(iter, NULL, NULL, &addr); if (addr == stop) break; + /* On s'arrêter si l'instruction est déjà décompilée */ + if (MACRO_IS_PROCESSED(iter)) break; + if (first == NULL) first = iter; last = iter; - /* On s'arrêter si l'instruction est déjà décompilée */ - if (g_object_get_data(G_OBJECT(iter), "decomp_done") != NULL) break; - g_object_set_data(G_OBJECT(iter), "decomp_done", iter); - /* On n'approfondit que les chemins qui se séparent */ if (!g_arch_instruction_has_destinations(iter)) { @@ -268,8 +334,12 @@ static GInstrBlock *build_instruction_block(GArchInstruction *instrs, vmpa_t sta dcount = g_arch_instruction_get_destinations(iter, &dests, &types); next_addr = 0; + cases_branches = NULL; + cases_count = 0; memset(&true_branch, 0, sizeof(branch_info)); memset(&false_branch, 0, sizeof(branch_info)); + excep_branches = NULL; + excep_count = 0; for (i = 0; i < dcount; i++) switch (types[i]) @@ -281,6 +351,7 @@ static GInstrBlock *build_instruction_block(GArchInstruction *instrs, vmpa_t sta result = g_virtual_block_new(); block = g_flow_block_new(instrs, first, iter); + MACRO_MARK_AS_PROCESSED(first); g_virtual_block_add_child(G_VIRTUAL_BLOCK(result), block); first = NULL; @@ -288,6 +359,17 @@ static GInstrBlock *build_instruction_block(GArchInstruction *instrs, vmpa_t sta break; + case ILT_CASE_JUMP: + + g_arch_instruction_get_location(dests[i], NULL, NULL, &addr); + + cases_branches = (branch_info *)realloc(cases_branches, + ++cases_count * sizeof(branch_info)); + memset(&cases_branches[cases_count - 1], 0, sizeof(branch_info)); + find_next_jumps(instrs, addr, end, &cases_branches[cases_count - 1]); + + break; + case ILT_JUMP_IF_TRUE: g_arch_instruction_get_location(dests[i], NULL, NULL, &addr); find_next_jumps(instrs, addr, end, &true_branch); @@ -298,12 +380,97 @@ static GInstrBlock *build_instruction_block(GArchInstruction *instrs, vmpa_t sta find_next_jumps(instrs, addr, end, &false_branch); break; + case ILT_CATCH_EXCEPTION: + + g_arch_instruction_get_location(dests[i], NULL, NULL, &addr); + + excep_branches = (branch_info *)realloc(excep_branches, + ++excep_count * sizeof(branch_info)); + memset(&excep_branches[excep_count - 1], 0, sizeof(branch_info)); + find_next_jumps(instrs, addr, end, &excep_branches[excep_count - 1]); + + break; + default: - next_addr = VMPA_MAX; + if (next_addr == 0) + next_addr = VMPA_MAX; break; } + /* Post-traitements de ILT_CASE_JUMP */ + if (cases_count > 0) + { + if (result == NULL) + result = g_virtual_block_new(); + + if (first != NULL) + { + block = g_flow_block_new(instrs, first, iter); + MACRO_MARK_AS_PROCESSED(first); + g_virtual_block_add_child(G_VIRTUAL_BLOCK(result), block); + first = NULL; + } + + //printf(" --- cases --- start\n"); + + next_addr = compute_first_common_addr_in_group(cases_branches, cases_count); + //printf(" stop :: 0x%08llx\n", next_addr); + + for (j = 0; j < cases_count; j++) + { + //printf(" ## %zu\n", j); + + block = build_instruction_block(instrs, cases_branches[j].jumps[0], end, next_addr); + + if (block != NULL) + + g_virtual_block_add_child(G_VIRTUAL_BLOCK(result), block); + + free(cases_branches[j].jumps); + + } + + //printf(" --- cases --- end\n"); + + + free(cases_branches); + + } + + /* Post-traitements de ILT_CATCH_EXCEPTION */ + if (excep_count > 0) + { + if (result == NULL) + result = g_virtual_block_new(); + + if (first != NULL) + { + block = g_flow_block_new(instrs, first, iter); + MACRO_MARK_AS_PROCESSED(first); + g_virtual_block_add_child(G_VIRTUAL_BLOCK(result), block); + first = NULL; + } + + for (j = 0; j < excep_count; j++) + { + stop_addr = compute_first_common_addr(&main_branch, &excep_branches[j]); + //next_addr = MIN(next_addr, end); + + block = build_instruction_block(instrs, excep_branches[j].jumps[0], end, stop_addr); + + if (block != NULL) + + g_virtual_block_add_child(G_VIRTUAL_BLOCK(result), block); + + free(excep_branches[j].jumps); + + } + + free(excep_branches); + + } + if (next_addr == VMPA_MAX) { iter = g_arch_instruction_get_next_iter(instrs, iter, end); @@ -318,14 +485,24 @@ static GInstrBlock *build_instruction_block(GArchInstruction *instrs, vmpa_t sta if (result == NULL) result = g_virtual_block_new(); - block = g_flow_block_new(instrs, first, iter); - g_virtual_block_add_child(G_VIRTUAL_BLOCK(result), block); - first = NULL; + if (first != NULL) + { + block = g_flow_block_new(instrs, first, iter); + MACRO_MARK_AS_PROCESSED(first); + g_virtual_block_add_child(G_VIRTUAL_BLOCK(result), block); + first = NULL; + } block = build_instruction_block(instrs, true_branch.jumps[0], end, next_addr); + + if (block != NULL) + g_virtual_block_add_child(G_VIRTUAL_BLOCK(result), block); block = build_instruction_block(instrs, false_branch.jumps[0], end, next_addr); + + if (block != NULL) + g_virtual_block_add_child(G_VIRTUAL_BLOCK(result), block); free(true_branch.jumps); @@ -342,15 +519,27 @@ static GInstrBlock *build_instruction_block(GArchInstruction *instrs, vmpa_t sta if (first != NULL && last != NULL) { - block = g_flow_block_new(instrs, first, last); + if (!MACRO_IS_PROCESSED(first)) + { + //printf("--close?--\n"); + block = g_flow_block_new(instrs, first, last); + MACRO_MARK_AS_PROCESSED(first); + //printf("--close!--\n"); - if (result == NULL) - result = block; - else - g_virtual_block_add_child(G_VIRTUAL_BLOCK(result), block); + if (result == NULL) + result = block; + else + g_virtual_block_add_child(G_VIRTUAL_BLOCK(result), block); + } } + if (result == NULL) + { + //printf("WARNING :: null !\n"); + //exit(0); + } + return result; } @@ -389,6 +578,7 @@ void group_routines_instructions(GArchInstruction *list, GBinRoutine **routines, block = build_instruction_block(list, start, end, VMPA_MAX); + g_binary_routine_set_basic_blocks(routines[i], block); gtk_extended_status_bar_update_activity(statusbar, id, (i + 1) * 1.0 / count); diff --git a/src/analysis/routine.c b/src/analysis/routine.c index d52b74a..048d11a 100644 --- a/src/analysis/routine.c +++ b/src/analysis/routine.c @@ -57,6 +57,7 @@ struct _GBinRoutine size_t locals_count; /* Nombre de variables locales */ GArchInstruction *instr; /* Instructions natives */ + GInstrBlock *blocks; /* Blocs basiques d'instruct° */ GDecInstruction *dinstr; /* Instructions décompilées */ }; @@ -800,6 +801,48 @@ void g_binary_routine_set_instructions(GBinRoutine *routine, GArchInstruction *i * * * Paramètres : routine = routine à consulter. * * * +* Description : Fournit les blocs basiques de la routine. * +* * +* Retour : Ensemble de blocs déterminés via les instructions. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GInstrBlock *g_binary_routine_get_basic_blocks(const GBinRoutine *routine) +{ + return routine->blocks; + +} + + +/****************************************************************************** +* * +* Paramètres : routine = routine à mettre à jour. * +* blocks = ensemble de blocs déterminés via les instructions. * +* * +* Description : Définit les blocs basiques de la routine. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void g_binary_routine_set_basic_blocks(GBinRoutine *routine, GInstrBlock *blocks) +{ + if (routine->blocks != NULL) + g_object_unref(G_OBJECT(routine->blocks)); + + routine->blocks = blocks; + +} + + +/****************************************************************************** +* * +* Paramètres : routine = routine à consulter. * +* * * Description : Fournit les instructions décompilées correspondantes. * * * * Retour : Ensemble d'instructions décompilées ou NULL. * diff --git a/src/analysis/routine.h b/src/analysis/routine.h index c6de039..fd77f5e 100644 --- a/src/analysis/routine.h +++ b/src/analysis/routine.h @@ -152,6 +152,12 @@ GArchInstruction *g_binary_routine_get_instructions(const GBinRoutine *); /* Définit les instructions natives de la routine. */ void g_binary_routine_set_instructions(GBinRoutine *, GArchInstruction *); +/* Fournit les blocs basiques de la routine. */ +GInstrBlock *g_binary_routine_get_basic_blocks(const GBinRoutine *); + +/* Définit les blocs basiques de la routine. */ +void g_binary_routine_set_basic_blocks(GBinRoutine *, GInstrBlock *); + /* Fournit les instructions décompilées correspondantes. */ GDecInstruction *g_binary_routine_get_decomp_instructions(const GBinRoutine *); diff --git a/src/glibext/gcodebuffer.c b/src/glibext/gcodebuffer.c index 1f1ff71..22bdcd8 100644 --- a/src/glibext/gcodebuffer.c +++ b/src/glibext/gcodebuffer.c @@ -121,10 +121,10 @@ static void g_code_buffer_class_init(GCodeBufferClass *); static void g_code_buffer_init(GCodeBuffer *); /* Convertit une adresse en indice de ligne. */ -static size_t _g_code_buffer_get_index_from_address(GCodeBuffer *, vmpa_t); +static size_t _g_code_buffer_get_index_from_address(GCodeBuffer *, vmpa_t, bool); /* Convertit une adresse en indice de ligne. */ -static size_t g_code_buffer_get_index_from_address(GCodeBuffer *, vmpa_t); +static size_t g_code_buffer_get_index_from_address(GCodeBuffer *, vmpa_t, bool); @@ -296,8 +296,8 @@ static void g_buffer_scan_process(GBufferScan *scan, GtkExtStatusBar *statusbar) /* TODO : lock scan->buffer->lines */ - first = g_code_buffer_get_index_from_address(scan->buffer, scan->start); - last = g_code_buffer_get_index_from_address(scan->buffer, scan->end); + first = g_code_buffer_get_index_from_address(scan->buffer, scan->start, true); + last = g_code_buffer_get_index_from_address(scan->buffer, scan->end, false); lines = scan->buffer->lines; @@ -397,6 +397,7 @@ GCodeBuffer *g_code_buffer_new(void) * * * Paramètres : buffer = composant GTK à mettre à jour. * * addr = adresse où va se situer la ligne. * +* first = indique si on l'arrête à la première ou la dernière.* * * * Description : Convertit une adresse en indice de ligne. * * * @@ -406,7 +407,7 @@ GCodeBuffer *g_code_buffer_new(void) * * ******************************************************************************/ -static size_t _g_code_buffer_get_index_from_address(GCodeBuffer *buffer, vmpa_t addr) +static size_t _g_code_buffer_get_index_from_address(GCodeBuffer *buffer, vmpa_t addr, bool first) { size_t result; /* Indice à retourner */ @@ -419,6 +420,11 @@ static size_t _g_code_buffer_get_index_from_address(GCodeBuffer *buffer, vmpa_t if (g_buffer_line_get_address(buffer->lines[result]) == addr) break; + if (!first) + for (; result < (buffer->used - 1); result++) + if (g_buffer_line_get_address(buffer->lines[result + 1]) != addr) + break; + return result; } @@ -428,6 +434,7 @@ static size_t _g_code_buffer_get_index_from_address(GCodeBuffer *buffer, vmpa_t * * * Paramètres : buffer = composant GTK à mettre à jour. * * addr = adresse où va se situer la ligne. * +* first = indique si on l'arrête à la première ou la dernière.* * * * Description : Convertit une adresse en indice de ligne. * * * @@ -437,11 +444,11 @@ static size_t _g_code_buffer_get_index_from_address(GCodeBuffer *buffer, vmpa_t * * ******************************************************************************/ -static size_t g_code_buffer_get_index_from_address(GCodeBuffer *buffer, vmpa_t addr) +static size_t g_code_buffer_get_index_from_address(GCodeBuffer *buffer, vmpa_t addr, bool first) { size_t result; /* Indice à retourner */ - result = _g_code_buffer_get_index_from_address(buffer, addr); + result = _g_code_buffer_get_index_from_address(buffer, addr, first); /** * Par commodités, on évite certaines instructions en cas d'échec dans les @@ -511,7 +518,7 @@ GBufferLine *g_code_buffer_insert_at(GCodeBuffer *buffer, vmpa_t addr, bool befo GBufferLine *result; /* Instance à retourner */ size_t index; /* Indice de la ligne visée */ - index = _g_code_buffer_get_index_from_address(buffer, addr); + index = _g_code_buffer_get_index_from_address(buffer, addr, true /* FIXME : after */); if (index == buffer->used) return NULL; if (buffer->used == buffer->count) @@ -586,7 +593,7 @@ GBufferLine *g_code_buffer_find_line_by_addr(const GCodeBuffer *buffer, vmpa_t a GBufferLine *result; /* Instance à retourner */ size_t index; /* Indice de la ligne visée */ - index = _g_code_buffer_get_index_from_address(buffer, addr); + index = _g_code_buffer_get_index_from_address(buffer, addr, true); if (index == buffer->used) result = NULL; @@ -907,8 +914,8 @@ static void g_buffer_view_compute_required_widths(GBufferView *view, bool addr, lines = view->buffer->lines; - first = g_code_buffer_get_index_from_address(view->buffer, view->start); - last = g_code_buffer_get_index_from_address(view->buffer, view->end); + first = g_code_buffer_get_index_from_address(view->buffer, view->start, true); + last = g_code_buffer_get_index_from_address(view->buffer, view->end, false); view->left_margin = 2 * view->line_height; view->left_text = 2.5 * view->line_height; @@ -1032,8 +1039,8 @@ void g_buffer_view_get_size(GBufferView *view, gint *width, gint *height, bool a *width += + MAX(col_width, full_width); - first = g_code_buffer_get_index_from_address(view->buffer, view->start); - last = g_code_buffer_get_index_from_address(view->buffer, view->end); + first = g_code_buffer_get_index_from_address(view->buffer, view->start, true); + last = g_code_buffer_get_index_from_address(view->buffer, view->end, false); *height *= (last - first + 1); @@ -1129,8 +1136,8 @@ void g_buffer_view_highlight_segments(GBufferView *view, gint x, gint y) else need_redraw = false; - first = g_code_buffer_get_index_from_address(view->buffer, view->start); - last = g_code_buffer_get_index_from_address(view->buffer, view->end); + first = g_code_buffer_get_index_from_address(view->buffer, view->start, true); + last = g_code_buffer_get_index_from_address(view->buffer, view->end, false); for (i = first; i < last; i++) view->highlighted = g_buffer_line_highlight_all_same_segments(view->buffer->lines[i], @@ -1210,13 +1217,13 @@ void g_buffer_view_draw(const GBufferView *view, const GdkEventExpose *event, Gd - first = g_code_buffer_get_index_from_address(view->buffer, view->start); + first = g_code_buffer_get_index_from_address(view->buffer, view->start, true); first += (real_y / view->line_height); last = first + (event->area.height / view->line_height); if (event->area.height % view->line_height > 0) last++; - end = g_code_buffer_get_index_from_address(view->buffer, view->end); + end = g_code_buffer_get_index_from_address(view->buffer, view->end, false); last = MIN(last, end); y = event->area.y - (real_y % view->line_height); @@ -1299,8 +1306,8 @@ bool g_buffer_view_get_address_coordinates(GBufferView *view, vmpa_t addr, gint lheight = g_buffer_view_get_line_height(view); current = VMPA_MAX; - first = g_code_buffer_get_index_from_address(view->buffer, view->start); - last = g_code_buffer_get_index_from_address(view->buffer, view->end); + first = g_code_buffer_get_index_from_address(view->buffer, view->start, true); + last = g_code_buffer_get_index_from_address(view->buffer, view->end, false); if (view->buffer->used > 0) for (i = first; i <= last; i++) diff --git a/src/gtkext/gtkgraphview.c b/src/gtkext/gtkgraphview.c index 543f2d7..67fb577 100644 --- a/src/gtkext/gtkgraphview.c +++ b/src/gtkext/gtkgraphview.c @@ -28,6 +28,7 @@ #include "gtkbufferview.h" #include "gtkviewpanel-int.h" #include "graph/layout.h" +#include "../analysis/blocks/flow.h" #include "../gui/editem.h" @@ -92,8 +93,10 @@ static void gtk_graph_view_cache_glance(GtkGraphView *, cairo_t *); /* Supprime tout contenu de l'afficheur de code en graphique. */ static void gtk_graph_view_reset(GtkGraphView *); -/* Liste d'éléments du graphique à placer. */ -static GtkViewPanel **gtk_graph_view_load_nodes(GtkGraphView *, GLoadedBinary *, vmpa_t, vmpa_t); +/* Définit la liste complète des éléments du futur graphique. * +* * +* Retour : Liste d'éléments du graphique à placer. */ +static GtkViewPanel **gtk_graph_view_load_nodes(GtkGraphView *, GLoadedBinary *, const GBinRoutine *); @@ -381,7 +384,7 @@ static void gtk_graph_view_define_main_address(GtkGraphView *view, vmpa_t addr) view->end = end; view->children = gtk_graph_view_load_nodes(view, GTK_VIEW_PANEL(view)->binary, - start, end); + routines[i]); view->allocs = (GtkAllocation *)calloc(view->children_count, sizeof(GtkAllocation)); @@ -650,10 +653,8 @@ static void gtk_graph_view_reset(GtkGraphView *view) /****************************************************************************** * * -* Paramètres : view = composant d'affichage GTK à mettre à jour. * -* binary = contenu binaire à l'origine des lignes. * -* start = première adresse à traiter. * -* end = première adresse hors cadre de l'opération. * +* Paramètres : view = composant d'affichage GTK à mettre à jour. * +* routine = routine à présenter via ledit composant. * * * * Description : Définit la liste complète des éléments du futur graphique. * * * @@ -663,94 +664,48 @@ static void gtk_graph_view_reset(GtkGraphView *view) * * ******************************************************************************/ -static GtkViewPanel **gtk_graph_view_load_nodes(GtkGraphView *view, GLoadedBinary *binary, vmpa_t start, vmpa_t end) +static GtkViewPanel **gtk_graph_view_load_nodes(GtkGraphView *view, GLoadedBinary *binary, const GBinRoutine *routine) { GtkViewPanel **result; /* Liste à retourner */ - size_t *count; /* Nombre d'éléments créés. */ - GArchInstruction *list; /* Liste des instructions */ GCodeBuffer *buffer; /* Tampon brut à découper */ bool *addr; /* Affichage des adresses ? */ bool *code; /* Affichage du binaire ? */ + size_t *count; /* Nombre d'éléments créés. */ + GInstrBlock *main_block; /* Premier bloc rattaché */ + GInstrBlock **blocks; /* Liste des blocs basiques */ + size_t i; /* Boucle de parcours */ vmpa_t first; /* Début d'un groupe de lignes */ - GArchInstruction *iter; /* Boucle de parcours */ vmpa_t last; /* Fin d'un groupe de lignes */ GBufferView *subview; /* Partie affichée du tampon */ - result = NULL; - - count = &view->children_count; - *count = 0; - - list = g_loaded_binary_get_instructions(binary); buffer = g_loaded_binary_get_disassembled_buffer(binary); addr = GTK_VIEW_PANEL(view)->display_addr; code = GTK_VIEW_PANEL(view)->display_code; - first = start; - last = first; - - for (iter = g_arch_instruction_find_by_address(list, start, true); - iter != NULL; - iter = g_arch_instruction_get_next_iter(list, iter, end)) - { - if (first != VMPA_MAX && g_arch_instruction_has_sources(iter)) - { - result = (GtkViewPanel **)realloc(result, ++(*count) * sizeof(GtkViewPanel *)); - - result[*count - 1] = GTK_VIEW_PANEL(gtk_block_view_new()); - gtk_widget_show(GTK_WIDGET(result[*count - 1])); - gtk_view_panel_attach_binary(result[*count - 1], binary, addr, code); - - gtk_view_panel_show_border(result[*count - 1], true); - - subview = g_buffer_view_new(buffer); - g_buffer_view_restrict(subview, first, last); - gtk_buffer_view_attach_buffer(GTK_BUFFER_VIEW(result[*count - 1]), - subview, addr, code); - - first = VMPA_MAX; - - } - - g_arch_instruction_get_location(iter, NULL, NULL, &last); - if (first == VMPA_MAX) first = last; - - if (g_arch_instruction_has_destinations(iter) || g_arch_instruction_is_return(iter)) - { - result = (GtkViewPanel **)realloc(result, ++(*count) * sizeof(GtkViewPanel *)); - - result[*count - 1] = GTK_VIEW_PANEL(gtk_block_view_new()); - gtk_widget_show(GTK_WIDGET(result[*count - 1])); - gtk_view_panel_attach_binary(result[*count - 1], binary, addr, code); - - gtk_view_panel_show_border(result[*count - 1], true); - - subview = g_buffer_view_new(buffer); - g_buffer_view_restrict(subview, first, last); - gtk_buffer_view_attach_buffer(GTK_BUFFER_VIEW(result[*count - 1]), - subview, addr, code); + count = &view->children_count; - first = VMPA_MAX; + main_block = g_binary_routine_get_basic_blocks(routine); - } + blocks = NULL; + *count = 0; + g_instr_block_list_all_blocks(main_block, &blocks, count); - } + result = (GtkViewPanel **)calloc(*count, sizeof(GtkViewPanel *)); - if (first != VMPA_MAX) + for (i = 0; i < *count; i++) { - result = (GtkViewPanel **)realloc(result, ++(*count) * sizeof(GtkViewPanel *)); + result[i] = GTK_VIEW_PANEL(gtk_block_view_new()); + gtk_widget_show(GTK_WIDGET(result[i])); + gtk_view_panel_attach_binary(result[i], binary, addr, code); - result[*count - 1] = GTK_VIEW_PANEL(gtk_block_view_new()); - gtk_widget_show(GTK_WIDGET(result[*count - 1])); - gtk_view_panel_attach_binary(result[*count - 1], binary, addr, code); + gtk_view_panel_show_border(result[i], true); - gtk_view_panel_show_border(result[*count - 1], true); + g_flow_block_get_boundary_addresses(G_FLOW_BLOCK(blocks[i]), &first, &last); subview = g_buffer_view_new(buffer); g_buffer_view_restrict(subview, first, last); - gtk_buffer_view_attach_buffer(GTK_BUFFER_VIEW(result[*count - 1]), - subview, addr, code); + gtk_buffer_view_attach_buffer(GTK_BUFFER_VIEW(result[i]), subview, addr, code); } -- cgit v0.11.2-87-g4458