Commit b0c6dd86 authored by Hiroshige Hayashizaki's avatar Hiroshige Hayashizaki Committed by V8 LUCI CQ

Allow compiled module invalidation at WasmStreaming::Finish()

This CL adds `can_use_compiled_module` parameter to
WasmStreaming::Finish() that is used by Chromium
https://chromium-review.googlesource.com/c/chromium/src/+/3282643
to invalidate compiled module bytes after SetCompiledModuleBytes().

Bug: chromium:1260939
Change-Id: Iebf0e8615c27c8622721777c664b06a53fb9ee91
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3297548Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78044}
parent a4626654
......@@ -157,8 +157,12 @@ class V8_EXPORT WasmStreaming final {
* {Finish} should be called after all received bytes where passed to
* {OnBytesReceived} to tell V8 that there will be no more bytes. {Finish}
* does not have to be called after {Abort} has been called already.
* If {can_use_compiled_module} is true and {SetCompiledModuleBytes} was
* previously called, the compiled module bytes can be used.
* If {can_use_compiled_module} is false, the compiled module bytes previously
* set by {SetCompiledModuleBytes} should not be used.
*/
void Finish();
void Finish(bool can_use_compiled_module = true);
/**
* Abort streaming compilation. If {exception} has a value, then the promise
......@@ -173,6 +177,8 @@ class V8_EXPORT WasmStreaming final {
* can be used, false otherwise. The buffer passed via {bytes} and {size}
* is owned by the caller. If {SetCompiledModuleBytes} returns true, the
* buffer must remain valid until either {Finish} or {Abort} completes.
* The compiled module bytes should not be used until {Finish(true)} is
* called, because they can be invalidated later by {Finish(false)}.
*/
bool SetCompiledModuleBytes(const uint8_t* bytes, size_t size);
......
......@@ -10400,7 +10400,7 @@ void WasmStreaming::OnBytesReceived(const uint8_t* bytes, size_t size) {
UNREACHABLE();
}
void WasmStreaming::Finish() { UNREACHABLE(); }
void WasmStreaming::Finish(bool can_use_compiled_module) { UNREACHABLE(); }
void WasmStreaming::Abort(MaybeLocal<Value> exception) { UNREACHABLE(); }
......
......@@ -35,7 +35,7 @@ class V8_EXPORT_PRIVATE AsyncStreamingDecoder : public StreamingDecoder {
// The buffer passed into OnBytesReceived is owned by the caller.
void OnBytesReceived(base::Vector<const uint8_t> bytes) override;
void Finish() override;
void Finish(bool can_use_compiled_module) override;
void Abort() override;
......@@ -258,7 +258,7 @@ size_t AsyncStreamingDecoder::DecodingState::ReadBytes(
return num_bytes;
}
void AsyncStreamingDecoder::Finish() {
void AsyncStreamingDecoder::Finish(bool can_use_compiled_module) {
TRACE_STREAMING("Finish\n");
DCHECK(!stream_finished_);
stream_finished_ = true;
......@@ -268,9 +268,12 @@ void AsyncStreamingDecoder::Finish() {
base::Vector<const uint8_t> wire_bytes =
base::VectorOf(wire_bytes_for_deserializing_);
// Try to deserialize the module from wire bytes and module bytes.
if (processor_->Deserialize(compiled_module_bytes_, wire_bytes)) return;
if (can_use_compiled_module &&
processor_->Deserialize(compiled_module_bytes_, wire_bytes))
return;
// Deserialization failed. Restart decoding using |wire_bytes|.
// Compiled module bytes are invalidated by can_use_compiled_module = false
// or the deserialization failed. Restart decoding using |wire_bytes|.
compiled_module_bytes_ = {};
DCHECK(!deserializing());
OnBytesReceived(wire_bytes);
......
......@@ -78,7 +78,7 @@ class V8_EXPORT_PRIVATE StreamingDecoder {
// The buffer passed into OnBytesReceived is owned by the caller.
virtual void OnBytesReceived(base::Vector<const uint8_t> bytes) = 0;
virtual void Finish() = 0;
virtual void Finish(bool can_use_compiled_module = true) = 0;
virtual void Abort() = 0;
......@@ -96,6 +96,7 @@ class V8_EXPORT_PRIVATE StreamingDecoder {
}
// Passes previously compiled module bytes from the embedder's cache.
// The content shouldn't be used until Finish(true) is called.
bool SetCompiledModuleBytes(
base::Vector<const uint8_t> compiled_module_bytes) {
compiled_module_bytes_ = compiled_module_bytes;
......@@ -124,6 +125,8 @@ class V8_EXPORT_PRIVATE StreamingDecoder {
std::string url_;
ModuleCompiledCallback module_compiled_callback_;
// The content of `compiled_module_bytes_` shouldn't be used until
// Finish(true) is called.
base::Vector<const uint8_t> compiled_module_bytes_;
};
......
......@@ -32,7 +32,7 @@ class V8_EXPORT_PRIVATE SyncStreamingDecoder : public StreamingDecoder {
buffer_size_ += bytes.size();
}
void Finish() override {
void Finish(bool can_use_compiled_module) override {
// We copy all received chunks into one byte buffer.
auto bytes = std::make_unique<uint8_t[]>(buffer_size_);
uint8_t* destination = bytes.get();
......@@ -43,7 +43,7 @@ class V8_EXPORT_PRIVATE SyncStreamingDecoder : public StreamingDecoder {
CHECK_EQ(destination - bytes.get(), buffer_size_);
// Check if we can deserialize the module from cache.
if (deserializing()) {
if (can_use_compiled_module && deserializing()) {
HandleScope scope(isolate_);
SaveAndSwitchContext saved_context(isolate_, *context_);
......
......@@ -63,7 +63,9 @@ class WasmStreaming::WasmStreamingImpl {
void OnBytesReceived(const uint8_t* bytes, size_t size) {
streaming_decoder_->OnBytesReceived(base::VectorOf(bytes, size));
}
void Finish() { streaming_decoder_->Finish(); }
void Finish(bool can_use_compiled_module) {
streaming_decoder_->Finish(can_use_compiled_module);
}
void Abort(MaybeLocal<Value> exception) {
i::HandleScope scope(reinterpret_cast<i::Isolate*>(isolate_));
......@@ -116,9 +118,9 @@ void WasmStreaming::OnBytesReceived(const uint8_t* bytes, size_t size) {
impl_->OnBytesReceived(bytes, size);
}
void WasmStreaming::Finish() {
void WasmStreaming::Finish(bool can_use_compiled_module) {
TRACE_EVENT0("v8.wasm", "wasm.FinishStreaming");
impl_->Finish();
impl_->Finish(can_use_compiled_module);
}
void WasmStreaming::Abort(MaybeLocal<Value> exception) {
......
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