-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
main_impl.cc
1443 lines (1297 loc) · 54.6 KB
/
main_impl.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
594
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 (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "bin/main_impl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory>
#include <utility>
#include "bin/builtin.h"
#include "bin/console.h"
#include "bin/crashpad.h"
#include "bin/dartdev_isolate.h"
#include "bin/dartutils.h"
#include "bin/dfe.h"
#include "bin/error_exit.h"
#include "bin/eventhandler.h"
#include "bin/file.h"
#include "bin/gzip.h"
#include "bin/isolate_data.h"
#include "bin/loader.h"
#include "bin/main_options.h"
#include "bin/platform.h"
#include "bin/process.h"
#include "bin/snapshot_utils.h"
#include "bin/thread.h"
#include "bin/utils.h"
#include "bin/vmservice_impl.h"
#include "include/bin/dart_io_api.h"
#include "include/dart_api.h"
#include "include/dart_embedder_api.h"
#include "include/dart_tools_api.h"
#include "platform/globals.h"
#include "platform/growable_array.h"
#include "platform/hashmap.h"
#include "platform/syslog.h"
#include "platform/text_buffer.h"
#include "platform/utils.h"
extern "C" {
extern const uint8_t kDartVmSnapshotData[];
extern const uint8_t kDartVmSnapshotInstructions[];
extern const uint8_t kDartCoreIsolateSnapshotData[];
extern const uint8_t kDartCoreIsolateSnapshotInstructions[];
}
namespace dart {
namespace bin {
// Snapshot pieces we link in a snapshot.
const uint8_t* vm_snapshot_data = kDartVmSnapshotData;
const uint8_t* vm_snapshot_instructions = kDartVmSnapshotInstructions;
const uint8_t* core_isolate_snapshot_data = kDartCoreIsolateSnapshotData;
const uint8_t* core_isolate_snapshot_instructions =
kDartCoreIsolateSnapshotInstructions;
/**
* Global state used to control and store generation of application snapshots.
* An application snapshot can be generated and run using the following
* command
* dart --snapshot-kind=app-jit --snapshot=<app_snapshot_filename>
* <script_uri> [<script_options>]
* To Run the application snapshot generated above, use :
* dart <app_snapshot_filename> [<script_options>]
*/
static bool vm_run_app_snapshot = false;
static char* app_script_uri = NULL;
static const uint8_t* app_isolate_snapshot_data = NULL;
static const uint8_t* app_isolate_snapshot_instructions = NULL;
static bool kernel_isolate_is_running = false;
static Dart_Isolate main_isolate = NULL;
#define SAVE_ERROR_AND_EXIT(result) \
*error = Utils::StrDup(Dart_GetError(result)); \
if (Dart_IsCompilationError(result)) { \
*exit_code = kCompilationErrorExitCode; \
} else if (Dart_IsApiError(result)) { \
*exit_code = kApiErrorExitCode; \
} else { \
*exit_code = kErrorExitCode; \
} \
Dart_ExitScope(); \
Dart_ShutdownIsolate(); \
return NULL;
#define CHECK_RESULT(result) \
if (Dart_IsError(result)) { \
SAVE_ERROR_AND_EXIT(result); \
}
#define CHECK_RESULT_CLEANUP(result, cleanup) \
if (Dart_IsError(result)) { \
delete (cleanup); \
SAVE_ERROR_AND_EXIT(result); \
}
static void WriteDepsFile() {
if (Options::depfile() == NULL) {
return;
}
File* file = File::Open(NULL, Options::depfile(), File::kWriteTruncate);
if (file == NULL) {
ErrorExit(kErrorExitCode, "Error: Unable to open snapshot depfile: %s\n\n",
Options::depfile());
}
bool success = true;
if (Options::depfile_output_filename() != NULL) {
success &= file->Print("%s: ", Options::depfile_output_filename());
} else {
success &= file->Print("%s: ", Options::snapshot_filename());
}
if (kernel_isolate_is_running) {
Dart_KernelCompilationResult result = Dart_KernelListDependencies();
if (result.status != Dart_KernelCompilationStatus_Ok) {
ErrorExit(
kErrorExitCode,
"Error: Failed to fetch dependencies from kernel service: %s\n\n",
result.error);
}
success &= file->WriteFully(result.kernel, result.kernel_size);
free(result.kernel);
}
success &= file->Print("\n");
if (!success) {
ErrorExit(kErrorExitCode, "Error: Unable to write snapshot depfile: %s\n\n",
Options::depfile());
}
file->Release();
}
static void OnExitHook(int64_t exit_code) {
if (Dart_CurrentIsolate() != main_isolate) {
Syslog::PrintErr(
"A snapshot was requested, but a secondary isolate "
"performed a hard exit (%" Pd64 ").\n",
exit_code);
Platform::Exit(kErrorExitCode);
}
if (exit_code == 0) {
if (Options::gen_snapshot_kind() == kAppJIT) {
Snapshot::GenerateAppJIT(Options::snapshot_filename());
}
WriteDepsFile();
}
}
static Dart_Handle SetupCoreLibraries(Dart_Isolate isolate,
IsolateData* isolate_data,
bool is_isolate_group_start,
const char** resolved_packages_config) {
auto isolate_group_data = isolate_data->isolate_group_data();
const auto packages_file = isolate_data->packages_file();
const auto script_uri = isolate_group_data->script_url;
Dart_Handle result;
// Prepare builtin and other core libraries for use to resolve URIs.
// Set up various closures, e.g: printing, timers etc.
// Set up package configuration for URI resolution.
result = DartUtils::PrepareForScriptLoading(false, Options::trace_loading());
if (Dart_IsError(result)) return result;
// Setup packages config if specified.
result = DartUtils::SetupPackageConfig(packages_file);
if (Dart_IsError(result)) return result;
if (!Dart_IsNull(result) && resolved_packages_config != nullptr) {
result = Dart_StringToCString(result, resolved_packages_config);
if (Dart_IsError(result)) return result;
ASSERT(*resolved_packages_config != nullptr);
#if !defined(DART_PRECOMPILED_RUNTIME)
if (is_isolate_group_start) {
isolate_group_data->set_resolved_packages_config(
*resolved_packages_config);
} else {
ASSERT(strcmp(isolate_group_data->resolved_packages_config(),
*resolved_packages_config) == 0);
}
#endif
}
result = Dart_SetEnvironmentCallback(DartUtils::EnvironmentCallback);
if (Dart_IsError(result)) return result;
// Setup the native resolver as the snapshot does not carry it.
Builtin::SetNativeResolver(Builtin::kBuiltinLibrary);
Builtin::SetNativeResolver(Builtin::kIOLibrary);
Builtin::SetNativeResolver(Builtin::kCLILibrary);
VmService::SetNativeResolver();
const char* namespc =
Dart_IsKernelIsolate(isolate) ? NULL : Options::namespc();
result =
DartUtils::SetupIOLibrary(namespc, script_uri, Options::exit_disabled());
if (Dart_IsError(result)) return result;
return Dart_Null();
}
static bool OnIsolateInitialize(void** child_callback_data, char** error) {
Dart_Isolate isolate = Dart_CurrentIsolate();
ASSERT(isolate != nullptr);
auto isolate_group_data =
reinterpret_cast<IsolateGroupData*>(Dart_CurrentIsolateGroupData());
auto isolate_data = new IsolateData(isolate_group_data);
*child_callback_data = isolate_data;
Dart_EnterScope();
const auto script_uri = isolate_group_data->script_url;
const bool isolate_run_app_snapshot =
isolate_group_data->RunFromAppSnapshot();
Dart_Handle result = SetupCoreLibraries(isolate, isolate_data,
/*group_start=*/false,
/*resolved_packages_config=*/nullptr);
if (Dart_IsError(result)) goto failed;
if (isolate_run_app_snapshot) {
result = Loader::InitForSnapshot(script_uri, isolate_data);
if (Dart_IsError(result)) goto failed;
} else {
result = DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri));
if (Dart_IsError(result)) goto failed;
if (isolate_group_data->kernel_buffer() != nullptr) {
// Various core-library parts will send requests to the Loader to resolve
// relative URIs and perform other related tasks. We need Loader to be
// initialized for this to work because loading from Kernel binary
// bypasses normal source code loading paths that initialize it.
const char* resolved_script_uri = NULL;
result = Dart_StringToCString(result, &resolved_script_uri);
if (Dart_IsError(result)) goto failed;
result = Loader::InitForSnapshot(resolved_script_uri, isolate_data);
if (Dart_IsError(result)) goto failed;
}
}
Dart_ExitScope();
return true;
failed:
*error = Utils::StrDup(Dart_GetError(result));
Dart_ExitScope();
return false;
}
static Dart_Isolate IsolateSetupHelper(Dart_Isolate isolate,
bool is_main_isolate,
const char* script_uri,
const char* packages_config,
bool isolate_run_app_snapshot,
Dart_IsolateFlags* flags,
char** error,
int* exit_code) {
Dart_EnterScope();
// Set up the library tag handler for the isolate group shared by all
// isolates in the group.
Dart_Handle result = Dart_SetLibraryTagHandler(Loader::LibraryTagHandler);
CHECK_RESULT(result);
result = Dart_SetDeferredLoadHandler(Loader::DeferredLoadHandler);
CHECK_RESULT(result);
auto isolate_data = reinterpret_cast<IsolateData*>(Dart_IsolateData(isolate));
const char* resolved_packages_config = nullptr;
result = SetupCoreLibraries(isolate, isolate_data,
/*is_isolate_group_start=*/true,
&resolved_packages_config);
CHECK_RESULT(result);
#if !defined(DART_PRECOMPILED_RUNTIME)
auto isolate_group_data = isolate_data->isolate_group_data();
const uint8_t* kernel_buffer = isolate_group_data->kernel_buffer().get();
intptr_t kernel_buffer_size = isolate_group_data->kernel_buffer_size();
if (!isolate_run_app_snapshot && kernel_buffer == NULL &&
!Dart_IsKernelIsolate(isolate)) {
if (!dfe.CanUseDartFrontend()) {
const char* format = "Dart frontend unavailable to compile script %s.";
intptr_t len = snprintf(NULL, 0, format, script_uri) + 1;
*error = reinterpret_cast<char*>(malloc(len));
ASSERT(error != NULL);
snprintf(*error, len, format, script_uri);
*exit_code = kErrorExitCode;
Dart_ExitScope();
Dart_ShutdownIsolate();
return NULL;
}
uint8_t* application_kernel_buffer = NULL;
intptr_t application_kernel_buffer_size = 0;
// Only pass snapshot = true when generating an AppJIT snapshot to avoid
// duplicate null-safety info messages from the frontend when generating
// a kernel snapshot (this flag is instead set in
// Snapshot::GenerateKernel()).
const bool snapshot = Options::gen_snapshot_kind() == kAppJIT;
dfe.CompileAndReadScript(script_uri, &application_kernel_buffer,
&application_kernel_buffer_size, error, exit_code,
resolved_packages_config, snapshot);
if (application_kernel_buffer == NULL) {
Dart_ExitScope();
Dart_ShutdownIsolate();
return NULL;
}
isolate_group_data->SetKernelBufferNewlyOwned(
application_kernel_buffer, application_kernel_buffer_size);
kernel_buffer = application_kernel_buffer;
kernel_buffer_size = application_kernel_buffer_size;
}
if (kernel_buffer != NULL) {
Dart_Handle uri = Dart_NewStringFromCString(script_uri);
CHECK_RESULT(uri);
Dart_Handle resolved_script_uri = DartUtils::ResolveScript(uri);
CHECK_RESULT(resolved_script_uri);
result = Dart_LoadScriptFromKernel(kernel_buffer, kernel_buffer_size);
CHECK_RESULT(result);
}
#endif // !defined(DART_PRECOMPILED_RUNTIME)
if (isolate_run_app_snapshot) {
Dart_Handle result = Loader::InitForSnapshot(script_uri, isolate_data);
CHECK_RESULT(result);
#if !defined(DART_PRECOMPILED_RUNTIME)
if (is_main_isolate) {
// Find the canonical uri of the app snapshot. We'll use this to decide if
// other isolates should use the app snapshot or the core snapshot.
const char* resolved_script_uri = NULL;
result = Dart_StringToCString(
DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri)),
&resolved_script_uri);
CHECK_RESULT(result);
ASSERT(app_script_uri == NULL);
app_script_uri = Utils::StrDup(resolved_script_uri);
}
#endif // !defined(DART_PRECOMPILED_RUNTIME)
} else {
#if !defined(DART_PRECOMPILED_RUNTIME)
// Load the specified application script into the newly created isolate.
Dart_Handle uri =
DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri));
CHECK_RESULT(uri);
if (kernel_buffer != NULL) {
// relative URIs and perform other related tasks. We need Loader to be
// initialized for this to work because loading from Kernel binary
// bypasses normal source code loading paths that initialize it.
const char* resolved_script_uri = NULL;
result = Dart_StringToCString(uri, &resolved_script_uri);
CHECK_RESULT(result);
result = Loader::InitForSnapshot(resolved_script_uri, isolate_data);
CHECK_RESULT(result);
}
Dart_TimelineEvent("LoadScript", Dart_TimelineGetMicros(),
Dart_GetMainPortId(), Dart_Timeline_Event_Async_End, 0,
NULL, NULL);
#else
UNREACHABLE();
#endif // !defined(DART_PRECOMPILED_RUNTIME)
}
if ((Options::gen_snapshot_kind() == kAppJIT) && is_main_isolate) {
result = Dart_SortClasses();
CHECK_RESULT(result);
}
// Disable pausing the DartDev isolate on start and exit.
const char* isolate_name = nullptr;
result = Dart_StringToCString(Dart_DebugName(), &isolate_name);
CHECK_RESULT(result);
#if !defined(DART_PRECOMPILED_RUNTIME)
if (strstr(isolate_name, DART_DEV_ISOLATE_NAME) != nullptr) {
Dart_SetShouldPauseOnStart(false);
Dart_SetShouldPauseOnExit(false);
}
#endif // !defined(DART_PRECOMPILED_RUNTIME)
// Make the isolate runnable so that it is ready to handle messages.
Dart_ExitScope();
Dart_ExitIsolate();
*error = Dart_IsolateMakeRunnable(isolate);
if (*error != NULL) {
Dart_EnterIsolate(isolate);
Dart_ShutdownIsolate();
return NULL;
}
return isolate;
}
#if !defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM)
// Returns newly created Kernel Isolate on success, NULL on failure.
// For now we only support the kernel isolate coming up from an
// application snapshot or from a .dill file.
static Dart_Isolate CreateAndSetupKernelIsolate(const char* script_uri,
const char* packages_config,
Dart_IsolateFlags* flags,
char** error,
int* exit_code) {
// Do not start a kernel isolate if we are doing a training run
// to create an app JIT snapshot and a kernel file is specified
// as the application to run.
if (Options::gen_snapshot_kind() == kAppJIT) {
const uint8_t* kernel_buffer = NULL;
intptr_t kernel_buffer_size = 0;
dfe.application_kernel_buffer(&kernel_buffer, &kernel_buffer_size);
if (kernel_buffer_size != 0) {
return NULL;
}
}
// Create and Start the kernel isolate.
const char* kernel_snapshot_uri = dfe.frontend_filename();
const char* uri =
kernel_snapshot_uri != NULL ? kernel_snapshot_uri : script_uri;
if (packages_config == NULL) {
packages_config = Options::packages_file();
}
Dart_Isolate isolate = NULL;
IsolateGroupData* isolate_group_data = nullptr;
IsolateData* isolate_data = nullptr;
bool isolate_run_app_snapshot = false;
AppSnapshot* app_snapshot = NULL;
// Kernel isolate uses an app snapshot or uses the dill file.
if ((kernel_snapshot_uri != NULL) &&
(app_snapshot = Snapshot::TryReadAppSnapshot(
kernel_snapshot_uri, /*force_load_elf_from_memory=*/false,
/*decode_uri=*/false)) != nullptr) {
const uint8_t* isolate_snapshot_data = NULL;
const uint8_t* isolate_snapshot_instructions = NULL;
const uint8_t* ignore_vm_snapshot_data;
const uint8_t* ignore_vm_snapshot_instructions;
isolate_run_app_snapshot = true;
app_snapshot->SetBuffers(
&ignore_vm_snapshot_data, &ignore_vm_snapshot_instructions,
&isolate_snapshot_data, &isolate_snapshot_instructions);
isolate_group_data = new IsolateGroupData(
uri, packages_config, app_snapshot, isolate_run_app_snapshot);
isolate_data = new IsolateData(isolate_group_data);
isolate = Dart_CreateIsolateGroup(
DART_KERNEL_ISOLATE_NAME, DART_KERNEL_ISOLATE_NAME,
isolate_snapshot_data, isolate_snapshot_instructions, flags,
isolate_group_data, isolate_data, error);
}
if (isolate == NULL) {
// Clear error from app snapshot and re-trying from kernel file.
free(*error);
*error = NULL;
delete isolate_data;
delete isolate_group_data;
const uint8_t* kernel_service_buffer = NULL;
intptr_t kernel_service_buffer_size = 0;
dfe.LoadKernelService(&kernel_service_buffer, &kernel_service_buffer_size);
ASSERT(kernel_service_buffer != NULL);
isolate_group_data = new IsolateGroupData(uri, packages_config, nullptr,
isolate_run_app_snapshot);
isolate_group_data->SetKernelBufferUnowned(
const_cast<uint8_t*>(kernel_service_buffer),
kernel_service_buffer_size);
isolate_data = new IsolateData(isolate_group_data);
isolate = Dart_CreateIsolateGroupFromKernel(
DART_KERNEL_ISOLATE_NAME, DART_KERNEL_ISOLATE_NAME,
kernel_service_buffer, kernel_service_buffer_size, flags,
isolate_group_data, isolate_data, error);
}
if (isolate == NULL) {
Syslog::PrintErr("%s\n", *error);
delete isolate_data;
delete isolate_group_data;
return NULL;
}
kernel_isolate_is_running = true;
return IsolateSetupHelper(isolate, false, uri, packages_config,
isolate_run_app_snapshot, flags, error, exit_code);
}
#endif // !defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM)
// Returns newly created Service Isolate on success, NULL on failure.
// For now we only support the service isolate coming up from sources
// which are compiled by the VM parser.
static Dart_Isolate CreateAndSetupServiceIsolate(const char* script_uri,
const char* packages_config,
Dart_IsolateFlags* flags,
char** error,
int* exit_code) {
#if !defined(PRODUCT)
ASSERT(script_uri != nullptr);
Dart_Isolate isolate = nullptr;
auto isolate_group_data =
new IsolateGroupData(script_uri, packages_config, nullptr, false);
ASSERT(flags != nullptr);
#if defined(DART_PRECOMPILED_RUNTIME)
// AOT: The service isolate is included in any AOT snapshot in non-PRODUCT
// mode - so we launch the vm-service from the main app AOT snapshot.
const uint8_t* isolate_snapshot_data = app_isolate_snapshot_data;
const uint8_t* isolate_snapshot_instructions =
app_isolate_snapshot_instructions;
flags->null_safety =
Dart_DetectNullSafety(nullptr, nullptr, nullptr, isolate_snapshot_data,
isolate_snapshot_instructions, nullptr, -1);
isolate = Dart_CreateIsolateGroup(
script_uri, DART_VM_SERVICE_ISOLATE_NAME, isolate_snapshot_data,
isolate_snapshot_instructions, flags, isolate_group_data,
/*isolate_data=*/nullptr, error);
#else
// JIT: Service isolate uses the core libraries snapshot.
// Set flag to load and retain the vmservice library.
flags->load_vmservice_library = true;
const uint8_t* isolate_snapshot_data = core_isolate_snapshot_data;
const uint8_t* isolate_snapshot_instructions =
core_isolate_snapshot_instructions;
isolate = Dart_CreateIsolateGroup(
script_uri, DART_VM_SERVICE_ISOLATE_NAME, isolate_snapshot_data,
isolate_snapshot_instructions, flags, isolate_group_data,
/*isolate_data=*/nullptr, error);
#endif // !defined(DART_PRECOMPILED_RUNTIME)
if (isolate == NULL) {
delete isolate_group_data;
return NULL;
}
Dart_EnterScope();
Dart_Handle result = Dart_SetLibraryTagHandler(Loader::LibraryTagHandler);
CHECK_RESULT(result);
result = Dart_SetDeferredLoadHandler(Loader::DeferredLoadHandler);
CHECK_RESULT(result);
int vm_service_server_port = INVALID_VM_SERVICE_SERVER_PORT;
if (Options::disable_dart_dev() || Options::disable_dds()) {
vm_service_server_port = Options::vm_service_server_port();
} else if (Options::vm_service_server_port() !=
INVALID_VM_SERVICE_SERVER_PORT) {
vm_service_server_port = 0;
}
// We do not want to wait for DDS to advertise availability of VM service in
// the following scenarios:
// - The DartDev CLI is disabled (CLI isolate starts DDS) and VM service is
// enabled.
// - DDS is disabled.
// TODO(bkonyi): do we want to tie DevTools / DDS to the CLI in the long run?
bool wait_for_dds_to_advertise_service =
!(Options::disable_dart_dev() || Options::disable_dds());
// Load embedder specific bits and return.
if (!VmService::Setup(
!wait_for_dds_to_advertise_service ? Options::vm_service_server_ip()
: DEFAULT_VM_SERVICE_SERVER_IP,
vm_service_server_port, Options::vm_service_dev_mode(),
Options::vm_service_auth_disabled(),
Options::vm_write_service_info_filename(), Options::trace_loading(),
Options::deterministic(), Options::enable_service_port_fallback(),
wait_for_dds_to_advertise_service, !Options::disable_observatory())) {
*error = Utils::StrDup(VmService::GetErrorMessage());
return NULL;
}
if (Options::compile_all()) {
result = Dart_CompileAll();
CHECK_RESULT(result);
}
result = Dart_SetEnvironmentCallback(DartUtils::EnvironmentCallback);
CHECK_RESULT(result);
Dart_ExitScope();
Dart_ExitIsolate();
return isolate;
#else // !defined(PRODUCT)
return NULL;
#endif // !defined(PRODUCT)
}
#if !defined(DART_PRECOMPILED_RUNTIME)
static Dart_Isolate CreateAndSetupDartDevIsolate(const char* script_uri,
const char* packages_config,
Dart_IsolateFlags* flags,
char** error,
int* exit_code) {
int64_t start = Dart_TimelineGetMicros();
auto dartdev_path = DartDevIsolate::TryResolveDartDevSnapshotPath();
Dart_Isolate isolate = nullptr;
const uint8_t* isolate_snapshot_data = core_isolate_snapshot_data;
const uint8_t* isolate_snapshot_instructions =
core_isolate_snapshot_instructions;
IsolateGroupData* isolate_group_data = nullptr;
IsolateData* isolate_data = nullptr;
if (error != nullptr) {
*error = nullptr;
}
AppSnapshot* app_snapshot = nullptr;
bool isolate_run_app_snapshot = true;
if (dartdev_path.get() != nullptr &&
(app_snapshot = Snapshot::TryReadAppSnapshot(
dartdev_path.get(), /*force_load_elf_from_memory=*/false,
/*decode_uri=*/false)) != nullptr) {
const uint8_t* isolate_snapshot_data = NULL;
const uint8_t* isolate_snapshot_instructions = NULL;
const uint8_t* ignore_vm_snapshot_data;
const uint8_t* ignore_vm_snapshot_instructions;
app_snapshot->SetBuffers(
&ignore_vm_snapshot_data, &ignore_vm_snapshot_instructions,
&isolate_snapshot_data, &isolate_snapshot_instructions);
isolate_group_data =
new IsolateGroupData(DART_DEV_ISOLATE_NAME, packages_config,
app_snapshot, isolate_run_app_snapshot);
isolate_data = new IsolateData(isolate_group_data);
isolate = Dart_CreateIsolateGroup(
DART_DEV_ISOLATE_NAME, DART_DEV_ISOLATE_NAME, isolate_snapshot_data,
isolate_snapshot_instructions, flags, isolate_group_data, isolate_data,
error);
}
if (isolate == nullptr) {
isolate_run_app_snapshot = false;
dartdev_path = DartDevIsolate::TryResolveDartDevKernelPath();
// Clear error from app snapshot and retry from kernel.
if (error != nullptr && *error != nullptr) {
free(*error);
*error = nullptr;
}
if (app_snapshot != nullptr) {
delete app_snapshot;
}
if (dartdev_path.get() == nullptr) {
Syslog::PrintErr(
"Failed to start the Dart CLI isolate. Could not resolve DartDev "
"snapshot or kernel.\n");
delete isolate_data;
delete isolate_group_data;
return nullptr;
}
isolate_group_data =
new IsolateGroupData(DART_DEV_ISOLATE_NAME, packages_config, nullptr,
isolate_run_app_snapshot);
uint8_t* application_kernel_buffer = NULL;
intptr_t application_kernel_buffer_size = 0;
dfe.ReadScript(dartdev_path.get(), &application_kernel_buffer,
&application_kernel_buffer_size, /*decode_uri=*/false);
isolate_group_data->SetKernelBufferNewlyOwned(
application_kernel_buffer, application_kernel_buffer_size);
isolate_data = new IsolateData(isolate_group_data);
isolate = Dart_CreateIsolateGroup(
DART_DEV_ISOLATE_NAME, DART_DEV_ISOLATE_NAME, isolate_snapshot_data,
isolate_snapshot_instructions, flags, isolate_group_data, isolate_data,
error);
}
Dart_Isolate created_isolate =
IsolateSetupHelper(isolate, false, DART_DEV_ISOLATE_NAME, packages_config,
isolate_run_app_snapshot, flags, error, exit_code);
int64_t end = Dart_TimelineGetMicros();
Dart_TimelineEvent("CreateAndSetupDartDevIsolate", start, end,
Dart_Timeline_Event_Duration, 0, NULL, NULL);
return created_isolate;
}
#endif // !defined(DART_PRECOMPILED_RUNTIME)
// Returns newly created Isolate on success, NULL on failure.
static Dart_Isolate CreateIsolateGroupAndSetupHelper(
bool is_main_isolate,
const char* script_uri,
const char* name,
const char* packages_config,
Dart_IsolateFlags* flags,
void* callback_data,
char** error,
int* exit_code,
bool force_no_sound_null_safety = false) {
int64_t start = Dart_TimelineGetMicros();
ASSERT(script_uri != NULL);
uint8_t* kernel_buffer = NULL;
std::shared_ptr<uint8_t> kernel_buffer_ptr;
intptr_t kernel_buffer_size = 0;
AppSnapshot* app_snapshot = NULL;
#if defined(DART_PRECOMPILED_RUNTIME)
const uint8_t* isolate_snapshot_data = nullptr;
const uint8_t* isolate_snapshot_instructions = nullptr;
if (is_main_isolate) {
isolate_snapshot_data = app_isolate_snapshot_data;
isolate_snapshot_instructions = app_isolate_snapshot_instructions;
} else {
// AOT: All isolates need to be run from AOT compiled snapshots.
const bool kForceLoadElfFromMemory = false;
app_snapshot =
Snapshot::TryReadAppSnapshot(script_uri, kForceLoadElfFromMemory);
if (app_snapshot == nullptr) {
*error = Utils::StrDup(
"The uri provided to `Isolate.spawnUri()` does not "
"contain a valid AOT snapshot.");
return nullptr;
}
const uint8_t* ignore_vm_snapshot_data;
const uint8_t* ignore_vm_snapshot_instructions;
app_snapshot->SetBuffers(
&ignore_vm_snapshot_data, &ignore_vm_snapshot_instructions,
&isolate_snapshot_data, &isolate_snapshot_instructions);
}
bool isolate_run_app_snapshot = true;
flags->null_safety =
Dart_DetectNullSafety(nullptr, nullptr, nullptr, isolate_snapshot_data,
isolate_snapshot_instructions, nullptr, -1);
#else
// JIT: Main isolate starts from the app snapshot, if any. Other isolates
// use the core libraries snapshot.
bool isolate_run_app_snapshot = false;
const uint8_t* isolate_snapshot_data = core_isolate_snapshot_data;
const uint8_t* isolate_snapshot_instructions =
core_isolate_snapshot_instructions;
if ((app_isolate_snapshot_data != NULL) &&
(is_main_isolate || ((app_script_uri != NULL) &&
(strcmp(script_uri, app_script_uri) == 0)))) {
isolate_run_app_snapshot = true;
isolate_snapshot_data = app_isolate_snapshot_data;
isolate_snapshot_instructions = app_isolate_snapshot_instructions;
} else if (!is_main_isolate) {
app_snapshot = Snapshot::TryReadAppSnapshot(script_uri);
if (app_snapshot != NULL) {
isolate_run_app_snapshot = true;
const uint8_t* ignore_vm_snapshot_data;
const uint8_t* ignore_vm_snapshot_instructions;
app_snapshot->SetBuffers(
&ignore_vm_snapshot_data, &ignore_vm_snapshot_instructions,
&isolate_snapshot_data, &isolate_snapshot_instructions);
}
}
if (flags->copy_parent_code && callback_data != nullptr) {
auto parent_isolate_group_data =
reinterpret_cast<IsolateData*>(callback_data)->isolate_group_data();
kernel_buffer_ptr = parent_isolate_group_data->kernel_buffer();
kernel_buffer = kernel_buffer_ptr.get();
kernel_buffer_size = parent_isolate_group_data->kernel_buffer_size();
}
if (kernel_buffer == NULL && !isolate_run_app_snapshot) {
dfe.ReadScript(script_uri, &kernel_buffer, &kernel_buffer_size,
/*decode_uri=*/true, &kernel_buffer_ptr);
}
PathSanitizer script_uri_sanitizer(script_uri);
PathSanitizer packages_config_sanitizer(packages_config);
if (force_no_sound_null_safety) {
flags->null_safety = false;
} else {
flags->null_safety = Dart_DetectNullSafety(
script_uri_sanitizer.sanitized_uri(),
packages_config_sanitizer.sanitized_uri(),
DartUtils::original_working_directory, isolate_snapshot_data,
isolate_snapshot_instructions, kernel_buffer, kernel_buffer_size);
}
#endif // !defined(DART_PRECOMPILED_RUNTIME)
auto isolate_group_data = new IsolateGroupData(
script_uri, packages_config, app_snapshot, isolate_run_app_snapshot);
if (kernel_buffer != NULL) {
if (kernel_buffer_ptr) {
isolate_group_data->SetKernelBufferAlreadyOwned(
std::move(kernel_buffer_ptr), kernel_buffer_size);
} else {
isolate_group_data->SetKernelBufferNewlyOwned(kernel_buffer,
kernel_buffer_size);
}
}
Dart_Isolate isolate = NULL;
IsolateData* isolate_data = nullptr;
#if !defined(DART_PRECOMPILED_RUNTIME)
if (!isolate_run_app_snapshot && (isolate_snapshot_data == NULL)) {
const uint8_t* platform_kernel_buffer = NULL;
intptr_t platform_kernel_buffer_size = 0;
dfe.LoadPlatform(&platform_kernel_buffer, &platform_kernel_buffer_size);
if (platform_kernel_buffer == NULL) {
platform_kernel_buffer = kernel_buffer;
platform_kernel_buffer_size = kernel_buffer_size;
}
if (platform_kernel_buffer == NULL) {
#if defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM)
FATAL(
"Binary built with --exclude-kernel-service. Cannot run"
" from source.");
#else
FATAL("platform_program cannot be NULL.");
#endif // defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM)
}
// TODO(sivachandra): When the platform program is unavailable, check if
// application kernel binary is self contained or an incremental binary.
// Isolate should be created only if it is a self contained kernel binary.
isolate_data = new IsolateData(isolate_group_data);
isolate = Dart_CreateIsolateGroupFromKernel(
script_uri, name, platform_kernel_buffer, platform_kernel_buffer_size,
flags, isolate_group_data, isolate_data, error);
} else {
isolate_data = new IsolateData(isolate_group_data);
isolate = Dart_CreateIsolateGroup(script_uri, name, isolate_snapshot_data,
isolate_snapshot_instructions, flags,
isolate_group_data, isolate_data, error);
}
#else
isolate_data = new IsolateData(isolate_group_data);
isolate = Dart_CreateIsolateGroup(script_uri, name, isolate_snapshot_data,
isolate_snapshot_instructions, flags,
isolate_group_data, isolate_data, error);
#endif // !defined(DART_PRECOMPILED_RUNTIME)
Dart_Isolate created_isolate = NULL;
if (isolate == NULL) {
delete isolate_data;
delete isolate_group_data;
} else {
created_isolate = IsolateSetupHelper(
isolate, is_main_isolate, script_uri, packages_config,
isolate_run_app_snapshot, flags, error, exit_code);
}
int64_t end = Dart_TimelineGetMicros();
Dart_TimelineEvent("CreateIsolateGroupAndSetupHelper", start, end,
Dart_Timeline_Event_Duration, 0, NULL, NULL);
return created_isolate;
}
#undef CHECK_RESULT
static Dart_Isolate CreateIsolateGroupAndSetup(const char* script_uri,
const char* main,
const char* package_root,
const char* package_config,
Dart_IsolateFlags* flags,
void* callback_data,
char** error) {
// The VM should never call the isolate helper with a NULL flags.
ASSERT(flags != NULL);
ASSERT(flags->version == DART_FLAGS_CURRENT_VERSION);
ASSERT(package_root == nullptr);
bool dontneed_safe = true;
#if defined(DART_HOST_OS_LINUX)
// This would also be true in Linux, except that Google3 overrides the default
// ELF interpreter to one that apparently doesn't create proper mappings.
dontneed_safe = false;
#elif defined(DEBUG)
// If the snapshot isn't file-backed, madvise(DONT_NEED) is destructive.
if (Options::force_load_elf_from_memory()) {
dontneed_safe = false;
}
#endif
flags->snapshot_is_dontneed_safe = dontneed_safe;
int exit_code = 0;
#if !defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM)
if (strcmp(script_uri, DART_KERNEL_ISOLATE_NAME) == 0) {
return CreateAndSetupKernelIsolate(script_uri, package_config, flags, error,
&exit_code);
}
#endif // !defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM)
#if !defined(DART_PRECOMPILED_RUNTIME)
if (strcmp(script_uri, DART_DEV_ISOLATE_NAME) == 0) {
return CreateAndSetupDartDevIsolate(script_uri, package_config, flags,
error, &exit_code);
}
#endif // !defined(DART_PRECOMPILED_RUNTIME)
if (strcmp(script_uri, DART_VM_SERVICE_ISOLATE_NAME) == 0) {
return CreateAndSetupServiceIsolate(script_uri, package_config, flags,
error, &exit_code);
}
bool is_main_isolate = false;
return CreateIsolateGroupAndSetupHelper(is_main_isolate, script_uri, main,
package_config, flags, callback_data,
error, &exit_code);
}
#if !defined(DART_PRECOMPILED_RUNTIME)
static const char* RegisterKernelBlob(const uint8_t* kernel_buffer,
intptr_t kernel_buffer_size) {
return dfe.RegisterKernelBlob(kernel_buffer, kernel_buffer_size);
}
static void UnregisterKernelBlob(const char* kernel_blob_uri) {
dfe.UnregisterKernelBlob(kernel_blob_uri);
}
#endif // !defined(DART_PRECOMPILED_RUNTIME)
static void OnIsolateShutdown(void* isolate_group_data, void* isolate_data) {
Dart_EnterScope();
Dart_Handle sticky_error = Dart_GetStickyError();
if (!Dart_IsNull(sticky_error) && !Dart_IsFatalError(sticky_error)) {
Syslog::PrintErr("%s\n", Dart_GetError(sticky_error));
}
Dart_ExitScope();
}
static void DeleteIsolateData(void* isolate_group_data, void* callback_data) {
auto isolate_data = reinterpret_cast<IsolateData*>(callback_data);
delete isolate_data;
}
static void DeleteIsolateGroupData(void* callback_data) {
auto isolate_group_data = reinterpret_cast<IsolateGroupData*>(callback_data);
delete isolate_group_data;
}
static const char* kStdoutStreamId = "Stdout";
static const char* kStderrStreamId = "Stderr";
static bool ServiceStreamListenCallback(const char* stream_id) {
if (strcmp(stream_id, kStdoutStreamId) == 0) {
SetCaptureStdout(true);
return true;
} else if (strcmp(stream_id, kStderrStreamId) == 0) {
SetCaptureStderr(true);
return true;
}
return false;
}
static void ServiceStreamCancelCallback(const char* stream_id) {
if (strcmp(stream_id, kStdoutStreamId) == 0) {
SetCaptureStdout(false);
} else if (strcmp(stream_id, kStderrStreamId) == 0) {
SetCaptureStderr(false);
}
}
static bool FileModifiedCallback(const char* url, int64_t since) {
auto path = File::UriToPath(url);
if (path == nullptr) {
// If it isn't a file on local disk, we don't know if it has been
// modified.
return true;
}
int64_t data[File::kStatSize];
File::Stat(NULL, path.get(), data);
if (data[File::kType] == File::kDoesNotExist) {
return true;
}
return data[File::kModifiedTime] > since;
}
static void EmbedderInformationCallback(Dart_EmbedderInformation* info) {
info->version = DART_EMBEDDER_INFORMATION_CURRENT_VERSION;
info->name = "Dart VM";
Process::GetRSSInformation(&(info->max_rss), &(info->current_rss));
}
#define CHECK_RESULT(result) \
if (Dart_IsError(result)) { \
const int exit_code = Dart_IsCompilationError(result) \
? kCompilationErrorExitCode \
: kErrorExitCode; \
ErrorExit(exit_code, "%s\n", Dart_GetError(result)); \
}
static void CompileAndSaveKernel(const char* script_name,
const char* package_config_override,
CommandLineOptions* dart_options) {
if (vm_run_app_snapshot) {
Syslog::PrintErr("Cannot create a script snapshot from an app snapshot.\n");
// The snapshot would contain references to the app snapshot instead of
// the core snapshot.
Platform::Exit(kErrorExitCode);
}
Snapshot::GenerateKernel(Options::snapshot_filename(), script_name,
package_config_override);
WriteDepsFile();
}
void RunMainIsolate(const char* script_name,
const char* package_config_override,
bool force_no_sound_null_safety,
CommandLineOptions* dart_options) {
if (script_name != NULL) {
const char* base_name = strrchr(script_name, '/');
if (base_name == NULL) {
base_name = script_name;
} else {
base_name++; // Skip '/'.
}
const intptr_t kMaxNameLength = 64;
char name[kMaxNameLength];
Utils::SNPrint(name, kMaxNameLength, "dart:%s", base_name);
Platform::SetProcessName(name);