Revert "[VM] Dart_Initialize no longer crashes after Dart_Cleanup"

This reverts commit 519ee905f9ca8c50a34983501bb3bd745306a600.

Reason for revert: Seeing multiple crashes on Windows

Original change's description:
> [VM] Dart_Initialize no longer crashes after Dart_Cleanup
> 
> Change-Id: I3cfdab9553aad045f024b6f9aec0b40b08234007
> Reviewed-on: https://dart-review.googlesource.com/75786
> Reviewed-by: Ryan Macnak <rmacnak@google.com>
> Commit-Queue: Ben Konyi <bkonyi@google.com>

TBR=bkonyi@google.com,rmacnak@google.com,zra@google.com

Change-Id: I33ad79dbc3fcf44f93612ff63bd2d8431f6067c8
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://dart-review.googlesource.com/76342
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Ben Konyi <bkonyi@google.com>
diff --git a/runtime/bin/run_vm_tests.cc b/runtime/bin/run_vm_tests.cc
index 89a5add..b7616aa 100644
--- a/runtime/bin/run_vm_tests.cc
+++ b/runtime/bin/run_vm_tests.cc
@@ -150,15 +150,8 @@
     isolate = Dart_CreateIsolate(
         DART_KERNEL_ISOLATE_NAME, main, isolate_snapshot_data,
         isolate_snapshot_instructions, NULL, NULL, flags, isolate_data, error);
-    if (*error != NULL) {
-      free(*error);
-      *error = NULL;
-    }
   }
   if (isolate == NULL) {
-    delete isolate_data;
-    isolate_data = NULL;
-
     bin::dfe.Init();
     bin::dfe.LoadKernelService(&kernel_service_buffer,
                                &kernel_service_buffer_size);
@@ -293,10 +286,6 @@
     ASSERT(error == NULL);
   }
 
