summaryrefslogtreecommitdiff
path: root/src/analysis/disass/rank.c
blob: 9dfdb428f2b63df58311626405e810e47525e1e3 (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
412
413

/* 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 "../blocks/flow.h"
#include "../blocks/virtual.h"


#if 0
/* Marque la profondeur d'un bloc d'instructions. */
static void rank_flow_block_content(GFlowBlock *, const GInstrBlock *);



/******************************************************************************
*                                                                             *
*  Paramètres  : block = bloc d'instructions démarrant la visite.             *
*                list  = ensemble des blocs basiques à parcourir.             *
*                                                                             *
*  Description : Marque la profondeur d'un bloc d'instructions.               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void rank_flow_block_content(GFlowBlock *block, const GInstrBlock *list)
{
    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 k;                               /* Boucle de parcours #1       */
    size_t i;                               /* Boucle de parcours #2       */
    vmpa_t addr;                            /* Adresse de la destination   */
    GInstrBlock *next;                      /* Bloc suivant à visiter      */
    bool loop;                              /* Détection de rebouclage     */
    //unsigned int old_rank;                  /* Rang avant intervention     */

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

    for (k = 0; k < 2; k++)
        for (i = 0; i < dcount; i++)
            switch (types[i])
            {
                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);
                    next = g_instr_block_find_by_addr(list, addr, true);

                    /**
                     * On traite en premier les liens qui conduisent à un rebouclage,
                     * afin d'avoir des indices importants à offrir pour les autres liens
                     * par la suite.
                     */
                    loop = g_flow_block_is_looping_to(G_FLOW_BLOCK(next), list, block);
                    //if (loop != (k == 0)) continue;

                    if (loop)
                    printf("next :: %u (i=%zu k=%zu)\n", g_flow_block_get_next_rank(block), i, k);


                    if (loop == (k == 0))
                    {
                    g_flow_block_set_rank(G_FLOW_BLOCK(next), g_flow_block_get_next_rank(block));

                    rank_flow_block_content(G_FLOW_BLOCK(next), list);
                    }
                    break;

                case ILT_LOOP:
                    g_arch_instruction_get_location(dests[i], NULL, NULL, &addr);
                    next = g_instr_block_find_by_addr(list, addr, true);


                    /**
                     * On traite en premier les liens qui conduisent à un rebouclage,
                     * afin d'avoir des indices importants à offrir pour les autres liens
                     * par la suite.
                     */
                    loop = g_flow_block_is_looping_to(G_FLOW_BLOCK(next), list, block);
                    //if (loop != (k == 0)) continue;


                    if (loop == (k == 0))
                    {
                        printf("loop next :: %u (i=%zu k=%zu)\n", g_flow_block_get_next_rank(block), i, k);


                    g_flow_block_set_next_rank(G_FLOW_BLOCK(next),
                                               g_flow_block_get_next_rank(block));

                    }

                    break;

                default:
                    break;

            }







#if 0
    /**
     * Si un bloc contient un retour sur lui même, on définit l'indice pour les
     * blocs suivants découlant de cette boucle avant de traiter ces blocs suivants.
     */
    for (i = 0; i < dcount; i++)
        switch (types[i])
        {
            case ILT_LOOP:
                g_arch_instruction_get_location(dests[i], NULL, NULL, &addr);
                next = g_instr_block_find_by_addr(list, addr, true);
                g_flow_block_set_next_rank(G_FLOW_BLOCK(next), g_flow_block_get_next_rank(block));
                break;

            default:
                break;

        }

    for (i = 0; i < dcount; i++)
        switch (types[i])
        {
            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);
                next = g_instr_block_find_by_addr(list, addr, true);

                old_rank = g_flow_block_get_rank(G_FLOW_BLOCK(next));
                g_flow_block_set_rank(G_FLOW_BLOCK(next), g_flow_block_get_next_rank(block));

                /* Si un traitement n'a pas déjà été fait... */
                if (old_rank == 0 || 1)
                    rank_flow_block_content(G_FLOW_BLOCK(next), list);

            default:
                break;

        }
#endif

}

#endif




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

/* Classe le contenu d'un bloc d'instructions virtuel. */
static unsigned int rank_virtual_block(GVirtualBlock *, const GInstrBlock *, unsigned int);

/* Classe le contenu d'un bloc d'instructions quelconque. */
static unsigned int rank_instructions_block(GInstrBlock *, const GInstrBlock *, unsigned int);






