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((u1*)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);
140 pDvmDex->isMappedReadOnly = true;
149 * Create a DexFile structure for a "partial" DEX. This is one that is in
150 * the process of being optimized. The optimization header isn't finished
151 * and we won't have any of the auxillary data tables, so we have to do
152 * the initialization slightly differently.
154 * Returns nonzero on error.
156 int dvmDexFileOpenPartial(const void* addr, int len, DvmDex** ppDvmDex)
160 int parseFlags = kDexParseDefault;
163 /* -- file is incomplete, new checksum has not yet been calculated
164 if (gDvm.verifyDexChecksum)
165 parseFlags |= kDexParseVerifyChecksum;
168 pDexFile = dexFileParse((u1*)addr, len, parseFlags);
169 if (pDexFile == NULL) {
170 LOGE("DEX parse failed\n");
173 pDvmDex = allocateAuxStructures(pDexFile);
174 if (pDvmDex == NULL) {
175 dexFileFree(pDexFile);
179 pDvmDex->isMappedReadOnly = false;
188 * Free up the DexFile and any associated data structures.
190 * Note we may be called with a partially-initialized DvmDex.
192 void dvmDexFileFree(DvmDex* pDvmDex)
197 dexFileFree(pDvmDex->pDexFile);
199 LOGV("+++ DEX %p: freeing aux structs\n", pDvmDex);
200 free(pDvmDex->pResStrings);
201 free(pDvmDex->pResClasses);
202 free(pDvmDex->pResMethods);
203 free(pDvmDex->pResFields);
204 dvmFreeAtomicCache(pDvmDex->pInterfaceCache);
206 sysReleaseShmem(&pDvmDex->memMap);
212 * Change the byte at the specified address to a new value. If the location
213 * already has the new value, do nothing.
215 * This requires changing the access permissions to read-write, updating
216 * the value, and then resetting the permissions.
218 * We need to ensure mutual exclusion at a page granularity to avoid a race
219 * where one threads sets read-write, another thread sets read-only, and
220 * then the first thread does a write. Since we don't do a lot of updates,
221 * and the window is small, we just use a lock across the entire DvmDex.
222 * We're only trying to make the page state change atomic; it's up to the
223 * caller to ensure that multiple threads aren't stomping on the same
224 * location (e.g. breakpoints and verifier/optimizer changes happening
227 * TODO: if we're back to the original state of the page, use
228 * madvise(MADV_DONTNEED) to release the private/dirty copy.
230 * Returns "true" on success.
232 bool dvmDexChangeDex1(DvmDex* pDvmDex, u1* addr, u1 newVal)
234 if (*addr == newVal) {
235 LOGV("+++ byte at %p is already 0x%02x\n", addr, newVal);
240 * We're not holding this for long, so we don't bother with switching
243 dvmLockMutex(&pDvmDex->modLock);
245 LOGV("+++ change byte at %p from 0x%02x to 0x%02x\n", addr, *addr, newVal);
246 if (sysChangeMapAccess(addr, 1, true, &pDvmDex->memMap) != 0) {
247 LOGD("NOTE: DEX page access change (->RW) failed\n");
248 /* expected on files mounted from FAT; keep going (may crash) */
253 if (sysChangeMapAccess(addr, 1, false, &pDvmDex->memMap) != 0) {
254 LOGD("NOTE: DEX page access change (->RO) failed\n");
255 /* expected on files mounted from FAT; keep going */
258 dvmUnlockMutex(&pDvmDex->modLock);
264 * Change the 2-byte value at the specified address to a new value. If the
265 * location already has the new value, do nothing.
267 * Otherwise works like dvmDexChangeDex1.
269 bool dvmDexChangeDex2(DvmDex* pDvmDex, u2* addr, u2 newVal)
271 if (*addr == newVal) {
272 LOGV("+++ value at %p is already 0x%04x\n", addr, newVal);
277 * We're not holding this for long, so we don't bother with switching
280 dvmLockMutex(&pDvmDex->modLock);
282 LOGV("+++ change 2byte at %p from 0x%04x to 0x%04x\n", addr, *addr, newVal);
283 if (sysChangeMapAccess(addr, 2, true, &pDvmDex->memMap) != 0) {
284 LOGD("NOTE: DEX page access change (->RW) failed\n");
285 /* expected on files mounted from FAT; keep going (may crash) */
290 if (sysChangeMapAccess(addr, 2, false, &pDvmDex->memMap) != 0) {
291 LOGD("NOTE: DEX page access change (->RO) failed\n");
292 /* expected on files mounted from FAT; keep going */
295 dvmUnlockMutex(&pDvmDex->modLock);