-  TesterState::vm_snapshot_data = dart::bin::vm_snapshot_data;
-  TesterState::create_callback = CreateIsolateAndSetup;
-  TesterState::cleanup_callback = CleanupIsolate;
-
   error = Dart::InitOnce(
       dart::bin::vm_snapshot_data, dart::bin::vm_snapshot_instructions,
       CreateIsolateAndSetup /* create */, NULL /* shutdown */,
@@ -315,10 +304,9 @@
   error = Dart::Cleanup();
   ASSERT(error == NULL);
 
-  TestCaseBase::RunAllRaw();
-
   bin::EventHandler::Stop();
 
+  TestCaseBase::RunAllRaw();
   // Print a warning message if no tests or benchmarks were matched.
   if (run_matches == 0) {
     bin::Log::PrintErr("No tests matched: %s\n", run_filter);
diff --git a/runtime/tests/vm/vm.status b/runtime/tests/vm/vm.status
index e769aa1..6730986 100644
--- a/runtime/tests/vm/vm.status
+++ b/runtime/tests/vm/vm.status
@@ -139,6 +139,10 @@
 dart/data_uri_import_test/utf16: Crash
 dart/data_uri_import_test/wrongmime: Crash
 
+[ $arch == x64 && ($compiler == dartk || $compiler == dartkb) && $system == windows && $strong ]
+cc/Profiler_BasicSourcePosition: Fail # http://dartbug.com/33224
+cc/Profiler_CodeTicks: Fail # dartbug.com/33337
+
 [ $arch == ia32 && $compiler != dartk && $compiler != dartkp && $compiler != dartkb && $system == windows && $runtime == vm && $mode == debug ]
 cc/BitTestImmediate: Crash # dartbug.com/34252
 
@@ -215,6 +219,8 @@
 cc/Profiler_FunctionTicks: Fail, Pass
 cc/Profiler_ToggleRecordAllocation: Fail, Pass
 cc/Profiler_TrivialRecordAllocation: Fail, Pass
+cc/Service_Address: Fail
+cc/Service_Code: Fail
 
 [ ($compiler == dartk || $compiler == dartkb) && $checked ]
 dart/redirection_type_shuffling_test/00: Pass # Works in --checked mode but not in --strong mode.
diff --git a/runtime/vm/code_observers.cc b/runtime/vm/code_observers.cc
index c1d982a..d8a9de9 100644
--- a/runtime/vm/code_observers.cc
+++ b/runtime/vm/code_observers.cc
@@ -47,7 +47,7 @@
   return false;
 }
 
-void CodeObservers::Cleanup() {
+void CodeObservers::DeleteAll() {
   for (intptr_t i = 0; i < observers_length_; i++) {
     delete observers_[i];
   }
@@ -57,9 +57,8 @@
 }
 
 void CodeObservers::InitOnce() {
-  if (mutex_ == NULL) {
-    mutex_ = new Mutex();
-  }
+  ASSERT(mutex_ == NULL);
+  mutex_ = new Mutex();
   ASSERT(mutex_ != NULL);
   OS::RegisterCodeObservers();
 }
diff --git a/runtime/vm/code_observers.h b/runtime/vm/code_observers.h
index f0d856b..f789675 100644
--- a/runtime/vm/code_observers.h
+++ b/runtime/vm/code_observers.h
@@ -68,7 +68,7 @@
   // Returns true if there is at least one active code observer.
   static bool AreActive();
 
-  static void Cleanup();
+  static void DeleteAll();
 
   static Mutex* mutex() { return mutex_; }
 
diff --git a/runtime/vm/compiler_test.cc b/runtime/vm/compiler_test.cc
index ae91b3e..8eb3586 100644
--- a/runtime/vm/compiler_test.cc
+++ b/runtime/vm/compiler_test.cc
@@ -195,7 +195,6 @@
     val = Instance::Cast(obj).EvaluateCompiledExpression(
         receiver_cls, kernel_bytes, kernel_length, Array::empty_array(),
         Array::empty_array(), TypeArguments::null_type_arguments());
-    free(const_cast<uint8_t*>(kernel_bytes));
   }
   EXPECT(!val.IsNull());
   EXPECT(!val.IsError());
diff --git a/runtime/vm/dart.cc b/runtime/vm/dart.cc
index a34ee6c..dcc1ac8 100644
--- a/runtime/vm/dart.cc
+++ b/runtime/vm/dart.cc
@@ -176,7 +176,7 @@
   Api::InitOnce();
   NativeSymbolResolver::InitOnce();
   NOT_IN_PRODUCT(Profiler::InitOnce());
-  SemiSpace::Init();
+  SemiSpace::InitOnce();
   NOT_IN_PRODUCT(Metric::InitOnce());
   StoreBuffer::InitOnce();
   MarkingStack::InitOnce();
@@ -454,7 +454,6 @@
   WaitForIsolateShutdown();
 
   IdleNotifier::Stop();
-
   // Shutdown the thread pool. On return, all thread pool threads have exited.
   if (FLAG_trace_shutdown) {
     OS::PrintErr("[+%" Pd64 "ms] SHUTDOWN: Deleting thread pool\n",
@@ -463,10 +462,6 @@
   delete thread_pool_;
   thread_pool_ = NULL;
 
-  Api::Cleanup();
-  delete predefined_handles_;
-  predefined_handles_ = NULL;
-
   // Disable creation of any new OSThread structures which means no more new
   // threads can do an EnterIsolate. This must come after isolate shutdown
   // because new threads may need to be spawned to shutdown the isolates.
@@ -490,18 +485,12 @@
   ShutdownIsolate();
   vm_isolate_ = NULL;
   ASSERT(Isolate::IsolateListLength() == 0);
-  PortMap::Cleanup();
   IdleNotifier::Cleanup();
+
   TargetCPUFeatures::Cleanup();
   MarkingStack::ShutDown();
   StoreBuffer::ShutDown();
-  MarkingStack::ShutDown();
-  Object::Cleanup();
-  SemiSpace::Cleanup();
-#if !defined(DART_PRECOMPILED_RUNTIME)
-  // Stubs are generated when not precompiled, clean them up.
-  StubCode::Cleanup();
-#endif
+
   // Delete the current thread's TLS and set it's TLS to null.
   // If it is the last thread then the destructor would call
   // OSThread::Cleanup.
@@ -517,7 +506,7 @@
     OS::PrintErr("[+%" Pd64 "ms] SHUTDOWN: Deleting code observers\n",
                  UptimeMillis());
   }
-  NOT_IN_PRODUCT(CodeObservers::Cleanup());
+  NOT_IN_PRODUCT(CodeObservers::DeleteAll());
   if (FLAG_support_timeline) {
     if (FLAG_trace_shutdown) {
       OS::PrintErr("[+%" Pd64 "ms] SHUTDOWN: Shutting down timeline\n",
diff --git a/runtime/vm/dart_api_impl.cc b/runtime/vm/dart_api_impl.cc
index 25b834d..7a079a0 100644
--- a/runtime/vm/dart_api_impl.cc
+++ b/runtime/vm/dart_api_impl.cc
@@ -479,9 +479,8 @@
 }
 
 void Api::InitOnce() {
-  if (api_native_key_ == kUnsetThreadLocalKey) {
-    api_native_key_ = OSThread::CreateThreadLocal();
-  }
+  ASSERT(api_native_key_ == kUnsetThreadLocalKey);
+  api_native_key_ = OSThread::CreateThreadLocal();
   ASSERT(api_native_key_ != kUnsetThreadLocalKey);
 }
 
@@ -512,13 +511,6 @@
   empty_string_handle_ = InitNewReadOnlyApiHandle(Symbols::Empty().raw());
 }
 
-void Api::Cleanup() {
-  true_handle_ = NULL;
-  false_handle_ = NULL;
-  null_handle_ = NULL;
-  empty_string_handle_ = NULL;
-}
-
 bool Api::StringGetPeerHelper(NativeArguments* arguments,
                               int arg_index,
                               void** peer) {
diff --git a/runtime/vm/dart_api_impl.h b/runtime/vm/dart_api_impl.h
index 6c5c322..76747d5 100644
--- a/runtime/vm/dart_api_impl.h
+++ b/runtime/vm/dart_api_impl.h
@@ -244,9 +244,6 @@
   // Allocates handles for objects in the VM isolate.
   static void InitHandles();
 
-  // Cleanup
-  static void Cleanup();
-
   // Helper function to get the peer value of an external string object.
   static bool StringGetPeerHelper(NativeArguments* args,
                                   int arg_index,
diff --git a/runtime/vm/dart_api_impl_test.cc b/runtime/vm/dart_api_impl_test.cc
index bf6f3c0..9b50641 100644
--- a/runtime/vm/dart_api_impl_test.cc
+++ b/runtime/vm/dart_api_impl_test.cc
@@ -4,7 +4,6 @@
 
 #include "vm/dart_api_impl.h"
 #include "bin/builtin.h"
-#include "bin/dartutils.h"
 #include "include/dart_api.h"
 #include "include/dart_native_api.h"
 #include "include/dart_tools_api.h"
@@ -13,7 +12,6 @@
 #include "platform/utils.h"
 #include "vm/class_finalizer.h"
 #include "vm/compiler/jit/compiler.h"
-#include "vm/dart.h"
 #include "vm/dart_api_state.h"
 #include "vm/debugger_api_impl_test.h"
 #include "vm/heap/verifier.h"
@@ -28,35 +26,6 @@
 
 #ifndef PRODUCT
 
-UNIT_TEST_CASE(DartAPI_DartInitializeAfterCleanup) {
-  Dart_InitializeParams params;
-  memset(&params, 0, sizeof(Dart_InitializeParams));
-  params.version = DART_INITIALIZE_PARAMS_CURRENT_VERSION;
-  params.vm_snapshot_data = TesterState::vm_snapshot_data;
-  params.create = TesterState::create_callback;
-  params.shutdown = TesterState::shutdown_callback;
-  params.cleanup = TesterState::cleanup_callback;
-  params.start_kernel_isolate = true;
-
-  // Reinitialize and ensure we can execute Dart code.
-  EXPECT(Dart_Initialize(&params) == NULL);
-  {
-    TestIsolateScope scope;
-    const char* kScriptChars =
-        "int testMain() {\n"
-        "  return 42;\n"
-        "}\n";
-    Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
-    EXPECT_VALID(lib);
-    Dart_Handle result = Dart_Invoke(lib, NewString("testMain"), 0, NULL);
-    EXPECT_VALID(result);
-    int64_t value = 0;
-    EXPECT_VALID(Dart_IntegerToInt64(result, &value));
-    EXPECT_EQ(42, value);
-  }
-  EXPECT(Dart_Cleanup() == NULL);
-}
-
 TEST_CASE(DartAPI_ErrorHandleBasics) {
   const char* kScriptChars =
       "void testMain() {\n"
@@ -88,6 +57,7 @@
   EXPECT(Dart_IsError(Dart_ErrorGetException(instance)));
   EXPECT(Dart_IsError(Dart_ErrorGetException(error)));
   EXPECT_VALID(Dart_ErrorGetException(exception));
+
   EXPECT(Dart_IsError(Dart_ErrorGetStackTrace(instance)));
   EXPECT(Dart_IsError(Dart_ErrorGetStackTrace(error)));
   EXPECT_VALID(Dart_ErrorGetStackTrace(exception));
@@ -3569,7 +3539,6 @@
     if (error != NULL) {
       return Dart_NewApiError(error);
     }
-    TestCaseBase::AddToKernelBuffers(kernel_buffer);
     return Dart_LoadScriptFromKernel(kernel_buffer, kernel_buffer_size);
   }
 }
diff --git a/runtime/vm/debugger_api_impl_test.cc b/runtime/vm/debugger_api_impl_test.cc
index 88792e5..b597adb 100644
--- a/runtime/vm/debugger_api_impl_test.cc
+++ b/runtime/vm/debugger_api_impl_test.cc
@@ -268,17 +268,14 @@
 
     const uint8_t* kernel_bytes = compilation_result.kernel;
     intptr_t kernel_length = compilation_result.kernel_size;
-    Dart_Handle result = Api::NewHandle(
-        T,
-        lib.EvaluateCompiledExpression(kernel_bytes, kernel_length,
-                                       /* type_definitions= */
-                                       Array::empty_array(),
-                                       /* param_values= */
-                                       Array::empty_array(),
-                                       /* type_param_values= */
-                                       TypeArguments::null_type_arguments()));
-    free(const_cast<uint8_t*>(kernel_bytes));
-    return result;
+    return Api::NewHandle(T, lib.EvaluateCompiledExpression(
+                                 kernel_bytes, kernel_length,
+                                 /* type_definitions= */
+                                 Array::empty_array(),
+                                 /* param_values= */
+                                 Array::empty_array(),
+                                 /* type_param_values= */
+                                 TypeArguments::null_type_arguments()));
   }
 }
 
diff --git a/runtime/vm/heap/pointer_block.cc b/runtime/vm/heap/pointer_block.cc
index 50443fc..2d0d2a1 100644
--- a/runtime/vm/heap/pointer_block.cc
+++ b/runtime/vm/heap/pointer_block.cc
@@ -29,15 +29,13 @@
 template <int BlockSize>
 void BlockStack<BlockSize>::InitOnce() {
   global_empty_ = new List();
-  if (global_mutex_ == NULL) {
-    global_mutex_ = new Mutex();
-  }
+  global_mutex_ = new Mutex();
 }
 
 template <int BlockSize>
 void BlockStack<BlockSize>::ShutDown() {
   delete global_empty_;
-  global_empty_ = NULL;
+  delete global_mutex_;
 }
 
 template <int BlockSize>
diff --git a/runtime/vm/heap/scavenger.cc b/runtime/vm/heap/scavenger.cc
index 17872dd..b732ff1 100644
--- a/runtime/vm/heap/scavenger.cc
+++ b/runtime/vm/heap/scavenger.cc
@@ -289,19 +289,12 @@
 Mutex* SemiSpace::mutex_ = NULL;
 SemiSpace* SemiSpace::cache_ = NULL;
 
-void SemiSpace::Init() {
-  if (mutex_ == NULL) {
-    mutex_ = new Mutex();
-  }
+void SemiSpace::InitOnce() {
+  ASSERT(mutex_ == NULL);
+  mutex_ = new Mutex();
   ASSERT(mutex_ != NULL);
 }
 
-void SemiSpace::Cleanup() {
-  MutexLocker locker(mutex_);
-  delete cache_;
-  cache_ = NULL;
-}
-
 SemiSpace* SemiSpace::New(intptr_t size_in_words, const char* name) {
   {
     MutexLocker locker(mutex_);
diff --git a/runtime/vm/heap/scavenger.h b/runtime/vm/heap/scavenger.h
index 412395d..45e0c3c 100644
--- a/runtime/vm/heap/scavenger.h
+++ b/runtime/vm/heap/scavenger.h
@@ -28,8 +28,7 @@
 // Wrapper around VirtualMemory that adds caching and handles the empty case.
 class SemiSpace {
  public:
-  static void Init();
-  static void Cleanup();
+  static void InitOnce();
 
   // Get a space of the given size. Returns NULL on out of memory. If size is 0,
   // returns an empty space: pointer(), start() and end() all return NULL.
diff --git a/runtime/vm/isolate.cc b/runtime/vm/isolate.cc
index 50e1b56..d26ed2e 100644
--- a/runtime/vm/isolate.cc
+++ b/runtime/vm/isolate.cc
@@ -1041,9 +1041,7 @@
 
 void Isolate::InitOnce() {
   create_callback_ = NULL;
-  if (isolates_list_monitor_ == NULL) {
-    isolates_list_monitor_ = new Monitor();
-  }
+  isolates_list_monitor_ = new Monitor();
   ASSERT(isolates_list_monitor_ != NULL);
   EnableIsolateCreation();
 }
diff --git a/runtime/vm/isolate_reload.cc b/runtime/vm/isolate_reload.cc
index 9cb1c82..4b784f1 100644
--- a/runtime/vm/isolate_reload.cc
+++ b/runtime/vm/isolate_reload.cc
@@ -629,9 +629,6 @@
         const String& error_str = String::Handle(String::New(retval.error));
         free(retval.error);
         const ApiError& error = ApiError::Handle(ApiError::New(error_str));
-        if (retval.kernel != NULL) {
-          free(const_cast<uint8_t*>(retval.kernel));
-        }
         AddReasonForCancelling(new Aborted(zone_, error));
         ReportReasonsForCancelling();
         CommonFinalizeTail();
diff --git a/runtime/vm/isolate_reload_test.cc b/runtime/vm/isolate_reload_test.cc
index 2d1f254..2b0594e 100644
--- a/runtime/vm/isolate_reload_test.cc
+++ b/runtime/vm/isolate_reload_test.cc
@@ -136,7 +136,6 @@
 
     lib = TestCase::ReloadTestKernel(kernel_buffer, kernel_buffer_size);
     EXPECT_VALID(lib);
-    free(const_cast<uint8_t*>(kernel_buffer));
   }
   result = Dart_Invoke(lib, NewString("main"), 0, NULL);
   result = Dart_IntegerToInt64(result, &value);
@@ -198,7 +197,6 @@
 
     lib = TestCase::ReloadTestKernel(kernel_buffer, kernel_buffer_size);
     EXPECT_VALID(lib);
-    free(const_cast<uint8_t*>(kernel_buffer));
   }
   result = Dart_Invoke(lib, NewString("main"), 0, NULL);
   result = Dart_IntegerToInt64(result, &value);
@@ -276,7 +274,6 @@
 
     lib = TestCase::ReloadTestKernel(kernel_buffer, kernel_buffer_size);
     EXPECT_VALID(lib);
-    free(const_cast<uint8_t*>(kernel_buffer));
   }
   result = Dart_Invoke(lib, NewString("main"), 0, NULL);
   result = Dart_IntegerToInt64(result, &value);
diff --git a/runtime/vm/kernel_isolate.cc b/runtime/vm/kernel_isolate.cc
index f0e228d..7a132b5 100644
--- a/runtime/vm/kernel_isolate.cc
+++ b/runtime/vm/kernel_isolate.cc
@@ -221,8 +221,6 @@
 };
 
 void KernelIsolate::Run() {
-  MonitorLocker ml(monitor_);
-  initializing_ = true;
   // Grab the isolate create callback here to avoid race conditions with tests
   // that change this after Dart_Initialize returns.
   create_callback_ = Isolate::CreateCallback();
diff --git a/runtime/vm/message_handler_test.cc b/runtime/vm/message_handler_test.cc
index 0677178..75e0077 100644
--- a/runtime/vm/message_handler_test.cc
+++ b/runtime/vm/message_handler_test.cc
@@ -40,10 +40,7 @@
         end_called_(false),
         results_(NULL) {}
 
-  ~TestMessageHandler() {
-    PortMap::ClosePorts(this);
-    delete[] port_buffer_;
-  }
+  ~TestMessageHandler() { delete[] port_buffer_; }
 
   void MessageNotify(Message::Priority priority) { notify_count_++; }
 
@@ -236,6 +233,7 @@
   EXPECT_EQ(port2, ports[0]);
   EXPECT_EQ(port3, ports[1]);
   EXPECT_EQ(port1, ports[2]);
+  PortMap::ClosePorts(&handler);
 }
 
 VM_UNIT_TEST_CASE(MessageHandler_HandleNextMessage_ProcessOOBAfterError) {
@@ -396,6 +394,7 @@
   }
   handler_peer.decrement_live_ports();
   EXPECT(!handler.HasLivePorts());
+  PortMap::ClosePorts(&handler);
   delete[] ports;
 }
 
diff --git a/runtime/vm/metrics.cc b/runtime/vm/metrics.cc
index d2208ac..94fd0bf 100644
--- a/runtime/vm/metrics.cc
+++ b/runtime/vm/metrics.cc
@@ -54,7 +54,7 @@
   RegisterWithVM();
 }
 
