1 /*
2  * Copyright (C) 2013 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 <errno.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include <sys/param.h>
21 #include <sys/ptrace.h>
22 #include <sys/types.h>
23 #include <ucontext.h>
24 #include <unistd.h>
25 
26 #include <backtrace/Backtrace.h>
27 #include <backtrace/BacktraceMap.h>
28 
29 #include "BacktraceLog.h"
30 #include "BacktracePtrace.h"
31 
32 #if !defined(__APPLE__)
PtraceRead(pid_t tid,uint64_t addr,word_t * out_value)33 static bool PtraceRead(pid_t tid, uint64_t addr, word_t* out_value) {
34   // ptrace() returns -1 and sets errno when the operation fails.
35   // To disambiguate -1 from a valid result, we clear errno beforehand.
36   errno = 0;
37   *out_value = ptrace(PTRACE_PEEKTEXT, tid, reinterpret_cast<void*>(addr), nullptr);
38   if (*out_value == static_cast<word_t>(-1) && errno) {
39     return false;
40   }
41   return true;
42 }
43 #endif
44 
ReadWord(uint64_t ptr,word_t * out_value)45 bool BacktracePtrace::ReadWord(uint64_t ptr, word_t* out_value) {
46 #if defined(__APPLE__)
47   BACK_LOGW("MacOS does not support reading from another pid.");
48   return false;
49 #else
50   if (!VerifyReadWordArgs(ptr, out_value)) {
51     return false;
52   }
53 
54   backtrace_map_t map;
55   FillInMap(ptr, &map);
56   if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
57     return false;
58   }
59 
60   return PtraceRead(Tid(), ptr, out_value);
61 #endif
62 }
63 
Read(uint64_t addr,uint8_t * buffer,size_t bytes)64 size_t BacktracePtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
65 #if defined(__APPLE__)
66   BACK_LOGW("MacOS does not support reading from another pid.");
67   return 0;
68 #else
69   backtrace_map_t map;
70   FillInMap(addr, &map);
71   if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
72     return 0;
73   }
74 
75   bytes = MIN(map.end - addr, bytes);
76   size_t bytes_read = 0;
77   word_t data_word;
78   size_t align_bytes = addr & (sizeof(word_t) - 1);
79   if (align_bytes != 0) {
80     if (!PtraceRead(Tid(), addr & ~(sizeof(word_t) - 1), &data_word)) {
81       return 0;
82     }
83     size_t copy_bytes = MIN(sizeof(word_t) - align_bytes, bytes);
84     memcpy(buffer, reinterpret_cast<uint8_t*>(&data_word) + align_bytes, copy_bytes);
85     addr += copy_bytes;
86     buffer += copy_bytes;
87     bytes -= copy_bytes;
88     bytes_read += copy_bytes;
89   }
90 
91   size_t num_words = bytes / sizeof(word_t);
92   for (size_t i = 0; i < num_words; i++) {
93     if (!PtraceRead(Tid(), addr, &data_word)) {
94       return bytes_read;
95     }
96     memcpy(buffer, &data_word, sizeof(word_t));
97     buffer += sizeof(word_t);
98     addr += sizeof(word_t);
99     bytes_read += sizeof(word_t);
100   }
101 
102   size_t left_over = bytes & (sizeof(word_t) - 1);
103   if (left_over) {
104     if (!PtraceRead(Tid(), addr, &data_word)) {
105       return bytes_read;
106     }
107     memcpy(buffer, &data_word, left_over);
108     bytes_read += left_over;
109   }
110   return bytes_read;
111 #endif
112 }
113