Bug 56742 - [4.8/4.9 regression] Optimization bug lead to uncaught throw
Summary: [4.8/4.9 regression] Optimization bug lead to uncaught throw
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 4.8.0
: P3 normal
Target Milestone: 4.8.2
Assignee: Richard Henderson
URL:
Keywords: wrong-code
: 56747 (view as bug list)
Depends on:
Blocks:
 
Reported: 2013-03-26 15:56 UTC by Kai Tietz
Modified: 2014-01-14 21:20 UTC (History)
8 users (show)

See Also:
Host:
Target: x86_64-w64-mingw32, x86_64-pc-cygwin
Build:
Known to work:
Known to fail: 4.8.0
Last reconfirmed: 2013-03-26 00:00:00


Attachments
proposed patch (863 bytes, patch)
2013-05-22 20:57 UTC, Richard Henderson
Details | Diff
proposed patch 2 (1004 bytes, patch)
2013-05-22 23:19 UTC, Richard Henderson
Details | Diff
save output log and debug window log (at end) (926 bytes, text/plain)
2014-01-14 21:15 UTC, at2010
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kai Tietz 2013-03-26 15:56:04 UTC
Hi,

the following testcase:

#include <string>

static int main_worker(int argc)
{
  std::string s[32]; // [31] => no segfault
  if (argc < 2)
    throw 42;
  return argc;
}

int main(int argc, char **argv)
{
  try {
    return main_worker(argc);
  }
  catch (int i) {
    return i;
  }
}

produces with optimization -O2 on execution the message:
 "terminate called after throwing an instance of 'int'"
and abort gets called.

If compiled with optimization level -O1, execution works as expected.
Comment 1 Andrew Pinski 2013-03-26 20:11:28 UTC
*** Bug 56747 has been marked as a duplicate of this bug. ***
Comment 2 Kai Tietz 2013-03-26 21:14:23 UTC
Hmm, yes indeed it is the -freorder-blocks option. One solution is to disallow after reload for SEH-target to modify jumps.

The following patch fixes the issue for me.


Index: i386.c
===================================================================
--- i386.c      (Revision 197118)
+++ i386.c      (Arbeitskopie)
@@ -3941,6 +3941,19 @@
   register_pass (&insert_vzeroupper_info);
 }

+/* Implement TARGET_CANNOT_MODIFY_JUMPS_P.  */
+static bool
+ix86_cannot_modify_jumps_p (void)
+{
+  if (TARGET_SEH && reload_completed
+      && cfun)
+    return true;
+  return false;
+}
+
+#undef  TARGET_CANNOT_MODIFY_JUMPS_P
+#define TARGET_CANNOT_MODIFY_JUMPS_P ix86_cannot_modify_jumps_p
+
 /* Update register usage after having seen the compiler flags.  */

 static void
Comment 3 Steven Bosscher 2013-03-26 23:43:40 UTC
(In reply to comment #2)
> Hmm, yes indeed it is the -freorder-blocks option. One solution is to 
> disallow after reload for SEH-target to modify jumps.

That's an awfully big hammer. Perhaps it's possible to first analyze
what is actually happening in bbreorder that causes this bug?
Comment 4 Kai Tietz 2013-03-27 09:40:16 UTC
(In reply to comment #3)
> (In reply to comment #2)
> > Hmm, yes indeed it is the -freorder-blocks option. One solution is to 
> > disallow after reload for SEH-target to modify jumps.
> 
> That's an awfully big hammer. Perhaps it's possible to first analyze
> what is actually happening in bbreorder that causes this bug?

Well, the issue is analyzed.  The issue is that SEH is table-based EH, and so after bb-reorder used labels for describing eh-regions are getting invalid.
If you take a look to the produces assembly for the example, you will notice that easily.
I admit that the first patch is a bit too invasive, as it disables bb-reorder in all cases.  But in fact we need to disable it just if there is a catch-region. A more improved variant is:

Index: i386.c
===================================================================
--- i386.c      (Revision 197118)
+++ i386.c      (Arbeitskopie)
@@ -3941,6 +3941,20 @@
   register_pass (&insert_vzeroupper_info);
 }

+/* Implement TARGET_CANNOT_MODIFY_JUMPS_P.  */
+static bool
+ix86_cannot_modify_jumps_p (void)
+{
+  if (TARGET_SEH && reload_completed
+      && cfun && cfun->eh
+      && cfun->eh->region_tree)
+    return true;
+  return false;
+}
+
+#undef  TARGET_CANNOT_MODIFY_JUMPS_P
+#define TARGET_CANNOT_MODIFY_JUMPS_P ix86_cannot_modify_jumps_p
+
 /* Update register usage after having seen the compiler flags.  */

 static void
Comment 5 Jason Merrill 2013-03-27 13:48:42 UTC
I can't reproduce this on x86_64-unknown-linux-gnu.

Is it a regression?

Changing component to rtl-optimization.
Comment 6 Kai Tietz 2013-03-27 14:43:24 UTC
Well, this issue is related to SEH exceptions.  So it is pretty clear, why you don't see it for linux.

It is serious bug for 4.8 gcc.  From user's perspective this is a regression.  Technical it is a bug in bb-reorder for SEH.
For 4.8 I think the above patch is the lowest invasive way to fix it.  For trunk we might be able to use an approach as dw2 uses here.  Not sure about later.  I will discuss with Richard Henderson.
Comment 7 Jakub Jelinek 2013-04-26 18:16:54 UTC
Does it work with 4.9?  If not, it should be marked as 4.8/4.9 Regression.
Comment 8 Richard Henderson 2013-05-22 20:54:58 UTC
Mine.
Comment 9 Richard Henderson 2013-05-22 20:57:21 UTC
Created attachment 30168 [details]
proposed patch

Kudos to Kai for finally figuring out what was going wrong inside the
system unwinder, and why.

I believe this patch will fix the problem -- at least it does for this
test case.  A full test cycle is just commencing...
Comment 10 Richard Henderson 2013-05-22 23:19:48 UTC
Created attachment 30169 [details]
proposed patch 2

Fixes a dwarf2 ICE caused by the previous version.
Full testing just restarting...
Comment 11 Richard Henderson 2013-05-31 23:18:01 UTC
Fixed.
Comment 12 at2010 2014-01-14 21:15:33 UTC
Created attachment 31834 [details]
save output log and debug window log (at end)
Comment 13 at2010 2014-01-14 21:20:41 UTC
Hello.

I also observed the 64bit compile problem while muxing. And after using the new build the problem is indeed resolved. However I still see a remnant wxwiget error in the logs that you may wish to fix as well.
Error is
In file /home/mosu/prog/video/mingw/cross-git-all/tmp-wxwidgets/wxWidgets-3.0.0/src/msw/window.cpp at line 576: 'SetFocus' failed with error 0x00000057 (the parameter is incorrect.).

This is the pre-fix log. The post-fix log is below.

mkvmerge v6.7.0 ('Back to the Ground') 64bit built on Jan  8 2014 15:10:52

'notrealfilename.avi': Using the demultiplexer for the format 'AVI'.

'notrealfilename.avi' track 0: Using the output module for the format 'MPEG-4'.

'notrealfilename.avi' track 1: Using the output module for the format 'AC3'.

The file 'notrealfilename (1).mkv' has been opened for writing.

Progress: 0%

[Fails with return code 3]

Output of Debug window:
12:34:22: dpi is 96/96
12:34:22: Querying mkvmerge's capabilities
12:34:23: Capability: VERSION=mkvmerge v6.7.0 ('Back to the Ground') 64bit built on Jan  8 2014 15:10:52
12:34:23: Capability: 
12:34:23: Capability: FLAC
12:34:23: Capability: 
12:34:23: about to check... btw int? 0
12:34:23: update state changed, now 1
12:34:26: update state changed, now 2
12:34:32: identify 1: command: ``"C:\Program Files (x86)\MKVToolNix\mkvmerge.exe" "@C:\Users\notme\AppData\Local\Temp\mmg-mkvmerge-options-3992-1389731672"''
12:34:33: identify 1: result: 0
12:34:33: identify 1: output[0]: ``File 'notrealfilename.avi': container: AVI [is_providing_timecodes:1]''
12:34:33: identify 1: output[1]: ``''
12:34:33: identify 1: output[2]: ``Track ID 0: video (MPEG-4p2)''
12:34:33: identify 1: output[3]: ``''
12:34:33: identify 1: output[4]: ``Track ID 1: audio (AC3/EAC3)''
12:34:33: identify 1: output[5]: ``''
12:36:58: Locale selection logic: select_locale English (English) uu_locale_lower en translation_c::get_default_ui_locale() en app->m_ui_locale en
12:37:20: Querying mkvmerge's capabilities
12:37:20: Capability: VERSION=mkvmerge v6.7.0 ('Back to the Ground') 64bit built on Jan  8 2014 15:10:52
12:37:20: Capability: 
12:37:20: Capability: FLAC
12:37:20: Capability: 
In file /home/mosu/prog/video/mingw/cross-git-all/tmp-wxwidgets/wxWidgets-3.0.0/src/msw/window.cpp at line 576: 'SetFocus' failed with error 0x00000057 (the parameter is incorrect.).
In file /home/mosu/prog/video/mingw/cross-git-all/tmp-wxwidgets/wxWidgets-3.0.0/src/msw/window.cpp at line 576: 'SetFocus' failed with error 0x00000057 (the parameter is incorrect.).
In file /home/mosu/prog/video/mingw/cross-git-all/tmp-wxwidgets/wxWidgets-3.0.0/src/msw/window.cpp at line 576: 'SetFocus' failed with error 0x00000057 (the parameter is incorrect.).
In file /home/mosu/prog/video/mingw/cross-git-all/tmp-wxwidgets/wxWidgets-3.0.0/src/msw/window.cpp at line 576: 'SetFocus' failed with error 0x00000057 (the parameter is incorrect.).

12:57:49: dpi is 96/96
12:57:49: Querying mkvmerge's capabilities
12:57:49: Capability: VERSION=mkvmerge v6.7.0 ('Back to the Ground') 64bit built on Jan  8 2014 15:10:52
12:57:49: Capability: 
12:57:49: Capability: FLAC
12:57:49: Capability: 
12:58:00: identify 1: command: ``"C:\Program Files (x86)\MKVToolNix\mkvmerge.exe" "@C:\Users\Marty\AppData\Local\Temp\mmg-mkvmerge-options-4540-1389733080"''
12:58:01: identify 1: result: 0
12:58:01: identify 1: output[0]: ``File 'notrealfilename.avi': container: AVI [is_providing_timecodes:1]''
12:58:01: identify 1: output[1]: ``''
12:58:01: identify 1: output[2]: ``Track ID 0: video (MPEG-4p2)''
12:58:01: identify 1: output[3]: ``''
12:58:01: identify 1: output[4]: ``Track ID 1: audio (AC3/EAC3)''
12:58:01: identify 1: output[5]: ``''
In file /home/mosu/prog/video/mingw/cross-git-all/tmp-wxwidgets/wxWidgets-3.0.0/src/msw/window.cpp at line 576: 'SetFocus' failed with error 0x00000057 (the parameter is incorrect.).


mkvmerge v6.7.0 ('Back to the Ground') 64bit built on Jan 11 2014 10:17:39

'notrealfilename.avi': Using the demultiplexer for the format 'AVI'.

'notrealfilename.avi' track 0: Using the output module for the format 'MPEG-4'.

'notrealfilename.avi' track 1: Using the output module for the format 'AC3'.

The file 'notrealfilename (2).mkv' has been opened for writing.


Progress: 0%
[omitted lines]
Progress: 100%

The cue entries (the index) are being written...
Muxing took 53 seconds.


Output of Debug window:
13:01:17: dpi is 96/96
13:01:17: Querying mkvmerge's capabilities
13:01:17: Capability: VERSION=mkvmerge v6.7.0 ('Back to the Ground') 64bit built on Jan 11 2014 10:17:39
13:01:17: Capability: 
13:01:17: Capability: FLAC
13:01:17: Capability: 
13:01:36: identify 1: command: ``"C:\Program Files (x86)\MKVToolNix\mkvmerge.exe" "@C:\Users\notme\AppData\Local\Temp\mmg-mkvmerge-options-4456-1389733296"''
13:01:37: identify 1: result: 0
13:01:37: identify 1: output[0]: ``File 'notrealfilename.avi': container: AVI [is_providing_timecodes:1]''
13:01:37: identify 1: output[1]: ``Track ID 0: video (MPEG-4p2)''
13:01:37: identify 1: output[2]: ``Track ID 1: audio (AC3/EAC3)''
13:01:37: identify 1: output[3]: ``''
In file /home/mosu/prog/video/mingw/cross-git-all/tmp-wxwidgets/wxWidgets-3.0.0/src/msw/window.cpp at line 576: 'SetFocus' failed with error 0x00000057 (the parameter is incorrect.).