-void Metric::CleanupInstance() {
+Metric::~Metric() {
   // Only deregister metrics which had been registered. Metrics without a name
   // are from shallow copy isolates.
   if (name_ != NULL) {
@@ -66,10 +66,6 @@
   }
 }
 
-Metric::~Metric() {
-  CleanupInstance();
-}
-
 #ifndef PRODUCT
 static const char* UnitString(intptr_t unit) {
   switch (unit) {
@@ -325,10 +321,6 @@
     }
     OS::PrintErr("\n");
   }
-#define VM_METRIC_CLEANUP(type, variable, name, unit)                          \
-  vm_metric_##variable##_.CleanupInstance();
-  VM_METRIC_LIST(VM_METRIC_CLEANUP);
-#undef VM_METRIC_CLEANUP
 }
 
 MaxMetric::MaxMetric() : Metric() {
diff --git a/runtime/vm/metrics.h b/runtime/vm/metrics.h
index 9e58fdf..530cc4d 100644
--- a/runtime/vm/metrics.h
+++ b/runtime/vm/metrics.h
@@ -57,8 +57,6 @@
   // Initialize and register a metric for the VM.
   void Init(const char* name, const char* description, Unit unit);
 
-  void CleanupInstance();
-
   virtual ~Metric();
 
 #ifndef PRODUCT
diff --git a/runtime/vm/object.cc b/runtime/vm/object.cc
index 84485ea..0a4719f 100644
--- a/runtime/vm/object.cc
+++ b/runtime/vm/object.cc
@@ -950,46 +950,6 @@
   void_type_->SetTypeTestingStub(instr);
 }
 
