Skip to contentSkip to conversation

arm (and others): fix some memory leaks

PATCH In review Peter Maydell 4 participants 15 messages Mar 2 – Apr 13 Arm/Aspeed machine support

lore
Share & tools
lore
#0 lore raw

v2 cover arm (and others): fix some memory leaks

This patchset fixes a couple of minor memory leaks I found by running "make check" for the arm targets with the clang leak sanitizer enabled. Version 2 also includes some patches by Fabiano from last year that fell through the cracks; I've rebased them and included them here.

Changes in v2:
* patch 1 now just moves the file to keep 'git log --follow' happy
* patch 1 fixes the filename in tests/docker/test-fuzz
* patch 2 has the SPDX/doc comment that were in patch 1 before
* patch 2 drops the walk_path suppression as one of Fabiano's patches fixes this leak properly
* patches 5-9 are from Fabiano; most were reviewed last year

Patches still needing review: just patch 1.

thanks
-- PMM

Fabiano Rosas (5):
chardev: Fix QIOChannel refcount
chardev: Don't attempt to unregister yank function more than once
chardev: Consolidate yank registration tests/qtest/test-x86-cpuid-compat: Free allocated memory tests/qtest/qos-test: Plug a couple of leaks

Peter Maydell (4):
scripts: Move lsan_suppressions.txt out of oss-fuzz subdir scripts/lsan_suppressions.txt: Add more leaks hw/arm/aspeed_gpio: Don't leak string in aspeed_gpio_init() tests/qtest/iommu-smmuv3-test: Free QPCIDevice

 .gitlab-ci.d/buildtest.yml             |  2 +-
 chardev/char-io.c                      |  5 ++++
 chardev/char-socket.c                  | 34 ++++++------------------
 hw/gpio/aspeed_gpio.c                  |  2 +-
 scripts/lsan_suppressions.txt          | 26 +++++++++++++++++++
 scripts/oss-fuzz/lsan_suppressions.txt |  5 ----
 tests/docker/test-fuzz                 |  2 +-
 tests/qtest/iommu-smmuv3-test.c        |  1 +
 tests/qtest/qos-test.c                 | 36 +++++++++++++++++++-------
 tests/qtest/test-x86-cpuid-compat.c    |  4 +++
 10 files changed, 73 insertions(+), 44 deletions(-)
 create mode 100644 scripts/lsan_suppressions.txt
 delete mode 100644 scripts/oss-fuzz/lsan_suppressions.txt
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/9 scripts: Move lsan_suppressions.txt out of oss-fuzz subdir

The oss-fuzz code uses an lsan_suppressions file to suppress certain leak-sanitizer cases that are known issues or not our code's bug. This is useful more widely than just for the fuzzer harness: if you want to build QEMU with the leak sanitizer enabled and run 'make check' then you will want to suppress some bogus leak reports.

Move the file up a directory.

---
v2: just move the file so we keep git log --follow happy; fix up reference in tests/docker/test-fuzz

---
 .gitlab-ci.d/buildtest.yml                   | 2 +-
 scripts/{oss-fuzz => }/lsan_suppressions.txt | 0
 tests/docker/test-fuzz                       | 2 +-
 3 files changed, 2 insertions(+), 2 deletions(-)
 rename scripts/{oss-fuzz => }/lsan_suppressions.txt (100%)
.gitlab-ci.d/buildtest.yml +1 -1
index 1b656b9eb0..7548057c54 100644
@@ -626,7 +626,7 @@ build-oss-fuzz:
     IMAGE: fedora
   script:
     - mkdir build-oss-fuzz
-    - export LSAN_OPTIONS=suppressions=scripts/oss-fuzz/lsan_suppressions.txt
+    - export LSAN_OPTIONS=suppressions=scripts/lsan_suppressions.txt
     - CC="clang" CXX="clang++" CFLAGS="-fsanitize=address"
       ./scripts/oss-fuzz/build.sh
     - export ASAN_OPTIONS="fast_unwind_on_malloc=0"
scripts/lsan_suppressions.txt renamed from scripts/oss-fuzz/lsan_suppressions.txt +0 -0
similarity index 100%
rename from scripts/oss-fuzz/lsan_suppressions.txt
rename to scripts/lsan_suppressions.txt
tests/docker/test-fuzz +1 -1
index 7e506ae1f6..d2bdc8afba 100755
@@ -18,7 +18,7 @@ cd "$BUILD_DIR"
 cp -a $QEMU_SRC .
 cd src
 mkdir build-oss-fuzz
