]> gcc.gnu.org Git - gcc.git/commitdiff
stack-protector: Check stack canary before throwing exception
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 14 Jul 2022 15:23:38 +0000 (08:23 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Wed, 28 Sep 2022 01:29:20 +0000 (18:29 -0700)
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
gcc/testsuite/g++.dg/fstack-protector-strong.C
gcc/testsuite/g++.dg/pr58245-1.C [new file with mode: 0644]

index bc96aff38f063cad2d7484babb16d80781e304e5..6dd6f73e978040f78931f90ca153bff10b55bdbd 100644 (file)
@@ -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 ();
index ae6d2fdb8df0dbde5e42afcd975ded4f049847c2..034af2ce9abe9545b37b5103fad45e3ded5376c5 100644 (file)
@@ -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 (file)
index 0000000..1439bc6
--- /dev/null
@@ -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 } } */
This page took 0.069742 seconds and 5 git commands to generate.