From fb025564dd57ef95b0d37881ded9f600cb90d8ec Mon Sep 17 00:00:00 2001 From: condret Date: Wed, 21 Jun 2023 18:55:12 +0200 Subject: [PATCH] Use REsilHooks in esil_stats.c ##esil --- libr/esil/esil.c | 4 ++++ libr/esil/esil_stats.c | 42 +++++++++++++++++++++++++++++++++++++++++- libr/include/r_esil.h | 4 ++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/libr/esil/esil.c b/libr/esil/esil.c index 7bb132a9e458e..e0f9f792ae331 100644 --- a/libr/esil/esil.c +++ b/libr/esil/esil.c @@ -114,6 +114,10 @@ R_API REsil *r_esil_new(int stacksize, int iotrap, unsigned int addrsize) { esil->addrmask = genmask (addrsize - 1); esil->trace = r_esil_trace_new (esil); esil->hooks = r_esil_hooks_new (); + esil->stats_mr_handle = UT32_MAX; + esil->stats_mw_handle = UT32_MAX; + esil->stats_rr_handle = UT32_MAX; + esil->stats_rw_handle = UT32_MAX; return esil; } diff --git a/libr/esil/esil_stats.c b/libr/esil/esil_stats.c index cbb7fa9bbe2e5..7e77d3b936f36 100644 --- a/libr/esil/esil_stats.c +++ b/libr/esil/esil_stats.c @@ -1,6 +1,7 @@ -/* radare - LGPL - Copyright 2014-2021 - pancake */ +/* radare - LGPL - Copyright 2014-2023 - pancake, condret */ #include +#include static bool hook_flag_read(REsil *esil, const char *flag, ut64 *num) { sdb_array_add (esil->stats, "flg.read", flag, 0); @@ -17,22 +18,40 @@ static bool hook_mem_read(REsil *esil, ut64 addr, ut8 *buf, int len) { return false; } +static void obs_mem_read(void *user, ut64 addr, ut8 *buf, int len) { + hook_mem_read ((REsil *)user, addr, buf, len); +} + static bool hook_mem_write(REsil *esil, ut64 addr, const ut8 *buf, int len) { sdb_array_add_num (esil->stats, "mem.write", addr, 0); return false; } +static void obs_mem_write(void *user, ut64 addr, ut8 *buf, int len) { + hook_mem_write ((REsil *)user, addr, buf, len); +} + static bool hook_reg_read(REsil *esil, const char *name, ut64 *res, int *size) { const char *key = (*name>='0' && *name<='9')? "num.load": "reg.read"; sdb_array_add (esil->stats, key, name, 0); return false; } +static void obs_reg_read(void *user, const char *name) { + ut64 fake_val; + int fake_size; + hook_reg_read ((REsil *)user, name, &fake_val, &fake_size); +} + static bool hook_reg_write(REsil *esil, const char *name, ut64 *val) { sdb_array_add (esil->stats, "reg.write", name, 0); return false; } +static void obs_reg_write(void *user, const char *name, ut64 val) { + hook_reg_write ((REsil *)user, name, &val); +} + static bool hook_NOP_mem_write(REsil *esil, ut64 addr, const ut8 *buf, int len) { eprintf ("NOP WRITE AT 0x%08"PFMT64x"\n", addr); return true; @@ -58,9 +77,30 @@ R_API void r_esil_stats(REsil *esil, int enable) { esil->cb.hook_mem_read = hook_mem_read; esil->cb.hook_mem_write = hook_mem_write; esil->cb.hook_reg_write = hook_reg_write; + if (esil->stats_mr_handle != UT32_MAX) { + esil->stats_mr_handle = r_esil_add_mem_read_obs (esil, obs_mem_read, esil); + } + if (esil->stats_mw_handle != UT32_MAX) { + esil->stats_mw_handle = r_esil_add_mem_write_obs (esil, obs_mem_write, esil); + } + if (esil->stats_rr_handle != UT32_MAX) { + esil->stats_rr_handle = r_esil_add_reg_read_obs (esil, obs_reg_read, esil); + } + if (esil->stats_rw_handle != UT32_MAX) { + esil->stats_rw_handle = r_esil_add_reg_write_obs (esil, obs_reg_write, esil); + } + esil->cb.hook_flag_read = hook_flag_read; esil->cb.hook_flag_read = hook_flag_read; esil->cb.hook_command = hook_command; } else { + r_esil_del_mem_read_obs (esil, esil->stats_mr_handle); + esil->stats_mr_handle = UT32_MAX; + r_esil_del_mem_write_obs (esil, esil->stats_mw_handle); + esil->stats_mw_handle = UT32_MAX; + r_esil_del_reg_read_obs (esil, esil->stats_rr_handle); + esil->stats_rr_handle = UT32_MAX; + r_esil_del_reg_write_obs (esil, esil->stats_rw_handle); + esil->stats_rw_handle = UT32_MAX; esil->cb.hook_mem_write = NULL; esil->cb.hook_flag_read = NULL; esil->cb.hook_command = NULL; diff --git a/libr/include/r_esil.h b/libr/include/r_esil.h index db7ec23aa6cae..354c0fbec939e 100644 --- a/libr/include/r_esil.h +++ b/libr/include/r_esil.h @@ -238,6 +238,10 @@ typedef struct r_esil_t { RList *active_plugins; /* deep esil parsing fills this */ Sdb *stats; + ut32 stats_mr_handle; + ut32 stats_mw_handle; + ut32 stats_rr_handle; + ut32 stats_rw_handle; REsilTrace *trace; REsilCallbacks cb; REsilHooks *hooks;