Commit ad19b86d authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Store wire bytes in OwnedVector

Another pair of {std::unique_ptr} and {size_t} that can be stored as
one {OwnedVector}, which allows to pass it as one thing.

R=mstarzinger@chromium.org

Bug: v8:7754
Change-Id: Ideac0dbd390ba8147b6620daa86f0d3da6c3b609
Reviewed-on: https://chromium-review.googlesource.com/1118236
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54091}
parent ad57eec5
......@@ -965,10 +965,8 @@ MaybeHandle<WasmModuleObject> CompileToModuleObject(
}
// TODO(wasm): only save the sections necessary to deserialize a
// {WasmModule}. E.g. function bodies could be omitted.
CHECK_GE(kMaxUInt32, wire_bytes.length());
size_t wire_bytes_len = wire_bytes.length();
std::unique_ptr<uint8_t[]> wire_bytes_copy(new uint8_t[wire_bytes_len]);
memcpy(wire_bytes_copy.get(), wire_bytes.start(), wire_bytes_len);
OwnedVector<uint8_t> wire_bytes_copy =
OwnedVector<uint8_t>::Of(wire_bytes.module_bytes());
// Create the module object.
// TODO(clemensh): For the same module (same bytes / same hash), we should
......@@ -991,7 +989,7 @@ MaybeHandle<WasmModuleObject> CompileToModuleObject(
// object.
Handle<WasmModuleObject> module_object = WasmModuleObject::New(
isolate, export_wrappers, std::move(module), env,
std::move(wire_bytes_copy), wire_bytes_len, script, asm_js_offset_table);
std::move(wire_bytes_copy), script, asm_js_offset_table);
CompileNativeModule(isolate, thrower, module_object, wasm_module, &env);
if (thrower->error()) return {};
......@@ -2272,8 +2270,7 @@ class AsyncStreamingProcessor final : public StreamingProcessor {
void OnFinishedChunk() override;
void OnFinishedStream(std::unique_ptr<uint8_t[]> bytes,
size_t length) override;
void OnFinishedStream(OwnedVector<uint8_t> bytes) override;
void OnError(DecodeResult result) override;
......@@ -2505,7 +2502,7 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep {
// Create the module object.
job_->module_object_ = WasmModuleObject::New(
job_->isolate_, export_wrappers, job_->module_, env,
std::move(job_->bytes_copy_), job_->wire_bytes_.length(), script,
{std::move(job_->bytes_copy_), job_->wire_bytes_.length()}, script,
asm_js_offset_table);
job_->native_module_ = job_->module_object_->native_module();
......@@ -2806,13 +2803,11 @@ void AsyncStreamingProcessor::OnFinishedChunk() {
}
// Finish the processing of the stream.
void AsyncStreamingProcessor::OnFinishedStream(std::unique_ptr<uint8_t[]> bytes,
size_t length) {
CHECK_LE(length, kMaxUInt32);
void AsyncStreamingProcessor::OnFinishedStream(OwnedVector<uint8_t> bytes) {
TRACE_STREAMING("Finish stream...\n");
if (job_->native_module_) {
job_->wire_bytes_ = ModuleWireBytes(bytes.get(), bytes.get() + length);
job_->native_module_->set_wire_bytes(std::move(bytes), length);
job_->wire_bytes_ = ModuleWireBytes(bytes.as_vector());
job_->native_module_->set_wire_bytes(std::move(bytes));
}
ModuleResult result = decoder_.FinishDecoding(false);
DCHECK(result.ok());
......
......@@ -64,8 +64,8 @@ void StreamingDecoder::Finish() {
return;
}
std::unique_ptr<uint8_t[]> bytes(new uint8_t[total_size_]);
uint8_t* cursor = bytes.get();
OwnedVector<uint8_t> bytes = OwnedVector<uint8_t>::New(total_size_);
uint8_t* cursor = bytes.start();
{
#define BYTES(x) (x & 0xFF), (x >> 8) & 0xFF, (x >> 16) & 0xFF, (x >> 24) & 0xFF
uint8_t module_header[]{BYTES(kWasmMagic), BYTES(kWasmVersion)};
......@@ -74,11 +74,11 @@ void StreamingDecoder::Finish() {
cursor += arraysize(module_header);
}
for (auto&& buffer : section_buffers_) {
DCHECK_LE(cursor - bytes.get() + buffer->length(), total_size_);
DCHECK_LE(cursor - bytes.start() + buffer->length(), total_size_);
memcpy(cursor, buffer->bytes(), buffer->length());
cursor += buffer->length();
}
processor_->OnFinishedStream(std::move(bytes), total_size_);
processor_->OnFinishedStream(std::move(bytes));
}
void StreamingDecoder::Abort() {
......
......@@ -44,8 +44,7 @@ class V8_EXPORT_PRIVATE StreamingProcessor {
// Report the end of the stream. If the stream was successful, all
// received bytes are passed by parameter. If there has been an error, an
// empty array is passed.
virtual void OnFinishedStream(std::unique_ptr<uint8_t[]> bytes,
size_t length) = 0;
virtual void OnFinishedStream(OwnedVector<uint8_t> bytes) = 0;
// Report an error detected in the StreamingDecoder.
virtual void OnError(DecodeResult result) = 0;
// Report the abortion of the stream.
......
......@@ -317,12 +317,9 @@ class V8_EXPORT_PRIVATE NativeModule final {
bool use_trap_handler() const { return use_trap_handler_; }
void set_lazy_compile_frozen(bool frozen) { lazy_compile_frozen_ = frozen; }
bool lazy_compile_frozen() const { return lazy_compile_frozen_; }
Vector<const byte> wire_bytes() const {
return {wire_bytes_.get(), wire_bytes_len_};
}
void set_wire_bytes(std::unique_ptr<const byte[]> wire_bytes, size_t length) {
Vector<const byte> wire_bytes() const { return wire_bytes_.as_vector(); }
void set_wire_bytes(OwnedVector<const byte> wire_bytes) {
wire_bytes_ = std::move(wire_bytes);
wire_bytes_len_ = length;
}
const WasmModule* module() const;
......@@ -377,8 +374,7 @@ class V8_EXPORT_PRIVATE NativeModule final {
std::unique_ptr<WasmCode* []> code_table_;
size_t wire_bytes_len_;
std::unique_ptr<const byte[]> wire_bytes_;
OwnedVector<const byte> wire_bytes_;
WasmCode* runtime_stub_table_[WasmCode::kRuntimeStubCount] = {nullptr};
......
......@@ -177,8 +177,8 @@ enum DispatchTableElements : int {
Handle<WasmModuleObject> WasmModuleObject::New(
Isolate* isolate, Handle<FixedArray> export_wrappers,
std::shared_ptr<const wasm::WasmModule> shared_module, wasm::ModuleEnv& env,
std::unique_ptr<const uint8_t[]> wire_bytes, size_t wire_bytes_len,
Handle<Script> script, Handle<ByteArray> asm_js_offset_table) {
OwnedVector<const uint8_t> wire_bytes, Handle<Script> script,
Handle<ByteArray> asm_js_offset_table) {
DCHECK_EQ(shared_module.get(), env.module);
// Now create the {WasmModuleObject}.
Handle<JSFunction> module_cons(
......@@ -206,7 +206,7 @@ Handle<WasmModuleObject> WasmModuleObject::New(
isolate, native_memory_estimate,
wasm::NativeModule::kCanAllocateMoreMemory, std::move(shared_module),
env);
native_module->set_wire_bytes(std::move(wire_bytes), wire_bytes_len);
native_module->set_wire_bytes(std::move(wire_bytes));
native_module->SetRuntimeStubs(isolate);
Handle<Managed<wasm::NativeModule>> managed_native_module =
Managed<wasm::NativeModule>::FromUniquePtr(isolate, memory_estimate,
......
......@@ -135,8 +135,8 @@ class WasmModuleObject : public JSObject {
static Handle<WasmModuleObject> New(
Isolate* isolate, Handle<FixedArray> export_wrappers,
std::shared_ptr<const wasm::WasmModule> module, wasm::ModuleEnv& env,
std::unique_ptr<const uint8_t[]> wire_bytes, size_t wire_bytes_len,
Handle<Script> script, Handle<ByteArray> asm_js_offset_table);
OwnedVector<const uint8_t> wire_bytes, Handle<Script> script,
Handle<ByteArray> asm_js_offset_table);
// Set a breakpoint on the given byte position inside the given module.
// This will affect all live and future instances of the module.
......
......@@ -533,14 +533,11 @@ MaybeHandle<WasmModuleObject> DeserializeNativeModule(
wasm::ModuleEnv env(module, use_trap_handler,
wasm::RuntimeExceptionSupport::kRuntimeExceptionSupport);
CHECK_LT(wire_bytes.size(), kMaxUInt32);
size_t wire_size = wire_bytes.size();
std::unique_ptr<uint8_t[]> wire_bytes_copy(new uint8_t[wire_size]);
memcpy(wire_bytes_copy.get(), wire_bytes.start(), wire_size);
OwnedVector<uint8_t> wire_bytes_copy = OwnedVector<uint8_t>::Of(wire_bytes);
Handle<WasmModuleObject> module_object = WasmModuleObject::New(
isolate, export_wrappers, std::move(decode_result.val), env,
std::move(wire_bytes_copy), wire_size, script, Handle<ByteArray>::null());
std::move(wire_bytes_copy), script, Handle<ByteArray>::null());
NativeModule* native_module = module_object->native_module();
if (FLAG_wasm_lazy_compilation) {
......
......@@ -184,10 +184,10 @@ uint32_t TestingModuleBuilder::AddBytes(Vector<const byte> bytes) {
// set", e.g. for function names.
uint32_t bytes_offset = old_size ? old_size : 1;
size_t new_size = bytes_offset + bytes.size();
std::unique_ptr<uint8_t[]> new_bytes(new uint8_t[new_size]);
memcpy(new_bytes.get(), old_bytes.start(), old_size);
memcpy(new_bytes.get() + bytes_offset, bytes.start(), bytes.length());
native_module_->set_wire_bytes(std::move(new_bytes), new_size);
OwnedVector<uint8_t> new_bytes = OwnedVector<uint8_t>::New(new_size);
memcpy(new_bytes.start(), old_bytes.start(), old_size);
memcpy(new_bytes.start() + bytes_offset, bytes.start(), bytes.length());
native_module_->set_wire_bytes(std::move(new_bytes));
return bytes_offset;
}
......@@ -216,8 +216,8 @@ Handle<WasmInstanceObject> TestingModuleBuilder::InitInstanceObject() {
Handle<FixedArray> export_wrappers = isolate_->factory()->NewFixedArray(0);
ModuleEnv env = CreateModuleEnv();
Handle<WasmModuleObject> module_object =
WasmModuleObject::New(isolate_, export_wrappers, test_module_, env,
nullptr, 0, script, Handle<ByteArray>::null());
WasmModuleObject::New(isolate_, export_wrappers, test_module_, env, {},
script, Handle<ByteArray>::null());
// This method is called when we initialize TestEnvironment. We don't
// have a memory yet, so we won't create it here. We'll update the
// interpreter when we get a memory. We do have globals, though.
......
......@@ -57,10 +57,8 @@ class MockStreamingProcessor : public StreamingProcessor {
void OnFinishedChunk() override {}
// Finish the processing of the stream.
void OnFinishedStream(std::unique_ptr<uint8_t[]> bytes,
size_t length) override {
void OnFinishedStream(OwnedVector<uint8_t> bytes) override {
received_bytes_ = std::move(bytes);
length_ = length;
}
// Report an error detected in the StreamingDecoder.
......@@ -71,16 +69,15 @@ class MockStreamingProcessor : public StreamingProcessor {
size_t num_sections() const { return num_sections_; }
size_t num_functions() const { return num_functions_; }
bool ok() const { return ok_; }
Vector<const uint8_t> received_bytes() {
return Vector<const uint8_t>(received_bytes_.get(), length_);
Vector<const uint8_t> received_bytes() const {
return received_bytes_.as_vector();
}
private:
size_t num_sections_ = 0;
size_t num_functions_ = 0;
bool ok_ = true;
std::unique_ptr<uint8_t[]> received_bytes_;
size_t length_;
OwnedVector<uint8_t> received_bytes_;
};
class WasmStreamingDecoderTest : public ::testing::Test {
......
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