ager@google.com | 607eea1 | 2012-01-10 16:50:14 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
Zachary Anderson | 6cd8a79 | 2017-07-13 08:08:33 -0700 | [diff] [blame] | 5 | #include <stdio.h> |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
Ben Konyi | 3849b50 | 2020-07-16 19:50:57 +0000 | [diff] [blame] | 8 | |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 9 | #include <memory> |
Alexander Aprelev | 7e37383 | 2020-07-17 04:37:48 +0000 | [diff] [blame] | 10 | |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 11 | #include "bin/builtin.h" |
Ben Konyi | 3c8d3b6 | 2018-03-12 20:31:09 +0000 | [diff] [blame] | 12 | #include "bin/console.h" |
Vyacheslav Egorov | f185012 | 2018-10-29 15:11:16 +0000 | [diff] [blame] | 13 | #include "bin/crashpad.h" |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 14 | #include "bin/dartdev_isolate.h" |
asiva@google.com | 0ce8298 | 2011-11-18 18:24:04 +0000 | [diff] [blame] | 15 | #include "bin/dartutils.h" |
Siva Annamalai | 286dd51 | 2017-05-25 15:40:36 -0700 | [diff] [blame] | 16 | #include "bin/dfe.h" |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 17 | #include "bin/error_exit.h" |
sgjesse@google.com | a6071e0 | 2012-01-09 07:38:30 +0000 | [diff] [blame] | 18 | #include "bin/eventhandler.h" |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 19 | #include "bin/file.h" |
Vyacheslav Egorov | f185012 | 2018-10-29 15:11:16 +0000 | [diff] [blame] | 20 | #include "bin/gzip.h" |
ager@google.com | f791a12 | 2012-06-29 14:40:17 +0000 | [diff] [blame] | 21 | #include "bin/isolate_data.h" |
John McCutchan | 6fd8fd7 | 2016-06-06 14:15:01 -0700 | [diff] [blame] | 22 | #include "bin/loader.h" |
Zachary Anderson | d7845c6 | 2017-09-11 18:58:39 +0000 | [diff] [blame] | 23 | #include "bin/main_options.h" |
sgjesse@google.com | d7c8c1f | 2011-11-23 14:49:18 +0000 | [diff] [blame] | 24 | #include "bin/platform.h" |
ager@google.com | ed3338d | 2012-02-01 18:16:45 +0000 | [diff] [blame] | 25 | #include "bin/process.h" |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 26 | #include "bin/snapshot_utils.h" |
iposva@google.com | e57e60c | 2014-08-14 05:00:37 +0000 | [diff] [blame] | 27 | #include "bin/thread.h" |
Ivan Posva | 6249c2b | 2015-12-03 17:46:02 -0800 | [diff] [blame] | 28 | #include "bin/utils.h" |
johnmccutchan@google.com | 42f5a39 | 2013-07-16 22:17:52 +0000 | [diff] [blame] | 29 | #include "bin/vmservice_impl.h" |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 30 | #include "include/bin/dart_io_api.h" |
| 31 | #include "include/dart_api.h" |
| 32 | #include "include/dart_embedder_api.h" |
| 33 | #include "include/dart_tools_api.h" |
ager@google.com | c4c92c4 | 2012-01-06 11:28:21 +0000 | [diff] [blame] | 34 | #include "platform/globals.h" |
P.Y. Laligand | 8f03520 | 2017-03-27 06:44:21 -0700 | [diff] [blame] | 35 | #include "platform/growable_array.h" |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 36 | #include "platform/hashmap.h" |
Ryan Macnak | 971f484 | 2019-04-22 20:15:43 +0000 | [diff] [blame] | 37 | #include "platform/syslog.h" |
Ivan Posva | 6249c2b | 2015-12-03 17:46:02 -0800 | [diff] [blame] | 38 | #include "platform/text_buffer.h" |
Ben Konyi | 6548694 | 2020-02-06 00:04:09 +0000 | [diff] [blame] | 39 | #include "platform/utils.h" |
ager@google.com | c4c92c4 | 2012-01-06 11:28:21 +0000 | [diff] [blame] | 40 | |
Ryan Macnak | 14ec8fa | 2017-05-24 14:38:09 -0700 | [diff] [blame] | 41 | extern "C" { |
| 42 | extern const uint8_t kDartVmSnapshotData[]; |
| 43 | extern const uint8_t kDartVmSnapshotInstructions[]; |
| 44 | extern const uint8_t kDartCoreIsolateSnapshotData[]; |
| 45 | extern const uint8_t kDartCoreIsolateSnapshotInstructions[]; |
| 46 | } |
| 47 | |
smok@google.com | ebbc202 | 2013-04-25 14:22:30 +0000 | [diff] [blame] | 48 | namespace dart { |
| 49 | namespace bin { |
| 50 | |
asiva | ac4b43f | 2020-01-08 18:56:21 +0000 | [diff] [blame] | 51 | // Snapshot pieces we link in a snapshot. |
Ryan Macnak | 14ec8fa | 2017-05-24 14:38:09 -0700 | [diff] [blame] | 52 | const uint8_t* vm_snapshot_data = kDartVmSnapshotData; |
| 53 | const uint8_t* vm_snapshot_instructions = kDartVmSnapshotInstructions; |
| 54 | const uint8_t* core_isolate_snapshot_data = kDartCoreIsolateSnapshotData; |
Ryan Macnak | 76df24b | 2017-05-31 13:49:09 -0700 | [diff] [blame] | 55 | const uint8_t* core_isolate_snapshot_instructions = |
| 56 | kDartCoreIsolateSnapshotInstructions; |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 57 | |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 58 | /** |
Zachary Anderson | d7845c6 | 2017-09-11 18:58:39 +0000 | [diff] [blame] | 59 | * Global state used to control and store generation of application snapshots. |
Siva Annamalai | af4d246 | 2016-10-19 17:42:01 -0700 | [diff] [blame] | 60 | * An application snapshot can be generated and run using the following |
| 61 | * command |
| 62 | * dart --snapshot-kind=app-jit --snapshot=<app_snapshot_filename> |
| 63 | * <script_uri> [<script_options>] |
| 64 | * To Run the application snapshot generated above, use : |
| 65 | * dart <app_snapshot_filename> [<script_options>] |
Siva Annamalai | 62b12f2 | 2016-02-05 15:28:59 -0800 | [diff] [blame] | 66 | */ |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 67 | static bool vm_run_app_snapshot = false; |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 68 | static char* app_script_uri = NULL; |
Ryan Macnak | b46af1e | 2017-01-23 10:25:02 -0800 | [diff] [blame] | 69 | static const uint8_t* app_isolate_snapshot_data = NULL; |
| 70 | static const uint8_t* app_isolate_snapshot_instructions = NULL; |
asiva | d1bd887 | 2018-08-21 20:28:06 +0000 | [diff] [blame] | 71 | static bool kernel_isolate_is_running = false; |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 72 | |
Ryan Macnak | 4d65db8a | 2016-10-31 15:56:25 -0700 | [diff] [blame] | 73 | static Dart_Isolate main_isolate = NULL; |
| 74 | |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 75 | #define SAVE_ERROR_AND_EXIT(result) \ |
Alexander Aprelev | d33e51d | 2020-06-25 03:36:41 +0000 | [diff] [blame] | 76 | *error = Utils::StrDup(Dart_GetError(result)); \ |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 77 | if (Dart_IsCompilationError(result)) { \ |
| 78 | *exit_code = kCompilationErrorExitCode; \ |
| 79 | } else if (Dart_IsApiError(result)) { \ |
| 80 | *exit_code = kApiErrorExitCode; \ |
| 81 | } else { \ |
| 82 | *exit_code = kErrorExitCode; \ |
| 83 | } \ |
| 84 | Dart_ExitScope(); \ |
| 85 | Dart_ShutdownIsolate(); \ |
| 86 | return NULL; |
| 87 | |
johnmccutchan@google.com | c76b6eb | 2014-01-14 22:34:43 +0000 | [diff] [blame] | 88 | #define CHECK_RESULT(result) \ |
| 89 | if (Dart_IsError(result)) { \ |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 90 | SAVE_ERROR_AND_EXIT(result); \ |
| 91 | } |
| 92 | |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 93 | #define CHECK_RESULT_CLEANUP(result, cleanup) \ |
| 94 | if (Dart_IsError(result)) { \ |
| 95 | delete (cleanup); \ |
| 96 | SAVE_ERROR_AND_EXIT(result); \ |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 97 | } |
johnmccutchan@google.com | c76b6eb | 2014-01-14 22:34:43 +0000 | [diff] [blame] | 98 | |
Zachary Anderson | f06bf4c | 2018-02-07 05:08:45 +0000 | [diff] [blame] | 99 | static void WriteDepsFile(Dart_Isolate isolate) { |
Zach Anderson | 00e3109 | 2018-07-31 17:09:31 +0000 | [diff] [blame] | 100 | if (Options::depfile() == NULL) { |
Zachary Anderson | f06bf4c | 2018-02-07 05:08:45 +0000 | [diff] [blame] | 101 | return; |
| 102 | } |
Samir Jindel | a846098 | 2019-12-10 12:26:05 +0000 | [diff] [blame] | 103 | File* file = File::Open(NULL, Options::depfile(), File::kWriteTruncate); |
Zachary Anderson | f06bf4c | 2018-02-07 05:08:45 +0000 | [diff] [blame] | 104 | if (file == NULL) { |
| 105 | ErrorExit(kErrorExitCode, "Error: Unable to open snapshot depfile: %s\n\n", |
Zach Anderson | 00e3109 | 2018-07-31 17:09:31 +0000 | [diff] [blame] | 106 | Options::depfile()); |
Zachary Anderson | f06bf4c | 2018-02-07 05:08:45 +0000 | [diff] [blame] | 107 | } |
| 108 | bool success = true; |
Daco Harkes | 3522358 | 2021-05-21 11:52:02 +0000 | [diff] [blame] | 109 | if (Options::depfile_output_filename() != NULL) { |
Zach Anderson | 00e3109 | 2018-07-31 17:09:31 +0000 | [diff] [blame] | 110 | success &= file->Print("%s: ", Options::depfile_output_filename()); |
Daco Harkes | 3522358 | 2021-05-21 11:52:02 +0000 | [diff] [blame] | 111 | } else { |
| 112 | success &= file->Print("%s: ", Options::snapshot_filename()); |
Zach Anderson | 00e3109 | 2018-07-31 17:09:31 +0000 | [diff] [blame] | 113 | } |
asiva | 355c73d | 2018-09-24 20:38:08 +0000 | [diff] [blame] | 114 | if (kernel_isolate_is_running) { |
| 115 | Dart_KernelCompilationResult result = Dart_KernelListDependencies(); |
| 116 | if (result.status != Dart_KernelCompilationStatus_Ok) { |
| 117 | ErrorExit( |
| 118 | kErrorExitCode, |
| 119 | "Error: Failed to fetch dependencies from kernel service: %s\n\n", |
| 120 | result.error); |
Ryan Macnak | 26c7ab1 | 2018-06-12 22:29:33 +0000 | [diff] [blame] | 121 | } |
asiva | 355c73d | 2018-09-24 20:38:08 +0000 | [diff] [blame] | 122 | success &= file->WriteFully(result.kernel, result.kernel_size); |
| 123 | free(result.kernel); |
Ryan Macnak | 26c7ab1 | 2018-06-12 22:29:33 +0000 | [diff] [blame] | 124 | } |
Zachary Anderson | f06bf4c | 2018-02-07 05:08:45 +0000 | [diff] [blame] | 125 | success &= file->Print("\n"); |
| 126 | if (!success) { |
| 127 | ErrorExit(kErrorExitCode, "Error: Unable to write snapshot depfile: %s\n\n", |
Zach Anderson | 00e3109 | 2018-07-31 17:09:31 +0000 | [diff] [blame] | 128 | Options::depfile()); |
Zachary Anderson | f06bf4c | 2018-02-07 05:08:45 +0000 | [diff] [blame] | 129 | } |
| 130 | file->Release(); |
Zachary Anderson | f06bf4c | 2018-02-07 05:08:45 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Zach Anderson | 00e3109 | 2018-07-31 17:09:31 +0000 | [diff] [blame] | 133 | static void OnExitHook(int64_t exit_code) { |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 134 | if (Dart_CurrentIsolate() != main_isolate) { |
Ryan Macnak | 971f484 | 2019-04-22 20:15:43 +0000 | [diff] [blame] | 135 | Syslog::PrintErr( |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 136 | "A snapshot was requested, but a secondary isolate " |
| 137 | "performed a hard exit (%" Pd64 ").\n", |
| 138 | exit_code); |
Ryan Macnak | 9eb216a | 2017-01-31 09:47:51 -0800 | [diff] [blame] | 139 | Platform::Exit(kErrorExitCode); |
| 140 | } |
Ryan Macnak | 6500f6e | 2017-02-13 11:17:31 -0800 | [diff] [blame] | 141 | if (exit_code == 0) { |
Zach Anderson | 00e3109 | 2018-07-31 17:09:31 +0000 | [diff] [blame] | 142 | if (Options::gen_snapshot_kind() == kAppJIT) { |
| 143 | Snapshot::GenerateAppJIT(Options::snapshot_filename()); |
| 144 | } |
Zachary Anderson | f06bf4c | 2018-02-07 05:08:45 +0000 | [diff] [blame] | 145 | WriteDepsFile(main_isolate); |
Ryan Macnak | 9eb216a | 2017-01-31 09:47:51 -0800 | [diff] [blame] | 146 | } |
Ryan Macnak | 9eb216a | 2017-01-31 09:47:51 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 149 | static Dart_Handle SetupCoreLibraries(Dart_Isolate isolate, |
| 150 | IsolateData* isolate_data, |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 151 | bool is_isolate_group_start, |
| 152 | const char** resolved_packages_config) { |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 153 | auto isolate_group_data = isolate_data->isolate_group_data(); |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 154 | const auto packages_file = isolate_data->packages_file(); |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 155 | const auto script_uri = isolate_group_data->script_url; |
| 156 | |
| 157 | Dart_Handle result; |
| 158 | |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 159 | // Prepare builtin and other core libraries for use to resolve URIs. |
| 160 | // Set up various closures, e.g: printing, timers etc. |
Martin Kustermann | a94b8ae | 2020-05-04 14:51:14 +0000 | [diff] [blame] | 161 | // Set up package configuration for URI resolution. |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 162 | result = DartUtils::PrepareForScriptLoading(false, Options::trace_loading()); |
| 163 | if (Dart_IsError(result)) return result; |
| 164 | |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 165 | // Setup packages config if specified. |
| 166 | result = DartUtils::SetupPackageConfig(packages_file); |
| 167 | if (Dart_IsError(result)) return result; |
| 168 | if (!Dart_IsNull(result) && resolved_packages_config != nullptr) { |
| 169 | result = Dart_StringToCString(result, resolved_packages_config); |
| 170 | if (Dart_IsError(result)) return result; |
| 171 | ASSERT(*resolved_packages_config != nullptr); |
| 172 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 173 | if (is_isolate_group_start) { |
| 174 | isolate_group_data->set_resolved_packages_config( |
| 175 | *resolved_packages_config); |
| 176 | } else { |
| 177 | ASSERT(strcmp(isolate_group_data->resolved_packages_config(), |
| 178 | *resolved_packages_config) == 0); |
| 179 | } |
| 180 | #endif |
| 181 | } |
| 182 | |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 183 | result = Dart_SetEnvironmentCallback(DartUtils::EnvironmentCallback); |
| 184 | if (Dart_IsError(result)) return result; |
| 185 | |
| 186 | // Setup the native resolver as the snapshot does not carry it. |
| 187 | Builtin::SetNativeResolver(Builtin::kBuiltinLibrary); |
| 188 | Builtin::SetNativeResolver(Builtin::kIOLibrary); |
| 189 | Builtin::SetNativeResolver(Builtin::kCLILibrary); |
| 190 | VmService::SetNativeResolver(); |
| 191 | |
| 192 | const char* namespc = |
| 193 | Dart_IsKernelIsolate(isolate) ? NULL : Options::namespc(); |
| 194 | result = |
| 195 | DartUtils::SetupIOLibrary(namespc, script_uri, Options::exit_disabled()); |
| 196 | if (Dart_IsError(result)) return result; |
| 197 | |
| 198 | return Dart_Null(); |
| 199 | } |
| 200 | |
| 201 | static bool OnIsolateInitialize(void** child_callback_data, char** error) { |
| 202 | Dart_Isolate isolate = Dart_CurrentIsolate(); |
| 203 | ASSERT(isolate != nullptr); |
| 204 | |
| 205 | auto isolate_group_data = |
| 206 | reinterpret_cast<IsolateGroupData*>(Dart_CurrentIsolateGroupData()); |
| 207 | |
| 208 | auto isolate_data = new IsolateData(isolate_group_data); |
| 209 | *child_callback_data = isolate_data; |
| 210 | |
| 211 | Dart_EnterScope(); |
| 212 | const auto script_uri = isolate_group_data->script_url; |
| 213 | const bool isolate_run_app_snapshot = |
| 214 | isolate_group_data->RunFromAppSnapshot(); |
| 215 | Dart_Handle result = SetupCoreLibraries(isolate, isolate_data, |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 216 | /*group_start=*/false, |
| 217 | /*resolved_packages_config=*/nullptr); |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 218 | if (Dart_IsError(result)) goto failed; |
| 219 | |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 220 | if (isolate_run_app_snapshot) { |
Ryan Macnak | 9cb5d8e5 | 2020-08-04 19:00:18 +0000 | [diff] [blame] | 221 | result = Loader::InitForSnapshot(script_uri, isolate_data); |
| 222 | if (Dart_IsError(result)) goto failed; |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 223 | } else { |
| 224 | result = DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri)); |
| 225 | if (Dart_IsError(result)) return result != nullptr; |
| 226 | |
| 227 | if (isolate_group_data->kernel_buffer().get() != nullptr) { |
| 228 | // Various core-library parts will send requests to the Loader to resolve |
| 229 | // relative URIs and perform other related tasks. We need Loader to be |
| 230 | // initialized for this to work because loading from Kernel binary |
| 231 | // bypasses normal source code loading paths that initialize it. |
| 232 | const char* resolved_script_uri = NULL; |
| 233 | result = Dart_StringToCString(result, &resolved_script_uri); |
| 234 | if (Dart_IsError(result)) goto failed; |
| 235 | result = Loader::InitForSnapshot(resolved_script_uri, isolate_data); |
| 236 | if (Dart_IsError(result)) goto failed; |
| 237 | } |
| 238 | } |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 239 | |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 240 | Dart_ExitScope(); |
Martin Kustermann | 42ac762 | 2020-11-12 08:49:38 +0000 | [diff] [blame] | 241 | return true; |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 242 | |
| 243 | failed: |
Alexander Aprelev | d33e51d | 2020-06-25 03:36:41 +0000 | [diff] [blame] | 244 | *error = Utils::StrDup(Dart_GetError(result)); |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 245 | Dart_ExitScope(); |
| 246 | return false; |
| 247 | } |
| 248 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 249 | static Dart_Isolate IsolateSetupHelper(Dart_Isolate isolate, |
| 250 | bool is_main_isolate, |
| 251 | const char* script_uri, |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 252 | const char* packages_config, |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 253 | bool isolate_run_app_snapshot, |
asiva | ee6351a | 2018-05-01 22:57:35 +0000 | [diff] [blame] | 254 | Dart_IsolateFlags* flags, |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 255 | char** error, |
| 256 | int* exit_code) { |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 257 | Dart_EnterScope(); |
| 258 | |
Alexander Aprelev | f66964c | 2019-07-23 20:03:42 +0000 | [diff] [blame] | 259 | // Set up the library tag handler for the isolate group shared by all |
| 260 | // isolates in the group. |
| 261 | Dart_Handle result = Dart_SetLibraryTagHandler(Loader::LibraryTagHandler); |
| 262 | CHECK_RESULT(result); |
Ryan Macnak | c5a94db | 2020-07-17 19:29:47 +0000 | [diff] [blame] | 263 | result = Dart_SetDeferredLoadHandler(Loader::DeferredLoadHandler); |
| 264 | CHECK_RESULT(result); |
Alexander Aprelev | f66964c | 2019-07-23 20:03:42 +0000 | [diff] [blame] | 265 | |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 266 | auto isolate_data = reinterpret_cast<IsolateData*>(Dart_IsolateData(isolate)); |
Vyacheslav Egorov | 23fd1a1 | 2016-11-16 13:56:20 +0100 | [diff] [blame] | 267 | |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 268 | const char* resolved_packages_config = nullptr; |
Alexander Aprelev | f66964c | 2019-07-23 20:03:42 +0000 | [diff] [blame] | 269 | result = SetupCoreLibraries(isolate, isolate_data, |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 270 | /*is_isolate_group_start=*/true, |
| 271 | &resolved_packages_config); |
asiva | ee6351a | 2018-05-01 22:57:35 +0000 | [diff] [blame] | 272 | CHECK_RESULT(result); |
| 273 | |
Siva Annamalai | 53cf85a | 2017-07-27 10:06:52 -0700 | [diff] [blame] | 274 | #if !defined(DART_PRECOMPILED_RUNTIME) |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 275 | auto isolate_group_data = isolate_data->isolate_group_data(); |
| 276 | const uint8_t* kernel_buffer = isolate_group_data->kernel_buffer().get(); |
| 277 | intptr_t kernel_buffer_size = isolate_group_data->kernel_buffer_size(); |
asiva | 355c73d | 2018-09-24 20:38:08 +0000 | [diff] [blame] | 278 | if (!isolate_run_app_snapshot && kernel_buffer == NULL && |
| 279 | !Dart_IsKernelIsolate(isolate)) { |
asiva | ee6351a | 2018-05-01 22:57:35 +0000 | [diff] [blame] | 280 | if (!dfe.CanUseDartFrontend()) { |
| 281 | const char* format = "Dart frontend unavailable to compile script %s."; |
| 282 | intptr_t len = snprintf(NULL, 0, format, script_uri) + 1; |
| 283 | *error = reinterpret_cast<char*>(malloc(len)); |
| 284 | ASSERT(error != NULL); |
| 285 | snprintf(*error, len, format, script_uri); |
| 286 | *exit_code = kErrorExitCode; |
| 287 | Dart_ExitScope(); |
| 288 | Dart_ShutdownIsolate(); |
| 289 | return NULL; |
| 290 | } |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 291 | uint8_t* application_kernel_buffer = NULL; |
| 292 | intptr_t application_kernel_buffer_size = 0; |
Johnni Winther | 55b34a4 | 2021-01-21 21:18:32 +0000 | [diff] [blame] | 293 | // Only pass snapshot = true when generating an AppJIT snapshot to avoid |
| 294 | // duplicate null-safety info messages from the frontend when generating |
| 295 | // a kernel snapshot (this flag is instead set in |
| 296 | // Snapshot::GenerateKernel()). |
| 297 | const bool snapshot = Options::gen_snapshot_kind() == kAppJIT; |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 298 | dfe.CompileAndReadScript(script_uri, &application_kernel_buffer, |
| 299 | &application_kernel_buffer_size, error, exit_code, |
Johnni Winther | 55b34a4 | 2021-01-21 21:18:32 +0000 | [diff] [blame] | 300 | resolved_packages_config, snapshot); |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 301 | if (application_kernel_buffer == NULL) { |
asiva | ee6351a | 2018-05-01 22:57:35 +0000 | [diff] [blame] | 302 | Dart_ExitScope(); |
| 303 | Dart_ShutdownIsolate(); |
| 304 | return NULL; |
| 305 | } |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 306 | isolate_group_data->SetKernelBufferNewlyOwned( |
| 307 | application_kernel_buffer, application_kernel_buffer_size); |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 308 | kernel_buffer = application_kernel_buffer; |
| 309 | kernel_buffer_size = application_kernel_buffer_size; |
asiva | ee6351a | 2018-05-01 22:57:35 +0000 | [diff] [blame] | 310 | } |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 311 | if (kernel_buffer != NULL) { |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 312 | Dart_Handle uri = Dart_NewStringFromCString(script_uri); |
| 313 | CHECK_RESULT(uri); |
| 314 | Dart_Handle resolved_script_uri = DartUtils::ResolveScript(uri); |
| 315 | CHECK_RESULT(resolved_script_uri); |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 316 | result = Dart_LoadScriptFromKernel(kernel_buffer, kernel_buffer_size); |
Siva Chandra | b47a5b2 | 2018-01-24 01:40:37 +0000 | [diff] [blame] | 317 | CHECK_RESULT(result); |
Vyacheslav Egorov | 23fd1a1 | 2016-11-16 13:56:20 +0100 | [diff] [blame] | 318 | } |
Siva Annamalai | 53cf85a | 2017-07-27 10:06:52 -0700 | [diff] [blame] | 319 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
| 320 | |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 321 | if (isolate_run_app_snapshot) { |
Ryan Macnak | 9cb5d8e5 | 2020-08-04 19:00:18 +0000 | [diff] [blame] | 322 | Dart_Handle result = Loader::InitForSnapshot(script_uri, isolate_data); |
| 323 | CHECK_RESULT(result); |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 324 | #if !defined(DART_PRECOMPILED_RUNTIME) |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 325 | if (is_main_isolate) { |
| 326 | // Find the canonical uri of the app snapshot. We'll use this to decide if |
| 327 | // other isolates should use the app snapshot or the core snapshot. |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 328 | const char* resolved_script_uri = NULL; |
| 329 | result = Dart_StringToCString( |
| 330 | DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri)), |
| 331 | &resolved_script_uri); |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 332 | CHECK_RESULT(result); |
| 333 | ASSERT(app_script_uri == NULL); |
Alexander Aprelev | d33e51d | 2020-06-25 03:36:41 +0000 | [diff] [blame] | 334 | app_script_uri = Utils::StrDup(resolved_script_uri); |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 335 | } |
asiva | 03777db | 2020-06-20 05:50:49 +0000 | [diff] [blame] | 336 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 337 | } else { |
| 338 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 339 | // Load the specified application script into the newly created isolate. |
| 340 | Dart_Handle uri = |
| 341 | DartUtils::ResolveScript(Dart_NewStringFromCString(script_uri)); |
| 342 | CHECK_RESULT(uri); |
| 343 | if (kernel_buffer != NULL) { |
| 344 | // relative URIs and perform other related tasks. We need Loader to be |
| 345 | // initialized for this to work because loading from Kernel binary |
| 346 | // bypasses normal source code loading paths that initialize it. |
| 347 | const char* resolved_script_uri = NULL; |
| 348 | result = Dart_StringToCString(uri, &resolved_script_uri); |
| 349 | CHECK_RESULT(result); |
| 350 | result = Loader::InitForSnapshot(resolved_script_uri, isolate_data); |
| 351 | CHECK_RESULT(result); |
| 352 | } |
| 353 | Dart_TimelineEvent("LoadScript", Dart_TimelineGetMicros(), |
| 354 | Dart_GetMainPortId(), Dart_Timeline_Event_Async_End, 0, |
| 355 | NULL, NULL); |
| 356 | #else |
| 357 | UNREACHABLE(); |
| 358 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
| 359 | } |
John McCutchan | 1ff7144 | 2015-08-18 15:18:02 -0700 | [diff] [blame] | 360 | |
Ryan Macnak | be9bb43 | 2021-08-30 20:26:57 +0000 | [diff] [blame] | 361 | if (Options::gen_snapshot_kind() == kAppJIT && !isolate_run_app_snapshot) { |
Ryan Macnak | d253642 | 2019-02-21 22:39:39 +0000 | [diff] [blame] | 362 | // If we sort, we must do it for all isolates, not just the main isolate, |
| 363 | // otherwise isolates related by spawnFunction will disagree on CIDs and |
Ryan Macnak | be9bb43 | 2021-08-30 20:26:57 +0000 | [diff] [blame] | 364 | // cannot correctly send each other messages. If we run from an app |
| 365 | // snapshot, things are already sorted, and other isolate created by |
| 366 | // spawnFunction will also load from the same snapshot. Sorting such isolate |
| 367 | // is counter-productive because it invalidates their code. |
| 368 | // After we switch to always using isolate groups, this be changed to |
| 369 | // `generating-app-jit && is_main_isolate`. |
Ryan Macnak | d253642 | 2019-02-21 22:39:39 +0000 | [diff] [blame] | 370 | result = Dart_SortClasses(); |
| 371 | CHECK_RESULT(result); |
| 372 | } |
| 373 | |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 374 | // Disable pausing the DartDev isolate on start and exit. |
| 375 | const char* isolate_name = nullptr; |
| 376 | result = Dart_StringToCString(Dart_DebugName(), &isolate_name); |
| 377 | CHECK_RESULT(result); |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 378 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 379 | if (strstr(isolate_name, DART_DEV_ISOLATE_NAME) != nullptr) { |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 380 | Dart_SetShouldPauseOnStart(false); |
| 381 | Dart_SetShouldPauseOnExit(false); |
| 382 | } |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 383 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 384 | |
iposva@google.com | 0d0fcbe | 2013-04-09 17:51:24 +0000 | [diff] [blame] | 385 | // Make the isolate runnable so that it is ready to handle messages. |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 386 | Dart_ExitScope(); |
iposva@google.com | 0d0fcbe | 2013-04-09 17:51:24 +0000 | [diff] [blame] | 387 | Dart_ExitIsolate(); |
Ryan Macnak | 607f4f5 | 2018-05-01 20:54:38 +0000 | [diff] [blame] | 388 | *error = Dart_IsolateMakeRunnable(isolate); |
| 389 | if (*error != NULL) { |
iposva@google.com | 0d0fcbe | 2013-04-09 17:51:24 +0000 | [diff] [blame] | 390 | Dart_EnterIsolate(isolate); |
| 391 | Dart_ShutdownIsolate(); |
| 392 | return NULL; |
| 393 | } |
| 394 | |
iposva@google.com | 0d0fcbe | 2013-04-09 17:51:24 +0000 | [diff] [blame] | 395 | return isolate; |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 396 | } |
| 397 | |
asiva | 1aaebed | 2018-02-28 01:15:47 +0000 | [diff] [blame] | 398 | #if !defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM) |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 399 | // Returns newly created Kernel Isolate on success, NULL on failure. |
| 400 | // For now we only support the kernel isolate coming up from an |
Siva Chandra | 4b73d12 | 2018-01-12 20:40:41 +0000 | [diff] [blame] | 401 | // application snapshot or from a .dill file. |
| 402 | static Dart_Isolate CreateAndSetupKernelIsolate(const char* script_uri, |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 403 | const char* packages_config, |
| 404 | Dart_IsolateFlags* flags, |
| 405 | char** error, |
| 406 | int* exit_code) { |
asiva | 1f5f197 | 2018-08-20 21:32:21 +0000 | [diff] [blame] | 407 | // Do not start a kernel isolate if we are doing a training run |
| 408 | // to create an app JIT snapshot and a kernel file is specified |
| 409 | // as the application to run. |
| 410 | if (Options::gen_snapshot_kind() == kAppJIT) { |
| 411 | const uint8_t* kernel_buffer = NULL; |
| 412 | intptr_t kernel_buffer_size = 0; |
| 413 | dfe.application_kernel_buffer(&kernel_buffer, &kernel_buffer_size); |
| 414 | if (kernel_buffer_size != 0) { |
| 415 | return NULL; |
| 416 | } |
Vyacheslav Egorov | 0320376 | 2018-08-19 19:58:32 +0000 | [diff] [blame] | 417 | } |
asiva | 1f5f197 | 2018-08-20 21:32:21 +0000 | [diff] [blame] | 418 | // Create and Start the kernel isolate. |
| 419 | const char* kernel_snapshot_uri = dfe.frontend_filename(); |
Siva Chandra | 4b73d12 | 2018-01-12 20:40:41 +0000 | [diff] [blame] | 420 | const char* uri = |
| 421 | kernel_snapshot_uri != NULL ? kernel_snapshot_uri : script_uri; |
| 422 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 423 | if (packages_config == NULL) { |
Zachary Anderson | d7845c6 | 2017-09-11 18:58:39 +0000 | [diff] [blame] | 424 | packages_config = Options::packages_file(); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 425 | } |
| 426 | |
asiva | cd3dded | 2018-09-07 21:22:13 +0000 | [diff] [blame] | 427 | Dart_Isolate isolate = NULL; |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 428 | IsolateGroupData* isolate_group_data = nullptr; |
| 429 | IsolateData* isolate_data = nullptr; |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 430 | bool isolate_run_app_snapshot = false; |
asiva | d1bd887 | 2018-08-21 20:28:06 +0000 | [diff] [blame] | 431 | AppSnapshot* app_snapshot = NULL; |
| 432 | // Kernel isolate uses an app snapshot or uses the dill file. |
| 433 | if ((kernel_snapshot_uri != NULL) && |
Ben Konyi | 4de9e80 | 2021-10-27 16:34:00 +0000 | [diff] [blame] | 434 | (app_snapshot = Snapshot::TryReadAppSnapshot( |
| 435 | kernel_snapshot_uri, /*force_load_elf_from_memory=*/false, |
| 436 | /*decode_uri=*/false)) != nullptr) { |
asiva | d1bd887 | 2018-08-21 20:28:06 +0000 | [diff] [blame] | 437 | const uint8_t* isolate_snapshot_data = NULL; |
| 438 | const uint8_t* isolate_snapshot_instructions = NULL; |
| 439 | const uint8_t* ignore_vm_snapshot_data; |
| 440 | const uint8_t* ignore_vm_snapshot_instructions; |
| 441 | isolate_run_app_snapshot = true; |
| 442 | app_snapshot->SetBuffers( |
| 443 | &ignore_vm_snapshot_data, &ignore_vm_snapshot_instructions, |
| 444 | &isolate_snapshot_data, &isolate_snapshot_instructions); |
Martin Kustermann | a94b8ae | 2020-05-04 14:51:14 +0000 | [diff] [blame] | 445 | isolate_group_data = new IsolateGroupData( |
| 446 | uri, packages_config, app_snapshot, isolate_run_app_snapshot); |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 447 | isolate_data = new IsolateData(isolate_group_data); |
| 448 | isolate = Dart_CreateIsolateGroup( |
Ben Konyi | ac2c934 | 2019-03-28 16:39:50 +0000 | [diff] [blame] | 449 | DART_KERNEL_ISOLATE_NAME, DART_KERNEL_ISOLATE_NAME, |
Ryan Macnak | 6a65ea9 | 2019-10-22 19:02:26 +0000 | [diff] [blame] | 450 | isolate_snapshot_data, isolate_snapshot_instructions, flags, |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 451 | isolate_group_data, isolate_data, error); |
asiva | cd3dded | 2018-09-07 21:22:13 +0000 | [diff] [blame] | 452 | } |
| 453 | if (isolate == NULL) { |
Ryan Macnak | c8bda47 | 2018-10-08 22:43:09 +0000 | [diff] [blame] | 454 | // Clear error from app snapshot and re-trying from kernel file. |
| 455 | free(*error); |
| 456 | *error = NULL; |
Martin Kustermann | b2171f1 | 2019-06-26 16:04:27 +0000 | [diff] [blame] | 457 | delete isolate_data; |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 458 | delete isolate_group_data; |
Ryan Macnak | c8bda47 | 2018-10-08 22:43:09 +0000 | [diff] [blame] | 459 | |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 460 | const uint8_t* kernel_service_buffer = NULL; |
| 461 | intptr_t kernel_service_buffer_size = 0; |
| 462 | dfe.LoadKernelService(&kernel_service_buffer, &kernel_service_buffer_size); |
| 463 | ASSERT(kernel_service_buffer != NULL); |
Martin Kustermann | a94b8ae | 2020-05-04 14:51:14 +0000 | [diff] [blame] | 464 | isolate_group_data = new IsolateGroupData(uri, packages_config, nullptr, |
| 465 | isolate_run_app_snapshot); |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 466 | isolate_group_data->SetKernelBufferUnowned( |
Alexander Aprelev | 7d14785 | 2018-12-14 23:45:57 +0000 | [diff] [blame] | 467 | const_cast<uint8_t*>(kernel_service_buffer), |
| 468 | kernel_service_buffer_size); |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 469 | isolate_data = new IsolateData(isolate_group_data); |
| 470 | isolate = Dart_CreateIsolateGroupFromKernel( |
Ben Konyi | ac2c934 | 2019-03-28 16:39:50 +0000 | [diff] [blame] | 471 | DART_KERNEL_ISOLATE_NAME, DART_KERNEL_ISOLATE_NAME, |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 472 | kernel_service_buffer, kernel_service_buffer_size, flags, |
| 473 | isolate_group_data, isolate_data, error); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 476 | if (isolate == NULL) { |
Ryan Macnak | 971f484 | 2019-04-22 20:15:43 +0000 | [diff] [blame] | 477 | Syslog::PrintErr("%s\n", *error); |
Martin Kustermann | b2171f1 | 2019-06-26 16:04:27 +0000 | [diff] [blame] | 478 | delete isolate_data; |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 479 | delete isolate_group_data; |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 480 | return NULL; |
| 481 | } |
asiva | d1bd887 | 2018-08-21 20:28:06 +0000 | [diff] [blame] | 482 | kernel_isolate_is_running = true; |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 483 | |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 484 | return IsolateSetupHelper(isolate, false, uri, packages_config, |
asiva | e70a7aa | 2018-10-10 23:19:24 +0000 | [diff] [blame] | 485 | isolate_run_app_snapshot, flags, error, exit_code); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 486 | } |
asiva | 1aaebed | 2018-02-28 01:15:47 +0000 | [diff] [blame] | 487 | #endif // !defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM) |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 488 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 489 | // Returns newly created Service Isolate on success, NULL on failure. |
| 490 | // For now we only support the service isolate coming up from sources |
| 491 | // which are compiled by the VM parser. |
| 492 | static Dart_Isolate CreateAndSetupServiceIsolate(const char* script_uri, |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 493 | const char* packages_config, |
| 494 | Dart_IsolateFlags* flags, |
| 495 | char** error, |
| 496 | int* exit_code) { |
asiva | 8e4421a | 2019-12-27 19:46:15 +0000 | [diff] [blame] | 497 | #if !defined(PRODUCT) |
Alexander Markov | 6b4fe2f | 2020-07-01 20:22:24 +0000 | [diff] [blame] | 498 | ASSERT(script_uri != nullptr); |
| 499 | Dart_Isolate isolate = nullptr; |
Martin Kustermann | a94b8ae | 2020-05-04 14:51:14 +0000 | [diff] [blame] | 500 | auto isolate_group_data = |
| 501 | new IsolateGroupData(script_uri, packages_config, nullptr, false); |
Alexander Markov | 6b4fe2f | 2020-07-01 20:22:24 +0000 | [diff] [blame] | 502 | ASSERT(flags != nullptr); |
asiva | 76470ac | 2018-09-27 00:55:07 +0000 | [diff] [blame] | 503 | |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 504 | #if defined(DART_PRECOMPILED_RUNTIME) |
asiva | 76470ac | 2018-09-27 00:55:07 +0000 | [diff] [blame] | 505 | // AOT: All isolates start from the app snapshot. |
| 506 | const uint8_t* isolate_snapshot_data = app_isolate_snapshot_data; |
| 507 | const uint8_t* isolate_snapshot_instructions = |
| 508 | app_isolate_snapshot_instructions; |
Alexander Markov | 6b4fe2f | 2020-07-01 20:22:24 +0000 | [diff] [blame] | 509 | flags->null_safety = |
| 510 | Dart_DetectNullSafety(nullptr, nullptr, nullptr, isolate_snapshot_data, |
| 511 | isolate_snapshot_instructions, nullptr, -1); |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 512 | isolate = Dart_CreateIsolateGroup( |
Ben Konyi | ac2c934 | 2019-03-28 16:39:50 +0000 | [diff] [blame] | 513 | script_uri, DART_VM_SERVICE_ISOLATE_NAME, isolate_snapshot_data, |
Ryan Macnak | 6a65ea9 | 2019-10-22 19:02:26 +0000 | [diff] [blame] | 514 | isolate_snapshot_instructions, flags, isolate_group_data, |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 515 | /*isolate_data=*/nullptr, error); |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 516 | #else |
asiva | 76470ac | 2018-09-27 00:55:07 +0000 | [diff] [blame] | 517 | // JIT: Service isolate uses the core libraries snapshot. |
| 518 | |
asiva | e70a7aa | 2018-10-10 23:19:24 +0000 | [diff] [blame] | 519 | // Set flag to load and retain the vmservice library. |
Siva Chandra | 8d7bc50 | 2018-01-11 11:12:04 -0800 | [diff] [blame] | 520 | flags->load_vmservice_library = true; |
Alexander Markov | 74792f4 | 2018-10-22 20:51:48 +0000 | [diff] [blame] | 521 | const uint8_t* isolate_snapshot_data = core_isolate_snapshot_data; |
| 522 | const uint8_t* isolate_snapshot_instructions = |
| 523 | core_isolate_snapshot_instructions; |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 524 | isolate = Dart_CreateIsolateGroup( |
Ben Konyi | ac2c934 | 2019-03-28 16:39:50 +0000 | [diff] [blame] | 525 | script_uri, DART_VM_SERVICE_ISOLATE_NAME, isolate_snapshot_data, |
Ryan Macnak | 6a65ea9 | 2019-10-22 19:02:26 +0000 | [diff] [blame] | 526 | isolate_snapshot_instructions, flags, isolate_group_data, |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 527 | /*isolate_data=*/nullptr, error); |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 528 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 529 | if (isolate == NULL) { |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 530 | delete isolate_group_data; |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 531 | return NULL; |
| 532 | } |
| 533 | |
| 534 | Dart_EnterScope(); |
| 535 | |
| 536 | Dart_Handle result = Dart_SetLibraryTagHandler(Loader::LibraryTagHandler); |
| 537 | CHECK_RESULT(result); |
Ryan Macnak | c5a94db | 2020-07-17 19:29:47 +0000 | [diff] [blame] | 538 | result = Dart_SetDeferredLoadHandler(Loader::DeferredLoadHandler); |
| 539 | CHECK_RESULT(result); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 540 | |
Ben Konyi | f0d88cc | 2020-09-03 15:20:14 +0000 | [diff] [blame] | 541 | int vm_service_server_port = INVALID_VM_SERVICE_SERVER_PORT; |
Ben Konyi | 123950f | 2021-09-16 20:31:45 +0000 | [diff] [blame] | 542 | if (Options::disable_dart_dev() || Options::disable_dds()) { |
Ben Konyi | f0d88cc | 2020-09-03 15:20:14 +0000 | [diff] [blame] | 543 | vm_service_server_port = Options::vm_service_server_port(); |
| 544 | } else if (Options::vm_service_server_port() != |
| 545 | INVALID_VM_SERVICE_SERVER_PORT) { |
| 546 | vm_service_server_port = 0; |
| 547 | } |
| 548 | |
Ben Konyi | b99466d | 2021-05-11 17:55:26 +0000 | [diff] [blame] | 549 | // We do not want to wait for DDS to advertise availability of VM service in |
| 550 | // the following scenarios: |
| 551 | // - The DartDev CLI is disabled (CLI isolate starts DDS) and VM service is |
| 552 | // enabled. |
Ben Konyi | 123950f | 2021-09-16 20:31:45 +0000 | [diff] [blame] | 553 | // - DDS is disabled. |
Ben Konyi | b99466d | 2021-05-11 17:55:26 +0000 | [diff] [blame] | 554 | // TODO(bkonyi): do we want to tie DevTools / DDS to the CLI in the long run? |
Ben Konyi | 123950f | 2021-09-16 20:31:45 +0000 | [diff] [blame] | 555 | bool wait_for_dds_to_advertise_service = |
| 556 | !(Options::disable_dart_dev() || Options::disable_dds()); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 557 | // Load embedder specific bits and return. |
Ben Konyi | c3da025 | 2020-09-16 18:38:28 +0000 | [diff] [blame] | 558 | if (!VmService::Setup( |
Ben Konyi | 123950f | 2021-09-16 20:31:45 +0000 | [diff] [blame] | 559 | !wait_for_dds_to_advertise_service ? Options::vm_service_server_ip() |
| 560 | : DEFAULT_VM_SERVICE_SERVER_IP, |
Ben Konyi | c3da025 | 2020-09-16 18:38:28 +0000 | [diff] [blame] | 561 | vm_service_server_port, Options::vm_service_dev_mode(), |
| 562 | Options::vm_service_auth_disabled(), |
| 563 | Options::vm_write_service_info_filename(), Options::trace_loading(), |
| 564 | Options::deterministic(), Options::enable_service_port_fallback(), |
Ben Konyi | 4faadf1 | 2021-03-24 23:59:23 +0000 | [diff] [blame] | 565 | wait_for_dds_to_advertise_service)) { |
Alexander Aprelev | d33e51d | 2020-06-25 03:36:41 +0000 | [diff] [blame] | 566 | *error = Utils::StrDup(VmService::GetErrorMessage()); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 567 | return NULL; |
| 568 | } |
Zachary Anderson | d7845c6 | 2017-09-11 18:58:39 +0000 | [diff] [blame] | 569 | if (Options::compile_all()) { |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 570 | result = Dart_CompileAll(); |
| 571 | CHECK_RESULT(result); |
| 572 | } |
Zach Anderson | 27c79f9 | 2018-09-06 02:23:21 +0000 | [diff] [blame] | 573 | result = Dart_SetEnvironmentCallback(DartUtils::EnvironmentCallback); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 574 | CHECK_RESULT(result); |
| 575 | Dart_ExitScope(); |
| 576 | Dart_ExitIsolate(); |
| 577 | return isolate; |
asiva | 8e4421a | 2019-12-27 19:46:15 +0000 | [diff] [blame] | 578 | #else // !defined(PRODUCT) |
| 579 | return NULL; |
| 580 | #endif // !defined(PRODUCT) |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 583 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 584 | |
| 585 | static Dart_Isolate CreateAndSetupDartDevIsolate(const char* script_uri, |
| 586 | const char* packages_config, |
| 587 | Dart_IsolateFlags* flags, |
| 588 | char** error, |
| 589 | int* exit_code) { |
| 590 | int64_t start = Dart_TimelineGetMicros(); |
| 591 | |
| 592 | auto dartdev_path = DartDevIsolate::TryResolveDartDevSnapshotPath(); |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 593 | |
Ben Konyi | c42c76f | 2021-01-11 19:41:50 +0000 | [diff] [blame] | 594 | Dart_Isolate isolate = nullptr; |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 595 | const uint8_t* isolate_snapshot_data = core_isolate_snapshot_data; |
| 596 | const uint8_t* isolate_snapshot_instructions = |
| 597 | core_isolate_snapshot_instructions; |
Ben Konyi | c42c76f | 2021-01-11 19:41:50 +0000 | [diff] [blame] | 598 | IsolateGroupData* isolate_group_data = nullptr; |
| 599 | IsolateData* isolate_data = nullptr; |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 600 | |
Ben Konyi | fd259d4 | 2021-01-11 23:25:28 +0000 | [diff] [blame] | 601 | if (error != nullptr) { |
| 602 | *error = nullptr; |
| 603 | } |
| 604 | AppSnapshot* app_snapshot = nullptr; |
Ben Konyi | c42c76f | 2021-01-11 19:41:50 +0000 | [diff] [blame] | 605 | bool isolate_run_app_snapshot = true; |
| 606 | if (dartdev_path.get() != nullptr && |
Ben Konyi | 4de9e80 | 2021-10-27 16:34:00 +0000 | [diff] [blame] | 607 | (app_snapshot = Snapshot::TryReadAppSnapshot( |
| 608 | dartdev_path.get(), /*force_load_elf_from_memory=*/false, |
| 609 | /*decode_uri=*/false)) != nullptr) { |
Ben Konyi | c42c76f | 2021-01-11 19:41:50 +0000 | [diff] [blame] | 610 | const uint8_t* isolate_snapshot_data = NULL; |
| 611 | const uint8_t* isolate_snapshot_instructions = NULL; |
| 612 | const uint8_t* ignore_vm_snapshot_data; |
| 613 | const uint8_t* ignore_vm_snapshot_instructions; |
| 614 | app_snapshot->SetBuffers( |
| 615 | &ignore_vm_snapshot_data, &ignore_vm_snapshot_instructions, |
| 616 | &isolate_snapshot_data, &isolate_snapshot_instructions); |
| 617 | isolate_group_data = |
Ben Konyi | 4de9e80 | 2021-10-27 16:34:00 +0000 | [diff] [blame] | 618 | new IsolateGroupData(DART_DEV_ISOLATE_NAME, packages_config, |
| 619 | app_snapshot, isolate_run_app_snapshot); |
Ben Konyi | c42c76f | 2021-01-11 19:41:50 +0000 | [diff] [blame] | 620 | isolate_data = new IsolateData(isolate_group_data); |
| 621 | isolate = Dart_CreateIsolateGroup( |
| 622 | DART_DEV_ISOLATE_NAME, DART_DEV_ISOLATE_NAME, isolate_snapshot_data, |
| 623 | isolate_snapshot_instructions, flags, isolate_group_data, isolate_data, |
| 624 | error); |
| 625 | } |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 626 | |
Ben Konyi | c42c76f | 2021-01-11 19:41:50 +0000 | [diff] [blame] | 627 | if (isolate == nullptr) { |
| 628 | isolate_run_app_snapshot = false; |
| 629 | dartdev_path = DartDevIsolate::TryResolveDartDevKernelPath(); |
| 630 | // Clear error from app snapshot and retry from kernel. |
Ben Konyi | cbc4601 | 2021-01-12 18:53:05 +0000 | [diff] [blame] | 631 | if (error != nullptr && *error != nullptr) { |
Ben Konyi | fd259d4 | 2021-01-11 23:25:28 +0000 | [diff] [blame] | 632 | free(*error); |
| 633 | *error = nullptr; |
| 634 | } |
Ben Konyi | c42c76f | 2021-01-11 19:41:50 +0000 | [diff] [blame] | 635 | |
| 636 | if (app_snapshot != nullptr) { |
| 637 | delete app_snapshot; |
| 638 | } |
| 639 | |
| 640 | if (dartdev_path.get() != nullptr) { |
| 641 | isolate_group_data = |
| 642 | new IsolateGroupData(DART_DEV_ISOLATE_NAME, packages_config, nullptr, |
| 643 | isolate_run_app_snapshot); |
| 644 | uint8_t* application_kernel_buffer = NULL; |
| 645 | intptr_t application_kernel_buffer_size = 0; |
| 646 | dfe.ReadScript(dartdev_path.get(), &application_kernel_buffer, |
Ben Konyi | 4de9e80 | 2021-10-27 16:34:00 +0000 | [diff] [blame] | 647 | &application_kernel_buffer_size, /*decode_uri=*/false); |
Ben Konyi | c42c76f | 2021-01-11 19:41:50 +0000 | [diff] [blame] | 648 | isolate_group_data->SetKernelBufferNewlyOwned( |
| 649 | application_kernel_buffer, application_kernel_buffer_size); |
| 650 | |
| 651 | isolate_data = new IsolateData(isolate_group_data); |
| 652 | isolate = Dart_CreateIsolateGroup( |
| 653 | DART_DEV_ISOLATE_NAME, DART_DEV_ISOLATE_NAME, isolate_snapshot_data, |
| 654 | isolate_snapshot_instructions, flags, isolate_group_data, |
| 655 | isolate_data, error); |
| 656 | } |
| 657 | } |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 658 | |
| 659 | Dart_Isolate created_isolate = nullptr; |
| 660 | if (isolate == nullptr) { |
Ben Konyi | c42c76f | 2021-01-11 19:41:50 +0000 | [diff] [blame] | 661 | Syslog::PrintErr("Failed to start the Dart CLI isolate\n"); |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 662 | delete isolate_data; |
| 663 | delete isolate_group_data; |
Ben Konyi | c42c76f | 2021-01-11 19:41:50 +0000 | [diff] [blame] | 664 | return nullptr; |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 665 | } else { |
| 666 | created_isolate = IsolateSetupHelper( |
| 667 | isolate, false, DART_DEV_ISOLATE_NAME, packages_config, |
Ben Konyi | c42c76f | 2021-01-11 19:41:50 +0000 | [diff] [blame] | 668 | isolate_run_app_snapshot, flags, error, exit_code); |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 669 | } |
| 670 | int64_t end = Dart_TimelineGetMicros(); |
| 671 | Dart_TimelineEvent("CreateAndSetupDartDevIsolate", start, end, |
| 672 | Dart_Timeline_Event_Duration, 0, NULL, NULL); |
| 673 | return created_isolate; |
| 674 | } |
| 675 | |
| 676 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
| 677 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 678 | // Returns newly created Isolate on success, NULL on failure. |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 679 | static Dart_Isolate CreateIsolateGroupAndSetupHelper( |
| 680 | bool is_main_isolate, |
| 681 | const char* script_uri, |
| 682 | const char* name, |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 683 | const char* packages_config, |
| 684 | Dart_IsolateFlags* flags, |
| 685 | void* callback_data, |
| 686 | char** error, |
| 687 | int* exit_code) { |
Alexander Aprelev | 85ee5ad | 2018-04-04 23:56:56 +0000 | [diff] [blame] | 688 | int64_t start = Dart_TimelineGetMicros(); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 689 | ASSERT(script_uri != NULL); |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 690 | uint8_t* kernel_buffer = NULL; |
Alexander Aprelev | 7d14785 | 2018-12-14 23:45:57 +0000 | [diff] [blame] | 691 | std::shared_ptr<uint8_t> parent_kernel_buffer; |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 692 | intptr_t kernel_buffer_size = 0; |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 693 | AppSnapshot* app_snapshot = NULL; |
| 694 | |
| 695 | #if defined(DART_PRECOMPILED_RUNTIME) |
| 696 | // AOT: All isolates start from the app snapshot. |
| 697 | bool isolate_run_app_snapshot = true; |
| 698 | const uint8_t* isolate_snapshot_data = app_isolate_snapshot_data; |
| 699 | const uint8_t* isolate_snapshot_instructions = |
| 700 | app_isolate_snapshot_instructions; |
asiva | a782dd8 | 2020-06-11 01:22:35 +0000 | [diff] [blame] | 701 | flags->null_safety = |
| 702 | Dart_DetectNullSafety(nullptr, nullptr, nullptr, isolate_snapshot_data, |
| 703 | isolate_snapshot_instructions, nullptr, -1); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 704 | #else |
| 705 | // JIT: Main isolate starts from the app snapshot, if any. Other isolates |
| 706 | // use the core libraries snapshot. |
| 707 | bool isolate_run_app_snapshot = false; |
| 708 | const uint8_t* isolate_snapshot_data = core_isolate_snapshot_data; |
| 709 | const uint8_t* isolate_snapshot_instructions = |
| 710 | core_isolate_snapshot_instructions; |
| 711 | if ((app_isolate_snapshot_data != NULL) && |
| 712 | (is_main_isolate || ((app_script_uri != NULL) && |
| 713 | (strcmp(script_uri, app_script_uri) == 0)))) { |
| 714 | isolate_run_app_snapshot = true; |
| 715 | isolate_snapshot_data = app_isolate_snapshot_data; |
| 716 | isolate_snapshot_instructions = app_isolate_snapshot_instructions; |
| 717 | } else if (!is_main_isolate) { |
| 718 | app_snapshot = Snapshot::TryReadAppSnapshot(script_uri); |
| 719 | if (app_snapshot != NULL) { |
| 720 | isolate_run_app_snapshot = true; |
| 721 | const uint8_t* ignore_vm_snapshot_data; |
| 722 | const uint8_t* ignore_vm_snapshot_instructions; |
| 723 | app_snapshot->SetBuffers( |
| 724 | &ignore_vm_snapshot_data, &ignore_vm_snapshot_instructions, |
| 725 | &isolate_snapshot_data, &isolate_snapshot_instructions); |
| 726 | } |
| 727 | } |
Alexander Aprelev | 7d14785 | 2018-12-14 23:45:57 +0000 | [diff] [blame] | 728 | |
Martin Kustermann | a64b06e | 2019-09-05 21:41:42 +0000 | [diff] [blame] | 729 | if (flags->copy_parent_code && callback_data != nullptr) { |
Martin Kustermann | 68e805f | 2019-08-23 15:15:58 +0000 | [diff] [blame] | 730 | auto parent_isolate_group_data = |
| 731 | reinterpret_cast<IsolateData*>(callback_data)->isolate_group_data(); |
| 732 | parent_kernel_buffer = parent_isolate_group_data->kernel_buffer(); |
Alexander Aprelev | 7d14785 | 2018-12-14 23:45:57 +0000 | [diff] [blame] | 733 | kernel_buffer = parent_kernel_buffer.get(); |
Martin Kustermann | 68e805f | 2019-08-23 15:15:58 +0000 | [diff] [blame] | 734 | kernel_buffer_size = parent_isolate_group_data->kernel_buffer_size(); |
Alexander Aprelev | 7d14785 | 2018-12-14 23:45:57 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | if (kernel_buffer == NULL && !isolate_run_app_snapshot) { |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 738 | dfe.ReadScript(script_uri, &kernel_buffer, &kernel_buffer_size); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 739 | } |
asiva | a782dd8 | 2020-06-11 01:22:35 +0000 | [diff] [blame] | 740 | PathSanitizer script_uri_sanitizer(script_uri); |
| 741 | PathSanitizer packages_config_sanitizer(packages_config); |
| 742 | flags->null_safety = Dart_DetectNullSafety( |
| 743 | script_uri_sanitizer.sanitized_uri(), |
| 744 | packages_config_sanitizer.sanitized_uri(), |
| 745 | DartUtils::original_working_directory, isolate_snapshot_data, |
| 746 | isolate_snapshot_instructions, kernel_buffer, kernel_buffer_size); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 747 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
| 748 | |
Martin Kustermann | a94b8ae | 2020-05-04 14:51:14 +0000 | [diff] [blame] | 749 | auto isolate_group_data = new IsolateGroupData( |
| 750 | script_uri, packages_config, app_snapshot, isolate_run_app_snapshot); |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 751 | if (kernel_buffer != NULL) { |
Alexander Aprelev | 7d14785 | 2018-12-14 23:45:57 +0000 | [diff] [blame] | 752 | if (parent_kernel_buffer) { |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 753 | isolate_group_data->SetKernelBufferAlreadyOwned( |
| 754 | std::move(parent_kernel_buffer), kernel_buffer_size); |
Alexander Aprelev | 7d14785 | 2018-12-14 23:45:57 +0000 | [diff] [blame] | 755 | } else { |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 756 | isolate_group_data->SetKernelBufferNewlyOwned(kernel_buffer, |
| 757 | kernel_buffer_size); |
Alexander Aprelev | 7d14785 | 2018-12-14 23:45:57 +0000 | [diff] [blame] | 758 | } |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 759 | } |
Siva Chandra | 71dba8a | 2018-02-01 15:11:29 +0000 | [diff] [blame] | 760 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 761 | Dart_Isolate isolate = NULL; |
Ben Konyi | 6338914 | 2018-02-07 19:35:39 +0000 | [diff] [blame] | 762 | |
Martin Kustermann | b745cdc | 2021-07-15 09:35:40 +0000 | [diff] [blame] | 763 | IsolateData* isolate_data = nullptr; |
Ben Konyi | 6338914 | 2018-02-07 19:35:39 +0000 | [diff] [blame] | 764 | #if !defined(DART_PRECOMPILED_RUNTIME) |
Liam Appelbe | e427c9c | 2019-06-26 20:57:46 +0000 | [diff] [blame] | 765 | if (!isolate_run_app_snapshot && (isolate_snapshot_data == NULL)) { |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 766 | const uint8_t* platform_kernel_buffer = NULL; |
| 767 | intptr_t platform_kernel_buffer_size = 0; |
asiva | 0a29264 | 2018-08-24 15:45:04 +0000 | [diff] [blame] | 768 | dfe.LoadPlatform(&platform_kernel_buffer, &platform_kernel_buffer_size); |
Ryan Macnak | a5c11d7 | 2018-05-14 20:58:08 +0000 | [diff] [blame] | 769 | if (platform_kernel_buffer == NULL) { |
| 770 | platform_kernel_buffer = kernel_buffer; |
| 771 | platform_kernel_buffer_size = kernel_buffer_size; |
| 772 | } |
| 773 | if (platform_kernel_buffer == NULL) { |
Ben Konyi | e35ed9c | 2018-05-23 20:35:53 +0000 | [diff] [blame] | 774 | #if defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM) |
| 775 | FATAL( |
| 776 | "Binary built with --exclude-kernel-service. Cannot run" |
| 777 | " from source."); |
| 778 | #else |
Ben Konyi | 6338914 | 2018-02-07 19:35:39 +0000 | [diff] [blame] | 779 | FATAL("platform_program cannot be NULL."); |
Ben Konyi | e35ed9c | 2018-05-23 20:35:53 +0000 | [diff] [blame] | 780 | #endif // defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM) |
Ben Konyi | 6338914 | 2018-02-07 19:35:39 +0000 | [diff] [blame] | 781 | } |
Siva Chandra | 71dba8a | 2018-02-01 15:11:29 +0000 | [diff] [blame] | 782 | // TODO(sivachandra): When the platform program is unavailable, check if |
| 783 | // application kernel binary is self contained or an incremental binary. |
| 784 | // Isolate should be created only if it is a self contained kernel binary. |
Martin Kustermann | b745cdc | 2021-07-15 09:35:40 +0000 | [diff] [blame] | 785 | isolate_data = new IsolateData(isolate_group_data); |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 786 | isolate = Dart_CreateIsolateGroupFromKernel( |
Ben Konyi | ac2c934 | 2019-03-28 16:39:50 +0000 | [diff] [blame] | 787 | script_uri, name, platform_kernel_buffer, platform_kernel_buffer_size, |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 788 | flags, isolate_group_data, isolate_data, error); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 789 | } else { |
Martin Kustermann | b745cdc | 2021-07-15 09:35:40 +0000 | [diff] [blame] | 790 | isolate_data = new IsolateData(isolate_group_data); |
Ryan Macnak | 6a65ea9 | 2019-10-22 19:02:26 +0000 | [diff] [blame] | 791 | isolate = Dart_CreateIsolateGroup(script_uri, name, isolate_snapshot_data, |
| 792 | isolate_snapshot_instructions, flags, |
| 793 | isolate_group_data, isolate_data, error); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 794 | } |
asiva | ee6351a | 2018-05-01 22:57:35 +0000 | [diff] [blame] | 795 | #else |
Martin Kustermann | b745cdc | 2021-07-15 09:35:40 +0000 | [diff] [blame] | 796 | isolate_data = new IsolateData(isolate_group_data); |
Ryan Macnak | 6a65ea9 | 2019-10-22 19:02:26 +0000 | [diff] [blame] | 797 | isolate = Dart_CreateIsolateGroup(script_uri, name, isolate_snapshot_data, |
| 798 | isolate_snapshot_instructions, flags, |
| 799 | isolate_group_data, isolate_data, error); |
Ben Konyi | 6338914 | 2018-02-07 19:35:39 +0000 | [diff] [blame] | 800 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
Alexander Aprelev | 85ee5ad | 2018-04-04 23:56:56 +0000 | [diff] [blame] | 801 | |
| 802 | Dart_Isolate created_isolate = NULL; |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 803 | if (isolate == NULL) { |
Martin Kustermann | b745cdc | 2021-07-15 09:35:40 +0000 | [diff] [blame] | 804 | delete isolate_data; |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 805 | delete isolate_group_data; |
Alexander Aprelev | 85ee5ad | 2018-04-04 23:56:56 +0000 | [diff] [blame] | 806 | } else { |
asiva | e70a7aa | 2018-10-10 23:19:24 +0000 | [diff] [blame] | 807 | created_isolate = IsolateSetupHelper( |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 808 | isolate, is_main_isolate, script_uri, packages_config, |
asiva | e70a7aa | 2018-10-10 23:19:24 +0000 | [diff] [blame] | 809 | isolate_run_app_snapshot, flags, error, exit_code); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 810 | } |
Alexander Aprelev | 85ee5ad | 2018-04-04 23:56:56 +0000 | [diff] [blame] | 811 | int64_t end = Dart_TimelineGetMicros(); |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 812 | Dart_TimelineEvent("CreateIsolateGroupAndSetupHelper", start, end, |
Alexander Aprelev | 85ee5ad | 2018-04-04 23:56:56 +0000 | [diff] [blame] | 813 | Dart_Timeline_Event_Duration, 0, NULL, NULL); |
Alexander Aprelev | 85ee5ad | 2018-04-04 23:56:56 +0000 | [diff] [blame] | 814 | return created_isolate; |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 815 | } |
| 816 | |
johnmccutchan@google.com | c76b6eb | 2014-01-14 22:34:43 +0000 | [diff] [blame] | 817 | #undef CHECK_RESULT |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 818 | |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 819 | static Dart_Isolate CreateIsolateGroupAndSetup(const char* script_uri, |
| 820 | const char* main, |
| 821 | const char* package_root, |
| 822 | const char* package_config, |
| 823 | Dart_IsolateFlags* flags, |
| 824 | void* callback_data, |
| 825 | char** error) { |
Ivan Posva | f5e3f94 | 2015-06-07 17:57:34 +0200 | [diff] [blame] | 826 | // The VM should never call the isolate helper with a NULL flags. |
| 827 | ASSERT(flags != NULL); |
| 828 | ASSERT(flags->version == DART_FLAGS_CURRENT_VERSION); |
Martin Kustermann | a94b8ae | 2020-05-04 14:51:14 +0000 | [diff] [blame] | 829 | ASSERT(package_root == nullptr); |
Ivan Posva | 60eab65 | 2015-10-12 09:05:37 -0700 | [diff] [blame] | 830 | int exit_code = 0; |
asiva | 1aaebed | 2018-02-28 01:15:47 +0000 | [diff] [blame] | 831 | #if !defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM) |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 832 | if (strcmp(script_uri, DART_KERNEL_ISOLATE_NAME) == 0) { |
Martin Kustermann | a94b8ae | 2020-05-04 14:51:14 +0000 | [diff] [blame] | 833 | return CreateAndSetupKernelIsolate(script_uri, package_config, flags, error, |
| 834 | &exit_code); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 835 | } |
asiva | 1aaebed | 2018-02-28 01:15:47 +0000 | [diff] [blame] | 836 | #endif // !defined(EXCLUDE_CFE_AND_KERNEL_PLATFORM) |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 837 | |
| 838 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 839 | if (strcmp(script_uri, DART_DEV_ISOLATE_NAME) == 0) { |
| 840 | return CreateAndSetupDartDevIsolate(script_uri, package_config, flags, |
| 841 | error, &exit_code); |
| 842 | } |
| 843 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
| 844 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 845 | if (strcmp(script_uri, DART_VM_SERVICE_ISOLATE_NAME) == 0) { |
Martin Kustermann | a94b8ae | 2020-05-04 14:51:14 +0000 | [diff] [blame] | 846 | return CreateAndSetupServiceIsolate(script_uri, package_config, flags, |
| 847 | error, &exit_code); |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 848 | } |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 849 | |
Siva Annamalai | 4b8cc6b | 2017-06-02 10:43:32 -0700 | [diff] [blame] | 850 | bool is_main_isolate = false; |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 851 | return CreateIsolateGroupAndSetupHelper(is_main_isolate, script_uri, main, |
Martin Kustermann | a94b8ae | 2020-05-04 14:51:14 +0000 | [diff] [blame] | 852 | package_config, flags, callback_data, |
| 853 | error, &exit_code); |
turnidge@google.com | 05b5d44 | 2012-05-23 18:32:51 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 856 | static void OnIsolateShutdown(void* isolate_group_data, void* isolate_data) { |
Alexander Aprelev | a53172c | 2018-10-17 17:12:47 +0000 | [diff] [blame] | 857 | Dart_EnterScope(); |
Alexander Markov | e64428a | 2019-01-18 01:41:18 +0000 | [diff] [blame] | 858 | Dart_Handle sticky_error = Dart_GetStickyError(); |
| 859 | if (!Dart_IsNull(sticky_error) && !Dart_IsFatalError(sticky_error)) { |
Ryan Macnak | 971f484 | 2019-04-22 20:15:43 +0000 | [diff] [blame] | 860 | Syslog::PrintErr("%s\n", Dart_GetError(sticky_error)); |
Alexander Markov | e64428a | 2019-01-18 01:41:18 +0000 | [diff] [blame] | 861 | } |
Alexander Aprelev | a53172c | 2018-10-17 17:12:47 +0000 | [diff] [blame] | 862 | Dart_ExitScope(); |
Vyacheslav Egorov | a5e1f89 | 2017-02-28 21:10:04 +0100 | [diff] [blame] | 863 | } |
| 864 | |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 865 | static void DeleteIsolateData(void* isolate_group_data, void* callback_data) { |
| 866 | auto isolate_data = reinterpret_cast<IsolateData*>(callback_data); |
Martin Kustermann | b2171f1 | 2019-06-26 16:04:27 +0000 | [diff] [blame] | 867 | delete isolate_data; |
ager@google.com | f791a12 | 2012-06-29 14:40:17 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 870 | static void DeleteIsolateGroupData(void* callback_data) { |
| 871 | auto isolate_group_data = reinterpret_cast<IsolateGroupData*>(callback_data); |
| 872 | delete isolate_group_data; |
| 873 | } |
| 874 | |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 875 | static const char* kStdoutStreamId = "Stdout"; |
| 876 | static const char* kStderrStreamId = "Stderr"; |
| 877 | |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 878 | static bool ServiceStreamListenCallback(const char* stream_id) { |
| 879 | if (strcmp(stream_id, kStdoutStreamId) == 0) { |
Todd Turnidge | b243fa5 | 2015-11-19 09:24:23 -0800 | [diff] [blame] | 880 | SetCaptureStdout(true); |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 881 | return true; |
| 882 | } else if (strcmp(stream_id, kStderrStreamId) == 0) { |
Todd Turnidge | b243fa5 | 2015-11-19 09:24:23 -0800 | [diff] [blame] | 883 | SetCaptureStderr(true); |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 884 | return true; |
| 885 | } |
| 886 | return false; |
| 887 | } |
| 888 | |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 889 | static void ServiceStreamCancelCallback(const char* stream_id) { |
| 890 | if (strcmp(stream_id, kStdoutStreamId) == 0) { |
Todd Turnidge | b243fa5 | 2015-11-19 09:24:23 -0800 | [diff] [blame] | 891 | SetCaptureStdout(false); |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 892 | } else if (strcmp(stream_id, kStderrStreamId) == 0) { |
Todd Turnidge | b243fa5 | 2015-11-19 09:24:23 -0800 | [diff] [blame] | 893 | SetCaptureStderr(false); |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 894 | } |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Todd Turnidge | 1166f83 | 2016-07-29 11:23:18 -0700 | [diff] [blame] | 897 | static bool FileModifiedCallback(const char* url, int64_t since) { |
Ben Konyi | 6be8d47 | 2018-01-11 14:53:02 -0800 | [diff] [blame] | 898 | if (strncmp(url, "file:///", 8) != 0) { |
Todd Turnidge | 1166f83 | 2016-07-29 11:23:18 -0700 | [diff] [blame] | 899 | // If it isn't a file on local disk, we don't know if it has been |
| 900 | // modified. |
| 901 | return true; |
| 902 | } |
| 903 | int64_t data[File::kStatSize]; |
Zachary Anderson | d0295c8 | 2017-08-30 09:34:34 -0700 | [diff] [blame] | 904 | File::Stat(NULL, url + 7, data); |
Todd Turnidge | 1166f83 | 2016-07-29 11:23:18 -0700 | [diff] [blame] | 905 | if (data[File::kType] == File::kDoesNotExist) { |
| 906 | return true; |
| 907 | } |
| 908 | bool modified = data[File::kModifiedTime] > since; |
| 909 | return modified; |
| 910 | } |
| 911 | |
Carlo Bernaschina | 0cbbeeb | 2017-08-14 17:02:22 -0700 | [diff] [blame] | 912 | static void EmbedderInformationCallback(Dart_EmbedderInformation* info) { |
Carlo Bernaschina | 0cbbeeb | 2017-08-14 17:02:22 -0700 | [diff] [blame] | 913 | info->version = DART_EMBEDDER_INFORMATION_CURRENT_VERSION; |
| 914 | info->name = "Dart VM"; |
Carlo Bernaschina | 491390e | 2017-08-16 10:59:20 -0700 | [diff] [blame] | 915 | Process::GetRSSInformation(&(info->max_rss), &(info->current_rss)); |
Carlo Bernaschina | 0cbbeeb | 2017-08-14 17:02:22 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 918 | #define CHECK_RESULT(result) \ |
| 919 | if (Dart_IsError(result)) { \ |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 920 | const int exit_code = Dart_IsCompilationError(result) \ |
| 921 | ? kCompilationErrorExitCode \ |
| 922 | : kErrorExitCode; \ |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 923 | ErrorExit(exit_code, "%s\n", Dart_GetError(result)); \ |
| 924 | } |
| 925 | |
Sigurd Meldgaard | ce272da | 2020-11-17 10:20:00 +0000 | [diff] [blame] | 926 | void RunMainIsolate(const char* script_name, |
| 927 | const char* package_config_override, |
| 928 | CommandLineOptions* dart_options) { |
Ryan Macnak | 70e3178 | 2021-08-31 19:33:10 +0000 | [diff] [blame] | 929 | if (script_name != NULL) { |
| 930 | const char* base_name = strrchr(script_name, '/'); |
| 931 | if (base_name == NULL) { |
| 932 | base_name = script_name; |
| 933 | } else { |
| 934 | base_name++; // Skip '/'. |
| 935 | } |
| 936 | const intptr_t kMaxNameLength = 64; |
| 937 | char name[kMaxNameLength]; |
| 938 | Utils::SNPrint(name, kMaxNameLength, "dart:%s", base_name); |
| 939 | Platform::SetProcessName(name); |
| 940 | } |
| 941 | |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 942 | // Call CreateIsolateGroupAndSetup which creates an isolate and loads up |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 943 | // the specified application script. |
| 944 | char* error = NULL; |
| 945 | int exit_code = 0; |
Vyacheslav Egorov | 7d52317 | 2017-08-25 09:48:20 +0200 | [diff] [blame] | 946 | Dart_IsolateFlags flags; |
| 947 | Dart_IsolateFlagsInitialize(&flags); |
Ben Konyi | 3b1e833 | 2021-07-22 18:15:10 +0000 | [diff] [blame] | 948 | flags.is_system_isolate = Options::mark_main_isolate_as_system_isolate(); |
Vyacheslav Egorov | 7d52317 | 2017-08-25 09:48:20 +0200 | [diff] [blame] | 949 | |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 950 | Dart_Isolate isolate = CreateIsolateGroupAndSetupHelper( |
Sigurd Meldgaard | ce272da | 2020-11-17 10:20:00 +0000 | [diff] [blame] | 951 | /* is_main_isolate */ true, script_name, "main", |
| 952 | Options::packages_file() == nullptr ? package_config_override |
| 953 | : Options::packages_file(), |
Ben Konyi | 9bac327 | 2020-08-13 22:04:24 +0000 | [diff] [blame] | 954 | &flags, NULL /* callback_data */, &error, &exit_code); |
Martin Kustermann | a4af44b | 2018-06-06 10:56:10 +0000 | [diff] [blame] | 955 | |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 956 | if (isolate == NULL) { |
Ryan Macnak | 971f484 | 2019-04-22 20:15:43 +0000 | [diff] [blame] | 957 | Syslog::PrintErr("%s\n", error); |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 958 | free(error); |
| 959 | error = NULL; |
| 960 | Process::TerminateExitCodeHandler(); |
| 961 | error = Dart_Cleanup(); |
| 962 | if (error != NULL) { |
Ryan Macnak | 971f484 | 2019-04-22 20:15:43 +0000 | [diff] [blame] | 963 | Syslog::PrintErr("VM cleanup failed: %s\n", error); |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 964 | free(error); |
| 965 | } |
Martin Kustermann | 0ae8697 | 2020-03-17 08:49:52 +0000 | [diff] [blame] | 966 | dart::embedder::Cleanup(); |
Zachary Anderson | 3563dc0 | 2015-10-29 23:35:46 -0700 | [diff] [blame] | 967 | Platform::Exit((exit_code != 0) ? exit_code : kErrorExitCode); |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 968 | } |
Ryan Macnak | 4d65db8a | 2016-10-31 15:56:25 -0700 | [diff] [blame] | 969 | main_isolate = isolate; |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 970 | |
| 971 | Dart_EnterIsolate(isolate); |
| 972 | ASSERT(isolate == Dart_CurrentIsolate()); |
| 973 | ASSERT(isolate != NULL); |
| 974 | Dart_Handle result; |
| 975 | |
| 976 | Dart_EnterScope(); |
| 977 | |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 978 | auto isolate_group_data = |
| 979 | reinterpret_cast<IsolateGroupData*>(Dart_IsolateGroupData(isolate)); |
Ryan Macnak | 2f25dcd | 2018-09-26 22:49:27 +0000 | [diff] [blame] | 980 | if (Options::gen_snapshot_kind() == kKernel) { |
Ryan Macnak | 76842f7 | 2017-12-05 17:09:52 +0000 | [diff] [blame] | 981 | if (vm_run_app_snapshot) { |
Ryan Macnak | 971f484 | 2019-04-22 20:15:43 +0000 | [diff] [blame] | 982 | Syslog::PrintErr( |
| 983 | "Cannot create a script snapshot from an app snapshot.\n"); |
Ryan Macnak | 76842f7 | 2017-12-05 17:09:52 +0000 | [diff] [blame] | 984 | // The snapshot would contain references to the app snapshot instead of |
| 985 | // the core snapshot. |
| 986 | Platform::Exit(kErrorExitCode); |
| 987 | } |
asiva | 355c73d | 2018-09-24 20:38:08 +0000 | [diff] [blame] | 988 | Snapshot::GenerateKernel(Options::snapshot_filename(), script_name, |
Siva Annamalai | d46a696 | 2020-06-20 08:02:53 +0000 | [diff] [blame] | 989 | isolate_group_data->resolved_packages_config()); |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 990 | } else { |
| 991 | // Lookup the library of the root script. |
| 992 | Dart_Handle root_lib = Dart_RootLibrary(); |
Ryan Macnak | 37dd876 | 2015-10-29 15:15:46 -0700 | [diff] [blame] | 993 | |
Ryan Macnak | cc28d2c | 2018-10-05 00:36:53 +0000 | [diff] [blame] | 994 | #if !defined(DART_PRECOMPILED_RUNTIME) |
Zachary Anderson | d7845c6 | 2017-09-11 18:58:39 +0000 | [diff] [blame] | 995 | if (Options::compile_all()) { |
Ryan Macnak | 9328d72 | 2016-01-08 10:19:14 -0800 | [diff] [blame] | 996 | result = Dart_CompileAll(); |
| 997 | CHECK_RESULT(result); |
| 998 | } |
Siva Annamalai | c29b19f | 2017-11-09 19:05:15 +0000 | [diff] [blame] | 999 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1000 | |
Ryan Macnak | cc28d2c | 2018-10-05 00:36:53 +0000 | [diff] [blame] | 1001 | if (Dart_IsNull(root_lib)) { |
| 1002 | ErrorExit(kErrorExitCode, "Unable to find root library for '%s'\n", |
| 1003 | script_name); |
| 1004 | } |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1005 | |
Ryan Macnak | cc28d2c | 2018-10-05 00:36:53 +0000 | [diff] [blame] | 1006 | // Create a closure for the main entry point which is in the exported |
| 1007 | // namespace of the root library or invoke a getter of the same name |
| 1008 | // in the exported namespace and return the resulting closure. |
| 1009 | Dart_Handle main_closure = |
| 1010 | Dart_GetField(root_lib, Dart_NewStringFromCString("main")); |
| 1011 | CHECK_RESULT(main_closure); |
| 1012 | if (!Dart_IsClosure(main_closure)) { |
| 1013 | ErrorExit(kErrorExitCode, "Unable to find 'main' in root library '%s'\n", |
| 1014 | script_name); |
| 1015 | } |
| 1016 | |
| 1017 | // Call _startIsolate in the isolate library to enable dispatching the |
| 1018 | // initial startup message. |
| 1019 | const intptr_t kNumIsolateArgs = 2; |
| 1020 | Dart_Handle isolate_args[kNumIsolateArgs]; |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 1021 | isolate_args[0] = main_closure; // entryPoint |
| 1022 | isolate_args[1] = dart_options->CreateRuntimeOptions(); // args |
Ryan Macnak | cc28d2c | 2018-10-05 00:36:53 +0000 | [diff] [blame] | 1023 | |
| 1024 | Dart_Handle isolate_lib = |
| 1025 | Dart_LookupLibrary(Dart_NewStringFromCString("dart:isolate")); |
| 1026 | result = |
| 1027 | Dart_Invoke(isolate_lib, Dart_NewStringFromCString("_startMainIsolate"), |
| 1028 | kNumIsolateArgs, isolate_args); |
| 1029 | CHECK_RESULT(result); |
| 1030 | |
| 1031 | // Keep handling messages until the last active receive port is closed. |
| 1032 | result = Dart_RunLoop(); |
| 1033 | // Generate an app snapshot after execution if specified. |
| 1034 | if (Options::gen_snapshot_kind() == kAppJIT) { |
| 1035 | if (!Dart_IsCompilationError(result)) { |
| 1036 | Snapshot::GenerateAppJIT(Options::snapshot_filename()); |
Ryan Macnak | 74f3d1b | 2017-06-05 16:56:16 -0700 | [diff] [blame] | 1037 | } |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1038 | } |
Ryan Macnak | cc28d2c | 2018-10-05 00:36:53 +0000 | [diff] [blame] | 1039 | CHECK_RESULT(result); |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Zachary Anderson | f06bf4c | 2018-02-07 05:08:45 +0000 | [diff] [blame] | 1042 | WriteDepsFile(isolate); |
P.Y. Laligand | 8f03520 | 2017-03-27 06:44:21 -0700 | [diff] [blame] | 1043 | |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1044 | Dart_ExitScope(); |
P.Y. Laligand | 8f03520 | 2017-03-27 06:44:21 -0700 | [diff] [blame] | 1045 | |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1046 | // Shutdown the isolate. |
| 1047 | Dart_ShutdownIsolate(); |
Todd Turnidge | 0d501ad | 2015-10-06 11:27:26 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | #undef CHECK_RESULT |
| 1051 | |
Zichang Guo | d6d37ed | 2020-07-31 21:09:40 +0000 | [diff] [blame] | 1052 | static bool CheckForInvalidPath(const char* path) { |
| 1053 | // TODO(zichangguo): "\\?\" is a prefix for paths on Windows. |
| 1054 | // Arguments passed are parsed as an URI. "\\?\" causes problems as a part |
| 1055 | // of URIs. This is a temporary workaround to prevent VM from crashing. |
| 1056 | // Issue: https://github.com/dart-lang/sdk/issues/42779 |
| 1057 | if (strncmp(path, "\\\\?\\", 4) == 0) { |
| 1058 | Syslog::PrintErr("\\\\?\\ prefix is not supported"); |
| 1059 | return false; |
| 1060 | } |
| 1061 | return true; |
| 1062 | } |
| 1063 | |
asiva | 8e4421a | 2019-12-27 19:46:15 +0000 | [diff] [blame] | 1064 | // Observatory assets are not included in a product build. |
| 1065 | #if !defined(PRODUCT) |
John McCutchan | 2844eb8 | 2015-10-26 07:21:23 -0700 | [diff] [blame] | 1066 | extern unsigned int observatory_assets_archive_len; |
John McCutchan | 33814bd | 2015-10-27 08:56:02 -0700 | [diff] [blame] | 1067 | extern const uint8_t* observatory_assets_archive; |
John McCutchan | 2844eb8 | 2015-10-26 07:21:23 -0700 | [diff] [blame] | 1068 | |
| 1069 | Dart_Handle GetVMServiceAssetsArchiveCallback() { |
John McCutchan | cc0f297 | 2015-12-16 12:35:58 -0800 | [diff] [blame] | 1070 | uint8_t* decompressed = NULL; |
Ryan Macnak | 7dfe62f | 2017-08-11 12:18:16 -0700 | [diff] [blame] | 1071 | intptr_t decompressed_len = 0; |
Zachary Anderson | 479a97b | 2016-11-04 12:30:56 -0700 | [diff] [blame] | 1072 | Decompress(observatory_assets_archive, observatory_assets_archive_len, |
| 1073 | &decompressed, &decompressed_len); |
| 1074 | Dart_Handle tar_file = |
| 1075 | DartUtils::MakeUint8Array(decompressed, decompressed_len); |
John McCutchan | cc0f297 | 2015-12-16 12:35:58 -0800 | [diff] [blame] | 1076 | // Free decompressed memory as it has been copied into a Dart array. |
| 1077 | free(decompressed); |
| 1078 | return tar_file; |
John McCutchan | 2844eb8 | 2015-10-26 07:21:23 -0700 | [diff] [blame] | 1079 | } |
asiva | 8e4421a | 2019-12-27 19:46:15 +0000 | [diff] [blame] | 1080 | #else // !defined(PRODUCT) |
Ivan Posva | 9535aa6 | 2016-03-04 16:05:54 -0800 | [diff] [blame] | 1081 | static Dart_GetVMServiceAssetsArchive GetVMServiceAssetsArchiveCallback = NULL; |
asiva | 8e4421a | 2019-12-27 19:46:15 +0000 | [diff] [blame] | 1082 | #endif // !defined(PRODUCT) |
John McCutchan | 2844eb8 | 2015-10-26 07:21:23 -0700 | [diff] [blame] | 1083 | |
johnmccutchan@google.com | a80d328 | 2013-11-26 18:13:51 +0000 | [diff] [blame] | 1084 | void main(int argc, char** argv) { |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 1085 | char* script_name = nullptr; |
Sigurd Meldgaard | ce272da | 2020-11-17 10:20:00 +0000 | [diff] [blame] | 1086 | // Allows the dartdev process to point to the desired package_config. |
| 1087 | char* package_config_override = nullptr; |
Zach Anderson | 664f756 | 2018-06-13 20:50:34 +0000 | [diff] [blame] | 1088 | const int EXTRA_VM_ARGUMENTS = 10; |
Srdjan Mitrovic | fc3cd69 | 2015-05-27 17:11:35 -0700 | [diff] [blame] | 1089 | CommandLineOptions vm_options(argc + EXTRA_VM_ARGUMENTS); |
Ben Konyi | 444490d | 2020-05-13 19:58:56 +0000 | [diff] [blame] | 1090 | CommandLineOptions dart_options(argc + EXTRA_VM_ARGUMENTS); |
asiva@google.com | c7f58ad | 2012-06-13 17:48:47 +0000 | [diff] [blame] | 1091 | bool print_flags_seen = false; |
tball@google.com | b3cae06 | 2013-04-05 20:55:10 +0000 | [diff] [blame] | 1092 | bool verbose_debug_seen = false; |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 1093 | |
Ben Konyi | 103d54b | 2019-09-24 20:43:20 +0000 | [diff] [blame] | 1094 | // Perform platform specific initialization. |
| 1095 | if (!Platform::Initialize()) { |
| 1096 | Syslog::PrintErr("Initialization failed\n"); |
Ryan Macnak | 1ed184b | 2017-10-23 22:19:39 +0000 | [diff] [blame] | 1097 | Platform::Exit(kErrorExitCode); |
sgjesse@google.com | d7c8c1f | 2011-11-23 14:49:18 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Ben Konyi | 3c8d3b6 | 2018-03-12 20:31:09 +0000 | [diff] [blame] | 1100 | // Save the console state so we can restore it at shutdown. |
| 1101 | Console::SaveConfig(); |
| 1102 | |
ager@google.com | 28b4314 | 2012-11-13 15:03:23 +0000 | [diff] [blame] | 1103 | // On Windows, the argv strings are code page encoded and not |
| 1104 | // utf8. We need to convert them to utf8. |
Soren Gjesse | 0415d92 | 2015-06-24 10:36:05 +0200 | [diff] [blame] | 1105 | bool argv_converted = ShellUtils::GetUtf8Argv(argc, argv); |
ager@google.com | 28b4314 | 2012-11-13 15:03:23 +0000 | [diff] [blame] | 1106 | |
Zachary Anderson | d7845c6 | 2017-09-11 18:58:39 +0000 | [diff] [blame] | 1107 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 1108 | // Processing of some command line flags directly manipulates dfe. |
| 1109 | Options::set_dfe(&dfe); |
| 1110 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
| 1111 | |
Zach Anderson | 664f756 | 2018-06-13 20:50:34 +0000 | [diff] [blame] | 1112 | // When running from the command line we assume that we are optimizing for |
| 1113 | // throughput, and therefore use a larger new gen semi space size and a faster |
| 1114 | // new gen growth factor unless others have been specified. |
| 1115 | if (kWordSize <= 4) { |
| 1116 | vm_options.AddArgument("--new_gen_semi_max_size=16"); |
| 1117 | } else { |
| 1118 | vm_options.AddArgument("--new_gen_semi_max_size=32"); |
| 1119 | } |
| 1120 | vm_options.AddArgument("--new_gen_growth_factor=4"); |
| 1121 | |
Samir Jindel | afac6a3 | 2019-10-03 13:40:12 +0000 | [diff] [blame] | 1122 | AppSnapshot* app_snapshot = nullptr; |
| 1123 | #if defined(DART_PRECOMPILED_RUNTIME) |
Clement Skau | 46b5839 | 2019-09-11 10:56:57 +0000 | [diff] [blame] | 1124 | // If the executable binary contains the runtime together with an appended |
| 1125 | // snapshot, load and run that. |
| 1126 | // Any arguments passed to such an executable are meant for the actual |
| 1127 | // application so skip all Dart VM flag parsing. |
Clement Skau | 46b5839 | 2019-09-11 10:56:57 +0000 | [diff] [blame] | 1128 | |
Clement Skau | 8d8faa7d | 2019-10-18 10:30:48 +0000 | [diff] [blame] | 1129 | const size_t kPathBufSize = PATH_MAX + 1; |
| 1130 | char executable_path[kPathBufSize]; |
| 1131 | if (Platform::ResolveExecutablePathInto(executable_path, kPathBufSize) > 0) { |
| 1132 | app_snapshot = Snapshot::TryReadAppendedAppSnapshotElf(executable_path); |
| 1133 | if (app_snapshot != nullptr) { |
| 1134 | script_name = argv[0]; |
Clement Skau | 46b5839 | 2019-09-11 10:56:57 +0000 | [diff] [blame] | 1135 | |
Clement Skau | 8d8faa7d | 2019-10-18 10:30:48 +0000 | [diff] [blame] | 1136 | // Store the executable name. |
| 1137 | Platform::SetExecutableName(argv[0]); |
| 1138 | |
| 1139 | // Parse out options to be passed to dart main. |
| 1140 | for (int i = 1; i < argc; i++) { |
| 1141 | dart_options.AddArgument(argv[i]); |
| 1142 | } |
Clement Skau | 46b5839 | 2019-09-11 10:56:57 +0000 | [diff] [blame] | 1143 | } |
Samir Jindel | afac6a3 | 2019-10-03 13:40:12 +0000 | [diff] [blame] | 1144 | } |
| 1145 | #endif |
| 1146 | |
| 1147 | // Parse command line arguments. |
Ben Konyi | 6548694 | 2020-02-06 00:04:09 +0000 | [diff] [blame] | 1148 | if (app_snapshot == nullptr) { |
Sigurd Meldgaard | f3fe9dc | 2020-12-04 14:22:10 +0000 | [diff] [blame] | 1149 | bool success = Options::ParseArguments( |
Ben Konyi | 6548694 | 2020-02-06 00:04:09 +0000 | [diff] [blame] | 1150 | argc, argv, vm_run_app_snapshot, &vm_options, &script_name, |
| 1151 | &dart_options, &print_flags_seen, &verbose_debug_seen); |
Sigurd Meldgaard | f3fe9dc | 2020-12-04 14:22:10 +0000 | [diff] [blame] | 1152 | if (!success) { |
Ben Konyi | 6548694 | 2020-02-06 00:04:09 +0000 | [diff] [blame] | 1153 | if (Options::help_option()) { |
| 1154 | Options::PrintUsage(); |
| 1155 | Platform::Exit(0); |
| 1156 | } else if (Options::version_option()) { |
| 1157 | Options::PrintVersion(); |
| 1158 | Platform::Exit(0); |
| 1159 | } else if (print_flags_seen) { |
| 1160 | // Will set the VM flags, print them out and then we exit as no |
| 1161 | // script was specified on the command line. |
| 1162 | char* error = |
| 1163 | Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
| 1164 | if (error != NULL) { |
| 1165 | Syslog::PrintErr("Setting VM flags failed: %s\n", error); |
| 1166 | free(error); |
| 1167 | Platform::Exit(kErrorExitCode); |
| 1168 | } |
| 1169 | Platform::Exit(0); |
| 1170 | } else { |
Ben Konyi | d2855e7 | 2021-08-13 23:01:51 +0000 | [diff] [blame] | 1171 | // This usage error case will only be invoked when |
| 1172 | // Options::disable_dart_dev() is false. |
Ben Konyi | 6548694 | 2020-02-06 00:04:09 +0000 | [diff] [blame] | 1173 | Options::PrintUsage(); |
Ryan Macnak | 607f4f5 | 2018-05-01 20:54:38 +0000 | [diff] [blame] | 1174 | Platform::Exit(kErrorExitCode); |
| 1175 | } |
Ben Konyi | 6548694 | 2020-02-06 00:04:09 +0000 | [diff] [blame] | 1176 | } |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 1177 | } |
Ben Konyi | 6548694 | 2020-02-06 00:04:09 +0000 | [diff] [blame] | 1178 | |
Zach Anderson | 27c79f9 | 2018-09-06 02:23:21 +0000 | [diff] [blame] | 1179 | DartUtils::SetEnvironment(Options::environment()); |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 1180 | |
Vyacheslav Egorov | 040ef6a | 2018-11-30 11:15:06 +0000 | [diff] [blame] | 1181 | if (Options::suppress_core_dump()) { |
| 1182 | Platform::SetCoreDumpResourceLimit(0); |
| 1183 | } else { |
| 1184 | InitializeCrashpadClient(); |
| 1185 | } |
Vyacheslav Egorov | 2c83eb5 | 2018-10-24 13:30:16 +0000 | [diff] [blame] | 1186 | |
Florian Schneider | 63e3f4d | 2016-07-13 09:41:06 -0700 | [diff] [blame] | 1187 | Loader::InitOnce(); |
| 1188 | |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 1189 | auto try_load_snapshots_lambda = [&](void) -> void { |
| 1190 | if (app_snapshot == nullptr) { |
| 1191 | // For testing purposes we add a flag to debug-mode to use the |
| 1192 | // in-memory ELF loader. |
| 1193 | const bool force_load_elf_from_memory = |
| 1194 | false DEBUG_ONLY(|| Options::force_load_elf_from_memory()); |
| 1195 | app_snapshot = |
| 1196 | Snapshot::TryReadAppSnapshot(script_name, force_load_elf_from_memory); |
| 1197 | } |
| 1198 | if (app_snapshot != nullptr) { |
| 1199 | vm_run_app_snapshot = true; |
| 1200 | app_snapshot->SetBuffers(&vm_snapshot_data, &vm_snapshot_instructions, |
| 1201 | &app_isolate_snapshot_data, |
| 1202 | &app_isolate_snapshot_instructions); |
| 1203 | } |
| 1204 | }; |
| 1205 | |
| 1206 | // At this point, script_name now points to a script if DartDev is disabled |
| 1207 | // or a valid file path was provided as the first non-flag argument. |
| 1208 | // Otherwise, script_name can be NULL if DartDev should be run. |
| 1209 | if (script_name != nullptr) { |
Zichang Guo | d6d37ed | 2020-07-31 21:09:40 +0000 | [diff] [blame] | 1210 | if (!CheckForInvalidPath(script_name)) { |
| 1211 | Platform::Exit(0); |
| 1212 | } |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 1213 | try_load_snapshots_lambda(); |
Ryan Macnak | 3d00ecd | 2016-10-14 09:57:54 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
Zachary Anderson | d7845c6 | 2017-09-11 18:58:39 +0000 | [diff] [blame] | 1216 | if (Options::gen_snapshot_kind() == kAppJIT) { |
Ryan Macnak | ad32e93 | 2016-10-07 10:34:57 -0700 | [diff] [blame] | 1217 | vm_options.AddArgument("--fields_may_be_reset"); |
| 1218 | } |
Ryan Macnak | 7546ef1 | 2016-05-06 12:40:04 -0700 | [diff] [blame] | 1219 | #if defined(DART_PRECOMPILED_RUNTIME) |
Florian Schneider | 23dd0b9 | 2016-02-04 16:33:37 -0800 | [diff] [blame] | 1220 | vm_options.AddArgument("--precompilation"); |
Florian Schneider | 0f48690 | 2016-02-02 17:51:15 -0800 | [diff] [blame] | 1221 | #endif |
Zach Anderson | 00e3109 | 2018-07-31 17:09:31 +0000 | [diff] [blame] | 1222 | // If we need to write an app-jit snapshot or a depfile, then add an exit |
| 1223 | // hook that writes the snapshot and/or depfile as appropriate. |
| 1224 | if ((Options::gen_snapshot_kind() == kAppJIT) || |
| 1225 | (Options::depfile() != NULL)) { |
| 1226 | Process::SetExitHook(OnExitHook); |
Ryan Macnak | 4d65db8a | 2016-10-31 15:56:25 -0700 | [diff] [blame] | 1227 | } |
Florian Schneider | 0f48690 | 2016-02-02 17:51:15 -0800 | [diff] [blame] | 1228 | |
Ben Konyi | 103d54b | 2019-09-24 20:43:20 +0000 | [diff] [blame] | 1229 | char* error = nullptr; |
| 1230 | if (!dart::embedder::InitOnce(&error)) { |
| 1231 | Syslog::PrintErr("Standalone embedder initialization failed: %s\n", error); |
| 1232 | free(error); |
| 1233 | Platform::Exit(kErrorExitCode); |
| 1234 | } |
| 1235 | |
Vyacheslav Egorov | c910738 | 2018-07-17 09:04:05 +0000 | [diff] [blame] | 1236 | error = Dart_SetVMFlags(vm_options.count(), vm_options.arguments()); |
Ryan Macnak | 607f4f5 | 2018-05-01 20:54:38 +0000 | [diff] [blame] | 1237 | if (error != NULL) { |
Ryan Macnak | 971f484 | 2019-04-22 20:15:43 +0000 | [diff] [blame] | 1238 | Syslog::PrintErr("Setting VM flags failed: %s\n", error); |
Ryan Macnak | 607f4f5 | 2018-05-01 20:54:38 +0000 | [diff] [blame] | 1239 | free(error); |
| 1240 | Platform::Exit(kErrorExitCode); |
| 1241 | } |
Vyacheslav Egorov | 0fe889d | 2017-10-23 11:27:47 +0000 | [diff] [blame] | 1242 | |
| 1243 | // Note: must read platform only *after* VM flags are parsed because |
| 1244 | // they might affect how the platform is loaded. |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 1245 | #if !defined(DART_PRECOMPILED_RUNTIME) |
Ben Konyi | 19875e9 | 2020-09-10 17:53:19 +0000 | [diff] [blame] | 1246 | // Load vm_platform_strong.dill for dart:* source support. |
| 1247 | dfe.Init(); |
asiva | ac520f9 | 2021-01-26 03:01:58 +0000 | [diff] [blame] | 1248 | dfe.set_verbosity(Options::verbosity_level()); |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 1249 | if (script_name != nullptr) { |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 1250 | uint8_t* application_kernel_buffer = NULL; |
| 1251 | intptr_t application_kernel_buffer_size = 0; |
| 1252 | dfe.ReadScript(script_name, &application_kernel_buffer, |
| 1253 | &application_kernel_buffer_size); |
| 1254 | if (application_kernel_buffer != NULL) { |
| 1255 | // Since we loaded the script anyway, save it. |
| 1256 | dfe.set_application_kernel_buffer(application_kernel_buffer, |
| 1257 | application_kernel_buffer_size); |
| 1258 | Options::dfe()->set_use_dfe(); |
| 1259 | } |
Siva Chandra | 8b40507 | 2017-06-22 10:10:42 -0700 | [diff] [blame] | 1260 | } |
| 1261 | #endif |
| 1262 | |
johnmccutchan@google.com | 9b1f39b | 2015-02-12 21:22:34 +0000 | [diff] [blame] | 1263 | // Initialize the Dart VM. |
Adam Barth | fac99f3 | 2016-08-09 12:25:29 -0700 | [diff] [blame] | 1264 | Dart_InitializeParams init_params; |
| 1265 | memset(&init_params, 0, sizeof(init_params)); |
| 1266 | init_params.version = DART_INITIALIZE_PARAMS_CURRENT_VERSION; |
Ryan Macnak | b46af1e | 2017-01-23 10:25:02 -0800 | [diff] [blame] | 1267 | init_params.vm_snapshot_data = vm_snapshot_data; |
| 1268 | init_params.vm_snapshot_instructions = vm_snapshot_instructions; |
Alexander Aprelev | 0425997 | 2019-07-08 23:49:05 +0000 | [diff] [blame] | 1269 | init_params.create_group = CreateIsolateGroupAndSetup; |
| 1270 | init_params.initialize_isolate = OnIsolateInitialize; |
| 1271 | init_params.shutdown_isolate = OnIsolateShutdown; |
| 1272 | init_params.cleanup_isolate = DeleteIsolateData; |
| 1273 | init_params.cleanup_group = DeleteIsolateGroupData; |
Adam Barth | fac99f3 | 2016-08-09 12:25:29 -0700 | [diff] [blame] | 1274 | init_params.file_open = DartUtils::OpenFile; |
| 1275 | init_params.file_read = DartUtils::ReadFile; |
| 1276 | init_params.file_write = DartUtils::WriteFile; |
| 1277 | init_params.file_close = DartUtils::CloseFile; |
| 1278 | init_params.entropy_source = DartUtils::EntropySource; |
| 1279 | init_params.get_service_assets = GetVMServiceAssetsArchiveCallback; |
Siva Annamalai | 73d3007 | 2017-08-11 12:38:22 -0700 | [diff] [blame] | 1280 | #if !defined(DART_PRECOMPILED_RUNTIME) |
asiva | 355c73d | 2018-09-24 20:38:08 +0000 | [diff] [blame] | 1281 | init_params.start_kernel_isolate = |
| 1282 | dfe.UseDartFrontend() && dfe.CanUseDartFrontend(); |
Siva Annamalai | 73d3007 | 2017-08-11 12:38:22 -0700 | [diff] [blame] | 1283 | #else |
| 1284 | init_params.start_kernel_isolate = false; |
| 1285 | #endif |
Adam Barth | fac99f3 | 2016-08-09 12:25:29 -0700 | [diff] [blame] | 1286 | |
Ryan Macnak | 607f4f5 | 2018-05-01 20:54:38 +0000 | [diff] [blame] | 1287 | error = Dart_Initialize(&init_params); |
Zachary Anderson | 7093f29 | 2015-09-15 12:49:52 -0700 | [diff] [blame] | 1288 | if (error != NULL) { |
Martin Kustermann | 0ae8697 | 2020-03-17 08:49:52 +0000 | [diff] [blame] | 1289 | dart::embedder::Cleanup(); |
Ryan Macnak | 971f484 | 2019-04-22 20:15:43 +0000 | [diff] [blame] | 1290 | Syslog::PrintErr("VM initialization failed: %s\n", error); |
Zachary Anderson | 7093f29 | 2015-09-15 12:49:52 -0700 | [diff] [blame] | 1291 | free(error); |
Zachary Anderson | 3563dc0 | 2015-10-29 23:35:46 -0700 | [diff] [blame] | 1292 | Platform::Exit(kErrorExitCode); |
johnmccutchan@google.com | 9b1f39b | 2015-02-12 21:22:34 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Todd Turnidge | e4684c7 | 2015-07-14 12:54:07 -0700 | [diff] [blame] | 1295 | Dart_SetServiceStreamCallbacks(&ServiceStreamListenCallback, |
| 1296 | &ServiceStreamCancelCallback); |
Todd Turnidge | 1166f83 | 2016-07-29 11:23:18 -0700 | [diff] [blame] | 1297 | Dart_SetFileModifiedCallback(&FileModifiedCallback); |
Carlo Bernaschina | 0cbbeeb | 2017-08-14 17:02:22 -0700 | [diff] [blame] | 1298 | Dart_SetEmbedderInformationCallback(&EmbedderInformationCallback); |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 1299 | bool ran_dart_dev = false; |
| 1300 | bool should_run_user_program = true; |
| 1301 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 1302 | if (DartDevIsolate::should_run_dart_dev() && !Options::disable_dart_dev() && |
| 1303 | Options::gen_snapshot_kind() == SnapshotKind::kNone) { |
| 1304 | DartDevIsolate::DartDev_Result dartdev_result = DartDevIsolate::RunDartDev( |
Sigurd Meldgaard | ce272da | 2020-11-17 10:20:00 +0000 | [diff] [blame] | 1305 | CreateIsolateGroupAndSetup, &package_config_override, &script_name, |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 1306 | &dart_options); |
| 1307 | ASSERT(dartdev_result != DartDevIsolate::DartDev_Result_Unknown); |
| 1308 | ran_dart_dev = true; |
| 1309 | should_run_user_program = |
| 1310 | (dartdev_result == DartDevIsolate::DartDev_Result_Run); |
| 1311 | if (should_run_user_program) { |
| 1312 | try_load_snapshots_lambda(); |
| 1313 | } |
| 1314 | } |
| 1315 | #endif // !defined(DART_PRECOMPILED_RUNTIME) |
johnmccutchan@google.com | 42f5a39 | 2013-07-16 22:17:52 +0000 | [diff] [blame] | 1316 | |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 1317 | if (should_run_user_program) { |
Ben Konyi | 49d7eba | 2020-07-27 21:51:03 +0000 | [diff] [blame] | 1318 | if (!Dart_IsPrecompiledRuntime() && Snapshot::IsAOTSnapshot(script_name)) { |
| 1319 | Syslog::PrintErr( |
| 1320 | "%s is an AOT snapshot and should be run with 'dartaotruntime'\n", |
| 1321 | script_name); |
| 1322 | Platform::Exit(kErrorExitCode); |
| 1323 | } else { |
| 1324 | // Run the main isolate until we aren't told to restart. |
Sigurd Meldgaard | ce272da | 2020-11-17 10:20:00 +0000 | [diff] [blame] | 1325 | RunMainIsolate(script_name, package_config_override, &dart_options); |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 1326 | } |
hausner@google.com | 9159389 | 2011-12-07 22:13:49 +0000 | [diff] [blame] | 1327 | } |
hausner@google.com | 95be461 | 2012-05-04 15:25:49 +0000 | [diff] [blame] | 1328 | |
ager@google.com | ed3338d | 2012-02-01 18:16:45 +0000 | [diff] [blame] | 1329 | // Terminate process exit-code handler. |
| 1330 | Process::TerminateExitCodeHandler(); |
ajohnsen@google.com | 1e2289e | 2013-07-17 07:26:05 +0000 | [diff] [blame] | 1331 | |
Zachary Anderson | 7093f29 | 2015-09-15 12:49:52 -0700 | [diff] [blame] | 1332 | error = Dart_Cleanup(); |
| 1333 | if (error != NULL) { |
Ryan Macnak | 971f484 | 2019-04-22 20:15:43 +0000 | [diff] [blame] | 1334 | Syslog::PrintErr("VM cleanup failed: %s\n", error); |
Zachary Anderson | 7093f29 | 2015-09-15 12:49:52 -0700 | [diff] [blame] | 1335 | free(error); |
| 1336 | } |
Martin Kustermann | 0ae8697 | 2020-03-17 08:49:52 +0000 | [diff] [blame] | 1337 | const intptr_t global_exit_code = Process::GlobalExitCode(); |
| 1338 | dart::embedder::Cleanup(); |
Zachary Anderson | 9395a5b | 2015-09-01 17:53:03 -0700 | [diff] [blame] | 1339 | |
Ryan Macnak | a317b90 | 2017-02-16 13:54:57 -0800 | [diff] [blame] | 1340 | delete app_snapshot; |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 1341 | free(app_script_uri); |
Ben Konyi | 157ebfe | 2020-07-21 16:23:18 +0000 | [diff] [blame] | 1342 | if (ran_dart_dev && script_name != nullptr) { |
| 1343 | free(script_name); |
| 1344 | } |
Ryan Macnak | 26a364f | 2017-01-17 15:59:41 -0800 | [diff] [blame] | 1345 | |
ager@google.com | 28b4314 | 2012-11-13 15:03:23 +0000 | [diff] [blame] | 1346 | // Free copied argument strings if converted. |
| 1347 | if (argv_converted) { |
Zachary Anderson | c75a817 | 2016-03-14 11:08:52 -0700 | [diff] [blame] | 1348 | for (int i = 0; i < argc; i++) { |
| 1349 | free(argv[i]); |
| 1350 | } |
ager@google.com | 28b4314 | 2012-11-13 15:03:23 +0000 | [diff] [blame] | 1351 | } |
ager@google.com | 7031be2 | 2012-02-01 14:19:25 +0000 | [diff] [blame] | 1352 | |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 1353 | // Free environment if any. |
Zachary Anderson | d7845c6 | 2017-09-11 18:58:39 +0000 | [diff] [blame] | 1354 | Options::DestroyEnvironment(); |
sgjesse@google.com | 36a67fa | 2013-10-31 05:46:57 +0000 | [diff] [blame] | 1355 | |
Martin Kustermann | 0ae8697 | 2020-03-17 08:49:52 +0000 | [diff] [blame] | 1356 | Platform::Exit(global_exit_code); |
dgrove@google.com | 4c0f559 | 2011-10-05 05:20:07 +0000 | [diff] [blame] | 1357 | } |
smok@google.com | ebbc202 | 2013-04-25 14:22:30 +0000 | [diff] [blame] | 1358 | |
| 1359 | } // namespace bin |
| 1360 | } // namespace dart |
| 1361 | |
| 1362 | int main(int argc, char** argv) { |
johnmccutchan@google.com | a80d328 | 2013-11-26 18:13:51 +0000 | [diff] [blame] | 1363 | dart::bin::main(argc, argv); |
| 1364 | UNREACHABLE(); |
smok@google.com | ebbc202 | 2013-04-25 14:22:30 +0000 | [diff] [blame] | 1365 | } |