New user self-registration is disabled due to spam. For an account please email bugs-admin@lists.llvm.org with your e-mail address and full name.

Bug 41508 - race condition deadlock in wasm linker
Summary: race condition deadlock in wasm linker
Status: RESOLVED FIXED
Alias: None
Product: lld
Classification: Unclassified
Component: All Bugs (show other bugs)
Version: unspecified
Hardware: All All
: P normal
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-04-15 17:36 PDT by Andrew Kelley
Modified: 2019-05-25 10:27 PDT (History)
5 users (show)

See Also:
Fixed By Commit(s):


Attachments
test.o (3.97 KB, application/octet-stream)
2019-04-15 17:36 PDT, Andrew Kelley
Details
libbuiltin.a (37.21 KB, application/octet-stream)
2019-04-15 17:36 PDT, Andrew Kelley
Details
libcompiler_rt.a (209.67 KB, application/octet-stream)
2019-04-15 17:37 PDT, Andrew Kelley
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Kelley 2019-04-15 17:36:21 PDT
Created attachment 21775 [details]
test.o

Tested against LLD 8.0.0.


Frontend code:

export fn cwa_main() i32 {
    force_crash() catch unreachable;

    return 0;
}

extern fn random_i32() i32;
const ErrorStuff = error{
    Stuff,
};

fn force_crash() !void {
    if (@mod(random_i32(), 2) == 1) {
        return error.Stuff;
    }
}


Command to build:

zig build-exe test.zig -target wasm32-freestanding

About every 1 in 6 times for me, it deadlocks with this stack trace:

Attaching to process 15445
[New LWP 15446]
[New LWP 15447]
[New LWP 15448]
[New LWP 15449]
^C[Thread debugging using libthread_db enabled]
Using host libthread_db library "/nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libthread_db.so.1".
0x00007f6078621ee2 in pthread_cond_wait@@GLIBC_2.3.2 ()
   from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libpthread.so.0
(gdb) thread apply all bt

Thread 5 (Thread 0x7f60764bb700 (LWP 15449)):
#0  0x00007f6078621ee2 in pthread_cond_wait@@GLIBC_2.3.2 () from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libpthread.so.0
#1  0x00007f60785470cc in std::condition_variable::wait(std::unique_lock<std::mutex>&) () from /nix/store/hlnxw4k6931bachvg5sv0cyaissimswb-gcc-7.4.0-lib/lib/libstdc++.so.6
#2  0x00000000027670b6 in std::condition_variable::wait<llvm::parallel::detail::Latch::sync() const::{lambda()#1}>(std::unique_lock<std::mutex>&, llvm::parallel::detail::Latch::sync() const::{lambda()#1}) (
    this=0x7f60764bab30, __lock=..., __p=...) at /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/include/c++/7.4.0/condition_variable:99
#3  0x0000000002765b5c in llvm::parallel::detail::Latch::sync (this=0x7f60764bab00) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:71
#4  0x0000000002765af2 in llvm::parallel::detail::Latch::~Latch (this=0x7f60764bab00, __in_chrg=<optimized out>) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:56
#5  0x0000000002770004 in llvm::parallel::detail::TaskGroup::~TaskGroup (this=0x7f60764bab00, __in_chrg=<optimized out>)
    at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:75
#6  0x0000000002ac9d52 in llvm::parallel::detail::parallel_for_each<lld::wasm::InputSection* const*, lld::wasm::CustomSection::writeTo(uint8_t*)::<lambda(const lld::wasm::InputSection*)> >(lld::wasm::InputSection * const *, lld::wasm::InputSection * const *, lld::wasm::CustomSection::<lambda(const lld::wasm::InputSection*)>) (Begin=0x519ff88, End=0x519ff90, Fn=...)
    at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:160
#7  0x0000000002ac983d in llvm::parallel::for_each<lld::wasm::InputSection* const*, lld::wasm::CustomSection::writeTo(uint8_t*)::<lambda(const lld::wasm::InputSection*)> >(llvm::parallel::parallel_execution_policy, lld::wasm::InputSection * const *, lld::wasm::InputSection * const *, lld::wasm::CustomSection::<lambda(const lld::wasm::InputSection*)>) (policy=..., Begin=0x519ff80, End=0x519ff90, Fn=...)
    at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:234
