Skip to content

Wasm2JS signed/unsigned miscompilation around right-shift when optimizations are enabled #10173

Closed
WebAssembly/binaryen
#2581
@juj

Description

@juj

a.cpp

#include <emscripten/emscripten.h>
#include <stdint.h>

int main()
{
	int index = EM_ASM_INT(return 0);
	uint32_t val = EM_ASM_INT(return 0xDFFFFFFE);
	uint32_t val2 = EM_ASM_INT(return 0xDFFFFFFE);
	EM_ASM(console.log($0), val == val2 >> index);
}

compiled with em++ a.cpp -o a.js -O1 -s WASM=0 incorrectly prints out 0, whereas it should print 1.

Generated main is

 function main($0, $1) {
  $0 = $0 | 0;
  $1 = $1 | 0;
  $0 = global$0 - 16 | 0;
  global$0 = $0;
  $1 = emscripten_asm_const_iii(1024, 1033, 0) | 0;
  HEAP32[$0 >> 2] = (emscripten_asm_const_iii(1034, 1033, 0) | 0) == emscripten_asm_const_iii(1034, 1033, 0) >>> $1;
  emscripten_asm_const_iii(1052, 1068, $0 | 0) | 0;
  global$0 = $0 + 16 | 0;
  return 0;
 }

where (emscripten_asm_const_iii(1034, 1033, 0) | 0) yields -536870914, whereas the right side emscripten_asm_const_iii(1034, 1033, 0) >>> $1 is unsigned and evaluates to 3758096382.

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