This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH] nptl: Add compiler barrier in nptl/tst-pthread-getattr
- From: Florian Weimer <fweimer at redhat dot com>
- To: libc-alpha at sourceware dot org
- Date: Mon, 22 Jul 2019 13:33:22 +0200
- Subject: [PATCH] nptl: Add compiler barrier in nptl/tst-pthread-getattr
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.
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