Fix tst-obprintf - and mcheck in general
authorAndreas Jaeger <aj@suse.de>
Thu, 3 May 2012 20:12:59 +0000 (22:12 +0200)
committerAndreas Jaeger <aj@suse.de>
Thu, 3 May 2012 20:12:59 +0000 (22:12 +0200)
tst-obprintf failed with GCC 4.7.

It turned out that this is the fault of GCC optimizing away the
following from malloc/mcheck.c:
      /* We call malloc() once here to ensure it is initialized.  */
      void *p = malloc (0);
      free (p);

gcc sees the malloc(0);free pair and removes it completely.

And now malloc is not properly initialized and we screw up if both
mcheck is used (via tst-obprintf) and MALLOC_CHECK_ is set (as it is in my
environment).

ChangeLog
malloc/mcheck.c

index 1427a45..0bffdf9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,10 @@
 2012-05-03  Andreas Jaeger  <aj@suse.de>
+
+       * malloc/mcheck.c (mcheck): Add barrier so that malloc/free pair
+       does not get optimized out.
+       (malloc_opt_barrier): New.
+
+2012-05-03  Andreas Jaeger  <aj@suse.de>
            Roland McGrath <roland@hack.frob.com>
 
         * Makerules (.PRECIOUS): Add %.symlist pattern to prevent
index 9213740..9d8a414 100644 (file)
@@ -370,6 +370,10 @@ mabort (enum mcheck_status status)
 #endif
 }
 
+/* Memory barrier so that GCC does not optimize out the argument.  */
+#define malloc_opt_barrier(x) \
+({ __typeof (x) __x = x; __asm ("" : "+m" (__x)); __x; })
+
 int
 mcheck (func)
      void (*func) (enum mcheck_status);
@@ -381,6 +385,8 @@ mcheck (func)
     {
       /* We call malloc() once here to ensure it is initialized.  */
       void *p = malloc (0);
+      /* GCC might optimize out the malloc/free pair without a barrier.  */
+      p = malloc_opt_barrier (p);
       free (p);
 
       old_free_hook = __free_hook;