Skip to content

[MinGW] lld Segmentation fault when build mesa with cfguard #78619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Andarwinux opened this issue Jan 18, 2024 · 6 comments · Fixed by #79063
Closed

[MinGW] lld Segmentation fault when build mesa with cfguard #78619

Andarwinux opened this issue Jan 18, 2024 · 6 comments · Fixed by #79063
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] lld:COFF

Comments

@Andarwinux
Copy link
Contributor

Andarwinux commented Jan 18, 2024

install llvm-mingw nightly

git clone https://gitlab.freedesktop.org/mesa/mesa.git
cd mesa 
mkdir build
cd build

export CFLAGS="-mguard=cf"
export LDFLAGS="-mguard=cf"

meson setup ./ ../ --cross-file=meson_cross.txt -Dvulkan-drivers=microsoft-experimental -Dgallium-drivers= -Dtools= --buildtype=release --default-library=static
ninja

x86_64-w64-mingw32-clang++  -o src/microsoft/vulkan/vulkan_dzn.dll src/microsoft/vulkan/vulkan_dzn.dll.p/meson-generated_.._dzn_entrypoints.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_cmd_buffer.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_descriptor_set.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_device.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_dxcore.cpp.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_image.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_meta.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_nir.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_pipeline.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_query.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_sync.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_util.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_wsi.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_dxgi.c.obj -Wl,-O1 -shared src/vulkan/vulkan_api.def -Wl,--start-group -Wl,--out-implib=src/microsoft/vulkan/vulkan_dzn.dll.a -Wl,--whole-archive src/vulkan/runtime/libvulkan_runtime.a src/vulkan/wsi/libvulkan_wsi.a -Wl,--no-whole-archive -Wl,--nxcompat -Wl,--dynamicbase -static-libgcc -static-libstdc++ -mguard=cf src/microsoft/compiler/libdxil_compiler.a src/util/libmesa_util.a src/util/libmesa_util_sse41.a subprojects/zlib-1.3/libz.a src/util/blake3/libblake3.a src/c11/impl/libmesa_util_c11.a src/microsoft/spirv_to_dxil/libspirv_to_dxil.a src/compiler/nir/libnir.a src/compiler/libcompiler.a src/vulkan/util/libvulkan_util.a subprojects/DirectX-Headers-1.0/libd3dx12-format-properties.a subprojects/DirectX-Headers-1.0/libDirectX-Guids.a src/util/libxmlconfig.a src/compiler/spirv/libvtn.a -Wl,--gc-sections -lm -lsynchronization -lversion -lsynchronization -lsynchronization -lversion -lsynchronization -lsynchronization -lsynchronization -lsynchronization -lsynchronization -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -Wl,--end-group                                                                                         PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.  Stack dump:                                                                                                  0.      Program arguments: ld.lld -m i386pep --shared -Bdynamic -e DllMainCRTStartup --enable-auto-image-base --guard-cf -o src/microsoft/vulkan/vulkan_dzn.dll llvm-mingw/x86_64-w64-mingw32/lib/dllcrt2.o llvm-mingw/x86_64-w64-mingw32/lib/crtbegin.o -Lllvm-mingw/x86_64-w64-mingw32/lib -Lllvm-mingw/x86_64-w64-mingw32/mingw/lib -Lllvm-mingw/lib/clang/18/lib/windows src/microsoft/vulkan/vulkan_dzn.dll.p/meson-generated_.._dzn_entrypoints.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_cmd_buffer.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_descriptor_set.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_device.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_dxcore.cpp.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_image.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_meta.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_nir.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_pipeline.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_query.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_sync.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_util.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_wsi.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_dxgi.c.obj -O1 src/vulkan/vulkan_api.def --start-group --out-implib=src/microsoft/vulkan/vulkan_dzn.dll.a --whole-archive src/vulkan/runtime/libvulkan_runtime.a src/vulkan/wsi/libvulkan_wsi.a --no-whole-archive --nxcompat --dynamicbase src/microsoft/compiler/libdxil_compiler.a src/util/libmesa_util.a src/util/libmesa_util_sse41.a subprojects/zlib-1.3/libz.a src/util/blake3/libblake3.a src/c11/impl/libmesa_util_c11.a src/microsoft/spirv_to_dxil/libspirv_to_dxil.a src/compiler/nir/libnir.a src/compiler/libcompiler.a src/vulkan/util/libvulkan_util.a subprojects/DirectX-Headers-1.0/libd3dx12-format-properties.a subprojects/DirectX-Headers-1.0/libDirectX-Guids.a src/util/libxmlconfig.a src/compiler/spirv/libvtn.a --gc-sections -lm -lsynchronization -lversion -lsynchronization -lsynchronization -lversion -lsynchronization -lsynchronization -lsynchronization -lsynchronization -lsynchronization -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 --end-group -Bstatic -lc++ -Bdynamic -lmingw32 llvm-mingw/lib/clang/18/lib/windows/libclang_rt.builtins-x86_64.a -l:libunwind.a -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 llvm-mingw/lib/clang/18/lib/windows/libclang_rt.builtins-x86_64.a -l:libunwind.a -lmoldname -lmingwex -lmsvcrt -lkernel32 llvm-mingw/x86_64-w64-mingw32/lib/crtend.o
 #0 0x00007fb944e389bf llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (../lib/libLLVM-18git.so+0x8dc9bf)
 #1 0x00007fb944e361c4 (../lib/libLLVM-18git.so+0x8da1c4)
 #2 0x00007fb9440e0010 (/usr/lib/libc.so.6+0x44010)
 #3 0x000055d6af788560 (ld.lld+0x138560)
 #4 0x000055d6af78926f (ld.lld+0x13926f)
 #5 0x000055d6af793103 (ld.lld+0x143103)
 #6 0x000055d6af795f0f lld::coff::writeResult(lld::coff::COFFLinkerContext&) (ld.lld+0x145f0f)
 #7 0x000055d6af73ca1f lld::coff::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) (ld.lld+0xeca1f)
 #8 0x000055d6af73ef3e lld::coff::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) (ld.lld+0xeef3e)
 #9 0x000055d6afac0498 lld::mingw::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) (ld.lld+0x470498)
