nptl: Add compiler barrier in nptl/tst-pthread-getattr

Message ID 87blxms3r1.fsf@oldenburg2.str.redhat.com
State Superseded
Headers

Commit Message

Florian Weimer July 22, 2019, 11:33 a.m. UTC
  Recent GCC versions warn about the attempt to return the address of a
local variable:

tst-pthread-getattr.c: In function ‘allocate_and_test’:
tst-pthread-getattr.c:54:10: error: function returns address of local variable [-Werror=return-local-addr]
   54 |   return mem;
      |          ^~~
In file included from ../include/alloca.h:3,
                 from tst-pthread-getattr.c:26:
../stdlib/alloca.h:35:23: note: declared here
   35 | # define alloca(size) __builtin_alloca (size)
      |                       ^~~~~~~~~~~~~~~~~~~~~~~
tst-pthread-getattr.c:51:9: note: in expansion of macro ‘alloca’
   51 |   mem = alloca ((size_t) (mem - target));
      |         ^~~~~~

2019-07-22  Florian Weimer  <fweimer@redhat.com>

	* nptl/tst-pthread-getattr.c (compiler_barrier): New function.
	(allocate_and_test): Use it.
  

Comments

Andreas Schwab July 22, 2019, 11:58 a.m. UTC | #1
On Jul 22 2019, Florian Weimer <fweimer@redhat.com> wrote:

> Recent GCC versions warn about the attempt to return the address of a
> local variable:
>
> tst-pthread-getattr.c: In function ‘allocate_and_test’:
> tst-pthread-getattr.c:54:10: error: function returns address of local variable [-Werror=return-local-addr]
>    54 |   return mem;
>       |          ^~~
> In file included from ../include/alloca.h:3,
>                  from tst-pthread-getattr.c:26:
> ../stdlib/alloca.h:35:23: note: declared here
>    35 | # define alloca(size) __builtin_alloca (size)
>       |                       ^~~~~~~~~~~~~~~~~~~~~~~
> tst-pthread-getattr.c:51:9: note: in expansion of macro ‘alloca’
>    51 |   mem = alloca ((size_t) (mem - target));
>       |         ^~~~~~
>
> 2019-07-22  Florian Weimer  <fweimer@redhat.com>
>
> 	* nptl/tst-pthread-getattr.c (compiler_barrier): New function.
> 	(allocate_and_test): Use it.

Does it work to change the return type to uintptr_t?

Andreas.
  

Patch

diff --git a/nptl/tst-pthread-getattr.c b/nptl/tst-pthread-getattr.c
index a954778767..a11a25437d 100644
--- a/nptl/tst-pthread-getattr.c
+++ b/nptl/tst-pthread-getattr.c
@@ -41,6 +41,15 @@ 
 
 static size_t pagesize;
 
+/* The weak attribute marks this function as interposable, so the
+   compiler cannot assume that the pointer remains unchanged.  */
+__attribute__ ((noinline, noclone,  weak))
+volatile char *
+compiler_barrier (volatile char *ptr)
+{
+  return ptr;
+}
+
 /* Check if the page in which TARGET lies is accessible.  This will segfault
    if it fails.  */
 static volatile char *
@@ -51,7 +60,10 @@  allocate_and_test (char *target)
   mem = alloca ((size_t) (mem - target));
 
   *mem = 42;
-  return mem;
+
+  /* Insert the compiler barrier to hide the fact that mem is
+     stack-allocated.  The address is only used in a diagnostic.  */
+  return compiler_barrier (mem);
 }
 
 static int