[fuchsia] Plumbing for the VMEX resource.

TEST=ci
Change-Id: I2e812e6eef2a3c6f91aec955609ece2ca4df15a1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/308805
Reviewed-by: Liam Appelbe <liama@google.com>
Commit-Queue: Ryan Macnak <rmacnak@google.com>
This commit is contained in:
Ryan Macnak 2023-06-20 19:28:18 +00:00 committed by Commit Queue
parent 70af4a9db0
commit dbd7b967af
15 changed files with 98 additions and 11 deletions

View file

@ -5,7 +5,6 @@
"sandbox": {
"features": [
"config-data",
"deprecated-ambient-replace-as-executable",
"isolated-cache-storage",
"isolated-persistent-storage",
"isolated-temp",
@ -16,6 +15,7 @@
"fuchsia.device.NameProvider",
"fuchsia.feedback.CrashReporter",
"fuchsia.intl.PropertyProvider",
"fuchsia.kernel.VmexResource",
"fuchsia.logger.LogSink",
"fuchsia.net.name.Lookup",
"fuchsia.posix.socket.Provider",

View file

@ -5,7 +5,6 @@
"sandbox": {
"features": [
"config-data",
"deprecated-ambient-replace-as-executable",
"isolated-cache-storage",
"isolated-persistent-storage",
"isolated-temp",
@ -16,6 +15,7 @@
"fuchsia.device.NameProvider",
"fuchsia.feedback.CrashReporter",
"fuchsia.intl.PropertyProvider",
"fuchsia.kernel.VmexResource",
"fuchsia.logger.LogSink",
"fuchsia.net.name.Lookup",
"fuchsia.posix.socket.Provider",

View file

@ -319,13 +319,22 @@ template("build_gen_snapshot_dart_io") {
if (is_fuchsia) {
if (using_fuchsia_gn_sdk) {
deps += [ "$fuchsia_sdk_root/fidl/fuchsia.io" ]
deps += [
"$fuchsia_sdk_root/fidl/fuchsia.io",
"$fuchsia_sdk_root/fidl/fuchsia.kernel",
]
public_deps = [ "$fuchsia_sdk_root/pkg/fdio" ]
} else if (using_fuchsia_sdk) {
deps += [ "$fuchsia_sdk_root/fidl:fuchsia.io" ]
deps += [
"$fuchsia_sdk_root/fidl:fuchsia.io",
"$fuchsia_sdk_root/fidl:fuchsia.kernel",
]
public_deps = [ "$fuchsia_sdk_root/pkg:fdio" ]
} else {
deps += [ "//sdk/fidl/fuchsia.io:fuchsia.io_hlcpp" ]
deps += [
"//sdk/fidl/fuchsia.io:fuchsia.io_hlcpp",
"//sdk/fidl/fuchsia.kernel:fuchsia.kernel_hlcpp",
]
public_deps = [ "//sdk/lib/fdio" ]
}
}
@ -431,13 +440,22 @@ template("dart_io") {
if (is_fuchsia) {
if (using_fuchsia_gn_sdk) {
deps += [ "$fuchsia_sdk_root/fidl/fuchsia.io" ]
deps += [
"$fuchsia_sdk_root/fidl/fuchsia.io",
"$fuchsia_sdk_root/fidl/fuchsia.kernel",
]
public_deps = [ "$fuchsia_sdk_root/pkg/fdio" ]
} else if (using_fuchsia_sdk) {
deps += [ "$fuchsia_sdk_root/fidl:fuchsia.io" ]
deps += [
"$fuchsia_sdk_root/fidl:fuchsia.io",
"$fuchsia_sdk_root/fidl:fuchsia.kernel",
]
public_deps = [ "$fuchsia_sdk_root/pkg:fdio" ]
} else {
deps += [ "//sdk/fidl/fuchsia.io:fuchsia.io_hlcpp" ]
deps += [
"//sdk/fidl/fuchsia.io:fuchsia.io_hlcpp",
"//sdk/fidl/fuchsia.kernel:fuchsia.kernel_hlcpp",
]
public_deps = [ "//sdk/lib/fdio" ]
}
}

View file

@ -199,6 +199,9 @@ int RunAnalyzer(int argc, char** argv) {
init_params.file_write = DartUtils::WriteFile;
init_params.file_close = DartUtils::CloseFile;
init_params.entropy_source = DartUtils::EntropySource;
#if defined(DART_HOST_OS_FUCHSIA)
init_params.vmex_resource = Platform::GetVMEXResource();
#endif
error = Dart_Initialize(&init_params);
if (error != nullptr) {

View file

@ -905,6 +905,9 @@ int main(int argc, char** argv) {
init_params.file_close = DartUtils::CloseFile;
init_params.entropy_source = DartUtils::EntropySource;
init_params.start_kernel_isolate = false;
#if defined(DART_HOST_OS_FUCHSIA)
init_params.vmex_resource = Platform::GetVMEXResource();
#endif
std::unique_ptr<MappedMemory> mapped_vm_snapshot_data;
std::unique_ptr<MappedMemory> mapped_vm_snapshot_instructions;

View file

@ -1355,6 +1355,13 @@ void main(int argc, char** argv) {
}
#else
init_params.start_kernel_isolate = false;
#endif
#if defined(DART_HOST_OS_FUCHSIA)
#if defined(DART_PRECOMPILED_RUNTIME)
init_params.vmex_resource = ZX_HANDLE_INVALID;
#else
init_params.vmex_resource = Platform::GetVMEXResource();
#endif
#endif
error = Dart_Initialize(&init_params);

View file

@ -102,6 +102,10 @@ class Platform {
static void SetCoreDumpResourceLimit(int value);
#if defined(DART_HOST_OS_FUCHSIA)
static zx_handle_t GetVMEXResource();
#endif
private:
// The path to the executable.
static const char* executable_name_;

View file

@ -7,6 +7,9 @@
#include "bin/platform.h"
#include <fuchsia/kernel/cpp/fidl.h>
#include <lib/fdio/directory.h>
#include <lib/zx/resource.h>
#include <string.h>
#include <sys/utsname.h>
#include <unistd.h>
@ -161,6 +164,18 @@ void Platform::SetCoreDumpResourceLimit(int value) {
// Not supported.
}
zx_handle_t Platform::GetVMEXResource() {
zx::resource vmex_resource;
fuchsia::kernel::VmexResourceSyncPtr vmex_resource_svc;
zx_status_t status = fdio_service_connect(
"/svc/fuchsia.kernel.VmexResource",
vmex_resource_svc.NewRequest().TakeChannel().release());
ASSERT(status == ZX_OK);
status = vmex_resource_svc->Get(&vmex_resource);
ASSERT(status == ZX_OK);
return vmex_resource.release();
}
} // namespace bin
} // namespace dart

View file

@ -392,6 +392,9 @@ static int Main(int argc, const char** argv) {
init_params.file_write = dart::bin::DartUtils::WriteFile;
init_params.file_close = dart::bin::DartUtils::CloseFile;
init_params.start_kernel_isolate = start_kernel_isolate;
#if defined(DART_HOST_OS_FUCHSIA)
init_params.vmex_resource = dart::bin::Platform::GetVMEXResource();
#endif
error = Dart::Init(&init_params);
if (error != nullptr) {
Syslog::PrintErr("Failed to initialize VM: %s\n", error);

View file

@ -25,6 +25,10 @@
#include <inttypes.h>
#include <stdbool.h>
#if defined(__Fuchsia__)
#include <zircon/types.h>
#endif
#ifdef __cplusplus
#define DART_EXTERN_C extern "C"
#else
@ -849,7 +853,7 @@ typedef Dart_Handle (*Dart_GetVMServiceAssetsArchive)(void);
* The current version of the Dart_InitializeFlags. Should be incremented every
* time Dart_InitializeFlags changes in a binary incompatible way.
*/
#define DART_INITIALIZE_PARAMS_CURRENT_VERSION (0x00000007)
#define DART_INITIALIZE_PARAMS_CURRENT_VERSION (0x00000008)
/** Forward declaration */
struct Dart_CodeObserver;
@ -992,6 +996,15 @@ typedef struct {
* Kernel blob unregistration callback function. See Dart_UnregisterKernelBlobCallback.
*/
Dart_UnregisterKernelBlobCallback unregister_kernel_blob;
#if defined(__Fuchsia__)
/**
* The resource needed to use zx_vmo_replace_as_executable. Can be
* ZX_HANDLE_INVALID if the process has ambient-replace-as-executable or if
* executable memory is not needed (e.g., this is an AOT runtime).
*/
zx_handle_t vmex_resource;
#endif
} Dart_InitializeParams;
/**

View file

@ -309,7 +309,11 @@ char* Dart::DartInit(const Dart_InitializeParams* params) {
NOT_IN_PRODUCT(CodeObservers::RegisterExternal(*params->code_observer));
}
start_time_micros_ = OS::GetCurrentMonotonicMicros();
#if defined(DART_HOST_OS_FUCHSIA)
VirtualMemory::Init(params->vmex_resource);
#else
VirtualMemory::Init();
#endif
#if defined(DART_PRECOMPILED_RUNTIME) && defined(DART_TARGET_OS_LINUX)
if (VirtualMemory::PageSize() > kElfPageSize) {

View file

@ -10,6 +10,10 @@
#include "vm/globals.h"
#include "vm/memory_region.h"
#if defined(DART_HOST_OS_FUCHSIA)
#include <zircon/types.h>
#endif
namespace dart {
class VirtualMemory {
@ -31,7 +35,11 @@ class VirtualMemory {
intptr_t size() const { return region_.size(); }
intptr_t AliasOffset() const { return alias_.start() - region_.start(); }
#if defined(DART_HOST_OS_FUCHSIA)
static void Init(zx_handle_t vmex_resource);
#else
static void Init();
#endif
static void Cleanup();
// Returns true if dual mapping is enabled.

View file

@ -43,6 +43,7 @@ uword VirtualMemory::page_size_ = 0;
static zx_handle_t compressed_heap_vmar_ = ZX_HANDLE_INVALID;
static uword compressed_heap_base_ = 0;
#endif // defined(DART_COMPRESSED_POINTERS)
static zx_handle_t vmex_resource_ = ZX_HANDLE_INVALID;
intptr_t VirtualMemory::CalculatePageSize() {
const intptr_t page_size = getpagesize();
@ -51,7 +52,7 @@ intptr_t VirtualMemory::CalculatePageSize() {
return page_size;
}
void VirtualMemory::Init() {
void VirtualMemory::Init(zx_handle_t vmex_resource) {
if (FLAG_old_gen_heap_size < 0 || FLAG_old_gen_heap_size > kMaxAddrSpaceMB) {
OS::PrintErr(
"warning: value specified for --old_gen_heap_size %d is larger than"
@ -89,9 +90,13 @@ void VirtualMemory::Init() {
#endif // defined(DART_COMPRESSED_POINTERS)
page_size_ = CalculatePageSize();
vmex_resource_ = vmex_resource;
}
void VirtualMemory::Cleanup() {
vmex_resource_ = ZX_HANDLE_INVALID;
page_size_ = 0;
#if defined(DART_COMPRESSED_POINTERS)
zx_vmar_destroy(compressed_heap_vmar_);
compressed_heap_vmar_ = ZX_HANDLE_INVALID;
@ -176,7 +181,7 @@ VirtualMemory* VirtualMemory::AllocateAligned(intptr_t size,
if (is_executable) {
// Add ZX_RIGHT_EXECUTE permission to VMO, so it can be mapped
// into memory as executable (now or later).
status = zx_vmo_replace_as_executable(vmo, ZX_HANDLE_INVALID, &vmo);
status = zx_vmo_replace_as_executable(vmo, vmex_resource_, &vmo);
if (status != ZX_OK) {
LOG_ERR("zx_vmo_replace_as_executable() failed: %s\n",
zx_status_get_string(status));

View file

@ -250,6 +250,8 @@ void VirtualMemory::Init() {
}
void VirtualMemory::Cleanup() {
page_size_ = 0;
#if defined(DART_COMPRESSED_POINTERS)
delete compressed_heap_;
compressed_heap_ = nullptr;

View file

@ -82,6 +82,8 @@ void VirtualMemory::Init() {
}
void VirtualMemory::Cleanup() {
page_size_ = 0;
#if defined(DART_COMPRESSED_POINTERS)
delete compressed_heap_;
compressed_heap_ = nullptr;