From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 84537 invoked by alias); 20 Sep 2018 15:47:00 -0000 Mailing-List: contact libc-stable-help@sourceware.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Subscribe: List-Archive: Sender: libc-stable-owner@sourceware.org Received: (qmail 84429 invoked by uid 89); 20 Sep 2018 15:46:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Checked: by ClamAV 0.100.1 on sourceware.org X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-Spam-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_HELO_PASS autolearn=ham version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on sourceware.org X-Spam-Level: X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 20 Sep 2018 15:46:57 +0000 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C18F983F3E for ; Thu, 20 Sep 2018 15:46:56 +0000 (UTC) Received: from oldenburg.str.redhat.com (dhcp-192-212.str.redhat.com [10.33.192.212]) by smtp.corp.redhat.com (Postfix) with ESMTP id 91A6B7EA20 for ; Thu, 20 Sep 2018 15:46:56 +0000 (UTC) Received: by oldenburg.str.redhat.com (Postfix, from userid 1000) id 02C60441981D5; Thu, 20 Sep 2018 17:46:56 +0200 (CEST) Date: Mon, 01 Jan 2018 00:00:00 -0000 To: libc-stable@sourceware.org Subject: [2.28 COMMITTED] Fix tst-setcontext9 for optimized small stacks. User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20180920154656.02C60441981D5@oldenburg.str.redhat.com> From: fweimer@redhat.com (Florian Weimer) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 20 Sep 2018 15:46:56 +0000 (UTC) X-IsSubscribed: yes X-SW-Source: 2018-09/txt/msg00006.txt.bz2 From: Carlos O'Donell If the compiler reduces the stack usage in function f1 before calling into function f2, then when we swapcontext back to f1 and continue execution we may overwrite registers that were spilled to the stack while f2 was executing. Later when we return to f2 the corrupt registers will be reloaded from the stack and the test will crash. This was most commonly observed on i686 with __x86.get_pc_thunk.dx and needing to save and restore $edx. Overall i686 has few registers and the spilling to the stack is bound to happen, therefore the solution to making this test robust is to split function f1 into two parts f1a and f1b, and allocate f1b it's own stack such that subsequent execution does not overwrite the stack in use by function f2. Tested on i686 and x86_64. Signed-off-by: Carlos O'Donell (cherry picked from commit 791b350dc725545e3f9b5db0f97ebdbc60c9735f) 2018-09-19 Carlos O'Donell * stdlib/tst-setcontext9.c (f1): Rename to... (f1a): ... this. (f1b): New function implementing lower half of f1 in alternate stack. diff --git a/stdlib/tst-setcontext9.c b/stdlib/tst-setcontext9.c index 4636ce9030..db8355766c 100644 --- a/stdlib/tst-setcontext9.c +++ b/stdlib/tst-setcontext9.c @@ -41,26 +41,55 @@ f2 (void) } static void -f1 (void) +f1b (void) { - puts ("start f1"); - if (getcontext (&ctx[2]) != 0) - { - printf ("%s: getcontext: %m\n", __FUNCTION__); - exit (EXIT_FAILURE); - } if (done) { - puts ("set context in f1"); + puts ("set context in f1b"); if (setcontext (&ctx[3]) != 0) { printf ("%s: setcontext: %m\n", __FUNCTION__); exit (EXIT_FAILURE); } } + exit (EXIT_FAILURE); +} + +static void +f1a (void) +{ + char st2[32768]; + puts ("start f1a"); + if (getcontext (&ctx[2]) != 0) + { + printf ("%s: getcontext: %m\n", __FUNCTION__); + exit (EXIT_FAILURE); + } + ctx[2].uc_stack.ss_sp = st2; + ctx[2].uc_stack.ss_size = sizeof st2; + ctx[2].uc_link = &ctx[0]; + makecontext (&ctx[2], (void (*) (void)) f1b, 0); f2 (); } +/* The execution path through the test looks like this: + do_test (call) + -> "making contexts" + -> "swap contexts" + f1a (via swapcontext to ctx[1], with alternate stack) + -> "start f1a" + f2 (call) + -> "swap contexts in f2" + f1b (via swapcontext to ctx[2], with alternate stack) + -> "set context in f1b" + do_test (via setcontext to ctx[3], main stack) + -> "setcontext" + f2 (via setcontext to ctx[4], with alternate stack) + -> "end f2" + + We must use an alternate stack for f1b, because if we don't then the + result of executing an earlier caller may overwrite registers + spilled to the stack in f2. */ static int do_test (void) { @@ -79,7 +108,7 @@ do_test (void) ctx[1].uc_stack.ss_sp = st1; ctx[1].uc_stack.ss_size = sizeof st1; ctx[1].uc_link = &ctx[0]; - makecontext (&ctx[1], (void (*) (void)) f1, 0); + makecontext (&ctx[1], (void (*) (void)) f1a, 0); puts ("swap contexts"); if (swapcontext (&ctx[3], &ctx[1]) != 0) {