Skip to content

[Old EH] JS stack unwinding support corrupts some floats #22743

Open
@hoodmane

Description

@hoodmane

main.c++

#include <stdexcept>
#include <stdio.h>
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++

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

$ 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions