From patchwork Sun Mar 19 15:10:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 66613 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 6C8B3383FBA3 for ; Sun, 19 Mar 2023 15:17:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6C8B3383FBA3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1679239044; bh=eY06BUzf/uIluApl37+MC1FUnJt4xIrbQH7Z5dpZC6s=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=aBiVDGhAFXuS3X+8wE4IAyTYqGuzCFoJWXdWj/JdKy29EvFMxCmWADIb0GKdkuCHo YRdIDH6ItfDCsbVo4CIO2l8oUfTaQ/2WqWhiLs/23LtEt+Ac1eJeTDo35Ik73ytWkK Uz01xeAE7q6+u/qy6kqR/sM9T3rJ6hx1/7k9MgR4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id DCF52385116F for ; Sun, 19 Mar 2023 15:11:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DCF52385116F Received: by mail-lf1-x131.google.com with SMTP id g17so12039941lfv.4 for ; Sun, 19 Mar 2023 08:11:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679238685; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eY06BUzf/uIluApl37+MC1FUnJt4xIrbQH7Z5dpZC6s=; b=SWKAfyPlL/+P+upriS8UHAQa6EdUp0NIqq7qvrfxWtgcJn9eVaFUS0bi9BzWGB48Hm 19QHhU1Qt0aSKI2EI8ESRdjUiw+KysRkqkGtOoAw/X5NCeQ7SnE0cFCqLmjKsWVDsM8r hFuQPzXWYd7pyLa+BpWCeg89vSXIdwz/xajeMMUUdUXNmiikNYr1e3wbzTPJx5ZsP+VE vp5hL71PFImVUEZ/1698F4zWTxEq9OXbGbzEaizAGSceVirryGHA1LIrZNHxPYzQCJLQ /1Sof+Klcr0Updc2+l2P2hmLsiAaagwC00dL86n1lPuDRLdi+/GcsOXxYfuq75GSa/Gs xVUw== X-Gm-Message-State: AO0yUKUt5yuSo01w0FTTbxjhvVqbNez624NZzAhbeoXU7UIUkTND7TTP m8pXhRU3uNFbJ2U3SHze65G//VUGX4txCw== X-Google-Smtp-Source: AK7set8ydJQZ8m18IuLJ6cU1FnlMeW2bD2h7jN3/O5iHNUAW/7L3IW2lht7JR7Qs4huCGjmc3+LhTQ== X-Received: by 2002:ac2:494b:0:b0:4b5:b06d:4300 with SMTP id o11-20020ac2494b000000b004b5b06d4300mr5200579lfi.29.1679238685167; Sun, 19 Mar 2023 08:11:25 -0700 (PDT) Received: from surface-pro-6.. ([2a00:1370:818c:4a57:577a:76f4:df43:5e66]) by smtp.gmail.com with ESMTPSA id m19-20020ac24253000000b004e90dee5469sm1274089lfl.157.2023.03.19.08.11.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Mar 2023 08:11:24 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Cc: Samuel Thibault , Sergey Bugaev Subject: [RFC PATCH glibc 30/34] hurd: Implement longjmp for x86_64 Date: Sun, 19 Mar 2023 18:10:13 +0300 Message-Id: <20230319151017.531737-31-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230319151017.531737-1-bugaevc@gmail.com> References: <20230319151017.531737-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Signed-off-by: Sergey Bugaev --- sysdeps/mach/hurd/x86_64/____longjmp_chk.S | 134 +++++++++++++++++++++ sysdeps/mach/hurd/x86_64/__longjmp.S | 102 ++++++++++++++++ 2 files changed, 236 insertions(+) create mode 100644 sysdeps/mach/hurd/x86_64/____longjmp_chk.S create mode 100644 sysdeps/mach/hurd/x86_64/__longjmp.S diff --git a/sysdeps/mach/hurd/x86_64/____longjmp_chk.S b/sysdeps/mach/hurd/x86_64/____longjmp_chk.S new file mode 100644 index 00000000..29f6c8c3 --- /dev/null +++ b/sysdeps/mach/hurd/x86_64/____longjmp_chk.S @@ -0,0 +1,134 @@ +/* Checked longjmp support. x86_64 Hurd version. + Copyright (C) 2001-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 +#include +#include + +#define SS_ONSTACK 1 + +/* Don't restore shadow stack register if shadow stack isn't enabled. */ +#if !SHSTK_ENABLED +# undef SHADOW_STACK_POINTER_OFFSET +#endif + + .section .rodata.str1.1,"aMS",@progbits,1 + .type longjmp_msg,@object +longjmp_msg: + .string "longjmp causes uninitialized stack frame" + .size longjmp_msg, .-longjmp_msg + + +#ifdef PIC +# define CALL_FAIL sub $8, %RSP_LP; \ + cfi_remember_state; \ + cfi_def_cfa_offset(16); \ + lea longjmp_msg(%rip), %RDI_LP; \ + call HIDDEN_JUMPTARGET(__fortify_fail); \ + nop; \ + cfi_restore_state +#else +# define CALL_FAIL sub $8, %RSP_LP; \ + cfi_remember_state; \ + cfi_def_cfa_offset(16); \ + mov $longjmp_msg, %RDI_LP; \ + call HIDDEN_JUMPTARGET(__fortify_fail); \ + nop; \ + cfi_restore_state +#endif + +/* Jump to the position specified by ENV, causing the + setjmp call there to return VAL, or 1 if VAL is 0. + void __longjmp (__jmp_buf env, int val). */ + .text +ENTRY(____longjmp_chk) + /* Restore registers. */ + mov (JB_RSP*8)(%rdi), %R8_LP + mov (JB_RBP*8)(%rdi),%R9_LP + mov (JB_PC*8)(%rdi), %RDX_LP +#ifdef PTR_DEMANGLE + PTR_DEMANGLE (%R8_LP) + PTR_DEMANGLE (%R9_LP) + PTR_DEMANGLE (%RDX_LP) +#endif + +#if !defined (SHARED) || IS_IN (rtld) +# ifdef PIC + movb __libc_tls_initialized(%rip), %r10b +# else + mobv $__libc_tls_initialized, %r10b +# endif + testb %r10b, %r10b + jz .Lok /* TLS not initialized yet */ +#endif + + movq %fs:SIGSTATE_OFFSET, %R10_LP + testq %R10_LP, %R10_LP + jz .Lok /* sigstate not initialized yet */ + + testl $SS_ONSTACK, (HURD_SIGSTATE__SIGALTSTACK__OFFSET + SIGALTSTACK__SS_FLAGS__OFFSET)(%R10_LP) + jnz .Lonstack + + /* We were on the main stack. Jumping to a higher-address + frame is always allowed, otherwise it's not allowed. */ + cmp %R8_LP, %RSP_LP + jbe .Lok + +.Lfail: CALL_FAIL + +.Lonstack: + cmpq (HURD_SIGSTATE__SIGALTSTACK__OFFSET + SIGALTSTACK__SS_SP__OFFSET)(%R10_LP), %R8_LP + jb .Loks /* Jumping below the altstack, switch */ + + movq (HURD_SIGSTATE__SIGALTSTACK__OFFSET + SIGALTSTACK__SS_SP__OFFSET)(%R10_LP), %R11_LP + addq (HURD_SIGSTATE__SIGALTSTACK__OFFSET + SIGALTSTACK__SS_SIZE__OFFSET)(%R10_LP), %R11_LP + cmpq %R11_LP, %R8_LP + jb .Lok /* Jumping inside the altstack, do not switch */ + + /* Jumping above the altstack, switch */ + +.Loks: + andl $~(SS_ONSTACK), (HURD_SIGSTATE__SIGALTSTACK__OFFSET + SIGALTSTACK__SS_FLAGS__OFFSET)(%R10_LP) + +.Lok: + /* We add unwind information for the target here. */ + cfi_def_cfa(%rdi, 0) + cfi_register(%rsp,%r8) + cfi_register(%rbp,%r9) + cfi_register(%rip,%rdx) + cfi_offset(%rbx,JB_RBX*8) + cfi_offset(%r12,JB_R12*8) + cfi_offset(%r13,JB_R13*8) + cfi_offset(%r14,JB_R14*8) + cfi_offset(%r15,JB_R15*8) + movq (JB_RBX*8)(%rdi), %rbx + movq (JB_R12*8)(%rdi), %r12 + movq (JB_R13*8)(%rdi), %r13 + movq (JB_R14*8)(%rdi), %r14 + movq (JB_R15*8)(%rdi), %r15 + /* Set return value for setjmp. */ + movl %esi, %eax + mov %R8_LP, %RSP_LP + movq %r9,%rbp + jmpq *%rdx +END (____longjmp_chk) diff --git a/sysdeps/mach/hurd/x86_64/__longjmp.S b/sysdeps/mach/hurd/x86_64/__longjmp.S new file mode 100644 index 00000000..9bbcfb2f --- /dev/null +++ b/sysdeps/mach/hurd/x86_64/__longjmp.S @@ -0,0 +1,102 @@ +/* longjmp support. x86_64/Hurd version. + Copyright (C) 2001-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 +#include +#include + +#define SS_ONSTACK 1 + +/* Don't restore shadow stack register if shadow stack isn't enabled. */ +#if !SHSTK_ENABLED +# undef SHADOW_STACK_POINTER_OFFSET +#endif + +/* Jump to the position specified by ENV, causing the + setjmp call there to return VAL, or 1 if VAL is 0. + void __longjmp (__jmp_buf env, int val). */ + .text +ENTRY(__longjmp) + /* Restore registers. */ + mov (JB_RSP*8)(%rdi), %R8_LP + mov (JB_RBP*8)(%rdi),%R9_LP + mov (JB_PC*8)(%rdi), %RDX_LP +#ifdef PTR_DEMANGLE + PTR_DEMANGLE (%R8_LP) + PTR_DEMANGLE (%R9_LP) + PTR_DEMANGLE (%RDX_LP) +#endif + +#if !defined (SHARED) || IS_IN (rtld) +# ifdef PIC + movb __libc_tls_initialized(%rip), %r10b +# else + mobv $__libc_tls_initialized, %r10b +# endif + testb %r10b, %r10b + jz .Lok /* TLS not initialized yet */ +#endif + + movq %fs:SIGSTATE_OFFSET, %R10_LP + testq %R10_LP, %R10_LP + jz .Lok /* sigstate not initialized yet */ + + testl $SS_ONSTACK, (HURD_SIGSTATE__SIGALTSTACK__OFFSET + SIGALTSTACK__SS_FLAGS__OFFSET)(%R10_LP) + jz .Lok + +.Lonstack: + cmpq (HURD_SIGSTATE__SIGALTSTACK__OFFSET + SIGALTSTACK__SS_SP__OFFSET)(%R10_LP), %R8_LP + jb .Loks /* Jumping below the altstack, switch */ + + movq (HURD_SIGSTATE__SIGALTSTACK__OFFSET + SIGALTSTACK__SS_SP__OFFSET)(%R10_LP), %R11_LP + addq (HURD_SIGSTATE__SIGALTSTACK__OFFSET + SIGALTSTACK__SS_SIZE__OFFSET)(%R10_LP), %R11_LP + cmpq %R11_LP, %R8_LP + jb .Lok /* Jumping inside the altstack, do not switch */ + + /* Jumping above the altstack, switch */ + +.Loks: + andl $~(SS_ONSTACK), (HURD_SIGSTATE__SIGALTSTACK__OFFSET + SIGALTSTACK__SS_FLAGS__OFFSET)(%R10_LP) + +.Lok: + /* We add unwind information for the target here. */ + cfi_def_cfa(%rdi, 0) + cfi_register(%rsp,%r8) + cfi_register(%rbp,%r9) + cfi_register(%rip,%rdx) + cfi_offset(%rbx,JB_RBX*8) + cfi_offset(%r12,JB_R12*8) + cfi_offset(%r13,JB_R13*8) + cfi_offset(%r14,JB_R14*8) + cfi_offset(%r15,JB_R15*8) + movq (JB_RBX*8)(%rdi), %rbx + movq (JB_R12*8)(%rdi), %r12 + movq (JB_R13*8)(%rdi), %r13 + movq (JB_R14*8)(%rdi), %r14 + movq (JB_R15*8)(%rdi), %r15 + /* Set return value for setjmp. */ + movl %esi, %eax + mov %R8_LP, %RSP_LP + movq %r9,%rbp + jmpq *%rdx +END (__longjmp) -- 2.39.2