emscripten-core / emscripten-core/emscripten
[Old EH] JS stack unwinding support corrupts some floats
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
### main.c++
```c++
#include
#include
using namespace std;
typedef unsigned int uint;
uint float_to_uint(float f);
float uint_to_float(uint i);
int main() {
uint x = 4287976789;
float f;
try {
f = uint_to_float(x);
} catch(runtime_error& e) {
printf("Caught runtime error...");
}
uint y = float_to_uint(f);
printf("x: %ud, y: %ud, y - x: %ud\n", x, y, y - x);
if (x != y) {
printf("Oops!\n");
return 1;
}
return 0;
}
```
### helper.c++
```c++
typedef unsigned int uint;
typedef union {
uint i;
float f;
} U;
uint float_to_uint(float f) {
U u;
u.f = f;
return u.i;
}
float uint_to_float(uint i) {
U u;
u.i = i;
return u.f;
}
```
### Running it
Compile and run with
```
em++ main.c++ -fexceptions -c && em++ helper.c++ -fexceptions -c && em++ main.o helper.o -fexceptions -o repro.js && node repro.js
```
it prints:
```
x: 4287976789d, y: 4292171093d, y - x: 4194304d
Oops!
```
On the other hand, if we compile and run with `-fwasm-exceptions`:
```
em++ main.c++ -fwasm-exceptions -c && em++ helper.c++ -fwasm-exceptions -c && em++ main.o helper.o -fwasm-exceptions -o repro.js && node repro.js
```
It prints the expected output:
```
x: 4287976789d, y: 4287976789d, y - x: 0d
```
### Emscripten version
```console
$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.68 (ceee49d2ecdab36a3feb85a684f8e5a453dde910)
clang version 20.0.0git (https:/github.com/llvm/llvm-project 5cc64bf60bc04b9315de3c679eb753de4d554a8a)
Target: wasm32-unknown-emscripten
Thread model: posix
```
Contributor guide
Assessment
This issue has not been assessed yet.