Undefined behavior error: call to function qemu_mutex_lock through pointer to incorrect function type
Host environment
- Operating system: Fedora 40
- Architecture: x86
- QEMU flavor: qemu-system-x86_64
- QEMU version: master branch (commit 3d48b6b6)
Description of problem
When compiling QEMU with:
./configure --cc=clang --extra-cflags=-fsanitize=undefined --extra-cflags=-fno-sanitize-recover=undefined --target-list=x86_64-softmmu
on a system that has Clang v17 or newer (e.g. on Fedora 39 or Fedora 40), the QEMU binary abort with an undefined behavior error:
$ ./qemu-system-x86_64
include/qemu/lockable.h:95:5: runtime error: call to function qemu_mutex_lock through pointer to incorrect function type 'void (*)(void *)'
include/qemu/thread.h:122:5: note: qemu_mutex_lock defined here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior include/qemu/lockable.h:95:5
Or for example when running make check-unit :
97/103 qemu:unit / test-yank ERROR 0.13s killed by signal 6 SIGABRT
>>> G_TEST_BUILDDIR=/tmp/qemu-ubsan/tests/unit ASAN_OPTIONS=halt_on_error=1:abort_on_error=1:print_summary=1 UBSAN_OPTIONS=halt_on_error=1:abort_on_error=1:print_summary=1:print_stacktrace=1 MALLOC_PERTURB_=201 G_TEST_SRCDIR=~/qemu/tests/unit /tmp/qemu-ubsan/tests/unit/test-yank --tap -k
――――――――――――――――――――――――― ✀ ―――――――――――――――――――――――――――――――――――――――――
stderr:
include/qemu/lockable.h:95:5: runtime error: call to function qemu_mutex_lock through pointer to incorrect function type 'void (*)(void *)'
include/qemu/thread.h:122:5: note: qemu_mutex_lock defined here
#0 0x55753123f8b9 in qemu_lockable_lock include/qemu/lockable.h:95:5
#1 0x55753123f8b9 in qemu_lockable_auto_lock include/qemu/lockable.h:105:5
#2 0x55753123f8b9 in qmp_query_yank util/yank.c:184:5
#3 0x5575311a35fe in is_yank_instance_registered tests/unit/test-yank.c:43:12
#4 0x5575311a35fe in char_change_test tests/unit/test-yank.c:128:5
#5 0x7f7f0a8cfbbf (/lib64/libglib-2.0.so.0+0x8bbbf) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
#6 0x7f7f0a8cfb2f (/lib64/libglib-2.0.so.0+0x8bb2f) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
#7 0x7f7f0a8cfb2f (/lib64/libglib-2.0.so.0+0x8bb2f) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
#8 0x7f7f0a8cfb2f (/lib64/libglib-2.0.so.0+0x8bb2f) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
#9 0x7f7f0a8d00c9 in g_test_run_suite (/lib64/libglib-2.0.so.0+0x8c0c9) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
#10 0x7f7f0a8d015f in g_test_run (/lib64/libglib-2.0.so.0+0x8c15f) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
#11 0x5575311a336f in main tests/unit/test-yank.c:248:12
#12 0x7f7f0a32d087 in __libc_start_call_main (/lib64/libc.so.6+0x2a087) (BuildId: b098f1c75a76548bb230d8f551eae07a2aeccf06)
#13 0x7f7f0a32d14a in __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x2a14a) (BuildId: b098f1c75a76548bb230d8f551eae07a2aeccf06)
#14 0x557531178d64 in _start (/tmp/qemu-ubsan/tests/unit/test-yank+0x77d64) (BuildId: 0bb470b7accec26b684d1c7e941239d31396604e)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior include/qemu/lockable.h:95:5
(test program exited with status code -6)
The way we abuse the (void *) parameter of QemuLockUnlockFunc seems to be undefined behavior, which could likely also trigger issues with CFI or certain compilers/architectures like emscripten, so we should try to avoid this. See also https://github.com/systemd/systemd/issues/29972 or https://github.com/python/cpython/issues/111178 for discussions in other projects.