bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] malloc-gnu-tests, etc.: use volatile for clang


From: Paul Eggert
Subject: [PATCH] malloc-gnu-tests, etc.: use volatile for clang
Date: Tue, 20 Apr 2021 17:13:12 -0700

In some more test locations, store the result of malloc etc. into
a volatile pointer so that clang doesn’t optimize away the malloc
and thus bypass the test.  This fixes a malloc-gnu test failure on
macOS 11.2.3 with clang 12.0.0 on ARM.
* tests/test-alloca-opt.c (do_allocation):
* tests/test-malloc-gnu.c (main):
* tests/test-malloca.c (do_allocation):
* tests/test-realloc-gnu.c (main):
* tests/test-reallocarray.c (main):
* tests/test-aligned-malloc.c (main):
* tests/test-aligned_alloc.c (main):
Store malloc etc. results into a volatile pointer.
---
 ChangeLog                   | 16 ++++++++++++++++
 tests/test-aligned-malloc.c |  8 ++++----
 tests/test-aligned_alloc.c  | 12 ++++++------
 tests/test-alloca-opt.c     |  2 +-
 tests/test-malloc-gnu.c     |  5 +++--
 tests/test-malloca.c        |  2 +-
 tests/test-realloc-gnu.c    |  5 +++--
 tests/test-reallocarray.c   |  3 ++-
 8 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0146e1594..d2ea4e509 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2021-04-20  Paul Eggert  <eggert@cs.ucla.edu>
+
+       malloc-gnu-tests, etc.: use volatile for clang
+       In some more test locations, store the result of malloc etc. into
+       a volatile pointer so that clang doesn’t optimize away the malloc
+       and thus bypass the test.  This fixes a malloc-gnu test failure on
+       macOS 11.2.3 with clang 12.0.0 on ARM.
+       * tests/test-alloca-opt.c (do_allocation):
+       * tests/test-malloc-gnu.c (main):
+       * tests/test-malloca.c (do_allocation):
+       * tests/test-realloc-gnu.c (main):
+       * tests/test-reallocarray.c (main):
+       * tests/test-aligned-malloc.c (main):
+       * tests/test-aligned_alloc.c (main):
+       Store malloc etc. results into a volatile pointer.
+
 2021-04-18  Paul Eggert  <eggert@cs.ucla.edu>
 
        malloc-gnu-tests: pacify -Walloc-size-larger-than
