From patchwork Mon Jul 22 13:34:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 33768 Received: (qmail 53457 invoked by alias); 22 Jul 2019 13:34:46 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 53375 invoked by uid 89); 22 Jul 2019 13:34:46 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-18.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=celebrate, H*f:sk:mvmr26i, H*i:sk:mvmr26i X-HELO: mx1.redhat.com From: Florian Weimer To: Andreas Schwab Cc: libc-alpha@sourceware.org Subject: Re: [PATCH] nptl: Add compiler barrier in nptl/tst-pthread-getattr References: <87blxms3r1.fsf@oldenburg2.str.redhat.com> Date: Mon, 22 Jul 2019 15:34:41 +0200 In-Reply-To: (Andreas Schwab's message of "Mon, 22 Jul 2019 13:58:01 +0200") Message-ID: <87v9vuqjke.fsf@oldenburg2.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (gnu/linux) MIME-Version: 1.0 * Andreas Schwab: > On Jul 22 2019, Florian Weimer 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 >> >> * 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? Thanks for the suggestion. I think this is the better fix. Patch below. Florian nptl: Use uintptr_t for address diagnostic 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)); | ^~~~~~ The address itself is used in a check in the caller, so using uintptr_t instead is reasonable. 2019-07-22 Florian Weimer * nptl/tst-pthread-getattr.c (allocate_and_test): Change return type to uintptr_t. (check_stack_top): Adjust. diff --git a/nptl/tst-pthread-getattr.c b/nptl/tst-pthread-getattr.c index a954778767..e3634ea0a7 100644 --- a/nptl/tst-pthread-getattr.c +++ b/nptl/tst-pthread-getattr.c @@ -43,7 +43,7 @@ static size_t pagesize; /* Check if the page in which TARGET lies is accessible. This will segfault if it fails. */ -static volatile char * +static volatile uintptr_t allocate_and_test (char *target) { volatile char *mem = (char *) &mem; @@ -51,7 +51,7 @@ allocate_and_test (char *target) mem = alloca ((size_t) (mem - target)); *mem = 42; - return mem; + return (uintptr_t) mem; } static int @@ -84,7 +84,6 @@ check_stack_top (void) { struct rlimit stack_limit; void *stackaddr; - volatile void *mem; size_t stacksize = 0; int ret; uintptr_t pagemask = ~(pagesize - 1); @@ -130,14 +129,14 @@ check_stack_top (void) stack and test access there. It is however sufficient to simply check if the top page is accessible, so we target our access halfway up the top page. Thanks Chris Metcalf for this idea. */ - mem = allocate_and_test (stackaddr + pagesize / 2); + uintptr_t mem = allocate_and_test (stackaddr + pagesize / 2); /* Before we celebrate, make sure we actually did test the same page. */ - if (((uintptr_t) stackaddr & pagemask) != ((uintptr_t) mem & pagemask)) + if (((uintptr_t) stackaddr & pagemask) != (mem & pagemask)) { printf ("We successfully wrote into the wrong page.\n" "Expected %#" PRIxPTR ", but got %#" PRIxPTR "\n", - (uintptr_t) stackaddr & pagemask, (uintptr_t) mem & pagemask); + (uintptr_t) stackaddr & pagemask, mem & pagemask); return 1; }