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