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

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



/* Matérialise les liens de retour arrière en tant que boucles. */
static void detect_back_edges(dragon_node *, size_t);



/******************************************************************************
*                                                                             *
*  Paramètres  : nodes = liste de noeuds détectés dans une routine.           *
*                count = taille de cette liste de noeuds à traiter.           *
*                                                                             *
*  Description : Matérialise les liens de retour arrière en tant que boucles. *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void detect_back_edges(dragon_node *nodes, size_t count)
{
    size_t k;                               /* Boucle de parcours #1       */
    dragon_node *node;                      /* Noeud à traiter             */
    const bitfield_t *dominators;           /* Liste de dominateurs        */
    GArchInstruction *last;                 /* Instruction finale de noeud */
    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 #2       */
    dragon_node *target;                    /* Noeud référence à tester    */
    size_t id;                              /* Indice du bit associé       */

    for (k = 1; k < count; k++)
    {
        node = get_dragon_node(nodes, k);

        dominators = get_domination_bits(node);

        get_dragon_node_bounding_instructions(node, NULL, &last);

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

        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:

                    target = find_node_for_instruction(nodes, count, false, dests[i]);
                    if (target == NULL) break;

                    id = get_dragon_node_index(nodes, target);

                    if (test_in_bit_field(dominators, id, 1))
                    {

                        /*
                        printf("BACKEDGE :: 0x%08lx -> 0x%08lx\n",
                               (unsigned int)g_arch_instruction_get_range(last)->addr.virtual,
                               (unsigned int)g_arch_instruction_get_range(dests[i])->addr.virtual);
                        */

                        /* status = */g_arch_instruction_change_link(last, dests[i], types[i], ILT_LOOP);


                    }

                    break;

                default:
                    break;

            }

    }

}


/******************************************************************************
*                                                                             *
*  Paramètres  : knight = informations quant à la complexité gérée du code.   *
*                                                                             *
*  Description : Détecte les boucles dans du code machine.                    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void detect_loops_in_code(dragon_knight *knight)
{
    dragon_node *nodes;                     /* Liste des noeuds détectés   */
    size_t count;                           /* Taille de cette liste       */

    get_dragon_knight_content(knight, &nodes, &count);

    compute_all_dominators(nodes, count);

    detect_back_edges(nodes, count);

}