• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "utils.h"
18 
19 #include <dirent.h>
20 #include <inttypes.h>
21 #include <pthread.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 
27 #include <fstream>
28 #include <memory>
29 #include <string>
30 
31 #include "android-base/file.h"
32 #include "android-base/stringprintf.h"
33 #include "android-base/strings.h"
34 
35 #include "base/mem_map.h"
36 #include "base/stl_util.h"
37 #include "bit_utils.h"
38 #include "os.h"
39 
40 #if defined(__APPLE__)
41 #include <crt_externs.h>
42 // NOLINTNEXTLINE - inclusion of syscall is dependent on arch
43 #include <sys/syscall.h>
44 #include "AvailabilityMacros.h"  // For MAC_OS_X_VERSION_MAX_ALLOWED
45 #endif
46 
47 #if defined(__linux__)
48 #include <linux/unistd.h>
49 // NOLINTNEXTLINE - inclusion of syscall is dependent on arch
50 #include <sys/syscall.h>
51 #endif
52 
53 #if defined(_WIN32)
54 #include <windows.h>
55 // This include needs to be here due to our coding conventions.  Unfortunately
56 // it drags in the definition of the dread ERROR macro.
57 #ifdef ERROR
58 #undef ERROR
59 #endif
60 #endif
61 
62 namespace art {
63 
64 using android::base::ReadFileToString;  // NOLINT - ReadFileToString is actually used
65 using android::base::StringPrintf;
66 
67 #if defined(__arm__)
68 
69 namespace {
70 
71 // Bitmap of caches to flush for cacheflush(2). Must be zero for ARM.
72 static constexpr int kCacheFlushFlags = 0x0;
73 
74 // Number of retry attempts when flushing cache ranges.
75 static constexpr size_t kMaxFlushAttempts = 4;
76 
CacheFlush(uintptr_t start,uintptr_t limit)77 int CacheFlush(uintptr_t start, uintptr_t limit) {
78   // The signature of cacheflush(2) seems to vary by source. On ARM the system call wrapper
79   //    (bionic/SYSCALLS.TXT) has the form: int cacheflush(long start, long end, long flags);
80   int r = cacheflush(start, limit, kCacheFlushFlags);
81   if (r == -1) {
82     CHECK_NE(errno, EINVAL);
83   }
84   return r;
85 }
86 
TouchAndFlushCacheLinesWithinPage(uintptr_t start,uintptr_t limit,size_t attempts,size_t page_size)87 bool TouchAndFlushCacheLinesWithinPage(uintptr_t start, uintptr_t limit, size_t attempts,
88                                        size_t page_size) {
89   CHECK_LT(start, limit);
90   CHECK_EQ(RoundDown(start, page_size), RoundDown(limit - 1, page_size)) << "range spans pages";
91   // Declare a volatile variable so the compiler does not elide reads from the page being touched.
92   [[maybe_unused]] volatile uint8_t v = 0;
93   for (size_t i = 0; i < attempts; ++i) {
94     // Touch page to maximize chance page is resident.
95     v = *reinterpret_cast<uint8_t*>(start);
96 
97     if (LIKELY(CacheFlush(start, limit) == 0)) {
98       return true;
99     }
100   }
101   return false;
102 }
103 
104 }  // namespace
105 
FlushCpuCaches(void * begin,void * end)106 bool FlushCpuCaches(void* begin, void* end) {
107   // This method is specialized for ARM as the generic implementation below uses the
108   // __builtin___clear_cache() intrinsic which is declared as void. On ARMv7 flushing the CPU
109   // caches is a privileged operation. The Linux kernel allows these operations to fail when they
110   // trigger a fault (e.g. page not resident). We use a wrapper for the ARM specific cacheflush()
111   // system call to detect the failure and potential erroneous state of the data and instruction
112   // caches.
113   //
114   // The Android bug for this is b/132205399 and there's a similar discussion on
115   // https://reviews.llvm.org/D37788. This is primarily an issue for the dual view JIT where the
116   // pages where code is executed are only ever RX and never RWX. When attempting to invalidate
117   // instruction cache lines in the RX mapping after writing fresh code in the RW mapping, the
118   // page may not be resident (due to memory pressure), and this means that a fault is raised in
119   // the midst of a cacheflush() call and the instruction cache lines are not invalidated and so
120   // have stale code.
121   //
122   // Other architectures fair better for reasons such as:
123   //
124   // (1) stronger coherence between the data and instruction caches.
125   //
126   // (2) fault handling that allows flushing/invalidation to continue after
127   //     a missing page has been faulted in.
128 
129   const size_t page_size = MemMap::GetPageSize();
130 
131   uintptr_t start = reinterpret_cast<uintptr_t>(begin);
132   const uintptr_t limit = reinterpret_cast<uintptr_t>(end);
133   if (LIKELY(CacheFlush(start, limit) == 0)) {
134     return true;
135   }
136 
137   // A rare failure has occurred implying that part of the range (begin, end] has been swapped
138   // out. Retry flushing but this time grouping cache-line flushes on individual pages and
139   // touching each page before flushing.
140   uintptr_t next_page = RoundUp(start + 1, page_size);
141   while (start < limit) {
142     uintptr_t boundary = std::min(next_page, limit);
143     if (!TouchAndFlushCacheLinesWithinPage(start, boundary, kMaxFlushAttempts, page_size)) {
144       return false;
145     }
146     start = boundary;
147     next_page += page_size;
148   }
149   return true;
150 }
151 
152 #else
153 
FlushCpuCaches(void * begin,void * end)154 bool FlushCpuCaches(void* begin, void* end) {
155   __builtin___clear_cache(reinterpret_cast<char*>(begin), reinterpret_cast<char*>(end));
156   return true;
157 }
158 
159 #endif
160 
161 #if defined(__linux__)
IsKernelVersionAtLeast(int reqd_major,int reqd_minor)162 bool IsKernelVersionAtLeast(int reqd_major, int reqd_minor) {
163   static auto version = []() -> std::pair<int, int> {
164     struct utsname uts;
165     int res, major, minor;
166     res = uname(&uts);
167     CHECK_EQ(res, 0);
168     CHECK_EQ(strcmp(uts.sysname, "Linux"), 0);
169     res = sscanf(uts.release, "%d.%d:", &major, &minor);
170     CHECK_EQ(res, 2);
171     return std::make_pair(major, minor);
172   }();
173   return version >= std::make_pair(reqd_major, reqd_minor);
174 }
175 #endif
176 
CacheOperationsMaySegFault()177 bool CacheOperationsMaySegFault() {
178 #if defined(__linux__) && defined(__aarch64__)
179   // Avoid issue on older ARM64 kernels where data cache operations could be classified as writes
180   // and cause segmentation faults. This was fixed in Linux 3.11rc2:
181   //
182   // https://github.com/torvalds/linux/commit/db6f41063cbdb58b14846e600e6bc3f4e4c2e888
183   //
184   // This behaviour means we should avoid the dual view JIT on the device. This is just
185   // an issue when running tests on devices that have an old kernel.
186   return !IsKernelVersionAtLeast(3, 12);
187 #else
188   return false;
189 #endif
190 }
191 
RunningOnVM()192 bool RunningOnVM() {
193   const char* on_vm = getenv("ART_TEST_ON_VM");
194   return on_vm != nullptr && std::strcmp("true", on_vm) == 0;
195 }
196 
GetTid()197 uint32_t GetTid() {
198 #if defined(__APPLE__)
199   uint64_t owner;
200   CHECK_PTHREAD_CALL(pthread_threadid_np, (nullptr, &owner), __FUNCTION__);  // Requires Mac OS 10.6
201   return owner;
202 #elif defined(__BIONIC__)
203   return gettid();
204 #elif defined(_WIN32)
205   return static_cast<pid_t>(::GetCurrentThreadId());
206 #else
207   return syscall(__NR_gettid);
208 #endif
209 }
210 
GetThreadName(pid_t tid)211 std::string GetThreadName(pid_t tid) {
212   std::string result;
213 #ifdef _WIN32
214   UNUSED(tid);
215   result = "<unknown>";
216 #else
217   // TODO: make this less Linux-specific.
218   if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) {
219     result.resize(result.size() - 1);  // Lose the trailing '\n'.
220   } else {
221     result = "<unknown>";
222   }
223 #endif
224   return result;
225 }
226 
PrettySize(uint64_t byte_count)227 std::string PrettySize(uint64_t byte_count) {
228   // The byte thresholds at which we display amounts.  A byte count is displayed
229   // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1].
230   static const uint64_t kUnitThresholds[] = {
231     0,        // B up to...
232     10*KB,    // KB up to...
233     10*MB,    // MB up to...
234     10ULL*GB  // GB from here.
235   };
236   static const uint64_t kBytesPerUnit[] = { 1, KB, MB, GB };
237   static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" };
238   int i = arraysize(kUnitThresholds);
239   while (--i > 0) {
240     if (byte_count >= kUnitThresholds[i]) {
241       break;
242     }
243   }
244   return StringPrintf("%" PRIu64 "%s",
245                       byte_count / kBytesPerUnit[i], kUnitStrings[i]);
246 }
247 
248 template <typename StrIn, typename Str>
Split(const StrIn & s,char separator,std::vector<Str> * out_result)249 void Split(const StrIn& s, char separator, std::vector<Str>* out_result) {
250   auto split = SplitString(std::string_view(s), separator);
251   for (std::string_view p : split) {
252     if (p.empty()) {
253       continue;
254     }
255     out_result->push_back(Str(p));
256   }
257 }
258 
259 template void Split(const char *const& s, char separator, std::vector<std::string>* out_result);
260 template void Split(const std::string& s, char separator, std::vector<std::string>* out_result);
261 template void Split(const char *const& s, char separator, std::vector<std::string_view>* out_result);
262 template void Split(const std::string_view& s,
263                     char separator,
264                     std::vector<std::string_view>* out_result);
265 template void Split(const std::string_view& s,
266                     char separator,
267                     std::vector<std::string>* out_result);
268 template void Split(const std::string& s,
269                     char separator,
270                     std::vector<std::string_view>* out_result);
271 
272 template <typename Str>
Split(const Str & s,char separator,size_t len,Str * out_result)273 void Split(const Str& s, char separator, size_t len, Str* out_result) {
274   Str* last = out_result + len;
275   auto split = SplitString(std::string_view(s), separator);
276   for (std::string_view p : split) {
277     if (p.empty()) {
278       continue;
279     }
280     if (out_result == last) {
281       return;
282     }
283     *out_result++ = Str(p);
284   }
285 }
286 
287 template void Split(const std::string& s, char separator, size_t len, std::string* out_result);
288 template void Split(const std::string_view& s,
289                     char separator,
290                     size_t len,
291                     std::string_view* out_result);
292 
SetThreadName(pthread_t thr,const char * thread_name)293 void SetThreadName(pthread_t thr, const char* thread_name) {
294   bool hasAt = false;
295   bool hasDot = false;
296   const char* s = thread_name;
297   while (*s) {
298     if (*s == '.') {
299       hasDot = true;
300     } else if (*s == '@') {
301       hasAt = true;
302     }
303     s++;
304   }
305   int len = s - thread_name;
306   if (len < 15 || hasAt || !hasDot) {
307     s = thread_name;
308   } else {
309     s = thread_name + len - 15;
310   }
311 #if defined(__linux__) || defined(_WIN32)
312   // pthread_setname_np fails rather than truncating long strings.
313   char buf[16];       // MAX_TASK_COMM_LEN=16 is hard-coded in the kernel.
314   strncpy(buf, s, sizeof(buf)-1);
315   buf[sizeof(buf)-1] = '\0';
316   errno = pthread_setname_np(thr, buf);
317   if (errno != 0) {
318     PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'";
319   }
320 #else  // __APPLE__
321   if (pthread_equal(thr, pthread_self())) {
322     pthread_setname_np(thread_name);
323   } else {
324     PLOG(WARNING) << "Unable to set the name of another thread to '" << thread_name << "'";
325   }
326 #endif
327 }
328 
SetThreadName(const char * thread_name)329 void SetThreadName(const char* thread_name) { SetThreadName(pthread_self(), thread_name); }
330 
GetTaskStats(pid_t tid,char * state,int * utime,int * stime,int * task_cpu)331 void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) {
332   *utime = *stime = *task_cpu = 0;
333 #ifdef _WIN32
334   // TODO: implement this.
335   UNUSED(tid);
336   *state = 'S';
337 #else
338   std::string stats;
339   // TODO: make this less Linux-specific.
340   if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) {
341     return;
342   }
343   // Skip the command, which may contain spaces.
344   stats = stats.substr(stats.find(')') + 2);
345   // Extract the three fields we care about.
346   std::vector<std::string> fields;
347   Split(stats, ' ', &fields);
348   *state = fields[0][0];
349   *utime = strtoull(fields[11].c_str(), nullptr, 10);
350   *stime = strtoull(fields[12].c_str(), nullptr, 10);
351   *task_cpu = strtoull(fields[36].c_str(), nullptr, 10);
352 #endif
353 }
354 
SleepForever()355 void SleepForever() {
356   while (true) {
357     sleep(100000000);
358   }
359 }
360 
GetProcessStatus(const char * key)361 std::string GetProcessStatus(const char* key) {
362   // Build search pattern of key and separator.
363   std::string pattern(key);
364   pattern.push_back(':');
365 
366   // Search for status lines starting with pattern.
367   std::ifstream fs("/proc/self/status");
368   std::string line;
369   while (std::getline(fs, line)) {
370     if (strncmp(pattern.c_str(), line.c_str(), pattern.size()) == 0) {
371       // Skip whitespace in matching line (if any).
372       size_t pos = line.find_first_not_of(" \t", pattern.size());
373       if (UNLIKELY(pos == std::string::npos)) {
374         break;
375       }
376       return std::string(line, pos);
377     }
378   }
379   return "<unknown>";
380 }
381 
GetOsThreadStat(pid_t tid,char * buf,size_t len)382 size_t GetOsThreadStat(pid_t tid, char* buf, size_t len) {
383 #if defined(__linux__)
384   static constexpr int NAME_BUF_SIZE = 60;
385   char file_name_buf[NAME_BUF_SIZE];
386   // We don't use just /proc/<pid>/stat since, in spite of some documentation to the contrary,
387   // those report utime and stime values for the whole process, not just the thread.
388   snprintf(file_name_buf, NAME_BUF_SIZE, "/proc/%d/task/%d/stat", getpid(), tid);
389   int stat_fd = open(file_name_buf, O_RDONLY | O_CLOEXEC);
390   if (stat_fd >= 0) {
391     ssize_t bytes_read = TEMP_FAILURE_RETRY(read(stat_fd, buf, len));
392     CHECK_GT(bytes_read, 0) << strerror(errno);
393     int ret = close(stat_fd);
394     CHECK_EQ(ret, 0) << strerror(errno);
395     buf[len - 1] = '\0';
396     return bytes_read;
397   }
398 #else
399   UNUSED(tid);
400   UNUSED(buf);
401   UNUSED(len);
402 #endif
403   return 0;
404 }
405 
GetOsThreadStatQuick(pid_t tid)406 std::string GetOsThreadStatQuick(pid_t tid) {
407 #if defined(__linux__)
408   static constexpr int BUF_SIZE = 100;
409   char buf[BUF_SIZE];
410   if (GetOsThreadStat(tid, buf, BUF_SIZE) == 0) {
411     snprintf(buf, BUF_SIZE, "Unknown state: %d", tid);
412   }
413   return buf;
414 #else
415   UNUSED(tid);
416   return "Unknown state";
417 #endif
418 }
419 
GetStateFromStatString(const std::string & stat_output)420 char GetStateFromStatString(const std::string& stat_output) {
421   size_t rparen_pos = stat_output.find(")");
422   if (rparen_pos == std::string::npos || rparen_pos >= stat_output.length() - 3) {
423     return '?';
424   }
425   size_t state_pos = stat_output.find_first_not_of(" ", rparen_pos + 1);
426   if (rparen_pos == std::string::npos) {
427     return '?';
428   }
429   return stat_output[state_pos];
430 }
431 
GetOtherThreadOsStats()432 std::string GetOtherThreadOsStats() {
433 #if defined(__linux__)
434   DIR* dir = opendir("/proc/self/task");
435   if (dir == nullptr) {
436     return std::string("Failed to open /proc/self/task: ") + strerror(errno);
437   }
438   pid_t me = GetTid();
439   struct dirent* de;
440   std::string result;
441   bool found_me = false;
442   errno = 0;
443   while ((de = readdir(dir)) != nullptr) {
444     if (de->d_name[0] == '.') {
445       continue;
446     }
447     pid_t tid = atoi(de->d_name);
448     if (tid == me) {
449       found_me = true;
450     } else {
451       if (!result.empty()) {
452         result += "; ";
453       }
454       result += tid == 0 ? std::string("bad tid: ") + de->d_name : GetOsThreadStatQuick(tid);
455     }
456   }
457   if (errno == EBADF) {
458     result += "(Bad directory)";
459   }
460   if (!found_me) {
461     result += "(Failed to find requestor)";
462   }
463   return result;
464 #else
465   return "Can't get other threads";
466 #endif
467 }
468 
469 }  // namespace art
470