Revert "[vm] Move `ResolveUri` to bin"

This reverts commit 1a614b1d862e8307e8d9d4966fca6a409f3ab41b.

Reason for revert: Breaks embedders in g3.

Original change's description:
> [vm] Move `ResolveUri` to bin
>
> This CL removes the native assets fallback from the VM.
>
> TEST=pkg/dartdev/test/native_assets/build_test.dart
> TEST=tests/ffi/native_assets/asset_relative_test.dart
>
> Closes: https://github.com/dart-lang/sdk/issues/55523
> Closes: https://github.com/dart-lang/sdk/issues/55925
> Change-Id: Ifc7e491300ae1b65bb5a9e5279cbcb45ad91e165
> Cq-Include-Trybots: dart/try:vm-aot-linux-debug-x64-try,vm-aot-linux-debug-x64c-try,vm-aot-mac-release-arm64-try,vm-aot-mac-release-x64-try,vm-aot-obfuscate-linux-release-x64-try,vm-aot-optimization-level-linux-release-x64-try,vm-aot-win-debug-arm64-try,vm-aot-win-debug-x64-try,vm-aot-win-debug-x64c-try,pkg-linux-debug-try,pkg-linux-release-arm64-try,pkg-mac-release-try,pkg-mac-release-arm64-try,pkg-win-release-try,pkg-win-release-arm64-try,vm-aot-asan-linux-release-x64-try,vm-asan-linux-release-x64-try,vm-aot-msan-linux-release-x64-try,vm-msan-linux-release-x64-try
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/370740
> Reviewed-by: Martin Kustermann <kustermann@google.com>
> Commit-Queue: Daco Harkes <dacoharkes@google.com>

Change-Id: Ib11db566b857089126cbfdf5af2d01648c348b84
Cq-Include-Trybots: dart/try:vm-aot-linux-debug-x64-try,vm-aot-linux-debug-x64c-try,vm-aot-mac-release-arm64-try,vm-aot-mac-release-x64-try,vm-aot-obfuscate-linux-release-x64-try,vm-aot-optimization-level-linux-release-x64-try,vm-aot-win-debug-arm64-try,vm-aot-win-debug-x64-try,vm-aot-win-debug-x64c-try,pkg-linux-debug-try,pkg-linux-release-arm64-try,pkg-mac-release-try,pkg-mac-release-arm64-try,pkg-win-release-try,pkg-win-release-arm64-try,vm-aot-asan-linux-release-x64-try,vm-asan-linux-release-x64-try,vm-aot-msan-linux-release-x64-try,vm-msan-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/372441
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
diff --git a/runtime/bin/BUILD.gn b/runtime/bin/BUILD.gn
index be08935..d5e5897 100644
--- a/runtime/bin/BUILD.gn
+++ b/runtime/bin/BUILD.gn
@@ -1031,6 +1031,7 @@
 
   # The VM sources are already included in libdart, so we just want to add in
   # the tests here.
+  platform_tests = rebase_path(platform_sources_tests, ".", "../platform")
   vm_tests = rebase_path(vm_sources_tests, ".", "../vm")
   compiler_tests = rebase_path(compiler_sources_tests, ".", "../vm/compiler")
   heap_tests = rebase_path(heap_sources_tests, ".", "../vm/heap")
@@ -1051,7 +1052,7 @@
               "vmservice_impl.cc",
               "vmservice_impl.h",
             ] + builtin_impl_tests + vm_tests + compiler_tests + heap_tests +
-            io_impl_tests
+            io_impl_tests + platform_tests
 }
 
 executable("run_vm_tests") {
diff --git a/runtime/bin/builtin_impl_sources.gni b/runtime/bin/builtin_impl_sources.gni
index 6e4b651..0db1b76 100644
--- a/runtime/bin/builtin_impl_sources.gni
+++ b/runtime/bin/builtin_impl_sources.gni
@@ -51,8 +51,6 @@
   "thread_macos.h",
   "thread_win.cc",
   "thread_win.h",
-  "uri.cc",
-  "uri.h",
   "utils.cc",
   "utils.h",
   "utils_fuchsia.cc",
@@ -71,5 +69,4 @@
   "priority_heap_test.cc",
   "snapshot_utils_test.cc",
   "test_utils.cc",
-  "uri_test.cc",
 ]
diff --git a/runtime/bin/loader.cc b/runtime/bin/loader.cc
index dbd6069..0055a7c 100644
--- a/runtime/bin/loader.cc
+++ b/runtime/bin/loader.cc
@@ -4,11 +4,18 @@
 
 #include "bin/loader.h"
 
+#include "bin/builtin.h"
 #include "bin/dartutils.h"
 #include "bin/dfe.h"
 #include "bin/error_exit.h"
+#include "bin/file.h"
+#include "bin/gzip.h"
+#include "bin/lockers.h"
 #include "bin/snapshot_utils.h"
-#include "bin/uri.h"
+#include "bin/utils.h"
+#include "include/dart_tools_api.h"
+#include "platform/growable_array.h"
+#include "platform/uri.h"
 #include "platform/utils.h"
 
 namespace dart {
diff --git a/runtime/bin/native_assets_api_impl.cc b/runtime/bin/native_assets_api_impl.cc
index a920fea..870604d 100644
--- a/runtime/bin/native_assets_api_impl.cc
+++ b/runtime/bin/native_assets_api_impl.cc
@@ -19,7 +19,7 @@
 #endif
 
 #include "bin/file.h"
-#include "bin/uri.h"
+#include "platform/uri.h"
 
 namespace dart {
 namespace bin {
diff --git a/runtime/lib/ffi_dynamic_library.cc b/runtime/lib/ffi_dynamic_library.cc
index 8f6eabe..e17a9bf 100644
--- a/runtime/lib/ffi_dynamic_library.cc
+++ b/runtime/lib/ffi_dynamic_library.cc
@@ -14,6 +14,7 @@
 #include <tchar.h>
 #endif
 
+#include "platform/uri.h"
 #include "vm/bootstrap_natives.h"
 #include "vm/dart_api_impl.h"
 #include "vm/exceptions.h"
@@ -280,6 +281,54 @@
   return result;
 }
 
+#if defined(DART_TARGET_OS_WINDOWS)
+// Replaces back slashes with forward slashes in place.
+static void ReplaceBackSlashes(char* cstr) {
+  const intptr_t length = strlen(cstr);
+  for (int i = 0; i < length; i++) {
+    cstr[i] = cstr[i] == '\\' ? '/' : cstr[i];
+  }
+}
+#endif
+
+const char* file_schema = "file://";
+const int file_schema_length = 7;
+
+// Get a file path with only forward slashes from the script path.
+static StringPtr GetPlatformScriptPath(Thread* thread) {
+  IsolateGroupSource* const source = thread->isolate_group()->source();
+
+#if defined(DART_TARGET_OS_WINDOWS)
+  // Isolate.spawnUri sets a `source` including the file schema.
+  // And on Windows we get an extra forward slash in that case.
+  const char* file_schema_slash = "file:///";
+  const int file_schema_slash_length = 8;
+  const char* path = source->script_uri;
+  if (strlen(source->script_uri) > file_schema_slash_length &&
+      strncmp(source->script_uri, file_schema_slash,
+              file_schema_slash_length) == 0) {
+    path = (source->script_uri + file_schema_slash_length);
+  }
+
+  // Replace backward slashes with forward slashes.
+  const intptr_t len = strlen(path);
+  char* path_copy = reinterpret_cast<char*>(malloc(len + 1));
+  snprintf(path_copy, len + 1, "%s", path);
+  ReplaceBackSlashes(path_copy);
+  const auto& result = String::Handle(String::New(path_copy));
+  free(path_copy);
+  return result.ptr();
+#else
+  // Isolate.spawnUri sets a `source` including the file schema.
+  if (strlen(source->script_uri) > file_schema_length &&
+      strncmp(source->script_uri, file_schema, file_schema_length) == 0) {
+    const char* path = (source->script_uri + file_schema_length);
+    return String::New(path);
+  }
+  return String::New(source->script_uri);
+#endif
+}
+
 // Array::null if asset is not in mapping or no mapping.
 static ArrayPtr GetAssetLocation(Thread* const thread, const String& asset) {
   Zone* const zone = thread->zone();
@@ -328,6 +377,77 @@
   return buffer.buffer();
 }
 
+// Fall back to old implementation temporarily to ease the roll into flutter.
+// TODO(https://dartbug.com/55523): Remove fallback and throw errors that
+// native assets API is not initialized.
+static void* FfiResolveAssetFallback(Thread* const thread,
+                                     const String& asset_type,
+                                     const String& path,
+                                     const String& symbol,
+                                     char** error) {
+  Zone* const zone = thread->zone();
+  void* handle = nullptr;
+  if (asset_type.Equals(Symbols::absolute())) {
+    handle = LoadDynamicLibrary(path.ToCString(), error);
+  } else if (asset_type.Equals(Symbols::relative())) {
+    const auto& platform_script_uri = String::Handle(
+        zone,
+        String::NewFormatted(
+            "%s%s", file_schema,
+            String::Handle(zone, GetPlatformScriptPath(thread)).ToCString()));
+    char* path_cstr = path.ToMallocCString();
+#if defined(DART_TARGET_OS_WINDOWS)
+    ReplaceBackSlashes(path_cstr);
+#endif
+    CStringUniquePtr target_uri =
+        ResolveUri(path_cstr, platform_script_uri.ToCString());
+    free(path_cstr);
+    if (!target_uri) {
+      *error = OS::SCreate(
+          /*use malloc*/ nullptr,
+          "Failed to resolve '%s' relative to "
+          "'%s'.",
+          path.ToCString(), platform_script_uri.ToCString());
+    } else {
+      const char* target_path = target_uri.get() + file_schema_length;
+      handle = LoadDynamicLibrary(target_path, error);
+    }
+  } else if (asset_type.Equals(Symbols::system())) {
+    handle = LoadDynamicLibrary(path.ToCString(), error);
+  } else if (asset_type.Equals(Symbols::process())) {
+#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_MACOS) ||              \
+    defined(DART_HOST_OS_ANDROID) || defined(DART_HOST_OS_FUCHSIA)
+    handle = RTLD_DEFAULT;
+#else
+    handle = kWindowsDynamicLibraryProcessPtr;
+#endif
+  } else if (asset_type.Equals(Symbols::executable())) {
+    handle = LoadDynamicLibrary(nullptr, error);
+  } else {
+    UNREACHABLE();
+  }
+  if (*error != nullptr) {
+    char* inner_error = *error;
+    *error = OS::SCreate(/*use malloc*/ nullptr,
+                         "Failed to load dynamic library '%s': %s",
+                         path.ToCString(), inner_error);
+    free(inner_error);
+  } else {
+    void* const result = ResolveSymbol(handle, symbol.ToCString(), error);
+    if (*error != nullptr) {
+      char* inner_error = *error;
+      *error = OS::SCreate(/*use malloc*/ nullptr,
+                           "Failed to lookup symbol '%s': %s",
+                           symbol.ToCString(), inner_error);
+      free(inner_error);
+    } else {
+      return result;
+    }
+  }
+  ASSERT(*error != nullptr);
+  return nullptr;
+}
+
 // If an error occurs populates |error| with an error message
 // (caller must free this message when it is no longer needed).
 //
@@ -356,42 +476,32 @@
   void* handle;
   if (asset_type.Equals(Symbols::absolute())) {
     if (native_assets_api->dlopen_absolute == nullptr) {
-      *error = OS::SCreate(/*use malloc*/ nullptr,
-                           "NativeAssetsApi::dlopen_absolute not set.");
-      return nullptr;
+      return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
     }
     NoActiveIsolateScope no_active_isolate_scope;
     handle = native_assets_api->dlopen_absolute(path_cstr, error);
   } else if (asset_type.Equals(Symbols::relative())) {
     if (native_assets_api->dlopen_relative == nullptr) {
-      *error = OS::SCreate(/*use malloc*/ nullptr,
-                           "NativeAssetsApi::dlopen_relative not set.");
-      return nullptr;
+      return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
     }
     NoActiveIsolateScope no_active_isolate_scope;
     handle = native_assets_api->dlopen_relative(path_cstr, error);
   } else if (asset_type.Equals(Symbols::system())) {
     if (native_assets_api->dlopen_system == nullptr) {
-      *error = OS::SCreate(/*use malloc*/ nullptr,
-                           "NativeAssetsApi::dlopen_system not set.");
-      return nullptr;
+      return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
     }
     NoActiveIsolateScope no_active_isolate_scope;
     handle = native_assets_api->dlopen_system(path_cstr, error);
   } else if (asset_type.Equals(Symbols::executable())) {
     if (native_assets_api->dlopen_executable == nullptr) {
-      *error = OS::SCreate(/*use malloc*/ nullptr,
-                           "NativeAssetsApi::dlopen_executable not set.");
-      return nullptr;
+      return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
     }
     NoActiveIsolateScope no_active_isolate_scope;
     handle = native_assets_api->dlopen_executable(error);
   } else {
     RELEASE_ASSERT(asset_type.Equals(Symbols::process()));
     if (native_assets_api->dlopen_process == nullptr) {
-      *error = OS::SCreate(/*use malloc*/ nullptr,
-                           "NativeAssetsApi::dlopen_process not set.");
-      return nullptr;
+      return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
     }
     NoActiveIsolateScope no_active_isolate_scope;
     handle = native_assets_api->dlopen_process(error);
@@ -401,9 +511,7 @@
     return nullptr;
   }
   if (native_assets_api->dlsym == nullptr) {
-    *error =
-        OS::SCreate(/*use malloc*/ nullptr, "NativeAssetsApi::dlsym not set.");
-    return nullptr;
+    return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
   }
   void* const result =
       native_assets_api->dlsym(handle, symbol.ToCString(), error);
diff --git a/runtime/platform/platform_sources.gni b/runtime/platform/platform_sources.gni
index e300478..db926b1 100644
--- a/runtime/platform/platform_sources.gni
+++ b/runtime/platform/platform_sources.gni
@@ -35,6 +35,8 @@
   "unwinding_records.cc",
   "unwinding_records.h",
   "unwinding_records_win.cc",
+  "uri.cc",
+  "uri.h",
   "utils.cc",
   "utils.h",
   "utils_android.cc",
@@ -43,3 +45,5 @@
   "utils_macos.cc",
   "utils_win.cc",
 ]
+
+platform_sources_tests = [ "uri_test.cc" ]
diff --git a/runtime/bin/uri.cc b/runtime/platform/uri.cc
similarity index 99%
rename from runtime/bin/uri.cc
rename to runtime/platform/uri.cc
index b0a1270..bc52efa 100644
--- a/runtime/bin/uri.cc
+++ b/runtime/platform/uri.cc
@@ -2,7 +2,7 @@
 // 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.
 
-#include "bin/uri.h"
+#include "platform/uri.h"
 
 #include <memory>
 #include <utility>
diff --git a/runtime/bin/uri.h b/runtime/platform/uri.h
similarity index 89%
rename from runtime/bin/uri.h
rename to runtime/platform/uri.h
index 65483ac..8201b70 100644
--- a/runtime/bin/uri.h
+++ b/runtime/platform/uri.h
@@ -2,8 +2,8 @@
 // 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.
 
-#ifndef RUNTIME_BIN_URI_H_
-#define RUNTIME_BIN_URI_H_
+#ifndef RUNTIME_PLATFORM_URI_H_
+#define RUNTIME_PLATFORM_URI_H_
 
 #include <memory>
 #include "platform/utils.h"
@@ -33,4 +33,4 @@
 
 }  // namespace dart
 
-#endif  // RUNTIME_BIN_URI_H_
+#endif  // RUNTIME_PLATFORM_URI_H_
diff --git a/runtime/bin/uri_test.cc b/runtime/platform/uri_test.cc
similarity index 99%
rename from runtime/bin/uri_test.cc
rename to runtime/platform/uri_test.cc
index e324de6..a2089bd 100644
--- a/runtime/bin/uri_test.cc
+++ b/runtime/platform/uri_test.cc
@@ -4,7 +4,7 @@
 
 #include "vm/unit_test.h"
 
-#include "bin/uri.h"
+#include "platform/uri.h"
 #include "platform/utils.h"
 
 namespace dart {
diff --git a/runtime/vm/unit_test.cc b/runtime/vm/unit_test.cc
index d83f587..dcbf8a9 100644
--- a/runtime/vm/unit_test.cc
+++ b/runtime/vm/unit_test.cc
@@ -9,9 +9,9 @@
 #include "bin/builtin.h"
 #include "bin/dartutils.h"
 #include "bin/isolate_data.h"
-#include "bin/uri.h"
 
 #include "platform/globals.h"
+#include "platform/uri.h"
 
 #include "vm/compiler/assembler/assembler.h"
 #include "vm/compiler/assembler/disassembler.h"