From 29f3cf8c660c5ce51dbcdbd0c770a1d9831cf1a8 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Sun, 11 Sep 2016 20:29:09 +0200 Subject: Handled Dex classes used as marker interfaces and Dex virtual methods. --- ChangeLog | 13 ++++++++ plugins/androhelpers/try_n_catch.c | 3 ++ plugins/readdex/class.c | 20 +++++++----- src/format/dex/class.c | 39 +++++++++++++++++++--- src/format/dex/method.c | 66 +++++++++++++++++++++++++++----------- src/format/dex/method.h | 2 +- 6 files changed, 111 insertions(+), 32 deletions(-) diff --git a/ChangeLog b/ChangeLog index a86d9e2..f381224 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +16-09-11 Cyrille Bagard + + * plugins/androhelpers/try_n_catch.c: + * plugins/readdex/class.c: + Update code. + + * src/format/dex/class.c: + Handle Dex classes used as marker interfaces. + + * src/format/dex/method.c: + * src/format/dex/method.h: + Handle Dex virtual methods. + 16-09-10 Cyrille Bagard * plugins/pychrysa/format/dex/dex.c: diff --git a/plugins/androhelpers/try_n_catch.c b/plugins/androhelpers/try_n_catch.c index 83be896..0e2ae43 100644 --- a/plugins/androhelpers/try_n_catch.c +++ b/plugins/androhelpers/try_n_catch.c @@ -347,6 +347,9 @@ static void look_for_exception_handlers(const GLoadedBinary *binary, const GDexF body = g_dex_method_get_dex_body(method); + if (body == NULL) + return; + if (body->tries_size == 0) return; diff --git a/plugins/readdex/class.c b/plugins/readdex/class.c index ccfdf9d..d3ca197 100644 --- a/plugins/readdex/class.c +++ b/plugins/readdex/class.c @@ -311,17 +311,21 @@ static bool annotate_dex_class_data(const GDexFormat *format, const GDexClass *c data = g_dex_class_get_data(class); - for (i = 0; i < data->static_fields_size && result; i++) - result = annotate_dex_encoded_field(format, &pos); + if (data != NULL) + { + for (i = 0; i < data->static_fields_size && result; i++) + result = annotate_dex_encoded_field(format, &pos); + + for (i = 0; i < data->instance_fields_size && result; i++) + result = annotate_dex_encoded_field(format, &pos); - for (i = 0; i < data->instance_fields_size && result; i++) - result = annotate_dex_encoded_field(format, &pos); + for (i = 0; i < data->direct_methods_size && result; i++) + result = annotate_dex_encoded_method(format, &data->direct_methods[i], &pos); - for (i = 0; i < data->direct_methods_size && result; i++) - result = annotate_dex_encoded_method(format, &data->direct_methods[i], &pos); + for (i = 0; i < data->virtual_methods_size && result; i++) + result = annotate_dex_encoded_method(format, &data->virtual_methods[i], &pos); - for (i = 0; i < data->virtual_methods_size && result; i++) - result = annotate_dex_encoded_method(format, &data->virtual_methods[i], &pos); + } /* Nettoyage final */ diff --git a/src/format/dex/class.c b/src/format/dex/class.c index 37fdd04..a88f3f2 100644 --- a/src/format/dex/class.c +++ b/src/format/dex/class.c @@ -39,6 +39,7 @@ struct _GDexClass GObject parent; /* A laisser en premier */ class_def_item definition; /* Définition de la classe */ + bool has_data; /* Indicateur de présence */ class_data_item data; /* Contenu de la classe */ GDexMethod **direct_methods; /* Méthodes propres */ @@ -199,14 +200,29 @@ GDexClass *g_dex_class_new(GDexFormat *format, const class_def_item *def) GBinRoutine *routine; /* Version interne de méthode */ GBinSymbol *symbol; /* Nouveau symbole construit */ + result = g_object_new(G_TYPE_DEX_CLASS, NULL); + + result->definition = *def; + result->has_data = (def->class_data_off != 0); + + /* Interface vide ? */ + if (!result->has_data) + { + result->dmethods_count = 0; + result->direct_methods = NULL; + + result->vmethods_count = 0; + result->virtual_methods = NULL; + + goto gdcn_done; + + } + init_vmpa(&addr, def->class_data_off, VMPA_NO_VIRTUAL); if (!read_dex_class_data_item(format, &addr, &data)) - return NULL; + goto gdcn_bad_item; - result = g_object_new(G_TYPE_DEX_CLASS, NULL); - - result->definition = *def; result->data = data; /** @@ -264,6 +280,7 @@ GDexClass *g_dex_class_new(GDexFormat *format, const class_def_item *def) return result; + gdcn_bad_item: gdcn_bad_method: g_object_unref(G_OBJECT(result)); @@ -306,7 +323,7 @@ const class_def_item *g_dex_class_get_definition(const GDexClass *class) const class_data_item *g_dex_class_get_data(const GDexClass *class) { - return &class->data; + return (class->has_data ? &class->data : NULL); } @@ -410,9 +427,20 @@ GDexMethod *g_dex_class_find_method_by_address(const GDexClass *class, vmpa_t ad { GDexMethod *result; /* Trouvaille à retourner */ size_t i; /* Boucle de parcours */ + phys_t offset; /* Emplacement de méthode */ result = NULL; +#if 0 /* FIXME */ + /* + +bool g_dex_method_get_offset(const GDexMethod *method, phys_t *offset) + + if (!g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), method->offset, &addr)) + return; + + */ + for (i = 0; i < class->dmethods_count && result == NULL; i++) if (addr == (vmpa_t)g_dex_method_get_offset(class->direct_methods[i])) result = class->direct_methods[i]; @@ -420,6 +448,7 @@ GDexMethod *g_dex_class_find_method_by_address(const GDexClass *class, vmpa_t ad for (i = 0; i < class->vmethods_count && result == NULL; i++) if (addr == (vmpa_t)g_dex_method_get_offset(class->virtual_methods[i])) result = class->virtual_methods[i]; +#endif return result; diff --git a/src/format/dex/method.c b/src/format/dex/method.c index f91af0d..d633d15 100644 --- a/src/format/dex/method.c +++ b/src/format/dex/method.c @@ -46,6 +46,7 @@ struct _GDexMethod /* FIXME : méthode interne seulement */ encoded_method info; /* Propriétés de la méthode */ + bool has_body; /* Indication de présence */ code_item body; /* Corps de la méthode */ off_t offset; /* Position du code */ @@ -183,33 +184,50 @@ GDexMethod *g_dex_method_new_defined(GDexFormat *format, const encoded_method *s phys_t ins_offset; /* Position physique du code */ mrange_t range; /* Emplacement du code associé */ - init_vmpa(&addr, seed->code_off, VMPA_NO_VIRTUAL); - - if (!read_dex_code_item(format, &addr, &item)) - return NULL; - *last += seed->method_idx_diff; - ins_offset = seed->code_off + offsetof(code_item, insns); - - if (!g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), ins_offset, &addr)) - return NULL; - result = get_method_from_dex_pool(format, *last); if (result == NULL) return NULL; result->info = *seed; - result->body = item; - result->offset = ins_offset; + result->has_body = (seed->code_off > 0); + + if (result->has_body) + { + init_vmpa(&addr, seed->code_off, VMPA_NO_VIRTUAL); + + if (!read_dex_code_item(format, &addr, &item)) + goto gdmnd_bad_code_item; + + ins_offset = seed->code_off + offsetof(code_item, insns); + + if (!g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), ins_offset, &addr)) + goto gdmnd_bad_translation; + + result->body = item; + + result->offset = ins_offset; + + init_mrange(&range, &addr, item.insns_size * sizeof(uint16_t)); + g_binary_routine_set_range(result->routine, &range); - init_mrange(&range, &addr, item.insns_size * sizeof(uint16_t)); - g_binary_routine_set_range(result->routine, &range); + } return result; + gdmnd_bad_translation: + + reset_dex_code_item(&item); + + gdmnd_bad_code_item: + + g_object_unref(G_OBJECT(result)); + + return NULL; + } @@ -286,7 +304,7 @@ const encoded_method *g_dex_method_get_dex_info(const GDexMethod *method) const code_item *g_dex_method_get_dex_body(const GDexMethod *method) { - return &method->body; + return (method->has_body ? &method->body : NULL); } @@ -340,6 +358,9 @@ void g_dex_method_include_as_portion(const GDexMethod *method, const GDexFormat if (method->info.access_flags & ACC_NATIVE) return; + if (!method->has_body) + return; + if (!g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), method->offset, &addr)) return; @@ -363,18 +384,27 @@ void g_dex_method_include_as_portion(const GDexMethod *method, const GDexFormat /****************************************************************************** * * * Paramètres : method = représentation interne du format DEX à consulter. * +* offset = position physique à renseigner. [OUT] * * * * Description : Indique la position de la méthode au sein du binaire. * * * -* Retour : Localisation dans le contenu binaire. * +* Retour : Validiter de la position dans le contenu binaire. * * * * Remarques : - * * * ******************************************************************************/ -off_t g_dex_method_get_offset(const GDexMethod *method) +bool g_dex_method_get_offset(const GDexMethod *method, phys_t *offset) { - return method->offset; + bool result; /* Indication à retourner */ + + result = method->has_body; + + if (result) + *offset = method->offset; + + return result; + } diff --git a/src/format/dex/method.h b/src/format/dex/method.h index c7e945d..68c6649 100644 --- a/src/format/dex/method.h +++ b/src/format/dex/method.h @@ -85,7 +85,7 @@ GBinRoutine *g_dex_method_get_routine(const GDexMethod *); void g_dex_method_include_as_portion(const GDexMethod *, const GDexFormat *, GPortionLayer *); /* Indique la position de la méthode au sein du binaire. */ -off_t g_dex_method_get_offset(const GDexMethod *); +bool g_dex_method_get_offset(const GDexMethod *method, phys_t *); /* Fournit des indications sur la nature d'une variable donnée. */ DexVariableIndex g_dex_method_get_variable(const GDexMethod *, uint32_t); -- cgit v0.11.2-87-g4458