<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/54386>54386</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            WebAssembly codegen TLS-related internal crash on Linux only
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          juj
      </td>
    </tr>
</table>

<pre>
    Building TLS support to Emscripten Wasm Workers branch, at https://github.com/emscripten-core/emscripten/pull/16495 runs into

```
test_wasm_worker_c11__Thread_local (test_browser.browser) ... wasm-ld: /b/s/w/ir/cache/builder/emscripten-releases/llvm-project/llvm/include/llvm/ADT/Optional.h:199: const T &llvm::optional_detail::OptionalStorage<unsigned int, true>::getValue() const & [T = unsigned int]: Assertion `hasVal' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /root/emsdk/upstream/bin/wasm-ld -o test.wasm /tmp/emscripten_temp_misajskw/c11__Thread_local_0.o /tmp/emscripten_temp_misajskw/report_result_1.o -L/root/cache/sysroot/lib/wasm32-emscripten /root/cache/sysroot/lib/wasm32-emscripten/crtbegin.o -lGL-ww -lal -lhtml5 -lstubs-debug -lnoexit -lc-ww-debug -ldlmalloc-ww -lcompiler_rt-ww -lc++-ww-noexcept -lc++abi-ww-noexcept -lsockets-ww -lwasm_workers-debug -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --import-memory --shared-memory --strip-debug --export-if-defined=main --export-if-defined=emscripten_wasm_worker_initialize --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__stdio_exit --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_init --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export-table -z stack-size=5242880 --initial-memory=16777216 --no-entry --max-memory=16777216 --global-base=1024
 #0 0x00007f096a2b0563 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/root/emsdk/upstream/bin/../lib/libLLVM-15git.so+0x1ab0563)
 #1 0x00007f096a2ae37e llvm::sys::RunSignalHandlers() (/root/emsdk/upstream/bin/../lib/libLLVM-15git.so+0x1aae37e)
 #2 0x00007f096a2b0a2f SignalHandler(int) Signals.cpp:0:0
 #3 0x00007f096cbe1890 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12890)
 #4 0x00007f0967b6be97 raise (/lib/x86_64-linux-gnu/libc.so.6+0x3ee97)
 #5 0x00007f0967b6d801 abort (/lib/x86_64-linux-gnu/libc.so.6+0x40801)
 #6 0x00007f0967b5d39a (/lib/x86_64-linux-gnu/libc.so.6+0x3039a)
 #7 0x00007f0967b5d412 (/lib/x86_64-linux-gnu/libc.so.6+0x30412)
 #8 0x0000000000684c0f lld::wasm::GlobalSymbol::getGlobalIndex() const (/root/emsdk/upstream/bin/wasm-ld+0x684c0f)
 #9 0x000000000069cce4 lld::wasm::GlobalSection::generateRelocationCode(llvm::raw_ostream&, bool) const (/root/emsdk/upstream/bin/wasm-ld+0x69cce4)
