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 /*
18 * Utility functions for dealing with optimized dex files.
19 */
20
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/file.h>
28 #include <errno.h>
29
30 #include "OptInvocation.h"
31 #include "DexFile.h"
32
33 static const char* kCacheDirectoryName = "dalvik-cache";
34 static const char* kClassesDex = "classes.dex";
35
36 #if defined(__aarch64__)
37 static const char* kInstructionSet = "arm64";
38 #elif defined(__arm__)
39 static const char* kInstructionSet = "arm";
40 #elif defined(__i386__)
41 static const char* kInstructionSet = "x86";
42 #elif defined(__mips__)
43 static const char* kInstructionSet = "mips";
44 #elif defined(__x86_64__)
45 static const char* kInstructionSet = "x86_64";
46 #else
47 #error Unsupported instruction set.
48 #endif
49
dexOptMkdir(const char * path,int mode)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 }
58
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 */
dexOptGenerateCacheFileName(const char * fileName,const char * subFileName)68 char* dexOptGenerateCacheFileName(const char* fileName, const char* subFileName)
69 {
70 char nameBuf[512];
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) {
87 ALOGE("Can't get CWD while opening jar file");
88 return NULL;
89 }
90 strncat(absoluteFile, "/", kBufLen);
91 }
92 strncat(absoluteFile, fileName, kBufLen);
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) {
100 strncat(absoluteFile, "/", kBufLen);
101 strncat(absoluteFile, subFileName, kBufLen);
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";
120 snprintf(nameBuf, kBufLen, "%s/%s", dataRoot, kCacheDirectoryName);
121 if (strcmp(dataRoot, "/data") != 0) {
122 int result = dexOptMkdir(nameBuf, 0700);
123 if (result != 0 && errno != EEXIST) {
124 ALOGE("Failed to create dalvik-cache directory %s: %s", nameBuf, strerror(errno));
125 return NULL;
126 }
127 }
128 snprintf(nameBuf, kBufLen, "%s/%s/%s", dataRoot, kCacheDirectoryName, kInstructionSet);
129 if (strcmp(dataRoot, "/data") != 0) {
130 int result = dexOptMkdir(nameBuf, 0700);
131 if (result != 0 && errno != EEXIST) {
132 ALOGE("Failed to create dalvik-cache directory %s: %s", nameBuf, strerror(errno));
133 return NULL;
134 }
135 }
136
137 /* Tack on the file name for the actual cache file path.
138 */
139 strncat(nameBuf, absoluteFile, kBufLen);
140
141 ALOGV("Cache file for '%s' '%s' is '%s'", fileName, subFileName, nameBuf);
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 */
dexOptCreateEmptyHeader(int fd)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;
174 ALOGE("opt header write failed: %s", strerror(errno));
175 return errno;
176 }
177
178 return 0;
179 }
180