<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 - Dynamic TLS variables in DLLs are only initialized for existing threads on Windows ABI"
   href="https://bugs.llvm.org/show_bug.cgi?id=50176">50176</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Dynamic TLS variables in DLLs are only initialized for existing threads on Windows ABI
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </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>adampetrone83@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>Created <span class=""><a href="attachment.cgi?id=24814" name="attach_24814" title="Source for the main application.">attachment 24814</a> <a href="attachment.cgi?id=24814&action=edit" title="Source for the main application.">[details]</a></span>
Source for the main application.

I initially discovered this while building a codebase with clang-cl.
I see this problem with clang 11, clang 12 and latest trunk (as of yesterday).
This is not a problem with msvc v19.28.

I believe it affects Windows ABI because if I build this program on Linux, the
dynamic TLS variable is initialized before first use as expected.

The crux of the problem boils down to:
1. Create a thread. Wait until this thread launches.
2. Load the DLL.
3. From the thread in Step 1, call a function in the DLL that accesses a
dynamic TLS variable.

This appears to have been addressed in msvc by the addition of
__dyn_tls_on_demand_init.
Looking at the assembly for this compiled with msvc shows the insertion of this
call.

See the section titled, 'TLS Guard changes'
<a href="https://docs.microsoft.com/en-us/cpp/overview/cpp-conformance-improvements?view=msvc-160">https://docs.microsoft.com/en-us/cpp/overview/cpp-conformance-improvements?view=msvc-160</a>

Building:
clang++ -shared "library.cpp" -o library.dll
clang++ "main.cpp" -o main.exe
./main.exe

On success, you'll see both lines:
main thread: value is 4294967295
thread_worker: value is 4294967295

On failure, you'll only see the main thread, as the worker crashes.

I'm attaching two files needed to reproduce this. If need be, I can trim down
the main.cpp
to only include the Windows code. I don't know if it's useful having the option
to build it under Linux as well.</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>