Undefined behavior error: call to function visit_type_InetSocketAddress_members 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 --extra-cflags=-fsanitize=undefined and --extra-cflags=-fno-sanitize-recover=undefined on a system that has Clang v17 or newer (e.g. on Fedora 39 or Fedora 40), the unit tests abort with an undefined behavior error.

Steps to reproduce

  1. ./configure --cc=clang --extra-cflags=-fsanitize=undefined --extra-cflags=-fno-sanitize-recover=undefined --target-list=x86_64-softmmu
  2. make -j$(nproc)
  3. make check-unit

Additional information

test-io-channel-socket aborts with:

 74/103 qemu:unit / test-io-channel-socket               ERROR            0.15s   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_=163 G_TEST_SRCDIR=tests/unit /tmp/qemu-ubsan/tests/unit/test-io-channel-socket --tap -k
―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ✀  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
stderr:
qapi/qapi-clone-visitor.c:188:5: runtime error: call to function visit_type_SocketAddress through pointer to incorrect function type 'bool (*)(struct Visitor *, const char *, void **, struct Error **)'
/tmp/qemu-ubsan/qapi/qapi-visit-sockets.c:487: note: visit_type_SocketAddress defined here
    #0 0x5642aa2f7f3b in qapi_clone qapi/qapi-clone-visitor.c:188:5
    #1 0x5642aa2c8ce5 in qio_channel_socket_listen_async io/channel-socket.c:285:18
    #2 0x5642aa2b8903 in test_io_channel_setup_async tests/unit/test-io-channel-socket.c:116:5
    #3 0x5642aa2b8204 in test_io_channel tests/unit/test-io-channel-socket.c:179:9
    #4 0x5642aa2b8129 in test_io_channel_ipv4 tests/unit/test-io-channel-socket.c:323:5
    #5 0x7f01212c0bbf  (/lib64/libglib-2.0.so.0+0x8bbbf) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
    #6 0x7f01212c0b2f  (/lib64/libglib-2.0.so.0+0x8bb2f) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
    #7 0x7f01212c0b2f  (/lib64/libglib-2.0.so.0+0x8bb2f) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
    #8 0x7f01212c0b2f  (/lib64/libglib-2.0.so.0+0x8bb2f) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
    #9 0x7f01212c10c9 in g_test_run_suite (/lib64/libglib-2.0.so.0+0x8c0c9) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
    #10 0x7f01212c115f in g_test_run (/lib64/libglib-2.0.so.0+0x8c15f) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
    #11 0x5642aa2b72ec in main tests/unit/test-io-channel-socket.c:613:12
    #12 0x7f0120d2d087 in __libc_start_call_main (/lib64/libc.so.6+0x2a087) (BuildId: b098f1c75a76548bb230d8f551eae07a2aeccf06)
    #13 0x7f0120d2d14a in __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x2a14a) (BuildId: b098f1c75a76548bb230d8f551eae07a2aeccf06)
    #14 0x5642aa28cd04 in _start (tests/unit/test-io-channel-socket+0x69d04) (BuildId: eeaee2b8d62ce3aa77ab8b447916a40defd78dc6)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior qapi/qapi-clone-visitor.c:188:5 

(test program exited with status code -6)

And test-char aborts with:

 99/103 qemu:unit / test-char                            ERROR            0.12s   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_=197 G_TEST_SRCDIR=tests/unit /tmp/qemu-ubsan/tests/unit/test-char --tap -k
―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ✀  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
stderr:
qapi/qapi-clone-visitor.c:202:5: runtime error: call to function visit_type_InetSocketAddress_members through pointer to incorrect function type 'bool (*)(struct Visitor *, void *, struct Error **)'
/tmp/qemu-ubsan/qapi/qapi-visit-sockets.c:65: note: visit_type_InetSocketAddress_members defined here
    #0 0x55ee1d20ad60 in qapi_clone_members qapi/qapi-clone-visitor.c:202:5
    #1 0x55ee1d24a993 in socket_address_flattenutil/qemu-sockets.c
    #2 0x55ee1d1f26f6 in qmp_chardev_open_udp chardev/char-udp.c:199:34
    #3 0x55ee1d1f5254 in qemu_char_open chardev/char.c:271:9
    #4 0x55ee1d1f5254 in chardev_new chardev/char.c:968:5
    #5 0x55ee1d1f45fd in qemu_chardev_new chardev/char.c:998:11
    #6 0x55ee1d1f45fd in qemu_chr_new_from_opts chardev/char.c:657:11
    #7 0x55ee1d1f49ac in qemu_chr_new_noreplay chardev/char.c:703:11
    #8 0x55ee1d1f4aed in qemu_chr_new_permit_mux_mon chardev/char.c:731:11
    #9 0x55ee1d1b45b8 in char_udp_test_internal tests/unit/test-char.c:590:15
    #10 0x7f3dd421abbf  (/lib64/libglib-2.0.so.0+0x8bbbf) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
    #11 0x7f3dd421ab2f  (/lib64/libglib-2.0.so.0+0x8bb2f) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
    #12 0x7f3dd421b0c9 in g_test_run_suite (/lib64/libglib-2.0.so.0+0x8c0c9) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
    #13 0x7f3dd421b15f in g_test_run (/lib64/libglib-2.0.so.0+0x8c15f) (BuildId: 795136df3faa85587229ddc59d709f81d6f697df)
    #14 0x55ee1d1af6bd in main tests/unit/test-char.c:1579:12
    #15 0x7f3dd3c3d087 in __libc_start_call_main (/lib64/libc.so.6+0x2a087) (BuildId: b098f1c75a76548bb230d8f551eae07a2aeccf06)
    #16 0x7f3dd3c3d14a in __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x2a14a) (BuildId: b098f1c75a76548bb230d8f551eae07a2aeccf06)
    #17 0x55ee1d184e34 in _start (/tmp/qemu-ubsan/tests/unit/test-char+0x78e34) (BuildId: afdf2ec9875e3011d3ff99174ec137dc79fff74e)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior .qapi/qapi-clone-visitor.c:202:5 

(test program exited with status code -6)

This undefined behavior 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, and #2345 (closed) for a similar problem in the QEMU lockable code.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information