[llvm-bugs] [Bug 50176] New: Dynamic TLS variables in DLLs are only initialized for existing threads on Windows ABI

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Apr 29 17:55:20 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=50176

            Bug ID: 50176
           Summary: Dynamic TLS variables in DLLs are only initialized for
                    existing threads on Windows ABI
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: adampetrone83 at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

Created attachment 24814
  --> https://bugs.llvm.org/attachment.cgi?id=24814&action=edit
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'
https://docs.microsoft.com/en-us/cpp/overview/cpp-conformance-improvements?view=msvc-160

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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210430/b4bcaa19/attachment.html>


More information about the llvm-bugs mailing list