#10 0x000055d6af708fca lld::unsafeLldMain(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, llvm::ArrayRef<lld::DriverDef>, bool) (ld.lld+0xb8fca)
#11 0x000055d6af708756 lld_main(int, char**, llvm::ToolContext const&) (ld.lld+0xb8756)
#12 0x000055d6af706a4b main (ld.lld+0xb6a4b)
#13 0x00007fb9440c3f50 (/usr/lib/libc.so.6+0x27f50)
#14 0x00007fb9440c400a __libc_start_main (/usr/lib/libc.so.6+0x2800a)
#15 0x000055d6af707fce _start (ld.lld+0xb7fce)
clang: error: unable to execute command: Segmentation fault (core dumped)
clang: error: linker command failed due to signal (use -v to see invocation)
ninja: build stopped: subcommand failed.

meson_cross.txt

[binaries]
c = 'x86_64-w64-mingw32-clang'
cpp = 'x86_64-w64-mingw32-clang++'
ar = 'x86_64-w64-mingw32-ar'
strip = 'x86_64-w64-mingw32-strip'
windres = 'x86_64-w64-mingw32-windres'
dlltool = 'x86_64-w64-mingw32-dlltool'

[host_machine]
system = 'windows'
cpu_family = 'x86_64'
cpu = 'x86_64-w64-mingw32'
endian = 'little'
@github-actions github-actions bot added the lld label Jan 18, 2024
@EugeneZelenko EugeneZelenko added lld:COFF crash Prefer [crash-on-valid] or [crash-on-invalid] and removed lld labels Jan 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 18, 2024

@llvm/issue-subscribers-lld-coff

Author: None (Andarwinux)

install llvm-mingw nightly
git clone https://gitlab.freedesktop.org/mesa/mesa.git
cd mesa 
mkdir build
cd build

export CFLAGS="-mguard=cf"
export LDFLAGS="-mguard=cf"

meson setup ./ ../ --cross-file=meson_cross.txt -Dvulkan-drivers=microsoft-experimental -Dgallium-drivers= -Dtools= --buildtype=release --default-library=static
ninja

