Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RISC-V] pc is not increased after execute ecall #1477

Closed
cla7aye15I4nd opened this issue Nov 2, 2021 · 1 comment
Closed

[RISC-V] pc is not increased after execute ecall #1477

cla7aye15I4nd opened this issue Nov 2, 2021 · 1 comment

Comments

@cla7aye15I4nd
Copy link

I am trying to simulate a simple riscv helloworld example and hook the syscall, but it seems like the uc2 do not increase the pc after executing ecall.

Here is my uc code:

import lief

from capstone import Cs, CS_ARCH_RISCV, CS_MODE_RISCV64

from unicorn.riscv_const import *
from unicorn import Uc, UC_ARCH_RISCV, UC_MODE_RISCV64, UC_HOOK_INTR

uc = Uc(UC_ARCH_RISCV, UC_MODE_RISCV64)
cs = Cs(CS_ARCH_RISCV, CS_MODE_RISCV64)

elf = lief.parse('hello')
code = bytearray(elf.get_section('.text').content)

uc.mem_map(0, 0x100000)
uc.mem_write(elf.entrypoint, bytes(code) + b'hello world\n!')

def hook_intr(uc, intno, user_data):
    print(f'Interrupt number: {intno} (pc = {hex(uc.reg_read(UC_RISCV_REG_PC))})')

for i in cs.disasm(code, elf.entrypoint):
    print("0x%x:\t%s\t%s" %(i.address, i.mnemonic, i.op_str))
    
uc.hook_add(UC_HOOK_INTR, hook_intr)
uc.emu_start(elf.entrypoint, 0, count=10)

and this is my riscv code:

.global _start

_start: addi  a0, x0, 1
        la    a1, helloworld
        addi  a2, x0, 13    
        addi  a7, x0, 64    
        ecall
        addi    a0, x0, 0
        addi    a7, x0, 93
        ecall

.data
helloworld:      .ascii "Hello RISCV!\n"

The output of the simulation is:

0x100b0:	addi	a0, zero, 1
0x100b4:	auipc	a1, 1
0x100b8:	addi	a1, a1, 0x20
0x100bc:	addi	a2, zero, 0xd
0x100c0:	addi	a7, zero, 0x40
0x100c4:	ecall	
0x100c8:	mv	a0, zero
0x100cc:	addi	a7, zero, 0x5d
0x100d0:	ecall	
Interrupt number: 8 (pc = 0x100c4)
Interrupt number: 8 (pc = 0x100c4)
Interrupt number: 8 (pc = 0x100c4)
Interrupt number: 8 (pc = 0x100c4)
Interrupt number: 8 (pc = 0x100c4)

We can find that the ecall has been executed, but the pc has not increased.

wtdcode added a commit that referenced this issue Nov 3, 2021
@wtdcode
Copy link
Member

wtdcode commented Nov 3, 2021

FIxed in 67e2386

@wtdcode wtdcode closed this as completed Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants