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 * Some utility functions for use with command-line utilities.
18 */
19 #include "DexFile.h"
20 #include "ZipArchive.h"
21 #include "CmdUtils.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <errno.h>
27
28
29 /*
30 * Extract "classes.dex" from archive file.
31 *
32 * If "quiet" is set, don't report common errors.
33 */
dexUnzipToFile(const char * zipFileName,const char * outFileName,bool quiet)34 UnzipToFileResult dexUnzipToFile(const char* zipFileName,
35 const char* outFileName, bool quiet)
36 {
37 UnzipToFileResult result = kUTFRSuccess;
38 static const char* kFileToExtract = "classes.dex";
39 ZipArchive archive;
40 ZipEntry entry;
41 bool unlinkOnFailure = false;
42 int fd = -1;
43
44 if (dexZipOpenArchive(zipFileName, &archive) != 0) {
45 if (!quiet) {
46 fprintf(stderr, "Unable to open '%s' as zip archive\n",
47 zipFileName);
48 }
49 result = kUTFRNotZip;
50 goto bail;
51 }
52
53 fd = open(outFileName, O_WRONLY | O_CREAT | O_EXCL, 0600);
54 if (fd < 0) {
55 fprintf(stderr, "Unable to create output file '%s': %s\n",
56 outFileName, strerror(errno));
57 result = kUTFROutputFileProblem;
58 goto bail;
59 }
60
61 unlinkOnFailure = true;
62
63 entry = dexZipFindEntry(&archive, kFileToExtract);
64 if (entry == NULL) {
65 if (!quiet) {
66 fprintf(stderr, "Unable to find '%s' in '%s'\n",
67 kFileToExtract, zipFileName);
68 }
69 result = kUTFRNoClassesDex;
70 goto bail;
71 }
72
73 if (dexZipExtractEntryToFile(&archive, entry, fd) != 0) {
74 fprintf(stderr, "Extract of '%s' from '%s' failed\n",
75 kFileToExtract, zipFileName);
76 result = kUTFRBadZip;
77 goto bail;
78 }
79
80 bail:
81 if (fd >= 0)
82 close(fd);
83 if (unlinkOnFailure && result != kUTFRSuccess)
84 unlink(outFileName);
85 dexZipCloseArchive(&archive);
86 return result;
87 }
88
89 /*
90 * Map the specified DEX file read-only (possibly after expanding it into a
91 * temp file from a Jar). Pass in a MemMapping struct to hold the info.
92 * If the file is an unoptimized DEX file, then byte-swapping and structural
93 * verification are performed on it before the memory is made read-only.
94 *
95 * The temp file is deleted after the map succeeds.
96 *
97 * This is intended for use by tools (e.g. dexdump) that need to get a
98 * read-only copy of a DEX file that could be in a number of different states.
99 *
100 * If "tempFileName" is NULL, a default value is used. The temp file is
101 * deleted after the map succeeds.
102 *
103 * If "quiet" is set, don't report common errors.
104 *
105 * Returns 0 (kUTFRSuccess) on success.
106 */
dexOpenAndMap(const char * fileName,const char * tempFileName,MemMapping * pMap,bool quiet)107 UnzipToFileResult dexOpenAndMap(const char* fileName, const char* tempFileName,
108 MemMapping* pMap, bool quiet)
109 {
110 UnzipToFileResult result = kUTFRGenericFailure;
111 int len = strlen(fileName);
112 char tempNameBuf[32];
113 bool removeTemp = false;
114 int fd = -1;
115
116 if (len < 5) {
117 if (!quiet) {
118 fprintf(stderr,
119 "ERROR: filename must end in .dex, .zip, .jar, or .apk\n");
120 }
121 result = kUTFRBadArgs;
122 goto bail;
123 }
124
125 if (strcasecmp(fileName + len -3, "dex") != 0) {
126 if (tempFileName == NULL) {
127 /*
128 * Try .zip/.jar/.apk, all of which are Zip archives with
129 * "classes.dex" inside. We need to extract the compressed
130 * data to a temp file, the location of which varies.
131 */
132 if (access("/tmp", W_OK) == 0)
133 sprintf(tempNameBuf, "/tmp/dex-temp-%d", getpid());
134 else
135 sprintf(tempNameBuf, "/sdcard/dex-temp-%d", getpid());
136
137 tempFileName = tempNameBuf;
138 }
139
140 result = dexUnzipToFile(fileName, tempFileName, quiet);
141
142 if (result == kUTFRSuccess) {
143 //printf("+++ Good unzip to '%s'\n", tempFileName);
144 fileName = tempFileName;
145 removeTemp = true;
146 } else if (result == kUTFRNotZip) {
147 if (!quiet) {
148 fprintf(stderr, "Not Zip, retrying as DEX\n");
149 }
150 } else {
151 if (!quiet && result == kUTFRNoClassesDex) {
152 fprintf(stderr, "Zip has no classes.dex\n");
153 }
154 goto bail;
155 }
156 }
157
158 result = kUTFRGenericFailure;
159
160 /*
161 * Pop open the (presumed) DEX file.
162 */
163 fd = open(fileName, O_RDONLY);
164 if (fd < 0) {
165 if (!quiet) {
166 fprintf(stderr, "ERROR: unable to open '%s': %s\n",
167 fileName, strerror(errno));
168 }
169 goto bail;
170 }
171
172 if (sysMapFileInShmemWritableReadOnly(fd, pMap) != 0) {
173 fprintf(stderr, "ERROR: Unable to map '%s'\n", fileName);
174 goto bail;
175 }
176
177 /*
178 * This call will fail if the file exists on a filesystem that
179 * doesn't support mprotect(). If that's the case, then the file
180 * will have already been mapped private-writable by the previous
181 * call, so we don't need to do anything special if this call
182 * returns non-zero.
183 */
184 sysChangeMapAccess(pMap->addr, pMap->length, true, pMap);
185
186 if (dexSwapAndVerifyIfNecessary(pMap->addr, pMap->length)) {
187 fprintf(stderr, "ERROR: Failed structural verification of '%s'\n",
188 fileName);
189 goto bail;
190 }
191
192 /*
193 * Similar to above, this call will fail if the file wasn't ever
194 * read-only to begin with. This is innocuous, though it is
195 * undesirable from a memory hygiene perspective.
196 */
197 sysChangeMapAccess(pMap->addr, pMap->length, false, pMap);
198
199 /*
200 * Success! Close the file and return with the start/length in pMap.
201 */
202 result = 0;
203
204 bail:
205 if (fd >= 0)
206 close(fd);
207 if (removeTemp) {
208 if (unlink(tempFileName) != 0) {
209 fprintf(stderr, "WARNING: unable to remove temp '%s'\n",
210 tempFileName);
211 }
212 }
213 return result;
214 }
215