From patchwork Wed Dec 20 15:23:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 82594 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0EC353858D20 for ; Wed, 20 Dec 2023 15:24:14 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by sourceware.org (Postfix) with ESMTPS id 9C87C3858D20 for ; Wed, 20 Dec 2023 15:23:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9C87C3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9C87C3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::433 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703085841; cv=none; b=emgUhY2OYPoOb27R3tiunCxYuOj4YXgDzsRk+CPnJg8f5MCP0cPHK4wBbnEf4MAs3XtWL2Vup5SFddJkhkMH71nd2KBHLSE62DAT0kBdIWMFwo1XuV8l8pIl5YhK2Zsz2gqSy1khkFFVw7KH3WuI79o3pgN4i1U1wiYNRbnsGjE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703085841; c=relaxed/simple; bh=oN+N3c1bPms31A+rLFiq3+cDgGgmvSsOX2kao1WuoSE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=mo8NVMBj9pz63b0HI1VHtq8+gNiQ34vecjZsY9dGyaiQpxBjVsRh3oUx4hgAXw1dxXkaoS50EALN7y7Q2WS4u2kvwgKzOc7Ddl05BhkB6Gj/XoKsq9aqQcYhePSXLGTpczWGp8W7Uq+1QEri2ClT/Si6zXi08pJm6wuDijg6Fk0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-6d84b68a866so2058542b3a.0 for ; Wed, 20 Dec 2023 07:23:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703085838; x=1703690638; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=v2t8cH9k9leOlgN4HvbNHuvWh05E0IilyuxvfziP95A=; b=ThREkfyLLWpd0tf2dQu4P0FZEsM79v9JTECi860v4yWDYNlTwmyejV774O9dX7/+Tm ANXBr3sKNU6tr1Lj+1D+kE5nOg7zaRIWD6DeZ06Q4FHJb9PaJ2roWPRP9AI47rcYtCFW Km+c/ioiLcqCYgq1Roi+jjrfZn6fuWjYAPbhJx2r1HMKKoryGbgQzi0SHIjJlU/ZITU+ VLJTLrqttvWHzc8WHlA29iTiKKJU8jpQ33XrkgXKvH637nfXVB0aUqhswnS113W+elyE T/HjZ65DY9438efhFqF0UUizwN+BU9mSbhBOsiXMQ3Ojsay/6gvLDNka33mZ2ZPJeWMG 4I8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703085838; x=1703690638; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=v2t8cH9k9leOlgN4HvbNHuvWh05E0IilyuxvfziP95A=; b=M6jVFFOR8sOzypA6yDd3elx9BZv8rMUoQJumG2DphVvH9zPq244fzlcTCDXxQSSZcw AP3rpX1hPbwjUgisJmNC3MnKD1MZza9Vwy6mhw7khRxe6JcGOSvFa1pbHKQEbrs/WhSg u+BvWtqjnN6xGpg75zwWn/jgOsVvPxquBBP4j1UOm9STg4r+ltTHG5z6y8HlhZ9ggkqI MS8YyZGqdWRprX5OuWtuO70DXUgEIHKoIYZ5AipXkJS1VcgrV99aJxfZqQw8nYif/+SP vibZFrhbUcsen+CBuAO25SEJaLpeSfOg8h11X7N1HNYT+tMznM4nRFBVzQ1e0lABP2l0 VxWA== X-Gm-Message-State: AOJu0YwW+hLtP9ch2NyDA72hnEossvtX1kMkhsqUYhGSFWWay4wrEbzR UECKpLk46Qajn+0GYbXRElX1S184wBM= X-Google-Smtp-Source: AGHT+IGGY8Z4dh+P5gYUQCu6wlrex5NP5cVm8vHO0nwN6/ytFUMcuTH170uph3kj9n5H6QcOsp51OQ== X-Received: by 2002:a05:6a20:3ca2:b0:190:c670:8e43 with SMTP id b34-20020a056a203ca200b00190c6708e43mr10433653pzj.92.1703085837619; Wed, 20 Dec 2023 07:23:57 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.129.147]) by smtp.gmail.com with ESMTPSA id kq3-20020a056a004b0300b006cdb17f9ffdsm22152455pfb.66.2023.12.20.07.23.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 07:23:57 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id D4B067402D7 for ; Wed, 20 Dec 2023 07:23:55 -0800 (PST) From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH] Add a setjmp/longjmp test between user contexts Date: Wed, 20 Dec 2023 07:23:55 -0800 Message-ID: <20231220152355.742046-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-3024.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Verify that setjmp and longjmp work correctly between user contexts. Arrange stacks for uctx_func1 and uctx_func2 so that ____longjmp_chk works when setjmp and longjmp are called from different user contexts. --- stdlib/Makefile | 1 + stdlib/tst-swapcontext2.c | 139 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 stdlib/tst-swapcontext2.c diff --git a/stdlib/Makefile b/stdlib/Makefile index 0b5ef699a2..3cd05d184e 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -256,6 +256,7 @@ tests := \ tst-strtol-locale \ tst-strtoll \ tst-swapcontext1 \ + tst-swapcontext2 \ tst-thread-quick_exit \ tst-tininess \ tst-unsetenv1 \ diff --git a/stdlib/tst-swapcontext2.c b/stdlib/tst-swapcontext2.c new file mode 100644 index 0000000000..e02199127d --- /dev/null +++ b/stdlib/tst-swapcontext2.c @@ -0,0 +1,139 @@ +/* Check setjmp/longjmp between user contexts. + Copyright (C) 2023 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 +#include +#include + +static ucontext_t uctx_main, uctx_func1, uctx_func2; +const char *str1 = "\e[31mswapcontext(&uctx_func1, &uctx_main)\e[0m"; +const char *str2 = "\e[34mswapcontext(&uctx_func2, &uctx_main)\e[0m"; +const char *fmt1 = "\e[31m"; +const char *fmt2 = "\e[34m"; +static jmp_buf jmpbuf; + +#define handle_error(msg) \ + do { perror(msg); exit(EXIT_FAILURE); } while (0) + +__attribute__((noinline, noclone)) +static void +func4(ucontext_t *uocp, ucontext_t *ucp, const char *str, const char *fmt) +{ + printf(" %sfunc4: %s\e[0m\n", fmt, str); + if (swapcontext(uocp, ucp) == -1) + handle_error("swapcontext"); + printf(" %sfunc4: returning\e[0m\n", fmt); +} + +__attribute__((noinline, noclone)) +static void +func3(ucontext_t *uocp, ucontext_t *ucp, const char *str, const char *fmt) +{ + printf(" %sfunc3: func4(uocp, ucp, str)\e[0m\n", fmt); + func4(uocp, ucp, str, fmt); + printf(" %sfunc3: returning\e[0m\n", fmt); +} + +__attribute__((noinline, noclone)) +static void +func1(void) +{ + while ( 1 ) + { + printf(" \e[31mfunc1: func3(&uctx_func1, &uctx_main, str1)\e[0m\n"); + if (setjmp (jmpbuf) != 0) + { + printf(" %sfunc1: from longjmp\e[0m\n", fmt1); + exit (EXIT_SUCCESS); + } + func3( &uctx_func1, &uctx_main, str1, fmt1); + } +} + +__attribute__((noinline, noclone)) +static void +func2(void) +{ + while ( 1 ) + { + printf(" \e[34mfunc2: func3(&uctx_func2, &uctx_main, str2)\e[0m\n"); + func3(&uctx_func2, &uctx_main, str2, fmt2); + printf(" %sfunc2: calling longjmp\e[0m\n", fmt2); + longjmp (jmpbuf, 1); + } +} + +static int +do_test (void) +{ + /* ____longjmp_chk has */ +#if 0 +#ifdef _STACK_GROWS_DOWN +#define called_from(this, saved) ((this) < (saved)) +#else +#define called_from(this, saved) ((this) > (saved)) +#endif + + /* If "env" is from a frame that called us, we're all set. */ + if (called_from(this_frame, saved_frame)) + __longjmp (env, val); +#endif + /* Arrange stacks for uctx_func1 and uctx_func2 so that called_from + is true when setjmp is called from uctx_func1 and longjmp is called + from uctx_func2. */ +#ifdef _STACK_GROWS_DOWN +# define UCTX_FUNC1_STACK 1 +# define UCTX_FUNC2_STACK 0 +#else +# define UCTX_FUNC1_STACK 0 +# define UCTX_FUNC2_STACK 1 +#endif + char func_stack[2][16384]; + int i; + + if (getcontext(&uctx_func1) == -1) + handle_error("getcontext"); + uctx_func1.uc_stack.ss_sp = func_stack[UCTX_FUNC1_STACK]; + uctx_func1.uc_stack.ss_size = sizeof (func_stack[UCTX_FUNC1_STACK]); + uctx_func1.uc_link = &uctx_main; + makecontext(&uctx_func1, func1, 0); + + if (getcontext(&uctx_func2) == -1) + handle_error("getcontext"); + uctx_func2.uc_stack.ss_sp = func_stack[UCTX_FUNC2_STACK]; + uctx_func2.uc_stack.ss_size = sizeof (func_stack[UCTX_FUNC2_STACK]); + uctx_func2.uc_link = &uctx_func1; + makecontext(&uctx_func2, func2, 0); + + for ( i = 0; i < 2; i++ ) + { + if (swapcontext(&uctx_main, &uctx_func1) == -1) + handle_error("swapcontext"); + printf(" \e[35mmain: swapcontext(&uctx_main, &uctx_func2)\n\e[0m"); + if (swapcontext(&uctx_main, &uctx_func2) == -1) + handle_error("swapcontext"); + printf(" \e[35mmain: swapcontext(&uctx_main, &uctx_func1)\n\e[0m"); + } + + printf("main: exiting\n"); + exit(EXIT_SUCCESS); +} + +#include