summaryrefslogtreecommitdiff
path: root/src/analysis/disass/rank.c
blob: 6ff194beea2970f605ce284cb8a546349af1b822 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411

/* Chrysalide - Outil d'analyse de fichiers binaires
 * rank.c - classement des blocs d'instructions
 *
 * Copyright (C) 2013-2017 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide 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.
 *
 *  Chrysalide 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"




#if 0

#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*/
    size_t dcount;                          /* Nombre de liens de dest.    */
    size_t i;                               /* Boucle de parcours          */
    instr_link_t *dest;                     /* Instr. visée par une autre  */
    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);

    g_arch_instruction_lock_dest(last);
    dcount = g_arch_instruction_count_destinations(last);

    for (i = 0; i < dcount; i++)
    {
        dest = g_arch_instruction_get_destination(last, i);

        range = g_arch_instruction_get_range(dest->linked);

        switch (dest->type)
        {
            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);

        }

    }

    g_arch_instruction_unlock_dest(last);

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : routine = routine regroupant les blocs à traiter.            *
*                                                                             *
*  Description : Classe les blocs des routines.                               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void rank_routine_blocks(GBinRoutine *routine)
{
    GInstrBlock *main_block;                /* Ensemble des blocs d'instr. */

    main_block = g_binary_routine_get_basic_blocks(routine);


    if (main_block == NULL) return;


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


#if 0

    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");


#endif




}



#endif







#include <assert.h>



/* Classe les blocs basiques d'une routine. */
void rank_routine_block(const GBlockList *, GBasicBlock *);




/******************************************************************************
*                                                                             *
*  Paramètres  : list  = ensemble de blocs basiques à traiter.                *
*                block = bloc d'analyse courant.                              *
*                                                                             *
*  Description : Classe les blocs basiques d'une routine.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void rank_routine_block(const GBlockList *list, GBasicBlock *block)
{
    unsigned int next;                      /* Rang suivant obtenu         */
    GArchInstruction *last;                 /* Dernière instruction du bloc*/
    size_t dcount;                          /* Nombre de liens de dest.    */
    size_t i;                               /* Boucle de parcours          */
    instr_link_t *dest;                     /* Instr. visée par une autre  */
    InstructionLinkType type;               /* Raccourci pour confort      */
    GBasicBlock *target;                    /* Bloc ciblé par un lien      */
    unsigned int rank;                      /* Rang à constituer           */

    next = g_basic_block_get_rank(block) + 1;

    g_basic_block_get_boundary(block, NULL, &last);

    g_arch_instruction_lock_dest(last);
    dcount = g_arch_instruction_count_destinations(last);

    for (i = 0; i < dcount; i++)
    {
        dest = g_arch_instruction_get_destination(last, i);

        type = dest->type;

        /* La boucle de remontée n'abaisse pas les rangs */
        if (type == ILT_LOOP) continue;

        /**
         * On se doit de suivre le même cheminement que celui emprunté lors
         * du parcours de create_dragon_nodes().
         * Sinon, les chemins divergent et une récursion infinie peut survenir.
         */
        if (type != ILT_EXEC_FLOW
            && type != ILT_JUMP
            && type != ILT_CASE_JUMP
            && type != ILT_JUMP_IF_TRUE
            && type != ILT_JUMP_IF_FALSE)
            continue;

        target = g_block_list_find_by_starting_instr(list, dest->linked);

        /**
         * 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>
         *        ....
         *
         */

        if (target != NULL)
        {
            rank = g_basic_block_get_rank(target);

            if (next > rank || rank == -1)
            {
                g_basic_block_set_rank(target, next);

                rank_routine_block(list, target);

            }

        }

    }

    g_arch_instruction_unlock_dest(last);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : routine = routine regroupant les blocs à traiter.            *
*                                                                             *
*  Description : Classe les blocs des routines.                               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void rank_routine_blocks(GBinRoutine *routine)
{
    GBlockList *blocks;                     /* Ensemble des blocs d'instr. */
    GBasicBlock *start;                     /* Bloc basique de départ      */

    blocks = g_binary_routine_get_basic_blocks(routine);

    start = g_block_list_get_block(blocks, 0 /* FIXME */);
    assert(start != NULL);


    g_basic_block_set_rank(start, 0);


    rank_routine_block(blocks, start);






}