Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

[llvm-mc] crash on Free Pascal WebAssembly output with DWARF debug info #47940

Closed Quuxplusone closed 3 years ago

Quuxplusone commented 3 years ago
Bugzilla Link PR48971
Status RESOLVED FIXED
Importance P enhancement
Reported by Nikolay Nikolov (nickysn@gmail.com)
Reported on 2021-01-30 15:53:22 -0800
Last modified on 2021-02-05 13:57:04 -0800
Version 11.0
Hardware PC Linux
CC aardappel@gmail.com, aheejin@gmail.com, dschuff@google.com, llvm-bugs@lists.llvm.org, ydelendik@mozilla.com
Fixed by commit(s)
Attachments system.wat.gz (916071 bytes, application/gzip)
Blocks
Blocked by
See also
Created attachment 24468
The actual file, produced by the FPC compiler, that causes the crash. Gzipped
to fit the 1MB bugzilla file attachment limit.

I'm working on a non-LLVM based WebAssembly back-end for the Free Pascal
Compiler. It only uses llvm-mc as a back-end assembler and wasm-ld as a linker.
When I enable the DWARF debug info output, I get the following error from llvm-
mc:

PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash
backtrace.
Stack dump:
0.  Program arguments: /usr/bin/llvm-mc --assemble --arch=wasm32 -mattr=+sign-
ext --filetype=obj -o ./system.o ./system.wat
 #0 0x00007f1373ede27a llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/lib64/libLLVM-11.so+0xa8b27a)
 #1 0x00007f1373edc4e4 llvm::sys::RunSignalHandlers() (/lib64/libLLVM-11.so+0xa894e4)
 #2 0x00007f1373edc676 (/lib64/libLLVM-11.so+0xa89676)
 #3 0x00007f13730dda60 __restore_rt (/lib64/libc.so.6+0x3da60)
 #4 0x00007f13751cbe89 (/lib64/libLLVM-11.so+0x1d78e89)
 #5 0x00007f1375179e71 llvm::MCAssembler::handleFixup(llvm::MCAsmLayout const&, llvm::MCFragment&, llvm::MCFixup const&) (/lib64/libLLVM-11.so+0x1d26e71)
 #6 0x00007f137517afc1 llvm::MCAssembler::layout(llvm::MCAsmLayout&) (/lib64/libLLVM-11.so+0x1d27fc1)
 #7 0x00007f137517b1a9 llvm::MCAssembler::Finish() (/lib64/libLLVM-11.so+0x1d281a9)
 #8 0x00007f13752016fc (/lib64/libLLVM-11.so+0x1dae6fc)
 #9 0x00005610c7f71ba1 (/usr/bin/llvm-mc+0xbba1)
#10 0x00007f13730c81e2 __libc_start_main (/lib64/libc.so.6+0x281e2)
#11 0x00005610c7f71dbe (/usr/bin/llvm-mc+0xbdbe)

I'm sure there's something wrong or unsupported in the FPC generated .wat file,
but it's very difficult for me to figure out what it is from this error,
because it doesn't give me a line number, nor an identifier, that I can search
for. I don't necessarily expect a very user friendly error, but at least the
line number where an expected error occured would be very helpful.

I'm using llvm-mc from the llvm-11.0.0-1.fc33.x86_64 package from the official
Fedora 33 repositories. I tried also building llvm-mc from the git main branch
from a wwek ago (commit 689de5841c1c4c9b0fe711b61d26f7425cf99423). With this
version I get this error:

