v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
bounded-page-allocator.h
Go to the documentation of this file.
1// Copyright 2018 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
5#ifndef V8_BASE_BOUNDED_PAGE_ALLOCATOR_H_
6#define V8_BASE_BOUNDED_PAGE_ALLOCATOR_H_
7
11
12namespace v8 {
13namespace base {
14
15// Defines the page initialization mode of a BoundedPageAllocator.
17 // The contents of allocated pages must be zero initialized. This causes any
18 // committed pages to be decommitted during FreePages and ReleasePages.
20 // Allocated pages do not have to be be zero initialized and can contain old
21 // data. This is slightly faster as comitted pages are not decommitted
22 // during FreePages and ReleasePages, but only made inaccessible.
24 // Assume pages are in discarded state and already have the right page
25 // permissions. Using this mode requires PageFreeingMode::kDiscard.
27};
28
29// Defines how BoundedPageAllocator frees pages when FreePages or ReleasePages
30// is requested.
31enum class PageFreeingMode {
32 // Pages are freed/released by setting permissions to kNoAccess. This is the
33 // preferred mode when current platform/configuration allows any page
34 // permissions reconfiguration.
36
37 // Pages are freed/released by using DiscardSystemPages of the underlying
38 // page allocator. This mode should be used for the cases when page permission
39 // reconfiguration is not allowed. In particular, on MacOS on ARM64 ("Apple
40 // M1"/Apple Silicon) it's not allowed to reconfigure RWX pages to anything
41 // else.
42 // This mode is not compatible with kAllocatedPagesMustBeZeroInitialized
43 // page initialization mode.
45};
46
47// This is a v8::PageAllocator implementation that allocates pages within the
48// pre-reserved region of virtual space. This class requires the virtual space
49// to be kept reserved during the lifetime of this object.
50// The main application of bounded page allocator are
51// - V8 heap pointer compression which requires the whole V8 heap to be
52// allocated within a contiguous range of virtual address space,
53// - executable page allocation, which allows to use PC-relative 32-bit code
54// displacement on certain 64-bit platforms.
55// Bounded page allocator uses other page allocator instance for doing actual
56// page allocations.
57// The implementation is thread-safe.
59 public:
60 enum class AllocationStatus {
61 kSuccess,
62 kFailedToCommit,
63 kRanOutOfReservation,
64 kHintedAddressTakenOrNotFound,
65 };
66
67 using Address = uintptr_t;
68
69 static const char* AllocationStatusToString(AllocationStatus);
70
72 size_t size, size_t allocate_page_size,
73 PageInitializationMode page_initialization_mode,
74 PageFreeingMode page_freeing_mode);
77 ~BoundedPageAllocator() override = default;
78
79 // These functions are not inlined to avoid https://crbug.com/v8/8275.
80 Address begin() const;
81 size_t size() const;
82
83 // Returns true if given address is in the range controlled by the bounded
84 // page allocator instance.
85 bool contains(Address address) const {
86 return region_allocator_.contains(address);
87 }
88
89 size_t AllocatePageSize() override { return allocate_page_size_; }
90
91 size_t CommitPageSize() override { return commit_page_size_; }
92
93 void SetRandomMmapSeed(int64_t seed) override {
95 }
96
97 void* GetRandomMmapAddr() override {
99 }
100
101 void* AllocatePages(void* hint, size_t size, size_t alignment,
102 Permission access) override;
103
104 bool ReserveForSharedMemoryMapping(void* address, size_t size) override;
105
106 // Allocates pages at given address, returns true on success.
107 bool AllocatePagesAt(Address address, size_t size, Permission access);
108
109 bool FreePages(void* address, size_t size) override;
110
111 bool ReleasePages(void* address, size_t size, size_t new_size) override;
112
113 bool SetPermissions(void* address, size_t size, Permission access) override;
114
115 bool RecommitPages(void* address, size_t size,
116 PageAllocator::Permission access) override;
117
118 bool DiscardSystemPages(void* address, size_t size) override;
119
120 bool DecommitPages(void* address, size_t size) override;
121
122 bool SealPages(void* address, size_t size) override;
123
125 return allocation_status_;
126 }
127
128 private:
131 const size_t commit_page_size_;
136 AllocationStatus allocation_status_ = AllocationStatus::kSuccess;
137};
138
139} // namespace base
140} // namespace v8
141
142#endif // V8_BASE_BOUNDED_PAGE_ALLOCATOR_H_
#define V8_BASE_EXPORT
Definition base-export.h:26
virtual void * GetRandomMmapAddr()=0
virtual void SetRandomMmapSeed(int64_t seed)=0
v8::base::RegionAllocator region_allocator_
void SetRandomMmapSeed(int64_t seed) override
BoundedPageAllocator & operator=(const BoundedPageAllocator &)=delete
const PageInitializationMode page_initialization_mode_
bool contains(Address address) const
BoundedPageAllocator(const BoundedPageAllocator &)=delete
v8::PageAllocator *const page_allocator_
AllocationStatus get_last_allocation_status() const
~BoundedPageAllocator() override=default
cppgc::PageAllocator * page_allocator_
Definition cpp-heap.cc:194
int start
uintptr_t Address
Definition memory.h:13