V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
platform-openbsd.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 OpenBSD and NetBSD goes here. For the
6 // POSIX-compatible parts, the implementation is in platform-posix.cc.
7 
8 #include <pthread.h>
9 #include <semaphore.h>
10 #include <signal.h>
11 #include <stdlib.h>
12 #include <sys/resource.h>
13 #include <sys/syscall.h>
14 #include <sys/time.h>
15 #include <sys/types.h>
16 
17 #include <errno.h>
18 #include <fcntl.h> // open
19 #include <stdarg.h>
20 #include <strings.h> // index
21 #include <sys/mman.h> // mmap & munmap
22 #include <sys/stat.h> // open
23 #include <unistd.h> // sysconf
24 
25 #include <cmath>
26 
27 #undef MAP_TYPE
28 
29 #include "src/base/macros.h"
30 #include "src/base/platform/platform-posix-time.h"
31 #include "src/base/platform/platform-posix.h"
32 #include "src/base/platform/platform.h"
33 
34 namespace v8 {
35 namespace base {
36 
37 TimezoneCache* OS::CreateTimezoneCache() {
38  return new PosixDefaultTimezoneCache();
39 }
40 
41 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
42  std::vector<SharedLibraryAddress> result;
43  // This function assumes that the layout of the file is as follows:
44  // hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
45  // If we encounter an unexpected situation we abort scanning further entries.
46  FILE* fp = fopen("/proc/self/maps", "r");
47  if (fp == nullptr) return result;
48 
49  // Allocate enough room to be able to store a full file name.
50  const int kLibNameLen = FILENAME_MAX + 1;
51  char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
52 
53  // This loop will terminate once the scanning hits an EOF.
54  while (true) {
55  uintptr_t start, end;
56  char attr_r, attr_w, attr_x, attr_p;
57  // Parse the addresses and permission bits at the beginning of the line.
58  if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
59  if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
60 
61  int c;
62  if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
63  // Found a read-only executable entry. Skip characters until we reach
64  // the beginning of the filename or the end of the line.
65  do {
66  c = getc(fp);
67  } while ((c != EOF) && (c != '\n') && (c != '/'));
68  if (c == EOF) break; // EOF: Was unexpected, just exit.
69 
70  // Process the filename if found.
71  if (c == '/') {
72  ungetc(c, fp); // Push the '/' back into the stream to be read below.
73 
74  // Read to the end of the line. Exit if the read fails.
75  if (fgets(lib_name, kLibNameLen, fp) == nullptr) break;
76 
77  // Drop the newline character read by fgets. We do not need to check
78  // for a zero-length string because we know that we at least read the
79  // '/' character.
80  lib_name[strlen(lib_name) - 1] = '\0';
81  } else {
82  // No library name found, just record the raw address range.
83  snprintf(lib_name, kLibNameLen,
84  "%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
85  }
86  result.push_back(SharedLibraryAddress(lib_name, start, end));
87  } else {
88  // Entry not describing executable data. Skip to end of line to set up
89  // reading the next entry.
90  do {
91  c = getc(fp);
92  } while ((c != EOF) && (c != '\n'));
93  if (c == EOF) break;
94  }
95  }
96  free(lib_name);
97  fclose(fp);
98  return result;
99 }
100 
101 void OS::SignalCodeMovingGC() {
102  // Support for ll_prof.py.
103  //
104  // The Linux profiler built into the kernel logs all mmap's with
105  // PROT_EXEC so that analysis tools can properly attribute ticks. We
106  // do a mmap with a name known by ll_prof.py and immediately munmap
107  // it. This injects a GC marker into the stream of events generated
108  // by the kernel and allows us to synchronize V8 code log and the
109  // kernel log.
110  int size = sysconf(_SC_PAGESIZE);
111  FILE* f = fopen(OS::GetGCFakeMMapFile(), "w+");
112  if (f == nullptr) {
113  OS::PrintError("Failed to open %s\n", OS::GetGCFakeMMapFile());
114  OS::Abort();
115  }
116  void* addr =
117  mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fileno(f), 0);
118  DCHECK(addr != MAP_FAILED);
119  OS::Free(addr, size);
120  fclose(f);
121 }
122 
123 } // namespace base
124 } // namespace v8
Definition: libplatform.h:13