v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
page-allocator.cc
Go to the documentation of this file.
1// Copyright 2017 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
8
9#if V8_OS_DARWIN
10#include <sys/mman.h> // For MAP_JIT.
11#endif
12
13namespace v8 {
14namespace base {
15
16#define STATIC_ASSERT_ENUM(a, b) \
17 static_assert(static_cast<int>(a) == static_cast<int>(b), \
18 "mismatching enum: " #a)
19
30
31#undef STATIC_ASSERT_ENUM
32
34 : allocate_page_size_(base::OS::AllocatePageSize()),
35 commit_page_size_(base::OS::CommitPageSize()) {}
36
40
44
45void* PageAllocator::AllocatePages(void* hint, size_t size, size_t alignment,
47#if !V8_HAS_PTHREAD_JIT_WRITE_PROTECT && !V8_HAS_BECORE_JIT_WRITE_PROTECT
48 // kNoAccessWillJitLater is only used on Apple Silicon. Map it to regular
49 // kNoAccess on other platforms, so code doesn't have to handle both enum
50 // values.
53 }
54#endif
55 return base::OS::Allocate(hint, size, alignment,
56 static_cast<base::OS::MemoryPermission>(access));
57}
58
60 public:
61 explicit SharedMemoryMapping(PageAllocator* page_allocator, void* ptr,
62 size_t size)
63 : page_allocator_(page_allocator), ptr_(ptr), size_(size) {}
65 void* GetMemory() const override { return ptr_; }
66
67 private:
69 void* ptr_;
70 size_t size_;
71};
72
74 public:
75 SharedMemory(PageAllocator* allocator, void* memory, size_t size)
76 : allocator_(allocator), ptr_(memory), size_(size) {}
77 void* GetMemory() const override { return ptr_; }
78 size_t GetSize() const override { return size_; }
79 std::unique_ptr<::v8::PageAllocator::SharedMemoryMapping> RemapTo(
80 void* new_address) const override {
81 if (allocator_->RemapShared(ptr_, new_address, size_)) {
82 return std::make_unique<SharedMemoryMapping>(allocator_, new_address,
83 size_);
84 } else {
85 return {};
86 }
87 }
88
90
91 private:
93 void* ptr_;
94 size_t size_;
95};
96
98#ifdef V8_OS_LINUX
99 return true;
100#else
101 return false;
102#endif
103}
104
105std::unique_ptr<v8::PageAllocator::SharedMemory>
106PageAllocator::AllocateSharedPages(size_t size, const void* original_address) {
107#ifdef V8_OS_LINUX
108 void* ptr =
110 CHECK_NOT_NULL(ptr);
111 memcpy(ptr, original_address, size);
112 bool success = base::OS::SetPermissions(
114 CHECK(success);
115
116 auto shared_memory =
117 std::make_unique<v8::base::SharedMemory>(this, ptr, size);
118 return shared_memory;
119#else
120 return {};
121#endif
122}
123
124void* PageAllocator::RemapShared(void* old_address, void* new_address,
125 size_t size) {
126#ifdef V8_OS_LINUX
127 return base::OS::RemapShared(old_address, new_address, size);
128#else
129 return nullptr;
130#endif
131}
132
133bool PageAllocator::FreePages(void* address, size_t size) {
134 base::OS::Free(address, size);
135 return true;
136}
137
138bool PageAllocator::ReleasePages(void* address, size_t size, size_t new_size) {
139 DCHECK_LT(new_size, size);
140 base::OS::Release(reinterpret_cast<uint8_t*>(address) + new_size,
141 size - new_size);
142 return true;
143}
144
145bool PageAllocator::SetPermissions(void* address, size_t size,
148 address, size, static_cast<base::OS::MemoryPermission>(access));
149}
150
151bool PageAllocator::RecommitPages(void* address, size_t size,
154 address, size, static_cast<base::OS::MemoryPermission>(access));
155}
156
157bool PageAllocator::DiscardSystemPages(void* address, size_t size) {
158 return base::OS::DiscardSystemPages(address, size);
159}
160
161bool PageAllocator::DecommitPages(void* address, size_t size) {
162 return base::OS::DecommitPages(address, size);
163}
164
165bool PageAllocator::SealPages(void* address, size_t size) {
166 return base::OS::SealPages(address, size);
167}
168
169} // namespace base
170} // namespace v8
static V8_WARN_UNUSED_RESULT void * AllocateShared(size_t size, MemoryPermission access)
static void * GetRandomMmapAddr()
static V8_WARN_UNUSED_RESULT void * RemapShared(void *old_address, void *new_address, size_t size)
static V8_WARN_UNUSED_RESULT bool SealPages(void *address, size_t size)
static V8_WARN_UNUSED_RESULT bool DiscardSystemPages(void *address, size_t size)
static V8_WARN_UNUSED_RESULT bool SetPermissions(void *address, size_t size, MemoryPermission access)
static V8_WARN_UNUSED_RESULT bool DecommitPages(void *address, size_t size)
static void Release(void *address, size_t size)
static V8_WARN_UNUSED_RESULT bool RecommitPages(void *address, size_t size, MemoryPermission access)
static V8_WARN_UNUSED_RESULT void * Allocate(void *address, size_t size, size_t alignment, MemoryPermission access)
static void SetRandomMmapSeed(int64_t seed)
static void Free(void *address, size_t size)
void * RemapShared(void *old_address, void *new_address, size_t size)
bool DiscardSystemPages(void *address, size_t size) override
void * AllocatePages(void *hint, size_t size, size_t alignment, PageAllocator::Permission access) override
void SetRandomMmapSeed(int64_t seed) override
void * GetRandomMmapAddr() override
std::unique_ptr< v8::PageAllocator::SharedMemory > AllocateSharedPages(size_t size, const void *original_address) override
bool SealPages(void *address, size_t size) override
bool CanAllocateSharedPages() override
bool RecommitPages(void *address, size_t size, PageAllocator::Permission access) override
bool DecommitPages(void *address, size_t size) override
bool FreePages(void *address, size_t size) override
bool ReleasePages(void *address, size_t size, size_t new_size) override
bool SetPermissions(void *address, size_t size, PageAllocator::Permission access) override
SharedMemoryMapping(PageAllocator *page_allocator, void *ptr, size_t size)
void * GetMemory() const override
SharedMemory(PageAllocator *allocator, void *memory, size_t size)
size_t GetSize() const override
PageAllocator * allocator_
std::unique_ptr<::v8::PageAllocator::SharedMemoryMapping > RemapTo(void *new_address) const override
void * GetMemory() const override
#define STATIC_ASSERT_ENUM(a, b)
#define CHECK(condition)
Definition logging.h:124
#define CHECK_NOT_NULL(val)
#define DCHECK_LT(v1, v2)
Definition logging.h:489