Issue 60949
Summary 14.0.6 llvm-ranlib: crash when called on emscripten .a file produced by GHC's new JS backend
Labels new issue
Assignees
Reporter sternenseemann
    When compiling just about any Haskell library using GHC's new JS backend which involves creating an `ar` file, `llvm-ranlib` is called (via emscripten's `emranlib`), triggering the following crash:

```
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/b4z1yykly634q6ric5d3w6162slxzrbf-emscripten-llvm-3.1.24/bin/llvm-ranlib test.a
 #0 0x00007f808df7afe4 PrintStackTraceSignalHandler(void*) (/nix/store/338k2wvhmwgkw3ipqd8f9rrym4sg1ipj-llvm-14.0.6-lib/lib/libLLVM-14.so+0xeaefe4)
 #1 0x00007f808df7866b SignalHandler(int) (/nix/store/338k2wvhmwgkw3ipqd8f9rrym4sg1ipj-llvm-14.0.6-lib/lib/libLLVM-14.so+0xeac66b)
 #2 0x00007f808cbe0bf0 __restore_rt (/nix/store/lqz6hmd86viw83f9qll2ip87jhb7p1ah-glibc-2.35-224/lib/libc.so.6+0x3dbf0)
 #3 0x00007f808cc2dbc7 __pthread_kill_implementation (/nix/store/lqz6hmd86viw83f9qll2ip87jhb7p1ah-glibc-2.35-224/lib/libc.so.6+0x8abc7)
 #4 0x00007f808cbe0b46 gsignal (/nix/store/lqz6hmd86viw83f9qll2ip87jhb7p1ah-glibc-2.35-224/lib/libc.so.6+0x3db46)
 #5 0x00007f808cbcb4b5 abort (/nix/store/lqz6hmd86viw83f9qll2ip87jhb7p1ah-glibc-2.35-224/lib/libc.so.6+0x284b5)
 #6 0x00007f808de80314 llvm::report_fatal_error(llvm::Twine const&, bool) (/nix/store/338k2wvhmwgkw3ipqd8f9rrym4sg1ipj-llvm-14.0.6-lib/lib/libLLVM-14.so+0xdb4314)
 #7 0x00007f808de8046a (/nix/store/338k2wvhmwgkw3ipqd8f9rrym4sg1ipj-llvm-14.0.6-lib/lib/libLLVM-14.so+0xdb446a)
 #8 0x00007f808fc1ffd1 readULEB128(llvm::object::WasmObjectFile::ReadContext&) (/nix/store/338k2wvhmwgkw3ipqd8f9rrym4sg1ipj-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2b53fd1)
 #9 0x00007f808fc2efd7 llvm::object::WasmObjectFile::WasmObjectFile(llvm::MemoryBufferRef, llvm::Error&) (/nix/store/338k2wvhmwgkw3ipqd8f9rrym4sg1ipj-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2b62fd7)
#10 0x00007f808fc2f442 llvm::object::ObjectFile::createWasmObjectFile(llvm::MemoryBufferRef) (/nix/store/338k2wvhmwgkw3ipqd8f9rrym4sg1ipj-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2b63442)
#11 0x00007f808fc13fbe llvm::object::ObjectFile::createObjectFile(llvm::MemoryBufferRef, llvm::file_magic, bool) (/nix/store/338k2wvhmwgkw3ipqd8f9rrym4sg1ipj-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2b47fbe)
#12 0x00007f808fc17879 llvm::object::SymbolicFile::createSymbolicFile(llvm::MemoryBufferRef, llvm::file_magic, llvm::LLVMContext*, bool) (/nix/store/338k2wvhmwgkw3ipqd8f9rrym4sg1ipj-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2b4b879)
#13 0x00007f808fb79066 llvm::writeArchiveToStream(llvm::raw_ostream&, llvm::ArrayRef<llvm::NewArchiveMember>, bool, llvm::object::Archive::Kind, bool, bool) (/nix/store/338k2wvhmwgkw3ipqd8f9rrym4sg1ipj-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2aad066)
#14 0x00007f808fb7b29f 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/338k2wvhmwgkw3ipqd8f9rrym4sg1ipj-llvm-14.0.6-lib/lib/libLLVM-14.so+0x2aaf29f)
#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/b4z1yykly634q6ric5d3w6162slxzrbf-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/b4z1yykly634q6ric5d3w6162slxzrbf-emscripten-llvm-3.1.24/bin/llvm-ranlib+0x40e96d)
#17 0x000000000040eb13 performOperation(ArchiveOperation, std::vector<llvm::NewArchiveMember, std::allocator<llvm::NewArchiveMember> >*) (/nix/store/b4z1yykly634q6ric5d3w6162slxzrbf-emscripten-llvm-3.1.24/bin/llvm-ranlib+0x40eb13)
#18 0x0000000000406a5c main (/nix/store/b4z1yykly634q6ric5d3w6162slxzrbf-emscripten-llvm-3.1.24/bin/llvm-ranlib+0x406a5c)
#19 0x00007f808cbcc24e __libc_start_call_main (/nix/store/lqz6hmd86viw83f9qll2ip87jhb7p1ah-glibc-2.35-224/lib/libc.so.6+0x2924e)
#20 0x00007f808cbcc309 __libc_start_main@GLIBC_2.2.5 (/nix/store/lqz6hmd86viw83f9qll2ip87jhb7p1ah-glibc-2.35-224/lib/libc.so.6+0x29309)
#21 0x0000000000407d75 _start (/nix/store/b4z1yykly634q6ric5d3w6162slxzrbf-emscripten-llvm-3.1.24/bin/llvm-ranlib+0x407d75)
```

