Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | * Functions to parse and manipulate the additional data tables added |
| 19 | * to optimized .dex files. |
| 20 | */ |
| 21 | |
| 22 | #include <zlib.h> |
| 23 | |
| 24 | #include "DexOptData.h" |
| 25 | |
| 26 | /* |
| 27 | * Check to see if a given data pointer is a valid double-word-aligned |
| 28 | * pointer into the given memory range (from start inclusive to end |
| 29 | * exclusive). Returns true if valid. |
| 30 | */ |
| 31 | static bool isValidPointer(const void* ptr, const void* start, const void* end) |
| 32 | { |
SangWook Han | b210a9f | 2012-07-15 22:42:28 +0900 | [diff] [blame] | 33 | return (ptr >= start) && (ptr < end) && (((uintptr_t) ptr & 7) == 0); |
Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /* (documented in header file) */ |
| 37 | u4 dexComputeOptChecksum(const DexOptHeader* pOptHeader) |
| 38 | { |
| 39 | const u1* start = (const u1*) pOptHeader + pOptHeader->depsOffset; |
| 40 | const u1* end = (const u1*) pOptHeader + |
| 41 | pOptHeader->optOffset + pOptHeader->optLength; |
| 42 | |
| 43 | uLong adler = adler32(0L, Z_NULL, 0); |
| 44 | |
| 45 | return (u4) adler32(adler, start, end - start); |
| 46 | } |
| 47 | |
Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 48 | /* (documented in header file) */ |
| 49 | bool dexParseOptData(const u1* data, size_t length, DexFile* pDexFile) |
| 50 | { |
| 51 | const void* pOptStart = data + pDexFile->pOptHeader->optOffset; |
| 52 | const void* pOptEnd = data + length; |
Dan Bornstein | a70a3d8 | 2011-04-14 13:08:06 -0700 | [diff] [blame] | 53 | const u4* pOpt = (const u4*) pOptStart; |
Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 54 | u4 optLength = (const u1*) pOptEnd - (const u1*) pOptStart; |
Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * Make sure the opt data start is in range and aligned. This may |
| 58 | * seem like a superfluous check, but (a) if the file got |
| 59 | * truncated, it might turn out that pOpt >= pOptEnd; and (b) |
| 60 | * if the opt data header got corrupted, pOpt might not be |
| 61 | * properly aligned. This test will catch both of these cases. |
| 62 | */ |
| 63 | if (!isValidPointer(pOpt, pOptStart, pOptEnd)) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 64 | ALOGE("Bogus opt data start pointer"); |
Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | |
| 68 | /* Make sure that the opt data length is a whole number of words. */ |
| 69 | if ((optLength & 3) != 0) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 70 | ALOGE("Unaligned opt data area end"); |
Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Make sure that the opt data area is large enough to have at least |
| 76 | * one chunk header. |
| 77 | */ |
| 78 | if (optLength < 8) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 79 | ALOGE("Undersized opt data area (%u)", optLength); |
Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | |
| 83 | /* Process chunks until we see the end marker. */ |
| 84 | while (*pOpt != kDexChunkEnd) { |
| 85 | if (!isValidPointer(pOpt + 2, pOptStart, pOptEnd)) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 86 | ALOGE("Bogus opt data content pointer at offset %u", |
Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 87 | ((const u1*) pOpt) - data); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | u4 size = *(pOpt + 1); |
| 92 | const u1* pOptData = (const u1*) (pOpt + 2); |
| 93 | |
| 94 | /* |
| 95 | * The rounded size is 64-bit aligned and includes +8 for the |
| 96 | * type/size header (which was extracted immediately above). |
| 97 | */ |
| 98 | u4 roundedSize = (size + 8 + 7) & ~7; |
| 99 | const u4* pNextOpt = pOpt + (roundedSize / sizeof(u4)); |
| 100 | |
| 101 | if (!isValidPointer(pNextOpt, pOptStart, pOptEnd)) { |
Steve Block | c1a4ab9 | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 102 | ALOGE("Opt data area problem for chunk of size %u at offset %u", |
Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 103 | size, ((const u1*) pOpt) - data); |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | switch (*pOpt) { |
| 108 | case kDexChunkClassLookup: |
| 109 | pDexFile->pClassLookup = (const DexClassLookup*) pOptData; |
| 110 | break; |
Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 111 | case kDexChunkRegisterMaps: |
Steve Block | 92c1f6f | 2011-10-20 11:55:54 +0100 | [diff] [blame] | 112 | ALOGV("+++ found register maps, size=%u", size); |
Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 113 | pDexFile->pRegisterMapPool = pOptData; |
| 114 | break; |
| 115 | default: |
Steve Block | 4308417 | 2012-01-04 20:04:51 +0000 | [diff] [blame] | 116 | ALOGI("Unknown chunk 0x%08x (%c%c%c%c), size=%d in opt data area", |
Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 117 | *pOpt, |
| 118 | (char) ((*pOpt) >> 24), (char) ((*pOpt) >> 16), |
| 119 | (char) ((*pOpt) >> 8), (char) (*pOpt), |
| 120 | size); |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | pOpt = pNextOpt; |
| 125 | } |
| 126 | |
Dan Bornstein | e377ef6 | 2010-08-31 16:50:00 -0700 | [diff] [blame] | 127 | return true; |
| 128 | } |