diff mbox series

[x86] Fix another -freorder-blocks-and-partition glitch with Windows SEH

Message ID 12750987.uLZWGnKmhe@fomalhaut
State New
Headers show
Series [x86] Fix another -freorder-blocks-and-partition glitch with Windows SEH | expand

Commit Message

Eric Botcazou April 19, 2021, 8:27 a.m. UTC
Since GCC 8, the -freorder-blocks-and-partition pass can split a function into 
hot and cold parts, thus generating 2 FDEs for a single function in DWARF for 
exception purposes and doing an equivalent trick for Windows SEH on x86-64.

Now the Windows system unwinder does not support arbitrarily large frames and 
there is even a hard limit on the encoding of the CFI, which changes the stack 
allocation strategy when it is topped and which must be reflected everywhere.

I overlooked that when implementing the -freorder-blocks-and-partition support 
back in 2018 and this results in an ICE when gigantic frames, like e.g. in the 
attached Ada testcase.

Bootstrapped on x86-64/Windows, applied on all active branches as obvious.


2021-04-19  Eric Botcazou  <ebotcazou@adacore.com>

	* config/i386/winnt.c (i386_pe_seh_cold_init): Properly deal with
	frames larger than the SEH maximum frame size.


2021-04-19  Eric Botcazou  <ebotcazou@adacore.com>

	* gnat.dg/opt92.adb: New test.
diff mbox series

Patch

diff --git a/gcc/config/i386/winnt.c b/gcc/config/i386/winnt.c
index cc121965294..b66263ad243 100644
--- a/gcc/config/i386/winnt.c
+++ b/gcc/config/i386/winnt.c
@@ -921,15 +921,17 @@  i386_pe_seh_cold_init (FILE *f, const char *name)
 
   /* In the normal case, the frame pointer is near the bottom of the frame
      so we can do the full stack allocation and set it afterwards.  There
-     is an exception when the function accesses prior frames so, in this
-     case, we need to pre-allocate a small chunk before setting it.  */
-  if (crtl->accesses_prior_frames)
-    alloc_offset = seh->cfa_offset;
-  else
+     is an exception if the function overflows the SEH maximum frame size
+     or accesses prior frames so, in this case, we need to pre-allocate a
+     small chunk of stack before setting it.  */
+  offset = seh->sp_offset - INCOMING_FRAME_SP_OFFSET;
+  if (offset < SEH_MAX_FRAME_SIZE && !crtl->accesses_prior_frames)
     alloc_offset = seh->sp_offset;
+  else
+    alloc_offset = MIN (seh->cfa_offset + 240, seh->sp_offset);
 
   offset = alloc_offset - INCOMING_FRAME_SP_OFFSET;
-  if (offset > 0 && offset < SEH_MAX_FRAME_SIZE)
+  if (offset > 0)
     fprintf (f, "\t.seh_stackalloc\t" HOST_WIDE_INT_PRINT_DEC "\n", offset);
 
   for (int regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
@@ -958,7 +960,7 @@  i386_pe_seh_cold_init (FILE *f, const char *name)
       fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
     }
 
-  if (crtl->accesses_prior_frames)
+  if (alloc_offset != seh->sp_offset)
     {
       offset = seh->sp_offset - alloc_offset;
       if (offset > 0 && offset < SEH_MAX_FRAME_SIZE)