From patchwork Wed Dec 20 15:34:42 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: 1878591 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Z8KJP3Pl; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SwHgS5XBYz20LV for ; Thu, 21 Dec 2023 02:35:00 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C5715385F028 for ; Wed, 20 Dec 2023 15:34:58 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by sourceware.org (Postfix) with ESMTPS id 874C8385E451 for ; Wed, 20 Dec 2023 15:34:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 874C8385E451 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 874C8385E451 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::52b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703086488; cv=none; b=w0OFiqZfIjU+wTLUD6xGhK06ewYYcCJHe1oac3cEwokRI+Rqnb0FHyhZT7eg+VliQsiZBEY01cSj3p7zZYwxJ8AYVyPBQSCB6kjR/YkS05C+XvM9jD1Go/cW4ea113S9SF6vEq1Mb4iEs7cjEeF+JNIaF3Wc1v3Tu0BLcI7NS+U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703086488; c=relaxed/simple; bh=TxxNfGauE6ml8PsTgL2ga1fuDnEtrJqUbZE/cTRhYwg=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=YtTLtfKtB8/V1kST8VsRB3rYOPsaKvYuX/aWDEFL8uu9SMf+ZoWMXa40C7UOBOQ9DOof6Wsi/IFlSfBsomO/SF/X67MqkMMkxxTDY3sYc1QWxWgmpd7/lExENPn7jHR1MiVdz2Ng7//kQKrDtqy/f7FgFa6S6/M6qgR1MtRDzqA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-5c699b44dddso2089249a12.1 for ; Wed, 20 Dec 2023 07:34:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703086484; x=1703691284; darn=sourceware.org; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=mw+3ADVqDUUgRX14nXMGYpooitFyAklymzQIxvQdX+k=; b=Z8KJP3PlGIgGh6LWx8aZ7+3SCnHNHZip2GEMXlV3UzxJrOaqZ2Xu9tsNdNYtrLG96z ZOFV996ZBV/sC1EZ9WWppnYB8lJsaTXxKyhs6DvCuM7qJ2skiv2+Yj0+tYpuyzvhQ1iS onj62w72NMUmuVqGPf0Vk8yvV0u0RK2/fcKeBaZRHiJPzBGqGJbBPgrThC/1VGy8jmrc bVQnU+86xQLSHqoKjqvH3xo7fLHjud9cc3RMP6YXrFa+Kwo0pb0CqN/3MJYNPmZP8trb CsRZMK1t40kLMCGnpqSxxbjfOe9XjpjDDMpCH9l5m87T6d+B2hCCSO3MDloWtQ+F3UI6 Gqsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703086484; x=1703691284; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mw+3ADVqDUUgRX14nXMGYpooitFyAklymzQIxvQdX+k=; b=WG5ObuqV30FSoGh7ZxX2L103N6p4rlbikrv18jiWm+yWg3VzcsURjViH2lZ2zfYbrZ OBiaMTRCiKnO6KZhl7frMl5ooVj/1vRnfb5kD1JA/QOavRLfH4wR1MjCX1hCUTiCR6s0 KiMremzIqcseQ+AVIFXmqUkqljZcvTYfokMDdP2K8sCaaD2iEL89A0CSARytJhv5MkST cTsAN/65ZeXK4iZsvgovF+o6VwATtllqX6VoBKIJbyA7egOg40J4Y7ZYUD4AMj2Y4qpy Y8z4CDKS7/QFqhq5TTrh2thPQ4PXncHa0s9L7jxtWZlMkvWY4S/4O6ZupQLdJ55a/2OS 3+FA== X-Gm-Message-State: AOJu0Yy1tp7yIflYuJ4IqZ+U75AGCQreHFmqkXSNccZnRploBDvPxHbN ku4pAOm4f7ycntRH5vHzeZyfJjrPk+A= X-Google-Smtp-Source: AGHT+IGv/fA9/8+xgmzHayV5y6dAKqvarTBlhipFNYgJqvtx5D1bz6Dfmhr9kL0JlzSxEApAeG+PZg== X-Received: by 2002:a17:90a:bd04:b0:28b:d3d8:e6c1 with SMTP id y4-20020a17090abd0400b0028bd3d8e6c1mr623134pjr.32.1703086484290; Wed, 20 Dec 2023 07:34:44 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.129.147]) by smtp.gmail.com with ESMTPSA id mf14-20020a17090b184e00b0028ae0184bfasm3862302pjb.49.2023.12.20.07.34.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 07:34:43 -0800 (PST) Received: by gnu-cfl-3.localdomain (Postfix, from userid 1000) id 8CA717403BA; Wed, 20 Dec 2023 07:34:42 -0800 (PST) Date: Wed, 20 Dec 2023 07:34:42 -0800 From: "H.J. Lu" To: GNU C Library Subject: [PATCH v2] Add a setjmp/longjmp test between user contexts Message-ID: MIME-Version: 1.0 Content-Disposition: inline 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+incoming=patchwork.ozlabs.org@sourceware.org Changes in v2: 1. Change do_test to exit(EXIT_FAILURE). H.J. Reviewed-by: Noah Goldstein --- 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 | 138 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 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..f679755649 --- /dev/null +++ b/stdlib/tst-swapcontext2.c @@ -0,0 +1,138 @@ +/* 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"); + } + + exit(EXIT_FAILURE); +} + +#include