Commit f9ee14e5 authored by bradnelson's avatar bradnelson Committed by Commit bot

Allow looser heap accesses historically emitted by Emscripten.

Older versions of Emscripten appear to emit Asm.js containing:
HEAP8[x] with x in int
As opposed to the spec legal construct:
HEAP8[x>>0] with x in int

As older programs and even benchmarks such as Embenchen
include these constructs, support them for compatibility.

BUG= https://code.google.com/p/v8/issues/detail?id=4203
TEST=test-asm-validator,mjsunit/asm-wasm
R=aseemgarg@chromium.org,titzer@chromium.org
LOG=N

Review URL: https://codereview.chromium.org/1692713006

Cr-Commit-Position: refs/heads/master@{#33964}
parent fb10f8fa
...@@ -764,6 +764,10 @@ void AsmTyper::VisitHeapAccess(Property* expr, bool assigning, ...@@ -764,6 +764,10 @@ void AsmTyper::VisitHeapAccess(Property* expr, bool assigning,
if (literal) { if (literal) {
RECURSE(VisitWithExpectation(literal, cache_.kAsmSigned, RECURSE(VisitWithExpectation(literal, cache_.kAsmSigned,
"array index expected to be integer")); "array index expected to be integer"));
} else {
int expected_shift = ElementShiftSize(type);
if (expected_shift == 0) {
RECURSE(Visit(expr->key()));
} else { } else {
BinaryOperation* bin = expr->key()->AsBinaryOperation(); BinaryOperation* bin = expr->key()->AsBinaryOperation();
if (bin == NULL || bin->op() != Token::SAR) { if (bin == NULL || bin->op() != Token::SAR) {
...@@ -778,11 +782,11 @@ void AsmTyper::VisitHeapAccess(Property* expr, bool assigning, ...@@ -778,11 +782,11 @@ void AsmTyper::VisitHeapAccess(Property* expr, bool assigning,
RECURSE(VisitWithExpectation(bin->right(), cache_.kAsmSigned, RECURSE(VisitWithExpectation(bin->right(), cache_.kAsmSigned,
"array shift expected to be integer")); "array shift expected to be integer"));
int n = static_cast<int>(right->raw_value()->AsNumber()); int n = static_cast<int>(right->raw_value()->AsNumber());
int expected_shift = ElementShiftSize(type);
if (expected_shift < 0 || n != expected_shift) { if (expected_shift < 0 || n != expected_shift) {
FAIL(right, "heap access shift must match element size"); FAIL(right, "heap access shift must match element size");
} }
bin->set_bounds(Bounds(cache_.kAsmSigned)); }
expr->key()->set_bounds(Bounds(cache_.kAsmSigned));
} }
Type* result_type; Type* result_type;
if (type->Is(cache_.kAsmIntArrayElement)) { if (type->Is(cache_.kAsmIntArrayElement)) {
...@@ -900,7 +904,8 @@ void AsmTyper::VisitProperty(Property* expr) { ...@@ -900,7 +904,8 @@ void AsmTyper::VisitProperty(Property* expr) {
// Only recurse at this point so that we avoid needing // Only recurse at this point so that we avoid needing
// stdlib.Math to have a real type. // stdlib.Math to have a real type.
RECURSE(VisitWithExpectation(expr->obj(), Type::Any(), "bad propety object")); RECURSE(
VisitWithExpectation(expr->obj(), Type::Any(), "bad property object"));
// For heap view or function table access. // For heap view or function table access.
if (computed_type_->IsArray()) { if (computed_type_->IsArray()) {
......
...@@ -725,6 +725,14 @@ class AsmWasmBuilderImpl : public AstVisitor { ...@@ -725,6 +725,14 @@ class AsmWasmBuilderImpl : public AstVisitor {
WasmOpcodes::LoadStoreOpcodeOf(mtype, is_set_op_), WasmOpcodes::LoadStoreOpcodeOf(mtype, is_set_op_),
WasmOpcodes::LoadStoreAccessOf(false)); WasmOpcodes::LoadStoreAccessOf(false));
is_set_op_ = false; is_set_op_ = false;
if (size == 1) {
// Allow more general expression in byte arrays than the spec
// strictly permits.
// Early versions of Emscripten emit HEAP8[HEAP32[..]|0] in
// places that strictly should be HEAP8[HEAP32[..]>>0].
RECURSE(Visit(expr->key()));
return;
} else {
Literal* value = expr->key()->AsLiteral(); Literal* value = expr->key()->AsLiteral();
if (value) { if (value) {
DCHECK(value->raw_value()->IsNumber()); DCHECK(value->raw_value()->IsNumber());
...@@ -749,6 +757,7 @@ class AsmWasmBuilderImpl : public AstVisitor { ...@@ -749,6 +757,7 @@ class AsmWasmBuilderImpl : public AstVisitor {
RECURSE(Visit(binop->left())); RECURSE(Visit(binop->left()));
return; return;
} }
}
UNREACHABLE(); UNREACHABLE();
} }
......
...@@ -1326,7 +1326,7 @@ TEST(Load1) { ...@@ -1326,7 +1326,7 @@ TEST(Load1) {
CHECK_EXPR(Property, Bounds(cache.kAsmInt)) { CHECK_EXPR(Property, Bounds(cache.kAsmInt)) {
CHECK_VAR(i8, Bounds(cache.kInt8Array)); CHECK_VAR(i8, Bounds(cache.kInt8Array));
CHECK_EXPR(BinaryOperation, Bounds(cache.kAsmSigned)) { CHECK_EXPR(BinaryOperation, Bounds(cache.kAsmSigned)) {
CHECK_VAR(x, Bounds(cache.kAsmSigned)); CHECK_VAR(x, Bounds(cache.kAsmInt));
CHECK_EXPR(Literal, Bounds(cache.kAsmFixnum)); CHECK_EXPR(Literal, Bounds(cache.kAsmFixnum));
} }
} }
...@@ -1386,7 +1386,7 @@ TEST(Store1) { ...@@ -1386,7 +1386,7 @@ TEST(Store1) {
CHECK_EXPR(Property, Bounds::Unbounded()) { CHECK_EXPR(Property, Bounds::Unbounded()) {
CHECK_VAR(i8, Bounds(cache.kInt8Array)); CHECK_VAR(i8, Bounds(cache.kInt8Array));
CHECK_EXPR(BinaryOperation, Bounds(cache.kAsmSigned)) { CHECK_EXPR(BinaryOperation, Bounds(cache.kAsmSigned)) {
CHECK_VAR(x, Bounds(cache.kAsmSigned)); CHECK_VAR(x, Bounds(cache.kAsmInt));
CHECK_EXPR(Literal, Bounds(cache.kAsmFixnum)); CHECK_EXPR(Literal, Bounds(cache.kAsmFixnum));
} }
} }
...@@ -1750,6 +1750,28 @@ TEST(ForeignFunction) { ...@@ -1750,6 +1750,28 @@ TEST(ForeignFunction) {
CHECK_FUNC_TYPES_END_2() CHECK_FUNC_TYPES_END_2()
} }
TEST(ByteArray) {
// Forbidden by asm.js spec, present in embenchen.
CHECK_FUNC_TYPES_BEGIN(
"function bar() { var x = 0; i8[x] = 2; }\n"
"function foo() { bar(); }") {
CHECK_EXPR(FunctionLiteral, FUNC_V_TYPE) {
CHECK_EXPR(Assignment, Bounds(cache.kAsmInt)) {
CHECK_VAR(x, Bounds(cache.kAsmInt));
CHECK_EXPR(Literal, Bounds(cache.kAsmFixnum));
}
CHECK_EXPR(Assignment, Bounds(cache.kAsmInt)) {
CHECK_EXPR(Property, Bounds::Unbounded()) {
CHECK_VAR(i8, Bounds(cache.kInt8Array));
CHECK_VAR(x, Bounds(cache.kAsmSigned));
}
CHECK_EXPR(Literal, Bounds(cache.kAsmFixnum));
}
}
CHECK_SKIP();
}
CHECK_FUNC_TYPES_END
}
TEST(BadExports) { TEST(BadExports) {
HARNESS_PREAMBLE() HARNESS_PREAMBLE()
...@@ -1768,7 +1790,14 @@ TEST(BadExports) { ...@@ -1768,7 +1790,14 @@ TEST(BadExports) {
TEST(NestedHeapAssignment) { TEST(NestedHeapAssignment) {
CHECK_FUNC_ERROR( CHECK_FUNC_ERROR(
"function bar() { var x = 0; i8[x = 1] = 2; }\n" "function bar() { var x = 0; i16[x = 1] = 2; }\n"
"function foo() { bar(); }",
"asm: line 39: expected >> in heap access\n");
}
TEST(BadOperatorHeapAssignment) {
CHECK_FUNC_ERROR(
"function bar() { var x = 0; i16[x & 1] = 2; }\n"
"function foo() { bar(); }", "function foo() { bar(); }",
"asm: line 39: expected >> in heap access\n"); "asm: line 39: expected >> in heap access\n");
} }
......
...@@ -1170,3 +1170,52 @@ function TestForeignVariables() { ...@@ -1170,3 +1170,52 @@ function TestForeignVariables() {
} }
TestForeignVariables(); TestForeignVariables();
(function() {
function TestByteHeapAccessCompat(stdlib, foreign, buffer) {
"use asm";
var HEAP8 = new stdlib.Uint8Array(buffer);
var HEAP32 = new stdlib.Int32Array(buffer);
function store(i, v) {
i = i | 0;
v = v | 0;
HEAP32[i >> 2] = v;
}
function storeb(i, v) {
i = i | 0;
v = v | 0;
HEAP8[i | 0] = v;
}
function load(i) {
i = i | 0;
return HEAP8[i] | 0;
}
function iload(i) {
i = i | 0;
return HEAP8[HEAP32[i >> 2] | 0] | 0;
}
return {load: load, iload: iload, store: store, storeb: storeb};
}
var m = _WASMEXP_.instantiateModuleFromAsm(
TestByteHeapAccessCompat.toString());
m.store(0, 20);
m.store(4, 21);
m.store(8, 22);
m.storeb(20, 123);
m.storeb(21, 42);
m.storeb(22, 77);
assertEquals(123, m.load(20));
assertEquals(42, m.load(21));
assertEquals(77, m.load(22));
assertEquals(123, m.iload(0));
assertEquals(42, m.iload(4));
assertEquals(77, m.iload(8));
})();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment