Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2016, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "read_apk.h" |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 27 | #include <memory> |
| 28 | |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 29 | #include <android-base/file.h> |
| 30 | #include <android-base/logging.h> |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 31 | #include <android-base/strings.h> |
Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 32 | #include <ziparchive/zip_archive.h> |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 33 | #include "read_elf.h" |
| 34 | #include "utils.h" |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 35 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 36 | namespace simpleperf { |
| 37 | |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 38 | std::unordered_map<std::string, ApkInspector::ApkNode> ApkInspector::embedded_elf_cache_; |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 39 | |
Yabin Cui | 41e32ca | 2016-02-18 12:11:40 -0800 | [diff] [blame] | 40 | EmbeddedElf* ApkInspector::FindElfInApkByOffset(const std::string& apk_path, uint64_t file_offset) { |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 41 | // Already in cache? |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 42 | ApkNode& node = embedded_elf_cache_[apk_path]; |
| 43 | auto it = node.offset_map.find(file_offset); |
| 44 | if (it != node.offset_map.end()) { |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 45 | return it->second.get(); |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 46 | } |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 47 | std::unique_ptr<EmbeddedElf> elf = FindElfInApkByOffsetWithoutCache(apk_path, file_offset); |
| 48 | EmbeddedElf* result = elf.get(); |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 49 | node.offset_map[file_offset] = std::move(elf); |
| 50 | if (result != nullptr) { |
| 51 | node.name_map[result->entry_name()] = result; |
| 52 | } |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 53 | return result; |
| 54 | } |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 55 | |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 56 | EmbeddedElf* ApkInspector::FindElfInApkByName(const std::string& apk_path, |
| 57 | const std::string& entry_name) { |
| 58 | ApkNode& node = embedded_elf_cache_[apk_path]; |
| 59 | auto it = node.name_map.find(entry_name); |
| 60 | if (it != node.name_map.end()) { |
| 61 | return it->second; |
| 62 | } |
| 63 | std::unique_ptr<EmbeddedElf> elf = FindElfInApkByNameWithoutCache(apk_path, entry_name); |
| 64 | EmbeddedElf* result = elf.get(); |
| 65 | node.name_map[entry_name] = result; |
| 66 | if (result != nullptr) { |
| 67 | node.offset_map[result->entry_offset()] = std::move(elf); |
| 68 | } |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | std::unique_ptr<EmbeddedElf> ApkInspector::FindElfInApkByOffsetWithoutCache( |
| 73 | const std::string& apk_path, uint64_t file_offset) { |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 74 | std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(apk_path); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 75 | if (!ahelper) { |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 76 | return nullptr; |
| 77 | } |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 78 | |
| 79 | // Iterate through the zip file. Look for a zip entry corresponding |
| 80 | // to an uncompressed blob whose range intersects with the mmap |
| 81 | // offset we're interested in. |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 82 | bool found = false; |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 83 | ZipEntry found_entry; |
| 84 | std::string found_entry_name; |
| 85 | bool result = ahelper->IterateEntries([&](ZipEntry& entry, const std::string& name) { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 86 | if (entry.method == kCompressStored && file_offset >= static_cast<uint64_t>(entry.offset) && |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 87 | file_offset < static_cast<uint64_t>(entry.offset) + entry.uncompressed_length) { |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 88 | found = true; |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 89 | found_entry = entry; |
| 90 | found_entry_name = name; |
| 91 | return false; |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 92 | } |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 93 | return true; |
| 94 | }); |
| 95 | if (!result || !found) { |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 96 | return nullptr; |
| 97 | } |
| 98 | |
| 99 | // We found something in the zip file at the right spot. Is it an ELF? |
Yabin Cui | 02e2033 | 2020-03-16 19:38:23 -0700 | [diff] [blame] | 100 | if (IsValidElfFile(ahelper->GetFd(), found_entry.offset) != ElfStatus::NO_ERROR) { |
Yabin Cui | c36ea8b | 2018-04-16 18:21:40 -0700 | [diff] [blame] | 101 | // Omit files that are not ELF files. |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 102 | return nullptr; |
| 103 | } |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 104 | return std::unique_ptr<EmbeddedElf>(new EmbeddedElf( |
| 105 | apk_path, found_entry_name, found_entry.offset, found_entry.uncompressed_length)); |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 108 | std::unique_ptr<EmbeddedElf> ApkInspector::FindElfInApkByNameWithoutCache( |
| 109 | const std::string& apk_path, const std::string& entry_name) { |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 110 | std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(apk_path); |
Yabin Cui | be7ec66 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 111 | if (!ahelper) { |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 112 | return nullptr; |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 113 | } |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 114 | ZipEntry zentry; |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 115 | if (!ahelper->FindEntry(entry_name, &zentry)) { |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 116 | return nullptr; |
| 117 | } |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 118 | if (zentry.method != kCompressStored || zentry.compressed_length != zentry.uncompressed_length) { |
Yabin Cui | 8422f34 | 2018-05-09 17:27:27 -0700 | [diff] [blame] | 119 | return nullptr; |
| 120 | } |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 121 | return std::unique_ptr<EmbeddedElf>( |
| 122 | new EmbeddedElf(apk_path, entry_name, zentry.offset, zentry.uncompressed_length)); |
Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 123 | } |
| 124 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 125 | // Refer file in apk in compliance with |
| 126 | // http://developer.android.com/reference/java/net/JarURLConnection.html. |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 127 | std::string GetUrlInApk(const std::string& apk_path, const std::string& elf_filename) { |
| 128 | return apk_path + "!/" + elf_filename; |
| 129 | } |
| 130 | |
| 131 | std::tuple<bool, std::string, std::string> SplitUrlInApk(const std::string& path) { |
| 132 | size_t pos = path.find("!/"); |
| 133 | if (pos == std::string::npos) { |
| 134 | return std::make_tuple(false, "", ""); |
| 135 | } |
| 136 | return std::make_tuple(true, path.substr(0, pos), path.substr(pos + 2)); |
| 137 | } |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 138 | |
Joel Fernandes | 7e8b2b9 | 2018-08-24 12:20:28 -0700 | [diff] [blame] | 139 | // Parse path like "[anon:dalvik-classes.dex extracted in memory from /..base.apk] (deleted)", |
| 140 | // or "/dev/ashmem/dalvik-classes.dex extracted in memory from /..base.apk (deleted)" on Android P. |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 141 | bool ParseExtractedInMemoryPath(const std::string& path, std::string* zip_path, |
| 142 | std::string* entry_name) { |
Joel Fernandes | 7e8b2b9 | 2018-08-24 12:20:28 -0700 | [diff] [blame] | 143 | const char* prefixes[2] = {"[anon:dalvik-", "/dev/ashmem/dalvik-"}; |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 144 | const char* key = " extracted in memory from "; |
| 145 | size_t pos = path.find(key); |
Joel Fernandes | 7e8b2b9 | 2018-08-24 12:20:28 -0700 | [diff] [blame] | 146 | if (pos != std::string::npos) { |
| 147 | for (const char* prefix : prefixes) { |
| 148 | if (android::base::StartsWith(path, prefix)) { |
| 149 | size_t entry_name_start = strlen(prefix); |
| 150 | size_t entry_name_end = pos; |
| 151 | size_t zip_path_start = pos + strlen(key); |
| 152 | size_t zip_path_end = path.find_first_of(" ]", zip_path_start); |
| 153 | if (zip_path_end == std::string::npos) { |
| 154 | zip_path_end = path.size(); |
| 155 | } |
| 156 | if (entry_name_start < entry_name_end && zip_path_start < zip_path_end) { |
| 157 | *entry_name = path.substr(entry_name_start, entry_name_end - entry_name_start); |
| 158 | *zip_path = path.substr(zip_path_start, zip_path_end - zip_path_start); |
Yabin Cui | 16ab77d | 2018-12-13 11:34:17 -0800 | [diff] [blame] | 159 | size_t multidex_separator_pos = zip_path->find('!'); |
| 160 | if (multidex_separator_pos != std::string::npos) { |
| 161 | zip_path->resize(multidex_separator_pos); |
| 162 | } |
Joel Fernandes | 7e8b2b9 | 2018-08-24 12:20:28 -0700 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | } |
Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | return false; |
| 169 | } |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 170 | |
| 171 | } // namespace simpleperf |