#8  0x0000000002ac970e in lld::parallelForEach<llvm::ArrayRef<lld::wasm::InputSection*>&, lld::wasm::CustomSection::writeTo(uint8_t*)::<lambda(const lld::wasm::InputSection*)> >(llvm::ArrayRef<lld::wasm::InputSection*> &, lld::wasm::CustomSection::<lambda(const lld::wasm::InputSection*)>) (Range=..., Fn=...) at /home/andy/downloads/zig/deps/lld/include/lld/Common/Threads.h:71
#9  0x0000000002ac948b in lld::wasm::CustomSection::writeTo (this=0x51c9080, Buf=0x7f6077cc52eb "") at /home/andy/downloads/zig/deps/lld/wasm/OutputSections.cpp:235
#10 0x0000000002aaaa15 in (anonymous namespace)::Writer::<lambda(lld::wasm::OutputSection*)>::operator()(lld::wasm::OutputSection *) const (__closure=0x7f60764badc8, S=0x51c9080)
    at /home/andy/downloads/zig/deps/lld/wasm/Writer.cpp:662
#11 0x0000000002ab0ada in std::for_each<__gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*> >, (anonymous namespace)::Writer::writeSections()::<lambda(lld::wasm::OutputSection*)> >(__gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*, std::allocator<lld::wasm::OutputSection*> > >, __gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*, std::allocator<lld::wasm::OutputSection*> > >, (anonymous namespace)::Writer::<lambda(lld::wasm::OutputSection*)>) (__first=0x51c9080, __last=0x51c9110, __f=...)
    at /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/include/c++/7.4.0/bits/stl_algo.h:3884
#12 0x0000000002ab094e in llvm::parallel::detail::<lambda()>::operator()(void) const (__closure=0x7f6060000b50) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:162
#13 0x0000000002ab2367 in std::_Function_handler<void(), llvm::parallel::detail::parallel_for_each(IterTy, IterTy, FuncTy) [with IterTy = __gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*> >; FuncTy = (anonymous namespace)::Writer::writeSections()::<lambda(lld::wasm::OutputSection*)>]::<lambda()> >::_M_invoke(const std::_Any_data &) (__functor=...)
    at /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/include/c++/7.4.0/bits/std_function.h:316
#14 0x00007f607912f336 in std::_Function_handler<void (), llvm::parallel::detail::TaskGroup::spawn(std::function<void ()>)::{lambda()#1}>::_M_invoke(std::_Any_data const&) ()
   from /nix/store/6h1hah8ndjxvij0r8xn0f5qb88vyv0hr-llvm-8.0.0-lib/lib/libLLVM-8.so
#15 0x00007f607912f4b5 in std::thread::_State_impl<std::thread::_Invoker<std::tuple<(anonymous namespace)::ThreadPoolExecutor::ThreadPoolExecutor(unsigned int)::{lambda()#1}::operator()() const::{lambda()#1}> > >::_M_run() () from /nix/store/6h1hah8ndjxvij0r8xn0f5qb88vyv0hr-llvm-8.0.0-lib/lib/libLLVM-8.so
#16 0x00007f607854cd7f in ?? () from /nix/store/hlnxw4k6931bachvg5sv0cyaissimswb-gcc-7.4.0-lib/lib/libstdc++.so.6
#17 0x00007f607861bef7 in start_thread () from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libpthread.so.0
#18 0x00007f607801922f in clone () from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libc.so.6

Thread 4 (Thread 0x7f6076cbc700 (LWP 15448)):
#0  0x00007f6078621ee2 in pthread_cond_wait@@GLIBC_2.3.2 () from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libpthread.so.0
#1  0x00007f60785470cc in std::condition_variable::wait(std::unique_lock<std::mutex>&) () from /nix/store/hlnxw4k6931bachvg5sv0cyaissimswb-gcc-7.4.0-lib/lib/libstdc++.so.6
#2  0x00000000027670b6 in std::condition_variable::wait<llvm::parallel::detail::Latch::sync() const::{lambda()#1}>(std::unique_lock<std::mutex>&, llvm::parallel::detail::Latch::sync() const::{lambda()#1}) (
    this=0x7f6076cbbb30, __lock=..., __p=...) at /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/include/c++/7.4.0/condition_variable:99
#3  0x0000000002765b5c in llvm::parallel::detail::Latch::sync (this=0x7f6076cbbb00) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:71
#4  0x0000000002765af2 in llvm::parallel::detail::Latch::~Latch (this=0x7f6076cbbb00, __in_chrg=<optimized out>) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:56
#5  0x0000000002770004 in llvm::parallel::detail::TaskGroup::~TaskGroup (this=0x7f6076cbbb00, __in_chrg=<optimized out>)
    at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:75
#6  0x0000000002ac9d52 in llvm::parallel::detail::parallel_for_each<lld::wasm::InputSection* const*, lld::wasm::CustomSection::writeTo(uint8_t*)::<lambda(const lld::wasm::InputSection*)> >(lld::wasm::InputSection * const *, lld::wasm::InputSection * const *, lld::wasm::CustomSection::<lambda(const lld::wasm::InputSection*)>) (Begin=0x516d8f8, End=0x516d900, Fn=...)
    at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:160
#7  0x0000000002ac983d in llvm::parallel::for_each<lld::wasm::InputSection* const*, lld::wasm::CustomSection::writeTo(uint8_t*)::<lambda(const lld::wasm::InputSection*)> >(llvm::parallel::parallel_execution_policy, lld::wasm::InputSection * const *, lld::wasm::InputSection * const *, lld::wasm::CustomSection::<lambda(const lld::wasm::InputSection*)>) (policy=..., Begin=0x516d8f0, End=0x516d900, Fn=...)
    at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:234
#8  0x0000000002ac970e in lld::parallelForEach<llvm::ArrayRef<lld::wasm::InputSection*>&, lld::wasm::CustomSection::writeTo(uint8_t*)::<lambda(const lld::wasm::InputSection*)> >(llvm::ArrayRef<lld::wasm::InputSe--Type <RET> for more, q to quit, c to continue without paging--
ction*> &, lld::wasm::CustomSection::<lambda(const lld::wasm::InputSection*)>) (Range=..., Fn=...) at /home/andy/downloads/zig/deps/lld/include/lld/Common/Threads.h:71
#9  0x0000000002ac948b in lld::wasm::CustomSection::writeTo (this=0x51c9110, Buf=0x7f6077cc5554 "") at /home/andy/downloads/zig/deps/lld/wasm/OutputSections.cpp:235
#10 0x0000000002aaaa15 in (anonymous namespace)::Writer::<lambda(lld::wasm::OutputSection*)>::operator()(lld::wasm::OutputSection *) const (__closure=0x7f6076cbbdc8, S=0x51c9110)
    at /home/andy/downloads/zig/deps/lld/wasm/Writer.cpp:662
#11 0x0000000002ab0ada in std::for_each<__gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*> >, (anonymous namespace)::Writer::writeSections()::<lambda(lld::wasm::OutputSection*)> >(__gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*, std::allocator<lld::wasm::OutputSection*> > >, __gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*, std::allocator<lld::wasm::OutputSection*> > >, (anonymous namespace)::Writer::<lambda(lld::wasm::OutputSection*)>) (__first=0x51c9110, __last=0x51c91a0, __f=...)
    at /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/include/c++/7.4.0/bits/stl_algo.h:3884
#12 0x0000000002ab094e in llvm::parallel::detail::<lambda()>::operator()(void) const (__closure=0x7f606c000b50) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:162
#13 0x0000000002ab2367 in std::_Function_handler<void(), llvm::parallel::detail::parallel_for_each(IterTy, IterTy, FuncTy) [with IterTy = __gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*> >; FuncTy = (anonymous namespace)::Writer::writeSections()::<lambda(lld::wasm::OutputSection*)>]::<lambda()> >::_M_invoke(const std::_Any_data &) (__functor=...)
    at /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/include/c++/7.4.0/bits/std_function.h:316
#14 0x00007f607912f336 in std::_Function_handler<void (), llvm::parallel::detail::TaskGroup::spawn(std::function<void ()>)::{lambda()#1}>::_M_invoke(std::_Any_data const&) ()
   from /nix/store/6h1hah8ndjxvij0r8xn0f5qb88vyv0hr-llvm-8.0.0-lib/lib/libLLVM-8.so