#10 0x000000000068bd68 lld::wasm::(anonymous namespace)::Writer::run() Writer.cpp:0:0
#11 0x00000000006859f4 lld::wasm::writeResult() (/root/emsdk/upstream/bin/wasm-ld+0x6859f4)
#12 0x0000000000666209 lld::wasm::(anonymous namespace)::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) Driver.cpp:0:0
#13 0x0000000000660fde lld::wasm::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) (/root/emsdk/upstream/bin/wasm-ld+0x660fde)
#14 0x000000000036fa22 lldMain(int, char const**, llvm::raw_ostream&, llvm::raw_ostream&, bool) lld.cpp:0:0
#15 0x000000000036f242 main (/root/emsdk/upstream/bin/wasm-ld+0x36f242)
#16 0x00007f0967b4eb97 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b97)
#17 0x000000000036ed7a _start (/root/emsdk/upstream/bin/wasm-ld+0x36ed7a)
emcc: error: '/root/emsdk/upstream/bin/wasm-ld -o test.wasm /tmp/emscripten_temp_misajskw/c11__Thread_local_0.o /tmp/emscripten_temp_misajskw/report_result_1.o -L/root/cache/sysroot/lib/wasm32-emscripten /root/cache/sysroot/lib/wasm32-emscripten/crtbegin.o -lGL-ww -lal -lhtml5 -lstubs-debug -lnoexit -lc-ww-debug -ldlmalloc-ww -lcompiler_rt-ww -lc++-ww-noexcept -lc++abi-ww-noexcept -lsockets-ww -lwasm_workers-debug -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --import-memory --shared-memory --strip-debug --export-if-defined=main --export-if-defined=emscripten_wasm_worker_initialize --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__stdio_exit --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_init --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export-table -z stack-size=5242880 --initial-memory=16777216 --no-entry --max-memory=16777216 --global-base=1024' failed (received SIGABRT (-6))
None
None
FAIL
test_wasm_worker_c11__Thread_local_minimal_runtime (test_browser.browser) ... wasm-ld: /b/s/w/ir/cache/builder/emscripten-releases/llvm-project/llvm/include/llvm/ADT/Optional.h:199: const T &llvm::optional_detail::OptionalStorage<unsigned int, true>::getValue() const & [T = unsigned int]: Assertion `hasVal' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /root/emsdk/upstream/bin/wasm-ld -o test.wasm /tmp/emscripten_temp_ho1ho4r7/c11__Thread_local_0.o /tmp/emscripten_temp_ho1ho4r7/report_result_1.o -L/root/cache/sysroot/lib/wasm32-emscripten /root/cache/sysroot/lib/wasm32-emscripten/crtbegin.o -lGL-ww -lal -lhtml5 -lstubs-debug -lnoexit -lc-ww-debug -ldlmalloc-ww-noerrno -lcompiler_rt-ww -lc++-ww-noexcept -lc++abi-ww-noexcept -lsockets-ww -lwasm_workers-debug -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --import-memory --shared-memory --strip-debug --export-if-defined=main --export-if-defined=emscripten_wasm_worker_initialize --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__stdio_exit --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_init --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export-table -z stack-size=5242880 --initial-memory=16777216 --no-entry --max-memory=16777216 --global-base=1024
 #0 0x00007f846699a563 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/root/emsdk/upstream/bin/../lib/libLLVM-15git.so+0x1ab0563)
 #1 0x00007f846699837e llvm::sys::RunSignalHandlers() (/root/emsdk/upstream/bin/../lib/libLLVM-15git.so+0x1aae37e)
 #2 0x00007f846699aa2f SignalHandler(int) Signals.cpp:0:0
 #3 0x00007f84692cb890 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12890)
 #4 0x00007f8464255e97 raise (/lib/x86_64-linux-gnu/libc.so.6+0x3ee97)
 #5 0x00007f8464257801 abort (/lib/x86_64-linux-gnu/libc.so.6+0x40801)
 #6 0x00007f846424739a (/lib/x86_64-linux-gnu/libc.so.6+0x3039a)
 #7 0x00007f8464247412 (/lib/x86_64-linux-gnu/libc.so.6+0x30412)
 #8 0x0000000000684c0f lld::wasm::GlobalSymbol::getGlobalIndex() const (/root/emsdk/upstream/bin/wasm-ld+0x684c0f)
 #9 0x000000000069cce4 lld::wasm::GlobalSection::generateRelocationCode(llvm::raw_ostream&, bool) const (/root/emsdk/upstream/bin/wasm-ld+0x69cce4)
#10 0x000000000068bd68 lld::wasm::(anonymous namespace)::Writer::run() Writer.cpp:0:0
#11 0x00000000006859f4 lld::wasm::writeResult() (/root/emsdk/upstream/bin/wasm-ld+0x6859f4)
#12 0x0000000000666209 lld::wasm::(anonymous namespace)::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) Driver.cpp:0:0
#13 0x0000000000660fde lld::wasm::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) (/root/emsdk/upstream/bin/wasm-ld+0x660fde)
#14 0x000000000036fa22 lldMain(int, char const**, llvm::raw_ostream&, llvm::raw_ostream&, bool) lld.cpp:0:0
#15 0x000000000036f242 main (/root/emsdk/upstream/bin/wasm-ld+0x36f242)
#16 0x00007f8464238b97 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b97)
#17 0x000000000036ed7a _start (/root/emsdk/upstream/bin/wasm-ld+0x36ed7a)
emcc: error: '/root/emsdk/upstream/bin/wasm-ld -o test.wasm /tmp/emscripten_temp_ho1ho4r7/c11__Thread_local_0.o /tmp/emscripten_temp_ho1ho4r7/report_result_1.o -L/root/cache/sysroot/lib/wasm32-emscripten /root/cache/sysroot/lib/wasm32-emscripten/crtbegin.o -lGL-ww -lal -lhtml5 -lstubs-debug -lnoexit -lc-ww-debug -ldlmalloc-ww-noerrno -lcompiler_rt-ww -lc++-ww-noexcept -lc++abi-ww-noexcept -lsockets-ww -lwasm_workers-debug -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --import-memory --shared-memory --strip-debug --export-if-defined=main --export-if-defined=emscripten_wasm_worker_initialize --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__stdio_exit --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_get_base --export=emscripten_stack_init --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export-table -z stack-size=5242880 --initial-memory=16777216 --no-entry --max-memory=16777216 --global-base=1024' failed (received SIGABRT (-6))
None
None
FAIL
```

