Skip to content

Commit

Permalink
fifi
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed May 23, 2024
1 parent e11a1e4 commit 935ad1c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
49 changes: 36 additions & 13 deletions libr/bin/bfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define R_STRING_SCAN_BUFFER_SIZE 4096
#define R_STRING_MAX_UNI_BLOCKS 4

static RBinClass *__getClass(RBinFile *bf, const char *name) {
static RBinClass *get_class(RBinFile *bf, const char *name) {
R_RETURN_VAL_IF_FAIL (bf && bf->bo && bf->bo->classes_ht && name, NULL);
#if R2_USE_NEW_ABI
void *htidxptr = ht_pp_find (bf->bo->classes_ht, name, NULL);
Expand Down Expand Up @@ -1099,6 +1099,19 @@ R_API RBinClass *r_bin_class_new(const char *name, const char *super, ut64 attr)
}

#if R2_USE_NEW_ABI
R_API void r_bin_class_init(RBinClass *c, const char *name, const char *super, ut64 attr) {
R_RETURN_IF_FAIL (c && name);
c->name = r_bin_name_new (name);
if (R_STR_ISNOTEMPTY (super)) {
c->super = r_list_newf (free);
r_list_append (c->super, r_bin_name_new (super));
}
// TODO: use vectors!
c->methods = r_list_newf (r_bin_symbol_free);
c->fields = r_list_newf (r_bin_field_free);
c->attr = attr;
}

R_API void r_bin_class_fini(RBinClass *k) {
if (k) {
free (k->name);
Expand Down Expand Up @@ -1127,7 +1140,7 @@ R_API void r_bin_class_free(RBinClass *k) {

R_API RBinClass *r_bin_file_add_class(RBinFile *bf, const char *name, const char *super, ut64 attr) {
R_RETURN_VAL_IF_FAIL (name && bf && bf->bo, NULL);
RBinClass *c = __getClass (bf, name);
RBinClass *c = get_class (bf, name);
if (c) {
if (R_STR_ISNOTEMPTY (super)) {
r_list_free (c->super);
Expand All @@ -1136,24 +1149,30 @@ R_API RBinClass *r_bin_file_add_class(RBinFile *bf, const char *name, const char
}
return c;
}
c = r_bin_class_new (name, super, attr);
if (c) {
#if R2_USE_NEW_ABI
c->index = RVecRBinClass_length (&bf->bo->classes);
RVecRBinClass_push_back (&bf->bo->classes, c);
// free (c);
const int htidx = c->index + 1;
ht_pp_update (bf->bo->classes_ht, name, (void*)(size_t)htidx);
// c = RVecRBinClass_last (&bf->bo->classes);
// c = RVecRBinClass_at (&bf->bo->classes, 0);
// return c;
RBinClass bc = {0};
r_bin_class_init (&bc, name, super, attr);
bc.index = RVecRBinClass_length (&bf->bo->classes);
RVecRBinClass_push_back (&bf->bo->classes, &bc);
// free (c);
// const int htidx = bc.index + 1;
const int htidx = RVecRBinClass_length (&bf->bo->classes); // bc.index + 1;
ht_pp_update (bf->bo->classes_ht, name, (void*)(size_t)htidx);
// eprintf ("0-> %s (%s)\n", r_bin_name_tostring (c->name), name);
c = RVecRBinClass_last (&bf->bo->classes);
// eprintf ("1-> %s (%s)\n", r_bin_name_tostring (c->name), name);
// free (c);
// c = RVecRBinClass_at (&bf->bo->classes, 0);
// return c;
#else
c = r_bin_class_new (name, super, attr);
if (c) {
// XXX. no need for a list, the ht is iterable too
c->index = r_list_length (bf->bo->classes);
r_list_append (bf->bo->classes, c);
ht_pp_insert (bf->bo->classes_ht, name, c);
#endif
}
#endif
return c;
}

Expand All @@ -1175,8 +1194,11 @@ R_API RBinSymbol *r_bin_file_add_method(RBinFile *bf, const char *klass, const c
sym->lang = lang;
char *name = r_str_newf ("%s::%s", klass, method);
ht_pp_insert (bf->bo->methods_ht, name, sym);
#if R2_USE_NEW_ABI
#else
// RBinSymbol *dsym = r_bin_symbol_clone (sym);
r_list_append (c->methods, sym);
#endif
free (name);
}
}
Expand Down Expand Up @@ -1253,6 +1275,7 @@ R_API RBinFile *r_bin_file_open(RBin *bin, const char *file, RBinFileOptions *op

// TODO Improve this API
R_API void r_bin_file_merge(RBinFile *dst, RBinFile *src) {
R_RETURN_IF_FAIL (dst && src);
// merge imports
// merge dbginfo
sdb_merge (dst->bo->kv, src->bo->kv);
Expand Down
4 changes: 4 additions & 0 deletions libr/bin/bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ R_API void r_bin_string_free(void *_str) {
}
}

// R2_600 - return RBinFile instead of bool. avoid toctou
R_API bool r_bin_open(RBin *bin, const char *file, RBinFileOptions *opt) {
R_RETURN_VAL_IF_FAIL (bin && bin->iob.io && opt, false);

Expand Down Expand Up @@ -270,6 +271,7 @@ R_API bool r_bin_reload(RBin *bin, ut32 bf_id, ut64 baseaddr) {
return res;
}

// R2_600 - return RBinFile instead of bool. avoid toctou
R_API bool r_bin_open_buf(RBin *bin, RBuffer *buf, RBinFileOptions *opt) {
R_RETURN_VAL_IF_FAIL (bin && opt, false);

Expand Down Expand Up @@ -318,10 +320,12 @@ R_API bool r_bin_open_buf(RBin *bin, RBuffer *buf, RBinFileOptions *opt) {
return false;
}
// r_ref (bf);
// return bf instead of bool!
bin->cur = bf;
return res;
}

// R2_600 - return RBinFile instead of bool. avoid toctou
R_API bool r_bin_open_io(RBin *bin, RBinFileOptions *opt) {
R_RETURN_VAL_IF_FAIL (bin && opt && bin->iob.io, false);
R_RETURN_VAL_IF_FAIL (opt->fd >= 0 && (st64)opt->sz >= 0, false);
Expand Down
13 changes: 11 additions & 2 deletions libr/bin/bobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,24 @@ static void r_bin_object_rebuild_classes_ht(RBinObject *bo) {
RBinClass *klass;
RBinSymbol *method;
#if R2_USE_NEW_ABI
int klass_idx = 0;
R_VEC_FOREACH (&bo->classes, klass)
#else
r_list_foreach (bo->classes, it, klass)
#endif
{
if (klass->name) {
ht_pp_insert (bo->classes_ht, klass->name, klass);
const char *klass_name = r_bin_name_tostring (klass->name);
#if R2_USE_NEW_ABI
void *htidxptr = (void*)(size_t)klass_idx;
ht_pp_insert (bo->classes_ht, klass_name, htidxptr);
klass_idx++;
/// TODO
#else
ht_pp_insert (bo->classes_ht, klass_name, klass);
#endif
r_list_foreach (klass->methods, it2, method) {
const char *klass_name = r_bin_name_tostring (klass->name);
// const char *klass_name = r_bin_name_tostring (klass->name);
const char *method_name = r_bin_name_tostring (method->name);
char *name = r_str_newf ("%s::%s", klass_name, method_name);
ht_pp_insert (bo->methods_ht, name, method);
Expand Down
14 changes: 8 additions & 6 deletions libr/bin/format/objc/mach0_classes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1221,25 +1221,22 @@ static char *get_class_name(RBinFile *bf, mach0_ut p) {

///////////////////////////////////////////////////////////////////////////////
static void get_class_ro_t(RBinFile *bf, bool *is_meta_class, RBinClass *klass, objc_cache_opt_info *oi, mach0_ut p) {
struct MACH0_(obj_t) *bin;
struct MACH0_(SClassRoT) cro = {0};
ut32 offset, left, i;
ut64 r, s;
int len;
bool bigendian;
ut8 scro[sizeof (struct MACH0_(SClassRoT))] = {0};

if (!bf || !bf->bo || !bf->bo->bin_obj || !bf->bo->info) {
R_LOG_WARN ("Invalid RBinFile pointer");
return;
}
bigendian = bf->bo->info->big_endian;
bin = (struct MACH0_(obj_t) *)bf->bo->bin_obj;
const bool bigendian = bf->bo->info->big_endian;
struct MACH0_(obj_t) *bin = (struct MACH0_(obj_t) *)bf->bo->bin_obj;
if (!(r = va2pa (bf, p, &offset, &left))) {
// eprintf ("No pointer\n");
return;
}

if (r + left < r || r + sizeof (cro) < r) {
return;
}
Expand Down Expand Up @@ -1323,7 +1320,6 @@ static void get_class_ro_t(RBinFile *bf, bool *is_meta_class, RBinClass *klass,
#else
sdb_set (bin->kv, "objc_class.format", "xxxxx isa super cache vtable data", 0);
#endif

if (cro.baseMethods > 0) {
const char *klass_name = r_bin_name_tostring2 (klass->name, 'd');
if (cro.baseMethods & 1) {
Expand Down Expand Up @@ -1659,7 +1655,10 @@ static void parse_type(RBinFile *bf, RList *list, SwiftType st, HtUP *symbols_ht
}
}
#if R2_USE_NEW_ABI
// eprintf ("PPP %s\n", r_bin_name_tostring (klass->name));
RVecRBinClass_push_back (&bf->bo->classes, klass);
free (klass);
klass = RVecRBinClass_last (&bf->bo->classes);
#else
r_list_append (list, klass);
#endif
Expand Down Expand Up @@ -1852,6 +1851,7 @@ RList *MACH0_(parse_classes)(RBinFile *bf, objc_cache_opt_info *oi) {
}
RBinClass *klass = r_bin_class_new ("", "", R_BIN_ATTR_PUBLIC);
R_FREE (klass->name); // allow NULL name in rbinclass?
// TODO r_bin_name_free (klass->name);
klass->lang = R_BIN_LANG_OBJC;
size = sizeof (mach0_ut);
if (ms.clslist.addr > bf->size || ms.clslist.addr + size > bf->size) {
Expand All @@ -1874,6 +1874,7 @@ RList *MACH0_(parse_classes)(RBinFile *bf, objc_cache_opt_info *oi) {
}
#if R2_USE_NEW_ABI
RVecRBinClass_push_back (&bo->classes, klass);
free (klass);
#else
r_list_append (ret, klass);
#endif
Expand Down Expand Up @@ -1946,6 +1947,7 @@ static RList *MACH0_(parse_categories)(RBinFile *bf, MetaSections *ms, const RSk
// klass->name = name;
}
#if R2_USE_NEW_ABI
// eprintf ("PPP %s\n", r_bin_name_tostring (klass->name));
RVecRBinClass_push_back (&bo->classes, klass);
#else
r_list_append (ret, klass);
Expand Down

0 comments on commit 935ad1c

Please sign in to comment.