• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Cygwin goes here. For the POSIX-compatible
6 // parts, the implementation is in platform-posix.cc.
7 
8 #include <errno.h>
9 #include <pthread.h>
10 #include <semaphore.h>
11 #include <stdarg.h>
12 #include <strings.h>    // index
13 #include <sys/mman.h>   // mmap & munmap
14 #include <sys/time.h>
15 #include <unistd.h>     // sysconf
16 
17 #include <cmath>
18 
19 #undef MAP_TYPE
20 
21 #include "src/base/macros.h"
22 #include "src/base/platform/platform.h"
23 #include "src/base/win32-headers.h"
24 
25 namespace v8 {
26 namespace base {
27 
28 
LocalTimezone(double time,TimezoneCache * cache)29 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
30   if (std::isnan(time)) return "";
31   time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
32   struct tm* t = localtime(&tv);  // NOLINT(runtime/threadsafe_fn)
33   if (NULL == t) return "";
34   return tzname[0];  // The location of the timezone string on Cygwin.
35 }
36 
37 
LocalTimeOffset(TimezoneCache * cache)38 double OS::LocalTimeOffset(TimezoneCache* cache) {
39   // On Cygwin, struct tm does not contain a tm_gmtoff field.
40   time_t utc = time(NULL);
41   DCHECK(utc != -1);
42   struct tm* loc = localtime(&utc);  // NOLINT(runtime/threadsafe_fn)
43   DCHECK(loc != NULL);
44   // time - localtime includes any daylight savings offset, so subtract it.
45   return static_cast<double>((mktime(loc) - utc) * msPerSecond -
46                              (loc->tm_isdst > 0 ? 3600 * msPerSecond : 0));
47 }
48 
49 
Allocate(const size_t requested,size_t * allocated,bool is_executable)50 void* OS::Allocate(const size_t requested,
51                    size_t* allocated,
52                    bool is_executable) {
53   const size_t msize = RoundUp(requested, sysconf(_SC_PAGESIZE));
54   int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
55   void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
56   if (mbase == MAP_FAILED) return NULL;
57   *allocated = msize;
58   return mbase;
59 }
60 
61 
GetSharedLibraryAddresses()62 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
63   std::vector<SharedLibraryAddresses> result;
64   // This function assumes that the layout of the file is as follows:
65   // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
66   // If we encounter an unexpected situation we abort scanning further entries.
67   FILE* fp = fopen("/proc/self/maps", "r");
68   if (fp == NULL) return result;
69 
70   // Allocate enough room to be able to store a full file name.
71   const int kLibNameLen = FILENAME_MAX + 1;
72   char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
73 
74   // This loop will terminate once the scanning hits an EOF.
75   while (true) {
76     uintptr_t start, end;
77     char attr_r, attr_w, attr_x, attr_p;
78     // Parse the addresses and permission bits at the beginning of the line.
79     if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
80     if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
81 
82     int c;
83     if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
84       // Found a read-only executable entry. Skip characters until we reach
85       // the beginning of the filename or the end of the line.
86       do {
87         c = getc(fp);
88       } while ((c != EOF) && (c != '\n') && (c != '/'));
89       if (c == EOF) break;  // EOF: Was unexpected, just exit.
90 
91       // Process the filename if found.
92       if (c == '/') {
93         ungetc(c, fp);  // Push the '/' back into the stream to be read below.
94 
95         // Read to the end of the line. Exit if the read fails.
96         if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
97 
98         // Drop the newline character read by fgets. We do not need to check
99         // for a zero-length string because we know that we at least read the
100         // '/' character.
101         lib_name[strlen(lib_name) - 1] = '\0';
102       } else {
103         // No library name found, just record the raw address range.
104         snprintf(lib_name, kLibNameLen,
105                  "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
106       }
107       result.push_back(SharedLibraryAddress(lib_name, start, end));
108     } else {
109       // Entry not describing executable data. Skip to end of line to set up
110       // reading the next entry.
111       do {
112         c = getc(fp);
113       } while ((c != EOF) && (c != '\n'));
114       if (c == EOF) break;
115     }
116   }
117   free(lib_name);
118   fclose(fp);
119   return result;
120 }
121 
122 
SignalCodeMovingGC()123 void OS::SignalCodeMovingGC() {
124   // Nothing to do on Cygwin.
125 }
126 
127 
128 // The VirtualMemory implementation is taken from platform-win32.cc.
129 // The mmap-based virtual memory implementation as it is used on most posix
130 // platforms does not work well because Cygwin does not support MAP_FIXED.
131 // This causes VirtualMemory::Commit to not always commit the memory region
132 // specified.
133 
RandomizedVirtualAlloc(size_t size,int action,int protection)134 static void* RandomizedVirtualAlloc(size_t size, int action, int protection) {
135   LPVOID base = NULL;
136 
137   if (protection == PAGE_EXECUTE_READWRITE || protection == PAGE_NOACCESS) {
138     // For exectutable pages try and randomize the allocation address
139     for (size_t attempts = 0; base == NULL && attempts < 3; ++attempts) {
140       base = VirtualAlloc(OS::GetRandomMmapAddr(), size, action, protection);
141     }
142   }
143 
144   // After three attempts give up and let the OS find an address to use.
145   if (base == NULL) base = VirtualAlloc(NULL, size, action, protection);
146 
147   return base;
148 }
149 
150 
VirtualMemory()151 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
152 
153 
VirtualMemory(size_t size)154 VirtualMemory::VirtualMemory(size_t size)
155     : address_(ReserveRegion(size)), size_(size) { }
156 
157 
VirtualMemory(size_t size,size_t alignment)158 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
159     : address_(NULL), size_(0) {
160   DCHECK((alignment % OS::AllocateAlignment()) == 0);
161   size_t request_size = RoundUp(size + alignment,
162                                 static_cast<intptr_t>(OS::AllocateAlignment()));
163   void* address = ReserveRegion(request_size);
164   if (address == NULL) return;
165   uint8_t* base = RoundUp(static_cast<uint8_t*>(address), alignment);
166   // Try reducing the size by freeing and then reallocating a specific area.
167   bool result = ReleaseRegion(address, request_size);
168   USE(result);
169   DCHECK(result);
170   address = VirtualAlloc(base, size, MEM_RESERVE, PAGE_NOACCESS);
171   if (address != NULL) {
172     request_size = size;
173     DCHECK(base == static_cast<uint8_t*>(address));
174   } else {
175     // Resizing failed, just go with a bigger area.
176     address = ReserveRegion(request_size);
177     if (address == NULL) return;
178   }
179   address_ = address;
180   size_ = request_size;
181 }
182 
183 
~VirtualMemory()184 VirtualMemory::~VirtualMemory() {
185   if (IsReserved()) {
186     bool result = ReleaseRegion(address_, size_);
187     DCHECK(result);
188     USE(result);
189   }
190 }
191 
192 
IsReserved()193 bool VirtualMemory::IsReserved() {
194   return address_ != NULL;
195 }
196 
197 
Reset()198 void VirtualMemory::Reset() {
199   address_ = NULL;
200   size_ = 0;
201 }
202 
203 
Commit(void * address,size_t size,bool is_executable)204 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
205   return CommitRegion(address, size, is_executable);
206 }
207 
208 
Uncommit(void * address,size_t size)209 bool VirtualMemory::Uncommit(void* address, size_t size) {
210   DCHECK(IsReserved());
211   return UncommitRegion(address, size);
212 }
213 
214 
ReserveRegion(size_t size)215 void* VirtualMemory::ReserveRegion(size_t size) {
216   return RandomizedVirtualAlloc(size, MEM_RESERVE, PAGE_NOACCESS);
217 }
218 
219 
CommitRegion(void * base,size_t size,bool is_executable)220 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
221   int prot = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
222   if (NULL == VirtualAlloc(base, size, MEM_COMMIT, prot)) {
223     return false;
224   }
225   return true;
226 }
227 
228 
Guard(void * address)229 bool VirtualMemory::Guard(void* address) {
230   if (NULL == VirtualAlloc(address,
231                            OS::CommitPageSize(),
232                            MEM_COMMIT,
233                            PAGE_NOACCESS)) {
234     return false;
235   }
236   return true;
237 }
238 
239 
UncommitRegion(void * base,size_t size)240 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
241   return VirtualFree(base, size, MEM_DECOMMIT) != 0;
242 }
243 
244 
ReleaseRegion(void * base,size_t size)245 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
246   return VirtualFree(base, 0, MEM_RELEASE) != 0;
247 }
248 
249 
HasLazyCommits()250 bool VirtualMemory::HasLazyCommits() {
251   // TODO(alph): implement for the platform.
252   return false;
253 }
254 
255 }  // namespace base
256 }  // namespace v8
257