Skip to content

Commit

Permalink
Refactor and extend few RNum $O->$$c|$$$c + error handling ##shell
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae authored Oct 30, 2024
1 parent 9f65b34 commit bf52a65
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 31 deletions.
3 changes: 2 additions & 1 deletion libr/core/cmd_anal.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ static RCoreHelpMessage help_msg_ab = {
"abj", " [addr]", "display basic block information in JSON",
"abl", "[?] [.-cqj]", "list all basic blocks",
"abo", "", "list opcode offsets of current basic block",
"abp", "[?] [addr] [num]", "follow basic blocks paths from current offset to addr",
"abp", "[?] [addr]", "follow basic blocks paths from $$ to `addr`",
"abt", "[tag] ([color])", "no args = show current trace tag, otherwise set the color",
"abx", " [hexpair-bytes]", "analyze N bytes",
NULL
Expand Down Expand Up @@ -13125,6 +13125,7 @@ static void cmd_anal_abp(RCore *core, const char *input) {
r_core_cmd_help (core, help_msg_abp);
return;
}
*p = 0;
ut64 addr = r_num_math (core->num, p + 1);
RList *paths = r_core_anal_graph_to (core, addr, n);
if (paths) {
Expand Down
16 changes: 11 additions & 5 deletions libr/core/cmd_help.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,15 @@ static RCoreHelpMessage help_msg_question_v = {
"Usage: ?v [$.]", "", "",
"flag", "", "offset of flag",
"$", "{ev}", "get value of eval config variable",
"$", "[addr:size]", "get value of eval config variable",
"$$", "", "here (current virtual seek)",
"$$c", "", "cursor position relative to current offset (previously $O)",
"$$$", "", "current non-temporary virtual seek",
"$$$c", "", "cursor + current non-temporary virtual seek",
"$?", "", "last comparison value",
"$alias", "=value", "alias commands (simple macros)",
"$b", "", "block size (see b command and the @! operator)",
"$B", "", "base address (aligned lowest map address)",
"$b", "", "block size",
"$c", "", "get terminal width in character columns",
"$Cn", "", "get nth call of function",
"$D", "", "current debug map base address ?v $D @ rsp",
Expand Down Expand Up @@ -367,7 +370,6 @@ static RCoreHelpMessage help_msg_question_v = {
"$M", "", "map address (lowest map address)",
"$m", "", "opcode memory reference (e.g. mov eax,[0x10] => 0x10)",
"$MM", "", "map size (lowest map address)",
"$O", "", "cursor here (current offset pointed by the cursor)",
"$o", "", "here (current disk io offset)",
"$p", "", "getpid()",
"$P", "", "pid of children (only in debug)",
Expand Down Expand Up @@ -1024,7 +1026,11 @@ static int cmd_help(void *data, const char *input) {
n = r_num_math (core->num, "$?");
}
if (core->num->nc.errors > 0) {
R_LOG_ERROR (core->num->nc.calc_err);
if (core->num->nc.calc_err) {
R_LOG_ERROR ("%s", core->num->nc.calc_err);
} else {
R_LOG_ERROR ("RNum.error");
}
}
if (core->num->dbz) {
R_LOG_ERROR ("Division by Zero");
Expand Down Expand Up @@ -1153,9 +1159,9 @@ static int cmd_help(void *data, const char *input) {
} else {
int i = 0;
const char *vars[] = {
"$$", "$$$", "$?", "$B", "$b", "$c", "$Cn", "$D", "$DB", "$DD", "$Dn",
"$$", "$$c", "$$$", "$$$c", "$?", "$B", "$b", "$c", "$Cn", "$D", "$DB", "$DD", "$Dn",
"$e", "$f", "$F", "$Fb", "$FB", "$Fe", "$FE", "$Ff", "$Fi", "$FI", "$Fj",
"$fl", "$FS", "$Fs", "$FSS", "$i", "$j", "$Ja", "$l", "$M", "$m", "$MM", "$O",
"$fl", "$FS", "$Fs", "$FSS", "$i", "$j", "$Ja", "$l", "$M", "$m", "$MM",
"$o", "$p", "$P", "$r", "$s", "$S", "$SS", "$v", "$w", "$Xn", NULL
};
const bool wideOffsets = r_config_get_i (core->config, "scr.wideoff");
Expand Down
56 changes: 37 additions & 19 deletions libr/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ static ut64 numvar_instruction_backward(RCore *core, const char *input) {
}

static ut64 numvar_instruction(RCore *core, const char *input) {
RAnalOp op;
ut64 addr = core->offset;
// N forward instructions
ut8 data[32];
Expand All @@ -528,7 +529,7 @@ static ut64 numvar_instruction(RCore *core, const char *input) {
}
for (i = 0; i < n; i++) {
r_io_read_at (core->io, val, data, sizeof (data));
RAnalOp op = {0};
r_anal_op_init (&op);
int ret = r_anal_op (core->anal, &op, val, data,
sizeof (data), R_ARCH_OP_MASK_BASIC);
if (ret < 1) {
Expand All @@ -541,14 +542,23 @@ static ut64 numvar_instruction(RCore *core, const char *input) {

}

static ut64 invalid_numvar(RCore *core, const char *str) {
R_LOG_ERROR ("Invalid variable '%s'", str);
core->num->nc.errors ++;
core->num->nc.calc_err = NULL;
// core->num->nc.calc_err = "Invalid $numvar";
return 0;
}

static ut64 num_callback(RNum *userptr, const char *str, int *ok) {
RCore *core = (RCore *)userptr; // XXX ?
RAnalFunction *fcn;
char *ptr, *bptr, *out = NULL;
RFlagItem *flag;
RBinSection *s;
RAnalOp op;
ut64 ret = 0;

RAnalOp op;
r_anal_op_init (&op);

if (ok) {
Expand Down Expand Up @@ -740,12 +750,10 @@ static ut64 num_callback(RNum *userptr, const char *str, int *ok) {
}
free (bptr);
return 0; // UT64_MAX;
} else {
int rows;
(void)r_cons_get_size (&rows);
return rows;
}
break;
int rows;
(void)r_cons_get_size (&rows);
return rows;
case 'e': // $e
if (str[2] == '{') { // $e{flag} flag off + size
char *flagName = strdup (str + 3);
Expand Down Expand Up @@ -804,7 +812,7 @@ static ut64 num_callback(RNum *userptr, const char *str, int *ok) {
return op.val;
case 'l': // $l opcode length
return op.size;
case 'b': // $b
case 'b': // "$b" block size
return core->blocksize;
case 's': // $s file size
if (str[2] == '{') { // $s{flag} flag size
Expand Down Expand Up @@ -853,16 +861,27 @@ static ut64 num_callback(RNum *userptr, const char *str, int *ok) {
case '?': // $?
return core->num->value; // rc;
case '$': // $$ offset
return str[2] == '$' ? core->prompt_offset : core->offset;
case 'o': { // $o
RBinSection *s = r_bin_get_section_at (r_bin_cur_object (core->bin), core->offset, true);
return s ? core->offset - s->vaddr + s->paddr : core->offset;
}
case 'O': // $O
if (core->print->cur_enabled) {
return core->offset + core->print->cur;
if (!strcmp (str, "$$")) {
return core->offset;
} else if (!strcmp (str, "$$c")) {
if (core->print->cur_enabled) {
return core->offset + core->print->cur;
}
return core->offset;
} else if (!strcmp (str, "$$$")) {
return core->prompt_offset;
} else if (!strcmp (str, "$$$c")) {
if (core->print->cur_enabled) {
return core->prompt_offset + core->print->cur;
}
return core->prompt_offset;
}
return invalid_numvar (core, str);
case 'o': // $o
{
RBinSection *s = r_bin_get_section_at (r_bin_cur_object (core->bin), core->offset, true);
return s ? core->offset - s->vaddr + s->paddr : core->offset;
}
return core->offset;
case 'C': // $C nth call
return getref (core, atoi (str + 2), 'r', R_ANAL_REF_TYPE_CALL);
case 'J': // $J nth jump
Expand Down Expand Up @@ -890,8 +909,7 @@ static ut64 num_callback(RNum *userptr, const char *str, int *ok) {
}
return 0;
default:
R_LOG_ERROR ("Invalid variable '%s'", str);
return 0;
return invalid_numvar (core, str);
}
break;
default:
Expand Down
19 changes: 13 additions & 6 deletions test/db/anal/emu
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ RUN

NAME=emu ret0
FILE=bins/mach0/ret0ret1restr
ARGS=-2
ARGS=-1
CMDS=<<EOF
aaa
aaa 2> /dev/null
aeim
?e ret0
s sym._ret0
Expand All @@ -39,6 +39,7 @@ aeb
psz@R0
EOF
EXPECT=<<EOF
WARN: Relocs has not been applied. Please use `-e bin.relocs.apply=true` or `-e bin.cache=true` next time
ret0
0x00000000
---
Expand All @@ -52,7 +53,7 @@ RUN

NAME=emu paths
FILE=bins/mach0/ret0ret1restr
ARGS=-2
ARGS=-1
CMDS=<<EOF
e asm.var.summary=0
aeim
Expand All @@ -61,7 +62,7 @@ s sym._retbool
af
agf
?e path
abp $$ 0x100007f1c
abp 0x100007f1c
?e fromblocks
f final.block=0x100007f1c
abf final.block
Expand All @@ -74,6 +75,7 @@ afbe $$ 0x100007f10;aeb 0x100007f10;aeb final.block
dr?R0
EOF
EXPECT=<<EOF
WARN: Relocs has not been applied. Please use `-e bin.relocs.apply=true` or `-e bin.cache=true` next time
ret paths
.-------------------------------------.
| 0x100007ed8 |
Expand Down Expand Up @@ -121,6 +123,8 @@ ret paths
`------------------------------------------------'
path
0x100007ed8
0x100007f10
0x100007f1c
fromblocks
0x100007f00
0x100007f10
Expand All @@ -133,14 +137,14 @@ RUN

NAME=emu paths with a macro
FILE=bins/mach0/ret0ret1restr
ARGS=-2
ARGS=-1
CMDS=<<EOF
aeim
?e ret paths
s sym._retbool
af
?e path
abp $$ 0x100007f1c
abp 0x100007f1c
?e fromblocks
f final.block=0x100007f1c
abf final.block
Expand All @@ -153,9 +157,12 @@ dr?R0
dr?R0
EOF
EXPECT=<<EOF
WARN: Relocs has not been applied. Please use `-e bin.relocs.apply=true` or `-e bin.cache=true` next time
ret paths
path
0x100007ed8
0x100007f10
0x100007f1c
fromblocks
0x100007f10
0x100007f00
Expand Down
49 changes: 49 additions & 0 deletions test/db/cmd/numvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
NAME=num vars blocksize
FILE=malloc://0x4000
CMDS=<<EOF
b
?vi $b
b 32
?vi $b
?e must be zero
?vi $b-`b`
EOF
EXPECT=<<EOF
0x100
256
32
must be zero
0
EOF
RUN

NAME=num vars here
FILE=malloc://0x4000
CMDS=<<EOF
?vi $$
s 32
?vi $$
?vi $$ @ 64
?vi $$$ @ 64
EOF
EXPECT=<<EOF
0
32
64
32
EOF
RUN

NAME=num vars invalid
FILE=malloc://0x4000
ARGS=-1
CMDS=<<EOF
?vi $$rrr
EOF
EXPECT=<<EOF
ERROR: Invalid variable '$$rrr'
ERROR: RNum.error
0
EOF
RUN

0 comments on commit bf52a65

Please sign in to comment.