Commit 76f37d3b authored by dtc-v8's avatar dtc-v8 Committed by Commit bot

wasm: change the module memory size to be multiples of the page size, 64k.

BUG=

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

Cr-Commit-Position: refs/heads/master@{#34450}
parent c6abc94f
...@@ -45,8 +45,8 @@ class ModuleDecoder : public Decoder { ...@@ -45,8 +45,8 @@ class ModuleDecoder : public Decoder {
pc_ = start_; pc_ = start_;
module->module_start = start_; module->module_start = start_;
module->module_end = limit_; module->module_end = limit_;
module->min_mem_size_log2 = 0; module->min_mem_pages = 0;
module->max_mem_size_log2 = 0; module->max_mem_pages = 0;
module->mem_export = false; module->mem_export = false;
module->mem_external = false; module->mem_external = false;
module->origin = origin_; module->origin = origin_;
...@@ -92,8 +92,9 @@ class ModuleDecoder : public Decoder { ...@@ -92,8 +92,9 @@ class ModuleDecoder : public Decoder {
limit_ = pc_; limit_ = pc_;
break; break;
case kDeclMemory: case kDeclMemory:
module->min_mem_size_log2 = consume_u8("min memory"); int length;
module->max_mem_size_log2 = consume_u8("max memory"); module->min_mem_pages = consume_u32v(&length, "min memory");
module->max_mem_pages = consume_u32v(&length, "max memory");
module->mem_export = consume_u8("export memory") != 0; module->mem_export = consume_u8("export memory") != 0;
break; break;
case kDeclSignatures: { case kDeclSignatures: {
...@@ -464,7 +465,8 @@ class ModuleDecoder : public Decoder { ...@@ -464,7 +465,8 @@ class ModuleDecoder : public Decoder {
// Validate that the segment will fit into the (minimum) memory. // Validate that the segment will fit into the (minimum) memory.
uint32_t memory_limit = uint32_t memory_limit =
1 << (module ? module->min_mem_size_log2 : WasmModule::kMaxMemSize); WasmModule::kPageSize * (module ? module->min_mem_pages
: WasmModule::kMaxMemPages);
if (!IsWithinLimit(memory_limit, segment->dest_addr, if (!IsWithinLimit(memory_limit, segment->dest_addr,
segment->source_size)) { segment->source_size)) {
error(pc_ - sizeof(uint32_t), "segment out of bounds of memory"); error(pc_ - sizeof(uint32_t), "segment out of bounds of memory");
......
...@@ -21,8 +21,8 @@ namespace wasm { ...@@ -21,8 +21,8 @@ namespace wasm {
std::ostream& operator<<(std::ostream& os, const WasmModule& module) { std::ostream& operator<<(std::ostream& os, const WasmModule& module) {
os << "WASM module with "; os << "WASM module with ";
os << (1 << module.min_mem_size_log2) << " min mem"; os << (module.min_mem_pages * module.kPageSize) << " min mem";
os << (1 << module.max_mem_size_log2) << " max mem"; os << (module.max_mem_pages * module.kPageSize) << " max mem";
os << module.functions.size() << " functions"; os << module.functions.size() << " functions";
os << module.functions.size() << " globals"; os << module.functions.size() << " globals";
os << module.functions.size() << " data segments"; os << module.functions.size() << " data segments";
...@@ -167,6 +167,7 @@ size_t AllocateGlobalsOffsets(std::vector<WasmGlobal>& globals) { ...@@ -167,6 +167,7 @@ size_t AllocateGlobalsOffsets(std::vector<WasmGlobal>& globals) {
void LoadDataSegments(WasmModule* module, byte* mem_addr, size_t mem_size) { void LoadDataSegments(WasmModule* module, byte* mem_addr, size_t mem_size) {
for (const WasmDataSegment& segment : module->data_segments) { for (const WasmDataSegment& segment : module->data_segments) {
if (!segment.init) continue; if (!segment.init) continue;
if (!segment.source_size) continue;
CHECK_LT(segment.dest_addr, mem_size); CHECK_LT(segment.dest_addr, mem_size);
CHECK_LE(segment.source_size, mem_size); CHECK_LE(segment.source_size, mem_size);
CHECK_LE(segment.dest_addr + segment.source_size, mem_size); CHECK_LE(segment.dest_addr + segment.source_size, mem_size);
...@@ -192,7 +193,7 @@ Handle<FixedArray> BuildFunctionTable(Isolate* isolate, WasmModule* module) { ...@@ -192,7 +193,7 @@ Handle<FixedArray> BuildFunctionTable(Isolate* isolate, WasmModule* module) {
Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size, Handle<JSArrayBuffer> NewArrayBuffer(Isolate* isolate, size_t size,
byte** backing_store) { byte** backing_store) {
if (size > (1 << WasmModule::kMaxMemSize)) { if (size > (WasmModule::kMaxMemPages * WasmModule::kPageSize)) {
// TODO(titzer): lift restriction on maximum memory allocated here. // TODO(titzer): lift restriction on maximum memory allocated here.
*backing_store = nullptr; *backing_store = nullptr;
return Handle<JSArrayBuffer>::null(); return Handle<JSArrayBuffer>::null();
...@@ -234,12 +235,11 @@ bool AllocateMemory(ErrorThrower* thrower, Isolate* isolate, ...@@ -234,12 +235,11 @@ bool AllocateMemory(ErrorThrower* thrower, Isolate* isolate,
DCHECK(instance->module); DCHECK(instance->module);
DCHECK(instance->mem_buffer.is_null()); DCHECK(instance->mem_buffer.is_null());
if (instance->module->min_mem_size_log2 > WasmModule::kMaxMemSize) { if (instance->module->min_mem_pages > WasmModule::kMaxMemPages) {
thrower->Error("Out of memory: wasm memory too large"); thrower->Error("Out of memory: wasm memory too large");
return false; return false;
} }
instance->mem_size = static_cast<size_t>(1) instance->mem_size = WasmModule::kPageSize * instance->module->min_mem_pages;
<< instance->module->min_mem_size_log2;
instance->mem_buffer = instance->mem_buffer =
NewArrayBuffer(isolate, instance->mem_size, &instance->mem_start); NewArrayBuffer(isolate, instance->mem_size, &instance->mem_start);
if (!instance->mem_start) { if (!instance->mem_start) {
...@@ -271,8 +271,8 @@ WasmModule::WasmModule() ...@@ -271,8 +271,8 @@ WasmModule::WasmModule()
: shared_isolate(nullptr), : shared_isolate(nullptr),
module_start(nullptr), module_start(nullptr),
module_end(nullptr), module_end(nullptr),
min_mem_size_log2(0), min_mem_pages(0),
max_mem_size_log2(0), max_mem_pages(0),
mem_export(false), mem_export(false),
mem_external(false), mem_external(false),
start_function_index(-1), start_function_index(-1),
......
...@@ -103,14 +103,15 @@ enum ModuleOrigin { kWasmOrigin, kAsmJsOrigin }; ...@@ -103,14 +103,15 @@ enum ModuleOrigin { kWasmOrigin, kAsmJsOrigin };
// Static representation of a module. // Static representation of a module.
struct WasmModule { struct WasmModule {
static const uint8_t kMinMemSize = 12; // Minimum memory size = 4kb static const uint32_t kPageSize = 0x10000; // Page size, 64kb.
static const uint8_t kMaxMemSize = 30; // Maximum memory size = 1gb static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb
static const uint32_t kMaxMemPages = 16384; // Maximum memory size = 1gb
Isolate* shared_isolate; // isolate for storing shared code. Isolate* shared_isolate; // isolate for storing shared code.
const byte* module_start; // starting address for the module bytes. const byte* module_start; // starting address for the module bytes.
const byte* module_end; // end address for the module bytes. const byte* module_end; // end address for the module bytes.
uint8_t min_mem_size_log2; // minimum size of the memory (log base 2). uint32_t min_mem_pages; // minimum size of the memory in 64k pages.
uint8_t max_mem_size_log2; // maximum size of the memory (log base 2). uint32_t max_mem_pages; // maximum size of the memory in 64k pages.
bool mem_export; // true if the memory is exported. bool mem_export; // true if the memory is exported.
bool mem_external; // true if the memory is external. bool mem_external; // true if the memory is external.
int start_function_index; // start function, if any. int start_function_index; // start function, if any.
......
...@@ -13,7 +13,7 @@ var module = (function () { ...@@ -13,7 +13,7 @@ var module = (function () {
return _WASMEXP_.instantiateModule(bytesWithHeader( return _WASMEXP_.instantiateModule(bytesWithHeader(
// -- memory // -- memory
kDeclMemory, kDeclMemory,
12, 12, 1, 1, 1, 1,
// -- signatures // -- signatures
kDeclSignatures, 1, kDeclSignatures, 1,
2, kAstI32, kAstI32, kAstI32, // int, int -> int 2, kAstI32, kAstI32, kAstI32, // int, int -> int
...@@ -50,7 +50,7 @@ for (var i = 0; i < 4; i++) { ...@@ -50,7 +50,7 @@ for (var i = 0; i < 4; i++) {
assertEquals(mem, module.memory); assertEquals(mem, module.memory);
} }
assertEquals(4096, module.memory.byteLength); assertEquals(65536, module.memory.byteLength);
// Check the properties of the sub function. // Check the properties of the sub function.
assertEquals("function", typeof module.sub); assertEquals("function", typeof module.sub);
...@@ -67,7 +67,7 @@ var module = (function() { ...@@ -67,7 +67,7 @@ var module = (function() {
return _WASMEXP_.instantiateModule(bytesWithHeader( return _WASMEXP_.instantiateModule(bytesWithHeader(
// -- memory // -- memory
kDeclMemory, kDeclMemory,
12, 12, 1, 1, 1, 1,
// -- signatures // -- signatures
kDeclSignatures, 1, kDeclSignatures, 1,
0, kAstStmt, // signature: void -> void 0, kAstStmt, // signature: void -> void
...@@ -101,7 +101,7 @@ for (var i = 0; i < 4; i++) { ...@@ -101,7 +101,7 @@ for (var i = 0; i < 4; i++) {
assertEquals(mem, module.memory); assertEquals(mem, module.memory);
} }
assertEquals(4096, module.memory.byteLength); assertEquals(65536, module.memory.byteLength);
// Check the properties of the sub function. // Check the properties of the sub function.
assertFalse(module.nop === undefined); assertFalse(module.nop === undefined);
...@@ -118,7 +118,7 @@ assertEquals(undefined, module.nop()); ...@@ -118,7 +118,7 @@ assertEquals(undefined, module.nop());
var data = bytesWithHeader( var data = bytesWithHeader(
// -- memory // -- memory
kDeclMemory, kDeclMemory,
12, 12, 1, 1, 1, 1,
// -- signatures // -- signatures
kDeclSignatures, 1, kDeclSignatures, 1,
2, kAstI32, kAstF64, kAstF64, // (f64,f64)->int 2, kAstI32, kAstF64, kAstF64, // (f64,f64)->int
......
...@@ -13,7 +13,7 @@ function testCallFFI(ffi) { ...@@ -13,7 +13,7 @@ function testCallFFI(ffi) {
var data = bytesWithHeader( var data = bytesWithHeader(
kDeclMemory, kDeclMemory,
12, 12, 1, // memory 1, 1, 1, // memory
// -- signatures // -- signatures
kDeclSignatures, 1, kDeclSignatures, 1,
2, kAstI32, kAstF64, kAstF64, // (f64,f64)->int 2, kAstI32, kAstF64, kAstF64, // (f64,f64)->int
......
...@@ -14,7 +14,7 @@ var kNameOffset = kHeaderSize + 19 + kBodySize + 1; ...@@ -14,7 +14,7 @@ var kNameOffset = kHeaderSize + 19 + kBodySize + 1;
var data = bytesWithHeader( var data = bytesWithHeader(
// -- memory // -- memory
kDeclMemory, kDeclMemory,
10, 10, 1, 1, 1, 1,
// -- signatures // -- signatures
kDeclSignatures, 1, kDeclSignatures, 1,
0, kAstI32, // signature: void -> int 0, kAstI32, // signature: void -> int
...@@ -51,7 +51,7 @@ for (var i = 0; i < 4; i++) { ...@@ -51,7 +51,7 @@ for (var i = 0; i < 4; i++) {
assertEquals(mem, module.memory); assertEquals(mem, module.memory);
} }
assertEquals(1024, module.memory.byteLength); assertEquals(65536, module.memory.byteLength);
// Check the properties of the main function. // Check the properties of the main function.
assertFalse(module.main === undefined); assertFalse(module.main === undefined);
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
load("test/mjsunit/wasm/wasm-constants.js"); load("test/mjsunit/wasm/wasm-constants.js");
var kMemSize = 4096; var kMemSize = 65536;
function genModule(memory) { function genModule(memory) {
var kBodySize = 27; var kBodySize = 27;
...@@ -14,7 +14,7 @@ function genModule(memory) { ...@@ -14,7 +14,7 @@ function genModule(memory) {
var data = bytesWithHeader( var data = bytesWithHeader(
kDeclMemory, kDeclMemory,
12, 12, 1, // memory 1, 1, 1, // memory
// -- signatures // -- signatures
kDeclSignatures, 1, kDeclSignatures, 1,
1, kAstI32, kAstI32, // int->int 1, kAstI32, kAstI32, // int->int
...@@ -137,7 +137,7 @@ function testOOBThrows() { ...@@ -137,7 +137,7 @@ function testOOBThrows() {
var data = bytesWithHeader( var data = bytesWithHeader(
kDeclMemory, kDeclMemory,
12, 12, 1, // memory = 4KB 1, 1, 1, // memory = 64KB
// -- signatures // -- signatures
kDeclSignatures, 1, kDeclSignatures, 1,
2, kAstI32, kAstI32, kAstI32, // int->int 2, kAstI32, kAstI32, kAstI32, // int->int
...@@ -166,13 +166,13 @@ function testOOBThrows() { ...@@ -166,13 +166,13 @@ function testOOBThrows() {
function read() { return module.geti(0, offset); } function read() { return module.geti(0, offset); }
function write() { return module.geti(offset, 0); } function write() { return module.geti(offset, 0); }
for (offset = 0; offset < 4092; offset++) { for (offset = 0; offset < 65533; offset++) {
assertEquals(0, read()); assertEquals(0, read());
assertEquals(0, write()); assertEquals(0, write());
} }
for (offset = 4093; offset < 4124; offset++) { for (offset = 65534; offset < 66536; offset++) {
assertTraps(kTrapMemOutOfBounds, read); assertTraps(kTrapMemOutOfBounds, read);
assertTraps(kTrapMemOutOfBounds, write); assertTraps(kTrapMemOutOfBounds, write);
} }
......
...@@ -20,7 +20,7 @@ function testSelect2(type) { ...@@ -20,7 +20,7 @@ function testSelect2(type) {
var data = bytesWithHeader( var data = bytesWithHeader(
// -- memory // -- memory
kDeclMemory, kDeclMemory,
12, 12, 1, // memory 1, 1, 1, // memory
// -- signatures // -- signatures
kDeclSignatures, 1, kDeclSignatures, 1,
2, type, type, type, // signature: (t,t)->t 2, type, type, type, // signature: (t,t)->t
...@@ -96,7 +96,7 @@ function testSelect10(type) { ...@@ -96,7 +96,7 @@ function testSelect10(type) {
var t = type; var t = type;
var data = bytesWithHeader( var data = bytesWithHeader(
kDeclMemory, kDeclMemory,
12, 12, 1, // memory 1, 1, 1, // memory
// signatures // signatures
kDeclSignatures, 1, kDeclSignatures, 1,
10, t,t,t,t,t,t,t,t,t,t,t, // (tx10)->t 10, t,t,t,t,t,t,t,t,t,t,t, // (tx10)->t
......
...@@ -616,7 +616,7 @@ TEST_F(WasmModuleVerifyTest, TwoDataSegments) { ...@@ -616,7 +616,7 @@ TEST_F(WasmModuleVerifyTest, TwoDataSegments) {
TEST_F(WasmModuleVerifyTest, DataSegmentWithInvalidSource) { TEST_F(WasmModuleVerifyTest, DataSegmentWithInvalidSource) {
const int dest_addr = 0x100; const int dest_addr = 0x100;
const byte mem_size_log2 = 15; const byte mem_pages = 1;
const int kHeaderSize = 8; const int kHeaderSize = 8;
const int kDataSize = 19; const int kDataSize = 19;
const int kTotalSize = kHeaderSize + kDataSize; const int kTotalSize = kHeaderSize + kDataSize;
...@@ -625,8 +625,8 @@ TEST_F(WasmModuleVerifyTest, DataSegmentWithInvalidSource) { ...@@ -625,8 +625,8 @@ TEST_F(WasmModuleVerifyTest, DataSegmentWithInvalidSource) {
for (int source_size = -1; source_size < 5 + kDataSize; source_size += 3) { for (int source_size = -1; source_size < 5 + kDataSize; source_size += 3) {
byte data[] = { byte data[] = {
kDeclMemory, kDeclMemory,
mem_size_log2, mem_pages,
mem_size_log2, mem_pages,
1, 1,
kDeclDataSegments, kDeclDataSegments,
1, 1,
...@@ -653,15 +653,15 @@ TEST_F(WasmModuleVerifyTest, DataSegmentWithInvalidDest) { ...@@ -653,15 +653,15 @@ TEST_F(WasmModuleVerifyTest, DataSegmentWithInvalidDest) {
const int source_size = 3; const int source_size = 3;
const int source_offset = 11; const int source_offset = 11;
for (byte mem_size_log2 = 12; mem_size_log2 < 20; mem_size_log2++) { for (byte mem_pages = 1; mem_pages < 16; mem_pages++) {
int mem_size = 1 << mem_size_log2; int mem_size = mem_pages * 0x10000; // 64k pages.
for (int dest_addr = mem_size - source_size; for (int dest_addr = mem_size - source_size;
dest_addr < mem_size + source_size; dest_addr++) { dest_addr < mem_size + source_size; dest_addr++) {
byte data[] = { byte data[] = {
kDeclMemory, kDeclMemory,
mem_size_log2, mem_pages,
mem_size_log2, mem_pages,
1, 1,
kDeclDataSegments, kDeclDataSegments,
1, 1,
......
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