blob: dd41751c2cdf2a696cf2fe947dc24fbcf0205454 [file] [log] [blame]
Dan Bornsteine377ef62010-08-31 16:50:00 -07001/*
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 */
31static bool isValidPointer(const void* ptr, const void* start, const void* end)
32{
SangWook Hanb210a9f2012-07-15 22:42:28 +090033 return (ptr >= start) && (ptr < end) && (((uintptr_t) ptr & 7) == 0);
Dan Bornsteine377ef62010-08-31 16:50:00 -070034}
35
36/* (documented in header file) */
37u4 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 Bornsteine377ef62010-08-31 16:50:00 -070048/* (documented in header file) */
49bool 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 Bornsteina70a3d82011-04-14 13:08:06 -070053 const u4* pOpt = (const u4*) pOptStart;
Dan Bornsteine377ef62010-08-31 16:50:00 -070054 u4 optLength = (const u1*) pOptEnd - (const u1*) pOptStart;
Dan Bornsteine377ef62010-08-31 16:50:00 -070055
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 Blockc1a4ab92012-01-06 19:16:58 +000064 ALOGE("Bogus opt data start pointer");
Dan Bornsteine377ef62010-08-31 16:50:00 -070065 return false;
66 }
67
68 /* Make sure that the opt data length is a whole number of words. */
69 if ((optLength & 3) != 0) {
Steve Blockc1a4ab92012-01-06 19:16:58 +000070 ALOGE("Unaligned opt data area end");
Dan Bornsteine377ef62010-08-31 16:50:00 -070071 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 Blockc1a4ab92012-01-06 19:16:58 +000079 ALOGE("Undersized opt data area (%u)", optLength);
Dan Bornsteine377ef62010-08-31 16:50:00 -070080 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 Blockc1a4ab92012-01-06 19:16:58 +000086 ALOGE("Bogus opt data content pointer at offset %u",
Dan Bornsteine377ef62010-08-31 16:50:00 -070087 ((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 Blockc1a4ab92012-01-06 19:16:58 +0000102 ALOGE("Opt data area problem for chunk of size %u at offset %u",
Dan Bornsteine377ef62010-08-31 16:50:00 -0700103 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 Bornsteine377ef62010-08-31 16:50:00 -0700111 case kDexChunkRegisterMaps:
Steve Block92c1f6f2011-10-20 11:55:54 +0100112 ALOGV("+++ found register maps, size=%u", size);
Dan Bornsteine377ef62010-08-31 16:50:00 -0700113 pDexFile->pRegisterMapPool = pOptData;
114 break;
115 default:
Steve Block43084172012-01-04 20:04:51 +0000116 ALOGI("Unknown chunk 0x%08x (%c%c%c%c), size=%d in opt data area",
Dan Bornsteine377ef62010-08-31 16:50:00 -0700117 *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 Bornsteine377ef62010-08-31 16:50:00 -0700127 return true;
128}