2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * VM-specific state associated with a DEX file.
24 * Create auxillary data structures.
26 * We need a 4-byte pointer for every reference to a class, method, field,
27 * or string constant. Summed up over all loaded DEX files (including the
28 * whoppers in the boostrap class path), this adds up to be quite a bit
31 * For more traditional VMs these values could be stuffed into the loaded
32 * class file constant pool area, but we don't have that luxury since our
33 * classes are memory-mapped read-only.
35 * The DEX optimizer will remove the need for some of these (e.g. we won't
36 * use the entry for virtual methods that are only called through
37 * invoke-virtual-quick), creating the possibility of some space reduction
40 static DvmDex* allocateAuxStructures(DexFile* pDexFile)
43 const DexHeader* pHeader;
44 u4 stringCount, classCount, methodCount, fieldCount;
46 pDvmDex = (DvmDex*) calloc(1, sizeof(DvmDex));
50 pDvmDex->pDexFile = pDexFile;
51 pDvmDex->pHeader = pDexFile->pHeader;
53 pHeader = pDvmDex->pHeader;
55 stringCount = pHeader->stringIdsSize;
56 classCount = pHeader->typeIdsSize;
57 methodCount = pHeader->methodIdsSize;
58 fieldCount = pHeader->fieldIdsSize;
60 pDvmDex->pResStrings = (struct StringObject**)
61 calloc(stringCount, sizeof(struct StringObject*));
63 pDvmDex->pResClasses = (struct ClassObject**)
64 calloc(classCount, sizeof(struct ClassObject*));
66 pDvmDex->pResMethods = (struct Method**)
67 calloc(methodCount, sizeof(struct Method*));
69 pDvmDex->pResFields = (struct Field**)
70 calloc(fieldCount, sizeof(struct Field*));
72 LOGV("+++ DEX %p: allocateAux %d+%d+%d+%d * 4 = %d bytes\n",
73 pDvmDex, stringCount, classCount, methodCount, fieldCount,
74 (stringCount + classCount + methodCount + fieldCount) * 4);
76 pDvmDex->pInterfaceCache = dvmAllocAtomicCache(DEX_INTERFACE_CACHE_SIZE);
78 if (pDvmDex->pResStrings == NULL ||
79 pDvmDex->pResClasses == NULL ||
80 pDvmDex->pResMethods == NULL ||
81 pDvmDex->pResFields == NULL ||
82 pDvmDex->pInterfaceCache == NULL)
84 LOGE("Alloc failure in allocateAuxStructures\n");
85 free(pDvmDex->pResStrings);
86 free(pDvmDex->pResClasses);
87 free(pDvmDex->pResMethods);
88 free(pDvmDex->pResFields);
98 * Given an open optimized DEX file, map it into read-only shared memory and
101 * Returns nonzero on error.
103 int dvmDexFileOpenFromFd(int fd, DvmDex** ppDvmDex)
108 int parseFlags = kDexParseDefault;
111 if (gDvm.verifyDexChecksum)
112 parseFlags |= kDexParseVerifyChecksum;
114 if (lseek(fd, 0, SEEK_SET) < 0) {
115 LOGE("lseek rewind failed\n");
119 if (sysMapFileInShmemWritableReadOnly(fd, &memMap) != 0) {
120 LOGE("Unable to map file\n");
124 pDexFile = dexFileParse(memMap.addr, memMap.length, parseFlags);
125 if (pDexFile == NULL) {
126 LOGE("DEX parse failed\n");
127 sysReleaseShmem(&memMap);
131 pDvmDex = allocateAuxStructures(pDexFile);
132 if (pDvmDex == NULL) {
133 dexFileFree(pDexFile);
134 sysReleaseShmem(&memMap);
138 /* tuck this into the DexFile so it gets released later */
139 sysCopyMap(&pDvmDex->memMap, &memMap);
148 * Create a DexFile structure for a "partial" DEX. This is one that is in
149 * the process of being optimized. The optimization header isn't finished
150 * and we won't have any of the auxillary data tables, so we have to do
151 * the initialization slightly differently.
153 * Returns nonzero on error.
155 int dvmDexFileOpenPartial(const void* addr, int len, DvmDex** ppDvmDex)
159 int parseFlags = kDexParseDefault;
162 /* -- file is incomplete, new checksum has not yet been calculated
163 if (gDvm.verifyDexChecksum)
164 parseFlags |= kDexParseVerifyChecksum;
167 pDexFile = dexFileParse(addr, len, parseFlags);
168 if (pDexFile == NULL) {
169 LOGE("DEX parse failed\n");
172 pDvmDex = allocateAuxStructures(pDexFile);
173 if (pDvmDex == NULL) {
174 dexFileFree(pDexFile);
186 * Free up the DexFile and any associated data structures.
188 * Note we may be called with a partially-initialized DvmDex.
190 void dvmDexFileFree(DvmDex* pDvmDex)
195 dexFileFree(pDvmDex->pDexFile);
197 LOGV("+++ DEX %p: freeing aux structs\n", pDvmDex);
198 free(pDvmDex->pResStrings);
199 free(pDvmDex->pResClasses);
200 free(pDvmDex->pResMethods);
201 free(pDvmDex->pResFields);
202 dvmFreeAtomicCache(pDvmDex->pInterfaceCache);
204 sysReleaseShmem(&pDvmDex->memMap);
210 * Change the byte at the specified address to a new value. If the location
211 * already has the new value, do nothing.
213 * This requires changing the access permissions to read-write, updating
214 * the value, and then resetting the permissions.
216 * We need to ensure mutual exclusion at a page granularity to avoid a race
217 * where one threads sets read-write, another thread sets read-only, and
218 * then the first thread does a write. Since we don't do a lot of updates,
219 * and the window is small, we just use a lock across the entire DvmDex.
220 * We're only trying to make the page state change atomic; it's up to the
221 * caller to ensure that multiple threads aren't stomping on the same
222 * location (e.g. breakpoints and verifier/optimizer changes happening
225 * TODO: if we're back to the original state of the page, use
226 * madvise(MADV_DONTNEED) to release the private/dirty copy.
228 * Returns "true" on success.
230 bool dvmDexChangeDex1(DvmDex* pDvmDex, u1* addr, u1 newVal)
232 if (*addr == newVal) {
233 LOGV("+++ byte at %p is already 0x%02x\n", addr, newVal);
238 * We're not holding this for long, so we don't bother with switching
241 dvmLockMutex(&pDvmDex->modLock);
243 LOGV("+++ change byte at %p from 0x%02x to 0x%02x\n", addr, *addr, newVal);
244 if (sysChangeMapAccess(addr, 1, true, &pDvmDex->memMap) != 0) {
245 LOGD("NOTE: DEX page access change (->RW) failed\n");
246 /* expected on files mounted from FAT; keep going (may crash) */
251 if (sysChangeMapAccess(addr, 1, false, &pDvmDex->memMap) != 0) {
252 LOGD("NOTE: DEX page access change (->RO) failed\n");
253 /* expected on files mounted from FAT; keep going */
256 dvmUnlockMutex(&pDvmDex->modLock);
262 * Change the 2-byte value at the specified address to a new value. If the
263 * location already has the new value, do nothing.
265 * Otherwise works like dvmDexChangeDex1.
267 bool dvmDexChangeDex2(DvmDex* pDvmDex, u2* addr, u2 newVal)
269 if (*addr == newVal) {
270 LOGV("+++ value at %p is already 0x%04x\n", addr, newVal);
275 * We're not holding this for long, so we don't bother with switching
278 dvmLockMutex(&pDvmDex->modLock);
280 LOGV("+++ change 2byte at %p from 0x%04x to 0x%04x\n", addr, *addr, newVal);
281 if (sysChangeMapAccess(addr, 2, true, &pDvmDex->memMap) != 0) {
282 LOGD("NOTE: DEX page access change (->RW) failed\n");
283 /* expected on files mounted from FAT; keep going (may crash) */
288 if (sysChangeMapAccess(addr, 2, false, &pDvmDex->memMap) != 0) {
289 LOGD("NOTE: DEX page access change (->RO) failed\n");
290 /* expected on files mounted from FAT; keep going */
293 dvmUnlockMutex(&pDvmDex->modLock);