]> Savannah Git Hosting - gnulib.git/commitdiff
Skip some tests when running under QEMU user-mode.
authorBruno Haible <bruno@clisp.org>
Sat, 28 Aug 2021 23:03:29 +0000 (01:03 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 28 Aug 2021 23:03:29 +0000 (01:03 +0200)
* tests/qemu.h: New file.
* tests/test-get-rusage-as.c: Include qemu.h.
(main): Skip the test when running under QEMU.
* tests/test-get-rusage-data.c: Include qemu.h.
(main): Skip the test when running under QEMU.
* tests/test-printf-posix2.c: Include qemu.h.
(main): Return with code 77 when running under QEMU.
* tests/test-fprintf-posix2.c: Include qemu.h.
(main): Return with code 77 when running under QEMU.
* tests/test-fprintf-posix3.c: Include qemu.h.
(main): Return with code 79 when running under QEMU.
* tests/test-fprintf-posix3.sh: Skip the test when the return code was
79.
* tests/test-dprintf-posix2.c: Include qemu.h.
(main): Return with code 79 when running under QEMU. Use return code 80
instead of 78.
* tests/test-dprintf-posix2.sh: Skip the test when the return code was
79. Update for changed return code.
* modules/get-rusage-as-tests (Files): Add qemu.h.
(Depends-on): Add stdbool.
* modules/get-rusage-data-tests (Files): Add qemu.h.
(Depends-on): Add stdbool.
* modules/fprintf-posix-tests (Files): Add qemu.h.
(Depends-on): Add stdbool.
* modules/dprintf-posix-tests (Files): Add qemu.h.
(Depends-on): Add stdbool.

14 files changed:
ChangeLog
modules/dprintf-posix-tests
modules/fprintf-posix-tests
modules/get-rusage-as-tests
modules/get-rusage-data-tests
tests/qemu.h [new file with mode: 0644]
tests/test-dprintf-posix2.c
tests/test-dprintf-posix2.sh
tests/test-fprintf-posix2.c
tests/test-fprintf-posix3.c
tests/test-fprintf-posix3.sh
tests/test-get-rusage-as.c
tests/test-get-rusage-data.c
tests/test-printf-posix2.c

index 2479ea6f5141312d7b21dd393c01eeff76d57273..12187eaf97ee4ca1d5af56bcc8932c9dceb5919b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,33 @@
+2021-08-28  Bruno Haible  <bruno@clisp.org>
+
+       Skip some tests when running under QEMU user-mode.
+       * tests/qemu.h: New file.
+       * tests/test-get-rusage-as.c: Include qemu.h.
+       (main): Skip the test when running under QEMU.
+       * tests/test-get-rusage-data.c: Include qemu.h.
+       (main): Skip the test when running under QEMU.
+       * tests/test-printf-posix2.c: Include qemu.h.
+       (main): Return with code 77 when running under QEMU.
+       * tests/test-fprintf-posix2.c: Include qemu.h.
+       (main): Return with code 77 when running under QEMU.
+       * tests/test-fprintf-posix3.c: Include qemu.h.
+       (main): Return with code 79 when running under QEMU.
+       * tests/test-fprintf-posix3.sh: Skip the test when the return code was
+       79.
+       * tests/test-dprintf-posix2.c: Include qemu.h.
+       (main): Return with code 79 when running under QEMU. Use return code 80
+       instead of 78.
+       * tests/test-dprintf-posix2.sh: Skip the test when the return code was
+       79. Update for changed return code.
+       * modules/get-rusage-as-tests (Files): Add qemu.h.
+       (Depends-on): Add stdbool.
+       * modules/get-rusage-data-tests (Files): Add qemu.h.
+       (Depends-on): Add stdbool.
+       * modules/fprintf-posix-tests (Files): Add qemu.h.
+       (Depends-on): Add stdbool.
+       * modules/dprintf-posix-tests (Files): Add qemu.h.
+       (Depends-on): Add stdbool.
+
 2021-08-28  Bruno Haible  <bruno@clisp.org>
 
        sigabbrev_np, sigdescr_np: Fix compilation error on Linux/alpha.
index 0c5a6e8cba1ec879b87f718d84e9869f2571f6f0..09ebbc06f9b46fd6d51117b0c1072113d4e9b95e 100644 (file)
@@ -5,10 +5,12 @@ tests/test-printf-posix.output
 tests/test-dprintf-posix2.sh
 tests/test-dprintf-posix2.c
 tests/infinity.h
+tests/qemu.h
 tests/signature.h
 tests/macros.h
 
 Depends-on:
+stdbool
 stdint
 get-rusage-as
 
index 258e3bfb28e62ad2d9e43fdfe27b71e74a220ff3..de0a8df7967350cae3a07407166ac0c230368bc7 100644 (file)
@@ -8,10 +8,12 @@ tests/test-fprintf-posix2.c
 tests/test-fprintf-posix3.sh
 tests/test-fprintf-posix3.c
 tests/infinity.h
+tests/qemu.h
 tests/signature.h
 tests/macros.h
 
 Depends-on:
+stdbool
 stdint
 get-rusage-as
 
index 9941009e37b7685a3d87605d171259b60c477256..67c011d712c09842eadc98b53227fe22f4a2077c 100644 (file)
@@ -1,8 +1,10 @@
 Files:
 tests/test-get-rusage-as.c
+tests/qemu.h
 tests/macros.h
 
 Depends-on:
+stdbool
 
 configure.ac:
 
index 109cce53e99bb2ea8c0b5424989c408986730567..d18d30bc234d15e33f418a4697a9b9d27eaef4ea 100644 (file)
@@ -1,9 +1,11 @@
 Files:
 tests/test-get-rusage-data.c
+tests/qemu.h
 tests/macros.h
 m4/musl.m4
 
 Depends-on:
+stdbool
 
 configure.ac:
 gl_MUSL_LIBC
diff --git a/tests/qemu.h b/tests/qemu.h
new file mode 100644 (file)
index 0000000..403b779
--- /dev/null
@@ -0,0 +1,99 @@
+/* Determine whether the current process is running under QEMU.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2021.  */
+
+#include <stdbool.h>
+#ifdef __linux__
+# include <fcntl.h>
+# include <string.h>
+# include <unistd.h>
+#endif
+
+/* This function determines whether the current process is running under QEMU
+   (user-mode).
+
+   It does so by looking at parts of the environment that QEMU does not emulate
+   100% perfectly well.
+
+   For comparison, the techniques given in the paper
+     Thomas Raffetseder, Christopher Kruegel, Engin Kirda
+     "Detecting System Emulators"
+     2007
+     https://publik.tuwien.ac.at/files/pub-inf_5317.pdf
+   apply to both the QEMU system mode and QEMU user mode.  */
+
+static bool
+is_running_under_qemu_user (void)
+{
+#ifdef __linux__
+  char buf[4096 + 1];
+  int fd;
+
+# if defined __m68k__
+  fd = open ("/proc/hardware", O_RDONLY);
+  if (fd >= 0)
+    {
+      int n = read (fd, buf, sizeof (buf) - 1);
+      close (fd);
+      if (n > 0)
+        {
+          buf[n] = '\0';
+          if (strstr (buf, "qemu") != NULL)
+            return true;
+        }
+    }
+# endif
+
+  fd = open ("/proc/cpuinfo", O_RDONLY);
+  if (fd >= 0)
+    {
+      int n = read (fd, buf, sizeof (buf) - 1);
+      close (fd);
+      if (n > 0)
+        {
+          buf[n] = '\0';
+# if defined __hppa__
+          if (strstr (buf, "QEMU") != NULL)
+            return true;
+# endif
+# if !(defined __i386__ || defined __x86_64__)
+          if (strstr (buf, "AuthenticAMD") != NULL
+              || strstr (buf, "GenuineIntel") != NULL)
+            return true;
+# endif
+# if !(defined __arm__ || defined __aarch64__)
+          if (strstr (buf, "ARM") != NULL
+              || strcasestr (buf, "aarch64") != NULL)
+            return true;
+# endif
+# if !defined __sparc__
+          if (strcasestr (buf, "SPARC") != NULL)
+            return true;
+# endif
+# if !defined __powerpc__
+          if (strstr (buf, "POWER") != NULL)
+            return true;
+# endif
+        }
+    }
+
+  /* If you need more heuristics, look at system calls that are not perfectly
+     well emulated in qemu/linux-user/syscall.c.  */
+#endif
+
+  return false;
+}
index bd5997567e5af366100977c319394f1e0127c268..67e14f7c81e68225dcc3ea9075980b40833c6979 100644 (file)
@@ -31,6 +31,7 @@
 # include <sys/resource.h>
 #endif
 
+#include "qemu.h"
 #include "resource-ext.h"
 
 /* Test against a memory leak in the fprintf replacement.  */
@@ -53,6 +54,12 @@ main (int argc, char *argv[])
   int arg;
   int result;
 
+  if (is_running_under_qemu_user ())
+    {
+      fprintf (stderr, "Skipping test: cannot trust address space size when running under QEMU\n");
+      return 79;
+    }
+
   /* Limit the amount of malloc()ed memory to MAX_ALLOC_TOTAL or less.  */
 
   /* On AIX systems, malloc() is limited by RLIMIT_DATA.  */
@@ -85,7 +92,7 @@ main (int argc, char *argv[])
       if (memory == NULL)
         return 1;
       memset (memory, 17, MAX_ALLOC_TOTAL);
-      result = 78;
+      result = 80;
     }
   else
     {
index 0fb38591d0dd44582d8c5d16b364800584792451..7a548188f31a873e23bd0676ffc1128a43471467 100755 (executable)
@@ -4,7 +4,7 @@
 
 (${CHECKER} ./test-dprintf-posix2${EXEEXT} 0
  result=$?
- if test $result != 77 && test $result != 78; then result=1; fi
+ if test $result != 77 && test $result != 79 && test $result != 80; then result=1; fi
  exit $result
 ) 2>/dev/null
 malloc_result=$?
@@ -12,6 +12,10 @@ if test $malloc_result = 77; then
   echo "Skipping test: no way to determine address space size"
   exit 77
 fi
+if test $malloc_result = 79; then
+  echo "Skipping test: cannot trust address space size when running under QEMU"
+  exit 77
+fi
 
 ${CHECKER} ./test-dprintf-posix2${EXEEXT} 1 > /dev/null
 result=$?
@@ -23,7 +27,7 @@ if test $result != 0; then
   exit 1
 fi
 
-if test $malloc_result = 78; then
+if test $malloc_result = 80; then
   echo "Skipping test: get_rusage_as() doesn't work"
   exit 77
 fi
index 8c3ea8bbc43d31ec6c43ecea422ae2bb334fc6bc..47f29116d3cb9069062fbd8305d8cf5b8a8066b1 100644 (file)
@@ -32,6 +32,8 @@
 #include <string.h>
 #include <errno.h>
 
+#include "qemu.h"
+
 int
 main (int argc, char *argv[])
 {
@@ -39,6 +41,9 @@ main (int argc, char *argv[])
   int arg;
   int ret;
 
+  if (is_running_under_qemu_user ())
+    return 77;
+
   /* Some printf implementations allocate temporary space with malloc.  */
   /* On BSD systems, malloc() is limited by RLIMIT_DATA.  */
 #ifdef RLIMIT_DATA
index 41172b4ea1613318480da690e76a0962f0fe8d7d..458f95784c59cf5cde5efd1e5c6f63319a377e79 100644 (file)
@@ -48,6 +48,7 @@ main ()
 # include <sys/resource.h>
 #endif
 
+#include "qemu.h"
 #include "resource-ext.h"
 
 /* Test against a memory leak in the fprintf replacement.  */
@@ -70,6 +71,12 @@ main (int argc, char *argv[])
   int arg;
   int result;
 
+  if (is_running_under_qemu_user ())
+    {
+      fprintf (stderr, "Skipping test: cannot trust address space size when running under QEMU\n");
+      return 79;
+    }
+
   /* Limit the amount of malloc()ed memory to MAX_ALLOC_TOTAL or less.  */
 
   /* On AIX systems, malloc() is limited by RLIMIT_DATA.  */
index 96609c7f2f6da46f6a0527739ecbc7beb34eef43..a79273ee2e111b99d0332832363e4d6917bd9a9d 100755 (executable)
@@ -4,7 +4,7 @@
 
 (${CHECKER} ./test-fprintf-posix3${EXEEXT} 0
  result=$?
- if test $result != 77 && test $result != 78 && test $result != 80; then result=1; fi
+ if test $result != 77 && test $result != 78 && test $result != 79 && test $result != 80; then result=1; fi
  exit $result
 ) 2>/dev/null
 malloc_result=$?
@@ -16,6 +16,10 @@ if test $malloc_result = 78; then
   echo "Skipping test: cannot trust address space size on this platform"
   exit 77
 fi
+if test $malloc_result = 79; then
+  echo "Skipping test: cannot trust address space size when running under QEMU"
+  exit 77
+fi
 
 ${CHECKER} ./test-fprintf-posix3${EXEEXT} 1 > /dev/null
 result=$?
index 41646db2b49d292825812baefc87ee7e4bfe6cad..7adb8cda3070499124ce9dba22041d2ebd249be3 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "qemu.h"
 #include "macros.h"
 
 void *memchunk1;
@@ -46,6 +47,11 @@ main ()
       fprintf (stderr, "Skipping test: no way to determine address space size\n");
       return 77;
     }
+  else if (is_running_under_qemu_user ())
+    {
+      fprintf (stderr, "Skipping test: running under QEMU\n");
+      return 77;
+    }
   else
     {
       /* The address space size is positive.  */
index c626540a32496e06efc84e396acb350b5f9a721a..a5acb4705c9ed8dce916d1d019262067bad3f3ca 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "qemu.h"
 #include "macros.h"
 
 void *memchunk1;
@@ -46,6 +47,11 @@ main ()
       fprintf (stderr, "Skipping test: no way to determine data segment size\n");
       return 77;
     }
+  else if (is_running_under_qemu_user ())
+    {
+      fprintf (stderr, "Skipping test: running under QEMU\n");
+      return 77;
+    }
   else
     {
       /* The data segment size is positive, except possibly at the beginning.  */
index dc3c4e5f29f0cb6cc9373d54ed91e0431b7355db..f8da61743835ac178c3fc9f726fd8d360c1a626b 100644 (file)
@@ -32,6 +32,8 @@
 #include <string.h>
 #include <errno.h>
 
+#include "qemu.h"
+
 int
 main (int argc, char *argv[])
 {
@@ -39,6 +41,9 @@ main (int argc, char *argv[])
   int arg;
   int ret;
 
+  if (is_running_under_qemu_user ())
+    return 77;
+
   /* Some printf implementations allocate temporary space with malloc.  */
   /* On BSD systems, malloc() is limited by RLIMIT_DATA.  */
 #ifdef RLIMIT_DATA