<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 - A range of ScopeRecord does not match the code enclosed by __try"
   href="https://bugs.llvm.org/show_bug.cgi?id=44174">44174</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>A range of ScopeRecord does not match the code enclosed by __try
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>9.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>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>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>leamovret@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>A range of ScopeRecord does not match the code enclosed by __try

I hit a potential bug that clang generates a wrong Windows SEH structure.

Here's an NMAKE project to reproduce the issue.
<a href="https://github.com/msmania/seh-debug/tree/191128-report-to-llvm">https://github.com/msmania/seh-debug/tree/191128-report-to-llvm</a>

My build environment consists of:

- Clang 9.0 x86_64-pc-windows-msvc
  (downloaded from <a href="http://releases.llvm.org/download.html">http://releases.llvm.org/download.html</a>)
- Visual Studio 2019 Community
- Windows 10 SDK 10.0.18362.0

To build, you open "x64 Native Tools Command Prompt for VS 2019" and run NMAKE.
 You may need to update `Makefile` to adjust installation paths such as
$(MSVCDIR), $(WIN10KITINCLUDE), or $(CLANG).

This Makefile builds src/main.cpp with two compilers clang++ and MSVC. An
executable will be generated as bin-clang/amd64/t.exe and bin/amd64/t.exe
respectively.

These executables run without any crash, but embedded SEH information in
clang's output looks not correct, especially translation from `__try` block
into a range of ScopeTable's record.  MSVC's output looks correct on the other
hand.  This will cause a second chance exception even from __try block.

For example, let's think about this code.

  __try {
    CoInitialize(nullptr);
  }
  __except (ExceptionFilter(GetExceptionInformation())) {
    __debugbreak();
  }

It's compiled like this.  Analyzing SEH info in the binary, the first xor,
setting the first argument to 0, is not included in the ScopeRecord.

   77 00007ff7`64dc1153 31c9            xor     ecx,ecx
   77 00007ff7`64dc1155 ff1555270000    call    qword ptr
       [t!_imp_CoInitialize (00007ff7`64dc38b0)]

This case should be ok because `xor ecx,ecx` will never throw an exception, but
in a more complex scenario, incorrect ScopeRecord will cause an unexpected
behavior.

More examples and detailed explanations are available in the links below:

Clang:
<a href="https://github.com/msmania/seh-debug/blob/191128-report-to-llvm/seh-clang.md">https://github.com/msmania/seh-debug/blob/191128-report-to-llvm/seh-clang.md</a>

MSVC:
<a href="https://github.com/msmania/seh-debug/blob/191128-report-to-llvm/seh-msvc.md">https://github.com/msmania/seh-debug/blob/191128-report-to-llvm/seh-msvc.md</a></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>