#15 0x00007f607912f4b5 in std::thread::_State_impl<std::thread::_Invoker<std::tuple<(anonymous namespace)::ThreadPoolExecutor::ThreadPoolExecutor(unsigned int)::{lambda()#1}::operator()() const::{lambda()#1}> > >::_M_run() () from /nix/store/6h1hah8ndjxvij0r8xn0f5qb88vyv0hr-llvm-8.0.0-lib/lib/libLLVM-8.so
#16 0x00007f607854cd7f in ?? () from /nix/store/hlnxw4k6931bachvg5sv0cyaissimswb-gcc-7.4.0-lib/lib/libstdc++.so.6
#17 0x00007f607861bef7 in start_thread () from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libpthread.so.0
#18 0x00007f607801922f in clone () from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libc.so.6

Thread 3 (Thread 0x7f60774bd700 (LWP 15447)):
#0  0x00007f6078621ee2 in pthread_cond_wait@@GLIBC_2.3.2 () from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libpthread.so.0
#1  0x00007f60785470cc in std::condition_variable::wait(std::unique_lock<std::mutex>&) () from /nix/store/hlnxw4k6931bachvg5sv0cyaissimswb-gcc-7.4.0-lib/lib/libstdc++.so.6
#2  0x00000000027670b6 in std::condition_variable::wait<llvm::parallel::detail::Latch::sync() const::{lambda()#1}>(std::unique_lock<std::mutex>&, llvm::parallel::detail::Latch::sync() const::{lambda()#1}) (
    this=0x7f60774bcb30, __lock=..., __p=...) at /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/include/c++/7.4.0/condition_variable:99
#3  0x0000000002765b5c in llvm::parallel::detail::Latch::sync (this=0x7f60774bcb00) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:71
#4  0x0000000002765af2 in llvm::parallel::detail::Latch::~Latch (this=0x7f60774bcb00, __in_chrg=<optimized out>) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:56
#5  0x0000000002770004 in llvm::parallel::detail::TaskGroup::~TaskGroup (this=0x7f60774bcb00, __in_chrg=<optimized out>)
    at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:75
#6  0x0000000002ac9d52 in llvm::parallel::detail::parallel_for_each<lld::wasm::InputSection* const*, lld::wasm::CustomSection::writeTo(uint8_t*)::<lambda(const lld::wasm::InputSection*)> >(lld::wasm::InputSection * const *, lld::wasm::InputSection * const *, lld::wasm::CustomSection::<lambda(const lld::wasm::InputSection*)>) (Begin=0x516d8b8, End=0x516d8c0, Fn=...)
    at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:160
#7  0x0000000002ac983d in llvm::parallel::for_each<lld::wasm::InputSection* const*, lld::wasm::CustomSection::writeTo(uint8_t*)::<lambda(const lld::wasm::InputSection*)> >(llvm::parallel::parallel_execution_policy, lld::wasm::InputSection * const *, lld::wasm::InputSection * const *, lld::wasm::CustomSection::<lambda(const lld::wasm::InputSection*)>) (policy=..., Begin=0x516d8b0, End=0x516d8c0, Fn=...)
    at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:234
#8  0x0000000002ac970e in lld::parallelForEach<llvm::ArrayRef<lld::wasm::InputSection*>&, lld::wasm::CustomSection::writeTo(uint8_t*)::<lambda(const lld::wasm::InputSection*)> >(llvm::ArrayRef<lld::wasm::InputSection*> &, lld::wasm::CustomSection::<lambda(const lld::wasm::InputSection*)>) (Range=..., Fn=...) at /home/andy/downloads/zig/deps/lld/include/lld/Common/Threads.h:71
#9  0x0000000002ac948b in lld::wasm::CustomSection::writeTo (this=0x51c9230, Buf=0x7f6077cc7815 "") at /home/andy/downloads/zig/deps/lld/wasm/OutputSections.cpp:235
#10 0x0000000002aaaa15 in (anonymous namespace)::Writer::<lambda(lld::wasm::OutputSection*)>::operator()(lld::wasm::OutputSection *) const (__closure=0x7f60774bcdc8, S=0x51c9230)
    at /home/andy/downloads/zig/deps/lld/wasm/Writer.cpp:662
#11 0x0000000002ab0ada in std::for_each<__gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*> >, (anonymous namespace)::Writer::writeSections()::<lambda(lld::wasm::OutputSection*)> >(__gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*, std::allocator<lld::wasm::OutputSection*> > >, __gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*, std::allocator<lld::wasm::OutputSection*> > >, (anonymous namespace)::Writer::<lambda(lld::wasm::OutputSection*)>) (__first=0x51c9230, __last=0x51b8238, __f=...)
    at /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/include/c++/7.4.0/bits/stl_algo.h:3884