-export LSAN_OPTIONS=suppressions=scripts/oss-fuzz/lsan_suppressions.txt
+export LSAN_OPTIONS=suppressions=scripts/lsan_suppressions.txt
 env CC="clang" CXX="clang++" CFLAGS="-fsanitize=address" ./scripts/oss-fuzz/build.sh
 export ASAN_OPTIONS="fast_unwind_on_malloc=0"
 for fuzzer in $(find ./build-oss-fuzz/DEST_DIR/ -executable -type f | grep -v slirp); do
Signature · 1 line
2.43.0
Reviewed-by on v2 1/9
#2 lore raw

Re: [PATCH v2 1/9] scripts: Move lsan_suppressions.txt out of oss-fuzz subdir

Quoted 12 lines · Peter Maydell

The oss-fuzz code uses an lsan_suppressions file to suppress certain leak-sanitizer cases that are known issues or not our code's bug. This is useful more widely than just for the fuzzer harness: if you want to build QEMU with the leak sanitizer enabled and run 'make check' then you will want to suppress some bogus leak reports.

Move the file up a directory.

---
v2: just move the file so we keep git log --follow happy; fix up reference in tests/docker/test-fuzz

Quoted 6 lines
---
  .gitlab-ci.d/buildtest.yml                   | 2 +-
  scripts/{oss-fuzz => }/lsan_suppressions.txt | 0
  tests/docker/test-fuzz                       | 2 +-
  3 files changed, 2 insertions(+), 2 deletions(-)

rename scripts/{oss-fuzz => }/lsan_suppressions.txt (100%)

Reviewed-by on v2 1/9
#3 lore raw

Re: [PATCH v2 1/9] scripts: Move lsan_suppressions.txt out of oss-fuzz subdir

In reply to Peter Maydell
Quoted 12 lines · Peter Maydell

The oss-fuzz code uses an lsan_suppressions file to suppress certain leak-sanitizer cases that are known issues or not our code's bug. This is useful more widely than just for the fuzzer harness: if you want to build QEMU with the leak sanitizer enabled and run 'make check' then you will want to suppress some bogus leak reports.

Move the file up a directory.

---
v2: just move the file so we keep git log --follow happy; fix up reference in tests/docker/test-fuzz

Quoted 6 lines
---
  .gitlab-ci.d/buildtest.yml                   | 2 +-
  scripts/{oss-fuzz => }/lsan_suppressions.txt | 0
  tests/docker/test-fuzz                       | 2 +-
  3 files changed, 2 insertions(+), 2 deletions(-)

rename scripts/{oss-fuzz => }/lsan_suppressions.txt (100%)

#4 lore raw

PATCH v2 2/9 scripts/lsan_suppressions.txt: Add more leaks

In reply to Peter Maydell

Running "make check" with the clang leak sanitizer reveals some leak reports which are either not our problem or else not a leak which is worth our time to fix. Add some suppressions for these.

While we're touching the file, add the usual SPDX header and a comment explaining how to use it.

---
v2: header comment moved here from patch 1; dropped walk_path suppression since Fabiano has a fix for it

---
 scripts/lsan_suppressions.txt | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
scripts/lsan_suppressions.txt +21 -0
index 7d90c280d0..f88bbab18b 100644
@@ -1,5 +1,26 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+# This is a set of suppressions for LeakSanitizer; you can use it by setting
+#   LSAN_OPTIONS="suppressions=/path/to/scripts/lsan_suppressions.txt"
+# when running a QEMU built with the leak-sanitizer.
+
 # The tcmalloc on Fedora37 confuses things
 leak:/lib64/libtcmalloc_minimal.so.4
 
 # libxkbcommon also leaks in qemu-keymap
 leak:/lib64/libxkbcommon.so.0
+
+# g_set_user_dirs() deliberately leaks the previous cached g_get_user_*
+# values. This is documented in upstream glib's valgrind-format
+# suppression file:
+# https://github.com/GNOME/glib/blob/main/tools/glib.supp
+# This avoids false positive leak reports for the qga-ssh-test.
+leak:g_set_user_dirs
+
+# qemu_irq_intercept_in is only used by the qtest harness, and
+# its API inherently involves a leak.
+# While we could keep track of the old IRQ data structure
+# in order to free it, it doesn't seem very important to fix
+# since it is only used by the qtest test harness.
+# Just ignore the leak, at least for the moment.
+leak:qemu_irq_intercept_in
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v2 2/9] scripts/lsan_suppressions.txt: Add more leaks

Hi Peter

