dex_file_verifier.cc revision 1a9735701d0826adbc9d68cd3762b78f96499cfb
12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/* 22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project 32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License. 62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at 72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * 102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software 112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and 142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License. 152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */ 1610037c866b04550fc5461058c398c2e3e509381ajeffhao 1710037c866b04550fc5461058c398c2e3e509381ajeffhao#include "dex_file_verifier.h" 1810037c866b04550fc5461058c398c2e3e509381ajeffhao 19e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe#include <inttypes.h> 2092572be7f754c213e615a62955cc5f65ca8c0c0eNarayan Kamath#include <zlib.h> 21e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 22700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers#include <memory> 2392572be7f754c213e615a62955cc5f65ca8c0c0eNarayan Kamath 24e222ee0b794f941af4fb1b32fb8224e32942ea7bElliott Hughes#include "base/stringprintf.h" 254f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h" 2610037c866b04550fc5461058c398c2e3e509381ajeffhao#include "leb128.h" 27a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes#include "safe_map.h" 28a67249065e4c9b3cf4a7c081d95a78df28291ee9Ian Rogers#include "utf-inl.h" 292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "utils.h" 3010037c866b04550fc5461058c398c2e3e509381ajeffhao 3110037c866b04550fc5461058c398c2e3e509381ajeffhaonamespace art { 3210037c866b04550fc5461058c398c2e3e509381ajeffhao 3310037c866b04550fc5461058c398c2e3e509381ajeffhaostatic uint32_t MapTypeToBitMask(uint32_t map_type) { 3410037c866b04550fc5461058c398c2e3e509381ajeffhao switch (map_type) { 3510037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeHeaderItem: return 1 << 0; 3610037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeStringIdItem: return 1 << 1; 3710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeTypeIdItem: return 1 << 2; 3810037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeProtoIdItem: return 1 << 3; 3910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeFieldIdItem: return 1 << 4; 4010037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeMethodIdItem: return 1 << 5; 4110037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDefItem: return 1 << 6; 4210037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeMapList: return 1 << 7; 4310037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeTypeList: return 1 << 8; 4410037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationSetRefList: return 1 << 9; 4510037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationSetItem: return 1 << 10; 4610037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDataItem: return 1 << 11; 4710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeCodeItem: return 1 << 12; 4810037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeStringDataItem: return 1 << 13; 4910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeDebugInfoItem: return 1 << 14; 5010037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationItem: return 1 << 15; 5110037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeEncodedArrayItem: return 1 << 16; 5210037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationsDirectoryItem: return 1 << 17; 5310037c866b04550fc5461058c398c2e3e509381ajeffhao } 5410037c866b04550fc5461058c398c2e3e509381ajeffhao return 0; 5510037c866b04550fc5461058c398c2e3e509381ajeffhao} 5610037c866b04550fc5461058c398c2e3e509381ajeffhao 5710037c866b04550fc5461058c398c2e3e509381ajeffhaostatic bool IsDataSectionType(uint32_t map_type) { 5810037c866b04550fc5461058c398c2e3e509381ajeffhao switch (map_type) { 5910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeHeaderItem: 6010037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeStringIdItem: 6110037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeTypeIdItem: 6210037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeProtoIdItem: 6310037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeFieldIdItem: 6410037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeMethodIdItem: 6510037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDefItem: 6610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 6710037c866b04550fc5461058c398c2e3e509381ajeffhao } 6810037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 6910037c866b04550fc5461058c398c2e3e509381ajeffhao} 7010037c866b04550fc5461058c398c2e3e509381ajeffhao 71e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampeconst char* DexFileVerifier::CheckLoadStringByIdx(uint32_t idx, const char* error_string) { 72df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe if (UNLIKELY(!CheckIndex(idx, dex_file_->NumStringIds(), error_string))) { 73e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return nullptr; 74e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe } 75e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return dex_file_->StringDataByIdx(idx); 76e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe} 77e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe 78e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampeconst char* DexFileVerifier::CheckLoadStringByTypeIdx(uint32_t type_idx, const char* error_string) { 79df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe if (UNLIKELY(!CheckIndex(type_idx, dex_file_->NumTypeIds(), error_string))) { 80e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return nullptr; 81e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe } 82e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe const DexFile::TypeId& type_id = dex_file_->GetTypeId(type_idx); 83e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe uint32_t idx = type_id.descriptor_idx_; 84e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return CheckLoadStringByIdx(idx, error_string); 85e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe} 86e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe 87e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampeconst DexFile::FieldId* DexFileVerifier::CheckLoadFieldId(uint32_t idx, const char* error_string) { 88df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe if (UNLIKELY(!CheckIndex(idx, dex_file_->NumFieldIds(), error_string))) { 89e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return nullptr; 90e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe } 91e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return &dex_file_->GetFieldId(idx); 92e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe} 93e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe 94e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampeconst DexFile::MethodId* DexFileVerifier::CheckLoadMethodId(uint32_t idx, const char* err_string) { 95df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe if (UNLIKELY(!CheckIndex(idx, dex_file_->NumMethodIds(), err_string))) { 96e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return nullptr; 97e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe } 98e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return &dex_file_->GetMethodId(idx); 99e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe} 100e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe 101e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe// Helper macro to load string and return false on error. 102e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe#define LOAD_STRING(var, idx, error) \ 103e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe const char* var = CheckLoadStringByIdx(idx, error); \ 104df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe if (UNLIKELY(var == nullptr)) { \ 105e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return false; \ 106e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe } 107e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe 108e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe// Helper macro to load string by type idx and return false on error. 109e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe#define LOAD_STRING_BY_TYPE(var, type_idx, error) \ 110e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe const char* var = CheckLoadStringByTypeIdx(type_idx, error); \ 111df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe if (UNLIKELY(var == nullptr)) { \ 112e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return false; \ 113e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe } 114e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe 115e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe// Helper macro to load method id. Return last parameter on error. 1165e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe#define LOAD_METHOD(var, idx, error_string, error_stmt) \ 117e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe const DexFile::MethodId* var = CheckLoadMethodId(idx, error_string); \ 118df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe if (UNLIKELY(var == nullptr)) { \ 1195e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe error_stmt; \ 120e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe } 121e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe 122e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe// Helper macro to load method id. Return last parameter on error. 1235e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe#define LOAD_FIELD(var, idx, fmt, error_stmt) \ 124e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe const DexFile::FieldId* var = CheckLoadFieldId(idx, fmt); \ 125df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe if (UNLIKELY(var == nullptr)) { \ 1265e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe error_stmt; \ 127e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe } 128e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe 12913735955f39b3b304c37d2b2840663c131262c18Ian Rogersbool DexFileVerifier::Verify(const DexFile* dex_file, const uint8_t* begin, size_t size, 1308d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers const char* location, std::string* error_msg) { 131700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers std::unique_ptr<DexFileVerifier> verifier(new DexFileVerifier(dex_file, begin, size, location)); 1328d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (!verifier->Verify()) { 1338d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers *error_msg = verifier->FailureReason(); 1348d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers return false; 1358d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers } 1368d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers return true; 1378d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers} 1388d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers 1398d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogersbool DexFileVerifier::CheckShortyDescriptorMatch(char shorty_char, const char* descriptor, 1408d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers bool is_return_type) { 14110037c866b04550fc5461058c398c2e3e509381ajeffhao switch (shorty_char) { 14210037c866b04550fc5461058c398c2e3e509381ajeffhao case 'V': 1438d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(!is_return_type)) { 1448d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Invalid use of void"); 14510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 14610037c866b04550fc5461058c398c2e3e509381ajeffhao } 147fc787ecd91127b2c8458afd94e5148e2ae51a1f5Ian Rogers FALLTHROUGH_INTENDED; 14810037c866b04550fc5461058c398c2e3e509381ajeffhao case 'B': 14910037c866b04550fc5461058c398c2e3e509381ajeffhao case 'C': 15010037c866b04550fc5461058c398c2e3e509381ajeffhao case 'D': 15110037c866b04550fc5461058c398c2e3e509381ajeffhao case 'F': 15210037c866b04550fc5461058c398c2e3e509381ajeffhao case 'I': 15310037c866b04550fc5461058c398c2e3e509381ajeffhao case 'J': 15410037c866b04550fc5461058c398c2e3e509381ajeffhao case 'S': 15510037c866b04550fc5461058c398c2e3e509381ajeffhao case 'Z': 1568d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((descriptor[0] != shorty_char) || (descriptor[1] != '\0'))) { 1578d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Shorty vs. primitive type mismatch: '%c', '%s'", 1588d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers shorty_char, descriptor); 15910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 16010037c866b04550fc5461058c398c2e3e509381ajeffhao } 16110037c866b04550fc5461058c398c2e3e509381ajeffhao break; 16210037c866b04550fc5461058c398c2e3e509381ajeffhao case 'L': 1638d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((descriptor[0] != 'L') && (descriptor[0] != '['))) { 1648d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Shorty vs. type mismatch: '%c', '%s'", shorty_char, descriptor); 16510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 16610037c866b04550fc5461058c398c2e3e509381ajeffhao } 16710037c866b04550fc5461058c398c2e3e509381ajeffhao break; 16810037c866b04550fc5461058c398c2e3e509381ajeffhao default: 1698d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad shorty character: '%c'", shorty_char); 17010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 17110037c866b04550fc5461058c398c2e3e509381ajeffhao } 17210037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 17310037c866b04550fc5461058c398c2e3e509381ajeffhao} 17410037c866b04550fc5461058c398c2e3e509381ajeffhao 17550d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampebool DexFileVerifier::CheckListSize(const void* start, size_t count, size_t elem_size, 176d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe const char* label) { 17750d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe // Check that size is not 0. 17850d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe CHECK_NE(elem_size, 0U); 17950d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe 18013735955f39b3b304c37d2b2840663c131262c18Ian Rogers const uint8_t* range_start = reinterpret_cast<const uint8_t*>(start); 18113735955f39b3b304c37d2b2840663c131262c18Ian Rogers const uint8_t* file_start = reinterpret_cast<const uint8_t*>(begin_); 18250d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe 18350d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe // Check for overflow. 18450d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe uintptr_t max = 0 - 1; 18550d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe size_t available_bytes_till_end_of_mem = max - reinterpret_cast<uintptr_t>(start); 18650d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe size_t max_count = available_bytes_till_end_of_mem / elem_size; 18750d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe if (max_count < count) { 18850d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe ErrorStringPrintf("Overflow in range for %s: %zx for %zu@%zu", label, 18950d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe static_cast<size_t>(range_start - file_start), 19050d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe count, elem_size); 19150d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe return false; 19250d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe } 19350d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe 19413735955f39b3b304c37d2b2840663c131262c18Ian Rogers const uint8_t* range_end = range_start + count * elem_size; 19513735955f39b3b304c37d2b2840663c131262c18Ian Rogers const uint8_t* file_end = file_start + size_; 19650d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe if (UNLIKELY((range_start < file_start) || (range_end > file_end))) { 19750d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe // Note: these two tests are enough as we make sure above that there's no overflow. 1988a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers ErrorStringPrintf("Bad range for %s: %zx to %zx", label, 199e3d5581266301e6a672af6233220037abf52fea1Ian Rogers static_cast<size_t>(range_start - file_start), 200e3d5581266301e6a672af6233220037abf52fea1Ian Rogers static_cast<size_t>(range_end - file_start)); 20110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 20210037c866b04550fc5461058c398c2e3e509381ajeffhao } 20310037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 20410037c866b04550fc5461058c398c2e3e509381ajeffhao} 20510037c866b04550fc5461058c398c2e3e509381ajeffhao 20613735955f39b3b304c37d2b2840663c131262c18Ian Rogersbool DexFileVerifier::CheckList(size_t element_size, const char* label, const uint8_t* *ptr) { 207d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe // Check that the list is available. The first 4B are the count. 208d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe if (!CheckListSize(*ptr, 1, 4U, label)) { 209d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe return false; 210d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe } 211d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe 212d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe uint32_t count = *reinterpret_cast<const uint32_t*>(*ptr); 213d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe if (count > 0) { 214d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe if (!CheckListSize(*ptr + 4, count, element_size, label)) { 215d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe return false; 216d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe } 217d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe } 218d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe 219d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe *ptr += 4 + count * element_size; 220d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe return true; 221d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe} 222d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe 2238d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogersbool DexFileVerifier::CheckIndex(uint32_t field, uint32_t limit, const char* label) { 2248d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(field >= limit)) { 2258d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad index for %s: %x >= %x", label, field, limit); 22610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 22710037c866b04550fc5461058c398c2e3e509381ajeffhao } 22810037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 22910037c866b04550fc5461058c398c2e3e509381ajeffhao} 23010037c866b04550fc5461058c398c2e3e509381ajeffhao 231d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampebool DexFileVerifier::CheckValidOffsetAndSize(uint32_t offset, uint32_t size, const char* label) { 232d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe if (size == 0) { 233d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe if (offset != 0) { 234d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe ErrorStringPrintf("Offset(%d) should be zero when size is zero for %s.", offset, label); 235d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe return false; 236d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe } 237d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe } 238d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe if (size_ <= offset) { 239d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe ErrorStringPrintf("Offset(%d) should be within file size(%zu) for %s.", offset, size_, label); 240d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe return false; 241d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe } 242d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe return true; 243d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe} 244d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe 2458d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogersbool DexFileVerifier::CheckHeader() { 246f6174e8a1566bb357e82506f7ec97dc359c90eb2jeffhao // Check file size from the header. 247f6174e8a1566bb357e82506f7ec97dc359c90eb2jeffhao uint32_t expected_size = header_->file_size_; 248f6174e8a1566bb357e82506f7ec97dc359c90eb2jeffhao if (size_ != expected_size) { 2498d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad file size (%zd, expected %ud)", size_, expected_size); 25010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 25110037c866b04550fc5461058c398c2e3e509381ajeffhao } 25210037c866b04550fc5461058c398c2e3e509381ajeffhao 25310037c866b04550fc5461058c398c2e3e509381ajeffhao // Compute and verify the checksum in the header. 25410037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t adler_checksum = adler32(0L, Z_NULL, 0); 25510037c866b04550fc5461058c398c2e3e509381ajeffhao const uint32_t non_sum = sizeof(header_->magic_) + sizeof(header_->checksum_); 25613735955f39b3b304c37d2b2840663c131262c18Ian Rogers const uint8_t* non_sum_ptr = reinterpret_cast<const uint8_t*>(header_) + non_sum; 257f6174e8a1566bb357e82506f7ec97dc359c90eb2jeffhao adler_checksum = adler32(adler_checksum, non_sum_ptr, expected_size - non_sum); 25810037c866b04550fc5461058c398c2e3e509381ajeffhao if (adler_checksum != header_->checksum_) { 2598d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad checksum (%08x, expected %08x)", adler_checksum, header_->checksum_); 26010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 26110037c866b04550fc5461058c398c2e3e509381ajeffhao } 26210037c866b04550fc5461058c398c2e3e509381ajeffhao 26310037c866b04550fc5461058c398c2e3e509381ajeffhao // Check the contents of the header. 26410037c866b04550fc5461058c398c2e3e509381ajeffhao if (header_->endian_tag_ != DexFile::kDexEndianConstant) { 2658d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Unexpected endian_tag: %x", header_->endian_tag_); 26610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 26710037c866b04550fc5461058c398c2e3e509381ajeffhao } 26810037c866b04550fc5461058c398c2e3e509381ajeffhao 26910037c866b04550fc5461058c398c2e3e509381ajeffhao if (header_->header_size_ != sizeof(DexFile::Header)) { 2708d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad header size: %ud", header_->header_size_); 27110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 27210037c866b04550fc5461058c398c2e3e509381ajeffhao } 27310037c866b04550fc5461058c398c2e3e509381ajeffhao 274d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe // Check that all offsets are inside the file. 275d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe bool result = 276d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe CheckValidOffsetAndSize(header_->link_off_, header_->link_size_, "link") && 277d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe CheckValidOffsetAndSize(header_->map_off_, header_->map_off_, "map") && 278d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe CheckValidOffsetAndSize(header_->string_ids_off_, header_->string_ids_size_, "string-ids") && 279d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe CheckValidOffsetAndSize(header_->type_ids_off_, header_->type_ids_size_, "type-ids") && 280d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe CheckValidOffsetAndSize(header_->proto_ids_off_, header_->proto_ids_size_, "proto-ids") && 281d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe CheckValidOffsetAndSize(header_->field_ids_off_, header_->field_ids_size_, "field-ids") && 282d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe CheckValidOffsetAndSize(header_->method_ids_off_, header_->method_ids_size_, "method-ids") && 283d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe CheckValidOffsetAndSize(header_->class_defs_off_, header_->class_defs_size_, "class-defs") && 284d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe CheckValidOffsetAndSize(header_->data_off_, header_->data_size_, "data"); 285d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe 286d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe return result; 28710037c866b04550fc5461058c398c2e3e509381ajeffhao} 28810037c866b04550fc5461058c398c2e3e509381ajeffhao 2898d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogersbool DexFileVerifier::CheckMap() { 290d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ + 291d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe header_->map_off_); 292d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe // Check that map list content is available. 293d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe if (!CheckListSize(map, 1, sizeof(DexFile::MapList), "maplist content")) { 294d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe return false; 295d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe } 296d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe 29710037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::MapItem* item = map->list_; 29810037c866b04550fc5461058c398c2e3e509381ajeffhao 29910037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t count = map->size_; 30010037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t last_offset = 0; 30110037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t data_item_count = 0; 30210037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t data_items_left = header_->data_size_; 30310037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t used_bits = 0; 30410037c866b04550fc5461058c398c2e3e509381ajeffhao 30510037c866b04550fc5461058c398c2e3e509381ajeffhao // Sanity check the size of the map list. 30610037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckListSize(item, count, sizeof(DexFile::MapItem), "map size")) { 30710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 30810037c866b04550fc5461058c398c2e3e509381ajeffhao } 30910037c866b04550fc5461058c398c2e3e509381ajeffhao 31010037c866b04550fc5461058c398c2e3e509381ajeffhao // Check the items listed in the map. 31110037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 0; i < count; i++) { 3128d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(last_offset >= item->offset_ && i != 0)) { 3138d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out of order map item: %x then %x", last_offset, item->offset_); 31410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 31510037c866b04550fc5461058c398c2e3e509381ajeffhao } 3168d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(item->offset_ >= header_->file_size_)) { 3178d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Map item after end of file: %x, size %x", 3188d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers item->offset_, header_->file_size_); 31910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 32010037c866b04550fc5461058c398c2e3e509381ajeffhao } 32110037c866b04550fc5461058c398c2e3e509381ajeffhao 32210037c866b04550fc5461058c398c2e3e509381ajeffhao if (IsDataSectionType(item->type_)) { 32310037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t icount = item->size_; 3248d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(icount > data_items_left)) { 3258d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Too many items in data section: %ud", data_item_count + icount); 32610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 32710037c866b04550fc5461058c398c2e3e509381ajeffhao } 32810037c866b04550fc5461058c398c2e3e509381ajeffhao data_items_left -= icount; 32910037c866b04550fc5461058c398c2e3e509381ajeffhao data_item_count += icount; 33010037c866b04550fc5461058c398c2e3e509381ajeffhao } 33110037c866b04550fc5461058c398c2e3e509381ajeffhao 33210037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t bit = MapTypeToBitMask(item->type_); 33310037c866b04550fc5461058c398c2e3e509381ajeffhao 3348d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(bit == 0)) { 3358d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Unknown map section type %x", item->type_); 33610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 33710037c866b04550fc5461058c398c2e3e509381ajeffhao } 33810037c866b04550fc5461058c398c2e3e509381ajeffhao 3398d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((used_bits & bit) != 0)) { 3408d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Duplicate map section of type %x", item->type_); 34110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 34210037c866b04550fc5461058c398c2e3e509381ajeffhao } 34310037c866b04550fc5461058c398c2e3e509381ajeffhao 34410037c866b04550fc5461058c398c2e3e509381ajeffhao used_bits |= bit; 34510037c866b04550fc5461058c398c2e3e509381ajeffhao last_offset = item->offset_; 34610037c866b04550fc5461058c398c2e3e509381ajeffhao item++; 34710037c866b04550fc5461058c398c2e3e509381ajeffhao } 34810037c866b04550fc5461058c398c2e3e509381ajeffhao 34910037c866b04550fc5461058c398c2e3e509381ajeffhao // Check for missing sections in the map. 3508d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeHeaderItem)) == 0)) { 3518d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Map is missing header entry"); 35210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 35310037c866b04550fc5461058c398c2e3e509381ajeffhao } 3548d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeMapList)) == 0)) { 3558d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Map is missing map_list entry"); 35610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 35710037c866b04550fc5461058c398c2e3e509381ajeffhao } 3588d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeStringIdItem)) == 0 && 3598d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ((header_->string_ids_off_ != 0) || (header_->string_ids_size_ != 0)))) { 3608d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Map is missing string_ids entry"); 36110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 36210037c866b04550fc5461058c398c2e3e509381ajeffhao } 3638d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeTypeIdItem)) == 0 && 3648d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ((header_->type_ids_off_ != 0) || (header_->type_ids_size_ != 0)))) { 3658d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Map is missing type_ids entry"); 36610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 36710037c866b04550fc5461058c398c2e3e509381ajeffhao } 3688d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeProtoIdItem)) == 0 && 3698d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ((header_->proto_ids_off_ != 0) || (header_->proto_ids_size_ != 0)))) { 3708d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Map is missing proto_ids entry"); 37110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 37210037c866b04550fc5461058c398c2e3e509381ajeffhao } 3738d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeFieldIdItem)) == 0 && 3748d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ((header_->field_ids_off_ != 0) || (header_->field_ids_size_ != 0)))) { 3758d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Map is missing field_ids entry"); 37610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 37710037c866b04550fc5461058c398c2e3e509381ajeffhao } 3788d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeMethodIdItem)) == 0 && 3798d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ((header_->method_ids_off_ != 0) || (header_->method_ids_size_ != 0)))) { 3808d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Map is missing method_ids entry"); 38110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 38210037c866b04550fc5461058c398c2e3e509381ajeffhao } 3838d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeClassDefItem)) == 0 && 3848d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ((header_->class_defs_off_ != 0) || (header_->class_defs_size_ != 0)))) { 3858d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Map is missing class_defs entry"); 38610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 38710037c866b04550fc5461058c398c2e3e509381ajeffhao } 38810037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 38910037c866b04550fc5461058c398c2e3e509381ajeffhao} 39010037c866b04550fc5461058c398c2e3e509381ajeffhao 39110037c866b04550fc5461058c398c2e3e509381ajeffhaouint32_t DexFileVerifier::ReadUnsignedLittleEndian(uint32_t size) { 39210037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t result = 0; 39313735955f39b3b304c37d2b2840663c131262c18Ian Rogers if (LIKELY(CheckListSize(ptr_, size, sizeof(uint8_t), "encoded_value"))) { 3948d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers for (uint32_t i = 0; i < size; i++) { 3958d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers result |= ((uint32_t) *(ptr_++)) << (i * 8); 3968d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers } 39710037c866b04550fc5461058c398c2e3e509381ajeffhao } 39810037c866b04550fc5461058c398c2e3e509381ajeffhao return result; 39910037c866b04550fc5461058c398c2e3e509381ajeffhao} 40010037c866b04550fc5461058c398c2e3e509381ajeffhao 40110037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item, 4028d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers uint32_t* handler_offsets, uint32_t handlers_size) { 40313735955f39b3b304c37d2b2840663c131262c18Ian Rogers const uint8_t* handlers_base = DexFile::GetCatchHandlerData(*code_item, 0); 40410037c866b04550fc5461058c398c2e3e509381ajeffhao 40510037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 0; i < handlers_size; i++) { 40610037c866b04550fc5461058c398c2e3e509381ajeffhao bool catch_all; 4078a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers size_t offset = ptr_ - handlers_base; 40810037c866b04550fc5461058c398c2e3e509381ajeffhao int32_t size = DecodeSignedLeb128(&ptr_); 40910037c866b04550fc5461058c398c2e3e509381ajeffhao 4108d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((size < -65536) || (size > 65536))) { 4118d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Invalid exception handler size: %d", size); 41210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 41310037c866b04550fc5461058c398c2e3e509381ajeffhao } 41410037c866b04550fc5461058c398c2e3e509381ajeffhao 41510037c866b04550fc5461058c398c2e3e509381ajeffhao if (size <= 0) { 41610037c866b04550fc5461058c398c2e3e509381ajeffhao catch_all = true; 41710037c866b04550fc5461058c398c2e3e509381ajeffhao size = -size; 41810037c866b04550fc5461058c398c2e3e509381ajeffhao } else { 41910037c866b04550fc5461058c398c2e3e509381ajeffhao catch_all = false; 42010037c866b04550fc5461058c398c2e3e509381ajeffhao } 42110037c866b04550fc5461058c398c2e3e509381ajeffhao 4228a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers handler_offsets[i] = static_cast<uint32_t>(offset); 42310037c866b04550fc5461058c398c2e3e509381ajeffhao 42410037c866b04550fc5461058c398c2e3e509381ajeffhao while (size-- > 0) { 42510037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t type_idx = DecodeUnsignedLeb128(&ptr_); 42610037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(type_idx, header_->type_ids_size_, "handler type_idx")) { 42710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 42810037c866b04550fc5461058c398c2e3e509381ajeffhao } 42910037c866b04550fc5461058c398c2e3e509381ajeffhao 43010037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t addr = DecodeUnsignedLeb128(&ptr_); 4318d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(addr >= code_item->insns_size_in_code_units_)) { 4328d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Invalid handler addr: %x", addr); 43310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 43410037c866b04550fc5461058c398c2e3e509381ajeffhao } 43510037c866b04550fc5461058c398c2e3e509381ajeffhao } 43610037c866b04550fc5461058c398c2e3e509381ajeffhao 43710037c866b04550fc5461058c398c2e3e509381ajeffhao if (catch_all) { 43810037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t addr = DecodeUnsignedLeb128(&ptr_); 4398d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(addr >= code_item->insns_size_in_code_units_)) { 4408d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Invalid handler catch_all_addr: %x", addr); 44110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 44210037c866b04550fc5461058c398c2e3e509381ajeffhao } 44310037c866b04550fc5461058c398c2e3e509381ajeffhao } 44410037c866b04550fc5461058c398c2e3e509381ajeffhao } 44510037c866b04550fc5461058c398c2e3e509381ajeffhao 44610037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 44710037c866b04550fc5461058c398c2e3e509381ajeffhao} 44810037c866b04550fc5461058c398c2e3e509381ajeffhao 449e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampebool DexFileVerifier::CheckClassDataItemField(uint32_t idx, 450e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t access_flags, 451e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t class_access_flags, 4521a9735701d0826adbc9d68cd3762b78f96499cfbAndreas Gampe uint16_t class_type_index, 4538d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers bool expect_static) { 454e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Check for overflow. 45510037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(idx, header_->field_ids_size_, "class_data_item field_idx")) { 45610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 45710037c866b04550fc5461058c398c2e3e509381ajeffhao } 45810037c866b04550fc5461058c398c2e3e509381ajeffhao 459e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Check that it's the right class. 460e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint16_t my_class_index = 461e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe (reinterpret_cast<const DexFile::FieldId*>(begin_ + header_->field_ids_off_) + idx)-> 462e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe class_idx_; 463e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (class_type_index != my_class_index) { 464e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe ErrorStringPrintf("Field's class index unexpected, %" PRIu16 "vs %" PRIu16, 465e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe my_class_index, 466e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe class_type_index); 467e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 468e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 469e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 470e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Check that it falls into the right class-data list. 47110037c866b04550fc5461058c398c2e3e509381ajeffhao bool is_static = (access_flags & kAccStatic) != 0; 4728d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(is_static != expect_static)) { 4738d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Static/instance field not in expected list"); 47410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 47510037c866b04550fc5461058c398c2e3e509381ajeffhao } 47610037c866b04550fc5461058c398c2e3e509381ajeffhao 477e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Check field access flags. 478e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe std::string error_msg; 479e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!CheckFieldAccessFlags(access_flags, class_access_flags, &error_msg)) { 480e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe ErrorStringPrintf("%s", error_msg.c_str()); 48110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 48210037c866b04550fc5461058c398c2e3e509381ajeffhao } 48310037c866b04550fc5461058c398c2e3e509381ajeffhao 48410037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 48510037c866b04550fc5461058c398c2e3e509381ajeffhao} 48610037c866b04550fc5461058c398c2e3e509381ajeffhao 487e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampebool DexFileVerifier::CheckClassDataItemMethod(uint32_t idx, 488e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t access_flags, 489e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t class_access_flags, 4901a9735701d0826adbc9d68cd3762b78f96499cfbAndreas Gampe uint16_t class_type_index, 491a574b0e4772e57134538c3c098d7538d957edc90Jeff Hao uint32_t code_offset, 492e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe std::unordered_set<uint32_t>* direct_method_indexes, 493a574b0e4772e57134538c3c098d7538d957edc90Jeff Hao bool expect_direct) { 494e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe DCHECK(direct_method_indexes != nullptr); 495e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Check for overflow. 49610037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(idx, header_->method_ids_size_, "class_data_item method_idx")) { 49710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 49810037c866b04550fc5461058c398c2e3e509381ajeffhao } 49910037c866b04550fc5461058c398c2e3e509381ajeffhao 500e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Check that it's the right class. 501e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint16_t my_class_index = 502e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe (reinterpret_cast<const DexFile::MethodId*>(begin_ + header_->method_ids_off_) + idx)-> 503e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe class_idx_; 504e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (class_type_index != my_class_index) { 505e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe ErrorStringPrintf("Method's class index unexpected, %" PRIu16 "vs %" PRIu16, 506e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe my_class_index, 507e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe class_type_index); 50810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 50910037c866b04550fc5461058c398c2e3e509381ajeffhao } 51010037c866b04550fc5461058c398c2e3e509381ajeffhao 511e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Check that it's not defined as both direct and virtual. 512a574b0e4772e57134538c3c098d7538d957edc90Jeff Hao if (expect_direct) { 513e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe direct_method_indexes->insert(idx); 514e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } else if (direct_method_indexes->find(idx) != direct_method_indexes->end()) { 515a574b0e4772e57134538c3c098d7538d957edc90Jeff Hao ErrorStringPrintf("Found virtual method with same index as direct method: %d", idx); 516a574b0e4772e57134538c3c098d7538d957edc90Jeff Hao return false; 517a574b0e4772e57134538c3c098d7538d957edc90Jeff Hao } 518a574b0e4772e57134538c3c098d7538d957edc90Jeff Hao 519e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Check method access flags. 520e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe bool has_code = (code_offset != 0); 521e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe std::string error_msg; 522e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!CheckMethodAccessFlags(idx, 523e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe access_flags, 524e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe class_access_flags, 525e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe has_code, 526e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe expect_direct, 527e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &error_msg)) { 528e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe ErrorStringPrintf("%s", error_msg.c_str()); 52910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 53010037c866b04550fc5461058c398c2e3e509381ajeffhao } 53110037c866b04550fc5461058c398c2e3e509381ajeffhao 53210037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 53310037c866b04550fc5461058c398c2e3e509381ajeffhao} 53410037c866b04550fc5461058c398c2e3e509381ajeffhao 5358a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogersbool DexFileVerifier::CheckPadding(size_t offset, uint32_t aligned_offset) { 53610037c866b04550fc5461058c398c2e3e509381ajeffhao if (offset < aligned_offset) { 53713735955f39b3b304c37d2b2840663c131262c18Ian Rogers if (!CheckListSize(begin_ + offset, aligned_offset - offset, sizeof(uint8_t), "section")) { 53810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 53910037c866b04550fc5461058c398c2e3e509381ajeffhao } 54010037c866b04550fc5461058c398c2e3e509381ajeffhao while (offset < aligned_offset) { 5418d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(*ptr_ != '\0')) { 5428a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers ErrorStringPrintf("Non-zero padding %x before section start at %zx", *ptr_, offset); 54310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 54410037c866b04550fc5461058c398c2e3e509381ajeffhao } 54510037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_++; 54610037c866b04550fc5461058c398c2e3e509381ajeffhao offset++; 54710037c866b04550fc5461058c398c2e3e509381ajeffhao } 54810037c866b04550fc5461058c398c2e3e509381ajeffhao } 54910037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 55010037c866b04550fc5461058c398c2e3e509381ajeffhao} 55110037c866b04550fc5461058c398c2e3e509381ajeffhao 55210037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckEncodedValue() { 55313735955f39b3b304c37d2b2840663c131262c18Ian Rogers if (!CheckListSize(ptr_, 1, sizeof(uint8_t), "encoded_value header")) { 55410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 55510037c866b04550fc5461058c398c2e3e509381ajeffhao } 55610037c866b04550fc5461058c398c2e3e509381ajeffhao 55710037c866b04550fc5461058c398c2e3e509381ajeffhao uint8_t header_byte = *(ptr_++); 55810037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask; 55910037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift; 56010037c866b04550fc5461058c398c2e3e509381ajeffhao 56110037c866b04550fc5461058c398c2e3e509381ajeffhao switch (value_type) { 56210037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationByte: 5638d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(value_arg != 0)) { 5648d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad encoded_value byte size %x", value_arg); 56510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 56610037c866b04550fc5461058c398c2e3e509381ajeffhao } 56710037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_++; 56810037c866b04550fc5461058c398c2e3e509381ajeffhao break; 56910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationShort: 57010037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationChar: 5718d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(value_arg > 1)) { 5728d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad encoded_value char/short size %x", value_arg); 57310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 57410037c866b04550fc5461058c398c2e3e509381ajeffhao } 57510037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += value_arg + 1; 57610037c866b04550fc5461058c398c2e3e509381ajeffhao break; 57710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationInt: 57810037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationFloat: 5798d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(value_arg > 3)) { 5808d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad encoded_value int/float size %x", value_arg); 58110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 58210037c866b04550fc5461058c398c2e3e509381ajeffhao } 58310037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += value_arg + 1; 58410037c866b04550fc5461058c398c2e3e509381ajeffhao break; 58510037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationLong: 58610037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationDouble: 58710037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += value_arg + 1; 58810037c866b04550fc5461058c398c2e3e509381ajeffhao break; 58910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationString: { 5908d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(value_arg > 3)) { 5918d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad encoded_value string size %x", value_arg); 59210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 59310037c866b04550fc5461058c398c2e3e509381ajeffhao } 59410037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1); 59510037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(idx, header_->string_ids_size_, "encoded_value string")) { 59610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 59710037c866b04550fc5461058c398c2e3e509381ajeffhao } 59810037c866b04550fc5461058c398c2e3e509381ajeffhao break; 59910037c866b04550fc5461058c398c2e3e509381ajeffhao } 60010037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationType: { 6018d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(value_arg > 3)) { 6028d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad encoded_value type size %x", value_arg); 60310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 60410037c866b04550fc5461058c398c2e3e509381ajeffhao } 60510037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1); 60610037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(idx, header_->type_ids_size_, "encoded_value type")) { 60710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 60810037c866b04550fc5461058c398c2e3e509381ajeffhao } 60910037c866b04550fc5461058c398c2e3e509381ajeffhao break; 61010037c866b04550fc5461058c398c2e3e509381ajeffhao } 61110037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationField: 61210037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationEnum: { 6138d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(value_arg > 3)) { 6148d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad encoded_value field/enum size %x", value_arg); 61510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 61610037c866b04550fc5461058c398c2e3e509381ajeffhao } 61710037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1); 61810037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(idx, header_->field_ids_size_, "encoded_value field")) { 61910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 62010037c866b04550fc5461058c398c2e3e509381ajeffhao } 62110037c866b04550fc5461058c398c2e3e509381ajeffhao break; 62210037c866b04550fc5461058c398c2e3e509381ajeffhao } 62310037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationMethod: { 6248d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(value_arg > 3)) { 6258d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad encoded_value method size %x", value_arg); 62610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 62710037c866b04550fc5461058c398c2e3e509381ajeffhao } 62810037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1); 62910037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(idx, header_->method_ids_size_, "encoded_value method")) { 63010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 63110037c866b04550fc5461058c398c2e3e509381ajeffhao } 63210037c866b04550fc5461058c398c2e3e509381ajeffhao break; 63310037c866b04550fc5461058c398c2e3e509381ajeffhao } 63410037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationArray: 6358d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(value_arg != 0)) { 6368d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad encoded_value array value_arg %x", value_arg); 63710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 63810037c866b04550fc5461058c398c2e3e509381ajeffhao } 63910037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckEncodedArray()) { 64010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 64110037c866b04550fc5461058c398c2e3e509381ajeffhao } 64210037c866b04550fc5461058c398c2e3e509381ajeffhao break; 64310037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationAnnotation: 6448d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(value_arg != 0)) { 6458d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad encoded_value annotation value_arg %x", value_arg); 64610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 64710037c866b04550fc5461058c398c2e3e509381ajeffhao } 64810037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckEncodedAnnotation()) { 64910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 65010037c866b04550fc5461058c398c2e3e509381ajeffhao } 65110037c866b04550fc5461058c398c2e3e509381ajeffhao break; 65210037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationNull: 6538d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(value_arg != 0)) { 6548d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad encoded_value null value_arg %x", value_arg); 65510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 65610037c866b04550fc5461058c398c2e3e509381ajeffhao } 65710037c866b04550fc5461058c398c2e3e509381ajeffhao break; 65810037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexAnnotationBoolean: 6598d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(value_arg > 1)) { 6608d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad encoded_value boolean size %x", value_arg); 66110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 66210037c866b04550fc5461058c398c2e3e509381ajeffhao } 66310037c866b04550fc5461058c398c2e3e509381ajeffhao break; 66410037c866b04550fc5461058c398c2e3e509381ajeffhao default: 6658d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bogus encoded_value value_type %x", value_type); 66610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 66710037c866b04550fc5461058c398c2e3e509381ajeffhao } 66810037c866b04550fc5461058c398c2e3e509381ajeffhao 66910037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 67010037c866b04550fc5461058c398c2e3e509381ajeffhao} 67110037c866b04550fc5461058c398c2e3e509381ajeffhao 67210037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckEncodedArray() { 67310037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t size = DecodeUnsignedLeb128(&ptr_); 67410037c866b04550fc5461058c398c2e3e509381ajeffhao 67510037c866b04550fc5461058c398c2e3e509381ajeffhao while (size--) { 67610037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckEncodedValue()) { 6778d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers failure_reason_ = StringPrintf("Bad encoded_array value: %s", failure_reason_.c_str()); 67810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 67910037c866b04550fc5461058c398c2e3e509381ajeffhao } 68010037c866b04550fc5461058c398c2e3e509381ajeffhao } 68110037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 68210037c866b04550fc5461058c398c2e3e509381ajeffhao} 68310037c866b04550fc5461058c398c2e3e509381ajeffhao 68410037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckEncodedAnnotation() { 68510037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t idx = DecodeUnsignedLeb128(&ptr_); 68610037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(idx, header_->type_ids_size_, "encoded_annotation type_idx")) { 68710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 68810037c866b04550fc5461058c398c2e3e509381ajeffhao } 68910037c866b04550fc5461058c398c2e3e509381ajeffhao 69010037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t size = DecodeUnsignedLeb128(&ptr_); 69110037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t last_idx = 0; 69210037c866b04550fc5461058c398c2e3e509381ajeffhao 69310037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 0; i < size; i++) { 69410037c866b04550fc5461058c398c2e3e509381ajeffhao idx = DecodeUnsignedLeb128(&ptr_); 69510037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(idx, header_->string_ids_size_, "annotation_element name_idx")) { 69610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 69710037c866b04550fc5461058c398c2e3e509381ajeffhao } 69810037c866b04550fc5461058c398c2e3e509381ajeffhao 6998d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(last_idx >= idx && i != 0)) { 7008d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order annotation_element name_idx: %x then %x", 7018d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers last_idx, idx); 70210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 70310037c866b04550fc5461058c398c2e3e509381ajeffhao } 70410037c866b04550fc5461058c398c2e3e509381ajeffhao 70510037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckEncodedValue()) { 70610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 70710037c866b04550fc5461058c398c2e3e509381ajeffhao } 70810037c866b04550fc5461058c398c2e3e509381ajeffhao 70910037c866b04550fc5461058c398c2e3e509381ajeffhao last_idx = idx; 71010037c866b04550fc5461058c398c2e3e509381ajeffhao } 71110037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 71210037c866b04550fc5461058c398c2e3e509381ajeffhao} 71310037c866b04550fc5461058c398c2e3e509381ajeffhao 714e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampebool DexFileVerifier::FindClassFlags(uint32_t index, 715e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe bool is_field, 716e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint16_t* class_type_index, 717e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t* class_access_flags) { 718e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe DCHECK(class_type_index != nullptr); 719e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe DCHECK(class_access_flags != nullptr); 72010037c866b04550fc5461058c398c2e3e509381ajeffhao 721e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // First check if the index is valid. 722e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (index >= (is_field ? header_->field_ids_size_ : header_->method_ids_size_)) { 723e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 72410037c866b04550fc5461058c398c2e3e509381ajeffhao } 725e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 726e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Next get the type index. 727e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (is_field) { 728e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *class_type_index = 729e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe (reinterpret_cast<const DexFile::FieldId*>(begin_ + header_->field_ids_off_) + index)-> 730e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe class_idx_; 731e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } else { 732e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *class_type_index = 733e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe (reinterpret_cast<const DexFile::MethodId*>(begin_ + header_->method_ids_off_) + index)-> 734e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe class_idx_; 735e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 736e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 737e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Check if that is valid. 738e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (*class_type_index >= header_->type_ids_size_) { 739e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 740e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 741e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 742e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Now search for the class def. This is basically a specialized version of the DexFile code, as 743e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // we should not trust that this is a valid DexFile just yet. 744e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe const DexFile::ClassDef* class_def_begin = 745e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe reinterpret_cast<const DexFile::ClassDef*>(begin_ + header_->class_defs_off_); 746e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe for (size_t i = 0; i < header_->class_defs_size_; ++i) { 747e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe const DexFile::ClassDef* class_def = class_def_begin + i; 748e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (class_def->class_idx_ == *class_type_index) { 749e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *class_access_flags = class_def->access_flags_; 750e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return true; 751ec96923620cb6f8a8f8205d89ea30f147e76e7faJeff Hao } 752e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 753e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 754e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Didn't find the class-def, not defined here... 755e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 756e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe} 757e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 758e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampebool DexFileVerifier::CheckOrderAndGetClassFlags(bool is_field, 759e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe const char* type_descr, 760e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t curr_index, 761e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t prev_index, 762e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe bool* have_class, 763e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint16_t* class_type_index, 764e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t* class_access_flags) { 765e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (curr_index < prev_index) { 766e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe ErrorStringPrintf("out-of-order %s indexes %" PRIu32 " and %" PRIu32, 767e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe type_descr, 768e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe prev_index, 769e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe curr_index); 770e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 771e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 772e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 773e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!*have_class) { 774e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *have_class = FindClassFlags(curr_index, is_field, class_type_index, class_access_flags); 775e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!*have_class) { 776e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Should have really found one. 777e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe ErrorStringPrintf("could not find declaring class for %s index %" PRIu32, 778e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe type_descr, 779e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe curr_index); 78010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 78110037c866b04550fc5461058c398c2e3e509381ajeffhao } 78210037c866b04550fc5461058c398c2e3e509381ajeffhao } 783e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return true; 784e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe} 785e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 786e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampetemplate <bool kStatic> 787e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampebool DexFileVerifier::CheckIntraClassDataItemFields(ClassDataItemIterator* it, 788e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe bool* have_class, 789e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint16_t* class_type_index, 790e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t* class_access_flags) { 791e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe DCHECK(it != nullptr); 792e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // These calls use the raw access flags to check whether the whole dex field is valid. 793e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t prev_index = 0; 794e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe for (; kStatic ? it->HasNextStaticField() : it->HasNextInstanceField(); it->Next()) { 795e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t curr_index = it->GetMemberIndex(); 796e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!CheckOrderAndGetClassFlags(true, 797e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kStatic ? "static field" : "instance field", 798e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe curr_index, 799e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe prev_index, 800e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe have_class, 801e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe class_type_index, 802e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe class_access_flags)) { 803ec96923620cb6f8a8f8205d89ea30f147e76e7faJeff Hao return false; 804ec96923620cb6f8a8f8205d89ea30f147e76e7faJeff Hao } 805ec96923620cb6f8a8f8205d89ea30f147e76e7faJeff Hao prev_index = curr_index; 806e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 807e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!CheckClassDataItemField(curr_index, 808e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe it->GetRawMemberAccessFlags(), 809e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *class_access_flags, 810e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *class_type_index, 811e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kStatic)) { 81210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 81310037c866b04550fc5461058c398c2e3e509381ajeffhao } 81410037c866b04550fc5461058c398c2e3e509381ajeffhao } 815e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 816e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return true; 817e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe} 818e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 819e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampetemplate <bool kDirect> 820e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampebool DexFileVerifier::CheckIntraClassDataItemMethods( 821e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe ClassDataItemIterator* it, 822e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe std::unordered_set<uint32_t>* direct_method_indexes, 823e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe bool* have_class, 824e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint16_t* class_type_index, 825e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t* class_access_flags) { 826e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t prev_index = 0; 827e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe for (; kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod(); it->Next()) { 828e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t curr_index = it->GetMemberIndex(); 829e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!CheckOrderAndGetClassFlags(false, 830e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kDirect ? "direct method" : "virtual method", 831e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe curr_index, 832e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe prev_index, 833e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe have_class, 834e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe class_type_index, 835e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe class_access_flags)) { 836ec96923620cb6f8a8f8205d89ea30f147e76e7faJeff Hao return false; 837ec96923620cb6f8a8f8205d89ea30f147e76e7faJeff Hao } 838ec96923620cb6f8a8f8205d89ea30f147e76e7faJeff Hao prev_index = curr_index; 839e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 840e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!CheckClassDataItemMethod(curr_index, 841e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe it->GetRawMemberAccessFlags(), 842e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *class_access_flags, 843e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *class_type_index, 844e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe it->GetMethodCodeItemOffset(), 845e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe direct_method_indexes, 846e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kDirect)) { 84710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 84810037c866b04550fc5461058c398c2e3e509381ajeffhao } 84910037c866b04550fc5461058c398c2e3e509381ajeffhao } 85010037c866b04550fc5461058c398c2e3e509381ajeffhao 851e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return true; 852e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe} 853e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 854e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampebool DexFileVerifier::CheckIntraClassDataItem() { 855e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe ClassDataItemIterator it(*dex_file_, ptr_); 856e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe std::unordered_set<uint32_t> direct_method_indexes; 857e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 858e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // This code is complicated by the fact that we don't directly know which class this belongs to. 859e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // So we need to explicitly search with the first item we find (either field or method), and then, 860e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // as the lookup is expensive, cache the result. 861e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe bool have_class = false; 862e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint16_t class_type_index; 863e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t class_access_flags; 864e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 865e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Check fields. 866e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!CheckIntraClassDataItemFields<true>(&it, 867e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &have_class, 868e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &class_type_index, 869e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &class_access_flags)) { 870e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 871e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 872e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!CheckIntraClassDataItemFields<false>(&it, 873e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &have_class, 874e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &class_type_index, 875e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &class_access_flags)) { 876e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 877e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 878e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 879e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Check methods. 880e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!CheckIntraClassDataItemMethods<true>(&it, 881e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &direct_method_indexes, 882e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &have_class, 883e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &class_type_index, 884e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &class_access_flags)) { 885e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 886e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 887e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!CheckIntraClassDataItemMethods<false>(&it, 888e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &direct_method_indexes, 889e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &have_class, 890e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &class_type_index, 891e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe &class_access_flags)) { 892e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 893e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 894e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 89510037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ = it.EndDataPointer(); 89610037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 89710037c866b04550fc5461058c398c2e3e509381ajeffhao} 89810037c866b04550fc5461058c398c2e3e509381ajeffhao 89910037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckIntraCodeItem() { 90010037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::CodeItem* code_item = reinterpret_cast<const DexFile::CodeItem*>(ptr_); 90150d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe if (!CheckListSize(code_item, 1, sizeof(DexFile::CodeItem), "code")) { 90210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 90310037c866b04550fc5461058c398c2e3e509381ajeffhao } 90410037c866b04550fc5461058c398c2e3e509381ajeffhao 9058d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(code_item->ins_size_ > code_item->registers_size_)) { 9068d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("ins_size (%ud) > registers_size (%ud)", 9078d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers code_item->ins_size_, code_item->registers_size_); 90810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 90910037c866b04550fc5461058c398c2e3e509381ajeffhao } 91010037c866b04550fc5461058c398c2e3e509381ajeffhao 9118d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((code_item->outs_size_ > 5) && 9128d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers (code_item->outs_size_ > code_item->registers_size_))) { 91310037c866b04550fc5461058c398c2e3e509381ajeffhao /* 91410037c866b04550fc5461058c398c2e3e509381ajeffhao * outs_size can be up to 5, even if registers_size is smaller, since the 91510037c866b04550fc5461058c398c2e3e509381ajeffhao * short forms of method invocation allow repetitions of a register multiple 91610037c866b04550fc5461058c398c2e3e509381ajeffhao * times within a single parameter list. However, longer parameter lists 91710037c866b04550fc5461058c398c2e3e509381ajeffhao * need to be represented in-order in the register file. 91810037c866b04550fc5461058c398c2e3e509381ajeffhao */ 9198d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("outs_size (%ud) > registers_size (%ud)", 9208d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers code_item->outs_size_, code_item->registers_size_); 92110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 92210037c866b04550fc5461058c398c2e3e509381ajeffhao } 92310037c866b04550fc5461058c398c2e3e509381ajeffhao 92410037c866b04550fc5461058c398c2e3e509381ajeffhao const uint16_t* insns = code_item->insns_; 92510037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t insns_size = code_item->insns_size_in_code_units_; 92610037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckListSize(insns, insns_size, sizeof(uint16_t), "insns size")) { 92710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 92810037c866b04550fc5461058c398c2e3e509381ajeffhao } 92910037c866b04550fc5461058c398c2e3e509381ajeffhao 93010037c866b04550fc5461058c398c2e3e509381ajeffhao // Grab the end of the insns if there are no try_items. 93110037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t try_items_size = code_item->tries_size_; 93210037c866b04550fc5461058c398c2e3e509381ajeffhao if (try_items_size == 0) { 93313735955f39b3b304c37d2b2840663c131262c18Ian Rogers ptr_ = reinterpret_cast<const uint8_t*>(&insns[insns_size]); 93410037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 93510037c866b04550fc5461058c398c2e3e509381ajeffhao } 93610037c866b04550fc5461058c398c2e3e509381ajeffhao 93710037c866b04550fc5461058c398c2e3e509381ajeffhao // try_items are 4-byte aligned. Verify the spacer is 0. 9388a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers if (((reinterpret_cast<uintptr_t>(&insns[insns_size]) & 3) != 0) && (insns[insns_size] != 0)) { 9398d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Non-zero padding: %x", insns[insns_size]); 94010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 94110037c866b04550fc5461058c398c2e3e509381ajeffhao } 94210037c866b04550fc5461058c398c2e3e509381ajeffhao 94310037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::TryItem* try_items = DexFile::GetTryItems(*code_item, 0); 94410037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckListSize(try_items, try_items_size, sizeof(DexFile::TryItem), "try_items size")) { 94510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 94610037c866b04550fc5461058c398c2e3e509381ajeffhao } 94710037c866b04550fc5461058c398c2e3e509381ajeffhao 9486a8df53d90e47e3256faf7ff0caed0acf377b99bAnestis Bechtsoudis ptr_ = DexFile::GetCatchHandlerData(*code_item, 0); 9496a8df53d90e47e3256faf7ff0caed0acf377b99bAnestis Bechtsoudis uint32_t handlers_size = DecodeUnsignedLeb128(&ptr_); 9506a8df53d90e47e3256faf7ff0caed0acf377b99bAnestis Bechtsoudis 9518d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((handlers_size == 0) || (handlers_size >= 65536))) { 9528d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Invalid handlers_size: %ud", handlers_size); 95310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 95410037c866b04550fc5461058c398c2e3e509381ajeffhao } 95510037c866b04550fc5461058c398c2e3e509381ajeffhao 956700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers std::unique_ptr<uint32_t[]> handler_offsets(new uint32_t[handlers_size]); 957ee0fa76b2e5d39ad36d1ff144b2d0270df81e606Elliott Hughes if (!CheckAndGetHandlerOffsets(code_item, &handler_offsets[0], handlers_size)) { 95810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 95910037c866b04550fc5461058c398c2e3e509381ajeffhao } 96010037c866b04550fc5461058c398c2e3e509381ajeffhao 96110037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t last_addr = 0; 96210037c866b04550fc5461058c398c2e3e509381ajeffhao while (try_items_size--) { 9638d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(try_items->start_addr_ < last_addr)) { 9648d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of_order try_item with start_addr: %x", try_items->start_addr_); 96510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 96610037c866b04550fc5461058c398c2e3e509381ajeffhao } 96710037c866b04550fc5461058c398c2e3e509381ajeffhao 9688d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(try_items->start_addr_ >= insns_size)) { 9698d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Invalid try_item start_addr: %x", try_items->start_addr_); 97010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 97110037c866b04550fc5461058c398c2e3e509381ajeffhao } 97210037c866b04550fc5461058c398c2e3e509381ajeffhao 97310037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t i; 97410037c866b04550fc5461058c398c2e3e509381ajeffhao for (i = 0; i < handlers_size; i++) { 97510037c866b04550fc5461058c398c2e3e509381ajeffhao if (try_items->handler_off_ == handler_offsets[i]) { 97610037c866b04550fc5461058c398c2e3e509381ajeffhao break; 97710037c866b04550fc5461058c398c2e3e509381ajeffhao } 97810037c866b04550fc5461058c398c2e3e509381ajeffhao } 97910037c866b04550fc5461058c398c2e3e509381ajeffhao 9808d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(i == handlers_size)) { 9818d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bogus handler offset: %x", try_items->handler_off_); 98210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 98310037c866b04550fc5461058c398c2e3e509381ajeffhao } 98410037c866b04550fc5461058c398c2e3e509381ajeffhao 98510037c866b04550fc5461058c398c2e3e509381ajeffhao last_addr = try_items->start_addr_ + try_items->insn_count_; 9868d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(last_addr > insns_size)) { 9878d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Invalid try_item insn_count: %x", try_items->insn_count_); 98810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 98910037c866b04550fc5461058c398c2e3e509381ajeffhao } 99010037c866b04550fc5461058c398c2e3e509381ajeffhao 99110037c866b04550fc5461058c398c2e3e509381ajeffhao try_items++; 99210037c866b04550fc5461058c398c2e3e509381ajeffhao } 99310037c866b04550fc5461058c398c2e3e509381ajeffhao 99410037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 99510037c866b04550fc5461058c398c2e3e509381ajeffhao} 99610037c866b04550fc5461058c398c2e3e509381ajeffhao 99710037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckIntraStringDataItem() { 99810037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t size = DecodeUnsignedLeb128(&ptr_); 99913735955f39b3b304c37d2b2840663c131262c18Ian Rogers const uint8_t* file_end = begin_ + size_; 100010037c866b04550fc5461058c398c2e3e509381ajeffhao 100110037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 0; i < size; i++) { 1002c647564845429bd709ed3338c13f15063c2f9fd9Brian Carlstrom CHECK_LT(i, size); // b/15014252 Prevents hitting the impossible case below 10038d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(ptr_ >= file_end)) { 10048d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("String data would go beyond end-of-file"); 100510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 100610037c866b04550fc5461058c398c2e3e509381ajeffhao } 100710037c866b04550fc5461058c398c2e3e509381ajeffhao 100810037c866b04550fc5461058c398c2e3e509381ajeffhao uint8_t byte = *(ptr_++); 100910037c866b04550fc5461058c398c2e3e509381ajeffhao 101010037c866b04550fc5461058c398c2e3e509381ajeffhao // Switch on the high 4 bits. 101110037c866b04550fc5461058c398c2e3e509381ajeffhao switch (byte >> 4) { 101210037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x00: 101310037c866b04550fc5461058c398c2e3e509381ajeffhao // Special case of bit pattern 0xxx. 10148d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(byte == 0)) { 1015c647564845429bd709ed3338c13f15063c2f9fd9Brian Carlstrom CHECK_LT(i, size); // b/15014252 Actually hit this impossible case with clang 10168d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("String data shorter than indicated utf16_size %x", size); 101710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 101810037c866b04550fc5461058c398c2e3e509381ajeffhao } 101910037c866b04550fc5461058c398c2e3e509381ajeffhao break; 102010037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x01: 102110037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x02: 102210037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x03: 102310037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x04: 102410037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x05: 102510037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x06: 102610037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x07: 102710037c866b04550fc5461058c398c2e3e509381ajeffhao // No extra checks necessary for bit pattern 0xxx. 102810037c866b04550fc5461058c398c2e3e509381ajeffhao break; 102910037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x08: 103010037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x09: 103110037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x0a: 103210037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x0b: 103310037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x0f: 103410037c866b04550fc5461058c398c2e3e509381ajeffhao // Illegal bit patterns 10xx or 1111. 103510037c866b04550fc5461058c398c2e3e509381ajeffhao // Note: 1111 is valid for normal UTF-8, but not here. 10368d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Illegal start byte %x in string data", byte); 103710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 103810037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x0c: 103910037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x0d: { 104010037c866b04550fc5461058c398c2e3e509381ajeffhao // Bit pattern 110x has an additional byte. 104110037c866b04550fc5461058c398c2e3e509381ajeffhao uint8_t byte2 = *(ptr_++); 10428d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((byte2 & 0xc0) != 0x80)) { 10438d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Illegal continuation byte %x in string data", byte2); 104410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 104510037c866b04550fc5461058c398c2e3e509381ajeffhao } 104610037c866b04550fc5461058c398c2e3e509381ajeffhao uint16_t value = ((byte & 0x1f) << 6) | (byte2 & 0x3f); 10478d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((value != 0) && (value < 0x80))) { 10488d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Illegal representation for value %x in string data", value); 104910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 105010037c866b04550fc5461058c398c2e3e509381ajeffhao } 105110037c866b04550fc5461058c398c2e3e509381ajeffhao break; 105210037c866b04550fc5461058c398c2e3e509381ajeffhao } 105310037c866b04550fc5461058c398c2e3e509381ajeffhao case 0x0e: { 105410037c866b04550fc5461058c398c2e3e509381ajeffhao // Bit pattern 1110 has 2 additional bytes. 105510037c866b04550fc5461058c398c2e3e509381ajeffhao uint8_t byte2 = *(ptr_++); 10568d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((byte2 & 0xc0) != 0x80)) { 10578d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Illegal continuation byte %x in string data", byte2); 105810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 105910037c866b04550fc5461058c398c2e3e509381ajeffhao } 106010037c866b04550fc5461058c398c2e3e509381ajeffhao uint8_t byte3 = *(ptr_++); 10618d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((byte3 & 0xc0) != 0x80)) { 10628d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Illegal continuation byte %x in string data", byte3); 106310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 106410037c866b04550fc5461058c398c2e3e509381ajeffhao } 106510037c866b04550fc5461058c398c2e3e509381ajeffhao uint16_t value = ((byte & 0x0f) << 12) | ((byte2 & 0x3f) << 6) | (byte3 & 0x3f); 10668d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(value < 0x800)) { 10678d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Illegal representation for value %x in string data", value); 106810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 106910037c866b04550fc5461058c398c2e3e509381ajeffhao } 107010037c866b04550fc5461058c398c2e3e509381ajeffhao break; 107110037c866b04550fc5461058c398c2e3e509381ajeffhao } 107210037c866b04550fc5461058c398c2e3e509381ajeffhao } 107310037c866b04550fc5461058c398c2e3e509381ajeffhao } 107410037c866b04550fc5461058c398c2e3e509381ajeffhao 10758d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(*(ptr_++) != '\0')) { 10768d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("String longer than indicated size %x", size); 107710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 107810037c866b04550fc5461058c398c2e3e509381ajeffhao } 107910037c866b04550fc5461058c398c2e3e509381ajeffhao 108010037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 108110037c866b04550fc5461058c398c2e3e509381ajeffhao} 108210037c866b04550fc5461058c398c2e3e509381ajeffhao 108310037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckIntraDebugInfoItem() { 108410037c866b04550fc5461058c398c2e3e509381ajeffhao DecodeUnsignedLeb128(&ptr_); 108510037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t parameters_size = DecodeUnsignedLeb128(&ptr_); 10868d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(parameters_size > 65536)) { 10878d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Invalid parameters_size: %x", parameters_size); 108810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 108910037c866b04550fc5461058c398c2e3e509381ajeffhao } 109010037c866b04550fc5461058c398c2e3e509381ajeffhao 109110037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t j = 0; j < parameters_size; j++) { 109210037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t parameter_name = DecodeUnsignedLeb128(&ptr_); 109310037c866b04550fc5461058c398c2e3e509381ajeffhao if (parameter_name != 0) { 109410037c866b04550fc5461058c398c2e3e509381ajeffhao parameter_name--; 109510037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(parameter_name, header_->string_ids_size_, "debug_info_item parameter_name")) { 109610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 109710037c866b04550fc5461058c398c2e3e509381ajeffhao } 109810037c866b04550fc5461058c398c2e3e509381ajeffhao } 109910037c866b04550fc5461058c398c2e3e509381ajeffhao } 110010037c866b04550fc5461058c398c2e3e509381ajeffhao 110110037c866b04550fc5461058c398c2e3e509381ajeffhao while (true) { 110210037c866b04550fc5461058c398c2e3e509381ajeffhao uint8_t opcode = *(ptr_++); 110310037c866b04550fc5461058c398c2e3e509381ajeffhao switch (opcode) { 110410037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::DBG_END_SEQUENCE: { 110510037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 110610037c866b04550fc5461058c398c2e3e509381ajeffhao } 110710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::DBG_ADVANCE_PC: { 110810037c866b04550fc5461058c398c2e3e509381ajeffhao DecodeUnsignedLeb128(&ptr_); 110910037c866b04550fc5461058c398c2e3e509381ajeffhao break; 111010037c866b04550fc5461058c398c2e3e509381ajeffhao } 111110037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::DBG_ADVANCE_LINE: { 111210037c866b04550fc5461058c398c2e3e509381ajeffhao DecodeSignedLeb128(&ptr_); 111310037c866b04550fc5461058c398c2e3e509381ajeffhao break; 111410037c866b04550fc5461058c398c2e3e509381ajeffhao } 111510037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::DBG_START_LOCAL: { 111610037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t reg_num = DecodeUnsignedLeb128(&ptr_); 11178d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(reg_num >= 65536)) { 11188d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad reg_num for opcode %x", opcode); 111910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 112010037c866b04550fc5461058c398c2e3e509381ajeffhao } 112110037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t name_idx = DecodeUnsignedLeb128(&ptr_); 112210037c866b04550fc5461058c398c2e3e509381ajeffhao if (name_idx != 0) { 112310037c866b04550fc5461058c398c2e3e509381ajeffhao name_idx--; 112410037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_START_LOCAL name_idx")) { 112510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 112610037c866b04550fc5461058c398c2e3e509381ajeffhao } 112710037c866b04550fc5461058c398c2e3e509381ajeffhao } 112810037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t type_idx = DecodeUnsignedLeb128(&ptr_); 112910037c866b04550fc5461058c398c2e3e509381ajeffhao if (type_idx != 0) { 113010037c866b04550fc5461058c398c2e3e509381ajeffhao type_idx--; 1131dd3208d3b2f4b78678a341f38a5cc7761c7fca91Logan Chien if (!CheckIndex(type_idx, header_->type_ids_size_, "DBG_START_LOCAL type_idx")) { 113210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 113310037c866b04550fc5461058c398c2e3e509381ajeffhao } 113410037c866b04550fc5461058c398c2e3e509381ajeffhao } 113510037c866b04550fc5461058c398c2e3e509381ajeffhao break; 113610037c866b04550fc5461058c398c2e3e509381ajeffhao } 113710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::DBG_END_LOCAL: 113810037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::DBG_RESTART_LOCAL: { 113910037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t reg_num = DecodeUnsignedLeb128(&ptr_); 11408d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(reg_num >= 65536)) { 11418d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad reg_num for opcode %x", opcode); 114210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 114310037c866b04550fc5461058c398c2e3e509381ajeffhao } 114410037c866b04550fc5461058c398c2e3e509381ajeffhao break; 114510037c866b04550fc5461058c398c2e3e509381ajeffhao } 114610037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::DBG_START_LOCAL_EXTENDED: { 114710037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t reg_num = DecodeUnsignedLeb128(&ptr_); 11488d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(reg_num >= 65536)) { 11498d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad reg_num for opcode %x", opcode); 115010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 115110037c866b04550fc5461058c398c2e3e509381ajeffhao } 115210037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t name_idx = DecodeUnsignedLeb128(&ptr_); 115310037c866b04550fc5461058c398c2e3e509381ajeffhao if (name_idx != 0) { 115410037c866b04550fc5461058c398c2e3e509381ajeffhao name_idx--; 115510037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED name_idx")) { 115610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 115710037c866b04550fc5461058c398c2e3e509381ajeffhao } 115810037c866b04550fc5461058c398c2e3e509381ajeffhao } 115910037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t type_idx = DecodeUnsignedLeb128(&ptr_); 116010037c866b04550fc5461058c398c2e3e509381ajeffhao if (type_idx != 0) { 116110037c866b04550fc5461058c398c2e3e509381ajeffhao type_idx--; 1162dd3208d3b2f4b78678a341f38a5cc7761c7fca91Logan Chien if (!CheckIndex(type_idx, header_->type_ids_size_, "DBG_START_LOCAL_EXTENDED type_idx")) { 116310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 116410037c866b04550fc5461058c398c2e3e509381ajeffhao } 116510037c866b04550fc5461058c398c2e3e509381ajeffhao } 116610037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t sig_idx = DecodeUnsignedLeb128(&ptr_); 116710037c866b04550fc5461058c398c2e3e509381ajeffhao if (sig_idx != 0) { 116810037c866b04550fc5461058c398c2e3e509381ajeffhao sig_idx--; 116910037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(sig_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED sig_idx")) { 117010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 117110037c866b04550fc5461058c398c2e3e509381ajeffhao } 117210037c866b04550fc5461058c398c2e3e509381ajeffhao } 117310037c866b04550fc5461058c398c2e3e509381ajeffhao break; 117410037c866b04550fc5461058c398c2e3e509381ajeffhao } 117510037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::DBG_SET_FILE: { 117610037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t name_idx = DecodeUnsignedLeb128(&ptr_); 117710037c866b04550fc5461058c398c2e3e509381ajeffhao if (name_idx != 0) { 117810037c866b04550fc5461058c398c2e3e509381ajeffhao name_idx--; 117910037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_SET_FILE name_idx")) { 118010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 118110037c866b04550fc5461058c398c2e3e509381ajeffhao } 118210037c866b04550fc5461058c398c2e3e509381ajeffhao } 118310037c866b04550fc5461058c398c2e3e509381ajeffhao break; 118410037c866b04550fc5461058c398c2e3e509381ajeffhao } 118510037c866b04550fc5461058c398c2e3e509381ajeffhao } 118610037c866b04550fc5461058c398c2e3e509381ajeffhao } 118710037c866b04550fc5461058c398c2e3e509381ajeffhao} 118810037c866b04550fc5461058c398c2e3e509381ajeffhao 118910037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckIntraAnnotationItem() { 119013735955f39b3b304c37d2b2840663c131262c18Ian Rogers if (!CheckListSize(ptr_, 1, sizeof(uint8_t), "annotation visibility")) { 119110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 119210037c866b04550fc5461058c398c2e3e509381ajeffhao } 119310037c866b04550fc5461058c398c2e3e509381ajeffhao 119410037c866b04550fc5461058c398c2e3e509381ajeffhao // Check visibility 119510037c866b04550fc5461058c398c2e3e509381ajeffhao switch (*(ptr_++)) { 119610037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexVisibilityBuild: 119710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexVisibilityRuntime: 119810037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexVisibilitySystem: 119910037c866b04550fc5461058c398c2e3e509381ajeffhao break; 120010037c866b04550fc5461058c398c2e3e509381ajeffhao default: 12018d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad annotation visibility: %x", *ptr_); 120210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 120310037c866b04550fc5461058c398c2e3e509381ajeffhao } 120410037c866b04550fc5461058c398c2e3e509381ajeffhao 120510037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckEncodedAnnotation()) { 120610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 120710037c866b04550fc5461058c398c2e3e509381ajeffhao } 120810037c866b04550fc5461058c398c2e3e509381ajeffhao 120910037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 121010037c866b04550fc5461058c398c2e3e509381ajeffhao} 121110037c866b04550fc5461058c398c2e3e509381ajeffhao 121210037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckIntraAnnotationsDirectoryItem() { 121310037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::AnnotationsDirectoryItem* item = 121410037c866b04550fc5461058c398c2e3e509381ajeffhao reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr_); 121550d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe if (!CheckListSize(item, 1, sizeof(DexFile::AnnotationsDirectoryItem), "annotations_directory")) { 121610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 121710037c866b04550fc5461058c398c2e3e509381ajeffhao } 121810037c866b04550fc5461058c398c2e3e509381ajeffhao 121910037c866b04550fc5461058c398c2e3e509381ajeffhao // Field annotations follow immediately after the annotations directory. 122010037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::FieldAnnotationsItem* field_item = 122110037c866b04550fc5461058c398c2e3e509381ajeffhao reinterpret_cast<const DexFile::FieldAnnotationsItem*>(item + 1); 122210037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t field_count = item->fields_size_; 122310037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckListSize(field_item, field_count, sizeof(DexFile::FieldAnnotationsItem), "field_annotations list")) { 122410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 122510037c866b04550fc5461058c398c2e3e509381ajeffhao } 122610037c866b04550fc5461058c398c2e3e509381ajeffhao 122710037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t last_idx = 0; 122810037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 0; i < field_count; i++) { 12298d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(last_idx >= field_item->field_idx_ && i != 0)) { 12308d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order field_idx for annotation: %x then %x", last_idx, field_item->field_idx_); 123110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 123210037c866b04550fc5461058c398c2e3e509381ajeffhao } 123310037c866b04550fc5461058c398c2e3e509381ajeffhao last_idx = field_item->field_idx_; 123410037c866b04550fc5461058c398c2e3e509381ajeffhao field_item++; 123510037c866b04550fc5461058c398c2e3e509381ajeffhao } 123610037c866b04550fc5461058c398c2e3e509381ajeffhao 123710037c866b04550fc5461058c398c2e3e509381ajeffhao // Method annotations follow immediately after field annotations. 123810037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::MethodAnnotationsItem* method_item = 123910037c866b04550fc5461058c398c2e3e509381ajeffhao reinterpret_cast<const DexFile::MethodAnnotationsItem*>(field_item); 124010037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t method_count = item->methods_size_; 124110037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckListSize(method_item, method_count, sizeof(DexFile::MethodAnnotationsItem), "method_annotations list")) { 124210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 124310037c866b04550fc5461058c398c2e3e509381ajeffhao } 124410037c866b04550fc5461058c398c2e3e509381ajeffhao 124510037c866b04550fc5461058c398c2e3e509381ajeffhao last_idx = 0; 124610037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 0; i < method_count; i++) { 12478d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(last_idx >= method_item->method_idx_ && i != 0)) { 12488d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order method_idx for annotation: %x then %x", 12498d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers last_idx, method_item->method_idx_); 125010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 125110037c866b04550fc5461058c398c2e3e509381ajeffhao } 125210037c866b04550fc5461058c398c2e3e509381ajeffhao last_idx = method_item->method_idx_; 125310037c866b04550fc5461058c398c2e3e509381ajeffhao method_item++; 125410037c866b04550fc5461058c398c2e3e509381ajeffhao } 125510037c866b04550fc5461058c398c2e3e509381ajeffhao 125610037c866b04550fc5461058c398c2e3e509381ajeffhao // Parameter annotations follow immediately after method annotations. 125710037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::ParameterAnnotationsItem* parameter_item = 125810037c866b04550fc5461058c398c2e3e509381ajeffhao reinterpret_cast<const DexFile::ParameterAnnotationsItem*>(method_item); 125910037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t parameter_count = item->parameters_size_; 12602b87ddf36abff711fa2233c49bffc7ceb03b15d7Dragos Sbirlea if (!CheckListSize(parameter_item, parameter_count, sizeof(DexFile::ParameterAnnotationsItem), 12618d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers "parameter_annotations list")) { 126210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 126310037c866b04550fc5461058c398c2e3e509381ajeffhao } 126410037c866b04550fc5461058c398c2e3e509381ajeffhao 126510037c866b04550fc5461058c398c2e3e509381ajeffhao last_idx = 0; 126610037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 0; i < parameter_count; i++) { 12678d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(last_idx >= parameter_item->method_idx_ && i != 0)) { 12688d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order method_idx for annotation: %x then %x", 12698d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers last_idx, parameter_item->method_idx_); 127010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 127110037c866b04550fc5461058c398c2e3e509381ajeffhao } 127210037c866b04550fc5461058c398c2e3e509381ajeffhao last_idx = parameter_item->method_idx_; 127310037c866b04550fc5461058c398c2e3e509381ajeffhao parameter_item++; 127410037c866b04550fc5461058c398c2e3e509381ajeffhao } 127510037c866b04550fc5461058c398c2e3e509381ajeffhao 127610037c866b04550fc5461058c398c2e3e509381ajeffhao // Return a pointer to the end of the annotations. 127713735955f39b3b304c37d2b2840663c131262c18Ian Rogers ptr_ = reinterpret_cast<const uint8_t*>(parameter_item); 127810037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 127910037c866b04550fc5461058c398c2e3e509381ajeffhao} 128010037c866b04550fc5461058c398c2e3e509381ajeffhao 1281b061cc1dc41a976f5a620c19498d4b2f4d1675dbAndreas Gampebool DexFileVerifier::CheckIntraSectionIterate(size_t offset, uint32_t section_count, 1282b061cc1dc41a976f5a620c19498d4b2f4d1675dbAndreas Gampe uint16_t type) { 128310037c866b04550fc5461058c398c2e3e509381ajeffhao // Get the right alignment mask for the type of section. 12848a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers size_t alignment_mask; 128510037c866b04550fc5461058c398c2e3e509381ajeffhao switch (type) { 128610037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDataItem: 128710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeStringDataItem: 128810037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeDebugInfoItem: 128910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationItem: 129010037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeEncodedArrayItem: 129110037c866b04550fc5461058c398c2e3e509381ajeffhao alignment_mask = sizeof(uint8_t) - 1; 129210037c866b04550fc5461058c398c2e3e509381ajeffhao break; 129310037c866b04550fc5461058c398c2e3e509381ajeffhao default: 129410037c866b04550fc5461058c398c2e3e509381ajeffhao alignment_mask = sizeof(uint32_t) - 1; 129510037c866b04550fc5461058c398c2e3e509381ajeffhao break; 129610037c866b04550fc5461058c398c2e3e509381ajeffhao } 129710037c866b04550fc5461058c398c2e3e509381ajeffhao 129810037c866b04550fc5461058c398c2e3e509381ajeffhao // Iterate through the items in the section. 1299b061cc1dc41a976f5a620c19498d4b2f4d1675dbAndreas Gampe for (uint32_t i = 0; i < section_count; i++) { 13008a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers size_t aligned_offset = (offset + alignment_mask) & ~alignment_mask; 130110037c866b04550fc5461058c398c2e3e509381ajeffhao 130210037c866b04550fc5461058c398c2e3e509381ajeffhao // Check the padding between items. 130310037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckPadding(offset, aligned_offset)) { 130410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 130510037c866b04550fc5461058c398c2e3e509381ajeffhao } 130610037c866b04550fc5461058c398c2e3e509381ajeffhao 130710037c866b04550fc5461058c398c2e3e509381ajeffhao // Check depending on the section type. 130810037c866b04550fc5461058c398c2e3e509381ajeffhao switch (type) { 130910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeStringIdItem: { 131050d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe if (!CheckListSize(ptr_, 1, sizeof(DexFile::StringId), "string_ids")) { 131110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 131210037c866b04550fc5461058c398c2e3e509381ajeffhao } 131310037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += sizeof(DexFile::StringId); 131410037c866b04550fc5461058c398c2e3e509381ajeffhao break; 131510037c866b04550fc5461058c398c2e3e509381ajeffhao } 131610037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeTypeIdItem: { 131750d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe if (!CheckListSize(ptr_, 1, sizeof(DexFile::TypeId), "type_ids")) { 131810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 131910037c866b04550fc5461058c398c2e3e509381ajeffhao } 132010037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += sizeof(DexFile::TypeId); 132110037c866b04550fc5461058c398c2e3e509381ajeffhao break; 132210037c866b04550fc5461058c398c2e3e509381ajeffhao } 132310037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeProtoIdItem: { 132450d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe if (!CheckListSize(ptr_, 1, sizeof(DexFile::ProtoId), "proto_ids")) { 132510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 132610037c866b04550fc5461058c398c2e3e509381ajeffhao } 132710037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += sizeof(DexFile::ProtoId); 132810037c866b04550fc5461058c398c2e3e509381ajeffhao break; 132910037c866b04550fc5461058c398c2e3e509381ajeffhao } 133010037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeFieldIdItem: { 133150d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe if (!CheckListSize(ptr_, 1, sizeof(DexFile::FieldId), "field_ids")) { 133210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 133310037c866b04550fc5461058c398c2e3e509381ajeffhao } 133410037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += sizeof(DexFile::FieldId); 133510037c866b04550fc5461058c398c2e3e509381ajeffhao break; 133610037c866b04550fc5461058c398c2e3e509381ajeffhao } 133710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeMethodIdItem: { 133850d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe if (!CheckListSize(ptr_, 1, sizeof(DexFile::MethodId), "method_ids")) { 133910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 134010037c866b04550fc5461058c398c2e3e509381ajeffhao } 134110037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += sizeof(DexFile::MethodId); 134210037c866b04550fc5461058c398c2e3e509381ajeffhao break; 134310037c866b04550fc5461058c398c2e3e509381ajeffhao } 134410037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDefItem: { 134550d1bc198b2e347d60df74c3b0c452e1f929dd2fAndreas Gampe if (!CheckListSize(ptr_, 1, sizeof(DexFile::ClassDef), "class_defs")) { 134610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 134710037c866b04550fc5461058c398c2e3e509381ajeffhao } 134810037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += sizeof(DexFile::ClassDef); 134910037c866b04550fc5461058c398c2e3e509381ajeffhao break; 135010037c866b04550fc5461058c398c2e3e509381ajeffhao } 135110037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeTypeList: { 1352d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe if (!CheckList(sizeof(DexFile::TypeItem), "type_list", &ptr_)) { 135310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 135410037c866b04550fc5461058c398c2e3e509381ajeffhao } 135510037c866b04550fc5461058c398c2e3e509381ajeffhao break; 135610037c866b04550fc5461058c398c2e3e509381ajeffhao } 135710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationSetRefList: { 1358d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe if (!CheckList(sizeof(DexFile::AnnotationSetRefItem), "annotation_set_ref_list", &ptr_)) { 135910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 136010037c866b04550fc5461058c398c2e3e509381ajeffhao } 136110037c866b04550fc5461058c398c2e3e509381ajeffhao break; 136210037c866b04550fc5461058c398c2e3e509381ajeffhao } 136310037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationSetItem: { 1364d4ae41fd7a4ed711277c61c0d7fd2a3759458728Andreas Gampe if (!CheckList(sizeof(uint32_t), "annotation_set_item", &ptr_)) { 136510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 136610037c866b04550fc5461058c398c2e3e509381ajeffhao } 136710037c866b04550fc5461058c398c2e3e509381ajeffhao break; 136810037c866b04550fc5461058c398c2e3e509381ajeffhao } 136910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDataItem: { 137010037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIntraClassDataItem()) { 137110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 137210037c866b04550fc5461058c398c2e3e509381ajeffhao } 137310037c866b04550fc5461058c398c2e3e509381ajeffhao break; 137410037c866b04550fc5461058c398c2e3e509381ajeffhao } 137510037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeCodeItem: { 137610037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIntraCodeItem()) { 137710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 137810037c866b04550fc5461058c398c2e3e509381ajeffhao } 137910037c866b04550fc5461058c398c2e3e509381ajeffhao break; 138010037c866b04550fc5461058c398c2e3e509381ajeffhao } 138110037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeStringDataItem: { 138210037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIntraStringDataItem()) { 138310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 138410037c866b04550fc5461058c398c2e3e509381ajeffhao } 138510037c866b04550fc5461058c398c2e3e509381ajeffhao break; 138610037c866b04550fc5461058c398c2e3e509381ajeffhao } 138710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeDebugInfoItem: { 138810037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIntraDebugInfoItem()) { 138910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 139010037c866b04550fc5461058c398c2e3e509381ajeffhao } 139110037c866b04550fc5461058c398c2e3e509381ajeffhao break; 139210037c866b04550fc5461058c398c2e3e509381ajeffhao } 139310037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationItem: { 139410037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIntraAnnotationItem()) { 139510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 139610037c866b04550fc5461058c398c2e3e509381ajeffhao } 139710037c866b04550fc5461058c398c2e3e509381ajeffhao break; 139810037c866b04550fc5461058c398c2e3e509381ajeffhao } 139910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeEncodedArrayItem: { 140010037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckEncodedArray()) { 140110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 140210037c866b04550fc5461058c398c2e3e509381ajeffhao } 140310037c866b04550fc5461058c398c2e3e509381ajeffhao break; 140410037c866b04550fc5461058c398c2e3e509381ajeffhao } 140510037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationsDirectoryItem: { 140610037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIntraAnnotationsDirectoryItem()) { 140710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 140810037c866b04550fc5461058c398c2e3e509381ajeffhao } 140910037c866b04550fc5461058c398c2e3e509381ajeffhao break; 141010037c866b04550fc5461058c398c2e3e509381ajeffhao } 141110037c866b04550fc5461058c398c2e3e509381ajeffhao default: 14128d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Unknown map item type %x", type); 141310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 141410037c866b04550fc5461058c398c2e3e509381ajeffhao } 141510037c866b04550fc5461058c398c2e3e509381ajeffhao 141610037c866b04550fc5461058c398c2e3e509381ajeffhao if (IsDataSectionType(type)) { 1417a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes offset_to_type_map_.Put(aligned_offset, type); 141810037c866b04550fc5461058c398c2e3e509381ajeffhao } 141910037c866b04550fc5461058c398c2e3e509381ajeffhao 14208a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers aligned_offset = ptr_ - begin_; 14218d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(aligned_offset > size_)) { 14228d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Item %d at ends out of bounds", i); 142310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 142410037c866b04550fc5461058c398c2e3e509381ajeffhao } 142510037c866b04550fc5461058c398c2e3e509381ajeffhao 142610037c866b04550fc5461058c398c2e3e509381ajeffhao offset = aligned_offset; 142710037c866b04550fc5461058c398c2e3e509381ajeffhao } 142810037c866b04550fc5461058c398c2e3e509381ajeffhao 142910037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 143010037c866b04550fc5461058c398c2e3e509381ajeffhao} 143110037c866b04550fc5461058c398c2e3e509381ajeffhao 14328a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogersbool DexFileVerifier::CheckIntraIdSection(size_t offset, uint32_t count, uint16_t type) { 143310037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t expected_offset; 143410037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t expected_size; 143510037c866b04550fc5461058c398c2e3e509381ajeffhao 143610037c866b04550fc5461058c398c2e3e509381ajeffhao // Get the expected offset and size from the header. 143710037c866b04550fc5461058c398c2e3e509381ajeffhao switch (type) { 143810037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeStringIdItem: 143910037c866b04550fc5461058c398c2e3e509381ajeffhao expected_offset = header_->string_ids_off_; 144010037c866b04550fc5461058c398c2e3e509381ajeffhao expected_size = header_->string_ids_size_; 144110037c866b04550fc5461058c398c2e3e509381ajeffhao break; 144210037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeTypeIdItem: 144310037c866b04550fc5461058c398c2e3e509381ajeffhao expected_offset = header_->type_ids_off_; 144410037c866b04550fc5461058c398c2e3e509381ajeffhao expected_size = header_->type_ids_size_; 144510037c866b04550fc5461058c398c2e3e509381ajeffhao break; 144610037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeProtoIdItem: 144710037c866b04550fc5461058c398c2e3e509381ajeffhao expected_offset = header_->proto_ids_off_; 144810037c866b04550fc5461058c398c2e3e509381ajeffhao expected_size = header_->proto_ids_size_; 144910037c866b04550fc5461058c398c2e3e509381ajeffhao break; 145010037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeFieldIdItem: 145110037c866b04550fc5461058c398c2e3e509381ajeffhao expected_offset = header_->field_ids_off_; 145210037c866b04550fc5461058c398c2e3e509381ajeffhao expected_size = header_->field_ids_size_; 145310037c866b04550fc5461058c398c2e3e509381ajeffhao break; 145410037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeMethodIdItem: 145510037c866b04550fc5461058c398c2e3e509381ajeffhao expected_offset = header_->method_ids_off_; 145610037c866b04550fc5461058c398c2e3e509381ajeffhao expected_size = header_->method_ids_size_; 145710037c866b04550fc5461058c398c2e3e509381ajeffhao break; 145810037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDefItem: 145910037c866b04550fc5461058c398c2e3e509381ajeffhao expected_offset = header_->class_defs_off_; 146010037c866b04550fc5461058c398c2e3e509381ajeffhao expected_size = header_->class_defs_size_; 146110037c866b04550fc5461058c398c2e3e509381ajeffhao break; 146210037c866b04550fc5461058c398c2e3e509381ajeffhao default: 14638d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad type for id section: %x", type); 146410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 146510037c866b04550fc5461058c398c2e3e509381ajeffhao } 146610037c866b04550fc5461058c398c2e3e509381ajeffhao 146710037c866b04550fc5461058c398c2e3e509381ajeffhao // Check that the offset and size are what were expected from the header. 14688d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(offset != expected_offset)) { 14698a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers ErrorStringPrintf("Bad offset for section: got %zx, expected %x", offset, expected_offset); 147010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 147110037c866b04550fc5461058c398c2e3e509381ajeffhao } 14728d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(count != expected_size)) { 14738d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Bad size for section: got %x, expected %x", count, expected_size); 147410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 147510037c866b04550fc5461058c398c2e3e509381ajeffhao } 147610037c866b04550fc5461058c398c2e3e509381ajeffhao 147710037c866b04550fc5461058c398c2e3e509381ajeffhao return CheckIntraSectionIterate(offset, count, type); 147810037c866b04550fc5461058c398c2e3e509381ajeffhao} 147910037c866b04550fc5461058c398c2e3e509381ajeffhao 14808a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogersbool DexFileVerifier::CheckIntraDataSection(size_t offset, uint32_t count, uint16_t type) { 14818a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers size_t data_start = header_->data_off_; 14828a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers size_t data_end = data_start + header_->data_size_; 148310037c866b04550fc5461058c398c2e3e509381ajeffhao 148410037c866b04550fc5461058c398c2e3e509381ajeffhao // Sanity check the offset of the section. 14858d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((offset < data_start) || (offset > data_end))) { 14868a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers ErrorStringPrintf("Bad offset for data subsection: %zx", offset); 148710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 148810037c866b04550fc5461058c398c2e3e509381ajeffhao } 148910037c866b04550fc5461058c398c2e3e509381ajeffhao 149010037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIntraSectionIterate(offset, count, type)) { 149110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 149210037c866b04550fc5461058c398c2e3e509381ajeffhao } 149310037c866b04550fc5461058c398c2e3e509381ajeffhao 14948a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers size_t next_offset = ptr_ - begin_; 149510037c866b04550fc5461058c398c2e3e509381ajeffhao if (next_offset > data_end) { 14968a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers ErrorStringPrintf("Out-of-bounds end of data subsection: %zx", next_offset); 149710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 149810037c866b04550fc5461058c398c2e3e509381ajeffhao } 149910037c866b04550fc5461058c398c2e3e509381ajeffhao 150010037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 150110037c866b04550fc5461058c398c2e3e509381ajeffhao} 150210037c866b04550fc5461058c398c2e3e509381ajeffhao 150310037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckIntraSection() { 150430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ + header_->map_off_); 150510037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::MapItem* item = map->list_; 150610037c866b04550fc5461058c398c2e3e509381ajeffhao 150710037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t count = map->size_; 15088a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers size_t offset = 0; 150930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers ptr_ = begin_; 151010037c866b04550fc5461058c398c2e3e509381ajeffhao 151110037c866b04550fc5461058c398c2e3e509381ajeffhao // Check the items listed in the map. 151210037c866b04550fc5461058c398c2e3e509381ajeffhao while (count--) { 151310037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t section_offset = item->offset_; 151410037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t section_count = item->size_; 151510037c866b04550fc5461058c398c2e3e509381ajeffhao uint16_t type = item->type_; 151610037c866b04550fc5461058c398c2e3e509381ajeffhao 151710037c866b04550fc5461058c398c2e3e509381ajeffhao // Check for padding and overlap between items. 151810037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckPadding(offset, section_offset)) { 151910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 15208d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers } else if (UNLIKELY(offset > section_offset)) { 15218a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers ErrorStringPrintf("Section overlap or out-of-order map: %zx, %x", offset, section_offset); 152210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 152310037c866b04550fc5461058c398c2e3e509381ajeffhao } 152410037c866b04550fc5461058c398c2e3e509381ajeffhao 152510037c866b04550fc5461058c398c2e3e509381ajeffhao // Check each item based on its type. 152610037c866b04550fc5461058c398c2e3e509381ajeffhao switch (type) { 152710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeHeaderItem: 15288d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(section_count != 1)) { 15298d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Multiple header items"); 153010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 153110037c866b04550fc5461058c398c2e3e509381ajeffhao } 15328d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(section_offset != 0)) { 15338d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Header at %x, not at start of file", section_offset); 153410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 153510037c866b04550fc5461058c398c2e3e509381ajeffhao } 153630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers ptr_ = begin_ + header_->header_size_; 153710037c866b04550fc5461058c398c2e3e509381ajeffhao offset = header_->header_size_; 153810037c866b04550fc5461058c398c2e3e509381ajeffhao break; 153910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeStringIdItem: 154010037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeTypeIdItem: 154110037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeProtoIdItem: 154210037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeFieldIdItem: 154310037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeMethodIdItem: 154410037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDefItem: 154510037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIntraIdSection(section_offset, section_count, type)) { 154610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 154710037c866b04550fc5461058c398c2e3e509381ajeffhao } 15488a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers offset = ptr_ - begin_; 154910037c866b04550fc5461058c398c2e3e509381ajeffhao break; 155010037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeMapList: 15518d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(section_count != 1)) { 15528d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Multiple map list items"); 155310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 155410037c866b04550fc5461058c398c2e3e509381ajeffhao } 15558d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(section_offset != header_->map_off_)) { 15568d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Map not at header-defined offset: %x, expected %x", 15578d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers section_offset, header_->map_off_); 155810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 155910037c866b04550fc5461058c398c2e3e509381ajeffhao } 156010037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += sizeof(uint32_t) + (map->size_ * sizeof(DexFile::MapItem)); 156110037c866b04550fc5461058c398c2e3e509381ajeffhao offset = section_offset + sizeof(uint32_t) + (map->size_ * sizeof(DexFile::MapItem)); 156210037c866b04550fc5461058c398c2e3e509381ajeffhao break; 156310037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeTypeList: 156410037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationSetRefList: 156510037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationSetItem: 156610037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDataItem: 156710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeCodeItem: 156810037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeStringDataItem: 156910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeDebugInfoItem: 157010037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationItem: 157110037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeEncodedArrayItem: 157210037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationsDirectoryItem: 157310037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIntraDataSection(section_offset, section_count, type)) { 157410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 157510037c866b04550fc5461058c398c2e3e509381ajeffhao } 15768a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers offset = ptr_ - begin_; 157710037c866b04550fc5461058c398c2e3e509381ajeffhao break; 157810037c866b04550fc5461058c398c2e3e509381ajeffhao default: 15798d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Unknown map item type %x", type); 158010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 158110037c866b04550fc5461058c398c2e3e509381ajeffhao } 158210037c866b04550fc5461058c398c2e3e509381ajeffhao 158310037c866b04550fc5461058c398c2e3e509381ajeffhao item++; 158410037c866b04550fc5461058c398c2e3e509381ajeffhao } 158510037c866b04550fc5461058c398c2e3e509381ajeffhao 158610037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 158710037c866b04550fc5461058c398c2e3e509381ajeffhao} 158810037c866b04550fc5461058c398c2e3e509381ajeffhao 15898a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogersbool DexFileVerifier::CheckOffsetToTypeMap(size_t offset, uint16_t type) { 159002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier auto it = offset_to_type_map_.find(offset); 15918d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(it == offset_to_type_map_.end())) { 15928a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers ErrorStringPrintf("No data map entry found @ %zx; expected %x", offset, type); 159310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 159410037c866b04550fc5461058c398c2e3e509381ajeffhao } 15958d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(it->second != type)) { 15968a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers ErrorStringPrintf("Unexpected data map entry @ %zx; expected %x, found %x", 15978d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers offset, type, it->second); 159810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 159910037c866b04550fc5461058c398c2e3e509381ajeffhao } 160010037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 160110037c866b04550fc5461058c398c2e3e509381ajeffhao} 160210037c866b04550fc5461058c398c2e3e509381ajeffhao 160313735955f39b3b304c37d2b2840663c131262c18Ian Rogersuint16_t DexFileVerifier::FindFirstClassDataDefiner(const uint8_t* ptr, bool* success) { 160410037c866b04550fc5461058c398c2e3e509381ajeffhao ClassDataItemIterator it(*dex_file_, ptr); 16055e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe *success = true; 160610037c866b04550fc5461058c398c2e3e509381ajeffhao 160710037c866b04550fc5461058c398c2e3e509381ajeffhao if (it.HasNextStaticField() || it.HasNextInstanceField()) { 16085e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe LOAD_FIELD(field, it.GetMemberIndex(), "first_class_data_definer field_id", 16095e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe *success = false; return DexFile::kDexNoIndex16) 1610e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return field->class_idx_; 161110037c866b04550fc5461058c398c2e3e509381ajeffhao } 161210037c866b04550fc5461058c398c2e3e509381ajeffhao 161310037c866b04550fc5461058c398c2e3e509381ajeffhao if (it.HasNextDirectMethod() || it.HasNextVirtualMethod()) { 16145e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe LOAD_METHOD(method, it.GetMemberIndex(), "first_class_data_definer method_id", 16155e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe *success = false; return DexFile::kDexNoIndex16) 1616e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return method->class_idx_; 161710037c866b04550fc5461058c398c2e3e509381ajeffhao } 161810037c866b04550fc5461058c398c2e3e509381ajeffhao 161910037c866b04550fc5461058c398c2e3e509381ajeffhao return DexFile::kDexNoIndex16; 162010037c866b04550fc5461058c398c2e3e509381ajeffhao} 162110037c866b04550fc5461058c398c2e3e509381ajeffhao 162213735955f39b3b304c37d2b2840663c131262c18Ian Rogersuint16_t DexFileVerifier::FindFirstAnnotationsDirectoryDefiner(const uint8_t* ptr, bool* success) { 162310037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::AnnotationsDirectoryItem* item = 162410037c866b04550fc5461058c398c2e3e509381ajeffhao reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr); 16255e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe *success = true; 16265e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe 162710037c866b04550fc5461058c398c2e3e509381ajeffhao if (item->fields_size_ != 0) { 162810037c866b04550fc5461058c398c2e3e509381ajeffhao DexFile::FieldAnnotationsItem* field_items = (DexFile::FieldAnnotationsItem*) (item + 1); 16295e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe LOAD_FIELD(field, field_items[0].field_idx_, "first_annotations_dir_definer field_id", 16305e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe *success = false; return DexFile::kDexNoIndex16) 1631e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return field->class_idx_; 163210037c866b04550fc5461058c398c2e3e509381ajeffhao } 163310037c866b04550fc5461058c398c2e3e509381ajeffhao 163410037c866b04550fc5461058c398c2e3e509381ajeffhao if (item->methods_size_ != 0) { 163510037c866b04550fc5461058c398c2e3e509381ajeffhao DexFile::MethodAnnotationsItem* method_items = (DexFile::MethodAnnotationsItem*) (item + 1); 1636e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_METHOD(method, method_items[0].method_idx_, "first_annotations_dir_definer method id", 16375e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe *success = false; return DexFile::kDexNoIndex16) 1638e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return method->class_idx_; 163910037c866b04550fc5461058c398c2e3e509381ajeffhao } 164010037c866b04550fc5461058c398c2e3e509381ajeffhao 164110037c866b04550fc5461058c398c2e3e509381ajeffhao if (item->parameters_size_ != 0) { 164210037c866b04550fc5461058c398c2e3e509381ajeffhao DexFile::ParameterAnnotationsItem* parameter_items = (DexFile::ParameterAnnotationsItem*) (item + 1); 1643e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_METHOD(method, parameter_items[0].method_idx_, "first_annotations_dir_definer method id", 16445e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe *success = false; return DexFile::kDexNoIndex16) 1645e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return method->class_idx_; 164610037c866b04550fc5461058c398c2e3e509381ajeffhao } 164710037c866b04550fc5461058c398c2e3e509381ajeffhao 164810037c866b04550fc5461058c398c2e3e509381ajeffhao return DexFile::kDexNoIndex16; 164910037c866b04550fc5461058c398c2e3e509381ajeffhao} 165010037c866b04550fc5461058c398c2e3e509381ajeffhao 165110037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckInterStringIdItem() { 165210037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::StringId* item = reinterpret_cast<const DexFile::StringId*>(ptr_); 165310037c866b04550fc5461058c398c2e3e509381ajeffhao 165410037c866b04550fc5461058c398c2e3e509381ajeffhao // Check the map to make sure it has the right offset->type. 165510037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckOffsetToTypeMap(item->string_data_off_, DexFile::kDexTypeStringDataItem)) { 165610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 165710037c866b04550fc5461058c398c2e3e509381ajeffhao } 165810037c866b04550fc5461058c398c2e3e509381ajeffhao 165910037c866b04550fc5461058c398c2e3e509381ajeffhao // Check ordering between items. 16602cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (previous_item_ != nullptr) { 166110037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::StringId* prev_item = reinterpret_cast<const DexFile::StringId*>(previous_item_); 166210037c866b04550fc5461058c398c2e3e509381ajeffhao const char* prev_str = dex_file_->GetStringData(*prev_item); 166310037c866b04550fc5461058c398c2e3e509381ajeffhao const char* str = dex_file_->GetStringData(*item); 16648d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(prev_str, str) >= 0)) { 16658d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order string_ids: '%s' then '%s'", prev_str, str); 166610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 166710037c866b04550fc5461058c398c2e3e509381ajeffhao } 166810037c866b04550fc5461058c398c2e3e509381ajeffhao } 166910037c866b04550fc5461058c398c2e3e509381ajeffhao 167010037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += sizeof(DexFile::StringId); 167110037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 167210037c866b04550fc5461058c398c2e3e509381ajeffhao} 167310037c866b04550fc5461058c398c2e3e509381ajeffhao 167410037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckInterTypeIdItem() { 167510037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::TypeId* item = reinterpret_cast<const DexFile::TypeId*>(ptr_); 1676e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe 1677e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_STRING(descriptor, item->descriptor_idx_, "inter_type_id_item descriptor_idx") 167810037c866b04550fc5461058c398c2e3e509381ajeffhao 167910037c866b04550fc5461058c398c2e3e509381ajeffhao // Check that the descriptor is a valid type. 16808d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(!IsValidDescriptor(descriptor))) { 16818d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Invalid type descriptor: '%s'", descriptor); 168210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 168310037c866b04550fc5461058c398c2e3e509381ajeffhao } 168410037c866b04550fc5461058c398c2e3e509381ajeffhao 168510037c866b04550fc5461058c398c2e3e509381ajeffhao // Check ordering between items. 16862cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (previous_item_ != nullptr) { 168710037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::TypeId* prev_item = reinterpret_cast<const DexFile::TypeId*>(previous_item_); 16888d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(prev_item->descriptor_idx_ >= item->descriptor_idx_)) { 16898d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order type_ids: %x then %x", 16908d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers prev_item->descriptor_idx_, item->descriptor_idx_); 169110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 169210037c866b04550fc5461058c398c2e3e509381ajeffhao } 169310037c866b04550fc5461058c398c2e3e509381ajeffhao } 169410037c866b04550fc5461058c398c2e3e509381ajeffhao 169510037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += sizeof(DexFile::TypeId); 169610037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 169710037c866b04550fc5461058c398c2e3e509381ajeffhao} 169810037c866b04550fc5461058c398c2e3e509381ajeffhao 169910037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckInterProtoIdItem() { 170010037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::ProtoId* item = reinterpret_cast<const DexFile::ProtoId*>(ptr_); 1701e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe 1702e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_STRING(shorty, item->shorty_idx_, "inter_proto_id_item shorty_idx") 1703e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe 170410037c866b04550fc5461058c398c2e3e509381ajeffhao if (item->parameters_off_ != 0 && 170510037c866b04550fc5461058c398c2e3e509381ajeffhao !CheckOffsetToTypeMap(item->parameters_off_, DexFile::kDexTypeTypeList)) { 170610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 170710037c866b04550fc5461058c398c2e3e509381ajeffhao } 170810037c866b04550fc5461058c398c2e3e509381ajeffhao 170910037c866b04550fc5461058c398c2e3e509381ajeffhao // Check the return type and advance the shorty. 1710e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_STRING_BY_TYPE(return_type, item->return_type_idx_, "inter_proto_id_item return_type_idx") 1711e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe if (!CheckShortyDescriptorMatch(*shorty, return_type, true)) { 171210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 171310037c866b04550fc5461058c398c2e3e509381ajeffhao } 171410037c866b04550fc5461058c398c2e3e509381ajeffhao shorty++; 171510037c866b04550fc5461058c398c2e3e509381ajeffhao 171610037c866b04550fc5461058c398c2e3e509381ajeffhao DexFileParameterIterator it(*dex_file_, *item); 171710037c866b04550fc5461058c398c2e3e509381ajeffhao while (it.HasNext() && *shorty != '\0') { 1718bb836e14541d5424328182f3e20ce08c691d041eAndreas Gampe if (!CheckIndex(it.GetTypeIdx(), dex_file_->NumTypeIds(), 1719bb836e14541d5424328182f3e20ce08c691d041eAndreas Gampe "inter_proto_id_item shorty type_idx")) { 1720bb836e14541d5424328182f3e20ce08c691d041eAndreas Gampe return false; 1721bb836e14541d5424328182f3e20ce08c691d041eAndreas Gampe } 172210037c866b04550fc5461058c398c2e3e509381ajeffhao const char* descriptor = it.GetDescriptor(); 172310037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckShortyDescriptorMatch(*shorty, descriptor, false)) { 172410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 172510037c866b04550fc5461058c398c2e3e509381ajeffhao } 172610037c866b04550fc5461058c398c2e3e509381ajeffhao it.Next(); 172710037c866b04550fc5461058c398c2e3e509381ajeffhao shorty++; 172810037c866b04550fc5461058c398c2e3e509381ajeffhao } 17298d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(it.HasNext() || *shorty != '\0')) { 17308d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Mismatched length for parameters and shorty"); 173110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 173210037c866b04550fc5461058c398c2e3e509381ajeffhao } 173310037c866b04550fc5461058c398c2e3e509381ajeffhao 173410037c866b04550fc5461058c398c2e3e509381ajeffhao // Check ordering between items. This relies on type_ids being in order. 17352cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (previous_item_ != nullptr) { 173610037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::ProtoId* prev = reinterpret_cast<const DexFile::ProtoId*>(previous_item_); 17378d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(prev->return_type_idx_ > item->return_type_idx_)) { 17388d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order proto_id return types"); 173910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 174010037c866b04550fc5461058c398c2e3e509381ajeffhao } else if (prev->return_type_idx_ == item->return_type_idx_) { 174110037c866b04550fc5461058c398c2e3e509381ajeffhao DexFileParameterIterator curr_it(*dex_file_, *item); 174210037c866b04550fc5461058c398c2e3e509381ajeffhao DexFileParameterIterator prev_it(*dex_file_, *prev); 174310037c866b04550fc5461058c398c2e3e509381ajeffhao 174410037c866b04550fc5461058c398c2e3e509381ajeffhao while (curr_it.HasNext() && prev_it.HasNext()) { 174510037c866b04550fc5461058c398c2e3e509381ajeffhao uint16_t prev_idx = prev_it.GetTypeIdx(); 174610037c866b04550fc5461058c398c2e3e509381ajeffhao uint16_t curr_idx = curr_it.GetTypeIdx(); 174710037c866b04550fc5461058c398c2e3e509381ajeffhao if (prev_idx == DexFile::kDexNoIndex16) { 174810037c866b04550fc5461058c398c2e3e509381ajeffhao break; 174910037c866b04550fc5461058c398c2e3e509381ajeffhao } 17508d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(curr_idx == DexFile::kDexNoIndex16)) { 17518d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order proto_id arguments"); 175210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 175310037c866b04550fc5461058c398c2e3e509381ajeffhao } 175410037c866b04550fc5461058c398c2e3e509381ajeffhao 175510037c866b04550fc5461058c398c2e3e509381ajeffhao if (prev_idx < curr_idx) { 175610037c866b04550fc5461058c398c2e3e509381ajeffhao break; 17578d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers } else if (UNLIKELY(prev_idx > curr_idx)) { 17588d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order proto_id arguments"); 175910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 176010037c866b04550fc5461058c398c2e3e509381ajeffhao } 176110037c866b04550fc5461058c398c2e3e509381ajeffhao 176210037c866b04550fc5461058c398c2e3e509381ajeffhao prev_it.Next(); 176310037c866b04550fc5461058c398c2e3e509381ajeffhao curr_it.Next(); 176410037c866b04550fc5461058c398c2e3e509381ajeffhao } 176510037c866b04550fc5461058c398c2e3e509381ajeffhao } 176610037c866b04550fc5461058c398c2e3e509381ajeffhao } 176710037c866b04550fc5461058c398c2e3e509381ajeffhao 176810037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += sizeof(DexFile::ProtoId); 176910037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 177010037c866b04550fc5461058c398c2e3e509381ajeffhao} 177110037c866b04550fc5461058c398c2e3e509381ajeffhao 177210037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckInterFieldIdItem() { 177310037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::FieldId* item = reinterpret_cast<const DexFile::FieldId*>(ptr_); 177410037c866b04550fc5461058c398c2e3e509381ajeffhao 177510037c866b04550fc5461058c398c2e3e509381ajeffhao // Check that the class descriptor is valid. 1776e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_field_id_item class_idx") 1777e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe if (UNLIKELY(!IsValidDescriptor(class_descriptor) || class_descriptor[0] != 'L')) { 1778e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe ErrorStringPrintf("Invalid descriptor for class_idx: '%s'", class_descriptor); 177910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 178010037c866b04550fc5461058c398c2e3e509381ajeffhao } 178110037c866b04550fc5461058c398c2e3e509381ajeffhao 178210037c866b04550fc5461058c398c2e3e509381ajeffhao // Check that the type descriptor is a valid field name. 1783e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_STRING_BY_TYPE(type_descriptor, item->type_idx_, "inter_field_id_item type_idx") 1784e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe if (UNLIKELY(!IsValidDescriptor(type_descriptor) || type_descriptor[0] == 'V')) { 1785e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe ErrorStringPrintf("Invalid descriptor for type_idx: '%s'", type_descriptor); 178610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 178710037c866b04550fc5461058c398c2e3e509381ajeffhao } 178810037c866b04550fc5461058c398c2e3e509381ajeffhao 178910037c866b04550fc5461058c398c2e3e509381ajeffhao // Check that the name is valid. 1790e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_STRING(descriptor, item->name_idx_, "inter_field_id_item name_idx") 17918d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(!IsValidMemberName(descriptor))) { 17928d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Invalid field name: '%s'", descriptor); 179310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 179410037c866b04550fc5461058c398c2e3e509381ajeffhao } 179510037c866b04550fc5461058c398c2e3e509381ajeffhao 179610037c866b04550fc5461058c398c2e3e509381ajeffhao // Check ordering between items. This relies on the other sections being in order. 17972cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (previous_item_ != nullptr) { 179810037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::FieldId* prev_item = reinterpret_cast<const DexFile::FieldId*>(previous_item_); 17998d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(prev_item->class_idx_ > item->class_idx_)) { 18008d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order field_ids"); 180110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 180210037c866b04550fc5461058c398c2e3e509381ajeffhao } else if (prev_item->class_idx_ == item->class_idx_) { 18038d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(prev_item->name_idx_ > item->name_idx_)) { 18048d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order field_ids"); 180510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 180610037c866b04550fc5461058c398c2e3e509381ajeffhao } else if (prev_item->name_idx_ == item->name_idx_) { 18078d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(prev_item->type_idx_ >= item->type_idx_)) { 18088d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order field_ids"); 180910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 181010037c866b04550fc5461058c398c2e3e509381ajeffhao } 181110037c866b04550fc5461058c398c2e3e509381ajeffhao } 181210037c866b04550fc5461058c398c2e3e509381ajeffhao } 181310037c866b04550fc5461058c398c2e3e509381ajeffhao } 181410037c866b04550fc5461058c398c2e3e509381ajeffhao 181510037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += sizeof(DexFile::FieldId); 181610037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 181710037c866b04550fc5461058c398c2e3e509381ajeffhao} 181810037c866b04550fc5461058c398c2e3e509381ajeffhao 181910037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckInterMethodIdItem() { 182010037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::MethodId* item = reinterpret_cast<const DexFile::MethodId*>(ptr_); 182110037c866b04550fc5461058c398c2e3e509381ajeffhao 182210037c866b04550fc5461058c398c2e3e509381ajeffhao // Check that the class descriptor is a valid reference name. 1823e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_method_id_item class_idx") 1824e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe if (UNLIKELY(!IsValidDescriptor(class_descriptor) || (class_descriptor[0] != 'L' && 1825e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe class_descriptor[0] != '['))) { 1826e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe ErrorStringPrintf("Invalid descriptor for class_idx: '%s'", class_descriptor); 182710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 182810037c866b04550fc5461058c398c2e3e509381ajeffhao } 182910037c866b04550fc5461058c398c2e3e509381ajeffhao 183010037c866b04550fc5461058c398c2e3e509381ajeffhao // Check that the name is valid. 1831df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe LOAD_STRING(descriptor, item->name_idx_, "inter_method_id_item name_idx") 18328d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(!IsValidMemberName(descriptor))) { 18338d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Invalid method name: '%s'", descriptor); 183410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 183510037c866b04550fc5461058c398c2e3e509381ajeffhao } 183610037c866b04550fc5461058c398c2e3e509381ajeffhao 1837df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe // Check that the proto id is valid. 1838df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe if (UNLIKELY(!CheckIndex(item->proto_idx_, dex_file_->NumProtoIds(), 1839df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe "inter_method_id_item proto_idx"))) { 1840df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe return false; 1841df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe } 1842df10b32c4d0adfa86201169692eaa7ef038b642cAndreas Gampe 184310037c866b04550fc5461058c398c2e3e509381ajeffhao // Check ordering between items. This relies on the other sections being in order. 18442cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (previous_item_ != nullptr) { 184510037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::MethodId* prev_item = reinterpret_cast<const DexFile::MethodId*>(previous_item_); 18468d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(prev_item->class_idx_ > item->class_idx_)) { 18478d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order method_ids"); 184810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 184910037c866b04550fc5461058c398c2e3e509381ajeffhao } else if (prev_item->class_idx_ == item->class_idx_) { 18508d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(prev_item->name_idx_ > item->name_idx_)) { 18518d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order method_ids"); 185210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 185310037c866b04550fc5461058c398c2e3e509381ajeffhao } else if (prev_item->name_idx_ == item->name_idx_) { 18548d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(prev_item->proto_idx_ >= item->proto_idx_)) { 18558d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order method_ids"); 185610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 185710037c866b04550fc5461058c398c2e3e509381ajeffhao } 185810037c866b04550fc5461058c398c2e3e509381ajeffhao } 185910037c866b04550fc5461058c398c2e3e509381ajeffhao } 186010037c866b04550fc5461058c398c2e3e509381ajeffhao } 186110037c866b04550fc5461058c398c2e3e509381ajeffhao 186210037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += sizeof(DexFile::MethodId); 186310037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 186410037c866b04550fc5461058c398c2e3e509381ajeffhao} 186510037c866b04550fc5461058c398c2e3e509381ajeffhao 186610037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckInterClassDefItem() { 186710037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::ClassDef* item = reinterpret_cast<const DexFile::ClassDef*>(ptr_); 186810037c866b04550fc5461058c398c2e3e509381ajeffhao 18690ba238dcc21ae3544e1e8cb5d108725db8a1c134Andreas Gampe // Check for duplicate class def. 18700ba238dcc21ae3544e1e8cb5d108725db8a1c134Andreas Gampe if (defined_classes_.find(item->class_idx_) != defined_classes_.end()) { 18710ba238dcc21ae3544e1e8cb5d108725db8a1c134Andreas Gampe ErrorStringPrintf("Redefinition of class with type idx: '%d'", item->class_idx_); 18720ba238dcc21ae3544e1e8cb5d108725db8a1c134Andreas Gampe return false; 18730ba238dcc21ae3544e1e8cb5d108725db8a1c134Andreas Gampe } 18740ba238dcc21ae3544e1e8cb5d108725db8a1c134Andreas Gampe defined_classes_.insert(item->class_idx_); 18750ba238dcc21ae3544e1e8cb5d108725db8a1c134Andreas Gampe 1876e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_class_def_item class_idx") 1877e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe if (UNLIKELY(!IsValidDescriptor(class_descriptor) || class_descriptor[0] != 'L')) { 1878e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe ErrorStringPrintf("Invalid class descriptor: '%s'", class_descriptor); 187910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 188010037c866b04550fc5461058c398c2e3e509381ajeffhao } 188110037c866b04550fc5461058c398c2e3e509381ajeffhao 1882acc2bb693d83102f93007d7c4881a94bbcc3b9bbAndreas Gampe // Only allow non-runtime modifiers. 1883acc2bb693d83102f93007d7c4881a94bbcc3b9bbAndreas Gampe if ((item->access_flags_ & ~kAccJavaFlagsMask) != 0) { 1884acc2bb693d83102f93007d7c4881a94bbcc3b9bbAndreas Gampe ErrorStringPrintf("Invalid class flags: '%d'", item->access_flags_); 1885acc2bb693d83102f93007d7c4881a94bbcc3b9bbAndreas Gampe return false; 1886acc2bb693d83102f93007d7c4881a94bbcc3b9bbAndreas Gampe } 1887acc2bb693d83102f93007d7c4881a94bbcc3b9bbAndreas Gampe 188810037c866b04550fc5461058c398c2e3e509381ajeffhao if (item->interfaces_off_ != 0 && 188910037c866b04550fc5461058c398c2e3e509381ajeffhao !CheckOffsetToTypeMap(item->interfaces_off_, DexFile::kDexTypeTypeList)) { 189010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 189110037c866b04550fc5461058c398c2e3e509381ajeffhao } 189210037c866b04550fc5461058c398c2e3e509381ajeffhao if (item->annotations_off_ != 0 && 189310037c866b04550fc5461058c398c2e3e509381ajeffhao !CheckOffsetToTypeMap(item->annotations_off_, DexFile::kDexTypeAnnotationsDirectoryItem)) { 189410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 189510037c866b04550fc5461058c398c2e3e509381ajeffhao } 189610037c866b04550fc5461058c398c2e3e509381ajeffhao if (item->class_data_off_ != 0 && 189710037c866b04550fc5461058c398c2e3e509381ajeffhao !CheckOffsetToTypeMap(item->class_data_off_, DexFile::kDexTypeClassDataItem)) { 189810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 189910037c866b04550fc5461058c398c2e3e509381ajeffhao } 190010037c866b04550fc5461058c398c2e3e509381ajeffhao if (item->static_values_off_ != 0 && 190110037c866b04550fc5461058c398c2e3e509381ajeffhao !CheckOffsetToTypeMap(item->static_values_off_, DexFile::kDexTypeEncodedArrayItem)) { 190210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 190310037c866b04550fc5461058c398c2e3e509381ajeffhao } 190410037c866b04550fc5461058c398c2e3e509381ajeffhao 190510037c866b04550fc5461058c398c2e3e509381ajeffhao if (item->superclass_idx_ != DexFile::kDexNoIndex16) { 1906e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_STRING_BY_TYPE(superclass_descriptor, item->superclass_idx_, 1907e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe "inter_class_def_item superclass_idx") 1908e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe if (UNLIKELY(!IsValidDescriptor(superclass_descriptor) || superclass_descriptor[0] != 'L')) { 1909e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe ErrorStringPrintf("Invalid superclass: '%s'", superclass_descriptor); 191010037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 191110037c866b04550fc5461058c398c2e3e509381ajeffhao } 191210037c866b04550fc5461058c398c2e3e509381ajeffhao } 191310037c866b04550fc5461058c398c2e3e509381ajeffhao 191410037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::TypeList* interfaces = dex_file_->GetInterfacesList(*item); 19152cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier if (interfaces != nullptr) { 191610037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t size = interfaces->Size(); 191710037c866b04550fc5461058c398c2e3e509381ajeffhao 191810037c866b04550fc5461058c398c2e3e509381ajeffhao // Ensure that all interfaces refer to classes (not arrays or primitives). 191910037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 0; i < size; i++) { 1920e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_STRING_BY_TYPE(inf_descriptor, interfaces->GetTypeItem(i).type_idx_, 1921e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe "inter_class_def_item interface type_idx") 1922e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe if (UNLIKELY(!IsValidDescriptor(inf_descriptor) || inf_descriptor[0] != 'L')) { 1923e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe ErrorStringPrintf("Invalid interface: '%s'", inf_descriptor); 192410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 192510037c866b04550fc5461058c398c2e3e509381ajeffhao } 192610037c866b04550fc5461058c398c2e3e509381ajeffhao } 192710037c866b04550fc5461058c398c2e3e509381ajeffhao 192810037c866b04550fc5461058c398c2e3e509381ajeffhao /* 192910037c866b04550fc5461058c398c2e3e509381ajeffhao * Ensure that there are no duplicates. This is an O(N^2) test, but in 193010037c866b04550fc5461058c398c2e3e509381ajeffhao * practice the number of interfaces implemented by any given class is low. 193110037c866b04550fc5461058c398c2e3e509381ajeffhao */ 193210037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 1; i < size; i++) { 193310037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t idx1 = interfaces->GetTypeItem(i).type_idx_; 193410037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t j =0; j < i; j++) { 193510037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t idx2 = interfaces->GetTypeItem(j).type_idx_; 19368d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(idx1 == idx2)) { 19378d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Duplicate interface: '%s'", dex_file_->StringByTypeIdx(idx1)); 193810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 193910037c866b04550fc5461058c398c2e3e509381ajeffhao } 194010037c866b04550fc5461058c398c2e3e509381ajeffhao } 194110037c866b04550fc5461058c398c2e3e509381ajeffhao } 194210037c866b04550fc5461058c398c2e3e509381ajeffhao } 194310037c866b04550fc5461058c398c2e3e509381ajeffhao 194410037c866b04550fc5461058c398c2e3e509381ajeffhao // Check that references in class_data_item are to the right class. 194510037c866b04550fc5461058c398c2e3e509381ajeffhao if (item->class_data_off_ != 0) { 194613735955f39b3b304c37d2b2840663c131262c18Ian Rogers const uint8_t* data = begin_ + item->class_data_off_; 19475e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe bool success; 19485e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe uint16_t data_definer = FindFirstClassDataDefiner(data, &success); 19495e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe if (!success) { 1950e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return false; 1951e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe } 19528d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((data_definer != item->class_idx_) && (data_definer != DexFile::kDexNoIndex16))) { 19538d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Invalid class_data_item"); 195410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 195510037c866b04550fc5461058c398c2e3e509381ajeffhao } 195610037c866b04550fc5461058c398c2e3e509381ajeffhao } 195710037c866b04550fc5461058c398c2e3e509381ajeffhao 195810037c866b04550fc5461058c398c2e3e509381ajeffhao // Check that references in annotations_directory_item are to right class. 195910037c866b04550fc5461058c398c2e3e509381ajeffhao if (item->annotations_off_ != 0) { 196013735955f39b3b304c37d2b2840663c131262c18Ian Rogers const uint8_t* data = begin_ + item->annotations_off_; 19615e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe bool success; 19625e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe uint16_t annotations_definer = FindFirstAnnotationsDirectoryDefiner(data, &success); 19635e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe if (!success) { 1964e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return false; 1965e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe } 19668d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY((annotations_definer != item->class_idx_) && 19678d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers (annotations_definer != DexFile::kDexNoIndex16))) { 19688d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Invalid annotations_directory_item"); 196910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 197010037c866b04550fc5461058c398c2e3e509381ajeffhao } 197110037c866b04550fc5461058c398c2e3e509381ajeffhao } 197210037c866b04550fc5461058c398c2e3e509381ajeffhao 197310037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ += sizeof(DexFile::ClassDef); 197410037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 197510037c866b04550fc5461058c398c2e3e509381ajeffhao} 197610037c866b04550fc5461058c398c2e3e509381ajeffhao 197710037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckInterAnnotationSetRefList() { 197810037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::AnnotationSetRefList* list = 197910037c866b04550fc5461058c398c2e3e509381ajeffhao reinterpret_cast<const DexFile::AnnotationSetRefList*>(ptr_); 198010037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::AnnotationSetRefItem* item = list->list_; 198110037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t count = list->size_; 198210037c866b04550fc5461058c398c2e3e509381ajeffhao 198310037c866b04550fc5461058c398c2e3e509381ajeffhao while (count--) { 198410037c866b04550fc5461058c398c2e3e509381ajeffhao if (item->annotations_off_ != 0 && 198510037c866b04550fc5461058c398c2e3e509381ajeffhao !CheckOffsetToTypeMap(item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) { 198610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 198710037c866b04550fc5461058c398c2e3e509381ajeffhao } 198810037c866b04550fc5461058c398c2e3e509381ajeffhao item++; 198910037c866b04550fc5461058c398c2e3e509381ajeffhao } 199010037c866b04550fc5461058c398c2e3e509381ajeffhao 199113735955f39b3b304c37d2b2840663c131262c18Ian Rogers ptr_ = reinterpret_cast<const uint8_t*>(item); 199210037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 199310037c866b04550fc5461058c398c2e3e509381ajeffhao} 199410037c866b04550fc5461058c398c2e3e509381ajeffhao 199510037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckInterAnnotationSetItem() { 199610037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::AnnotationSetItem* set = reinterpret_cast<const DexFile::AnnotationSetItem*>(ptr_); 199710037c866b04550fc5461058c398c2e3e509381ajeffhao const uint32_t* offsets = set->entries_; 199810037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t count = set->size_; 199910037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t last_idx = 0; 200010037c866b04550fc5461058c398c2e3e509381ajeffhao 200110037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 0; i < count; i++) { 200210037c866b04550fc5461058c398c2e3e509381ajeffhao if (*offsets != 0 && !CheckOffsetToTypeMap(*offsets, DexFile::kDexTypeAnnotationItem)) { 200310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 200410037c866b04550fc5461058c398c2e3e509381ajeffhao } 200510037c866b04550fc5461058c398c2e3e509381ajeffhao 200610037c866b04550fc5461058c398c2e3e509381ajeffhao // Get the annotation from the offset and the type index for the annotation. 200710037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::AnnotationItem* annotation = 200830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers reinterpret_cast<const DexFile::AnnotationItem*>(begin_ + *offsets); 200910037c866b04550fc5461058c398c2e3e509381ajeffhao const uint8_t* data = annotation->annotation_; 201010037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t idx = DecodeUnsignedLeb128(&data); 201110037c866b04550fc5461058c398c2e3e509381ajeffhao 20128d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers if (UNLIKELY(last_idx >= idx && i != 0)) { 20138d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Out-of-order entry types: %x then %x", last_idx, idx); 201410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 201510037c866b04550fc5461058c398c2e3e509381ajeffhao } 201610037c866b04550fc5461058c398c2e3e509381ajeffhao 201710037c866b04550fc5461058c398c2e3e509381ajeffhao last_idx = idx; 201810037c866b04550fc5461058c398c2e3e509381ajeffhao offsets++; 201910037c866b04550fc5461058c398c2e3e509381ajeffhao } 202010037c866b04550fc5461058c398c2e3e509381ajeffhao 202113735955f39b3b304c37d2b2840663c131262c18Ian Rogers ptr_ = reinterpret_cast<const uint8_t*>(offsets); 202210037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 202310037c866b04550fc5461058c398c2e3e509381ajeffhao} 202410037c866b04550fc5461058c398c2e3e509381ajeffhao 202510037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckInterClassDataItem() { 202610037c866b04550fc5461058c398c2e3e509381ajeffhao ClassDataItemIterator it(*dex_file_, ptr_); 20275e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe bool success; 20285e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe uint16_t defining_class = FindFirstClassDataDefiner(ptr_, &success); 20295e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe if (!success) { 2030e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return false; 2031e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe } 203210037c866b04550fc5461058c398c2e3e509381ajeffhao 203310037c866b04550fc5461058c398c2e3e509381ajeffhao for (; it.HasNextStaticField() || it.HasNextInstanceField(); it.Next()) { 20345e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe LOAD_FIELD(field, it.GetMemberIndex(), "inter_class_data_item field_id", return false) 2035e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe if (UNLIKELY(field->class_idx_ != defining_class)) { 20368d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Mismatched defining class for class_data_item field"); 203710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 203810037c866b04550fc5461058c398c2e3e509381ajeffhao } 203910037c866b04550fc5461058c398c2e3e509381ajeffhao } 204010037c866b04550fc5461058c398c2e3e509381ajeffhao for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) { 204110037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t code_off = it.GetMethodCodeItemOffset(); 204210037c866b04550fc5461058c398c2e3e509381ajeffhao if (code_off != 0 && !CheckOffsetToTypeMap(code_off, DexFile::kDexTypeCodeItem)) { 204310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 204410037c866b04550fc5461058c398c2e3e509381ajeffhao } 20455e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe LOAD_METHOD(method, it.GetMemberIndex(), "inter_class_data_item method_id", return false) 2046e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe if (UNLIKELY(method->class_idx_ != defining_class)) { 20478d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Mismatched defining class for class_data_item method"); 204810037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 204910037c866b04550fc5461058c398c2e3e509381ajeffhao } 205010037c866b04550fc5461058c398c2e3e509381ajeffhao } 205110037c866b04550fc5461058c398c2e3e509381ajeffhao 205210037c866b04550fc5461058c398c2e3e509381ajeffhao ptr_ = it.EndDataPointer(); 205310037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 205410037c866b04550fc5461058c398c2e3e509381ajeffhao} 205510037c866b04550fc5461058c398c2e3e509381ajeffhao 205610037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckInterAnnotationsDirectoryItem() { 205710037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::AnnotationsDirectoryItem* item = 205810037c866b04550fc5461058c398c2e3e509381ajeffhao reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr_); 20595e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe bool success; 20605e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe uint16_t defining_class = FindFirstAnnotationsDirectoryDefiner(ptr_, &success); 20615e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe if (!success) { 2062e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe return false; 2063e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe } 206410037c866b04550fc5461058c398c2e3e509381ajeffhao 206510037c866b04550fc5461058c398c2e3e509381ajeffhao if (item->class_annotations_off_ != 0 && 206610037c866b04550fc5461058c398c2e3e509381ajeffhao !CheckOffsetToTypeMap(item->class_annotations_off_, DexFile::kDexTypeAnnotationSetItem)) { 206710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 206810037c866b04550fc5461058c398c2e3e509381ajeffhao } 206910037c866b04550fc5461058c398c2e3e509381ajeffhao 207010037c866b04550fc5461058c398c2e3e509381ajeffhao // Field annotations follow immediately after the annotations directory. 207110037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::FieldAnnotationsItem* field_item = 207210037c866b04550fc5461058c398c2e3e509381ajeffhao reinterpret_cast<const DexFile::FieldAnnotationsItem*>(item + 1); 207310037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t field_count = item->fields_size_; 207410037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 0; i < field_count; i++) { 20755e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe LOAD_FIELD(field, field_item->field_idx_, "inter_annotations_directory_item field_id", 20765e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe return false) 2077e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe if (UNLIKELY(field->class_idx_ != defining_class)) { 20788d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Mismatched defining class for field_annotation"); 207910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 208010037c866b04550fc5461058c398c2e3e509381ajeffhao } 208110037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckOffsetToTypeMap(field_item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) { 208210037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 208310037c866b04550fc5461058c398c2e3e509381ajeffhao } 208410037c866b04550fc5461058c398c2e3e509381ajeffhao field_item++; 208510037c866b04550fc5461058c398c2e3e509381ajeffhao } 208610037c866b04550fc5461058c398c2e3e509381ajeffhao 208710037c866b04550fc5461058c398c2e3e509381ajeffhao // Method annotations follow immediately after field annotations. 208810037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::MethodAnnotationsItem* method_item = 208910037c866b04550fc5461058c398c2e3e509381ajeffhao reinterpret_cast<const DexFile::MethodAnnotationsItem*>(field_item); 209010037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t method_count = item->methods_size_; 209110037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 0; i < method_count; i++) { 2092e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_METHOD(method, method_item->method_idx_, "inter_annotations_directory_item method_id", 20935e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe return false) 2094e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe if (UNLIKELY(method->class_idx_ != defining_class)) { 20958d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Mismatched defining class for method_annotation"); 209610037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 209710037c866b04550fc5461058c398c2e3e509381ajeffhao } 209810037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckOffsetToTypeMap(method_item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) { 209910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 210010037c866b04550fc5461058c398c2e3e509381ajeffhao } 210110037c866b04550fc5461058c398c2e3e509381ajeffhao method_item++; 210210037c866b04550fc5461058c398c2e3e509381ajeffhao } 210310037c866b04550fc5461058c398c2e3e509381ajeffhao 210410037c866b04550fc5461058c398c2e3e509381ajeffhao // Parameter annotations follow immediately after method annotations. 210510037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::ParameterAnnotationsItem* parameter_item = 210610037c866b04550fc5461058c398c2e3e509381ajeffhao reinterpret_cast<const DexFile::ParameterAnnotationsItem*>(method_item); 210710037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t parameter_count = item->parameters_size_; 210810037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 0; i < parameter_count; i++) { 2109e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe LOAD_METHOD(parameter_method, parameter_item->method_idx_, 21105e31ddadd29325649260aa186e9ffa8ccdb370a2Andreas Gampe "inter_annotations_directory_item parameter method_id", return false) 2111e09269ca05e3014e86198e9a2cf6092026fafefdAndreas Gampe if (UNLIKELY(parameter_method->class_idx_ != defining_class)) { 21128d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Mismatched defining class for parameter_annotation"); 211310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 211410037c866b04550fc5461058c398c2e3e509381ajeffhao } 21152b87ddf36abff711fa2233c49bffc7ceb03b15d7Dragos Sbirlea if (!CheckOffsetToTypeMap(parameter_item->annotations_off_, 21162b87ddf36abff711fa2233c49bffc7ceb03b15d7Dragos Sbirlea DexFile::kDexTypeAnnotationSetRefList)) { 211710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 211810037c866b04550fc5461058c398c2e3e509381ajeffhao } 211910037c866b04550fc5461058c398c2e3e509381ajeffhao parameter_item++; 212010037c866b04550fc5461058c398c2e3e509381ajeffhao } 212110037c866b04550fc5461058c398c2e3e509381ajeffhao 212213735955f39b3b304c37d2b2840663c131262c18Ian Rogers ptr_ = reinterpret_cast<const uint8_t*>(parameter_item); 212310037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 212410037c866b04550fc5461058c398c2e3e509381ajeffhao} 212510037c866b04550fc5461058c398c2e3e509381ajeffhao 21268a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogersbool DexFileVerifier::CheckInterSectionIterate(size_t offset, uint32_t count, uint16_t type) { 212710037c866b04550fc5461058c398c2e3e509381ajeffhao // Get the right alignment mask for the type of section. 21288a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers size_t alignment_mask; 212910037c866b04550fc5461058c398c2e3e509381ajeffhao switch (type) { 213010037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDataItem: 213110037c866b04550fc5461058c398c2e3e509381ajeffhao alignment_mask = sizeof(uint8_t) - 1; 213210037c866b04550fc5461058c398c2e3e509381ajeffhao break; 213310037c866b04550fc5461058c398c2e3e509381ajeffhao default: 213410037c866b04550fc5461058c398c2e3e509381ajeffhao alignment_mask = sizeof(uint32_t) - 1; 213510037c866b04550fc5461058c398c2e3e509381ajeffhao break; 213610037c866b04550fc5461058c398c2e3e509381ajeffhao } 213710037c866b04550fc5461058c398c2e3e509381ajeffhao 213810037c866b04550fc5461058c398c2e3e509381ajeffhao // Iterate through the items in the section. 21392cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier previous_item_ = nullptr; 214010037c866b04550fc5461058c398c2e3e509381ajeffhao for (uint32_t i = 0; i < count; i++) { 214110037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t new_offset = (offset + alignment_mask) & ~alignment_mask; 214230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers ptr_ = begin_ + new_offset; 214313735955f39b3b304c37d2b2840663c131262c18Ian Rogers const uint8_t* prev_ptr = ptr_; 214410037c866b04550fc5461058c398c2e3e509381ajeffhao 214510037c866b04550fc5461058c398c2e3e509381ajeffhao // Check depending on the section type. 214610037c866b04550fc5461058c398c2e3e509381ajeffhao switch (type) { 214710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeStringIdItem: { 214810037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckInterStringIdItem()) { 214910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 215010037c866b04550fc5461058c398c2e3e509381ajeffhao } 215110037c866b04550fc5461058c398c2e3e509381ajeffhao break; 215210037c866b04550fc5461058c398c2e3e509381ajeffhao } 215310037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeTypeIdItem: { 215410037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckInterTypeIdItem()) { 215510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 215610037c866b04550fc5461058c398c2e3e509381ajeffhao } 215710037c866b04550fc5461058c398c2e3e509381ajeffhao break; 215810037c866b04550fc5461058c398c2e3e509381ajeffhao } 215910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeProtoIdItem: { 216010037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckInterProtoIdItem()) { 216110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 216210037c866b04550fc5461058c398c2e3e509381ajeffhao } 216310037c866b04550fc5461058c398c2e3e509381ajeffhao break; 216410037c866b04550fc5461058c398c2e3e509381ajeffhao } 216510037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeFieldIdItem: { 216610037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckInterFieldIdItem()) { 216710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 216810037c866b04550fc5461058c398c2e3e509381ajeffhao } 216910037c866b04550fc5461058c398c2e3e509381ajeffhao break; 217010037c866b04550fc5461058c398c2e3e509381ajeffhao } 217110037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeMethodIdItem: { 217210037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckInterMethodIdItem()) { 217310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 217410037c866b04550fc5461058c398c2e3e509381ajeffhao } 217510037c866b04550fc5461058c398c2e3e509381ajeffhao break; 217610037c866b04550fc5461058c398c2e3e509381ajeffhao } 217710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDefItem: { 217810037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckInterClassDefItem()) { 217910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 218010037c866b04550fc5461058c398c2e3e509381ajeffhao } 218110037c866b04550fc5461058c398c2e3e509381ajeffhao break; 218210037c866b04550fc5461058c398c2e3e509381ajeffhao } 218310037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationSetRefList: { 218410037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckInterAnnotationSetRefList()) { 218510037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 218610037c866b04550fc5461058c398c2e3e509381ajeffhao } 218710037c866b04550fc5461058c398c2e3e509381ajeffhao break; 218810037c866b04550fc5461058c398c2e3e509381ajeffhao } 218910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationSetItem: { 219010037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckInterAnnotationSetItem()) { 219110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 219210037c866b04550fc5461058c398c2e3e509381ajeffhao } 219310037c866b04550fc5461058c398c2e3e509381ajeffhao break; 219410037c866b04550fc5461058c398c2e3e509381ajeffhao } 219510037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDataItem: { 219610037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckInterClassDataItem()) { 219710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 219810037c866b04550fc5461058c398c2e3e509381ajeffhao } 219910037c866b04550fc5461058c398c2e3e509381ajeffhao break; 220010037c866b04550fc5461058c398c2e3e509381ajeffhao } 220110037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationsDirectoryItem: { 220210037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckInterAnnotationsDirectoryItem()) { 220310037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 220410037c866b04550fc5461058c398c2e3e509381ajeffhao } 220510037c866b04550fc5461058c398c2e3e509381ajeffhao break; 220610037c866b04550fc5461058c398c2e3e509381ajeffhao } 220710037c866b04550fc5461058c398c2e3e509381ajeffhao default: 22088d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Unknown map item type %x", type); 220910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 221010037c866b04550fc5461058c398c2e3e509381ajeffhao } 221110037c866b04550fc5461058c398c2e3e509381ajeffhao 221210037c866b04550fc5461058c398c2e3e509381ajeffhao previous_item_ = prev_ptr; 22138a6bbfc66e3cf01d4aa07ee08b515beee481d553Ian Rogers offset = ptr_ - begin_; 221410037c866b04550fc5461058c398c2e3e509381ajeffhao } 221510037c866b04550fc5461058c398c2e3e509381ajeffhao 221610037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 221710037c866b04550fc5461058c398c2e3e509381ajeffhao} 221810037c866b04550fc5461058c398c2e3e509381ajeffhao 221910037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::CheckInterSection() { 222030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ + header_->map_off_); 222110037c866b04550fc5461058c398c2e3e509381ajeffhao const DexFile::MapItem* item = map->list_; 222210037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t count = map->size_; 222310037c866b04550fc5461058c398c2e3e509381ajeffhao 222410037c866b04550fc5461058c398c2e3e509381ajeffhao // Cross check the items listed in the map. 222510037c866b04550fc5461058c398c2e3e509381ajeffhao while (count--) { 222610037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t section_offset = item->offset_; 222710037c866b04550fc5461058c398c2e3e509381ajeffhao uint32_t section_count = item->size_; 222810037c866b04550fc5461058c398c2e3e509381ajeffhao uint16_t type = item->type_; 222910037c866b04550fc5461058c398c2e3e509381ajeffhao 223010037c866b04550fc5461058c398c2e3e509381ajeffhao switch (type) { 223110037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeHeaderItem: 223210037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeMapList: 223310037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeTypeList: 223410037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeCodeItem: 223510037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeStringDataItem: 223610037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeDebugInfoItem: 223710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationItem: 223810037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeEncodedArrayItem: 223910037c866b04550fc5461058c398c2e3e509381ajeffhao break; 224010037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeStringIdItem: 224110037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeTypeIdItem: 224210037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeProtoIdItem: 224310037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeFieldIdItem: 224410037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeMethodIdItem: 224510037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDefItem: 224610037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationSetRefList: 224710037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationSetItem: 224810037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeClassDataItem: 224910037c866b04550fc5461058c398c2e3e509381ajeffhao case DexFile::kDexTypeAnnotationsDirectoryItem: { 225010037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckInterSectionIterate(section_offset, section_count, type)) { 225110037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 225210037c866b04550fc5461058c398c2e3e509381ajeffhao } 225310037c866b04550fc5461058c398c2e3e509381ajeffhao break; 225410037c866b04550fc5461058c398c2e3e509381ajeffhao } 225510037c866b04550fc5461058c398c2e3e509381ajeffhao default: 22568d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers ErrorStringPrintf("Unknown map item type %x", type); 225710037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 225810037c866b04550fc5461058c398c2e3e509381ajeffhao } 225910037c866b04550fc5461058c398c2e3e509381ajeffhao 226010037c866b04550fc5461058c398c2e3e509381ajeffhao item++; 226110037c866b04550fc5461058c398c2e3e509381ajeffhao } 226210037c866b04550fc5461058c398c2e3e509381ajeffhao 226310037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 226410037c866b04550fc5461058c398c2e3e509381ajeffhao} 226510037c866b04550fc5461058c398c2e3e509381ajeffhao 226610037c866b04550fc5461058c398c2e3e509381ajeffhaobool DexFileVerifier::Verify() { 226710037c866b04550fc5461058c398c2e3e509381ajeffhao // Check the header. 226810037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckHeader()) { 226910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 227010037c866b04550fc5461058c398c2e3e509381ajeffhao } 227110037c866b04550fc5461058c398c2e3e509381ajeffhao 227210037c866b04550fc5461058c398c2e3e509381ajeffhao // Check the map section. 227310037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckMap()) { 227410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 227510037c866b04550fc5461058c398c2e3e509381ajeffhao } 227610037c866b04550fc5461058c398c2e3e509381ajeffhao 227710037c866b04550fc5461058c398c2e3e509381ajeffhao // Check structure within remaining sections. 227810037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckIntraSection()) { 227910037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 228010037c866b04550fc5461058c398c2e3e509381ajeffhao } 228110037c866b04550fc5461058c398c2e3e509381ajeffhao 228210037c866b04550fc5461058c398c2e3e509381ajeffhao // Check references from one section to another. 228310037c866b04550fc5461058c398c2e3e509381ajeffhao if (!CheckInterSection()) { 228410037c866b04550fc5461058c398c2e3e509381ajeffhao return false; 228510037c866b04550fc5461058c398c2e3e509381ajeffhao } 228610037c866b04550fc5461058c398c2e3e509381ajeffhao 228710037c866b04550fc5461058c398c2e3e509381ajeffhao return true; 228810037c866b04550fc5461058c398c2e3e509381ajeffhao} 228910037c866b04550fc5461058c398c2e3e509381ajeffhao 22908d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogersvoid DexFileVerifier::ErrorStringPrintf(const char* fmt, ...) { 22918d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers va_list ap; 22928d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers va_start(ap, fmt); 22938d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers DCHECK(failure_reason_.empty()) << failure_reason_; 22948d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers failure_reason_ = StringPrintf("Failure to verify dex file '%s': ", location_); 22958d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers StringAppendV(&failure_reason_, fmt, ap); 22968d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers va_end(ap); 22978d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers} 22988d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers 2299e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe// Fields and methods may have only one of public/protected/private. 2300e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampestatic bool CheckAtMostOneOfPublicProtectedPrivate(uint32_t flags) { 2301e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe size_t count = (((flags & kAccPublic) == 0) ? 0 : 1) + 2302e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe (((flags & kAccProtected) == 0) ? 0 : 1) + 2303e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe (((flags & kAccPrivate) == 0) ? 0 : 1); 2304e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return count <= 1; 2305e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe} 2306e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2307e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampebool DexFileVerifier::CheckFieldAccessFlags(uint32_t field_access_flags, 2308e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t class_access_flags, 2309e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe std::string* error_msg) { 2310e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Generally sort out >16-bit flags. 2311e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((field_access_flags & ~kAccJavaFlagsMask) != 0) { 2312e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Bad class_data_item field access_flags %x", field_access_flags); 2313e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2314e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2315e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2316e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Flags allowed on fields, in general. Other lower-16-bit flags are to be ignored. 2317e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe constexpr uint32_t kFieldAccessFlags = kAccPublic | 2318e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccPrivate | 2319e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccProtected | 2320e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccStatic | 2321e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccFinal | 2322e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccVolatile | 2323e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccTransient | 2324e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccSynthetic | 2325e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccEnum; 2326e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2327e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Fields may have only one of public/protected/final. 2328e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!CheckAtMostOneOfPublicProtectedPrivate(field_access_flags)) { 2329e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Field may have only one of public/protected/private, %x", 2330e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe field_access_flags); 2331e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2332e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2333e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2334e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Interfaces have a pretty restricted list. 2335e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((class_access_flags & kAccInterface) != 0) { 2336e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Interface fields must be public final static. 2337e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe constexpr uint32_t kPublicFinalStatic = kAccPublic | kAccFinal | kAccStatic; 2338e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((field_access_flags & kPublicFinalStatic) != kPublicFinalStatic) { 2339e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Interface field is not public final static: %x", 2340e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe field_access_flags); 2341e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2342e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2343e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Interface fields may be synthetic, but may not have other flags. 2344e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe constexpr uint32_t kDisallowed = ~(kPublicFinalStatic | kAccSynthetic); 2345e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((field_access_flags & kFieldAccessFlags & kDisallowed) != 0) { 2346e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Interface field has disallowed flag: %x", field_access_flags); 2347e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2348e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2349e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return true; 2350e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2351e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2352e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Volatile fields may not be final. 2353e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe constexpr uint32_t kVolatileFinal = kAccVolatile | kAccFinal; 2354e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((field_access_flags & kVolatileFinal) == kVolatileFinal) { 2355e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = "Fields may not be volatile and final"; 2356e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2357e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2358e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2359e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return true; 2360e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe} 2361e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2362e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe// Try to find the name of the method with the given index. We do not want to rely on DexFile 2363e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe// infrastructure at this point, so do it all by hand. begin and header correspond to begin_ and 2364e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe// header_ of the DexFileVerifier. str will contain the pointer to the method name on success 2365e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe// (flagged by the return value), otherwise error_msg will contain an error string. 2366e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampestatic bool FindMethodName(uint32_t method_index, 2367e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe const uint8_t* begin, 2368e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe const DexFile::Header* header, 2369e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe const char** str, 2370e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe std::string* error_msg) { 2371e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (method_index >= header->method_ids_size_) { 2372e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = "Method index not available for method flags verification"; 2373e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2374e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2375e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t string_idx = 2376e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe (reinterpret_cast<const DexFile::MethodId*>(begin + header->method_ids_off_) + 2377e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_index)->name_idx_; 2378e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (string_idx >= header->string_ids_size_) { 2379e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = "String index not available for method flags verification"; 2380e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2381e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2382e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t string_off = 2383e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe (reinterpret_cast<const DexFile::StringId*>(begin + header->string_ids_off_) + string_idx)-> 2384e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe string_data_off_; 2385e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (string_off >= header->file_size_) { 2386e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = "String offset out of bounds for method flags verification"; 2387e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2388e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2389e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe const uint8_t* str_data_ptr = begin + string_off; 2390e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe DecodeUnsignedLeb128(&str_data_ptr); 2391e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *str = reinterpret_cast<const char*>(str_data_ptr); 2392e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return true; 2393e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe} 2394e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2395e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampebool DexFileVerifier::CheckMethodAccessFlags(uint32_t method_index, 2396e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t method_access_flags, 2397e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe uint32_t class_access_flags, 2398e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe bool has_code, 2399e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe bool expect_direct, 2400e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe std::string* error_msg) { 2401e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Generally sort out >16-bit flags, except dex knows Constructor and DeclaredSynchronized. 2402e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe constexpr uint32_t kAllMethodFlags = 2403e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccJavaFlagsMask | kAccConstructor | kAccDeclaredSynchronized; 2404e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((method_access_flags & ~kAllMethodFlags) != 0) { 2405e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Bad class_data_item method access_flags %x", method_access_flags); 2406e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2407e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2408e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2409e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Flags allowed on fields, in general. Other lower-16-bit flags are to be ignored. 2410e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe constexpr uint32_t kMethodAccessFlags = kAccPublic | 2411e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccPrivate | 2412e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccProtected | 2413e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccStatic | 2414e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccFinal | 2415e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccSynthetic | 2416e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccSynchronized | 2417e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccBridge | 2418e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccVarargs | 2419e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccNative | 2420e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccAbstract | 2421e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccStrict; 2422e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2423e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Methods may have only one of public/protected/final. 2424e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!CheckAtMostOneOfPublicProtectedPrivate(method_access_flags)) { 2425e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Method may have only one of public/protected/private, %x", 2426e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_access_flags); 2427e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2428e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2429e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2430e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Try to find the name, to check for constructor properties. 2431e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe const char* str; 2432e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!FindMethodName(method_index, begin_, header_, &str, error_msg)) { 2433e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2434e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2435e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe bool is_init_by_name = false; 2436e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe constexpr const char* kInitName = "<init>"; 2437e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe size_t str_offset = (reinterpret_cast<const uint8_t*>(str) - begin_); 2438e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (header_->file_size_ - str_offset >= sizeof(kInitName)) { 2439e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe is_init_by_name = strcmp(kInitName, str) == 0; 2440e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2441e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe bool is_clinit_by_name = false; 2442e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe constexpr const char* kClinitName = "<clinit>"; 2443e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (header_->file_size_ - str_offset >= sizeof(kClinitName)) { 2444e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe is_clinit_by_name = strcmp(kClinitName, str) == 0; 2445e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2446e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe bool is_constructor = is_init_by_name || is_clinit_by_name; 2447e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2448e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Only methods named "<clinit>" or "<init>" may be marked constructor. Note: we cannot enforce 2449e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // the reverse for backwards compatibility reasons. 2450e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (((method_access_flags & kAccConstructor) != 0) && !is_constructor) { 2451e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Method %" PRIu32 " is marked constructor, but doesn't match name", 2452e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_index); 2453e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2454e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2455e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Check that the static constructor (= static initializer) is named "<clinit>" and that the 2456e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // instance constructor is called "<init>". 2457e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (is_constructor) { 2458e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe bool is_static = (method_access_flags & kAccStatic) != 0; 2459e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (is_static ^ is_clinit_by_name) { 2460e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Constructor %" PRIu32 " is not flagged correctly wrt/ static.", 2461e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_index); 2462e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2463e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2464e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2465e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Check that static and private methods, as well as constructors, are in the direct methods list, 2466e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // and other methods in the virtual methods list. 2467e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe bool is_direct = (method_access_flags & (kAccStatic | kAccPrivate)) != 0 || is_constructor; 2468e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (is_direct != expect_direct) { 2469e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Direct/virtual method %" PRIu32 " not in expected list %d", 2470e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_index, 2471e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe expect_direct); 2472e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2473e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2474e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2475e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2476e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // From here on out it is easier to mask out the bits we're supposed to ignore. 2477e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_access_flags &= kMethodAccessFlags; 2478e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2479e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // If there aren't any instructions, make sure that's expected. 2480e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (!has_code) { 2481e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Only native or abstract methods may not have code. 2482e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((method_access_flags & (kAccNative | kAccAbstract)) == 0) { 2483e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Method %" PRIu32 " has no code, but is not marked native or " 2484e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe "abstract", 2485e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_index); 2486e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2487e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2488e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Constructors must always have code. 2489e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (is_constructor) { 2490e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Constructor %u must not be abstract or native", method_index); 2491e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2492e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2493e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((method_access_flags & kAccAbstract) != 0) { 2494e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Abstract methods are not allowed to have the following flags. 2495e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe constexpr uint32_t kForbidden = 2496e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccPrivate | kAccStatic | kAccFinal | kAccNative | kAccStrict | kAccSynchronized; 2497e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((method_access_flags & kForbidden) != 0) { 2498e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Abstract method %" PRIu32 " has disallowed access flags %x", 2499e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_index, 2500e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_access_flags); 2501e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2502e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2503e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Abstract methods must be in an abstract class or interface. 2504e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((class_access_flags & (kAccInterface | kAccAbstract)) == 0) { 2505e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Method %" PRIu32 " is abstract, but the declaring class " 2506e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe "is neither abstract nor an interface", method_index); 2507e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2508e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2509e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2510e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Interfaces are special. 2511e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((class_access_flags & kAccInterface) != 0) { 2512e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Interface methods must be public and abstract. 2513e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((method_access_flags & (kAccPublic | kAccAbstract)) != (kAccPublic | kAccAbstract)) { 2514e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Interface method %" PRIu32 " is not public and abstract", 2515e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_index); 2516e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2517e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2518e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // At this point, we know the method is public and abstract. This means that all the checks 2519e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // for invalid combinations above applies. In addition, interface methods must not be 2520e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // protected. This is caught by the check for only-one-of-public-protected-private. 2521e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2522e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return true; 2523e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2524e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2525e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // When there's code, the method must not be native or abstract. 2526e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((method_access_flags & (kAccNative | kAccAbstract)) != 0) { 2527e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Method %" PRIu32 " has code, but is marked native or abstract", 2528e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_index); 2529e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2530e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2531e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2532e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Only the static initializer may have code in an interface. 2533e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (((class_access_flags & kAccInterface) != 0) && !is_clinit_by_name) { 2534e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Non-clinit interface method %" PRIu32 " should not have code", 2535e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_index); 2536e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2537e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2538e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2539e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe // Instance constructors must not be synchronized and a few other flags. 2540e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if (is_init_by_name) { 2541e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe static constexpr uint32_t kInitAllowed = 2542e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe kAccPrivate | kAccProtected | kAccPublic | kAccStrict | kAccVarargs | kAccSynthetic; 2543e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe if ((method_access_flags & ~kInitAllowed) != 0) { 2544e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe *error_msg = StringPrintf("Constructor %" PRIu32 " flagged inappropriately %x", 2545e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_index, 2546e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe method_access_flags); 2547e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return false; 2548e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2549e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe } 2550e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 2551e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe return true; 2552e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe} 2553e6215c0ec4b1bb71b722fdbf7e62eaf3be8a91d5Andreas Gampe 255410037c866b04550fc5461058c398c2e3e509381ajeffhao} // namespace art 2555