Skip to content

Commit

Permalink
Remove the dependency of core->block in pi/pI (#4023)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeiweiHu authored Dec 20, 2023
1 parent ba4be10 commit 4f7e0e1
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 37 deletions.
12 changes: 10 additions & 2 deletions librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -2826,7 +2826,11 @@ RZ_IPI RzCmdStatus rz_assembly_of_hex_alias_handler(RzCore *core, int argc, cons
}

RZ_IPI RzCmdStatus rz_print_instructions_handler(RzCore *core, int argc, const char **argv) {
ut64 len = argc > 1 ? rz_num_math(core->num, argv[1]) : core->blocksize;
if (argc <= 1) {
RZ_LOG_ERROR("Invalid arguments\n");
return RZ_CMD_STATUS_ERROR;
}
ut64 len = rz_num_math(core->num, argv[1]);
rz_core_print_disasm_instructions(core, len, 0);
return RZ_CMD_STATUS_OK;
}
Expand Down Expand Up @@ -5067,7 +5071,11 @@ RZ_IPI RzCmdStatus rz_print_key_mosaic_handler(RzCore *core, int argc, const cha
}

RZ_IPI RzCmdStatus rz_print_instr_handler(RzCore *core, int argc, const char **argv) {
ut64 N = argc > 1 ? rz_num_math(core->num, argv[1]) : core->blocksize;
if (argc <= 1) {
RZ_LOG_ERROR("Invalid arguments\n");
return RZ_CMD_STATUS_ERROR;
}
ut64 N = rz_num_math(core->num, argv[1]);
if (N == 0) {
return RZ_CMD_STATUS_ERROR;
}
Expand Down
4 changes: 2 additions & 2 deletions librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -13432,7 +13432,7 @@ static const RzCmdDescArg print_instr_args[] = {
{ 0 },
};
static const RzCmdDescHelp print_instr_help = {
.summary = "Print <N> instructions/bytes",
.summary = "Disassemble and print <N> instructions",
.args = print_instr_args,
};

Expand Down Expand Up @@ -13550,7 +13550,7 @@ static const RzCmdDescArg print_instructions_args[] = {
{ 0 },
};
static const RzCmdDescHelp print_instructions_help = {
.summary = "Print <N> instructions/bytes",
.summary = "Disassemble and print <N> bytes",
.args = print_instructions_args,
};

Expand Down
4 changes: 2 additions & 2 deletions librz/core/cmd_descs/cmd_print.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ commands:
summary: Print instructions
subcommands:
- name: pi
summary: Print <N> instructions/bytes
summary: Disassemble and print <N> instructions
cname: print_instr
args:
- name: N
Expand Down Expand Up @@ -626,7 +626,7 @@ commands:
summary: Print instructions
subcommands:
- name: pI
summary: Print <N> instructions/bytes
summary: Disassemble and print <N> bytes
cname: print_instructions
args:
- name: N
Expand Down
2 changes: 1 addition & 1 deletion librz/core/core_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ RZ_IPI void rz_core_flag_range_print(RzFlag *f, RzCmdStateOutput *state, ut64 ra
/* cdisasm.c */
RZ_IPI bool rz_disasm_check_end(int nb_opcodes, int i_opcodes, int nb_bytes, int i_bytes);
RZ_IPI void rz_core_asm_bb_middle(RZ_NONNULL RzCore *core, ut64 at, RZ_INOUT RZ_NONNULL int *oplen, RZ_NONNULL int *ret);
RZ_IPI bool rz_core_handle_backwards_disasm(RZ_NONNULL RzCore *core,
RZ_DEPRECATE RZ_IPI bool rz_core_handle_backwards_disasm(RZ_NONNULL RzCore *core,
RZ_NONNULL RZ_INOUT int *pn_opcodes, RZ_NONNULL RZ_INOUT int *pn_bytes);

/* cprint.c */
Expand Down
81 changes: 63 additions & 18 deletions librz/core/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -5640,8 +5640,8 @@ RZ_API int rz_core_print_disasm_instructions_with_buf(RzCore *core, ut64 address
RzDisasmState *ds = NULL;
int i, j, ret, len = 0;
char *tmpopstr;
const ut64 old_offset = core->offset;
bool hasanalysis = false;
bool alloc_buf = !buf;
const size_t addrbytes = buf ? 1 : core->io->addrbytes;
int skip_bytes_flag = 0, skip_bytes_bb = 0;

Expand All @@ -5656,16 +5656,21 @@ RZ_API int rz_core_print_disasm_instructions_with_buf(RzCore *core, ut64 address
ds->len = nb_opcodes * 8;

if (!buf) {
rz_core_seek(core, address, true);
buf = core->block;
buf = malloc(RZ_ABS(nb_bytes) + 1);
if (!buf) {
RZ_LOG_ERROR("Fail to alloc memory.");
return 0;
}
if (rz_io_nread_at(core->io, address, buf, RZ_ABS(nb_bytes) + 1) == -1) {
RZ_LOG_ERROR("Fail to read from 0x%" PFMT64x ".", address);
free(buf);
return 0;
}
}

core->offset = address;

rz_cons_break_push(NULL, NULL);
// build ranges to map addr with bits
j = 0;
toro:
for (i = 0; rz_disasm_check_end(nb_opcodes, j, nb_bytes, addrbytes * i); i += ret, j++) {
ds->at = address + i;
ds->vat = rz_core_pava(core, ds->at);
Expand Down Expand Up @@ -5785,28 +5790,26 @@ RZ_API int rz_core_print_disasm_instructions_with_buf(RzCore *core, ut64 address
ds->hint = NULL;
}
}
if (buf == core->block && nb_opcodes > 0 && j < nb_opcodes) {
rz_core_seek(core, core->offset + i, true);
goto toro;
}
rz_cons_break_pop();
ds_free(ds);
core->offset = old_offset;
rz_reg_arena_pop(core->analysis->reg);

if (alloc_buf) {
free(buf);
}
return len;
}

/**
* \brief Converting negative numbers n_opcodes and n_opcodes
* \brief (DEPRECATED, consider rz_core_backward_offset)
* Converting negative numbers n_opcodes and n_opcodes
* to positive numbers n_opcodes and n_opcodes
* and seek the appropriate offset
* \param core RzCore reference
* \param pn_opcodes Pointer to n_opcodes
* \param pn_bytes Pointer to n_bytes
* \return success
*/
RZ_IPI bool rz_core_handle_backwards_disasm(RZ_NONNULL RzCore *core,
RZ_DEPRECATE RZ_IPI bool rz_core_handle_backwards_disasm(RZ_NONNULL RzCore *core,
RZ_NONNULL RZ_INOUT int *pn_opcodes, RZ_NONNULL RZ_INOUT int *pn_bytes) {
rz_return_val_if_fail(core && pn_opcodes && pn_bytes, false);

Expand Down Expand Up @@ -5849,18 +5852,60 @@ RZ_IPI bool rz_core_handle_backwards_disasm(RZ_NONNULL RzCore *core,
return true;
}

/**
* \brief Calculate the offset while \p pn_opcodes and \p pn_bytes
* are negative, and \p pn_opcodes and \p pn_bytes will be
* converted to positive numbers.
* \param core RzCore reference
* \prarm pn_opcodes Pointer to n_opcodes
* \param pn_bytes Pointer to n_bytes
* \return calculated offset
*/
RZ_IPI ut64 rz_core_backward_offset(RZ_NONNULL RzCore *core, RZ_NONNULL RZ_INOUT int *pn_opcodes, RZ_NONNULL RZ_INOUT int *pn_bytes) {
rz_return_val_if_fail(core && pn_opcodes && pn_bytes, false);

if (*pn_opcodes >= 0 && *pn_bytes >= 0) {
return core->offset;
}

ut64 opcode_offset = core->offset;
if (*pn_opcodes < 0) {
*pn_opcodes = -*pn_opcodes;
if (!rz_core_prevop_addr(core, core->offset, *pn_opcodes, &opcode_offset)) {
opcode_offset = rz_core_prevop_addr_force(core, core->offset, *pn_opcodes);
}
}

ut64 byte_offset = core->offset;
if (*pn_bytes < 0) {
*pn_bytes = RZ_MIN(RZ_ABS(*pn_bytes), RZ_CORE_MAX_DISASM);
byte_offset = core->offset - *pn_bytes;
}

return RZ_MIN(opcode_offset, byte_offset);
}

/* Disassemble either `nb_opcodes` instructions, or
* `nb_bytes` bytes; both can be negative.
* Set to 0 the parameter you don't use */
#define MAX_OPSIZE 16
#define MIN_OPSIZE 1
RZ_API int rz_core_print_disasm_instructions(RzCore *core, int nb_bytes, int nb_opcodes) {
const ut64 ocore_offset = core->offset;
int ret = -1;
if (rz_core_handle_backwards_disasm(core, &nb_opcodes, &nb_bytes)) {
ret = rz_core_print_disasm_instructions_with_buf(core, core->offset, NULL, nb_bytes, nb_opcodes);
// handler negative parameters
ut64 offset = rz_core_backward_offset(core, &nb_opcodes, &nb_bytes);
// set the parameter equaling to 0 to a value that won't affect another parameter
if (nb_bytes == 0 && nb_opcodes != 0) {
nb_bytes = MAX_OPSIZE * RZ_ABS(nb_opcodes) + 1;
}
rz_core_seek(core, ocore_offset, true);
if (nb_bytes != 0 && nb_opcodes == 0) {
nb_opcodes = nb_bytes / MIN_OPSIZE + 1;
}
ret = rz_core_print_disasm_instructions_with_buf(core, offset, NULL, nb_bytes, nb_opcodes);
return ret;
}
#undef MIN_OPSIZE
#undef MAX_OPSIZE

RZ_API int rz_core_print_disasm_json(RzCore *core, ut64 addr, ut8 *buf, int nb_bytes, int nb_opcodes, PJ *pj) {
ut64 old_offset = core->offset;
Expand Down
1 change: 0 additions & 1 deletion librz/core/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,3 @@ modules += { 'rz_core': {
],
'plugins': [core_plugins]
}}

14 changes: 9 additions & 5 deletions test/db/analysis/tms320.c64x_32
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ CMDS=<<EOF
e asm.arch=tms320
e asm.cpu=c64x
e asm.pseudo=true
pi~=
pi 0x100~=
EOF
EXPECT=<<EOF
a17 = (half) 0x13cf
Expand Down Expand Up @@ -99,10 +99,11 @@ b0 = 2 .bitset b0 .. 2
a30 = (word) *+b15[0x4883]
*+b15[0x47c1] = (word) b17
*+b15[0x7fff] = (word) b31
a2 = (half) *+b15[0x0]
a16 = a17 * a29
b0 = b18 + 4
b16 = 2 == b22
a1 = a16 * a15
*-a3[a7] = (word) b1
a10 = 8 ext a0 .. 0
a18 = max(a24, a16)
b20 = (half) *+b14[0x5bc3]
b30 = (word) *-a16[0]
a0 = 0x10 ext a15 .. 0
a2 = 8 ext a29 .. 0x18
Expand Down Expand Up @@ -201,6 +202,9 @@ a16 = (half) *+b14[0x29f0]
a0 = 0 ext a0 .. 0
b30 = b30 * b17
a16 = 2 .bitset a2 .. 2
a0 = 0x10 .bitset a28 .. 0x10
b3 = b15 * b16
a7:a6 = a5 * a18
EOF
RUN

Expand Down
3 changes: 1 addition & 2 deletions test/db/analysis/v850
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ sd +128
e asm.arch=v850
e asm.pseudo=true
b 64
pi~=
pi 64~=
EOF
EXPECT=<<EOF
*(r8 + 0x1047) = (byte) r24
Expand Down Expand Up @@ -396,4 +396,3 @@ r25 = (byte) *(r8805 + 0x5)
r9 = r0
EOF
RUN

4 changes: 2 additions & 2 deletions test/db/cmd/cmd_foreach
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ FILE=./bins/elf/hello_world
ARGS=-A
CMDS=<<EOF
s 0x844
(_;s;b;pi)() @@i
(_;s;b;pi 0x1)() @@i
EOF
EXPECT=<<EOF
0x844
Expand Down Expand Up @@ -398,4 +398,4 @@ EXPECT=<<EOF
0x222c0

EOF
RUN
RUN
2 changes: 1 addition & 1 deletion test/db/cmd/cmd_open
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ EXPECT=<<EOF
0* 0x100000000 ################-------------- 0x100005000 r-x 3 fmap.TEXT
1 0x100005000 ---------------####----------- 0x100006000 r-- 3 fmap.DATA
2 0x100006000 ------------------############ 0x10000a000 r-- 3 fmap.LINKEDIT
=> 0x1000011e8 ------------------------------ 0x1000012e8
=> 0x1000011e8 ------------------------------ 0x1000012e8
EOF
RUN

Expand Down
31 changes: 30 additions & 1 deletion test/db/cmd/cmd_pi
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,33 @@ CMDS=e scr.color=1 ; pi 1
EXPECT=<<EOF
xor ebp, ebp
EOF
RUN
RUN

NAME=negative pi
FILE=malloc://1024
CMDS=<<EOF
e asm.arch=x86
e asm.bits=64
wx 0x90
s 0x1
pi -1
EOF
EXPECT=<<EOF
nop
EOF
RUN

NAME=negative pI
FILE=malloc://1024
CMDS=<<EOF
e asm.arch=x86
e asm.bits=64
wx 0x9090
s 0x2
pI -2
EOF
EXPECT=<<EOF
nop
nop
EOF
RUN

0 comments on commit 4f7e0e1

Please sign in to comment.