Skip to content

JALR causes wrong behavior on AsmMachine when rs1 and rd use the same register. #92

@jjyr

Description

@jjyr

Describe the bug

The following instruction will cause different behavior on DefaultMachine(rust) and AsmMachine(asm).

   1021a:	00000097          	auipc	ra,0x0
   1021e:	fd4080e7          	jalr	-44(ra) # 0x101ee

Let's focus on the instruction 1021e. We expect this instruction to jump to rs1(which is ra) - 14 and set the pc + 4 to the rd(which is ra) register. The DefaultMachine handles this as expected. But unfortunately, the AsmMachine set the registers in a wrong ordering:

https://github.com/nervosnetwork/ckb-vm/blob/develop/src/machine/asm/execute.S#L441

AsmMachine set rd first, then read from rs1, when the rs1 and rd use the different registers this behavior is correct, but if the rs1 and rd use the same register then the WRITE_RD operation overwrite the value of rs1.

How to fix

To simply fix this problem, we can move the WRITE_RD to the after of the REGISTER_ADDRESS(RS1).

The change may cause the network consensus split. We need to wait for a hard-fork to apply it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    t:bugType: This doesn't seem right.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions