Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void generateModuleInterface(String moduleInterfaceName) throws IOExcepti

public void generateMetaWasm(Set<Integer> interpretedFunctions) throws IOException {
byte[] wasmBytes = Files.readAllBytes(config.wasmFile());
var module = Parser.builder().includeSectionId(SectionId.CODE).build().parse(wasmBytes);
var module = Parser.parse(wasmBytes);

var writer = new WasmWriter();
Parser.parseWithoutDecoding(
Expand Down
6 changes: 1 addition & 5 deletions cli/src/main/java/run/endive/experimental/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ public void run() {
var result = export.apply(params);
if (result != null) {
for (var r : result) {
if (result == null) {
Comment thread
Marcono1234 marked this conversation as resolved.
System.out.println(0);
} else {
System.out.println(r); // Check floating point results
}
System.out.println(r); // Check floating point results
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,8 @@ private static void I64_SUB(MStack stack) {
private static void I32_MUL(MStack stack) {
var a = stack.pop();
var b = stack.pop();
stack.push(a * b);
int result = (int) (a * b);
stack.push(result);
}

private static void I64_MUL(MStack stack) {
Expand Down Expand Up @@ -1507,6 +1508,9 @@ private static void I32_SHR_S(MStack stack) {
private static void I32_SHL(MStack stack) {
var c = (int) stack.pop();
var v = (int) stack.pop();
// Intentionally get the result as 32 bit `int` (despite it being converted to `long` when
// pushed to the stack)
//noinspection IntegerMultiplicationImplicitCastToLong
stack.push(v << c);
Comment on lines +1511 to 1514

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IntelliJ was warning that because v and c are int, the result is an int as well and could overflow before it is implicitly converted to long when pushed on the stack.

I assume that is intended here though since this is an operation for i32? Not completely sure though.

}

Expand Down Expand Up @@ -3239,7 +3243,7 @@ private static void TRY_TABLE(

private static void IF(
StackFrame frame, MStack stack, Instance instance, AnnotatedInstruction instruction) {
var predValue = stack.pop();
var predValue = (int) stack.pop();
var paramsSize = numberOfParams(instance, instruction);
var returnsSize = numberOfValuesToReturn(instance, instruction);
frame.pushCtrl(instruction.opcode(), paramsSize, returnsSize, stack.size() - paramsSize);
Expand Down
16 changes: 16 additions & 0 deletions runtime/src/test/java/run/endive/runtime/WasmModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ public void shouldRunABasicAdd() {
assertEquals(11L, result);
}

@Test
public void shouldWrapI32MulOverflow() {
var instance = Instance.builder(loadModule("compiled/i32_mul_overflow.wat.wasm")).build();
var mul = instance.export("mul");
var mulIf = instance.export("mul_if");

// 65536 * 65536 = 2^32, which wraps to 0 as an i32
assertEquals(0L, mul.apply(65536L, 65536L)[0]);
assertEquals(0L, mulIf.apply(65536L, 65536L)[0]);

assertEquals(6L, mul.apply(2L, 3L)[0]);
assertEquals(1L, mulIf.apply(2L, 3L)[0]);
assertEquals(-6L, mul.apply(-2L, 3L)[0]);
assertEquals(1L, mulIf.apply(-2L, 3L)[0]);
}

@Test
public void shouldSupportBrTable() {
var instance = Instance.builder(loadModule("compiled/br_table.wat.wasm")).build();
Expand Down
50 changes: 9 additions & 41 deletions simd/src/main/java/run/endive/simd/SimdInterpreterMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -1369,8 +1369,6 @@ private static byte addSatU(byte a, byte b) {
int result = Byte.toUnsignedInt(a) + Byte.toUnsignedInt(b);
if (result >= 0xFF) {
return (byte) 0xFF;
} else if (result < 0) {
return 0;
Comment thread
Marcono1234 marked this conversation as resolved.
} else {
return (byte) result;
}
Expand Down Expand Up @@ -1736,53 +1734,23 @@ private static void BINOP(
}

private static boolean lt(float a, float b) {
if (Float.isNaN(b)) {
return false;
} else if ((a == 0.0f && b == -0.0f) || (a == -0.0f && b == 0.0f)) {
return false;
} else {
return Float.compare(a, b) < 0;
}
return a < b;
}
Comment thread
Marcono1234 marked this conversation as resolved.

private static boolean le(float a, float b) {
if (Float.isNaN(b)) {
return false;
} else if ((a == 0.0f && b == -0.0f) || (a == -0.0f && b == 0.0f)) {
return true;
} else {
return Float.compare(a, b) <= 0;
}
return a <= b;
}

private static boolean gt(float a, float b) {
if (Float.isNaN(a)) {
return false;
} else if ((a == 0.0f && b == -0.0f) || (a == -0.0f && b == 0.0f)) {
return false;
} else {
return Float.compare(a, b) > 0;
}
return a > b;
}

private static boolean ge(float a, float b) {
if (Float.isNaN(a)) {
return false;
} else if ((a == 0.0f && b == -0.0f) || (a == -0.0f && b == 0.0f)) {
return true;
} else {
return Float.compare(a, b) >= 0;
}
return a >= b;
}

private static boolean equals(float a, float b) {
if (Float.isNaN(a) || Float.isNaN(b)) {
return false;
} else if ((a == 0.0f && b == -0.0f) || (a == -0.0f && b == 0.0f)) {
return true;
} else {
return Float.compare(a, b) == 0;
}
return a == b;
}

private static void F32x4(MStack stack, BiFunction<Float, Float, Long> fn) {
Expand Down Expand Up @@ -1925,7 +1893,7 @@ private static void BITMASK(MStack stack, Function<LongVector, long[]> reduce) {
var result = 0L;
for (int i = 0; i < vals.length; i++) {
if (vals[i] < 0) {
result |= 1 << i;
result |= 1L << i;
Comment thread
Marcono1234 marked this conversation as resolved.
}
}

Expand Down Expand Up @@ -2564,7 +2532,7 @@ private static void I64x2_EXTMUL_LOW_I32x4_S(MStack stack) {

var res =
new long[] {
v1[0] * v2[0], v1[1] * v2[1],
((long) v1[0]) * v2[0], ((long) v1[1]) * v2[1],
Comment thread
Marcono1234 marked this conversation as resolved.
};
var result = Value.i64ToVec(res);
System.arraycopy(result, 0, stack.array(), offset, 2);
Expand All @@ -2587,7 +2555,7 @@ private static void I64x2_EXTMUL_HIGH_I32x4_S(MStack stack) {

var res =
new long[] {
v1[2] * v2[2], v1[3] * v2[3],
((long) v1[2]) * v2[2], ((long) v1[3]) * v2[3],
};
var result = Value.i64ToVec(res);
System.arraycopy(result, 0, stack.array(), offset, 2);
Expand Down Expand Up @@ -3007,7 +2975,7 @@ private static void I8x16_SWIZZLE(MStack stack) {
if (id < 8) {
base = (baseLow >> (id * 8)) & 0xFFL;
} else if (id < 16) {
base = (baseHigh >> (id * 8)) & 0xFFL;
base = (baseHigh >> ((id - 8) * 8)) & 0xFFL;
Comment thread
Marcono1234 marked this conversation as resolved.
} else {
base = 0x00L;
}
Expand Down
Binary file not shown.
13 changes: 13 additions & 0 deletions wasm-corpus/src/main/resources/wat/i32_mul_overflow.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(module
;; mul(a, b) returns a*b wrapped to 32 bits
(func (export "mul") (param $a i32) (param $b i32) (result i32)
(i32.mul (local.get $a) (local.get $b))
)
;; mul_if(a, b) returns 1 if the wrapped a*b is non-zero, 0 otherwise
(func (export "mul_if") (param $a i32) (param $b i32) (result i32)
(if (result i32) (i32.mul (local.get $a) (local.get $b))
(then (i32.const 1))
(else (i32.const 0))
)
)
)
2 changes: 1 addition & 1 deletion wasm/src/main/java/run/endive/wasm/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ private static Instruction parseInstruction(ByteBuffer buffer) {
operands.add(n);
for (var j = 0; j < n; j++) {
var catchOp = readByte(buffer);
operands.add(0L | catchOp);
operands.add((long) catchOp);
Comment thread
Marcono1234 marked this conversation as resolved.
var catchOpcode = CatchOpCode.byOpCode(catchOp);
switch (catchOpcode) {
case CATCH:
Expand Down