V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
platform-linux.cc
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 #include <cmath>
31 
32 #undef MAP_TYPE
33 
34 #include "src/base/macros.h"
35 #include "src/base/platform/platform-posix-time.h"
36 #include "src/base/platform/platform-posix.h"
37 #include "src/base/platform/platform.h"
38 
39 namespace v8 {
40 namespace base {
41 
42 #ifdef __arm__
43 
44 bool OS::ArmUsingHardFloat() {
45 // GCC versions 4.6 and above define __ARM_PCS or __ARM_PCS_VFP to specify
46 // the Floating Point ABI used (PCS stands for Procedure Call Standard).
47 // We use these as well as a couple of other defines to statically determine
48 // what FP ABI used.
49 // GCC versions 4.4 and below don't support hard-fp.
50 // GCC versions 4.5 may support hard-fp without defining __ARM_PCS or
51 // __ARM_PCS_VFP.
52 
53 #define GCC_VERSION \
54  (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
55 #if GCC_VERSION >= 40600 && !defined(__clang__)
56 #if defined(__ARM_PCS_VFP)
57  return true;
58 #else
59  return false;
60 #endif
61 
62 #elif GCC_VERSION < 40500 && !defined(__clang__)
63  return false;
64 
65 #else
66 #if defined(__ARM_PCS_VFP)
67  return true;
68 #elif defined(__ARM_PCS) || defined(__SOFTFP__) || defined(__SOFTFP) || \
69  !defined(__VFP_FP__)
70  return false;
71 #else
72 #error \
73  "Your version of compiler does not report the FP ABI compiled for." \
74  "Please report it on this issue" \
75  "http://code.google.com/p/v8/issues/detail?id=2140"
76 
77 #endif
78 #endif
79 #undef GCC_VERSION
80 }
81 
82 #endif // def __arm__
83 
84 TimezoneCache* OS::CreateTimezoneCache() {
85  return new PosixDefaultTimezoneCache();
86 }
87 
88 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
89  std::vector<SharedLibraryAddress> result;
90  // This function assumes that the layout of the file is as follows:
91  // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
92  // If we encounter an unexpected situation we abort scanning further entries.
93  FILE* fp = fopen("/proc/self/maps", "r");
94  if (fp == nullptr) return result;
95 
96  // Allocate enough room to be able to store a full file name.
97  const int kLibNameLen = FILENAME_MAX + 1;
98  char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
99 
100  // This loop will terminate once the scanning hits an EOF.
101  while (true) {
102  uintptr_t start, end, offset;
103  char attr_r, attr_w, attr_x, attr_p;
104  // Parse the addresses and permission bits at the beginning of the line.
105  if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
106  if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
107  if (fscanf(fp, "%" V8PRIxPTR, &offset) != 1) break;
108 
109  // Adjust {start} based on {offset}.
110  start -= offset;
111 
112  int c;
113  if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
114  // Found a read-only executable entry. Skip characters until we reach
115  // the beginning of the filename or the end of the line.
116  do {
117  c = getc(fp);
118  } while ((c != EOF) && (c != '\n') && (c != '/') && (c != '['));
119  if (c == EOF) break; // EOF: Was unexpected, just exit.
120 
121  // Process the filename if found.
122  if ((c == '/') || (c == '[')) {
123  // Push the '/' or '[' back into the stream to be read below.
124  ungetc(c, fp);
125 
126  // Read to the end of the line. Exit if the read fails.
127  if (fgets(lib_name, kLibNameLen, fp) == nullptr) break;
128 
129  // Drop the newline character read by fgets. We do not need to check
130  // for a zero-length string because we know that we at least read the
131  // '/' or '[' character.
132  lib_name[strlen(lib_name) - 1] = '\0';
133  } else {
134  // No library name found, just record the raw address range.
135  snprintf(lib_name, kLibNameLen, "%08" V8PRIxPTR "-%08" V8PRIxPTR, start,
136  end);
137  }
138  result.push_back(SharedLibraryAddress(lib_name, start, end));
139  } else {
140  // Entry not describing executable data. Skip to end of line to set up
141  // reading the next entry.
142  do {
143  c = getc(fp);
144  } while ((c != EOF) && (c != '\n'));
145  if (c == EOF) break;
146  }
147  }
148  free(lib_name);
149  fclose(fp);
150  return result;
151 }
152 
153 void OS::SignalCodeMovingGC() {
154  // Support for ll_prof.py.
155  //
156  // The Linux profiler built into the kernel logs all mmap's with
157  // PROT_EXEC so that analysis tools can properly attribute ticks. We
158  // do a mmap with a name known by ll_prof.py and immediately munmap
159  // it. This injects a GC marker into the stream of events generated
160  // by the kernel and allows us to synchronize V8 code log and the
161  // kernel log.
162  long size = sysconf(_SC_PAGESIZE); // NOLINT(runtime/int)
163  FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+");
164  if (f == nullptr) {
165  OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
166  OS::Abort();
167  }
168  void* addr = mmap(OS::GetRandomMmapAddr(), size, PROT_READ | PROT_EXEC,
169  MAP_PRIVATE, fileno(f), 0);
170  DCHECK_NE(MAP_FAILED, addr);
171  CHECK(Free(addr, size));
172  fclose(f);
173 }
174 
175 } // namespace base
176 } // namespace v8
Definition: libplatform.h:13