Skip to content

Commit d61c869

Browse files
committed
deps: V8: backport cd10ad7cdbe5
Original commit message: [compiler] reset script details in functions deserialized from code cache During the serialization of the code cache, V8 would wipe out the host-defined options, so after a script id deserialized from the code cache, the host-defined options need to be reset on the script using what's provided by the embedder when doing the deserializing compilation, otherwise the HostImportModuleDynamically callbacks can't get the data it needs to implement dynamic import(). Change-Id: I33cc6a5e43b6469d3527242e083f7ae6d8ed0c6a Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5401780 Reviewed-by: Leszek Swirski <[email protected]> Commit-Queue: Joyee Cheung <[email protected]> Cr-Commit-Position: refs/heads/main@{#93323} Refs: v8/v8@cd10ad7 Co-authored-by: Joyee Cheung <[email protected]>
1 parent c5ee93e commit d61c869

File tree

6 files changed

+247
-26
lines changed

6 files changed

+247
-26
lines changed

deps/v8/src/codegen/compiler.cc

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,10 +1632,8 @@ BackgroundCompileTask::BackgroundCompileTask(
16321632

16331633
BackgroundCompileTask::~BackgroundCompileTask() = default;
16341634

1635-
namespace {
1636-
16371635
void SetScriptFieldsFromDetails(Isolate* isolate, Script script,
1638-
ScriptDetails script_details,
1636+
const ScriptDetails& script_details,
16391637
DisallowGarbageCollection* no_gc) {
16401638
Handle<Object> script_name;
16411639
if (script_details.name_obj.ToHandle(&script_name)) {
@@ -1661,6 +1659,8 @@ void SetScriptFieldsFromDetails(Isolate* isolate, Script script,
16611659
}
16621660
}
16631661

1662+
namespace {
1663+
16641664
#ifdef ENABLE_SLOW_DCHECKS
16651665

16661666
// A class which traverses the object graph for a newly compiled Script and
@@ -2344,10 +2344,10 @@ void BackgroundDeserializeTask::MergeWithExistingScript() {
23442344

23452345
MaybeHandle<SharedFunctionInfo> BackgroundDeserializeTask::Finish(
23462346
Isolate* isolate, Handle<String> source,
2347-
ScriptOriginOptions origin_options) {
2347+
const ScriptDetails& script_details) {
23482348
return CodeSerializer::FinishOffThreadDeserialize(
23492349
isolate, std::move(off_thread_data_), &cached_data_, source,
2350-
origin_options, &background_merge_task_);
2350+
script_details, &background_merge_task_);
23512351
}
23522352

23532353
// ----------------------------------------------------------------------------
@@ -3486,8 +3486,8 @@ MaybeHandle<SharedFunctionInfo> GetSharedFunctionInfoForScriptImpl(
34863486
"V8.CompileDeserialize");
34873487
if (deserialize_task) {
34883488
// If there's a cache consume task, finish it.
3489-
maybe_result = deserialize_task->Finish(isolate, source,
3490-
script_details.origin_options);
3489+
maybe_result =
3490+
deserialize_task->Finish(isolate, source, script_details);
34913491
// It is possible at this point that there is a Script object for this
34923492
// script in the compilation cache (held in the variable maybe_script),
34933493
// which does not match maybe_result->script(). This could happen any of
@@ -3508,8 +3508,7 @@ MaybeHandle<SharedFunctionInfo> GetSharedFunctionInfoForScriptImpl(
35083508
// would be non-trivial.
35093509
} else {
35103510
maybe_result = CodeSerializer::Deserialize(
3511-
isolate, cached_data, source, script_details.origin_options,
3512-
maybe_script);
3511+
isolate, cached_data, source, script_details, maybe_script);
35133512
}
35143513

35153514
bool consuming_code_cache_succeeded = false;
@@ -3676,7 +3675,7 @@ MaybeHandle<JSFunction> Compiler::GetWrappedFunction(
36763675
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"),
36773676
"V8.CompileDeserialize");
36783677
maybe_result = CodeSerializer::Deserialize(isolate, cached_data, source,
3679-
script_details.origin_options);
3678+
script_details);
36803679
bool consuming_code_cache_succeeded = false;
36813680
if (maybe_result.ToHandle(&result)) {
36823681
is_compiled_scope = result->is_compiled_scope(isolate);

deps/v8/src/codegen/compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ class V8_EXPORT_PRIVATE BackgroundDeserializeTask {
644644

645645
MaybeHandle<SharedFunctionInfo> Finish(Isolate* isolate,
646646
Handle<String> source,
647-
ScriptOriginOptions origin_options);
647+
const ScriptDetails& script_details);
648648

649649
bool rejected() const { return cached_data_.rejected(); }
650650

deps/v8/src/codegen/script-details.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ struct ScriptDetails {
3535
const ScriptOriginOptions origin_options;
3636
};
3737

38+
void SetScriptFieldsFromDetails(Isolate* isolate, Script script,
39+
const ScriptDetails& script_details,
40+
DisallowGarbageCollection* no_gc);
3841
} // namespace internal
3942
} // namespace v8
4043

deps/v8/src/snapshot/code-serializer.cc

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,12 @@ class StressOffThreadDeserializeThread final : public base::Thread {
310310
CodeSerializer::StartDeserializeOffThread(&local_isolate, cached_data_);
311311
}
312312

313-
MaybeHandle<SharedFunctionInfo> Finalize(Isolate* isolate,
314-
Handle<String> source,
315-
ScriptOriginOptions origin_options) {
313+
MaybeHandle<SharedFunctionInfo> Finalize(
314+
Isolate* isolate, Handle<String> source,
315+
const ScriptDetails& script_details) {
316316
return CodeSerializer::FinishOffThreadDeserialize(
317317
isolate, std::move(off_thread_data_), cached_data_, source,
318-
origin_options);
318+
script_details);
319319
}
320320

321321
private:
@@ -326,7 +326,8 @@ class StressOffThreadDeserializeThread final : public base::Thread {
326326

327327
void FinalizeDeserialization(Isolate* isolate,
328328
Handle<SharedFunctionInfo> result,
329-
const base::ElapsedTimer& timer) {
329+
const base::ElapsedTimer& timer,
330+
const ScriptDetails& script_details) {
330331
const bool log_code_creation =
331332
isolate->v8_file_logger()->is_listening_to_code_events() ||
332333
isolate->is_profiling() ||
@@ -381,8 +382,14 @@ void FinalizeDeserialization(Isolate* isolate,
381382
}
382383
}
383384

385+
Handle<Script> script(Script::cast(result->script()), isolate);
386+
// Reset the script details, including host-defined options.
387+
{
388+
DisallowGarbageCollection no_gc;
389+
SetScriptFieldsFromDetails(isolate, *script, script_details, &no_gc);
390+
}
391+
384392
if (needs_source_positions) {
385-
Handle<Script> script(Script::cast(result->script()), isolate);
386393
Script::InitLineEnds(isolate, script);
387394
}
388395
}
@@ -406,13 +413,13 @@ void BaselineBatchCompileIfSparkplugCompiled(Isolate* isolate, Script script) {
406413

407414
MaybeHandle<SharedFunctionInfo> CodeSerializer::Deserialize(
408415
Isolate* isolate, AlignedCachedData* cached_data, Handle<String> source,
409-
ScriptOriginOptions origin_options,
416+
const ScriptDetails& script_details,
410417
MaybeHandle<Script> maybe_cached_script) {
411418
if (v8_flags.stress_background_compile) {
412419
StressOffThreadDeserializeThread thread(isolate, cached_data);
413420
CHECK(thread.Start());
414421
thread.Join();
415-
return thread.Finalize(isolate, source, origin_options);
422+
return thread.Finalize(isolate, source, script_details);
416423
// TODO(leszeks): Compare off-thread deserialized data to on-thread.
417424
}
418425

@@ -425,7 +432,8 @@ MaybeHandle<SharedFunctionInfo> CodeSerializer::Deserialize(
425432
SerializedCodeSanityCheckResult sanity_check_result =
426433
SerializedCodeSanityCheckResult::kSuccess;
427434
const SerializedCodeData scd = SerializedCodeData::FromCachedData(
428-
cached_data, SerializedCodeData::SourceHash(source, origin_options),
435+
cached_data,
436+
SerializedCodeData::SourceHash(source, script_details.origin_options),
429437
&sanity_check_result);
430438
if (sanity_check_result != SerializedCodeSanityCheckResult::kSuccess) {
431439
if (v8_flags.profile_deserialization)
@@ -471,7 +479,7 @@ MaybeHandle<SharedFunctionInfo> CodeSerializer::Deserialize(
471479
PrintF("[Deserializing from %d bytes took %0.3f ms]\n", length, ms);
472480
}
473481

474-
FinalizeDeserialization(isolate, result, timer);
482+
FinalizeDeserialization(isolate, result, timer, script_details);
475483

476484
return scope.CloseAndEscape(result);
477485
}
@@ -526,7 +534,7 @@ CodeSerializer::StartDeserializeOffThread(LocalIsolate* local_isolate,
526534
MaybeHandle<SharedFunctionInfo> CodeSerializer::FinishOffThreadDeserialize(
527535
Isolate* isolate, OffThreadDeserializeData&& data,
528536
AlignedCachedData* cached_data, Handle<String> source,
529-
ScriptOriginOptions origin_options,
537+
const ScriptDetails& script_details,
530538
BackgroundMergeTask* background_merge_task) {
531539
base::ElapsedTimer timer;
532540
if (v8_flags.profile_deserialization || v8_flags.log_function_events)
@@ -541,7 +549,8 @@ MaybeHandle<SharedFunctionInfo> CodeSerializer::FinishOffThreadDeserialize(
541549
data.sanity_check_result;
542550
const SerializedCodeData scd =
543551
SerializedCodeData::FromPartiallySanityCheckedCachedData(
544-
cached_data, SerializedCodeData::SourceHash(source, origin_options),
552+
cached_data,
553+
SerializedCodeData::SourceHash(source, script_details.origin_options),
545554
&sanity_check_result);
546555
if (sanity_check_result != SerializedCodeSanityCheckResult::kSuccess) {
547556
// The only case where the deserialization result could exist despite a
@@ -612,7 +621,7 @@ MaybeHandle<SharedFunctionInfo> CodeSerializer::FinishOffThreadDeserialize(
612621
length, ms);
613622
}
614623

615-
FinalizeDeserialization(isolate, result, timer);
624+
FinalizeDeserialization(isolate, result, timer, script_details);
616625

617626
DCHECK(!background_merge_task ||
618627
!background_merge_task->HasPendingForegroundWork());

deps/v8/src/snapshot/code-serializer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define V8_SNAPSHOT_CODE_SERIALIZER_H_
77

88
#include "src/base/macros.h"
9+
#include "src/codegen/script-details.h"
910
#include "src/snapshot/serializer.h"
1011
#include "src/snapshot/snapshot-data.h"
1112

@@ -85,7 +86,7 @@ class CodeSerializer : public Serializer {
8586

8687
V8_WARN_UNUSED_RESULT static MaybeHandle<SharedFunctionInfo> Deserialize(
8788
Isolate* isolate, AlignedCachedData* cached_data, Handle<String> source,
88-
ScriptOriginOptions origin_options,
89+
const ScriptDetails& script_details,
8990
MaybeHandle<Script> maybe_cached_script = {});
9091

9192
V8_WARN_UNUSED_RESULT static OffThreadDeserializeData
@@ -96,7 +97,7 @@ class CodeSerializer : public Serializer {
9697
FinishOffThreadDeserialize(
9798
Isolate* isolate, OffThreadDeserializeData&& data,
9899
AlignedCachedData* cached_data, Handle<String> source,
99-
ScriptOriginOptions origin_options,
100+
const ScriptDetails& script_details,
100101
BackgroundMergeTask* background_merge_task = nullptr);
101102

102103
uint32_t source_hash() const { return source_hash_; }

0 commit comments

Comments
 (0)