From patchwork Sat Jul 21 14:20:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 28549 Received: (qmail 82417 invoked by alias); 21 Jul 2018 14:20:45 -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 82198 invoked by uid 89); 21 Jul 2018 14:20:43 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.8 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=Touch, poisoned, orig_context, child_context X-HELO: mail-pl0-f47.google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=xFLzSKhi6z7BA7OaSDCPnxPJUCCg/CjPzKfwyTXBETE=; b=snp5i00FsIX2yEACd+UhAUTYpIHMVHqZVUJmVqGxXEucJozB9b4ACQBhtDFwmsU3Hw 91VDj43UwHeuCEWkI4+HsPGdkx0vIAQFIPMackgkPRjtQ1V0bcbA/g6YE90yB5mGPTzA plpHKY0i31tF2IE5fyFH1/usiyDK4IwlY1hG/gCCNuvotjgnQeaZsYN/kXELorC5P8zt GJuEaXZTeodXGUZ0oAouBpxCRAXwFoXzBndWSmTN1o0tyb/+Z8LG4/KhOHR7+RimyGcS zuWh8L8Z5pa2KB2u6k0E5Slcr7PH0SGiVzAwST+qo1zop1FMw5De/BQ3KzLyUw/4y/XX zbpg== Return-Path: From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: Carlos O'Donell Subject: [PATCH 08/12] Add a test for swapcontext with a wrapper Date: Sat, 21 Jul 2018 07:20:31 -0700 Message-Id: <20180721142035.21059-9-hjl.tools@gmail.com> In-Reply-To: <20180721142035.21059-1-hjl.tools@gmail.com> References: <20180721142035.21059-1-hjl.tools@gmail.com> Check swapcontext works with a wrapper. * stdlib/Makefile (tests): Add tst-swapcontext1. * stdlib/tst-swapcontext1.c: New test. Reviewed-by: Carlos O'Donell --- stdlib/Makefile | 3 +- stdlib/tst-swapcontext1.c | 92 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 stdlib/tst-swapcontext1.c diff --git a/stdlib/Makefile b/stdlib/Makefile index b5e55b0a55..bc8929f2b9 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -84,7 +84,8 @@ tests := tst-strtol tst-strtod testmb testrand testsort testdiv \ tst-cxa_atexit tst-on_exit test-atexit-race \ test-at_quick_exit-race test-cxa_atexit-race \ test-on_exit-race test-dlclose-exit-race \ - tst-makecontext-align test-bz22786 tst-strtod-nan-sign + tst-makecontext-align test-bz22786 tst-strtod-nan-sign \ + tst-swapcontext1 tests-internal := tst-strtod1i tst-strtod3 tst-strtod4 tst-strtod5i \ tst-tls-atexit tst-tls-atexit-nodelete diff --git a/stdlib/tst-swapcontext1.c b/stdlib/tst-swapcontext1.c new file mode 100644 index 0000000000..6dd52f22a6 --- /dev/null +++ b/stdlib/tst-swapcontext1.c @@ -0,0 +1,92 @@ +/* Check swapcontext wrapper. + Modified from c-c++-common/asan/swapcontext-test-1.c in GCC testsuite. + Copyright (C) 2018 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +__attribute__((noinline, noclone)) +__INDIRECT_RETURN +int +myswapcontext (ucontext_t *oucp, ucontext_t *ucp) +{ + int res = swapcontext (oucp, ucp); + return res; +} + +ucontext_t orig_context; +ucontext_t child_context; + +void +Child (int mode) +{ + char x[32] = {0}; /* Stack gets poisoned. */ + printf("Child: %p\n", x); + /* (a) Do nothing, just return to parent function. + (b) Jump into the original function. Stack remains poisoned unless we do + something. */ + if (mode == 1) + { + if (myswapcontext (&child_context, &orig_context) < 0) + { + perror ("swapcontext"); + _exit (0); + } + } +} + +int +Run (int arg, int mode) +{ + int i; + const int kStackSize = 1 << 20; + char child_stack[kStackSize + 1]; + printf ("Child stack: %p\n", child_stack); + /* Setup child context. */ + getcontext (&child_context); + child_context.uc_stack.ss_sp = child_stack; + child_context.uc_stack.ss_size = kStackSize / 2; + if (mode == 0) + child_context.uc_link = &orig_context; + makecontext (&child_context, (void (*)(void))Child, 1, mode); + if (myswapcontext (&orig_context, &child_context) < 0) + { + perror("swapcontext"); + return 0; + } + /* Touch childs's stack to make sure it's unpoisoned. */ + for (i = 0; i < kStackSize; i++) + child_stack[i] = i; + return child_stack[arg]; +} + +volatile int zero = 0; + +static int +do_test (void) +{ + int ret = 0; + ret += Run (zero, 0); + fprintf (stderr, "Test1 passed\n"); + ret += Run (zero, 1); + fprintf (stderr, "Test2 passed\n"); + return ret; +} + +#include