`emranlib` is just a wrapper around `llvm-ranlib` that transparently passes through all arguments to `llvm-ranlib`. `/nix/store/b4z1yykly634q6ric5d3w6162slxzrbf-emscripten-llvm-3.1.24/bin/llvm-ranlib` is just a symlink to normal `llvm-ranlib` 14.0.6:

```console
> realpath /nix/store/b4z1yykly634q6ric5d3w6162slxzrbf-emscripten-llvm-3.1.24/bin/llvm-ranlib
/nix/store/p8wpdbr2m12ydclalh33sf8mlygxgh11-llvm-14.0.6/bin/llvm-ar
> /nix/store/b4z1yykly634q6ric5d3w6162slxzrbf-emscripten-llvm-3.1.24/bin/llvm-ranlib --version
LLVM (http://llvm.org/):
  LLVM version 14.0.6
  Optimized build.
  Default target: x86_64-unknown-linux-gnu
  Host CPU: skylake
```

It should be easy to reproduce the crash by running on `llvm-ranlib` on the [archive file produced by GHC](https://sterni.lv/tmp/repro-ranlib-crash.a.tar.gz) (had to upload externally due to GitHub's file size limit) by just executing `llvm-ranlib repro-ranlib-crash.a`.

<details>

<summary>Reproducing this using just GHC</summary>

1. You need to build GHC as a cross-compiler for `_javascript_-unknown-ghcjs` (for older revisions `js-unknown-ghcjs`) – using LLVM 14.0.6 of course. [Build instructions are available upstream](https://gitlab.haskell.org/ghc/ghc/-/wikis/_javascript_-backend). I used GHC revision `a203ad854ffee802e6bf0aca26e6c9a99bec3865`.  
  If you want to use Nix, you can checkout `e23efa222d1d7af7fa4380a392afd359f9e5116c` of [my nixpkgs fork](https://github.com/sternenseemann/nixpkgs/tree/e23efa222d1d7af7fa4380a392afd359f9e5116c) and build `nix-build -A pkgsCross.ghcjs.buildPackages.haskell.compiler.ghcHEAD`.
2. Create the following `Test.hs`:
       
       module Test (test) where
       
       test x = x + 41

3. Run `_javascript_-unknown-ghcjs-ghc -outputdir build/ -v -o repro-ranlib-crash.a --make -staticlib Test.hs`. You should see the crash already.
4. You can reproduce the crash again by running `llvm-ranlib repro-ranlib-crash.a`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to