From patchwork Wed Mar 28 13:04:48 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: 26501 Received: (qmail 43619 invoked by alias); 28 Mar 2018 13:04:58 -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 43536 invoked by uid 89); 28 Mar 2018 13:04:57 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.3 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=unavailable version=3.3.2 spammy=pop, saved X-HELO: mail-oi0-f46.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=u6vdlDwksaRnXwxa14mWRXLdK6jpFv4erPSfngA/DV0=; b=VNpgIpxufBnodTdVVW2iA+WC8REZUxcGKgIn48Io4XgqJruil3qwSZCW+C5nVfOcvL 4Eln2J04el1wxs0H0Z+tKSCxkOswMfzdSSoH7MgffVa8NIL1AEGQPYSPmB3GlqIj0/Kr hzFbVw0OsEmnAIDnoVh/qqwKDmmnlpyMZsH5WsVn5QvoRoPQe1UepSCe+7+a3Sd/aC3m tLWXO/sGMoWJmJIokw6cyiW0UpPfOC5p8T/EfldXfbt8drntq7qpPGO3VIvynK1Z2cdE BJ2kjqs8h5nVeoln1AQhRCTL78vSY8F7cuq6RKK/hR3msF/SysS8axz2DvZ1bN8Z7PPX KacQ== X-Gm-Message-State: AElRT7FNOgkTm7gW1g0Y8wRIu0wqYwKTL+fiz6ExBheMAOsZESr4oSJo 8rhbcxlZ8rQAZX7BMUmmtFmJUeSzxaZJjc1n+bs= X-Google-Smtp-Source: AIpwx4/V88MGg/Bohy4Hi9n1K7bg/dHNyozdUmqeeEO+rTWQklrHetkOZe/F8ZS6WDOowH043dgpPxSkqjxFmlBm+EQ= X-Received: by 10.202.199.67 with SMTP id x64mr2124346oif.100.1522242289504; Wed, 28 Mar 2018 06:04:49 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: "H.J. Lu" Date: Wed, 28 Mar 2018 06:04:48 -0700 Message-ID: Subject: Re: RFA: Need to extend x86 psABI to support thread cancellation and alternate signal stack To: Florian Weimer Cc: GNU C Library , GCC Development , "Carlos O'Donell" On Tue, Mar 27, 2018 at 8:55 AM, H.J. Lu wrote: > On Tue, Mar 27, 2018 at 8:43 AM, Florian Weimer wrote: >> On 03/27/2018 01:26 PM, H.J. Lu wrote: >> >>> 2. Since shadow stack is never saved and restored by compiler, unwinder >>> in libgcc counts how many stack frame it has to unwind and uses INCSSP >>> to pop shadow stack. This can't unwind the original shadow stack when >>> the alternate shadow stack is used. _URC_NO_REASON_CANCEL >>> works only if longjmp will be used to finish stack unwinding, which is >>> the case for thread cancellation in glibc. >>> >>> Here are patches for GCC: >>> >>> >>> https://github.com/hjl-tools/gcc/commit/e9ff815941406e38fa629947af4d809b9129e860 >>> >>> and glibc: >>> >>> >>> https://github.com/hjl-tools/glibc/commit/1aec81528ab26aa8a8a7965317b6e1a8ba4526aa >>> >>> They fixed the issue. >> >> >> The patches are nice and short, but: Do they really fix the issue? They >> make cancellation work again, but they do not fix the general unwinding >> issue with alternate signal handler stacks AFAICS. > > That is true. We do support unwinding with alternate signal handler stack > using longjmp. If there is another use case of unwinding with alternate signal > handler stack, we can investigate. If this isn't a valid use case, we > don't want > to create a very complex scheme to support it. Here is a GCC only patch without changing glibc. If we are in alternate signal stack, don't try to unwind shadow stack. diff --git a/libgcc/config/i386/linux-unwind.h b/libgcc/config/i386/linux-unwind.h index f1f52334d8d..0e082873a5f 100644 --- a/libgcc/config/i386/linux-unwind.h +++ b/libgcc/config/i386/linux-unwind.h @@ -24,6 +24,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see /* Unwind shadow stack for -fcf-protection -mshstk. */ #if defined __SHSTK__ && defined __CET__ +# define _Unwind_Frames_Extra_Skip_Alternate_Signal_Stack # include "config/i386/shadow-stack-unwind.h" #endif diff --git a/libgcc/unwind.inc b/libgcc/unwind.inc index 68c08964d30..9fbde024135 100644 --- a/libgcc/unwind.inc +++ b/libgcc/unwind.inc @@ -152,6 +152,19 @@ _Unwind_ForcedUnwind_Phase2 (struct _Unwind_Exception *exc, void *stop_argument = (void *) (_Unwind_Ptr) exc->private_2; _Unwind_Reason_Code code, stop_code; unsigned long frames = 1; + int in_signal_stack = 0; +#ifdef _Unwind_Frames_Extra_Skip_Alternate_Signal_Stack + stack_t signal_stack; + + if (sigaltstack (NULL, &signal_stack) == 0 + && signal_stack.ss_flags == SS_ONSTACK) + { + void *stack = __builtin_frame_address (0); + if (stack >= signal_stack.ss_sp + && stack < (signal_stack.ss_sp + signal_stack.ss_size)) + in_signal_stack = 1; + } +#endif while (1) { @@ -193,6 +206,9 @@ _Unwind_ForcedUnwind_Phase2 (struct _Unwind_Exception *exc, frames++; } + if (in_signal_stack) + frames = 0; + *frames_p = frames; return code; }