From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1039) id E1B6D3858D28; Wed, 28 Sep 2022 01:29:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1B6D3858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664328598; bh=7rxyMM4ZkZ2VfEZD1iK1fLpvmElXBCjlzww3soCqgP0=; h=From:To:Subject:Date:From; b=H/Qw0MCegyf0tGRiGICMD8fpHP1JJehejZHEJ96kC9bzQqXC8CyPsyB1A6euDh1G7 u2DQrVCJq43TYlv198L6bEsooI/VYhYhCx4Yp3M6XzD5Zknpji7+PWH5OTzDSkK6jt zIQT0kQhZSMdkqWxKc1fD0OSL6jOwsomlg/HJOyM= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: H.J. Lu To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-2909] stack-protector: Check stack canary before throwing exception X-Act-Checkin: gcc X-Git-Author: H.J. Lu X-Git-Refname: refs/heads/master X-Git-Oldrev: 28390443ff72039ee8215fef2560fe76690af155 X-Git-Newrev: a25982ada523689c8745d7fb4b1b93c8f5dab2e7 Message-Id: <20220928012958.E1B6D3858D28@sourceware.org> Date: Wed, 28 Sep 2022 01:29:58 +0000 (GMT) List-Id: https://gcc.gnu.org/g:a25982ada523689c8745d7fb4b1b93c8f5dab2e7 commit r13-2909-ga25982ada523689c8745d7fb4b1b93c8f5dab2e7 Author: H.J. Lu Date: Thu Jul 14 08:23:38 2022 -0700 stack-protector: Check stack canary before throwing exception 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. Diff: --- 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(-) 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 } } */