v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
platform-freebsd.cc
Go to the documentation of this file.
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 // Platform-specific code for FreeBSD goes here. For the POSIX-compatible
29 // parts, the implementation is in platform-posix.cc.
30 
31 #include <pthread.h>
32 #include <semaphore.h>
33 #include <signal.h>
34 #include <sys/time.h>
35 #include <sys/resource.h>
36 #include <sys/types.h>
37 #include <sys/ucontext.h>
38 #include <stdlib.h>
39 
40 #include <sys/types.h> // mmap & munmap
41 #include <sys/mman.h> // mmap & munmap
42 #include <sys/stat.h> // open
43 #include <sys/fcntl.h> // open
44 #include <unistd.h> // getpagesize
45 // If you don't have execinfo.h then you need devel/libexecinfo from ports.
46 #include <strings.h> // index
47 #include <errno.h>
48 #include <stdarg.h>
49 #include <limits.h>
50 
51 #undef MAP_TYPE
52 
53 #include "v8.h"
54 #include "v8threads.h"
55 
56 #include "platform.h"
57 #include "vm-state-inl.h"
58 
59 
60 namespace v8 {
61 namespace internal {
62 
63 
64 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
65  if (std::isnan(time)) return "";
66  time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
67  struct tm* t = localtime(&tv);
68  if (NULL == t) return "";
69  return t->tm_zone;
70 }
71 
72 
73 double OS::LocalTimeOffset(TimezoneCache* cache) {
74  time_t tv = time(NULL);
75  struct tm* t = localtime(&tv);
76  // tm_gmtoff includes any daylight savings offset, so subtract it.
77  return static_cast<double>(t->tm_gmtoff * msPerSecond -
78  (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
79 }
80 
81 
82 void* OS::Allocate(const size_t requested,
83  size_t* allocated,
84  bool executable) {
85  const size_t msize = RoundUp(requested, getpagesize());
86  int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0);
87  void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
88 
89  if (mbase == MAP_FAILED) {
90  LOG(Isolate::Current(), StringEvent("OS::Allocate", "mmap failed"));
91  return NULL;
92  }
93  *allocated = msize;
94  return mbase;
95 }
96 
97 
98 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
99  public:
100  PosixMemoryMappedFile(FILE* file, void* memory, int size)
101  : file_(file), memory_(memory), size_(size) { }
102  virtual ~PosixMemoryMappedFile();
103  virtual void* memory() { return memory_; }
104  virtual int size() { return size_; }
105  private:
106  FILE* file_;
107  void* memory_;
108  int size_;
109 };
110 
111 
112 OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
113  FILE* file = fopen(name, "r+");
114  if (file == NULL) return NULL;
115 
116  fseek(file, 0, SEEK_END);
117  int size = ftell(file);
118 
119  void* memory =
120  mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
121  return new PosixMemoryMappedFile(file, memory, size);
122 }
123 
124 
125 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
126  void* initial) {
127  FILE* file = fopen(name, "w+");
128  if (file == NULL) return NULL;
129  int result = fwrite(initial, size, 1, file);
130  if (result < 1) {
131  fclose(file);
132  return NULL;
133  }
134  void* memory =
135  mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
136  return new PosixMemoryMappedFile(file, memory, size);
137 }
138 
139 
141  if (memory_) munmap(memory_, size_);
142  fclose(file_);
143 }
144 
145 
146 static unsigned StringToLong(char* buffer) {
147  return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT
148 }
149 
150 
151 void OS::LogSharedLibraryAddresses(Isolate* isolate) {
152  static const int MAP_LENGTH = 1024;
153  int fd = open("/proc/self/maps", O_RDONLY);
154  if (fd < 0) return;
155  while (true) {
156  char addr_buffer[11];
157  addr_buffer[0] = '0';
158  addr_buffer[1] = 'x';
159  addr_buffer[10] = 0;
160  int result = read(fd, addr_buffer + 2, 8);
161  if (result < 8) break;
162  unsigned start = StringToLong(addr_buffer);
163  result = read(fd, addr_buffer + 2, 1);
164  if (result < 1) break;
165  if (addr_buffer[2] != '-') break;
166  result = read(fd, addr_buffer + 2, 8);
167  if (result < 8) break;
168  unsigned end = StringToLong(addr_buffer);
169  char buffer[MAP_LENGTH];
170  int bytes_read = -1;
171  do {
172  bytes_read++;
173  if (bytes_read >= MAP_LENGTH - 1)
174  break;
175  result = read(fd, buffer + bytes_read, 1);
176  if (result < 1) break;
177  } while (buffer[bytes_read] != '\n');
178  buffer[bytes_read] = 0;
179  // Ignore mappings that are not executable.
180  if (buffer[3] != 'x') continue;
181  char* start_of_path = index(buffer, '/');
182  // There may be no filename in this line. Skip to next.
183  if (start_of_path == NULL) continue;
184  buffer[bytes_read] = 0;
185  LOG(isolate, SharedLibraryEvent(start_of_path, start, end));
186  }
187  close(fd);
188 }
189 
190 
191 void OS::SignalCodeMovingGC() {
192 }
193 
194 
195 
196 // Constants used for mmap.
197 static const int kMmapFd = -1;
198 static const int kMmapFdOffset = 0;
199 
200 
201 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
202 
203 
204 VirtualMemory::VirtualMemory(size_t size)
205  : address_(ReserveRegion(size)), size_(size) { }
206 
207 
208 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
209  : address_(NULL), size_(0) {
210  ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
211  size_t request_size = RoundUp(size + alignment,
212  static_cast<intptr_t>(OS::AllocateAlignment()));
213  void* reservation = mmap(OS::GetRandomMmapAddr(),
214  request_size,
215  PROT_NONE,
216  MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
217  kMmapFd,
218  kMmapFdOffset);
219  if (reservation == MAP_FAILED) return;
220 
221  Address base = static_cast<Address>(reservation);
222  Address aligned_base = RoundUp(base, alignment);
223  ASSERT_LE(base, aligned_base);
224 
225  // Unmap extra memory reserved before and after the desired block.
226  if (aligned_base != base) {
227  size_t prefix_size = static_cast<size_t>(aligned_base - base);
228  OS::Free(base, prefix_size);
229  request_size -= prefix_size;
230  }
231 
232  size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
233  ASSERT_LE(aligned_size, request_size);
234 
235  if (aligned_size != request_size) {
236  size_t suffix_size = request_size - aligned_size;
237  OS::Free(aligned_base + aligned_size, suffix_size);
238  request_size -= suffix_size;
239  }
240 
241  ASSERT(aligned_size == request_size);
242 
243  address_ = static_cast<void*>(aligned_base);
244  size_ = aligned_size;
245 }
246 
247 
249  if (IsReserved()) {
250  bool result = ReleaseRegion(address(), size());
251  ASSERT(result);
252  USE(result);
253  }
254 }
255 
256 
258  return address_ != NULL;
259 }
260 
261 
262 void VirtualMemory::Reset() {
263  address_ = NULL;
264  size_ = 0;
265 }
266 
267 
268 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
269  return CommitRegion(address, size, is_executable);
270 }
271 
272 
273 bool VirtualMemory::Uncommit(void* address, size_t size) {
274  return UncommitRegion(address, size);
275 }
276 
277 
278 bool VirtualMemory::Guard(void* address) {
279  OS::Guard(address, OS::CommitPageSize());
280  return true;
281 }
282 
283 
284 void* VirtualMemory::ReserveRegion(size_t size) {
285  void* result = mmap(OS::GetRandomMmapAddr(),
286  size,
287  PROT_NONE,
288  MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
289  kMmapFd,
290  kMmapFdOffset);
291 
292  if (result == MAP_FAILED) return NULL;
293 
294  return result;
295 }
296 
297 
298 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
299  int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
300  if (MAP_FAILED == mmap(base,
301  size,
302  prot,
303  MAP_PRIVATE | MAP_ANON | MAP_FIXED,
304  kMmapFd,
305  kMmapFdOffset)) {
306  return false;
307  }
308  return true;
309 }
310 
311 
312 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
313  return mmap(base,
314  size,
315  PROT_NONE,
316  MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | MAP_FIXED,
317  kMmapFd,
318  kMmapFdOffset) != MAP_FAILED;
319 }
320 
321 
322 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
323  return munmap(base, size) == 0;
324 }
325 
326 
328  // TODO(alph): implement for the platform.
329  return false;
330 }
331 
332 } } // namespace v8::internal
byte * Address
Definition: globals.h:186
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
static void Free(void *address, const size_t size)
static void LogSharedLibraryAddresses(Isolate *isolate)
#define LOG(isolate, Call)
Definition: log.h:86
static void SignalCodeMovingGC()
static void * GetRandomMmapAddr()
static void * ReserveRegion(size_t size)
PosixMemoryMappedFile(FILE *file, void *memory, int size)
#define ASSERT(condition)
Definition: checks.h:329
static MemoryMappedFile * open(const char *name)
int isnan(double x)
bool IsAligned(T value, U alignment)
Definition: utils.h:211
static MemoryMappedFile * create(const char *name, int size, void *initial)
bool Commit(void *address, size_t size, bool is_executable)
static void Guard(void *address, const size_t size)
T RoundUp(T x, intptr_t m)
Definition: utils.h:144
#define ASSERT_LE(v1, v2)
Definition: checks.h:334
static const char * LocalTimezone(double time, TimezoneCache *cache)
static bool ReleaseRegion(void *base, size_t size)
static bool CommitRegion(void *base, size_t size, bool is_executable)
static void * Allocate(const size_t requested, size_t *allocated, bool is_executable)
static bool UncommitRegion(void *base, size_t size)
void USE(T)
Definition: globals.h:341
static size_t AllocateAlignment()
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter trace hydrogen to given file name trace inlining decisions trace store elimination trace all use positions trace global value numbering trace hydrogen escape analysis trace the tracking of allocation sites trace map generalization environment for every instruction deoptimize every n garbage collections put a break point before deoptimizing deoptimize uncommon cases use on stack replacement trace array bounds check elimination perform array index dehoisting use load elimination use store elimination use constant folding eliminate unreachable code number of stress runs when picking a function to watch for shared function not JSFunction itself flushes the cache of optimized code for closures on every GC functions with arguments object maximum number of escape analysis fix point iterations allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms concurrent on stack replacement do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes number of stack frames inspected by the profiler percentage of ICs that must have type info to allow optimization extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV 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 expose natives in global object expose freeBuffer extension expose gc extension under the specified name expose externalize string extension number of stack frames to capture disable builtin natives files print name of functions for which code is generated use random jit cookie to mask large constants trace lazy optimization use adaptive optimizations always try to OSR functions trace optimize function deoptimization minimum length for automatic enable preparsing maximum number of optimization attempts before giving up cache prototype transitions trace debugging JSON request response trace out of bounds accesses to external arrays trace_js_array_abuse automatically set the debug break flag when debugger commands are in the queue abort by crashing maximum length of function source code printed in a stack trace max size of the new max size of the old max size of executable always perform global GCs print one trace line following each garbage collection do not print trace line after scavenger collection print statistics of the maximum memory committed for the heap in name
Definition: flags.cc:505
static intptr_t CommitPageSize()
static double LocalTimeOffset(TimezoneCache *cache)
bool Uncommit(void *address, size_t size)