blob: 5b3cab42d8ce2ab4664dc2ef3c87fdf3719e4f35 [file] [log] [blame]
Brian Carlstromb0460ea2011-07-29 10:08:05 -07001/*
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 Rogers8d31bbd2013-10-13 10:44:14 -070020#include <stdio.h>
Andreas Gampe0dfc3152017-04-24 07:58:06 -070021#include <sys/mman.h> // For the PROT_* and MAP_* constants.
Brian Carlstromb0460ea2011-07-29 10:08:05 -070022#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
Ian Rogers700a4022014-05-19 16:49:03 -070025#include <vector>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070026
Igor Murashkin271a0f82017-02-14 21:14:17 +000027#include "android-base/stringprintf.h"
Andreas Gampe0c2d3e52017-07-03 12:50:44 -070028#include "ziparchive/zip_archive.h"
29
Andreas Gampe3b7dc352017-06-06 20:02:03 -070030#include "base/bit_utils.h"
Elliott Hughes76160052012-12-12 16:31:20 -080031#include "base/unix_file/fd_file.h"
Nicolas Geoffrayf3075272018-01-08 12:41:19 +000032#include "dex/dex_file.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070033
Brian Carlstromb0460ea2011-07-29 10:08:05 -070034namespace art {
35
Igor Murashkin271a0f82017-02-14 21:14:17 +000036// Log file contents and mmap info when mapping entries directly.
37static constexpr const bool kDebugZipMapDirectly = false;
38
39using android::base::StringPrintf;
40
Brian Carlstromb0460ea2011-07-29 10:08:05 -070041uint32_t ZipEntry::GetUncompressedLength() {
Narayan Kamath92572be2013-11-28 14:06:24 +000042 return zip_entry_->uncompressed_length;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070043}
44
45uint32_t ZipEntry::GetCrc32() {
Narayan Kamath92572be2013-11-28 14:06:24 +000046 return zip_entry_->crc32;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070047}
48
Igor Murashkin271a0f82017-02-14 21:14:17 +000049bool ZipEntry::IsUncompressed() {
50 return zip_entry_->method == kCompressStored;
51}
52
Nicolas Geoffrayf3075272018-01-08 12:41:19 +000053bool ZipEntry::IsAlignedTo(size_t alignment) const {
Igor Murashkin271a0f82017-02-14 21:14:17 +000054 DCHECK(IsPowerOfTwo(alignment)) << alignment;
55 return IsAlignedParam(zip_entry_->offset, static_cast<int>(alignment));
56}
57
Nicolas Geoffrayf3075272018-01-08 12:41:19 +000058bool ZipEntry::IsAlignedToDexHeader() const {
59 return IsAlignedTo(alignof(DexFile::Header));
60}
61
Mathieu Chartier661974a2014-01-09 11:23:53 -080062ZipEntry::~ZipEntry() {
63 delete zip_entry_;
64}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070065
Ian Rogers8d31bbd2013-10-13 10:44:14 -070066bool ZipEntry::ExtractToFile(File& file, std::string* error_msg) {
Narayan Kamath92572be2013-11-28 14:06:24 +000067 const int32_t error = ExtractEntryToFile(handle_, zip_entry_, file.Fd());
68 if (error) {
69 *error_msg = std::string(ErrorCodeString(error));
Brian Carlstrom89521892011-12-07 22:05:07 -080070 return false;
71 }
72
Narayan Kamath92572be2013-11-28 14:06:24 +000073 return true;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070074}
75
Brian Carlstrom0aa504b2014-05-23 02:47:28 -070076MemMap* ZipEntry::ExtractToMemMap(const char* zip_filename, const char* entry_filename,
77 std::string* error_msg) {
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070078 std::string name(entry_filename);
79 name += " extracted in memory from ";
Brian Carlstrom0aa504b2014-05-23 02:47:28 -070080 name += zip_filename;
Ian Rogers700a4022014-05-19 16:49:03 -070081 std::unique_ptr<MemMap> map(MemMap::MapAnonymous(name.c_str(),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070082 nullptr, GetUncompressedLength(),
Vladimir Marko5c42c292015-02-25 12:02:49 +000083 PROT_READ | PROT_WRITE, false, false,
84 error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070085 if (map.get() == nullptr) {
86 DCHECK(!error_msg->empty());
Narayan Kamath92572be2013-11-28 14:06:24 +000087 return nullptr;
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070088 }
89
Narayan Kamath92572be2013-11-28 14:06:24 +000090 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 Carlstrom4922e9d2013-07-09 17:18:47 -070095 }
96
97 return map.release();
98}
99
Igor Murashkin271a0f82017-02-14 21:14:17 +0000100MemMap* 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 Chartier792111c2018-02-15 13:02:15 -0800196MemMap* 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 Hughesad6c9c32012-01-19 17:39:12 -0800209static 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 Rogers8d31bbd2013-10-13 10:44:14 -0700223ZipArchive* ZipArchive::Open(const char* filename, std::string* error_msg) {
224 DCHECK(filename != nullptr);
Narayan Kamath92572be2013-11-28 14:06:24 +0000225
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 Carlstromb0460ea2011-07-29 10:08:05 -0700232 }
Narayan Kamath92572be2013-11-28 14:06:24 +0000233
234 SetCloseOnExec(GetFileDescriptor(handle));
235 return new ZipArchive(handle);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700236}
237
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700238ZipArchive* ZipArchive::OpenFromFd(int fd, const char* filename, std::string* error_msg) {
Narayan Kamath92572be2013-11-28 14:06:24 +0000239 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 Carlstromb0460ea2011-07-29 10:08:05 -0700248 }
Narayan Kamath92572be2013-11-28 14:06:24 +0000249
250 SetCloseOnExec(GetFileDescriptor(handle));
251 return new ZipArchive(handle);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700252}
253
Narayan Kamath92572be2013-11-28 14:06:24 +0000254ZipEntry* ZipArchive::Find(const char* name, std::string* error_msg) const {
255 DCHECK(name != nullptr);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700256
Narayan Kamath92572be2013-11-28 14:06:24 +0000257 // Resist the urge to delete the space. <: is a bigraph sequence.
Ian Rogers700a4022014-05-19 16:49:03 -0700258 std::unique_ptr< ::ZipEntry> zip_entry(new ::ZipEntry);
Yusuke Sato64db62d2015-06-25 14:56:49 -0700259 const int32_t error = FindEntry(handle_, ZipString(name), zip_entry.get());
Narayan Kamath92572be2013-11-28 14:06:24 +0000260 if (error) {
261 *error_msg = std::string(ErrorCodeString(error));
262 return nullptr;
Kenny Root72fcca22013-09-19 09:25:34 -0700263 }
264
Igor Murashkin271a0f82017-02-14 21:14:17 +0000265 return new ZipEntry(handle_, zip_entry.release(), name);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700266}
267
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000268ZipArchive::~ZipArchive() {
269 CloseArchive(handle_);
270}
271
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700272} // namespace art