From patchwork Wed Aug 17 22:18:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 588 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:38f:b0:2d5:3c95:9e21 with SMTP id 15csp2927185pxh; Wed, 17 Aug 2022 15:19:43 -0700 (PDT) X-Google-Smtp-Source: AA6agR69irGaIu6Aqr83PIB3y5ddNBbD+mZkYAL4bdA85gyFWU14IVVEs9E7+fLMRoMTSDZzu41c X-Received: by 2002:a05:6402:2684:b0:43d:9964:d2a0 with SMTP id w4-20020a056402268400b0043d9964d2a0mr137914edd.250.1660774783691; Wed, 17 Aug 2022 15:19:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660774783; cv=none; d=google.com; s=arc-20160816; b=yzHPCNhmCyfjMNMXfbEKD74RWYVsUchD0h6GDOueZKqwkolSLg6bnNtD9DeyBMHEhK 3gLIPIr53mMig+nWaC+48swH/5BbEYVdpkZdHCXHtIcpnQOOWkwpQdkxMVSePzgogIce UBi61wAzkMgoEShKm45iJn7srmbM2Y6KDuBQ76ygCWPSsfZ7bGUIO2OT0MMutZlzPSFC 4FfSS6/MzZpMnb/Wm7JA2qTQ/MKkDk+X66gDwRHL6h3OWBSzXv0XwPxgNu2zQ3AEkcaQ XEw5+uoBrSILwmNfey3Heq51SrvfNMOI/mUPSuQ/MqFWF9CBgFQrdvWrcVMfCwTsBKxj AdKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=W4xELiYbsjbWSV77zkIKrK8iQoO10Uwzuoo3KpXLr8Y=; b=rdgVppJVwv0uiyni/bNISUM0LXdbs699kStynZ8QTCuVacFsnGN2sbSEpap6CB29aw RtwGLO1SFk8IlRwbMrFqCNEHa6C6bWi3otjBnivNWTrxojKKUdTQyRzf4FpLR5i42shP dz3xt5dq/mt9l1TlxCGOkcat2rmvwhb1ukhDpD6WOiz3D3gehZXExo65UxkRqS3Hx+TV tVwp9gX/W71jF6hOetkFe45+Q42P3o8Gfph9+0K84gCs4azbPZynLwe26CNPB9le8dw0 j0YsPXhhMkZu67PAyshm6b1ctqaRuoIGsRULDbhy8CsboVx6UEjxbYQt9VFn81LDwkgB e+Bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=qW0i4LzJ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id h9-20020a05640250c900b0043e33c1d5eesi14899655edb.230.2022.08.17.15.19.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Aug 2022 15:19:43 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=qW0i4LzJ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8D72C3858285 for ; Wed, 17 Aug 2022 22:19:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8D72C3858285 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1660774782; bh=W4xELiYbsjbWSV77zkIKrK8iQoO10Uwzuoo3KpXLr8Y=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=qW0i4LzJQN0MKn6LGavOM2QdSaVn+GLndGg1I0IMyvDJKqJka5GwyqCLrANpvi2GS FegoR2sl0rTj2pjz4xN7okH41il3s9jYDnNyHcw1MxwFFbWYIbjVz4Hd5XBxJl2dPK YAA1ul070BRV/GcmMIs3WcbninuaLJVhfMBuISOw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by sourceware.org (Postfix) with ESMTPS id F2E7A3858C50 for ; Wed, 17 Aug 2022 22:18:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F2E7A3858C50 Received: by mail-pg1-x533.google.com with SMTP id c24so13091576pgg.11 for ; Wed, 17 Aug 2022 15:18:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc; bh=W4xELiYbsjbWSV77zkIKrK8iQoO10Uwzuoo3KpXLr8Y=; b=62dcy+Z+8r6ARVZAmVki/+xad4T5DkQYPwxhI+CrxvquHfSr0D4kzgW1tfRZNc/pYD Ipa/qH+LMc4rGshxMSUU2EqwkDXnlpRqCa4s3nijBwCAUCz2DBwWlGrLCJ+Y5buzzhM+ uZfMu58xp4nzwDa2B+S+ajuuWLnNbHUFEA07TVm0V6GpzDO+ajx46pgcKmIJxoqFrqEt 0YmejXsFp6gLPWfJJ8gypS2qD1NpA5YV79lXHKjE0P8L+pvlSBC2J56Yjjwg1v7wb1dM cFVGpqRPWpzWWKyvCTNr0Y+Dq48Op8l8upZj4334hx/9w7z88WSLhL9CLbn4DUbUQ3Ld tSaQ== X-Gm-Message-State: ACgBeo0uBJExAnVRu7l9DAZGeQmpGz6DsvfPNibr6CMwaM/sh/5Jh8JG w9ttkWDgt4hDoKPykzWAFnYXeGf5g/g= X-Received: by 2002:a65:6216:0:b0:41d:8248:3d05 with SMTP id d22-20020a656216000000b0041d82483d05mr275472pgv.36.1660774737598; Wed, 17 Aug 2022 15:18:57 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.56.38.79]) by smtp.gmail.com with ESMTPSA id a1-20020a170902710100b0016c50179b1esm412800pll.152.2022.08.17.15.18.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Aug 2022 15:18:57 -0700 (PDT) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 00584C00EF for ; Wed, 17 Aug 2022 15:18:55 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH v2] stack-protector: Check stack canary before throwing exception Date: Wed, 17 Aug 2022 15:18:55 -0700 Message-Id: <20220817221855.35043-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 X-Spam-Status: No, score=-3025.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: 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: "H.J. Lu via Gcc-patches" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1741448579901707138?= X-GMAIL-MSGID: =?utf-8?q?1741448579901707138?= Check stack canary before throwing exception to avoid stack corruption. gcc/ PR middle-end/58245 * calls.cc: Include "tree-eh.h". (expand_call): Check stack canary before throwing exception. gcc/testsuite/ PR middle-end/58245 * g++.dg/fstack-protector-strong.C: Adjusted. * g++.dg/pr58245-1.C: New test. --- gcc/calls.cc | 6 +++++- gcc/testsuite/g++.dg/fstack-protector-strong.C | 2 +- gcc/testsuite/g++.dg/pr58245-1.C | 10 ++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/pr58245-1.C diff --git a/gcc/calls.cc b/gcc/calls.cc index bc96aff38f0..6dd6f73e978 100644 --- a/gcc/calls.cc +++ b/gcc/calls.cc @@ -60,6 +60,7 @@ along with GCC; see the file COPYING3. If not see #include "attr-fnspec.h" #include "value-query.h" #include "tree-pretty-print.h" +#include "tree-eh.h" /* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */ #define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT) @@ -3154,7 +3155,10 @@ expand_call (tree exp, rtx target, int ignore) if (pass && (flags & ECF_MALLOC)) start_sequence (); - if (pass == 0 + /* Check the canary value for sibcall or function which doesn't + return and could throw. */ + if ((pass == 0 + || ((flags & ECF_NORETURN) != 0 && tree_could_throw_p (exp))) && crtl->stack_protect_guard && targetm.stack_protect_runtime_enabled_p ()) stack_protect_epilogue (); diff --git a/gcc/testsuite/g++.dg/fstack-protector-strong.C b/gcc/testsuite/g++.dg/fstack-protector-strong.C index ae6d2fdb8df..034af2ce9ab 100644 --- a/gcc/testsuite/g++.dg/fstack-protector-strong.C +++ b/gcc/testsuite/g++.dg/fstack-protector-strong.C @@ -85,4 +85,4 @@ int foo7 (B *p) return p->return_slot ().a1; } -/* { dg-final { scan-assembler-times "stack_chk_fail" 7 } } */ +/* { dg-final { scan-assembler-times "stack_chk_fail" 8 } } */ diff --git a/gcc/testsuite/g++.dg/pr58245-1.C b/gcc/testsuite/g++.dg/pr58245-1.C new file mode 100644 index 00000000000..1439bc62e71 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr58245-1.C @@ -0,0 +1,10 @@ +/* { dg-do compile { target i?86-*-* x86_64-*-* rs6000-*-* s390x-*-* } } */ +/* { dg-options "-O2 -fstack-protector-all" } */ + +void +bar (void) +{ + throw 1; +} + +/* { dg-final { scan-assembler-times "stack_chk_fail" 1 } } */