Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2439c41

Browse files
mkustermanncommit-bot@chromium.org
authored andcommitted
[vm/concurrency] Make main Kernel loading independent of current isolate
Since loading kernel will affect all isolates within a group, there should be nothing isolate-specific inside the kernel loader. To ensure we don't have (or introduce) any accidental uses, we add a NoActiveIsolateScope to the main parts of kernel loading. Only loading of native extensions - done by the kernel loader - currently requires an active isolate. The reason for this is that loading native extensions happen by calling out to embedder, which calls back into the VM using the our embedding API (which currently requires an active isolate) Issue dart-lang/sdk#36097 TEST=Refactoring of existing implementation. Change-Id: I96e64dbfe7148b76b8fa006fe5dbde8c1f904504 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/184269 Commit-Queue: Martin Kustermann <[email protected]> Reviewed-by: Alexander Aprelev <[email protected]>
1 parent 51950ab commit 2439c41

File tree

5 files changed

+33
-19
lines changed

5 files changed

+33
-19
lines changed

runtime/observatory/tests/service/get_vm_timeline_rpc_test.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ void allEventsHaveIsolateNumber(List events) {
111111
Map arguments = event['args'];
112112
expect(arguments, isA<Map>());
113113
expect(arguments['isolateGroupId'], isA<String>());
114-
if (!const ['GC', 'Compiler', 'CompilerVerbose'].contains(event['cat'])) {
114+
if (!const ['GC', 'Compiler', 'CompilerVerbose'].contains(event['cat']) &&
115+
!const ['FinishTopLevelClassLoading', 'FinishClassLoading']
116+
.contains(event['name'])) {
115117
expect(arguments['isolateId'], isA<String>());
116118
}
117119
}

runtime/observatory_2/tests/service_2/get_vm_timeline_rpc_test.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ void allEventsHaveIsolateNumber(List events) {
111111
Map arguments = event['args'];
112112
expect(arguments, isA<Map>());
113113
expect(arguments['isolateGroupId'], isA<String>());
114-
if (!const ['GC', 'Compiler', 'CompilerVerbose'].contains(event['cat'])) {
114+
if (!const ['GC', 'Compiler', 'CompilerVerbose'].contains(event['cat']) &&
115+
!const ['FinishTopLevelClassLoading', 'FinishClassLoading']
116+
.contains(event['name'])) {
115117
expect(arguments['isolateId'], isA<String>());
116118
}
117119
}

runtime/vm/isolate.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ typedef FixedCache<intptr_t, CatchEntryMovesRefPtr, 16> CatchEntryMovesCache;
187187
V(NONPRODUCT, asserts, EnableAsserts, enable_asserts, FLAG_enable_asserts) \
188188
V(NONPRODUCT, use_field_guards, UseFieldGuards, use_field_guards, \
189189
FLAG_use_field_guards) \
190+
V(PRODUCT, should_load_vmservice_library, ShouldLoadVmService, \
191+
load_vmservice_library, false) \
190192
V(NONPRODUCT, use_osr, UseOsr, use_osr, FLAG_use_osr)
191193

192194
#define BOOL_ISOLATE_FLAG_LIST_DEFAULT_GETTER(V) \
193-
V(PRODUCT, should_load_vmservice_library, ShouldLoadVmService, \
194-
load_vmservice_library, false) \
195195
V(PRODUCT, copy_parent_code, CopyParentCode, copy_parent_code, false) \
196196
V(PRODUCT, is_system_isolate, IsSystemIsolate, is_system_isolate, false)
197197

@@ -512,6 +512,14 @@ class IsolateGroup : public IntrusiveDListEntry<IsolateGroup> {
512512
return null_safety() || FLAG_strict_null_safety_checks;
513513
}
514514

515+
bool should_load_vmservice() const {
516+
return ShouldLoadVmServiceBit::decode(isolate_group_flags_);
517+
}
518+
void set_should_load_vmservice(bool value) {
519+
isolate_group_flags_ =
520+
ShouldLoadVmServiceBit::update(value, isolate_group_flags_);
521+
}
522+
515523
#if !defined(PRODUCT)
516524
#if !defined(DART_PRECOMPILED_RUNTIME)
517525
bool HasAttemptedReload() const {
@@ -820,6 +828,7 @@ class IsolateGroup : public IntrusiveDListEntry<IsolateGroup> {
820828
V(HasAttemptedReload) \
821829
V(NullSafety) \
822830
V(RemappingCids) \
831+
V(ShouldLoadVmService) \
823832
V(NullSafetySet) \
824833
V(Obfuscate) \
825834
V(UseFieldGuards) \
@@ -1360,13 +1369,6 @@ class Isolate : public BaseIsolate, public IntrusiveDListEntry<Isolate> {
13601369
FLAG_randomize_optimization_counter;
13611370
}
13621371

1363-
bool should_load_vmservice() const {
1364-
return ShouldLoadVmServiceBit::decode(isolate_flags_);
1365-
}
1366-
void set_should_load_vmservice(bool value) {
1367-
isolate_flags_ = ShouldLoadVmServiceBit::update(value, isolate_flags_);
1368-
}
1369-
13701372
const DispatchTable* dispatch_table() const {
13711373
return group()->dispatch_table();
13721374
}
@@ -1556,7 +1558,6 @@ class Isolate : public BaseIsolate, public IntrusiveDListEntry<Isolate> {
15561558
V(HasAttemptedStepping) \
15571559
V(ShouldPausePostServiceRequest) \
15581560
V(CopyParentCode) \
1559-
V(ShouldLoadVmService) \
15601561
V(IsSystemIsolate)
15611562

15621563
// Isolate specific flags.
@@ -1764,9 +1765,11 @@ class EnterIsolateGroupScope {
17641765
//
17651766
// This can be used in code (e.g. GC, Kernel Loader) that should not operate on
17661767
// an individual isolate.
1767-
class NoActiveIsolateScope {
1768+
class NoActiveIsolateScope : public StackResource {
17681769
public:
1769-
NoActiveIsolateScope() : thread_(Thread::Current()) {
1770+
NoActiveIsolateScope()
1771+
: StackResource(Thread::Current()),
1772+
thread_(static_cast<Thread*>(thread())) {
17701773
saved_isolate_ = thread_->isolate_;
17711774
thread_->isolate_ = nullptr;
17721775
}

runtime/vm/kernel_loader.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ void KernelLoader::ReadLoadingUnits() {
238238
Object& KernelLoader::LoadEntireProgram(Program* program,
239239
bool process_pending_classes) {
240240
Thread* thread = Thread::Current();
241+
241242
TIMELINE_DURATION(thread, Isolate, "LoadKernel");
242243

243244
if (program->is_single_program()) {
@@ -772,6 +773,8 @@ ObjectPtr KernelLoader::LoadProgram(bool process_pending_classes) {
772773
}
773774

774775
void KernelLoader::LoadLibrary(const Library& library) {
776+
NoActiveIsolateScope no_active_isolate_scope;
777+
775778
// This will be invoked by VM bootstrapping code.
776779
SafepointWriteRwLocker ml(thread_, thread_->isolate_group()->program_lock());
777780

@@ -986,6 +989,8 @@ LibraryPtr KernelLoader::LoadLibrary(intptr_t index) {
986989
"not allowed");
987990
}
988991

992+
NoActiveIsolateScope no_active_isolate_scope;
993+
989994
// Read library index.
990995
library_kernel_offset_ = library_offset(index);
991996
correction_offset_ = library_kernel_offset_;
@@ -1001,7 +1006,7 @@ LibraryPtr KernelLoader::LoadLibrary(intptr_t index) {
10011006

10021007
LibraryHelper library_helper(&helper_, kernel_binary_version_);
10031008
library_helper.ReadUntilIncluding(LibraryHelper::kCanonicalName);
1004-
if (!FLAG_precompiled_mode && !I->should_load_vmservice()) {
1009+
if (!FLAG_precompiled_mode && !IG->should_load_vmservice()) {
10051010
StringIndex lib_name_index =
10061011
H.CanonicalNameString(library_helper.canonical_name_);
10071012
if (H.StringEquals(lib_name_index, kVMServiceIOLibraryUri)) {
@@ -1791,6 +1796,8 @@ void KernelLoader::FinishClassLoading(const Class& klass,
17911796
}
17921797

17931798
void KernelLoader::FinishLoading(const Class& klass) {
1799+
NoActiveIsolateScope no_active_isolate_scope;
1800+
17941801
ASSERT(klass.IsTopLevel() || (klass.kernel_offset() > 0));
17951802

17961803
Zone* zone = Thread::Current()->zone();

runtime/vm/kernel_loader.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,16 @@ class KernelLoader : public ValueObject {
214214
intptr_t kernel_buffer_length,
215215
const String& url);
216216

217-
void FinishTopLevelClassLoading(const Class& toplevel_class,
218-
const Library& library,
219-
const LibraryIndex& library_index);
220-
221217
static void FinishLoading(const Class& klass);
222218

223219
void ReadObfuscationProhibitions();
224220
void ReadLoadingUnits();
225221

226222
private:
223+
void FinishTopLevelClassLoading(const Class& toplevel_class,
224+
const Library& library,
225+
const LibraryIndex& library_index);
226+
227227
// Check for the presence of a (possibly const) constructor for the
228228
// 'ExternalName' class. If found, returns the name parameter to the
229229
// constructor.

0 commit comments

Comments
 (0)