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

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


#include "../../common/bits.h"



/* Suit un flot d'exécution à la recherche de boucles. */
static void track_loops_in_code(const GArchProcessor *, const mrange_t *, const vmpa2t *, memfield_t *);



/******************************************************************************
*                                                                             *
*  Paramètres  : proc  = 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(const GArchProcessor *proc, const mrange_t *range, const vmpa2t *start, memfield_t *flow)
{
    bool exit_track;                        /* Détermine la fin du parcours*/
    GArchInstruction *iter;                 /* Boucle de parcours          */
    const mrange_t *irange;                 /* Emplacement d'instruction   */
    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 vmpa2t *addr;                     /* Prochaine adresse de saut   */
    memfield_t *next_flow;                  /* Suite de l'exécution        */

    set_in_mem_field(flow, start);

    exit_track = false;

    for (iter = g_arch_processor_find_instr_by_address(proc, start);
         iter != NULL && !exit_track;
         iter = g_arch_instruction_get_next_iter(iter /* FIXME : list*/, iter, ~0))
    {
        /* L'instruction sort-elle des clous ? */

        irange = g_arch_instruction_get_range(iter);

        if (!mrange_contains_mrange(range, irange))
            break;

        /* Fin de parcours ? */

        if (g_arch_instruction_get_flags(iter) & AIF_RETURN_POINT)
            break;

        /**
         * Afin de détecter les boucles le plus en aval possible,
         * on marque toutes les arrivées potentielles de boucles comme jalons.
         * Ainsi la détection se réalise sur l'ultime saut qui boucle effectivement.
         */
        if (g_arch_instruction_has_sources(iter))
        {
            addr = get_mrange_addr(irange);

            if (!test_in_mem_field(flow, addr))
                set_in_mem_field(flow, start);

        }

        /* Analyse des destinations */

        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_CATCH_EXCEPTION:

                    irange = g_arch_instruction_get_range(dests[i]);
                    addr = get_mrange_addr(irange);

                    next_flow = create_mem_field_from(flow);
                    track_loops_in_code(proc, range, addr, next_flow);
                    delete_mem_field(next_flow);

                    break;

                case ILT_EXEC_FLOW:
                case ILT_JUMP:
                case ILT_CASE_JUMP:
                case ILT_JUMP_IF_TRUE:
                case ILT_JUMP_IF_FALSE:

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

                    irange = g_arch_instruction_get_range(dests[i]);

                    if (!mrange_contains_mrange(range, irange))
                        break;

                    addr = get_mrange_addr(irange);

                    if (test_in_mem_field(flow, addr))
                        /* status = */g_arch_instruction_change_link(iter, dests[i], types[i], ILT_LOOP);

                    else
                    {
                        next_flow = dup_mem_field(flow);
                        track_loops_in_code(proc, range, addr, next_flow);
                        delete_mem_field(next_flow);
                    }

                    break;

                default:
                    break;

            }

    }

}


/******************************************************************************
*                                                                             *
*  Paramètres  : proc      = 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(const GArchProcessor *proc, GBinRoutine **routines, size_t count, GtkExtStatusBar *statusbar, bstatus_id_t id)
{
    size_t i;                               /* Boucle de parcours          */
    const mrange_t *range;                  /* Couverture d'une routine    */
    memfield_t *flow;                       /* Flot d'exécution à suivre   */

    for (i = 0; i < count; i++)
    {
        range = g_binary_routine_get_range(routines[i]);

        flow = create_mem_field(range);
        track_loops_in_code(proc, range, get_mrange_addr(range), flow);
        delete_mem_field(flow);

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

    }

}