Skip to content

Commit

Permalink
Smarter anal.nopskip - improves behaviour, but not configurable ##anal
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Oct 6, 2022
1 parent 18cdcd3 commit 16e9348
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 14 deletions.
42 changes: 36 additions & 6 deletions libr/anal/fcn.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ static inline bool op_is_set_bp(const char *op_dst, const char *op_src, const ch
}

static inline bool does_arch_destroys_dst(const char *arch) {
return arch && (!strncmp (arch, "arm", 3) || !strcmp (arch, "riscv") || !strcmp (arch, "ppc"));
return arch && (r_str_startswith (arch, "arm") || r_str_startswith (arch, "riscv") || r_str_startswith (arch, "ppc"));
}

static inline bool has_vars(RAnal *anal, ut64 addr) {
Expand Down Expand Up @@ -563,9 +563,9 @@ static int fcn_recurse(RAnal *anal, RAnalFunction *fcn, ut64 addr, ut64 len, int
0
};
bool arch_destroys_dst = does_arch_destroys_dst (anal->cur->arch);
const bool is_arm = anal->cur->arch && !strncmp (anal->cur->arch, "arm", 3);
const bool is_v850 = is_arm ? false: (anal->cur->arch && (!strncmp (anal->cur->arch, "v850", 4) || !strncmp (anal->coreb.cfgGet (anal->coreb.core, "asm.cpu"), "v850", 4)));
const bool is_x86 = is_arm ? false: anal->cur->arch && !strncmp (anal->cur->arch, "x86", 3);
const bool is_arm = anal->cur->arch && r_str_startswith (anal->cur->arch, "arm");
const bool is_v850 = is_arm ? false: (anal->cur->arch && (r_str_startswith (anal->cur->arch, "v850") || r_str_startswith (anal->coreb.cfgGet (anal->coreb.core, "asm.cpu"), "v850")));
const bool is_x86 = is_arm ? false: anal->cur->arch && r_str_startswith (anal->cur->arch, "x86");
const bool is_amd64 = is_x86 ? fcn->cc && !strcmp (fcn->cc, "amd64") : false;
const bool is_dalvik = is_x86 ? false : anal->cur->arch && !strncmp (anal->cur->arch, "dalvik", 6);
RRegItem *variadic_reg = NULL;
Expand Down Expand Up @@ -717,16 +717,34 @@ static int fcn_recurse(RAnal *anal, RAnalFunction *fcn, ut64 addr, ut64 len, int
src1 = r_vector_index_ptr (op->srcs, 1);

if (anal->opt.nopskip && fcn->addr == at) {
if (at == 0) {
goto noskip;
}
if (anal->config->bits == 64) {
if (at % 8) {
goto noskip;
}
} else {
if (r_anal_archinfo (anal, R_ANAL_ARCHINFO_ALIGN) == 4) {
/// TODO 5.8.0 use anal->codealign vs dataalign
if (at % 4) {
goto noskip;
}
}
}
RFlagItem *fi = anal->flb.get_at (anal->flb.f, addr, false);
if (!fi || strncmp (fi->name, "sym.", 4)) {
if (fi) {
goto noskip;
}
#if 1
if ((addr + delay.un_idx - oplen) == fcn->addr) {
if (r_anal_block_relocate (bb, bb->addr + oplen, bb->size - oplen)) {
fcn->addr += oplen;
idx = delay.un_idx;
goto repeat;
}
}
}
#endif
switch (op->type & R_ANAL_OP_TYPE_MASK) {
case R_ANAL_OP_TYPE_TRAP:
case R_ANAL_OP_TYPE_ILL:
Expand All @@ -742,6 +760,18 @@ static int fcn_recurse(RAnal *anal, RAnalFunction *fcn, ut64 addr, ut64 len, int
}
}
}
goto complete;
noskip:
#if 0
if ((addr + delay.un_idx - oplen) == fcn->addr) {
if (r_anal_block_relocate (bb, bb->addr + oplen, bb->size - oplen)) {
fcn->addr += oplen;
idx = delay.un_idx;
goto repeat;
}
}
#endif
complete:
if (op->hint.new_bits) {
r_anal_hint_set_bits (anal, op->jump, op->hint.new_bits);
}
Expand Down
2 changes: 1 addition & 1 deletion test/db/anal/arm
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ af
afl~[2]
EOF
EXPECT=<<EOF
8
12
EOF
RUN

Expand Down
9 changes: 9 additions & 0 deletions test/db/anal/mips
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,19 @@ e asm.lines.fcn=false
s 0x8060b4f8
wx 0000000000000000002e0500032e05000f00801021100000492d1808000082900800e003211080000000829007004010000000000016020003160200f8ff451000000000482d180801008424211000000800e003000000000000a290050040100100a524000082a00000a290552d1808010084240800e003000080a000000000e0ffbd27542d180c000000000800e0032000bd270000000000000000
aa 2> /dev/null
aaa
pd 38
EOF
EXPECT=<<EOF
;-- pc:
0x8060b4f8 00000000 nop
(fcn) fcn.8060b4fc 4
// void fcn.8060b4fc (int32_t arg1, int32_t arg2);
; arg int32_t arg1 @ a0
; arg int32_t arg2 @ a1
0x8060b4fc 00000000 nop
(fcn) fcn.8060b500 80
// void fcn.8060b500 (int32_t arg1, int32_t arg2);
; arg int32_t arg1 @ a0
; arg int32_t arg2 @ a1
0x8060b500 002e0500 sll a1, a1, 0x18
Expand All @@ -479,6 +486,7 @@ EXPECT=<<EOF
0x8060b548 0800e003 jr ra
0x8060b54c 00000000 nop
(fcn) fcn.8060b550 36
// void fcn.8060b550 (int32_t arg1, int32_t arg2);
; arg int32_t arg1 @ a0
; arg int32_t arg2 @ a1
0x8060b550 0000a290 lbu v0, (a1)
Expand All @@ -492,6 +500,7 @@ EXPECT=<<EOF
0x8060b570 000080a0 sb zero, (a0)
0x8060b574 00000000 nop
(fcn) fcn.8060b578 20
// void fcn.8060b578 (int32_t arg1, int32_t arg2);
; arg int32_t arg1 @ a0
; arg int32_t arg2 @ a1
0x8060b578 e0ffbd27 addiu sp, sp, -0x20
Expand Down
6 changes: 2 additions & 4 deletions test/db/anal/v850
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ EXPECT=<<EOF
0x00107416 1 24 sym._wcrtomb
0x0010742e 1 22 sym.__wctomb_r
0x00107468 1 22 sym.__write
0x001052d0 3 14 fcn.001052d0
0x00107490 1 18 fcn.00107490
0x001052cc 1 4 loc.___trap0
0x00107490 1 18 fcn.00107490
0x001074a4 1 20 fcn.001074a4
0x001074bc 1 24 sym.__lseek
0x001074d4 1 22 sym.__read
Expand Down Expand Up @@ -286,7 +285,7 @@ EXPECT=<<EOF
0x0010d91c 17 342 fcn.0010d91c
0x0010e2a2 7 60 sym._fileno
0x0010e372 1 20 fcn.0010e372
0x001138ec 3 14 fcn.001138ec
0x001138e8 1 4 loc.___trap0
0x0010e78e 15 160 sym.__findenv_r
0x0010e82e 1 18 sym.__getenv_r
0x0010e86e 1 14 sym._iswspace
Expand Down Expand Up @@ -318,7 +317,6 @@ EXPECT=<<EOF
0x0010f3b2 12 272 sym.___d2b
0x0010f4c2 4 148 sym.___ratio
0x0010f66a 1 18 fcn.0010f66a
0x001138e8 1 4 loc.___trap0
0x0010f6e8 1 22 sym._putc
0x0010f732 14 494 sym.__realloc_r
0x0010fc5c 5 82 sym.__sbrk
Expand Down
15 changes: 12 additions & 3 deletions test/db/formats/elf/vax
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ s+2
pdf
EOF
EXPECT=<<EOF
/ 72: fcn.00010b2e ();
;-- section..text:
;-- .text:
;-- __start:
;-- _start:
;-- r15:
;-- pc:
; NULL XREF from aav.0x00010001 @ +0x17(r)
/ 74: entry0 ();
| 0x00010b2c 01 nop ; [11] -r-x section size 31963 named .text
| 0x00010b2d 01 nop
| 0x00010b2e dd59 pushl r9
| 0x00010b30 dd57 pushl r7
| 0x00010b32 fb02ef3d0000. calls $0x2, sym.___start
Expand All @@ -21,12 +30,12 @@ EXPECT=<<EOF
| | 0x00010b5c c25057 subl2 r0, r7
| | 0x00010b5f ca0357 bicl2 $0x3, r7
| | 0x00010b62 9e47ef539501. movab loc.__CTOR_LIST_END__[r7], r7
| | ; CODE XREF from fcn.00010b2e @ 0x10b73(x)
| | ; CODE XREF from entry0 @ 0x10b73(x)
| .--> 0x00010b6a d08650 movl (r6)+, r0
| :| 0x00010b6d fb0060 calls $0x0, (r0)
| :| 0x00010b70 d15657 cmpl r6, r7
| `==< 0x00010b73 12f5 bneq 0x00010b6a
| | ; CODE XREF from fcn.00010b2e @ 0x10b50(x)
| | ; CODE XREF from entry0 @ 0x10b50(x)
\ `-> 0x00010b75 04 ret
EOF
RUN
20 changes: 20 additions & 0 deletions test/db/formats/web_assembly
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ nth paddr vaddr bind type size lib name
EOF
RUN

NAME=WASM Export functions with custom section
FILE=bins/wasm/sections.wasm
CMDS=iE
EXPECT=<<EOF
[Exports]

nth paddr vaddr bind type size lib name
---------------------------------------------------
8 0x000003f1 0x000003f1 GLOBAL FUNC 16 _start
16 0x000004f5 0x000004f5 GLOBAL FUNC 3 stackSave
17 0x000004fa 0x000004fa GLOBAL FUNC 5 stackRestore
18 0x00000505 0x00000505 GLOBAL FUNC 15 stackAlloc
19 0x00000516 0x00000516 GLOBAL FUNC 19 emscripten_stack_init
20 0x0000052b 0x0000052b GLOBAL FUNC 6 emscripten_stack_get_free
21 0x00000533 0x00000533 GLOBAL FUNC 3 emscripten_stack_get_base
22 0x00000538 0x00000538 GLOBAL FUNC 3 emscripten_stack_get_end
23 0x0000053d 0x0000053d GLOBAL FUNC 4 __errno_location
EOF
RUN

NAME=WASM Export functions w/o custom section
FILE=bins/wasm/unary.wasm
CMDS=iE
Expand Down

0 comments on commit 16e9348

Please sign in to comment.