From cf055d6b8830a61b2ae5b05077c1a95b0e3c4d23 Mon Sep 17 00:00:00 2001 From: Alexander Aprelev Date: Wed, 26 Sep 2018 14:10:53 +0000 Subject: [PATCH] [vm] Fix isolate reload tests so that they can pass kernel files for isolate reload. Change-Id: I695761a790354eacc0d617cb914d7a4ea2b652aa Reviewed-on: https://dart-review.googlesource.com/76300 Reviewed-by: Ryan Macnak --- runtime/vm/isolate.cc | 23 ++++++++++++++++++++++- runtime/vm/isolate.h | 5 +++++ runtime/vm/isolate_reload.cc | 35 ++++++++++++++++++++++------------- runtime/vm/isolate_reload.h | 4 +++- runtime/vm/unit_test.cc | 18 ++++++++++-------- runtime/vm/unit_test.h | 3 ++- 6 files changed, 64 insertions(+), 24 deletions(-) diff --git a/runtime/vm/isolate.cc b/runtime/vm/isolate.cc index d26ed2ef29a30..254c654afb062 100644 --- a/runtime/vm/isolate.cc +++ b/runtime/vm/isolate.cc @@ -1260,7 +1260,28 @@ bool Isolate::ReloadSources(JSONStream* js, ASSERT(!IsReloading()); SetHasAttemptedReload(true); reload_context_ = new IsolateReloadContext(this, js); - reload_context_->Reload(force_reload, root_script_url, packages_url); + reload_context_->Reload(force_reload, root_script_url, packages_url, + /* kernel_buffer= */ NULL, + /* kernel_buffer_size= */ 0); + bool success = !reload_context_->reload_aborted(); + if (!dont_delete_reload_context) { + DeleteReloadContext(); + } + return success; +} + +bool Isolate::ReloadKernel(JSONStream* js, + bool force_reload, + const uint8_t* kernel_buffer, + intptr_t kernel_buffer_size, + bool dont_delete_reload_context) { + ASSERT(!IsReloading()); + SetHasAttemptedReload(true); + reload_context_ = new IsolateReloadContext(this, js); + reload_context_->Reload(force_reload, + /* root_script_url= */ NULL, + /* packages_url= */ NULL, kernel_buffer, + kernel_buffer_size); bool success = !reload_context_->reload_aborted(); if (!dont_delete_reload_context) { DeleteReloadContext(); diff --git a/runtime/vm/isolate.h b/runtime/vm/isolate.h index c47a9d651b3c0..4c6163d674aa2 100644 --- a/runtime/vm/isolate.h +++ b/runtime/vm/isolate.h @@ -307,6 +307,11 @@ class Isolate : public BaseIsolate { const char* root_script_url = NULL, const char* packages_url = NULL, bool dont_delete_reload_context = false); + bool ReloadKernel(JSONStream* js, + bool force_reload, + const uint8_t* kernel_buffer = NULL, + intptr_t kernel_buffer_size = 0, + bool dont_delete_reload_context = false); #endif // !defined(PRODUCT) && !defined(DART_PRECOMPILED_RUNTIME) const char* MakeRunnable(); diff --git a/runtime/vm/isolate_reload.cc b/runtime/vm/isolate_reload.cc index 4b784f1153687..1df033400aad9 100644 --- a/runtime/vm/isolate_reload.cc +++ b/runtime/vm/isolate_reload.cc @@ -557,9 +557,12 @@ static void AcceptCompilation(Thread* thread) { } // NOTE: This function returns *after* FinalizeLoading is called. +// If [root_script_url] is null, attempt to load from [kernel_buffer]. void IsolateReloadContext::Reload(bool force_reload, const char* root_script_url, - const char* packages_url_) { + const char* packages_url_, + const uint8_t* kernel_buffer, + intptr_t kernel_buffer_size) { TIMELINE_SCOPE(Reload); Thread* thread = Thread::Current(); ASSERT(isolate() == thread->isolate()); @@ -610,18 +613,25 @@ void IsolateReloadContext::Reload(bool force_reload, // compiled, so ReadKernelFromFile returns NULL. kernel_program.set(kernel::Program::ReadFromFile(root_script_url)); if (kernel_program.get() == NULL) { - Dart_SourceFile* modified_scripts = NULL; - intptr_t modified_scripts_count = 0; - - FindModifiedSources(thread, force_reload, &modified_scripts, - &modified_scripts_count, packages_url_); - Dart_KernelCompilationResult retval; - { - TransitionVMToNative transition(thread); - retval = KernelIsolate::CompileToKernel(root_lib_url.ToCString(), NULL, - 0, modified_scripts_count, - modified_scripts, true, NULL); + if (kernel_buffer != NULL && kernel_buffer_size != 0) { + retval.kernel = const_cast(kernel_buffer); + retval.kernel_size = kernel_buffer_size; + retval.status = Dart_KernelCompilationStatus_Ok; + } else { + Dart_SourceFile* modified_scripts = NULL; + intptr_t modified_scripts_count = 0; + + FindModifiedSources(thread, force_reload, &modified_scripts, + &modified_scripts_count, packages_url_); + + { + TransitionVMToNative transition(thread); + retval = KernelIsolate::CompileToKernel( + root_lib_url.ToCString(), NULL, 0, modified_scripts_count, + modified_scripts, true, NULL); + did_kernel_compilation = true; + } } if (retval.status != Dart_KernelCompilationStatus_Ok) { @@ -634,7 +644,6 @@ void IsolateReloadContext::Reload(bool force_reload, CommonFinalizeTail(); return; } - did_kernel_compilation = true; // The ownership of the kernel buffer goes now to the VM. const ExternalTypedData& typed_data = ExternalTypedData::Handle( diff --git a/runtime/vm/isolate_reload.h b/runtime/vm/isolate_reload.h index c225685ba4c21..c1d29073cbdaf 100644 --- a/runtime/vm/isolate_reload.h +++ b/runtime/vm/isolate_reload.h @@ -137,7 +137,9 @@ class IsolateReloadContext { void Reload(bool force_reload, const char* root_script_url = NULL, - const char* packages_url = NULL); + const char* packages_url = NULL, + const uint8_t* kernel_buffer = NULL, + intptr_t kernel_buffer_size = 0); // All zone allocated objects must be allocated from this zone. Zone* zone() const { return zone_; } diff --git a/runtime/vm/unit_test.cc b/runtime/vm/unit_test.cc index dc76612e6eec3..d57729d0e0637 100644 --- a/runtime/vm/unit_test.cc +++ b/runtime/vm/unit_test.cc @@ -184,7 +184,8 @@ static Dart_Handle IsolateReloadTestLibSource() { } static void ReloadTest(Dart_NativeArguments native_args) { - Dart_Handle result = TestCase::TriggerReload(); + Dart_Handle result = TestCase::TriggerReload(/* kernel_buffer= */ NULL, + /* kernel_buffer_size= */ 0); if (Dart_IsError(result)) { Dart_PropagateError(result); } @@ -610,17 +611,18 @@ Dart_Handle TestCase::SetReloadTestScript(const char* script) { } } -Dart_Handle TestCase::TriggerReload() { +Dart_Handle TestCase::TriggerReload(const uint8_t* kernel_buffer, + intptr_t kernel_buffer_size) { Thread* thread = Thread::Current(); Isolate* isolate = thread->isolate(); JSONStream js; bool success = false; { TransitionNativeToVM transition(thread); - success = isolate->ReloadSources(&js, - false, // force_reload - NULL, NULL, - true); // dont_delete_reload_context + success = isolate->ReloadKernel(&js, + false, // force_reload + kernel_buffer, kernel_buffer_size, + true); // dont_delete_reload_context OS::PrintErr("RELOAD REPORT:\n%s\n", js.ToCString()); } @@ -662,12 +664,12 @@ Dart_Handle TestCase::ReloadTestScript(const char* script) { SetReloadTestScript(script); } - return TriggerReload(); + return TriggerReload(/* kernel_buffer= */ NULL, /* kernel_buffer_size= */ 0); } Dart_Handle TestCase::ReloadTestKernel(const uint8_t* kernel_buffer, intptr_t kernel_buffer_size) { - return TriggerReload(); + return TriggerReload(kernel_buffer, kernel_buffer_size); } #endif // !PRODUCT diff --git a/runtime/vm/unit_test.h b/runtime/vm/unit_test.h index 1115f6d6dc9b3..ecdf353d0158c 100644 --- a/runtime/vm/unit_test.h +++ b/runtime/vm/unit_test.h @@ -382,7 +382,8 @@ class TestCase : TestCaseBase { static Dart_Handle SetReloadTestScript(const char* script); // Initiates the reload. - static Dart_Handle TriggerReload(); + static Dart_Handle TriggerReload(const uint8_t* kernel_buffer, + intptr_t kernel_buffer_size); // Helper function which reloads the current isolate using |script|. static Dart_Handle ReloadTestScript(const char* script);