#12 0x0000000002ab094e in llvm::parallel::detail::<lambda()>::operator()(void) const (__closure=0x7f6068000b50) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:162
#13 0x0000000002ab2367 in std::_Function_handler<void(), llvm::parallel::detail::parallel_for_each(IterTy, IterTy, FuncTy) [with IterTy = __gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*> >; FuncTy = (anonymous namespace)::Writer::writeSections()::<lambda(lld::wasm::OutputSection*)>]::<lambda()> >::_M_invoke(const std::_Any_data &) (__functor=...)
    at /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/include/c++/7.4.0/bits/std_function.h:316
#14 0x00007f607912f336 in std::_Function_handler<void (), llvm::parallel::detail::TaskGroup::spawn(std::function<void ()>)::{lambda()#1}>::_M_invoke(std::_Any_data const&) ()
   from /nix/store/6h1hah8ndjxvij0r8xn0f5qb88vyv0hr-llvm-8.0.0-lib/lib/libLLVM-8.so
#15 0x00007f607912f4b5 in std::thread::_State_impl<std::thread::_Invoker<std::tuple<(anonymous namespace)::ThreadPoolExecutor::ThreadPoolExecutor(unsigned int)::{lambda()#1}::operator()() const::{lambda()#1}> > >::_M_run() () from /nix/store/6h1hah8ndjxvij0r8xn0f5qb88vyv0hr-llvm-8.0.0-lib/lib/libLLVM-8.so
#16 0x00007f607854cd7f in ?? () from /nix/store/hlnxw4k6931bachvg5sv0cyaissimswb-gcc-7.4.0-lib/lib/libstdc++.so.6
--Type <RET> for more, q to quit, c to continue without paging--
#17 0x00007f607861bef7 in start_thread () from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libpthread.so.0
#18 0x00007f607801922f in clone () from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libc.so.6

Thread 2 (Thread 0x7f6077cbe700 (LWP 15446)):
#0  0x00007f6078621ee2 in pthread_cond_wait@@GLIBC_2.3.2 () from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libpthread.so.0
#1  0x00007f60785470cc in std::condition_variable::wait(std::unique_lock<std::mutex>&) () from /nix/store/hlnxw4k6931bachvg5sv0cyaissimswb-gcc-7.4.0-lib/lib/libstdc++.so.6
#2  0x00000000027670b6 in std::condition_variable::wait<llvm::parallel::detail::Latch::sync() const::{lambda()#1}>(std::unique_lock<std::mutex>&, llvm::parallel::detail::Latch::sync() const::{lambda()#1}) (
    this=0x7f6077cbdb20, __lock=..., __p=...) at /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/include/c++/7.4.0/condition_variable:99
#3  0x0000000002765b5c in llvm::parallel::detail::Latch::sync (this=0x7f6077cbdaf0) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:71
#4  0x0000000002765af2 in llvm::parallel::detail::Latch::~Latch (this=0x7f6077cbdaf0, __in_chrg=<optimized out>) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:56
#5  0x0000000002770004 in llvm::parallel::detail::TaskGroup::~TaskGroup (this=0x7f6077cbdaf0, __in_chrg=<optimized out>)
    at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:75
#6  0x0000000002ac9d52 in llvm::parallel::detail::parallel_for_each<lld::wasm::InputSection* const*, lld::wasm::CustomSection::writeTo(uint8_t*)::<lambda(const lld::wasm::InputSection*)> >(lld::wasm::InputSection * const *, lld::wasm::InputSection * const *, lld::wasm::CustomSection::<lambda(const lld::wasm::InputSection*)>) (Begin=0x516d978, End=0x516d980, Fn=...)
    at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:160
#7  0x0000000002ac983d in llvm::parallel::for_each<lld::wasm::InputSection* const*, lld::wasm::CustomSection::writeTo(uint8_t*)::<lambda(const lld::wasm::InputSection*)> >(llvm::parallel::parallel_execution_policy, lld::wasm::InputSection * const *, lld::wasm::InputSection * const *, lld::wasm::CustomSection::<lambda(const lld::wasm::InputSection*)>) (policy=..., Begin=0x516d970, End=0x516d980, Fn=...)
    at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:234