llvm-mc: /home/nickysn/tralala/llvm/llvm-
project/llvm/include/llvm/ADT/DenseMap.h:1239: llvm::DenseMapIterator<KeyT,
ValueT, KeyInfoT, Bucket, IsConst>::value_type* llvm::DenseMapIterator<KeyT,
ValueT, KeyInfoT, Bucket, IsConst>::operator->() const [with KeyT = const
llvm::MCSection*; ValueT = const llvm::MCSymbol*; KeyInfoT =
llvm::DenseMapInfo<const llvm::MCSection*>; Bucket =
llvm::detail::DenseMapPair<const llvm::MCSection*, const llvm::MCSymbol*>; bool
IsConst = false; llvm::DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket,
IsConst>::pointer = llvm::detail::DenseMapPair<const llvm::MCSection*, const
llvm::MCSymbol*>*; llvm::DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket,
IsConst>::value_type = llvm::detail::DenseMapPair<const llvm::MCSection*, const
llvm::MCSymbol*>]: Assertion `Ptr != End && "dereferencing end() iterator"'
failed.
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash
backtrace.
Stack dump:
0.  Program arguments: /home/nickysn/tralala/llvm/llvm-project/build-
lld/bin/llvm-mc --assemble --arch=wasm32 -mattr=+sign-ext --filetype=obj -o
./system.o ./system.wat
 #0 0x0000000000c58ac5 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/nickysn/tralala/llvm/llvm-project/llvm/lib/Support/Unix/Signals.inc:565:22
 #1 0x0000000000c58b7c PrintStackTraceSignalHandler(void*) /home/nickysn/tralala/llvm/llvm-project/llvm/lib/Support/Unix/Signals.inc:632:1
 #2 0x0000000000c56b7c llvm::sys::RunSignalHandlers() /home/nickysn/tralala/llvm/llvm-project/llvm/lib/Support/Signals.cpp:71:20
 #3 0x0000000000c58518 SignalHandler(int) /home/nickysn/tralala/llvm/llvm-project/llvm/lib/Support/Unix/Signals.inc:407:1
 #4 0x00007f40c93c11e0 __restore_rt (/lib64/libpthread.so.0+0x141e0)
 #5 0x00007f40c8ea89d5 raise (/lib64/libc.so.6+0x3d9d5)
 #6 0x00007f40c8e918a4 abort (/lib64/libc.so.6+0x268a4)
 #7 0x00007f40c8e91789 _nl_load_domain.cold (/lib64/libc.so.6+0x26789)
 #8 0x00007f40c8ea1026 (/lib64/libc.so.6+0x36026)
 #9 0x0000000000ae738e llvm::DenseMapIterator<llvm::MCSection const*, llvm::MCSymbol const*, llvm::DenseMapInfo<llvm::MCSection const*>, llvm::detail::DenseMapPair<llvm::MCSection const*, llvm::MCSymbol const*>, false>::operator->() const /home/nickysn/tralala/llvm/llvm-project/llvm/include/llvm/ADT/DenseMap.h:1240:35
#10 0x0000000000ad4f8d (anonymous
namespace)::WasmObjectWriter::recordRelocation(llvm::MCAssembler&,
llvm::MCAsmLayout const&, llvm::MCFragment const*, llvm::MCFixup const&,
llvm::MCValue, unsigned long&) /home/nickysn/tralala/llvm/llvm-
project/llvm/lib/MC/WasmObjectWriter.cpp:492:21
#11 0x0000000000a3e399 llvm::MCAssembler::handleFixup(llvm::MCAsmLayout const&,
llvm::MCFragment&, llvm::MCFixup const&) /home/nickysn/tralala/llvm/llvm-
project/llvm/lib/MC/MCAssembler.cpp:812:35
#12 0x0000000000a3eeb2 llvm::MCAssembler::layout(llvm::MCAsmLayout&)
/home/nickysn/tralala/llvm/llvm-project/llvm/lib/MC/MCAssembler.cpp:936:48
#13 0x0000000000a3efdc llvm::MCAssembler::Finish()
/home/nickysn/tralala/llvm/llvm-project/llvm/lib/MC/MCAssembler.cpp:951:35
#14 0x0000000000a9d112 llvm::MCObjectStreamer::finishImpl()
/home/nickysn/tralala/llvm/llvm-project/llvm/lib/MC/MCObjectStreamer.cpp:863:1
#15 0x0000000000abb52b llvm::MCWasmStreamer::finishImpl()
/home/nickysn/tralala/llvm/llvm-project/llvm/lib/MC/MCWasmStreamer.cpp:186:1
#16 0x0000000000aad917 llvm::MCStreamer::Finish(llvm::SMLoc)
/home/nickysn/tralala/llvm/llvm-project/llvm/lib/MC/MCStreamer.cpp:992:1
#17 0x0000000000b25e39 (anonymous namespace)::AsmParser::Run(bool, bool)
/home/nickysn/tralala/llvm/llvm-project/llvm/lib/MC/MCParser/AsmParser.cpp:1003:10
#18 0x0000000000409989 AssembleInput(char const*, llvm::Target const*,
llvm::SourceMgr&, llvm::MCContext&, llvm::MCStreamer&, llvm::MCAsmInfo&,
llvm::MCSubtargetInfo&, llvm::MCInstrInfo&, llvm::MCTargetOptions const&)
/home/nickysn/tralala/llvm/llvm-project/llvm/tools/llvm-mc/llvm-mc.cpp:309:45
#19 0x000000000040b035 main /home/nickysn/tralala/llvm/llvm-
project/llvm/tools/llvm-mc/llvm-mc.cpp:540:24
#20 0x00007f40c8e931e2 __libc_start_main (/lib64/libc.so.6+0x281e2)
#21 0x000000000040906e _start (/home/nickysn/tralala/llvm/llvm-project/build-
lld/bin/llvm-mc+0x40906e)
Quuxplusone commented 3 years ago

Attached system.wat.gz (916071 bytes, application/gzip): The actual file, produced by the FPC compiler, that causes the crash. Gzipped to fit the 1MB bugzilla file attachment limit.

Quuxplusone commented 3 years ago
Hi, thanks for the bug report.
We haven't looked at this in detail yet, but if you're going to be trying out
DWARF I would definitely recommend working from the main branch, as we've put a
lot of work on debugging since the LLVM 11 branch, and are continuing to work
on it actively.
Quuxplusone commented 3 years ago
This turns the crash into an error: https://reviews.llvm.org/D95838

We're not sure what the assembly in question is meant to accomplish, though it
seems to want to put data in a text section, which is not supported in Wasm.
The offending bit of code in your output is:

.section .text.b_DEBUGSTART_$SYSTEM,"",@
.globl  DEBUGSTART_$SYSTEM
    .type   DEBUGSTART_$SYSTEM,@object
DEBUGSTART_$SYSTEM:
# End asmlist al_begin
# Begin asmlist al_procedures

.section .text.n_system_$$_errno2inoutres$word$$word,"",@

Here, DEBUGSTART_$SYSTEM is defined as "object".
Quuxplusone commented 3 years ago
(In reply to Wouter van Oortmerssen from comment #2)
> This turns the crash into an error: https://reviews.llvm.org/D95838
>
> We're not sure what the assembly in question is meant to accomplish, though
> it seems to want to put data in a text section, which is not supported in
> Wasm.
> The offending bit of code in your output is:
>
> .section .text.b_DEBUGSTART_$SYSTEM,"",@
> .globl    DEBUGSTART_$SYSTEM
>   .type   DEBUGSTART_$SYSTEM,@object
> DEBUGSTART_$SYSTEM:
> # End asmlist al_begin
> # Begin asmlist al_procedures
>
> .section .text.n_system_$$_errno2inoutres$word$$word,"",@
>
> Here, DEBUGSTART_$SYSTEM is defined as "object".

Thanks!

The DEBUGSTART_$SYSTEM and DEBUGEND_$SYSTEM symbols are used as labels in order
to define the DW_AT_low_pc and DW_AT_high_pc attributes for the
DW_TAG_compile_unit tag for the system unit. What is the correct way to specify
these attributes with Wasm? Should DEBUGSTART_$SYSTEM and DEBUGEND_$SYSTEM be
defined as zero length functions? Or should we use some totally different
approach for this target?
Quuxplusone commented 3 years ago
I don't think you actually need to define symbols for this purpose.
For example, I tried using clang's assembly output to compile a simple function
with debug info:
__attribute__((noinline)) int inc(int x) {
  return x + 1;
}

int main() {
  return inc(0);
}

The beginning of the assembly output for "inc"

        .hidden inc                             # -- Begin function inc
        .globl  inc
        .type   inc,@function
inc:                                    # @inc
.Lfunc_begin0:           <-- this is just a label, but not a symbol
        .file   1 "/s/emr/emscripten-releases" "fib.c"
        .loc    1 1 0                           # fib.c:1:0
        .functype       inc (i32) -> (i32)
        .local          i32, i32, i32, i32, i32, i32
# %bb.0:  <- code in the first basic block below

And then further down in the beginning of the .debug_info section:
        .section        .debug_info,"",@
.Lcu_begin0:
        .int32  .Ldebug_info_end0-.Ldebug_info_start0 # Length of Unit
.Ldebug_info_start0:
        .int16  4                               # DWARF version number
        .int32  .debug_abbrev0                  # Offset Into Abbrev. Section
        .int8   4                               # Address Size (in bytes)
        .int8   1                               # Abbrev [1] 0xb:0x62 DW_TAG_compile_unit
        .int32  .Linfo_string0                  # DW_AT_producer
        .int16  12                              # DW_AT_language
        .int32  .Linfo_string1                  # DW_AT_name
        .int32  .Lline_table_start0             # DW_AT_stmt_list
        .int32  .Linfo_string2                  # DW_AT_comp_dir
        .int32  0                               # DW_AT_low_pc
        .int32  .Ldebug_ranges0                 # DW_AT_ranges
        .int8   2                               # Abbrev [2] 0x26:0x27 DW_TAG_subprogram
        .int32  .Lfunc_begin0                   # DW_AT_low_pc
        .int32  .Lfunc_end0-.Lfunc_begin0       # DW_AT_high_pc

The last 2 lines refer to labels in the text section.
Quuxplusone commented 3 years ago
Ok, the new error landed: https://reviews.llvm.org/D95838
That should fix this particular bug, but let me know if you run into other
issues.