emscripten-core / emscripten-core/emscripten

Assertion failed: (isa<X>(Val) && "cast<Ty>() argument of incompatible type!")

Open
#8,731 34 comments 0 reactions 0 assignees View on GitHub
wontfix
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

**UPDATE: I'd closed this due to not being able to reproduce it in modern Emscripten, but it seems to be arising for someone else when Link-Time-Optimization is enabled has been found ([see below](https://github.com/emscripten-core/emscripten/issues/8731#issuecomment-618982673)), so I've reopened it.**

I apologize for not having a small self-contained repro case for this, but I can't seem to get it to happen when the code is isolated out of its larger context. So I'm hoping that the errors might point to how it might be narrowed down, or whether it's an LLVM bug or what.

I basically have code that looks like this:

unsigned int addr;
if (local_eval)
addr = MAIN_THREAD_EM_ASM_INT(
{ return reb.Box(eval(UTF8ToString($0))) }, // direct call
utf8
);
else
addr = MAIN_THREAD_EM_ASM_INT(
{ return reb.Box((1,eval)(UTF8ToString($0))) }, // indirect call
utf8
);
REBVAL *result = (REBVAL*)addr;
return result;

*(For what this pertains to, see the article ["Global Eval: What are the Options"](http://perfectionkills.com/global-eval-what-are-the-options/))*

At the link step this produces errors (which I have attached at the bottom). However, there are many changes I can make which don't preserve my intent...but do make the errors go away:

1. Comment out the second branch
2. Change the second branch to match the code of the first, so just **eval** and not **(1,eval)**
3. Change from MAIN_THREAD_EM_ASM_INT to plain EM_ASM_INT, or just use plain MAIN_THREAD_EM_ASM

*(Note on 2: I saw that sometimes there are problems with commas and you have to wrap code in parentheses, but that doesn't seem to help here (also the `(1, eval)` pattern works elsewhere).)*

None of those changes serve my purpose (I just mention them as data points). However it works if I rewrite the code not to share the result variable of the evaluation directly...giving each branch its own unsigned int:

if (local_eval) {
unsigned int addr = MAIN_THREAD_EM_ASM_INT(
{ return reb.Box(eval(UTF8ToString($0))) }, // direct call
utf8
);
result = (REBVAL*)addr;
}
else {
unsigned int addr = MAIN_THREAD_EM_ASM_INT(
{ return reb.Box((1,eval)(UTF8ToString($0))) }, // indirect call
utf8
);
result = (REBVAL*)addr;
}
return result;

So it seems to be a fairly peculiar intersection of circumstances. Taking basically the exact code and sticking it in a standalone `int main()` doesn't reproduce the problem with an attempt at the same command-line options.

I'm using emcc 1.38.28 on Kubuntu Linux, and the command line options look like:

> emcc -shared -o libr3.js -Os -s ENVIRONMENT='web,worker' -s ASSERTIONS=1 --closure 0 --minify 0 --post-js prep/include/reb-lib.js -s DISABLE_EXCEPTION_CATCHING=1 -s DEMANGLE_SUPPORT=0 -s EXPORTED_FUNCTIONS=@prep/include/libr3.exports.json -s "EXTRA_EXPORTED_RUNTIME_METHODS=['allocateUTF8']" -s WASM=1 -s SAFE_HEAP=0 -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=1

The errors from the top example come back looking like:

```
#0 0x000000000130c4ba llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0x130c4ba)
#1 0x000000000130a59e llvm::sys::RunSignalHandlers() (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0x130a59e)
#2 0x000000000130a6ec SignalHandler(int) (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0x130a6ec)
#3 0x00007fec44509150 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x13150)
#4 0x00007fec4379e024 (/lib/x86_64-linux-gnu/libc.so.6+0x17f024)
#5 0x00000000008131a7 void std::__cxx11::basic_string, std::allocator >::_M_construct(char*, char*, std::forward_iterator_tag) [clone .isra.709] (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0x8131a7)
#6 0x0000000000850b60 (anonymous namespace)::JSWriter::handleAsmConst(llvm::Instruction const*, (anonymous namespace)::JSWriter::EmAsmCallType) [clone .constprop.1144] (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0x850b60)
#7 0x0000000000851897 (anonymous namespace)::JSWriter::CH_emscripten_asm_const_int_sync_on_main_thread(llvm::Instruction const*, std::__cxx11::basic_string, std::allocator >, int) (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0x851897)
#8 0x0000000000853e10 (anonymous namespace)::JSWriter::generateExpression(llvm::User const*, llvm::raw_string_ostream&) [clone .constprop.1159] (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0x853e10)
#9 0x0000000000875abf (anonymous namespace)::JSWriter::printFunctionBody(llvm::Function const*) [clone .constprop.1118] (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0x875abf)
#10 0x00000000008791a9 (anonymous namespace)::JSWriter::printModuleBody() [clone .constprop.1110] (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0x8791a9)
#11 0x000000000087cb99 (anonymous namespace)::JSWriter::runOnModule(llvm::Module&) (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0x87cb99)
#12 0x0000000000df7224 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0xdf7224)
#13 0x000000000060a0f7 compileModule(char**, llvm::LLVMContext&) [clone .constprop.394] (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0x60a0f7)
#14 0x00000000005c2d3d main (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0x5c2d3d)
#15 0x00007fec436401c1 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x211c1)
#16 0x00000000005fee79 _start (/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc+0x5fee79)
Stack dump:
0. Program arguments: /home/hostilefork/emsdk/clang/e1.38.28_64bit/llc /tmp/emscripten_temp_3OLAkN/libr3.bc -march=js -filetype=asm -o /tmp/tmpZRAqgX.4.js -emscripten-stack-size=5242880 -O2 -emscripten-precise-f32 -emscripten-enable-pthreads -emscripten-assertions=1 -emscripten-global-base=1024 -emscripten-no-exit-runtime -emscripten-wasm -emscripten-only-wasm
1. Running pass 'JavaScript backend' on module '/tmp/emscripten_temp_3OLAkN/libr3.bc'.
Traceback (most recent call last):
File "/home/hostilefork/emsdk/emscripten/1.38.28/emcc.py", line 3220, in
sys.exit(run(sys.argv))
File "/home/hostilefork/emsdk/emscripten/1.38.28/emcc.py", line 1882, in run
final = shared.Building.emscripten(final, target + '.mem', js_libraries)
File "/home/hostilefork/emsdk/emscripten/1.38.28/tools/shared.py", line 2315, in emscripten
emscripten.run(infile, outfile, memfile, js_libraries)
File "/home/hostilefork/emsdk/emscripten/1.38.28/emscripten.py", line 2499, in run
return temp_files.run_and_clean(lambda: emscripter(
File "/home/hostilefork/emsdk/emscripten/1.38.28/tools/tempfiles.py", line 105, in run_and_clean
return func()
File "/home/hostilefork/emsdk/emscripten/1.38.28/emscripten.py", line 2500, in
infile, outfile_obj, memfile, libraries, shared.COMPILER_ENGINE, temp_files, shared.DEBUG)
File "/home/hostilefork/emsdk/emscripten/1.38.28/emscripten.py", line 89, in emscript
backend_output = compile_js(infile, temp_files, DEBUG)
File "/home/hostilefork/emsdk/emscripten/1.38.28/emscripten.py", line 118, in compile_js
jsrun.timeout_run(subprocess.Popen(backend_args, stdout=subprocess.PIPE, universal_newlines=True), note_args=backend_args)
File "/home/hostilefork/emsdk/emscripten/1.38.28/tools/jsrun.py", line 27, in timeout_run
raise Exception('Subprocess "' + ' '.join(note_args) + '" failed with exit code ' + str(proc.returncode) + '!')
Exception: Subprocess "/home/hostilefork/emsdk/clang/e1.38.28_64bit/llc /tmp/emscripten_temp_3OLAkN/libr3.bc -march=js -filetype=asm -o /tmp/tmpZRAqgX.4.js -emscripten-stack-size=5242880 -O2 -emscripten-precise-f32 -emscripten-enable-pthreads -emscripten-assertions=1 -emscripten-global-base=1024 -emscripten-no-exit-runtime -emscripten-wasm -emscripten-only-wasm" failed with exit code -11!
makefile:223: recipe for target 'libr3.js' failed
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.