Reland "[vm] Move ResolveUri to bin"

This is a reland of commit 1a614b1d86
Bug: b/348315159

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: Ib40d3a8c7d4b7c6226b6e4db4b3016cc32591239
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/+/372500
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Daco Harkes <dacoharkes@google.com>
This commit is contained in:
Daco Harkes 2024-06-26 11:10:06 +00:00 committed by Commit Queue
parent 8b45eef446
commit 8f962bdff6
10 changed files with 30 additions and 147 deletions

View file

@ -1031,7 +1031,6 @@ source_set("run_vm_tests_set") {
# 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")
@ -1052,7 +1051,7 @@ source_set("run_vm_tests_set") {
"vmservice_impl.cc",
"vmservice_impl.h",
] + builtin_impl_tests + vm_tests + compiler_tests + heap_tests +
io_impl_tests + platform_tests
io_impl_tests
}
executable("run_vm_tests") {

View file

@ -51,6 +51,8 @@ builtin_impl_sources = [
"thread_macos.h",
"thread_win.cc",
"thread_win.h",
"uri.cc",
"uri.h",
"utils.cc",
"utils.h",
"utils_fuchsia.cc",
@ -69,4 +71,5 @@ builtin_impl_tests = [
"priority_heap_test.cc",
"snapshot_utils_test.cc",
"test_utils.cc",
"uri_test.cc",
]

View file

@ -4,18 +4,11 @@
#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/utils.h"
#include "include/dart_tools_api.h"
#include "platform/growable_array.h"
#include "platform/uri.h"
#include "bin/uri.h"
#include "platform/utils.h"
namespace dart {

View file

@ -20,7 +20,7 @@
#endif
#include "bin/file.h"
#include "platform/uri.h"
#include "bin/uri.h"
namespace dart {
namespace bin {

View file

@ -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 "platform/uri.h"
#include "bin/uri.h"
#include <memory>
#include <utility>

View file

@ -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_PLATFORM_URI_H_
#define RUNTIME_PLATFORM_URI_H_
#ifndef RUNTIME_BIN_URI_H_
#define RUNTIME_BIN_URI_H_
#include <memory>
#include "platform/utils.h"
@ -33,4 +33,4 @@ CStringUniquePtr ResolveUri(const char* ref_uri, const char* base_uri);
} // namespace dart
#endif // RUNTIME_PLATFORM_URI_H_
#endif // RUNTIME_BIN_URI_H_

View file

@ -4,7 +4,7 @@
#include "vm/unit_test.h"
#include "platform/uri.h"
#include "bin/uri.h"
#include "platform/utils.h"
namespace dart {

View file

@ -14,7 +14,6 @@
#include <tchar.h>
#endif
#include "platform/uri.h"
#include "vm/bootstrap_natives.h"
#include "vm/dart_api_impl.h"
#include "vm/exceptions.h"
@ -281,54 +280,6 @@ static void* FfiResolveWithFfiNativeResolver(Thread* const thread,
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();
@ -377,77 +328,6 @@ static char* AvailableAssetsToCString(Thread* const thread) {
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).
//
@ -476,32 +356,42 @@ static void* FfiResolveAsset(Thread* const thread,
void* handle;
if (asset_type.Equals(Symbols::absolute())) {
if (native_assets_api->dlopen_absolute == nullptr) {
return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
*error = OS::SCreate(/*use malloc*/ nullptr,
"NativeAssetsApi::dlopen_absolute not set.");
return nullptr;
}
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) {
return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
*error = OS::SCreate(/*use malloc*/ nullptr,
"NativeAssetsApi::dlopen_relative not set.");
return nullptr;
}
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) {
return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
*error = OS::SCreate(/*use malloc*/ nullptr,
"NativeAssetsApi::dlopen_system not set.");
return nullptr;
}
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) {
return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
*error = OS::SCreate(/*use malloc*/ nullptr,
"NativeAssetsApi::dlopen_executable not set.");
return nullptr;
}
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) {
return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
*error = OS::SCreate(/*use malloc*/ nullptr,
"NativeAssetsApi::dlopen_process not set.");
return nullptr;
}
NoActiveIsolateScope no_active_isolate_scope;
handle = native_assets_api->dlopen_process(error);
@ -511,7 +401,9 @@ static void* FfiResolveAsset(Thread* const thread,
return nullptr;
}
if (native_assets_api->dlsym == nullptr) {
return FfiResolveAssetFallback(thread, asset_type, path, symbol, error);
*error =
OS::SCreate(/*use malloc*/ nullptr, "NativeAssetsApi::dlsym not set.");
return nullptr;
}
void* const result =
native_assets_api->dlsym(handle, symbol.ToCString(), error);

View file

@ -35,8 +35,6 @@ platform_sources = [
"unwinding_records.cc",
"unwinding_records.h",
"unwinding_records_win.cc",
"uri.cc",
"uri.h",
"utils.cc",
"utils.h",
"utils_android.cc",
@ -45,5 +43,3 @@ platform_sources = [
"utils_macos.cc",
"utils_win.cc",
]
platform_sources_tests = [ "uri_test.cc" ]

View file

@ -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"