The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / v8 / src / platform-macos.cc @ 40c0f755
History | View | Annotate | Download (14.3 KB)
1 |
// Copyright 2006-2008 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 MacOS goes here. For the POSIX comaptible parts
|
29 |
// the implementation is in platform-posix.cc.
|
30 |
|
31 |
#include <ucontext.h> |
32 |
#include <unistd.h> |
33 |
#include <sys/mman.h> |
34 |
#include <mach/mach_init.h> |
35 |
|
36 |
#include <AvailabilityMacros.h> |
37 |
|
38 |
#ifdef MAC_OS_X_VERSION_10_5
|
39 |
# include <execinfo.h> // backtrace, backtrace_symbols |
40 |
#endif
|
41 |
|
42 |
#include <pthread.h> |
43 |
#include <semaphore.h> |
44 |
#include <signal.h> |
45 |
#include <mach/semaphore.h> |
46 |
#include <mach/task.h> |
47 |
#include <sys/time.h> |
48 |
#include <sys/resource.h> |
49 |
#include <sys/types.h> |
50 |
#include <stdarg.h> |
51 |
#include <stdlib.h> |
52 |
|
53 |
#include <errno.h> |
54 |
|
55 |
#undef MAP_TYPE
|
56 |
|
57 |
#include "v8.h" |
58 |
|
59 |
#include "platform.h" |
60 |
|
61 |
namespace v8 { namespace internal { |
62 |
|
63 |
// 0 is never a valid thread id on MacOSX since a ptread_t is
|
64 |
// a pointer.
|
65 |
static const pthread_t kNoThread = (pthread_t) 0; |
66 |
|
67 |
|
68 |
double ceiling(double x) { |
69 |
// Correct Mac OS X Leopard 'ceil' behavior.
|
70 |
if (-1.0 < x && x < 0.0) { |
71 |
return -0.0; |
72 |
} else {
|
73 |
return ceil(x);
|
74 |
} |
75 |
} |
76 |
|
77 |
|
78 |
void OS::Setup() {
|
79 |
// Seed the random number generator.
|
80 |
// Convert the current time to a 64-bit integer first, before converting it
|
81 |
// to an unsigned. Going directly will cause an overflow and the seed to be
|
82 |
// set to all ones. The seed will be identical for different instances that
|
83 |
// call this setup code within the same millisecond.
|
84 |
uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis());
|
85 |
srandom(static_cast<unsigned int>(seed)); |
86 |
} |
87 |
|
88 |
|
89 |
// We keep the lowest and highest addresses mapped as a quick way of
|
90 |
// determining that pointers are outside the heap (used mostly in assertions
|
91 |
// and verification). The estimate is conservative, ie, not all addresses in
|
92 |
// 'allocated' space are actually allocated to our heap. The range is
|
93 |
// [lowest, highest), inclusive on the low and and exclusive on the high end.
|
94 |
static void* lowest_ever_allocated = reinterpret_cast<void*>(-1); |
95 |
static void* highest_ever_allocated = reinterpret_cast<void*>(0); |
96 |
|
97 |
|
98 |
static void UpdateAllocatedSpaceLimits(void* address, int size) { |
99 |
lowest_ever_allocated = Min(lowest_ever_allocated, address); |
100 |
highest_ever_allocated = |
101 |
Max(highest_ever_allocated, |
102 |
reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size)); |
103 |
} |
104 |
|
105 |
|
106 |
bool OS::IsOutsideAllocatedSpace(void* address) { |
107 |
return address < lowest_ever_allocated || address >= highest_ever_allocated;
|
108 |
} |
109 |
|
110 |
|
111 |
size_t OS::AllocateAlignment() { |
112 |
return getpagesize();
|
113 |
} |
114 |
|
115 |
|
116 |
void* OS::Allocate(const size_t requested, |
117 |
size_t* allocated, |
118 |
bool is_executable) {
|
119 |
const size_t msize = RoundUp(requested, getpagesize());
|
120 |
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
121 |
void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0); |
122 |
if (mbase == MAP_FAILED) {
|
123 |
LOG(StringEvent("OS::Allocate", "mmap failed")); |
124 |
return NULL; |
125 |
} |
126 |
*allocated = msize; |
127 |
UpdateAllocatedSpaceLimits(mbase, msize); |
128 |
return mbase;
|
129 |
} |
130 |
|
131 |
|
132 |
void OS::Free(void* address, const size_t size) { |
133 |
// TODO(1240712): munmap has a return value which is ignored here.
|
134 |
munmap(address, size); |
135 |
} |
136 |
|
137 |
|
138 |
#ifdef ENABLE_HEAP_PROTECTION
|
139 |
|
140 |
void OS::Protect(void* address, size_t size) { |
141 |
UNIMPLEMENTED(); |
142 |
} |
143 |
|
144 |
|
145 |
void OS::Unprotect(void* address, size_t size, bool is_executable) { |
146 |
UNIMPLEMENTED(); |
147 |
} |
148 |
|
149 |
#endif
|
150 |
|
151 |
|
152 |
void OS::Sleep(int milliseconds) { |
153 |
usleep(1000 * milliseconds);
|
154 |
} |
155 |
|
156 |
|
157 |
void OS::Abort() {
|
158 |
// Redirect to std abort to signal abnormal program termination
|
159 |
abort(); |
160 |
} |
161 |
|
162 |
|
163 |
void OS::DebugBreak() {
|
164 |
asm("int $3"); |
165 |
} |
166 |
|
167 |
|
168 |
class PosixMemoryMappedFile : public OS::MemoryMappedFile { |
169 |
public:
|
170 |
PosixMemoryMappedFile(FILE* file, void* memory, int size) |
171 |
: file_(file), memory_(memory), size_(size) { } |
172 |
virtual ~PosixMemoryMappedFile();
|
173 |
virtual void* memory() { return memory_; } |
174 |
private:
|
175 |
FILE* file_; |
176 |
void* memory_;
|
177 |
int size_;
|
178 |
}; |
179 |
|
180 |
|
181 |
OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size, |
182 |
void* initial) {
|
183 |
FILE* file = fopen(name, "w+");
|
184 |
if (file == NULL) return NULL; |
185 |
fwrite(initial, size, 1, file);
|
186 |
void* memory =
|
187 |
mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0); |
188 |
return new PosixMemoryMappedFile(file, memory, size); |
189 |
} |
190 |
|
191 |
|
192 |
PosixMemoryMappedFile::~PosixMemoryMappedFile() { |
193 |
if (memory_) munmap(memory_, size_);
|
194 |
fclose(file_); |
195 |
} |
196 |
|
197 |
|
198 |
void OS::LogSharedLibraryAddresses() {
|
199 |
// TODO(1233579): Implement.
|
200 |
} |
201 |
|
202 |
|
203 |
double OS::nan_value() {
|
204 |
return NAN;
|
205 |
} |
206 |
|
207 |
|
208 |
int OS::ActivationFrameAlignment() {
|
209 |
// OS X activation frames must be 16 byte-aligned; see "Mac OS X ABI
|
210 |
// Function Call Guide".
|
211 |
return 16; |
212 |
} |
213 |
|
214 |
|
215 |
int OS::StackWalk(StackFrame* frames, int frames_size) { |
216 |
#ifndef MAC_OS_X_VERSION_10_5
|
217 |
return 0; |
218 |
#else
|
219 |
void** addresses = NewArray<void*>(frames_size); |
220 |
int frames_count = backtrace(addresses, frames_size);
|
221 |
|
222 |
char** symbols;
|
223 |
symbols = backtrace_symbols(addresses, frames_count); |
224 |
if (symbols == NULL) { |
225 |
DeleteArray(addresses); |
226 |
return kStackWalkError;
|
227 |
} |
228 |
|
229 |
for (int i = 0; i < frames_count; i++) { |
230 |
frames[i].address = addresses[i]; |
231 |
// Format a text representation of the frame based on the information
|
232 |
// available.
|
233 |
SNPrintF(MutableCStrVector(frames[i].text, |
234 |
kStackWalkMaxTextLen), |
235 |
"%s",
|
236 |
symbols[i]); |
237 |
// Make sure line termination is in place.
|
238 |
frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; |
239 |
} |
240 |
|
241 |
DeleteArray(addresses); |
242 |
free(symbols); |
243 |
|
244 |
return frames_count;
|
245 |
#endif
|
246 |
} |
247 |
|
248 |
|
249 |
// Constants used for mmap.
|
250 |
static const int kMmapFd = -1; |
251 |
static const int kMmapFdOffset = 0; |
252 |
|
253 |
|
254 |
VirtualMemory::VirtualMemory(size_t size) { |
255 |
address_ = mmap(NULL, size, PROT_NONE,
|
256 |
MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
257 |
kMmapFd, kMmapFdOffset); |
258 |
size_ = size; |
259 |
} |
260 |
|
261 |
|
262 |
VirtualMemory::~VirtualMemory() { |
263 |
if (IsReserved()) {
|
264 |
if (0 == munmap(address(), size())) address_ = MAP_FAILED; |
265 |
} |
266 |
} |
267 |
|
268 |
|
269 |
bool VirtualMemory::IsReserved() {
|
270 |
return address_ != MAP_FAILED;
|
271 |
} |
272 |
|
273 |
|
274 |
bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) { |
275 |
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
276 |
if (MAP_FAILED == mmap(address, size, prot,
|
277 |
MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
278 |
kMmapFd, kMmapFdOffset)) { |
279 |
return false; |
280 |
} |
281 |
|
282 |
UpdateAllocatedSpaceLimits(address, size); |
283 |
return true; |
284 |
} |
285 |
|
286 |
|
287 |
bool VirtualMemory::Uncommit(void* address, size_t size) { |
288 |
return mmap(address, size, PROT_NONE,
|
289 |
MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, |
290 |
kMmapFd, kMmapFdOffset) != MAP_FAILED; |
291 |
} |
292 |
|
293 |
|
294 |
class ThreadHandle::PlatformData : public Malloced { |
295 |
public:
|
296 |
explicit PlatformData(ThreadHandle::Kind kind) {
|
297 |
Initialize(kind); |
298 |
} |
299 |
|
300 |
void Initialize(ThreadHandle::Kind kind) {
|
301 |
switch (kind) {
|
302 |
case ThreadHandle::SELF: thread_ = pthread_self(); break; |
303 |
case ThreadHandle::INVALID: thread_ = kNoThread; break; |
304 |
} |
305 |
} |
306 |
pthread_t thread_; // Thread handle for pthread.
|
307 |
}; |
308 |
|
309 |
|
310 |
|
311 |
ThreadHandle::ThreadHandle(Kind kind) { |
312 |
data_ = new PlatformData(kind);
|
313 |
} |
314 |
|
315 |
|
316 |
void ThreadHandle::Initialize(ThreadHandle::Kind kind) {
|
317 |
data_->Initialize(kind); |
318 |
} |
319 |
|
320 |
|
321 |
ThreadHandle::~ThreadHandle() { |
322 |
delete data_;
|
323 |
} |
324 |
|
325 |
|
326 |
bool ThreadHandle::IsSelf() const { |
327 |
return pthread_equal(data_->thread_, pthread_self());
|
328 |
} |
329 |
|
330 |
|
331 |
bool ThreadHandle::IsValid() const { |
332 |
return data_->thread_ != kNoThread;
|
333 |
} |
334 |
|
335 |
|
336 |
Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) { |
337 |
} |
338 |
|
339 |
|
340 |
Thread::~Thread() { |
341 |
} |
342 |
|
343 |
|
344 |
static void* ThreadEntry(void* arg) { |
345 |
Thread* thread = reinterpret_cast<Thread*>(arg);
|
346 |
// This is also initialized by the first argument to pthread_create() but we
|
347 |
// don't know which thread will run first (the original thread or the new
|
348 |
// one) so we initialize it here too.
|
349 |
thread->thread_handle_data()->thread_ = pthread_self(); |
350 |
ASSERT(thread->IsValid()); |
351 |
thread->Run(); |
352 |
return NULL; |
353 |
} |
354 |
|
355 |
|
356 |
void Thread::Start() {
|
357 |
pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this); |
358 |
} |
359 |
|
360 |
|
361 |
void Thread::Join() {
|
362 |
pthread_join(thread_handle_data()->thread_, NULL);
|
363 |
} |
364 |
|
365 |
|
366 |
Thread::LocalStorageKey Thread::CreateThreadLocalKey() { |
367 |
pthread_key_t key; |
368 |
int result = pthread_key_create(&key, NULL); |
369 |
USE(result); |
370 |
ASSERT(result == 0);
|
371 |
return static_cast<LocalStorageKey>(key); |
372 |
} |
373 |
|
374 |
|
375 |
void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
|
376 |
pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
|
377 |
int result = pthread_key_delete(pthread_key);
|
378 |
USE(result); |
379 |
ASSERT(result == 0);
|
380 |
} |
381 |
|
382 |
|
383 |
void* Thread::GetThreadLocal(LocalStorageKey key) {
|
384 |
pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
|
385 |
return pthread_getspecific(pthread_key);
|
386 |
} |
387 |
|
388 |
|
389 |
void Thread::SetThreadLocal(LocalStorageKey key, void* value) { |
390 |
pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
|
391 |
pthread_setspecific(pthread_key, value); |
392 |
} |
393 |
|
394 |
|
395 |
void Thread::YieldCPU() {
|
396 |
sched_yield(); |
397 |
} |
398 |
|
399 |
|
400 |
class MacOSMutex : public Mutex { |
401 |
public:
|
402 |
|
403 |
MacOSMutex() { |
404 |
// For some reason the compiler doesn't allow you to write
|
405 |
// "this->mutex_ = PTHREAD_..." directly on mac.
|
406 |
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; |
407 |
pthread_mutexattr_t attr; |
408 |
pthread_mutexattr_init(&attr); |
409 |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
410 |
pthread_mutex_init(&m, &attr); |
411 |
mutex_ = m; |
412 |
} |
413 |
|
414 |
~MacOSMutex() { pthread_mutex_destroy(&mutex_); } |
415 |
|
416 |
int Lock() { return pthread_mutex_lock(&mutex_); } |
417 |
|
418 |
int Unlock() { return pthread_mutex_unlock(&mutex_); } |
419 |
|
420 |
private:
|
421 |
pthread_mutex_t mutex_; |
422 |
}; |
423 |
|
424 |
|
425 |
Mutex* OS::CreateMutex() { |
426 |
return new MacOSMutex(); |
427 |
} |
428 |
|
429 |
|
430 |
class MacOSSemaphore : public Semaphore { |
431 |
public:
|
432 |
explicit MacOSSemaphore(int count) { |
433 |
semaphore_create(mach_task_self(), &semaphore_, SYNC_POLICY_FIFO, count); |
434 |
} |
435 |
|
436 |
~MacOSSemaphore() { |
437 |
semaphore_destroy(mach_task_self(), semaphore_); |
438 |
} |
439 |
|
440 |
// The MacOS mach semaphore documentation claims it does not have spurious
|
441 |
// wakeups, the way pthreads semaphores do. So the code from the linux
|
442 |
// platform is not needed here.
|
443 |
void Wait() { semaphore_wait(semaphore_); }
|
444 |
|
445 |
bool Wait(int timeout); |
446 |
|
447 |
void Signal() { semaphore_signal(semaphore_); }
|
448 |
|
449 |
private:
|
450 |
semaphore_t semaphore_; |
451 |
}; |
452 |
|
453 |
|
454 |
bool MacOSSemaphore::Wait(int timeout) { |
455 |
mach_timespec_t ts; |
456 |
ts.tv_sec = timeout / 1000000;
|
457 |
ts.tv_nsec = (timeout % 1000000) * 1000; |
458 |
return semaphore_timedwait(semaphore_, ts) != KERN_OPERATION_TIMED_OUT;
|
459 |
} |
460 |
|
461 |
|
462 |
Semaphore* OS::CreateSemaphore(int count) {
|
463 |
return new MacOSSemaphore(count); |
464 |
} |
465 |
|
466 |
|
467 |
#ifdef ENABLE_LOGGING_AND_PROFILING
|
468 |
|
469 |
static Sampler* active_sampler_ = NULL; |
470 |
|
471 |
static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
472 |
USE(info); |
473 |
if (signal != SIGPROF) return; |
474 |
if (active_sampler_ == NULL) return; |
475 |
|
476 |
TickSample sample; |
477 |
|
478 |
// If profiling, we extract the current pc and sp.
|
479 |
if (active_sampler_->IsProfiling()) {
|
480 |
// Extracting the sample from the context is extremely machine dependent.
|
481 |
ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
|
482 |
mcontext_t& mcontext = ucontext->uc_mcontext; |
483 |
#if __DARWIN_UNIX03
|
484 |
sample.pc = mcontext->__ss.__eip; |
485 |
sample.sp = mcontext->__ss.__esp; |
486 |
sample.fp = mcontext->__ss.__ebp; |
487 |
#else // !__DARWIN_UNIX03 |
488 |
sample.pc = mcontext->ss.eip; |
489 |
sample.sp = mcontext->ss.esp; |
490 |
sample.fp = mcontext->ss.ebp; |
491 |
#endif // __DARWIN_UNIX03 |
492 |
} |
493 |
|
494 |
// We always sample the VM state.
|
495 |
sample.state = Logger::state(); |
496 |
|
497 |
active_sampler_->Tick(&sample); |
498 |
} |
499 |
|
500 |
|
501 |
class Sampler::PlatformData : public Malloced { |
502 |
public:
|
503 |
PlatformData() { |
504 |
signal_handler_installed_ = false;
|
505 |
} |
506 |
|
507 |
bool signal_handler_installed_;
|
508 |
struct sigaction old_signal_handler_;
|
509 |
struct itimerval old_timer_value_;
|
510 |
}; |
511 |
|
512 |
|
513 |
Sampler::Sampler(int interval, bool profiling) |
514 |
: interval_(interval), profiling_(profiling), active_(false) {
|
515 |
data_ = new PlatformData();
|
516 |
} |
517 |
|
518 |
|
519 |
Sampler::~Sampler() { |
520 |
delete data_;
|
521 |
} |
522 |
|
523 |
|
524 |
void Sampler::Start() {
|
525 |
// There can only be one active sampler at the time on POSIX
|
526 |
// platforms.
|
527 |
if (active_sampler_ != NULL) return; |
528 |
|
529 |
// Request profiling signals.
|
530 |
struct sigaction sa;
|
531 |
sa.sa_sigaction = ProfilerSignalHandler; |
532 |
sigemptyset(&sa.sa_mask); |
533 |
sa.sa_flags = SA_SIGINFO; |
534 |
if (sigaction(SIGPROF, &sa, &data_->old_signal_handler_) != 0) return; |
535 |
data_->signal_handler_installed_ = true;
|
536 |
|
537 |
// Set the itimer to generate a tick for each interval.
|
538 |
itimerval itimer; |
539 |
itimer.it_interval.tv_sec = interval_ / 1000;
|
540 |
itimer.it_interval.tv_usec = (interval_ % 1000) * 1000; |
541 |
itimer.it_value.tv_sec = itimer.it_interval.tv_sec; |
542 |
itimer.it_value.tv_usec = itimer.it_interval.tv_usec; |
543 |
setitimer(ITIMER_PROF, &itimer, &data_->old_timer_value_); |
544 |
|
545 |
// Set this sampler as the active sampler.
|
546 |
active_sampler_ = this;
|
547 |
active_ = true;
|
548 |
} |
549 |
|
550 |
|
551 |
void Sampler::Stop() {
|
552 |
// Restore old signal handler
|
553 |
if (data_->signal_handler_installed_) {
|
554 |
setitimer(ITIMER_PROF, &data_->old_timer_value_, NULL);
|
555 |
sigaction(SIGPROF, &data_->old_signal_handler_, 0);
|
556 |
data_->signal_handler_installed_ = false;
|
557 |
} |
558 |
|
559 |
// This sampler is no longer the active sampler.
|
560 |
active_sampler_ = NULL;
|
561 |
active_ = false;
|
562 |
} |
563 |
|
564 |
#endif // ENABLE_LOGGING_AND_PROFILING |
565 |
|
566 |
} } // namespace v8::internal
|