Bug 95741 - Optimization skips destructor and calls terminate directly
Summary: Optimization skips destructor and calls terminate directly
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: EH, wrong-code
Depends on:
Blocks:
 
Reported: 2020-06-18 12:30 UTC by Marc Poulhiès
Modified: 2024-03-12 06:36 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-06-18 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marc Poulhiès 2020-06-18 12:30:59 UTC
The following code has different executions in O0 and O3.
It looks like GCC is able to optimize the `throw s` in `test()` by calling `terminate()` directly as it knows the destructor will raise an exception:

```
terminate called after throwing an instance of 'int'
Aborted
```

But without optimization, the destructor will be executed and the `set_terminate` function will hook `my_term` and the program will return 0.

I'm not sure if this is a valid optimization and the original code is wrong, or if the code is valid.

I've tried with various GCC version from 7.5 to 10.1.0.

#include <exception>
#include <unistd.h>

int test ( void );

void my_term ( void )
{
  // only way to exit SUCCESS
  _exit(0);
}

int main(void)
{
  test ();

  // unreachable
  return 1;
}


struct S1 {
public:
  ~S1 (  ) noexcept (false)
  {
    // moving this outside of ~S1 (eg. in main()) makes the program behave
    // consistently for O0~O3
    std::set_terminate(&my_term);

    // this should trigger the terminate() as we are throwing in a dtor called
    // during exception handling ?
    throw 1;
  };
};


int test(void)
{
  try
    {
      int s = 0;
      S1  ss;

      // this should trigger the dtor for ss
      throw s;

      // unreachable
      return -1;
    }
  catch ( ... ) {
    // unreachable
    return -1;
  }
}
Comment 1 Jonathan Wakely 2020-06-18 13:30:34 UTC
[except.terminate] allows std::terminate to be called without stack unwinding in some cases, but I don't think that applies here. The standard specifically says "An implementation is not permitted to finish stack unwinding prematurely based on a determination that the unwind process will eventually cause a call to std::terminate()." That seems to be what's happening here.
Comment 2 Jonathan Wakely 2020-06-18 13:35:07 UTC
I assume the bug summary is a typo, and you meant to say "Optimization skips destructor and calls terminate directly", right?
Comment 3 Marc Poulhiès 2020-06-18 13:41:25 UTC
 Yes you are right, sorry. Mistake caused by previous change in the title...
Comment 4 Andrew Pinski 2021-08-17 01:16:47 UTC
Related to PR 34258 and PR 83400 .  there might be a few others.