leaningtech / cheerp-meta

Cheerp - a C/C++ compiler for Web applications - compiles to WebAssembly and JavaScript
https://labs.leaningtech.com/cheerp
Other
1.02k stars 50 forks source link

A miscompile issue caused by a finite loop #135

Closed zyz9740 closed 1 year ago

zyz9740 commented 1 year ago

finite_loop_miscompile.zip

You can try this one. I don't know why cheerp doesn't print.

carlopi commented 1 year ago

This is a known issue in the fact that there is a mismatch between LLVM's IR and WebAssembly provided opcodes.

For perfomance reasons, the current default is that unsafe conversion are allowed, but might results in WebAssembly runtime traps. Executing this with node, the following error is raised:

RuntimeError: float unrepresentable in integer range
    at wasm://wasm/000149b6:wasm-function[97]:0x3e4f
    at bJ (/home/carlo/work/bug126/10-10-15-59/X/crash-bug-10-11/finite_loop_miscompile/random_cheerp.js:1:1205)
    at /home/carlo/work/bug126/10-10-15-59/X/crash-bug-10-11/finite_loop_miscompile/random_cheerp.js:1:1749

User side the escape hatch is providing the flag -cheerp-avoid-wasm-traps at compile time, that would emulate saturating conversion between floating point and integers (at the cost of slightly more verbose and slower code).

Adding -cheerp-avoid-wasm-traps works as intended in your testcase.

I will review whether better documentation for this option might be required, but for now I will consider this closed.