[PATCH v2 2/6] tests: Fix mock chaining on macOS

Roman Bolshakov posted 6 patches 5 years, 2 months ago
[PATCH v2 2/6] tests: Fix mock chaining on macOS
Posted by Roman Bolshakov 5 years, 2 months ago
Some tests in qemuxml2argvtest need opendir() from virpcimock, others
need opendir() from virfilewrapper.

But as of now, only opendir() from virpcimock has an effect.
real_opendir in virpcimock has a pointer to opendir$INODE64 in
libsystem_kernel.dylib instead of pointing to opendir$INODE64 in
qemuxml2argvtest (from virfilewrapper). And because the second one is
never used, tests that rely on prefixes added by virFileWrapperAddPrefix
fail.

That can be fixed if dlsym(3) is asked explicitly to search symbols in
main executable with RTLD_MAIN_ONLY before going to other dylibs.
Existing RTLD_NEXT handle results into libsystem_kernel.dylib being
searched before main executable.

Signed-off-by: Roman Bolshakov <r.bolshakov@yadro.com>
---
 tests/virmock.h    | 13 +++++++++++++
 tests/virpcimock.c |  1 +
 2 files changed, 14 insertions(+)

diff --git a/tests/virmock.h b/tests/virmock.h
index dea5feb80f..5250e733a1 100644
--- a/tests/virmock.h
+++ b/tests/virmock.h
@@ -284,9 +284,21 @@
     static void (*real_##name)(void); \
     void wrap_##name(void)
 
+#if defined(VIR_MOCK_LOOKUP_MAIN) && defined(__APPLE__)
+# define VIR_MOCK_REAL_INIT_MAIN(name, alias) \
+    do { \
+        if (real_##name == NULL) { \
+            real_##name = dlsym(RTLD_MAIN_ONLY, alias); \
+        } \
+    } while (0)
+#else
+# define VIR_MOCK_REAL_INIT_MAIN(name, alias) \
+    do {} while (0)
+#endif
 
 #define VIR_MOCK_REAL_INIT(name) \
     do { \
+        VIR_MOCK_REAL_INIT_MAIN(name, #name); \
         if (real_##name == NULL && \
             !(real_##name = dlsym(RTLD_NEXT, \
                                   #name))) { \
@@ -297,6 +309,7 @@
 
 #define VIR_MOCK_REAL_INIT_ALIASED(name, alias) \
     do { \
+        VIR_MOCK_REAL_INIT_MAIN(name, alias); \
         if (real_##name == NULL && \
             !(real_##name = dlsym(RTLD_NEXT, \
                                   alias))) { \
diff --git a/tests/virpcimock.c b/tests/virpcimock.c
index 686f894e99..4aa96cae08 100644
--- a/tests/virpcimock.c
+++ b/tests/virpcimock.c
@@ -19,6 +19,7 @@
 #include <config.h>
 
 #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)
+# define VIR_MOCK_LOOKUP_MAIN
 # include "virmock.h"
 # include <unistd.h>
 # include <fcntl.h>
-- 
2.29.2


Re: [PATCH v2 2/6] tests: Fix mock chaining on macOS
Posted by Michal Prívozník 5 years, 2 months ago
On 11/23/20 11:10 PM, Roman Bolshakov wrote:
> Some tests in qemuxml2argvtest need opendir() from virpcimock, others
> need opendir() from virfilewrapper.
> 
> But as of now, only opendir() from virpcimock has an effect.
> real_opendir in virpcimock has a pointer to opendir$INODE64 in
> libsystem_kernel.dylib instead of pointing to opendir$INODE64 in
> qemuxml2argvtest (from virfilewrapper). And because the second one is
> never used, tests that rely on prefixes added by virFileWrapperAddPrefix
> fail.
> 
> That can be fixed if dlsym(3) is asked explicitly to search symbols in
> main executable with RTLD_MAIN_ONLY before going to other dylibs.
> Existing RTLD_NEXT handle results into libsystem_kernel.dylib being
> searched before main executable.
> 
> Signed-off-by: Roman Bolshakov <r.bolshakov@yadro.com>
> ---
>   tests/virmock.h    | 13 +++++++++++++
>   tests/virpcimock.c |  1 +
>   2 files changed, 14 insertions(+)

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

Michal