From patchwork Thu Jan 6 18:05:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhiyao Ma X-Patchwork-Id: 1576226 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=loXgwAt/; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JVDmz3xmGz9ssD for ; Fri, 7 Jan 2022 05:06:10 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 39A883858022 for ; Thu, 6 Jan 2022 18:06:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 39A883858022 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1641492367; bh=5pFFzNE0UMfQUSOkkl8daJL/8f7rj6NdKROGWJxiPlo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=loXgwAt/4/fN+EJHffqLzwyNnAMTlFRv3Gmoo2+3XQs5LNLuvHc+QV1mdzXq/8Amm 1OivOrDawObC1Ngxsik8tmgE3Ak1pPb8vwBpSHdB5DvRUSpG/u+rXuVfR+C8v4TH+t Omeso4VR4d+pW4wsjH34OaISTLa7odxYNoIyk4Ik= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by sourceware.org (Postfix) with ESMTPS id 769EF3858409 for ; Thu, 6 Jan 2022 18:05:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 769EF3858409 Received: by mail-ed1-x52b.google.com with SMTP id b13so12514169edd.8 for ; Thu, 06 Jan 2022 10:05:24 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:thread-topic:thread-index:date :message-id:accept-language:content-language:mime-version; bh=4phXulshwyHMPVo5ol5f8MRYuuLM45/7L/uyX8QsLvY=; b=HY4L/qwj4FEvh+EkK6rMCphEuVeSayN/WKY7qNpltZuz+XeAbo9HRElD6JBTDgP2Gp 3xI78R+mSjsAB7FrcY+wPbojcU2plOG/Fc3MX79wxwKETYx/qAYhA8BGz1JWPVLoxWky IC7r7Uv3m4IxahU3YYmtyySEmjqjiDsl6LuoK4IHj2AKgdZaxVfaRCiy4w+/GoN8cxHI 6M1T6paXRXLJcNrpnQ9i236wKM1PKOW2GsNcF/squn+a85g1mjxTdUNjYGRNHD3yuqA8 1c+akZ9ClmUJiYR9TlzXP1v8oefS2ZpO0cnLb7RkxL9hc6zxhC6hhfG8jyCYImpa23gr DfbQ== X-Gm-Message-State: AOAM531JNDTQZvD8zhJh49BZdEFceoTWx8W7yVumq3E6VUBedCc2ZEKX t3993iEdPrrYU3Rp0wsXi1LZMf4HpWp59g== X-Google-Smtp-Source: ABdhPJwwfBZVxlb/upxX9MqIdOH14YUF8NkbFWFWcShKV0Jcs8wShVllDXouVXh6lZSJgt0k5SvgUQ== X-Received: by 2002:a17:907:3e8f:: with SMTP id hs15mr46274342ejc.340.1641492322859; Thu, 06 Jan 2022 10:05:22 -0800 (PST) Received: from OSZP286MB1199.JPNP286.PROD.OUTLOOK.COM ([2603:1046:c0a:206d::5]) by smtp.gmail.com with ESMTPSA id q12sm660806ejn.219.2022.01.06.10.05.21 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Jan 2022 10:05:22 -0800 (PST) To: "gcc-patches@gcc.gnu.org" Subject: [PATCH] libgcc: Fix a broken call/return address prediction Thread-Topic: [PATCH] libgcc: Fix a broken call/return address prediction Thread-Index: AQHYAyPcahUf5VEcyEGNB6oPHxM/Hw== X-MS-Exchange-MessageSentRepresentingType: 1 Date: Thu, 6 Jan 2022 18:05:18 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-Exchange-Organization-SCL: -1 X-MS-TNEF-Correlator: X-MS-Exchange-Organization-RecordReviewCfmType: 0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Zhiyao Ma via Gcc-patches From: Zhiyao Ma Reply-To: Zhiyao Ma Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch fixed a broken call/return address prediction in segmented stack implementation on x86_64 by leveraging the red-zone under the stack pointer. 2022-01-06 Zhiyao Ma libgcc/ChangeLog: * config/i386/morestack.S: Modified instructions. --- libgcc/config/i386/morestack.S | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) -- 2.25.1 diff --git a/libgcc/config/i386/morestack.S b/libgcc/config/i386/morestack.S index f1cf32dec9f..61c91ce4a35 100644 --- a/libgcc/config/i386/morestack.S +++ b/libgcc/config/i386/morestack.S @@ -213,14 +213,19 @@ __morestack_non_split: cmpl $0x185d8d4c,(%rax) je 2f - # This breaks call/return prediction, as described above. - incq 8(%rsp) # Increment the return address. + movq %rax,-8(%rsp) # Save the incremented return + # address to the red-zone below + # the stack pointer. It is + # guaranteed not to be corrupted. popq %rax # Restore register. .cfi_adjust_cfa_offset -8 # Adjust for popped register. - ret # Return to caller. + callq *-16(%rsp) # Call into the caller's + # function body. + + ret # Return to caller's prologue. 2: popq %rax # Restore register.