Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 "zip_archive.h" |
| 18 | |
| 19 | #include <fcntl.h> |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
Andreas Gampe | 0dfc315 | 2017-04-24 07:58:06 -0700 | [diff] [blame] | 21 | #include <sys/mman.h> // For the PROT_* and MAP_* constants. |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 25 | #include <vector> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 26 | |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 27 | #include "android-base/stringprintf.h" |
Andreas Gampe | 0c2d3e5 | 2017-07-03 12:50:44 -0700 | [diff] [blame] | 28 | #include "ziparchive/zip_archive.h" |
| 29 | |
Andreas Gampe | 3b7dc35 | 2017-06-06 20:02:03 -0700 | [diff] [blame] | 30 | #include "base/bit_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 31 | #include "base/unix_file/fd_file.h" |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 32 | #include "dex/dex_file.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 33 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 34 | namespace art { |
| 35 | |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 36 | // Log file contents and mmap info when mapping entries directly. |
| 37 | static constexpr const bool kDebugZipMapDirectly = false; |
| 38 | |
| 39 | using android::base::StringPrintf; |
| 40 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 41 | uint32_t ZipEntry::GetUncompressedLength() { |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 42 | return zip_entry_->uncompressed_length; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | uint32_t ZipEntry::GetCrc32() { |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 46 | return zip_entry_->crc32; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 49 | bool ZipEntry::IsUncompressed() { |
| 50 | return zip_entry_->method == kCompressStored; |
| 51 | } |
| 52 | |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 53 | bool ZipEntry::IsAlignedTo(size_t alignment) const { |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 54 | DCHECK(IsPowerOfTwo(alignment)) << alignment; |
| 55 | return IsAlignedParam(zip_entry_->offset, static_cast<int>(alignment)); |
| 56 | } |
| 57 | |
Nicolas Geoffray | f307527 | 2018-01-08 12:41:19 +0000 | [diff] [blame] | 58 | bool ZipEntry::IsAlignedToDexHeader() const { |
| 59 | return IsAlignedTo(alignof(DexFile::Header)); |
| 60 | } |
| 61 | |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 62 | ZipEntry::~ZipEntry() { |
| 63 | delete zip_entry_; |
| 64 | } |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 65 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 66 | bool ZipEntry::ExtractToFile(File& file, std::string* error_msg) { |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 67 | const int32_t error = ExtractEntryToFile(handle_, zip_entry_, file.Fd()); |
| 68 | if (error) { |
| 69 | *error_msg = std::string(ErrorCodeString(error)); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 73 | return true; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Brian Carlstrom | 0aa504b | 2014-05-23 02:47:28 -0700 | [diff] [blame] | 76 | MemMap* ZipEntry::ExtractToMemMap(const char* zip_filename, const char* entry_filename, |
| 77 | std::string* error_msg) { |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 78 | std::string name(entry_filename); |
| 79 | name += " extracted in memory from "; |
Brian Carlstrom | 0aa504b | 2014-05-23 02:47:28 -0700 | [diff] [blame] | 80 | name += zip_filename; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 81 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous(name.c_str(), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 82 | nullptr, GetUncompressedLength(), |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 83 | PROT_READ | PROT_WRITE, false, false, |
| 84 | error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 85 | if (map.get() == nullptr) { |
| 86 | DCHECK(!error_msg->empty()); |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 87 | return nullptr; |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 90 | const int32_t error = ExtractToMemory(handle_, zip_entry_, |
| 91 | map->Begin(), map->Size()); |
| 92 | if (error) { |
| 93 | *error_msg = std::string(ErrorCodeString(error)); |
| 94 | return nullptr; |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | return map.release(); |
| 98 | } |
| 99 | |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 100 | MemMap* ZipEntry::MapDirectlyFromFile(const char* zip_filename, std::string* error_msg) { |
| 101 | const int zip_fd = GetFileDescriptor(handle_); |
| 102 | const char* entry_filename = entry_name_.c_str(); |
| 103 | |
| 104 | // Should not happen since we don't have a memory ZipArchive constructor. |
| 105 | // However the underlying ZipArchive isn't required to have an FD, |
| 106 | // so check to be sure. |
| 107 | CHECK_GE(zip_fd, 0) << |
| 108 | StringPrintf("Cannot map '%s' (in zip '%s') directly because the zip archive " |
| 109 | "is not file backed.", |
| 110 | entry_filename, |
| 111 | zip_filename); |
| 112 | |
| 113 | if (!IsUncompressed()) { |
| 114 | *error_msg = StringPrintf("Cannot map '%s' (in zip '%s') directly because it is compressed.", |
| 115 | entry_filename, |
| 116 | zip_filename); |
| 117 | return nullptr; |
| 118 | } else if (zip_entry_->uncompressed_length != zip_entry_->compressed_length) { |
| 119 | *error_msg = StringPrintf("Cannot map '%s' (in zip '%s') directly because " |
| 120 | "entry has bad size (%u != %u).", |
| 121 | entry_filename, |
| 122 | zip_filename, |
| 123 | zip_entry_->uncompressed_length, |
| 124 | zip_entry_->compressed_length); |
| 125 | return nullptr; |
| 126 | } |
| 127 | |
| 128 | std::string name(entry_filename); |
| 129 | name += " mapped directly in memory from "; |
| 130 | name += zip_filename; |
| 131 | |
| 132 | const off_t offset = zip_entry_->offset; |
| 133 | |
| 134 | if (kDebugZipMapDirectly) { |
| 135 | LOG(INFO) << "zip_archive: " << "make mmap of " << name << " @ offset = " << offset; |
| 136 | } |
| 137 | |
| 138 | std::unique_ptr<MemMap> map( |
| 139 | MemMap::MapFileAtAddress(nullptr, // Expected pointer address |
| 140 | GetUncompressedLength(), // Byte count |
| 141 | PROT_READ | PROT_WRITE, |
| 142 | MAP_PRIVATE, |
| 143 | zip_fd, |
| 144 | offset, |
| 145 | false, // Don't restrict allocation to lower4GB |
| 146 | false, // Doesn't overlap existing map (reuse=false) |
| 147 | name.c_str(), |
| 148 | /*out*/error_msg)); |
| 149 | |
| 150 | if (map == nullptr) { |
| 151 | DCHECK(!error_msg->empty()); |
| 152 | } |
| 153 | |
| 154 | if (kDebugZipMapDirectly) { |
| 155 | // Dump contents of file, same format as using this shell command: |
| 156 | // $> od -j <offset> -t x1 <zip_filename> |
| 157 | static constexpr const int kMaxDumpChars = 15; |
| 158 | lseek(zip_fd, 0, SEEK_SET); |
| 159 | |
| 160 | int count = offset + kMaxDumpChars; |
| 161 | |
| 162 | std::string tmp; |
| 163 | char buf; |
| 164 | |
| 165 | // Dump file contents. |
| 166 | int i = 0; |
| 167 | while (read(zip_fd, &buf, 1) > 0 && i < count) { |
| 168 | tmp += StringPrintf("%3d ", (unsigned int)buf); |
| 169 | ++i; |
| 170 | } |
| 171 | |
| 172 | LOG(INFO) << "map_fd raw bytes starting at 0"; |
| 173 | LOG(INFO) << "" << tmp; |
| 174 | LOG(INFO) << "---------------------------"; |
| 175 | |
| 176 | // Dump map contents. |
| 177 | if (map != nullptr) { |
| 178 | tmp = ""; |
| 179 | |
| 180 | count = kMaxDumpChars; |
| 181 | |
| 182 | uint8_t* begin = map->Begin(); |
| 183 | for (i = 0; i < count; ++i) { |
| 184 | tmp += StringPrintf("%3d ", (unsigned int)begin[i]); |
| 185 | } |
| 186 | |
| 187 | LOG(INFO) << "map address " << StringPrintf("%p", begin); |
| 188 | LOG(INFO) << "map first " << kMaxDumpChars << " chars:"; |
| 189 | LOG(INFO) << tmp; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return map.release(); |
| 194 | } |
| 195 | |
Mathieu Chartier | 792111c | 2018-02-15 13:02:15 -0800 | [diff] [blame^] | 196 | MemMap* ZipEntry::MapDirectlyOrExtract(const char* zip_filename, |
| 197 | const char* entry_filename, |
| 198 | std::string* error_msg) { |
| 199 | if (IsUncompressed() && GetFileDescriptor(handle_) >= 0) { |
| 200 | MemMap* ret = MapDirectlyFromFile(zip_filename, error_msg); |
| 201 | if (ret != nullptr) { |
| 202 | return ret; |
| 203 | } |
| 204 | } |
| 205 | // Fall back to extraction for the failure case. |
| 206 | return ExtractToMemMap(zip_filename, entry_filename, error_msg); |
| 207 | } |
| 208 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 209 | static void SetCloseOnExec(int fd) { |
| 210 | // This dance is more portable than Linux's O_CLOEXEC open(2) flag. |
| 211 | int flags = fcntl(fd, F_GETFD); |
| 212 | if (flags == -1) { |
| 213 | PLOG(WARNING) << "fcntl(" << fd << ", F_GETFD) failed"; |
| 214 | return; |
| 215 | } |
| 216 | int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
| 217 | if (rc == -1) { |
| 218 | PLOG(WARNING) << "fcntl(" << fd << ", F_SETFD, " << flags << ") failed"; |
| 219 | return; |
| 220 | } |
| 221 | } |
| 222 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 223 | ZipArchive* ZipArchive::Open(const char* filename, std::string* error_msg) { |
| 224 | DCHECK(filename != nullptr); |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 225 | |
| 226 | ZipArchiveHandle handle; |
| 227 | const int32_t error = OpenArchive(filename, &handle); |
| 228 | if (error) { |
| 229 | *error_msg = std::string(ErrorCodeString(error)); |
| 230 | CloseArchive(handle); |
| 231 | return nullptr; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 232 | } |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 233 | |
| 234 | SetCloseOnExec(GetFileDescriptor(handle)); |
| 235 | return new ZipArchive(handle); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 238 | ZipArchive* ZipArchive::OpenFromFd(int fd, const char* filename, std::string* error_msg) { |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 239 | DCHECK(filename != nullptr); |
| 240 | DCHECK_GT(fd, 0); |
| 241 | |
| 242 | ZipArchiveHandle handle; |
| 243 | const int32_t error = OpenArchiveFd(fd, filename, &handle); |
| 244 | if (error) { |
| 245 | *error_msg = std::string(ErrorCodeString(error)); |
| 246 | CloseArchive(handle); |
| 247 | return nullptr; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 248 | } |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 249 | |
| 250 | SetCloseOnExec(GetFileDescriptor(handle)); |
| 251 | return new ZipArchive(handle); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 254 | ZipEntry* ZipArchive::Find(const char* name, std::string* error_msg) const { |
| 255 | DCHECK(name != nullptr); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 256 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 257 | // Resist the urge to delete the space. <: is a bigraph sequence. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 258 | std::unique_ptr< ::ZipEntry> zip_entry(new ::ZipEntry); |
Yusuke Sato | 64db62d | 2015-06-25 14:56:49 -0700 | [diff] [blame] | 259 | const int32_t error = FindEntry(handle_, ZipString(name), zip_entry.get()); |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 260 | if (error) { |
| 261 | *error_msg = std::string(ErrorCodeString(error)); |
| 262 | return nullptr; |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Igor Murashkin | 271a0f8 | 2017-02-14 21:14:17 +0000 | [diff] [blame] | 265 | return new ZipEntry(handle_, zip_entry.release(), name); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 268 | ZipArchive::~ZipArchive() { |
| 269 | CloseArchive(handle_); |
| 270 | } |
| 271 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 272 | } // namespace art |