Quoted 9 lines · Peter Maydell

Running "make check" with the clang leak sanitizer reveals some leak reports which are either not our problem or else not a leak which is worth our time to fix. Add some suppressions for these.

While we're touching the file, add the usual SPDX header and a comment explaining how to use it.

Why not wire it to meson.build tests? something like this:

+lsan_suppress = 'LSAN_OPTIONS=suppressions=' + \
+ meson.project_source_root() / 'scripts/lsan_suppressions.txt'
add_test_setup('quick', exclude_suites: ['slow', 'thorough'], is_default: true,
- env: ['RUST_BACKTRACE=1'])
+ env: ['RUST_BACKTRACE=1', lsan_suppress])
add_test_setup('slow', exclude_suites: ['thorough'],
- env: ['G_TEST_SLOW=1', 'SPEED=slow', 'RUST_BACKTRACE=1'])
+ env: ['G_TEST_SLOW=1', 'SPEED=slow',
'RUST_BACKTRACE=1', lsan_suppress])
add_test_setup('thorough',
- env: ['G_TEST_SLOW=1', 'SPEED=thorough', 'RUST_BACKTRACE=1'])
+ env: ['G_TEST_SLOW=1', 'SPEED=thorough',
'RUST_BACKTRACE=1', lsan_suppress])

Quoted 44 lines

---
v2: header comment moved here from patch 1; dropped walk_path suppression since Fabiano has a fix for it

---
 scripts/lsan_suppressions.txt | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
scripts/lsan_suppressions.txt +21 -1
index 7d90c280d0..f88bbab18b 100644
@@ -1,5 +1,26 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+# This is a set of suppressions for LeakSanitizer; you can use it by setting
+#   LSAN_OPTIONS="suppressions=/path/to/scripts/lsan_suppressions.txt"
+# when running a QEMU built with the leak-sanitizer.
+
 # The tcmalloc on Fedora37 confuses things
 leak:/lib64/libtcmalloc_minimal.so.4
 
 # libxkbcommon also leaks in qemu-keymap
 leak:/lib64/libxkbcommon.so.0
+
+# g_set_user_dirs() deliberately leaks the previous cached g_get_user_*
+# values. This is documented in upstream glib's valgrind-format
+# suppression file:
+# https://github.com/GNOME/glib/blob/main/tools/glib.supp
+# This avoids false positive leak reports for the qga-ssh-test.
+leak:g_set_user_dirs
+
+# qemu_irq_intercept_in is only used by the qtest harness, and
+# its API inherently involves a leak.
+# While we could keep track of the old IRQ data structure
+# in order to free it, it doesn't seem very important to fix
+# since it is only used by the qtest test harness.
+# Just ignore the leak, at least for the moment.
+leak:qemu_irq_intercept_in
--

2.43.0

Signature · 1 line
Marc-André Lureau
#6 lore raw

Re: [PATCH v2 2/9] scripts/lsan_suppressions.txt: Add more leaks

Quoted 16 lines · Marc-André Lureau

Hi Peter

Quoted 9 lines · Peter Maydell

Running "make check" with the clang leak sanitizer reveals some leak reports which are either not our problem or else not a leak which is worth our time to fix. Add some suppressions for these.

While we're touching the file, add the usual SPDX header and a comment explaining how to use it.

Why not wire it to meson.build tests? something like this:

Review

Quoted 2 lines
(quoted diff) +2 -0
+lsan_suppress = 'LSAN_OPTIONS=suppressions=' + \
+                meson.project_source_root() / 'scripts/lsan_suppressions.txt'

Mostly because I didn't want to do more here than I had to.

But also:
(1) this isn't the only thing you might want to set in LSAN_OPTIONS:
you probably also want "fast_unwind_on_malloc=0" and maybe to set other ones like abort_on_error or print_stacktrace

(2) as a user you need to set all this stuff anyway if you want
to check a single test run, so hiding half of it in the meson.build files wouldn't really gain me anything

(3) meson itself sets sanitizer option flags if the user hasn't:
see pyvenv/lib/python3.12/site-packages/mesonbuild/mtest.py or equivalent in your build/ directory. At the moment it doesn't set anything in LSAN_OPTIONS, but it might in future...

If we can get to the point of having a CI job which builds with the leak sanitizer then I vaguely asusmed we'd set LSAN_OPTIONS in the gitlab CI yaml, as we do in the build-oss-fuzz template.

thanks
-- PMM

#7 lore raw

PATCH v2 3/9 hw/arm/aspeed_gpio: Don't leak string in aspeed_gpio_init()

