From patchwork Tue May 26 06:16:59 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jesse Huang X-Patchwork-Id: 135665 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 056D24B9DB5F for ; Tue, 26 May 2026 06:25:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 056D24B9DB5F Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=cmtQ631C X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-dy1-x132b.google.com (mail-dy1-x132b.google.com [IPv6:2607:f8b0:4864:20::132b]) by sourceware.org (Postfix) with ESMTPS id 670F94BA7986 for ; Tue, 26 May 2026 06:18:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 670F94BA7986 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 670F94BA7986 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::132b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779776289; cv=none; b=XVN/iDUaxGIlBMBvphfClF0HY0QMVR8HeNKdgW0p3SKoCmo5t76BPKjkdJXAFFqTB6CGjEI6TM9StcWbxK2Opvp89+qyZJCzaVyVHMISyA5x7rumJEbC6u5xjyDy6ChmHh5zFpTb4wwq3BcCJFfse21EN//8AjFp1f8VXCsV/hs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779776289; c=relaxed/simple; bh=gsM9hijP1PqYvHmjVAPHHVkbkI1Yov7g4w9D+qyx5gs=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Mqy/qLwU709A6O8lxOdMV/k9KpwXcdQgsotmpoemUwJzjLJU/ZHDS0IdXjmWrEPRx0QeDrBPcXeTMbItwT2HjDCI6axv8qh9mA6VdnpyZx/++BjW8cDUcfKNXAvuNuK33f3yuSg+ROzszFwm1tPEbHIpPoOLLBDbZcyyoAzi0/E= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=cmtQ631C DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 670F94BA7986 Received: by mail-dy1-x132b.google.com with SMTP id 5a478bee46e88-304545e6c7fso3855908eec.0 for ; Mon, 25 May 2026 23:18:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1779776288; x=1780381088; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=F3UPFyj/sfFJTTWw+M14ZxF7dbNG/AKJ4tBsMVWE8Ag=; b=cmtQ631C8v1BoVYaDnQyGiCkjOdHidi6kqNGRNiw1Lx995JE2ZoDdWUCGB4kGpI8lg 6Wm0fR5pvhASorU7N5hKzgNLCHy42aVuhnl0nP/kdphS+9IthRv3s7l9CiOvrFRSUuiR moKQ4xs2VuAS92WlJQXfLgSL6JjZFloFy3gM857lLOxmNs1GyLmTlbADrc5rqFOIglCp cvrawU7pcBLu3zMQr/wAcPHryJECh0qT9nas6dOiSUmOwb3SfMsHw0VJcfVm9a3ur9rC IKyNooVOzFUXNXCykM2QkVzrtYGhwdS3DrFTXFu/07Aeeft11MqsNKr39GOdxWSQejRt nbJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779776288; x=1780381088; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject: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z3LzTvHxmD0t21XYOjbryoHc C8vlnnzDGNPhXfnFn17af/Y3sPfIOZp4hB1cVlYGkM6svKK/+AoPg5lN7IrPA8NtAahATcv3xGm 5pWcoyTDFMQdYTNgh4eUBbl5llBnNV9+t96B5zdegj X-Received: by 2002:a05:7300:e424:b0:304:819f:5029 with SMTP id 5a478bee46e88-304819f50famr3123286eec.2.1779776288205; Mon, 25 May 2026 23:18:08 -0700 (PDT) Received: from sw08.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-30452461cb5sm14062504eec.31.2026.05.25.23.18.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 May 2026 23:18:07 -0700 (PDT) From: Jesse Huang To: libc-alpha@sourceware.org Cc: andrew@sifive.com, darius@bluespec.com, debug@rivosinc.com, jeffreyalaw@gmail.com, kito.cheng@sifive.com, palmer@dabbelt.com, Jesse Huang , Jerry Zhang Jian Subject: [PATCH v4 13/17] riscv/cfi: Add internal sigset_t union and use it for both ucontext/jmpbuf Date: Mon, 25 May 2026 23:16:59 -0700 Message-Id: <20260526061703.2188042-14-jesse.huang@sifive.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20260526061703.2188042-1-jesse.huang@sifive.com> References: <20260526061703.2188042-1-jesse.huang@sifive.com> MIME-Version: 1.0 X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLOCKED shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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 Co-authored-by: Jerry Zhang Jian --- .../sysv/linux/riscv/bits/types/__sigset_t.h | 43 +++++++++++++++++++ sysdeps/unix/sysv/linux/riscv/setjmpP.h | 31 +------------ sysdeps/unix/sysv/linux/riscv/sys/ucontext.h | 14 +++++- 3 files changed, 57 insertions(+), 31 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/riscv/bits/types/__sigset_t.h diff --git a/sysdeps/unix/sysv/linux/riscv/bits/types/__sigset_t.h b/sysdeps/unix/sysv/linux/riscv/bits/types/__sigset_t.h new file mode 100644 index 0000000000..d83c76876d --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/bits/types/__sigset_t.h @@ -0,0 +1,43 @@ +/* Architecture-specific __sigset_t definition. RISC-V version. */ +#ifndef ____sigset_t_defined +#define ____sigset_t_defined + +#define _SIGSET_NWORDS (1024 / (8 * sizeof (unsigned long int))) +typedef struct +{ + unsigned long int __val[_SIGSET_NWORDS]; +} __sigset_t; + +#define __ALIGN_DOWN(base, size) ((base) & -((__typeof__ (base)) (size))) +#define __ALIGN_UP(base, size) __ALIGN_DOWN ((base) + (size) - 1, (size)) + +/* Number of bits per long. */ +#define _SSP_SIGSET_BITS_PER_WORD (8 * sizeof (unsigned long int)) +/* This holds the number of signals, 512 should be sufficient for future. + expansion */ +#define _SSP_SIGSET_NSIG 512 +/* Number of longs to hold all signals. */ +#define _SSP_SIGSET_NWORDS \ + (__ALIGN_UP (_SSP_SIGSET_NSIG, _SSP_SIGSET_BITS_PER_WORD) \ + / _SSP_SIGSET_BITS_PER_WORD) + +typedef struct + { + unsigned long int __val[_SSP_SIGSET_NWORDS]; + } __ssp_sigset_t; + +typedef union + { + __sigset_t __saved_mask_compat; + struct + { + __ssp_sigset_t __saved_mask; + /* Used for shadow stack pointer. NB: Shadow stack pointer + must have the same alignment as __saved_mask. Otherwise + offset of __saved_mask will be changed. */ + unsigned long int __ssp; + unsigned long int __ssp_base; + } __saved; + } __ssp_sigset_arch_t; + +#endif diff --git a/sysdeps/unix/sysv/linux/riscv/setjmpP.h b/sysdeps/unix/sysv/linux/riscv/setjmpP.h index 43cb28e2d1..b237f7baf4 100644 --- a/sysdeps/unix/sysv/linux/riscv/setjmpP.h +++ b/sysdeps/unix/sysv/linux/riscv/setjmpP.h @@ -23,42 +23,13 @@ #include #include -/* Number of bits per long. */ -#define _JUMP_BUF_SIGSET_BITS_PER_WORD (8 * sizeof (unsigned long int)) -/* This holds the number of signals, 512 should be sufficient for future. - expansion */ -#define _JUMP_BUF_SIGSET_NSIG 512 -/* Number of longs to hold all signals. */ -#define _JUMP_BUF_SIGSET_NWORDS \ - (ALIGN_UP (_JUMP_BUF_SIGSET_NSIG, _JUMP_BUF_SIGSET_BITS_PER_WORD) \ - / _JUMP_BUF_SIGSET_BITS_PER_WORD) - -typedef struct - { - unsigned long int __val[_JUMP_BUF_SIGSET_NWORDS]; - } __jmp_buf_sigset_t; - -typedef union - { - __sigset_t __saved_mask_compat; - struct - { - __jmp_buf_sigset_t __saved_mask; - /* Used for shadow stack pointer. NB: Shadow stack pointer - must have the same alignment as __saved_mask. Otherwise - offset of __saved_mask will be changed. */ - unsigned long int __ssp; - unsigned long int __ssp_base; - } __saved; - } __jmpbuf_arch_t; - /* has NB: We use setjmp in thread cancellation and this saves the shadow stack register, but __libc_unwind_longjmp doesn't restore the shadow stack register since cancellation never returns after longjmp. */ #undef __sigset_t -#define __sigset_t __jmpbuf_arch_t +#define __sigset_t __ssp_sigset_arch_t #include #undef __saved_mask #define __saved_mask __saved_mask.__saved.__saved_mask diff --git a/sysdeps/unix/sysv/linux/riscv/sys/ucontext.h b/sysdeps/unix/sysv/linux/riscv/sys/ucontext.h index 312be3cfc3..37cfc41fb6 100644 --- a/sysdeps/unix/sysv/linux/riscv/sys/ucontext.h +++ b/sysdeps/unix/sysv/linux/riscv/sys/ucontext.h @@ -23,6 +23,7 @@ #include +#include #include #include @@ -90,7 +91,18 @@ typedef struct ucontext_t unsigned long int __uc_flags; struct ucontext_t *uc_link; stack_t uc_stack; - sigset_t uc_sigmask; + /* Internal overlay for uc_sigmask to store CFI shadow stack state while + keeping the public API type as sigset_t. */ + union + { + sigset_t uc_sigmask; /* Public view. */ + struct + { + __ssp_sigset_t __saved_mask; + unsigned long int __ssp; + unsigned long int __ssp_base; + } __saved; /* Internal view. */ + }; /* There's some padding here to allow sigset_t to be expanded in the future. Though this is unlikely, other architectures put uc_sigmask at the end of this structure and explicitly state it can be