<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/63811>63811</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            LLVM ERROR: malformed uleb128, extends past end (lld, wasm module asm, Windows)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          CAD97
      </td>
    </tr>
</table>

<pre>
    I triggered this from Rust so the minimized repro is in Rust; I don't know how to get this into clang/llvm terms on Windows. Asking rustc to emit `.ll`, `.s`, or `.bc` doesn't hit the error, so this is probably a code generator issue, IIUC. I've also provided the `.ll` and `.s`, but couldn't find the entry points to reproduce from those (the rust distribution exposes a rust-lld but not a rust-llc, as far as I can tell).

<details><summary>Version info</summary>

```text
$ rustc -Vv
rustc 1.73.0-nightly (8ca44ef9c 2023-07-10)
binary: rustc
commit-hash: 8ca44ef9caa4049d584fbbce218c219cdca33a2f
commit-date: 2023-07-10
host: x86_64-pc-windows-msvc
release: 1.73.0-nightly
LLVM version: 16.0.5

$ rust-lld -flavor wasm --version --verbose
LLD 16.0.5
```

-----
</details>

<details><summary>Rust reproduction</summary>

```rust
#![feature(asm_experimental_arch)]
#![no_std]

core::arch::global_asm! {
    r#"
    .section        .text.make_answer,"",@
    .globl make_answer
    .type   make_answer,@function
    make_answer:
 end_function
    "#,
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}
```

Compile with `rustc --edition=2021 src\lib.rs --crate-type=cdylib --target wasm32-unknown-unknown`.

-----
</details>

<details><summary>LLVM-IR</summary>

Add `--emit=llvm-ir` to the `rustc` invocation to generate a `.ll` file. This step does not result in any errors.

```rust
; ModuleID = 'lib.ade653b175f4ee81-cgu.0'
source_filename = "lib.ade653b175f4ee81-cgu.0"
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
target triple = "wasm32-unknown-unknown"

module asm ""
module asm " .section        .text.make_answer,\22\22,@"
module asm "    .globl make_answer"
module asm "    .type   make_answer,@function"
module asm "    make_answer:"
module asm "    end_function"
module asm " "

; Function Attrs: noreturn nounwind
define hidden void @rust_begin_unwind(ptr align 4 %_info) unnamed_addr #0 {
start:
  br label %bb1

bb1:                                              ; preds = %bb1, %start
  br label %bb1
}

attributes #0 = { noreturn nounwind "target-cpu"="generic" }
```

-----
</details>

<details><summary>asm</summary>

Similarly, `--emit=asm` stops compilation before the error occurs.

```asm
        .text
        .file   "lib.ade653b175f4ee81-cgu.0"


        .section        .text.make_answer,"",@
        .globl  make_answer
make_answer:
        end_function



        .tabletype      __indirect_function_table, funcref
        .functype       rust_begin_unwind (i32) -> ()
        .section        .text.rust_begin_unwind,"",@
        .hidden rust_begin_unwind
        .globl  rust_begin_unwind
        .type   rust_begin_unwind,@function
rust_begin_unwind:
        .functype       rust_begin_unwind (i32) -> ()
.LBB0_1:
        loop            
        br      0
.LBB0_2:
        end_loop
        end_function

        .section        .custom_section.target_features,"",@
        .int8   2
        .int8   43
        .int8   15
        .ascii  "mutable-globals"
        .int8   43
        .int8   8
        .ascii  "sign-ext"
        .section        .text.rust_begin_unwind,"",@
```

It looks like perhaps the `.type` didn't make it through translation?

-----
</details>

<details><summary>LLD error</summary>

Unfortunately I wasn't able to find the tool to provide `.ll` directly to on Windows (the rust distribution provides a `rust-lld` but not a `rust-llc`), so reproduction uses the `.o` produced by the previous `rustc` invocation. None of the `-L` arguments provided by rustc are actually needed for this reproduction, so this copy of the error is generated without them.

