Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1066)

Side by Side Diff: runtime/bin/loader.cc

Issue 1998963003: Rework standalone to use a synchronous loader that does not invoke Dart code (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
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
5
6 #include "bin/loader.h"
7
8 #include "bin/builtin.h"
9 #include "bin/dartutils.h"
10 #include "bin/extensions.h"
11 #include "bin/lockers.h"
12
13 namespace dart {
14 namespace bin {
15
16 // Development flag.
17 static bool trace_loader = false;
18
19 Loader::Loader(IsolateData* isolate_data)
20 : port_(ILLEGAL_PORT),
21 isolate_data_(isolate_data),
22 error_(Dart_Null()),
23 monitor_(NULL),
24 pending_operations_(0),
25 results_(NULL),
26 results_length_(0),
27 results_capacity_(0) {
28 monitor_ = new Monitor();
29 ASSERT(isolate_data_ != NULL);
30 port_ = Dart_NewNativePort("Loader",
31 Loader::NativeMessageHandler,
32 false);
33 isolate_data_->set_loader(this);
34 AddLoader(port_, isolate_data_);
35 }
36
37
38 Loader::~Loader() {
39 ASSERT(port_ != ILLEGAL_PORT);
40 RemoveLoader(port_);
41 Dart_CloseNativePort(port_);
42 port_ = ILLEGAL_PORT;
43 isolate_data_->set_loader(NULL);
44 isolate_data_ = NULL;
45 delete monitor_;
46 monitor_ = NULL;
47 for (intptr_t i = 0; i < results_length_; i++) {
48 results_[i].Cleanup();
49 }
50 free(results_);
51 results_ = NULL;
52 }
53
54
55 // Copy the contents of |message| into an |IOResult|.
56 void Loader::IOResult::Setup(Dart_CObject* message) {
57 ASSERT(message->type == Dart_CObject_kArray);
58 ASSERT(message->value.as_array.length == 4);
59 Dart_CObject* tag_message = message->value.as_array.values[0];
60 ASSERT(tag_message != NULL);
61 Dart_CObject* uri_message = message->value.as_array.values[1];
62 ASSERT(uri_message != NULL);
63 Dart_CObject* library_uri_message = message->value.as_array.values[2];
64 ASSERT(library_uri_message != NULL);
65 Dart_CObject* payload_message = message->value.as_array.values[3];
66 ASSERT(payload_message != NULL);
67
68 // Grab the tag.
69 ASSERT(tag_message->type == Dart_CObject_kInt32);
70 tag = tag_message->value.as_int32;
71
72 // Grab the uri id.
73 ASSERT(uri_message->type == Dart_CObject_kString);
74 uri = strdup(uri_message->value.as_string);
75
76 // Grab the library uri if one is present.
77 if (library_uri_message->type != Dart_CObject_kNull) {
78 ASSERT(library_uri_message->type == Dart_CObject_kString);
79 library_uri = strdup(library_uri_message->value.as_string);
80 } else {
81 library_uri = NULL;
82 }
83
84 // Grab the payload.
85 if (payload_message->type == Dart_CObject_kString) {
86 // Payload is an error message.
87 payload_length = strlen(payload_message->value.as_string);
88 payload =
89 reinterpret_cast<uint8_t*>(strdup(payload_message->value.as_string));
90 } else {
91 // Payload is the contents of a file.
92 ASSERT(payload_message->type == Dart_CObject_kTypedData);
93 ASSERT(payload_message->value.as_typed_data.type == Dart_TypedData_kUint8);
94 payload_length = payload_message->value.as_typed_data.length;
95 payload = reinterpret_cast<uint8_t*>(malloc(payload_length));
96 memmove(payload,
97 payload_message->value.as_typed_data.values,
98 payload_length);
99 }
100 }
101
102
103 void Loader::IOResult::Cleanup() {
104 free(uri);
105 free(library_uri);
106 free(payload);
107 }
108
109
110 // Send the Loader Initialization message to the service isolate. This
111 // message is sent the first time a loader is constructed for an isolate and
112 // seeds the service isolate with some initial state about this isolate.
113 void Loader::Init(const char* package_root,
114 const char* packages_file,
115 const char* working_directory) {
116 // This port delivers loading messages to the service isolate.
117 Dart_Port loader_port = Builtin::LoadPort();
118 ASSERT(loader_port != ILLEGAL_PORT);
119
120 // Keep in sync with loader.dart.
121 const intptr_t _Dart_kInitLoader = 4;
122
123 Dart_Handle request = Dart_NewList(7);
124 Dart_ListSetAt(request, 0, trace_loader ? Dart_True() : Dart_False());
125 Dart_ListSetAt(request, 1, Dart_NewInteger(Dart_GetMainPortId()));
126 Dart_ListSetAt(request, 2, Dart_NewInteger(_Dart_kInitLoader));
127 Dart_ListSetAt(request, 3, Dart_NewSendPort(port_));
128 Dart_ListSetAt(request, 4,
129 (package_root == NULL) ? Dart_Null() :
130 Dart_NewStringFromCString(package_root));
131 Dart_ListSetAt(request, 5,
132 (packages_file == NULL) ? Dart_Null() :
133 Dart_NewStringFromCString(packages_file));
134 Dart_ListSetAt(request, 6,
135 Dart_NewStringFromCString(working_directory));
136
137 bool success = Dart_Post(loader_port, request);
138 ASSERT(success);
139 }
140
141
142 // Forward a request from the tag handler to the service isolate.
143 void Loader::SendRequest(Dart_LibraryTag tag,
144 Dart_Handle url,
145 Dart_Handle library_url) {
146 // This port delivers loading messages to the service isolate.
147 Dart_Port loader_port = Builtin::LoadPort();
148 ASSERT(loader_port != ILLEGAL_PORT);
149
150 Dart_Handle request = Dart_NewList(6);
151 Dart_ListSetAt(request, 0, trace_loader ? Dart_True() : Dart_False());
152 Dart_ListSetAt(request, 1, Dart_NewInteger(Dart_GetMainPortId()));
153 Dart_ListSetAt(request, 2, Dart_NewInteger(tag));
154 Dart_ListSetAt(request, 3, Dart_NewSendPort(port_));
155
156 Dart_ListSetAt(request, 4, url);
157 Dart_ListSetAt(request, 5, library_url);
158
159 if (Dart_Post(loader_port, request)) {
160 MonitorLocker ml(monitor_);
161 pending_operations_++;
162 }
163 }
164
165
166 void Loader::QueueMessage(Dart_CObject* message) {
167 MonitorLocker ml(monitor_);
168 if (results_length_ == results_capacity_) {
169 // Grow to an initial capacity or double in size.
170 results_capacity_ = (results_capacity_ == 0) ? 4 : results_capacity_ * 2;
171 results_ =
172 reinterpret_cast<IOResult*>(
173 realloc(results_,
174 sizeof(IOResult) * results_capacity_));
175 ASSERT(results_ != NULL);
176 }
177 ASSERT(results_ != NULL);
178 ASSERT(results_length_ < results_capacity_);
179 results_[results_length_].Setup(message);
180 results_length_++;
181 ml.Notify();
182 }
183
184
185 void Loader::BlockUntilComplete() {
186 MonitorLocker ml(monitor_);
187
188 while (true) {
189 // If |ProcessQueueLocked| returns false, we've hit an error and should
190 // stop loading.
191 if (!ProcessQueueLocked()) {
192 break;
193 }
194
195 // When |pending_operations_| hits 0, we are done loading.
196 if (pending_operations_ == 0) {
197 break;
198 }
199
200 // Wait to be notified about new I/O results.
201 ml.Wait();
202 }
203 }
204
205
206 bool Loader::ProcessResultLocked(Loader::IOResult* result) {
207 // A negative result tag indicates a loading error occurred in the service
208 // isolate. The payload is a C string of the error message.
209 if (result->tag < 0) {
210 error_ =
211 Dart_NewUnhandledExceptionError(
212 Dart_NewStringFromUTF8(result->payload,
213 result->payload_length));
214
215 return false;
216 }
217
218 // We have to copy everything we care about out of |result| because after
219 // dropping the lock below |result| may no longer valid.
220 Dart_Handle uri =
221 Dart_NewStringFromCString(reinterpret_cast<char*>(result->uri));
222 Dart_Handle library_uri = Dart_Null();
223 if (result->library_uri != NULL) {
224 library_uri =
225 Dart_NewStringFromCString(reinterpret_cast<char*>(result->library_uri));
226 }
227 Dart_Handle source =
228 Dart_NewStringFromUTF8(result->payload,
229 result->payload_length);
230 intptr_t tag = result->tag;
231
232 // No touching.
233 result = NULL;
234
235 // We must drop the lock here because the tag handler may be recursively
236 // invoked and it will attempt to acquire the lock to queue more work.
237 monitor_->Exit();
238
239 Dart_Handle dart_result = Dart_Null();
240
241
242 switch (tag) {
243 case Dart_kImportTag:
244 dart_result = Dart_LoadLibrary(uri, source, 0, 0);
245 break;
246 case Dart_kSourceTag: {
247 ASSERT(library_uri != Dart_Null());
248 Dart_Handle library = Dart_LookupLibrary(library_uri);
249 ASSERT(!Dart_IsError(library));
250 dart_result = Dart_LoadSource(library, uri, source, 0, 0);
251 }
252 break;
253 case Dart_kScriptTag:
254 dart_result = Dart_LoadScript(uri, source, 0, 0);
255 break;
256 default:
257 UNREACHABLE();
258 }
259
260 // Re-acquire the lock before exiting the function (it was held before entry),
261 monitor_->Enter();
262 if (Dart_IsError(dart_result)) {
263 // Remember the error if we encountered one.
264 error_ = dart_result;
265 return false;
266 }
267
268 return true;
269 }
270
271
272 bool Loader::ProcessQueueLocked() {
273 bool hit_error = false;
274 for (intptr_t i = 0; i < results_length(); i++) {
275 if (!hit_error) {
276 hit_error = !ProcessResultLocked(&results_[i]);
277 }
278 pending_operations_--;
279 ASSERT(hit_error || (pending_operations_ >= 0));
280 results_[i].Cleanup();
281 }
282 results_length_ = 0;
283 return !hit_error;
284 }
285
286
287 static bool IsWindowsHost() {
288 #if defined(TARGET_OS_WINDOWS)
289 return true;
290 #else // defined(TARGET_OS_WINDOWS)
291 return false;
292 #endif // defined(TARGET_OS_WINDOWS)
293 }
294
295
296 Dart_Handle Loader::LibraryTagHandler(Dart_LibraryTag tag,
297 Dart_Handle library,
298 Dart_Handle url) {
299 if (tag == Dart_kCanonicalizeUrl) {
300 return Dart_DefaultCanonicalizeUrl(library, url);
301 }
302 const char* url_string = NULL;
303 Dart_Handle result = Dart_StringToCString(url, &url_string);
304 if (Dart_IsError(result)) {
305 return result;
306 }
307
308 // Special case for handling dart: imports and parts.
309 if (tag != Dart_kScriptTag) {
310 // Grab the library's url.
311 Dart_Handle library_url = Dart_LibraryUrl(library);
312 const char* library_url_string = NULL;
313 result = Dart_StringToCString(library_url, &library_url_string);
314 if (Dart_IsError(result)) {
315 return result;
316 }
317
318 bool is_dart_scheme_url = DartUtils::IsDartSchemeURL(url_string);
319 bool is_dart_library = DartUtils::IsDartSchemeURL(library_url_string);
320
321 if (is_dart_scheme_url || is_dart_library) {
322 return DartColonLibraryTagHandler(tag,
323 library,
324 url,
325 library_url_string,
326 url_string);
327 }
328 }
329
330 if (DartUtils::IsDartExtensionSchemeURL(url_string)) {
331 // Load a native code shared library to use in a native extension
332 if (tag != Dart_kImportTag) {
333 return DartUtils::NewError("Dart extensions must use import: '%s'",
334 url_string);
335 }
336 Dart_Handle library_url = Dart_LibraryUrl(library);
337 Dart_Handle library_file_path = DartUtils::LibraryFilePath(library_url);
338 const char* lib_path_str = NULL;
339 Dart_StringToCString(library_file_path, &lib_path_str);
340 const char* extension_path = DartUtils::RemoveScheme(url_string);
341 if (strchr(extension_path, '/') != NULL ||
342 (IsWindowsHost() && strchr(extension_path, '\\') != NULL)) {
343 return DartUtils::NewError(
344 "Relative paths for dart extensions are not supported: '%s'",
345 extension_path);
346 }
347 return Extensions::LoadExtension(lib_path_str,
348 extension_path,
349 library);
350 }
351
352 IsolateData* isolate_data =
353 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData());
354 ASSERT(isolate_data != NULL);
355
356 // Grab this isolate's loader.
357 Loader* loader = NULL;
358
359 // The outer invocation of the tag handler for this isolate. We make the outer
360 // invocation block and allow any nested invocations to operate in parallel.
361 bool blocking_call = !isolate_data->HasLoader();
362
363 if (!isolate_data->HasLoader()) {
364 // The isolate doesn't have a loader -- this is the outer invocation which
365 // will block.
366
367 // Setup the loader. The constructor does a bunch of leg work.
368 loader = new Loader(isolate_data);
369 loader->Init(isolate_data->package_root,
370 isolate_data->packages_file,
371 DartUtils::original_working_directory);
372 } else {
373 // The isolate has a loader -- this is an inner invocation that will queue
374 // work with the service isolate.
375 // Use the existing loader.
376 loader = isolate_data->loader();
377 }
378 ASSERT(loader != NULL);
379 ASSERT(isolate_data->HasLoader());
380
381 loader->SendRequest(tag,
382 url,
383 (library != Dart_Null()) ?
384 Dart_LibraryUrl(library) : Dart_Null());
385
386 if (blocking_call) {
387 // The outer invocation of the tag handler will block here until all nested
388 // invocations complete.
389 loader->BlockUntilComplete();
390
391 // Remember the error (if any).
392 Dart_Handle error = loader->error();
393 // Destroy the loader. The destructor does a bunch of leg work.
394 delete loader;
395
396 // An error occurred during loading.
397 if (Dart_IsError(error)) {
398 return error;
399 }
400
401 // Finalize loading. This will complete any futures for completed deferred
402 // loads.
403 error = Dart_FinalizeLoading(true);
404 if (Dart_IsError(error)) {
405 return error;
406 }
407 }
408 return Dart_Null();
409 }
410
411
412 Dart_Handle Loader::DartColonLibraryTagHandler(Dart_LibraryTag tag,
413 Dart_Handle library,
414 Dart_Handle url,
415 const char* library_url_string,
416 const char* url_string) {
417 // Handle canonicalization, 'import' and 'part' of 'dart:' libraries.
418 if (tag == Dart_kCanonicalizeUrl) {
419 // These will be handled internally.
420 return url;
421 } else if (tag == Dart_kImportTag) {
422 Builtin::BuiltinLibraryId id = Builtin::FindId(url_string);
423 if (id == Builtin::kInvalidLibrary) {
424 return DartUtils::NewError("The built-in library '%s' is not available"
425 " on the stand-alone VM.\n", url_string);
426 }
427 return Builtin::LoadLibrary(url, id);
428 } else {
429 ASSERT(tag == Dart_kSourceTag);
430 Builtin::BuiltinLibraryId id = Builtin::FindId(library_url_string);
431 if (id == Builtin::kInvalidLibrary) {
432 return DartUtils::NewError("The built-in library '%s' is not available"
433 " on the stand-alone VM. Trying to load"
434 " '%s'.\n", library_url_string, url_string);
435 }
436 // Prepend the library URI to form a unique script URI for the part.
437 intptr_t len = snprintf(NULL, 0, "%s/%s", library_url_string, url_string);
438 char* part_uri = reinterpret_cast<char*>(malloc(len + 1));
439 snprintf(part_uri, len + 1, "%s/%s", library_url_string, url_string);
440 Dart_Handle part_uri_obj = DartUtils::NewString(part_uri);
441 free(part_uri);
442 return Dart_LoadSource(library,
443 part_uri_obj,
444 Builtin::PartSource(id, url_string), 0, 0);
445 }
446 // All cases should have been handled above.
447 UNREACHABLE();
448 }
449
450
451 Mutex Loader::loader_infos_lock_;
452 Loader::LoaderInfo* Loader::loader_infos_ = NULL;
453 intptr_t Loader::loader_infos_length_ = 0;
454 intptr_t Loader::loader_infos_capacity_ = 0;
455
456
457 // Add a mapping from |port| to |isolate_data| (really the loader). When a
458 // native message arrives, we use this map to report the I/O result to the
459 // correct loader.
460 // This happens whenever an isolate begins loading.
461 void Loader::AddLoader(Dart_Port port, IsolateData* isolate_data) {
462 ASSERT(LoaderFor(port) == NULL);
463 if (loader_infos_length_ == loader_infos_capacity_) {
464 // Grow to an initial capacity or double in size.
465 loader_infos_capacity_ =
466 (loader_infos_capacity_ == 0) ? 4 : loader_infos_capacity_ * 2;
467 loader_infos_ =
468 reinterpret_cast<Loader::LoaderInfo*>(
469 realloc(loader_infos_,
470 sizeof(Loader::LoaderInfo) * loader_infos_capacity_));
471 ASSERT(loader_infos_ != NULL);
472 // Initialize new entries.
473 for (intptr_t i = loader_infos_length_; i < loader_infos_capacity_; i++) {
474 loader_infos_[i].port = ILLEGAL_PORT;
475 loader_infos_[i].isolate_data = NULL;
476 }
477 }
478 ASSERT(loader_infos_length_ < loader_infos_capacity_);
479 loader_infos_[loader_infos_length_].port = port;
480 loader_infos_[loader_infos_length_].isolate_data = isolate_data;
481 loader_infos_length_++;
482 ASSERT(LoaderFor(port) != NULL);
483 }
484
485
486 // Remove |port| from the map.
487 // This happens once an isolate has finished loading.
488 void Loader::RemoveLoader(Dart_Port port) {
489 const intptr_t index = LoaderIndexFor(port);
490 ASSERT(index >= 0);
491 const intptr_t last = loader_infos_length_ - 1;
492 ASSERT(last >= 0);
493 if (index != last) {
494 // Swap with the tail.
495 loader_infos_[index] = loader_infos_[last];
496 }
497 loader_infos_length_--;
498 }
499
500
501 intptr_t Loader::LoaderIndexFor(Dart_Port port) {
502 for (intptr_t i = 0; i < loader_infos_length_; i++) {
503 if (loader_infos_[i].port == port) {
504 return i;
505 }
506 }
507 return -1;
508 }
509
510
511 Loader* Loader::LoaderFor(Dart_Port port) {
512 intptr_t index = LoaderIndexFor(port);
513 if (index < 0) {
514 return NULL;
515 }
516 return loader_infos_[index].isolate_data->loader();
517 }
518
519
520 void Loader::NativeMessageHandler(Dart_Port dest_port_id,
521 Dart_CObject* message) {
522 Loader* loader = LoaderFor(dest_port_id);
523 if (loader == NULL) {
524 return;
525 }
526 loader->QueueMessage(message);
527 }
528
529 } // namespace bin
530 } // namespace dart
OLDNEW
« runtime/bin/builtin.dart ('K') | « runtime/bin/loader.h ('k') | runtime/bin/main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698