summaryrefslogtreecommitdiff
path: root/src/gtkext/graph/layout.c
blob: 1748c40e864d033d7cfbd4de8945729e4bc97309 (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

/* OpenIDA - Outil d'analyse de fichiers binaires
 * layout.c - mise en place de graphique
 *
 * Copyright (C) 2009-2013 Cyrille Bagard
 *
 *  This file is part of OpenIDA.
 *
 *  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 "layout.h"


#include <malloc.h>
#include <string.h>
#include <gtk/gtkfixed.h>


#include "dot.h"
#include "node.h"
#include "../gtkbufferview.h"
#include "../../analysis/binary.h"
#include "../../analysis/blocks/flow.h"
#include "../../common/extstr.h"



/* Taille maximale des introductions aux clusters */
#define CLUSTER_DESC_LEN 128

/* Taille maximale des descriptions de liens */
#define LINKS_DESC_LEN 128


/* Paramètres de construction des commandes */
typedef struct _visitor_dot_params
{
    GGraphNode **nodes;                     /* Intermédiaires en place     */
    size_t count;                           /* Quantité de noeuds en place */

    unsigned int level;                     /* Profondeur de la visite     */
    char *cmds;                             /* Description à envoyer à dot */

} visitor_dot_params;


/* Construit les commandes pour dot en parcourant les noeuds. */
static bool register_graph_nodes(GInstrBlock *, BlockVisitOrder, visitor_dot_params *);

/* Etablit tous les liens entre les différents morceaux de code. */
static char *complete_graph_links(const GtkGraphView *, GtkViewPanel **, size_t, char *);



/******************************************************************************
*                                                                             *
*  Paramètres  : view  = support où placer les différents éléments.           *
*                views = morceaux de code à afficher de façon organisée.      *
*                count = quantité de ces morceaux de code.                    *
*                                                                             *
*  Description : Dispose une série de morceaux d'affichage en graphique.      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool build_graph_view(GtkGraphView *view, GInstrBlock *blocks, GtkViewPanel **views, size_t count)
{
    visitor_dot_params params;              /* Paramètres de construction  */
    size_t i;                               /* Boucle de parcours          */
    graph_layout *layout;                   /* Graphique construit         */
    GtkLinkRenderer **links;                /* Liens graphiques construits */
    size_t links_count;                     /* Quantité de ces liens       */

    /* Création de la glue */

    params.nodes = (GGraphNode **)calloc(count, sizeof(GGraphNode *));
    params.count = count;

    for (i = 0; i < count; i++)
        params.nodes[i] = g_graph_node_new(GTK_WIDGET(views[i]));
 
    /* Définition du graphique */

    params.level = 1;
    params.cmds = strdup("digraph G {\n  overlap=false;\n  splines=ortho;\n  compound=true;\n");

    g_instr_block_visit(blocks, (instr_block_visitor_cb)register_graph_nodes, &params);

    params.cmds = complete_graph_links(view, views, count, params.cmds);

    params.cmds = stradd(params.cmds, "}");

    layout = create_graph_layout(params.cmds);

    /* Affichage du graphique */

    place_nodes_of_graph_layout(layout, view,  params.nodes, count);

    links = create_links_from_graph_layout(layout, &links_count,  params.nodes, count);
    gtk_graph_view_attach_links(view, links, links_count);

    gtk_widget_queue_draw(GTK_WIDGET(view));

    delete_graph_layout(layout);

    for (i = 0; i < count; i++)
        g_object_unref(params.nodes[i]);

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : block  = bloc d'instructions concerné par la visite.         *
*                order  = position dans la visite.                            *
*                params = informations à mettre à jour pour dot.              *
*                                                                             *
*  Description : Construit les commandes pour dot en parcourant les noeuds.   *
*                                                                             *
*  Retour      : true si le parcours a été jusqu'à son terme, false sinon.    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool register_graph_nodes(GInstrBlock *block, BlockVisitOrder order, visitor_dot_params *params)
{
    char *cmds;                             /* Raccourci d'usage pratique  */
    vmpa_t start;                           /* Adresse de départ d'un bloc */
    GGraphNode *node;                       /* Noeud rattaché              */
    unsigned int i;                         /* Boucle de parcours          */
    char buffer[CLUSTER_DESC_LEN];          /* Tampon pour les commandes   */

    cmds = params->cmds;

    if (G_IS_FLOW_BLOCK(block))
    {
        g_flow_block_get_boundary_addresses(G_FLOW_BLOCK(block), &start, NULL);
        node = find_graph_node_by_start_address(params->nodes, params->count, start);

        cmds = g_graph_node_register_for_dot(node, cmds, params->level);

    }
    else
    {
        if (order == BVO_IN)
        {
            for (i = 0; i < params->level; i++)
                cmds = stradd(cmds, DOT_IDENT);

            snprintf(buffer, CLUSTER_DESC_LEN, "subgraph cluster_v%p {\n", block);
            cmds = stradd(cmds, buffer);

            params->level++;

            for (i = 0; i < params->level; i++)
                cmds = stradd(cmds, DOT_IDENT);

            cmds = stradd(cmds, "style=invisible;\n");

        }
        else if (order == BVO_OUT)
        {
            params->level--;

            for (i = 0; i < params->level; i++)
                cmds = stradd(cmds, DOT_IDENT);

            cmds = stradd(cmds, "}\n");

        }

    }

    params->cmds = cmds;

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : view  = support contenant les différentes lignes.            *
*                views = morceaux de code à afficher de façon organisée.      *
*                count = quantité de ces morceaux de code.                    *
*                desc  = description du graphique à compléter. [OUT]          *
*                                                                             *
*  Description : Etablit tous les liens entre les différents morceaux de code.*
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static char *complete_graph_links(const GtkGraphView *view, GtkViewPanel **views, size_t count, char *desc)
{
    GLoadedBinary *binary;                  /* Binaire rattaché aux vues   */
    GArchInstruction *instrs;               /* Instructions pour assembleur*/
    GBufferView *buffer;                    /* Tampon d'une partie de code */
    vmpa_t end;                             /* Adresse finale du tampon    */
    size_t i;                               /* Boucle de parcours #1       */
    GArchInstruction *last;                 /* Dernière instruc. d'un bloc */
    vmpa_t addr;                            /* Addresse 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 j;                               /* Boucle de parcours #2       */
    size_t k;                               /* Boucle de parcours #3       */
    char cmd[LINKS_DESC_LEN];               /* Tampon pour l'ajout de liens*/

    if (count == 0)
        return desc;

    binary = gtk_view_panel_get_binary(views[0]);
    instrs = g_loaded_binary_get_instructions(binary);

    for (i = 0; i < count; i++)
    {
        buffer = gtk_buffer_view_get_buffer(GTK_BUFFER_VIEW(views[i]));
        g_buffer_view_get_restrictions(buffer, NULL, &end);

        last = g_arch_instruction_find_by_address(instrs, end, true);
        g_arch_instruction_get_location(last, NULL, NULL, &addr);

        if (g_arch_instruction_has_destinations(last))
        {
            dcount = g_arch_instruction_get_destinations(last, &dests, &types, NULL);

            for (j = 0; j < dcount; j++)
            {
                g_arch_instruction_get_location(dests[j], NULL, NULL, &addr);

                for (k = 0; k < count; k++)
                    if (gtk_view_panel_contain_address(views[k], addr))
                        break;

                if (k < count)
                    switch (types[j])
                    {
                        case ILT_EXEC_FLOW:
                        case ILT_JUMP:
                        case ILT_CASE_JUMP:
                            snprintf(cmd, LINKS_DESC_LEN,
                                     "_%p:s -> _%p:n  [ltail=cluster_%p, lhead=cluster_%p];\n",
                                     views[i], views[k], views[i], views[k]);
                            desc = stradd(desc, cmd);
                            break;

                        case ILT_JUMP_IF_TRUE:
                            snprintf(cmd, LINKS_DESC_LEN,
                                     "_%p:s -> _%p:n  [ltail=cluster_%p, lhead=cluster_%p, " \
                                     "color=green];\n",
                                     views[i], views[k], views[i], views[k]);
                            desc = stradd(desc, cmd);
                            break;

                        case ILT_JUMP_IF_FALSE:
                            snprintf(cmd, LINKS_DESC_LEN,
                                     "_%p:s -> _%p:n  [ltail=cluster_%p, lhead=cluster_%p, " \
                                     "color=red];\n",
                                     views[i], views[k], views[i], views[k]);
                            desc = stradd(desc, cmd);
                            break;

                        case ILT_LOOP:
                            snprintf(cmd, LINKS_DESC_LEN,
                                     "_%p:s -> _%p:n  [ltail=cluster_%p, lhead=cluster_%p, " \
                                     "color=blue];\n",
                                     views[i], views[k], views[i], views[k]);
                            desc = stradd(desc, cmd);
                            break;

                        case ILT_CATCH_EXCEPTION:
                            snprintf(cmd, LINKS_DESC_LEN,
                                     "_%p:s -> _%p:n  [ltail=cluster_%p, lhead=cluster_%p, " \
                                     "color=gray];\n",
                                     views[i], views[k], views[i], views[k]);
                            desc = stradd(desc, cmd);
                            break;

                        default:
                            break;

                    }

            }

        }

    }

    return desc;

}