In reply to Peter Maydell

We allocate the string for the GPIO property name, but never free it. Use g_autofree to avoid this.

---
 hw/gpio/aspeed_gpio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/gpio/aspeed_gpio.c +1 -1
index d9237d4360..7d0f87e90c 100644
@@ -1488,7 +1488,7 @@ static void aspeed_gpio_init(Object *obj)
     }
 
     for (int i = 0; i < agc->nr_gpio_sets; i++) {
-        char *name = g_strdup_printf("gpio-set[%d]", i);
+        g_autofree char *name = g_strdup_printf("gpio-set[%d]", i);
         object_property_add(obj, name, "uint32", aspeed_gpio_get_set,
         aspeed_gpio_set_set, NULL, NULL);
     }
Signature · 1 line
2.43.0
#8 lore raw

PATCH v2 4/9 tests/qtest/iommu-smmuv3-test: Free QPCIDevice

In reply to Peter Maydell

The QPCIDevice we get via qpci_device_foreach() is allocated memory, and we need to g_free() it after use.

This fixes asan leaks like this:

Direct leak of 64 byte(s) in 1 object(s) allocated from:

    #0 0x622a5f16913d in calloc (/home/pm215/qemu/build/arm-clang/tests/qtest/iommu-smmuv3-test+0x1d413d) (BuildId: bc598be1f4ad6d1a9a600c55aeef36108bdb6a04)
    #1 0x73ee41c0f771 in g_malloc0 (/lib/x86_64-linux-gnu/libglib-2.0.so.0+0x63771) (BuildId: 116e142b9b52c8a4dfd403e759e71ab8f95d8bb3)
    #2 0x622a5f1d4cec in qpci_device_find /home/pm215/qemu/build/arm-clang/../../tests/qtest/libqos/pci.c:82:11
    #3 0x622a5f1d4cec in qpci_device_foreach /home/pm215/qemu/build/arm-clang/../../tests/qtest/libqos/pci.c:34:19
    #4 0x622a5f23cc73 in setup_qtest_pci_device /home/pm215/qemu/build/arm-clang/../../tests/qtest/iommu-smmuv3-test.c:45:5
    #5 0x622a5f23cc73 in run_smmuv3_translation /home/pm215/qemu/build/arm-clang/../../tests/qtest/iommu-smmuv3-test.c:74:11
---
 tests/qtest/iommu-smmuv3-test.c | 1 +
 1 file changed, 1 insertion(+)
tests/qtest/iommu-smmuv3-test.c +1 -0
index cced49a9b6..dae9821b92 100644
@@ -77,6 +77,7 @@ static void run_smmuv3_translation(const QSMMUTestConfig *cfg)
     g_test_message("### SMMUv3 translation mode=%d sec_sid=%d ###",
                    cfg->trans_mode, cfg->sec_sid);
     qsmmu_run_translation_case(qts, dev, bar, VIRT_SMMU_BASE, cfg);
+    g_free(dev);
     qtest_quit(qts);
 }
 
Signature · 1 line
2.43.0
#9 lore raw

PATCH v2 5/9 chardev: Fix QIOChannel refcount

In reply to Peter Maydell

From: Fabiano Rosas <[email protected]>

The IOWatchPoll holds a reference to the iochannel while the "child" source (iwp->src) is removed from the context and freed. Freeing the source leads to the iochannel being also freed at qio_channel_fd_source_finalize().

Later, io_watch_poll_prepare() tries to create another source with the same iochannel and hits an use after free:

==8241==ERROR: AddressSanitizer: heap-use-after-free on address 0x514000000040 READ of size 8 at 0x514000000040 thread T2

    #0 0x561c2d272fcd in object_get_class  ../qom/object.c:1043:17
    #1 0x561c2d338f84 in QIO_CHANNEL_GET_CLASS  include/io/channel.h:29:1
    #2 0x561c2d33b26f in qio_channel_create_watch  ../io/channel.c:388:30
    #3 0x561c2d2f0993 in io_watch_poll_prepare  ../chardev/char-io.c:65:20
    ...

0x514000000040 is located 0 bytes inside of 392-byte region [0x514000000040,0x5140000001c8) freed by thread T2 here:

    #0 0x561c2d2319a5 in free
    #1 0x7fb2c0926638 in g_free
    #2 0x561c2d276507 in object_finalize  ../qom/object.c:734:9
    #3 0x561c2d271d0d in object_unref  ../qom/object.c:1231:9
    #4 0x561c2d32ef1d in qio_channel_fd_source_finalize  ../io/channel-watch.c:95:5
    #5 0x7fb2c091d124 in g_source_unref_internal ../glib/gmain.c:2298
    #6 0x561c2d2f0b6c in io_watch_poll_prepare  ../chardev/char-io.c:71:9
    ...

