Skip to content

Instantly share code, notes, and snippets.

@kkebo
Last active December 21, 2019 23:29
Show Gist options
  • Save kkebo/ffed5c2b6a9892e48f2cdb69de1c2d5a to your computer and use it in GitHub Desktop.
Save kkebo/ffed5c2b6a9892e48f2cdb69de1c2d5a to your computer and use it in GitHub Desktop.
Workarounds for crash of clang on a-Shell

The issue

When I compiled a C source code directly into a Wasm binary using clang, a-Shell crashed in a destructor of PrettyStackTraceEntry.

Specifically, the following line's assert fails after it is called some times.

https://github.com/holzschu/llvm/blob/aa7e203b38f8d2416747c28d6bd51724c0eecef6/lib/Support/PrettyStackTrace.cpp#L210

Steps to reproduce

$ cat > test.c
int add(int a, int b) {
    return a + b;
}
$ unsetenv CCC_OVERRIDE_OPTIONS
$ clang --target=wasm32 -nostdlib -Os -s -Wl,--no-entry -Wl,--export-all -o test.wasm test.c

Then, a-Shell will crash.

Workarounds

  1. Build LLVM with -DLLVM_ENABLE_BACKTRACES=OFF

    diff --git a/bootstrap.sh b/bootstrap.sh
    index 6a6d451c109..182e242bc86 100755
    --- a/bootstrap.sh
    +++ b/bootstrap.sh
    @@ -102,6 +102,7 @@ cmake -G Ninja \
     -DLLVM_ENABLE_FFI=ON \
     -DLLVM_ENABLE_THREADS=OFF \
     -DLLVM_ENABLE_TERMINFO=OFF \
    +-DLLVM_ENABLE_BACKTRACES=OFF \
     -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF \
     -DFFI_LIBRARY_PATH=${FFI_SRCDIR}/build/Debug-iphoneos/libffi.a \
     -DFFI_INCLUDE_DIR=${FFI_SRCDIR}/build_iphoneos-arm64/include \
  2. Or, remove that lines and build LLVM

    diff --git a/lib/Support/PrettyStackTrace.cpp b/lib/Support/PrettyStackTrace.cpp
    index bfb238cc853..d26f0a67c14 100644
    --- a/lib/Support/PrettyStackTrace.cpp
    +++ b/lib/Support/PrettyStackTrace.cpp
    @@ -206,6 +206,7 @@ PrettyStackTraceEntry::PrettyStackTraceEntry() {
     }
    
     PrettyStackTraceEntry::~PrettyStackTraceEntry() {
    +#if 0
     #if ENABLE_BACKTRACES
       assert(PrettyStackTraceHead == this &&
              "Pretty stack trace entry destruction is out of order");
    @@ -213,6 +214,7 @@ PrettyStackTraceEntry::~PrettyStackTraceEntry() {
       // Handle SIGINFO first, because we already started destructing.
       printForSigInfoIfNeeded();
     #endif
    +#endif
     }
    
     void PrettyStackTraceString::print(raw_ostream &OS) const { OS << Str << "\n"; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment