1 // Copyright 2012 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_UTILS_ALLOCATION_H_
6 #define V8_UTILS_ALLOCATION_H_
7
8 #include "include/v8-platform.h"
9 #include "src/base/address-region.h"
10 #include "src/base/compiler-specific.h"
11 #include "src/base/platform/platform.h"
12 #include "src/common/globals.h"
13 #include "src/init/v8.h"
14
15 namespace v8 {
16 namespace internal {
17
18 class Isolate;
19
20 // This file defines memory allocation functions. If a first attempt at an
21 // allocation fails, these functions call back into the embedder, then attempt
22 // the allocation a second time. The embedder callback must not reenter V8.
23
24 // Called when allocation routines fail to allocate, even with a possible retry.
25 // This function should not return, but should terminate the current processing.
26 [[noreturn]] V8_EXPORT_PRIVATE void FatalProcessOutOfMemory(
27 Isolate* isolate, const char* message);
28
29 // Superclass for classes managed with new & delete.
30 class V8_EXPORT_PRIVATE Malloced {
31 public:
32 static void* operator new(size_t size);
33 static void operator delete(void* p);
34 };
35
36 template <typename T>
NewArray(size_t size)37 T* NewArray(size_t size) {
38 T* result = new (std::nothrow) T[size];
39 if (result == nullptr) {
40 V8::GetCurrentPlatform()->OnCriticalMemoryPressure();
41 result = new (std::nothrow) T[size];
42 if (result == nullptr) FatalProcessOutOfMemory(nullptr, "NewArray");
43 }
44 return result;
45 }
46
47 template <typename T, typename = typename std::enable_if<
48 base::is_trivially_copyable<T>::value>::type>
NewArray(size_t size,T default_val)49 T* NewArray(size_t size, T default_val) {
50 T* result = reinterpret_cast<T*>(NewArray<uint8_t>(sizeof(T) * size));
51 for (size_t i = 0; i < size; ++i) result[i] = default_val;
52 return result;
53 }
54
55 template <typename T>
DeleteArray(T * array)56 void DeleteArray(T* array) {
57 delete[] array;
58 }
59
60 // The normal strdup functions use malloc. These versions of StrDup
61 // and StrNDup uses new and calls the FatalProcessOutOfMemory handler
62 // if allocation fails.
63 V8_EXPORT_PRIVATE char* StrDup(const char* str);
64 char* StrNDup(const char* str, int n);
65
66 // Allocation policy for allocating in the C free store using malloc
67 // and free. Used as the default policy for lists.
68 class FreeStoreAllocationPolicy {
69 public:
70 template <typename T, typename TypeTag = T[]>
NewArray(size_t length)71 V8_INLINE T* NewArray(size_t length) {
72 return static_cast<T*>(Malloced::operator new(length * sizeof(T)));
73 }
74 template <typename T, typename TypeTag = T[]>
DeleteArray(T * p,size_t length)75 V8_INLINE void DeleteArray(T* p, size_t length) {
76 Malloced::operator delete(p);
77 }
78 };
79
80 // Performs a malloc, with retry logic on failure. Returns nullptr on failure.
81 // Call free to release memory allocated with this function.
82 void* AllocWithRetry(size_t size);
83
84 V8_EXPORT_PRIVATE void* AlignedAlloc(size_t size, size_t alignment);
85 V8_EXPORT_PRIVATE void AlignedFree(void* ptr);
86
87 // Returns platfrom page allocator instance. Guaranteed to be a valid pointer.
88 V8_EXPORT_PRIVATE v8::PageAllocator* GetPlatformPageAllocator();
89
90 // Sets the given page allocator as the platform page allocator and returns
91 // the current one. This function *must* be used only for testing purposes.
92 // It is not thread-safe and the testing infrastructure should ensure that
93 // the tests do not modify the value simultaneously.
94 V8_EXPORT_PRIVATE v8::PageAllocator* SetPlatformPageAllocatorForTesting(
95 v8::PageAllocator* page_allocator);
96
97 // Gets the page granularity for AllocatePages and FreePages. Addresses returned
98 // by AllocatePages are aligned to this size.
99 V8_EXPORT_PRIVATE size_t AllocatePageSize();
100
101 // Gets the granularity at which the permissions and release calls can be made.
102 V8_EXPORT_PRIVATE size_t CommitPageSize();
103
104 // Sets the random seed so that GetRandomMmapAddr() will generate repeatable
105 // sequences of random mmap addresses.
106 V8_EXPORT_PRIVATE void SetRandomMmapSeed(int64_t seed);
107
108 // Generate a random address to be used for hinting allocation calls.
109 V8_EXPORT_PRIVATE void* GetRandomMmapAddr();
110
111 // Allocates memory. Permissions are set according to the access argument.
112 // |address| is a hint. |size| and |alignment| must be multiples of
113 // AllocatePageSize(). Returns the address of the allocated memory, with the
114 // specified size and alignment, or nullptr on failure.
115 V8_EXPORT_PRIVATE
116 V8_WARN_UNUSED_RESULT void* AllocatePages(v8::PageAllocator* page_allocator,
117 void* address, size_t size,
118 size_t alignment,
119 PageAllocator::Permission access);
120
121 // Frees memory allocated by a call to AllocatePages. |address| and |size| must
122 // be multiples of AllocatePageSize(). Returns true on success, otherwise false.
123 V8_EXPORT_PRIVATE
124 V8_WARN_UNUSED_RESULT bool FreePages(v8::PageAllocator* page_allocator,
125 void* address, const size_t size);
126
127 // Releases memory that is no longer needed. The range specified by |address|
128 // and |size| must be an allocated memory region. |size| and |new_size| must be
129 // multiples of CommitPageSize(). Memory from |new_size| to |size| is released.
130 // Released memory is left in an undefined state, so it should not be accessed.
131 // Returns true on success, otherwise false.
132 V8_EXPORT_PRIVATE
133 V8_WARN_UNUSED_RESULT bool ReleasePages(v8::PageAllocator* page_allocator,
134 void* address, size_t size,
135 size_t new_size);
136
137 // Sets permissions according to |access|. |address| and |size| must be
138 // multiples of CommitPageSize(). Setting permission to kNoAccess may
139 // cause the memory contents to be lost. Returns true on success, otherwise
140 // false.
141 V8_EXPORT_PRIVATE
142 V8_WARN_UNUSED_RESULT bool SetPermissions(v8::PageAllocator* page_allocator,
143 void* address, size_t size,
144 PageAllocator::Permission access);
SetPermissions(v8::PageAllocator * page_allocator,Address address,size_t size,PageAllocator::Permission access)145 inline bool SetPermissions(v8::PageAllocator* page_allocator, Address address,
146 size_t size, PageAllocator::Permission access) {
147 return SetPermissions(page_allocator, reinterpret_cast<void*>(address), size,
148 access);
149 }
150
151 // Function that may release reserved memory regions to allow failed allocations
152 // to succeed. |length| is the amount of memory needed. Returns |true| if memory
153 // could be released, false otherwise.
154 V8_EXPORT_PRIVATE bool OnCriticalMemoryPressure(size_t length);
155
156 // Represents and controls an area of reserved memory.
157 class VirtualMemory final {
158 public:
159 enum JitPermission { kNoJit, kMapAsJittable };
160
161 // Empty VirtualMemory object, controlling no reserved memory.
162 V8_EXPORT_PRIVATE VirtualMemory();
163
164 // Reserves virtual memory containing an area of the given size that is
165 // aligned per |alignment| rounded up to the |page_allocator|'s allocate page
166 // size. The |size| must be aligned with |page_allocator|'s commit page size.
167 // This may not be at the position returned by address().
168 V8_EXPORT_PRIVATE VirtualMemory(v8::PageAllocator* page_allocator,
169 size_t size, void* hint, size_t alignment = 1,
170 JitPermission jit = kNoJit);
171
172 // Construct a virtual memory by assigning it some already mapped address
173 // and size.
VirtualMemory(v8::PageAllocator * page_allocator,Address address,size_t size)174 VirtualMemory(v8::PageAllocator* page_allocator, Address address, size_t size)
175 : page_allocator_(page_allocator), region_(address, size) {
176 DCHECK_NOT_NULL(page_allocator);
177 DCHECK(IsAligned(address, page_allocator->AllocatePageSize()));
178 DCHECK(IsAligned(size, page_allocator->CommitPageSize()));
179 }
180
181 // Releases the reserved memory, if any, controlled by this VirtualMemory
182 // object.
183 V8_EXPORT_PRIVATE ~VirtualMemory();
184
185 // Move constructor.
VirtualMemory(VirtualMemory && other)186 VirtualMemory(VirtualMemory&& other) V8_NOEXCEPT { *this = std::move(other); }
187
188 // Move assignment operator.
189 VirtualMemory& operator=(VirtualMemory&& other) V8_NOEXCEPT {
190 DCHECK(!IsReserved());
191 page_allocator_ = other.page_allocator_;
192 region_ = other.region_;
193 other.Reset();
194 return *this;
195 }
196
197 // Returns whether the memory has been reserved.
IsReserved()198 bool IsReserved() const { return region_.begin() != kNullAddress; }
199
200 // Initialize or resets an embedded VirtualMemory object.
201 V8_EXPORT_PRIVATE void Reset();
202
page_allocator()203 v8::PageAllocator* page_allocator() { return page_allocator_; }
204
region()205 base::AddressRegion region() const { return region_; }
206
207 // Returns the start address of the reserved memory.
208 // If the memory was reserved with an alignment, this address is not
209 // necessarily aligned. The user might need to round it up to a multiple of
210 // the alignment to get the start of the aligned block.
address()211 Address address() const {
212 DCHECK(IsReserved());
213 return region_.begin();
214 }
215
end()216 Address end() const {
217 DCHECK(IsReserved());
218 return region_.end();
219 }
220
221 // Returns the size of the reserved memory. The returned value is only
222 // meaningful when IsReserved() returns true.
223 // If the memory was reserved with an alignment, this size may be larger
224 // than the requested size.
size()225 size_t size() const { return region_.size(); }
226
227 // Sets permissions according to the access argument. address and size must be
228 // multiples of CommitPageSize(). Returns true on success, otherwise false.
229 V8_EXPORT_PRIVATE bool SetPermissions(Address address, size_t size,
230 PageAllocator::Permission access);
231
232 // Releases memory after |free_start|. Returns the number of bytes released.
233 V8_EXPORT_PRIVATE size_t Release(Address free_start);
234
235 // Frees all memory.
236 V8_EXPORT_PRIVATE void Free();
237
238 // As with Free but does not write to the VirtualMemory object itself so it
239 // can be called on a VirtualMemory that is itself not writable.
240 V8_EXPORT_PRIVATE void FreeReadOnly();
241
InVM(Address address,size_t size)242 bool InVM(Address address, size_t size) {
243 return region_.contains(address, size);
244 }
245
246 private:
247 // Page allocator that controls the virtual memory.
248 v8::PageAllocator* page_allocator_ = nullptr;
249 base::AddressRegion region_;
250
251 DISALLOW_COPY_AND_ASSIGN(VirtualMemory);
252 };
253
254 } // namespace internal
255 } // namespace v8
256
257 #endif // V8_UTILS_ALLOCATION_H_
258