Skip to content

Commit

Permalink
* Import the r_regex api in libr/util/regex from OpenBSD source
Browse files Browse the repository at this point in the history
  - Added a r2-like API on top of it
  - Make RSearch and RMagic use this new api, so
* Only load default magicpath files when no file is passed to RMagic
* Initial work on r_listrange optimization in RAnal
  - #define USE_NEW_FCN_STORE
  - Still work-in-progress
* Implemented a RPoolFactory singleton api to accelerate
  allocations of little objects in the future
* Fix sys/mingw32.sh for osx
* Added sys/maemo.sh
  • Loading branch information
radare committed Sep 14, 2011
1 parent 597fc91 commit e8af149
Show file tree
Hide file tree
Showing 31 changed files with 5,283 additions and 161 deletions.
4 changes: 2 additions & 2 deletions autogen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
# -- pancake
acr -p
if [ -n "$1" ]; then
echo "./configure $@"
./configure $@
echo "./configure $@"
./configure $@
fi
13 changes: 6 additions & 7 deletions libr/anal/anal.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ R_API RAnal *r_anal_new() {
anal->reg = r_reg_new ();
anal->lineswidth = 0;
anal->fcns = r_anal_fcn_list_new ();
anal->fcnstore = r_listrange_new ();
anal->refs = r_anal_ref_list_new ();
anal->vartypes = r_anal_var_type_list_new ();
r_anal_set_bits (anal, 32);
Expand All @@ -50,11 +51,10 @@ R_API RAnal *r_anal_new() {

R_API RAnal *r_anal_free(RAnal *anal) {
if (anal) {
/* TODO: Free a->anals here */
if (anal->fcns)
r_list_free (anal->fcns);
if (anal->vartypes)
r_list_free (anal->vartypes);
/* TODO: Free anals here */
r_listrange_free (anal->fcnstore);
r_list_free (anal->fcns);
r_list_free (anal->vartypes);
}
free (anal);
return NULL;
Expand Down Expand Up @@ -95,8 +95,7 @@ R_API int r_anal_use(RAnal *anal, const char *name) {
}

R_API int r_anal_set_reg_profile(RAnal *anal) {
if (anal)
if (anal->cur && anal->cur->set_reg_profile)
if (anal && anal->cur && anal->cur->set_reg_profile)
return anal->cur->set_reg_profile (anal);
return R_FALSE;
}
Expand Down
114 changes: 65 additions & 49 deletions libr/anal/fcn.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
#include <r_util.h>
#include <r_list.h>

#define VERBOSE if(0)
/* work in progress */
#define USE_NEW_FCN_STORE 0

R_API RAnalFcn *r_anal_fcn_new() {
RAnalFcn *fcn = R_NEW (RAnalFcn);
if (fcn) {
memset (fcn, 0, sizeof (RAnalFcn));
fcn->addr = -1;
fcn->stack = 0;
fcn->vars = r_anal_var_list_new ();
fcn->refs = r_anal_ref_list_new ();
fcn->xrefs = r_anal_ref_list_new ();
fcn->bbs = r_anal_bb_list_new ();
fcn->fingerprint = NULL;
fcn->diff = r_anal_diff_new ();
}
if (!fcn) return NULL;
memset (fcn, 0, sizeof (RAnalFcn));
fcn->addr = -1;
fcn->stack = 0;
fcn->vars = r_anal_var_list_new ();
fcn->refs = r_anal_ref_list_new ();
fcn->xrefs = r_anal_ref_list_new ();
fcn->bbs = r_anal_bb_list_new ();
fcn->fingerprint = NULL;
fcn->diff = r_anal_diff_new ();
return fcn;
}

R_API RList *r_anal_fcn_list_new() {
RList *list = r_list_new ();
if (!list) return NULL;
list->free = &r_anal_fcn_free;
return list;
}
Expand Down Expand Up @@ -55,7 +56,7 @@ R_API int r_anal_fcn(RAnal *anal, RAnalFcn *fcn, ut64 addr, ut8 *buf, ut64 len,
while (idx < len) {
if ((oplen = r_anal_op (anal, &op, addr+idx, buf+idx, len-idx)) == 0) {
if (idx == 0) {
VERBOSE eprintf ("Unknown opcode at 0x%08"PFMT64x"\n", addr+idx);
// eprintf ("Unknown opcode at 0x%08"PFMT64x"\n", addr+idx);
return R_ANAL_RET_END;
} else break;
}
Expand Down Expand Up @@ -116,14 +117,8 @@ R_API int r_anal_fcn(RAnal *anal, RAnalFcn *fcn, ut64 addr, ut8 *buf, ut64 len,
}

R_API int r_anal_fcn_add(RAnal *anal, ut64 addr, ut64 size, const char *name, int type, RAnalDiff *diff) {
RAnalFcn *fcn = NULL, *fcni;
RListIter *iter;
int append = 0;
r_list_foreach (anal->fcns, iter, fcni)
if (addr == fcni->addr) {
fcn = fcni;
break;
}
RAnalFcn *fcn = r_anal_fcn_find (anal, addr, R_ANAL_FCN_TYPE_ROOT);
if (fcn == NULL) {
if (!(fcn = r_anal_fcn_new ()))
return R_FALSE;
Expand All @@ -141,40 +136,64 @@ R_API int r_anal_fcn_add(RAnal *anal, ut64 addr, ut64 size, const char *name, in
if (diff->name)
fcn->diff->name = strdup (diff->name);
}
#if USE_NEW_FCN_STORE
if (append) r_listrange_add (anal->fcnstore, fcn);
#else
if (append) r_list_append (anal->fcns, fcn);
#endif
return R_TRUE;
}

R_API int r_anal_fcn_del(RAnal *anal, ut64 addr) {
RAnalFcn *fcni;
RListIter it, *iter;
if (addr == 0) {
r_list_free (anal->fcns);
if (!(anal->fcns = r_anal_fcn_list_new ()))
return R_FALSE;
} else
r_list_foreach (anal->fcns, iter, fcni) {
if (addr >= fcni->addr && addr < fcni->addr+fcni->size) {
it.n = iter->n;
r_list_delete (anal->fcns, iter);
iter = &it;
} else {
#if USE_NEW_FCN_STORE
// XXX: must only get the function if starting at 0?
RAnalFcn *f = r_listrange_find_in_range (anal->fcnstore, addr);
if (f) r_listrange_del (anal->fcnstore, f);
#else
RAnalFcn *fcni;
RListIter it, *iter;
r_list_foreach (anal->fcns, iter, fcni) {
if (addr >= fcni->addr && addr < fcni->addr+fcni->size) {
it.n = iter->n;
r_list_delete (anal->fcns, iter);
iter = &it;
}
}
#endif
}
return R_TRUE;
}

R_API RAnalFcn *r_anal_fcn_find(RAnal *anal, ut64 addr, int type) {
int root = type & R_ANAL_FCN_TYPE_ROOT;
#if USE_NEW_FCN_STORE
// TODO: type is ignored here? wtf.. we need more work on fcnstore
if (root) r_listrange_find_root (anal->fcnstore, addr);
return r_listrange_find_in_range (anal->fcnstore, addr);
#else
RAnalFcn *fcn, *ret = NULL;
RListIter *iter;
r_list_foreach (anal->fcns, iter, fcn) {
if (type == R_ANAL_FCN_TYPE_NULL || (fcn->type & type))
if (addr == fcn->addr ||
(ret == NULL && (addr > fcn->addr && addr < fcn->addr+fcn->size)))
ret = fcn;
if (type == R_ANAL_FCN_TYPE_NULL || (fcn->type & type)) {
if (root) {
if (addr == fcn->addr)
ret = fcn;
} else {
if (addr == fcn->addr || (ret == NULL && (addr > fcn->addr && addr < fcn->addr+fcn->size)))
ret = fcn;
}
}
}
return ret;
#endif
}

/* rename RAnalFcnBB.add() */
R_API int r_anal_fcn_add_bb(RAnalFcn *fcn, ut64 addr, ut64 size, ut64 jump, ut64 fail, int type, RAnalDiff *diff) {
RAnalBlock *bb = NULL, *bbi;
RListIter *iter;
Expand All @@ -185,7 +204,8 @@ R_API int r_anal_fcn_add_bb(RAnalFcn *fcn, ut64 addr, ut64 size, ut64 jump, ut64
bb = bbi;
mid = 0;
break;
} else if (addr > bbi->addr && addr < bbi->addr+bbi->size)
} else
if (addr > bbi->addr && addr < bbi->addr+bbi->size)
mid = 1;
}
if (mid)
Expand All @@ -211,6 +231,7 @@ R_API int r_anal_fcn_add_bb(RAnalFcn *fcn, ut64 addr, ut64 size, ut64 jump, ut64
return R_TRUE;
}

// TODO: rename fcn_bb_split()
R_API int r_anal_fcn_split_bb(RAnalFcn *fcn, RAnalBlock *bb, ut64 addr) {
RAnalBlock *bbi;
RAnalOp *opi;
Expand Down Expand Up @@ -253,6 +274,7 @@ R_API int r_anal_fcn_split_bb(RAnalFcn *fcn, RAnalBlock *bb, ut64 addr) {
return R_ANAL_RET_NEW;
}

// TODO: rename fcn_bb_overlap()
R_API int r_anal_fcn_overlap_bb(RAnalFcn *fcn, RAnalBlock *bb) {
RAnalBlock *bbi;
RListIter nit; // hack to make r_list_unlink not fail that hard
Expand Down Expand Up @@ -290,9 +312,7 @@ R_API int r_anal_fcn_cc(RAnalFcn *fcn) {
int ret = 0, retbb;

r_list_foreach (fcn->bbs, iter, bbi) {
if ((bbi->type & R_ANAL_BB_TYPE_LAST))
retbb = 1;
else retbb = 0;
retbb = ((bbi->type & R_ANAL_BB_TYPE_LAST))? 1: 0;
ret += bbi->conditional + retbb;
}
return ret;
Expand Down Expand Up @@ -324,37 +344,32 @@ R_API char *r_anal_fcn_to_string(RAnal *a, RAnalFcn* fs) {
if (!(arg = r_anal_fcn_get_var (fs, i,
R_ANAL_VAR_TYPE_ARG|R_ANAL_VAR_TYPE_ARGREG)))
break;
if (arg->array>1) {
if (i) sign = r_str_concatf (sign, ", %s %s:%02x[%d]",
arg->vartype, arg->name, arg->delta, arg->array);
else sign = r_str_concatf (sign, "%s %s:%02x[%d]",
if (arg->array>1)
sign = r_str_concatf (sign, i?", %s %s:%02x[%d]":"%s %s:%02x[%d]",
arg->vartype, arg->name, arg->delta, arg->array);
} else {
if (i) sign = r_str_concatf (sign, ", %s %s:%02x",
else sign = r_str_concatf (sign, i?", %s %s:%02x":"%s %s:%02x",
arg->vartype, arg->name, arg->delta);
else sign = r_str_concatf (sign, "%s %s:%02x",
arg->vartype, arg->name, arg->delta);
}
}
return (sign = r_str_concatf (sign, ");"));
}

// TODO: This function is not fully implemented
R_API int r_anal_fcn_from_string(RAnal *a, RAnalFcn *f, const char *_str) {
/* set function signature from string */
R_API int r_anal_fcn_from_string(RAnal *a, RAnalFcn *f, const char *sig) {
char *p, *q, *r, *str;
RAnalVar *var;
int i, arg;

if (!a || !f) {
if (!a || !f || !sig) {
eprintf ("r_anal_fcn_from_string: No function received\n");
return R_FALSE;
}
str = strdup (_str);
str = strdup (sig);
/* TODO : implement parser */
//r_list_destroy (fs->vars);
//set: fs->vars = r_list_new ();
//set: fs->name
eprintf ("ORIG=(%s)\n", _str);
eprintf ("ORIG=(%s)\n", sig);
p = strchr (str, '(');
if (!p) goto parsefail;
*p = 0;
Expand Down Expand Up @@ -397,6 +412,7 @@ R_API int r_anal_fcn_from_string(RAnal *a, RAnalFcn *f, const char *_str) {
return R_TRUE;

parsefail:
free (str);
eprintf ("Function string parse fail\n");
return R_FALSE;
}
Loading

0 comments on commit e8af149

Please sign in to comment.