-void Object::Cleanup() {
-  null_ = reinterpret_cast<RawObject*>(RAW_NULL);
-  class_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  dynamic_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  void_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  unresolved_class_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  type_arguments_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  patch_class_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  function_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  closure_data_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  signature_data_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  redirection_data_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  field_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  literal_token_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  token_stream_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  script_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  library_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  namespace_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  kernel_program_info_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  code_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  instructions_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  object_pool_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  pc_descriptors_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  code_source_map_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  stackmap_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  var_descriptors_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  exception_handlers_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  context_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  context_scope_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  singletargetcache_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  unlinkedcall_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  icdata_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  megamorphic_cache_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  subtypetestcache_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  api_error_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  language_error_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  unhandled_exception_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-  unwind_error_class_ = reinterpret_cast<RawClass*>(RAW_NULL);
-}
-
 // An object visitor which will mark all visited objects. This is used to
 // premark all objects in the vm_isolate_ heap.  Also precalculates hash
 // codes so that we can get the identity hash code of objects in the read-
@@ -12521,10 +12481,6 @@
   kernel::KernelLoader loader(kernel_pgm);
   const Object& result = Object::Handle(
       loader.LoadExpressionEvaluationFunction(library_url, klass));
-
-  delete kernel_pgm;
-  kernel_pgm = NULL;
-
   if (result.IsError()) return result.raw();
 
   const Function& callee = Function::Cast(result);
