summaryrefslogtreecommitdiff
path: root/plugins/readdex/code.c
blob: d17c4ccf3c1b26e308daeeeee474efd3cf30126b (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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563

/* Chrysalide - Outil d'analyse de fichiers binaires
 * code.c - annotation des éléments de code Dalvik
 *
 * Copyright (C) 2016-2017 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide 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.
 *
 *  Chrysalide 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 "code.h"


#include <plugins/dex/dex_def.h>
#include <plugins/fmtp/parser.h>



/* Définition des champs */

typedef struct _code_item_data
{
    uint16_t tries_size;                    /* Nombre de gestionnaires     */
    uint32_t insns_size;                    /* Nombre d'instructions       */

} code_item_data;

/* Récupère le nombre de couvertures pour exceptions. */
static bool get_code_tries_size_value(const fmt_field_def *, GBinContent *, vmpa2t *, SourceEndian, code_item_data *);

/* Récupère le nombre de blocs d'instructions. */
static bool get_code_insns_size_value(const fmt_field_def *, GBinContent *, vmpa2t *, SourceEndian, code_item_data *);

/* Récupère le nombre d'éléments d'une liste de couvertures. */
static bool get_encoded_catch_handler_list_size_value(const fmt_field_def *, GBinContent *, vmpa2t *, SourceEndian, uleb128_t *);

/* Récupère le nombre d'exécptions gérées dans une couverture. */
static bool get_encoded_catch_handler_size_value(const fmt_field_def *, GBinContent *, vmpa2t *, SourceEndian, leb128_t *);


static fmt_field_def _dex_code_item[] = {

    {
        .name = "registers_size",

        .size = MDS_16_BITS,
        .repeat = 1,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Number of registers used by this code"))

    },

    {
        .name = "ins_size",

        .size = MDS_16_BITS,
        .repeat = 1,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Number of words of incoming arguments to the method that this code is for"))

    },

    {
        .name = "outs_size",

        .size = MDS_16_BITS,
        .repeat = 1,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Number of words of outgoing argument space required by this code for method invocation"))

    },

    {
        .name = "tries_size",

        .get_value = (get_fdef_value_cb)get_code_tries_size_value,

        .size = MDS_16_BITS,
        .repeat = 1,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Number of try_items for this instance"))

    },

    {
        .name = "debug_info_off",

        .size = MDS_32_BITS,
        .repeat = 1,

        PLAIN_COMMENT(__("Offset to the debug info sequence for this code"))

    },

    {
        .name = "insns_size",

        .get_value = (get_fdef_value_cb)get_code_insns_size_value,

        .size = MDS_32_BITS,
        .repeat = 1,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Size of the instructions list, in 16-bit code units"))

    }

};

static fmt_field_def _dex_code_item_padding[] = {

    {
        .name = "padding",

        .size = MDS_16_BITS,
        .repeat = 1,

        PLAIN_COMMENT(__("Padding"))

    }

};

static fmt_field_def _dex_try_item[] = {

    {
        .name = "start_addr",

        .size = MDS_32_BITS,
        .repeat = 1,

        PLAIN_COMMENT(__("Start address of the block of code covered by this entry"))

    },

    {
        .name = "insn_count",

        .size = MDS_16_BITS,
        .repeat = 1,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Number of 16-bit code units covered by this entry"))

    },

    {
        .name = "handler_off",

        .size = MDS_16_BITS,
        .repeat = 1,

        PLAIN_COMMENT(__("Offset to the encoded_catch_handler for this entry"))

    }

};

static fmt_field_def _dex_encoded_catch_handler_list[] = {

    {
        .name = "size",

        .get_value = (get_fdef_value_cb)get_encoded_catch_handler_list_size_value,

        .is_uleb128 = true,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Size of the list, in entries"))

    }

};

static fmt_field_def _dex_encoded_catch_handler[] = {

    {
        .name = "size",

        .get_value = (get_fdef_value_cb)get_encoded_catch_handler_size_value,

        .is_leb128 = true,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Number of static fields defined in this item"))

    }

};

static fmt_field_def _dex_encoded_catch_handler_all[] = {

    {
        .name = "catch_all_addr",

        .is_uleb128 = true,

        PLAIN_COMMENT(__("Bytecode address of the catch-all handler"))

    }

};

static fmt_field_def _dex_encoded_type_addr_pair[] = {

    {
        .name = "type_idx",

        .is_uleb128 = true,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Index for the type of the exception to catch"))

    },

    {
        .name = "addr",

        .is_uleb128 = true,

        DISPLAY_RULES(IOD_DEC),

        PLAIN_COMMENT(__("Bytecode address of the associated exception handler"))

    }

};



/* Commente les définitions d'une protection contre exceptions. */
static bool annotate_dex_try_item(const GDexFormat *, GPreloadInfo *, vmpa2t *);

/*Commente les définitions des listes de gestion d'exceptions. */
static bool annotate_dex_encoded_catch_handler_list(const GDexFormat *, GPreloadInfo *, vmpa2t *);

/* Commente les définitions d'une prise en compte d'exceptions. */
static bool annotate_dex_encoded_catch_handler(const GDexFormat *, GPreloadInfo *, vmpa2t *);

/* Commente les définitions des gestions d'exceptions par type. */
static bool annotate_dex_encoded_type_addr_pair(const GDexFormat *, GPreloadInfo *, vmpa2t *);



/******************************************************************************
*                                                                             *
*  Paramètres  : def     = définition à l'origine de l'appel.                 *
*                content = contenu binaire à venir lire.                      *
*                pos     = position de la tête de lecture.                    *
*                endian  = ordre des bits dans la source.                     *
*                data    = lieu d'enregistrement de la lecture. [OUT]         *
*                                                                             *
*  Description : Récupère le nombre de couvertures pour exceptions.           *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool get_code_tries_size_value(const fmt_field_def *def, GBinContent *content, vmpa2t *pos, SourceEndian endian, code_item_data *data)
{
    bool result;                            /* Bilan à retourner           */

    result = g_binary_content_read_u16(content, pos, endian, &data->tries_size);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : def     = définition à l'origine de l'appel.                 *
*                content = contenu binaire à venir lire.                      *
*                pos     = position de la tête de lecture.                    *
*                endian  = ordre des bits dans la source.                     *
*                data    = lieu d'enregistrement de la lecture. [OUT]         *
*                                                                             *
*  Description : Récupère le nombre de blocs d'instructions.                  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool get_code_insns_size_value(const fmt_field_def *def, GBinContent *content, vmpa2t *pos, SourceEndian endian, code_item_data *data)
{
    bool result;                            /* Bilan à retourner           */

    result = g_binary_content_read_u32(content, pos, endian, &data->insns_size);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : def     = définition à l'origine de l'appel.                 *
*                content = contenu binaire à venir lire.                      *
*                pos     = position de la tête de lecture.                    *
*                endian  = ordre des bits dans la source.                     *
*                size    = lieu d'enregistrement de la lecture. [OUT]         *
*                                                                             *
*  Description : Récupère le nombre d'éléments d'une liste de couvertures.    *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool get_encoded_catch_handler_list_size_value(const fmt_field_def *def, GBinContent *content, vmpa2t *pos, SourceEndian endian, uleb128_t *size)
{
    bool result;                            /* Bilan à retourner           */

    result = g_binary_content_read_uleb128(content, pos, size);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : def     = définition à l'origine de l'appel.                 *
*                content = contenu binaire à venir lire.                      *
*                pos     = position de la tête de lecture.                    *
*                endian  = ordre des bits dans la source.                     *
*                size    = lieu d'enregistrement de la lecture. [OUT]         *
*                                                                             *
*  Description : Récupère le nombre d'exécptions gérées dans une couverture.  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool get_encoded_catch_handler_size_value(const fmt_field_def *def, GBinContent *content, vmpa2t *pos, SourceEndian endian, leb128_t *size)
{
    bool result;                            /* Bilan à retourner           */

    result = g_binary_content_read_leb128(content, pos, size);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                info   = informations à constituer en avance de phase.       *
*                                                                             *
*  Description : Commente les définitions d'un corps de méthode.              *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool annotate_dex_code_item(const GDexFormat *format, GPreloadInfo *info, uleb128_t offset)
{
    bool result;                            /* Bilan à retourner           */
    vmpa2t pos;                             /* Tête de lecture des symboles*/
    code_item_data data;                    /* Valeurs brutes lues         */
    uint16_t i;                             /* Boucle de parcours          */

    result = g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), offset, &pos);

    if (!result)
        goto adci_exit;

    result = parse_field_definitions(PARSING_DEFS(_dex_code_item), G_BIN_FORMAT(format), info, &pos, &data);

    if (!result)
        goto adci_exit;

    /* insns */

    advance_vmpa(&pos, data.insns_size * 2);

    /* padding */

    if (data.insns_size % 2 != 0)
        result = parse_field_definitions(PARSING_DEFS(_dex_code_item_padding),
                                         G_BIN_FORMAT(format), info, &pos, NULL);

    /* tries */

    for (i = 0; i < data.tries_size && result; i++)
        result = annotate_dex_try_item(format, info, &pos);

    if (data.tries_size > 0 && result)
        result = annotate_dex_encoded_catch_handler_list(format, info, &pos);

 adci_exit:

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                info   = informations à constituer en avance de phase.       *
*                pos    = tête de lecture pour les symboles.                  *
*                                                                             *
*  Description : Commente les définitions d'une protection contre exceptions. *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool annotate_dex_try_item(const GDexFormat *format, GPreloadInfo *info, vmpa2t *pos)
{
    bool result;                            /* Bilan à retourner           */

    result = parse_field_definitions(PARSING_DEFS(_dex_try_item), G_BIN_FORMAT(format), info, pos, NULL);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                info   = informations à constituer en avance de phase.       *
*                pos    = tête de lecture physique des symboles.              *
*                                                                             *
*  Description : Commente les définitions des listes de gestion d'exceptions. *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool annotate_dex_encoded_catch_handler_list(const GDexFormat *format, GPreloadInfo *info, vmpa2t *pos)
{
    bool result;                            /* Bilan à retourner           */
    uleb128_t size;                         /* Nombre d'entrées            */
    uleb128_t i;                            /* Boucle de parcours          */

    result = parse_field_definitions(PARSING_DEFS(_dex_encoded_catch_handler_list),
                                     G_BIN_FORMAT(format), info, pos, &size);

    for (i = 0; i < size && result; i++)
        result = annotate_dex_encoded_catch_handler(format, info, pos);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                info   = informations à constituer en avance de phase.       *
*                pos    = tête de lecture physique des symboles.              *
*                                                                             *
*  Description : Commente les définitions d'une prise en compte d'exceptions. *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool annotate_dex_encoded_catch_handler(const GDexFormat *format, GPreloadInfo *info, vmpa2t *pos)
{
    bool result;                            /* Bilan à retourner           */
    leb128_t size;                          /* Nombre de gestionnaires     */
    bool has_catch_all;                     /* Gestion par défaut ?        */
    uleb128_t i;                            /* Boucle de parcours          */

    result = parse_field_definitions(PARSING_DEFS(_dex_encoded_catch_handler),
                                     G_BIN_FORMAT(format), info, pos, &size);

    if (!result)
        goto adech_exit;

    has_catch_all = (size <= 0);

    if (size < 0)
        size *= -1;

    /* handlers */

    for (i = 0; i < size && result; i++)
        result = annotate_dex_encoded_type_addr_pair(format, info, pos);

    /* catch_all_addr */

    if (result && has_catch_all)
        result = parse_field_definitions(PARSING_DEFS(_dex_encoded_catch_handler_all),
                                         G_BIN_FORMAT(format), info, pos, &size);

 adech_exit:

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                info   = informations à constituer en avance de phase.       *
*                pos    = tête de lecture des symboles.                       *
*                                                                             *
*  Description : Commente les définitions des gestions d'exceptions par type. *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool annotate_dex_encoded_type_addr_pair(const GDexFormat *format, GPreloadInfo *info, vmpa2t *pos)
{
    bool result;                            /* Bilan à retourner           */

    result = parse_field_definitions(PARSING_DEFS(_dex_encoded_type_addr_pair),
                                     G_BIN_FORMAT(format), info, pos, NULL);

    return result;

}