Home | History | Annotate | Download | only in libdex
      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 {
     33     return (ptr >= start) && (ptr < end) && (((u4) ptr & 7) == 0);
     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 
     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;
     53     const u4* pOpt = pOptStart;
     54     u4 optLength = (const u1*) pOptEnd - (const u1*) pOptStart;
     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)) {
     64         LOGE("Bogus opt data start pointer\n");
     65         return false;
     66     }
     67 
     68     /* Make sure that the opt data length is a whole number of words. */
     69     if ((optLength & 3) != 0) {
     70         LOGE("Unaligned opt data area end\n");
     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) {
     79         LOGE("Undersized opt data area (%u)\n", optLength);
     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)) {
     86             LOGE("Bogus opt data content pointer at offset %u\n",
     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)) {
    102             LOGE("Opt data area problem for chunk of size %u at offset %u\n",
    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;
    111         case kDexChunkRegisterMaps:
    112             LOGV("+++ found register maps, size=%u\n", size);
    113             pDexFile->pRegisterMapPool = pOptData;
    114             break;
    115         default:
    116             LOGI("Unknown chunk 0x%08x (%c%c%c%c), size=%d in opt data area\n",
    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 
    127     return true;
    128 }
    129