Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | List of all members
dart::VirtualMemory Class Reference

#include <virtual_memory.h>

Public Types

enum  Protection {
  kNoAccess , kReadOnly , kReadWrite , kReadExecute ,
  kReadWriteExecute
}
 

Public Member Functions

 ~VirtualMemory ()
 
uword start () const
 
uword end () const
 
void * address () const
 
intptr_t size () const
 
bool Contains (uword addr) const
 
void Protect (Protection mode)
 
bool DuplicateRX (VirtualMemory *target)
 
void Truncate (intptr_t new_size)
 
bool vm_owns_region () const
 

Static Public Member Functions

static void Init ()
 
static void Cleanup ()
 
static void Protect (void *address, intptr_t size, Protection mode)
 
static void DontNeed (void *address, intptr_t size)
 
static VirtualMemoryAllocate (intptr_t size, bool is_executable, bool is_compressed, const char *name)
 
static VirtualMemoryAllocateAligned (intptr_t size, intptr_t alignment, bool is_executable, bool is_compressed, const char *name)
 
static intptr_t PageSize ()
 
static bool InSamePage (uword address0, uword address1)
 
static VirtualMemoryForImagePage (void *pointer, uword size)
 

Detailed Description

Definition at line 19 of file virtual_memory.h.

Member Enumeration Documentation

◆ Protection

Enumerator
kNoAccess 
kReadOnly 
kReadWrite 
kReadExecute 
kReadWriteExecute 

Definition at line 21 of file virtual_memory.h.

Constructor & Destructor Documentation

◆ ~VirtualMemory()

dart::VirtualMemory::~VirtualMemory ( )

Member Function Documentation

◆ address()

void * dart::VirtualMemory::address ( ) const
inline

Definition at line 34 of file virtual_memory.h.

34{ return region_.pointer(); }
void * pointer() const

◆ Allocate()

static VirtualMemory * dart::VirtualMemory::Allocate ( intptr_t  size,
bool  is_executable,
bool  is_compressed,
const char *  name 
)
inlinestatic

Definition at line 54 of file virtual_memory.h.

57 {
58 return AllocateAligned(size, PageSize(), is_executable, is_compressed,
59 name);
60 }
static VirtualMemory * AllocateAligned(intptr_t size, intptr_t alignment, bool is_executable, bool is_compressed, const char *name)
static intptr_t PageSize()
intptr_t size() const
const char *const name

◆ AllocateAligned()

static VirtualMemory * dart::VirtualMemory::AllocateAligned ( intptr_t  size,
intptr_t  alignment,
bool  is_executable,
bool  is_compressed,
const char *  name 
)
static

◆ Cleanup()

static void dart::VirtualMemory::Cleanup ( )
static

◆ Contains()

bool dart::VirtualMemory::Contains ( uword  addr) const
inline

Definition at line 44 of file virtual_memory.h.

44{ return region_.Contains(addr); }
bool Contains(uword address) const

◆ DontNeed()

static void dart::VirtualMemory::DontNeed ( void *  address,
intptr_t  size 
)
static

◆ DuplicateRX()

bool dart::VirtualMemory::DuplicateRX ( VirtualMemory target)

Definition at line 48 of file virtual_memory.cc.

48 {
49 const intptr_t aligned_size = Utils::RoundUp(size(), PageSize());
50 ASSERT_LESS_OR_EQUAL(aligned_size, target->size());
51
52#if defined(DART_HOST_OS_MACOS) && defined(DART_PRECOMPILED_RUNTIME)
53 // Mac is special cased because iOS doesn't allow allocating new executable
54 // memory, so the default approach would fail. We are allowed to make new
55 // mappings of existing executable memory using vm_remap though, which is
56 // effectively the same for non-writable memory.
57 const mach_port_t task = mach_task_self();
58 const vm_address_t source_address = reinterpret_cast<vm_address_t>(address());
59 const vm_size_t mem_size = aligned_size;
60 const vm_prot_t read_execute = VM_PROT_READ | VM_PROT_EXECUTE;
61 vm_prot_t current_protection = read_execute;
62 vm_prot_t max_protection = read_execute;
63 vm_address_t target_address =
64 reinterpret_cast<vm_address_t>(target->address());
65 kern_return_t status = vm_remap(
66 task, &target_address, mem_size,
67 /*mask=*/0,
68 /*flags=*/VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE, task, source_address,
69 /*copy=*/true, &current_protection, &max_protection,
70 /*inheritance=*/VM_INHERIT_NONE);
71 if (status != KERN_SUCCESS) {
72 return false;
73 }
74 ASSERT(reinterpret_cast<void*>(target_address) == target->address());
75 ASSERT_EQUAL(current_protection & read_execute, read_execute);
76 ASSERT_EQUAL(max_protection & read_execute, read_execute);
77 return true;
78
79#else // defined(DART_HOST_OS_MACOS)
80 // TODO(52497): Use dual mapping on platforms where it's supported.
81 // Check that target doesn't overlap with this.
82 ASSERT(target->start() >= end() || target->end() <= start());
83 memcpy(target->address(), address(), size()); // NOLINT
84 Protect(target->address(), aligned_size, kReadExecute);
85 return true;
86#endif // defined(DART_HOST_OS_MACOS)
87}
#define ASSERT_LESS_OR_EQUAL(expected, actual)
Definition assert.h:313
#define ASSERT_EQUAL(expected, actual)
Definition assert.h:309
static constexpr T RoundUp(T x, uintptr_t alignment, uintptr_t offset=0)
Definition utils.h:105
static void Protect(void *address, intptr_t size, Protection mode)
void * address() const
uword start() const
#define ASSERT(E)
uint32_t * target
int vm_prot_t
Definition mach_o.h:18

