Lifting the base (integer) extension of RISC-V, some compressed variants of integer instructions, and some multiplication/division instructions#6287
Conversation
|
@notxvilka for visiblity, not ready for merge. Next: Adapt the code to work for 64-bit arch as well as 32-bit, and test against machine traces from BAP QEMU. |
1e2a880 to
119d814
Compare
119d814 to
085c2ae
Compare
085c2ae to
2e32ca1
Compare
|
@notxvilka @Rot127 @wargio this is ready for a look btw, it's mostly complete but I'm fighting an issue with some compressed instruction decoding (capstone bug) |
33626ac to
36fa429
Compare
|
NOTE-1: I initially intended this to only cover integer (rv32I and rv64I) subsets of the ISA, but in order to enable real-world testing on gcc-compiled binaries I had to support compressed subsets, so this PR is less orthogonal than I would like, there is even a couple of multiplication and division instructions in there. Sorry. This is annoying but trying to link against libc from non-IMAFD binaries is even more annoying. I will try to keep future extensions as self-contained as possible. NOTE-2: In order to modularize, each ISA extension is seperated into a different 1- The lifting function name For example, 1- This architecture allows arbitrary cross-sharing of decoding logic with any instruction instead of hardcoding it into a specific extension or instruction group. There is also |
1c65e28 to
1055dab
Compare
a357f47 to
db4c13b
Compare
1208ba9 to
c6f4465
Compare
20ea324 to
f50a6d6
Compare
Rot127
left a comment
There was a problem hiding this comment.
Please finish those ones before continuing to work on the others.
There are still a few failing and some leaks.
| RZ_LOG_ERROR("op_str: %s\n", insn->op_str); | ||
| RZ_LOG_ERROR("need_effective_addr: %d\n", insn->detail->riscv.need_effective_addr); | ||
| RZ_LOG_ERROR("op_count: %u\n", insn->detail->riscv.op_count); | ||
| for (int i = 0; i < insn->detail->riscv.op_count; i++) { | ||
| RZ_LOG_ERROR("operands[%d].type: %d\n", i, insn->detail->riscv.operands[i].type); | ||
| if (insn->detail->riscv.operands[i].type == RISCV_OP_REG) { | ||
| RZ_LOG_ERROR(" REG = %d\n", insn->detail->riscv.operands[i].reg); | ||
| } else if (insn->detail->riscv.operands[i].type == RISCV_OP_IMM) { | ||
| RZ_LOG_ERROR(" IMM = 0x%" PFMT64x "\n", (ut64)insn->detail->riscv.operands[i].imm); | ||
| } else if (insn->detail->riscv.operands[i].type == RISCV_OP_MEM) { | ||
| RZ_LOG_ERROR(" MEM base = %d, disp = 0x%" PFMT64x "\n", insn->detail->riscv.operands[i].mem.base, (ut64)insn->detail->riscv.operands[i].mem.disp); | ||
| } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Also please remove this logging function before merging.
|
Ouh, and please add a test which prints instructions with Because there are already a few: |
…riants of the instructions in it for 32-bit arch variant only, and add some tests
16bdfd8 to
3275ee2
Compare
Rot127
left a comment
There was a problem hiding this comment.
The branch instructions leak:
=================================================================
==34077==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 40 byte(s) in 1 object(s) allocated from:
#0 0x75d3c0ae68a3 in calloc (/lib64/libasan.so.8+0xe68a3) (BuildId: 25975f766867e9e604dc5a71a8befeaed3301942)
#1 0x75d3bb244749 in rz_il_op_new_bitv_from_ut64 ../librz/il/il_opcodes.c:180
#2 0x75d3b39571d3 in riscv_il_get_reg ../librz/arch/isa/riscv/riscv_il_base.h:17
#3 0x75d3b3959a58 in rz_riscv_lift_c_beqz ../librz/arch/isa/riscv/riscv_il_compressed.c:69
#4 0x75d3b393d8ad in rz_riscv_lift_instr ../librz/arch/isa/riscv/riscv_il.c:152
#5 0x75d3b3f3d9c9 in analyze_op ../librz/arch/p/analysis/analysis_riscv_cs.c:1929
#6 0x75d3b408cad1 in rz_analysis_op ../librz/arch/op.c:126
#7 0x75d3c067d960 in print_disassembly_output ../librz/main/rz-asm.c:331
#8 0x75d3c068393e in rz_main_rz_asm ../librz/main/rz-asm.c:896
#9 0x0000004007ad in main ../binrz/rz-asm/rz-asm.c:8
#10 0x75d3bcc105b4 in __libc_start_call_main (/lib64/libc.so.6+0x35b4) (BuildId: 88c03969b4319fc0140734fe863825ec4a1cdb0d)
#11 0x75d3bcc10667 in __libc_start_main@@GLIBC_2.34 (/lib64/libc.so.6+0x3667) (BuildId: 88c03969b4319fc0140734fe863825ec4a1cdb0d)
#12 0x0000004006c4 in _start (/home/user/.local/bin/rz-asm+0x4006c4) (BuildId: 7e9c8cb46628eb0719490f7b5b48ea6d6bb3e8b9)
Indirect leak of 32 byte(s) in 1 object(s) allocated from:
#0 0x75d3c0ae68a3 in calloc (/lib64/libasan.so.8+0xe68a3) (BuildId: 25975f766867e9e604dc5a71a8befeaed3301942)
#1 0x75d3bd61a391 in rz_bv_new ../librz/util/bitvector.c:101
#2 0x75d3bd627c56 in rz_bv_new_from_ut64 ../librz/util/bitvector.c:1747
#3 0x75d3bb244725 in rz_il_op_new_bitv_from_ut64 ../librz/il/il_opcodes.c:176
#4 0x75d3b39571d3 in riscv_il_get_reg ../librz/arch/isa/riscv/riscv_il_base.h:17
#5 0x75d3b3959a58 in rz_riscv_lift_c_beqz ../librz/arch/isa/riscv/riscv_il_compressed.c:69
#6 0x75d3b393d8ad in rz_riscv_lift_instr ../librz/arch/isa/riscv/riscv_il.c:152
#7 0x75d3b3f3d9c9 in analyze_op ../librz/arch/p/analysis/analysis_riscv_cs.c:1929
#8 0x75d3b408cad1 in rz_analysis_op ../librz/arch/op.c:126
#9 0x75d3c067d960 in print_disassembly_output ../librz/main/rz-asm.c:331
#10 0x75d3c068393e in rz_main_rz_asm ../librz/main/rz-asm.c:896
#11 0x0000004007ad in main ../binrz/rz-asm/rz-asm.c:8
#12 0x75d3bcc105b4 in __libc_start_call_main (/lib64/libc.so.6+0x35b4) (BuildId: 88c03969b4319fc0140734fe863825ec4a1cdb0d)
#13 0x75d3bcc10667 in __libc_start_main@@GLIBC_2.34 (/lib64/libc.so.6+0x3667) (BuildId: 88c03969b4319fc0140734fe863825ec4a1cdb0d)
#14 0x0000004006c4 in _start (/home/user/.local/bin/rz-asm+0x4006c4) (BuildId: 7e9c8cb46628eb0719490f7b5b48ea6d6bb3e8b9)
SUMMARY: AddressSanitizer: 72 byte(s) leaked in 2 allocation(s).
Also many others, please run the asm tests with LSAN
|
Also: |
Your checklist for this pull request
RZ_APIfunction and struct this PR changes.RZ_API).Detailed description
...
Test plan
...
Closing issues
PR Stack Structure
This PR is the first of a 4-PR stack that lifts the IMAFD RISC-V archiecture plus some privileged instructions.
The following is a table for ease of navigation from any PR to any other, please filter by the commit(s) mentioned in the table when reviewing the diff in the "Files Changed" tab to avoid the noise of the base branches.
Because it's painful to maintain this stack manually with no Github or other tooling support, I won't grow it any further until one of those 4 PRs gets merged.
Depends-on: rizinorg/rz-tracetest#29
Depends-on: BinaryAnalysisPlatform/qemu#44