master, 9.6: GHC's javascript backend produces ar files that make llvm-ranlib crash
Summary
When creating the ar file for a library (e.g. when building a library using Cabal) with the js backend, GHC calls emranlib
(which is a wrapper around llvm-ranlib
) on an ar file that makes it crash. This is possibly a bug in LLVM and I have already reported it on their side, but I figured it makes sense to have an issue here as well.
cc @TerrorJack @hsyl20 @doyougnu
Steps to reproduce
- Use GHC 9.6.1 or GHC HEAD (tested a203ad85, 04b80850). I got mine from nixpkgs, i.e.
pkgsCross.ghcjs.buildPackages.haskell.compiler.{ghc961,ghcHEAD}
. - Create a
Test.hs
like so:module Test (test) where test x = x + 41
- Run
javascript-unknown-ghcjs-ghc -v -outputdir build -staticlib --make Test.hs
:
Glasgow Haskell Compiler, Version 9.7.20230406, stage 1 booted by GHC version 9.2.4
*** initializing unit database:
package flags []
loading package database /nix/store/ps7cax4g5lqw81b8f2myv1xbvrvchndi-javascript-unknown-ghcjs-ghc-native-bignum-9.7.20230406/lib/javascript-unknown-ghcjs-ghc-9.7.20230406/lib/package.conf.d
wired-in package ghc-prim mapped to ghc-prim-0.10.0
wired-in package ghc-bignum mapped to ghc-bignum-1.3
wired-in package base mapped to base-4.18.0.0
wired-in package rts mapped to rts-1.0.2
wired-in package template-haskell mapped to template-haskell-2.20.0.0
wired-in package ghc mapped to ghc-9.7
!!! initializing unit database: finished in 1.47 milliseconds, allocated 2.167 megabytes
*** Chasing dependencies:
Chasing modules from: main:Test.hs
!!! Chasing dependencies: finished in 0.72 milliseconds, allocated 0.344 megabytes
Ready for upsweep
[SingleModule(main:Test []), SingleModule(LN: [main:Test])]
compile: input file Test.hs
*** Checking old interface for Test (use -ddump-hi-diffs for more details):
[1 of 2] Compiling Test ( Test.hs, build/Test.o )
*** Parser [Test]:
!!! Parser [Test]: finished in 0.13 milliseconds, allocated 0.096 megabytes
*** Renamer/typechecker [Test]:
!!! Renamer/typechecker [Test]: finished in 35.07 milliseconds, allocated 13.290 megabytes
*** Desugar [Test]:
Result size of Desugar (before optimization)
= {terms: 23, types: 18, coercions: 0, joins: 0/3}
Result size of Desugar (after optimization)
= {terms: 17, types: 12, coercions: 0, joins: 0/0}
!!! Desugar [Test]: finished in 0.44 milliseconds, allocated 0.250 megabytes
*** Simplifier [Test]:
Result size of Simplifier iteration=1
= {terms: 25, types: 16, coercions: 0, joins: 0/0}
Result size of Simplifier
= {terms: 25, types: 16, coercions: 0, joins: 0/0}
!!! Simplifier [Test]: finished in 0.36 milliseconds, allocated 0.220 megabytes
*** CoreTidy [Test]:
!!! CoreTidy [Test]: finished in 0.07 milliseconds, allocated 0.046 megabytes
Result size of Tidy Core
= {terms: 25, types: 16, coercions: 0, joins: 0/0}
*** CorePrep [Test]:
Result size of CorePrep
= {terms: 29, types: 18, coercions: 0, joins: 0/2}
!!! CorePrep [Test]: finished in 0.14 milliseconds, allocated 0.093 megabytes
*** CoreToStg [Test]:
*** Stg2Stg:
!!! CoreToStg [Test]: finished in 0.10 milliseconds, allocated 0.062 megabytes
*** CodeGen [Test]:
!!! CodeGen [Test]: finished in 4.85 milliseconds, allocated 4.541 megabytes
*** WriteIface [build/Test.hi]:
writeBinIface: 2 Names
writeBinIface: 22 dict entries
!!! WriteIface [build/Test.hi]: finished in 0.49 milliseconds, allocated 1.088 megabytes
*** systool:touch:
*** Touching object file:
touch build/Test.o
!!! systool:touch: finished in 0.42 milliseconds, allocated 0.096 megabytes
*** Deleting temp files:
Deleting:
*** Deleting temp subdirs:
Deleting:
link: hmi ...
Test
link: linkables are ...
LinkableM (2023-04-06 16:55:11.927257346 UTC) Test
[DotO build/Test.o]
link: pkg deps are ...
base
[2 of 2] Linking main.jsexe [Objects changed]
*** systool:ranlib:
*** Ranlib:
/nix/store/qa01ynplsyrhd42h2s28wzsd5s12c64b-emscripten-3.1.24/bin/emranlib main.jsexe
LLVM ERROR: malformed uleb128, extends past end
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0. Program arguments: /nix/store/cqq9dclf9x0zbvzhkbbzjkbrfv1s12dj-emscripten-llvm-3.1.24/bin/llvm-ranlib main.jsexe
#0 0x00007fc0e393b2c4 PrintStackTraceSignalHandler(void*) (/nix/store/6pss9zx726ndklf50b77gvyhn107v1mq-llvm-14.0.6-lib/lib/libLLVM-14.so+0xf3b2c4)
#1 0x00007fc0e393894b SignalHandler(int) (/nix/store/6pss9zx726ndklf50b77gvyhn107v1mq-llvm-14.0.6-lib/lib/libLLVM-14.so+0xf3894b)
#2 0x00007fc0e223dbf0 __restore_rt (/nix/store/8xk4yl1r3n6kbyn05qhan7nbag7npymx-glibc-2.35-224/lib/libc.so.6+0x3dbf0)
#3 0x00007fc0e228abc7 __pthread_kill_implementation (/nix/store/8xk4yl1r3n6kbyn05qhan7nbag7npymx-glibc-2.35-224/lib/libc.so.6+0x8abc7)
#4 0x00007fc0e223db46 gsignal (/nix/store/8xk4yl1r3n6kbyn05qhan7nbag7npymx-glibc-2.35-224/lib/libc.so.6+0x3db46)
#5 0x00007fc0e22284b5 abort (/nix/store/8xk4yl1r3n6kbyn05qhan7nbag7npymx-glibc-2.35-224/lib/libc.so.6+0x284b5)
#6 0x00007fc0e38405f4 llvm::report_fatal_error(llvm::Twine const&, bool) (/nix/store/6pss9zx726ndklf50b77gvyhn107v1mq-llvm-14.0.6-lib/lib/libLLVM-14.so+0xe405f4)
#7 0x00007fc0e384074a (/nix/store/6pss9zx726ndklf50b77gvyhn107v1mq-llvm-14.0.6-lib/lib/libLLVM-14.so+0xe4074a)
#8 0x00007fc0e55e0351 readULEB128(llvm::object::WasmObjectFile::ReadContext&) (/nix/store/6pss9zx726ndklf50b77gvyhn107v1mq-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2be0351)
#9 0x00007fc0e55ef357 llvm::object::WasmObjectFile::WasmObjectFile(llvm::MemoryBufferRef, llvm::Error&) (/nix/store/6pss9zx726ndklf50b77gvyhn107v1mq-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2bef357)
#10 0x00007fc0e55ef7c2 llvm::object::ObjectFile::createWasmObjectFile(llvm::MemoryBufferRef) (/nix/store/6pss9zx726ndklf50b77gvyhn107v1mq-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2bef7c2)
#11 0x00007fc0e55d433e llvm::object::ObjectFile::createObjectFile(llvm::MemoryBufferRef, llvm::file_magic, bool) (/nix/store/6pss9zx726ndklf50b77gvyhn107v1mq-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2bd433e)
#12 0x00007fc0e55d7bf9 llvm::object::SymbolicFile::createSymbolicFile(llvm::MemoryBufferRef, llvm::file_magic, llvm::LLVMContext*, bool) (/nix/store/6pss9zx726ndklf50b77gvyhn107v1mq-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2bd7bf9)
#13 0x00007fc0e55393e6 llvm::writeArchiveToStream(llvm::raw_ostream&, llvm::ArrayRef<llvm::NewArchiveMember>, bool, llvm::object::Archive::Kind, bool, bool) (/nix/store/6pss9zx726ndklf50b77gvyhn107v1mq-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2b393e6)
#14 0x00007fc0e553b61f llvm::writeArchive(llvm::StringRef, llvm::ArrayRef<llvm::NewArchiveMember>, bool, llvm::object::Archive::Kind, bool, bool, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer> >) (/nix/store/6pss9zx726ndklf50b77gvyhn107v1mq-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2b3b61f)
#15 0x000000000040c1bc performWriteOperation(ArchiveOperation, llvm::object::Archive*, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer> >, std::vector<llvm::NewArchiveMember, std::allocator<llvm::NewArchiveMember> >*) (/nix/store/cqq9dclf9x0zbvzhkbbzjkbrfv1s12dj-emscripten-llvm-3.1.24/bin/llvm-ranlib+0x40c1bc)
#16 0x000000000040e96d performOperation(ArchiveOperation, llvm::object::Archive*, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer> >, std::vector<llvm::NewArchiveMember, std::allocator<llvm::NewArchiveMember> >*) (/nix/store/cqq9dclf9x0zbvzhkbbzjkbrfv1s12dj-emscripten-llvm-3.1.24/bin/llvm-ranlib+0x40e96d)
#17 0x000000000040eb13 performOperation(ArchiveOperation, std::vector<llvm::NewArchiveMember, std::allocator<llvm::NewArchiveMember> >*) (/nix/store/cqq9dclf9x0zbvzhkbbzjkbrfv1s12dj-emscripten-llvm-3.1.24/bin/llvm-ranlib+0x40eb13)
#18 0x0000000000406a5c main (/nix/store/cqq9dclf9x0zbvzhkbbzjkbrfv1s12dj-emscripten-llvm-3.1.24/bin/llvm-ranlib+0x406a5c)
#19 0x00007fc0e222924e __libc_start_call_main (/nix/store/8xk4yl1r3n6kbyn05qhan7nbag7npymx-glibc-2.35-224/lib/libc.so.6+0x2924e)
#20 0x00007fc0e2229309 __libc_start_main@GLIBC_2.2.5 (/nix/store/8xk4yl1r3n6kbyn05qhan7nbag7npymx-glibc-2.35-224/lib/libc.so.6+0x29309)
#21 0x0000000000407d75 _start (/nix/store/cqq9dclf9x0zbvzhkbbzjkbrfv1s12dj-emscripten-llvm-3.1.24/bin/llvm-ranlib+0x407d75)
javascript-unknown-ghcjs-ghc-9.7.20230406: `emranlib' failed in phase `Ranlib'. (Exit code: 250)
*** Deleting temp files:
Deleting:
*** Deleting temp subdirs:
Deleting:
*** Deleting temp dirs:
Deleting:
The ar file produced on my machine is available here as well (I think produced with a203ad85).
Expected behavior
Compilation succeeds including the Ranlib phase.
Environment
Optional:
- Operating System: NixOS
- System Architecture: x86_64-linux