1 // Copyright 2014 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 AIX goes here. For the POSIX comaptible parts
6 // 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/resource.h>
14 #include <sys/time.h>
15 #include <sys/ucontext.h>
16 
17 #include <errno.h>
18 #include <fcntl.h>  // open
19 #include <limits.h>
20 #include <stdarg.h>
21 #include <strings.h>    // index
22 #include <sys/mman.h>   // mmap & munmap
23 #include <sys/stat.h>   // open
24 #include <sys/types.h>  // mmap & munmap
25 #include <unistd.h>     // getpagesize
26 
27 #include <cmath>
28 
29 #undef MAP_TYPE
30 
31 #include "src/base/macros.h"
32 #include "src/base/platform/platform-posix.h"
33 #include "src/base/platform/platform.h"
34 
35 namespace v8 {
36 namespace base {
37 
38 
39 class AIXTimezoneCache : public PosixTimezoneCache {
40   const char* LocalTimezone(double time) override;
41 
42   double LocalTimeOffset(double time_ms, bool is_utc) override;
43 
~AIXTimezoneCache()44   ~AIXTimezoneCache() override {}
45 };
46 
LocalTimezone(double time_ms)47 const char* AIXTimezoneCache::LocalTimezone(double time_ms) {
48   if (std::isnan(time_ms)) return "";
49   time_t tv = static_cast<time_t>(floor(time_ms / msPerSecond));
50   struct tm tm;
51   struct tm* t = localtime_r(&tv, &tm);
52   if (nullptr == t) return "";
53   return tzname[0];  // The location of the timezone string on AIX.
54 }
55 
LocalTimeOffset(double time_ms,bool is_utc)56 double AIXTimezoneCache::LocalTimeOffset(double time_ms, bool is_utc) {
57   // On AIX, struct tm does not contain a tm_gmtoff field.
58   time_t utc = time(nullptr);
59   DCHECK_NE(utc, -1);
60   struct tm tm;
61   struct tm* loc = localtime_r(&utc, &tm);
62   DCHECK_NOT_NULL(loc);
63   return static_cast<double>((mktime(loc) - utc) * msPerSecond);
64 }
65 
CreateTimezoneCache()66 TimezoneCache* OS::CreateTimezoneCache() { return new AIXTimezoneCache(); }
67 
StringToLong(char * buffer)68 static unsigned StringToLong(char* buffer) {
69   return static_cast<unsigned>(strtol(buffer, nullptr, 16));  // NOLINT
70 }
71 
GetSharedLibraryAddresses()72 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
73   std::vector<SharedLibraryAddress> result;
74   static const int MAP_LENGTH = 1024;
75   int fd = open("/proc/self/maps", O_RDONLY);
76   if (fd < 0) return result;
77   while (true) {
78     char addr_buffer[11];
79     addr_buffer[0] = '0';
80     addr_buffer[1] = 'x';
81     addr_buffer[10] = 0;
82     ssize_t rc = read(fd, addr_buffer + 2, 8);
83     if (rc < 8) break;
84     unsigned start = StringToLong(addr_buffer);
85     rc = read(fd, addr_buffer + 2, 1);
86     if (rc < 1) break;
87     if (addr_buffer[2] != '-') break;
88     rc = read(fd, addr_buffer + 2, 8);
89     if (rc < 8) break;
90     unsigned end = StringToLong(addr_buffer);
91     char buffer[MAP_LENGTH];
92     int bytes_read = -1;
93     do {
94       bytes_read++;
95       if (bytes_read >= MAP_LENGTH - 1) break;
96       rc = read(fd, buffer + bytes_read, 1);
97       if (rc < 1) break;
98     } while (buffer[bytes_read] != '\n');
99     buffer[bytes_read] = 0;
100     // Ignore mappings that are not executable.
101     if (buffer[3] != 'x') continue;
102     char* start_of_path = index(buffer, '/');
103     // There may be no filename in this line.  Skip to next.
104     if (start_of_path == nullptr) continue;
105     buffer[bytes_read] = 0;
106     result.push_back(SharedLibraryAddress(start_of_path, start, end));
107   }
108   close(fd);
109   return result;
110 }
111 
SignalCodeMovingGC()112 void OS::SignalCodeMovingGC() {}
113 
114 }  // namespace base
115 }  // namespace v8
116