x86_64-w64-mingw32-clang++  -o src/microsoft/vulkan/vulkan_dzn.dll src/microsoft/vulkan/vulkan_dzn.dll.p/meson-generated_.._dzn_entrypoints.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_cmd_buffer.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_descriptor_set.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_device.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_dxcore.cpp.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_image.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_meta.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_nir.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_pipeline.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_query.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_sync.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_util.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_wsi.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_dxgi.c.obj -Wl,-O1 -shared src/vulkan/vulkan_api.def -Wl,--start-group -Wl,--out-implib=src/microsoft/vulkan/vulkan_dzn.dll.a -Wl,--whole-archive src/vulkan/runtime/libvulkan_runtime.a src/vulkan/wsi/libvulkan_wsi.a -Wl,--no-whole-archive -Wl,--nxcompat -Wl,--dynamicbase -static-libgcc -static-libstdc++ -mguard=cf src/microsoft/compiler/libdxil_compiler.a src/util/libmesa_util.a src/util/libmesa_util_sse41.a subprojects/zlib-1.3/libz.a src/util/blake3/libblake3.a src/c11/impl/libmesa_util_c11.a src/microsoft/spirv_to_dxil/libspirv_to_dxil.a src/compiler/nir/libnir.a src/compiler/libcompiler.a src/vulkan/util/libvulkan_util.a subprojects/DirectX-Headers-1.0/libd3dx12-format-properties.a subprojects/DirectX-Headers-1.0/libDirectX-Guids.a src/util/libxmlconfig.a src/compiler/spirv/libvtn.a -Wl,--gc-sections -lm -lsynchronization -lversion -lsynchronization -lsynchronization -lversion -lsynchronization -lsynchronization -lsynchronization -lsynchronization -lsynchronization -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -Wl,--end-group                                                                                         PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.  Stack dump:                                                                                                  0.      Program arguments: ld.lld -m i386pep --shared -Bdynamic -e DllMainCRTStartup --enable-auto-image-base --guard-cf -o src/microsoft/vulkan/vulkan_dzn.dll llvm-mingw/x86_64-w64-mingw32/lib/dllcrt2.o llvm-mingw/x86_64-w64-mingw32/lib/crtbegin.o -Lllvm-mingw/x86_64-w64-mingw32/lib -Lllvm-mingw/x86_64-w64-mingw32/mingw/lib -Lllvm-mingw/lib/clang/18/lib/windows src/microsoft/vulkan/vulkan_dzn.dll.p/meson-generated_.._dzn_entrypoints.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_cmd_buffer.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_descriptor_set.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_device.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_dxcore.cpp.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_image.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_meta.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_nir.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_pipeline.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_query.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_sync.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_util.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_wsi.c.obj src/microsoft/vulkan/vulkan_dzn.dll.p/dzn_dxgi.c.obj -O1 src/vulkan/vulkan_api.def --start-group --out-implib=src/microsoft/vulkan/vulkan_dzn.dll.a --whole-archive src/vulkan/runtime/libvulkan_runtime.a src/vulkan/wsi/libvulkan_wsi.a --no-whole-archive --nxcompat --dynamicbase src/microsoft/compiler/libdxil_compiler.a src/util/libmesa_util.a src/util/libmesa_util_sse41.a subprojects/zlib-1.3/libz.a src/util/blake3/libblake3.a src/c11/impl/libmesa_util_c11.a src/microsoft/spirv_to_dxil/libspirv_to_dxil.a src/compiler/nir/libnir.a src/compiler/libcompiler.a src/vulkan/util/libvulkan_util.a subprojects/DirectX-Headers-1.0/libd3dx12-format-properties.a subprojects/DirectX-Headers-1.0/libDirectX-Guids.a src/util/libxmlconfig.a src/compiler/spirv/libvtn.a --gc-sections -lm -lsynchronization -lversion -lsynchronization -lsynchronization -lversion -lsynchronization -lsynchronization -lsynchronization -lsynchronization -lsynchronization -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 --end-group -Bstatic -lc++ -Bdynamic -lmingw32 llvm-mingw/lib/clang/18/lib/windows/libclang_rt.builtins-x86_64.a -l:libunwind.a -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 llvm-mingw/lib/clang/18/lib/windows/libclang_rt.builtins-x86_64.a -l:libunwind.a -lmoldname -lmingwex -lmsvcrt -lkernel32 llvm-mingw/x86_64-w64-mingw32/lib/crtend.o
 #<!-- -->0 0x00007fb944e389bf llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) (../lib/libLLVM-18git.so+0x8dc9bf)
 #<!-- -->1 0x00007fb944e361c4 (../lib/libLLVM-18git.so+0x8da1c4)
 #<!-- -->2 0x00007fb9440e0010 (/usr/lib/libc.so.6+0x44010)
 #<!-- -->3 0x000055d6af788560 (ld.lld+0x138560)
 #<!-- -->4 0x000055d6af78926f (ld.lld+0x13926f)
 #<!-- -->5 0x000055d6af793103 (ld.lld+0x143103)
 #<!-- -->6 0x000055d6af795f0f lld::coff::writeResult(lld::coff::COFFLinkerContext&amp;) (ld.lld+0x145f0f)
 #<!-- -->7 0x000055d6af73ca1f lld::coff::LinkerDriver::linkerMain(llvm::ArrayRef&lt;char const*&gt;) (ld.lld+0xeca1f)
 #<!-- -->8 0x000055d6af73ef3e lld::coff::link(llvm::ArrayRef&lt;char const*&gt;, llvm::raw_ostream&amp;, llvm::raw_ostream&amp;, bool, bool) (ld.lld+0xeef3e)
 #<!-- -->9 0x000055d6afac0498 lld::mingw::link(llvm::ArrayRef&lt;char const*&gt;, llvm::raw_ostream&amp;, llvm::raw_ostream&amp;, bool, bool) (ld.lld+0x470498)
