llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.92k stars 11.52k forks source link

clang miscompilation when using experimental-mv abi #71124

Open TerrorJack opened 10 months ago

TerrorJack commented 10 months ago

repro.tar.zst uploaded at https://files.catbox.moe/5asjku.zst

The above tarball contains an archive with all combined dependencies, and a CPP-processed C source file to reproduce the bug. The bug happens on both clang-17 and trunk. No wasi-sdk build or wasi-libc sysroot is needed to reproduce and diagnose the bug, vanilla clang with wasm32 target shall do:

clang --target=wasm32 \
   -Oz -mmultivalue -c hgcd2.orig.c -o hgcd2.o -Xclang -target-abi -Xclang experimental-mv

wasm-ld -m wasm32 ghc_5.o repro.a hgcd2.o -o integerPowMod.wasm

wasmtime integerPowMod.wasm

The above example produces invalid code and crashes with:

Error: failed to run main module `integerPowMod.wasm`

Caused by:
    0: failed to invoke command default
    1: error while executing at wasm backtrace:
           0: 0x15b647 - <unknown>!abort
           1: 0x15216d - <unknown>!dlfree
           2: 0x15012d - <unknown>!free
           3: 0x5d161 - <unknown>!__gmp_default_free
           4: 0x5d3f3 - <unknown>!__gmp_tmp_reentrant_free
           5: 0x9e6e3 - <unknown>!__gmpn_gcdext
           6: 0xa1dcd - <unknown>!__gmpz_gcdext
           7: 0xa2bad - <unknown>!__gmpz_invert
           8: 0xa941f - <unknown>!integer_gmp_invert
           9: 0xaa4cb - <unknown>!_blk_c1yM
          10: 0x4ab2f - <unknown>!StgRun
          11: 0x2a60c - <unknown>!scheduleWaitThread
          12: 0xa233 - <unknown>!rts_evalLazyIO
          13: 0xa362 - <unknown>!hs_main
          14: 0x6ec1 - <unknown>!main
          15: 0x17ad12 - <unknown>!__main_void
          16: 0x17ad84 - <unknown>!_start
       note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
    2: wasm trap: wasm `unreachable` instruction executed

However, if either -Oz is changed to -Os (or -O3 and other optimization levels), or if -Xclang -target-abi -Xclang experimental-mv, then it compiles and runs fine, producing the correct result 7690788370462822410877571751743509720259.

llvmbot commented 10 months ago

@llvm/issue-subscribers-backend-webassembly

Author: Cheng Shao (TerrorJack)

`repro.tar.zst` uploaded at https://files.catbox.moe/5asjku.zst The above tarball contains an archive with all combined dependencies, and a CPP-processed C source file to reproduce the bug. The bug happens on both `clang-17` and trunk. No `wasi-sdk` build or `wasi-libc` sysroot is needed to reproduce and diagnose the bug, vanilla `clang` with `wasm32` target shall do: ``` clang --target=wasm32 \ -Oz -mmultivalue -c hgcd2.orig.c -o hgcd2.o -Xclang -target-abi -Xclang experimental-mv wasm-ld -m wasm32 ghc_5.o repro.a hgcd2.o -o integerPowMod.wasm wasmtime integerPowMod.wasm ``` The above example produces invalid code and crashes with: ``` Error: failed to run main module `integerPowMod.wasm` Caused by: 0: failed to invoke command default 1: error while executing at wasm backtrace: 0: 0x15b647 - <unknown>!abort 1: 0x15216d - <unknown>!dlfree 2: 0x15012d - <unknown>!free 3: 0x5d161 - <unknown>!__gmp_default_free 4: 0x5d3f3 - <unknown>!__gmp_tmp_reentrant_free 5: 0x9e6e3 - <unknown>!__gmpn_gcdext 6: 0xa1dcd - <unknown>!__gmpz_gcdext 7: 0xa2bad - <unknown>!__gmpz_invert 8: 0xa941f - <unknown>!integer_gmp_invert 9: 0xaa4cb - <unknown>!_blk_c1yM 10: 0x4ab2f - <unknown>!StgRun 11: 0x2a60c - <unknown>!scheduleWaitThread 12: 0xa233 - <unknown>!rts_evalLazyIO 13: 0xa362 - <unknown>!hs_main 14: 0x6ec1 - <unknown>!main 15: 0x17ad12 - <unknown>!__main_void 16: 0x17ad84 - <unknown>!_start note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information 2: wasm trap: wasm `unreachable` instruction executed ``` However, if either `-Oz` is changed to `-Os` (or `-O3` and other optimization levels), or if `-Xclang -target-abi -Xclang experimental-mv`, then it compiles and runs fine, producing the correct result `7690788370462822410877571751743509720259`.