<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - Windows x64 SEH __except inside __finally the finally runs twice"
   href="https://llvm.org/bugs/show_bug.cgi?id=30761">30761</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Windows x64 SEH __except inside __finally the finally runs twice
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.9
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>nb@ravenbrook.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=17474" name="attach_17474" title="C source, with disassembly and comparison to code generated by Visual Studio">attachment 17474</a> <a href="attachment.cgi?id=17474&action=edit" title="C source, with disassembly and comparison to code generated by Visual Studio">[details]</a></span>
C source, with disassembly and comparison to code generated by Visual Studio

Using Clang 3.9.0 for x64 on Windows:

int f(int x)
{
    __try {
        __try {
            x = g(x);
        } __except((x == 42) ? EXCEPTION_EXECUTE_HANDLER :
EXCEPTION_CONTINUE_SEARCH) {
            ++x;
        }
    } __finally {
        x *= 2;
    }
    return x;
}

if called with x == 42, and g raises an exception, then f returns 170 and not
86 as it should.  The finally clause is run twice, once as a "cleanup" function
called from __C_specific_handler, after the filter fires but before the
handler, and once in the continuation from the try clause.  It is incorrect for
the cleanup function to be called in this case.

Looking through the generated code, and comparing with that produced by Visual
Studio, the problem seems to be that the data produced by Clang doesn't trigger
the EXCEPTION_TARGET_UNWIND case inside __C_specific_handler.  That case
specifically prevents cleanup functions from being called if the TargetIP is in
the ScopeRecord's range.  However, the TargetIP in this case is the address of
the handler continuation, and the handler continuation has not been placed in
the range of the scope record.

The attachment here has a detailed disassembly and commentary on the unwind
data etc, and a comparison with code generated by Visual Studio.


Clang generates a good filter function and continuation code for the __except
clause, and also generates a cleanup function for the __finally clause.</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>