From 7da32d9655a074995de29fc6c1ac7bcd5aa6bfe6 Mon Sep 17 00:00:00 2001
From: imxcc <xingchaochao@huawei.com>
Date: Fri, 9 Jul 2021 15:26:08 +0800
Subject: [PATCH] tests: fix stat mocking with Fedora rawhide
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

GLibC has a really complicated way of dealing with the 'stat' function
historically, which means our mocks in turn have to look at four
different possible functions to replace, stat, stat64, __xstat,
__xstat64.

In Fedora 33 and earlier:

 - libvirt.so links to __xstat64
 - libc.so library exports stat, stat64, __xstat, __xstat64
 - sys/stat.h header exposes stat and __xstat

In Fedora 34 rawhide:

 - libvirt.so links to stat64
 - libc.so library exports stat, stat64, __xstat, __xstat64
 - sys/stat.h header exposes stat

Historically we only looked at the exported symbols from libc.so to
decide which to mock.

In F34 though we must not consider __xstat / __xstat64 though because
they only existance for binary compatibility. Newly built binaries
won't reference them.

Thus we must introduce a header file check into our logic for deciding
which symbol to mock. We must ignore the __xstat / __xstat64 symbols
if they don't appear in the sys/stat.h header, even if they appear
in libc.so

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Signed-off-by: Peng Liang <liangpeng10@huawei.com>
Signed-off-by: imxcc <xingchaochao@huawei.com>
---
 configure.ac               |  1 +
 tests/virmockstathelpers.c | 58 ++++++++++++++++++++++----------------
 2 files changed, 35 insertions(+), 24 deletions(-)

diff --git a/configure.ac b/configure.ac
index e565437062..cb62e5aac8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -396,6 +396,7 @@ AC_CHECK_HEADERS([\
 dnl Check whether endian provides handy macros.
 AC_CHECK_DECLS([htole64], [], [], [[#include <endian.h>]])
 AC_CHECK_FUNCS([stat stat64 __xstat __xstat64 lstat lstat64 __lxstat __lxstat64])
+AC_CHECK_DECLS([stat, stat64, __xstat, __xstat64, lstat, lstat64, __lxstat, __lxstat64], [], [], [[#include <sys/stat.h>]])
 
 AC_CHECK_TYPE([struct ifreq],
   [AC_DEFINE([HAVE_STRUCT_IFREQ],[1],
diff --git a/tests/virmockstathelpers.c b/tests/virmockstathelpers.c
index ba26e7bd45..5118ffc731 100644
--- a/tests/virmockstathelpers.c
+++ b/tests/virmockstathelpers.c
@@ -69,35 +69,45 @@
  *  - If __xstat & __xstat64 exist, then stat & stat64 will not exist
  *    as symbols in the library, so the latter should not be mocked.
  *
+ *  - If __xstat exists in the library, but not the header than it
+ *    it is just there for binary back compat and should not be
+ *    mocked
+ *
  * The same all applies to lstat()
  */
 
 
+#if !HAVE_DECL___XSTAT
+# if defined(HAVE_STAT) && !defined(_FILE_OFFSET_BITS) || defined(__APPLE__)
+#  define MOCK_STAT
+# endif
+# if defined(HAVE_STAT64)
+#  define MOCK_STAT64
+# endif
+#else /* HAVE_DECL___XSTAT */
+# if defined(HAVE___XSTAT) && !defined(_FILE_OFFSET_BITS)
+#  define MOCK___XSTAT
+# endif
+# if defined(HAVE___XSTAT64)
+#  define MOCK___XSTAT64
+# endif
+#endif /* HAVE_DECL___XSTAT */
 
-#if defined(HAVE_STAT) && !defined(HAVE___XSTAT) && !defined(_FILE_OFFSET_BITS)
-# define MOCK_STAT
-#endif
-#if defined(HAVE_STAT64) && !defined(HAVE___XSTAT64)
-# define MOCK_STAT64
-#endif
-#if defined(HAVE___XSTAT) && !defined(_FILE_OFFSET_BITS)
-# define MOCK___XSTAT
-#endif
-#if defined(HAVE___XSTAT64)
-# define MOCK___XSTAT64
-#endif
-#if defined(HAVE_LSTAT) && !defined(HAVE___LXSTAT) && !defined(_FILE_OFFSET_BITS)
-# define MOCK_LSTAT
-#endif
-#if defined(HAVE_LSTAT64) && !defined(HAVE___LXSTAT64)
-# define MOCK_LSTAT64
-#endif
-#if defined(HAVE___LXSTAT) && !defined(_FILE_OFFSET_BITS)
-# define MOCK___LXSTAT
-#endif
-#if defined(HAVE___LXSTAT64)
-# define MOCK___LXSTAT64
-#endif
+#if !HAVE_DECL___LXSTAT
+# if defined(HAVE_LSTAT) && !defined(_FILE_OFFSET_BITS) || defined(__APPLE__)
+#  define MOCK_LSTAT
+# endif
+# if defined(HAVE_LSTAT64)
+#  define MOCK_LSTAT64
+# endif
+#else /* HAVE_DECL___LXSTAT */
+# if defined(HAVE___LXSTAT) && !defined(_FILE_OFFSET_BITS)
+#  define MOCK___LXSTAT
+# endif
+# if defined(HAVE___LXSTAT64)
+#  define MOCK___LXSTAT64
+# endif
+#endif /* HAVE_DECL___LXSTAT */
 
 #ifdef MOCK_STAT
 static int (*real_stat)(const char *path, struct stat *sb);
-- 
2.27.0