previously allocated by thread T3 (connect) here:

    #0 0x561c2d231c69 in malloc
    #1 0x7fb2c0926518 in g_malloc
    #2 0x561c2d27246e in object_new_with_type  ../qom/object.c:767:15
    #3 0x561c2d272530 in object_new  ../qom/object.c:789:12
    #4 0x561c2d320193 in qio_channel_socket_new  ../io/channel-socket.c:64:31
    #5 0x561c2d308013 in tcp_chr_connect_client_async  ../chardev/char-socket.c:1181:12
    #6 0x561c2d3002e7 in qmp_chardev_open_socket_client  ../chardev/char-socket.c:1281:9
    ...

Fix the issue by incrementing the iochannel reference count when the IOWatchPoll takes a reference and decrementing when it is finalized.

---
 chardev/char-io.c | 5 +++++
 1 file changed, 5 insertions(+)
chardev/char-io.c +5 -0
index beac5cd245..0e5ba316ed 100644
@@ -88,6 +88,9 @@ static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
 static void io_watch_poll_finalize(GSource *source)
 {
     IOWatchPoll *iwp = io_watch_poll_from_source(source);
+
+    object_unref(OBJECT(iwp->ioc));
+
     if (iwp->src) {
         g_source_destroy(iwp->src);
         g_source_unref(iwp->src);
@@ -117,6 +120,8 @@ GSource *io_add_watch_poll(Chardev *chr,
     iwp->fd_can_read = fd_can_read;
     iwp->opaque = user_data;
     iwp->ioc = ioc;
+    object_ref(OBJECT(iwp->ioc));
+
     iwp->fd_read = (GSourceFunc) fd_read;
     iwp->src = NULL;
     iwp->context = context;
Signature · 1 line
2.43.0
#10 lore raw

PATCH v2 6/9 chardev: Don't attempt to unregister yank function more than once

In reply to Peter Maydell

From: Fabiano Rosas <[email protected]>

tcp_chr_free_connection() can be called multiple times in succession, in which case the yank function will get as argument a NULL s->sioc that has been cleared by the previous tcp_chr_free_connection() call.

This leads to an abort() at yank_unregister_function().

 #0  __GI_raise (sig=6) at ../sysdeps/unix/sysv/linux/raise.c:51
 #1  __GI_abort () at abort.c:79
 #2  qtest_check_status (s=0x513000005600) at ../tests/qtest/libqtest.c:209
 #3  qtest_wait_qemu (s=0x513000005600) at ../tests/qtest/libqtest.c:273
 #4  qtest_kill_qemu (s=0x513000005600) at ../tests/qtest/libqtest.c:285
 #5  kill_qemu_hook_func (s=0x513000005600) at ../tests/qtest/libqtest.c:294
 #6  g_hook_list_invoke (hook_list=0x55ea9cc750c0 <abrt_hooks>, may_recurse=0) at ../glib/ghook.c:534
 #7  sigabrt_handler (signo=6) at ../tests/qtest/libqtest.c:299
 #8  <signal handler called>
 #9  __GI_raise (sig=6) at ../sysdeps/unix/sysv/linux/raise.c:51
 #10 __GI_abort () at abort.c:79
 #11 yank_unregister_function (instance=0x7fb26f2ea9a0,
     func=0x55ea9bcc0a10 <char_socket_yank_iochannel>, opaque=0x0) at
     ../util/yank.c:151
 #12 tcp_chr_free_connection (chr=0x51300000ffc0) at ../chardev/char-socket.c:385
 #13 tcp_chr_disconnect_locked (chr=0x51300000ffc0) at ../chardev/char-socket.c:477
 #14 tcp_chr_disconnect (chr=0x51300000ffc0) at ../chardev/char-socket.c:495
 #15 tcp_chr_hup (channel=0x514000000040, cond=G_IO_HUP, opaque=0x51300000ffc0) at ../chardev/char-socket.c:536
 #16 qio_channel_fd_source_dispatch (source=0x50c0000b5fc0, callback=0x55ea9bcd6770 <tcp_chr_hup>,
     user_data=0x51300000ffc0) at ../io/channel-watch.c:84
 #17 g_main_dispatch (context=0x50f000000040) at ../glib/gmain.c:3381
 #18 g_main_context_dispatch (context=context@entry=0x50f000000040) at ../glib/gmain.c:4099
 #19 g_main_context_iterate (context=0x50f000000040, block=block@entry=1, dispatch=dispatch@entry=1,
     self=<optimized out>) at ../glib/gmain.c:4175

