Skip to content

qemu-aarch64: wrong execution result when executing the code

Host environment

  • Operating system: Ubuntu 22.04.4 LTS
  • OS/kernel version: Linux vm 6.5.0-26-generic #26~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Mar 12 10:22:43 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
  • Architecture: x86_64
  • QEMU flavor: qemu-aarch64
  • QEMU version: 8.2.1, 8.2.2, 9.0.0-rc0, latest commit 6a4180af
  • QEMU command line: qemu-aarch64 -L /usr/aarch64-linux-gnu -E LD_LIBRARY_PATH=/usr/aarch64-linux-gnu/lib foo

Emulated/Virtualized environment

Runs on QEMU user mode

Description of problem

The following aarch64 code results in the wrong execution result 4611686018427387903, which is 0x3fffffffffffffff. (The correct result is -1) The bug seems to be introduced in between v8.1.5 and v8.2.1 since the results are correct in v8.1.5.

// foo.c
#include <stdio.h>
#include <stdint.h>

int64_t callme(size_t _1, size_t _2, int64_t a, int64_t b, int64_t c);

int main() {
    int64_t ret = callme(0, 0, 0, 1, 2);
    printf("%ld\n", ret);
    return 0;
}
// foo.S
.global callme
callme:
  cmp   x2, x3
  cset  x12, lt
  and   w11, w12, #0xff
  cmp   w11, #0x0
  csetm x14, ne
  lsr   x13, x14, x4
  sxtb  x0, w13
  ret

Steps to reproduce

  1. Build the code with aarch64-linux-gnu-gcc foo.c foo.S -o foo (aarch64-linux-gnu-gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0)
  2. Run the code with qemu-aarch64 -L /usr/aarch64-linux-gnu -E LD_LIBRARY_PATH=/usr/aarch64-linux-gnu/lib foo and see the result

Additional information

  • Original discussion is held in this wasmtime issue. Thanks to Alex Crichton for clarifying this bug.
Edited by pjy
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information