diff --git a/tests/test-aligned-malloc.c b/tests/test-aligned-malloc.c
index 0a1b4d710..92f34bca3 100644
--- a/tests/test-aligned-malloc.c
+++ b/tests/test-aligned-malloc.c
@@ -63,10 +63,10 @@ main (int argc, char *argv[])
 {
   static size_t sizes[] =
     { 13, 8, 17, 450, 320, 1, 99, 4, 15, 16, 2, 76, 37, 127, 2406, 641 };
-  void *aligned4_blocks[SIZEOF (sizes)];
-  void *aligned8_blocks[SIZEOF (sizes)];
-  void *aligned16_blocks[SIZEOF (sizes)];
-  void *aligned32_blocks[SIZEOF (sizes)];
+  void *volatile aligned4_blocks[SIZEOF (sizes)];
+  void *volatile aligned8_blocks[SIZEOF (sizes)];
+  void *volatile aligned16_blocks[SIZEOF (sizes)];
+  void *volatile aligned32_blocks[SIZEOF (sizes)];
   size_t i;
 
   for (i = 0; i < SIZEOF (sizes); i++)
diff --git a/tests/test-aligned_alloc.c b/tests/test-aligned_alloc.c
index 123c25123..9aba4c8ba 100644
--- a/tests/test-aligned_alloc.c
+++ b/tests/test-aligned_alloc.c
@@ -36,12 +36,12 @@ main (int argc, char *argv[])
 #if HAVE_ALIGNED_ALLOC
   static size_t sizes[] =
     { 13, 8, 17, 450, 320, 1, 99, 4, 15, 16, 2, 76, 37, 127, 2406, 641, 5781 };
-  void *aligned2_blocks[SIZEOF (sizes)];
-  void *aligned4_blocks[SIZEOF (sizes)];
-  void *aligned8_blocks[SIZEOF (sizes)];
-  void *aligned16_blocks[SIZEOF (sizes)];
-  void *aligned32_blocks[SIZEOF (sizes)];
-  void *aligned64_blocks[SIZEOF (sizes)];
+  void *volatile aligned2_blocks[SIZEOF (sizes)];
+  void *volatile aligned4_blocks[SIZEOF (sizes)];
+  void *volatile aligned8_blocks[SIZEOF (sizes)];
+  void *volatile aligned16_blocks[SIZEOF (sizes)];
+  void *volatile aligned32_blocks[SIZEOF (sizes)];
+  void *volatile aligned64_blocks[SIZEOF (sizes)];
   size_t i;
 
   for (i = 0; i < SIZEOF (sizes); i++)
diff --git a/tests/test-alloca-opt.c b/tests/test-alloca-opt.c
index edca8e65f..fdbf6f588 100644
--- a/tests/test-alloca-opt.c
+++ b/tests/test-alloca-opt.c
@@ -25,7 +25,7 @@
 static void
 do_allocation (int n)
 {
-  void *ptr = alloca (n);
+  void *volatile ptr = alloca (n);
   (void) ptr;
 }
 
diff --git a/tests/test-malloc-gnu.c b/tests/test-malloc-gnu.c
index e1dfde452..d8e7b04a8 100644
--- a/tests/test-malloc-gnu.c
+++ b/tests/test-malloc-gnu.c
@@ -23,7 +23,7 @@ int
 main (int argc, char **argv)
 {
   /* Check that malloc (0) is not a NULL pointer.  */
-  char *p = malloc (0);
+  void *volatile p = malloc (0);
   if (p == NULL)
     return 1;
   free (p);
@@ -32,7 +32,8 @@ main (int argc, char **argv)
   if (PTRDIFF_MAX < SIZE_MAX)
     {
       size_t one = argc != 12345;
-      if (malloc (PTRDIFF_MAX + one) != NULL)
+      p = malloc (PTRDIFF_MAX + one);
+      if (p != NULL)
         return 1;
     }
 
diff --git a/tests/test-malloca.c b/tests/test-malloca.c
index deb813c5a..52d95a5d9 100644
--- a/tests/test-malloca.c
+++ b/tests/test-malloca.c
@@ -25,7 +25,7 @@
 static void
 do_allocation (int n)
 {
-  void *ptr = malloca (n);
+  void *volatile ptr = malloca (n);
   freea (ptr);
   safe_alloca (n);
 }
diff --git a/tests/test-realloc-gnu.c b/tests/test-realloc-gnu.c
index b62ee6bad..f4c00c0bf 100644
--- a/tests/test-realloc-gnu.c
+++ b/tests/test-realloc-gnu.c
@@ -23,7 +23,7 @@ int
 main (int argc, char **argv)
 {
   /* Check that realloc (NULL, 0) is not a NULL pointer.  */
-  char *p = realloc (NULL, 0);
+  void *volatile p = realloc (NULL, 0);
   if (p == NULL)
     return 1;
 
@@ -32,7 +32,8 @@ main (int argc, char **argv)
   if (PTRDIFF_MAX < SIZE_MAX)
     {
       size_t one = argc != 12345;
-      if (realloc (p, PTRDIFF_MAX + one) != NULL)
+      p = realloc (p, PTRDIFF_MAX + one);
+      if (p != NULL)
         return 1;
     }
 
diff --git a/tests/test-reallocarray.c b/tests/test-reallocarray.c
index a6e0cbbfd..9c4a189ef 100644
--- a/tests/test-reallocarray.c
+++ b/tests/test-reallocarray.c
@@ -31,7 +31,8 @@ main ()
       of memory larger than SIZE_MAX bytes.  */
    for (n = 2; n != 0; n <<= 1)
      {
-       if (reallocarray (NULL, (size_t) -1 / n + 1, n))
+       void *volatile p = reallocarray (NULL, (size_t) -1 / n + 1, n);
+       if (p)
          return 1;
 
        /* Ensure that errno is correctly set.  */
-- 
2.30.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]