#8  0x0000000002ac970e in lld::parallelForEach<llvm::ArrayRef<lld::wasm::InputSection*>&, lld::wasm::CustomSection::writeTo(uint8_t*)::<lambda(const lld::wasm::InputSection*)> >(llvm::ArrayRef<lld::wasm::InputSection*> &, lld::wasm::CustomSection::<lambda(const lld::wasm::InputSection*)>) (Range=..., Fn=...) at /home/andy/downloads/zig/deps/lld/include/lld/Common/Threads.h:71
#9  0x0000000002ac948b in lld::wasm::CustomSection::writeTo (this=0x51c91a0, Buf=0x7f6077cc7226 "") at /home/andy/downloads/zig/deps/lld/wasm/OutputSections.cpp:235
#10 0x0000000002aaaa15 in (anonymous namespace)::Writer::<lambda(lld::wasm::OutputSection*)>::operator()(lld::wasm::OutputSection *) const (__closure=0x7f6077cbddb8, S=0x51c91a0)
    at /home/andy/downloads/zig/deps/lld/wasm/Writer.cpp:662
#11 0x0000000002ab0ada in std::for_each<__gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*> >, (anonymous namespace)::Writer::writeSections()::<lambda(lld::wasm::OutputSection*)> >(__gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*, std::allocator<lld::wasm::OutputSection*> > >, __gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*, std::allocator<lld::wasm::OutputSection*> > >, (anonymous namespace)::Writer::<lambda(lld::wasm::OutputSection*)>) (__first=0x51c91a0, __last=0x51c9230, __f=...)
    at /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/include/c++/7.4.0/bits/stl_algo.h:3884
#12 0x0000000002ab094e in llvm::parallel::detail::<lambda()>::operator()(void) const (__closure=0x7f6070000fa0) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:162
#13 0x0000000002ab2367 in std::_Function_handler<void(), llvm::parallel::detail::parallel_for_each(IterTy, IterTy, FuncTy) [with IterTy = __gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*> >; FuncTy = (anonymous namespace)::Writer::writeSections()::<lambda(lld::wasm::OutputSection*)>]::<lambda()> >::_M_invoke(const std::_Any_data &) (__functor=...)
    at /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/include/c++/7.4.0/bits/std_function.h:316
#14 0x00007f607912f336 in std::_Function_handler<void (), llvm::parallel::detail::TaskGroup::spawn(std::function<void ()>)::{lambda()#1}>::_M_invoke(std::_Any_data const&) ()
   from /nix/store/6h1hah8ndjxvij0r8xn0f5qb88vyv0hr-llvm-8.0.0-lib/lib/libLLVM-8.so
#15 0x00007f607912faad in std::thread::_State_impl<std::thread::_Invoker<std::tuple<(anonymous namespace)::ThreadPoolExecutor::ThreadPoolExecutor(unsigned int)::{lambda()#1}> > >::_M_run() ()
   from /nix/store/6h1hah8ndjxvij0r8xn0f5qb88vyv0hr-llvm-8.0.0-lib/lib/libLLVM-8.so
#16 0x00007f607854cd7f in ?? () from /nix/store/hlnxw4k6931bachvg5sv0cyaissimswb-gcc-7.4.0-lib/lib/libstdc++.so.6
#17 0x00007f607861bef7 in start_thread () from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libpthread.so.0
#18 0x00007f607801922f in clone () from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libc.so.6

Thread 1 (Thread 0x7f6077d0a740 (LWP 15445)):
#0  0x00007f6078621ee2 in pthread_cond_wait@@GLIBC_2.3.2 () from /nix/store/681354n3k44r8z90m35hm8945vsp95h1-glibc-2.27/lib/libpthread.so.0
#1  0x00007f60785470cc in std::condition_variable::wait(std::unique_lock<std::mutex>&) () from /nix/store/hlnxw4k6931bachvg5sv0cyaissimswb-gcc-7.4.0-lib/lib/libstdc++.so.6
#2  0x00000000027670b6 in std::condition_variable::wait<llvm::parallel::detail::Latch::sync() const::{lambda()#1}>(std::unique_lock<std::mutex>&, llvm::parallel::detail::Latch::sync() const::{lambda()#1}) (
    this=0x7ffd54a7f310, __lock=..., __p=...) at /nix/store/d4n93jn9fdq8fkmkm1q8f32lfagvibjk-gcc-7.4.0/include/c++/7.4.0/condition_variable:99