◆ end()

uword dart::VirtualMemory::end ( ) const
inline

Definition at line 33 of file virtual_memory.h.

33{ return region_.end(); }
uword end() const

◆ ForImagePage()

VirtualMemory * dart::VirtualMemory::ForImagePage ( void *  pointer,
uword  size 
)
static

Definition at line 34 of file virtual_memory.cc.

34 {
35 // Memory for precompilated instructions was allocated by the embedder, so
36 // create a VirtualMemory without allocating.
37 MemoryRegion region(pointer, size);
38 MemoryRegion reserved(nullptr, 0); // null reservation indicates VM should
39 // not attempt to free this memory.
40 VirtualMemory* memory = new VirtualMemory(region, reserved);
41 ASSERT(!memory->vm_owns_region());
42 return memory;
43}
ClipOpAndAA opAA SkRegion region
Definition SkRecords.h:238

◆ Init()

static void dart::VirtualMemory::Init ( )
static

◆ InSamePage()

bool dart::VirtualMemory::InSamePage ( uword  address0,
uword  address1 
)
static

Definition at line 16 of file virtual_memory.cc.

16 {
17 return (Utils::RoundDown(address0, PageSize()) ==
18 Utils::RoundDown(address1, PageSize()));
19}
static constexpr T RoundDown(T x, intptr_t alignment)
Definition utils.h:93

◆ PageSize()

static intptr_t dart::VirtualMemory::PageSize ( )
inlinestatic

Definition at line 79 of file virtual_memory.h.

79 {
80 ASSERT(page_size_ != 0);
81 return page_size_;
82 }

◆ Protect() [1/2]

void dart::VirtualMemory::Protect ( Protection  mode)
inline

Definition at line 48 of file virtual_memory.h.

48{ return Protect(address(), size(), mode); }

◆ Protect() [2/2]

static void dart::VirtualMemory::Protect ( void *  address,
intptr_t  size,
Protection  mode 
)
static

◆ size()

intptr_t dart::VirtualMemory::size ( ) const
inline

Definition at line 35 of file virtual_memory.h.

35{ return region_.size(); }
uword size() const

◆ start()

uword dart::VirtualMemory::start ( ) const
inline

Definition at line 32 of file virtual_memory.h.

32{ return region_.start(); }
uword start() const

◆ Truncate()

void dart::VirtualMemory::Truncate ( intptr_t  new_size)

Definition at line 21 of file virtual_memory.cc.

21 {
22 ASSERT(Utils::IsAligned(new_size, PageSize()));
23 ASSERT(new_size <= size());
24 if (reserved_.size() ==
25 region_.size()) { // Don't create holes in reservation.
26 if (FreeSubSegment(reinterpret_cast<void*>(start() + new_size),
27 size() - new_size)) {
28 reserved_.set_size(new_size);
29 }
30 }
31 region_.Subregion(region_, 0, new_size);
32}
void Subregion(const MemoryRegion &from, uword offset, uword size)
void set_size(uword new_size)
static constexpr bool IsAligned(T x, uintptr_t alignment, uintptr_t offset=0)
Definition utils.h:77

◆ vm_owns_region()

bool dart::VirtualMemory::vm_owns_region ( ) const
inline

Definition at line 92 of file virtual_memory.h.

92{ return reserved_.pointer() != nullptr; }

The documentation for this class was generated from the following files: