Properly handle empty map after read-only map.
Recently, the maps for an elf in memory might show up looking like:
f0000-f1000 0 r-- /system/lib/libc.so
f1000-f2000 0 ---
f2000-f3000 1000 r-x /system/lib/libc.so
f3000-f4000 2000 rw- /system/lib/libc.so
The problem is that there is logic in the code that assumed that the
map before the execute map must be the read-only map. In the case
above, this is not true. Add a new prev_real_map that will point
to the previous map that is not one of these empty maps.
This will fix the backtraces that look like this:
#00 pc 0000000000050d58 /apex/com.android.runtime/lib64/bionic/libc.so!libc.so (offset 0x50000) (syscall+24) (BuildId: 5252408bf30e395d49ee270b54c77ca4)
To get rid of the !libc.so and the offset value, which is not correct.
Added new unit tests to verify this.
Added new offline test which an empty map between read-only and execute
map. Before this change, the backtraces had lines like
libc.so!libc.so (offset XXX) would be present.
Bug: 148075852
Test: Ran unit tests.
Change-Id: Ie04bfc96b8f91ed885cb1e655cf1e346efe48a45
diff --git a/libunwindstack/Maps.cpp b/libunwindstack/Maps.cpp
index 0ab68db..8f49ad9 100644
--- a/libunwindstack/Maps.cpp
+++ b/libunwindstack/Maps.cpp
@@ -60,6 +60,8 @@
}
bool Maps::Parse() {
+ MapInfo* prev_map = nullptr;
+ MapInfo* prev_real_map = nullptr;
return android::procinfo::ReadMapFile(
GetMapsFile(),
[&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t, const char* name) {
@@ -67,17 +69,24 @@
if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
}
- maps_.emplace_back(
- new MapInfo(maps_.empty() ? nullptr : maps_.back().get(), start, end, pgoff,
- flags, name));
+ maps_.emplace_back(new MapInfo(prev_map, prev_real_map, start, end, pgoff, flags, name));
+ prev_map = maps_.back().get();
+ if (!prev_map->IsBlank()) {
+ prev_real_map = prev_map;
+ }
});
}
void Maps::Add(uint64_t start, uint64_t end, uint64_t offset, uint64_t flags,
const std::string& name, uint64_t load_bias) {
+ MapInfo* prev_map = maps_.empty() ? nullptr : maps_.back().get();
+ MapInfo* prev_real_map = prev_map;
+ while (prev_real_map != nullptr && prev_real_map->IsBlank()) {
+ prev_real_map = prev_real_map->prev_map;
+ }
+
auto map_info =
- std::make_unique<MapInfo>(maps_.empty() ? nullptr : maps_.back().get(), start, end, offset,
- flags, name);
+ std::make_unique<MapInfo>(prev_map, prev_real_map, start, end, offset, flags, name);
map_info->load_bias = load_bias;
maps_.emplace_back(std::move(map_info));
}
@@ -89,14 +98,21 @@
// Set the prev_map values on the info objects.
MapInfo* prev_map = nullptr;
+ MapInfo* prev_real_map = nullptr;
for (const auto& map_info : maps_) {
map_info->prev_map = prev_map;
+ map_info->prev_real_map = prev_real_map;
prev_map = map_info.get();
+ if (!prev_map->IsBlank()) {
+ prev_real_map = prev_map;
+ }
}
}
bool BufferMaps::Parse() {
std::string content(buffer_);
+ MapInfo* prev_map = nullptr;
+ MapInfo* prev_real_map = nullptr;
return android::procinfo::ReadMapFileContent(
&content[0],
[&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t, const char* name) {
@@ -104,9 +120,11 @@
if (strncmp(name, "/dev/", 5) == 0 && strncmp(name + 5, "ashmem/", 7) != 0) {
flags |= unwindstack::MAPS_FLAGS_DEVICE_MAP;
}
- maps_.emplace_back(
- new MapInfo(maps_.empty() ? nullptr : maps_.back().get(), start, end, pgoff,
- flags, name));
+ maps_.emplace_back(new MapInfo(prev_map, prev_real_map, start, end, pgoff, flags, name));
+ prev_map = maps_.back().get();
+ if (!prev_map->IsBlank()) {
+ prev_real_map = prev_map;
+ }
});
}