v8/test/mjsunit/asm/asm-memory.js
Michael Starzinger f6296b344e [asm.js] Fix heap buffer checking during instantiation.
This makes sure that asm.js modules can only be instantiated with a
valid {ArrayBuffer} as the underlying heap buffer for all cases where
accepting anything else would be observably different from JavaScript
proper.

R=clemensh@chromium.org
TEST=mjsunit/asm/asm-memory
BUG=chromium:715505,chromium:715748

Change-Id: I355686200151c5667bf836824de922d657a8d943
Reviewed-on: https://chromium-review.googlesource.com/488521
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#44938}
2017-04-27 14:47:03 +00:00

56 lines
1.8 KiB
JavaScript

// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --allow-natives-syntax
(function TestUnalignedMemory() {
// Test that a buffer whose length is not a multiple of the element size of a
// heap view throws the proper {RangeError} during instantiation.
function Module(stdlib, foreign, heap) {
"use asm";
var a = new stdlib.Int32Array(heap);
function f() {}
return { f:f };
}
assertThrows(() => Module(this, {}, new ArrayBuffer(2)), RangeError);
assertThrows(() => Module(this, {}, new ArrayBuffer(10)), RangeError);
assertDoesNotThrow(() => Module(this, {}, new ArrayBuffer(4)));
assertDoesNotThrow(() => Module(this, {}, new ArrayBuffer(16)));
assertFalse(%IsAsmWasmCode(Module));
})();
(function TestMissingMemory() {
// Test that a buffer is required for instantiation of modules containing any
// heap views. JavaScript needs to create individual buffers for each view.
function Module(stdlib, foreign, heap) {
"use asm";
var a = new stdlib.Int16Array(heap);
var b = new stdlib.Int32Array(heap);
function f() {
a[0] = 0x1234;
return b[0] | 0;
}
return { f:f };
}
var m = Module(this, {}, undefined);
assertFalse(%IsAsmWasmCode(Module));
assertEquals(0, m.f());
})();
(function TestNonBufferMemory() {
// Test that a buffer has to be an instance of {ArrayBuffer} in order to be
// valid. JavaScript will also accept any other array-like object.
function Module(stdlib, foreign, heap) {
"use asm";
var a = new stdlib.Int32Array(heap);
function f() {
return a[0] | 0;
}
return { f:f };
}
var m = Module(this, {}, [ 23, 42 ]);
assertFalse(%IsAsmWasmCode(Module));
assertEquals(23, m.f());
})();