summaryrefslogtreecommitdiff
path: root/src/analysis/disass/rank.c
blob: 758e15d849b49e34b278d93b2f43e1cef9d02ca2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

/* Chrysalide - Outil d'analyse de fichiers binaires
 * rank.c - classement des blocs d'instructions
 *
 * Copyright (C) 2013 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  OpenIDA 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.
 *
 *  OpenIDA 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "rank.h"


#include <assert.h>
#include <sys/param.h>


#include "../blocks/flow.h"
#include "../blocks/virtual.h"



/* Classe le contenu d'un bloc d'instructions exécutées. */
static bool rank_flow_block(GFlowBlock *, BlockVisitOrder, const GInstrBlock *);



/******************************************************************************
*                                                                             *
*  Paramètres  : block = bloc d'instructions concerné par la visite.          *
*                order = indication quant au sens de visite.                  *
*                list  = ensemble des blocs basiques à parcourir.             *
*                                                                             *
*  Description : Classe le contenu d'un bloc d'instructions exécutées.        *
*                                                                             *
*  Retour      : true pour continuer la visite.                               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool rank_flow_block(GFlowBlock *block, BlockVisitOrder order, const GInstrBlock *list)
{
    unsigned int next;                      /* Rang suivant obtenu         */
    GInstrBlock *links;                     /* Blocs liés au bloc courant  */
    GArchInstruction *last;                 /* Dernière instruction du bloc*/
    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          */
    const mrange_t *range;                  /* Emplacement d'une cible     */
    GFlowBlock *target;                     /* Bloc ciblé par un lien      */
    unsigned int rank;                      /* Rang à constituer           */

    /* Si le bloc visité n'est pas basique... */
    if (order != BVO_PENDING) return true;

    next = g_flow_block_get_rank(block) + 1;

    links = g_instr_block_get_links_block(G_INSTR_BLOCK(block));

    g_flow_block_get_boundary(block, NULL, &last);
    dcount = g_arch_instruction_get_destinations(last, &dests, &types, NULL);

    for (i = 0; i < dcount; i++)
    {
        range = g_arch_instruction_get_range(dests[i]);

        switch (types[i])
        {
            case ILT_EXEC_FLOW:
            case ILT_CATCH_EXCEPTION:
                target = G_FLOW_BLOCK(g_instr_block_find_by_addr(list, get_mrange_addr(range), true));
                assert(target != NULL);
                break;

            case ILT_JUMP:
                target = G_FLOW_BLOCK(g_instr_block_find_by_addr(list, get_mrange_addr(range), true));

                /**
                 * Les sauts ne se font pas toujours à l'intérieur d'une même fonction.
                 * Par exemple sous ARM :
                 *
                 *    00008358 <call_gmon_start>:
                 *        ....
                 *        8362:       f7ff bfcf       b.w     8304 <_init+0x38>
                 *        ....
                 *
                 */
                /* assert(target != NULL);*/

                break;

            case ILT_CASE_JUMP:
                target = G_FLOW_BLOCK(g_instr_block_find_by_addr(links, get_mrange_addr(range), true));
                assert(target != NULL);
                break;

            case ILT_JUMP_IF_TRUE:
            case ILT_JUMP_IF_FALSE:

                /**
                 * Même dans les branches if/then/else, il n'y a pas forcément de groupe de blocs
                 * associé. C'est par exemple le cas dans des constructions telles que celle de
                 * la fonction __libc_csu_init() :
                 *
                 *    if (...)
                 *       do
                 *       {
                 *          ...
                 *       }
                 *       while (...)
                 *
                 *    ...
                 *
                 * Le code final est étiquetté comme étant le 'else' de la première condition,
                 * donc au niveau de la condition de bouclage, il appartient déjà à un autre
                 * et n'est donc pas réattribué une seconde fois.
                 *
                 */

                if (links != NULL)
                    target = G_FLOW_BLOCK(g_instr_block_find_by_addr(links, get_mrange_addr(range), true));
                else
                    target = NULL;

                /**
                 * Le bloc visé ne se trouve pas toujours dans les blocs attachés :
                 *
                 * { bloc IF }
                 *    { bloc THEN }
                 * { block NEXT }
                 *
                 * Le bloc NEXT est ici à rechercher dans la liste globale.
                 */

                if (target == NULL)
                    target = G_FLOW_BLOCK(g_instr_block_find_by_addr(list, get_mrange_addr(range), true));

                assert(target != NULL);

                break;

            default:
                target = NULL;
                break;

        }

        if (target != NULL)
        {
            rank = MAX(next, g_flow_block_get_rank(target));

            g_flow_block_set_rank(target, rank);

        }

    }

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : list      = ensemble d'instructions à relier.                *
*                routines  = prototypes existants à insérer.                  *
*                count     = quantité de ces prototypes.                      *
*                statusbar = barre de statut avec progression à mettre à jour.*
*                id        = identifiant du message affiché à l'utilisateur.  *
*                                                                             *
*  Description : Classe les blocs des routines.                               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void rank_routines_blocks(GBinRoutine **routines, size_t count, GtkExtStatusBar *statusbar, bstatus_id_t id)
{
    size_t i;                               /* Boucle de parcours          */
    GInstrBlock *main_block;                /* Ensemble des blocs d'instr. */

    for (i = 0; i < count; i++)
    {
        main_block = g_binary_routine_get_basic_blocks(routines[i]);

        g_instr_block_visit(main_block, (instr_block_visitor_cb)rank_flow_block, main_block);



        printf("===== BLOCK(S) xXXx ======\n");


        bool visit_ranked_block(GInstrBlock *blk, BlockVisitOrder order, int *indent)
        {
            int i;

            switch (order)
            {
                case BVO_IN:
                case BVO_PENDING:

                    for (i = 0; i < *indent; i++)
                        printf("   ");

                    printf("%p '%s'", blk, G_OBJECT_TYPE_NAME(blk));

                    if (G_IS_FLOW_BLOCK(blk))
                    {
                        vmpa2t start;
                        vmpa2t end;

                        g_flow_block_get_boundary_addresses(G_FLOW_BLOCK(blk), &start, &end);

                        printf("  0x%08x -> 0x%08x (rank = %u)",
                               (unsigned int)start.virtual,
                               (unsigned int)end.virtual,
                               g_flow_block_get_rank(G_FLOW_BLOCK(blk)));

                    }

                    printf("\n");

                    if (order == BVO_IN) (*indent)++;
                    break;

                case BVO_OUT:
                    (*indent)--;
                    break;

            }

            return true;

        }

        g_instr_block_visit(main_block, (instr_block_visitor_cb)visit_ranked_block, (int []){ 0 });

        printf("\n");












    }

}