From af0ab36f7ca370e950ebcc9bb67b13bbae3c98f5 Mon Sep 17 00:00:00 2001 From: John McCutchan Date: Tue, 6 Oct 2015 07:58:14 -0700 Subject: [PATCH] Make dart:_vmservice a proper builtin library - Drops custom resource table for sources. - Stops loading dart:vmservice library by sources. R=rmacnak@google.com Review URL: https://codereview.chromium.org/1387043002 . --- runtime/bin/vmservice/vmservice_io.dart | 2 +- runtime/lib/mirrors.cc | 14 +- .../{vm/service => lib/vmservice}/client.dart | 0 .../service => lib/vmservice}/constants.dart | 0 .../service => lib/vmservice}/message.dart | 0 .../vmservice}/message_router.dart | 0 .../vmservice}/running_isolate.dart | 0 .../vmservice}/running_isolates.dart | 0 .../service => lib/vmservice}/vmservice.dart | 0 runtime/lib/vmservice_sources.gypi | 18 ++ runtime/vm/bootstrap.cc | 4 + runtime/vm/bootstrap.h | 1 + runtime/vm/bootstrap_natives.cc | 7 + runtime/vm/dart.cc | 2 +- runtime/vm/object.cc | 5 + runtime/vm/object.h | 1 + runtime/vm/object_store.cc | 1 + runtime/vm/object_store.h | 6 + runtime/vm/service_isolate.cc | 169 +----------------- runtime/vm/service_isolate.h | 9 +- runtime/vm/symbols.h | 2 +- runtime/vm/vm.gypi | 52 +++--- 22 files changed, 91 insertions(+), 202 deletions(-) rename runtime/{vm/service => lib/vmservice}/client.dart (100%) rename runtime/{vm/service => lib/vmservice}/constants.dart (100%) rename runtime/{vm/service => lib/vmservice}/message.dart (100%) rename runtime/{vm/service => lib/vmservice}/message_router.dart (100%) rename runtime/{vm/service => lib/vmservice}/running_isolate.dart (100%) rename runtime/{vm/service => lib/vmservice}/running_isolates.dart (100%) rename runtime/{vm/service => lib/vmservice}/vmservice.dart (100%) create mode 100644 runtime/lib/vmservice_sources.gypi diff --git a/runtime/bin/vmservice/vmservice_io.dart b/runtime/bin/vmservice/vmservice_io.dart index d9b0c754f17..eb44a6c52a5 100644 --- a/runtime/bin/vmservice/vmservice_io.dart +++ b/runtime/bin/vmservice/vmservice_io.dart @@ -8,7 +8,7 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'dart:isolate'; -import 'dart:vmservice'; +import 'dart:_vmservice'; part 'loader.dart'; part 'resources.dart'; diff --git a/runtime/lib/mirrors.cc b/runtime/lib/mirrors.cc index 65f7c62a632..2d5145f98f9 100644 --- a/runtime/lib/mirrors.cc +++ b/runtime/lib/mirrors.cc @@ -382,9 +382,17 @@ static RawInstance* CreateLibraryMirror(const Library& lib) { str = lib.name(); args.SetAt(1, str); str = lib.url(); - if (str.Equals("dart:_builtin") || str.Equals("dart:_blink")) { - // Censored library (grumble). - return Instance::null(); + const char* censored_libraries[] = { + "dart:_builtin", + "dart:_blink", + "dart:_vmservice", + NULL, + }; + for (intptr_t i = 0; censored_libraries[i] != NULL; i++) { + if (str.Equals(censored_libraries[i])) { + // Censored library (grumble). + return Instance::null(); + } } if (str.Equals("dart:io")) { // Hack around dart:io being loaded into non-service isolates in Dartium. diff --git a/runtime/vm/service/client.dart b/runtime/lib/vmservice/client.dart similarity index 100% rename from runtime/vm/service/client.dart rename to runtime/lib/vmservice/client.dart diff --git a/runtime/vm/service/constants.dart b/runtime/lib/vmservice/constants.dart similarity index 100% rename from runtime/vm/service/constants.dart rename to runtime/lib/vmservice/constants.dart diff --git a/runtime/vm/service/message.dart b/runtime/lib/vmservice/message.dart similarity index 100% rename from runtime/vm/service/message.dart rename to runtime/lib/vmservice/message.dart diff --git a/runtime/vm/service/message_router.dart b/runtime/lib/vmservice/message_router.dart similarity index 100% rename from runtime/vm/service/message_router.dart rename to runtime/lib/vmservice/message_router.dart diff --git a/runtime/vm/service/running_isolate.dart b/runtime/lib/vmservice/running_isolate.dart similarity index 100% rename from runtime/vm/service/running_isolate.dart rename to runtime/lib/vmservice/running_isolate.dart diff --git a/runtime/vm/service/running_isolates.dart b/runtime/lib/vmservice/running_isolates.dart similarity index 100% rename from runtime/vm/service/running_isolates.dart rename to runtime/lib/vmservice/running_isolates.dart diff --git a/runtime/vm/service/vmservice.dart b/runtime/lib/vmservice/vmservice.dart similarity index 100% rename from runtime/vm/service/vmservice.dart rename to runtime/lib/vmservice/vmservice.dart diff --git a/runtime/lib/vmservice_sources.gypi b/runtime/lib/vmservice_sources.gypi new file mode 100644 index 00000000000..e60a4c8830e --- /dev/null +++ b/runtime/lib/vmservice_sources.gypi @@ -0,0 +1,18 @@ +# Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file +# for details. All rights reserved. Use of this source code is governed by a +# BSD-style license that can be found in the LICENSE file. + +# Sources that make up the library "dart:_vmservice". + +{ + 'sources': [ + 'vmservice/vmservice.dart', + # The above file needs to be first as it imports required libraries. + 'vmservice/client.dart', + 'vmservice/constants.dart', + 'vmservice/running_isolate.dart', + 'vmservice/running_isolates.dart', + 'vmservice/message.dart', + 'vmservice/message_router.dart', + ], +} diff --git a/runtime/vm/bootstrap.cc b/runtime/vm/bootstrap.cc index 912aa1f36bc..a01f107034e 100644 --- a/runtime/vm/bootstrap.cc +++ b/runtime/vm/bootstrap.cc @@ -75,6 +75,10 @@ static bootstrap_lib_props bootstrap_libraries[] = { typed_data, Bootstrap::typed_data_source_paths_, Bootstrap::typed_data_patch_paths_), + INIT_LIBRARY(ObjectStore::kVMService, + _vmservice, + Bootstrap::vmservice_source_paths_, + NULL), { ObjectStore::kNone, NULL, NULL, NULL, NULL } }; diff --git a/runtime/vm/bootstrap.h b/runtime/vm/bootstrap.h index 76758b624b0..ddb7d74877e 100644 --- a/runtime/vm/bootstrap.h +++ b/runtime/vm/bootstrap.h @@ -33,6 +33,7 @@ class Bootstrap : public AllStatic { static const char* profiler_source_paths_[]; static const char* typed_data_source_paths_[]; static const char* utf_source_paths_[]; + static const char* vmservice_source_paths_[]; // Source path mapping for patch URI and 'parts'. static const char* async_patch_paths_[]; diff --git a/runtime/vm/bootstrap_natives.cc b/runtime/vm/bootstrap_natives.cc index 07c49280479..09cb7560795 100644 --- a/runtime/vm/bootstrap_natives.cc +++ b/runtime/vm/bootstrap_natives.cc @@ -10,6 +10,7 @@ #include "vm/dart_api_impl.h" #include "vm/object.h" #include "vm/object_store.h" +#include "vm/service_isolate.h" namespace dart { @@ -129,6 +130,12 @@ void Bootstrap::SetupNativeResolver() { ASSERT(!library.IsNull()); library.set_native_entry_resolver(resolver); library.set_native_entry_symbol_resolver(symbol_resolver); + + library = Library::VMServiceLibrary(); + ASSERT(!library.IsNull()); + library.set_native_entry_resolver( + reinterpret_cast( + &ServiceIsolate::NativeResolver)); } diff --git a/runtime/vm/dart.cc b/runtime/vm/dart.cc index 50e33aad512..8061e9d5eef 100644 --- a/runtime/vm/dart.cc +++ b/runtime/vm/dart.cc @@ -363,7 +363,7 @@ RawError* Dart::InitializeIsolate(const uint8_t* snapshot_buffer, void* data) { I->class_table()->Print(); } - ServiceIsolate::MaybeInjectVMServiceLibrary(I); + ServiceIsolate::MaybeMakeServiceIsolate(I); ServiceIsolate::SendIsolateStartupMessage(); I->debugger()->NotifyIsolateCreated(); diff --git a/runtime/vm/object.cc b/runtime/vm/object.cc index e9c1aaacf0b..bdcfac32bf0 100644 --- a/runtime/vm/object.cc +++ b/runtime/vm/object.cc @@ -10259,6 +10259,11 @@ RawLibrary* Library::TypedDataLibrary() { } +RawLibrary* Library::VMServiceLibrary() { + return Isolate::Current()->object_store()->vmservice_library(); +} + + const char* Library::ToCString() const { const String& name = String::Handle(url()); return OS::SCreate(Thread::Current()->zone(), diff --git a/runtime/vm/object.h b/runtime/vm/object.h index 3bfe15e4e5e..457f85f5cee 100644 --- a/runtime/vm/object.h +++ b/runtime/vm/object.h @@ -3491,6 +3491,7 @@ class Library : public Object { static RawLibrary* NativeWrappersLibrary(); static RawLibrary* ProfilerLibrary(); static RawLibrary* TypedDataLibrary(); + static RawLibrary* VMServiceLibrary(); // Eagerly compile all classes and functions in the library. static RawError* CompileAll(); diff --git a/runtime/vm/object_store.cc b/runtime/vm/object_store.cc index 8c32a4a5abe..88c77ebbcf6 100644 --- a/runtime/vm/object_store.cc +++ b/runtime/vm/object_store.cc @@ -71,6 +71,7 @@ ObjectStore::ObjectStore() profiler_library_(Library::null()), root_library_(Library::null()), typed_data_library_(Library::null()), + vmservice_library_(Library::null()), libraries_(GrowableObjectArray::null()), pending_classes_(GrowableObjectArray::null()), pending_functions_(GrowableObjectArray::null()), diff --git a/runtime/vm/object_store.h b/runtime/vm/object_store.h index fda986d8c6d..805b9b1b465 100644 --- a/runtime/vm/object_store.h +++ b/runtime/vm/object_store.h @@ -32,6 +32,7 @@ class ObjectStore { kMirrors, kProfiler, kTypedData, + kVMService, }; ~ObjectStore(); @@ -265,6 +266,7 @@ class ObjectStore { RawLibrary* mirrors_library() const { return mirrors_library_; } RawLibrary* profiler_library() const { return profiler_library_; } RawLibrary* typed_data_library() const { return typed_data_library_; } + RawLibrary* vmservice_library() const { return vmservice_library_; } void set_bootstrap_library(BootstrapLibraryId index, const Library& value) { switch (index) { @@ -301,6 +303,9 @@ class ObjectStore { case kTypedData: typed_data_library_ = value.raw(); break; + case kVMService: + vmservice_library_ = value.raw(); + break; default: UNREACHABLE(); } @@ -520,6 +525,7 @@ class ObjectStore { RawLibrary* profiler_library_; RawLibrary* root_library_; RawLibrary* typed_data_library_; + RawLibrary* vmservice_library_; RawGrowableObjectArray* libraries_; RawGrowableObjectArray* pending_classes_; RawGrowableObjectArray* pending_functions_; diff --git a/runtime/vm/service_isolate.cc b/runtime/vm/service_isolate.cc index c07638fcd9f..4bcd2d4d352 100644 --- a/runtime/vm/service_isolate.cc +++ b/runtime/vm/service_isolate.cc @@ -31,80 +31,6 @@ DEFINE_FLAG(bool, trace_service, false, "Trace VM service requests."); DEFINE_FLAG(bool, trace_service_pause_events, false, "Trace VM service isolate pause events."); -struct ResourcesEntry { - const char* path_; - const char* resource_; - int length_; -}; - -extern ResourcesEntry __service_resources_[]; - -class Resources { - public: - static const int kNoSuchInstance = -1; - static int ResourceLookup(const char* path, const char** resource) { - ResourcesEntry* table = ResourceTable(); - for (int i = 0; table[i].path_ != NULL; i++) { - const ResourcesEntry& entry = table[i]; - if (strcmp(path, entry.path_) == 0) { - *resource = entry.resource_; - ASSERT(entry.length_ > 0); - return entry.length_; - } - } - return kNoSuchInstance; - } - - static const char* Path(int idx) { - ASSERT(idx >= 0); - ResourcesEntry* entry = At(idx); - if (entry == NULL) { - return NULL; - } - ASSERT(entry->path_ != NULL); - return entry->path_; - } - - static int Length(int idx) { - ASSERT(idx >= 0); - ResourcesEntry* entry = At(idx); - if (entry == NULL) { - return kNoSuchInstance; - } - ASSERT(entry->path_ != NULL); - return entry->length_; - } - - static const uint8_t* Resource(int idx) { - ASSERT(idx >= 0); - ResourcesEntry* entry = At(idx); - if (entry == NULL) { - return NULL; - } - return reinterpret_cast(entry->resource_); - } - - private: - static ResourcesEntry* At(int idx) { - ASSERT(idx >= 0); - ResourcesEntry* table = ResourceTable(); - for (int i = 0; table[i].path_ != NULL; i++) { - if (idx == i) { - return &table[i]; - } - } - return NULL; - } - - static ResourcesEntry* ResourceTable() { - return &__service_resources_[0]; - } - - DISALLOW_ALLOCATION(); - DISALLOW_IMPLICIT_CONSTRUCTORS(Resources); -}; - - static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { void* new_ptr = realloc(reinterpret_cast(ptr), new_size); return reinterpret_cast(new_ptr); @@ -350,9 +276,9 @@ static ServiceNativeEntry _ServiceNativeEntries[] = { }; -static Dart_NativeFunction ServiceNativeResolver(Dart_Handle name, - int num_arguments, - bool* auto_setup_scope) { +Dart_NativeFunction ServiceIsolate::NativeResolver(Dart_Handle name, + int num_arguments, + bool* auto_setup_scope) { const Object& obj = Object::Handle(Api::UnwrapHandle(name)); if (!obj.IsString()) { return NULL; @@ -529,7 +455,7 @@ void ServiceIsolate::SetLoadPort(Dart_Port port) { } -void ServiceIsolate::MaybeInjectVMServiceLibrary(Isolate* I) { +void ServiceIsolate::MaybeMakeServiceIsolate(Isolate* I) { Thread* T = Thread::Current(); ASSERT(I == T->isolate()); ASSERT(I != NULL); @@ -543,46 +469,6 @@ void ServiceIsolate::MaybeInjectVMServiceLibrary(Isolate* I) { return; } SetServiceIsolate(I); - - StackZone zone(T); - HANDLESCOPE(T); - - // Register dart:vmservice library. - const String& url_str = String::Handle(Z, Symbols::DartVMService().raw()); - const Library& library = Library::Handle(Z, Library::New(url_str)); - library.Register(); - library.set_native_entry_resolver(ServiceNativeResolver); - - // Temporarily install our library tag handler. - I->set_library_tag_handler(LibraryTagHandler); - - // Get script source. - const char* resource = NULL; - const char* path = "/vmservice.dart"; - intptr_t r = Resources::ResourceLookup(path, &resource); - ASSERT(r != Resources::kNoSuchInstance); - ASSERT(resource != NULL); - const String& source_str = String::Handle(Z, - String::FromUTF8(reinterpret_cast(resource), r)); - ASSERT(!source_str.IsNull()); - const Script& script = Script::Handle(Z, - Script::New(url_str, source_str, RawScript::kLibraryTag)); - - // Compile script. - Dart_EnterScope(); // Need to enter scope for tag handler. - library.SetLoadInProgress(); - const Error& error = Error::Handle(Z, Compiler::Compile(library, script)); - if (!error.IsNull()) { - OS::PrintErr("vm-service: Isolate creation error: %s\n", - error.ToErrorCString()); - } - ASSERT(error.IsNull()); - Dart_Handle result = Dart_FinalizeLoading(false); - ASSERT(!Dart_IsError(result)); - Dart_ExitScope(); - - // Uninstall our library tag handler. - I->set_library_tag_handler(NULL); } @@ -804,51 +690,4 @@ void ServiceIsolate::Shutdown() { } } - -Dart_Handle ServiceIsolate::GetSource(const char* name) { - ASSERT(name != NULL); - int i = 0; - while (true) { - const char* path = Resources::Path(i); - if (path == NULL) { - break; - } - ASSERT(*path != '\0'); - // Skip the '/'. - path++; - if (strcmp(name, path) == 0) { - const uint8_t* str = Resources::Resource(i); - intptr_t length = Resources::Length(i); - return Dart_NewStringFromUTF8(str, length); - } - i++; - } - FATAL1("vm-service: Could not find embedded source file: %s ", name); - return Dart_Null(); -} - - -Dart_Handle ServiceIsolate::LibraryTagHandler(Dart_LibraryTag tag, - Dart_Handle library, - Dart_Handle url) { - if (tag == Dart_kCanonicalizeUrl) { - // url is already canonicalized. - return url; - } - if (tag != Dart_kSourceTag) { - FATAL("ServiceIsolate::LibraryTagHandler encountered an unexpected tag."); - } - ASSERT(tag == Dart_kSourceTag); - const char* url_string = NULL; - Dart_Handle result = Dart_StringToCString(url, &url_string); - if (Dart_IsError(result)) { - return result; - } - Dart_Handle source = GetSource(url_string); - if (Dart_IsError(source)) { - return source; - } - return Dart_LoadSource(library, url, source, 0, 0); -} - } // namespace dart diff --git a/runtime/vm/service_isolate.h b/runtime/vm/service_isolate.h index 86433c9a9cb..bb7e0290087 100644 --- a/runtime/vm/service_isolate.h +++ b/runtime/vm/service_isolate.h @@ -42,14 +42,14 @@ class ServiceIsolate : public AllStatic { static void ConstructExitMessageAndCache(Isolate* isolate); static void FinishedExiting(); static void FinishedInitializing(); - static void MaybeInjectVMServiceLibrary(Isolate* isolate); + static void MaybeMakeServiceIsolate(Isolate* isolate); static Dart_IsolateCreateCallback create_callback() { return create_callback_; } - static Dart_Handle GetSource(const char* name); - static Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, Dart_Handle library, - Dart_Handle url); + static Dart_NativeFunction NativeResolver(Dart_Handle name, + int num_arguments, + bool* auto_setup_scope); static Dart_IsolateCreateCallback create_callback_; static uint8_t* exit_message_; @@ -65,6 +65,7 @@ class ServiceIsolate : public AllStatic { friend class Dart; friend class RunServiceTask; friend class ServiceIsolateNatives; + friend class Bootstrap; }; } // namespace dart diff --git a/runtime/vm/symbols.h b/runtime/vm/symbols.h index c9fdbd4833a..d59ea20acec 100644 --- a/runtime/vm/symbols.h +++ b/runtime/vm/symbols.h @@ -343,7 +343,7 @@ class ObjectPointerVisitor; V(DartIsolate, "dart:isolate") \ V(DartMirrors, "dart:mirrors") \ V(DartTypedData, "dart:typed_data") \ - V(DartVMService, "dart:vmservice") \ + V(DartVMService, "dart:_vmservice") \ V(DartIOLibName, "dart.io") \ V(EvalSourceUri, "evaluate:source") \ V(_Random, "_Random") \ diff --git a/runtime/vm/vm.gypi b/runtime/vm/vm.gypi index e298592923c..1ccd0cbfa61 100644 --- a/runtime/vm/vm.gypi +++ b/runtime/vm/vm.gypi @@ -26,30 +26,23 @@ 'mirrors_cc_file': '<(gen_source_dir)/mirrors_gen.cc', 'mirrors_patch_cc_file': '<(gen_source_dir)/mirrors_patch_gen.cc', 'profiler_cc_file': '<(gen_source_dir)/profiler_gen.cc', - 'service_cc_file': '<(gen_source_dir)/service_gen.cc', 'snapshot_test_dat_file': '<(gen_source_dir)/snapshot_test.dat', 'snapshot_test_in_dat_file': 'snapshot_test_in.dat', 'snapshot_test_dart_file': 'snapshot_test.dart', 'typed_data_cc_file': '<(gen_source_dir)/typed_data_gen.cc', 'typed_data_patch_cc_file': '<(gen_source_dir)/typed_data_patch_gen.cc', + 'vmservice_cc_file': '<(gen_source_dir)/vmservice_gen.cc', }, 'targets': [ { 'target_name': 'libdart_vm', 'type': 'static_library', 'toolsets':['host', 'target'], - 'dependencies': [ - 'generate_service_cc_file#host' - ], 'includes': [ 'vm_sources.gypi', '../platform/platform_headers.gypi', '../platform/platform_sources.gypi', ], - 'sources': [ - # Include generated source files. - '<(service_cc_file)', - ], 'sources/': [ # Exclude all _test.[cc|h] files. ['exclude', '_test\\.(cc|h)$'], @@ -105,18 +98,11 @@ 'target_name': 'libdart_vm_nosnapshot', 'type': 'static_library', 'toolsets':['host', 'target'], - 'dependencies': [ - 'generate_service_cc_file#host' - ], 'includes': [ 'vm_sources.gypi', '../platform/platform_headers.gypi', '../platform/platform_sources.gypi', ], - 'sources': [ - # Include generated source files. - '<(service_cc_file)', - ], 'sources/': [ # Exclude all _test.[cc|h] files. ['exclude', '_test\\.(cc|h)$'], @@ -197,6 +183,7 @@ 'generate_profiler_cc_file#host', 'generate_typed_data_cc_file#host', 'generate_typed_data_patch_cc_file#host', + 'generate_vmservice_cc_file#host', ], 'includes': [ '../lib/async_sources.gypi', @@ -233,6 +220,7 @@ '<(profiler_cc_file)', '<(typed_data_cc_file)', '<(typed_data_patch_cc_file)', + '<(vmservice_cc_file)', ], 'include_dirs': [ '..', @@ -252,6 +240,7 @@ '../lib/math_sources.gypi', '../lib/mirrors_sources.gypi', '../lib/typed_data_sources.gypi', + '../lib/vmservice_sources.gypi', ], 'sources': [ 'bootstrap_nocore.cc', @@ -1128,32 +1117,41 @@ ] }, { - 'target_name': 'generate_service_cc_file', + 'target_name': 'generate_vmservice_cc_file', 'type': 'none', 'toolsets':['host'], 'includes': [ - 'service_sources.gypi', + '../lib/vmservice_sources.gypi', + ], + 'sources/': [ + # Exclude all .[cc|h] files. + # This is only here for reference. Excludes happen after + # variable expansion, so the script has to do its own + # exclude processing of the sources being passed. + ['exclude', '\\.cc|h$'], ], 'actions': [ { - 'action_name': 'generate_service_cc', + 'action_name': 'generate_vmservice_cc', 'inputs': [ - '../tools/create_resources.py', + '../tools/gen_library_src_paths.py', + '<(libgen_in_cc_file)', '<@(_sources)', ], 'outputs': [ - '<(service_cc_file)', + '<(vmservice_cc_file)', ], 'action': [ 'python', - 'tools/create_resources.py', - '--output', '<(service_cc_file)', - '--outer_namespace', 'dart', - '--table_name', 'service', - '--root_prefix', 'vm/service/', - '<@(_sources)' + 'tools/gen_library_src_paths.py', + '--output', '<(vmservice_cc_file)', + '--input_cc', '<(libgen_in_cc_file)', + '--include', 'vm/bootstrap.h', + '--var_name', 'dart::Bootstrap::vmservice_source_paths_', + '--library_name', 'dart:_vmservice', + '<@(_sources)', ], - 'message': 'Generating ''<(service_cc_file)'' file.' + 'message': 'Generating ''<(vmservice_cc_file)'' file.' }, ] },