Skip to content

Mitigate crash on arrays of void or v128 #2232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2022
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
28 changes: 24 additions & 4 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
getConstValueI64High,
getConstValueF32,
getConstValueF64,
getConstValueV128,
getBlockChildCount,
getBlockChildAt,
getBlockName,
Expand Down Expand Up @@ -193,6 +194,7 @@ import {
writeI64,
writeF32,
writeF64,
writeV128,
uniqueMap,
isPowerOf2,
v128_zero,
Expand Down Expand Up @@ -1929,6 +1931,20 @@ export class Compiler extends DiagnosticEmitter {
}
break;
}
case <u32>TypeRef.V128: {
for (let i = 0; i < length; ++i) {
let value = values[i];
assert(getExpressionType(value) == elementTypeRef);
assert(getExpressionId(value) == ExpressionId.Const);
writeV128(getConstValueV128(value), buf, pos);
pos += 16;
}
break;
}
case <u32>TypeRef.None: {
// nothing to write
break;
}
default: assert(false);
}
return pos;
Expand Down Expand Up @@ -8269,11 +8285,15 @@ export class Compiler extends DiagnosticEmitter {
let elementExpression = expressions[i];
if (elementExpression.kind != NodeKind.OMITTED) {
let expr = this.compileExpression(<Expression>elementExpression, elementType, Constraints.CONV_IMPLICIT);
let precomp = module.runExpression(expr, ExpressionRunnerFlags.PreserveSideeffects);
if (precomp) {
expr = precomp;
} else {
if (getExpressionType(expr) != elementType.toRef()) {
isStatic = false;
} else {
let precomp = module.runExpression(expr, ExpressionRunnerFlags.PreserveSideeffects);
if (precomp) {
expr = precomp;
} else {
isStatic = false;
}
}
values[i] = expr;
} else {
Expand Down
11 changes: 11 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,17 @@ export function getConstValueF64(expr: ExpressionRef): f64 {
return binaryen._BinaryenConstGetValueF64(expr);
}

export function getConstValueV128(expr: ExpressionRef): Uint8Array {
let cArr = binaryen._malloc(16);
binaryen._BinaryenConstGetValueV128(expr, cArr);
let out = new Uint8Array(16);
for (let i = 0; i < 16; ++i) {
out[i] = binaryen.__i32_load8_u(cArr + i);
}
binaryen._free(cArr);
return out;
}

export function isConstZero(expr: ExpressionRef): bool {
if (getExpressionId(expr) != ExpressionId.Const) return false;
var type = getExpressionType(expr);
Expand Down
11 changes: 11 additions & 0 deletions src/util/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,14 @@ export function writeF64(value: f64, buffer: Uint8Array, offset: i32): void {
writeI32(i64_low(valueI64), buffer, offset);
writeI32(i64_high(valueI64), buffer, offset + 4);
}

/** Reads a 128-bit vector from the specified buffer. */
export function readV128(buffer: Uint8Array, offset: i32): Uint8Array {
return buffer.slice(offset, offset + 16);
}

/** Writes a 128-bit vector to the specified buffer. */
export function writeV128(value: Uint8Array, buffer: Uint8Array, offset: i32): void {
assert(value.length == 16);
buffer.set(value, offset);
}