/******************************************************************************
*                                                                             *
*  Paramètres  : block = bloc d'instructions concerné par la visite.          *
*                list  = ensemble des blocs basiques à parcourir.             *
*                rank  = rang courant du classement en courant.               *
*                                                                             *
*  Description : Classe le contenu d'un bloc d'instructions exécutées.        *
*                                                                             *
*  Retour      : Rang pour les blocs suivants.                                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static unsigned int rank_flow_block(GFlowBlock *block, const GInstrBlock *list, unsigned int rank)
{
    unsigned int result;                    /* Rang suivant à retourner    */
    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 k;                               /* Boucle de parcours #1       */
    size_t i;                               /* Boucle de parcours #2       */
    vmpa_t addr;                            /* Adresse de la destination   */
    GInstrBlock *next;                      /* Bloc suivant à visiter      */
    bool loop;                              /* Détection de rebouclage     */

    /* On traite le bloc courant */

    result = rank;
    g_flow_block_set_rank(block, result++);

    /* Viennent ensuite les blocs rattachés */

    links = g_instr_block_get_links_block(G_INSTR_BLOCK(block));

    if (links != NULL)
    {
        g_flow_block_get_boundary(block, NULL, &last);
        dcount = g_arch_instruction_get_destinations(last, &dests, &types, NULL);

        for (k = 0; k < 2; k++)
            for (i = 0; i < dcount; i++)
                switch (types[i])
                {
                    case ILT_CASE_JUMP:
                    case ILT_JUMP_IF_TRUE:
                    case ILT_JUMP_IF_FALSE:

                        g_arch_instruction_get_location(dests[i], NULL, NULL, &addr);

                        next = g_instr_block_find_by_addr(links, addr, true);

                        /**
                         * En cas d'une branche unique, l'adresse peut ne pas être trouvée
                         * dans les sous-blocs ; logiquement, elle sera traitée plus tard,
                         * dans la continuité de ce bloc.
                         */
                        if (next == NULL) break;

                        /**
                         * On traite en premier les liens qui conduisent à un rebouclage,
                         * afin d'avoir des indices importants à offrir pour les autres liens
                         * par la suite.
                         */
                        loop = g_flow_block_is_looping_to(G_FLOW_BLOCK(next), list, block);
                        if (loop != (k == 0)) continue;

                        next = g_instr_block_find_by_addr(links, addr, false);
                        result = MAX(rank_instructions_block(next, list, rank + 1), result);

                        break;

                    default:
                        break;

                }

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : block = bloc d'instructions concerné par la visite.          *
*                list  = ensemble des blocs basiques à parcourir.             *
*                rank  = rang courant du classement en courant.               *
*                                                                             *
*  Description : Classe le contenu d'un bloc d'instructions virtuel.          *
*                                                                             *
*  Retour      : Rang pour les blocs suivants.                                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static unsigned int rank_virtual_block(GVirtualBlock *block, const GInstrBlock *list, unsigned int rank)
{
    unsigned int result;                    /* Rang suivant à retourner    */
    size_t max;                             /* Borne du parcours           */
    size_t i;                               /* Boucle de parcours          */
    GInstrBlock *child;                     /* Sous-bloc à traiter         */

    result = rank;

    max = g_virtual_block_count_children(block);

    for (i = 0; i < max; i++)
    {
        child = g_virtual_block_get_child(block, i);
        if (!G_IS_FLOW_BLOCK(child)) continue;

        result = rank_instructions_block(child, list, result);

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : block = bloc d'instructions concerné par la visite.          *
*                list  = ensemble des blocs basiques à parcourir.             *
*                rank  = rang courant du classement en courant.               *
*                                                                             *
*  Description : Classe le contenu d'un bloc d'instructions quelconque.       *
*                                                                             *
*  Retour      : Rang pour les blocs suivants.                                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static unsigned int rank_instructions_block(GInstrBlock *block, const GInstrBlock *list, unsigned int rank)
{
    unsigned int result;                    /* Rang suivant à retourner    */

    if (G_IS_VIRTUAL_BLOCK(block))
        result = rank_virtual_block(G_VIRTUAL_BLOCK(block), list, rank);

    else if (G_FLOW_BLOCK(block))
        result = rank_flow_block(G_FLOW_BLOCK(block), list, rank);

    else
        result = rank;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : routine = routine à traiter.                                 *
*                                                                             *
*  Description : Classe les blocs d'une routine donnée.                       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void rank_routine_blocks(GBinRoutine *routine)
{
    GInstrBlock *list;                      /* Ensemble des blocs d'instr. */
    //vmpa_t start;                           /* Adresse de départ           */
    //GFlowBlock *first;                      /* Premier bloc de la routine  */

    list = g_binary_routine_get_basic_blocks(routine);

    rank_instructions_block(list, list, 0);

    /*
    start = g_binary_routine_get_address(routine);
    first = G_FLOW_BLOCK(g_instr_block_find_by_addr(list, start, true));

    rank_flow_block_content(first, list);
    */

}


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

    for (i = 0; i < count; i++)
        rank_routine_blocks(routines[i]);

}