#20 g_main_loop_run (loop=0x502000055690) at ../glib/gmain.c:4373

Commit ebae6477dc ("chardev: check if the chardev is registered for yanking") seems to have encountered a similar issue, but checking s->registered_yank is not a complete solution because that flag pertains to the yank instance, not to each individual function.

Skip the yank_unregister_function() in case s->sioc is already NULL, which indicates the last yank function was already removed.

---
 chardev/char-socket.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
chardev/char-socket.c +2 -1
index 5adeb90865..4e82915cf1 100644
@@ -370,7 +370,8 @@ static void tcp_chr_free_connection(Chardev *chr)
 
     tcp_chr_set_msgfds(chr, NULL, 0);
     remove_fd_in_watch(chr);
-    if (s->registered_yank &&
+
+    if (s->registered_yank && s->sioc &&
         (s->state == TCP_CHARDEV_STATE_CONNECTING
         || s->state == TCP_CHARDEV_STATE_CONNECTED)) {
         yank_unregister_function(CHARDEV_YANK_INSTANCE(chr->label),
Signature · 1 line
2.43.0
#11 lore raw

PATCH v2 7/9 chardev: Consolidate yank registration

In reply to Peter Maydell

From: Fabiano Rosas <[email protected]>

There's currently five places where the yank function is being registered and they all come right before tcp_chr_new_client(). Fold them into it.

---
 chardev/char-socket.c | 31 ++++++-------------------------
 1 file changed, 6 insertions(+), 25 deletions(-)
chardev/char-socket.c +6 -25
index 4e82915cf1..e064b105c5 100644
@@ -905,6 +905,12 @@ static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc)
     s->sioc = sioc;
     object_ref(OBJECT(sioc));
 
+    if (s->registered_yank) {
+        yank_register_function(CHARDEV_YANK_INSTANCE(chr->label),
+                               char_socket_yank_iochannel,
+                               QIO_CHANNEL(sioc));
+    }
+
     if (s->do_nodelay) {
         qio_channel_set_delay(s->ioc, false);
     }
@@ -943,11 +949,6 @@ static int tcp_chr_add_client(Chardev *chr, int fd)
     }
     tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
     tcp_chr_set_client_ioc_name(chr, sioc);
-    if (s->registered_yank) {
-        yank_register_function(CHARDEV_YANK_INSTANCE(chr->label),
-                               char_socket_yank_iochannel,
-                               QIO_CHANNEL(sioc));
-    }
     ret = tcp_chr_new_client(chr, sioc);
     object_unref(OBJECT(sioc));
     return ret;
@@ -962,11 +963,6 @@ static void tcp_chr_accept(QIONetListener *listener,
 
     tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
     tcp_chr_set_client_ioc_name(chr, cioc);
-    if (s->registered_yank) {
-        yank_register_function(CHARDEV_YANK_INSTANCE(chr->label),
-                               char_socket_yank_iochannel,
-                               QIO_CHANNEL(cioc));
-    }
     tcp_chr_new_client(chr, cioc);
 }
 
@@ -982,11 +978,6 @@ static int tcp_chr_connect_client_sync(Chardev *chr, Error **errp)
         object_unref(OBJECT(sioc));
         return -1;
     }
-    if (s->registered_yank) {
-        yank_register_function(CHARDEV_YANK_INSTANCE(chr->label),
-                               char_socket_yank_iochannel,
-                               QIO_CHANNEL(sioc));
-    }
     tcp_chr_new_client(chr, sioc);
     object_unref(OBJECT(sioc));
     return 0;
@@ -1002,11 +993,6 @@ static void tcp_chr_accept_server_sync(Chardev *chr)
     tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
     sioc = qio_net_listener_wait_client(s->listener);
     tcp_chr_set_client_ioc_name(chr, sioc);
-    if (s->registered_yank) {
-        yank_register_function(CHARDEV_YANK_INSTANCE(chr->label),
-                               char_socket_yank_iochannel,
-                               QIO_CHANNEL(sioc));
-    }
     tcp_chr_new_client(chr, sioc);
     object_unref(OBJECT(sioc));
 }
@@ -1180,11 +1166,6 @@ static void tcp_chr_connect_client_async(Chardev *chr)
     tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
     sioc = qio_channel_socket_new();
     tcp_chr_set_client_ioc_name(chr, sioc);
