[wasm] Grow memory should return -1 on failure.

Return -1 instead of out of throwing errors, update tests.

R=titzer@chromium.org, ahaas@chromium.org

Review-Url: https://codereview.chromium.org/2216443002
Cr-Commit-Position: refs/heads/master@{#38350}
This commit is contained in:
gdeepti 2016-08-04 13:37:39 -07:00 committed by Commit bot
parent dfd8db8bec
commit 1269306a3b
5 changed files with 19 additions and 45 deletions

View File

@ -538,7 +538,6 @@ class CallSiteUtils : public AllStatic {
T(WasmTrapFloatUnrepresentable, "integer result unrepresentable") \
T(WasmTrapFuncInvalid, "invalid function") \
T(WasmTrapFuncSigMismatch, "function signature mismatch") \
T(WasmTrapMemAllocationFail, "failed to allocate memory") \
T(WasmTrapInvalidIndex, "invalid index into function table")
class MessageTemplate {

View File

@ -58,15 +58,13 @@ RUNTIME_FUNCTION(Runtime_WasmGrowMemory) {
// TODO(gdeepti): Fix bounds check to take into account size of memtype.
new_size = delta_pages * wasm::WasmModule::kPageSize;
if (delta_pages > wasm::WasmModule::kMaxMemPages) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds));
return *isolate->factory()->NewNumberFromInt(-1);
}
new_mem_start =
static_cast<Address>(isolate->array_buffer_allocator()->Allocate(
static_cast<uint32_t>(new_size)));
if (new_mem_start == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail));
return *isolate->factory()->NewNumberFromInt(-1);
}
#if DEBUG
// Double check the API allocator actually zero-initialized the memory.
@ -86,13 +84,11 @@ RUNTIME_FUNCTION(Runtime_WasmGrowMemory) {
new_size = old_size + delta_pages * wasm::WasmModule::kPageSize;
if (new_size >
wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds));
return *isolate->factory()->NewNumberFromInt(-1);
}
new_mem_start = static_cast<Address>(realloc(old_mem_start, new_size));
if (new_mem_start == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail));
return *isolate->factory()->NewNumberFromInt(-1);
}
old_buffer->set_is_external(true);
isolate->heap()->UnregisterArrayBuffer(*old_buffer);
@ -110,8 +106,8 @@ RUNTIME_FUNCTION(Runtime_WasmGrowMemory) {
CHECK(wasm::UpdateWasmModuleMemory(module_object, old_mem_start,
new_mem_start, old_size, new_size));
return *isolate->factory()->NewNumberFromUint(old_size /
wasm::WasmModule::kPageSize);
return *isolate->factory()->NewNumberFromInt(old_size /
wasm::WasmModule::kPageSize);
}
RUNTIME_FUNCTION(Runtime_JITSingleFunction) {

View File

@ -472,7 +472,6 @@ enum WasmOpcode {
V(TrapFloatUnrepresentable) \
V(TrapFuncInvalid) \
V(TrapFuncSigMismatch) \
V(TrapMemAllocationFail) \
V(TrapInvalidIndex)
enum TrapReason {

View File

@ -34,45 +34,33 @@ function testGrowMemoryReadWrite() {
for(offset = 0; offset <= (kPageSize - 4); offset++) {
poke(20);
assertEquals(peek(), 20);
assertEquals(20, peek());
}
for (offset = kPageSize - 3; offset < kPageSize + 4; offset++) {
assertTraps(kTrapMemOutOfBounds, poke);
assertTraps(kTrapMemOutOfBounds, peek);
}
try {
assertEquals(growMem(3), 1);
} catch (e) {
assertEquals("object", typeof e);
assertEquals(e.message, kTrapMsgs[kTrapMemAllocationFail]);
return;
}
assertEquals(1, growMem(3));
for (offset = kPageSize; offset <= 4*kPageSize -4; offset++) {
poke(20);
assertEquals(peek(), 20);
assertEquals(20, peek());
}
for (offset = 4*kPageSize - 3; offset < 4*kPageSize + 4; offset++) {
assertTraps(kTrapMemOutOfBounds, poke);
assertTraps(kTrapMemOutOfBounds, peek);
}
try {
assertEquals(growMem(15), 4);
} catch (e) {
assertEquals("object", typeof e);
assertEquals(e.message, kTrapMsgs[kTrapMemAllocationFail]);
return;
}
assertEquals(4, growMem(15));
for (offset = 4*kPageSize - 3; offset <= 4*kPageSize + 4; offset++) {
poke(20);
assertEquals(peek(), 20);
assertEquals(20, peek());
}
for (offset = 19*kPageSize - 10; offset <= 19*kPageSize - 4; offset++) {
poke(20);
assertEquals(peek(), 20);
assertEquals(20, peek());
}
for (offset = 19*kPageSize - 3; offset < 19*kPageSize + 5; offset++) {
assertTraps(kTrapMemOutOfBounds, poke);
@ -93,17 +81,11 @@ function testGrowMemoryZeroInitialSize() {
assertTraps(kTrapMemOutOfBounds, peek);
assertTraps(kTrapMemOutOfBounds, poke);
try {
assertEquals(growMem(1), 0);
} catch (e) {
assertEquals("object", typeof e);
assertEquals(e.message, kTrapMsgs[kTrapMemAllocationFail]);
return;
}
assertEquals(0, growMem(1));
for(offset = 0; offset <= kPageSize - 4; offset++) {
poke(20);
assertEquals(peek(), 20);
assertEquals(20, peek());
}
//TODO(gdeepti): Fix tests with correct write boundaries
@ -119,8 +101,8 @@ function testGrowMemoryTrapMaxPagesZeroInitialMemory() {
var builder = genGrowMemoryBuilder();
var module = builder.instantiate();
var maxPages = 16385;
function growMem() { return module.exports.grow_memory(maxPages); }
assertTraps(kTrapMemOutOfBounds, growMem);
function growMem(pages) { return module.exports.grow_memory(pages); }
assertEquals(-1, growMem(maxPages));
}
testGrowMemoryTrapMaxPagesZeroInitialMemory();
@ -130,8 +112,8 @@ function testGrowMemoryTrapMaxPages() {
builder.addMemory(1, 1, false);
var module = builder.instantiate();
var maxPages = 16384;
function growMem() { return module.exports.grow_memory(maxPages); }
assertTraps(kTrapMemOutOfBounds, growMem);
function growMem(pages) { return module.exports.grow_memory(pages); }
assertEquals(-1, growMem(maxPages));
}
testGrowMemoryTrapMaxPages();

View File

@ -321,8 +321,7 @@ var kTrapRemByZero = 4;
var kTrapFloatUnrepresentable = 5;
var kTrapFuncInvalid = 6;
var kTrapFuncSigMismatch = 7;
var kTrapMemAllocationFail = 8;
var kTrapInvalidIndex = 9;
var kTrapInvalidIndex = 8;
var kTrapMsgs = [
"unreachable",
@ -333,7 +332,6 @@ var kTrapMsgs = [
"integer result unrepresentable",
"invalid function",
"function signature mismatch",
"failed to allocate memory",
"invalid index into function table"
];