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