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
41 static DvmDex* allocateAuxStructures(DexFile* pDexFile)
44 const DexHeader* pHeader;
45 u4 stringSize, classSize, methodSize, fieldSize;
47 pHeader = pDexFile->pHeader;
49 stringSize = pHeader->stringIdsSize * sizeof(struct StringObject*);
50 classSize = pHeader->typeIdsSize * sizeof(struct ClassObject*);
51 methodSize = pHeader->methodIdsSize * sizeof(struct Method*);
52 fieldSize = pHeader->fieldIdsSize * sizeof(struct Field*);
54 u4 totalSize = sizeof(DvmDex) +
55 stringSize + classSize + methodSize + fieldSize;
57 u1 *blob = (u1 *)dvmAllocRegion(totalSize,
58 PROT_READ | PROT_WRITE, "dalvik-aux-structure");
59 if ((void *)blob == MAP_FAILED)
62 pDvmDex = (DvmDex*)blob;
63 blob += sizeof(DvmDex);
65 pDvmDex->pDexFile = pDexFile;
66 pDvmDex->pHeader = pHeader;
68 pDvmDex->pResStrings = (struct StringObject**)blob;
70 pDvmDex->pResClasses = (struct ClassObject**)blob;
72 pDvmDex->pResMethods = (struct Method**)blob;
74 pDvmDex->pResFields = (struct Field**)blob;
76 ALOGV("+++ DEX %p: allocateAux (%d+%d+%d+%d)*4 = %d bytes",
77 pDvmDex, stringSizei/4, classSize/4, methodSize/4, fieldSize/4,
78 stringSize + classSize + methodSize + fieldSize);
80 pDvmDex->pInterfaceCache = dvmAllocAtomicCache(DEX_INTERFACE_CACHE_SIZE);
86 * Given an open optimized DEX file, map it into read-only shared memory and
89 * Returns nonzero on error.
91 int dvmDexFileOpenFromFd(int fd, DvmDex** ppDvmDex)
96 int parseFlags = kDexParseDefault;
99 if (gDvm.verifyDexChecksum)
100 parseFlags |= kDexParseVerifyChecksum;
102 if (lseek(fd, 0, SEEK_SET) < 0) {
103 ALOGE("lseek rewind failed");
107 if (sysMapFileInShmemWritableReadOnly(fd, &memMap) != 0) {
108 ALOGE("Unable to map file");
112 pDexFile = dexFileParse((u1*)memMap.addr, memMap.length, parseFlags);
113 if (pDexFile == NULL) {
114 ALOGE("DEX parse failed");
115 sysReleaseShmem(&memMap);
119 pDvmDex = allocateAuxStructures(pDexFile);
120 if (pDvmDex == NULL) {
121 dexFileFree(pDexFile);
122 sysReleaseShmem(&memMap);
126 /* tuck this into the DexFile so it gets released later */
127 sysCopyMap(&pDvmDex->memMap, &memMap);
128 pDvmDex->isMappedReadOnly = true;
137 * Create a DexFile structure for a "partial" DEX. This is one that is in
138 * the process of being optimized. The optimization header isn't finished
139 * and we won't have any of the auxillary data tables, so we have to do
140 * the initialization slightly differently.
142 * Returns nonzero on error.
144 int dvmDexFileOpenPartial(const void* addr, int len, DvmDex** ppDvmDex)
148 int parseFlags = kDexParseDefault;
151 /* -- file is incomplete, new checksum has not yet been calculated
152 if (gDvm.verifyDexChecksum)
153 parseFlags |= kDexParseVerifyChecksum;
156 pDexFile = dexFileParse((u1*)addr, len, parseFlags);
157 if (pDexFile == NULL) {
158 ALOGE("DEX parse failed");
161 pDvmDex = allocateAuxStructures(pDexFile);
162 if (pDvmDex == NULL) {
163 dexFileFree(pDexFile);
167 pDvmDex->isMappedReadOnly = false;
176 * Free up the DexFile and any associated data structures.
178 * Note we may be called with a partially-initialized DvmDex.
180 void dvmDexFileFree(DvmDex* pDvmDex)
187 totalSize = pDvmDex->pHeader->stringIdsSize * sizeof(struct StringObject*);
188 totalSize += pDvmDex->pHeader->typeIdsSize * sizeof(struct ClassObject*);
189 totalSize += pDvmDex->pHeader->methodIdsSize * sizeof(struct Method*);
190 totalSize += pDvmDex->pHeader->fieldIdsSize * sizeof(struct Field*);
191 totalSize += sizeof(DvmDex);
193 dexFileFree(pDvmDex->pDexFile);
195 ALOGV("+++ DEX %p: freeing aux structs", pDvmDex);
196 dvmFreeAtomicCache(pDvmDex->pInterfaceCache);
197 sysReleaseShmem(&pDvmDex->memMap);
198 munmap(pDvmDex, totalSize);
203 * Change the byte at the specified address to a new value. If the location
204 * already has the new value, do nothing.
206 * This requires changing the access permissions to read-write, updating
207 * the value, and then resetting the permissions.
209 * We need to ensure mutual exclusion at a page granularity to avoid a race
210 * where one threads sets read-write, another thread sets read-only, and
211 * then the first thread does a write. Since we don't do a lot of updates,
212 * and the window is small, we just use a lock across the entire DvmDex.
213 * We're only trying to make the page state change atomic; it's up to the
214 * caller to ensure that multiple threads aren't stomping on the same
215 * location (e.g. breakpoints and verifier/optimizer changes happening
218 * TODO: if we're back to the original state of the page, use
219 * madvise(MADV_DONTNEED) to release the private/dirty copy.
221 * Returns "true" on success.
223 bool dvmDexChangeDex1(DvmDex* pDvmDex, u1* addr, u1 newVal)
225 if (*addr == newVal) {
226 ALOGV("+++ byte at %p is already 0x%02x", addr, newVal);
231 * We're not holding this for long, so we don't bother with switching
234 dvmLockMutex(&pDvmDex->modLock);
236 ALOGV("+++ change byte at %p from 0x%02x to 0x%02x", addr, *addr, newVal);
237 if (sysChangeMapAccess(addr, 1, true, &pDvmDex->memMap) != 0) {
238 ALOGD("NOTE: DEX page access change (->RW) failed");
239 /* expected on files mounted from FAT; keep going (may crash) */
244 if (sysChangeMapAccess(addr, 1, false, &pDvmDex->memMap) != 0) {
245 ALOGD("NOTE: DEX page access change (->RO) failed");
246 /* expected on files mounted from FAT; keep going */
249 dvmUnlockMutex(&pDvmDex->modLock);
255 * Change the 2-byte value at the specified address to a new value. If the
256 * location already has the new value, do nothing.
258 * Otherwise works like dvmDexChangeDex1.
260 bool dvmDexChangeDex2(DvmDex* pDvmDex, u2* addr, u2 newVal)
262 if (*addr == newVal) {
263 ALOGV("+++ value at %p is already 0x%04x", addr, newVal);
268 * We're not holding this for long, so we don't bother with switching
271 dvmLockMutex(&pDvmDex->modLock);
273 ALOGV("+++ change 2byte at %p from 0x%04x to 0x%04x", addr, *addr, newVal);
274 if (sysChangeMapAccess(addr, 2, true, &pDvmDex->memMap) != 0) {
275 ALOGD("NOTE: DEX page access change (->RW) failed");
276 /* expected on files mounted from FAT; keep going (may crash) */
281 if (sysChangeMapAccess(addr, 2, false, &pDvmDex->memMap) != 0) {
282 ALOGD("NOTE: DEX page access change (->RO) failed");
283 /* expected on files mounted from FAT; keep going */
286 dvmUnlockMutex(&pDvmDex->modLock);