Skip to content

Threaded issue for RISC-V host #2254

@coden-s

Description

@coden-s

I’m working on emulating AArch64 on RISC-V linux (a Banana Pi development board).
My basic test case runs through different loops and returns how many times the loops were hit. I’ve verified that it works when running on a single thread both when emulating AArch64 on RISC-V linux and when emulating AArch64 on x86_64 linux. There’s also a threaded version of the test which makes 8 separate threads which each run the same test in parallel. Each thread individually loads the shared object, creates its own UC instance and stack and begins emulation. No resources are shared between the threads. The threaded test always works when emulating AArch64 on x86_64 linux.
However, when emulating AArch64 on RISC-V linux, tests occasionally fail by hitting an unhandled CPU exception. It hits the add instruction at 0x59c, then seems to jump multiple pages and continue executing until the loop restarts (which is where it breaks). Despite this, the instructions seem to be executing fine as the store hooks work and are using expected values.

 598: b94007e0      ldr     w0, [sp, #0x4]
 59c: 11000400      add     w0, w0, #0x1        // update sum
 5a0: b90007e0      str     w0, [sp, #0x4] 
 5a4: b9400fe0      ldr     w0, [sp, #0xc]
 5a8: 51000400      sub     w0, w0, #0x1        // update i
 5ac: b9000fe0      str     w0, [sp, #0xc]
 5b0: b9400fe0      ldr     w0, [sp, #0xc]
 5b4: 7100001f      cmp     w0, #0x0
 5b8: 54ffff0c      b.gt    0x598 <loop+0x40>

It always breaks at the same address when going through the loop for the first time, though the thread that breaks isn’t always the same (so the address it jumps to varies since each thread has a unique start address).
Debug log of it breaking:

[Thread 7] Executing instruction at 0x3f7c001598 (598): e0 07 40 b9
[Thread 7] Executing instruction at 0x3f7c00159c (59c): 00 04 00 11
[Thread 7] Executing instruction with bad PC at 0x3e7c0015a0 (ffffffff000005a0) // (huge jump in addresses but instructions seem to continue executing correctly??)
[Thread 7] Memory WRITE at 0x7c027cc4: size=4, value=0x6
[Thread 7] Executing instruction with bad PC at 0x3e7c0015a4 (ffffffff000005a4)
[Thread 7] Executing instruction with bad PC at 0x3e7c0015a8 (ffffffff000005a8)
[Thread 7] Executing instruction with bad PC at 0x3e7c0015ac (ffffffff000005ac)
[Thread 7] Memory WRITE at 0x7c027ccc: size=4, value=0x7
[Thread 7] Executing instruction with bad PC at 0x3e7c0015b0 (ffffffff000005b0)
[Thread 7] Executing instruction with bad PC at 0x3e7c0015b4 (ffffffff000005b4)
[Thread 7] Executing instruction with bad PC at 0x3e7c0015b8 (ffffffff000005b8)
[Thread 7] Executing instruction with bad PC at 0x3e7c001598 (ffffffff00000598)
[Thread 7] Error while emulating: Unhandled CPU exception (UC_ERR_EXCEPTION)
[Thread 7] Test failed! Returned: 7

I've attached the test case and the loop test it uses.

unicorn_loop_test.c

loop.c

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions