Commit 68bec6c3 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Fix serialization of lazily compiled modules

R=mstarzinger@chromium.org

Change-Id: If72df4425633cac8e98f0dd6adb80512e226f251
Reviewed-on: https://chromium-review.googlesource.com/1097323
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53677}
parent d775c956
......@@ -268,19 +268,18 @@ NativeModuleSerializer::NativeModuleSerializer(Isolate* isolate,
}
size_t NativeModuleSerializer::MeasureCode(const WasmCode* code) const {
return code->instructions().size() + code->reloc_info().size() +
code->source_positions().size() +
if (code->kind() == WasmCode::kLazyStub) return sizeof(size_t);
DCHECK_EQ(WasmCode::kFunction, code->kind());
return kCodeHeaderSize + code->instructions().size() +
code->reloc_info().size() + code->source_positions().size() +
code->protected_instructions().size() *
sizeof(trap_handler::ProtectedInstructionData);
}
size_t NativeModuleSerializer::Measure() const {
size_t size = kHeaderSize;
uint32_t first_wasm_fn = native_module_->num_imported_functions();
uint32_t total_fns = native_module_->num_functions();
for (uint32_t i = first_wasm_fn; i < total_fns; ++i) {
size += kCodeHeaderSize;
size += MeasureCode(native_module_->code(i));
for (WasmCode* code : native_module_->code_table()) {
size += MeasureCode(code);
}
return size;
}
......@@ -291,6 +290,11 @@ void NativeModuleSerializer::WriteHeader(Writer* writer) {
}
void NativeModuleSerializer::WriteCode(const WasmCode* code, Writer* writer) {
if (code->kind() == WasmCode::kLazyStub) {
writer->Write(size_t{0});
return;
}
DCHECK_EQ(WasmCode::kFunction, code->kind());
// Write the size of the entire code section, followed by the code header.
writer->Write(MeasureCode(code));
writer->Write(code->constant_pool_offset());
......@@ -385,10 +389,7 @@ bool NativeModuleSerializer::Write(Writer* writer) {
WriteHeader(writer);
uint32_t total_fns = native_module_->num_functions();
uint32_t first_wasm_fn = native_module_->num_imported_functions();
for (uint32_t i = first_wasm_fn; i < total_fns; ++i) {
const WasmCode* code = native_module_->code(i);
for (WasmCode* code : native_module_->code_table()) {
WriteCode(code, writer);
}
return true;
......@@ -462,7 +463,7 @@ bool NativeModuleDeserializer::ReadHeader(Reader* reader) {
bool NativeModuleDeserializer::ReadCode(uint32_t fn_index, Reader* reader) {
size_t code_section_size = reader->Read<size_t>();
USE(code_section_size);
if (code_section_size == 0) return true;
size_t constant_pool_offset = reader->Read<size_t>();
size_t safepoint_table_offset = reader->Read<size_t>();
size_t handler_table_offset = reader->Read<size_t>();
......@@ -613,9 +614,12 @@ MaybeHandle<WasmModuleObject> DeserializeNativeModule(
wasm::RuntimeExceptionSupport::kRuntimeExceptionSupport);
Handle<WasmCompiledModule> compiled_module =
WasmCompiledModule::New(isolate, shared->module(), env);
compiled_module->GetNativeModule()->SetSharedModuleData(shared);
NativeModuleDeserializer deserializer(isolate,
compiled_module->GetNativeModule());
NativeModule* native_module = compiled_module->GetNativeModule();
native_module->SetSharedModuleData(shared);
if (FLAG_wasm_lazy_compilation) {
native_module->SetLazyBuiltin(BUILTIN_CODE(isolate, WasmCompileLazy));
}
NativeModuleDeserializer deserializer(isolate, native_module);
Reader reader(data + kVersionSize);
if (!deserializer.Read(&reader)) return {};
......
// Copyright 2018 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: --wasm-lazy-compilation --allow-natives-syntax --expose-gc
load('test/mjsunit/wasm/wasm-constants.js');
load('test/mjsunit/wasm/wasm-module-builder.js');
(function SerializeUncompiledModule() {
print(arguments.callee.name);
const [wire_bytes, i1, buff] = (function GenerateInstance() {
const builder = new WasmModuleBuilder();
// Add 20 functions.
for (let i = 0; i < 20; ++i) {
builder.addFunction('f' + i, kSig_i_i)
.addBody([kExprI32Const, i])
.exportFunc();
}
const wire_bytes = builder.toBuffer();
const module = new WebAssembly.Module(wire_bytes);
const buff = %SerializeWasmModule(module);
return [wire_bytes, new WebAssembly.Instance(module), buff];
})();
gc();
const module = %DeserializeWasmModule(buff, wire_bytes);
const i2 = new WebAssembly.Instance(module);
assertEquals(13, i2.exports.f13());
assertEquals(11, i1.exports.f11());
})();
(function SerializePartlyCompiledModule() {
print(arguments.callee.name);
const [wire_bytes, i1, buff] = (function GenerateInstance() {
const builder = new WasmModuleBuilder();
// Add 20 functions.
for (let i = 0; i < 20; ++i) {
builder.addFunction('f' + i, kSig_i_i)
.addBody([kExprI32Const, i])
.exportFunc();
}
const wire_bytes = builder.toBuffer();
const module = new WebAssembly.Module(wire_bytes);
const buff = %SerializeWasmModule(module);
const i1 = new WebAssembly.Instance(module);
assertEquals(2, i1.exports.f2());
assertEquals(11, i1.exports.f11());
return [wire_bytes, i1, buff];
})();
gc();
const module = %DeserializeWasmModule(buff, wire_bytes);
const i2 = new WebAssembly.Instance(module);
assertEquals(13, i2.exports.f13());
assertEquals(11, i1.exports.f11());
assertEquals(9, i1.exports.f9());
})();
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