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