[wasm] Use common serialization API for all modes.

This funnels all serialization and deserialization calls through the
common interface in the wasm-serialization.h file. All call sites are
now uniform, independent of the --wasm-jit-to-native feature.

R=titzer@chromium.org
BUG=v8:6876

Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng
Change-Id: I105907acfeba4b0e277b2003d099c5db6ab59dd3
Reviewed-on: https://chromium-review.googlesource.com/860042
Reviewed-by: Ben Titzer <titzer@chromium.org>
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50501}
This commit is contained in:
Michael Starzinger 2018-01-11 11:43:34 +01:00 committed by Commit Bot
parent 4d09583ad9
commit 4e116c4bc6
5 changed files with 54 additions and 98 deletions

View File

@ -7544,19 +7544,7 @@ WasmCompiledModule::SerializedModule WasmCompiledModule::Serialize() {
i::Handle<i::WasmModuleObject>::cast(Utils::OpenHandle(this));
i::Handle<i::WasmCompiledModule> compiled_part =
i::handle(i::WasmCompiledModule::cast(obj->compiled_module()));
if (i::FLAG_wasm_jit_to_native) {
i::Isolate* isolate = obj->GetIsolate();
return i::wasm::SerializeNativeModule(isolate, compiled_part);
} else {
std::unique_ptr<i::ScriptData> script_data =
i::WasmCompiledModuleSerializer::SerializeWasmModule(obj->GetIsolate(),
compiled_part);
script_data->ReleaseDataOwnership();
size_t size = static_cast<size_t>(script_data->length());
return {std::unique_ptr<const uint8_t[]>(script_data->data()), size};
}
return i::wasm::SerializeNativeModule(obj->GetIsolate(), compiled_part);
}
MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize(
@ -7564,24 +7552,14 @@ MaybeLocal<WasmCompiledModule> WasmCompiledModule::Deserialize(
const WasmCompiledModule::CallerOwnedBuffer& serialized_module,
const WasmCompiledModule::CallerOwnedBuffer& wire_bytes) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
i::MaybeHandle<i::FixedArray> maybe_compiled_part;
if (i::FLAG_wasm_jit_to_native) {
maybe_compiled_part = i::wasm::DeserializeNativeModule(
i_isolate, {serialized_module.first, serialized_module.second},
{wire_bytes.first, wire_bytes.second});
} else {
int size = static_cast<int>(serialized_module.second);
i::ScriptData sc(serialized_module.first, size);
maybe_compiled_part =
i::WasmCompiledModuleSerializer::DeserializeWasmModule(
i_isolate, &sc, {wire_bytes.first, wire_bytes.second});
}
i::Handle<i::FixedArray> compiled_part;
if (!maybe_compiled_part.ToHandle(&compiled_part)) {
i::MaybeHandle<i::WasmCompiledModule> maybe_compiled_module =
i::wasm::DeserializeNativeModule(
i_isolate, {serialized_module.first, serialized_module.second},
{wire_bytes.first, wire_bytes.second});
i::Handle<i::WasmCompiledModule> compiled_module;
if (!maybe_compiled_module.ToHandle(&compiled_module)) {
return MaybeLocal<WasmCompiledModule>();
}
i::Handle<i::WasmCompiledModule> compiled_module =
handle(i::WasmCompiledModule::cast(*compiled_part));
return Local<WasmCompiledModule>::Cast(
Utils::ToLocal(i::Handle<i::JSObject>::cast(
i::WasmModuleObject::New(i_isolate, compiled_module))));

View File

@ -15,7 +15,6 @@
#include "src/frames-inl.h"
#include "src/isolate-inl.h"
#include "src/runtime-profiler.h"
#include "src/snapshot/code-serializer.h"
#include "src/snapshot/natives.h"
#include "src/trap-handler/trap-handler.h"
#include "src/wasm/memory-tracing.h"
@ -995,24 +994,14 @@ RUNTIME_FUNCTION(Runtime_SerializeWasmModule) {
CONVERT_ARG_HANDLE_CHECKED(WasmModuleObject, module_obj, 0);
Handle<WasmCompiledModule> orig(module_obj->compiled_module());
if (FLAG_wasm_jit_to_native) {
std::pair<std::unique_ptr<byte[]>, size_t> serialized_module =
wasm::SerializeNativeModule(isolate, orig);
int data_size = static_cast<int>(serialized_module.second);
void* buff = isolate->array_buffer_allocator()->Allocate(data_size);
Handle<JSArrayBuffer> ret = isolate->factory()->NewJSArrayBuffer();
JSArrayBuffer::Setup(ret, isolate, false, buff, data_size);
memcpy(buff, serialized_module.first.get(), data_size);
return *ret;
} else {
std::unique_ptr<ScriptData> data =
WasmCompiledModuleSerializer::SerializeWasmModule(isolate, orig);
void* buff = isolate->array_buffer_allocator()->Allocate(data->length());
Handle<JSArrayBuffer> ret = isolate->factory()->NewJSArrayBuffer();
JSArrayBuffer::Setup(ret, isolate, false, buff, data->length());
memcpy(buff, data->data(), data->length());
return *ret;
}
std::pair<std::unique_ptr<const byte[]>, size_t> serialized_module =
wasm::SerializeNativeModule(isolate, orig);
int data_size = static_cast<int>(serialized_module.second);
void* buff = isolate->array_buffer_allocator()->Allocate(data_size);
Handle<JSArrayBuffer> ret = isolate->factory()->NewJSArrayBuffer();
JSArrayBuffer::Setup(ret, isolate, false, buff, data_size);
memcpy(buff, serialized_module.first.get(), data_size);
return *ret;
}
// Take an array buffer and attempt to reconstruct a compiled wasm module.
@ -1026,38 +1015,28 @@ RUNTIME_FUNCTION(Runtime_DeserializeWasmModule) {
Address mem_start = static_cast<Address>(buffer->backing_store());
size_t mem_size = static_cast<size_t>(buffer->byte_length()->Number());
// DeserializeWasmModule will allocate. We assume JSArrayBuffer doesn't
// get relocated.
// Note that {wasm::DeserializeNativeModule} will allocate. We assume the
// JSArrayBuffer doesn't get relocated.
bool already_external = wire_bytes->is_external();
if (!already_external) {
wire_bytes->set_is_external(true);
isolate->heap()->UnregisterArrayBuffer(*wire_bytes);
}
MaybeHandle<FixedArray> maybe_compiled_module;
if (FLAG_wasm_jit_to_native) {
maybe_compiled_module = wasm::DeserializeNativeModule(
isolate, {mem_start, mem_size},
Vector<const uint8_t>(
reinterpret_cast<uint8_t*>(wire_bytes->backing_store()),
static_cast<int>(wire_bytes->byte_length()->Number())));
} else {
ScriptData sc(mem_start, static_cast<int>(mem_size));
maybe_compiled_module = WasmCompiledModuleSerializer::DeserializeWasmModule(
isolate, &sc,
Vector<const uint8_t>(
reinterpret_cast<uint8_t*>(wire_bytes->backing_store()),
static_cast<int>(wire_bytes->byte_length()->Number())));
}
MaybeHandle<WasmCompiledModule> maybe_compiled_module =
wasm::DeserializeNativeModule(
isolate, {mem_start, mem_size},
Vector<const uint8_t>(
reinterpret_cast<uint8_t*>(wire_bytes->backing_store()),
static_cast<int>(wire_bytes->byte_length()->Number())));
if (!already_external) {
wire_bytes->set_is_external(false);
isolate->heap()->RegisterNewArrayBuffer(*wire_bytes);
}
Handle<FixedArray> compiled_module;
Handle<WasmCompiledModule> compiled_module;
if (!maybe_compiled_module.ToHandle(&compiled_module)) {
return isolate->heap()->undefined_value();
}
return *WasmModuleObject::New(
isolate, Handle<WasmCompiledModule>::cast(compiled_module));
return *WasmModuleObject::New(isolate, compiled_module);
}
RUNTIME_FUNCTION(Runtime_ValidateWasmInstancesChain) {

View File

@ -856,19 +856,10 @@ Maybe<bool> ValueSerializer::WriteWasmModule(Handle<WasmModuleObject> object) {
String::WriteToFlat(*wire_bytes, destination, 0, wire_bytes_length);
}
if (FLAG_wasm_jit_to_native) {
std::pair<std::unique_ptr<byte[]>, size_t> serialized_module =
wasm::SerializeNativeModule(isolate_, compiled_part);
WriteVarint<uint32_t>(static_cast<uint32_t>(serialized_module.second));
WriteRawBytes(serialized_module.first.get(), serialized_module.second);
} else {
std::unique_ptr<ScriptData> script_data =
WasmCompiledModuleSerializer::SerializeWasmModule(isolate_,
compiled_part);
int script_data_length = script_data->length();
WriteVarint<uint32_t>(script_data_length);
WriteRawBytes(script_data->data(), script_data_length);
}
std::pair<std::unique_ptr<const byte[]>, size_t> serialized_module =
wasm::SerializeNativeModule(isolate_, compiled_part);
WriteVarint<uint32_t>(static_cast<uint32_t>(serialized_module.second));
WriteRawBytes(serialized_module.first.get(), serialized_module.second);
return ThrowIfOutOfMemory();
}
@ -1715,22 +1706,11 @@ MaybeHandle<JSObject> ValueDeserializer::ReadWasmModule() {
}
// Try to deserialize the compiled module first.
Handle<FixedArray> compiled_part;
Handle<WasmCompiledModule> compiled_module;
MaybeHandle<JSObject> result;
if (FLAG_wasm_jit_to_native) {
if (wasm::DeserializeNativeModule(isolate_, compiled_bytes, wire_bytes)
.ToHandle(&compiled_part)) {
result = WasmModuleObject::New(
isolate_, Handle<WasmCompiledModule>::cast(compiled_part));
}
} else {
ScriptData script_data(compiled_bytes.start(), compiled_bytes.length());
if (WasmCompiledModuleSerializer::DeserializeWasmModule(
isolate_, &script_data, wire_bytes)
.ToHandle(&compiled_part)) {
result = WasmModuleObject::New(
isolate_, Handle<WasmCompiledModule>::cast(compiled_part));
}
if (wasm::DeserializeNativeModule(isolate_, compiled_bytes, wire_bytes)
.ToHandle(&compiled_module)) {
result = WasmModuleObject::New(isolate_, compiled_module);
}
if (result.is_null()) {
wasm::ErrorThrower thrower(isolate_, "ValueDeserializer::ReadWasmModule");

View File

@ -9,6 +9,7 @@
#include "src/external-reference-table.h"
#include "src/objects-inl.h"
#include "src/objects.h"
#include "src/snapshot/code-serializer.h"
#include "src/snapshot/serializer-common.h"
#include "src/version.h"
#include "src/wasm/module-compiler.h"
@ -496,8 +497,16 @@ size_t NativeModuleSerializer::Write(Vector<byte> dest) {
}
// static
std::pair<std::unique_ptr<byte[]>, size_t> SerializeNativeModule(
std::pair<std::unique_ptr<const byte[]>, size_t> SerializeNativeModule(
Isolate* isolate, Handle<WasmCompiledModule> compiled_module) {
if (!FLAG_wasm_jit_to_native) {
std::unique_ptr<ScriptData> script_data =
WasmCompiledModuleSerializer::SerializeWasmModule(isolate,
compiled_module);
script_data->ReleaseDataOwnership();
size_t size = static_cast<size_t>(script_data->length());
return {std::unique_ptr<const byte[]>(script_data->data()), size};
}
NativeModule* native_module = compiled_module->GetNativeModule();
NativeModuleSerializer serializer(isolate, native_module);
size_t version_size = kVersionSize;
@ -682,6 +691,16 @@ Address NativeModuleDeserializer::GetTrampolineOrStubFromTag(uint32_t tag) {
MaybeHandle<WasmCompiledModule> DeserializeNativeModule(
Isolate* isolate, Vector<const byte> data, Vector<const byte> wire_bytes) {
if (!FLAG_wasm_jit_to_native) {
ScriptData script_data(data.start(), data.length());
Handle<FixedArray> compiled_module;
if (!WasmCompiledModuleSerializer::DeserializeWasmModule(
isolate, &script_data, wire_bytes)
.ToHandle(&compiled_module)) {
return {};
}
return Handle<WasmCompiledModule>::cast(compiled_module);
}
if (!IsWasmCodegenAllowed(isolate, isolate->native_context())) {
return {};
}

View File

@ -11,7 +11,7 @@ namespace v8 {
namespace internal {
namespace wasm {
std::pair<std::unique_ptr<byte[]>, size_t> SerializeNativeModule(
std::pair<std::unique_ptr<const byte[]>, size_t> SerializeNativeModule(
Isolate* isolate, Handle<WasmCompiledModule> compiled_module);
MaybeHandle<WasmCompiledModule> DeserializeNativeModule(