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