[serializer] allow SnapshotCreator to destruct without a blob

Previously SnapshotCreator demanded a blob to be created before
it can be destructed in debug build, this patch removes the
DCHECK so that the embedder can choose not to create the blob
when e.g. the snapshot building isn't successful due to errors.

Change-Id: I72939be1e0d79b257b9761f48a72e45325a1f6d8
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3716682
Reviewed-by: Camillo Bruni <cbruni@chromium.org>
Commit-Queue: Joyee Cheung <joyee@igalia.com>
Cr-Commit-Position: refs/heads/main@{#81644}
This commit is contained in:
Joyee Cheung 2022-07-11 23:11:35 +08:00 committed by V8 LUCI CQ
parent 7683b9cd86
commit f3cad8cec6
2 changed files with 31 additions and 4 deletions

View File

@ -496,7 +496,6 @@ SnapshotCreator::SnapshotCreator(const intptr_t* external_references,
SnapshotCreator::~SnapshotCreator() { SnapshotCreator::~SnapshotCreator() {
SnapshotCreatorData* data = SnapshotCreatorData::cast(data_); SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
DCHECK(data->created_);
Isolate* v8_isolate = data->isolate_; Isolate* v8_isolate = data->isolate_;
v8_isolate->Exit(); v8_isolate->Exit();
v8_isolate->Dispose(); v8_isolate->Dispose();
@ -605,9 +604,12 @@ StartupData SnapshotCreator::CreateBlob(
SnapshotCreatorData* data = SnapshotCreatorData::cast(data_); SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
Isolate* isolate = data->isolate_; Isolate* isolate = data->isolate_;
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
DCHECK(!data->created_); Utils::ApiCheck(!data->created_, "v8::SnapshotCreator::CreateBlob",
DCHECK(!data->default_context_.IsEmpty()); "CreateBlob() cannot be called more than once on the same "
"SnapshotCreator.");
Utils::ApiCheck(
!data->default_context_.IsEmpty(), "v8::SnapshotCreator::CreateBlob",
"CreateBlob() cannot be called before the default context is set.");
const int num_additional_contexts = static_cast<int>(data->contexts_.size()); const int num_additional_contexts = static_cast<int>(data->contexts_.size());
const int num_contexts = num_additional_contexts + 1; // The default context. const int num_contexts = num_additional_contexts + 1; // The default context.

View File

@ -2867,6 +2867,31 @@ TEST(Regress503552) {
delete cache_data; delete cache_data;
} }
UNINITIALIZED_TEST(SnapshotCreatorBlobNotCreated) {
DisableAlwaysOpt();
DisableEmbeddedBlobRefcounting();
{
v8::SnapshotCreator creator;
v8::Isolate* isolate = creator.GetIsolate();
{
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = v8::Context::New(isolate);
v8::Context::Scope context_scope(context);
v8::TryCatch try_catch(isolate);
v8::Local<v8::String> code = v8_str("throw new Error('test');");
CHECK(v8::Script::Compile(context, code)
.ToLocalChecked()
->Run(context)
.IsEmpty());
CHECK(try_catch.HasCaught());
}
// SnapshotCreator should be destroyed just fine even when no
// blob is created.
}
FreeCurrentEmbeddedBlob();
}
UNINITIALIZED_TEST(SnapshotCreatorMultipleContexts) { UNINITIALIZED_TEST(SnapshotCreatorMultipleContexts) {
DisableAlwaysOpt(); DisableAlwaysOpt();
DisableEmbeddedBlobRefcounting(); DisableEmbeddedBlobRefcounting();