diff --git a/runtime/vm/object.h b/runtime/vm/object.h
index 8025cc8..4aace8f 100644
--- a/runtime/vm/object.h
+++ b/runtime/vm/object.h
@@ -465,8 +465,6 @@
   static void FinalizeVMIsolate(Isolate* isolate);
   static void FinalizeReadOnlyObject(RawObject* object);
 
-  static void Cleanup();
-
   // Initialize a new isolate either from a Kernel IR, from source, or from a
   // snapshot.
   static RawError* Init(Isolate* isolate,
diff --git a/runtime/vm/os_android.cc b/runtime/vm/os_android.cc
index 8a35d40..78e7e45 100644
--- a/runtime/vm/os_android.cc
+++ b/runtime/vm/os_android.cc
@@ -357,6 +357,12 @@
 }
 
 void OS::InitOnce() {
+  // TODO(5411554): For now we check that initonce is called only once,
+  // Once there is more formal mechanism to call InitOnce we can move
+  // this check there.
+  static bool init_once_called = false;
+  ASSERT(init_once_called == false);
+  init_once_called = true;
 }
 
 void OS::Shutdown() {}
diff --git a/runtime/vm/os_fuchsia.cc b/runtime/vm/os_fuchsia.cc
index e03fbcf..a6b7a18 100644
--- a/runtime/vm/os_fuchsia.cc
+++ b/runtime/vm/os_fuchsia.cc
@@ -254,6 +254,12 @@
 }
 
 void OS::InitOnce() {
+  // TODO(5411554): For now we check that initonce is called only once,
+  // Once there is more formal mechanism to call InitOnce we can move
+  // this check there.
+  static bool init_once_called = false;
+  ASSERT(init_once_called == false);
+  init_once_called = true;
   auto environment_services = std::make_shared<component::Services>();
   auto env_service_root = component::subtle::CreateStaticServiceRootHandle();
   environment_services->Bind(std::move(env_service_root));
diff --git a/runtime/vm/os_linux.cc b/runtime/vm/os_linux.cc
index 0058494..13936fb 100644
--- a/runtime/vm/os_linux.cc
+++ b/runtime/vm/os_linux.cc
@@ -655,7 +655,14 @@
   va_end(args);
 }
 
