The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [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 | */ |
Dan Bornstein | 6f9edbe | 2011-06-02 12:24:11 -0700 | [diff] [blame] | 16 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 17 | /* |
Dan Bornstein | 6f9edbe | 2011-06-02 12:24:11 -0700 | [diff] [blame] | 18 | * Utility functions for dealing with optimized dex files. |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 19 | */ |
Dan Bornstein | 6f9edbe | 2011-06-02 12:24:11 -0700 | [diff] [blame] | 20 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 21 | #include |
| 22 | #include |
| 23 | #include |
| 24 | #include |
| 25 | #include |
| 26 | #include |
| 27 | #include |
| 28 | #include |
| 29 | |
| 30 | #include "OptInvocation.h" |
| 31 | #include "DexFile.h" |
| 32 | |
Dan Bornstein | 6f9edbe | 2011-06-02 12:24:11 -0700 | [diff] [blame] | 33 | static const char* kCacheDirectoryName = "dalvik-cache"; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 34 | static const char* kClassesDex = "classes.dex"; |
| 35 | |
Ian Rogers | e4e5d8c | 2014-05-01 08:53:35 -0700 | [diff] [blame] | 36 | #if defined(__aarch64__) |
| 37 | static const char* kInstructionSet = "arm64"; |
| 38 | #elif defined(__arm__) |
| 39 | static const char* kInstructionSet = "arm"; |
Narayan Kamath | 44e824d | 2014-04-29 13:56:36 +0100 | [diff] [blame] | 40 | #elif defined(__i386__) |
Ian Rogers | e4e5d8c | 2014-05-01 08:53:35 -0700 | [diff] [blame] | 41 | static const char* kInstructionSet = "x86"; |
Narayan Kamath | 44e824d | 2014-04-29 13:56:36 +0100 | [diff] [blame] | 42 | #elif defined(__mips__) |
Ian Rogers | e4e5d8c | 2014-05-01 08:53:35 -0700 | [diff] [blame] | 43 | static const char* kInstructionSet = "mips"; |
| 44 | #elif defined(__x86_64__) |
| 45 | static const char* kInstructionSet = "x86_64"; |
Narayan Kamath | 44e824d | 2014-04-29 13:56:36 +0100 | [diff] [blame] | 46 | #else |
| 47 | #error Unsupported instruction set. |
| 48 | #endif |
| 49 | |
Brian Carlstrom | 7d765f1 | 2014-05-29 12:50:07 -0700 | [diff] [blame] | 50 | static int dexOptMkdir(const char* path, int mode) |
| 51 | { |
| 52 | #ifdef _WIN32 |
| 53 | return mkdir(path); |
| 54 | #else |
| 55 | return mkdir(path, mode); |
| 56 | #endif |
| 57 | } |
Narayan Kamath | 44e824d | 2014-04-29 13:56:36 +0100 | [diff] [blame] | 58 | |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 59 | /* |
| 60 | * Given the filename of a .jar or .dex file, construct the DEX file cache |
| 61 | * name. |
| 62 | * |
| 63 | * For a Jar, "subFileName" is the name of the entry (usually "classes.dex"). |
| 64 | * For a DEX, it may be NULL. |
| 65 | * |
| 66 | * Returns a newly-allocated string, or NULL on failure. |
| 67 | */ |
| 68 | char* dexOptGenerateCacheFileName(const char* fileName, const char* subFileName) |
| 69 | { |
| 70 | char nameBuf[512]; |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 71 | char absoluteFile[sizeof(nameBuf)]; |
| 72 | const size_t kBufLen = sizeof(nameBuf) - 1; |
| 73 | const char* dataRoot; |
| 74 | char* cp; |
| 75 | |
| 76 | /* |
| 77 | * Get the absolute path of the Jar or DEX file. |
| 78 | */ |
| 79 | absoluteFile[0] = '\0'; |
| 80 | if (fileName[0] != '/') { |
| 81 | /* |
| 82 | * Generate the absolute path. This doesn't do everything it |
| 83 | * should, e.g. if filename is "./out/whatever" it doesn't crunch |
| 84 | * the leading "./" out, but it'll do. |
| 85 | */ |
| 86 | if (getcwd(absoluteFile, kBufLen) == NULL) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 87 | ALOGE("Can't get CWD while opening jar file"); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 88 | return NULL; |
| 89 | } |
Aart Bik | 338aeaf | 2016-04-21 15:51:41 -0700 | [diff] [blame^] | 90 | strncat(absoluteFile, "/", kBufLen - strlen(absoluteFile)); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 91 | } |
Aart Bik | 338aeaf | 2016-04-21 15:51:41 -0700 | [diff] [blame^] | 92 | strncat(absoluteFile, fileName, kBufLen - strlen(absoluteFile)); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * Append the name of the Jar file entry, if any. This is not currently |
| 96 | * required, but will be if we start putting more than one DEX file |
| 97 | * in a Jar. |
| 98 | */ |
| 99 | if (subFileName != NULL) { |
Aart Bik | 338aeaf | 2016-04-21 15:51:41 -0700 | [diff] [blame^] | 100 | strncat(absoluteFile, "/", kBufLen - strlen(absoluteFile)); |
| 101 | strncat(absoluteFile, subFileName, kBufLen - strlen(absoluteFile)); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* Turn the path into a flat filename by replacing |
| 105 | * any slashes after the first one with '@' characters. |
| 106 | */ |
| 107 | cp = absoluteFile + 1; |
| 108 | while (*cp != '\0') { |
| 109 | if (*cp == '/') { |
| 110 | *cp = '@'; |
| 111 | } |
| 112 | cp++; |
| 113 | } |
| 114 | |
| 115 | /* Build the name of the cache directory. |
| 116 | */ |
| 117 | dataRoot = getenv("ANDROID_DATA"); |
| 118 | if (dataRoot == NULL) |
| 119 | dataRoot = "/data"; |
Brian Carlstrom | cc54b98 | 2014-05-28 11:04:29 -0700 | [diff] [blame] | 120 | snprintf(nameBuf, kBufLen, "%s/%s", dataRoot, kCacheDirectoryName); |
| 121 | if (strcmp(dataRoot, "/data") != 0) { |
Brian Carlstrom | 7d765f1 | 2014-05-29 12:50:07 -0700 | [diff] [blame] | 122 | int result = dexOptMkdir(nameBuf, 0700); |
Brian Carlstrom | cc54b98 | 2014-05-28 11:04:29 -0700 | [diff] [blame] | 123 | if (result != 0 && errno != EEXIST) { |
| 124 | ALOGE("Failed to create dalvik-cache directory %s: %s", nameBuf, strerror(errno)); |
| 125 | return NULL; |
| 126 | } |
| 127 | } |
Narayan Kamath | 44e824d | 2014-04-29 13:56:36 +0100 | [diff] [blame] | 128 | snprintf(nameBuf, kBufLen, "%s/%s/%s", dataRoot, kCacheDirectoryName, kInstructionSet); |
Brian Carlstrom | cc54b98 | 2014-05-28 11:04:29 -0700 | [diff] [blame] | 129 | if (strcmp(dataRoot, "/data") != 0) { |
Brian Carlstrom | 7d765f1 | 2014-05-29 12:50:07 -0700 | [diff] [blame] | 130 | int result = dexOptMkdir(nameBuf, 0700); |
Brian Carlstrom | cc54b98 | 2014-05-28 11:04:29 -0700 | [diff] [blame] | 131 | if (result != 0 && errno != EEXIST) { |
| 132 | ALOGE("Failed to create dalvik-cache directory %s: %s", nameBuf, strerror(errno)); |
| 133 | return NULL; |
| 134 | } |
| 135 | } |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 136 | |
| 137 | /* Tack on the file name for the actual cache file path. |
| 138 | */ |
Aart Bik | 338aeaf | 2016-04-21 15:51:41 -0700 | [diff] [blame^] | 139 | strncat(nameBuf, absoluteFile, kBufLen - strlen(nameBuf)); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 140 | |
Steve Block | 92c1f6f | 2011-10-20 11:55:54 +0100 | [diff] [blame] | 141 | ALOGV("Cache file for '%s' '%s' is '%s'", fileName, subFileName, nameBuf); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 142 | return strdup(nameBuf); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Create a skeletal "opt" header in a new file. Most of the fields are |
| 147 | * initialized to garbage, but we fill in "dexOffset" so others can |
| 148 | * see how large the header is. |
| 149 | * |
| 150 | * "fd" must be positioned at the start of the file. On return, it will |
| 151 | * be positioned just past the header, and the place where the DEX data |
| 152 | * should go. |
| 153 | * |
| 154 | * Returns 0 on success, errno on failure. |
| 155 | */ |
| 156 | int dexOptCreateEmptyHeader(int fd) |
| 157 | { |
| 158 | DexOptHeader optHdr; |
| 159 | ssize_t actual; |
| 160 | |
| 161 | assert(lseek(fd, 0, SEEK_CUR) == 0); |
| 162 | |
| 163 | /* |
| 164 | * The data is only expected to be readable on the current system, so |
| 165 | * we just write the structure. We do need the file offset to be 64-bit |
| 166 | * aligned to fulfill a DEX requirement. |
| 167 | */ |
| 168 | assert((sizeof(optHdr) & 0x07) == 0); |
| 169 | memset(&optHdr, 0xff, sizeof(optHdr)); |
| 170 | optHdr.dexOffset = sizeof(optHdr); |
| 171 | actual = write(fd, &optHdr, sizeof(optHdr)); |
| 172 | if (actual != sizeof(optHdr)) { |
| 173 | int err = errno ? errno : -1; |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 174 | ALOGE("opt header write failed: %s", strerror(errno)); |
The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 175 | return errno; |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |