V8 Project
platform-macos.cc
Go to the documentation of this file.
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 // Platform-specific code for MacOS goes here. For the POSIX-compatible
6 // parts, the implementation is in platform-posix.cc.
7 
8 #include <dlfcn.h>
9 #include <mach/mach_init.h>
10 #include <mach-o/dyld.h>
11 #include <mach-o/getsect.h>
12 #include <sys/mman.h>
13 #include <unistd.h>
14 
15 #include <AvailabilityMacros.h>
16 
17 #include <errno.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>
23 #include <pthread.h>
24 #include <semaphore.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/resource.h>
30 #include <sys/sysctl.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 
34 #include <cmath>
35 
36 #undef MAP_TYPE
37 
38 #include "src/base/macros.h"
40 
41 
42 namespace v8 {
43 namespace base {
44 
45 
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;
52 
53 
54 void* OS::Allocate(const size_t requested,
55  size_t* allocated,
56  bool is_executable) {
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(),
60  msize,
61  prot,
62  MAP_PRIVATE | MAP_ANON,
63  kMmapFd,
65  if (mbase == MAP_FAILED) return NULL;
66  *allocated = msize;
67  return mbase;
68 }
69 
70 
71 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
72  public:
74  : file_(file), memory_(memory), size_(size) { }
76  virtual void* memory() { return memory_; }
77  virtual int size() { return size_; }
78  private:
79  FILE* file_;
80  void* memory_;
81  int size_;
82 };
83 
84 
85 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
86  FILE* file = fopen(name, "r+");
87  if (file == NULL) return NULL;
88 
89  fseek(file, 0, SEEK_END);
90  int size = ftell(file);
91 
92  void* memory =
93  mmap(OS::GetRandomMmapAddr(),
94  size,
95  PROT_READ | PROT_WRITE,
96  MAP_SHARED,
97  fileno(file),
98  0);
99  return new PosixMemoryMappedFile(file, memory, size);
100 }
101 
102 
103 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
104  void* initial) {
105  FILE* file = fopen(name, "w+");
106  if (file == NULL) return NULL;
107  int result = fwrite(initial, size, 1, file);
108  if (result < 1) {
109  fclose(file);
110  return NULL;
111  }
112  void* memory =
113  mmap(OS::GetRandomMmapAddr(),
114  size,
115  PROT_READ | PROT_WRITE,
116  MAP_SHARED,
117  fileno(file),
118  0);
119  return new PosixMemoryMappedFile(file, memory, size);
120 }
121 
122 
124  if (memory_) OS::Free(memory_, size_);
125  fclose(file_);
126 }
127 
128 
129 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
130  std::vector<SharedLibraryAddress> result;
131  unsigned int images_count = _dyld_image_count();
132  for (unsigned int i = 0; i < images_count; ++i) {
133  const mach_header* header = _dyld_get_image_header(i);
134  if (header == NULL) continue;
135 #if V8_HOST_ARCH_X64
136  uint64_t size;
137  char* code_ptr = getsectdatafromheader_64(
138  reinterpret_cast<const mach_header_64*>(header),
139  SEG_TEXT,
140  SECT_TEXT,
141  &size);
142 #else
143  unsigned int size;
144  char* code_ptr = getsectdatafromheader(header, SEG_TEXT, SECT_TEXT, &size);
145 #endif
146  if (code_ptr == NULL) continue;
147  const uintptr_t slide = _dyld_get_image_vmaddr_slide(i);
148  const uintptr_t start = reinterpret_cast<uintptr_t>(code_ptr) + slide;
149  result.push_back(
150  SharedLibraryAddress(_dyld_get_image_name(i), start, start + size));
151  }
152  return result;
153 }
154 
155 
156 void OS::SignalCodeMovingGC() {
157 }
158 
159 
160 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
161  if (std::isnan(time)) return "";
162  time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
163  struct tm* t = localtime(&tv);
164  if (NULL == t) return "";
165  return t->tm_zone;
166 }
167 
168 
169 double OS::LocalTimeOffset(TimezoneCache* cache) {
170  time_t tv = time(NULL);
171  struct tm* t = localtime(&tv);
172  // tm_gmtoff includes any daylight savings offset, so subtract it.
173  return static_cast<double>(t->tm_gmtoff * msPerSecond -
174  (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
175 }
176 
177 
178 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
179 
180 
182  : address_(ReserveRegion(size)), size_(size) { }
183 
184 
185 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
186  : address_(NULL), size_(0) {
187  DCHECK((alignment % OS::AllocateAlignment()) == 0);
188  size_t request_size = RoundUp(size + alignment,
189  static_cast<intptr_t>(OS::AllocateAlignment()));
190  void* reservation = mmap(OS::GetRandomMmapAddr(),
191  request_size,
192  PROT_NONE,
193  MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
194  kMmapFd,
195  kMmapFdOffset);
196  if (reservation == MAP_FAILED) return;
197 
198  uint8_t* base = static_cast<uint8_t*>(reservation);
199  uint8_t* aligned_base = RoundUp(base, alignment);
200  DCHECK_LE(base, aligned_base);
201 
202  // Unmap extra memory reserved before and after the desired block.
203  if (aligned_base != base) {
204  size_t prefix_size = static_cast<size_t>(aligned_base - base);
205  OS::Free(base, prefix_size);
206  request_size -= prefix_size;
207  }
208 
209  size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
210  DCHECK_LE(aligned_size, request_size);
211 
212  if (aligned_size != request_size) {
213  size_t suffix_size = request_size - aligned_size;
214  OS::Free(aligned_base + aligned_size, suffix_size);
215  request_size -= suffix_size;
216  }
217 
218  DCHECK(aligned_size == request_size);
219 
220  address_ = static_cast<void*>(aligned_base);
221  size_ = aligned_size;
222 }
223 
224 
226  if (IsReserved()) {
227  bool result = ReleaseRegion(address(), size());
228  DCHECK(result);
229  USE(result);
230  }
231 }
232 
233 
235  return address_ != NULL;
236 }
237 
238 
239 void VirtualMemory::Reset() {
240  address_ = NULL;
241  size_ = 0;
242 }
243 
244 
245 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
246  return CommitRegion(address, size, is_executable);
247 }
248 
249 
250 bool VirtualMemory::Uncommit(void* address, size_t size) {
251  return UncommitRegion(address, size);
252 }
253 
254 
255 bool VirtualMemory::Guard(void* address) {
257  return true;
258 }
259 
260 
261 void* VirtualMemory::ReserveRegion(size_t size) {
262  void* result = mmap(OS::GetRandomMmapAddr(),
263  size,
264  PROT_NONE,
265  MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
266  kMmapFd,
267  kMmapFdOffset);
268 
269  if (result == MAP_FAILED) return NULL;
270 
271  return result;
272 }
273 
274 
275 bool VirtualMemory::CommitRegion(void* address,
276  size_t size,
277  bool is_executable) {
278  int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
279  if (MAP_FAILED == mmap(address,
280  size,
281  prot,
282  MAP_PRIVATE | MAP_ANON | MAP_FIXED,
283  kMmapFd,
284  kMmapFdOffset)) {
285  return false;
286  }
287  return true;
288 }
289 
290 
291 bool VirtualMemory::UncommitRegion(void* address, size_t size) {
292  return mmap(address,
293  size,
294  PROT_NONE,
295  MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
296  kMmapFd,
297  kMmapFdOffset) != MAP_FAILED;
298 }
299 
300 
301 bool VirtualMemory::ReleaseRegion(void* address, size_t size) {
302  return munmap(address, size) == 0;
303 }
304 
305 
307  return false;
308 }
309 
310 } } // namespace v8::base
static MemoryMappedFile * create(const char *name, int size, void *initial)
static MemoryMappedFile * open(const char *name)
static void * GetRandomMmapAddr()
static size_t AllocateAlignment()
static void SignalCodeMovingGC()
static intptr_t CommitPageSize()
static void Guard(void *address, const size_t size)
static void * Allocate(const size_t requested, size_t *allocated, bool is_executable)
static const char * LocalTimezone(double time, TimezoneCache *cache)
static std::vector< SharedLibraryAddress > GetSharedLibraryAddresses()
static double LocalTimeOffset(TimezoneCache *cache)
static void Free(void *address, const size_t size)
static const int msPerSecond
Definition: platform.h:303
PosixMemoryMappedFile(FILE *file, void *memory, int size)
bool Commit(void *address, size_t size, bool is_executable)
static bool UncommitRegion(void *base, size_t size)
static bool ReleaseRegion(void *base, size_t size)
bool Guard(void *address)
bool Uncommit(void *address, size_t size)
static bool CommitRegion(void *base, size_t size, bool is_executable)
static void * ReserveRegion(size_t size)
enable harmony numeric enable harmony object literal extensions Optimize object size
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in name
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in only print modified registers Trace simulator debug messages Implied by trace sim abort randomize hashes to avoid predictable hash Fixed seed to use to hash property Print the time it takes to deserialize the snapshot A filename with extra code to be included in the A file to write the raw snapshot bytes A file to write the raw context snapshot bytes Write V8 startup blob file(mksnapshot only)") DEFINE_BOOL(profile_hydrogen_code_stub_compilation
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
#define DCHECK_LE(v1, v2)
Definition: logging.h:210
#define DCHECK(condition)
Definition: logging.h:205
void USE(T)
Definition: macros.h:322
T RoundUp(T x, intptr_t m)
Definition: macros.h:407
static const int kMmapFdOffset
static const int kMmapFd
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20