-
Notifications
You must be signed in to change notification settings - Fork 6k
/
dart_isolate.cc
1386 lines (1176 loc) · 48.2 KB
/
dart_isolate.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/runtime/dart_isolate.h"
#include <cstdlib>
#include <tuple>
#include <utility>
#include "flutter/fml/logging.h"
#include "flutter/fml/posix_wrappers.h"
#include "flutter/fml/trace_event.h"
#include "flutter/lib/io/dart_io.h"
#include "flutter/lib/ui/dart_runtime_hooks.h"
#include "flutter/lib/ui/dart_ui.h"
#include "flutter/lib/ui/window/platform_isolate.h"
#include "flutter/runtime/dart_isolate_group_data.h"
#include "flutter/runtime/dart_plugin_registrant.h"
#include "flutter/runtime/dart_service_isolate.h"
#include "flutter/runtime/dart_vm.h"
#include "flutter/runtime/dart_vm_lifecycle.h"
#include "flutter/runtime/isolate_configuration.h"
#include "flutter/runtime/runtime_controller.h"
#include "fml/message_loop_task_queues.h"
#include "fml/task_source.h"
#include "fml/time/time_point.h"
#include "third_party/dart/runtime/include/bin/native_assets_api.h"
#include "third_party/dart/runtime/include/dart_api.h"
#include "third_party/dart/runtime/include/dart_tools_api.h"
#include "third_party/tonic/converter/dart_converter.h"
#include "third_party/tonic/dart_class_library.h"
#include "third_party/tonic/dart_class_provider.h"
#include "third_party/tonic/dart_message_handler.h"
#include "third_party/tonic/dart_state.h"
#include "third_party/tonic/file_loader/file_loader.h"
#include "third_party/tonic/logging/dart_invoke.h"
#include "third_party/tonic/scopes/dart_api_scope.h"
#include "third_party/tonic/scopes/dart_isolate_scope.h"
namespace flutter {
namespace {
constexpr std::string_view kFileUriPrefix = "file://";
class DartErrorString {
public:
DartErrorString() {}
~DartErrorString() {
if (str_) {
::free(str_);
}
}
char** error() { return &str_; }
const char* str() const { return str_; }
explicit operator bool() const { return str_ != nullptr; }
private:
FML_DISALLOW_COPY_AND_ASSIGN(DartErrorString);
char* str_ = nullptr;
};
} // anonymous namespace
DartIsolate::Flags::Flags() : Flags(nullptr) {}
DartIsolate::Flags::Flags(const Dart_IsolateFlags* flags) {
if (flags) {
flags_ = *flags;
} else {
::Dart_IsolateFlagsInitialize(&flags_);
}
}
DartIsolate::Flags::~Flags() = default;
void DartIsolate::Flags::SetNullSafetyEnabled(bool enabled) {
flags_.null_safety = enabled;
}
void DartIsolate::Flags::SetIsDontNeedSafe(bool value) {
flags_.snapshot_is_dontneed_safe = value;
}
Dart_IsolateFlags DartIsolate::Flags::Get() const {
return flags_;
}
std::weak_ptr<DartIsolate> DartIsolate::CreateRunningRootIsolate(
const Settings& settings,
const fml::RefPtr<const DartSnapshot>& isolate_snapshot,
std::unique_ptr<PlatformConfiguration> platform_configuration,
Flags isolate_flags,
const fml::closure& root_isolate_create_callback,
const fml::closure& isolate_create_callback,
const fml::closure& isolate_shutdown_callback,
std::optional<std::string> dart_entrypoint,
std::optional<std::string> dart_entrypoint_library,
const std::vector<std::string>& dart_entrypoint_args,
std::unique_ptr<IsolateConfiguration> isolate_configuration,
const UIDartState::Context& context,
const DartIsolate* spawning_isolate) {
if (!isolate_snapshot) {
FML_LOG(ERROR) << "Invalid isolate snapshot.";
return {};
}
if (!isolate_configuration) {
FML_LOG(ERROR) << "Invalid isolate configuration.";
return {};
}
isolate_flags.SetNullSafetyEnabled(
isolate_configuration->IsNullSafetyEnabled(*isolate_snapshot));
isolate_flags.SetIsDontNeedSafe(isolate_snapshot->IsDontNeedSafe());
auto isolate = CreateRootIsolate(settings, //
isolate_snapshot, //
std::move(platform_configuration), //
isolate_flags, //
isolate_create_callback, //
isolate_shutdown_callback, //
context, //
spawning_isolate //
)
.lock();
if (!isolate) {
FML_LOG(ERROR) << "Could not create root isolate.";
return {};
}
fml::ScopedCleanupClosure shutdown_on_error([isolate]() {
if (!isolate->Shutdown()) {
FML_DLOG(ERROR) << "Could not shutdown transient isolate.";
}
});
if (isolate->GetPhase() != DartIsolate::Phase::LibrariesSetup) {
FML_LOG(ERROR) << "Root isolate was created in an incorrect phase: "
<< static_cast<int>(isolate->GetPhase());
return {};
}
if (!isolate_configuration->PrepareIsolate(*isolate.get())) {
FML_LOG(ERROR) << "Could not prepare isolate.";
return {};
}
if (isolate->GetPhase() != DartIsolate::Phase::Ready) {
FML_LOG(ERROR) << "Root isolate not in the ready phase for Dart entrypoint "
"invocation.";
return {};
}
if (settings.root_isolate_create_callback) {
// Isolate callbacks always occur in isolate scope and before user code has
// had a chance to run.
tonic::DartState::Scope scope(isolate.get());
settings.root_isolate_create_callback(*isolate.get());
}
if (root_isolate_create_callback) {
root_isolate_create_callback();
}
if (!isolate->RunFromLibrary(std::move(dart_entrypoint_library), //
std::move(dart_entrypoint), //
dart_entrypoint_args)) {
FML_LOG(ERROR) << "Could not run the run main Dart entrypoint.";
return {};
}
if (settings.root_isolate_shutdown_callback) {
isolate->AddIsolateShutdownCallback(
settings.root_isolate_shutdown_callback);
}
shutdown_on_error.Release();
return isolate;
}
void DartIsolate::SpawnIsolateShutdownCallback(
std::shared_ptr<DartIsolateGroupData>* isolate_group_data,
std::shared_ptr<DartIsolate>* isolate_data) {
DartIsolate::DartIsolateShutdownCallback(isolate_group_data, isolate_data);
}
std::weak_ptr<DartIsolate> DartIsolate::CreateRootIsolate(
const Settings& settings,
fml::RefPtr<const DartSnapshot> isolate_snapshot,
std::unique_ptr<PlatformConfiguration> platform_configuration,
const Flags& flags,
const fml::closure& isolate_create_callback,
const fml::closure& isolate_shutdown_callback,
const UIDartState::Context& context,
const DartIsolate* spawning_isolate) {
TRACE_EVENT0("flutter", "DartIsolate::CreateRootIsolate");
// Only needed if this is the main isolate for the group.
std::unique_ptr<std::shared_ptr<DartIsolateGroupData>> isolate_group_data;
auto isolate_data = std::make_unique<std::shared_ptr<DartIsolate>>(
std::shared_ptr<DartIsolate>(new DartIsolate(
/*settings=*/settings,
/*is_root_isolate=*/true,
/*context=*/context,
/*is_spawning_in_group=*/!!spawning_isolate)));
DartErrorString error;
Dart_Isolate vm_isolate = nullptr;
auto isolate_flags = flags.Get();
IsolateMaker isolate_maker;
if (spawning_isolate) {
isolate_maker =
[spawning_isolate](
std::shared_ptr<DartIsolateGroupData>* isolate_group_data,
std::shared_ptr<DartIsolate>* isolate_data,
Dart_IsolateFlags* flags, char** error) {
return Dart_CreateIsolateInGroup(
/*group_member=*/spawning_isolate->isolate(),
/*name=*/
spawning_isolate->GetIsolateGroupData()
.GetAdvisoryScriptEntrypoint()
.c_str(),
/*shutdown_callback=*/
reinterpret_cast<Dart_IsolateShutdownCallback>(
DartIsolate::SpawnIsolateShutdownCallback),
/*cleanup_callback=*/
reinterpret_cast<Dart_IsolateCleanupCallback>(
DartIsolateCleanupCallback),
/*child_isolate_data=*/isolate_data,
/*error=*/error);
};
} else {
// The child isolate preparer is null but will be set when the isolate is
// being prepared to run.
isolate_group_data =
std::make_unique<std::shared_ptr<DartIsolateGroupData>>(
std::shared_ptr<DartIsolateGroupData>(new DartIsolateGroupData(
settings, // settings
std::move(isolate_snapshot), // isolate snapshot
context.advisory_script_uri, // advisory URI
context.advisory_script_entrypoint, // advisory entrypoint
nullptr, // child isolate preparer
isolate_create_callback, // isolate create callback
isolate_shutdown_callback // isolate shutdown callback
)));
isolate_maker = [](std::shared_ptr<DartIsolateGroupData>*
isolate_group_data,
std::shared_ptr<DartIsolate>* isolate_data,
Dart_IsolateFlags* flags, char** error) {
return Dart_CreateIsolateGroup(
(*isolate_group_data)->GetAdvisoryScriptURI().c_str(),
(*isolate_group_data)->GetAdvisoryScriptEntrypoint().c_str(),
(*isolate_group_data)->GetIsolateSnapshot()->GetDataMapping(),
(*isolate_group_data)->GetIsolateSnapshot()->GetInstructionsMapping(),
flags, isolate_group_data, isolate_data, error);
};
}
vm_isolate = CreateDartIsolateGroup(std::move(isolate_group_data),
std::move(isolate_data), &isolate_flags,
error.error(), isolate_maker);
if (error) {
FML_LOG(ERROR) << "CreateRootIsolate failed: " << error.str();
}
if (vm_isolate == nullptr) {
return {};
}
std::shared_ptr<DartIsolate>* root_isolate_data =
static_cast<std::shared_ptr<DartIsolate>*>(Dart_IsolateData(vm_isolate));
(*root_isolate_data)
->SetPlatformConfiguration(std::move(platform_configuration));
return (*root_isolate_data)->GetWeakIsolatePtr();
}
Dart_Isolate DartIsolate::CreatePlatformIsolate(Dart_Handle entry_point,
char** error) {
*error = nullptr;
PlatformConfiguration* platform_config = platform_configuration();
FML_DCHECK(platform_config != nullptr);
std::shared_ptr<PlatformIsolateManager> platform_isolate_manager =
platform_config->client()->GetPlatformIsolateManager();
std::weak_ptr<PlatformIsolateManager> weak_platform_isolate_manager =
platform_isolate_manager;
if (platform_isolate_manager->HasShutdownMaybeFalseNegative()) {
// Don't set the error string. We want to silently ignore this error,
// because the engine is shutting down.
FML_LOG(INFO) << "CreatePlatformIsolate called after shutdown";
return nullptr;
}
Dart_Isolate parent_isolate = isolate();
Dart_ExitIsolate(); // Exit parent_isolate.
const TaskRunners& task_runners = GetTaskRunners();
fml::RefPtr<fml::TaskRunner> platform_task_runner =
task_runners.GetPlatformTaskRunner();
FML_DCHECK(platform_task_runner);
auto isolate_group_data = std::shared_ptr<DartIsolateGroupData>(
*static_cast<std::shared_ptr<DartIsolateGroupData>*>(
Dart_IsolateGroupData(parent_isolate)));
Settings settings(isolate_group_data->GetSettings());
// PlatformIsolate.spawn should behave like Isolate.spawn when unhandled
// exceptions happen (log the exception, but don't terminate the app). But the
// default unhandled_exception_callback may terminate the app, because it is
// only called for the root isolate (child isolates are managed by the VM and
// have a different error code path). So override it to simply log the error.
settings.unhandled_exception_callback = [](const std::string& error,
const std::string& stack_trace) {
FML_LOG(ERROR) << "Unhandled exception:\n" << error << "\n" << stack_trace;
return true;
};
// The platform isolate task observer must be added on the platform thread. So
// schedule the add function on the platform task runner.
TaskObserverAdd old_task_observer_add = settings.task_observer_add;
settings.task_observer_add = [old_task_observer_add, platform_task_runner,
weak_platform_isolate_manager](
intptr_t key, const fml::closure& callback) {
platform_task_runner->PostTask([old_task_observer_add,
weak_platform_isolate_manager, key,
callback]() {
std::shared_ptr<PlatformIsolateManager> platform_isolate_manager =
weak_platform_isolate_manager.lock();
if (platform_isolate_manager == nullptr ||
platform_isolate_manager->HasShutdown()) {
// Shutdown happened in between this task being posted, and it running.
// platform_isolate has already been shut down. Do nothing.
FML_LOG(INFO) << "Shutdown before platform isolate task observer added";
return;
}
old_task_observer_add(key, callback);
});
};
UIDartState::Context context(task_runners);
context.advisory_script_uri = isolate_group_data->GetAdvisoryScriptURI();
context.advisory_script_entrypoint =
isolate_group_data->GetAdvisoryScriptEntrypoint();
auto isolate_data = std::make_unique<std::shared_ptr<DartIsolate>>(
std::shared_ptr<DartIsolate>(
new DartIsolate(settings, context, platform_isolate_manager)));
IsolateMaker isolate_maker =
[parent_isolate](
std::shared_ptr<DartIsolateGroupData>* unused_isolate_group_data,
std::shared_ptr<DartIsolate>* isolate_data, Dart_IsolateFlags* flags,
char** error) {
return Dart_CreateIsolateInGroup(
/*group_member=*/parent_isolate,
/*name=*/"PlatformIsolate",
/*shutdown_callback=*/
reinterpret_cast<Dart_IsolateShutdownCallback>(
DartIsolate::SpawnIsolateShutdownCallback),
/*cleanup_callback=*/
reinterpret_cast<Dart_IsolateCleanupCallback>(
DartIsolateCleanupCallback),
/*child_isolate_data=*/isolate_data,
/*error=*/error);
};
Dart_Isolate platform_isolate = CreateDartIsolateGroup(
nullptr, std::move(isolate_data), nullptr, error, isolate_maker);
Dart_EnterIsolate(parent_isolate);
if (*error) {
return nullptr;
}
if (!platform_isolate_manager->RegisterPlatformIsolate(platform_isolate)) {
// The PlatformIsolateManager was shutdown while we were creating the
// isolate. This means that we're shutting down the engine. We need to
// shutdown the platform isolate.
FML_LOG(INFO) << "Shutdown during platform isolate creation";
tonic::DartIsolateScope isolate_scope(platform_isolate);
Dart_ShutdownIsolate();
return nullptr;
}
tonic::DartApiScope api_scope;
Dart_PersistentHandle entry_point_handle =
Dart_NewPersistentHandle(entry_point);
platform_task_runner->PostTask([entry_point_handle, platform_isolate,
weak_platform_isolate_manager]() {
std::shared_ptr<PlatformIsolateManager> platform_isolate_manager =
weak_platform_isolate_manager.lock();
if (platform_isolate_manager == nullptr ||
platform_isolate_manager->HasShutdown()) {
// Shutdown happened in between this task being posted, and it running.
// platform_isolate has already been shut down. Do nothing.
FML_LOG(INFO) << "Shutdown before platform isolate entry point";
return;
}
tonic::DartIsolateScope isolate_scope(platform_isolate);
tonic::DartApiScope api_scope;
Dart_Handle entry_point = Dart_HandleFromPersistent(entry_point_handle);
Dart_DeletePersistentHandle(entry_point_handle);
// Disable Isolate.exit().
Dart_Handle isolate_lib = Dart_LookupLibrary(tonic::ToDart("dart:isolate"));
FML_CHECK(!tonic::CheckAndHandleError(isolate_lib));
Dart_Handle isolate_type = Dart_GetNonNullableType(
isolate_lib, tonic::ToDart("Isolate"), 0, nullptr);
FML_CHECK(!tonic::CheckAndHandleError(isolate_type));
Dart_Handle result =
Dart_SetField(isolate_type, tonic::ToDart("_mayExit"), Dart_False());
FML_CHECK(!tonic::CheckAndHandleError(result));
tonic::DartInvokeVoid(entry_point);
});
return platform_isolate;
}
DartIsolate::DartIsolate(const Settings& settings,
bool is_root_isolate,
const UIDartState::Context& context,
bool is_spawning_in_group)
: UIDartState(settings.task_observer_add,
settings.task_observer_remove,
settings.log_tag,
settings.unhandled_exception_callback,
settings.log_message_callback,
DartVMRef::GetIsolateNameServer(),
is_root_isolate,
context),
may_insecurely_connect_to_all_domains_(
settings.may_insecurely_connect_to_all_domains),
is_platform_isolate_(false),
is_spawning_in_group_(is_spawning_in_group),
domain_network_policy_(settings.domain_network_policy) {
phase_ = Phase::Uninitialized;
}
DartIsolate::DartIsolate(
const Settings& settings,
const UIDartState::Context& context,
std::shared_ptr<PlatformIsolateManager> platform_isolate_manager)
: UIDartState(settings.task_observer_add,
settings.task_observer_remove,
settings.log_tag,
settings.unhandled_exception_callback,
settings.log_message_callback,
DartVMRef::GetIsolateNameServer(),
false, // is_root_isolate
context),
may_insecurely_connect_to_all_domains_(
settings.may_insecurely_connect_to_all_domains),
is_platform_isolate_(true),
is_spawning_in_group_(false),
domain_network_policy_(settings.domain_network_policy),
platform_isolate_manager_(std::move(platform_isolate_manager)) {
phase_ = Phase::Uninitialized;
}
DartIsolate::~DartIsolate() {
if (IsRootIsolate() && GetMessageHandlingTaskRunner()) {
FML_DCHECK(GetMessageHandlingTaskRunner()->RunsTasksOnCurrentThread());
}
}
DartIsolate::Phase DartIsolate::GetPhase() const {
return phase_;
}
std::string DartIsolate::GetServiceId() {
const char* service_id_buf = Dart_IsolateServiceId(isolate());
std::string service_id(service_id_buf);
free(const_cast<char*>(service_id_buf));
return service_id;
}
bool DartIsolate::Initialize(Dart_Isolate dart_isolate) {
TRACE_EVENT0("flutter", "DartIsolate::Initialize");
if (phase_ != Phase::Uninitialized) {
return false;
}
FML_DCHECK(dart_isolate != nullptr);
FML_DCHECK(dart_isolate == Dart_CurrentIsolate());
// After this point, isolate scopes can be safely used.
SetIsolate(dart_isolate);
// For the root isolate set the "AppStartUp" as soon as the root isolate
// has been initialized. This is to ensure that all the timeline events
// that have the set user-tag will be listed user AppStartUp.
if (IsRootIsolate()) {
tonic::DartApiScope api_scope;
Dart_SetCurrentUserTag(Dart_NewUserTag("AppStartUp"));
}
if (is_platform_isolate_) {
SetMessageHandlingTaskRunner(GetTaskRunners().GetPlatformTaskRunner(),
true);
} else {
SetMessageHandlingTaskRunner(GetTaskRunners().GetUITaskRunner(), false);
}
if (tonic::CheckAndHandleError(
Dart_SetLibraryTagHandler(tonic::DartState::HandleLibraryTag))) {
return false;
}
if (tonic::CheckAndHandleError(
Dart_SetDeferredLoadHandler(OnDartLoadLibrary))) {
return false;
}
if (!UpdateThreadPoolNames()) {
return false;
}
phase_ = Phase::Initialized;
return true;
}
fml::RefPtr<fml::TaskRunner> DartIsolate::GetMessageHandlingTaskRunner() const {
return message_handling_task_runner_;
}
bool DartIsolate::LoadLoadingUnit(
intptr_t loading_unit_id,
std::unique_ptr<const fml::Mapping> snapshot_data,
std::unique_ptr<const fml::Mapping> snapshot_instructions) {
tonic::DartState::Scope scope(this);
fml::RefPtr<DartSnapshot> dart_snapshot =
DartSnapshot::IsolateSnapshotFromMappings(
std::move(snapshot_data), std::move(snapshot_instructions));
Dart_Handle result = Dart_DeferredLoadComplete(
loading_unit_id, dart_snapshot->GetDataMapping(),
dart_snapshot->GetInstructionsMapping());
if (tonic::CheckAndHandleError(result)) {
LoadLoadingUnitError(loading_unit_id, Dart_GetError(result),
/*transient*/ true);
return false;
}
loading_unit_snapshots_.insert(dart_snapshot);
return true;
}
void DartIsolate::LoadLoadingUnitError(intptr_t loading_unit_id,
const std::string& error_message,
bool transient) {
tonic::DartState::Scope scope(this);
Dart_Handle result = Dart_DeferredLoadCompleteError(
loading_unit_id, error_message.c_str(), transient);
tonic::CheckAndHandleError(result);
}
void DartIsolate::SetMessageHandlingTaskRunner(
const fml::RefPtr<fml::TaskRunner>& runner,
bool post_directly_to_runner) {
if (!runner) {
return;
}
message_handling_task_runner_ = runner;
tonic::DartMessageHandler::TaskDispatcher dispatcher;
#ifdef OS_FUCHSIA
post_directly_to_runner = true;
#endif
if (post_directly_to_runner) {
dispatcher = [runner](std::function<void()> task) {
runner->PostTask([task = std::move(task)]() {
TRACE_EVENT0("flutter", "DartIsolate::HandleMessage");
task();
});
};
} else {
dispatcher = [runner](std::function<void()> task) {
auto task_queues = fml::MessageLoopTaskQueues::GetInstance();
task_queues->RegisterTask(
runner->GetTaskQueueId(),
[task = std::move(task)]() {
TRACE_EVENT0("flutter", "DartIsolate::HandleMessage");
task();
},
fml::TimePoint::Now(), fml::TaskSourceGrade::kDartEventLoop);
};
}
message_handler().Initialize(dispatcher);
}
// Updating thread names here does not change the underlying OS thread names.
// Instead, this is just additional metadata for the Dart VM Service to show the
// thread name of the isolate.
bool DartIsolate::UpdateThreadPoolNames() const {
// TODO(chinmaygarde): This implementation does not account for multiple
// shells sharing the same (or subset of) threads.
const auto& task_runners = GetTaskRunners();
if (auto task_runner = task_runners.GetRasterTaskRunner()) {
task_runner->PostTask(
[label = task_runners.GetLabel() std::string{".raster"}]() {
Dart_SetThreadName(label.c_str());
});
}
if (auto task_runner = task_runners.GetUITaskRunner()) {
task_runner->PostTask(
[label = task_runners.GetLabel() std::string{".ui"}]() {
Dart_SetThreadName(label.c_str());
});
}
if (auto task_runner = task_runners.GetIOTaskRunner()) {
task_runner->PostTask(
[label = task_runners.GetLabel() std::string{".io"}]() {
Dart_SetThreadName(label.c_str());
});
}
if (auto task_runner = task_runners.GetPlatformTaskRunner()) {
bool is_merged_platform_ui_thread =
task_runner == task_runners.GetUITaskRunner();
std::string label;
if (is_merged_platform_ui_thread) {
label = task_runners.GetLabel() std::string{".ui"};
} else {
label = task_runners.GetLabel() std::string{".platform"};
}
task_runner->PostTask(
[label = std::move(label)]() { Dart_SetThreadName(label.c_str()); });
}
return true;
}
bool DartIsolate::LoadLibraries() {
TRACE_EVENT0("flutter", "DartIsolate::LoadLibraries");
if (phase_ != Phase::Initialized) {
return false;
}
tonic::DartState::Scope scope(this);
DartIO::InitForIsolate(may_insecurely_connect_to_all_domains_,
domain_network_policy_);
DartUI::InitForIsolate(GetIsolateGroupData().GetSettings());
const bool is_service_isolate = Dart_IsServiceIsolate(isolate());
DartRuntimeHooks::Install(IsRootIsolate() && !is_service_isolate,
GetAdvisoryScriptURI());
if (!is_service_isolate) {
class_library().add_provider(
"ui", std::make_unique<tonic::DartClassProvider>(this, "dart:ui"));
}
phase_ = Phase::LibrariesSetup;
return true;
}
bool DartIsolate::PrepareForRunningFromPrecompiledCode() {
TRACE_EVENT0("flutter", "DartIsolate::PrepareForRunningFromPrecompiledCode");
if (phase_ != Phase::LibrariesSetup) {
return false;
}
tonic::DartState::Scope scope(this);
if (Dart_IsNull(Dart_RootLibrary())) {
return false;
}
if (!MarkIsolateRunnable()) {
return false;
}
if (GetIsolateGroupData().GetChildIsolatePreparer() == nullptr) {
GetIsolateGroupData().SetChildIsolatePreparer([](DartIsolate* isolate) {
return isolate->PrepareForRunningFromPrecompiledCode();
});
}
const fml::closure& isolate_create_callback =
GetIsolateGroupData().GetIsolateCreateCallback();
if (isolate_create_callback) {
isolate_create_callback();
}
phase_ = Phase::Ready;
return true;
}
bool DartIsolate::LoadKernel(const std::shared_ptr<const fml::Mapping>& mapping,
bool last_piece) {
if (!Dart_IsKernel(mapping->GetMapping(), mapping->GetSize())) {
return false;
}
// Mapping must be retained until isolate group shutdown.
GetIsolateGroupData().AddKernelBuffer(mapping);
Dart_Handle library =
Dart_LoadLibraryFromKernel(mapping->GetMapping(), mapping->GetSize());
if (tonic::CheckAndHandleError(library)) {
return false;
}
if (!last_piece) {
// More to come.
return true;
}
Dart_SetRootLibrary(library);
if (tonic::CheckAndHandleError(Dart_FinalizeLoading(false))) {
return false;
}
return true;
}
[[nodiscard]] bool DartIsolate::PrepareForRunningFromKernel(
const std::shared_ptr<const fml::Mapping>& mapping,
bool child_isolate,
bool last_piece) {
TRACE_EVENT0("flutter", "DartIsolate::PrepareForRunningFromKernel");
if (phase_ != Phase::LibrariesSetup) {
return false;
}
if (DartVM::IsRunningPrecompiledCode()) {
return false;
}
tonic::DartState::Scope scope(this);
if (!child_isolate && !is_spawning_in_group_) {
if (!mapping || mapping->GetSize() == 0) {
return false;
}
// Use root library provided by kernel in favor of one provided by snapshot.
Dart_SetRootLibrary(Dart_Null());
if (!LoadKernel(mapping, last_piece)) {
return false;
}
}
if (!last_piece) {
// More to come.
return true;
}
if (Dart_IsNull(Dart_RootLibrary())) {
return false;
}
if (!MarkIsolateRunnable()) {
return false;
}
// Child isolate shares root isolate embedder_isolate (lines 691 and 693
// below). Re-initializing child_isolate_preparer_ lambda while it is being
// executed leads to crashes.
if (GetIsolateGroupData().GetChildIsolatePreparer() == nullptr) {
GetIsolateGroupData().SetChildIsolatePreparer(
[buffers =
GetIsolateGroupData().GetKernelBuffers()](DartIsolate* isolate) {
for (uint64_t i = 0; i < buffers.size(); i ) {
bool last_piece = i 1 == buffers.size();
const std::shared_ptr<const fml::Mapping>& buffer = buffers.at(i);
if (!isolate->PrepareForRunningFromKernel(buffer,
/*child_isolate=*/true,
last_piece)) {
return false;
}
}
return true;
});
}
const fml::closure& isolate_create_callback =
GetIsolateGroupData().GetIsolateCreateCallback();
if (isolate_create_callback) {
isolate_create_callback();
}
phase_ = Phase::Ready;
return true;
}
[[nodiscard]] bool DartIsolate::PrepareForRunningFromKernels(
std::vector<std::shared_ptr<const fml::Mapping>> kernels) {
const auto count = kernels.size();
if (count == 0) {
return false;
}
for (size_t i = 0; i < count; i) {
bool last = (i == (count - 1));
if (!PrepareForRunningFromKernel(kernels[i], /*child_isolate=*/false,
last)) {
return false;
}
}
return true;
}
[[nodiscard]] bool DartIsolate::PrepareForRunningFromKernels(
std::vector<std::unique_ptr<const fml::Mapping>> kernels) {
std::vector<std::shared_ptr<const fml::Mapping>> shared_kernels;
shared_kernels.reserve(kernels.size());
for (auto& kernel : kernels) {
shared_kernels.emplace_back(std::move(kernel));
}
return PrepareForRunningFromKernels(shared_kernels);
}
bool DartIsolate::MarkIsolateRunnable() {
TRACE_EVENT0("flutter", "DartIsolate::MarkIsolateRunnable");
if (phase_ != Phase::LibrariesSetup) {
return false;
}
// This function may only be called from an active isolate scope.
if (Dart_CurrentIsolate() != isolate()) {
return false;
}
// There must be no current isolate to mark an isolate as being runnable.
Dart_ExitIsolate();
char* error = Dart_IsolateMakeRunnable(isolate());
if (error) {
FML_DLOG(ERROR) << error;
::free(error);
// Failed. Restore the isolate.
Dart_EnterIsolate(isolate());
return false;
}
// Success. Restore the isolate.
Dart_EnterIsolate(isolate());
return true;
}
[[nodiscard]] static bool InvokeMainEntrypoint(
Dart_Handle user_entrypoint_function,
Dart_Handle args) {
if (tonic::CheckAndHandleError(user_entrypoint_function)) {
FML_LOG(ERROR) << "Could not resolve main entrypoint function.";
return false;
}
Dart_Handle start_main_isolate_function =
tonic::DartInvokeField(Dart_LookupLibrary(tonic::ToDart("dart:isolate")),
"_getStartMainIsolateFunction", {});
if (tonic::CheckAndHandleError(start_main_isolate_function)) {
FML_LOG(ERROR) << "Could not resolve main entrypoint trampoline.";
return false;
}
if (tonic::CheckAndHandleError(tonic::DartInvokeField(
Dart_LookupLibrary(tonic::ToDart("dart:ui")), "_runMain",
{start_main_isolate_function, user_entrypoint_function, args}))) {
FML_LOG(ERROR) << "Could not invoke the main entrypoint.";
return false;
}
return true;
}
bool DartIsolate::RunFromLibrary(std::optional<std::string> library_name,
std::optional<std::string> entrypoint,
const std::vector<std::string>& args) {
TRACE_EVENT0("flutter", "DartIsolate::RunFromLibrary");
if (phase_ != Phase::Ready) {
return false;
}
tonic::DartState::Scope scope(this);
auto library_handle =
library_name.has_value() && !library_name.value().empty()
? ::Dart_LookupLibrary(tonic::ToDart(library_name.value().c_str()))
: ::Dart_RootLibrary();
auto entrypoint_handle = entrypoint.has_value() && !entrypoint.value().empty()
? tonic::ToDart(entrypoint.value().c_str())
: tonic::ToDart("main");
if (!FindAndInvokeDartPluginRegistrant()) {
// TODO(gaaclarke): Remove once the framework PR lands that uses `--source`
// to compile the Dart Plugin Registrant
// (https://github.com/flutter/flutter/pull/100572).
InvokeDartPluginRegistrantIfAvailable(library_handle);
}
auto user_entrypoint_function =
::Dart_GetField(library_handle, entrypoint_handle);
auto entrypoint_args = tonic::ToDart(args);
if (!InvokeMainEntrypoint(user_entrypoint_function, entrypoint_args)) {
return false;
}
phase_ = Phase::Running;
return true;
}
bool DartIsolate::Shutdown() {
TRACE_EVENT0("flutter", "DartIsolate::Shutdown");
// This call may be re-entrant since Dart_ShutdownIsolate can invoke the
// cleanup callback which deletes the embedder side object of the dart isolate
// (a.k.a. this).
if (phase_ == Phase::Shutdown) {
return false;
}
phase_ = Phase::Shutdown;
Dart_Isolate vm_isolate = isolate();
// The isolate can be nullptr if this instance is the stub isolate data used
// during root isolate creation.
if (vm_isolate != nullptr) {
// We need to enter the isolate because Dart_ShutdownIsolate does not take
// the isolate to shutdown as a parameter.
FML_DCHECK(Dart_CurrentIsolate() == nullptr);
Dart_EnterIsolate(vm_isolate);
Dart_ShutdownIsolate();
FML_DCHECK(Dart_CurrentIsolate() == nullptr);
}
return true;
}
Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate(
const char* package_root,
const char* package_config,
Dart_IsolateFlags* flags,
char** error) {
auto vm_data = DartVMRef::GetVMData();
if (!vm_data) {
*error = fml::strdup(
"Could not access VM data to initialize isolates. This may be because "
"the VM has initialized shutdown on another thread already.");
return nullptr;
}
const auto& settings = vm_data->GetSettings();
if (!settings.enable_vm_service) {
return nullptr;
}
flags->load_vmservice_library = true;
#if (FLUTTER_RUNTIME_MODE != FLUTTER_RUNTIME_MODE_DEBUG)
// TODO(68663): The service isolate in debug mode is always launched without
// sound null safety. Fix after the isolate snapshot data is created with the
// right flags.
flags->null_safety = vm_data->GetServiceIsolateSnapshotNullSafety();
#endif
UIDartState::Context context(
TaskRunners("io.flutter." DART_VM_SERVICE_ISOLATE_NAME, nullptr, nullptr,
nullptr, nullptr));
context.advisory_script_uri = DART_VM_SERVICE_ISOLATE_NAME;
context.advisory_script_entrypoint = DART_VM_SERVICE_ISOLATE_NAME;
std::weak_ptr<DartIsolate> weak_service_isolate =
DartIsolate::CreateRootIsolate(vm_data->GetSettings(), //
vm_data->GetServiceIsolateSnapshot(), //
nullptr, //
DartIsolate::Flags{flags}, //
nullptr, //
nullptr, //
context); //
std::shared_ptr<DartIsolate> service_isolate = weak_service_isolate.lock();
if (!service_isolate) {
*error = fml::strdup("Could not create the service isolate.");
FML_DLOG(ERROR) << *error;
return nullptr;