Commit 1269306a authored by gdeepti's avatar gdeepti Committed by Commit bot

[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}
parent dfd8db8b
...@@ -538,7 +538,6 @@ class CallSiteUtils : public AllStatic { ...@@ -538,7 +538,6 @@ class CallSiteUtils : public AllStatic {
T(WasmTrapFloatUnrepresentable, "integer result unrepresentable") \ T(WasmTrapFloatUnrepresentable, "integer result unrepresentable") \
T(WasmTrapFuncInvalid, "invalid function") \ T(WasmTrapFuncInvalid, "invalid function") \
T(WasmTrapFuncSigMismatch, "function signature mismatch") \ T(WasmTrapFuncSigMismatch, "function signature mismatch") \
T(WasmTrapMemAllocationFail, "failed to allocate memory") \
T(WasmTrapInvalidIndex, "invalid index into function table") T(WasmTrapInvalidIndex, "invalid index into function table")
class MessageTemplate { class MessageTemplate {
......
...@@ -58,15 +58,13 @@ RUNTIME_FUNCTION(Runtime_WasmGrowMemory) { ...@@ -58,15 +58,13 @@ RUNTIME_FUNCTION(Runtime_WasmGrowMemory) {
// TODO(gdeepti): Fix bounds check to take into account size of memtype. // TODO(gdeepti): Fix bounds check to take into account size of memtype.
new_size = delta_pages * wasm::WasmModule::kPageSize; new_size = delta_pages * wasm::WasmModule::kPageSize;
if (delta_pages > wasm::WasmModule::kMaxMemPages) { if (delta_pages > wasm::WasmModule::kMaxMemPages) {
THROW_NEW_ERROR_RETURN_FAILURE( return *isolate->factory()->NewNumberFromInt(-1);
isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds));
} }
new_mem_start = new_mem_start =
static_cast<Address>(isolate->array_buffer_allocator()->Allocate( static_cast<Address>(isolate->array_buffer_allocator()->Allocate(
static_cast<uint32_t>(new_size))); static_cast<uint32_t>(new_size)));
if (new_mem_start == NULL) { if (new_mem_start == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE( return *isolate->factory()->NewNumberFromInt(-1);
isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail));
} }
#if DEBUG #if DEBUG
// Double check the API allocator actually zero-initialized the memory. // Double check the API allocator actually zero-initialized the memory.
...@@ -86,13 +84,11 @@ RUNTIME_FUNCTION(Runtime_WasmGrowMemory) { ...@@ -86,13 +84,11 @@ RUNTIME_FUNCTION(Runtime_WasmGrowMemory) {
new_size = old_size + delta_pages * wasm::WasmModule::kPageSize; new_size = old_size + delta_pages * wasm::WasmModule::kPageSize;
if (new_size > if (new_size >
wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) { wasm::WasmModule::kMaxMemPages * wasm::WasmModule::kPageSize) {
THROW_NEW_ERROR_RETURN_FAILURE( return *isolate->factory()->NewNumberFromInt(-1);
isolate, NewRangeError(MessageTemplate::kWasmTrapMemOutOfBounds));
} }
new_mem_start = static_cast<Address>(realloc(old_mem_start, new_size)); new_mem_start = static_cast<Address>(realloc(old_mem_start, new_size));
if (new_mem_start == NULL) { if (new_mem_start == NULL) {
THROW_NEW_ERROR_RETURN_FAILURE( return *isolate->factory()->NewNumberFromInt(-1);
isolate, NewRangeError(MessageTemplate::kWasmTrapMemAllocationFail));
} }
old_buffer->set_is_external(true); old_buffer->set_is_external(true);
isolate->heap()->UnregisterArrayBuffer(*old_buffer); isolate->heap()->UnregisterArrayBuffer(*old_buffer);
...@@ -110,8 +106,8 @@ RUNTIME_FUNCTION(Runtime_WasmGrowMemory) { ...@@ -110,8 +106,8 @@ RUNTIME_FUNCTION(Runtime_WasmGrowMemory) {
CHECK(wasm::UpdateWasmModuleMemory(module_object, old_mem_start, CHECK(wasm::UpdateWasmModuleMemory(module_object, old_mem_start,
new_mem_start, old_size, new_size)); new_mem_start, old_size, new_size));
return *isolate->factory()->NewNumberFromUint(old_size / return *isolate->factory()->NewNumberFromInt(old_size /
wasm::WasmModule::kPageSize); wasm::WasmModule::kPageSize);
} }
RUNTIME_FUNCTION(Runtime_JITSingleFunction) { RUNTIME_FUNCTION(Runtime_JITSingleFunction) {
......
...@@ -472,7 +472,6 @@ enum WasmOpcode { ...@@ -472,7 +472,6 @@ enum WasmOpcode {
V(TrapFloatUnrepresentable) \ V(TrapFloatUnrepresentable) \
V(TrapFuncInvalid) \ V(TrapFuncInvalid) \
V(TrapFuncSigMismatch) \ V(TrapFuncSigMismatch) \
V(TrapMemAllocationFail) \
V(TrapInvalidIndex) V(TrapInvalidIndex)
enum TrapReason { enum TrapReason {
......
...@@ -34,45 +34,33 @@ function testGrowMemoryReadWrite() { ...@@ -34,45 +34,33 @@ function testGrowMemoryReadWrite() {
for(offset = 0; offset <= (kPageSize - 4); offset++) { for(offset = 0; offset <= (kPageSize - 4); offset++) {
poke(20); poke(20);
assertEquals(peek(), 20); assertEquals(20, peek());
} }
for (offset = kPageSize - 3; offset < kPageSize + 4; offset++) { for (offset = kPageSize - 3; offset < kPageSize + 4; offset++) {
assertTraps(kTrapMemOutOfBounds, poke); assertTraps(kTrapMemOutOfBounds, poke);
assertTraps(kTrapMemOutOfBounds, peek); assertTraps(kTrapMemOutOfBounds, peek);
} }
try { assertEquals(1, growMem(3));
assertEquals(growMem(3), 1);
} catch (e) {
assertEquals("object", typeof e);
assertEquals(e.message, kTrapMsgs[kTrapMemAllocationFail]);
return;
}
for (offset = kPageSize; offset <= 4*kPageSize -4; offset++) { for (offset = kPageSize; offset <= 4*kPageSize -4; offset++) {
poke(20); poke(20);
assertEquals(peek(), 20); assertEquals(20, peek());
} }
for (offset = 4*kPageSize - 3; offset < 4*kPageSize + 4; offset++) { for (offset = 4*kPageSize - 3; offset < 4*kPageSize + 4; offset++) {
assertTraps(kTrapMemOutOfBounds, poke); assertTraps(kTrapMemOutOfBounds, poke);
assertTraps(kTrapMemOutOfBounds, peek); assertTraps(kTrapMemOutOfBounds, peek);
} }
try { assertEquals(4, growMem(15));
assertEquals(growMem(15), 4);
} catch (e) {
assertEquals("object", typeof e);
assertEquals(e.message, kTrapMsgs[kTrapMemAllocationFail]);
return;
}
for (offset = 4*kPageSize - 3; offset <= 4*kPageSize + 4; offset++) { for (offset = 4*kPageSize - 3; offset <= 4*kPageSize + 4; offset++) {
poke(20); poke(20);
assertEquals(peek(), 20); assertEquals(20, peek());
} }
for (offset = 19*kPageSize - 10; offset <= 19*kPageSize - 4; offset++) { for (offset = 19*kPageSize - 10; offset <= 19*kPageSize - 4; offset++) {
poke(20); poke(20);
assertEquals(peek(), 20); assertEquals(20, peek());
} }
for (offset = 19*kPageSize - 3; offset < 19*kPageSize + 5; offset++) { for (offset = 19*kPageSize - 3; offset < 19*kPageSize + 5; offset++) {
assertTraps(kTrapMemOutOfBounds, poke); assertTraps(kTrapMemOutOfBounds, poke);
...@@ -93,17 +81,11 @@ function testGrowMemoryZeroInitialSize() { ...@@ -93,17 +81,11 @@ function testGrowMemoryZeroInitialSize() {
assertTraps(kTrapMemOutOfBounds, peek); assertTraps(kTrapMemOutOfBounds, peek);
assertTraps(kTrapMemOutOfBounds, poke); assertTraps(kTrapMemOutOfBounds, poke);
try { assertEquals(0, growMem(1));
assertEquals(growMem(1), 0);
} catch (e) {
assertEquals("object", typeof e);
assertEquals(e.message, kTrapMsgs[kTrapMemAllocationFail]);
return;
}
for(offset = 0; offset <= kPageSize - 4; offset++) { for(offset = 0; offset <= kPageSize - 4; offset++) {
poke(20); poke(20);
assertEquals(peek(), 20); assertEquals(20, peek());
} }
//TODO(gdeepti): Fix tests with correct write boundaries //TODO(gdeepti): Fix tests with correct write boundaries
...@@ -119,8 +101,8 @@ function testGrowMemoryTrapMaxPagesZeroInitialMemory() { ...@@ -119,8 +101,8 @@ function testGrowMemoryTrapMaxPagesZeroInitialMemory() {
var builder = genGrowMemoryBuilder(); var builder = genGrowMemoryBuilder();
var module = builder.instantiate(); var module = builder.instantiate();
var maxPages = 16385; var maxPages = 16385;
function growMem() { return module.exports.grow_memory(maxPages); } function growMem(pages) { return module.exports.grow_memory(pages); }
assertTraps(kTrapMemOutOfBounds, growMem); assertEquals(-1, growMem(maxPages));
} }
testGrowMemoryTrapMaxPagesZeroInitialMemory(); testGrowMemoryTrapMaxPagesZeroInitialMemory();
...@@ -130,8 +112,8 @@ function testGrowMemoryTrapMaxPages() { ...@@ -130,8 +112,8 @@ function testGrowMemoryTrapMaxPages() {
builder.addMemory(1, 1, false); builder.addMemory(1, 1, false);
var module = builder.instantiate(); var module = builder.instantiate();
var maxPages = 16384; var maxPages = 16384;
function growMem() { return module.exports.grow_memory(maxPages); } function growMem(pages) { return module.exports.grow_memory(pages); }
assertTraps(kTrapMemOutOfBounds, growMem); assertEquals(-1, growMem(maxPages));
} }
testGrowMemoryTrapMaxPages(); testGrowMemoryTrapMaxPages();
...@@ -321,8 +321,7 @@ var kTrapRemByZero = 4; ...@@ -321,8 +321,7 @@ var kTrapRemByZero = 4;
var kTrapFloatUnrepresentable = 5; var kTrapFloatUnrepresentable = 5;
var kTrapFuncInvalid = 6; var kTrapFuncInvalid = 6;
var kTrapFuncSigMismatch = 7; var kTrapFuncSigMismatch = 7;
var kTrapMemAllocationFail = 8; var kTrapInvalidIndex = 8;
var kTrapInvalidIndex = 9;
var kTrapMsgs = [ var kTrapMsgs = [
"unreachable", "unreachable",
...@@ -333,7 +332,6 @@ var kTrapMsgs = [ ...@@ -333,7 +332,6 @@ var kTrapMsgs = [
"integer result unrepresentable", "integer result unrepresentable",
"invalid function", "invalid function",
"function signature mismatch", "function signature mismatch",
"failed to allocate memory",
"invalid index into function table" "invalid index into function table"
]; ];
......
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