Skip to content

Wrong conversion from double to int64 with SAFE_HEAP flag #1110

Closed
@metalpavel

Description

@metalpavel

Hi,
I create example for this issue:

#include <iostream>
#include <cmath>

int64_t __attribute__ ((noinline)) toSInteger64(double x) {
  return (x > (double)std::numeric_limits<int64_t>::min() &&
          x < (double)std::numeric_limits<int64_t>::max())
             ? (int64_t)x
             : (std::signbit(x) ? std::numeric_limits<int64_t>::min()
                                : std::numeric_limits<int64_t>::max());
}

int main(int argc, char ** argv) {
    double a = -123.0 * double(argc + 1);
    int64_t b = toSInteger64(a);
    std::cout << "result: " << b << std::endl;
    return 0;
}

Compile command:
em++ hello.cpp -O3 -s WASM=1 --emit-symbol-map --bind -s TOTAL_MEMORY=67108864 -s ALLOW_MEMORY_GROWTH=1 --memory-init-file 1 -g -s SAFE_HEAP=1 -s BINARYEN=1 -o hello.html

generate function:
(func $__Z12toSInteger64d (param $0 f64) (result i32)
and it crashes at 32.trunc_u/f64

without SAFE_HEAP it generates correct code:
(func $__Z12toSInteger64d (param $0 f64) (result i64)
with correct conversion i64.trunc_s/f64

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