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 Linux goes here. For the POSIX-compatible
6 // parts, the implementation is in platform-posix.cc.
7
8 #include <pthread.h>
9 #include <semaphore.h>
10 #include <signal.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sys/prctl.h>
14 #include <sys/resource.h>
15 #include <sys/syscall.h>
16 #include <sys/time.h>
17
18 // Ubuntu Dapper requires memory pages to be marked as
19 // executable. Otherwise, OS raises an exception when executing code
20 // in that page.
21 #include <errno.h>
22 #include <fcntl.h> // open
23 #include <stdarg.h>
24 #include <strings.h> // index
25 #include <sys/mman.h> // mmap & munmap
26 #include <sys/stat.h> // open
27 #include <sys/types.h> // mmap & munmap
28 #include <unistd.h> // sysconf
29
30 // GLibc on ARM defines mcontext_t has a typedef for 'struct sigcontext'.
31 // Old versions of the C library <signal.h> didn't define the type.
32 #if defined(__ANDROID__) && !defined(__BIONIC_HAVE_UCONTEXT_T) && \
33 (defined(__arm__) || defined(__aarch64__)) && \
34 !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT)
35 #include <asm/sigcontext.h> // NOLINT
36 #endif
37
38 #if defined(LEAK_SANITIZER)
39 #include <sanitizer/lsan_interface.h>
40 #endif
41
42 #include <cmath>
43
44 #undef MAP_TYPE
45
46 #include "src/base/macros.h"
47 #include "src/base/platform/platform.h"
48
49 namespace v8 {
50 namespace base {
51
52
53 #ifdef __arm__
54
ArmUsingHardFloat()55 bool OS::ArmUsingHardFloat() {
56 // GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
57 // the Floating Point ABI used (PCS stands for Procedure Call Standard).
58 // We use these as well as a couple of other defines to statically determine
59 // what FP ABI used.
60 // GCC versions 4.4 and below don't support hard-fp.
61 // GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
62 // __ARM_PCS_VFP.
63
64 #define GCC_VERSION (__GNUC__ * 10000 \
65 + __GNUC_MINOR__ * 100 \
66 + __GNUC_PATCHLEVEL__)
67 #if GCC_VERSION >= 40600 && !defined(__clang__)
68 #if defined(__ARM_PCS_VFP)
69 return true;
70 #else
71 return false;
72 #endif
73
74 #elif GCC_VERSION < 40500 && !defined(__clang__)
75 return false;
76
77 #else
78 #if defined(__ARM_PCS_VFP)
79 return true;
80 #elif defined(__ARM_PCS) || defined(__SOFTFP__) || defined(__SOFTFP) || \
81 !defined(__VFP_FP__)
82 return false;
83 #else
84 #error "Your version of compiler does not report the FP ABI compiled for." \
85 "Please report it on this issue" \
86 "http://code.google.com/p/v8/issues/detail?id=2140"
87
88 #endif
89 #endif
90 #undef GCC_VERSION
91 }
92
93 #endif // def __arm__
94
95
LocalTimezone(double time,TimezoneCache * cache)96 const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
97 if (std::isnan(time)) return "";
98 time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
99 struct tm tm;
100 struct tm* t = localtime_r(&tv, &tm);
101 if (!t || !t->tm_zone) return "";
102 return t->tm_zone;
103 }
104
105
LocalTimeOffset(TimezoneCache * cache)106 double OS::LocalTimeOffset(TimezoneCache* cache) {
107 time_t tv = time(NULL);
108 struct tm tm;
109 struct tm* t = localtime_r(&tv, &tm);
110 // tm_gmtoff includes any daylight savings offset, so subtract it.
111 return static_cast<double>(t->tm_gmtoff * msPerSecond -
112 (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
113 }
114
115
Allocate(const size_t requested,size_t * allocated,bool is_executable)116 void* OS::Allocate(const size_t requested,
117 size_t* allocated,
118 bool is_executable) {
119 const size_t msize = RoundUp(requested, AllocateAlignment());
120 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
121 void* addr = OS::GetRandomMmapAddr();
122 void* mbase = mmap(addr, msize, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
123 if (mbase == MAP_FAILED) return NULL;
124 *allocated = msize;
125 return mbase;
126 }
127
128
GetSharedLibraryAddresses()129 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
130 std::vector<SharedLibraryAddress> result;
131 // This function assumes that the layout of the file is as follows:
132 // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
133 // If we encounter an unexpected situation we abort scanning further entries.
134 FILE* fp = fopen("/proc/self/maps", "r");
135 if (fp == NULL) return result;
136
137 // Allocate enough room to be able to store a full file name.
138 const int kLibNameLen = FILENAME_MAX + 1;
139 char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
140
141 // This loop will terminate once the scanning hits an EOF.
142 while (true) {
143 uintptr_t start, end;
144 char attr_r, attr_w, attr_x, attr_p;
145 // Parse the addresses and permission bits at the beginning of the line.
146 if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
147 if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
148
149 int c;
150 if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
151 // Found a read-only executable entry. Skip characters until we reach
152 // the beginning of the filename or the end of the line.
153 do {
154 c = getc(fp);
155 } while ((c != EOF) && (c != '\n') && (c != '/') && (c != '['));
156 if (c == EOF) break; // EOF: Was unexpected, just exit.
157
158 // Process the filename if found.
159 if ((c == '/') || (c == '[')) {
160 // Push the '/' or '[' back into the stream to be read below.
161 ungetc(c, fp);
162
163 // Read to the end of the line. Exit if the read fails.
164 if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
165
166 // Drop the newline character read by fgets. We do not need to check
167 // for a zero-length string because we know that we at least read the
168 // '/' or '[' character.
169 lib_name[strlen(lib_name) - 1] = '\0';
170 } else {
171 // No library name found, just record the raw address range.
172 snprintf(lib_name, kLibNameLen,
173 "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
174 }
175 result.push_back(SharedLibraryAddress(lib_name, start, end));
176 } else {
177 // Entry not describing executable data. Skip to end of line to set up
178 // reading the next entry.
179 do {
180 c = getc(fp);
181 } while ((c != EOF) && (c != '\n'));
182 if (c == EOF) break;
183 }
184 }
185 free(lib_name);
186 fclose(fp);
187 return result;
188 }
189
190
SignalCodeMovingGC()191 void OS::SignalCodeMovingGC() {
192 // Support for ll_prof.py.
193 //
194 // The Linux profiler built into the kernel logs all mmap's with
195 // PROT_EXEC so that analysis tools can properly attribute ticks. We
196 // do a mmap with a name known by ll_prof.py and immediately munmap
197 // it. This injects a GC marker into the stream of events generated
198 // by the kernel and allows us to synchronize V8 code log and the
199 // kernel log.
200 long size = sysconf(_SC_PAGESIZE); // NOLINT(runtime/int)
201 FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+");
202 if (f == NULL) {
203 OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
204 OS::Abort();
205 }
206 void* addr = mmap(OS::GetRandomMmapAddr(), size,
207 PROT_READ | PROT_EXEC,
208 MAP_PRIVATE, fileno(f), 0);
209 DCHECK_NE(MAP_FAILED, addr);
210 OS::Free(addr, size);
211 fclose(f);
212 }
213
214
215 // Constants used for mmap.
216 static const int kMmapFd = -1;
217 static const int kMmapFdOffset = 0;
218
219
VirtualMemory()220 VirtualMemory::VirtualMemory() : address_(NULL), size_(0) { }
221
222
VirtualMemory(size_t size)223 VirtualMemory::VirtualMemory(size_t size)
224 : address_(ReserveRegion(size)), size_(size) { }
225
226
VirtualMemory(size_t size,size_t alignment)227 VirtualMemory::VirtualMemory(size_t size, size_t alignment)
228 : address_(NULL), size_(0) {
229 DCHECK((alignment % OS::AllocateAlignment()) == 0);
230 size_t request_size = RoundUp(size + alignment,
231 static_cast<intptr_t>(OS::AllocateAlignment()));
232 void* reservation = mmap(OS::GetRandomMmapAddr(),
233 request_size,
234 PROT_NONE,
235 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
236 kMmapFd,
237 kMmapFdOffset);
238 if (reservation == MAP_FAILED) return;
239
240 uint8_t* base = static_cast<uint8_t*>(reservation);
241 uint8_t* aligned_base = RoundUp(base, alignment);
242 DCHECK_LE(base, aligned_base);
243
244 // Unmap extra memory reserved before and after the desired block.
245 if (aligned_base != base) {
246 size_t prefix_size = static_cast<size_t>(aligned_base - base);
247 OS::Free(base, prefix_size);
248 request_size -= prefix_size;
249 }
250
251 size_t aligned_size = RoundUp(size, OS::AllocateAlignment());
252 DCHECK_LE(aligned_size, request_size);
253
254 if (aligned_size != request_size) {
255 size_t suffix_size = request_size - aligned_size;
256 OS::Free(aligned_base + aligned_size, suffix_size);
257 request_size -= suffix_size;
258 }
259
260 DCHECK(aligned_size == request_size);
261
262 address_ = static_cast<void*>(aligned_base);
263 size_ = aligned_size;
264 #if defined(LEAK_SANITIZER)
265 __lsan_register_root_region(address_, size_);
266 #endif
267 }
268
269
~VirtualMemory()270 VirtualMemory::~VirtualMemory() {
271 if (IsReserved()) {
272 bool result = ReleaseRegion(address(), size());
273 DCHECK(result);
274 USE(result);
275 }
276 }
277
278
IsReserved()279 bool VirtualMemory::IsReserved() {
280 return address_ != NULL;
281 }
282
283
Reset()284 void VirtualMemory::Reset() {
285 address_ = NULL;
286 size_ = 0;
287 }
288
289
Commit(void * address,size_t size,bool is_executable)290 bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
291 CHECK(InVM(address, size));
292 return CommitRegion(address, size, is_executable);
293 }
294
295
Uncommit(void * address,size_t size)296 bool VirtualMemory::Uncommit(void* address, size_t size) {
297 CHECK(InVM(address, size));
298 return UncommitRegion(address, size);
299 }
300
301
Guard(void * address)302 bool VirtualMemory::Guard(void* address) {
303 CHECK(InVM(address, OS::CommitPageSize()));
304 OS::Guard(address, OS::CommitPageSize());
305 return true;
306 }
307
308
ReserveRegion(size_t size)309 void* VirtualMemory::ReserveRegion(size_t size) {
310 void* result = mmap(OS::GetRandomMmapAddr(),
311 size,
312 PROT_NONE,
313 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
314 kMmapFd,
315 kMmapFdOffset);
316
317 if (result == MAP_FAILED) return NULL;
318
319 #if defined(LEAK_SANITIZER)
320 __lsan_register_root_region(result, size);
321 #endif
322 return result;
323 }
324
325
CommitRegion(void * base,size_t size,bool is_executable)326 bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) {
327 int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0);
328 if (MAP_FAILED == mmap(base,
329 size,
330 prot,
331 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
332 kMmapFd,
333 kMmapFdOffset)) {
334 return false;
335 }
336
337 return true;
338 }
339
340
UncommitRegion(void * base,size_t size)341 bool VirtualMemory::UncommitRegion(void* base, size_t size) {
342 return mmap(base,
343 size,
344 PROT_NONE,
345 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED,
346 kMmapFd,
347 kMmapFdOffset) != MAP_FAILED;
348 }
349
ReleasePartialRegion(void * base,size_t size,void * free_start,size_t free_size)350 bool VirtualMemory::ReleasePartialRegion(void* base, size_t size,
351 void* free_start, size_t free_size) {
352 #if defined(LEAK_SANITIZER)
353 __lsan_unregister_root_region(base, size);
354 __lsan_register_root_region(base, size - free_size);
355 #endif
356 return munmap(free_start, free_size) == 0;
357 }
358
ReleaseRegion(void * base,size_t size)359 bool VirtualMemory::ReleaseRegion(void* base, size_t size) {
360 #if defined(LEAK_SANITIZER)
361 __lsan_unregister_root_region(base, size);
362 #endif
363 return munmap(base, size) == 0;
364 }
365
366
HasLazyCommits()367 bool VirtualMemory::HasLazyCommits() {
368 return true;
369 }
370
371 } // namespace base
372 } // namespace v8
373