2012-01-20 16:17:08 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2011-02-17 16:33:10 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
// Platform specific code for Cygwin goes here. For the POSIX comaptible parts
|
|
|
|
// the implementation is in platform-posix.cc.
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <strings.h> // index
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/mman.h> // mmap & munmap
|
|
|
|
#include <unistd.h> // sysconf
|
|
|
|
|
|
|
|
#undef MAP_TYPE
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
2012-03-30 14:30:46 +00:00
|
|
|
#include "platform-posix.h"
|
2011-02-17 16:33:10 +00:00
|
|
|
#include "platform.h"
|
2013-04-11 14:22:04 +00:00
|
|
|
#include "simulator.h"
|
2011-02-17 16:33:10 +00:00
|
|
|
#include "v8threads.h"
|
|
|
|
#include "vm-state-inl.h"
|
|
|
|
#include "win32-headers.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
|
|
const char* OS::LocalTimezone(double time) {
|
2013-04-19 13:26:47 +00:00
|
|
|
if (std::isnan(time)) return "";
|
2011-02-17 16:33:10 +00:00
|
|
|
time_t tv = static_cast<time_t>(floor(time/msPerSecond));
|
|
|
|
struct tm* t = localtime(&tv);
|
|
|
|
if (NULL == t) return "";
|
|
|
|
return tzname[0]; // The location of the timezone string on Cygwin.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double OS::LocalTimeOffset() {
|
|
|
|
// On Cygwin, struct tm does not contain a tm_gmtoff field.
|
|
|
|
time_t utc = time(NULL);
|
|
|
|
ASSERT(utc != -1);
|
|
|
|
struct tm* loc = localtime(&utc);
|
|
|
|
ASSERT(loc != NULL);
|
|
|
|
// time - localtime includes any daylight savings offset, so subtract it.
|
|
|
|
return static_cast<double>((mktime(loc) - utc) * msPerSecond -
|
|
|
|
(loc->tm_isdst > 0 ? 3600 * msPerSecond : 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* OS::Allocate(const size_t requested,
|
|
|
|
size_t* allocated,
|
|
|
|
bool is_executable) {
|
|
|
|
const size_t msize = RoundUp(requested, sysconf(_SC_PAGESIZE));
|
|
|
|
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
|
|
|
|
void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
if (mbase == MAP_FAILED) {
|
2011-03-18 20:35:07 +00:00
|
|
|
LOG(ISOLATE, StringEvent("OS::Allocate", "mmap failed"));
|
2011-02-17 16:33:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*allocated = msize;
|
|
|
|
return mbase;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-16 12:36:44 +00:00
|
|
|
void OS::DumpBacktrace() {
|
|
|
|
// Currently unsupported.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-17 16:33:10 +00:00
|
|
|
class PosixMemoryMappedFile : public OS::MemoryMappedFile {
|
|
|
|
public:
|
|
|
|
PosixMemoryMappedFile(FILE* file, void* memory, int size)
|
|
|
|
: file_(file), memory_(memory), size_(size) { }
|
|
|
|
virtual ~PosixMemoryMappedFile();
|
|
|
|
virtual void* memory() { return memory_; }
|
|
|
|
virtual int size() { return size_; }
|
|
|
|
private:
|
|
|
|
FILE* file_;
|
|
|
|
void* memory_;
|
|
|
|
int size_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
|
2011-02-21 17:17:26 +00:00
|
|
|
FILE* file = fopen(name, "r+");
|
2011-02-17 16:33:10 +00:00
|
|
|
if (file == NULL) return NULL;
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
int size = ftell(file);
|
|
|
|
|
|
|
|
void* memory =
|
|
|
|
mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
|
|
|
|
return new PosixMemoryMappedFile(file, memory, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
|
|
|
|
void* initial) {
|
|
|
|
FILE* file = fopen(name, "w+");
|
|
|
|
if (file == NULL) return NULL;
|
|
|
|
int result = fwrite(initial, size, 1, file);
|
|
|
|
if (result < 1) {
|
|
|
|
fclose(file);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
void* memory =
|
|
|
|
mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
|
|
|
|
return new PosixMemoryMappedFile(file, memory, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PosixMemoryMappedFile::~PosixMemoryMappedFile() {
|
|
|
|
if (memory_) munmap(memory_, size_);
|
|
|
|
fclose(file_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::LogSharedLibraryAddresses() {
|
|
|
|
// This function assumes that the layout of the file is as follows:
|
|
|
|
// hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
|
|
|
|
// If we encounter an unexpected situation we abort scanning further entries.
|
|
|
|
FILE* fp = fopen("/proc/self/maps", "r");
|
|
|
|
if (fp == NULL) return;
|
|
|
|
|
|
|
|
// Allocate enough room to be able to store a full file name.
|
|
|
|
const int kLibNameLen = FILENAME_MAX + 1;
|
|
|
|
char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
|
|
|
|
|
2011-03-30 17:16:36 +00:00
|
|
|
i::Isolate* isolate = ISOLATE;
|
2011-02-17 16:33:10 +00:00
|
|
|
// This loop will terminate once the scanning hits an EOF.
|
|
|
|
while (true) {
|
|
|
|
uintptr_t start, end;
|
|
|
|
char attr_r, attr_w, attr_x, attr_p;
|
|
|
|
// Parse the addresses and permission bits at the beginning of the line.
|
|
|
|
if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
|
|
|
|
if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
|
|
|
|
|
|
|
|
int c;
|
|
|
|
if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
|
|
|
|
// Found a read-only executable entry. Skip characters until we reach
|
|
|
|
// the beginning of the filename or the end of the line.
|
|
|
|
do {
|
|
|
|
c = getc(fp);
|
|
|
|
} while ((c != EOF) && (c != '\n') && (c != '/'));
|
|
|
|
if (c == EOF) break; // EOF: Was unexpected, just exit.
|
|
|
|
|
|
|
|
// Process the filename if found.
|
|
|
|
if (c == '/') {
|
|
|
|
ungetc(c, fp); // Push the '/' back into the stream to be read below.
|
|
|
|
|
|
|
|
// Read to the end of the line. Exit if the read fails.
|
|
|
|
if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
|
|
|
|
|
|
|
|
// Drop the newline character read by fgets. We do not need to check
|
|
|
|
// for a zero-length string because we know that we at least read the
|
|
|
|
// '/' character.
|
|
|
|
lib_name[strlen(lib_name) - 1] = '\0';
|
|
|
|
} else {
|
|
|
|
// No library name found, just record the raw address range.
|
|
|
|
snprintf(lib_name, kLibNameLen,
|
|
|
|
"%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
|
|
|
|
}
|
2011-03-30 17:16:36 +00:00
|
|
|
LOG(isolate, SharedLibraryEvent(lib_name, start, end));
|
2011-02-17 16:33:10 +00:00
|
|
|
} else {
|
2012-01-13 13:09:52 +00:00
|
|
|
// Entry not describing executable data. Skip to end of line to set up
|
2011-02-17 16:33:10 +00:00
|
|
|
// reading the next entry.
|
|
|
|
do {
|
|
|
|
c = getc(fp);
|
|
|
|
} while ((c != EOF) && (c != '\n'));
|
|
|
|
if (c == EOF) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(lib_name);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OS::SignalCodeMovingGC() {
|
|
|
|
// Nothing to do on Cygwin.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int OS::StackWalk(Vector<OS::StackFrame> frames) {
|
|
|
|
// Not supported on Cygwin.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-30 17:16:36 +00:00
|
|
|
// The VirtualMemory implementation is taken from platform-win32.cc.
|
|
|
|
// The mmap-based virtual memory implementation as it is used on most posix
|
|
|
|
// platforms does not work well because Cygwin does not support MAP_FIXED.
|
|
|
|
// This causes VirtualMemory::Commit to not always commit the memory region
|
|
|
|
// specified.
|
|
|
|
|
2013-04-16 12:36:44 +00:00
|
|
|
static void* GetRandomAddr() {
|
|
|
|
Isolate* isolate = Isolate::UncheckedCurrent();
|
|
|
|
// Note that the current isolate isn't set up in a call path via
|
|
|
|
// CpuFeatures::Probe. We don't care about randomization in this case because
|
|
|
|
// the code page is immediately freed.
|
|
|
|
if (isolate != NULL) {
|
|
|
|
// The address range used to randomize RWX allocations in OS::Allocate
|
|
|
|
// Try not to map pages into the default range that windows loads DLLs
|
|
|
|
// Use a multiple of 64k to prevent committing unused memory.
|
|
|
|
// Note: This does not guarantee RWX regions will be within the
|
|
|
|
// range kAllocationRandomAddressMin to kAllocationRandomAddressMax
|
|
|
|
#ifdef V8_HOST_ARCH_64_BIT
|
|
|
|
static const intptr_t kAllocationRandomAddressMin = 0x0000000080000000;
|
|
|
|
static const intptr_t kAllocationRandomAddressMax = 0x000003FFFFFF0000;
|
|
|
|
#else
|
|
|
|
static const intptr_t kAllocationRandomAddressMin = 0x04000000;
|
|
|
|
static const intptr_t kAllocationRandomAddressMax = 0x3FFF0000;
|
|
|
|
#endif
|
|
|
|
uintptr_t address = (V8::RandomPrivate(isolate) << kPageSizeBits)
|
|
|
|
| kAllocationRandomAddressMin;
|
|
|
|
address &= kAllocationRandomAddressMax;
|
|
|
|
return reinterpret_cast<void *>(address);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void* RandomizedVirtualAlloc(size_t size, int action, int protection) {
|
|
|
|
LPVOID base = NULL;
|
|
|
|
|
|
|
|
if (protection == PAGE_EXECUTE_READWRITE || protection == PAGE_NOACCESS) {
|
|
|
|
// For exectutable pages try and randomize the allocation address
|
|
|
|
for (size_t attempts = 0; base == NULL && attempts < 3; ++attempts) {
|
|
|
|
base = VirtualAlloc(GetRandomAddr(), size, action, protection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// After three attempts give up and let the OS find an address to use.
|
|
|
|
if (base == NULL) base = VirtualAlloc(NULL, size, action, protection);
|
|
|
|
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-12 11:10:19 +00:00
|
|
|
VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
|
2011-02-17 16:33:10 +00:00
|
|
|
|
|
|
|
|
2013-04-12 11:10:19 +00:00
|
|
|
VirtualMemory::VirtualMemory(size_t size)
|
|
|
|
: address_(ReserveRegion(size)), size_(size) { }
|
2011-02-17 16:33:10 +00:00
|
|
|
|
|
|
|
|
2013-04-16 12:36:44 +00:00
|
|
|
VirtualMemory::VirtualMemory(size_t size, size_t alignment)
|
|
|
|
: address_(NULL), size_(0) {
|
|
|
|
ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment())));
|
|
|
|
size_t request_size = RoundUp(size + alignment,
|
|
|
|
static_cast<intptr_t>(OS::AllocateAlignment()));
|
|
|
|
void* address = ReserveRegion(request_size);
|
|
|
|
if (address == NULL) return;
|
|
|
|
Address base = RoundUp(static_cast<Address>(address), alignment);
|
|
|
|
// Try reducing the size by freeing and then reallocating a specific area.
|
|
|
|
bool result = ReleaseRegion(address, request_size);
|
|
|
|
USE(result);
|
|
|
|
ASSERT(result);
|
|
|
|
address = VirtualAlloc(base, size, MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
if (address != NULL) {
|
|
|
|
request_size = size;
|
|
|
|
ASSERT(base == static_cast<Address>(address));
|
|
|
|
} else {
|
|
|
|
// Resizing failed, just go with a bigger area.
|
|
|
|
address = ReserveRegion(request_size);
|
|
|
|
if (address == NULL) return;
|
|
|
|
}
|
|
|
|
address_ = address;
|
|
|
|
size_ = request_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-17 16:33:10 +00:00
|
|
|
VirtualMemory::~VirtualMemory() {
|
|
|
|
if (IsReserved()) {
|
2013-04-16 12:36:44 +00:00
|
|
|
bool result = ReleaseRegion(address_, size_);
|
|
|
|
ASSERT(result);
|
|
|
|
USE(result);
|
2011-02-17 16:33:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-12 11:10:19 +00:00
|
|
|
bool VirtualMemory::IsReserved() {
|
|
|
|
return address_ != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VirtualMemory::Reset() {
|
|
|
|
address_ = NULL;
|
|
|
|
size_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-17 16:33:10 +00:00
|
|
|
bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
|
2013-04-16 12:36:44 +00:00
|
|
|
return CommitRegion(address, size, is_executable);
|
2011-02-17 16:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool VirtualMemory::Uncommit(void* address, size_t size) {
|
2011-03-30 17:16:36 +00:00
|
|
|
ASSERT(IsReserved());
|
2013-04-16 12:36:44 +00:00
|
|
|
return UncommitRegion(address, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* VirtualMemory::ReserveRegion(size_t size) {
|
|
|
|
return RandomizedVirtualAlloc(size, MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
|
|
|
|
int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
|
|
|
|
if (NULL == VirtualAlloc(base, size, MEM_COMMIT, prot)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2011-02-17 16:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-23 12:11:24 +00:00
|
|
|
bool VirtualMemory::Guard(void* address) {
|
|
|
|
if (NULL == VirtualAlloc(address,
|
|
|
|
OS::CommitPageSize(),
|
|
|
|
MEM_COMMIT,
|
|
|
|
PAGE_READONLY | PAGE_GUARD)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-12 11:10:19 +00:00
|
|
|
bool VirtualMemory::UncommitRegion(void* base, size_t size) {
|
2013-04-16 12:36:44 +00:00
|
|
|
return VirtualFree(base, size, MEM_DECOMMIT) != 0;
|
2013-04-12 11:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
|
2013-04-16 12:36:44 +00:00
|
|
|
return VirtualFree(base, 0, MEM_RELEASE) != 0;
|
2013-04-12 11:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-22 16:33:10 +00:00
|
|
|
bool VirtualMemory::HasLazyCommits() {
|
|
|
|
// TODO(alph): implement for the platform.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-05 14:10:39 +00:00
|
|
|
void OS::SetUp() {
|
|
|
|
// Seed the random number generator.
|
|
|
|
// Convert the current time to a 64-bit integer first, before converting it
|
|
|
|
// to an unsigned. Going directly can cause an overflow and the seed to be
|
|
|
|
// set to all ones. The seed will be identical for different instances that
|
|
|
|
// call this setup code within the same millisecond.
|
|
|
|
uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis());
|
|
|
|
srandom(static_cast<unsigned int>(seed));
|
2012-04-13 14:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-17 16:33:10 +00:00
|
|
|
} } // namespace v8::internal
|