-    if (s->registered_yank) {
-        yank_register_function(CHARDEV_YANK_INSTANCE(chr->label),
-                               char_socket_yank_iochannel,
-                               QIO_CHANNEL(sioc));
-    }
     /*
      * Normally code would use the qio_channel_socket_connect_async
      * method which uses a QIOTask + qio_task_set_error internally
Signature · 1 line
2.43.0
#12 lore raw

PATCH v2 8/9 tests/qtest/test-x86-cpuid-compat: Free allocated memory

In reply to Peter Maydell

From: Fabiano Rosas <[email protected]>

Free the test arguments after test execution.

---
 tests/qtest/test-x86-cpuid-compat.c | 4 ++++
 1 file changed, 4 insertions(+)
tests/qtest/test-x86-cpuid-compat.c +4 -0
index 5e0547e81b..b2d884054a 100644
@@ -65,6 +65,8 @@ static void test_cpuid_prop(const void *data)
 
     qobject_unref(value);
     g_free(path);
+    g_free((void *)args->cmdline);
+    g_free((void *)data);
 }
 
 static void add_cpuid_test(const char *name, const char *cpu,
@@ -161,6 +163,8 @@ static void test_feature_flag(const void *data)
     qobject_unref(present);
     qobject_unref(filtered);
     g_free(path);
+    g_free((void *)args->cmdline);
+    g_free((void *)data);
 }
 
 /*
Signature · 1 line
2.43.0
#13 lore raw

PATCH v2 9/9 tests/qtest/qos-test: Plug a couple of leaks

In reply to Peter Maydell

From: Fabiano Rosas <[email protected]>

The walk_path() function of qos-test.c, which walks the graph and adds tests to the test suite uses GLib's g_test_add_data_func_full() function:

g_test_add_data_func_full (const char *testpath,

                           gpointer        test_data,
                           GTestDataFunc   test_func,
                           GDestroyNotify  data_free_func)

Despite GLib's documentation stating that @data_free_func is a destructor for @test_data, this is not the case. The destructor is supposed to be paired with a constructor, which GLib only accepts via g_test_create_case().

Providing externally allocated data plus a destructor function only works if the test is guaranteed to execute, otherwise the test_data is never deallocated.

Due to how subprocessess are implemented in qos-test, each test gets added twice and an extra test gets added per subprocess. In a regular run, the extra subprocess will not be executed and in a single test run (-p), none of the other tests will be executed (+1 per subprocess), leaking 'path_vec' and 'subprocess_path'.

Fix this by storing all the path vectors in a list and freeing them all at the end of the program (including subprocess invocations) and moving the allocation of 'subprocess_path' into run_one_subprocess().

While here add some documentation explaining why the graph needs to be walked twice and tests re-added.

[PMM: rebased; rewrote the comment in main() a bit to account
for the if (g_test_subprocess()) block it was previously inside no longer being present. ]

---
 tests/qtest/qos-test.c | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)
tests/qtest/qos-test.c +26 -10
index 00f39f33f6..50fa0ceef3 100644
@@ -31,6 +31,7 @@
 #include "libqos/qos_external.h"
 
 static char *old_path;
+static GSList *path_vecs;
 
 
 /**
@@ -182,11 +183,16 @@ static void run_one_test(const void *arg)
 
 static void subprocess_run_one_test(const void *arg)
 {
-    const gchar *path = arg;
-    g_test_trap_subprocess(path, 180 * G_USEC_PER_SEC,
+    char **path_vec = (char **) arg;
+    gchar *path = g_strjoinv("/", path_vec + 1);
+    gchar *subprocess_path = g_strdup_printf("/%s/subprocess", path);
+
+    g_test_trap_subprocess(subprocess_path, 180 * G_USEC_PER_SEC,
                            G_TEST_SUBPROCESS_INHERIT_STDOUT |
                            G_TEST_SUBPROCESS_INHERIT_STDERR);
     g_test_trap_assert_passed();
+    g_free(path);
+    g_free(subprocess_path);
 }
 
 static void destroy_pathv(void *arg)
@@ -238,6 +244,7 @@ static void walk_path(QOSGraphNode *orig_path, int len)
     GString *cmd_line = g_string_new("");
     GString *cmd_line2 = g_string_new("");
 
+    path_vecs = g_slist_append(path_vecs, path_vec);
     path = qos_graph_get_node(node_name); /* root */
     node_name = qos_graph_edge_get_dest(path->path_edge); /* machine name */
 
