summaryrefslogtreecommitdiff
path: root/src/analysis/disass/loop.c
blob: 79e3786006366721e7a21d8a5e4a516c7c797d61 (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * loop.c - détection des boucles dans du code machine
 *
 * 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 "loop.h"


#include <malloc.h>
#include <stdlib.h>
#include <string.h>



/* Suivi du flot d'exécution */
typedef struct _exec_flow
{
    vmpa_t *steps;                          /* Jalons dans l'exécution     */
    size_t count;                           /* Quantité de ces points      */

} exec_flow;


/* Initialise un flot d'exécution. */
static exec_flow *create_exec_flow(void);

/* Réalise la copie d'un flot d'exécution. */
static exec_flow *dup_exec_flow(const exec_flow *);

/* Efface de la mémoire un flot d'exécution. */
static void delete_exec_flow(exec_flow *);

/* Recherche si le chemin d'exécution a déjà mené à une adresse. */
static bool is_new_exec_flow(const exec_flow *, vmpa_t);

/* Ajoute une adresse jalon dans un flot d'exécution. */
static void add_step_into_exec_flow(exec_flow *, vmpa_t);

/* Suit un flot d'exécution à la recherche de boucles. */
static void track_loops_in_code(GArchInstruction *, vmpa_t, vmpa_t, exec_flow *);



/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Initialise un flot d'exécution.                              *
*                                                                             *
*  Retour      : Flot d'exécution nouveau.                                    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static exec_flow *create_exec_flow(void)
{
    exec_flow *result;                      /* Flot vierge à retourner     */

    result = (exec_flow *)calloc(1, sizeof(exec_flow));

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : src = jalons de l'exécution à dupliquer.                     *
*                                                                             *
*  Description : Réalise la copie d'un flot d'exécution.                      *
*                                                                             *
*  Retour      : Flot d'exécution copié.                                      *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static exec_flow *dup_exec_flow(const exec_flow *src)
{
    exec_flow *result;                      /* Copie à retourner           */

    result = (exec_flow *)calloc(1, sizeof(exec_flow));

    result->steps = (vmpa_t *)malloc(src->count * sizeof(vmpa_t));
    memcpy(result->steps, src->steps, src->count * sizeof(vmpa_t));

    result->count = src->count;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : flow = jalons de l'exécution à supprimer.                    *
*                                                                             *
*  Description : Efface de la mémoire un flot d'exécution.                    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void delete_exec_flow(exec_flow *flow)
{
    free(flow->steps);

    free(flow);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : flow = ensemble des jalons de l'exécution du code.           *
*                addr = adresse d'un nouvel embranchement.                    *
*                                                                             *
*  Description : Recherche si le chemin d'exécution a déjà mené à une adresse.*
*                                                                             *
*  Retour      : true si l'instruction est visitée pour la première fois.     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool is_new_exec_flow(const exec_flow *flow, vmpa_t addr)
{
    void *ret;                              /* Conclusion d'une recherche  */

    ret = bsearch(&addr, flow->steps, flow->count,
                  sizeof(vmpa_t), (__compar_fn_t)compare_vmpa);

    return (ret == NULL);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : flow = jalons de l'exécution du code à compléter.            *
*                addr = adresse d'un nouvel embranchement.                    *
*                                                                             *
*  Description : Ajoute une adresse jalon dans un flot d'exécution.           *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void add_step_into_exec_flow(exec_flow *flow, vmpa_t addr)
{
    flow->steps = (vmpa_t *)realloc(flow->steps, ++flow->count * sizeof(vmpa_t));
    flow->steps[flow->count - 1] = addr;

    qsort(flow->steps, flow->count, sizeof(vmpa_t), (__compar_fn_t)compare_vmpa);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : list  = ensemble d'instructions à parcourir.                 *
*                start = adresse du début de l'analyse.                       *
*                end   = adresse de fin de la routine traitée.                *
*                flow  = ensemble des jalons de l'exécution du code.          *
*                                                                             *
*  Description : Suit un flot d'exécution à la recherche de boucles.          *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void track_loops_in_code(GArchInstruction *list, vmpa_t start, vmpa_t end, exec_flow *flow)
{
    bool exit_track;                        /* Détermine la fin du parcours*/
    GArchInstruction *iter;                 /* Boucle de parcours          */
    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          */
    vmpa_t addr;                            /* Prochaine adresse de saut   */
    exec_flow *next_flow;                   /* Suite de l'exécution        */

    add_step_into_exec_flow(flow, start);

    exit_track = false;

    for (iter = g_arch_instruction_find_by_address(list, start, true);
         iter != NULL && !exit_track;
         iter = g_arch_instruction_get_next_iter(list, iter, end))
    {
        if (g_arch_instruction_is_return(iter))
            break;

        dcount = g_arch_instruction_get_destinations(iter, &dests, &types, NULL);
        if (dcount == 0) continue;

        for (i = 0; i < dcount; i++)
            switch (types[i])
            {
                case ILT_LOOP:
                    /**
                     * On est déjà passé par là, donc on peut arrêter le parcours courant.
                     */
                    exit_track = true;
                    break;

                case ILT_CALL:
                case ILT_CATCH_EXCEPTION:
                    break;

                default:
                    /**
                     * On se lance dans d'autres suivis qui vont parcourir le reste des
                     * instructions, donc on peut arrêter le parcours courant ici.
                     */
                    exit_track = true;

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

                    if (!is_new_exec_flow(flow, addr))
                        types[i] = ILT_LOOP;

                    else
                    {
                        next_flow = dup_exec_flow(flow);
                        track_loops_in_code(list, addr, end, next_flow);
                        delete_exec_flow(next_flow);
                    }

                    break;

            }

    }

}


/******************************************************************************
*                                                                             *
*  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 : Détecte les boucles dans du code machine.                    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void detect_loops_in_code(GArchInstruction *list, GBinRoutine **routines, size_t count, GtkExtStatusBar *statusbar, bstatus_id_t id)
{
    size_t i;                               /* Boucle de parcours          */
    vmpa_t start;                           /* Adresse de départ           */
    vmpa_t end;                             /* Adresse de fin              */
    exec_flow *flow;                        /* Flot d'exécution à suivre   */

    for (i = 0; i < count; i++)
    {
        start = g_binary_routine_get_address(routines[i]);
        end = start + g_binary_routine_get_size(routines[i]);

        flow = create_exec_flow();
        track_loops_in_code(list, start, end, flow);
        delete_exec_flow(flow);

        gtk_extended_status_bar_update_activity(statusbar, id, (i + 1) * 1.0 / count);

    }

}