#3  0x0000000002765b5c in llvm::parallel::detail::Latch::sync (this=0x7ffd54a7f2e0) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:71
#4  0x0000000002765af2 in llvm::parallel::detail::Latch::~Latch (this=0x7ffd54a7f2e0, __in_chrg=<optimized out>) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:56
#5  0x0000000002770004 in llvm::parallel::detail::TaskGroup::~TaskGroup (this=0x7ffd54a7f2e0, __in_chrg=<optimized out>)
    at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:75
#6  0x0000000002ab0a8e in llvm::parallel::detail::parallel_for_each<__gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*> >, (anonymous namespace)::Writer::writeSections()::<lambda(lld::wasm::OutputSection*)> >(__gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*, std::allocator<lld::wasm::OutputSection*> > >, __gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*, std::allocator<lld::wasm::OutputSection*> > >, (anonymous namespace)::Writer::<lambda(lld::wasm::OutputSection*)>) (Begin=0x51b8238, 
    End=0x519c6d8, Fn=...) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:160
#7  0x0000000002aafbad in llvm::parallel::for_each<__gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*> >, (anonymous namespace)::Writer::writeSections()::<lambda(lld::--Type <RET> for more, q to quit, c to continue without paging--
wasm::OutputSection*)> >(llvm::parallel::parallel_execution_policy, __gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*, std::allocator<lld::wasm::OutputSection*> > >, __gnu_cxx::__normal_iterator<lld::wasm::OutputSection**, std::vector<lld::wasm::OutputSection*, std::allocator<lld::wasm::OutputSection*> > >, (anonymous namespace)::Writer::<lambda(lld::wasm::OutputSection*)>)
    (policy=..., Begin=0x51b7da0, End=0x519c6d8, Fn=...) at /nix/store/7c7w734amha8y5r7rhp7nxpvchbpds2y-llvm-8.0.0/include/llvm/Support/Parallel.h:234
#8  0x0000000002aaf431 in lld::parallelForEach<std::vector<lld::wasm::OutputSection*>&, (anonymous namespace)::Writer::writeSections()::<lambda(lld::wasm::OutputSection*)> >(std::vector<lld::wasm::OutputSection*, std::allocator<lld::wasm::OutputSection*> > &, (anonymous namespace)::Writer::<lambda(lld::wasm::OutputSection*)>) (Range=std::vector of length 18, capacity 32 = {...}, Fn=...)
    at /home/andy/downloads/zig/deps/lld/include/lld/Common/Threads.h:71
#9  0x0000000002aaaa5f in (anonymous namespace)::Writer::writeSections (this=0x7ffd54a7f8a0) at /home/andy/downloads/zig/deps/lld/wasm/Writer.cpp:662
#10 0x0000000002aae860 in (anonymous namespace)::Writer::run (this=0x7ffd54a7f8a0) at /home/andy/downloads/zig/deps/lld/wasm/Writer.cpp:1192
#11 0x0000000002aaeed5 in lld::wasm::writeResult () at /home/andy/downloads/zig/deps/lld/wasm/Writer.cpp:1223
#12 0x0000000002a841ac in (anonymous namespace)::LinkerDriver::link (this=0x7ffd54a800d0, ArgsArr=...) at /home/andy/downloads/zig/deps/lld/wasm/Driver.cpp:620
#13 0x0000000002a80c96 in lld::wasm::link (Args=..., CanExitEarly=false, Error=...) at /home/andy/downloads/zig/deps/lld/wasm/Driver.cpp:93
#14 0x0000000000ddb3ba in ZigLLDLink (oformat=ZigLLVM_Wasm, args=0x517ce70, arg_count=10, append_diagnostic=0xda60b3 <link_diag_callback(void*, char const*, size_t)>, context=0x7ffd54a80240)
    at /home/andy/downloads/zig/src/zig_llvm.cpp:925
#15 0x0000000000da612f in zig_lld_link (oformat=ZigLLVM_Wasm, args=0x517ce70, arg_count=10, diag=0x7ffd54a80240) at /home/andy/downloads/zig/src/link.cpp:1135
#16 0x0000000000da7eb0 in codegen_link (g=0x50b2d70) at /home/andy/downloads/zig/src/link.cpp:1648
#17 0x0000000000d3304d in codegen_build_and_link (g=0x50b2d70) at /home/andy/downloads/zig/src/codegen.cpp:9403
#18 0x0000000000dad438 in main (argc=5, argv=0x7ffd54a80b88) at /home/andy/downloads/zig/src/main.cpp:1142


