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.
5 // Platform-specific code for MacOS goes here. For the POSIX-compatible
6 // parts, the implementation is in platform-posix.cc.
9 #include <mach/mach_init.h>
10 #include <mach-o/dyld.h>
11 #include <mach-o/getsect.h>
15 #include <AvailabilityMacros.h>
18 #include <libkern/OSAtomic.h>
19 #include <mach/mach.h>
20 #include <mach/semaphore.h>
21 #include <mach/task.h>
22 #include <mach/vm_statistics.h>
24 #include <semaphore.h>
29 #include <sys/resource.h>
30 #include <sys/sysctl.h>
32 #include <sys/types.h>
38 #include "src/base/macros.h"
39 #include "src/base/platform/platform.h"
46 // Constants used for mmap.
47 // kMmapFd is used to pass vm_alloc flags to tag the region with the user
48 // defined tag 255 This helps identify V8-allocated regions in memory analysis
49 // tools like vmmap(1).
50 static const int kMmapFd = VM_MAKE_TAG(255);
51 static const off_t kMmapFdOffset = 0;
54 void* OS::Allocate(const size_t requested,
57 const size_t msize = RoundUp(requested, getpagesize());
58 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
59 void* mbase = mmap(OS::GetRandomMmapAddr(),
62 MAP_PRIVATE | MAP_ANON,
65 if (mbase == MAP_FAILED) return NULL;
71 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
72 std::vector<SharedLibraryAddress> result;
73 unsigned int images_count = _dyld_image_count();
74 for (unsigned int i = 0; i < images_count; ++i) {
75 const mach_header* header = _dyld_get_image_header(i);
76 if (header == NULL) continue;
79 char* code_ptr = getsectdatafromheader_64(
80 reinterpret_cast<const mach_header_64*>(header),
86 char* code_ptr = getsectdatafromheader(header, SEG_TEXT, SECT_TEXT, &size);
88 if (code_ptr == NULL) continue;
89 const uintptr_t slide = _dyld_get_image_vmaddr_slide(i);
90 const uintptr_t start = reinterpret_cast<uintptr_t>(code_ptr) + slide;
92 SharedLibraryAddress(_dyld_get_image_name(i), start, start + size));
98 void OS::SignalCodeMovingGC() {
102 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
103 if (std::isnan(time)) return "";
104 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
105 struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn)
106 if (NULL == t) return "";
111 double OS::LocalTimeOffset(TimezoneCache* cache) {
112 time_t tv = time(NULL);
113 struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn)
114 // tm_gmtoff includes any daylight savings offset, so subtract it.
115 return static_cast<double>(t->tm_gmtoff * msPerSecond -
116 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
120 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
123 VirtualMemory::VirtualMemory(size_t size)
124 : address_(ReserveRegion(size)), size_(size) { }
127 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
128 : address_(NULL), size_(0) {
129 DCHECK((alignment % OS::AllocateAlignment()) == 0);
130 size_t request_size = RoundUp(size + alignment,
131 static_cast<intptr_t>(OS::AllocateAlignment()));
132 void* reservation = mmap(OS::GetRandomMmapAddr(),
135 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
138 if (reservation == MAP_FAILED) return;
140 uint8_t* base = static_cast<uint8_t*>(reservation);
141 uint8_t* aligned_base = RoundUp(base, alignment);
142 DCHECK_LE(base, aligned_base);
144 // Unmap extra memory reserved before and after the desired block.
145 if (aligned_base != base) {
146 size_t prefix_size = static_cast<size_t>(aligned_base - base);
147 OS::Free(base, prefix_size);
148 request_size -= prefix_size;
151 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
152 DCHECK_LE(aligned_size, request_size);
154 if (aligned_size != request_size) {
155 size_t suffix_size = request_size - aligned_size;
156 OS::Free(aligned_base + aligned_size, suffix_size);
157 request_size -= suffix_size;
160 DCHECK(aligned_size == request_size);
162 address_ = static_cast<void*>(aligned_base);
163 size_ = aligned_size;
167 VirtualMemory::~VirtualMemory() {
169 bool result = ReleaseRegion(address(), size());
176 bool VirtualMemory::IsReserved() {
177 return address_ != NULL;
181 void VirtualMemory::Reset() {
187 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
188 return CommitRegion(address, size, is_executable);
192 bool VirtualMemory::Uncommit(void* address, size_t size) {
193 return UncommitRegion(address, size);
197 bool VirtualMemory::Guard(void* address) {
198 OS::Guard(address, OS::CommitPageSize());
203 void* VirtualMemory::ReserveRegion(size_t size) {
204 void* result = mmap(OS::GetRandomMmapAddr(),
207 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
211 if (result == MAP_FAILED) return NULL;
217 bool VirtualMemory::CommitRegion(void* address,
219 bool is_executable) {
220 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
221 if (MAP_FAILED == mmap(address,
224 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
233 bool VirtualMemory::UncommitRegion(void* address, size_t size) {
237 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
239 kMmapFdOffset) != MAP_FAILED;
243 bool VirtualMemory::ReleaseRegion(void* address, size_t size) {
244 return munmap(address, size) == 0;
248 bool VirtualMemory::HasLazyCommits() {