From patchwork Tue Aug 18 15:42:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Uros Bizjak X-Patchwork-Id: 1347076 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: 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=WfJitzmD; dkim-atps=neutral Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BWFYc490gz9sRK for ; Wed, 19 Aug 2020 01:43:14 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 52D2F3850407; Tue, 18 Aug 2020 15:43:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 52D2F3850407 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1597765389; bh=TFfgx6Z/XsvHxuyTP5ZLQpsEkULAygb9834lJTr1Zwk=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=WfJitzmDpwDF/yjNxeiDkqbTuSLaTeWogGW01k7Lm4pI08TXwtJRT933GVmQ+4kK5 vtfgSHk/2xy8xSjKGKvhl4RoGrDFGIIR9LDaLM7NH3hiV5TNHTK+te/BfqZQ0mEVTE QSrWlqK869ooS3b1zfMcYEVmNM50HUyXMkYm6mNo= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by sourceware.org (Postfix) with ESMTPS id 489343857C4D for ; Tue, 18 Aug 2020 15:43:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 489343857C4D Received: by mail-qk1-x72b.google.com with SMTP id 2so18616075qkf.10 for ; Tue, 18 Aug 2020 08:43:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to:cc; bh=TFfgx6Z/XsvHxuyTP5ZLQpsEkULAygb9834lJTr1Zwk=; b=W9AGdEEvxzSrFowVAQQIM7XZhjW/6FYrQ5ieg4woPA9keKOXjXx26oS55PdDBw8o+x VLInn0Ir3iCD4RhIF9NQU2jyLZh4pQLmrfCdwtTq/gzdOqD+69G6s8yoWIHdGnLlqZq9 j4tyVlugaM2m9nhZIdZLX0H/N9pv6MEG9i4oCTT9OxYWIWP2Xe6TsvLKhOFRvgfMrW2l 932GUtWGm9OYdn7rrc9MLqnPSpE8Ty2uUvUobtvNlc1QnoqbXbwYmQ/eUWyWtHmB9/so 33hKlfYWmAT/vB2fjRevV+uYwS3KzqJHkeXys2IMGvYEX9fgi7U3Wk6vCFad8CYNW9QP yGRA== X-Gm-Message-State: AOAM533BD9StVTlPIvpe7NK/JNwzkBrsSuQuJ93KRxf5djkt+AnInIHY BOODrw4ZAJv0sF6NM2pqi7WapH40tuvZVGMe25dSmexIx6ojCg== X-Google-Smtp-Source: ABdhPJwzJvF+QdsdLbDLv3RyS/LxnpJT9RhjPzkWmA2OADmXGARNatDzatCMd/6+kLMKrqe0GbvOokVWpz9EmWcvos8= X-Received: by 2002:a37:6783:: with SMTP id b125mr17289228qkc.465.1597765385627; Tue, 18 Aug 2020 08:43:05 -0700 (PDT) MIME-Version: 1.0 Date: Tue, 18 Aug 2020 17:42:54 +0200 Message-ID: Subject: [committed] i386: Rewrite restore_stack_nonlocal expander [PR96536]. To: "gcc-patches@gcc.gnu.org" X-Spam-Status: No, score=-9.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org 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: Uros Bizjak via Gcc-patches From: Uros Bizjak Reply-To: Uros Bizjak Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" -fcf-protection code in restore_stack_nonlocal uses a branch based on a clobber result. The patch adds missing compare and completely rewrites the expander to use high-level functions in RTL construction. 2020-08-18 Uroš Bizjak gcc/ChangeLog: PR target/96536 * config/i386/i386.md (restore_stack_nonlocal): Add missing compare RTX. Rewrite expander to use high-level functions in RTL construction. Bootstrapped and regression tested on x86_64-linux-gnu {,-m32}. Also bootstrapped and regtested by Hongtao on CET enabled target (thanks). Uros. diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index 3985c771d00..05af8639dbc 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -19133,15 +19133,17 @@ "" { rtx stack_slot; - if ((flag_cf_protection & CF_RETURN)) + + if (flag_cf_protection & CF_RETURN) { - /* Copy shadow stack pointer to the first slot and stack ppointer - to the second slot. */ + /* Copy shadow stack pointer to the first slot + and stack pointer to the second slot. */ rtx ssp_slot = adjust_address (operands[0], word_mode, 0); stack_slot = adjust_address (operands[0], Pmode, UNITS_PER_WORD); - rtx ssp = force_reg (word_mode, const0_rtx); - emit_insn (gen_rdssp (word_mode, ssp, ssp)); - emit_move_insn (ssp_slot, ssp); + + rtx reg_ssp = force_reg (word_mode, const0_rtx); + emit_insn (gen_rdssp (word_mode, reg_ssp, reg_ssp)); + emit_move_insn (ssp_slot, reg_ssp); } else stack_slot = adjust_address (operands[0], Pmode, 0); @@ -19155,95 +19157,64 @@ "" { rtx stack_slot; - if ((flag_cf_protection & CF_RETURN)) + + if (flag_cf_protection & CF_RETURN) { - /* Restore shadow stack pointer from the first slot and stack - pointer from the second slot. */ + /* Restore shadow stack pointer from the first slot + and stack pointer from the second slot. */ rtx ssp_slot = adjust_address (operands[1], word_mode, 0); stack_slot = adjust_address (operands[1], Pmode, UNITS_PER_WORD); - rtx flags, jump, noadj_label, inc_label, loop_label; - rtx reg_adj, reg_ssp, tmp, clob; - /* Get the current shadow stack pointer. The code below will check if SHSTK feature is enabled. If it is not enabled the RDSSP instruction is a NOP. */ - reg_ssp = force_reg (word_mode, const0_rtx); + rtx reg_ssp = force_reg (word_mode, const0_rtx); emit_insn (gen_rdssp (word_mode, reg_ssp, reg_ssp)); - /* Compare through substraction the saved and the current ssp to decide - if ssp has to be adjusted. */ - tmp = gen_rtx_SET (reg_ssp, gen_rtx_MINUS (word_mode, reg_ssp, - ssp_slot)); - clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG)); - tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob)); - emit_insn (tmp); + /* Compare through subtraction the saved and the current ssp + to decide if ssp has to be adjusted. */ + reg_ssp = expand_simple_binop (word_mode, MINUS, + reg_ssp, ssp_slot, + reg_ssp, 1, OPTAB_DIRECT); /* Compare and jump over adjustment code. */ - noadj_label = gen_label_rtx (); - flags = gen_rtx_REG (CCZmode, FLAGS_REG); - tmp = gen_rtx_EQ (VOIDmode, flags, const0_rtx); - tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp, - gen_rtx_LABEL_REF (VOIDmode, noadj_label), - pc_rtx); - jump = emit_jump_insn (gen_rtx_SET (pc_rtx, tmp)); - JUMP_LABEL (jump) = noadj_label; - - /* Compute the numebr of frames to adjust. */ - reg_adj = gen_lowpart (ptr_mode, reg_ssp); - tmp = gen_rtx_SET (reg_adj, - gen_rtx_LSHIFTRT (ptr_mode, - negate_rtx (ptr_mode, reg_adj), - GEN_INT ((word_mode == SImode) - ? 2 - : 3))); - clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG)); - tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob)); - emit_insn (tmp); + rtx noadj_label = gen_label_rtx (); + emit_cmp_and_jump_insns (reg_ssp, const0_rtx, EQ, NULL_RTX, + word_mode, 1, noadj_label); - /* Check if number of frames <= 255 so no loop is needed. */ - tmp = gen_rtx_COMPARE (CCmode, reg_adj, GEN_INT (255)); - flags = gen_rtx_REG (CCmode, FLAGS_REG); - emit_insn (gen_rtx_SET (flags, tmp)); + /* Compute the number of frames to adjust. */ + rtx reg_adj = gen_lowpart (ptr_mode, reg_ssp); + rtx reg_adj_neg = expand_simple_unop (ptr_mode, NEG, reg_adj, + NULL_RTX, 1); - inc_label = gen_label_rtx (); - tmp = gen_rtx_LEU (VOIDmode, flags, const0_rtx); - tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp, - gen_rtx_LABEL_REF (VOIDmode, inc_label), - pc_rtx); - jump = emit_jump_insn (gen_rtx_SET (pc_rtx, tmp)); - JUMP_LABEL (jump) = inc_label; + reg_adj = expand_simple_binop (ptr_mode, LSHIFTRT, reg_adj_neg, + GEN_INT (exact_log2 (UNITS_PER_WORD)), + reg_adj, 1, OPTAB_DIRECT); - rtx reg_255 = gen_reg_rtx (word_mode); - emit_move_insn (reg_255, GEN_INT (255)); + /* Check if number of frames <= 255 so no loop is needed. */ + rtx inc_label = gen_label_rtx (); + emit_cmp_and_jump_insns (reg_adj, GEN_INT (255), LEU, NULL_RTX, + ptr_mode, 1, inc_label); /* Adjust the ssp in a loop. */ - loop_label = gen_label_rtx (); + rtx loop_label = gen_label_rtx (); emit_label (loop_label); LABEL_NUSES (loop_label) = 1; + rtx reg_255 = force_reg (word_mode, GEN_INT (255)); emit_insn (gen_incssp (word_mode, reg_255)); - tmp = gen_rtx_SET (reg_adj, gen_rtx_MINUS (ptr_mode, - reg_adj, - GEN_INT (255))); - clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG)); - tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob)); - emit_insn (tmp); - - tmp = gen_rtx_COMPARE (CCmode, reg_adj, GEN_INT (255)); - flags = gen_rtx_REG (CCmode, FLAGS_REG); - emit_insn (gen_rtx_SET (flags, tmp)); - - /* Jump to the loop label. */ - tmp = gen_rtx_GTU (VOIDmode, flags, const0_rtx); - tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp, - gen_rtx_LABEL_REF (VOIDmode, loop_label), - pc_rtx); - jump = emit_jump_insn (gen_rtx_SET (pc_rtx, tmp)); - JUMP_LABEL (jump) = loop_label; + + reg_adj = expand_simple_binop (ptr_mode, MINUS, + reg_adj, GEN_INT (255), + reg_adj, 1, OPTAB_DIRECT); + + /* Compare and jump to the loop label. */ + emit_cmp_and_jump_insns (reg_adj, GEN_INT (255), GTU, NULL_RTX, + ptr_mode, 1, loop_label); emit_label (inc_label); LABEL_NUSES (inc_label) = 1; + emit_insn (gen_incssp (word_mode, reg_ssp)); emit_label (noadj_label);