@@ -297,15 +304,15 @@ static void walk_path(QOSGraphNode *orig_path, int len)
     path_vec[0] = g_string_free(cmd_line, false);
 
     if (path->u.test.subprocess) {
-        gchar *subprocess_path = g_strdup_printf("/%s/%s/subprocess",
-                                                 qtest_get_arch(), path_str);
-        qtest_add_data_func_full(path_str, subprocess_path,
-                                 subprocess_run_one_test, g_free);
-        g_test_add_data_func_full(subprocess_path, path_vec,
-                                  run_one_test, destroy_pathv);
+        gchar *subprocess_path = g_strdup_printf("%s/%s", path_str,
+                                                 "subprocess");
+
+        qtest_add_data_func(path_str, path_vec, subprocess_run_one_test);
+        qtest_add_data_func(subprocess_path, path_vec, run_one_test);
+
+        g_free(subprocess_path);
     } else {
-        qtest_add_data_func_full(path_str, path_vec,
-                                 run_one_test, destroy_pathv);
+        qtest_add_data_func(path_str, path_vec, run_one_test);
     }
 
     g_free(path_str);
@@ -340,6 +347,14 @@ int main(int argc, char **argv, char** envp)
     module_call_init(MODULE_INIT_LIBQOS);
     qos_set_machines_devices_available();
 
+    /*
+     * Even if this invocation was done to run a single test in a
+     * subprocess (i.e. g_test_subprocess() is true), gtester doesn't
+     * expose the test name, so w still need to execute the whole
+     * thing as normal, including walking the QOS graph to add all
+     * the tests, in order for g_test_run() to find the one /subprocess
+     * test that it is going to execute.
+     */
     qos_graph_foreach_test_path(walk_path);
     if (g_test_verbose()) {
         qos_dump_graph();
@@ -348,5 +363,6 @@ int main(int argc, char **argv, char** envp)
     qtest_end();
     qos_graph_destroy();
     g_free(old_path);
+    g_slist_free_full(path_vecs, (GDestroyNotify)destroy_pathv);
     return 0;
 }
Signature · 1 line
2.43.0
#14 lore raw
In reply to Peter Maydell

Since these patches have all been reviewed, I'm going to take them into target-arm.next. Let me know if you'd prefer something else or if anybody wants more time to review.

thanks
-- PMM

Quoted 45 lines · Peter Maydell

This patchset fixes a couple of minor memory leaks I found by running "make check" for the arm targets with the clang leak sanitizer enabled. Version 2 also includes some patches by Fabiano from last year that fell through the cracks; I've rebased them and included them here.

Changes in v2:
* patch 1 now just moves the file to keep 'git log --follow' happy
* patch 1 fixes the filename in tests/docker/test-fuzz
* patch 2 has the SPDX/doc comment that were in patch 1 before
* patch 2 drops the walk_path suppression as one of Fabiano's patches fixes this leak properly
* patches 5-9 are from Fabiano; most were reviewed last year

Patches still needing review: just patch 1.

thanks
-- PMM

Fabiano Rosas (5):
chardev: Fix QIOChannel refcount
chardev: Don't attempt to unregister yank function more than once
chardev: Consolidate yank registration tests/qtest/test-x86-cpuid-compat: Free allocated memory tests/qtest/qos-test: Plug a couple of leaks

Peter Maydell (4):
scripts: Move lsan_suppressions.txt out of oss-fuzz subdir scripts/lsan_suppressions.txt: Add more leaks hw/arm/aspeed_gpio: Don't leak string in aspeed_gpio_init() tests/qtest/iommu-smmuv3-test: Free QPCIDevice

 .gitlab-ci.d/buildtest.yml             |  2 +-
 chardev/char-io.c                      |  5 ++++
 chardev/char-socket.c                  | 34 ++++++------------------
 hw/gpio/aspeed_gpio.c                  |  2 +-
 scripts/lsan_suppressions.txt          | 26 +++++++++++++++++++
 scripts/oss-fuzz/lsan_suppressions.txt |  5 ----
 tests/docker/test-fuzz                 |  2 +-
 tests/qtest/iommu-smmuv3-test.c        |  1 +
 tests/qtest/qos-test.c                 | 36 +++++++++++++++++++-------
 tests/qtest/test-x86-cpuid-compat.c    |  4 +++
 10 files changed, 73 insertions(+), 44 deletions(-)
 create mode 100644 scripts/lsan_suppressions.txt
 delete mode 100644 scripts/oss-fuzz/lsan_suppressions.txt