Commit 89f839e5 authored by Michael Starzinger's avatar Michael Starzinger Committed by Commit Bot

[asm.js] Correctly set minimum memory size to zero.

This makes sure the minimum memory size for WebAssembly modules derived
from asm.js is set to zero. It allows instatiation without allocating an
underlying memory, when such memory is unused. It also fixes a bug in
patching of embedded memory sizes for asm.js modules.

R=ahaas@chromium.org
TEST=mjsunit/regress/regress-crbug-759327
BUG=chromium:759327

Change-Id: If5a965b96a03cbb5ba15bc41fbaf359f74961f41
Reviewed-on: https://chromium-review.googlesource.com/637912
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#47646}
parent 53990037
......@@ -88,6 +88,7 @@ AsmJsParser::AsmJsParser(Zone* zone, uintptr_t stack_limit,
call_coercion_deferred_(nullptr),
pending_label_(0),
global_imports_(zone) {
module_builder_->SetMinMemorySize(0);
InitializeStdlibTypes();
}
......
......@@ -1111,6 +1111,9 @@ void SharedFunctionInfo::SharedFunctionInfoPrint(std::ostream& os) { // NOLINT
if (HasBytecodeArray()) {
os << "\n - bytecode_array = " << bytecode_array();
}
if (HasAsmWasmData()) {
os << "\n - asm_wasm_data = " << Brief(asm_wasm_data());
}
PrintSourceCode(os);
// Script files are often large, hard to read.
// os << "\n - script =";
......
......@@ -24,6 +24,7 @@ const uint32_t kWasmVersion = 0x01;
const uint8_t kWasmFunctionTypeForm = 0x60;
const uint8_t kWasmAnyFunctionTypeForm = 0x70;
const uint8_t kResizableMaximumFlag = 1;
const uint8_t kNoMaximumFlag = 0;
enum SectionCode : int8_t {
kUnknownSectionCode = 0, // code for unknown sections
......
......@@ -223,7 +223,8 @@ WasmModuleBuilder::WasmModuleBuilder(Zone* zone)
indirect_functions_(zone),
globals_(zone),
signature_map_(zone),
start_function_index_(-1) {}
start_function_index_(-1),
min_memory_size_(16) {}
WasmFunctionBuilder* WasmModuleBuilder::AddFunction(FunctionSig* sig) {
functions_.push_back(new (zone_) WasmFunctionBuilder(this));
......@@ -313,6 +314,10 @@ uint32_t WasmModuleBuilder::AddGlobal(ValueType type, bool exported,
return static_cast<uint32_t>(globals_.size() - 1);
}
void WasmModuleBuilder::SetMinMemorySize(uint32_t value) {
min_memory_size_ = value;
}
void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const {
// == Emit magic =============================================================
buffer.write_u32(kWasmMagic);
......@@ -383,10 +388,9 @@ void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const {
// == emit memory declaration ================================================
{
size_t start = EmitSection(kMemorySectionCode, buffer);
buffer.write_u8(1); // memory count
buffer.write_u32v(kResizableMaximumFlag);
buffer.write_u32v(16); // min memory size
buffer.write_u32v(32); // max memory size
buffer.write_u8(1); // memory count
buffer.write_u8(kNoMaximumFlag); // no max provided
buffer.write_u32v(min_memory_size_); // min memory size
FixupSection(buffer, start);
}
......
......@@ -234,6 +234,7 @@ class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
void SetIndirectFunction(uint32_t indirect, uint32_t direct);
void MarkStartFunction(WasmFunctionBuilder* builder);
void AddExport(Vector<const char> name, WasmFunctionBuilder* builder);
void SetMinMemorySize(uint32_t value);
// Writing methods.
void WriteTo(ZoneBuffer& buffer) const;
......@@ -290,6 +291,7 @@ class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
ZoneVector<WasmGlobal> globals_;
SignatureMap signature_map_;
int start_function_index_;
uint32_t min_memory_size_;
};
inline FunctionSig* WasmFunctionBuilder::signature() {
......
// 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 --expose-gc
function Module(stdlib, env, heap) {
"use asm";
var MEM = new stdlib.Int32Array(heap);
function f() {
MEM[0] = 0;
}
return { f: f };
}
function instantiate() {
var buffer = new ArrayBuffer(0);
Module(this, {}, buffer).f();
try {} finally {}
gc();
Module(this, {}, buffer).f();
}
instantiate();
assertTrue(%IsAsmWasmCode(Module));
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