<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Throwing C++ exceptions crashes in code which is compiled with clang++ 8 and running on Cygwin64"
   href="https://bugs.llvm.org/show_bug.cgi?id=43668">43668</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Throwing C++ exceptions crashes in code which is compiled with clang++ 8 and running on Cygwin64
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>8.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>zettsu.tatsuya@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Throwing C++ exceptions crashes in code which is compiled with clang++ 8.0.1
(Target: x86_64-unknown-windows-cygnus) and running on Cygwin 64-bit
installation 3.0.7. Compiling with -O0 option causes a crash and compiling with
-O2 option does not.

This does not occur with Cygwin g++ 7.4.0, MinGW g++ 8.2.0 and Windows native
clang++ 8.0.1 (Target: x86_64-pc-windows-msvc).

The following code crashes. Full source code is available on
<a href="https://github.com/zettsu-t/throwAndCatch">https://github.com/zettsu-t/throwAndCatch</a> .

1. A free function calls another function
2. The later function invokes (calls the operator() of) a std::function
instance
3. The invoked instance throws an exception.

  void invokeFunc(std::function<void(void)>& func) {
    func();
  }
  void throwInLocal(void) {
    throw std::runtime_error("Error");
  }
  std::function<void(void)> funcObj = &throwInLocal;
  invokeFunc(funcObj);

This is a gdb backtrace when throwing an exception crashed.

  gdb: unknown target exception 0x20474343 at 0x7fface6ea839
  Thread 1 "throw_catch_clang" received signal ?, Unknown signal.
  0x00007fface6ea839 in RaiseException () from
/cygdrive/c/WINDOWS/System32/KERNELBASE.dll
  (gdb) bt
  #0  0x00007fface6ea839 in RaiseException () from
/cygdrive/c/WINDOWS/System32/KERNELBASE.dll
  #1  0x00000003de6bcbf1 in cyggcc_s-seh-1!_Unwind_RaiseException () from
/usr/bin/cyggcc_s-seh-1.dll
  #2  0x00000003c3599148 in cygstdc++-6!.cxa_throw () from
/usr/bin/cygstdc++-6.dll
  #3  0x0000000100401948 in (anonymous namespace)::throwInLocal () at
throw_catch_main.cpp:12
  #4  0x0000000100402c65 in std::_Function_handler<void (), void
(*)()>::_M_invoke(std::_Any_data const&)
      (__functor=...) at
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/std_function.h:316
  #5  0x00000001004032aa in std::function<void ()>::operator()() const
(this=0xffffcb58)
      at
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/std_function.h:706
  #6  0x00000000ffffc8a8 in ?? ()
  Backtrace stopped: previous frame inner to this frame (corrupt stack?)

I found invoking the std::function instance in other statements prevents
crashes like shown below.

  std::function<void(void)> funcObj = &throwInLocal;
  funcObj();
  invokeFunc(funcObj);</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>