This is only observed on the Emscripten CI that runs on Linux. When doing the Emscripten WebAssembly build from a Windows host, the internal error does not occur.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWt1v2zYQ_2ucF0KCJOvzwQ9u0nYFsq1IguVRoCTKYkKJBknVyf76HUnZlty0-egKrIMM2bJI3t3vjsc78qCCV4-rdz1lFe026ObyGsl-u-VCIcXR-1aWgm4V6dAtli265eKeCIkKgbuyWQTnCCvUKLWVi-V6EXyAa0NV0xduyVt4IAd6p-SCTFrgYdszBjc_DrMIib6TiHaKL7yLhbcefmNvuMyjIlLlO0CS7wySvPT9PL9pBMFVzniJGVoEqRlVCL6TRLjDfRFkyHVdpIkdVgFcGPmhgK-E7w6-FMZ8KHHZaJiFNoimGqsgCCNYEk3A2JfW2Qp-R0o1PGoWXcn6ihwb1hc38PvnVlHeYeY2INbPMi285J1U6AZAxGYsmG-55sPAvCIKU2Yb99TXigu8IYvlORiKbjpSaWvpOVCih-b3dviGqL8wg4Yg1TpbOSAFLaJ3IG55gSbk0YVGs5ZgIi0GgakbLIHDIkhQDSBI5Vrbf758v75-D95RtFQhjIp-gwTZe8p3nGCwxYnJqJS9MSXCnYZiLIdUQ1ApsGxQgct7JXBJBvHXChpQ1bdbLcU0edCVfRZ8I3CLsNj0LemUHKZWcK7s9FX3cO-3UoGXaCAF1b43OAJyONL-4upnTadAwHjWc0Xabd5Sie_kvfaTr1wu91z-IkprrVwQ2TOV-0DlXB6R7l1PPsqhhdFiALoMnCNf9AYaPVaogmxop8Wyj5fObgd3WDAOa1TLIrhL1RfSqYieWYd1nDzARDushKGH1oq1mIHalhwmeAsuInKhhoZF8A4uTaHpS7JVx1Zc0JMOyct7oqSlHa3rA4pWew2C4NHCrBHhbBgvMHMwo1g6GFbFo6Qw4xc1ZpIchpMOF4yM1HfkHbs7dFcwJbqfSYEch7Z6Vpy-q0hN9bI4NLWk5eIRnmWDBalGzwrY7iE65MGMprUzcAA8LabdN7pGDjKOZL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