capstone icon indicating copy to clipboard operation
capstone copied to clipboard

Invalid ID for riscv64 RET instruction

Open radare opened this issue 6 years ago • 6 comments

$ ./cstool/cstool -d riscv64 67800000
 0  67 80 00 00  ret
	ID: 207 (jalr)

i have started to add the riscv support to r2 by using Capstone (we have been using our own analysis and the gnu disassembler until now), so i'll be adding more issues when i compare results and improve the plugin outputs.

radare avatar May 20 '19 17:05 radare

@fanfuqiang @citypw i think the mapping for RET is wrong in https://github.com/aquynh/capstone/blob/next/arch/RISCV/RISCVMappingInsn.inc?

aquynh avatar May 21 '19 01:05 aquynh

sorry, let me check.

fanfuqiang avatar May 21 '19 04:05 fanfuqiang

I looked at the mapping file, and think the bug is elsewhere. Is RET just an alias? If so, you may forget to map alias mnemonic back to alias ID, which explains why we still see the original instruction ID.

aquynh avatar May 21 '19 04:05 aquynh

I looked at the mapping file, and think the bug is elsewhere. Is RET just an alias? If so, you may forget to map alias mnemonic back to alias ID, which explains why we still see the original instruction ID.

Thx, I am fixing.

fanfuqiang avatar May 21 '19 05:05 fanfuqiang

Specification of riscv has: Pseudoinstruction: ret equals Base Instructions: jalr x0, x1, 0; auipc x6, offset[31:12] for legibility. so as ret is not a real Instruction in chip. I had dig into the code. looks like it is an upstream bug. when LLVM meet jalr x0, x1, 0 immediately return "ret". Of course, this happens when enabling alias instructions.

I am working on this.

fanfuqiang avatar May 21 '19 10:05 fanfuqiang

@radare @aquynh Sorry, guys, my mistake.

Actually, ret only ALIAS jalr x0, x1, 0. The bad typesetting of RISCV specification makes me blind. For now, I use an easy way to fix this. I just disable alias instruction display when Capstone enables details.

CASE A: ./cstool -d riscv64 67800000 0 67 80 00 00 jalr zero, ra, 0

ID: 207 (jalr)

op_count: 3

	operands[0].type: REG = zero

	operands[1].type: REG = ra

	operands[2].type: IMM = 0x0

So what do you want? enable alias instruction display like this?

CASE B: ./cstool -d riscv64 67800000 0 67 80 00 00 ret

ID: 207 (jalr)

op_count: 3

	operands[0].type: REG = zero

	operands[1].type: REG = ra

	operands[2].type: IMM = 0x0

or previous?

fanfuqiang avatar May 28 '19 23:05 fanfuqiang