```text
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: D:\\.rust\\rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\bin\\rust-lld.exe -flavor wasm --rsp-quoting=posix --export=__heap_base --export=__data_end -z stack-size=1048576 --stack-first --allow-undefined --fatal-warnings --no-demangle --no-entry lib.lib.ade653b175f4ee81-cgu.0.rcgu.o -o lib.wasm --gc-sections --no-entry -O0
Exception Code: 0xC000001D
 #0 0x00007ff6afbdf9c6 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x1aff9c6)
 #1 0x00007ff6afb8ef5e (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x1aaef5e)
 #2 0x00007ff6afb8f194 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x1aaf194)
 #3 0x00007ff6afbe6a9a (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x1b06a9a)
 #4 0x00007ff6afbe6944 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x1b06944)
 #5 0x00007ff6b061ae78 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x253ae78)
 #6 0x00007ff6b0615e48 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x2535e48)
 #7 0x00007ff6b0613ff0 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x2533ff0)
 #8 0x00007ff6b0613943 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x2533943)
 #9 0x00007ff6b06135b5 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x25335b5)
#10 0x00007ff6af60051e (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x152051e)
#11 0x00007ff6af602131 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x1522131)
#12 0x00007ff6af5fae5e (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x151ae5e)
#13 0x00007ff6ae3e2792 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x302792)
#14 0x00007ff6ae3d4b98 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x2f4b98)
#15 0x00007ff6ae3d08ea (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x2f08ea)
#16 0x00007ff6ae3cce50 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x2ece50)
#17 0x00007ff6ae0e1cac (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x1cac)
#18 0x00007ff6ae0e1438 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x1438)
#19 0x00007ff6afb7b3b8 (D:\.rust\rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\bin\rust-lld.exe+0x1a9b3b8)
#20 0x00007fff12f626ad (C:\WINDOWS\System32\KERNEL32.DLL+0x126ad)
#21 0x00007fff13f8aa68 (C:\WINDOWS\SYSTEM32\ntdll.dll+0x5aa68)
```

-----
</details>

Rust sets the flag to make `llvm_unreachable` generate `trap`, and the exit code is STATUS_ILLEGAL_INSTRUCTION, so the error probably comes from one of those.

<details><summary>Variations</summary>

Module level asm of

```asm
.section        .text.make_answer,"",@
.globl make_answer
.type make_answer,@function
make_answer:
end_function
```

is causing a crash with STATUS_ILLEGAL_INSTRUCTION, printing LLVM ERROR: malformed uleb128, extends past end;

```asm
.section .text.make_answer,"",@
.globl  make_answer
.type make_answer,@function
make_answer:
    .functype make_answer () -> ()
end_function
```

causes a silent crash due to STATUS_ACCESS_VIOLATION without printing anything; and

```asm
.functype make_answer () -> ()
.section .text.make_answer,"",@
.globl  make_answer
.type make_answer,@function
make_answer:
    .functype make_answer () -> ()
end_function
```

compiles without crashing... sometimes. It seems essentially random whether this crashes or not.

-----
</details>

XY: I'm attempting to play around with defining `externref`-speaking functions directly, without using a post-processing step. I built the GAS syntax by looking at `--emit=asm` and mirroring what was done there; as such there's likely all sorts of "you're holding it wrong" in how I was trying to do things. But LLVM still shouldn't be crashing. There's [other](https://github.com/llvm/llvm-project/commit/ef7ca14) cases of wasm codegen reaching `llvm_unreachable` getting fixed; this is likely another one of those.

I would try to reproduce with trunk llc on godbolt compiler explorer, but if it's possible to get it to do the codegen step, I don't know how.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWklz47iS_jXwJYMKEhS1HHyQJfuFYtxVHeXq7nknBUgmJUyBAAcAbat__USC1EJvtbw-KEZhSySITH7IRC5IQDgntxrxmmU3LFtdidbvjL1eLlbz6VVuyv31GryV2y1aLMHvpIPKmhq-tM6DM-B3CLXUspZ_YwkWG2tAOpA69GDpDayhNJrxqYdv2jzBzjyBN7BF33GT2hsolNBbxu-UeqzBo60dGA1_SV2aJzeChfsm9RZs63xBxFhLD2wSj5Rik5jxZbhx_bWx4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