DexPrepare.cpp revision 6f9edbe7d3a83f7741c4d56f9f5be1414ecabc9f
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 * Prepare a DEX file for use by the VM. Depending upon the VM options 19 * we will attempt to verify and/or optimize the code, possibly appending 20 * register maps. 21 * 22 * TODO: the format of the optimized header is currently "whatever we 23 * happen to write", since the VM that writes it is by definition the same 24 * as the VM that reads it. Still, it should be better documented and 25 * more rigorously structured. 26 */ 27#include "Dalvik.h" 28#include "libdex/OptInvocation.h" 29#include "analysis/RegisterMap.h" 30#include "analysis/Optimize.h" 31 32#include <zlib.h> 33 34#include <libgen.h> 35#include <stdlib.h> 36#include <unistd.h> 37#include <sys/mman.h> 38#include <sys/stat.h> 39#include <sys/file.h> 40#include <sys/stat.h> 41#include <sys/types.h> 42#include <sys/wait.h> 43#include <fcntl.h> 44#include <errno.h> 45#include <unistd.h> 46 47 48/* fwd */ 49static bool rewriteDex(u1* addr, int len, bool doVerify, bool doOpt, 50 DexClassLookup** ppClassLookup, DvmDex** ppDvmDex); 51static bool loadAllClasses(DvmDex* pDvmDex); 52static void verifyAndOptimizeClasses(DexFile* pDexFile, bool doVerify, 53 bool doOpt); 54static void verifyAndOptimizeClass(DexFile* pDexFile, ClassObject* clazz, 55 const DexClassDef* pClassDef, bool doVerify, bool doOpt); 56static void updateChecksum(u1* addr, int len, DexHeader* pHeader); 57static int writeDependencies(int fd, u4 modWhen, u4 crc); 58static bool writeOptData(int fd, const DexClassLookup* pClassLookup,\ 59 const RegisterMapBuilder* pRegMapBuilder); 60static bool computeFileChecksum(int fd, off_t start, size_t length, u4* pSum); 61 62/* 63 * Get just the directory portion of the given path. This is just like 64 * dirname(), except it (a) never modifies its argument and (b) always 65 * returns allocated storage that must subsequently be free()d. 66 */ 67static char* saneDirName(const char* fileName) { 68 const char* lastSlash = strrchr(fileName, '/'); 69 70 if (lastSlash == NULL) { 71 return strdup("."); // strdup() to make free() always be appropriate. 72 } 73 74 size_t length = lastSlash - fileName + 1; // +1 for the '\0' byte. 75 char* result = (char*) malloc(length); 76 77 if (result != NULL) { 78 strlcpy(result, fileName, length); 79 } 80 81 return result; 82} 83 84/* 85 * Helper for dvmOpenCacheDexFile() in a known-error case: Check to 86 * see if the directory part of the given path (all but the last 87 * component) exists and is writable. Complain to the log if not. 88 */ 89static bool directoryIsValid(const char* fileName) 90{ 91 char* dirName = saneDirName(fileName); 92 93 if (dirName == NULL) { 94 LOGE("Could not get directory name of dex cache file '%s': %s", fileName, strerror(errno)); 95 return false; 96 } 97 98 bool ok = true; 99 struct stat status; 100 101 if (stat(dirName, &status) < 0) { 102 LOGE("Could not stat dex cache directory '%s': %s", dirName, strerror(errno)); 103 ok = false; 104 } 105 106 if (ok && !S_ISDIR(status.st_mode)) { 107 LOGE("Dex cache directory isn't a directory: %s", dirName); 108 ok = false; 109 } 110 111 if (ok && access(dirName, R_OK) < 0) { 112 LOGE("Dex cache directory isn't readable: %s", dirName); 113 ok = false; 114 } 115 116 if (ok && access(dirName, W_OK) < 0) { 117 LOGE("Dex cache directory isn't writable: %s", dirName); 118 ok = false; 119 } 120 121 free(dirName); 122 return ok; 123} 124 125/* 126 * Return the fd of an open file in the DEX file cache area. If the cache 127 * file doesn't exist or is out of date, this will remove the old entry, 128 * create a new one (writing only the file header), and return with the 129 * "new file" flag set. 130 * 131 * It's possible to execute from an unoptimized DEX file directly, 132 * assuming the byte ordering and structure alignment is correct, but 133 * disadvantageous because some significant optimizations are not possible. 134 * It's not generally possible to do the same from an uncompressed Jar 135 * file entry, because we have to guarantee 32-bit alignment in the 136 * memory-mapped file. 137 * 138 * For a Jar/APK file (a zip archive with "classes.dex" inside), "modWhen" 139 * and "crc32" come from the Zip directory entry. For a stand-alone DEX 140 * file, it's the modification date of the file and the Adler32 from the 141 * DEX header (which immediately follows the magic). If these don't 142 * match what's stored in the opt header, we reject the file immediately. 143 * 144 * On success, the file descriptor will be positioned just past the "opt" 145 * file header, and will be locked with flock. "*pCachedName" will point 146 * to newly-allocated storage. 147 */ 148int dvmOpenCachedDexFile(const char* fileName, const char* cacheFileName, 149 u4 modWhen, u4 crc, bool isBootstrap, bool* pNewFile, bool createIfMissing) 150{ 151 int fd, cc; 152 struct stat fdStat, fileStat; 153 bool readOnly = false; 154 155 *pNewFile = false; 156 157retry: 158 /* 159 * Try to open the cache file. If we've been asked to, 160 * create it if it doesn't exist. 161 */ 162 fd = createIfMissing ? open(cacheFileName, O_CREAT|O_RDWR, 0644) : -1; 163 if (fd < 0) { 164 fd = open(cacheFileName, O_RDONLY, 0); 165 if (fd < 0) { 166 if (createIfMissing) { 167 const char* errnoString = strerror(errno); 168 if (directoryIsValid(cacheFileName)) { 169 LOGE("Can't open dex cache file '%s': %s", cacheFileName, errnoString); 170 } 171 } 172 return fd; 173 } 174 readOnly = true; 175 } else { 176 fchmod(fd, 0644); 177 } 178 179 /* 180 * Grab an exclusive lock on the cache file. If somebody else is 181 * working on it, we'll block here until they complete. Because 182 * we're waiting on an external resource, we go into VMWAIT mode. 183 */ 184 LOGV("DexOpt: locking cache file %s (fd=%d, boot=%d)", 185 cacheFileName, fd, isBootstrap); 186 ThreadStatus oldStatus = dvmChangeStatus(NULL, THREAD_VMWAIT); 187 cc = flock(fd, LOCK_EX | LOCK_NB); 188 if (cc != 0) { 189 LOGD("DexOpt: sleeping on flock(%s)", cacheFileName); 190 cc = flock(fd, LOCK_EX); 191 } 192 dvmChangeStatus(NULL, oldStatus); 193 if (cc != 0) { 194 LOGE("Can't lock dex cache '%s': %d", cacheFileName, cc); 195 close(fd); 196 return -1; 197 } 198 LOGV("DexOpt: locked cache file"); 199 200 /* 201 * Check to see if the fd we opened and locked matches the file in 202 * the filesystem. If they don't, then somebody else unlinked ours 203 * and created a new file, and we need to use that one instead. (If 204 * we caught them between the unlink and the create, we'll get an 205 * ENOENT from the file stat.) 206 */ 207 cc = fstat(fd, &fdStat); 208 if (cc != 0) { 209 LOGE("Can't stat open file '%s'", cacheFileName); 210 LOGVV("DexOpt: unlocking cache file %s", cacheFileName); 211 goto close_fail; 212 } 213 cc = stat(cacheFileName, &fileStat); 214 if (cc != 0 || 215 fdStat.st_dev != fileStat.st_dev || fdStat.st_ino != fileStat.st_ino) 216 { 217 LOGD("DexOpt: our open cache file is stale; sleeping and retrying"); 218 LOGVV("DexOpt: unlocking cache file %s", cacheFileName); 219 flock(fd, LOCK_UN); 220 close(fd); 221 usleep(250 * 1000); /* if something is hosed, don't peg machine */ 222 goto retry; 223 } 224 225 /* 226 * We have the correct file open and locked. If the file size is zero, 227 * then it was just created by us, and we want to fill in some fields 228 * in the "opt" header and set "*pNewFile". Otherwise, we want to 229 * verify that the fields in the header match our expectations, and 230 * reset the file if they don't. 231 */ 232 if (fdStat.st_size == 0) { 233 if (readOnly) { 234 LOGW("DexOpt: file has zero length and isn't writable"); 235 goto close_fail; 236 } 237 cc = dexOptCreateEmptyHeader(fd); 238 if (cc != 0) 239 goto close_fail; 240 *pNewFile = true; 241 LOGV("DexOpt: successfully initialized new cache file"); 242 } else { 243 bool expectVerify, expectOpt; 244 245 if (gDvm.classVerifyMode == VERIFY_MODE_NONE) { 246 expectVerify = false; 247 } else if (gDvm.classVerifyMode == VERIFY_MODE_REMOTE) { 248 expectVerify = !isBootstrap; 249 } else /*if (gDvm.classVerifyMode == VERIFY_MODE_ALL)*/ { 250 expectVerify = true; 251 } 252 253 if (gDvm.dexOptMode == OPTIMIZE_MODE_NONE) { 254 expectOpt = false; 255 } else if (gDvm.dexOptMode == OPTIMIZE_MODE_VERIFIED || 256 gDvm.dexOptMode == OPTIMIZE_MODE_FULL) { 257 expectOpt = expectVerify; 258 } else /*if (gDvm.dexOptMode == OPTIMIZE_MODE_ALL)*/ { 259 expectOpt = true; 260 } 261 262 LOGV("checking deps, expecting vfy=%d opt=%d", 263 expectVerify, expectOpt); 264 265 if (!dvmCheckOptHeaderAndDependencies(fd, true, modWhen, crc, 266 expectVerify, expectOpt)) 267 { 268 if (readOnly) { 269 /* 270 * We could unlink and rewrite the file if we own it or 271 * the "sticky" bit isn't set on the directory. However, 272 * we're not able to truncate it, which spoils things. So, 273 * give up now. 274 */ 275 if (createIfMissing) { 276 LOGW("Cached DEX '%s' (%s) is stale and not writable", 277 fileName, cacheFileName); 278 } 279 goto close_fail; 280 } 281 282 /* 283 * If we truncate the existing file before unlinking it, any 284 * process that has it mapped will fail when it tries to touch 285 * the pages. 286 * 287 * This is very important. The zygote process will have the 288 * boot DEX files (core, framework, etc.) mapped early. If 289 * (say) core.dex gets updated, and somebody launches an app 290 * that uses App.dex, then App.dex gets reoptimized because it's 291 * dependent upon the boot classes. However, dexopt will be 292 * using the *new* core.dex to do the optimizations, while the 293 * app will actually be running against the *old* core.dex 294 * because it starts from zygote. 295 * 296 * Even without zygote, it's still possible for a class loader 297 * to pull in an APK that was optimized against an older set 298 * of DEX files. We must ensure that everything fails when a 299 * boot DEX gets updated, and for general "why aren't my 300 * changes doing anything" purposes its best if we just make 301 * everything crash when a DEX they're using gets updated. 302 */ 303 LOGD("ODEX file is stale or bad; removing and retrying (%s)", 304 cacheFileName); 305 if (ftruncate(fd, 0) != 0) { 306 LOGW("Warning: unable to truncate cache file '%s': %s", 307 cacheFileName, strerror(errno)); 308 /* keep going */ 309 } 310 if (unlink(cacheFileName) != 0) { 311 LOGW("Warning: unable to remove cache file '%s': %d %s", 312 cacheFileName, errno, strerror(errno)); 313 /* keep going; permission failure should probably be fatal */ 314 } 315 LOGVV("DexOpt: unlocking cache file %s", cacheFileName); 316 flock(fd, LOCK_UN); 317 close(fd); 318 goto retry; 319 } else { 320 LOGV("DexOpt: good deps in cache file"); 321 } 322 } 323 324 assert(fd >= 0); 325 return fd; 326 327close_fail: 328 flock(fd, LOCK_UN); 329 close(fd); 330 return -1; 331} 332 333/* 334 * Unlock the file descriptor. 335 * 336 * Returns "true" on success. 337 */ 338bool dvmUnlockCachedDexFile(int fd) 339{ 340 LOGVV("DexOpt: unlocking cache file fd=%d", fd); 341 return (flock(fd, LOCK_UN) == 0); 342} 343 344 345/* 346 * Given a descriptor for a file with DEX data in it, produce an 347 * optimized version. 348 * 349 * The file pointed to by "fd" is expected to be a locked shared resource 350 * (or private); we make no efforts to enforce multi-process correctness 351 * here. 352 * 353 * "fileName" is only used for debug output. "modWhen" and "crc" are stored 354 * in the dependency set. 355 * 356 * The "isBootstrap" flag determines how the optimizer and verifier handle 357 * package-scope access checks. When optimizing, we only load the bootstrap 358 * class DEX files and the target DEX, so the flag determines whether the 359 * target DEX classes are given a (synthetic) non-NULL classLoader pointer. 360 * This only really matters if the target DEX contains classes that claim to 361 * be in the same package as bootstrap classes. 362 * 363 * The optimizer will need to load every class in the target DEX file. 364 * This is generally undesirable, so we start a subprocess to do the 365 * work and wait for it to complete. 366 * 367 * Returns "true" on success. All data will have been written to "fd". 368 */ 369bool dvmOptimizeDexFile(int fd, off_t dexOffset, long dexLength, 370 const char* fileName, u4 modWhen, u4 crc, bool isBootstrap) 371{ 372 const char* lastPart = strrchr(fileName, '/'); 373 if (lastPart != NULL) 374 lastPart++; 375 else 376 lastPart = fileName; 377 378 LOGD("DexOpt: --- BEGIN '%s' (bootstrap=%d) ---", lastPart, isBootstrap); 379 380 pid_t pid; 381 382 /* 383 * This could happen if something in our bootclasspath, which we thought 384 * was all optimized, got rejected. 385 */ 386 if (gDvm.optimizing) { 387 LOGW("Rejecting recursive optimization attempt on '%s'", fileName); 388 return false; 389 } 390 391 pid = fork(); 392 if (pid == 0) { 393 static const int kUseValgrind = 0; 394 static const char* kDexOptBin = "/bin/dexopt"; 395 static const char* kValgrinder = "/usr/bin/valgrind"; 396 static const int kFixedArgCount = 10; 397 static const int kValgrindArgCount = 5; 398 static const int kMaxIntLen = 12; // '-'+10dig+'\0' -OR- 0x+8dig 399 int bcpSize = dvmGetBootPathSize(); 400 int argc = kFixedArgCount + bcpSize 401 + (kValgrindArgCount * kUseValgrind); 402 const char* argv[argc+1]; // last entry is NULL 403 char values[argc][kMaxIntLen]; 404 char* execFile; 405 const char* androidRoot; 406 int flags; 407 408 /* change process groups, so we don't clash with ProcessManager */ 409 setpgid(0, 0); 410 411 /* full path to optimizer */ 412 androidRoot = getenv("ANDROID_ROOT"); 413 if (androidRoot == NULL) { 414 LOGW("ANDROID_ROOT not set, defaulting to /system"); 415 androidRoot = "/system"; 416 } 417 execFile = (char*)malloc(strlen(androidRoot) + strlen(kDexOptBin) + 1); 418 strcpy(execFile, androidRoot); 419 strcat(execFile, kDexOptBin); 420 421 /* 422 * Create arg vector. 423 */ 424 int curArg = 0; 425 426 if (kUseValgrind) { 427 /* probably shouldn't ship the hard-coded path */ 428 argv[curArg++] = (char*)kValgrinder; 429 argv[curArg++] = "--tool=memcheck"; 430 argv[curArg++] = "--leak-check=yes"; // check for leaks too 431 argv[curArg++] = "--leak-resolution=med"; // increase from 2 to 4 432 argv[curArg++] = "--num-callers=16"; // default is 12 433 assert(curArg == kValgrindArgCount); 434 } 435 argv[curArg++] = execFile; 436 437 argv[curArg++] = "--dex"; 438 439 sprintf(values[2], "%d", DALVIK_VM_BUILD); 440 argv[curArg++] = values[2]; 441 442 sprintf(values[3], "%d", fd); 443 argv[curArg++] = values[3]; 444 445 sprintf(values[4], "%d", (int) dexOffset); 446 argv[curArg++] = values[4]; 447 448 sprintf(values[5], "%d", (int) dexLength); 449 argv[curArg++] = values[5]; 450 451 argv[curArg++] = (char*)fileName; 452 453 sprintf(values[7], "%d", (int) modWhen); 454 argv[curArg++] = values[7]; 455 456 sprintf(values[8], "%d", (int) crc); 457 argv[curArg++] = values[8]; 458 459 flags = 0; 460 if (gDvm.dexOptMode != OPTIMIZE_MODE_NONE) { 461 flags |= DEXOPT_OPT_ENABLED; 462 if (gDvm.dexOptMode == OPTIMIZE_MODE_ALL) 463 flags |= DEXOPT_OPT_ALL; 464 } 465 if (gDvm.classVerifyMode != VERIFY_MODE_NONE) { 466 flags |= DEXOPT_VERIFY_ENABLED; 467 if (gDvm.classVerifyMode == VERIFY_MODE_ALL) 468 flags |= DEXOPT_VERIFY_ALL; 469 } 470 if (isBootstrap) 471 flags |= DEXOPT_IS_BOOTSTRAP; 472 if (gDvm.generateRegisterMaps) 473 flags |= DEXOPT_GEN_REGISTER_MAPS; 474 sprintf(values[9], "%d", flags); 475 argv[curArg++] = values[9]; 476 477 assert(((!kUseValgrind && curArg == kFixedArgCount) || 478 ((kUseValgrind && curArg == kFixedArgCount+kValgrindArgCount)))); 479 480 ClassPathEntry* cpe; 481 for (cpe = gDvm.bootClassPath; cpe->ptr != NULL; cpe++) { 482 argv[curArg++] = cpe->fileName; 483 } 484 assert(curArg == argc); 485 486 argv[curArg] = NULL; 487 488 if (kUseValgrind) 489 execv(kValgrinder, const_cast<char**>(argv)); 490 else 491 execv(execFile, const_cast<char**>(argv)); 492 493 LOGE("execv '%s'%s failed: %s", execFile, 494 kUseValgrind ? " [valgrind]" : "", strerror(errno)); 495 exit(1); 496 } else { 497 LOGV("DexOpt: waiting for verify+opt, pid=%d", (int) pid); 498 int status; 499 pid_t gotPid; 500 501 /* 502 * Wait for the optimization process to finish. We go into VMWAIT 503 * mode here so GC suspension won't have to wait for us. 504 */ 505 ThreadStatus oldStatus = dvmChangeStatus(NULL, THREAD_VMWAIT); 506 while (true) { 507 gotPid = waitpid(pid, &status, 0); 508 if (gotPid == -1 && errno == EINTR) { 509 LOGD("waitpid interrupted, retrying"); 510 } else { 511 break; 512 } 513 } 514 dvmChangeStatus(NULL, oldStatus); 515 if (gotPid != pid) { 516 LOGE("waitpid failed: wanted %d, got %d: %s", 517 (int) pid, (int) gotPid, strerror(errno)); 518 return false; 519 } 520 521 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 522 LOGD("DexOpt: --- END '%s' (success) ---", lastPart); 523 return true; 524 } else { 525 LOGW("DexOpt: --- END '%s' --- status=0x%04x, process failed", 526 lastPart, status); 527 return false; 528 } 529 } 530} 531 532/* 533 * Do the actual optimization. This is executed in the dexopt process. 534 * 535 * For best use of disk/memory, we want to extract once and perform 536 * optimizations in place. If the file has to expand or contract 537 * to match local structure padding/alignment expectations, we want 538 * to do the rewrite as part of the extract, rather than extracting 539 * into a temp file and slurping it back out. (The structure alignment 540 * is currently correct for all platforms, and this isn't expected to 541 * change, so we should be okay with having it already extracted.) 542 * 543 * Returns "true" on success. 544 */ 545bool dvmContinueOptimization(int fd, off_t dexOffset, long dexLength, 546 const char* fileName, u4 modWhen, u4 crc, bool isBootstrap) 547{ 548 DexClassLookup* pClassLookup = NULL; 549 RegisterMapBuilder* pRegMapBuilder = NULL; 550 551 assert(gDvm.optimizing); 552 553 LOGV("Continuing optimization (%s, isb=%d)", fileName, isBootstrap); 554 555 assert(dexOffset >= 0); 556 557 /* quick test so we don't blow up on empty file */ 558 if (dexLength < (int) sizeof(DexHeader)) { 559 LOGE("too small to be DEX"); 560 return false; 561 } 562 if (dexOffset < (int) sizeof(DexOptHeader)) { 563 LOGE("not enough room for opt header"); 564 return false; 565 } 566 567 bool result = false; 568 569 /* 570 * Drop this into a global so we don't have to pass it around. We could 571 * also add a field to DexFile, but since it only pertains to DEX 572 * creation that probably doesn't make sense. 573 */ 574 gDvm.optimizingBootstrapClass = isBootstrap; 575 576 { 577 /* 578 * Map the entire file (so we don't have to worry about page 579 * alignment). The expectation is that the output file contains 580 * our DEX data plus room for a small header. 581 */ 582 bool success; 583 void* mapAddr; 584 mapAddr = mmap(NULL, dexOffset + dexLength, PROT_READ|PROT_WRITE, 585 MAP_SHARED, fd, 0); 586 if (mapAddr == MAP_FAILED) { 587 LOGE("unable to mmap DEX cache: %s", strerror(errno)); 588 goto bail; 589 } 590 591 bool doVerify, doOpt; 592 if (gDvm.classVerifyMode == VERIFY_MODE_NONE) { 593 doVerify = false; 594 } else if (gDvm.classVerifyMode == VERIFY_MODE_REMOTE) { 595 doVerify = !gDvm.optimizingBootstrapClass; 596 } else /*if (gDvm.classVerifyMode == VERIFY_MODE_ALL)*/ { 597 doVerify = true; 598 } 599 600 if (gDvm.dexOptMode == OPTIMIZE_MODE_NONE) { 601 doOpt = false; 602 } else if (gDvm.dexOptMode == OPTIMIZE_MODE_VERIFIED || 603 gDvm.dexOptMode == OPTIMIZE_MODE_FULL) { 604 doOpt = doVerify; 605 } else /*if (gDvm.dexOptMode == OPTIMIZE_MODE_ALL)*/ { 606 doOpt = true; 607 } 608 609 /* 610 * Rewrite the file. Byte reordering, structure realigning, 611 * class verification, and bytecode optimization are all performed 612 * here. 613 * 614 * In theory the file could change size and bits could shift around. 615 * In practice this would be annoying to deal with, so the file 616 * layout is designed so that it can always be rewritten in place. 617 * 618 * This creates the class lookup table as part of doing the processing. 619 */ 620 success = rewriteDex(((u1*) mapAddr) + dexOffset, dexLength, 621 doVerify, doOpt, &pClassLookup, NULL); 622 623 if (success) { 624 DvmDex* pDvmDex = NULL; 625 u1* dexAddr = ((u1*) mapAddr) + dexOffset; 626 627 if (dvmDexFileOpenPartial(dexAddr, dexLength, &pDvmDex) != 0) { 628 LOGE("Unable to create DexFile"); 629 success = false; 630 } else { 631 /* 632 * If configured to do so, generate register map output 633 * for all verified classes. The register maps were 634 * generated during verification, and will now be serialized. 635 */ 636 if (gDvm.generateRegisterMaps) { 637 pRegMapBuilder = dvmGenerateRegisterMaps(pDvmDex); 638 if (pRegMapBuilder == NULL) { 639 LOGE("Failed generating register maps"); 640 success = false; 641 } 642 } 643 644 DexHeader* pHeader = (DexHeader*)pDvmDex->pHeader; 645 updateChecksum(dexAddr, dexLength, pHeader); 646 647 dvmDexFileFree(pDvmDex); 648 } 649 } 650 651 /* unmap the read-write version, forcing writes to disk */ 652 if (msync(mapAddr, dexOffset + dexLength, MS_SYNC) != 0) { 653 LOGW("msync failed: %s", strerror(errno)); 654 // weird, but keep going 655 } 656#if 1 657 /* 658 * This causes clean shutdown to fail, because we have loaded classes 659 * that point into it. For the optimizer this isn't a problem, 660 * because it's more efficient for the process to simply exit. 661 * Exclude this code when doing clean shutdown for valgrind. 662 */ 663 if (munmap(mapAddr, dexOffset + dexLength) != 0) { 664 LOGE("munmap failed: %s", strerror(errno)); 665 goto bail; 666 } 667#endif 668 669 if (!success) 670 goto bail; 671 } 672 673 /* get start offset, and adjust deps start for 64-bit alignment */ 674 off_t depsOffset, optOffset, endOffset, adjOffset; 675 int depsLength, optLength; 676 u4 optChecksum; 677 678 depsOffset = lseek(fd, 0, SEEK_END); 679 if (depsOffset < 0) { 680 LOGE("lseek to EOF failed: %s", strerror(errno)); 681 goto bail; 682 } 683 adjOffset = (depsOffset + 7) & ~(0x07); 684 if (adjOffset != depsOffset) { 685 LOGV("Adjusting deps start from %d to %d", 686 (int) depsOffset, (int) adjOffset); 687 depsOffset = adjOffset; 688 lseek(fd, depsOffset, SEEK_SET); 689 } 690 691 /* 692 * Append the dependency list. 693 */ 694 if (writeDependencies(fd, modWhen, crc) != 0) { 695 LOGW("Failed writing dependencies"); 696 goto bail; 697 } 698 699 /* compute deps length, then adjust opt start for 64-bit alignment */ 700 optOffset = lseek(fd, 0, SEEK_END); 701 depsLength = optOffset - depsOffset; 702 703 adjOffset = (optOffset + 7) & ~(0x07); 704 if (adjOffset != optOffset) { 705 LOGV("Adjusting opt start from %d to %d", 706 (int) optOffset, (int) adjOffset); 707 optOffset = adjOffset; 708 lseek(fd, optOffset, SEEK_SET); 709 } 710 711 /* 712 * Append any optimized pre-computed data structures. 713 */ 714 if (!writeOptData(fd, pClassLookup, pRegMapBuilder)) { 715 LOGW("Failed writing opt data"); 716 goto bail; 717 } 718 719 endOffset = lseek(fd, 0, SEEK_END); 720 optLength = endOffset - optOffset; 721 722 /* compute checksum from start of deps to end of opt area */ 723 if (!computeFileChecksum(fd, depsOffset, 724 (optOffset+optLength) - depsOffset, &optChecksum)) 725 { 726 goto bail; 727 } 728 729 /* 730 * Output the "opt" header with all values filled in and a correct 731 * magic number. 732 */ 733 DexOptHeader optHdr; 734 memset(&optHdr, 0xff, sizeof(optHdr)); 735 memcpy(optHdr.magic, DEX_OPT_MAGIC, 4); 736 memcpy(optHdr.magic+4, DEX_OPT_MAGIC_VERS, 4); 737 optHdr.dexOffset = (u4) dexOffset; 738 optHdr.dexLength = (u4) dexLength; 739 optHdr.depsOffset = (u4) depsOffset; 740 optHdr.depsLength = (u4) depsLength; 741 optHdr.optOffset = (u4) optOffset; 742 optHdr.optLength = (u4) optLength; 743#if __BYTE_ORDER != __LITTLE_ENDIAN 744 optHdr.flags = DEX_OPT_FLAG_BIG; 745#else 746 optHdr.flags = 0; 747#endif 748 optHdr.checksum = optChecksum; 749 750 fsync(fd); /* ensure previous writes go before header is written */ 751 752 lseek(fd, 0, SEEK_SET); 753 if (sysWriteFully(fd, &optHdr, sizeof(optHdr), "DexOpt opt header") != 0) 754 goto bail; 755 756 LOGV("Successfully wrote DEX header"); 757 result = true; 758 759 //dvmRegisterMapDumpStats(); 760 761bail: 762 dvmFreeRegisterMapBuilder(pRegMapBuilder); 763 free(pClassLookup); 764 return result; 765} 766 767/* 768 * Prepare an in-memory DEX file. 769 * 770 * The data was presented to the VM as a byte array rather than a file. 771 * We want to do the same basic set of operations, but we can just leave 772 * them in memory instead of writing them out to a cached optimized DEX file. 773 */ 774bool dvmPrepareDexInMemory(u1* addr, size_t len, DvmDex** ppDvmDex) 775{ 776 DexClassLookup* pClassLookup = NULL; 777 778 /* 779 * Byte-swap, realign, verify basic DEX file structure. 780 * 781 * We could load + verify + optimize here as well, but that's probably 782 * not desirable. 783 * 784 * (The bulk-verification code is currently only setting the DEX 785 * file's "verified" flag, not updating the ClassObject. This would 786 * also need to be changed, or we will try to verify the class twice, 787 * and possibly reject it when optimized opcodes are encountered.) 788 */ 789 if (!rewriteDex(addr, len, false, false, &pClassLookup, ppDvmDex)) { 790 return false; 791 } 792 793 (*ppDvmDex)->pDexFile->pClassLookup = pClassLookup; 794 795 return true; 796} 797 798/* 799 * Perform in-place rewrites on a memory-mapped DEX file. 800 * 801 * If this is called from a short-lived child process (dexopt), we can 802 * go nutty with loading classes and allocating memory. When it's 803 * called to prepare classes provided in a byte array, we may want to 804 * be more conservative. 805 * 806 * If "ppClassLookup" is non-NULL, a pointer to a newly-allocated 807 * DexClassLookup will be returned on success. 808 * 809 * If "ppDvmDex" is non-NULL, a newly-allocated DvmDex struct will be 810 * returned on success. 811 */ 812static bool rewriteDex(u1* addr, int len, bool doVerify, bool doOpt, 813 DexClassLookup** ppClassLookup, DvmDex** ppDvmDex) 814{ 815 DexClassLookup* pClassLookup = NULL; 816 u8 prepWhen, loadWhen, verifyOptWhen; 817 DvmDex* pDvmDex = NULL; 818 bool result = false; 819 const char* msgStr = "???"; 820 821 /* if the DEX is in the wrong byte order, swap it now */ 822 if (dexSwapAndVerify(addr, len) != 0) 823 goto bail; 824 825 /* 826 * Now that the DEX file can be read directly, create a DexFile struct 827 * for it. 828 */ 829 if (dvmDexFileOpenPartial(addr, len, &pDvmDex) != 0) { 830 LOGE("Unable to create DexFile"); 831 goto bail; 832 } 833 834 /* 835 * Create the class lookup table. This will eventually be appended 836 * to the end of the .odex. 837 * 838 * We create a temporary link from the DexFile for the benefit of 839 * class loading, below. 840 */ 841 pClassLookup = dexCreateClassLookup(pDvmDex->pDexFile); 842 if (pClassLookup == NULL) 843 goto bail; 844 pDvmDex->pDexFile->pClassLookup = pClassLookup; 845 846 /* 847 * If we're not going to attempt to verify or optimize the classes, 848 * there's no value in loading them, so bail out early. 849 */ 850 if (!doVerify && !doOpt) { 851 result = true; 852 goto bail; 853 } 854 855 prepWhen = dvmGetRelativeTimeUsec(); 856 857 /* 858 * Load all classes found in this DEX file. If they fail to load for 859 * some reason, they won't get verified (which is as it should be). 860 */ 861 if (!loadAllClasses(pDvmDex)) 862 goto bail; 863 loadWhen = dvmGetRelativeTimeUsec(); 864 865 /* 866 * Create a data structure for use by the bytecode optimizer. 867 * We need to look up methods in a few classes, so this may cause 868 * a bit of class loading. We usually do this during VM init, but 869 * for dexopt on core.jar the order of operations gets a bit tricky, 870 * so we defer it to here. 871 */ 872 if (!dvmCreateInlineSubsTable()) 873 goto bail; 874 875 /* 876 * Verify and optimize all classes in the DEX file (command-line 877 * options permitting). 878 * 879 * This is best-effort, so there's really no way for dexopt to 880 * fail at this point. 881 */ 882 verifyAndOptimizeClasses(pDvmDex->pDexFile, doVerify, doOpt); 883 verifyOptWhen = dvmGetRelativeTimeUsec(); 884 885 if (doVerify && doOpt) 886 msgStr = "verify+opt"; 887 else if (doVerify) 888 msgStr = "verify"; 889 else if (doOpt) 890 msgStr = "opt"; 891 LOGD("DexOpt: load %dms, %s %dms", 892 (int) (loadWhen - prepWhen) / 1000, 893 msgStr, 894 (int) (verifyOptWhen - loadWhen) / 1000); 895 896 result = true; 897 898bail: 899 /* 900 * On success, return the pieces that the caller asked for. 901 */ 902 if (ppDvmDex == NULL || !result) { 903 dvmDexFileFree(pDvmDex); 904 } else { 905 *ppDvmDex = pDvmDex; 906 } 907 908 if (ppClassLookup == NULL || !result) { 909 free(pClassLookup); 910 } else { 911 *ppClassLookup = pClassLookup; 912 } 913 914 /* break link between the two */ 915 pDvmDex->pDexFile->pClassLookup = NULL; 916 917 return result; 918} 919 920/* 921 * Try to load all classes in the specified DEX. If they have some sort 922 * of broken dependency, e.g. their superclass lives in a different DEX 923 * that wasn't previously loaded into the bootstrap class path, loading 924 * will fail. This is the desired behavior. 925 * 926 * We have no notion of class loader at this point, so we load all of 927 * the classes with the bootstrap class loader. It turns out this has 928 * exactly the behavior we want, and has no ill side effects because we're 929 * running in a separate process and anything we load here will be forgotten. 930 * 931 * We set the CLASS_MULTIPLE_DEFS flag here if we see multiple definitions. 932 * This works because we only call here as part of optimization / pre-verify, 933 * not during verification as part of loading a class into a running VM. 934 * 935 * This returns "false" if the world is too screwed up to do anything 936 * useful at all. 937 */ 938static bool loadAllClasses(DvmDex* pDvmDex) 939{ 940 u4 count = pDvmDex->pDexFile->pHeader->classDefsSize; 941 u4 idx; 942 int loaded = 0; 943 944 LOGV("DexOpt: +++ trying to load %d classes", count); 945 946 dvmSetBootPathExtraDex(pDvmDex); 947 948 /* 949 * At this point, it is safe -- and necessary! -- to look up the 950 * VM's required classes and members, even when what we are in the 951 * process of processing is the core library that defines these 952 * classes itself. (The reason it is necessary is that in the act 953 * of initializing the class Class, below, the system will end up 954 * referring to many of the class references that got set up by 955 * this call.) 956 */ 957 if (!dvmFindRequiredClassesAndMembers()) { 958 return false; 959 } 960 961 /* 962 * We have some circularity issues with Class and Object that are 963 * most easily avoided by ensuring that Object is never the first 964 * thing we try to find-and-initialize. The call to 965 * dvmFindSystemClass() here takes care of that situation. (We 966 * only need to do this when loading classes from the DEX file 967 * that contains Object, and only when Object comes first in the 968 * list, but it costs very little to do it in all cases.) 969 */ 970 if (!dvmInitClass(gDvm.classJavaLangClass)) { 971 LOGE("ERROR: failed to initialize the class Class!"); 972 return false; 973 } 974 975 for (idx = 0; idx < count; idx++) { 976 const DexClassDef* pClassDef; 977 const char* classDescriptor; 978 ClassObject* newClass; 979 980 pClassDef = dexGetClassDef(pDvmDex->pDexFile, idx); 981 classDescriptor = 982 dexStringByTypeIdx(pDvmDex->pDexFile, pClassDef->classIdx); 983 984 LOGV("+++ loading '%s'", classDescriptor); 985 //newClass = dvmDefineClass(pDexFile, classDescriptor, 986 // NULL); 987 newClass = dvmFindSystemClassNoInit(classDescriptor); 988 if (newClass == NULL) { 989 LOGV("DexOpt: failed loading '%s'", classDescriptor); 990 dvmClearOptException(dvmThreadSelf()); 991 } else if (newClass->pDvmDex != pDvmDex) { 992 /* 993 * We don't load the new one, and we tag the first one found 994 * with the "multiple def" flag so the resolver doesn't try 995 * to make it available. 996 */ 997 LOGD("DexOpt: '%s' has an earlier definition; blocking out", 998 classDescriptor); 999 SET_CLASS_FLAG(newClass, CLASS_MULTIPLE_DEFS); 1000 } else { 1001 loaded++; 1002 } 1003 } 1004 LOGV("DexOpt: +++ successfully loaded %d classes", loaded); 1005 1006 dvmSetBootPathExtraDex(NULL); 1007 return true; 1008} 1009 1010/* 1011 * Verify and/or optimize all classes that were successfully loaded from 1012 * this DEX file. 1013 */ 1014static void verifyAndOptimizeClasses(DexFile* pDexFile, bool doVerify, 1015 bool doOpt) 1016{ 1017 u4 count = pDexFile->pHeader->classDefsSize; 1018 u4 idx; 1019 1020 for (idx = 0; idx < count; idx++) { 1021 const DexClassDef* pClassDef; 1022 const char* classDescriptor; 1023 ClassObject* clazz; 1024 1025 pClassDef = dexGetClassDef(pDexFile, idx); 1026 classDescriptor = dexStringByTypeIdx(pDexFile, pClassDef->classIdx); 1027 1028 /* all classes are loaded into the bootstrap class loader */ 1029 clazz = dvmLookupClass(classDescriptor, NULL, false); 1030 if (clazz != NULL) { 1031 verifyAndOptimizeClass(pDexFile, clazz, pClassDef, doVerify, doOpt); 1032 1033 } else { 1034 // TODO: log when in verbose mode 1035 LOGV("DexOpt: not optimizing unavailable class '%s'", 1036 classDescriptor); 1037 } 1038 } 1039 1040#ifdef VERIFIER_STATS 1041 LOGI("Verifier stats:"); 1042 LOGI(" methods examined : %u", gDvm.verifierStats.methodsExamined); 1043 LOGI(" monitor-enter methods : %u", gDvm.verifierStats.monEnterMethods); 1044 LOGI(" instructions examined : %u", gDvm.verifierStats.instrsExamined); 1045 LOGI(" instructions re-examined: %u", gDvm.verifierStats.instrsReexamined); 1046 LOGI(" copying of register sets: %u", gDvm.verifierStats.copyRegCount); 1047 LOGI(" merging of register sets: %u", gDvm.verifierStats.mergeRegCount); 1048 LOGI(" ...that caused changes : %u", gDvm.verifierStats.mergeRegChanged); 1049 LOGI(" uninit searches : %u", gDvm.verifierStats.uninitSearches); 1050 LOGI(" max memory required : %u", gDvm.verifierStats.biggestAlloc); 1051#endif 1052} 1053 1054/* 1055 * Verify and/or optimize a specific class. 1056 */ 1057static void verifyAndOptimizeClass(DexFile* pDexFile, ClassObject* clazz, 1058 const DexClassDef* pClassDef, bool doVerify, bool doOpt) 1059{ 1060 const char* classDescriptor; 1061 bool verified = false; 1062 1063 if (clazz->pDvmDex->pDexFile != pDexFile) { 1064 /* 1065 * The current DEX file defined a class that is also present in the 1066 * bootstrap class path. The class loader favored the bootstrap 1067 * version, which means that we have a pointer to a class that is 1068 * (a) not the one we want to examine, and (b) mapped read-only, 1069 * so we will seg fault if we try to rewrite instructions inside it. 1070 */ 1071 LOGD("DexOpt: not verifying/optimizing '%s': multiple definitions", 1072 clazz->descriptor); 1073 return; 1074 } 1075 1076 classDescriptor = dexStringByTypeIdx(pDexFile, pClassDef->classIdx); 1077 1078 /* 1079 * First, try to verify it. 1080 */ 1081 if (doVerify) { 1082 if (dvmVerifyClass(clazz)) { 1083 /* 1084 * Set the "is preverified" flag in the DexClassDef. We 1085 * do it here, rather than in the ClassObject structure, 1086 * because the DexClassDef is part of the odex file. 1087 */ 1088 assert((clazz->accessFlags & JAVA_FLAGS_MASK) == 1089 pClassDef->accessFlags); 1090 ((DexClassDef*)pClassDef)->accessFlags |= CLASS_ISPREVERIFIED; 1091 verified = true; 1092 } else { 1093 // TODO: log when in verbose mode 1094 LOGV("DexOpt: '%s' failed verification", classDescriptor); 1095 } 1096 } 1097 1098 if (doOpt) { 1099 bool needVerify = (gDvm.dexOptMode == OPTIMIZE_MODE_VERIFIED || 1100 gDvm.dexOptMode == OPTIMIZE_MODE_FULL); 1101 if (!verified && needVerify) { 1102 LOGV("DexOpt: not optimizing '%s': not verified", 1103 classDescriptor); 1104 } else { 1105 dvmOptimizeClass(clazz, false); 1106 1107 /* set the flag whether or not we actually changed anything */ 1108 ((DexClassDef*)pClassDef)->accessFlags |= CLASS_ISOPTIMIZED; 1109 } 1110 } 1111} 1112 1113 1114/* 1115 * Get the cache file name from a ClassPathEntry. 1116 */ 1117static const char* getCacheFileName(const ClassPathEntry* cpe) 1118{ 1119 switch (cpe->kind) { 1120 case kCpeJar: 1121 return dvmGetJarFileCacheFileName((JarFile*) cpe->ptr); 1122 case kCpeDex: 1123 return dvmGetRawDexFileCacheFileName((RawDexFile*) cpe->ptr); 1124 default: 1125 LOGE("DexOpt: unexpected cpe kind %d", cpe->kind); 1126 dvmAbort(); 1127 return NULL; 1128 } 1129} 1130 1131/* 1132 * Get the SHA-1 signature. 1133 */ 1134static const u1* getSignature(const ClassPathEntry* cpe) 1135{ 1136 DvmDex* pDvmDex; 1137 1138 switch (cpe->kind) { 1139 case kCpeJar: 1140 pDvmDex = dvmGetJarFileDex((JarFile*) cpe->ptr); 1141 break; 1142 case kCpeDex: 1143 pDvmDex = dvmGetRawDexFileDex((RawDexFile*) cpe->ptr); 1144 break; 1145 default: 1146 LOGE("unexpected cpe kind %d", cpe->kind); 1147 dvmAbort(); 1148 pDvmDex = NULL; // make gcc happy 1149 } 1150 1151 assert(pDvmDex != NULL); 1152 return pDvmDex->pDexFile->pHeader->signature; 1153} 1154 1155 1156/* 1157 * Dependency layout: 1158 * 4b Source file modification time, in seconds since 1970 UTC 1159 * 4b CRC-32 from Zip entry, or Adler32 from source DEX header 1160 * 4b Dalvik VM build number 1161 * 4b Number of dependency entries that follow 1162 * Dependency entries: 1163 * 4b Name length (including terminating null) 1164 * var Full path of cache entry (null terminated) 1165 * 20b SHA-1 signature from source DEX file 1166 * 1167 * If this changes, update DEX_OPT_MAGIC_VERS. 1168 */ 1169static const size_t kMinDepSize = 4 * 4; 1170static const size_t kMaxDepSize = 4 * 4 + 2048; // sanity check 1171 1172/* 1173 * Read the "opt" header, verify it, then read the dependencies section 1174 * and verify that data as well. 1175 * 1176 * If "sourceAvail" is "true", this will verify that "modWhen" and "crc" 1177 * match up with what is stored in the header. If they don't, we reject 1178 * the file so that it can be recreated from the updated original. If 1179 * "sourceAvail" isn't set, e.g. for a .odex file, we ignore these arguments. 1180 * 1181 * On successful return, the file will be seeked immediately past the 1182 * "opt" header. 1183 */ 1184bool dvmCheckOptHeaderAndDependencies(int fd, bool sourceAvail, u4 modWhen, 1185 u4 crc, bool expectVerify, bool expectOpt) 1186{ 1187 DexOptHeader optHdr; 1188 u1* depData = NULL; 1189 const u1* magic; 1190 off_t posn; 1191 int result = false; 1192 ssize_t actual; 1193 1194 /* 1195 * Start at the start. The "opt" header, when present, will always be 1196 * the first thing in the file. 1197 */ 1198 if (lseek(fd, 0, SEEK_SET) != 0) { 1199 LOGE("DexOpt: failed to seek to start of file: %s", strerror(errno)); 1200 goto bail; 1201 } 1202 1203 /* 1204 * Read and do trivial verification on the opt header. The header is 1205 * always in host byte order. 1206 */ 1207 actual = read(fd, &optHdr, sizeof(optHdr)); 1208 if (actual < 0) { 1209 LOGE("DexOpt: failed reading opt header: %s", strerror(errno)); 1210 goto bail; 1211 } else if (actual != sizeof(optHdr)) { 1212 LOGE("DexOpt: failed reading opt header (got %d of %zd)", 1213 (int) actual, sizeof(optHdr)); 1214 goto bail; 1215 } 1216 1217 magic = optHdr.magic; 1218 if (memcmp(magic, DEX_MAGIC, 4) == 0) { 1219 /* somebody probably pointed us at the wrong file */ 1220 LOGD("DexOpt: expected optimized DEX, found unoptimized"); 1221 goto bail; 1222 } else if (memcmp(magic, DEX_OPT_MAGIC, 4) != 0) { 1223 /* not a DEX file, or previous attempt was interrupted */ 1224 LOGD("DexOpt: incorrect opt magic number (0x%02x %02x %02x %02x)", 1225 magic[0], magic[1], magic[2], magic[3]); 1226 goto bail; 1227 } 1228 if (memcmp(magic+4, DEX_OPT_MAGIC_VERS, 4) != 0) { 1229 LOGW("DexOpt: stale opt version (0x%02x %02x %02x %02x)", 1230 magic[4], magic[5], magic[6], magic[7]); 1231 goto bail; 1232 } 1233 if (optHdr.depsLength < kMinDepSize || optHdr.depsLength > kMaxDepSize) { 1234 LOGW("DexOpt: weird deps length %d, bailing", optHdr.depsLength); 1235 goto bail; 1236 } 1237 1238 /* 1239 * Do the header flags match up with what we want? 1240 * 1241 * The only thing we really can't handle is incorrect byte ordering. 1242 */ 1243 { 1244 const u4 matchMask = DEX_OPT_FLAG_BIG; 1245 u4 expectedFlags = 0; 1246#if __BYTE_ORDER != __LITTLE_ENDIAN 1247 expectedFlags |= DEX_OPT_FLAG_BIG; 1248#endif 1249 if ((expectedFlags & matchMask) != (optHdr.flags & matchMask)) { 1250 LOGI("DexOpt: header flag mismatch (0x%02x vs 0x%02x, mask=0x%02x)", 1251 expectedFlags, optHdr.flags, matchMask); 1252 goto bail; 1253 } 1254 } 1255 1256 posn = lseek(fd, optHdr.depsOffset, SEEK_SET); 1257 if (posn < 0) { 1258 LOGW("DexOpt: seek to deps failed: %s", strerror(errno)); 1259 goto bail; 1260 } 1261 1262 /* 1263 * Read all of the dependency stuff into memory. 1264 */ 1265 depData = (u1*) malloc(optHdr.depsLength); 1266 if (depData == NULL) { 1267 LOGW("DexOpt: unable to allocate %d bytes for deps", 1268 optHdr.depsLength); 1269 goto bail; 1270 } 1271 actual = read(fd, depData, optHdr.depsLength); 1272 if (actual < 0) { 1273 LOGW("DexOpt: failed reading deps: %s", strerror(errno)); 1274 goto bail; 1275 } else if (actual != (ssize_t) optHdr.depsLength) { 1276 LOGW("DexOpt: failed reading deps: got %d of %d", 1277 (int) actual, optHdr.depsLength); 1278 goto bail; 1279 } 1280 1281 /* 1282 * Verify simple items. 1283 */ 1284 const u1* ptr; 1285 u4 val; 1286 1287 ptr = depData; 1288 val = read4LE(&ptr); 1289 if (sourceAvail && val != modWhen) { 1290 LOGI("DexOpt: source file mod time mismatch (%08x vs %08x)", 1291 val, modWhen); 1292 goto bail; 1293 } 1294 val = read4LE(&ptr); 1295 if (sourceAvail && val != crc) { 1296 LOGI("DexOpt: source file CRC mismatch (%08x vs %08x)", val, crc); 1297 goto bail; 1298 } 1299 val = read4LE(&ptr); 1300 if (val != DALVIK_VM_BUILD) { 1301 LOGD("DexOpt: VM build version mismatch (%d vs %d)", 1302 val, DALVIK_VM_BUILD); 1303 goto bail; 1304 } 1305 1306 /* 1307 * Verify dependencies on other cached DEX files. It must match 1308 * exactly with what is currently defined in the bootclasspath. 1309 */ 1310 ClassPathEntry* cpe; 1311 u4 numDeps; 1312 1313 numDeps = read4LE(&ptr); 1314 LOGV("+++ DexOpt: numDeps = %d", numDeps); 1315 for (cpe = gDvm.bootClassPath; cpe->ptr != NULL; cpe++) { 1316 const char* cacheFileName = 1317 dvmPathToAbsolutePortion(getCacheFileName(cpe)); 1318 assert(cacheFileName != NULL); /* guaranteed by Class.c */ 1319 1320 const u1* signature = getSignature(cpe); 1321 size_t len = strlen(cacheFileName) +1; 1322 u4 storedStrLen; 1323 1324 if (numDeps == 0) { 1325 /* more entries in bootclasspath than in deps list */ 1326 LOGI("DexOpt: not all deps represented"); 1327 goto bail; 1328 } 1329 1330 storedStrLen = read4LE(&ptr); 1331 if (len != storedStrLen || 1332 strcmp(cacheFileName, (const char*) ptr) != 0) 1333 { 1334 LOGI("DexOpt: mismatch dep name: '%s' vs. '%s'", 1335 cacheFileName, ptr); 1336 goto bail; 1337 } 1338 1339 ptr += storedStrLen; 1340 1341 if (memcmp(signature, ptr, kSHA1DigestLen) != 0) { 1342 LOGI("DexOpt: mismatch dep signature for '%s'", cacheFileName); 1343 goto bail; 1344 } 1345 ptr += kSHA1DigestLen; 1346 1347 LOGV("DexOpt: dep match on '%s'", cacheFileName); 1348 1349 numDeps--; 1350 } 1351 1352 if (numDeps != 0) { 1353 /* more entries in deps list than in classpath */ 1354 LOGI("DexOpt: Some deps went away"); 1355 goto bail; 1356 } 1357 1358 // consumed all data and no more? 1359 if (ptr != depData + optHdr.depsLength) { 1360 LOGW("DexOpt: Spurious dep data? %d vs %d", 1361 (int) (ptr - depData), optHdr.depsLength); 1362 assert(false); 1363 } 1364 1365 result = true; 1366 1367bail: 1368 free(depData); 1369 return result; 1370} 1371 1372/* 1373 * Write the dependency info to "fd" at the current file position. 1374 */ 1375static int writeDependencies(int fd, u4 modWhen, u4 crc) 1376{ 1377 u1* buf = NULL; 1378 int result = -1; 1379 ssize_t bufLen; 1380 ClassPathEntry* cpe; 1381 int numDeps; 1382 1383 /* 1384 * Count up the number of completed entries in the bootclasspath. 1385 */ 1386 numDeps = 0; 1387 bufLen = 0; 1388 for (cpe = gDvm.bootClassPath; cpe->ptr != NULL; cpe++) { 1389 const char* cacheFileName = 1390 dvmPathToAbsolutePortion(getCacheFileName(cpe)); 1391 assert(cacheFileName != NULL); /* guaranteed by Class.c */ 1392 1393 LOGV("+++ DexOpt: found dep '%s'", cacheFileName); 1394 1395 numDeps++; 1396 bufLen += strlen(cacheFileName) +1; 1397 } 1398 1399 bufLen += 4*4 + numDeps * (4+kSHA1DigestLen); 1400 1401 buf = (u1*)malloc(bufLen); 1402 1403 set4LE(buf+0, modWhen); 1404 set4LE(buf+4, crc); 1405 set4LE(buf+8, DALVIK_VM_BUILD); 1406 set4LE(buf+12, numDeps); 1407 1408 // TODO: do we want to add dvmGetInlineOpsTableLength() here? Won't 1409 // help us if somebody replaces an existing entry, but it'd catch 1410 // additions/removals. 1411 1412 u1* ptr = buf + 4*4; 1413 for (cpe = gDvm.bootClassPath; cpe->ptr != NULL; cpe++) { 1414 const char* cacheFileName = 1415 dvmPathToAbsolutePortion(getCacheFileName(cpe)); 1416 assert(cacheFileName != NULL); /* guaranteed by Class.c */ 1417 1418 const u1* signature = getSignature(cpe); 1419 int len = strlen(cacheFileName) +1; 1420 1421 if (ptr + 4 + len + kSHA1DigestLen > buf + bufLen) { 1422 LOGE("DexOpt: overran buffer"); 1423 dvmAbort(); 1424 } 1425 1426 set4LE(ptr, len); 1427 ptr += 4; 1428 memcpy(ptr, cacheFileName, len); 1429 ptr += len; 1430 memcpy(ptr, signature, kSHA1DigestLen); 1431 ptr += kSHA1DigestLen; 1432 } 1433 1434 assert(ptr == buf + bufLen); 1435 1436 result = sysWriteFully(fd, buf, bufLen, "DexOpt dep info"); 1437 1438 free(buf); 1439 return result; 1440} 1441 1442 1443/* 1444 * Write a block of data in "chunk" format. 1445 * 1446 * The chunk header fields are always in "native" byte order. If "size" 1447 * is not a multiple of 8 bytes, the data area is padded out. 1448 */ 1449static bool writeChunk(int fd, u4 type, const void* data, size_t size) 1450{ 1451 union { /* save a syscall by grouping these together */ 1452 char raw[8]; 1453 struct { 1454 u4 type; 1455 u4 size; 1456 } ts; 1457 } header; 1458 1459 assert(sizeof(header) == 8); 1460 1461 LOGV("Writing chunk, type=%.4s size=%d", (char*) &type, size); 1462 1463 header.ts.type = type; 1464 header.ts.size = (u4) size; 1465 if (sysWriteFully(fd, &header, sizeof(header), 1466 "DexOpt opt chunk header write") != 0) 1467 { 1468 return false; 1469 } 1470 1471 if (size > 0) { 1472 if (sysWriteFully(fd, data, size, "DexOpt opt chunk write") != 0) 1473 return false; 1474 } 1475 1476 /* if necessary, pad to 64-bit alignment */ 1477 if ((size & 7) != 0) { 1478 int padSize = 8 - (size & 7); 1479 LOGV("size was %d, inserting %d pad bytes", size, padSize); 1480 lseek(fd, padSize, SEEK_CUR); 1481 } 1482 1483 assert( ((int)lseek(fd, 0, SEEK_CUR) & 7) == 0); 1484 1485 return true; 1486} 1487 1488/* 1489 * Write opt data. 1490 * 1491 * We have different pieces, some of which may be optional. To make the 1492 * most effective use of space, we use a "chunk" format, with a 4-byte 1493 * type and a 4-byte length. We guarantee 64-bit alignment for the data, 1494 * so it can be used directly when the file is mapped for reading. 1495 */ 1496static bool writeOptData(int fd, const DexClassLookup* pClassLookup, 1497 const RegisterMapBuilder* pRegMapBuilder) 1498{ 1499 /* pre-computed class lookup hash table */ 1500 if (!writeChunk(fd, (u4) kDexChunkClassLookup, 1501 pClassLookup, pClassLookup->size)) 1502 { 1503 return false; 1504 } 1505 1506 /* register maps (optional) */ 1507 if (pRegMapBuilder != NULL) { 1508 if (!writeChunk(fd, (u4) kDexChunkRegisterMaps, 1509 pRegMapBuilder->data, pRegMapBuilder->size)) 1510 { 1511 return false; 1512 } 1513 } 1514 1515 /* write the end marker */ 1516 if (!writeChunk(fd, (u4) kDexChunkEnd, NULL, 0)) { 1517 return false; 1518 } 1519 1520 return true; 1521} 1522 1523/* 1524 * Compute a checksum on a piece of an open file. 1525 * 1526 * File will be positioned at end of checksummed area. 1527 * 1528 * Returns "true" on success. 1529 */ 1530static bool computeFileChecksum(int fd, off_t start, size_t length, u4* pSum) 1531{ 1532 unsigned char readBuf[8192]; 1533 ssize_t actual; 1534 uLong adler; 1535 1536 if (lseek(fd, start, SEEK_SET) != start) { 1537 LOGE("Unable to seek to start of checksum area (%ld): %s", 1538 (long) start, strerror(errno)); 1539 return false; 1540 } 1541 1542 adler = adler32(0L, Z_NULL, 0); 1543 1544 while (length != 0) { 1545 size_t wanted = (length < sizeof(readBuf)) ? length : sizeof(readBuf); 1546 actual = read(fd, readBuf, wanted); 1547 if (actual <= 0) { 1548 LOGE("Read failed (%d) while computing checksum (len=%zu): %s", 1549 (int) actual, length, strerror(errno)); 1550 return false; 1551 } 1552 1553 adler = adler32(adler, readBuf, actual); 1554 1555 length -= actual; 1556 } 1557 1558 *pSum = adler; 1559 return true; 1560} 1561 1562/* 1563 * Update the Adler-32 checksum stored in the DEX file. This covers the 1564 * swapped and optimized DEX data, but does not include the opt header 1565 * or optimized data. 1566 */ 1567static void updateChecksum(u1* addr, int len, DexHeader* pHeader) 1568{ 1569 /* 1570 * Rewrite the checksum. We leave the SHA-1 signature alone. 1571 */ 1572 uLong adler = adler32(0L, Z_NULL, 0); 1573 const int nonSum = sizeof(pHeader->magic) + sizeof(pHeader->checksum); 1574 1575 adler = adler32(adler, addr + nonSum, len - nonSum); 1576 pHeader->checksum = adler; 1577} 1578