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