On Tue, May 14, 2024 at 01:06:18PM +0200, Michal Privoznik wrote:
I've noticed some tests fail to run under valgrind with the
following error:
$ valgrind --leak-check=full --trace-children=yes ./qemuxmlconftest
valgrind: symbol lookup error: libvirt.git/_build/tests/libdomaincapsmock.so: undefined
symbol: virQEMUCapsGet
But without valgrind the test passes just fine. While we usually
don't want to change our code just to adhere to random tools, in
this case we ought to make an exception because valgrind helps us
to detect memory leaks.
NB, the --trace-children=yes is needed whenever a test
re-executes itself, i.e. when it uses mocks. Otherwise we'd just
get (boring) result for the first invocation of main() which does
nothing more than sets up the environment and calls exec().
When running the test binary without valgrind I can see the
libtest_qemu_driver.so being loaded even after exec:
$ LD_DEBUG=libs ./qemuxmlconftest 2>&1 | grep -e libtest_qemu_driver.so -e
virQEMUCapsGet
6439: find library=libtest_qemu_driver.so [0]; searching
6439: trying file=libvirt.git/_build/tests/../src/libtest_qemu_driver.so
6439: trying
file=libvirt.git/_build/tests/glibc-hwcaps/x86-64-v3/libtest_qemu_driver.so
6439: trying
file=libvirt.git/_build/tests/glibc-hwcaps/x86-64-v2/libtest_qemu_driver.so
6439: trying file=libvirt.git/_build/tests/libtest_qemu_driver.so
6439: calling init: libvirt.git/_build/tests/libtest_qemu_driver.so
6439: find library=libtest_qemu_driver.so [0]; searching
6439: trying file=libvirt.git/_build/tests/libtest_qemu_driver.so
6439: calling init: libvirt.git/_build/tests/libtest_qemu_driver.so
6439: calling fini: libvirt.git/_build/tests/libtest_qemu_driver.so [0]
But running the same under valgrind:
$ LD_DEBUG=libs valgrind --leak-check=full --trace-children=yes ./qemuxmlconftest
2>&1 | grep -e libtest_qemu_driver.so -e virQEMUCapsGet
6515: find library=libtest_qemu_driver.so [0]; searching
6515: trying file=libvirt.git/_build/tests/../src/libtest_qemu_driver.so
6515: trying
file=libvirt.git/_build/tests/glibc-hwcaps/x86-64-v3/libtest_qemu_driver.so
6515: trying
file=libvirt.git/_build/tests/glibc-hwcaps/x86-64-v2/libtest_qemu_driver.so
6515: trying file=libvirt.git/_build/tests/libtest_qemu_driver.so
6515: calling init: libvirt.git/_build/tests/libtest_qemu_driver.so
6515: libvirt.git/_build/tests/libdomaincapsmock.so: error: symbol lookup error:
undefined symbol: virQEMUCapsGet (fatal)
valgrind: symbol lookup error: libvirt.git/_build/tests/libdomaincapsmock.so: undefined
symbol: virQEMUCapsGet
To me, it looks like valgrind forced linker to lookup symbols
"sooner", as individual libraries are loaded. But I must admit I
have no idea how valgrind does that (or if that's even valgrind's
'fault').
But fix is pretty simple: link mocks that rely on symbols from
the QEMU driver with the QEMU driver, well, its test suite
suitable version (libtest_qemu_driver.so).
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
Reviewed-by: Martin Kletzander <mkletzan(a)redhat.com>
---
This obsoletes some patches I've sent earlier:
https://lists.libvirt.org/archives/list/devel@lists.libvirt.org/message/R...
https://lists.libvirt.org/archives/list/devel@lists.libvirt.org/message/Z...
My initial fix was bothering me as I did not understand what was
happening nor why it made the valgrind happy. I still don't quite
understand what is going on, but at least this fix seems 'more correct'.
tests/meson.build | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/tests/meson.build b/tests/meson.build
index ba2d319347..2f1eda1f95 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -73,7 +73,6 @@ endif
# * deps - additional dependencies (optional, default [])
mock_libs = [
- { 'name': 'domaincapsmock' },
{ 'name': 'vircgroupmock' },
{ 'name': 'virdnsmasqmock' },
{ 'name': 'virfilecachemock' },
@@ -175,7 +174,7 @@ if conf.has('WITH_QEMU')
{ 'name': 'qemucaps2xmlmock' },
{ 'name': 'qemucapsprobemock', 'link_with': [
test_qemu_driver_lib ] },
{ 'name': 'qemucpumock' },
- { 'name': 'qemuhotplugmock', 'link_with': [
test_utils_qemu_lib, test_utils_lib ] },
+ { 'name': 'qemuhotplugmock', 'link_with': [
test_qemu_driver_lib, test_utils_qemu_lib, test_utils_lib ] },
{ 'name': 'qemuxml2argvmock' },
{ 'name': 'virhostidmock' },
]
@@ -185,6 +184,11 @@ else
test_utils_qemu_monitor_lib = []
endif
+mock_libs += [
+ # domaincapsmock has some code guarded with WITH_QEMU
+ { 'name': 'domaincapsmock', 'link_with': [ test_qemu_driver_lib
] },
+]
+
test_file_wrapper_lib = static_library(
'test_file_wrapper',
[ 'virfilewrapper.c' ],
--
2.43.2