LibWasm: Ensure that value signs are preserved when casting

Also makes normal arithmetic operations more spec-compliant by actually
ignoring overflow on them.
This commit is contained in:
Ali Mohammad Pur 2021-06-01 23:13:27 +04:30 committed by Ali Mohammad Pur
parent 02b3238c41
commit b15a5d6ada
2 changed files with 9 additions and 9 deletions

View file

@ -173,10 +173,10 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyModule::wasm_invoke)
auto value = vm.argument(index++).to_double(global_object);
switch (param.kind()) {
case Wasm::ValueType::Kind::I32:
arguments.append(Wasm::Value(static_cast<i32>(value)));
arguments.append(Wasm::Value(param, static_cast<u64>(value)));
break;
case Wasm::ValueType::Kind::I64:
arguments.append(Wasm::Value(static_cast<i64>(value)));
arguments.append(Wasm::Value(param, static_cast<u64>(value)));
break;
case Wasm::ValueType::Kind::F32:
arguments.append(Wasm::Value(static_cast<float>(value)));

View file

@ -700,17 +700,17 @@ void BytecodeInterpreter::interpret(Configuration& configuration, InstructionPoi
case Instructions::i32_popcnt.value():
goto unimplemented;
case Instructions::i32_add.value():
OVF_CHECKED_BINARY_NUMERIC_OPERATION(i32, +, i32);
BINARY_NUMERIC_OPERATION(i32, +, i32);
case Instructions::i32_sub.value():
OVF_CHECKED_BINARY_NUMERIC_OPERATION(i32, -, i32);
BINARY_NUMERIC_OPERATION(i32, -, i32);
case Instructions::i32_mul.value():
OVF_CHECKED_BINARY_NUMERIC_OPERATION(i32, *, i32);
BINARY_NUMERIC_OPERATION(i32, *, i32);
case Instructions::i32_divs.value():
OVF_CHECKED_BINARY_NUMERIC_OPERATION(i32, /, i32, TRAP_IF_NOT(rhs.value() != 0));
BINARY_NUMERIC_OPERATION(i32, /, i32, TRAP_IF_NOT(!(Checked<i32>(lhs.value()) /= rhs.value()).has_overflow()));
case Instructions::i32_divu.value():
OVF_CHECKED_BINARY_NUMERIC_OPERATION(u32, /, i32, TRAP_IF_NOT(rhs.value() != 0));
BINARY_NUMERIC_OPERATION(u32, /, i32, TRAP_IF_NOT(rhs.value() != 0));
case Instructions::i32_rems.value():
BINARY_NUMERIC_OPERATION(i32, %, i32, TRAP_IF_NOT(rhs.value() != 0));
BINARY_NUMERIC_OPERATION(i32, %, i32, TRAP_IF_NOT(!(Checked<i32>(lhs.value()) /= rhs.value()).has_overflow()));
case Instructions::i32_remu.value():
BINARY_NUMERIC_OPERATION(u32, %, i32, TRAP_IF_NOT(rhs.value() != 0));
case Instructions::i32_and.value():
@ -742,7 +742,7 @@ void BytecodeInterpreter::interpret(Configuration& configuration, InstructionPoi
case Instructions::i64_divu.value():
OVF_CHECKED_BINARY_NUMERIC_OPERATION(u64, /, i64, TRAP_IF_NOT(rhs.value() != 0));
case Instructions::i64_rems.value():
BINARY_NUMERIC_OPERATION(i64, %, i64, TRAP_IF_NOT(rhs.value() != 0));
BINARY_NUMERIC_OPERATION(i64, %, i64, TRAP_IF_NOT(!(Checked<i32>(lhs.value()) /= rhs.value()).has_overflow()));
case Instructions::i64_remu.value():
BINARY_NUMERIC_OPERATION(u64, %, i64, TRAP_IF_NOT(rhs.value() != 0));
case Instructions::i64_and.value():