Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 16 | |
| 17 | #include "dex_file_verifier.h" |
| 18 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 19 | #include <zlib.h> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 21 | |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 22 | #include "base/stringprintf.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 23 | #include "dex_file-inl.h" |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 24 | #include "leb128.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 25 | #include "safe_map.h" |
Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 26 | #include "utf-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "utils.h" |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | static uint32_t MapTypeToBitMask(uint32_t map_type) { |
| 32 | switch (map_type) { |
| 33 | case DexFile::kDexTypeHeaderItem: return 1 << 0; |
| 34 | case DexFile::kDexTypeStringIdItem: return 1 << 1; |
| 35 | case DexFile::kDexTypeTypeIdItem: return 1 << 2; |
| 36 | case DexFile::kDexTypeProtoIdItem: return 1 << 3; |
| 37 | case DexFile::kDexTypeFieldIdItem: return 1 << 4; |
| 38 | case DexFile::kDexTypeMethodIdItem: return 1 << 5; |
| 39 | case DexFile::kDexTypeClassDefItem: return 1 << 6; |
| 40 | case DexFile::kDexTypeMapList: return 1 << 7; |
| 41 | case DexFile::kDexTypeTypeList: return 1 << 8; |
| 42 | case DexFile::kDexTypeAnnotationSetRefList: return 1 << 9; |
| 43 | case DexFile::kDexTypeAnnotationSetItem: return 1 << 10; |
| 44 | case DexFile::kDexTypeClassDataItem: return 1 << 11; |
| 45 | case DexFile::kDexTypeCodeItem: return 1 << 12; |
| 46 | case DexFile::kDexTypeStringDataItem: return 1 << 13; |
| 47 | case DexFile::kDexTypeDebugInfoItem: return 1 << 14; |
| 48 | case DexFile::kDexTypeAnnotationItem: return 1 << 15; |
| 49 | case DexFile::kDexTypeEncodedArrayItem: return 1 << 16; |
| 50 | case DexFile::kDexTypeAnnotationsDirectoryItem: return 1 << 17; |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static bool IsDataSectionType(uint32_t map_type) { |
| 56 | switch (map_type) { |
| 57 | case DexFile::kDexTypeHeaderItem: |
| 58 | case DexFile::kDexTypeStringIdItem: |
| 59 | case DexFile::kDexTypeTypeIdItem: |
| 60 | case DexFile::kDexTypeProtoIdItem: |
| 61 | case DexFile::kDexTypeFieldIdItem: |
| 62 | case DexFile::kDexTypeMethodIdItem: |
| 63 | case DexFile::kDexTypeClassDefItem: |
| 64 | return false; |
| 65 | } |
| 66 | return true; |
| 67 | } |
| 68 | |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 69 | const char* DexFileVerifier::CheckLoadStringByIdx(uint32_t idx, const char* error_string) { |
Andreas Gampe | df10b32 | 2014-06-11 21:46:05 -0700 | [diff] [blame] | 70 | if (UNLIKELY(!CheckIndex(idx, dex_file_->NumStringIds(), error_string))) { |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 71 | return nullptr; |
| 72 | } |
| 73 | return dex_file_->StringDataByIdx(idx); |
| 74 | } |
| 75 | |
| 76 | const char* DexFileVerifier::CheckLoadStringByTypeIdx(uint32_t type_idx, const char* error_string) { |
Andreas Gampe | df10b32 | 2014-06-11 21:46:05 -0700 | [diff] [blame] | 77 | if (UNLIKELY(!CheckIndex(type_idx, dex_file_->NumTypeIds(), error_string))) { |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 78 | return nullptr; |
| 79 | } |
| 80 | const DexFile::TypeId& type_id = dex_file_->GetTypeId(type_idx); |
| 81 | uint32_t idx = type_id.descriptor_idx_; |
| 82 | return CheckLoadStringByIdx(idx, error_string); |
| 83 | } |
| 84 | |
| 85 | const DexFile::FieldId* DexFileVerifier::CheckLoadFieldId(uint32_t idx, const char* error_string) { |
Andreas Gampe | df10b32 | 2014-06-11 21:46:05 -0700 | [diff] [blame] | 86 | if (UNLIKELY(!CheckIndex(idx, dex_file_->NumFieldIds(), error_string))) { |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 87 | return nullptr; |
| 88 | } |
| 89 | return &dex_file_->GetFieldId(idx); |
| 90 | } |
| 91 | |
| 92 | const DexFile::MethodId* DexFileVerifier::CheckLoadMethodId(uint32_t idx, const char* err_string) { |
Andreas Gampe | df10b32 | 2014-06-11 21:46:05 -0700 | [diff] [blame] | 93 | if (UNLIKELY(!CheckIndex(idx, dex_file_->NumMethodIds(), err_string))) { |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 94 | return nullptr; |
| 95 | } |
| 96 | return &dex_file_->GetMethodId(idx); |
| 97 | } |
| 98 | |
| 99 | // Helper macro to load string and return false on error. |
| 100 | #define LOAD_STRING(var, idx, error) \ |
| 101 | const char* var = CheckLoadStringByIdx(idx, error); \ |
Andreas Gampe | df10b32 | 2014-06-11 21:46:05 -0700 | [diff] [blame] | 102 | if (UNLIKELY(var == nullptr)) { \ |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 103 | return false; \ |
| 104 | } |
| 105 | |
| 106 | // Helper macro to load string by type idx and return false on error. |
| 107 | #define LOAD_STRING_BY_TYPE(var, type_idx, error) \ |
| 108 | const char* var = CheckLoadStringByTypeIdx(type_idx, error); \ |
Andreas Gampe | df10b32 | 2014-06-11 21:46:05 -0700 | [diff] [blame] | 109 | if (UNLIKELY(var == nullptr)) { \ |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 110 | return false; \ |
| 111 | } |
| 112 | |
| 113 | // Helper macro to load method id. Return last parameter on error. |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 114 | #define LOAD_METHOD(var, idx, error_string, error_stmt) \ |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 115 | const DexFile::MethodId* var = CheckLoadMethodId(idx, error_string); \ |
Andreas Gampe | df10b32 | 2014-06-11 21:46:05 -0700 | [diff] [blame] | 116 | if (UNLIKELY(var == nullptr)) { \ |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 117 | error_stmt; \ |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // Helper macro to load method id. Return last parameter on error. |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 121 | #define LOAD_FIELD(var, idx, fmt, error_stmt) \ |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 122 | const DexFile::FieldId* var = CheckLoadFieldId(idx, fmt); \ |
Andreas Gampe | df10b32 | 2014-06-11 21:46:05 -0700 | [diff] [blame] | 123 | if (UNLIKELY(var == nullptr)) { \ |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 124 | error_stmt; \ |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 127 | bool DexFileVerifier::Verify(const DexFile* dex_file, const byte* begin, size_t size, |
| 128 | const char* location, std::string* error_msg) { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 129 | std::unique_ptr<DexFileVerifier> verifier(new DexFileVerifier(dex_file, begin, size, location)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 130 | if (!verifier->Verify()) { |
| 131 | *error_msg = verifier->FailureReason(); |
| 132 | return false; |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | bool DexFileVerifier::CheckShortyDescriptorMatch(char shorty_char, const char* descriptor, |
| 138 | bool is_return_type) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 139 | switch (shorty_char) { |
| 140 | case 'V': |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 141 | if (UNLIKELY(!is_return_type)) { |
| 142 | ErrorStringPrintf("Invalid use of void"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | // Intentional fallthrough. |
| 146 | case 'B': |
| 147 | case 'C': |
| 148 | case 'D': |
| 149 | case 'F': |
| 150 | case 'I': |
| 151 | case 'J': |
| 152 | case 'S': |
| 153 | case 'Z': |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 154 | if (UNLIKELY((descriptor[0] != shorty_char) || (descriptor[1] != '\0'))) { |
| 155 | ErrorStringPrintf("Shorty vs. primitive type mismatch: '%c', '%s'", |
| 156 | shorty_char, descriptor); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | break; |
| 160 | case 'L': |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 161 | if (UNLIKELY((descriptor[0] != 'L') && (descriptor[0] != '['))) { |
| 162 | ErrorStringPrintf("Shorty vs. type mismatch: '%c', '%s'", shorty_char, descriptor); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 163 | return false; |
| 164 | } |
| 165 | break; |
| 166 | default: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 167 | ErrorStringPrintf("Bad shorty character: '%c'", shorty_char); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | return true; |
| 171 | } |
| 172 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 173 | bool DexFileVerifier::CheckPointerRange(const void* start, const void* end, const char* label) { |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 174 | const byte* range_start = reinterpret_cast<const byte*>(start); |
| 175 | const byte* range_end = reinterpret_cast<const byte*>(end); |
| 176 | const byte* file_start = reinterpret_cast<const byte*>(begin_); |
| 177 | const byte* file_end = file_start + size_; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 178 | if (UNLIKELY((range_start < file_start) || (range_start > file_end) || |
| 179 | (range_end < file_start) || (range_end > file_end))) { |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 180 | ErrorStringPrintf("Bad range for %s: %zx to %zx", label, |
Ian Rogers | e3d5581 | 2014-06-11 13:00:44 -0700 | [diff] [blame] | 181 | static_cast<size_t>(range_start - file_start), |
| 182 | static_cast<size_t>(range_end - file_start)); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 183 | return false; |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | bool DexFileVerifier::CheckListSize(const void* start, uint32_t count, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 189 | uint32_t element_size, const char* label) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 190 | const byte* list_start = reinterpret_cast<const byte*>(start); |
| 191 | return CheckPointerRange(list_start, list_start + (count * element_size), label); |
| 192 | } |
| 193 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 194 | bool DexFileVerifier::CheckIndex(uint32_t field, uint32_t limit, const char* label) { |
| 195 | if (UNLIKELY(field >= limit)) { |
| 196 | ErrorStringPrintf("Bad index for %s: %x >= %x", label, field, limit); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | return true; |
| 200 | } |
| 201 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 202 | bool DexFileVerifier::CheckHeader() { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 203 | // Check file size from the header. |
| 204 | uint32_t expected_size = header_->file_size_; |
| 205 | if (size_ != expected_size) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 206 | ErrorStringPrintf("Bad file size (%zd, expected %ud)", size_, expected_size); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | |
| 210 | // Compute and verify the checksum in the header. |
| 211 | uint32_t adler_checksum = adler32(0L, Z_NULL, 0); |
| 212 | const uint32_t non_sum = sizeof(header_->magic_) + sizeof(header_->checksum_); |
| 213 | const byte* non_sum_ptr = reinterpret_cast<const byte*>(header_) + non_sum; |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 214 | adler_checksum = adler32(adler_checksum, non_sum_ptr, expected_size - non_sum); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 215 | if (adler_checksum != header_->checksum_) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 216 | ErrorStringPrintf("Bad checksum (%08x, expected %08x)", adler_checksum, header_->checksum_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 217 | return false; |
| 218 | } |
| 219 | |
| 220 | // Check the contents of the header. |
| 221 | if (header_->endian_tag_ != DexFile::kDexEndianConstant) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 222 | ErrorStringPrintf("Unexpected endian_tag: %x", header_->endian_tag_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 223 | return false; |
| 224 | } |
| 225 | |
| 226 | if (header_->header_size_ != sizeof(DexFile::Header)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 227 | ErrorStringPrintf("Bad header size: %ud", header_->header_size_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 234 | bool DexFileVerifier::CheckMap() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 235 | const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ + header_->map_off_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 236 | const DexFile::MapItem* item = map->list_; |
| 237 | |
| 238 | uint32_t count = map->size_; |
| 239 | uint32_t last_offset = 0; |
| 240 | uint32_t data_item_count = 0; |
| 241 | uint32_t data_items_left = header_->data_size_; |
| 242 | uint32_t used_bits = 0; |
| 243 | |
| 244 | // Sanity check the size of the map list. |
| 245 | if (!CheckListSize(item, count, sizeof(DexFile::MapItem), "map size")) { |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | // Check the items listed in the map. |
| 250 | for (uint32_t i = 0; i < count; i++) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 251 | if (UNLIKELY(last_offset >= item->offset_ && i != 0)) { |
| 252 | ErrorStringPrintf("Out of order map item: %x then %x", last_offset, item->offset_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 253 | return false; |
| 254 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 255 | if (UNLIKELY(item->offset_ >= header_->file_size_)) { |
| 256 | ErrorStringPrintf("Map item after end of file: %x, size %x", |
| 257 | item->offset_, header_->file_size_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 258 | return false; |
| 259 | } |
| 260 | |
| 261 | if (IsDataSectionType(item->type_)) { |
| 262 | uint32_t icount = item->size_; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 263 | if (UNLIKELY(icount > data_items_left)) { |
| 264 | ErrorStringPrintf("Too many items in data section: %ud", data_item_count + icount); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | data_items_left -= icount; |
| 268 | data_item_count += icount; |
| 269 | } |
| 270 | |
| 271 | uint32_t bit = MapTypeToBitMask(item->type_); |
| 272 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 273 | if (UNLIKELY(bit == 0)) { |
| 274 | ErrorStringPrintf("Unknown map section type %x", item->type_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 275 | return false; |
| 276 | } |
| 277 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 278 | if (UNLIKELY((used_bits & bit) != 0)) { |
| 279 | ErrorStringPrintf("Duplicate map section of type %x", item->type_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | |
| 283 | used_bits |= bit; |
| 284 | last_offset = item->offset_; |
| 285 | item++; |
| 286 | } |
| 287 | |
| 288 | // Check for missing sections in the map. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 289 | if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeHeaderItem)) == 0)) { |
| 290 | ErrorStringPrintf("Map is missing header entry"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 291 | return false; |
| 292 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 293 | if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeMapList)) == 0)) { |
| 294 | ErrorStringPrintf("Map is missing map_list entry"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 295 | return false; |
| 296 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 297 | if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeStringIdItem)) == 0 && |
| 298 | ((header_->string_ids_off_ != 0) || (header_->string_ids_size_ != 0)))) { |
| 299 | ErrorStringPrintf("Map is missing string_ids entry"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 300 | return false; |
| 301 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 302 | if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeTypeIdItem)) == 0 && |
| 303 | ((header_->type_ids_off_ != 0) || (header_->type_ids_size_ != 0)))) { |
| 304 | ErrorStringPrintf("Map is missing type_ids entry"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 305 | return false; |
| 306 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 307 | if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeProtoIdItem)) == 0 && |
| 308 | ((header_->proto_ids_off_ != 0) || (header_->proto_ids_size_ != 0)))) { |
| 309 | ErrorStringPrintf("Map is missing proto_ids entry"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 310 | return false; |
| 311 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 312 | if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeFieldIdItem)) == 0 && |
| 313 | ((header_->field_ids_off_ != 0) || (header_->field_ids_size_ != 0)))) { |
| 314 | ErrorStringPrintf("Map is missing field_ids entry"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 315 | return false; |
| 316 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 317 | if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeMethodIdItem)) == 0 && |
| 318 | ((header_->method_ids_off_ != 0) || (header_->method_ids_size_ != 0)))) { |
| 319 | ErrorStringPrintf("Map is missing method_ids entry"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 320 | return false; |
| 321 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 322 | if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeClassDefItem)) == 0 && |
| 323 | ((header_->class_defs_off_ != 0) || (header_->class_defs_size_ != 0)))) { |
| 324 | ErrorStringPrintf("Map is missing class_defs entry"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 325 | return false; |
| 326 | } |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 327 | return true; |
| 328 | } |
| 329 | |
| 330 | uint32_t DexFileVerifier::ReadUnsignedLittleEndian(uint32_t size) { |
| 331 | uint32_t result = 0; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 332 | if (LIKELY(CheckPointerRange(ptr_, ptr_ + size, "encoded_value"))) { |
| 333 | for (uint32_t i = 0; i < size; i++) { |
| 334 | result |= ((uint32_t) *(ptr_++)) << (i * 8); |
| 335 | } |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 336 | } |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 337 | return result; |
| 338 | } |
| 339 | |
| 340 | bool DexFileVerifier::CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 341 | uint32_t* handler_offsets, uint32_t handlers_size) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 342 | const byte* handlers_base = DexFile::GetCatchHandlerData(*code_item, 0); |
| 343 | |
| 344 | for (uint32_t i = 0; i < handlers_size; i++) { |
| 345 | bool catch_all; |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 346 | size_t offset = ptr_ - handlers_base; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 347 | int32_t size = DecodeSignedLeb128(&ptr_); |
| 348 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 349 | if (UNLIKELY((size < -65536) || (size > 65536))) { |
| 350 | ErrorStringPrintf("Invalid exception handler size: %d", size); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | |
| 354 | if (size <= 0) { |
| 355 | catch_all = true; |
| 356 | size = -size; |
| 357 | } else { |
| 358 | catch_all = false; |
| 359 | } |
| 360 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 361 | handler_offsets[i] = static_cast<uint32_t>(offset); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 362 | |
| 363 | while (size-- > 0) { |
| 364 | uint32_t type_idx = DecodeUnsignedLeb128(&ptr_); |
| 365 | if (!CheckIndex(type_idx, header_->type_ids_size_, "handler type_idx")) { |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | uint32_t addr = DecodeUnsignedLeb128(&ptr_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 370 | if (UNLIKELY(addr >= code_item->insns_size_in_code_units_)) { |
| 371 | ErrorStringPrintf("Invalid handler addr: %x", addr); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 372 | return false; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if (catch_all) { |
| 377 | uint32_t addr = DecodeUnsignedLeb128(&ptr_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 378 | if (UNLIKELY(addr >= code_item->insns_size_in_code_units_)) { |
| 379 | ErrorStringPrintf("Invalid handler catch_all_addr: %x", addr); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 380 | return false; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | bool DexFileVerifier::CheckClassDataItemField(uint32_t idx, uint32_t access_flags, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 389 | bool expect_static) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 390 | if (!CheckIndex(idx, header_->field_ids_size_, "class_data_item field_idx")) { |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | bool is_static = (access_flags & kAccStatic) != 0; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 395 | if (UNLIKELY(is_static != expect_static)) { |
| 396 | ErrorStringPrintf("Static/instance field not in expected list"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 397 | return false; |
| 398 | } |
| 399 | |
| 400 | uint32_t access_field_mask = kAccPublic | kAccPrivate | kAccProtected | kAccStatic | |
| 401 | kAccFinal | kAccVolatile | kAccTransient | kAccSynthetic | kAccEnum; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 402 | if (UNLIKELY((access_flags & ~access_field_mask) != 0)) { |
| 403 | ErrorStringPrintf("Bad class_data_item field access_flags %x", access_flags); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 404 | return false; |
| 405 | } |
| 406 | |
| 407 | return true; |
| 408 | } |
| 409 | |
| 410 | bool DexFileVerifier::CheckClassDataItemMethod(uint32_t idx, uint32_t access_flags, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 411 | uint32_t code_offset, bool expect_direct) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 412 | if (!CheckIndex(idx, header_->method_ids_size_, "class_data_item method_idx")) { |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | bool is_direct = (access_flags & (kAccStatic | kAccPrivate | kAccConstructor)) != 0; |
| 417 | bool expect_code = (access_flags & (kAccNative | kAccAbstract)) == 0; |
| 418 | bool is_synchronized = (access_flags & kAccSynchronized) != 0; |
| 419 | bool allow_synchronized = (access_flags & kAccNative) != 0; |
| 420 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 421 | if (UNLIKELY(is_direct != expect_direct)) { |
| 422 | ErrorStringPrintf("Direct/virtual method not in expected list"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 423 | return false; |
| 424 | } |
| 425 | |
| 426 | uint32_t access_method_mask = kAccPublic | kAccPrivate | kAccProtected | kAccStatic | |
| 427 | kAccFinal | kAccSynchronized | kAccBridge | kAccVarargs | kAccNative | kAccAbstract | |
| 428 | kAccStrict | kAccSynthetic | kAccConstructor | kAccDeclaredSynchronized; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 429 | if (UNLIKELY(((access_flags & ~access_method_mask) != 0) || |
| 430 | (is_synchronized && !allow_synchronized))) { |
| 431 | ErrorStringPrintf("Bad class_data_item method access_flags %x", access_flags); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 432 | return false; |
| 433 | } |
| 434 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 435 | if (UNLIKELY(expect_code && (code_offset == 0))) { |
| 436 | ErrorStringPrintf("Unexpected zero value for class_data_item method code_off with access " |
| 437 | "flags %x", access_flags); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 438 | return false; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 439 | } else if (UNLIKELY(!expect_code && (code_offset != 0))) { |
| 440 | ErrorStringPrintf("Unexpected non-zero value %x for class_data_item method code_off" |
| 441 | " with access flags %x", code_offset, access_flags); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 442 | return false; |
| 443 | } |
| 444 | |
| 445 | return true; |
| 446 | } |
| 447 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 448 | bool DexFileVerifier::CheckPadding(size_t offset, uint32_t aligned_offset) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 449 | if (offset < aligned_offset) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 450 | if (!CheckPointerRange(begin_ + offset, begin_ + aligned_offset, "section")) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 451 | return false; |
| 452 | } |
| 453 | while (offset < aligned_offset) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 454 | if (UNLIKELY(*ptr_ != '\0')) { |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 455 | ErrorStringPrintf("Non-zero padding %x before section start at %zx", *ptr_, offset); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 456 | return false; |
| 457 | } |
| 458 | ptr_++; |
| 459 | offset++; |
| 460 | } |
| 461 | } |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | bool DexFileVerifier::CheckEncodedValue() { |
| 466 | if (!CheckPointerRange(ptr_, ptr_ + 1, "encoded_value header")) { |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | uint8_t header_byte = *(ptr_++); |
| 471 | uint32_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask; |
| 472 | uint32_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift; |
| 473 | |
| 474 | switch (value_type) { |
| 475 | case DexFile::kDexAnnotationByte: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 476 | if (UNLIKELY(value_arg != 0)) { |
| 477 | ErrorStringPrintf("Bad encoded_value byte size %x", value_arg); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 478 | return false; |
| 479 | } |
| 480 | ptr_++; |
| 481 | break; |
| 482 | case DexFile::kDexAnnotationShort: |
| 483 | case DexFile::kDexAnnotationChar: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 484 | if (UNLIKELY(value_arg > 1)) { |
| 485 | ErrorStringPrintf("Bad encoded_value char/short size %x", value_arg); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 486 | return false; |
| 487 | } |
| 488 | ptr_ += value_arg + 1; |
| 489 | break; |
| 490 | case DexFile::kDexAnnotationInt: |
| 491 | case DexFile::kDexAnnotationFloat: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 492 | if (UNLIKELY(value_arg > 3)) { |
| 493 | ErrorStringPrintf("Bad encoded_value int/float size %x", value_arg); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 494 | return false; |
| 495 | } |
| 496 | ptr_ += value_arg + 1; |
| 497 | break; |
| 498 | case DexFile::kDexAnnotationLong: |
| 499 | case DexFile::kDexAnnotationDouble: |
| 500 | ptr_ += value_arg + 1; |
| 501 | break; |
| 502 | case DexFile::kDexAnnotationString: { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 503 | if (UNLIKELY(value_arg > 3)) { |
| 504 | ErrorStringPrintf("Bad encoded_value string size %x", value_arg); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 505 | return false; |
| 506 | } |
| 507 | uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1); |
| 508 | if (!CheckIndex(idx, header_->string_ids_size_, "encoded_value string")) { |
| 509 | return false; |
| 510 | } |
| 511 | break; |
| 512 | } |
| 513 | case DexFile::kDexAnnotationType: { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 514 | if (UNLIKELY(value_arg > 3)) { |
| 515 | ErrorStringPrintf("Bad encoded_value type size %x", value_arg); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 516 | return false; |
| 517 | } |
| 518 | uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1); |
| 519 | if (!CheckIndex(idx, header_->type_ids_size_, "encoded_value type")) { |
| 520 | return false; |
| 521 | } |
| 522 | break; |
| 523 | } |
| 524 | case DexFile::kDexAnnotationField: |
| 525 | case DexFile::kDexAnnotationEnum: { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 526 | if (UNLIKELY(value_arg > 3)) { |
| 527 | ErrorStringPrintf("Bad encoded_value field/enum size %x", value_arg); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 528 | return false; |
| 529 | } |
| 530 | uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1); |
| 531 | if (!CheckIndex(idx, header_->field_ids_size_, "encoded_value field")) { |
| 532 | return false; |
| 533 | } |
| 534 | break; |
| 535 | } |
| 536 | case DexFile::kDexAnnotationMethod: { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 537 | if (UNLIKELY(value_arg > 3)) { |
| 538 | ErrorStringPrintf("Bad encoded_value method size %x", value_arg); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 539 | return false; |
| 540 | } |
| 541 | uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1); |
| 542 | if (!CheckIndex(idx, header_->method_ids_size_, "encoded_value method")) { |
| 543 | return false; |
| 544 | } |
| 545 | break; |
| 546 | } |
| 547 | case DexFile::kDexAnnotationArray: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 548 | if (UNLIKELY(value_arg != 0)) { |
| 549 | ErrorStringPrintf("Bad encoded_value array value_arg %x", value_arg); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 550 | return false; |
| 551 | } |
| 552 | if (!CheckEncodedArray()) { |
| 553 | return false; |
| 554 | } |
| 555 | break; |
| 556 | case DexFile::kDexAnnotationAnnotation: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 557 | if (UNLIKELY(value_arg != 0)) { |
| 558 | ErrorStringPrintf("Bad encoded_value annotation value_arg %x", value_arg); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 559 | return false; |
| 560 | } |
| 561 | if (!CheckEncodedAnnotation()) { |
| 562 | return false; |
| 563 | } |
| 564 | break; |
| 565 | case DexFile::kDexAnnotationNull: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 566 | if (UNLIKELY(value_arg != 0)) { |
| 567 | ErrorStringPrintf("Bad encoded_value null value_arg %x", value_arg); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 568 | return false; |
| 569 | } |
| 570 | break; |
| 571 | case DexFile::kDexAnnotationBoolean: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 572 | if (UNLIKELY(value_arg > 1)) { |
| 573 | ErrorStringPrintf("Bad encoded_value boolean size %x", value_arg); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 574 | return false; |
| 575 | } |
| 576 | break; |
| 577 | default: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 578 | ErrorStringPrintf("Bogus encoded_value value_type %x", value_type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 579 | return false; |
| 580 | } |
| 581 | |
| 582 | return true; |
| 583 | } |
| 584 | |
| 585 | bool DexFileVerifier::CheckEncodedArray() { |
| 586 | uint32_t size = DecodeUnsignedLeb128(&ptr_); |
| 587 | |
| 588 | while (size--) { |
| 589 | if (!CheckEncodedValue()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 590 | failure_reason_ = StringPrintf("Bad encoded_array value: %s", failure_reason_.c_str()); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 591 | return false; |
| 592 | } |
| 593 | } |
| 594 | return true; |
| 595 | } |
| 596 | |
| 597 | bool DexFileVerifier::CheckEncodedAnnotation() { |
| 598 | uint32_t idx = DecodeUnsignedLeb128(&ptr_); |
| 599 | if (!CheckIndex(idx, header_->type_ids_size_, "encoded_annotation type_idx")) { |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | uint32_t size = DecodeUnsignedLeb128(&ptr_); |
| 604 | uint32_t last_idx = 0; |
| 605 | |
| 606 | for (uint32_t i = 0; i < size; i++) { |
| 607 | idx = DecodeUnsignedLeb128(&ptr_); |
| 608 | if (!CheckIndex(idx, header_->string_ids_size_, "annotation_element name_idx")) { |
| 609 | return false; |
| 610 | } |
| 611 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 612 | if (UNLIKELY(last_idx >= idx && i != 0)) { |
| 613 | ErrorStringPrintf("Out-of-order annotation_element name_idx: %x then %x", |
| 614 | last_idx, idx); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 615 | return false; |
| 616 | } |
| 617 | |
| 618 | if (!CheckEncodedValue()) { |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | last_idx = idx; |
| 623 | } |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | bool DexFileVerifier::CheckIntraClassDataItem() { |
| 628 | ClassDataItemIterator it(*dex_file_, ptr_); |
| 629 | |
| 630 | for (; it.HasNextStaticField(); it.Next()) { |
| 631 | if (!CheckClassDataItemField(it.GetMemberIndex(), it.GetMemberAccessFlags(), true)) { |
| 632 | return false; |
| 633 | } |
| 634 | } |
| 635 | for (; it.HasNextInstanceField(); it.Next()) { |
| 636 | if (!CheckClassDataItemField(it.GetMemberIndex(), it.GetMemberAccessFlags(), false)) { |
| 637 | return false; |
| 638 | } |
| 639 | } |
| 640 | for (; it.HasNextDirectMethod(); it.Next()) { |
| 641 | if (!CheckClassDataItemMethod(it.GetMemberIndex(), it.GetMemberAccessFlags(), |
| 642 | it.GetMethodCodeItemOffset(), true)) { |
| 643 | return false; |
| 644 | } |
| 645 | } |
| 646 | for (; it.HasNextVirtualMethod(); it.Next()) { |
| 647 | if (!CheckClassDataItemMethod(it.GetMemberIndex(), it.GetMemberAccessFlags(), |
| 648 | it.GetMethodCodeItemOffset(), false)) { |
| 649 | return false; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | ptr_ = it.EndDataPointer(); |
| 654 | return true; |
| 655 | } |
| 656 | |
| 657 | bool DexFileVerifier::CheckIntraCodeItem() { |
| 658 | const DexFile::CodeItem* code_item = reinterpret_cast<const DexFile::CodeItem*>(ptr_); |
| 659 | if (!CheckPointerRange(code_item, code_item + 1, "code")) { |
| 660 | return false; |
| 661 | } |
| 662 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 663 | if (UNLIKELY(code_item->ins_size_ > code_item->registers_size_)) { |
| 664 | ErrorStringPrintf("ins_size (%ud) > registers_size (%ud)", |
| 665 | code_item->ins_size_, code_item->registers_size_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 666 | return false; |
| 667 | } |
| 668 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 669 | if (UNLIKELY((code_item->outs_size_ > 5) && |
| 670 | (code_item->outs_size_ > code_item->registers_size_))) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 671 | /* |
| 672 | * outs_size can be up to 5, even if registers_size is smaller, since the |
| 673 | * short forms of method invocation allow repetitions of a register multiple |
| 674 | * times within a single parameter list. However, longer parameter lists |
| 675 | * need to be represented in-order in the register file. |
| 676 | */ |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 677 | ErrorStringPrintf("outs_size (%ud) > registers_size (%ud)", |
| 678 | code_item->outs_size_, code_item->registers_size_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 679 | return false; |
| 680 | } |
| 681 | |
| 682 | const uint16_t* insns = code_item->insns_; |
| 683 | uint32_t insns_size = code_item->insns_size_in_code_units_; |
| 684 | if (!CheckListSize(insns, insns_size, sizeof(uint16_t), "insns size")) { |
| 685 | return false; |
| 686 | } |
| 687 | |
| 688 | // Grab the end of the insns if there are no try_items. |
| 689 | uint32_t try_items_size = code_item->tries_size_; |
| 690 | if (try_items_size == 0) { |
| 691 | ptr_ = reinterpret_cast<const byte*>(&insns[insns_size]); |
| 692 | return true; |
| 693 | } |
| 694 | |
| 695 | // try_items are 4-byte aligned. Verify the spacer is 0. |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 696 | if (((reinterpret_cast<uintptr_t>(&insns[insns_size]) & 3) != 0) && (insns[insns_size] != 0)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 697 | ErrorStringPrintf("Non-zero padding: %x", insns[insns_size]); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 698 | return false; |
| 699 | } |
| 700 | |
| 701 | const DexFile::TryItem* try_items = DexFile::GetTryItems(*code_item, 0); |
| 702 | ptr_ = DexFile::GetCatchHandlerData(*code_item, 0); |
| 703 | uint32_t handlers_size = DecodeUnsignedLeb128(&ptr_); |
| 704 | |
| 705 | if (!CheckListSize(try_items, try_items_size, sizeof(DexFile::TryItem), "try_items size")) { |
| 706 | return false; |
| 707 | } |
| 708 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 709 | if (UNLIKELY((handlers_size == 0) || (handlers_size >= 65536))) { |
| 710 | ErrorStringPrintf("Invalid handlers_size: %ud", handlers_size); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 711 | return false; |
| 712 | } |
| 713 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 714 | std::unique_ptr<uint32_t[]> handler_offsets(new uint32_t[handlers_size]); |
Elliott Hughes | ee0fa76 | 2012-03-26 17:12:41 -0700 | [diff] [blame] | 715 | if (!CheckAndGetHandlerOffsets(code_item, &handler_offsets[0], handlers_size)) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 716 | return false; |
| 717 | } |
| 718 | |
| 719 | uint32_t last_addr = 0; |
| 720 | while (try_items_size--) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 721 | if (UNLIKELY(try_items->start_addr_ < last_addr)) { |
| 722 | ErrorStringPrintf("Out-of_order try_item with start_addr: %x", try_items->start_addr_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 723 | return false; |
| 724 | } |
| 725 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 726 | if (UNLIKELY(try_items->start_addr_ >= insns_size)) { |
| 727 | ErrorStringPrintf("Invalid try_item start_addr: %x", try_items->start_addr_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 728 | return false; |
| 729 | } |
| 730 | |
| 731 | uint32_t i; |
| 732 | for (i = 0; i < handlers_size; i++) { |
| 733 | if (try_items->handler_off_ == handler_offsets[i]) { |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 738 | if (UNLIKELY(i == handlers_size)) { |
| 739 | ErrorStringPrintf("Bogus handler offset: %x", try_items->handler_off_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 740 | return false; |
| 741 | } |
| 742 | |
| 743 | last_addr = try_items->start_addr_ + try_items->insn_count_; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 744 | if (UNLIKELY(last_addr > insns_size)) { |
| 745 | ErrorStringPrintf("Invalid try_item insn_count: %x", try_items->insn_count_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 746 | return false; |
| 747 | } |
| 748 | |
| 749 | try_items++; |
| 750 | } |
| 751 | |
| 752 | return true; |
| 753 | } |
| 754 | |
| 755 | bool DexFileVerifier::CheckIntraStringDataItem() { |
| 756 | uint32_t size = DecodeUnsignedLeb128(&ptr_); |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 757 | const byte* file_end = begin_ + size_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 758 | |
| 759 | for (uint32_t i = 0; i < size; i++) { |
Brian Carlstrom | c647564 | 2014-05-27 11:14:12 -0700 | [diff] [blame] | 760 | CHECK_LT(i, size); // b/15014252 Prevents hitting the impossible case below |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 761 | if (UNLIKELY(ptr_ >= file_end)) { |
| 762 | ErrorStringPrintf("String data would go beyond end-of-file"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 763 | return false; |
| 764 | } |
| 765 | |
| 766 | uint8_t byte = *(ptr_++); |
| 767 | |
| 768 | // Switch on the high 4 bits. |
| 769 | switch (byte >> 4) { |
| 770 | case 0x00: |
| 771 | // Special case of bit pattern 0xxx. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 772 | if (UNLIKELY(byte == 0)) { |
Brian Carlstrom | c647564 | 2014-05-27 11:14:12 -0700 | [diff] [blame] | 773 | CHECK_LT(i, size); // b/15014252 Actually hit this impossible case with clang |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 774 | ErrorStringPrintf("String data shorter than indicated utf16_size %x", size); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 775 | return false; |
| 776 | } |
| 777 | break; |
| 778 | case 0x01: |
| 779 | case 0x02: |
| 780 | case 0x03: |
| 781 | case 0x04: |
| 782 | case 0x05: |
| 783 | case 0x06: |
| 784 | case 0x07: |
| 785 | // No extra checks necessary for bit pattern 0xxx. |
| 786 | break; |
| 787 | case 0x08: |
| 788 | case 0x09: |
| 789 | case 0x0a: |
| 790 | case 0x0b: |
| 791 | case 0x0f: |
| 792 | // Illegal bit patterns 10xx or 1111. |
| 793 | // Note: 1111 is valid for normal UTF-8, but not here. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 794 | ErrorStringPrintf("Illegal start byte %x in string data", byte); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 795 | return false; |
| 796 | case 0x0c: |
| 797 | case 0x0d: { |
| 798 | // Bit pattern 110x has an additional byte. |
| 799 | uint8_t byte2 = *(ptr_++); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 800 | if (UNLIKELY((byte2 & 0xc0) != 0x80)) { |
| 801 | ErrorStringPrintf("Illegal continuation byte %x in string data", byte2); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 802 | return false; |
| 803 | } |
| 804 | uint16_t value = ((byte & 0x1f) << 6) | (byte2 & 0x3f); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 805 | if (UNLIKELY((value != 0) && (value < 0x80))) { |
| 806 | ErrorStringPrintf("Illegal representation for value %x in string data", value); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 807 | return false; |
| 808 | } |
| 809 | break; |
| 810 | } |
| 811 | case 0x0e: { |
| 812 | // Bit pattern 1110 has 2 additional bytes. |
| 813 | uint8_t byte2 = *(ptr_++); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 814 | if (UNLIKELY((byte2 & 0xc0) != 0x80)) { |
| 815 | ErrorStringPrintf("Illegal continuation byte %x in string data", byte2); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 816 | return false; |
| 817 | } |
| 818 | uint8_t byte3 = *(ptr_++); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 819 | if (UNLIKELY((byte3 & 0xc0) != 0x80)) { |
| 820 | ErrorStringPrintf("Illegal continuation byte %x in string data", byte3); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 821 | return false; |
| 822 | } |
| 823 | uint16_t value = ((byte & 0x0f) << 12) | ((byte2 & 0x3f) << 6) | (byte3 & 0x3f); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 824 | if (UNLIKELY(value < 0x800)) { |
| 825 | ErrorStringPrintf("Illegal representation for value %x in string data", value); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 826 | return false; |
| 827 | } |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 833 | if (UNLIKELY(*(ptr_++) != '\0')) { |
| 834 | ErrorStringPrintf("String longer than indicated size %x", size); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 835 | return false; |
| 836 | } |
| 837 | |
| 838 | return true; |
| 839 | } |
| 840 | |
| 841 | bool DexFileVerifier::CheckIntraDebugInfoItem() { |
| 842 | DecodeUnsignedLeb128(&ptr_); |
| 843 | uint32_t parameters_size = DecodeUnsignedLeb128(&ptr_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 844 | if (UNLIKELY(parameters_size > 65536)) { |
| 845 | ErrorStringPrintf("Invalid parameters_size: %x", parameters_size); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 846 | return false; |
| 847 | } |
| 848 | |
| 849 | for (uint32_t j = 0; j < parameters_size; j++) { |
| 850 | uint32_t parameter_name = DecodeUnsignedLeb128(&ptr_); |
| 851 | if (parameter_name != 0) { |
| 852 | parameter_name--; |
| 853 | if (!CheckIndex(parameter_name, header_->string_ids_size_, "debug_info_item parameter_name")) { |
| 854 | return false; |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | while (true) { |
| 860 | uint8_t opcode = *(ptr_++); |
| 861 | switch (opcode) { |
| 862 | case DexFile::DBG_END_SEQUENCE: { |
| 863 | return true; |
| 864 | } |
| 865 | case DexFile::DBG_ADVANCE_PC: { |
| 866 | DecodeUnsignedLeb128(&ptr_); |
| 867 | break; |
| 868 | } |
| 869 | case DexFile::DBG_ADVANCE_LINE: { |
| 870 | DecodeSignedLeb128(&ptr_); |
| 871 | break; |
| 872 | } |
| 873 | case DexFile::DBG_START_LOCAL: { |
| 874 | uint32_t reg_num = DecodeUnsignedLeb128(&ptr_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 875 | if (UNLIKELY(reg_num >= 65536)) { |
| 876 | ErrorStringPrintf("Bad reg_num for opcode %x", opcode); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 877 | return false; |
| 878 | } |
| 879 | uint32_t name_idx = DecodeUnsignedLeb128(&ptr_); |
| 880 | if (name_idx != 0) { |
| 881 | name_idx--; |
| 882 | if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_START_LOCAL name_idx")) { |
| 883 | return false; |
| 884 | } |
| 885 | } |
| 886 | uint32_t type_idx = DecodeUnsignedLeb128(&ptr_); |
| 887 | if (type_idx != 0) { |
| 888 | type_idx--; |
| 889 | if (!CheckIndex(type_idx, header_->string_ids_size_, "DBG_START_LOCAL type_idx")) { |
| 890 | return false; |
| 891 | } |
| 892 | } |
| 893 | break; |
| 894 | } |
| 895 | case DexFile::DBG_END_LOCAL: |
| 896 | case DexFile::DBG_RESTART_LOCAL: { |
| 897 | uint32_t reg_num = DecodeUnsignedLeb128(&ptr_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 898 | if (UNLIKELY(reg_num >= 65536)) { |
| 899 | ErrorStringPrintf("Bad reg_num for opcode %x", opcode); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 900 | return false; |
| 901 | } |
| 902 | break; |
| 903 | } |
| 904 | case DexFile::DBG_START_LOCAL_EXTENDED: { |
| 905 | uint32_t reg_num = DecodeUnsignedLeb128(&ptr_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 906 | if (UNLIKELY(reg_num >= 65536)) { |
| 907 | ErrorStringPrintf("Bad reg_num for opcode %x", opcode); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 908 | return false; |
| 909 | } |
| 910 | uint32_t name_idx = DecodeUnsignedLeb128(&ptr_); |
| 911 | if (name_idx != 0) { |
| 912 | name_idx--; |
| 913 | if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED name_idx")) { |
| 914 | return false; |
| 915 | } |
| 916 | } |
| 917 | uint32_t type_idx = DecodeUnsignedLeb128(&ptr_); |
| 918 | if (type_idx != 0) { |
| 919 | type_idx--; |
| 920 | if (!CheckIndex(type_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED type_idx")) { |
| 921 | return false; |
| 922 | } |
| 923 | } |
| 924 | uint32_t sig_idx = DecodeUnsignedLeb128(&ptr_); |
| 925 | if (sig_idx != 0) { |
| 926 | sig_idx--; |
| 927 | if (!CheckIndex(sig_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED sig_idx")) { |
| 928 | return false; |
| 929 | } |
| 930 | } |
| 931 | break; |
| 932 | } |
| 933 | case DexFile::DBG_SET_FILE: { |
| 934 | uint32_t name_idx = DecodeUnsignedLeb128(&ptr_); |
| 935 | if (name_idx != 0) { |
| 936 | name_idx--; |
| 937 | if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_SET_FILE name_idx")) { |
| 938 | return false; |
| 939 | } |
| 940 | } |
| 941 | break; |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | bool DexFileVerifier::CheckIntraAnnotationItem() { |
| 948 | if (!CheckPointerRange(ptr_, ptr_ + 1, "annotation visibility")) { |
| 949 | return false; |
| 950 | } |
| 951 | |
| 952 | // Check visibility |
| 953 | switch (*(ptr_++)) { |
| 954 | case DexFile::kDexVisibilityBuild: |
| 955 | case DexFile::kDexVisibilityRuntime: |
| 956 | case DexFile::kDexVisibilitySystem: |
| 957 | break; |
| 958 | default: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 959 | ErrorStringPrintf("Bad annotation visibility: %x", *ptr_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 960 | return false; |
| 961 | } |
| 962 | |
| 963 | if (!CheckEncodedAnnotation()) { |
| 964 | return false; |
| 965 | } |
| 966 | |
| 967 | return true; |
| 968 | } |
| 969 | |
| 970 | bool DexFileVerifier::CheckIntraAnnotationsDirectoryItem() { |
| 971 | const DexFile::AnnotationsDirectoryItem* item = |
| 972 | reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr_); |
| 973 | if (!CheckPointerRange(item, item + 1, "annotations_directory")) { |
| 974 | return false; |
| 975 | } |
| 976 | |
| 977 | // Field annotations follow immediately after the annotations directory. |
| 978 | const DexFile::FieldAnnotationsItem* field_item = |
| 979 | reinterpret_cast<const DexFile::FieldAnnotationsItem*>(item + 1); |
| 980 | uint32_t field_count = item->fields_size_; |
| 981 | if (!CheckListSize(field_item, field_count, sizeof(DexFile::FieldAnnotationsItem), "field_annotations list")) { |
| 982 | return false; |
| 983 | } |
| 984 | |
| 985 | uint32_t last_idx = 0; |
| 986 | for (uint32_t i = 0; i < field_count; i++) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 987 | if (UNLIKELY(last_idx >= field_item->field_idx_ && i != 0)) { |
| 988 | ErrorStringPrintf("Out-of-order field_idx for annotation: %x then %x", last_idx, field_item->field_idx_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 989 | return false; |
| 990 | } |
| 991 | last_idx = field_item->field_idx_; |
| 992 | field_item++; |
| 993 | } |
| 994 | |
| 995 | // Method annotations follow immediately after field annotations. |
| 996 | const DexFile::MethodAnnotationsItem* method_item = |
| 997 | reinterpret_cast<const DexFile::MethodAnnotationsItem*>(field_item); |
| 998 | uint32_t method_count = item->methods_size_; |
| 999 | if (!CheckListSize(method_item, method_count, sizeof(DexFile::MethodAnnotationsItem), "method_annotations list")) { |
| 1000 | return false; |
| 1001 | } |
| 1002 | |
| 1003 | last_idx = 0; |
| 1004 | for (uint32_t i = 0; i < method_count; i++) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1005 | if (UNLIKELY(last_idx >= method_item->method_idx_ && i != 0)) { |
| 1006 | ErrorStringPrintf("Out-of-order method_idx for annotation: %x then %x", |
| 1007 | last_idx, method_item->method_idx_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1008 | return false; |
| 1009 | } |
| 1010 | last_idx = method_item->method_idx_; |
| 1011 | method_item++; |
| 1012 | } |
| 1013 | |
| 1014 | // Parameter annotations follow immediately after method annotations. |
| 1015 | const DexFile::ParameterAnnotationsItem* parameter_item = |
| 1016 | reinterpret_cast<const DexFile::ParameterAnnotationsItem*>(method_item); |
| 1017 | uint32_t parameter_count = item->parameters_size_; |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1018 | if (!CheckListSize(parameter_item, parameter_count, sizeof(DexFile::ParameterAnnotationsItem), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1019 | "parameter_annotations list")) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1020 | return false; |
| 1021 | } |
| 1022 | |
| 1023 | last_idx = 0; |
| 1024 | for (uint32_t i = 0; i < parameter_count; i++) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1025 | if (UNLIKELY(last_idx >= parameter_item->method_idx_ && i != 0)) { |
| 1026 | ErrorStringPrintf("Out-of-order method_idx for annotation: %x then %x", |
| 1027 | last_idx, parameter_item->method_idx_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1028 | return false; |
| 1029 | } |
| 1030 | last_idx = parameter_item->method_idx_; |
| 1031 | parameter_item++; |
| 1032 | } |
| 1033 | |
| 1034 | // Return a pointer to the end of the annotations. |
| 1035 | ptr_ = reinterpret_cast<const byte*>(parameter_item); |
| 1036 | return true; |
| 1037 | } |
| 1038 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1039 | bool DexFileVerifier::CheckIntraSectionIterate(size_t offset, uint32_t count, uint16_t type) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1040 | // Get the right alignment mask for the type of section. |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1041 | size_t alignment_mask; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1042 | switch (type) { |
| 1043 | case DexFile::kDexTypeClassDataItem: |
| 1044 | case DexFile::kDexTypeStringDataItem: |
| 1045 | case DexFile::kDexTypeDebugInfoItem: |
| 1046 | case DexFile::kDexTypeAnnotationItem: |
| 1047 | case DexFile::kDexTypeEncodedArrayItem: |
| 1048 | alignment_mask = sizeof(uint8_t) - 1; |
| 1049 | break; |
| 1050 | default: |
| 1051 | alignment_mask = sizeof(uint32_t) - 1; |
| 1052 | break; |
| 1053 | } |
| 1054 | |
| 1055 | // Iterate through the items in the section. |
| 1056 | for (uint32_t i = 0; i < count; i++) { |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1057 | size_t aligned_offset = (offset + alignment_mask) & ~alignment_mask; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1058 | |
| 1059 | // Check the padding between items. |
| 1060 | if (!CheckPadding(offset, aligned_offset)) { |
| 1061 | return false; |
| 1062 | } |
| 1063 | |
| 1064 | // Check depending on the section type. |
| 1065 | switch (type) { |
| 1066 | case DexFile::kDexTypeStringIdItem: { |
| 1067 | if (!CheckPointerRange(ptr_, ptr_ + sizeof(DexFile::StringId), "string_ids")) { |
| 1068 | return false; |
| 1069 | } |
| 1070 | ptr_ += sizeof(DexFile::StringId); |
| 1071 | break; |
| 1072 | } |
| 1073 | case DexFile::kDexTypeTypeIdItem: { |
| 1074 | if (!CheckPointerRange(ptr_, ptr_ + sizeof(DexFile::TypeId), "type_ids")) { |
| 1075 | return false; |
| 1076 | } |
| 1077 | ptr_ += sizeof(DexFile::TypeId); |
| 1078 | break; |
| 1079 | } |
| 1080 | case DexFile::kDexTypeProtoIdItem: { |
| 1081 | if (!CheckPointerRange(ptr_, ptr_ + sizeof(DexFile::ProtoId), "proto_ids")) { |
| 1082 | return false; |
| 1083 | } |
| 1084 | ptr_ += sizeof(DexFile::ProtoId); |
| 1085 | break; |
| 1086 | } |
| 1087 | case DexFile::kDexTypeFieldIdItem: { |
| 1088 | if (!CheckPointerRange(ptr_, ptr_ + sizeof(DexFile::FieldId), "field_ids")) { |
| 1089 | return false; |
| 1090 | } |
| 1091 | ptr_ += sizeof(DexFile::FieldId); |
| 1092 | break; |
| 1093 | } |
| 1094 | case DexFile::kDexTypeMethodIdItem: { |
| 1095 | if (!CheckPointerRange(ptr_, ptr_ + sizeof(DexFile::MethodId), "method_ids")) { |
| 1096 | return false; |
| 1097 | } |
| 1098 | ptr_ += sizeof(DexFile::MethodId); |
| 1099 | break; |
| 1100 | } |
| 1101 | case DexFile::kDexTypeClassDefItem: { |
| 1102 | if (!CheckPointerRange(ptr_, ptr_ + sizeof(DexFile::ClassDef), "class_defs")) { |
| 1103 | return false; |
| 1104 | } |
| 1105 | ptr_ += sizeof(DexFile::ClassDef); |
| 1106 | break; |
| 1107 | } |
| 1108 | case DexFile::kDexTypeTypeList: { |
| 1109 | const DexFile::TypeList* list = reinterpret_cast<const DexFile::TypeList*>(ptr_); |
| 1110 | const DexFile::TypeItem* item = &list->GetTypeItem(0); |
| 1111 | uint32_t count = list->Size(); |
| 1112 | |
| 1113 | if (!CheckPointerRange(list, list + 1, "type_list") || |
| 1114 | !CheckListSize(item, count, sizeof(DexFile::TypeItem), "type_list size")) { |
| 1115 | return false; |
| 1116 | } |
| 1117 | ptr_ = reinterpret_cast<const byte*>(item + count); |
| 1118 | break; |
| 1119 | } |
| 1120 | case DexFile::kDexTypeAnnotationSetRefList: { |
| 1121 | const DexFile::AnnotationSetRefList* list = |
| 1122 | reinterpret_cast<const DexFile::AnnotationSetRefList*>(ptr_); |
| 1123 | const DexFile::AnnotationSetRefItem* item = list->list_; |
| 1124 | uint32_t count = list->size_; |
| 1125 | |
| 1126 | if (!CheckPointerRange(list, list + 1, "annotation_set_ref_list") || |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1127 | !CheckListSize(item, count, sizeof(DexFile::AnnotationSetRefItem), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1128 | "annotation_set_ref_list size")) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1129 | return false; |
| 1130 | } |
| 1131 | ptr_ = reinterpret_cast<const byte*>(item + count); |
| 1132 | break; |
| 1133 | } |
| 1134 | case DexFile::kDexTypeAnnotationSetItem: { |
| 1135 | const DexFile::AnnotationSetItem* set = |
| 1136 | reinterpret_cast<const DexFile::AnnotationSetItem*>(ptr_); |
| 1137 | const uint32_t* item = set->entries_; |
| 1138 | uint32_t count = set->size_; |
| 1139 | |
| 1140 | if (!CheckPointerRange(set, set + 1, "annotation_set_item") || |
| 1141 | !CheckListSize(item, count, sizeof(uint32_t), "annotation_set_item size")) { |
| 1142 | return false; |
| 1143 | } |
| 1144 | ptr_ = reinterpret_cast<const byte*>(item + count); |
| 1145 | break; |
| 1146 | } |
| 1147 | case DexFile::kDexTypeClassDataItem: { |
| 1148 | if (!CheckIntraClassDataItem()) { |
| 1149 | return false; |
| 1150 | } |
| 1151 | break; |
| 1152 | } |
| 1153 | case DexFile::kDexTypeCodeItem: { |
| 1154 | if (!CheckIntraCodeItem()) { |
| 1155 | return false; |
| 1156 | } |
| 1157 | break; |
| 1158 | } |
| 1159 | case DexFile::kDexTypeStringDataItem: { |
| 1160 | if (!CheckIntraStringDataItem()) { |
| 1161 | return false; |
| 1162 | } |
| 1163 | break; |
| 1164 | } |
| 1165 | case DexFile::kDexTypeDebugInfoItem: { |
| 1166 | if (!CheckIntraDebugInfoItem()) { |
| 1167 | return false; |
| 1168 | } |
| 1169 | break; |
| 1170 | } |
| 1171 | case DexFile::kDexTypeAnnotationItem: { |
| 1172 | if (!CheckIntraAnnotationItem()) { |
| 1173 | return false; |
| 1174 | } |
| 1175 | break; |
| 1176 | } |
| 1177 | case DexFile::kDexTypeEncodedArrayItem: { |
| 1178 | if (!CheckEncodedArray()) { |
| 1179 | return false; |
| 1180 | } |
| 1181 | break; |
| 1182 | } |
| 1183 | case DexFile::kDexTypeAnnotationsDirectoryItem: { |
| 1184 | if (!CheckIntraAnnotationsDirectoryItem()) { |
| 1185 | return false; |
| 1186 | } |
| 1187 | break; |
| 1188 | } |
| 1189 | default: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1190 | ErrorStringPrintf("Unknown map item type %x", type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1191 | return false; |
| 1192 | } |
| 1193 | |
| 1194 | if (IsDataSectionType(type)) { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 1195 | offset_to_type_map_.Put(aligned_offset, type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1198 | aligned_offset = ptr_ - begin_; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1199 | if (UNLIKELY(aligned_offset > size_)) { |
| 1200 | ErrorStringPrintf("Item %d at ends out of bounds", i); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1201 | return false; |
| 1202 | } |
| 1203 | |
| 1204 | offset = aligned_offset; |
| 1205 | } |
| 1206 | |
| 1207 | return true; |
| 1208 | } |
| 1209 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1210 | bool DexFileVerifier::CheckIntraIdSection(size_t offset, uint32_t count, uint16_t type) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1211 | uint32_t expected_offset; |
| 1212 | uint32_t expected_size; |
| 1213 | |
| 1214 | // Get the expected offset and size from the header. |
| 1215 | switch (type) { |
| 1216 | case DexFile::kDexTypeStringIdItem: |
| 1217 | expected_offset = header_->string_ids_off_; |
| 1218 | expected_size = header_->string_ids_size_; |
| 1219 | break; |
| 1220 | case DexFile::kDexTypeTypeIdItem: |
| 1221 | expected_offset = header_->type_ids_off_; |
| 1222 | expected_size = header_->type_ids_size_; |
| 1223 | break; |
| 1224 | case DexFile::kDexTypeProtoIdItem: |
| 1225 | expected_offset = header_->proto_ids_off_; |
| 1226 | expected_size = header_->proto_ids_size_; |
| 1227 | break; |
| 1228 | case DexFile::kDexTypeFieldIdItem: |
| 1229 | expected_offset = header_->field_ids_off_; |
| 1230 | expected_size = header_->field_ids_size_; |
| 1231 | break; |
| 1232 | case DexFile::kDexTypeMethodIdItem: |
| 1233 | expected_offset = header_->method_ids_off_; |
| 1234 | expected_size = header_->method_ids_size_; |
| 1235 | break; |
| 1236 | case DexFile::kDexTypeClassDefItem: |
| 1237 | expected_offset = header_->class_defs_off_; |
| 1238 | expected_size = header_->class_defs_size_; |
| 1239 | break; |
| 1240 | default: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1241 | ErrorStringPrintf("Bad type for id section: %x", type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1242 | return false; |
| 1243 | } |
| 1244 | |
| 1245 | // Check that the offset and size are what were expected from the header. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1246 | if (UNLIKELY(offset != expected_offset)) { |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1247 | ErrorStringPrintf("Bad offset for section: got %zx, expected %x", offset, expected_offset); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1248 | return false; |
| 1249 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1250 | if (UNLIKELY(count != expected_size)) { |
| 1251 | ErrorStringPrintf("Bad size for section: got %x, expected %x", count, expected_size); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1252 | return false; |
| 1253 | } |
| 1254 | |
| 1255 | return CheckIntraSectionIterate(offset, count, type); |
| 1256 | } |
| 1257 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1258 | bool DexFileVerifier::CheckIntraDataSection(size_t offset, uint32_t count, uint16_t type) { |
| 1259 | size_t data_start = header_->data_off_; |
| 1260 | size_t data_end = data_start + header_->data_size_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1261 | |
| 1262 | // Sanity check the offset of the section. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1263 | if (UNLIKELY((offset < data_start) || (offset > data_end))) { |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1264 | ErrorStringPrintf("Bad offset for data subsection: %zx", offset); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1265 | return false; |
| 1266 | } |
| 1267 | |
| 1268 | if (!CheckIntraSectionIterate(offset, count, type)) { |
| 1269 | return false; |
| 1270 | } |
| 1271 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1272 | size_t next_offset = ptr_ - begin_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1273 | if (next_offset > data_end) { |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1274 | ErrorStringPrintf("Out-of-bounds end of data subsection: %zx", next_offset); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1275 | return false; |
| 1276 | } |
| 1277 | |
| 1278 | return true; |
| 1279 | } |
| 1280 | |
| 1281 | bool DexFileVerifier::CheckIntraSection() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1282 | const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ + header_->map_off_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1283 | const DexFile::MapItem* item = map->list_; |
| 1284 | |
| 1285 | uint32_t count = map->size_; |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1286 | size_t offset = 0; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1287 | ptr_ = begin_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1288 | |
| 1289 | // Check the items listed in the map. |
| 1290 | while (count--) { |
| 1291 | uint32_t section_offset = item->offset_; |
| 1292 | uint32_t section_count = item->size_; |
| 1293 | uint16_t type = item->type_; |
| 1294 | |
| 1295 | // Check for padding and overlap between items. |
| 1296 | if (!CheckPadding(offset, section_offset)) { |
| 1297 | return false; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1298 | } else if (UNLIKELY(offset > section_offset)) { |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1299 | ErrorStringPrintf("Section overlap or out-of-order map: %zx, %x", offset, section_offset); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1300 | return false; |
| 1301 | } |
| 1302 | |
| 1303 | // Check each item based on its type. |
| 1304 | switch (type) { |
| 1305 | case DexFile::kDexTypeHeaderItem: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1306 | if (UNLIKELY(section_count != 1)) { |
| 1307 | ErrorStringPrintf("Multiple header items"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1308 | return false; |
| 1309 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1310 | if (UNLIKELY(section_offset != 0)) { |
| 1311 | ErrorStringPrintf("Header at %x, not at start of file", section_offset); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1312 | return false; |
| 1313 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1314 | ptr_ = begin_ + header_->header_size_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1315 | offset = header_->header_size_; |
| 1316 | break; |
| 1317 | case DexFile::kDexTypeStringIdItem: |
| 1318 | case DexFile::kDexTypeTypeIdItem: |
| 1319 | case DexFile::kDexTypeProtoIdItem: |
| 1320 | case DexFile::kDexTypeFieldIdItem: |
| 1321 | case DexFile::kDexTypeMethodIdItem: |
| 1322 | case DexFile::kDexTypeClassDefItem: |
| 1323 | if (!CheckIntraIdSection(section_offset, section_count, type)) { |
| 1324 | return false; |
| 1325 | } |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1326 | offset = ptr_ - begin_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1327 | break; |
| 1328 | case DexFile::kDexTypeMapList: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1329 | if (UNLIKELY(section_count != 1)) { |
| 1330 | ErrorStringPrintf("Multiple map list items"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1331 | return false; |
| 1332 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1333 | if (UNLIKELY(section_offset != header_->map_off_)) { |
| 1334 | ErrorStringPrintf("Map not at header-defined offset: %x, expected %x", |
| 1335 | section_offset, header_->map_off_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1336 | return false; |
| 1337 | } |
| 1338 | ptr_ += sizeof(uint32_t) + (map->size_ * sizeof(DexFile::MapItem)); |
| 1339 | offset = section_offset + sizeof(uint32_t) + (map->size_ * sizeof(DexFile::MapItem)); |
| 1340 | break; |
| 1341 | case DexFile::kDexTypeTypeList: |
| 1342 | case DexFile::kDexTypeAnnotationSetRefList: |
| 1343 | case DexFile::kDexTypeAnnotationSetItem: |
| 1344 | case DexFile::kDexTypeClassDataItem: |
| 1345 | case DexFile::kDexTypeCodeItem: |
| 1346 | case DexFile::kDexTypeStringDataItem: |
| 1347 | case DexFile::kDexTypeDebugInfoItem: |
| 1348 | case DexFile::kDexTypeAnnotationItem: |
| 1349 | case DexFile::kDexTypeEncodedArrayItem: |
| 1350 | case DexFile::kDexTypeAnnotationsDirectoryItem: |
| 1351 | if (!CheckIntraDataSection(section_offset, section_count, type)) { |
| 1352 | return false; |
| 1353 | } |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1354 | offset = ptr_ - begin_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1355 | break; |
| 1356 | default: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1357 | ErrorStringPrintf("Unknown map item type %x", type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1358 | return false; |
| 1359 | } |
| 1360 | |
| 1361 | item++; |
| 1362 | } |
| 1363 | |
| 1364 | return true; |
| 1365 | } |
| 1366 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1367 | bool DexFileVerifier::CheckOffsetToTypeMap(size_t offset, uint16_t type) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1368 | auto it = offset_to_type_map_.find(offset); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1369 | if (UNLIKELY(it == offset_to_type_map_.end())) { |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1370 | ErrorStringPrintf("No data map entry found @ %zx; expected %x", offset, type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1371 | return false; |
| 1372 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1373 | if (UNLIKELY(it->second != type)) { |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1374 | ErrorStringPrintf("Unexpected data map entry @ %zx; expected %x, found %x", |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1375 | offset, type, it->second); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1376 | return false; |
| 1377 | } |
| 1378 | return true; |
| 1379 | } |
| 1380 | |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1381 | uint16_t DexFileVerifier::FindFirstClassDataDefiner(const byte* ptr, bool* success) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1382 | ClassDataItemIterator it(*dex_file_, ptr); |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1383 | *success = true; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1384 | |
| 1385 | if (it.HasNextStaticField() || it.HasNextInstanceField()) { |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1386 | LOAD_FIELD(field, it.GetMemberIndex(), "first_class_data_definer field_id", |
| 1387 | *success = false; return DexFile::kDexNoIndex16) |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1388 | return field->class_idx_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | if (it.HasNextDirectMethod() || it.HasNextVirtualMethod()) { |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1392 | LOAD_METHOD(method, it.GetMemberIndex(), "first_class_data_definer method_id", |
| 1393 | *success = false; return DexFile::kDexNoIndex16) |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1394 | return method->class_idx_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | return DexFile::kDexNoIndex16; |
| 1398 | } |
| 1399 | |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1400 | uint16_t DexFileVerifier::FindFirstAnnotationsDirectoryDefiner(const byte* ptr, bool* success) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1401 | const DexFile::AnnotationsDirectoryItem* item = |
| 1402 | reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr); |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1403 | *success = true; |
| 1404 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1405 | if (item->fields_size_ != 0) { |
| 1406 | DexFile::FieldAnnotationsItem* field_items = (DexFile::FieldAnnotationsItem*) (item + 1); |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1407 | LOAD_FIELD(field, field_items[0].field_idx_, "first_annotations_dir_definer field_id", |
| 1408 | *success = false; return DexFile::kDexNoIndex16) |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1409 | return field->class_idx_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | if (item->methods_size_ != 0) { |
| 1413 | DexFile::MethodAnnotationsItem* method_items = (DexFile::MethodAnnotationsItem*) (item + 1); |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1414 | LOAD_METHOD(method, method_items[0].method_idx_, "first_annotations_dir_definer method id", |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1415 | *success = false; return DexFile::kDexNoIndex16) |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1416 | return method->class_idx_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | if (item->parameters_size_ != 0) { |
| 1420 | DexFile::ParameterAnnotationsItem* parameter_items = (DexFile::ParameterAnnotationsItem*) (item + 1); |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1421 | LOAD_METHOD(method, parameter_items[0].method_idx_, "first_annotations_dir_definer method id", |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1422 | *success = false; return DexFile::kDexNoIndex16) |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1423 | return method->class_idx_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | return DexFile::kDexNoIndex16; |
| 1427 | } |
| 1428 | |
| 1429 | bool DexFileVerifier::CheckInterStringIdItem() { |
| 1430 | const DexFile::StringId* item = reinterpret_cast<const DexFile::StringId*>(ptr_); |
| 1431 | |
| 1432 | // Check the map to make sure it has the right offset->type. |
| 1433 | if (!CheckOffsetToTypeMap(item->string_data_off_, DexFile::kDexTypeStringDataItem)) { |
| 1434 | return false; |
| 1435 | } |
| 1436 | |
| 1437 | // Check ordering between items. |
| 1438 | if (previous_item_ != NULL) { |
| 1439 | const DexFile::StringId* prev_item = reinterpret_cast<const DexFile::StringId*>(previous_item_); |
| 1440 | const char* prev_str = dex_file_->GetStringData(*prev_item); |
| 1441 | const char* str = dex_file_->GetStringData(*item); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1442 | if (UNLIKELY(CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(prev_str, str) >= 0)) { |
| 1443 | ErrorStringPrintf("Out-of-order string_ids: '%s' then '%s'", prev_str, str); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1444 | return false; |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | ptr_ += sizeof(DexFile::StringId); |
| 1449 | return true; |
| 1450 | } |
| 1451 | |
| 1452 | bool DexFileVerifier::CheckInterTypeIdItem() { |
| 1453 | const DexFile::TypeId* item = reinterpret_cast<const DexFile::TypeId*>(ptr_); |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1454 | |
| 1455 | LOAD_STRING(descriptor, item->descriptor_idx_, "inter_type_id_item descriptor_idx") |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1456 | |
| 1457 | // Check that the descriptor is a valid type. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1458 | if (UNLIKELY(!IsValidDescriptor(descriptor))) { |
| 1459 | ErrorStringPrintf("Invalid type descriptor: '%s'", descriptor); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1460 | return false; |
| 1461 | } |
| 1462 | |
| 1463 | // Check ordering between items. |
| 1464 | if (previous_item_ != NULL) { |
| 1465 | const DexFile::TypeId* prev_item = reinterpret_cast<const DexFile::TypeId*>(previous_item_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1466 | if (UNLIKELY(prev_item->descriptor_idx_ >= item->descriptor_idx_)) { |
| 1467 | ErrorStringPrintf("Out-of-order type_ids: %x then %x", |
| 1468 | prev_item->descriptor_idx_, item->descriptor_idx_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1469 | return false; |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | ptr_ += sizeof(DexFile::TypeId); |
| 1474 | return true; |
| 1475 | } |
| 1476 | |
| 1477 | bool DexFileVerifier::CheckInterProtoIdItem() { |
| 1478 | const DexFile::ProtoId* item = reinterpret_cast<const DexFile::ProtoId*>(ptr_); |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1479 | |
| 1480 | LOAD_STRING(shorty, item->shorty_idx_, "inter_proto_id_item shorty_idx") |
| 1481 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1482 | if (item->parameters_off_ != 0 && |
| 1483 | !CheckOffsetToTypeMap(item->parameters_off_, DexFile::kDexTypeTypeList)) { |
| 1484 | return false; |
| 1485 | } |
| 1486 | |
| 1487 | // Check the return type and advance the shorty. |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1488 | LOAD_STRING_BY_TYPE(return_type, item->return_type_idx_, "inter_proto_id_item return_type_idx") |
| 1489 | if (!CheckShortyDescriptorMatch(*shorty, return_type, true)) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1490 | return false; |
| 1491 | } |
| 1492 | shorty++; |
| 1493 | |
| 1494 | DexFileParameterIterator it(*dex_file_, *item); |
| 1495 | while (it.HasNext() && *shorty != '\0') { |
Andreas Gampe | bb836e1 | 2014-06-13 15:31:40 -0700 | [diff] [blame] | 1496 | if (!CheckIndex(it.GetTypeIdx(), dex_file_->NumTypeIds(), |
| 1497 | "inter_proto_id_item shorty type_idx")) { |
| 1498 | return false; |
| 1499 | } |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1500 | const char* descriptor = it.GetDescriptor(); |
| 1501 | if (!CheckShortyDescriptorMatch(*shorty, descriptor, false)) { |
| 1502 | return false; |
| 1503 | } |
| 1504 | it.Next(); |
| 1505 | shorty++; |
| 1506 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1507 | if (UNLIKELY(it.HasNext() || *shorty != '\0')) { |
| 1508 | ErrorStringPrintf("Mismatched length for parameters and shorty"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1509 | return false; |
| 1510 | } |
| 1511 | |
| 1512 | // Check ordering between items. This relies on type_ids being in order. |
| 1513 | if (previous_item_ != NULL) { |
| 1514 | const DexFile::ProtoId* prev = reinterpret_cast<const DexFile::ProtoId*>(previous_item_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1515 | if (UNLIKELY(prev->return_type_idx_ > item->return_type_idx_)) { |
| 1516 | ErrorStringPrintf("Out-of-order proto_id return types"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1517 | return false; |
| 1518 | } else if (prev->return_type_idx_ == item->return_type_idx_) { |
| 1519 | DexFileParameterIterator curr_it(*dex_file_, *item); |
| 1520 | DexFileParameterIterator prev_it(*dex_file_, *prev); |
| 1521 | |
| 1522 | while (curr_it.HasNext() && prev_it.HasNext()) { |
| 1523 | uint16_t prev_idx = prev_it.GetTypeIdx(); |
| 1524 | uint16_t curr_idx = curr_it.GetTypeIdx(); |
| 1525 | if (prev_idx == DexFile::kDexNoIndex16) { |
| 1526 | break; |
| 1527 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1528 | if (UNLIKELY(curr_idx == DexFile::kDexNoIndex16)) { |
| 1529 | ErrorStringPrintf("Out-of-order proto_id arguments"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1530 | return false; |
| 1531 | } |
| 1532 | |
| 1533 | if (prev_idx < curr_idx) { |
| 1534 | break; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1535 | } else if (UNLIKELY(prev_idx > curr_idx)) { |
| 1536 | ErrorStringPrintf("Out-of-order proto_id arguments"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1537 | return false; |
| 1538 | } |
| 1539 | |
| 1540 | prev_it.Next(); |
| 1541 | curr_it.Next(); |
| 1542 | } |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | ptr_ += sizeof(DexFile::ProtoId); |
| 1547 | return true; |
| 1548 | } |
| 1549 | |
| 1550 | bool DexFileVerifier::CheckInterFieldIdItem() { |
| 1551 | const DexFile::FieldId* item = reinterpret_cast<const DexFile::FieldId*>(ptr_); |
| 1552 | |
| 1553 | // Check that the class descriptor is valid. |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1554 | LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_field_id_item class_idx") |
| 1555 | if (UNLIKELY(!IsValidDescriptor(class_descriptor) || class_descriptor[0] != 'L')) { |
| 1556 | ErrorStringPrintf("Invalid descriptor for class_idx: '%s'", class_descriptor); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1557 | return false; |
| 1558 | } |
| 1559 | |
| 1560 | // Check that the type descriptor is a valid field name. |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1561 | LOAD_STRING_BY_TYPE(type_descriptor, item->type_idx_, "inter_field_id_item type_idx") |
| 1562 | if (UNLIKELY(!IsValidDescriptor(type_descriptor) || type_descriptor[0] == 'V')) { |
| 1563 | ErrorStringPrintf("Invalid descriptor for type_idx: '%s'", type_descriptor); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1564 | return false; |
| 1565 | } |
| 1566 | |
| 1567 | // Check that the name is valid. |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1568 | LOAD_STRING(descriptor, item->name_idx_, "inter_field_id_item name_idx") |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1569 | if (UNLIKELY(!IsValidMemberName(descriptor))) { |
| 1570 | ErrorStringPrintf("Invalid field name: '%s'", descriptor); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1571 | return false; |
| 1572 | } |
| 1573 | |
| 1574 | // Check ordering between items. This relies on the other sections being in order. |
| 1575 | if (previous_item_ != NULL) { |
| 1576 | const DexFile::FieldId* prev_item = reinterpret_cast<const DexFile::FieldId*>(previous_item_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1577 | if (UNLIKELY(prev_item->class_idx_ > item->class_idx_)) { |
| 1578 | ErrorStringPrintf("Out-of-order field_ids"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1579 | return false; |
| 1580 | } else if (prev_item->class_idx_ == item->class_idx_) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1581 | if (UNLIKELY(prev_item->name_idx_ > item->name_idx_)) { |
| 1582 | ErrorStringPrintf("Out-of-order field_ids"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1583 | return false; |
| 1584 | } else if (prev_item->name_idx_ == item->name_idx_) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1585 | if (UNLIKELY(prev_item->type_idx_ >= item->type_idx_)) { |
| 1586 | ErrorStringPrintf("Out-of-order field_ids"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1587 | return false; |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | ptr_ += sizeof(DexFile::FieldId); |
| 1594 | return true; |
| 1595 | } |
| 1596 | |
| 1597 | bool DexFileVerifier::CheckInterMethodIdItem() { |
| 1598 | const DexFile::MethodId* item = reinterpret_cast<const DexFile::MethodId*>(ptr_); |
| 1599 | |
| 1600 | // Check that the class descriptor is a valid reference name. |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1601 | LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_method_id_item class_idx") |
| 1602 | if (UNLIKELY(!IsValidDescriptor(class_descriptor) || (class_descriptor[0] != 'L' && |
| 1603 | class_descriptor[0] != '['))) { |
| 1604 | ErrorStringPrintf("Invalid descriptor for class_idx: '%s'", class_descriptor); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1605 | return false; |
| 1606 | } |
| 1607 | |
| 1608 | // Check that the name is valid. |
Andreas Gampe | df10b32 | 2014-06-11 21:46:05 -0700 | [diff] [blame] | 1609 | LOAD_STRING(descriptor, item->name_idx_, "inter_method_id_item name_idx") |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1610 | if (UNLIKELY(!IsValidMemberName(descriptor))) { |
| 1611 | ErrorStringPrintf("Invalid method name: '%s'", descriptor); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1612 | return false; |
| 1613 | } |
| 1614 | |
Andreas Gampe | df10b32 | 2014-06-11 21:46:05 -0700 | [diff] [blame] | 1615 | // Check that the proto id is valid. |
| 1616 | if (UNLIKELY(!CheckIndex(item->proto_idx_, dex_file_->NumProtoIds(), |
| 1617 | "inter_method_id_item proto_idx"))) { |
| 1618 | return false; |
| 1619 | } |
| 1620 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1621 | // Check ordering between items. This relies on the other sections being in order. |
| 1622 | if (previous_item_ != NULL) { |
| 1623 | const DexFile::MethodId* prev_item = reinterpret_cast<const DexFile::MethodId*>(previous_item_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1624 | if (UNLIKELY(prev_item->class_idx_ > item->class_idx_)) { |
| 1625 | ErrorStringPrintf("Out-of-order method_ids"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1626 | return false; |
| 1627 | } else if (prev_item->class_idx_ == item->class_idx_) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1628 | if (UNLIKELY(prev_item->name_idx_ > item->name_idx_)) { |
| 1629 | ErrorStringPrintf("Out-of-order method_ids"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1630 | return false; |
| 1631 | } else if (prev_item->name_idx_ == item->name_idx_) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1632 | if (UNLIKELY(prev_item->proto_idx_ >= item->proto_idx_)) { |
| 1633 | ErrorStringPrintf("Out-of-order method_ids"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1634 | return false; |
| 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | ptr_ += sizeof(DexFile::MethodId); |
| 1641 | return true; |
| 1642 | } |
| 1643 | |
| 1644 | bool DexFileVerifier::CheckInterClassDefItem() { |
| 1645 | const DexFile::ClassDef* item = reinterpret_cast<const DexFile::ClassDef*>(ptr_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1646 | |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1647 | LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_class_def_item class_idx") |
| 1648 | if (UNLIKELY(!IsValidDescriptor(class_descriptor) || class_descriptor[0] != 'L')) { |
| 1649 | ErrorStringPrintf("Invalid class descriptor: '%s'", class_descriptor); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1650 | return false; |
| 1651 | } |
| 1652 | |
| 1653 | if (item->interfaces_off_ != 0 && |
| 1654 | !CheckOffsetToTypeMap(item->interfaces_off_, DexFile::kDexTypeTypeList)) { |
| 1655 | return false; |
| 1656 | } |
| 1657 | if (item->annotations_off_ != 0 && |
| 1658 | !CheckOffsetToTypeMap(item->annotations_off_, DexFile::kDexTypeAnnotationsDirectoryItem)) { |
| 1659 | return false; |
| 1660 | } |
| 1661 | if (item->class_data_off_ != 0 && |
| 1662 | !CheckOffsetToTypeMap(item->class_data_off_, DexFile::kDexTypeClassDataItem)) { |
| 1663 | return false; |
| 1664 | } |
| 1665 | if (item->static_values_off_ != 0 && |
| 1666 | !CheckOffsetToTypeMap(item->static_values_off_, DexFile::kDexTypeEncodedArrayItem)) { |
| 1667 | return false; |
| 1668 | } |
| 1669 | |
| 1670 | if (item->superclass_idx_ != DexFile::kDexNoIndex16) { |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1671 | LOAD_STRING_BY_TYPE(superclass_descriptor, item->superclass_idx_, |
| 1672 | "inter_class_def_item superclass_idx") |
| 1673 | if (UNLIKELY(!IsValidDescriptor(superclass_descriptor) || superclass_descriptor[0] != 'L')) { |
| 1674 | ErrorStringPrintf("Invalid superclass: '%s'", superclass_descriptor); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1675 | return false; |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | const DexFile::TypeList* interfaces = dex_file_->GetInterfacesList(*item); |
| 1680 | if (interfaces != NULL) { |
| 1681 | uint32_t size = interfaces->Size(); |
| 1682 | |
| 1683 | // Ensure that all interfaces refer to classes (not arrays or primitives). |
| 1684 | for (uint32_t i = 0; i < size; i++) { |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1685 | LOAD_STRING_BY_TYPE(inf_descriptor, interfaces->GetTypeItem(i).type_idx_, |
| 1686 | "inter_class_def_item interface type_idx") |
| 1687 | if (UNLIKELY(!IsValidDescriptor(inf_descriptor) || inf_descriptor[0] != 'L')) { |
| 1688 | ErrorStringPrintf("Invalid interface: '%s'", inf_descriptor); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1689 | return false; |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | /* |
| 1694 | * Ensure that there are no duplicates. This is an O(N^2) test, but in |
| 1695 | * practice the number of interfaces implemented by any given class is low. |
| 1696 | */ |
| 1697 | for (uint32_t i = 1; i < size; i++) { |
| 1698 | uint32_t idx1 = interfaces->GetTypeItem(i).type_idx_; |
| 1699 | for (uint32_t j =0; j < i; j++) { |
| 1700 | uint32_t idx2 = interfaces->GetTypeItem(j).type_idx_; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1701 | if (UNLIKELY(idx1 == idx2)) { |
| 1702 | ErrorStringPrintf("Duplicate interface: '%s'", dex_file_->StringByTypeIdx(idx1)); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1703 | return false; |
| 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | // Check that references in class_data_item are to the right class. |
| 1710 | if (item->class_data_off_ != 0) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1711 | const byte* data = begin_ + item->class_data_off_; |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1712 | bool success; |
| 1713 | uint16_t data_definer = FindFirstClassDataDefiner(data, &success); |
| 1714 | if (!success) { |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1715 | return false; |
| 1716 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1717 | if (UNLIKELY((data_definer != item->class_idx_) && (data_definer != DexFile::kDexNoIndex16))) { |
| 1718 | ErrorStringPrintf("Invalid class_data_item"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1719 | return false; |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | // Check that references in annotations_directory_item are to right class. |
| 1724 | if (item->annotations_off_ != 0) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1725 | const byte* data = begin_ + item->annotations_off_; |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1726 | bool success; |
| 1727 | uint16_t annotations_definer = FindFirstAnnotationsDirectoryDefiner(data, &success); |
| 1728 | if (!success) { |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1729 | return false; |
| 1730 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1731 | if (UNLIKELY((annotations_definer != item->class_idx_) && |
| 1732 | (annotations_definer != DexFile::kDexNoIndex16))) { |
| 1733 | ErrorStringPrintf("Invalid annotations_directory_item"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1734 | return false; |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | ptr_ += sizeof(DexFile::ClassDef); |
| 1739 | return true; |
| 1740 | } |
| 1741 | |
| 1742 | bool DexFileVerifier::CheckInterAnnotationSetRefList() { |
| 1743 | const DexFile::AnnotationSetRefList* list = |
| 1744 | reinterpret_cast<const DexFile::AnnotationSetRefList*>(ptr_); |
| 1745 | const DexFile::AnnotationSetRefItem* item = list->list_; |
| 1746 | uint32_t count = list->size_; |
| 1747 | |
| 1748 | while (count--) { |
| 1749 | if (item->annotations_off_ != 0 && |
| 1750 | !CheckOffsetToTypeMap(item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) { |
| 1751 | return false; |
| 1752 | } |
| 1753 | item++; |
| 1754 | } |
| 1755 | |
| 1756 | ptr_ = reinterpret_cast<const byte*>(item); |
| 1757 | return true; |
| 1758 | } |
| 1759 | |
| 1760 | bool DexFileVerifier::CheckInterAnnotationSetItem() { |
| 1761 | const DexFile::AnnotationSetItem* set = reinterpret_cast<const DexFile::AnnotationSetItem*>(ptr_); |
| 1762 | const uint32_t* offsets = set->entries_; |
| 1763 | uint32_t count = set->size_; |
| 1764 | uint32_t last_idx = 0; |
| 1765 | |
| 1766 | for (uint32_t i = 0; i < count; i++) { |
| 1767 | if (*offsets != 0 && !CheckOffsetToTypeMap(*offsets, DexFile::kDexTypeAnnotationItem)) { |
| 1768 | return false; |
| 1769 | } |
| 1770 | |
| 1771 | // Get the annotation from the offset and the type index for the annotation. |
| 1772 | const DexFile::AnnotationItem* annotation = |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1773 | reinterpret_cast<const DexFile::AnnotationItem*>(begin_ + *offsets); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1774 | const uint8_t* data = annotation->annotation_; |
| 1775 | uint32_t idx = DecodeUnsignedLeb128(&data); |
| 1776 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1777 | if (UNLIKELY(last_idx >= idx && i != 0)) { |
| 1778 | ErrorStringPrintf("Out-of-order entry types: %x then %x", last_idx, idx); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1779 | return false; |
| 1780 | } |
| 1781 | |
| 1782 | last_idx = idx; |
| 1783 | offsets++; |
| 1784 | } |
| 1785 | |
| 1786 | ptr_ = reinterpret_cast<const byte*>(offsets); |
| 1787 | return true; |
| 1788 | } |
| 1789 | |
| 1790 | bool DexFileVerifier::CheckInterClassDataItem() { |
| 1791 | ClassDataItemIterator it(*dex_file_, ptr_); |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1792 | bool success; |
| 1793 | uint16_t defining_class = FindFirstClassDataDefiner(ptr_, &success); |
| 1794 | if (!success) { |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1795 | return false; |
| 1796 | } |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1797 | |
| 1798 | for (; it.HasNextStaticField() || it.HasNextInstanceField(); it.Next()) { |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1799 | LOAD_FIELD(field, it.GetMemberIndex(), "inter_class_data_item field_id", return false) |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1800 | if (UNLIKELY(field->class_idx_ != defining_class)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1801 | ErrorStringPrintf("Mismatched defining class for class_data_item field"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1802 | return false; |
| 1803 | } |
| 1804 | } |
| 1805 | for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) { |
| 1806 | uint32_t code_off = it.GetMethodCodeItemOffset(); |
| 1807 | if (code_off != 0 && !CheckOffsetToTypeMap(code_off, DexFile::kDexTypeCodeItem)) { |
| 1808 | return false; |
| 1809 | } |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1810 | LOAD_METHOD(method, it.GetMemberIndex(), "inter_class_data_item method_id", return false) |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1811 | if (UNLIKELY(method->class_idx_ != defining_class)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1812 | ErrorStringPrintf("Mismatched defining class for class_data_item method"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1813 | return false; |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | ptr_ = it.EndDataPointer(); |
| 1818 | return true; |
| 1819 | } |
| 1820 | |
| 1821 | bool DexFileVerifier::CheckInterAnnotationsDirectoryItem() { |
| 1822 | const DexFile::AnnotationsDirectoryItem* item = |
| 1823 | reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr_); |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1824 | bool success; |
| 1825 | uint16_t defining_class = FindFirstAnnotationsDirectoryDefiner(ptr_, &success); |
| 1826 | if (!success) { |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1827 | return false; |
| 1828 | } |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1829 | |
| 1830 | if (item->class_annotations_off_ != 0 && |
| 1831 | !CheckOffsetToTypeMap(item->class_annotations_off_, DexFile::kDexTypeAnnotationSetItem)) { |
| 1832 | return false; |
| 1833 | } |
| 1834 | |
| 1835 | // Field annotations follow immediately after the annotations directory. |
| 1836 | const DexFile::FieldAnnotationsItem* field_item = |
| 1837 | reinterpret_cast<const DexFile::FieldAnnotationsItem*>(item + 1); |
| 1838 | uint32_t field_count = item->fields_size_; |
| 1839 | for (uint32_t i = 0; i < field_count; i++) { |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1840 | LOAD_FIELD(field, field_item->field_idx_, "inter_annotations_directory_item field_id", |
| 1841 | return false) |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1842 | if (UNLIKELY(field->class_idx_ != defining_class)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1843 | ErrorStringPrintf("Mismatched defining class for field_annotation"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1844 | return false; |
| 1845 | } |
| 1846 | if (!CheckOffsetToTypeMap(field_item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) { |
| 1847 | return false; |
| 1848 | } |
| 1849 | field_item++; |
| 1850 | } |
| 1851 | |
| 1852 | // Method annotations follow immediately after field annotations. |
| 1853 | const DexFile::MethodAnnotationsItem* method_item = |
| 1854 | reinterpret_cast<const DexFile::MethodAnnotationsItem*>(field_item); |
| 1855 | uint32_t method_count = item->methods_size_; |
| 1856 | for (uint32_t i = 0; i < method_count; i++) { |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1857 | LOAD_METHOD(method, method_item->method_idx_, "inter_annotations_directory_item method_id", |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1858 | return false) |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1859 | if (UNLIKELY(method->class_idx_ != defining_class)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1860 | ErrorStringPrintf("Mismatched defining class for method_annotation"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1861 | return false; |
| 1862 | } |
| 1863 | if (!CheckOffsetToTypeMap(method_item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) { |
| 1864 | return false; |
| 1865 | } |
| 1866 | method_item++; |
| 1867 | } |
| 1868 | |
| 1869 | // Parameter annotations follow immediately after method annotations. |
| 1870 | const DexFile::ParameterAnnotationsItem* parameter_item = |
| 1871 | reinterpret_cast<const DexFile::ParameterAnnotationsItem*>(method_item); |
| 1872 | uint32_t parameter_count = item->parameters_size_; |
| 1873 | for (uint32_t i = 0; i < parameter_count; i++) { |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1874 | LOAD_METHOD(parameter_method, parameter_item->method_idx_, |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 1875 | "inter_annotations_directory_item parameter method_id", return false) |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 1876 | if (UNLIKELY(parameter_method->class_idx_ != defining_class)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1877 | ErrorStringPrintf("Mismatched defining class for parameter_annotation"); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1878 | return false; |
| 1879 | } |
Dragos Sbirlea | 2b87ddf | 2013-05-28 14:14:12 -0700 | [diff] [blame] | 1880 | if (!CheckOffsetToTypeMap(parameter_item->annotations_off_, |
| 1881 | DexFile::kDexTypeAnnotationSetRefList)) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1882 | return false; |
| 1883 | } |
| 1884 | parameter_item++; |
| 1885 | } |
| 1886 | |
| 1887 | ptr_ = reinterpret_cast<const byte*>(parameter_item); |
| 1888 | return true; |
| 1889 | } |
| 1890 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1891 | bool DexFileVerifier::CheckInterSectionIterate(size_t offset, uint32_t count, uint16_t type) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1892 | // Get the right alignment mask for the type of section. |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1893 | size_t alignment_mask; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1894 | switch (type) { |
| 1895 | case DexFile::kDexTypeClassDataItem: |
| 1896 | alignment_mask = sizeof(uint8_t) - 1; |
| 1897 | break; |
| 1898 | default: |
| 1899 | alignment_mask = sizeof(uint32_t) - 1; |
| 1900 | break; |
| 1901 | } |
| 1902 | |
| 1903 | // Iterate through the items in the section. |
| 1904 | previous_item_ = NULL; |
| 1905 | for (uint32_t i = 0; i < count; i++) { |
| 1906 | uint32_t new_offset = (offset + alignment_mask) & ~alignment_mask; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1907 | ptr_ = begin_ + new_offset; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1908 | const byte* prev_ptr = ptr_; |
| 1909 | |
| 1910 | // Check depending on the section type. |
| 1911 | switch (type) { |
| 1912 | case DexFile::kDexTypeStringIdItem: { |
| 1913 | if (!CheckInterStringIdItem()) { |
| 1914 | return false; |
| 1915 | } |
| 1916 | break; |
| 1917 | } |
| 1918 | case DexFile::kDexTypeTypeIdItem: { |
| 1919 | if (!CheckInterTypeIdItem()) { |
| 1920 | return false; |
| 1921 | } |
| 1922 | break; |
| 1923 | } |
| 1924 | case DexFile::kDexTypeProtoIdItem: { |
| 1925 | if (!CheckInterProtoIdItem()) { |
| 1926 | return false; |
| 1927 | } |
| 1928 | break; |
| 1929 | } |
| 1930 | case DexFile::kDexTypeFieldIdItem: { |
| 1931 | if (!CheckInterFieldIdItem()) { |
| 1932 | return false; |
| 1933 | } |
| 1934 | break; |
| 1935 | } |
| 1936 | case DexFile::kDexTypeMethodIdItem: { |
| 1937 | if (!CheckInterMethodIdItem()) { |
| 1938 | return false; |
| 1939 | } |
| 1940 | break; |
| 1941 | } |
| 1942 | case DexFile::kDexTypeClassDefItem: { |
| 1943 | if (!CheckInterClassDefItem()) { |
| 1944 | return false; |
| 1945 | } |
| 1946 | break; |
| 1947 | } |
| 1948 | case DexFile::kDexTypeAnnotationSetRefList: { |
| 1949 | if (!CheckInterAnnotationSetRefList()) { |
| 1950 | return false; |
| 1951 | } |
| 1952 | break; |
| 1953 | } |
| 1954 | case DexFile::kDexTypeAnnotationSetItem: { |
| 1955 | if (!CheckInterAnnotationSetItem()) { |
| 1956 | return false; |
| 1957 | } |
| 1958 | break; |
| 1959 | } |
| 1960 | case DexFile::kDexTypeClassDataItem: { |
| 1961 | if (!CheckInterClassDataItem()) { |
| 1962 | return false; |
| 1963 | } |
| 1964 | break; |
| 1965 | } |
| 1966 | case DexFile::kDexTypeAnnotationsDirectoryItem: { |
| 1967 | if (!CheckInterAnnotationsDirectoryItem()) { |
| 1968 | return false; |
| 1969 | } |
| 1970 | break; |
| 1971 | } |
| 1972 | default: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1973 | ErrorStringPrintf("Unknown map item type %x", type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1974 | return false; |
| 1975 | } |
| 1976 | |
| 1977 | previous_item_ = prev_ptr; |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 1978 | offset = ptr_ - begin_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | return true; |
| 1982 | } |
| 1983 | |
| 1984 | bool DexFileVerifier::CheckInterSection() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 1985 | const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ + header_->map_off_); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 1986 | const DexFile::MapItem* item = map->list_; |
| 1987 | uint32_t count = map->size_; |
| 1988 | |
| 1989 | // Cross check the items listed in the map. |
| 1990 | while (count--) { |
| 1991 | uint32_t section_offset = item->offset_; |
| 1992 | uint32_t section_count = item->size_; |
| 1993 | uint16_t type = item->type_; |
| 1994 | |
| 1995 | switch (type) { |
| 1996 | case DexFile::kDexTypeHeaderItem: |
| 1997 | case DexFile::kDexTypeMapList: |
| 1998 | case DexFile::kDexTypeTypeList: |
| 1999 | case DexFile::kDexTypeCodeItem: |
| 2000 | case DexFile::kDexTypeStringDataItem: |
| 2001 | case DexFile::kDexTypeDebugInfoItem: |
| 2002 | case DexFile::kDexTypeAnnotationItem: |
| 2003 | case DexFile::kDexTypeEncodedArrayItem: |
| 2004 | break; |
| 2005 | case DexFile::kDexTypeStringIdItem: |
| 2006 | case DexFile::kDexTypeTypeIdItem: |
| 2007 | case DexFile::kDexTypeProtoIdItem: |
| 2008 | case DexFile::kDexTypeFieldIdItem: |
| 2009 | case DexFile::kDexTypeMethodIdItem: |
| 2010 | case DexFile::kDexTypeClassDefItem: |
| 2011 | case DexFile::kDexTypeAnnotationSetRefList: |
| 2012 | case DexFile::kDexTypeAnnotationSetItem: |
| 2013 | case DexFile::kDexTypeClassDataItem: |
| 2014 | case DexFile::kDexTypeAnnotationsDirectoryItem: { |
| 2015 | if (!CheckInterSectionIterate(section_offset, section_count, type)) { |
| 2016 | return false; |
| 2017 | } |
| 2018 | break; |
| 2019 | } |
| 2020 | default: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 2021 | ErrorStringPrintf("Unknown map item type %x", type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 2022 | return false; |
| 2023 | } |
| 2024 | |
| 2025 | item++; |
| 2026 | } |
| 2027 | |
| 2028 | return true; |
| 2029 | } |
| 2030 | |
| 2031 | bool DexFileVerifier::Verify() { |
| 2032 | // Check the header. |
| 2033 | if (!CheckHeader()) { |
| 2034 | return false; |
| 2035 | } |
| 2036 | |
| 2037 | // Check the map section. |
| 2038 | if (!CheckMap()) { |
| 2039 | return false; |
| 2040 | } |
| 2041 | |
| 2042 | // Check structure within remaining sections. |
| 2043 | if (!CheckIntraSection()) { |
| 2044 | return false; |
| 2045 | } |
| 2046 | |
| 2047 | // Check references from one section to another. |
| 2048 | if (!CheckInterSection()) { |
| 2049 | return false; |
| 2050 | } |
| 2051 | |
| 2052 | return true; |
| 2053 | } |
| 2054 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 2055 | void DexFileVerifier::ErrorStringPrintf(const char* fmt, ...) { |
| 2056 | va_list ap; |
| 2057 | va_start(ap, fmt); |
| 2058 | DCHECK(failure_reason_.empty()) << failure_reason_; |
| 2059 | failure_reason_ = StringPrintf("Failure to verify dex file '%s': ", location_); |
| 2060 | StringAppendV(&failure_reason_, fmt, ap); |
| 2061 | va_end(ap); |
| 2062 | } |
| 2063 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 2064 | } // namespace art |