1 /*
2 * Copyright (C) 2016 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 <fcntl.h>
19 #include <inttypes.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/mman.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include <android-base/unique_fd.h>
28 #include <procinfo/process_map.h>
29
30 #include <algorithm>
31 #include <cctype>
32 #include <memory>
33 #include <string>
34 #include <vector>
35
36 #include <unwindstack/Elf.h>
37 #include <unwindstack/Maps.h>
38 #include <unwindstack/Memory.h>
39
40 namespace unwindstack {
41
Find(uint64_t pc)42 MapInfo* Maps::Find(uint64_t pc) {
43 if (maps_.empty()) {
44 return nullptr;
45 }
46 size_t first = 0;
47 size_t last = maps_.size();
48 while (first < last) {
49 size_t index = (first + last) / 2;
50 const auto& cur = maps_[index];
51 if (pc >= cur->start() && pc < cur->end()) {
52 return cur.get();
53 } else if (pc < cur->start()) {
54 last = index;
55 } else {
56 first = index + 1;
57 }
58 }
59 return nullptr;
60 }
61
Parse()62 bool Maps::Parse() {
63 MapInfo* prev_map = nullptr;
64 MapInfo* prev_real_map = nullptr;
65 return android::procinfo::ReadMapFile(GetMapsFile(),
66 [&](const android::procinfo::MapInfo& mapinfo) {
67 // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
68 auto flags = mapinfo.flags;
69 if (strncmp(mapinfo.name.c_str(), "/dev/", 5) == 0 &&
70 strncmp(mapinfo.name.c_str() + 5, "ashmem/", 7) != 0) {
71 flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
72 }
73 maps_.emplace_back(new MapInfo(prev_map, prev_real_map, mapinfo.start, mapinfo.end,
74 mapinfo.pgoff, flags, mapinfo.name));
75 prev_map = maps_.back().get();
76 if (!prev_map->IsBlank()) {
77 prev_real_map = prev_map;
78 }
79 });
80 }
81
Add(uint64_t start,uint64_t end,uint64_t offset,uint64_t flags,const std::string & name,uint64_t load_bias)82 void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
83 const std::string& name, uint64_t load_bias) {
84 MapInfo* prev_map = maps_.empty() ? nullptr : maps_.back().get();
85 MapInfo* prev_real_map = prev_map;
86 while (prev_real_map != nullptr && prev_real_map->IsBlank()) {
87 prev_real_map = prev_real_map->prev_map();
88 }
89
90 auto map_info =
91 std::make_unique<MapInfo>(prev_map, prev_real_map, start, end, offset, flags, name);
92 map_info->set_load_bias(load_bias);
93 maps_.emplace_back(std::move(map_info));
94 }
95
Sort()96 void Maps::Sort() {
97 std::sort(maps_.begin(), maps_.end(),
98 [](const std::unique_ptr<MapInfo>& a, const std::unique_ptr<MapInfo>& b) {
99 return a->start() < b->start();
100 });
101
102 // Set the prev_map values on the info objects.
103 MapInfo* prev_map = nullptr;
104 MapInfo* prev_real_map = nullptr;
105 for (const auto& map_info : maps_) {
106 map_info->set_prev_map(prev_map);
107 map_info->set_prev_real_map(prev_real_map);
108 prev_map = map_info.get();
109 if (!prev_map->IsBlank()) {
110 prev_real_map = prev_map;
111 }
112 }
113 }
114
Parse()115 bool BufferMaps::Parse() {
116 std::string content(buffer_);
117 MapInfo* prev_map = nullptr;
118 MapInfo* prev_real_map = nullptr;
119 return android::procinfo::ReadMapFileContent(
120 &content[0], [&](const android::procinfo::MapInfo& mapinfo) {
121 // Mark a device map in /dev/ and not in /dev/ashmem/ specially.
122 auto flags = mapinfo.flags;
123 if (strncmp(mapinfo.name.c_str(), "/dev/", 5) == 0 &&
124 strncmp(mapinfo.name.c_str() + 5, "ashmem/", 7) != 0) {
125 flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
126 }
127 maps_.emplace_back(new MapInfo(prev_map, prev_real_map, mapinfo.start, mapinfo.end,
128 mapinfo.pgoff, flags, mapinfo.name));
129 prev_map = maps_.back().get();
130 if (!prev_map->IsBlank()) {
131 prev_real_map = prev_map;
132 }
133 });
134 }
135
GetMapsFile() const136 const std::string RemoteMaps::GetMapsFile() const {
137 return "/proc/" + std::to_string(pid_) + "/maps";
138 }
139
GetMapsFile() const140 const std::string LocalUpdatableMaps::GetMapsFile() const {
141 return "/proc/self/maps";
142 }
143
LocalUpdatableMaps()144 LocalUpdatableMaps::LocalUpdatableMaps() : Maps() {
145 pthread_rwlock_init(&maps_rwlock_, nullptr);
146 }
147
Find(uint64_t pc)148 MapInfo* LocalUpdatableMaps::Find(uint64_t pc) {
149 pthread_rwlock_rdlock(&maps_rwlock_);
150 MapInfo* map_info = Maps::Find(pc);
151 pthread_rwlock_unlock(&maps_rwlock_);
152
153 if (map_info == nullptr) {
154 pthread_rwlock_wrlock(&maps_rwlock_);
155 // This is guaranteed not to invalidate any previous MapInfo objects so
156 // we don't need to worry about any MapInfo* values already in use.
157 if (Reparse()) {
158 map_info = Maps::Find(pc);
159 }
160 pthread_rwlock_unlock(&maps_rwlock_);
161 }
162
163 return map_info;
164 }
165
Parse()166 bool LocalUpdatableMaps::Parse() {
167 pthread_rwlock_wrlock(&maps_rwlock_);
168 bool parsed = Maps::Parse();
169 pthread_rwlock_unlock(&maps_rwlock_);
170 return parsed;
171 }
172
Reparse(bool * any_changed)173 bool LocalUpdatableMaps::Reparse(/*out*/ bool* any_changed) {
174 // New maps will be added at the end without deleting the old ones.
175 size_t last_map_idx = maps_.size();
176 if (!Maps::Parse()) {
177 maps_.resize(last_map_idx);
178 return false;
179 }
180
181 size_t search_map_idx = 0;
182 size_t num_deleted_old_entries = 0;
183 size_t num_deleted_new_entries = 0;
184 for (size_t new_map_idx = last_map_idx; new_map_idx < maps_.size(); new_map_idx++) {
185 auto& new_map_info = maps_[new_map_idx];
186 uint64_t start = new_map_info->start();
187 uint64_t end = new_map_info->end();
188 uint64_t flags = new_map_info->flags();
189 const std::string& name = new_map_info->name();
190 for (size_t old_map_idx = search_map_idx; old_map_idx < last_map_idx; old_map_idx++) {
191 auto& info = maps_[old_map_idx];
192 if (start == info->start() && end == info->end() && flags == info->flags() &&
193 name == info->name()) {
194 // No need to check
195 search_map_idx = old_map_idx + 1;
196 if (new_map_idx + 1 < maps_.size()) {
197 maps_[new_map_idx + 1]->set_prev_map(info.get());
198 maps_[new_map_idx + 1]->set_prev_real_map(info->IsBlank() ? info->prev_real_map()
199 : info.get());
200 }
201 maps_[new_map_idx] = nullptr;
202 num_deleted_new_entries++;
203 break;
204 } else if (info->start() > start) {
205 // Stop, there isn't going to be a match.
206 search_map_idx = old_map_idx;
207 break;
208 }
209
210 // Never delete these maps, they may be in use. The assumption is
211 // that there will only every be a handful of these so waiting
212 // to destroy them is not too expensive.
213 saved_maps_.emplace_back(std::move(info));
214 search_map_idx = old_map_idx + 1;
215 maps_[old_map_idx] = nullptr;
216 num_deleted_old_entries++;
217 }
218 if (search_map_idx >= last_map_idx) {
219 break;
220 }
221 }
222
223 // Now move out any of the maps that never were found.
224 for (size_t i = search_map_idx; i < last_map_idx; i++) {
225 saved_maps_.emplace_back(std::move(maps_[i]));
226 maps_[i] = nullptr;
227 num_deleted_old_entries++;
228 }
229
230 // Sort all of the values such that the nullptrs wind up at the end, then
231 // resize them away.
232 std::sort(maps_.begin(), maps_.end(), [](const auto& a, const auto& b) {
233 if (a == nullptr) {
234 return false;
235 } else if (b == nullptr) {
236 return true;
237 }
238 return a->start() < b->start();
239 });
240 maps_.resize(maps_.size() - num_deleted_old_entries - num_deleted_new_entries);
241
242 if (any_changed != nullptr) {
243 *any_changed = num_deleted_old_entries != 0 || maps_.size() != last_map_idx;
244 }
245
246 return true;
247 }
248
249 } // namespace unwindstack
250