Flutter Engine
The Flutter Engine
virtual_memory_fuchsia.cc
Go to the documentation of this file.
1// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/globals.h"
6#if defined(DART_HOST_OS_FUCHSIA)
7
9
10#include <zircon/process.h>
11#include <zircon/status.h>
12#include <zircon/syscalls.h>
13
14#include "platform/assert.h"
15#include "platform/utils.h"
16
17namespace dart {
18namespace bin {
19
20uword VirtualMemory::page_size_ = 0;
21
22intptr_t VirtualMemory::CalculatePageSize() {
23 const intptr_t page_size = getpagesize();
24 ASSERT(page_size != 0);
25 ASSERT(Utils::IsPowerOfTwo(page_size));
26 return page_size;
27}
28
29VirtualMemory* VirtualMemory::Allocate(intptr_t size,
30 bool is_executable,
31 const char* name) {
32 ASSERT(Utils::IsAligned(size, page_size_));
33 zx_handle_t vmar = zx_vmar_root_self();
34 zx_handle_t vmo = ZX_HANDLE_INVALID;
35 zx_status_t status = zx_vmo_create(size, 0u, &vmo);
36 if (status != ZX_OK) {
37 return nullptr;
38 }
39
40 if (name != nullptr) {
41 zx_object_set_property(vmo, ZX_PROP_NAME, name, strlen(name));
42 }
43
44 if (is_executable) {
45 // Add ZX_RIGHT_EXECUTE permission to VMO, so it can be mapped
46 // into memory as executable (now or later).
47 status = zx_vmo_replace_as_executable(vmo, ZX_HANDLE_INVALID, &vmo);
48 if (status != ZX_OK) {
49 zx_handle_close(vmo);
50 return nullptr;
51 }
52 }
53
54 const zx_vm_option_t region_options =
55 ZX_VM_PERM_READ | ZX_VM_PERM_WRITE |
56 (is_executable ? ZX_VM_PERM_EXECUTE : 0);
57 uword base;
58 status = zx_vmar_map(vmar, region_options, 0, vmo, 0u, size, &base);
59 zx_handle_close(vmo);
60 if (status != ZX_OK) {
61 return nullptr;
62 }
63
64 return new VirtualMemory(reinterpret_cast<void*>(base), size);
65}
66
68 if (address_ != nullptr) {
69 zx_status_t status = zx_vmar_unmap(
70 zx_vmar_root_self(), reinterpret_cast<uword>(address_), size_);
71 if (status != ZX_OK) {
72 FATAL("zx_vmar_unmap failed: %s\n", zx_status_get_string(status));
73 }
74 }
75}
76
77void VirtualMemory::Protect(void* address, intptr_t size, Protection mode) {
78 const uword start_address = reinterpret_cast<uword>(address);
79 const uword end_address = start_address + size;
80 const uword page_address = Utils::RoundDown(start_address, PageSize());
81 uint32_t prot = 0;
82 switch (mode) {
83 case kNoAccess:
84 prot = 0;
85 break;
86 case kReadOnly:
87 prot = ZX_VM_PERM_READ;
88 break;
89 case kReadWrite:
90 prot = ZX_VM_PERM_READ | ZX_VM_PERM_WRITE;
91 break;
92 case kReadExecute:
93 prot = ZX_VM_PERM_READ | ZX_VM_PERM_EXECUTE;
94 break;
96 prot = ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_PERM_EXECUTE;
97 break;
98 }
99 zx_status_t status = zx_vmar_protect(zx_vmar_root_self(), prot, page_address,
100 end_address - page_address);
101 if (status != ZX_OK) {
102 FATAL("zx_vmar_protect(0x%lx, 0x%lx) failed: %s\n", page_address,
103 end_address - page_address, zx_status_get_string(status));
104 }
105}
106
107} // namespace bin
108} // namespace dart
109
110#endif // defined(DART_HOST_OS_FUCHSIA)
static constexpr T RoundDown(T x, intptr_t alignment)
Definition: utils.h:108
static constexpr bool IsAligned(T x, uintptr_t alignment, uintptr_t offset=0)
Definition: utils.h:92
static constexpr bool IsPowerOfTwo(T x)
Definition: utils.h:76
intptr_t size() const
static VirtualMemory * Allocate(intptr_t size, bool is_executable, const char *name)
static void Protect(void *address, intptr_t size, Protection mode)
static intptr_t PageSize()
#define ASSERT(E)
#define FATAL(error)
Definition: dart_vm.cc:33
const char *const name
uintptr_t uword
Definition: globals.h:501
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259