-void OS::InitOnce() {}
+void OS::InitOnce() {
+  // TODO(5411554): For now we check that initonce is called only once,
+  // Once there is more formal mechanism to call InitOnce we can move
+  // this check there.
+  static bool init_once_called = false;
+  ASSERT(init_once_called == false);
+  init_once_called = true;
+}
 
 void OS::Shutdown() {}
 
diff --git a/runtime/vm/os_macos.cc b/runtime/vm/os_macos.cc
index 62e8cae..e423c7a 100644
--- a/runtime/vm/os_macos.cc
+++ b/runtime/vm/os_macos.cc
@@ -303,6 +303,13 @@
 }
 
 void OS::InitOnce() {
+  // TODO(5411554): For now we check that initonce is called only once,
+  // Once there is more formal mechanism to call InitOnce we can move
+  // this check there.
+  static bool init_once_called = false;
+  ASSERT(init_once_called == false);
+  init_once_called = true;
+
   // See https://github.com/dart-lang/sdk/issues/29539
   // This is a workaround for a macos bug, we eagerly call localtime_r so that
   // libnotify is initialized early before any fork happens.
diff --git a/runtime/vm/os_thread.cc b/runtime/vm/os_thread.cc
index 13ef8a4..125dabf 100644
--- a/runtime/vm/os_thread.cc
+++ b/runtime/vm/os_thread.cc
@@ -138,15 +138,13 @@
 
 void OSThread::InitOnce() {
   // Allocate the global OSThread lock.
-  if (thread_list_lock_ == NULL) {
-    thread_list_lock_ = new Mutex();
-  }
+  ASSERT(thread_list_lock_ == NULL);
+  thread_list_lock_ = new Mutex();
   ASSERT(thread_list_lock_ != NULL);
 
   // Create the thread local key.
-  if (thread_key_ == kUnsetThreadLocalKey) {
-    thread_key_ = CreateThreadLocal(DeleteThread);
-  }
+  ASSERT(thread_key_ == kUnsetThreadLocalKey);
+  thread_key_ = CreateThreadLocal(DeleteThread);
   ASSERT(thread_key_ != kUnsetThreadLocalKey);
 
   // Enable creation of OSThread structures in the VM.
diff --git a/runtime/vm/os_win.cc b/runtime/vm/os_win.cc
index 333cb87..c9a102b 100644
--- a/runtime/vm/os_win.cc
+++ b/runtime/vm/os_win.cc
@@ -313,10 +313,11 @@
 }
 
 void OS::InitOnce() {
+  // TODO(5411554): For now we check that initonce is called only once,
+  // Once there is more formal mechanism to call InitOnce we can move
+  // this check there.
   static bool init_once_called = false;
-  if (init_once_called) {
-    return;
-  }
+  ASSERT(init_once_called == false);
   init_once_called = true;
   // Do not pop up a message box when abort is called.
   _set_abort_behavior(0, _WRITE_ABORT_MSG);
diff --git a/runtime/vm/port.cc b/runtime/vm/port.cc
index d594596..27ee26d 100644
--- a/runtime/vm/port.cc
+++ b/runtime/vm/port.cc
@@ -277,10 +277,7 @@
 }
 
 void PortMap::InitOnce() {
-  if (mutex_ == NULL) {
-    mutex_ = new Mutex();
-  }
-  ASSERT(mutex_ != NULL);
+  mutex_ = new Mutex();
   prng_ = new Random();
 
   static const intptr_t kInitialCapacity = 8;
@@ -293,22 +290,6 @@
   deleted_ = 0;
 }
 
-void PortMap::Cleanup() {
-  ASSERT(map_ != NULL);
-  ASSERT(prng_ != NULL);
-  for (intptr_t i = 0; i < capacity_; ++i) {
-    auto handler = map_[i].handler;
-    if (handler != NULL && handler != deleted_entry_) {
-      ClosePorts(handler);
-      delete handler;
-    }
-  }
-  delete prng_;
-  prng_ = NULL;
-  delete[] map_;
-  map_ = NULL;
-}
-
 void PortMap::PrintPortsForMessageHandler(MessageHandler* handler,
                                           JSONStream* stream) {
 #ifndef PRODUCT
diff --git a/runtime/vm/port.h b/runtime/vm/port.h
index 9a05151..113562e 100644
--- a/runtime/vm/port.h
+++ b/runtime/vm/port.h
@@ -55,7 +55,6 @@
   static Isolate* GetIsolate(Dart_Port id);
 
   static void InitOnce();
-  static void Cleanup();
 
   static void PrintPortsForMessageHandler(MessageHandler* handler,
                                           JSONStream* stream);
diff --git a/runtime/vm/service_test.cc b/runtime/vm/service_test.cc
index a48c0e6..d132892 100644
--- a/runtime/vm/service_test.cc
+++ b/runtime/vm/service_test.cc
@@ -30,10 +30,7 @@
  public:
   ServiceTestMessageHandler() : _msg(NULL) {}
 
-  ~ServiceTestMessageHandler() {
-    PortMap::ClosePorts(this);
-    free(_msg);
-  }
+  ~ServiceTestMessageHandler() { free(_msg); }
 
   MessageStatus HandleMessage(Message* message) {
     if (_msg != NULL) {
diff --git a/runtime/vm/stub_code.cc b/runtime/vm/stub_code.cc
index 2736d96..472a825 100644
--- a/runtime/vm/stub_code.cc
+++ b/runtime/vm/stub_code.cc
@@ -47,11 +47,6 @@
   // Stubs will be loaded from the snapshot.
   UNREACHABLE();
 }
-
-void StubCode::Cleanup() {
-  // Stubs will be loaded from the snapshot.
-  UNREACHABLE();
-}
 #else
 
 #define STUB_CODE_GENERATE(name)                                               \
@@ -66,16 +61,6 @@
 
 #undef STUB_CODE_GENERATE
 
-#define STUB_CODE_CLEANUP(name)                                                \
-  delete entries_[k##name##Index];                                             \
-  entries_[k##name##Index] = NULL;
-
-void StubCode::Cleanup() {
-  VM_STUB_CODE_LIST(STUB_CODE_CLEANUP);
-}
-
-#undef STUB_CODE_CLEANUP
-
 RawCode* StubCode::Generate(const char* name,
                             void (*GenerateStub)(Assembler* assembler)) {
   ObjectPoolWrapper object_pool_wrapper;
diff --git a/runtime/vm/stub_code.h b/runtime/vm/stub_code.h
index 55dad0b..73ff3dd 100644
--- a/runtime/vm/stub_code.h
+++ b/runtime/vm/stub_code.h
@@ -150,7 +150,6 @@
   // only once and the stub code resides in the vm_isolate heap.
   static void InitOnce();
 
-  static void Cleanup();
   static void VisitObjectPointers(ObjectPointerVisitor* visitor);
 
   // Returns true if stub code has been initialized.
diff --git a/runtime/vm/unit_test.cc b/runtime/vm/unit_test.cc
index f262110..dc76612 100644
--- a/runtime/vm/unit_test.cc
+++ b/runtime/vm/unit_test.cc
@@ -37,11 +37,6 @@
 const uint8_t* platform_strong_dill = kPlatformStrongDill;
 const intptr_t platform_strong_dill_size = kPlatformStrongDillSize;
 
-const uint8_t* TesterState::vm_snapshot_data = NULL;
-Dart_IsolateCreateCallback TesterState::create_callback = NULL;
-Dart_IsolateShutdownCallback TesterState::shutdown_callback = NULL;
-Dart_IsolateCleanupCallback TesterState::cleanup_callback = NULL;
-
 DEFINE_FLAG(bool,
             use_dart_frontend,
             true,
@@ -49,14 +44,8 @@
 
 DECLARE_FLAG(bool, strong);
 
-void KernelBufferList::AddBufferToList(const uint8_t* kernel_buffer) {
-  next_ = new KernelBufferList(kernel_buffer_, next_);
-  kernel_buffer_ = kernel_buffer;
-}
-
 TestCaseBase* TestCaseBase::first_ = NULL;
 TestCaseBase* TestCaseBase::tail_ = NULL;
-KernelBufferList* TestCaseBase::current_kernel_buffers_ = NULL;
 
 TestCaseBase::TestCaseBase(const char* name)
     : raw_test_(false), next_(NULL), name_(name) {
@@ -73,7 +62,6 @@
   while (test != NULL) {
     if (test->raw_test_) {
       test->RunTest();
-      CleanupState();
     }
     test = test->next_;
   }
@@ -84,28 +72,11 @@
   while (test != NULL) {
     if (!test->raw_test_) {
       test->RunTest();
-      CleanupState();
     }
     test = test->next_;
   }
 }
 
-void TestCaseBase::CleanupState() {
-  if (current_kernel_buffers_ != NULL) {
-    delete current_kernel_buffers_;
-    current_kernel_buffers_ = NULL;
-  }
-}
-
-void TestCaseBase::AddToKernelBuffers(const uint8_t* kernel_buffer) {
-  ASSERT(kernel_buffer != NULL);
-  if (current_kernel_buffers_ == NULL) {
-    current_kernel_buffers_ = new KernelBufferList(kernel_buffer);
-  } else {
-    current_kernel_buffers_->AddBufferToList(kernel_buffer);
-  }
-}
-
 Dart_Isolate TestCase::CreateIsolate(const uint8_t* data_buffer,
                                      intptr_t len,
                                      const uint8_t* instr_buffer,
@@ -351,18 +322,10 @@
     char* result =
         OS::SCreate(zone, "Compilation failed %s", compilation_result.error);
     free(compilation_result.error);
-    if (compilation_result.kernel != NULL) {
-      free(const_cast<uint8_t*>(compilation_result.kernel));
-    }
-    *kernel_buffer = NULL;
-    *kernel_buffer_size = 0;
     return result;
   }
   *kernel_buffer = compilation_result.kernel;
   *kernel_buffer_size = compilation_result.kernel_size;
-  if (compilation_result.error != NULL) {
-    free(compilation_result.error);
-  }
   if (kernel_buffer == NULL) {
     return OS::SCreate(zone, "front end generated a NULL kernel file");
   }
@@ -561,9 +524,6 @@
         Dart_LoadLibraryFromKernel(kernel_buffer, kernel_buffer_size);
     EXPECT_VALID(lib);
 
-    // Ensure kernel buffer isn't leaked after test is run.
-    AddToKernelBuffers(kernel_buffer);
-
     // TODO(32618): Kernel doesn't correctly represent the root library.
     lib = Dart_LookupLibrary(Dart_NewStringFromCString(sourcefiles[0].uri));
     DART_CHECK_VALID(lib);
@@ -606,9 +566,6 @@
       Dart_LoadLibraryFromKernel(kernel_buffer, kernel_buffer_size);
   DART_CHECK_VALID(lib);
 
-  // Ensure kernel buffer isn't leaked after test is run.
-  AddToKernelBuffers(kernel_buffer);
-
   // BOGUS: Kernel doesn't correctly represent the root library.
   lib = Dart_LookupLibrary(Dart_NewStringFromCString(
       entry_script_uri != NULL ? entry_script_uri : sourcefiles[0].uri));
@@ -699,14 +656,12 @@
     if (compilation_result.status != Dart_KernelCompilationStatus_Ok) {
       Dart_Handle result = Dart_NewApiError(compilation_result.error);
       free(compilation_result.error);
-      if (compilation_result.kernel != NULL) {
-        free(const_cast<uint8_t*>(compilation_result.kernel));
-      }
       return result;
     }
   } else {
     SetReloadTestScript(script);
   }
+
   return TriggerReload();
 }
 
diff --git a/runtime/vm/unit_test.h b/runtime/vm/unit_test.h
index 2c87b67..1115f6d 100644
--- a/runtime/vm/unit_test.h
+++ b/runtime/vm/unit_test.h
@@ -277,36 +277,6 @@
 extern const intptr_t platform_dill_size;
 extern const intptr_t platform_strong_dill_size;
 
-class TesterState : public AllStatic {
- public:
-  static const uint8_t* vm_snapshot_data;
-  static Dart_IsolateCreateCallback create_callback;
-  static Dart_IsolateShutdownCallback shutdown_callback;
-  static Dart_IsolateCleanupCallback cleanup_callback;
-};
-
-class KernelBufferList {
- public:
-  explicit KernelBufferList(const uint8_t* kernel_buffer)
-      : kernel_buffer_(kernel_buffer), next_(NULL) {}
-
-  KernelBufferList(const uint8_t* kernel_buffer, KernelBufferList* next)
-      : kernel_buffer_(kernel_buffer), next_(next) {}
-
-  ~KernelBufferList() {
-    free(const_cast<uint8_t*>(kernel_buffer_));
-    if (next_ != NULL) {
-      delete next_;
-    }
-  }
-
-  void AddBufferToList(const uint8_t* kernel_buffer);
-
- private:
-  const uint8_t* kernel_buffer_;
-  KernelBufferList* next_;
-};
-
 class TestCaseBase {
  public:
   explicit TestCaseBase(const char* name);
@@ -319,11 +289,8 @@
 
   static void RunAll();
   static void RunAllRaw();
-  static void CleanupState();
-  static void AddToKernelBuffers(const uint8_t* kernel_buffer);
 
  protected:
-  static KernelBufferList* current_kernel_buffers_;
   bool raw_test_;
 
  private: