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: 56827 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 AC54A3858422 for ; Wed, 17 Aug 2022 22:19:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AC54A3858422 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1660774768; bh=W4xELiYbsjbWSV77zkIKrK8iQoO10Uwzuoo3KpXLr8Y=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=N9aTOkBtGeH9Ia1dDqciv/4FKswTDc/kEFyZpk8l8FBrVzejminIgp9yEe1lJhTzQ Sr8jLOrcdXx5Wb0nWEQzZ20mQseTUbWK0Ekk1qb2P9W7vHFWWoY7CuZ6iLAXp2LNHx J52DUThd41+6nPwKph3vVbAZEfMQ4IINfWYaGxBY= 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-Google-Smtp-Source: AA6agR6ZSEwkvb0ilHFltyd/rG5imkvB1rz2tcjwqY/Ffv6Q0EfRszlJPYFfBXkXmnUsXznusWl37g== 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 } } */