#<!-- -->10 0x000055d6af708fca lld::unsafeLldMain(llvm::ArrayRef&lt;char const*&gt;, llvm::raw_ostream&amp;, llvm::raw_ostream&amp;, llvm::ArrayRef&lt;lld::DriverDef&gt;, bool) (ld.lld+0xb8fca)
#<!-- -->11 0x000055d6af708756 lld_main(int, char**, llvm::ToolContext const&amp;) (ld.lld+0xb8756)
#<!-- -->12 0x000055d6af706a4b main (ld.lld+0xb6a4b)
#<!-- -->13 0x00007fb9440c3f50 (/usr/lib/libc.so.6+0x27f50)
#<!-- -->14 0x00007fb9440c400a __libc_start_main (/usr/lib/libc.so.6+0x2800a)
#<!-- -->15 0x000055d6af707fce _start (ld.lld+0xb7fce)
clang: error: unable to execute command: Segmentation fault (core dumped)
clang: error: linker command failed due to signal (use -v to see invocation)
ninja: build stopped: subcommand failed.

meson_cross.txt

c = 'x86_64-w64-mingw32-clang'
cpp = 'x86_64-w64-mingw32-clang++'
ar = 'x86_64-w64-mingw32-ar'
strip = 'x86_64-w64-mingw32-strip'
windres = 'x86_64-w64-mingw32-windres'
dlltool = 'x86_64-w64-mingw32-dlltool'

[host_machine]
system = 'windows'
cpu_family = 'x86_64'
cpu = 'x86_64-w64-mingw32'
endian = 'little'

@mstorsjo
Copy link
Member

Can you rerun the link command with -Wl,--reproduce=repro.tar, compress that repro.tar and share it somehow? That lets us repro the exact crash without needing the rest of the toolchain and the source.

Also, you say that the issue reproduces with the latest nightlies. Does this mean that it's a regression from the latest stable release, or did you only happen to test with a nightly?

@Andarwinux
Copy link
Contributor Author

Can you rerun the link command with -Wl,--reproduce=repro.tar, compress that repro.tar and share it somehow? That lets us repro the exact crash without needing the rest of the toolchain and the source.

repro.tar.zip

Also, you say that the issue reproduces with the latest nightlies. Does this mean that it's a regression from the latest stable release, or did you only happen to test with a nightly?

This doesn't seem to be a regression, the same thing happens with llvm17.

@mstorsjo
Copy link
Member

Can you rerun the link command with -Wl,--reproduce=repro.tar, compress that repro.tar and share it somehow? That lets us repro the exact crash without needing the rest of the toolchain and the source.

repro.tar.zip

Thanks! It turns out that the lld reproducer generation doesn’t work well here - we’re missing rewriting paths for the wholearchive options. That’s trivial to fix though, but after that, we run into the issue that some of the bundled archives are thin archives, which don’t work here (the repro file would need to contain the input loose object files for that archive, and possibly rewrite the thin archive). That’s not quite trivial to fix…

I did manage to reproduce the issue myself by building mesa manually with the given instructions though.

Also, you say that the issue reproduces with the latest nightlies. Does this mean that it's a regression from the latest stable release, or did you only happen to test with a nightly?

This doesn't seem to be a regression, the same thing happens with llvm17.

Ok, thanks for checking!

@mstorsjo
Copy link
Member

I've reduced the issue to the following minimal testcase:

void weakfunc(void) __attribute__((weak));
typedef void (*funcptr)(void);
funcptr funcs[] = { weakfunc };
void entry(void) { }
$ clang -target x86_64-windows-gnu -c weak-undef-addrtaken.c -mguard=cf
$ lld-link weak-undef-addrtaken.o -out:out.exe -subsystem:console -entry:entry -guard:cf
lld-link: /home/martin/code/llvm-project/llvm/include/llvm/Support/Casting.h:662: decltype(auto) llvm::dyn_cast(From *) [To = lld::coff::SectionChunk, From = lld::coff::Chunk]: Assertion `detail::isPresent(Val) && "dyn_cast on a non-existent value"' failed.

mstorsjo added a commit to mstorsjo/llvm-project that referenced this issue Jan 22, 2024
When marking symbols as having their address taken, we can have the
sitaution where we have the address taken of a weak symbol. If there's
no strong definition of the symbol, the symbol ends up as an absolute
symbol with the value null. In those cases, we don't have any
Chunk. Skip such symbols from the cfguard tables.

This fixes llvm#78619.
@mstorsjo
Copy link
Member

I posted a PR that fixes this at #79063.

mstorsjo added a commit that referenced this issue Jan 23, 2024
)

When marking symbols as having their address taken, we can have the
sitaution where we have the address taken of a weak symbol. If there's
no strong definition of the symbol, the symbol ends up as an absolute
symbol with the value null. In those cases, we don't have any Chunk.
Skip such symbols from the cfguard tables.

This fixes #78619.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] lld:COFF
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants