2018-05-08 09:05:01 +00:00
|
|
|
/* radare - LGPL - Copyright 2016-2018 - oddcoder, sivaramaaa */
|
2016-08-16 11:38:40 +00:00
|
|
|
/* type matching - type propagation */
|
2016-08-16 01:26:26 +00:00
|
|
|
|
2016-08-13 10:09:14 +00:00
|
|
|
#include <r_anal.h>
|
|
|
|
#include <r_util.h>
|
|
|
|
#include <r_core.h>
|
2016-08-25 09:26:35 +00:00
|
|
|
#define LOOP_MAX 10
|
|
|
|
|
2016-08-23 11:10:15 +00:00
|
|
|
enum {
|
2016-11-14 00:30:14 +00:00
|
|
|
ROMEM = 0,
|
2016-08-23 11:10:15 +00:00
|
|
|
ASM_TRACE,
|
|
|
|
ANAL_TRACE,
|
|
|
|
DBG_TRACE,
|
|
|
|
NONULL,
|
|
|
|
STATES_SIZE
|
|
|
|
};
|
2016-08-13 10:09:14 +00:00
|
|
|
|
2016-11-23 17:36:11 +00:00
|
|
|
static bool r_anal_emul_init(RCore *core, RConfigHold *hc) {
|
2017-09-11 18:40:22 +00:00
|
|
|
r_config_save_num (hc, "esil.romem", "asm.trace", "dbg.trace",
|
|
|
|
"esil.nonull", NULL);
|
2016-08-13 10:09:14 +00:00
|
|
|
r_config_set (core->config, "esil.romem", "true");
|
|
|
|
r_config_set (core->config, "asm.trace", "true");
|
|
|
|
r_config_set (core->config, "dbg.trace", "true");
|
|
|
|
r_config_set (core->config, "esil.nonull", "true");
|
2016-08-22 09:22:22 +00:00
|
|
|
const char *bp = r_reg_get_name (core->anal->reg, R_REG_NAME_BP);
|
|
|
|
const char *sp = r_reg_get_name (core->anal->reg, R_REG_NAME_SP);
|
2016-11-10 12:35:38 +00:00
|
|
|
if ((bp && !r_reg_getv (core->anal->reg, bp)) && (sp && !r_reg_getv (core->anal->reg, sp))) {
|
2018-10-22 21:35:55 +00:00
|
|
|
eprintf ("Stack isn't initialized.\n");
|
2016-08-22 09:22:22 +00:00
|
|
|
eprintf ("Try running aei and aeim commands before aftm for default stack initialization\n");
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-17 23:27:11 +00:00
|
|
|
return (core->anal->esil != NULL);
|
2016-08-13 10:09:14 +00:00
|
|
|
}
|
2016-08-15 15:34:12 +00:00
|
|
|
|
2016-11-23 17:36:11 +00:00
|
|
|
static void r_anal_emul_restore(RCore *core, RConfigHold *hc) {
|
|
|
|
r_config_restore (hc);
|
|
|
|
r_config_hold_free (hc);
|
2016-08-23 11:10:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 10:11:22 +00:00
|
|
|
#define SDB_CONTAINS(i,s) sdb_array_contains (trace, sdb_fmt ("%d.reg.write", i), s, 0)
|
2018-05-08 09:05:01 +00:00
|
|
|
|
|
|
|
static bool type_pos_hit(RAnal *anal, Sdb *trace, bool in_stack, int idx, int size, const char *place) {
|
|
|
|
if (in_stack) {
|
|
|
|
const char *sp_name = r_reg_get_name (anal->reg, R_REG_NAME_SP);
|
|
|
|
ut64 sp = r_reg_getv (anal->reg, sp_name);
|
|
|
|
ut64 write_addr = sdb_num_get (trace, sdb_fmt ("%d.mem.write", idx), 0);
|
|
|
|
return (write_addr == sp + size);
|
|
|
|
} else {
|
2018-06-23 10:11:22 +00:00
|
|
|
return SDB_CONTAINS (idx, place);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 13:22:09 +00:00
|
|
|
static void var_rename(RAnal *anal, RAnalVar *v, const char *name, ut64 addr) {
|
2018-06-23 10:11:22 +00:00
|
|
|
if (!name || !v) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-24 18:09:44 +00:00
|
|
|
if (!*name || !strcmp (name , "...")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool is_default = (!strncmp (v->name, "local_", 6)
|
|
|
|
|| !strncmp (v->name, "arg_", 4))? true: false;
|
2018-06-23 10:11:22 +00:00
|
|
|
if (*name == '*') {
|
|
|
|
name++;
|
2018-05-08 09:05:01 +00:00
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
// longer name tends to be meaningful like "src" instead of "s1"
|
|
|
|
if (!is_default && (strlen (v->name) > strlen (name))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RAnalFunction *fcn = r_anal_get_fcn_in (anal, addr, 0);
|
2018-06-29 12:45:35 +00:00
|
|
|
if (!fcn) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-17 20:02:02 +00:00
|
|
|
r_anal_var_rename (anal, fcn->addr, 1, v->kind, v->name, name, false);
|
2018-05-08 09:05:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 13:22:09 +00:00
|
|
|
static void var_retype(RAnal *anal, RAnalVar *var, const char *vname, char *type, ut64 addr, bool ref, bool pfx) {
|
2018-06-23 10:11:22 +00:00
|
|
|
if (!type || !var) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-11 15:00:37 +00:00
|
|
|
char *trim = r_str_trim (type);
|
|
|
|
if (!*trim) {
|
2018-06-24 18:09:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
bool is_ptr = (vname && *vname == '*')? true: false;
|
|
|
|
if (!strncmp (trim, "int", 3) || (!is_ptr && !strcmp (trim, "void"))) {
|
|
|
|
// default or void type
|
2018-06-23 10:11:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-08 13:26:37 +00:00
|
|
|
const char *tmp = strstr (var->type, "int");
|
|
|
|
bool is_default = tmp? true: false;
|
2018-07-14 17:47:34 +00:00
|
|
|
if (!is_default && strncmp (var->type, "void", 4)) {
|
|
|
|
// return since type is already propgated
|
|
|
|
// except for "void *", since "void *" => "char *" is possible
|
2018-06-24 18:09:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-09-21 15:26:39 +00:00
|
|
|
RStrBuf *sb = r_strbuf_new ("");
|
2018-07-08 13:26:37 +00:00
|
|
|
if (pfx) {
|
|
|
|
if (is_default && strncmp (var->type, "signed", 6)) {
|
2018-09-21 15:26:39 +00:00
|
|
|
r_strbuf_setf (sb, "%s %s", type, tmp);
|
2018-07-08 13:26:37 +00:00
|
|
|
} else {
|
2018-09-23 15:02:41 +00:00
|
|
|
r_strbuf_free (sb);
|
2018-07-08 13:26:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2018-09-21 15:26:39 +00:00
|
|
|
r_strbuf_set (sb, trim);
|
2018-07-08 13:26:37 +00:00
|
|
|
}
|
2018-09-21 15:26:39 +00:00
|
|
|
if (!strncmp (r_strbuf_get (sb), "const ", 6)) {
|
2018-06-23 10:11:22 +00:00
|
|
|
// Droping const from type
|
|
|
|
//TODO: Infering const type
|
2018-09-21 15:26:39 +00:00
|
|
|
r_strbuf_setf (sb, "%s", type + 6);
|
2018-06-23 10:11:22 +00:00
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
if (is_ptr) {
|
2018-06-24 18:09:44 +00:00
|
|
|
//type *ptr => type *
|
2018-09-21 15:26:39 +00:00
|
|
|
r_strbuf_append (sb, " *");
|
2018-06-23 10:11:22 +00:00
|
|
|
}
|
2018-09-21 15:26:39 +00:00
|
|
|
if (ref) {
|
|
|
|
if (r_str_endswith (r_strbuf_get (sb), "*")) { // type * => type **
|
|
|
|
r_strbuf_append (sb, "*");
|
|
|
|
} else { // type => type *
|
|
|
|
r_strbuf_append (sb, " *");
|
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
}
|
2018-09-21 15:26:39 +00:00
|
|
|
r_anal_var_retype (anal, addr, 1, var->delta, var->kind, r_strbuf_get (sb), var->size, var->isarg, var->name);
|
|
|
|
r_strbuf_free (sb);
|
2018-06-23 10:11:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 13:22:09 +00:00
|
|
|
static void get_src_regname(RCore *core, ut64 addr, char *regname, int size) {
|
2018-07-08 13:26:37 +00:00
|
|
|
RAnal *anal = core->anal;
|
|
|
|
RAnalOp *op = r_core_anal_op (core, addr, R_ANAL_OP_MASK_ESIL);
|
2018-08-07 14:12:36 +00:00
|
|
|
if (!op) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-11 15:00:37 +00:00
|
|
|
char *op_esil = strdup (r_strbuf_get (&op->esil));
|
|
|
|
char *tmp = strchr (op_esil, ',');
|
2018-06-23 10:11:22 +00:00
|
|
|
if (tmp) {
|
2018-07-08 13:26:37 +00:00
|
|
|
*tmp = '\0';
|
2018-06-23 10:11:22 +00:00
|
|
|
}
|
2018-07-11 15:00:37 +00:00
|
|
|
memset (regname, 0, size);
|
|
|
|
RRegItem *ri = r_reg_get (anal->reg, op_esil, -1);
|
2018-07-08 13:26:37 +00:00
|
|
|
if (ri) {
|
|
|
|
if ((anal->bits == 64) && (ri->size == 32)) {
|
2018-07-31 17:14:06 +00:00
|
|
|
const char *reg = r_reg_32_to_64 (anal->reg, op_esil);
|
|
|
|
if (reg) {
|
|
|
|
free (op_esil);
|
2018-08-02 13:25:34 +00:00
|
|
|
op_esil = strdup (reg);
|
2018-07-31 17:14:06 +00:00
|
|
|
}
|
2018-07-08 13:26:37 +00:00
|
|
|
}
|
2018-07-11 15:00:37 +00:00
|
|
|
strncpy (regname, op_esil, size - 1);
|
2018-06-23 10:11:22 +00:00
|
|
|
}
|
2018-07-11 15:00:37 +00:00
|
|
|
free (op_esil);
|
2018-07-08 13:26:37 +00:00
|
|
|
r_anal_op_free (op);
|
2018-06-23 10:11:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 13:22:09 +00:00
|
|
|
static ut64 get_addr(Sdb *trace, const char *regname, int idx) {
|
2018-07-11 15:00:37 +00:00
|
|
|
if (!regname || !*regname) {
|
2018-06-23 10:11:22 +00:00
|
|
|
return UT64_MAX;
|
|
|
|
}
|
|
|
|
const char *query = sdb_fmt ("%d.reg.read.%s", idx, regname);
|
|
|
|
return r_num_math (NULL, sdb_const_get (trace, query, 0));
|
|
|
|
}
|
|
|
|
|
2018-08-19 21:05:54 +00:00
|
|
|
static int cond_invert (int cond) {
|
|
|
|
int res = 0;
|
|
|
|
switch (cond) {
|
|
|
|
case R_ANAL_COND_LE:
|
|
|
|
res = R_ANAL_COND_GT;
|
|
|
|
break;
|
|
|
|
case R_ANAL_COND_LT:
|
|
|
|
res = R_ANAL_COND_GE;
|
|
|
|
break;
|
|
|
|
case R_ANAL_COND_GE:
|
|
|
|
res = R_ANAL_COND_LT;
|
|
|
|
break;
|
|
|
|
case R_ANAL_COND_GT:
|
|
|
|
res = R_ANAL_COND_LE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RKEY(a,k,d) sdb_fmt ("var.range.0x%"PFMT64x ".%c.%d", a, k, d)
|
|
|
|
#define ADB a->sdb_fcns
|
|
|
|
|
|
|
|
static void var_add_range (RAnal *a, RAnalVar *var, int cond, ut64 val) {
|
|
|
|
const char *key = RKEY (var->addr, var->kind, var->delta);
|
|
|
|
sdb_array_append_num (ADB, key, cond, 0);
|
|
|
|
sdb_array_append_num (ADB, key, val, 0);
|
|
|
|
}
|
|
|
|
|
2018-08-26 12:16:14 +00:00
|
|
|
R_API RStrBuf *var_get_constraint (RAnal *a, RAnalVar *var) {
|
2018-08-19 21:05:54 +00:00
|
|
|
const char *key = RKEY (var->addr, var->kind, var->delta);
|
|
|
|
int i, n = sdb_array_length (ADB, key);
|
|
|
|
|
|
|
|
if (n < 2) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-10-08 07:55:13 +00:00
|
|
|
|
|
|
|
bool low = false, high = false;
|
|
|
|
RStrBuf *sb = r_strbuf_new ("");
|
|
|
|
|
2018-08-19 21:05:54 +00:00
|
|
|
for (i = 0; i < n; i += 2) {
|
|
|
|
ut64 cond = sdb_array_get_num (ADB, key, i, 0);
|
|
|
|
ut64 val = sdb_array_get_num (ADB, key, i + 1, 0);
|
|
|
|
switch (cond) {
|
|
|
|
case R_ANAL_COND_LE:
|
|
|
|
if (high) {
|
|
|
|
r_strbuf_append (sb, " && ");
|
|
|
|
}
|
|
|
|
r_strbuf_append (sb, sdb_fmt ("<= 0x%"PFMT64x "", val));
|
|
|
|
low = true;
|
|
|
|
break;
|
|
|
|
case R_ANAL_COND_LT:
|
|
|
|
if (high) {
|
|
|
|
r_strbuf_append (sb, " && ");
|
|
|
|
}
|
|
|
|
r_strbuf_append (sb, sdb_fmt ("< 0x%"PFMT64x "", val));
|
|
|
|
low = true;
|
|
|
|
break;
|
|
|
|
case R_ANAL_COND_GE:
|
|
|
|
r_strbuf_append (sb, sdb_fmt (">= 0x%"PFMT64x "", val));
|
|
|
|
high = true;
|
|
|
|
break;
|
|
|
|
case R_ANAL_COND_GT:
|
|
|
|
r_strbuf_append (sb, sdb_fmt ("> 0x%"PFMT64x "", val));
|
|
|
|
high = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (low && high && i != n-2) {
|
|
|
|
r_strbuf_append (sb, " || ");
|
|
|
|
low = false;
|
|
|
|
high = false;
|
|
|
|
}
|
|
|
|
}
|
2018-08-26 12:16:14 +00:00
|
|
|
return sb;
|
2018-08-19 21:05:54 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 15:17:56 +00:00
|
|
|
static RList *parse_format(RCore *core, char *fmt) {
|
2018-08-19 21:05:54 +00:00
|
|
|
RList *ret = r_list_new ();
|
2018-07-22 15:17:56 +00:00
|
|
|
Sdb *s = core->anal->sdb_fmts;
|
2018-08-02 13:25:34 +00:00
|
|
|
const char *spec = r_config_get (core->config, "anal.types.spec");
|
2018-07-22 15:17:56 +00:00
|
|
|
char arr[10] = {0};
|
2018-07-20 13:22:09 +00:00
|
|
|
char *ptr = strchr (fmt, '%');
|
2018-07-22 15:17:56 +00:00
|
|
|
fmt[strlen(fmt) - 1] = '\0';
|
2018-07-20 13:22:09 +00:00
|
|
|
while (ptr) {
|
|
|
|
ptr += 1;
|
2018-07-22 15:17:56 +00:00
|
|
|
// strip [width] specifier
|
|
|
|
while (IS_DIGIT (*ptr)) { ptr++; }
|
2018-07-24 03:04:34 +00:00
|
|
|
r_str_ncpy (arr, ptr, sizeof (arr) - 1);
|
2018-07-22 15:17:56 +00:00
|
|
|
char *tmp = arr;
|
|
|
|
while (tmp && (IS_LOWER (*tmp) || IS_UPPER (*tmp))) { tmp++; }
|
|
|
|
*tmp = '\0';
|
|
|
|
const char *query = sdb_fmt ("spec.%s.%s", spec, arr);
|
|
|
|
char *type = (char *) sdb_const_get (s, query, 0);
|
|
|
|
if (type) {
|
|
|
|
r_list_append (ret, type);
|
2018-07-20 13:22:09 +00:00
|
|
|
}
|
|
|
|
ptr = strchr (ptr, '%');
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:39 +00:00
|
|
|
#define DEFAULT_MAX 3
|
2018-07-12 11:46:37 +00:00
|
|
|
#define REG_SZ 10
|
2018-08-19 21:05:54 +00:00
|
|
|
#define MAX_INSTR 5
|
2018-07-04 18:00:39 +00:00
|
|
|
|
2018-07-14 17:47:34 +00:00
|
|
|
static void type_match(RCore *core, ut64 addr, char *fcn_name, ut64 baddr, const char* cc,
|
2018-07-04 18:00:39 +00:00
|
|
|
int prev_idx, bool userfnc, ut64 caddr) {
|
2016-08-13 10:09:14 +00:00
|
|
|
Sdb *trace = core->anal->esil->db_trace;
|
2018-05-18 08:32:31 +00:00
|
|
|
Sdb *TDB = core->anal->sdb_types;
|
2016-08-13 10:09:14 +00:00
|
|
|
RAnal *anal = core->anal;
|
2018-07-20 13:22:09 +00:00
|
|
|
RList *types = NULL;
|
2018-05-08 09:05:01 +00:00
|
|
|
int idx = sdb_num_get (trace, "idx", 0);
|
2018-08-02 13:25:34 +00:00
|
|
|
bool verbose = r_config_get_i (core->config, "anal.types.verbose");
|
2018-07-20 13:22:09 +00:00
|
|
|
bool stack_rev = false, in_stack = false, format = false;
|
2018-05-08 09:05:01 +00:00
|
|
|
|
2018-06-23 10:11:22 +00:00
|
|
|
if (!fcn_name || !cc) {
|
2016-09-04 16:13:10 +00:00
|
|
|
return;
|
2016-08-13 10:09:14 +00:00
|
|
|
}
|
2018-07-20 13:22:09 +00:00
|
|
|
int i, j, pos = 0, size = 0, max = r_type_func_args_count (TDB, fcn_name);
|
2018-05-08 09:05:01 +00:00
|
|
|
const char *place = r_anal_cc_arg (anal, cc, 1);
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_push (NULL, NULL);
|
2018-05-08 09:05:01 +00:00
|
|
|
|
|
|
|
if (!strcmp (place, "stack_rev")) {
|
|
|
|
stack_rev = true;
|
|
|
|
}
|
|
|
|
if (!strncmp (place, "stack", 5)) {
|
|
|
|
in_stack = true;
|
|
|
|
}
|
2018-08-02 13:25:34 +00:00
|
|
|
if (verbose && !strncmp (fcn_name, "sym.imp.", 8)) {
|
|
|
|
eprintf ("%s missing function definition\n", fcn_name + 8);
|
|
|
|
}
|
2018-07-04 18:00:39 +00:00
|
|
|
if (!max) {
|
|
|
|
if (!in_stack) {
|
|
|
|
max = r_anal_cc_max_arg(anal, cc);
|
|
|
|
} else {
|
|
|
|
max = DEFAULT_MAX;
|
|
|
|
}
|
|
|
|
}
|
2016-08-13 10:09:14 +00:00
|
|
|
for (i = 0; i < max; i++) {
|
2018-05-08 09:05:01 +00:00
|
|
|
int arg_num = stack_rev ? (max - 1 - i) : i;
|
2018-07-20 13:22:09 +00:00
|
|
|
char *type = NULL;
|
|
|
|
const char *name = NULL;
|
|
|
|
if (format) {
|
|
|
|
if (r_list_empty (types)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
type = r_str_new (r_list_get_n (types, pos++));
|
|
|
|
} else {
|
|
|
|
type = r_type_func_args_type (TDB, fcn_name, arg_num);
|
|
|
|
name = r_type_func_args_name (TDB, fcn_name, arg_num);
|
|
|
|
}
|
|
|
|
if (!type && !userfnc) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-08 09:05:01 +00:00
|
|
|
if (!in_stack) {
|
2018-07-20 13:22:09 +00:00
|
|
|
//XXX: param arg_num must be fixed to support floating point register
|
2018-05-08 09:05:01 +00:00
|
|
|
place = r_anal_cc_arg (anal, cc, arg_num + 1);
|
2016-11-20 18:20:14 +00:00
|
|
|
}
|
2018-07-12 11:46:37 +00:00
|
|
|
char regname[REG_SZ] = {0};
|
2018-06-23 10:11:22 +00:00
|
|
|
ut64 xaddr = UT64_MAX;
|
2018-07-08 13:26:37 +00:00
|
|
|
bool memref = false;
|
2018-06-23 10:11:22 +00:00
|
|
|
bool cmt_set = false;
|
|
|
|
bool res = false;
|
|
|
|
// Backtrace instruction from source sink to prev source sink
|
2018-05-08 09:05:01 +00:00
|
|
|
for (j = idx; j >= prev_idx; j--) {
|
2018-06-23 10:11:22 +00:00
|
|
|
ut64 instr_addr = sdb_num_get (trace, sdb_fmt ("%d.addr", j), 0);
|
2018-07-14 17:47:34 +00:00
|
|
|
if (instr_addr < baddr) {
|
|
|
|
break;
|
2018-07-03 16:26:06 +00:00
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
RAnalOp *op = r_core_anal_op (core, instr_addr, R_ANAL_OP_MASK_BASIC);
|
2018-07-14 17:47:34 +00:00
|
|
|
if (!op) {
|
2018-07-03 16:26:06 +00:00
|
|
|
r_anal_op_free (op);
|
2018-06-23 10:11:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
RAnalOp *next_op = r_core_anal_op (core, instr_addr + op->size, R_ANAL_OP_MASK_BASIC);
|
|
|
|
if (!next_op || (j != idx && (next_op->type == R_ANAL_OP_TYPE_CALL
|
|
|
|
|| next_op->type == R_ANAL_OP_TYPE_JMP))) {
|
|
|
|
r_anal_op_free (op);
|
|
|
|
r_anal_op_free (next_op);
|
|
|
|
break;
|
|
|
|
}
|
2018-07-04 18:00:39 +00:00
|
|
|
char *key = NULL;
|
2018-07-09 10:39:47 +00:00
|
|
|
RAnalVar *var = op->var;
|
2018-07-04 18:00:39 +00:00
|
|
|
if (!in_stack) {
|
|
|
|
key = sdb_fmt ("fcn.0x%08"PFMT64x".arg.%s", caddr, place);
|
|
|
|
} else {
|
|
|
|
key = sdb_fmt ("fcn.0x%08"PFMT64x".arg.%d", caddr, size);
|
|
|
|
}
|
2018-07-08 13:26:37 +00:00
|
|
|
const char *query = sdb_fmt ("%d.mem.read", j);
|
2018-07-09 10:39:47 +00:00
|
|
|
if (op->type == R_ANAL_OP_TYPE_MOV && sdb_const_get (trace, query, 0)) {
|
2018-07-08 13:26:37 +00:00
|
|
|
memref = (!memref && var && (var->kind != R_ANAL_VAR_KIND_REG))? false: true;
|
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
// Match type from function param to instr
|
2018-05-08 09:05:01 +00:00
|
|
|
if (type_pos_hit (anal, trace, in_stack, j, size, place)) {
|
2018-07-04 18:00:39 +00:00
|
|
|
if (!cmt_set && type && name) {
|
2018-07-20 10:45:51 +00:00
|
|
|
r_meta_set_string (anal, R_META_TYPE_VARTYPE, instr_addr,
|
2018-06-23 10:11:22 +00:00
|
|
|
sdb_fmt ("%s%s%s", type, r_str_endswith (type, "*") ? "" : " ", name));
|
|
|
|
cmt_set = true;
|
2018-07-20 13:22:09 +00:00
|
|
|
if ((op->ptr && op->ptr != UT64_MAX) && !strcmp (name, "format")) {
|
|
|
|
RFlagItem *f = r_flag_get_i (core->flags, op->ptr);
|
|
|
|
if (f && !strncmp (f->name, "str", 3)) {
|
2018-07-22 15:17:56 +00:00
|
|
|
types = parse_format (core, f->realname);
|
2018-07-20 13:22:09 +00:00
|
|
|
max += r_list_length (types);
|
|
|
|
format = true;
|
|
|
|
}
|
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
}
|
|
|
|
if (var) {
|
2018-07-04 18:00:39 +00:00
|
|
|
if (!userfnc) {
|
2018-07-08 13:26:37 +00:00
|
|
|
var_retype (anal, var, name, type, addr, memref, false);
|
2018-07-04 18:00:39 +00:00
|
|
|
var_rename (anal, var, name, addr);
|
|
|
|
} else {
|
|
|
|
// Set callee argument info
|
|
|
|
sdb_set (anal->sdb_fcns, key, var->type, 0);
|
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
res = true;
|
|
|
|
} else {
|
2018-07-11 15:00:37 +00:00
|
|
|
get_src_regname (core, instr_addr, regname, sizeof (regname));
|
2018-06-23 10:11:22 +00:00
|
|
|
xaddr = get_addr (trace, regname, j);
|
2016-08-13 10:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
// Type propagate by following source reg
|
2018-07-11 15:00:37 +00:00
|
|
|
if (!res && *regname && SDB_CONTAINS (j, regname)) {
|
2018-06-23 10:11:22 +00:00
|
|
|
if (var) {
|
2018-07-04 18:00:39 +00:00
|
|
|
if (!userfnc) {
|
2018-07-08 13:26:37 +00:00
|
|
|
var_retype (anal, var, name, type, addr, memref, false);
|
2018-07-04 18:00:39 +00:00
|
|
|
var_rename (anal, var, name, addr);
|
|
|
|
} else {
|
|
|
|
sdb_set (anal->sdb_fcns, key, var->type, 0);
|
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
res = true;
|
|
|
|
} else {
|
2018-07-09 10:39:47 +00:00
|
|
|
switch (op->type) {
|
|
|
|
case R_ANAL_OP_TYPE_MOV:
|
|
|
|
case R_ANAL_OP_TYPE_PUSH:
|
2018-07-11 15:00:37 +00:00
|
|
|
get_src_regname (core, instr_addr, regname, sizeof (regname));
|
2018-07-09 10:39:47 +00:00
|
|
|
break;
|
|
|
|
case R_ANAL_OP_TYPE_LEA:
|
|
|
|
case R_ANAL_OP_TYPE_LOAD:
|
|
|
|
case R_ANAL_OP_TYPE_STORE:
|
2018-07-11 15:00:37 +00:00
|
|
|
res = true;
|
2018-07-09 10:39:47 +00:00
|
|
|
break;
|
2018-06-23 10:11:22 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
} else if (var && res && xaddr && (xaddr != UT64_MAX)) { // Type progation using value
|
2018-07-12 11:46:37 +00:00
|
|
|
char tmp[REG_SZ] = {0};
|
2018-07-11 15:00:37 +00:00
|
|
|
get_src_regname (core, instr_addr, tmp, sizeof (tmp));
|
|
|
|
ut64 ptr = get_addr (trace, tmp, j);
|
2018-06-23 10:11:22 +00:00
|
|
|
if (ptr == xaddr) {
|
2018-07-08 13:26:37 +00:00
|
|
|
var_retype (anal, var, name, type, addr, memref, false);
|
2018-06-23 10:11:22 +00:00
|
|
|
}
|
2018-05-08 09:05:01 +00:00
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
r_anal_op_free (op);
|
2018-07-14 17:47:34 +00:00
|
|
|
r_anal_op_free (next_op);
|
2018-05-08 09:05:01 +00:00
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
size += anal->bits / 8;
|
|
|
|
free (type);
|
2018-05-08 09:05:01 +00:00
|
|
|
}
|
2018-07-20 13:22:09 +00:00
|
|
|
r_list_free (types);
|
2018-06-23 10:11:22 +00:00
|
|
|
r_cons_break_pop ();
|
2018-05-08 09:05:01 +00:00
|
|
|
}
|
2016-08-16 08:41:16 +00:00
|
|
|
|
2016-11-14 00:17:58 +00:00
|
|
|
R_API void r_core_anal_type_match(RCore *core, RAnalFunction *fcn) {
|
2017-01-18 17:20:47 +00:00
|
|
|
RAnalBlock *bb;
|
|
|
|
RListIter *it;
|
2018-05-08 09:05:01 +00:00
|
|
|
RAnalOp aop = {0};
|
2018-06-23 10:11:22 +00:00
|
|
|
RAnal *anal = core->anal;
|
|
|
|
Sdb *TDB = anal->sdb_types;
|
|
|
|
bool resolved = false;
|
2017-01-18 17:20:47 +00:00
|
|
|
|
|
|
|
if (!core|| !fcn) {
|
2016-08-25 12:55:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-08 09:05:01 +00:00
|
|
|
if (!core->anal->esil) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-19 21:05:54 +00:00
|
|
|
bool chk_constraint = r_config_get_i (core->config, "anal.types.constraint");
|
2018-05-08 09:05:01 +00:00
|
|
|
int ret, bsize = R_MAX (64, core->blocksize);
|
2018-06-23 10:11:22 +00:00
|
|
|
const int mininstrsz = r_anal_archinfo (anal, R_ANAL_ARCHINFO_MIN_OP_SIZE);
|
2018-05-08 09:05:01 +00:00
|
|
|
const int minopcode = R_MAX (1, mininstrsz);
|
2018-06-23 10:11:22 +00:00
|
|
|
int cur_idx , prev_idx = anal->esil->trace_idx;
|
2018-05-08 09:05:01 +00:00
|
|
|
RConfigHold *hc = r_config_hold_new (core->config);
|
2016-11-23 17:36:11 +00:00
|
|
|
if (!hc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!r_anal_emul_init (core, hc) || !fcn) {
|
|
|
|
r_anal_emul_restore (core, hc);
|
2016-08-13 10:09:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-08 09:05:01 +00:00
|
|
|
ut8 *buf = malloc (bsize);
|
|
|
|
if (!buf) {
|
|
|
|
free (buf);
|
2018-07-05 09:48:56 +00:00
|
|
|
r_anal_emul_restore (core, hc);
|
2018-05-08 09:05:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
char *fcn_name = NULL;
|
|
|
|
char *ret_type = NULL;
|
2018-07-08 13:26:37 +00:00
|
|
|
bool str_flag = false;
|
2018-07-11 15:00:37 +00:00
|
|
|
bool prop = false;
|
2018-07-14 17:47:34 +00:00
|
|
|
bool prev_var = false;
|
2018-07-11 15:00:37 +00:00
|
|
|
char prev_type[256] = {0};
|
2018-07-08 13:26:37 +00:00
|
|
|
const char *prev_dest = NULL;
|
2018-06-23 10:11:22 +00:00
|
|
|
const char *ret_reg = NULL;
|
|
|
|
const char *pc = r_reg_get_name (core->dbg->reg, R_REG_NAME_PC);
|
|
|
|
RRegItem *r = r_reg_get (core->dbg->reg, pc, -1);
|
2018-07-08 20:45:39 +00:00
|
|
|
r_cons_break_push (NULL, NULL);
|
2017-08-22 22:21:28 +00:00
|
|
|
r_list_foreach (fcn->bbs, it, bb) {
|
2017-01-18 17:20:47 +00:00
|
|
|
ut64 addr = bb->addr;
|
2018-06-23 10:11:22 +00:00
|
|
|
int i = 0;
|
|
|
|
r_reg_set_value (core->dbg->reg, r, addr);
|
2018-05-08 09:05:01 +00:00
|
|
|
while (1) {
|
|
|
|
if (r_cons_is_breaked ()) {
|
|
|
|
goto out_function;
|
|
|
|
}
|
|
|
|
if (i >= (bsize - 32)) {
|
|
|
|
i = 0;
|
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
ut64 pcval = r_reg_getv (core->anal->reg, pc);
|
|
|
|
if ((addr >= bb->addr + bb->size) || (addr < bb->addr) || pcval != addr) {
|
2018-07-09 06:22:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-05-08 09:05:01 +00:00
|
|
|
if (!i) {
|
2018-05-21 21:06:00 +00:00
|
|
|
r_io_read_at (core->io, addr, buf, bsize);
|
2018-05-08 09:05:01 +00:00
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
ret = r_anal_op (anal, &aop, addr, buf + i, bsize - i, R_ANAL_OP_MASK_BASIC);
|
2018-05-08 09:05:01 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
i += minopcode;
|
|
|
|
addr += minopcode;
|
|
|
|
r_anal_op_fini (&aop);
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
int loop_count = sdb_num_get (anal->esil->db_trace, sdb_fmt ("0x%"PFMT64x".count", addr), 0);
|
2018-07-09 06:22:22 +00:00
|
|
|
if (loop_count > LOOP_MAX || aop.type == R_ANAL_OP_TYPE_RET) {
|
2018-06-23 10:11:22 +00:00
|
|
|
r_anal_op_fini (&aop);
|
2017-01-18 17:20:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
sdb_num_set (anal->esil->db_trace, sdb_fmt ("0x%"PFMT64x".count", addr), loop_count + 1, 0);
|
|
|
|
if (r_anal_op_nonlinear (aop.type)) { // skip the instr
|
|
|
|
r_reg_set_value (core->dbg->reg, r, addr + ret);
|
|
|
|
} else {
|
|
|
|
r_core_esil_step (core, UT64_MAX, NULL, NULL);
|
|
|
|
}
|
2018-07-04 18:00:39 +00:00
|
|
|
bool userfnc = false;
|
2018-06-23 10:11:22 +00:00
|
|
|
Sdb *trace = anal->esil->db_trace;
|
|
|
|
cur_idx = sdb_num_get (trace, "idx", 0);
|
2018-07-14 17:47:34 +00:00
|
|
|
RAnalVar *var = aop.var;
|
|
|
|
RAnalOp *next_op = r_core_anal_op (core, addr + ret, R_ANAL_OP_MASK_BASIC);
|
|
|
|
ut32 type = aop.type & R_ANAL_OP_TYPE_MASK;
|
2018-05-08 09:05:01 +00:00
|
|
|
if (aop.type == R_ANAL_OP_TYPE_CALL) {
|
2018-06-23 10:11:22 +00:00
|
|
|
RAnalFunction *fcn_call = r_anal_get_fcn_in (anal, aop.jump, -1);
|
2018-05-08 09:05:01 +00:00
|
|
|
if (fcn_call) {
|
2018-06-23 10:11:22 +00:00
|
|
|
if (r_type_func_exist (TDB, fcn_call->name)) {
|
|
|
|
fcn_name = strdup (fcn_call->name);
|
2018-07-04 18:00:39 +00:00
|
|
|
} else {
|
|
|
|
fcn_name = r_type_func_guess (TDB, fcn_call->name);
|
|
|
|
}
|
|
|
|
if (!fcn_name) {
|
|
|
|
fcn_name = strdup (fcn_call->name);
|
|
|
|
userfnc = true;
|
2018-06-23 10:11:22 +00:00
|
|
|
}
|
|
|
|
const char* cc = r_anal_cc_func (anal, fcn_name);
|
|
|
|
if (cc && r_anal_cc_exist (anal, cc)) {
|
2018-07-14 17:47:34 +00:00
|
|
|
type_match (core, addr, fcn_name, bb->addr, cc, prev_idx,
|
2018-07-04 18:00:39 +00:00
|
|
|
userfnc, fcn_call->addr);
|
2018-06-23 10:11:22 +00:00
|
|
|
prev_idx = cur_idx;
|
|
|
|
ret_type = (char *) r_type_func_ret (TDB, fcn_name);
|
|
|
|
ret_reg = r_anal_cc_ret (anal, cc);
|
|
|
|
resolved = false;
|
|
|
|
}
|
2018-08-05 09:54:34 +00:00
|
|
|
if (!strcmp (fcn_name, "__stack_chk_fail")) {
|
|
|
|
const char *query = sdb_fmt ("%d.addr", cur_idx - 1);
|
|
|
|
ut64 mov_addr = sdb_num_get (trace, query, 0);
|
|
|
|
RAnalOp *mop = r_core_anal_op (core, mov_addr, R_ANAL_OP_MASK_BASIC);
|
|
|
|
if (mop && mop->var) {
|
|
|
|
ut32 type = mop->type & R_ANAL_OP_TYPE_MASK;
|
|
|
|
if (type == R_ANAL_OP_TYPE_MOV) {
|
|
|
|
var_rename (anal, mop->var, "canary", addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r_anal_op_free (mop);
|
|
|
|
}
|
2018-06-23 10:11:22 +00:00
|
|
|
free (fcn_name);
|
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
} else if (!resolved && ret_type && ret_reg) {
|
2018-07-08 13:26:37 +00:00
|
|
|
// Forward propgation of function return type
|
2018-07-14 17:47:34 +00:00
|
|
|
char src[REG_SZ] = {0};
|
|
|
|
const char *query = sdb_fmt ("%d.reg.write", cur_idx);
|
|
|
|
const char *cur_dest = sdb_const_get (trace, query, 0);
|
|
|
|
get_src_regname (core, aop.addr, src, sizeof (src));
|
|
|
|
if (ret_reg && *src && strstr (ret_reg, src)) {
|
|
|
|
if (var && aop.direction == R_ANAL_OP_DIR_WRITE) {
|
2018-07-08 13:26:37 +00:00
|
|
|
var_retype (anal, var, NULL, ret_type, addr, false, false);
|
|
|
|
resolved = true;
|
2018-07-14 17:47:34 +00:00
|
|
|
} else if (type == R_ANAL_OP_TYPE_MOV) {
|
|
|
|
ret_reg = cur_dest;
|
|
|
|
}
|
|
|
|
} else if (cur_dest) {
|
|
|
|
char *foo = r_str_new (cur_dest);
|
|
|
|
char *tmp = strchr (foo, ',');
|
|
|
|
if (tmp) {
|
|
|
|
*tmp = '\0';
|
2018-07-08 13:26:37 +00:00
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
if (strstr (ret_reg, foo) || (tmp && strstr (ret_reg, tmp + 1))) {
|
2018-07-08 13:26:37 +00:00
|
|
|
resolved = true;
|
2018-09-21 15:26:39 +00:00
|
|
|
} else if (type == R_ANAL_OP_TYPE_MOV &&
|
|
|
|
(next_op && next_op->type == R_ANAL_OP_TYPE_MOV)){
|
|
|
|
// Progate return type passed using pointer
|
|
|
|
// int *ret; *ret = strlen(s);
|
|
|
|
// TODO: memref check , dest and next src match
|
|
|
|
char nsrc[REG_SZ] = {0};
|
|
|
|
get_src_regname (core, next_op->addr, nsrc, sizeof (nsrc));
|
|
|
|
if (ret_reg && *nsrc && strstr (ret_reg, nsrc) && var &&
|
|
|
|
aop.direction == R_ANAL_OP_DIR_READ) {
|
|
|
|
var_retype (anal, var, NULL, ret_type, addr, true, false);
|
|
|
|
}
|
2018-07-08 13:26:37 +00:00
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
free (foo);
|
2018-06-23 10:11:22 +00:00
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
}
|
|
|
|
// Type Propgation using intruction access pattern
|
|
|
|
if (var) {
|
|
|
|
bool sign = false;
|
2018-07-08 13:26:37 +00:00
|
|
|
if ((type == R_ANAL_OP_TYPE_CMP) && next_op) {
|
|
|
|
if (next_op->sign) {
|
|
|
|
sign = true;
|
|
|
|
} else {
|
|
|
|
// cmp [local_ch], rax ; jb
|
|
|
|
var_retype (anal, var, NULL, "unsigned", addr, false, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// cmp [local_ch], rax ; jge
|
|
|
|
if (sign || aop.sign) {
|
|
|
|
var_retype (anal, var, NULL, "signed", addr, false, true);
|
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
// lea rax , str.hello ; mov [local_ch], rax;
|
|
|
|
// mov rdx , [local_4h] ; mov [local_8h], rdx;
|
|
|
|
if (prev_dest && (type == R_ANAL_OP_TYPE_MOV || type == R_ANAL_OP_TYPE_STORE)) {
|
2018-07-12 11:46:37 +00:00
|
|
|
char reg[REG_SZ] = {0};
|
2018-07-11 15:00:37 +00:00
|
|
|
get_src_regname (core, addr, reg, sizeof (reg));
|
2018-07-14 17:47:34 +00:00
|
|
|
bool match = strstr (prev_dest, reg)? true: false;
|
|
|
|
if (str_flag && match) {
|
|
|
|
var_retype (anal, var, NULL, "const char *", addr, false, false);
|
2018-07-11 15:00:37 +00:00
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
if (prop && match && prev_var) {
|
|
|
|
var_retype (anal, var, NULL, prev_type, addr, false, false);
|
2018-07-08 13:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-19 21:05:54 +00:00
|
|
|
if (chk_constraint && var && (type == R_ANAL_OP_TYPE_CMP && aop.ptr != UT64_MAX)
|
|
|
|
&& next_op && next_op->type == R_ANAL_OP_TYPE_CJMP) {
|
|
|
|
bool jmp = false;
|
|
|
|
RAnalOp *jmp_op = {0};
|
|
|
|
ut64 jmp_addr = next_op->jump;
|
2018-11-03 18:59:10 +00:00
|
|
|
RAnalBlock *jmpbb = r_anal_fcn_bbget_in (anal, fcn, jmp_addr);
|
2018-08-19 21:05:54 +00:00
|
|
|
|
|
|
|
// Check exit status of jmp branch
|
|
|
|
for (i = 0; i < MAX_INSTR ; i++) {
|
|
|
|
jmp_op = r_core_anal_op (core, jmp_addr, R_ANAL_OP_MASK_BASIC);
|
|
|
|
if ((jmp_op->type == R_ANAL_OP_TYPE_RET && r_anal_bb_is_in_offset (jmpbb, jmp_addr))
|
|
|
|
|| jmp_op->type == R_ANAL_OP_TYPE_CJMP) {
|
|
|
|
jmp = true;
|
2018-10-31 00:06:51 +00:00
|
|
|
r_anal_op_free (jmp_op);
|
2018-08-19 21:05:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
jmp_addr += jmp_op->size;
|
|
|
|
r_anal_op_free (jmp_op);
|
|
|
|
}
|
|
|
|
int cond = jmp? cond_invert (next_op->cond): next_op->cond;
|
2018-10-31 00:06:51 +00:00
|
|
|
var_add_range (anal, var, cond, aop.val);
|
2018-08-19 21:05:54 +00:00
|
|
|
}
|
2018-07-08 13:26:37 +00:00
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
prev_var = (var && aop.direction == R_ANAL_OP_DIR_READ)? true: false;
|
2018-07-11 15:00:37 +00:00
|
|
|
str_flag = false;
|
|
|
|
prop = false;
|
|
|
|
prev_dest = NULL;
|
2018-07-14 17:47:34 +00:00
|
|
|
switch (type) {
|
|
|
|
case R_ANAL_OP_TYPE_MOV:
|
|
|
|
case R_ANAL_OP_TYPE_LEA:
|
|
|
|
case R_ANAL_OP_TYPE_LOAD:
|
|
|
|
if (aop.ptr && aop.refptr && aop.ptr != UT64_MAX) {
|
|
|
|
if (type == R_ANAL_OP_TYPE_LOAD) {
|
|
|
|
ut8 buf[256] = {0};
|
|
|
|
r_io_read_at (core->io, aop.ptr, buf, sizeof (buf) - 1);
|
|
|
|
ut64 ptr = r_read_ble (buf, core->print->big_endian, aop.refptr * 8);
|
|
|
|
if (ptr && ptr != UT64_MAX) {
|
|
|
|
RFlagItem *f = r_flag_get_i (core->flags, ptr);
|
|
|
|
if (f && !strncmp (f->name, "str", 3)) {
|
|
|
|
str_flag = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (r_flag_exist_at (core->flags, "str", 3, aop.ptr)) {
|
|
|
|
str_flag = true;
|
|
|
|
}
|
2017-08-22 22:21:28 +00:00
|
|
|
}
|
2018-09-21 15:26:39 +00:00
|
|
|
// mov dword [local_4h], str.hello;
|
|
|
|
if (var && str_flag) {
|
|
|
|
var_retype (anal, var, NULL, "const char *", addr, false, false);
|
|
|
|
}
|
2018-07-11 15:00:37 +00:00
|
|
|
const char *query = sdb_fmt ("%d.reg.write", cur_idx);
|
|
|
|
prev_dest = sdb_const_get (trace, query, 0);
|
|
|
|
if (var) {
|
|
|
|
strncpy (prev_type, var->type, sizeof (prev_type) - 1);
|
2018-07-14 17:47:34 +00:00
|
|
|
prop = true;
|
2018-07-11 15:00:37 +00:00
|
|
|
}
|
2016-08-16 08:41:16 +00:00
|
|
|
}
|
2018-05-08 09:05:01 +00:00
|
|
|
i += ret;
|
|
|
|
addr += ret;
|
2018-07-08 13:26:37 +00:00
|
|
|
r_anal_op_free (next_op);
|
2018-05-08 09:05:01 +00:00
|
|
|
r_anal_op_fini (&aop);
|
|
|
|
|
2016-08-13 10:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
const char *place = r_anal_cc_arg (anal, fcn->cc, 1);
|
2018-07-03 16:26:06 +00:00
|
|
|
// Type propgation for register based args
|
2018-07-04 18:00:39 +00:00
|
|
|
RList *list = r_anal_var_list (anal, fcn, R_ANAL_VAR_KIND_REG);
|
|
|
|
RAnalVar *rvar, *bp_var;
|
|
|
|
RListIter *iter , *iter2;
|
2018-07-03 16:26:06 +00:00
|
|
|
r_list_foreach (list, iter, rvar) {
|
2018-07-04 18:00:39 +00:00
|
|
|
RAnalVar *lvar = get_link_var (anal, fcn->addr, rvar);
|
|
|
|
RRegItem *i = r_reg_index_get (anal->reg, rvar->delta);
|
2018-07-23 15:21:23 +00:00
|
|
|
if (!i) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
bool res = true;
|
2018-08-10 18:15:52 +00:00
|
|
|
char *type = NULL;
|
2018-07-08 13:26:37 +00:00
|
|
|
const char *query = sdb_fmt ("fcn.0x%08"PFMT64x".arg.%s", fcn->addr, i->name);
|
2018-08-10 18:15:52 +00:00
|
|
|
const char *qres = sdb_const_get (anal->sdb_fcns, query, NULL);
|
|
|
|
if (qres) {
|
|
|
|
type = strdup (qres);
|
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
if (lvar) {
|
2018-08-10 18:15:52 +00:00
|
|
|
// Propagate local var type = to => register-based var
|
2018-07-08 13:26:37 +00:00
|
|
|
var_retype (anal, rvar, NULL, lvar->type, fcn->addr, false, false);
|
2018-08-10 18:15:52 +00:00
|
|
|
// Propagate local var type <= from = register-based var
|
|
|
|
var_retype (anal, lvar, NULL, rvar->type, fcn->addr, false, false);
|
2018-07-14 17:47:34 +00:00
|
|
|
if (!strstr (lvar->type, "int")) {
|
|
|
|
res = false;
|
|
|
|
}
|
2018-07-04 18:00:39 +00:00
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
if (type && res) {
|
2018-08-10 18:15:52 +00:00
|
|
|
// Propgate type to local var and register based var passed
|
|
|
|
// from caller function
|
2018-07-08 13:26:37 +00:00
|
|
|
var_retype (anal, rvar, NULL, type, fcn->addr, false, false);
|
2018-07-05 12:05:11 +00:00
|
|
|
if (lvar) {
|
2018-07-08 13:26:37 +00:00
|
|
|
var_retype (anal, lvar, NULL, type, fcn->addr, false, false);
|
2018-07-05 12:05:11 +00:00
|
|
|
}
|
2018-07-04 18:00:39 +00:00
|
|
|
}
|
2018-08-10 18:15:52 +00:00
|
|
|
free (type);
|
2018-07-04 18:00:39 +00:00
|
|
|
r_anal_var_free (lvar);
|
|
|
|
}
|
|
|
|
// Type propgation from caller to callee function for stack based arguments
|
2018-07-14 17:47:34 +00:00
|
|
|
if (place && !strncmp (place, "stack", 5)) {
|
|
|
|
RList *list2 = r_anal_var_list (anal, fcn, R_ANAL_VAR_KIND_BPV);
|
|
|
|
r_list_foreach (list2, iter2, bp_var) {
|
|
|
|
if (bp_var->isarg) {
|
|
|
|
const char *query = sdb_fmt ("fcn.0x%08"PFMT64x".arg.%d", fcn->addr, (bp_var->delta - 8));
|
|
|
|
char *type = (char *) sdb_const_get (anal->sdb_fcns, query, NULL);
|
|
|
|
if (type) {
|
|
|
|
var_retype (anal, bp_var, NULL, type, fcn->addr, false, false);
|
|
|
|
}
|
2018-07-04 18:00:39 +00:00
|
|
|
}
|
2018-07-03 16:26:06 +00:00
|
|
|
}
|
2018-07-14 17:47:34 +00:00
|
|
|
r_list_free (list2);
|
2018-07-03 16:26:06 +00:00
|
|
|
}
|
|
|
|
r_list_free (list);
|
2018-05-08 09:05:01 +00:00
|
|
|
out_function:
|
|
|
|
free (buf);
|
|
|
|
r_cons_break_pop();
|
2016-11-23 17:36:11 +00:00
|
|
|
r_anal_emul_restore (core, hc);
|
2018-06-23 10:11:22 +00:00
|
|
|
sdb_reset (anal->esil->db_trace);
|
2016-08-13 10:09:14 +00:00
|
|
|
}
|