Downstream bug report: https://github.com/ziglang/zig/issues/2283

I have attached a repro. To use the attached files for the repro:

wasm-ld -error-limit=0 --no-entry --allow-undefined --export-all -o ./test.wasm ./test.o libbuiltin.a libcompiler_rt.a
Comment 1 Andrew Kelley 2019-04-15 17:36:58 PDT
Created attachment 21776 [details]
libbuiltin.a
Comment 2 Andrew Kelley 2019-04-15 17:37:17 PDT
Created attachment 21777 [details]
libcompiler_rt.a
Comment 3 Rui Ueyama 2019-04-16 00:47:28 PDT
Andrew,

Can you try this patch? LLVM's parallelForEach is not reentrant, but looks like we nest that function here.


diff --git a/lld/wasm/OutputSections.cpp b/lld/wasm/OutputSections.cpp
index 70ef9f8cae7..a59ef8ca10a 100644
--- a/lld/wasm/OutputSections.cpp
+++ b/lld/wasm/OutputSections.cpp
@@ -110,8 +110,8 @@ void CodeSection::writeTo(uint8_t *Buf) {
   memcpy(Buf, CodeSectionHeader.data(), CodeSectionHeader.size());
 
   // Write code section bodies
-  parallelForEach(Functions,
-                  [&](const InputChunk *Chunk) { Chunk->writeTo(Buf); });
+  for (const InputChunk *Chunk : Functions)
+    Chunk->writeTo(Buf);
 }
 
 uint32_t CodeSection::numRelocations() const {
@@ -175,7 +175,7 @@ void DataSection::writeTo(uint8_t *Buf) {
   // Write data section headers
   memcpy(Buf, DataSectionHeader.data(), DataSectionHeader.size());
 
-  parallelForEach(Segments, [&](const OutputSegment *Segment) {
+  for (const OutputSegment *Segment : Segments) {
     // Write data segment header
     uint8_t *SegStart = Buf + Segment->SectionOffset;
     memcpy(SegStart, Segment->Header.data(), Segment->Header.size());
@@ -183,7 +183,7 @@ void DataSection::writeTo(uint8_t *Buf) {
     // Write segment data payload
     for (const InputChunk *Chunk : Segment->InputSegments)
       Chunk->writeTo(Buf);
-  });
+  }
 }
 
 uint32_t DataSection::numRelocations() const {
@@ -231,8 +231,8 @@ void CustomSection::writeTo(uint8_t *Buf) {
   Buf += NameData.size();
 
   // Write custom sections payload
-  parallelForEach(InputSections,
-                  [&](const InputSection *Section) { Section->writeTo(Buf); });
+  for (const InputSection *Section : InputSections)
+    Section->writeTo(Buf);
 }
 
 uint32_t CustomSection::numRelocations() const {
Comment 4 Andrew Kelley 2019-04-16 00:51:07 PDT
Hi Rui,

I confirm this patch solves the issue.
Comment 5 Rui Ueyama 2019-04-16 00:52:35 PDT
Cool! I'll send this patch for review. I'll also create a patch to add an assertion so that we can find this error without hassle.
Comment 6 Andrew Kelley 2019-04-16 00:53:11 PDT
Great! Thanks for the quick turnaround :)
Comment 7 Dan Gohman 2019-04-24 07:30:55 PDT
The patch landed: https://github.com/llvm/llvm-project/commit/5081e41bdae2eb14a3f3eb8810263f9fea8fc7c1

Is this bug resolved now?
Comment 8 Rui Ueyama 2019-04-25 04:38:30 PDT
Yes, that's fixed by that patch.
Comment 9 Justin Clift 2019-05-25 09:32:13 PDT
Sorry for the state change.  Was trying to add this to the 8.0.1 release, and forgot it won't work "as is" as the bug is already fixed.

Just to double check, this *should* go into 8.0.1 yeah?
Comment 10 Dan Gohman 2019-05-25 10:27:28 PDT
Yes, this is now merged in the 8.0.1 branch: https://bugs.llvm.org/show_bug.cgi?id=41602