blob: 4f33070afc22617169300536a7c47089706c168c [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
jeffhao10037c82012-01-23 15:06:23 -080016
17#include "dex_file_verifier.h"
18
Narayan Kamath92572be2013-11-28 14:06:24 +000019#include <zlib.h>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Narayan Kamath92572be2013-11-28 14:06:24 +000021
Elliott Hughese222ee02012-12-13 14:41:43 -080022#include "base/stringprintf.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070023#include "dex_file-inl.h"
jeffhao10037c82012-01-23 15:06:23 -080024#include "leb128.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070025#include "safe_map.h"
Ian Rogersa6724902013-09-23 09:23:37 -070026#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "utils.h"
jeffhao10037c82012-01-23 15:06:23 -080028
29namespace art {
30
31static 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
55static 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 Gampee09269c2014-06-06 18:45:35 -070069const char* DexFileVerifier::CheckLoadStringByIdx(uint32_t idx, const char* error_string) {
70 if (!CheckIndex(idx, dex_file_->NumStringIds(), error_string)) {
71 return nullptr;
72 }
73 return dex_file_->StringDataByIdx(idx);
74}
75
76const char* DexFileVerifier::CheckLoadStringByTypeIdx(uint32_t type_idx, const char* error_string) {
77 if (!CheckIndex(type_idx, dex_file_->NumTypeIds(), error_string)) {
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
85const DexFile::FieldId* DexFileVerifier::CheckLoadFieldId(uint32_t idx, const char* error_string) {
86 if (!CheckIndex(idx, dex_file_->NumFieldIds(), error_string)) {
87 return nullptr;
88 }
89 return &dex_file_->GetFieldId(idx);
90}
91
92const DexFile::MethodId* DexFileVerifier::CheckLoadMethodId(uint32_t idx, const char* err_string) {
93 if (!CheckIndex(idx, dex_file_->NumMethodIds(), err_string)) {
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); \
102 if (var == nullptr) { \
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); \
109 if (var == nullptr) { \
110 return false; \
111 }
112
113// Helper macro to load method id. Return last parameter on error.
114#define LOAD_METHOD(var, idx, error_string, error_val) \
115 const DexFile::MethodId* var = CheckLoadMethodId(idx, error_string); \
116 if (var == nullptr) { \
117 return error_val; \
118 }
119
120// Helper macro to load method id. Return last parameter on error.
121#define LOAD_FIELD(var, idx, fmt, error_val) \
122 const DexFile::FieldId* var = CheckLoadFieldId(idx, fmt); \
123 if (var == nullptr) { \
124 return error_val; \
125 }
126
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700127bool DexFileVerifier::Verify(const DexFile* dex_file, const byte* begin, size_t size,
128 const char* location, std::string* error_msg) {
Ian Rogers700a4022014-05-19 16:49:03 -0700129 std::unique_ptr<DexFileVerifier> verifier(new DexFileVerifier(dex_file, begin, size, location));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700130 if (!verifier->Verify()) {
131 *error_msg = verifier->FailureReason();
132 return false;
133 }
134 return true;
135}
136
137bool DexFileVerifier::CheckShortyDescriptorMatch(char shorty_char, const char* descriptor,
138 bool is_return_type) {
jeffhao10037c82012-01-23 15:06:23 -0800139 switch (shorty_char) {
140 case 'V':
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700141 if (UNLIKELY(!is_return_type)) {
142 ErrorStringPrintf("Invalid use of void");
jeffhao10037c82012-01-23 15:06:23 -0800143 return false;
144 }
145 // Intentional fallthrough.
146 case 'B':
147 case 'C':
148 case 'D':
149 case 'F':
150 case 'I':
151 case 'J':
152 case 'S':
153 case 'Z':
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700154 if (UNLIKELY((descriptor[0] != shorty_char) || (descriptor[1] != '\0'))) {
155 ErrorStringPrintf("Shorty vs. primitive type mismatch: '%c', '%s'",
156 shorty_char, descriptor);
jeffhao10037c82012-01-23 15:06:23 -0800157 return false;
158 }
159 break;
160 case 'L':
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700161 if (UNLIKELY((descriptor[0] != 'L') && (descriptor[0] != '['))) {
162 ErrorStringPrintf("Shorty vs. type mismatch: '%c', '%s'", shorty_char, descriptor);
jeffhao10037c82012-01-23 15:06:23 -0800163 return false;
164 }
165 break;
166 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700167 ErrorStringPrintf("Bad shorty character: '%c'", shorty_char);
jeffhao10037c82012-01-23 15:06:23 -0800168 return false;
169 }
170 return true;
171}
172
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700173bool DexFileVerifier::CheckPointerRange(const void* start, const void* end, const char* label) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800174 const byte* range_start = reinterpret_cast<const byte*>(start);
175 const byte* range_end = reinterpret_cast<const byte*>(end);
176 const byte* file_start = reinterpret_cast<const byte*>(begin_);
177 const byte* file_end = file_start + size_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700178 if (UNLIKELY((range_start < file_start) || (range_start > file_end) ||
179 (range_end < file_start) || (range_end > file_end))) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800180 ErrorStringPrintf("Bad range for %s: %zx to %zx", label,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700181 range_start - file_start, range_end - file_start);
jeffhao10037c82012-01-23 15:06:23 -0800182 return false;
183 }
184 return true;
185}
186
187bool DexFileVerifier::CheckListSize(const void* start, uint32_t count,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700188 uint32_t element_size, const char* label) {
jeffhao10037c82012-01-23 15:06:23 -0800189 const byte* list_start = reinterpret_cast<const byte*>(start);
190 return CheckPointerRange(list_start, list_start + (count * element_size), label);
191}
192
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700193bool DexFileVerifier::CheckIndex(uint32_t field, uint32_t limit, const char* label) {
194 if (UNLIKELY(field >= limit)) {
195 ErrorStringPrintf("Bad index for %s: %x >= %x", label, field, limit);
jeffhao10037c82012-01-23 15:06:23 -0800196 return false;
197 }
198 return true;
199}
200
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700201bool DexFileVerifier::CheckHeader() {
jeffhaof6174e82012-01-31 16:14:17 -0800202 // Check file size from the header.
203 uint32_t expected_size = header_->file_size_;
204 if (size_ != expected_size) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700205 ErrorStringPrintf("Bad file size (%zd, expected %ud)", size_, expected_size);
jeffhao10037c82012-01-23 15:06:23 -0800206 return false;
207 }
208
209 // Compute and verify the checksum in the header.
210 uint32_t adler_checksum = adler32(0L, Z_NULL, 0);
211 const uint32_t non_sum = sizeof(header_->magic_) + sizeof(header_->checksum_);
212 const byte* non_sum_ptr = reinterpret_cast<const byte*>(header_) + non_sum;
jeffhaof6174e82012-01-31 16:14:17 -0800213 adler_checksum = adler32(adler_checksum, non_sum_ptr, expected_size - non_sum);
jeffhao10037c82012-01-23 15:06:23 -0800214 if (adler_checksum != header_->checksum_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700215 ErrorStringPrintf("Bad checksum (%08x, expected %08x)", adler_checksum, header_->checksum_);
jeffhao10037c82012-01-23 15:06:23 -0800216 return false;
217 }
218
219 // Check the contents of the header.
220 if (header_->endian_tag_ != DexFile::kDexEndianConstant) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700221 ErrorStringPrintf("Unexpected endian_tag: %x", header_->endian_tag_);
jeffhao10037c82012-01-23 15:06:23 -0800222 return false;
223 }
224
225 if (header_->header_size_ != sizeof(DexFile::Header)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700226 ErrorStringPrintf("Bad header size: %ud", header_->header_size_);
jeffhao10037c82012-01-23 15:06:23 -0800227 return false;
228 }
229
230 return true;
231}
232
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700233bool DexFileVerifier::CheckMap() {
Ian Rogers30fab402012-01-23 15:43:46 -0800234 const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ + header_->map_off_);
jeffhao10037c82012-01-23 15:06:23 -0800235 const DexFile::MapItem* item = map->list_;
236
237 uint32_t count = map->size_;
238 uint32_t last_offset = 0;
239 uint32_t data_item_count = 0;
240 uint32_t data_items_left = header_->data_size_;
241 uint32_t used_bits = 0;
242
243 // Sanity check the size of the map list.
244 if (!CheckListSize(item, count, sizeof(DexFile::MapItem), "map size")) {
245 return false;
246 }
247
248 // Check the items listed in the map.
249 for (uint32_t i = 0; i < count; i++) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700250 if (UNLIKELY(last_offset >= item->offset_ && i != 0)) {
251 ErrorStringPrintf("Out of order map item: %x then %x", last_offset, item->offset_);
jeffhao10037c82012-01-23 15:06:23 -0800252 return false;
253 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700254 if (UNLIKELY(item->offset_ >= header_->file_size_)) {
255 ErrorStringPrintf("Map item after end of file: %x, size %x",
256 item->offset_, header_->file_size_);
jeffhao10037c82012-01-23 15:06:23 -0800257 return false;
258 }
259
260 if (IsDataSectionType(item->type_)) {
261 uint32_t icount = item->size_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700262 if (UNLIKELY(icount > data_items_left)) {
263 ErrorStringPrintf("Too many items in data section: %ud", data_item_count + icount);
jeffhao10037c82012-01-23 15:06:23 -0800264 return false;
265 }
266 data_items_left -= icount;
267 data_item_count += icount;
268 }
269
270 uint32_t bit = MapTypeToBitMask(item->type_);
271
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700272 if (UNLIKELY(bit == 0)) {
273 ErrorStringPrintf("Unknown map section type %x", item->type_);
jeffhao10037c82012-01-23 15:06:23 -0800274 return false;
275 }
276
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700277 if (UNLIKELY((used_bits & bit) != 0)) {
278 ErrorStringPrintf("Duplicate map section of type %x", item->type_);
jeffhao10037c82012-01-23 15:06:23 -0800279 return false;
280 }
281
282 used_bits |= bit;
283 last_offset = item->offset_;
284 item++;
285 }
286
287 // Check for missing sections in the map.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700288 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeHeaderItem)) == 0)) {
289 ErrorStringPrintf("Map is missing header entry");
jeffhao10037c82012-01-23 15:06:23 -0800290 return false;
291 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700292 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeMapList)) == 0)) {
293 ErrorStringPrintf("Map is missing map_list entry");
jeffhao10037c82012-01-23 15:06:23 -0800294 return false;
295 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700296 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeStringIdItem)) == 0 &&
297 ((header_->string_ids_off_ != 0) || (header_->string_ids_size_ != 0)))) {
298 ErrorStringPrintf("Map is missing string_ids entry");
jeffhao10037c82012-01-23 15:06:23 -0800299 return false;
300 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700301 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeTypeIdItem)) == 0 &&
302 ((header_->type_ids_off_ != 0) || (header_->type_ids_size_ != 0)))) {
303 ErrorStringPrintf("Map is missing type_ids entry");
jeffhao10037c82012-01-23 15:06:23 -0800304 return false;
305 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700306 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeProtoIdItem)) == 0 &&
307 ((header_->proto_ids_off_ != 0) || (header_->proto_ids_size_ != 0)))) {
308 ErrorStringPrintf("Map is missing proto_ids entry");
jeffhao10037c82012-01-23 15:06:23 -0800309 return false;
310 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700311 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeFieldIdItem)) == 0 &&
312 ((header_->field_ids_off_ != 0) || (header_->field_ids_size_ != 0)))) {
313 ErrorStringPrintf("Map is missing field_ids entry");
jeffhao10037c82012-01-23 15:06:23 -0800314 return false;
315 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700316 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeMethodIdItem)) == 0 &&
317 ((header_->method_ids_off_ != 0) || (header_->method_ids_size_ != 0)))) {
318 ErrorStringPrintf("Map is missing method_ids entry");
jeffhao10037c82012-01-23 15:06:23 -0800319 return false;
320 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700321 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeClassDefItem)) == 0 &&
322 ((header_->class_defs_off_ != 0) || (header_->class_defs_size_ != 0)))) {
323 ErrorStringPrintf("Map is missing class_defs entry");
jeffhao10037c82012-01-23 15:06:23 -0800324 return false;
325 }
jeffhao10037c82012-01-23 15:06:23 -0800326 return true;
327}
328
329uint32_t DexFileVerifier::ReadUnsignedLittleEndian(uint32_t size) {
330 uint32_t result = 0;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700331 if (LIKELY(CheckPointerRange(ptr_, ptr_ + size, "encoded_value"))) {
332 for (uint32_t i = 0; i < size; i++) {
333 result |= ((uint32_t) *(ptr_++)) << (i * 8);
334 }
jeffhao10037c82012-01-23 15:06:23 -0800335 }
jeffhao10037c82012-01-23 15:06:23 -0800336 return result;
337}
338
339bool DexFileVerifier::CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700340 uint32_t* handler_offsets, uint32_t handlers_size) {
jeffhao10037c82012-01-23 15:06:23 -0800341 const byte* handlers_base = DexFile::GetCatchHandlerData(*code_item, 0);
342
343 for (uint32_t i = 0; i < handlers_size; i++) {
344 bool catch_all;
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800345 size_t offset = ptr_ - handlers_base;
jeffhao10037c82012-01-23 15:06:23 -0800346 int32_t size = DecodeSignedLeb128(&ptr_);
347
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700348 if (UNLIKELY((size < -65536) || (size > 65536))) {
349 ErrorStringPrintf("Invalid exception handler size: %d", size);
jeffhao10037c82012-01-23 15:06:23 -0800350 return false;
351 }
352
353 if (size <= 0) {
354 catch_all = true;
355 size = -size;
356 } else {
357 catch_all = false;
358 }
359
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800360 handler_offsets[i] = static_cast<uint32_t>(offset);
jeffhao10037c82012-01-23 15:06:23 -0800361
362 while (size-- > 0) {
363 uint32_t type_idx = DecodeUnsignedLeb128(&ptr_);
364 if (!CheckIndex(type_idx, header_->type_ids_size_, "handler type_idx")) {
365 return false;
366 }
367
368 uint32_t addr = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700369 if (UNLIKELY(addr >= code_item->insns_size_in_code_units_)) {
370 ErrorStringPrintf("Invalid handler addr: %x", addr);
jeffhao10037c82012-01-23 15:06:23 -0800371 return false;
372 }
373 }
374
375 if (catch_all) {
376 uint32_t addr = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700377 if (UNLIKELY(addr >= code_item->insns_size_in_code_units_)) {
378 ErrorStringPrintf("Invalid handler catch_all_addr: %x", addr);
jeffhao10037c82012-01-23 15:06:23 -0800379 return false;
380 }
381 }
382 }
383
384 return true;
385}
386
387bool DexFileVerifier::CheckClassDataItemField(uint32_t idx, uint32_t access_flags,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700388 bool expect_static) {
jeffhao10037c82012-01-23 15:06:23 -0800389 if (!CheckIndex(idx, header_->field_ids_size_, "class_data_item field_idx")) {
390 return false;
391 }
392
393 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700394 if (UNLIKELY(is_static != expect_static)) {
395 ErrorStringPrintf("Static/instance field not in expected list");
jeffhao10037c82012-01-23 15:06:23 -0800396 return false;
397 }
398
399 uint32_t access_field_mask = kAccPublic | kAccPrivate | kAccProtected | kAccStatic |
400 kAccFinal | kAccVolatile | kAccTransient | kAccSynthetic | kAccEnum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700401 if (UNLIKELY((access_flags & ~access_field_mask) != 0)) {
402 ErrorStringPrintf("Bad class_data_item field access_flags %x", access_flags);
jeffhao10037c82012-01-23 15:06:23 -0800403 return false;
404 }
405
406 return true;
407}
408
409bool DexFileVerifier::CheckClassDataItemMethod(uint32_t idx, uint32_t access_flags,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700410 uint32_t code_offset, bool expect_direct) {
jeffhao10037c82012-01-23 15:06:23 -0800411 if (!CheckIndex(idx, header_->method_ids_size_, "class_data_item method_idx")) {
412 return false;
413 }
414
415 bool is_direct = (access_flags & (kAccStatic | kAccPrivate | kAccConstructor)) != 0;
416 bool expect_code = (access_flags & (kAccNative | kAccAbstract)) == 0;
417 bool is_synchronized = (access_flags & kAccSynchronized) != 0;
418 bool allow_synchronized = (access_flags & kAccNative) != 0;
419
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700420 if (UNLIKELY(is_direct != expect_direct)) {
421 ErrorStringPrintf("Direct/virtual method not in expected list");
jeffhao10037c82012-01-23 15:06:23 -0800422 return false;
423 }
424
425 uint32_t access_method_mask = kAccPublic | kAccPrivate | kAccProtected | kAccStatic |
426 kAccFinal | kAccSynchronized | kAccBridge | kAccVarargs | kAccNative | kAccAbstract |
427 kAccStrict | kAccSynthetic | kAccConstructor | kAccDeclaredSynchronized;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700428 if (UNLIKELY(((access_flags & ~access_method_mask) != 0) ||
429 (is_synchronized && !allow_synchronized))) {
430 ErrorStringPrintf("Bad class_data_item method access_flags %x", access_flags);
jeffhao10037c82012-01-23 15:06:23 -0800431 return false;
432 }
433
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700434 if (UNLIKELY(expect_code && (code_offset == 0))) {
435 ErrorStringPrintf("Unexpected zero value for class_data_item method code_off with access "
436 "flags %x", access_flags);
jeffhao10037c82012-01-23 15:06:23 -0800437 return false;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700438 } else if (UNLIKELY(!expect_code && (code_offset != 0))) {
439 ErrorStringPrintf("Unexpected non-zero value %x for class_data_item method code_off"
440 " with access flags %x", code_offset, access_flags);
jeffhao10037c82012-01-23 15:06:23 -0800441 return false;
442 }
443
444 return true;
445}
446
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800447bool DexFileVerifier::CheckPadding(size_t offset, uint32_t aligned_offset) {
jeffhao10037c82012-01-23 15:06:23 -0800448 if (offset < aligned_offset) {
Ian Rogers30fab402012-01-23 15:43:46 -0800449 if (!CheckPointerRange(begin_ + offset, begin_ + aligned_offset, "section")) {
jeffhao10037c82012-01-23 15:06:23 -0800450 return false;
451 }
452 while (offset < aligned_offset) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700453 if (UNLIKELY(*ptr_ != '\0')) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800454 ErrorStringPrintf("Non-zero padding %x before section start at %zx", *ptr_, offset);
jeffhao10037c82012-01-23 15:06:23 -0800455 return false;
456 }
457 ptr_++;
458 offset++;
459 }
460 }
461 return true;
462}
463
464bool DexFileVerifier::CheckEncodedValue() {
465 if (!CheckPointerRange(ptr_, ptr_ + 1, "encoded_value header")) {
466 return false;
467 }
468
469 uint8_t header_byte = *(ptr_++);
470 uint32_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
471 uint32_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
472
473 switch (value_type) {
474 case DexFile::kDexAnnotationByte:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700475 if (UNLIKELY(value_arg != 0)) {
476 ErrorStringPrintf("Bad encoded_value byte size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800477 return false;
478 }
479 ptr_++;
480 break;
481 case DexFile::kDexAnnotationShort:
482 case DexFile::kDexAnnotationChar:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700483 if (UNLIKELY(value_arg > 1)) {
484 ErrorStringPrintf("Bad encoded_value char/short size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800485 return false;
486 }
487 ptr_ += value_arg + 1;
488 break;
489 case DexFile::kDexAnnotationInt:
490 case DexFile::kDexAnnotationFloat:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700491 if (UNLIKELY(value_arg > 3)) {
492 ErrorStringPrintf("Bad encoded_value int/float size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800493 return false;
494 }
495 ptr_ += value_arg + 1;
496 break;
497 case DexFile::kDexAnnotationLong:
498 case DexFile::kDexAnnotationDouble:
499 ptr_ += value_arg + 1;
500 break;
501 case DexFile::kDexAnnotationString: {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700502 if (UNLIKELY(value_arg > 3)) {
503 ErrorStringPrintf("Bad encoded_value string size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800504 return false;
505 }
506 uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
507 if (!CheckIndex(idx, header_->string_ids_size_, "encoded_value string")) {
508 return false;
509 }
510 break;
511 }
512 case DexFile::kDexAnnotationType: {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700513 if (UNLIKELY(value_arg > 3)) {
514 ErrorStringPrintf("Bad encoded_value type size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800515 return false;
516 }
517 uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
518 if (!CheckIndex(idx, header_->type_ids_size_, "encoded_value type")) {
519 return false;
520 }
521 break;
522 }
523 case DexFile::kDexAnnotationField:
524 case DexFile::kDexAnnotationEnum: {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700525 if (UNLIKELY(value_arg > 3)) {
526 ErrorStringPrintf("Bad encoded_value field/enum size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800527 return false;
528 }
529 uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
530 if (!CheckIndex(idx, header_->field_ids_size_, "encoded_value field")) {
531 return false;
532 }
533 break;
534 }
535 case DexFile::kDexAnnotationMethod: {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700536 if (UNLIKELY(value_arg > 3)) {
537 ErrorStringPrintf("Bad encoded_value method size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800538 return false;
539 }
540 uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
541 if (!CheckIndex(idx, header_->method_ids_size_, "encoded_value method")) {
542 return false;
543 }
544 break;
545 }
546 case DexFile::kDexAnnotationArray:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700547 if (UNLIKELY(value_arg != 0)) {
548 ErrorStringPrintf("Bad encoded_value array value_arg %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800549 return false;
550 }
551 if (!CheckEncodedArray()) {
552 return false;
553 }
554 break;
555 case DexFile::kDexAnnotationAnnotation:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700556 if (UNLIKELY(value_arg != 0)) {
557 ErrorStringPrintf("Bad encoded_value annotation value_arg %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800558 return false;
559 }
560 if (!CheckEncodedAnnotation()) {
561 return false;
562 }
563 break;
564 case DexFile::kDexAnnotationNull:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700565 if (UNLIKELY(value_arg != 0)) {
566 ErrorStringPrintf("Bad encoded_value null value_arg %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800567 return false;
568 }
569 break;
570 case DexFile::kDexAnnotationBoolean:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700571 if (UNLIKELY(value_arg > 1)) {
572 ErrorStringPrintf("Bad encoded_value boolean size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800573 return false;
574 }
575 break;
576 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700577 ErrorStringPrintf("Bogus encoded_value value_type %x", value_type);
jeffhao10037c82012-01-23 15:06:23 -0800578 return false;
579 }
580
581 return true;
582}
583
584bool DexFileVerifier::CheckEncodedArray() {
585 uint32_t size = DecodeUnsignedLeb128(&ptr_);
586
587 while (size--) {
588 if (!CheckEncodedValue()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700589 failure_reason_ = StringPrintf("Bad encoded_array value: %s", failure_reason_.c_str());
jeffhao10037c82012-01-23 15:06:23 -0800590 return false;
591 }
592 }
593 return true;
594}
595
596bool DexFileVerifier::CheckEncodedAnnotation() {
597 uint32_t idx = DecodeUnsignedLeb128(&ptr_);
598 if (!CheckIndex(idx, header_->type_ids_size_, "encoded_annotation type_idx")) {
599 return false;
600 }
601
602 uint32_t size = DecodeUnsignedLeb128(&ptr_);
603 uint32_t last_idx = 0;
604
605 for (uint32_t i = 0; i < size; i++) {
606 idx = DecodeUnsignedLeb128(&ptr_);
607 if (!CheckIndex(idx, header_->string_ids_size_, "annotation_element name_idx")) {
608 return false;
609 }
610
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700611 if (UNLIKELY(last_idx >= idx && i != 0)) {
612 ErrorStringPrintf("Out-of-order annotation_element name_idx: %x then %x",
613 last_idx, idx);
jeffhao10037c82012-01-23 15:06:23 -0800614 return false;
615 }
616
617 if (!CheckEncodedValue()) {
618 return false;
619 }
620
621 last_idx = idx;
622 }
623 return true;
624}
625
626bool DexFileVerifier::CheckIntraClassDataItem() {
627 ClassDataItemIterator it(*dex_file_, ptr_);
628
629 for (; it.HasNextStaticField(); it.Next()) {
630 if (!CheckClassDataItemField(it.GetMemberIndex(), it.GetMemberAccessFlags(), true)) {
631 return false;
632 }
633 }
634 for (; it.HasNextInstanceField(); it.Next()) {
635 if (!CheckClassDataItemField(it.GetMemberIndex(), it.GetMemberAccessFlags(), false)) {
636 return false;
637 }
638 }
639 for (; it.HasNextDirectMethod(); it.Next()) {
640 if (!CheckClassDataItemMethod(it.GetMemberIndex(), it.GetMemberAccessFlags(),
641 it.GetMethodCodeItemOffset(), true)) {
642 return false;
643 }
644 }
645 for (; it.HasNextVirtualMethod(); it.Next()) {
646 if (!CheckClassDataItemMethod(it.GetMemberIndex(), it.GetMemberAccessFlags(),
647 it.GetMethodCodeItemOffset(), false)) {
648 return false;
649 }
650 }
651
652 ptr_ = it.EndDataPointer();
653 return true;
654}
655
656bool DexFileVerifier::CheckIntraCodeItem() {
657 const DexFile::CodeItem* code_item = reinterpret_cast<const DexFile::CodeItem*>(ptr_);
658 if (!CheckPointerRange(code_item, code_item + 1, "code")) {
659 return false;
660 }
661
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700662 if (UNLIKELY(code_item->ins_size_ > code_item->registers_size_)) {
663 ErrorStringPrintf("ins_size (%ud) > registers_size (%ud)",
664 code_item->ins_size_, code_item->registers_size_);
jeffhao10037c82012-01-23 15:06:23 -0800665 return false;
666 }
667
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700668 if (UNLIKELY((code_item->outs_size_ > 5) &&
669 (code_item->outs_size_ > code_item->registers_size_))) {
jeffhao10037c82012-01-23 15:06:23 -0800670 /*
671 * outs_size can be up to 5, even if registers_size is smaller, since the
672 * short forms of method invocation allow repetitions of a register multiple
673 * times within a single parameter list. However, longer parameter lists
674 * need to be represented in-order in the register file.
675 */
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700676 ErrorStringPrintf("outs_size (%ud) > registers_size (%ud)",
677 code_item->outs_size_, code_item->registers_size_);
jeffhao10037c82012-01-23 15:06:23 -0800678 return false;
679 }
680
681 const uint16_t* insns = code_item->insns_;
682 uint32_t insns_size = code_item->insns_size_in_code_units_;
683 if (!CheckListSize(insns, insns_size, sizeof(uint16_t), "insns size")) {
684 return false;
685 }
686
687 // Grab the end of the insns if there are no try_items.
688 uint32_t try_items_size = code_item->tries_size_;
689 if (try_items_size == 0) {
690 ptr_ = reinterpret_cast<const byte*>(&insns[insns_size]);
691 return true;
692 }
693
694 // try_items are 4-byte aligned. Verify the spacer is 0.
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800695 if (((reinterpret_cast<uintptr_t>(&insns[insns_size]) & 3) != 0) && (insns[insns_size] != 0)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700696 ErrorStringPrintf("Non-zero padding: %x", insns[insns_size]);
jeffhao10037c82012-01-23 15:06:23 -0800697 return false;
698 }
699
700 const DexFile::TryItem* try_items = DexFile::GetTryItems(*code_item, 0);
701 ptr_ = DexFile::GetCatchHandlerData(*code_item, 0);
702 uint32_t handlers_size = DecodeUnsignedLeb128(&ptr_);
703
704 if (!CheckListSize(try_items, try_items_size, sizeof(DexFile::TryItem), "try_items size")) {
705 return false;
706 }
707
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700708 if (UNLIKELY((handlers_size == 0) || (handlers_size >= 65536))) {
709 ErrorStringPrintf("Invalid handlers_size: %ud", handlers_size);
jeffhao10037c82012-01-23 15:06:23 -0800710 return false;
711 }
712
Ian Rogers700a4022014-05-19 16:49:03 -0700713 std::unique_ptr<uint32_t[]> handler_offsets(new uint32_t[handlers_size]);
Elliott Hughesee0fa762012-03-26 17:12:41 -0700714 if (!CheckAndGetHandlerOffsets(code_item, &handler_offsets[0], handlers_size)) {
jeffhao10037c82012-01-23 15:06:23 -0800715 return false;
716 }
717
718 uint32_t last_addr = 0;
719 while (try_items_size--) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700720 if (UNLIKELY(try_items->start_addr_ < last_addr)) {
721 ErrorStringPrintf("Out-of_order try_item with start_addr: %x", try_items->start_addr_);
jeffhao10037c82012-01-23 15:06:23 -0800722 return false;
723 }
724
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700725 if (UNLIKELY(try_items->start_addr_ >= insns_size)) {
726 ErrorStringPrintf("Invalid try_item start_addr: %x", try_items->start_addr_);
jeffhao10037c82012-01-23 15:06:23 -0800727 return false;
728 }
729
730 uint32_t i;
731 for (i = 0; i < handlers_size; i++) {
732 if (try_items->handler_off_ == handler_offsets[i]) {
733 break;
734 }
735 }
736
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700737 if (UNLIKELY(i == handlers_size)) {
738 ErrorStringPrintf("Bogus handler offset: %x", try_items->handler_off_);
jeffhao10037c82012-01-23 15:06:23 -0800739 return false;
740 }
741
742 last_addr = try_items->start_addr_ + try_items->insn_count_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700743 if (UNLIKELY(last_addr > insns_size)) {
744 ErrorStringPrintf("Invalid try_item insn_count: %x", try_items->insn_count_);
jeffhao10037c82012-01-23 15:06:23 -0800745 return false;
746 }
747
748 try_items++;
749 }
750
751 return true;
752}
753
754bool DexFileVerifier::CheckIntraStringDataItem() {
755 uint32_t size = DecodeUnsignedLeb128(&ptr_);
jeffhaof6174e82012-01-31 16:14:17 -0800756 const byte* file_end = begin_ + size_;
jeffhao10037c82012-01-23 15:06:23 -0800757
758 for (uint32_t i = 0; i < size; i++) {
Brian Carlstromc6475642014-05-27 11:14:12 -0700759 CHECK_LT(i, size); // b/15014252 Prevents hitting the impossible case below
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700760 if (UNLIKELY(ptr_ >= file_end)) {
761 ErrorStringPrintf("String data would go beyond end-of-file");
jeffhao10037c82012-01-23 15:06:23 -0800762 return false;
763 }
764
765 uint8_t byte = *(ptr_++);
766
767 // Switch on the high 4 bits.
768 switch (byte >> 4) {
769 case 0x00:
770 // Special case of bit pattern 0xxx.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700771 if (UNLIKELY(byte == 0)) {
Brian Carlstromc6475642014-05-27 11:14:12 -0700772 CHECK_LT(i, size); // b/15014252 Actually hit this impossible case with clang
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700773 ErrorStringPrintf("String data shorter than indicated utf16_size %x", size);
jeffhao10037c82012-01-23 15:06:23 -0800774 return false;
775 }
776 break;
777 case 0x01:
778 case 0x02:
779 case 0x03:
780 case 0x04:
781 case 0x05:
782 case 0x06:
783 case 0x07:
784 // No extra checks necessary for bit pattern 0xxx.
785 break;
786 case 0x08:
787 case 0x09:
788 case 0x0a:
789 case 0x0b:
790 case 0x0f:
791 // Illegal bit patterns 10xx or 1111.
792 // Note: 1111 is valid for normal UTF-8, but not here.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700793 ErrorStringPrintf("Illegal start byte %x in string data", byte);
jeffhao10037c82012-01-23 15:06:23 -0800794 return false;
795 case 0x0c:
796 case 0x0d: {
797 // Bit pattern 110x has an additional byte.
798 uint8_t byte2 = *(ptr_++);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700799 if (UNLIKELY((byte2 & 0xc0) != 0x80)) {
800 ErrorStringPrintf("Illegal continuation byte %x in string data", byte2);
jeffhao10037c82012-01-23 15:06:23 -0800801 return false;
802 }
803 uint16_t value = ((byte & 0x1f) << 6) | (byte2 & 0x3f);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700804 if (UNLIKELY((value != 0) && (value < 0x80))) {
805 ErrorStringPrintf("Illegal representation for value %x in string data", value);
jeffhao10037c82012-01-23 15:06:23 -0800806 return false;
807 }
808 break;
809 }
810 case 0x0e: {
811 // Bit pattern 1110 has 2 additional bytes.
812 uint8_t byte2 = *(ptr_++);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700813 if (UNLIKELY((byte2 & 0xc0) != 0x80)) {
814 ErrorStringPrintf("Illegal continuation byte %x in string data", byte2);
jeffhao10037c82012-01-23 15:06:23 -0800815 return false;
816 }
817 uint8_t byte3 = *(ptr_++);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700818 if (UNLIKELY((byte3 & 0xc0) != 0x80)) {
819 ErrorStringPrintf("Illegal continuation byte %x in string data", byte3);
jeffhao10037c82012-01-23 15:06:23 -0800820 return false;
821 }
822 uint16_t value = ((byte & 0x0f) << 12) | ((byte2 & 0x3f) << 6) | (byte3 & 0x3f);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700823 if (UNLIKELY(value < 0x800)) {
824 ErrorStringPrintf("Illegal representation for value %x in string data", value);
jeffhao10037c82012-01-23 15:06:23 -0800825 return false;
826 }
827 break;
828 }
829 }
830 }
831
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700832 if (UNLIKELY(*(ptr_++) != '\0')) {
833 ErrorStringPrintf("String longer than indicated size %x", size);
jeffhao10037c82012-01-23 15:06:23 -0800834 return false;
835 }
836
837 return true;
838}
839
840bool DexFileVerifier::CheckIntraDebugInfoItem() {
841 DecodeUnsignedLeb128(&ptr_);
842 uint32_t parameters_size = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700843 if (UNLIKELY(parameters_size > 65536)) {
844 ErrorStringPrintf("Invalid parameters_size: %x", parameters_size);
jeffhao10037c82012-01-23 15:06:23 -0800845 return false;
846 }
847
848 for (uint32_t j = 0; j < parameters_size; j++) {
849 uint32_t parameter_name = DecodeUnsignedLeb128(&ptr_);
850 if (parameter_name != 0) {
851 parameter_name--;
852 if (!CheckIndex(parameter_name, header_->string_ids_size_, "debug_info_item parameter_name")) {
853 return false;
854 }
855 }
856 }
857
858 while (true) {
859 uint8_t opcode = *(ptr_++);
860 switch (opcode) {
861 case DexFile::DBG_END_SEQUENCE: {
862 return true;
863 }
864 case DexFile::DBG_ADVANCE_PC: {
865 DecodeUnsignedLeb128(&ptr_);
866 break;
867 }
868 case DexFile::DBG_ADVANCE_LINE: {
869 DecodeSignedLeb128(&ptr_);
870 break;
871 }
872 case DexFile::DBG_START_LOCAL: {
873 uint32_t reg_num = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700874 if (UNLIKELY(reg_num >= 65536)) {
875 ErrorStringPrintf("Bad reg_num for opcode %x", opcode);
jeffhao10037c82012-01-23 15:06:23 -0800876 return false;
877 }
878 uint32_t name_idx = DecodeUnsignedLeb128(&ptr_);
879 if (name_idx != 0) {
880 name_idx--;
881 if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_START_LOCAL name_idx")) {
882 return false;
883 }
884 }
885 uint32_t type_idx = DecodeUnsignedLeb128(&ptr_);
886 if (type_idx != 0) {
887 type_idx--;
888 if (!CheckIndex(type_idx, header_->string_ids_size_, "DBG_START_LOCAL type_idx")) {
889 return false;
890 }
891 }
892 break;
893 }
894 case DexFile::DBG_END_LOCAL:
895 case DexFile::DBG_RESTART_LOCAL: {
896 uint32_t reg_num = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700897 if (UNLIKELY(reg_num >= 65536)) {
898 ErrorStringPrintf("Bad reg_num for opcode %x", opcode);
jeffhao10037c82012-01-23 15:06:23 -0800899 return false;
900 }
901 break;
902 }
903 case DexFile::DBG_START_LOCAL_EXTENDED: {
904 uint32_t reg_num = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700905 if (UNLIKELY(reg_num >= 65536)) {
906 ErrorStringPrintf("Bad reg_num for opcode %x", opcode);
jeffhao10037c82012-01-23 15:06:23 -0800907 return false;
908 }
909 uint32_t name_idx = DecodeUnsignedLeb128(&ptr_);
910 if (name_idx != 0) {
911 name_idx--;
912 if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED name_idx")) {
913 return false;
914 }
915 }
916 uint32_t type_idx = DecodeUnsignedLeb128(&ptr_);
917 if (type_idx != 0) {
918 type_idx--;
919 if (!CheckIndex(type_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED type_idx")) {
920 return false;
921 }
922 }
923 uint32_t sig_idx = DecodeUnsignedLeb128(&ptr_);
924 if (sig_idx != 0) {
925 sig_idx--;
926 if (!CheckIndex(sig_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED sig_idx")) {
927 return false;
928 }
929 }
930 break;
931 }
932 case DexFile::DBG_SET_FILE: {
933 uint32_t name_idx = DecodeUnsignedLeb128(&ptr_);
934 if (name_idx != 0) {
935 name_idx--;
936 if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_SET_FILE name_idx")) {
937 return false;
938 }
939 }
940 break;
941 }
942 }
943 }
944}
945
946bool DexFileVerifier::CheckIntraAnnotationItem() {
947 if (!CheckPointerRange(ptr_, ptr_ + 1, "annotation visibility")) {
948 return false;
949 }
950
951 // Check visibility
952 switch (*(ptr_++)) {
953 case DexFile::kDexVisibilityBuild:
954 case DexFile::kDexVisibilityRuntime:
955 case DexFile::kDexVisibilitySystem:
956 break;
957 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700958 ErrorStringPrintf("Bad annotation visibility: %x", *ptr_);
jeffhao10037c82012-01-23 15:06:23 -0800959 return false;
960 }
961
962 if (!CheckEncodedAnnotation()) {
963 return false;
964 }
965
966 return true;
967}
968
969bool DexFileVerifier::CheckIntraAnnotationsDirectoryItem() {
970 const DexFile::AnnotationsDirectoryItem* item =
971 reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr_);
972 if (!CheckPointerRange(item, item + 1, "annotations_directory")) {
973 return false;
974 }
975
976 // Field annotations follow immediately after the annotations directory.
977 const DexFile::FieldAnnotationsItem* field_item =
978 reinterpret_cast<const DexFile::FieldAnnotationsItem*>(item + 1);
979 uint32_t field_count = item->fields_size_;
980 if (!CheckListSize(field_item, field_count, sizeof(DexFile::FieldAnnotationsItem), "field_annotations list")) {
981 return false;
982 }
983
984 uint32_t last_idx = 0;
985 for (uint32_t i = 0; i < field_count; i++) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700986 if (UNLIKELY(last_idx >= field_item->field_idx_ && i != 0)) {
987 ErrorStringPrintf("Out-of-order field_idx for annotation: %x then %x", last_idx, field_item->field_idx_);
jeffhao10037c82012-01-23 15:06:23 -0800988 return false;
989 }
990 last_idx = field_item->field_idx_;
991 field_item++;
992 }
993
994 // Method annotations follow immediately after field annotations.
995 const DexFile::MethodAnnotationsItem* method_item =
996 reinterpret_cast<const DexFile::MethodAnnotationsItem*>(field_item);
997 uint32_t method_count = item->methods_size_;
998 if (!CheckListSize(method_item, method_count, sizeof(DexFile::MethodAnnotationsItem), "method_annotations list")) {
999 return false;
1000 }
1001
1002 last_idx = 0;
1003 for (uint32_t i = 0; i < method_count; i++) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001004 if (UNLIKELY(last_idx >= method_item->method_idx_ && i != 0)) {
1005 ErrorStringPrintf("Out-of-order method_idx for annotation: %x then %x",
1006 last_idx, method_item->method_idx_);
jeffhao10037c82012-01-23 15:06:23 -08001007 return false;
1008 }
1009 last_idx = method_item->method_idx_;
1010 method_item++;
1011 }
1012
1013 // Parameter annotations follow immediately after method annotations.
1014 const DexFile::ParameterAnnotationsItem* parameter_item =
1015 reinterpret_cast<const DexFile::ParameterAnnotationsItem*>(method_item);
1016 uint32_t parameter_count = item->parameters_size_;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001017 if (!CheckListSize(parameter_item, parameter_count, sizeof(DexFile::ParameterAnnotationsItem),
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001018 "parameter_annotations list")) {
jeffhao10037c82012-01-23 15:06:23 -08001019 return false;
1020 }
1021
1022 last_idx = 0;
1023 for (uint32_t i = 0; i < parameter_count; i++) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001024 if (UNLIKELY(last_idx >= parameter_item->method_idx_ && i != 0)) {
1025 ErrorStringPrintf("Out-of-order method_idx for annotation: %x then %x",
1026 last_idx, parameter_item->method_idx_);
jeffhao10037c82012-01-23 15:06:23 -08001027 return false;
1028 }
1029 last_idx = parameter_item->method_idx_;
1030 parameter_item++;
1031 }
1032
1033 // Return a pointer to the end of the annotations.
1034 ptr_ = reinterpret_cast<const byte*>(parameter_item);
1035 return true;
1036}
1037
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001038bool DexFileVerifier::CheckIntraSectionIterate(size_t offset, uint32_t count, uint16_t type) {
jeffhao10037c82012-01-23 15:06:23 -08001039 // Get the right alignment mask for the type of section.
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001040 size_t alignment_mask;
jeffhao10037c82012-01-23 15:06:23 -08001041 switch (type) {
1042 case DexFile::kDexTypeClassDataItem:
1043 case DexFile::kDexTypeStringDataItem:
1044 case DexFile::kDexTypeDebugInfoItem:
1045 case DexFile::kDexTypeAnnotationItem:
1046 case DexFile::kDexTypeEncodedArrayItem:
1047 alignment_mask = sizeof(uint8_t) - 1;
1048 break;
1049 default:
1050 alignment_mask = sizeof(uint32_t) - 1;
1051 break;
1052 }
1053
1054 // Iterate through the items in the section.
1055 for (uint32_t i = 0; i < count; i++) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001056 size_t aligned_offset = (offset + alignment_mask) & ~alignment_mask;
jeffhao10037c82012-01-23 15:06:23 -08001057
1058 // Check the padding between items.
1059 if (!CheckPadding(offset, aligned_offset)) {
1060 return false;
1061 }
1062
1063 // Check depending on the section type.
1064 switch (type) {
1065 case DexFile::kDexTypeStringIdItem: {
1066 if (!CheckPointerRange(ptr_, ptr_ + sizeof(DexFile::StringId), "string_ids")) {
1067 return false;
1068 }
1069 ptr_ += sizeof(DexFile::StringId);
1070 break;
1071 }
1072 case DexFile::kDexTypeTypeIdItem: {
1073 if (!CheckPointerRange(ptr_, ptr_ + sizeof(DexFile::TypeId), "type_ids")) {
1074 return false;
1075 }
1076 ptr_ += sizeof(DexFile::TypeId);
1077 break;
1078 }
1079 case DexFile::kDexTypeProtoIdItem: {
1080 if (!CheckPointerRange(ptr_, ptr_ + sizeof(DexFile::ProtoId), "proto_ids")) {
1081 return false;
1082 }
1083 ptr_ += sizeof(DexFile::ProtoId);
1084 break;
1085 }
1086 case DexFile::kDexTypeFieldIdItem: {
1087 if (!CheckPointerRange(ptr_, ptr_ + sizeof(DexFile::FieldId), "field_ids")) {
1088 return false;
1089 }
1090 ptr_ += sizeof(DexFile::FieldId);
1091 break;
1092 }
1093 case DexFile::kDexTypeMethodIdItem: {
1094 if (!CheckPointerRange(ptr_, ptr_ + sizeof(DexFile::MethodId), "method_ids")) {
1095 return false;
1096 }
1097 ptr_ += sizeof(DexFile::MethodId);
1098 break;
1099 }
1100 case DexFile::kDexTypeClassDefItem: {
1101 if (!CheckPointerRange(ptr_, ptr_ + sizeof(DexFile::ClassDef), "class_defs")) {
1102 return false;
1103 }
1104 ptr_ += sizeof(DexFile::ClassDef);
1105 break;
1106 }
1107 case DexFile::kDexTypeTypeList: {
1108 const DexFile::TypeList* list = reinterpret_cast<const DexFile::TypeList*>(ptr_);
1109 const DexFile::TypeItem* item = &list->GetTypeItem(0);
1110 uint32_t count = list->Size();
1111
1112 if (!CheckPointerRange(list, list + 1, "type_list") ||
1113 !CheckListSize(item, count, sizeof(DexFile::TypeItem), "type_list size")) {
1114 return false;
1115 }
1116 ptr_ = reinterpret_cast<const byte*>(item + count);
1117 break;
1118 }
1119 case DexFile::kDexTypeAnnotationSetRefList: {
1120 const DexFile::AnnotationSetRefList* list =
1121 reinterpret_cast<const DexFile::AnnotationSetRefList*>(ptr_);
1122 const DexFile::AnnotationSetRefItem* item = list->list_;
1123 uint32_t count = list->size_;
1124
1125 if (!CheckPointerRange(list, list + 1, "annotation_set_ref_list") ||
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001126 !CheckListSize(item, count, sizeof(DexFile::AnnotationSetRefItem),
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001127 "annotation_set_ref_list size")) {
jeffhao10037c82012-01-23 15:06:23 -08001128 return false;
1129 }
1130 ptr_ = reinterpret_cast<const byte*>(item + count);
1131 break;
1132 }
1133 case DexFile::kDexTypeAnnotationSetItem: {
1134 const DexFile::AnnotationSetItem* set =
1135 reinterpret_cast<const DexFile::AnnotationSetItem*>(ptr_);
1136 const uint32_t* item = set->entries_;
1137 uint32_t count = set->size_;
1138
1139 if (!CheckPointerRange(set, set + 1, "annotation_set_item") ||
1140 !CheckListSize(item, count, sizeof(uint32_t), "annotation_set_item size")) {
1141 return false;
1142 }
1143 ptr_ = reinterpret_cast<const byte*>(item + count);
1144 break;
1145 }
1146 case DexFile::kDexTypeClassDataItem: {
1147 if (!CheckIntraClassDataItem()) {
1148 return false;
1149 }
1150 break;
1151 }
1152 case DexFile::kDexTypeCodeItem: {
1153 if (!CheckIntraCodeItem()) {
1154 return false;
1155 }
1156 break;
1157 }
1158 case DexFile::kDexTypeStringDataItem: {
1159 if (!CheckIntraStringDataItem()) {
1160 return false;
1161 }
1162 break;
1163 }
1164 case DexFile::kDexTypeDebugInfoItem: {
1165 if (!CheckIntraDebugInfoItem()) {
1166 return false;
1167 }
1168 break;
1169 }
1170 case DexFile::kDexTypeAnnotationItem: {
1171 if (!CheckIntraAnnotationItem()) {
1172 return false;
1173 }
1174 break;
1175 }
1176 case DexFile::kDexTypeEncodedArrayItem: {
1177 if (!CheckEncodedArray()) {
1178 return false;
1179 }
1180 break;
1181 }
1182 case DexFile::kDexTypeAnnotationsDirectoryItem: {
1183 if (!CheckIntraAnnotationsDirectoryItem()) {
1184 return false;
1185 }
1186 break;
1187 }
1188 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001189 ErrorStringPrintf("Unknown map item type %x", type);
jeffhao10037c82012-01-23 15:06:23 -08001190 return false;
1191 }
1192
1193 if (IsDataSectionType(type)) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001194 offset_to_type_map_.Put(aligned_offset, type);
jeffhao10037c82012-01-23 15:06:23 -08001195 }
1196
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001197 aligned_offset = ptr_ - begin_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001198 if (UNLIKELY(aligned_offset > size_)) {
1199 ErrorStringPrintf("Item %d at ends out of bounds", i);
jeffhao10037c82012-01-23 15:06:23 -08001200 return false;
1201 }
1202
1203 offset = aligned_offset;
1204 }
1205
1206 return true;
1207}
1208
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001209bool DexFileVerifier::CheckIntraIdSection(size_t offset, uint32_t count, uint16_t type) {
jeffhao10037c82012-01-23 15:06:23 -08001210 uint32_t expected_offset;
1211 uint32_t expected_size;
1212
1213 // Get the expected offset and size from the header.
1214 switch (type) {
1215 case DexFile::kDexTypeStringIdItem:
1216 expected_offset = header_->string_ids_off_;
1217 expected_size = header_->string_ids_size_;
1218 break;
1219 case DexFile::kDexTypeTypeIdItem:
1220 expected_offset = header_->type_ids_off_;
1221 expected_size = header_->type_ids_size_;
1222 break;
1223 case DexFile::kDexTypeProtoIdItem:
1224 expected_offset = header_->proto_ids_off_;
1225 expected_size = header_->proto_ids_size_;
1226 break;
1227 case DexFile::kDexTypeFieldIdItem:
1228 expected_offset = header_->field_ids_off_;
1229 expected_size = header_->field_ids_size_;
1230 break;
1231 case DexFile::kDexTypeMethodIdItem:
1232 expected_offset = header_->method_ids_off_;
1233 expected_size = header_->method_ids_size_;
1234 break;
1235 case DexFile::kDexTypeClassDefItem:
1236 expected_offset = header_->class_defs_off_;
1237 expected_size = header_->class_defs_size_;
1238 break;
1239 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001240 ErrorStringPrintf("Bad type for id section: %x", type);
jeffhao10037c82012-01-23 15:06:23 -08001241 return false;
1242 }
1243
1244 // Check that the offset and size are what were expected from the header.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001245 if (UNLIKELY(offset != expected_offset)) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001246 ErrorStringPrintf("Bad offset for section: got %zx, expected %x", offset, expected_offset);
jeffhao10037c82012-01-23 15:06:23 -08001247 return false;
1248 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001249 if (UNLIKELY(count != expected_size)) {
1250 ErrorStringPrintf("Bad size for section: got %x, expected %x", count, expected_size);
jeffhao10037c82012-01-23 15:06:23 -08001251 return false;
1252 }
1253
1254 return CheckIntraSectionIterate(offset, count, type);
1255}
1256
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001257bool DexFileVerifier::CheckIntraDataSection(size_t offset, uint32_t count, uint16_t type) {
1258 size_t data_start = header_->data_off_;
1259 size_t data_end = data_start + header_->data_size_;
jeffhao10037c82012-01-23 15:06:23 -08001260
1261 // Sanity check the offset of the section.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001262 if (UNLIKELY((offset < data_start) || (offset > data_end))) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001263 ErrorStringPrintf("Bad offset for data subsection: %zx", offset);
jeffhao10037c82012-01-23 15:06:23 -08001264 return false;
1265 }
1266
1267 if (!CheckIntraSectionIterate(offset, count, type)) {
1268 return false;
1269 }
1270
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001271 size_t next_offset = ptr_ - begin_;
jeffhao10037c82012-01-23 15:06:23 -08001272 if (next_offset > data_end) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001273 ErrorStringPrintf("Out-of-bounds end of data subsection: %zx", next_offset);
jeffhao10037c82012-01-23 15:06:23 -08001274 return false;
1275 }
1276
1277 return true;
1278}
1279
1280bool DexFileVerifier::CheckIntraSection() {
Ian Rogers30fab402012-01-23 15:43:46 -08001281 const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ + header_->map_off_);
jeffhao10037c82012-01-23 15:06:23 -08001282 const DexFile::MapItem* item = map->list_;
1283
1284 uint32_t count = map->size_;
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001285 size_t offset = 0;
Ian Rogers30fab402012-01-23 15:43:46 -08001286 ptr_ = begin_;
jeffhao10037c82012-01-23 15:06:23 -08001287
1288 // Check the items listed in the map.
1289 while (count--) {
1290 uint32_t section_offset = item->offset_;
1291 uint32_t section_count = item->size_;
1292 uint16_t type = item->type_;
1293
1294 // Check for padding and overlap between items.
1295 if (!CheckPadding(offset, section_offset)) {
1296 return false;
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001297 } else if (UNLIKELY(offset > section_offset)) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001298 ErrorStringPrintf("Section overlap or out-of-order map: %zx, %x", offset, section_offset);
jeffhao10037c82012-01-23 15:06:23 -08001299 return false;
1300 }
1301
1302 // Check each item based on its type.
1303 switch (type) {
1304 case DexFile::kDexTypeHeaderItem:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001305 if (UNLIKELY(section_count != 1)) {
1306 ErrorStringPrintf("Multiple header items");
jeffhao10037c82012-01-23 15:06:23 -08001307 return false;
1308 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001309 if (UNLIKELY(section_offset != 0)) {
1310 ErrorStringPrintf("Header at %x, not at start of file", section_offset);
jeffhao10037c82012-01-23 15:06:23 -08001311 return false;
1312 }
Ian Rogers30fab402012-01-23 15:43:46 -08001313 ptr_ = begin_ + header_->header_size_;
jeffhao10037c82012-01-23 15:06:23 -08001314 offset = header_->header_size_;
1315 break;
1316 case DexFile::kDexTypeStringIdItem:
1317 case DexFile::kDexTypeTypeIdItem:
1318 case DexFile::kDexTypeProtoIdItem:
1319 case DexFile::kDexTypeFieldIdItem:
1320 case DexFile::kDexTypeMethodIdItem:
1321 case DexFile::kDexTypeClassDefItem:
1322 if (!CheckIntraIdSection(section_offset, section_count, type)) {
1323 return false;
1324 }
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001325 offset = ptr_ - begin_;
jeffhao10037c82012-01-23 15:06:23 -08001326 break;
1327 case DexFile::kDexTypeMapList:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001328 if (UNLIKELY(section_count != 1)) {
1329 ErrorStringPrintf("Multiple map list items");
jeffhao10037c82012-01-23 15:06:23 -08001330 return false;
1331 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001332 if (UNLIKELY(section_offset != header_->map_off_)) {
1333 ErrorStringPrintf("Map not at header-defined offset: %x, expected %x",
1334 section_offset, header_->map_off_);
jeffhao10037c82012-01-23 15:06:23 -08001335 return false;
1336 }
1337 ptr_ += sizeof(uint32_t) + (map->size_ * sizeof(DexFile::MapItem));
1338 offset = section_offset + sizeof(uint32_t) + (map->size_ * sizeof(DexFile::MapItem));
1339 break;
1340 case DexFile::kDexTypeTypeList:
1341 case DexFile::kDexTypeAnnotationSetRefList:
1342 case DexFile::kDexTypeAnnotationSetItem:
1343 case DexFile::kDexTypeClassDataItem:
1344 case DexFile::kDexTypeCodeItem:
1345 case DexFile::kDexTypeStringDataItem:
1346 case DexFile::kDexTypeDebugInfoItem:
1347 case DexFile::kDexTypeAnnotationItem:
1348 case DexFile::kDexTypeEncodedArrayItem:
1349 case DexFile::kDexTypeAnnotationsDirectoryItem:
1350 if (!CheckIntraDataSection(section_offset, section_count, type)) {
1351 return false;
1352 }
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001353 offset = ptr_ - begin_;
jeffhao10037c82012-01-23 15:06:23 -08001354 break;
1355 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001356 ErrorStringPrintf("Unknown map item type %x", type);
jeffhao10037c82012-01-23 15:06:23 -08001357 return false;
1358 }
1359
1360 item++;
1361 }
1362
1363 return true;
1364}
1365
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001366bool DexFileVerifier::CheckOffsetToTypeMap(size_t offset, uint16_t type) {
Mathieu Chartier02e25112013-08-14 16:14:24 -07001367 auto it = offset_to_type_map_.find(offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001368 if (UNLIKELY(it == offset_to_type_map_.end())) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001369 ErrorStringPrintf("No data map entry found @ %zx; expected %x", offset, type);
jeffhao10037c82012-01-23 15:06:23 -08001370 return false;
1371 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001372 if (UNLIKELY(it->second != type)) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001373 ErrorStringPrintf("Unexpected data map entry @ %zx; expected %x, found %x",
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001374 offset, type, it->second);
jeffhao10037c82012-01-23 15:06:23 -08001375 return false;
1376 }
1377 return true;
1378}
1379
Andreas Gampee09269c2014-06-06 18:45:35 -07001380uint32_t DexFileVerifier::FindFirstClassDataDefiner(const byte* ptr) {
jeffhao10037c82012-01-23 15:06:23 -08001381 ClassDataItemIterator it(*dex_file_, ptr);
1382
1383 if (it.HasNextStaticField() || it.HasNextInstanceField()) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001384 LOAD_FIELD(field, it.GetMemberIndex(), "first_class_data_definer field_id", 0x10000U)
1385 return field->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001386 }
1387
1388 if (it.HasNextDirectMethod() || it.HasNextVirtualMethod()) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001389 LOAD_METHOD(method, it.GetMemberIndex(), "first_class_data_definer method_id", 0x10000U)
1390 return method->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001391 }
1392
1393 return DexFile::kDexNoIndex16;
1394}
1395
Andreas Gampee09269c2014-06-06 18:45:35 -07001396uint32_t DexFileVerifier::FindFirstAnnotationsDirectoryDefiner(const byte* ptr) {
jeffhao10037c82012-01-23 15:06:23 -08001397 const DexFile::AnnotationsDirectoryItem* item =
1398 reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr);
1399 if (item->fields_size_ != 0) {
1400 DexFile::FieldAnnotationsItem* field_items = (DexFile::FieldAnnotationsItem*) (item + 1);
Andreas Gampee09269c2014-06-06 18:45:35 -07001401 LOAD_FIELD(field, field_items[0].field_idx_, "first_annotations_dir_definer field_id", 0x10000U)
1402 return field->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001403 }
1404
1405 if (item->methods_size_ != 0) {
1406 DexFile::MethodAnnotationsItem* method_items = (DexFile::MethodAnnotationsItem*) (item + 1);
Andreas Gampee09269c2014-06-06 18:45:35 -07001407 LOAD_METHOD(method, method_items[0].method_idx_, "first_annotations_dir_definer method id",
1408 0x10000U)
1409 return method->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001410 }
1411
1412 if (item->parameters_size_ != 0) {
1413 DexFile::ParameterAnnotationsItem* parameter_items = (DexFile::ParameterAnnotationsItem*) (item + 1);
Andreas Gampee09269c2014-06-06 18:45:35 -07001414 LOAD_METHOD(method, parameter_items[0].method_idx_, "first_annotations_dir_definer method id",
1415 0x10000U)
1416 return method->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001417 }
1418
1419 return DexFile::kDexNoIndex16;
1420}
1421
1422bool DexFileVerifier::CheckInterStringIdItem() {
1423 const DexFile::StringId* item = reinterpret_cast<const DexFile::StringId*>(ptr_);
1424
1425 // Check the map to make sure it has the right offset->type.
1426 if (!CheckOffsetToTypeMap(item->string_data_off_, DexFile::kDexTypeStringDataItem)) {
1427 return false;
1428 }
1429
1430 // Check ordering between items.
1431 if (previous_item_ != NULL) {
1432 const DexFile::StringId* prev_item = reinterpret_cast<const DexFile::StringId*>(previous_item_);
1433 const char* prev_str = dex_file_->GetStringData(*prev_item);
1434 const char* str = dex_file_->GetStringData(*item);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001435 if (UNLIKELY(CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(prev_str, str) >= 0)) {
1436 ErrorStringPrintf("Out-of-order string_ids: '%s' then '%s'", prev_str, str);
jeffhao10037c82012-01-23 15:06:23 -08001437 return false;
1438 }
1439 }
1440
1441 ptr_ += sizeof(DexFile::StringId);
1442 return true;
1443}
1444
1445bool DexFileVerifier::CheckInterTypeIdItem() {
1446 const DexFile::TypeId* item = reinterpret_cast<const DexFile::TypeId*>(ptr_);
Andreas Gampee09269c2014-06-06 18:45:35 -07001447
1448 LOAD_STRING(descriptor, item->descriptor_idx_, "inter_type_id_item descriptor_idx")
jeffhao10037c82012-01-23 15:06:23 -08001449
1450 // Check that the descriptor is a valid type.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001451 if (UNLIKELY(!IsValidDescriptor(descriptor))) {
1452 ErrorStringPrintf("Invalid type descriptor: '%s'", descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001453 return false;
1454 }
1455
1456 // Check ordering between items.
1457 if (previous_item_ != NULL) {
1458 const DexFile::TypeId* prev_item = reinterpret_cast<const DexFile::TypeId*>(previous_item_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001459 if (UNLIKELY(prev_item->descriptor_idx_ >= item->descriptor_idx_)) {
1460 ErrorStringPrintf("Out-of-order type_ids: %x then %x",
1461 prev_item->descriptor_idx_, item->descriptor_idx_);
jeffhao10037c82012-01-23 15:06:23 -08001462 return false;
1463 }
1464 }
1465
1466 ptr_ += sizeof(DexFile::TypeId);
1467 return true;
1468}
1469
1470bool DexFileVerifier::CheckInterProtoIdItem() {
1471 const DexFile::ProtoId* item = reinterpret_cast<const DexFile::ProtoId*>(ptr_);
Andreas Gampee09269c2014-06-06 18:45:35 -07001472
1473 LOAD_STRING(shorty, item->shorty_idx_, "inter_proto_id_item shorty_idx")
1474
jeffhao10037c82012-01-23 15:06:23 -08001475 if (item->parameters_off_ != 0 &&
1476 !CheckOffsetToTypeMap(item->parameters_off_, DexFile::kDexTypeTypeList)) {
1477 return false;
1478 }
1479
1480 // Check the return type and advance the shorty.
Andreas Gampee09269c2014-06-06 18:45:35 -07001481 LOAD_STRING_BY_TYPE(return_type, item->return_type_idx_, "inter_proto_id_item return_type_idx")
1482 if (!CheckShortyDescriptorMatch(*shorty, return_type, true)) {
jeffhao10037c82012-01-23 15:06:23 -08001483 return false;
1484 }
1485 shorty++;
1486
1487 DexFileParameterIterator it(*dex_file_, *item);
1488 while (it.HasNext() && *shorty != '\0') {
1489 const char* descriptor = it.GetDescriptor();
1490 if (!CheckShortyDescriptorMatch(*shorty, descriptor, false)) {
1491 return false;
1492 }
1493 it.Next();
1494 shorty++;
1495 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001496 if (UNLIKELY(it.HasNext() || *shorty != '\0')) {
1497 ErrorStringPrintf("Mismatched length for parameters and shorty");
jeffhao10037c82012-01-23 15:06:23 -08001498 return false;
1499 }
1500
1501 // Check ordering between items. This relies on type_ids being in order.
1502 if (previous_item_ != NULL) {
1503 const DexFile::ProtoId* prev = reinterpret_cast<const DexFile::ProtoId*>(previous_item_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001504 if (UNLIKELY(prev->return_type_idx_ > item->return_type_idx_)) {
1505 ErrorStringPrintf("Out-of-order proto_id return types");
jeffhao10037c82012-01-23 15:06:23 -08001506 return false;
1507 } else if (prev->return_type_idx_ == item->return_type_idx_) {
1508 DexFileParameterIterator curr_it(*dex_file_, *item);
1509 DexFileParameterIterator prev_it(*dex_file_, *prev);
1510
1511 while (curr_it.HasNext() && prev_it.HasNext()) {
1512 uint16_t prev_idx = prev_it.GetTypeIdx();
1513 uint16_t curr_idx = curr_it.GetTypeIdx();
1514 if (prev_idx == DexFile::kDexNoIndex16) {
1515 break;
1516 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001517 if (UNLIKELY(curr_idx == DexFile::kDexNoIndex16)) {
1518 ErrorStringPrintf("Out-of-order proto_id arguments");
jeffhao10037c82012-01-23 15:06:23 -08001519 return false;
1520 }
1521
1522 if (prev_idx < curr_idx) {
1523 break;
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001524 } else if (UNLIKELY(prev_idx > curr_idx)) {
1525 ErrorStringPrintf("Out-of-order proto_id arguments");
jeffhao10037c82012-01-23 15:06:23 -08001526 return false;
1527 }
1528
1529 prev_it.Next();
1530 curr_it.Next();
1531 }
1532 }
1533 }
1534
1535 ptr_ += sizeof(DexFile::ProtoId);
1536 return true;
1537}
1538
1539bool DexFileVerifier::CheckInterFieldIdItem() {
1540 const DexFile::FieldId* item = reinterpret_cast<const DexFile::FieldId*>(ptr_);
1541
1542 // Check that the class descriptor is valid.
Andreas Gampee09269c2014-06-06 18:45:35 -07001543 LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_field_id_item class_idx")
1544 if (UNLIKELY(!IsValidDescriptor(class_descriptor) || class_descriptor[0] != 'L')) {
1545 ErrorStringPrintf("Invalid descriptor for class_idx: '%s'", class_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001546 return false;
1547 }
1548
1549 // Check that the type descriptor is a valid field name.
Andreas Gampee09269c2014-06-06 18:45:35 -07001550 LOAD_STRING_BY_TYPE(type_descriptor, item->type_idx_, "inter_field_id_item type_idx")
1551 if (UNLIKELY(!IsValidDescriptor(type_descriptor) || type_descriptor[0] == 'V')) {
1552 ErrorStringPrintf("Invalid descriptor for type_idx: '%s'", type_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001553 return false;
1554 }
1555
1556 // Check that the name is valid.
Andreas Gampee09269c2014-06-06 18:45:35 -07001557 LOAD_STRING(descriptor, item->name_idx_, "inter_field_id_item name_idx")
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001558 if (UNLIKELY(!IsValidMemberName(descriptor))) {
1559 ErrorStringPrintf("Invalid field name: '%s'", descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001560 return false;
1561 }
1562
1563 // Check ordering between items. This relies on the other sections being in order.
1564 if (previous_item_ != NULL) {
1565 const DexFile::FieldId* prev_item = reinterpret_cast<const DexFile::FieldId*>(previous_item_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001566 if (UNLIKELY(prev_item->class_idx_ > item->class_idx_)) {
1567 ErrorStringPrintf("Out-of-order field_ids");
jeffhao10037c82012-01-23 15:06:23 -08001568 return false;
1569 } else if (prev_item->class_idx_ == item->class_idx_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001570 if (UNLIKELY(prev_item->name_idx_ > item->name_idx_)) {
1571 ErrorStringPrintf("Out-of-order field_ids");
jeffhao10037c82012-01-23 15:06:23 -08001572 return false;
1573 } else if (prev_item->name_idx_ == item->name_idx_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001574 if (UNLIKELY(prev_item->type_idx_ >= item->type_idx_)) {
1575 ErrorStringPrintf("Out-of-order field_ids");
jeffhao10037c82012-01-23 15:06:23 -08001576 return false;
1577 }
1578 }
1579 }
1580 }
1581
1582 ptr_ += sizeof(DexFile::FieldId);
1583 return true;
1584}
1585
1586bool DexFileVerifier::CheckInterMethodIdItem() {
1587 const DexFile::MethodId* item = reinterpret_cast<const DexFile::MethodId*>(ptr_);
1588
1589 // Check that the class descriptor is a valid reference name.
Andreas Gampee09269c2014-06-06 18:45:35 -07001590 LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_method_id_item class_idx")
1591 if (UNLIKELY(!IsValidDescriptor(class_descriptor) || (class_descriptor[0] != 'L' &&
1592 class_descriptor[0] != '['))) {
1593 ErrorStringPrintf("Invalid descriptor for class_idx: '%s'", class_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001594 return false;
1595 }
1596
1597 // Check that the name is valid.
Andreas Gampee09269c2014-06-06 18:45:35 -07001598 LOAD_STRING(descriptor, item->name_idx_, "inter_method_id_item class_idx")
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001599 if (UNLIKELY(!IsValidMemberName(descriptor))) {
1600 ErrorStringPrintf("Invalid method name: '%s'", descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001601 return false;
1602 }
1603
1604 // Check ordering between items. This relies on the other sections being in order.
1605 if (previous_item_ != NULL) {
1606 const DexFile::MethodId* prev_item = reinterpret_cast<const DexFile::MethodId*>(previous_item_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001607 if (UNLIKELY(prev_item->class_idx_ > item->class_idx_)) {
1608 ErrorStringPrintf("Out-of-order method_ids");
jeffhao10037c82012-01-23 15:06:23 -08001609 return false;
1610 } else if (prev_item->class_idx_ == item->class_idx_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001611 if (UNLIKELY(prev_item->name_idx_ > item->name_idx_)) {
1612 ErrorStringPrintf("Out-of-order method_ids");
jeffhao10037c82012-01-23 15:06:23 -08001613 return false;
1614 } else if (prev_item->name_idx_ == item->name_idx_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001615 if (UNLIKELY(prev_item->proto_idx_ >= item->proto_idx_)) {
1616 ErrorStringPrintf("Out-of-order method_ids");
jeffhao10037c82012-01-23 15:06:23 -08001617 return false;
1618 }
1619 }
1620 }
1621 }
1622
1623 ptr_ += sizeof(DexFile::MethodId);
1624 return true;
1625}
1626
1627bool DexFileVerifier::CheckInterClassDefItem() {
1628 const DexFile::ClassDef* item = reinterpret_cast<const DexFile::ClassDef*>(ptr_);
jeffhao10037c82012-01-23 15:06:23 -08001629
Andreas Gampee09269c2014-06-06 18:45:35 -07001630 LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_class_def_item class_idx")
1631 if (UNLIKELY(!IsValidDescriptor(class_descriptor) || class_descriptor[0] != 'L')) {
1632 ErrorStringPrintf("Invalid class descriptor: '%s'", class_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001633 return false;
1634 }
1635
1636 if (item->interfaces_off_ != 0 &&
1637 !CheckOffsetToTypeMap(item->interfaces_off_, DexFile::kDexTypeTypeList)) {
1638 return false;
1639 }
1640 if (item->annotations_off_ != 0 &&
1641 !CheckOffsetToTypeMap(item->annotations_off_, DexFile::kDexTypeAnnotationsDirectoryItem)) {
1642 return false;
1643 }
1644 if (item->class_data_off_ != 0 &&
1645 !CheckOffsetToTypeMap(item->class_data_off_, DexFile::kDexTypeClassDataItem)) {
1646 return false;
1647 }
1648 if (item->static_values_off_ != 0 &&
1649 !CheckOffsetToTypeMap(item->static_values_off_, DexFile::kDexTypeEncodedArrayItem)) {
1650 return false;
1651 }
1652
1653 if (item->superclass_idx_ != DexFile::kDexNoIndex16) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001654 LOAD_STRING_BY_TYPE(superclass_descriptor, item->superclass_idx_,
1655 "inter_class_def_item superclass_idx")
1656 if (UNLIKELY(!IsValidDescriptor(superclass_descriptor) || superclass_descriptor[0] != 'L')) {
1657 ErrorStringPrintf("Invalid superclass: '%s'", superclass_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001658 return false;
1659 }
1660 }
1661
1662 const DexFile::TypeList* interfaces = dex_file_->GetInterfacesList(*item);
1663 if (interfaces != NULL) {
1664 uint32_t size = interfaces->Size();
1665
1666 // Ensure that all interfaces refer to classes (not arrays or primitives).
1667 for (uint32_t i = 0; i < size; i++) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001668 LOAD_STRING_BY_TYPE(inf_descriptor, interfaces->GetTypeItem(i).type_idx_,
1669 "inter_class_def_item interface type_idx")
1670 if (UNLIKELY(!IsValidDescriptor(inf_descriptor) || inf_descriptor[0] != 'L')) {
1671 ErrorStringPrintf("Invalid interface: '%s'", inf_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001672 return false;
1673 }
1674 }
1675
1676 /*
1677 * Ensure that there are no duplicates. This is an O(N^2) test, but in
1678 * practice the number of interfaces implemented by any given class is low.
1679 */
1680 for (uint32_t i = 1; i < size; i++) {
1681 uint32_t idx1 = interfaces->GetTypeItem(i).type_idx_;
1682 for (uint32_t j =0; j < i; j++) {
1683 uint32_t idx2 = interfaces->GetTypeItem(j).type_idx_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001684 if (UNLIKELY(idx1 == idx2)) {
1685 ErrorStringPrintf("Duplicate interface: '%s'", dex_file_->StringByTypeIdx(idx1));
jeffhao10037c82012-01-23 15:06:23 -08001686 return false;
1687 }
1688 }
1689 }
1690 }
1691
1692 // Check that references in class_data_item are to the right class.
1693 if (item->class_data_off_ != 0) {
Ian Rogers30fab402012-01-23 15:43:46 -08001694 const byte* data = begin_ + item->class_data_off_;
Andreas Gampee09269c2014-06-06 18:45:35 -07001695 uint32_t data_definer = FindFirstClassDataDefiner(data);
1696 if (data_definer >= 0x10000U) {
1697 return false;
1698 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001699 if (UNLIKELY((data_definer != item->class_idx_) && (data_definer != DexFile::kDexNoIndex16))) {
1700 ErrorStringPrintf("Invalid class_data_item");
jeffhao10037c82012-01-23 15:06:23 -08001701 return false;
1702 }
1703 }
1704
1705 // Check that references in annotations_directory_item are to right class.
1706 if (item->annotations_off_ != 0) {
Ian Rogers30fab402012-01-23 15:43:46 -08001707 const byte* data = begin_ + item->annotations_off_;
Andreas Gampee09269c2014-06-06 18:45:35 -07001708 uint32_t annotations_definer = FindFirstAnnotationsDirectoryDefiner(data);
1709 if (annotations_definer >= 0x10000U) {
1710 return false;
1711 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001712 if (UNLIKELY((annotations_definer != item->class_idx_) &&
1713 (annotations_definer != DexFile::kDexNoIndex16))) {
1714 ErrorStringPrintf("Invalid annotations_directory_item");
jeffhao10037c82012-01-23 15:06:23 -08001715 return false;
1716 }
1717 }
1718
1719 ptr_ += sizeof(DexFile::ClassDef);
1720 return true;
1721}
1722
1723bool DexFileVerifier::CheckInterAnnotationSetRefList() {
1724 const DexFile::AnnotationSetRefList* list =
1725 reinterpret_cast<const DexFile::AnnotationSetRefList*>(ptr_);
1726 const DexFile::AnnotationSetRefItem* item = list->list_;
1727 uint32_t count = list->size_;
1728
1729 while (count--) {
1730 if (item->annotations_off_ != 0 &&
1731 !CheckOffsetToTypeMap(item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
1732 return false;
1733 }
1734 item++;
1735 }
1736
1737 ptr_ = reinterpret_cast<const byte*>(item);
1738 return true;
1739}
1740
1741bool DexFileVerifier::CheckInterAnnotationSetItem() {
1742 const DexFile::AnnotationSetItem* set = reinterpret_cast<const DexFile::AnnotationSetItem*>(ptr_);
1743 const uint32_t* offsets = set->entries_;
1744 uint32_t count = set->size_;
1745 uint32_t last_idx = 0;
1746
1747 for (uint32_t i = 0; i < count; i++) {
1748 if (*offsets != 0 && !CheckOffsetToTypeMap(*offsets, DexFile::kDexTypeAnnotationItem)) {
1749 return false;
1750 }
1751
1752 // Get the annotation from the offset and the type index for the annotation.
1753 const DexFile::AnnotationItem* annotation =
Ian Rogers30fab402012-01-23 15:43:46 -08001754 reinterpret_cast<const DexFile::AnnotationItem*>(begin_ + *offsets);
jeffhao10037c82012-01-23 15:06:23 -08001755 const uint8_t* data = annotation->annotation_;
1756 uint32_t idx = DecodeUnsignedLeb128(&data);
1757
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001758 if (UNLIKELY(last_idx >= idx && i != 0)) {
1759 ErrorStringPrintf("Out-of-order entry types: %x then %x", last_idx, idx);
jeffhao10037c82012-01-23 15:06:23 -08001760 return false;
1761 }
1762
1763 last_idx = idx;
1764 offsets++;
1765 }
1766
1767 ptr_ = reinterpret_cast<const byte*>(offsets);
1768 return true;
1769}
1770
1771bool DexFileVerifier::CheckInterClassDataItem() {
1772 ClassDataItemIterator it(*dex_file_, ptr_);
Andreas Gampee09269c2014-06-06 18:45:35 -07001773 uint32_t defining_class = FindFirstClassDataDefiner(ptr_);
1774 if (defining_class >= 0x10000U) {
1775 return false;
1776 }
jeffhao10037c82012-01-23 15:06:23 -08001777
1778 for (; it.HasNextStaticField() || it.HasNextInstanceField(); it.Next()) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001779 LOAD_FIELD(field, it.GetMemberIndex(), "inter_class_data_item field_id", false)
1780 if (UNLIKELY(field->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001781 ErrorStringPrintf("Mismatched defining class for class_data_item field");
jeffhao10037c82012-01-23 15:06:23 -08001782 return false;
1783 }
1784 }
1785 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
1786 uint32_t code_off = it.GetMethodCodeItemOffset();
1787 if (code_off != 0 && !CheckOffsetToTypeMap(code_off, DexFile::kDexTypeCodeItem)) {
1788 return false;
1789 }
Andreas Gampee09269c2014-06-06 18:45:35 -07001790 LOAD_METHOD(method, it.GetMemberIndex(), "inter_class_data_item method_id", false)
1791 if (UNLIKELY(method->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001792 ErrorStringPrintf("Mismatched defining class for class_data_item method");
jeffhao10037c82012-01-23 15:06:23 -08001793 return false;
1794 }
1795 }
1796
1797 ptr_ = it.EndDataPointer();
1798 return true;
1799}
1800
1801bool DexFileVerifier::CheckInterAnnotationsDirectoryItem() {
1802 const DexFile::AnnotationsDirectoryItem* item =
1803 reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr_);
Andreas Gampee09269c2014-06-06 18:45:35 -07001804 uint32_t defining_class = FindFirstAnnotationsDirectoryDefiner(ptr_);
1805 if (defining_class >= 0x10000U) {
1806 return false;
1807 }
jeffhao10037c82012-01-23 15:06:23 -08001808
1809 if (item->class_annotations_off_ != 0 &&
1810 !CheckOffsetToTypeMap(item->class_annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
1811 return false;
1812 }
1813
1814 // Field annotations follow immediately after the annotations directory.
1815 const DexFile::FieldAnnotationsItem* field_item =
1816 reinterpret_cast<const DexFile::FieldAnnotationsItem*>(item + 1);
1817 uint32_t field_count = item->fields_size_;
1818 for (uint32_t i = 0; i < field_count; i++) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001819 LOAD_FIELD(field, field_item->field_idx_, "inter_annotations_directory_item field_id", false)
1820 if (UNLIKELY(field->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001821 ErrorStringPrintf("Mismatched defining class for field_annotation");
jeffhao10037c82012-01-23 15:06:23 -08001822 return false;
1823 }
1824 if (!CheckOffsetToTypeMap(field_item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
1825 return false;
1826 }
1827 field_item++;
1828 }
1829
1830 // Method annotations follow immediately after field annotations.
1831 const DexFile::MethodAnnotationsItem* method_item =
1832 reinterpret_cast<const DexFile::MethodAnnotationsItem*>(field_item);
1833 uint32_t method_count = item->methods_size_;
1834 for (uint32_t i = 0; i < method_count; i++) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001835 LOAD_METHOD(method, method_item->method_idx_, "inter_annotations_directory_item method_id",
1836 false)
1837 if (UNLIKELY(method->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001838 ErrorStringPrintf("Mismatched defining class for method_annotation");
jeffhao10037c82012-01-23 15:06:23 -08001839 return false;
1840 }
1841 if (!CheckOffsetToTypeMap(method_item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
1842 return false;
1843 }
1844 method_item++;
1845 }
1846
1847 // Parameter annotations follow immediately after method annotations.
1848 const DexFile::ParameterAnnotationsItem* parameter_item =
1849 reinterpret_cast<const DexFile::ParameterAnnotationsItem*>(method_item);
1850 uint32_t parameter_count = item->parameters_size_;
1851 for (uint32_t i = 0; i < parameter_count; i++) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001852 LOAD_METHOD(parameter_method, parameter_item->method_idx_,
1853 "inter_annotations_directory_item parameter method_id", false)
1854 if (UNLIKELY(parameter_method->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001855 ErrorStringPrintf("Mismatched defining class for parameter_annotation");
jeffhao10037c82012-01-23 15:06:23 -08001856 return false;
1857 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001858 if (!CheckOffsetToTypeMap(parameter_item->annotations_off_,
1859 DexFile::kDexTypeAnnotationSetRefList)) {
jeffhao10037c82012-01-23 15:06:23 -08001860 return false;
1861 }
1862 parameter_item++;
1863 }
1864
1865 ptr_ = reinterpret_cast<const byte*>(parameter_item);
1866 return true;
1867}
1868
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001869bool DexFileVerifier::CheckInterSectionIterate(size_t offset, uint32_t count, uint16_t type) {
jeffhao10037c82012-01-23 15:06:23 -08001870 // Get the right alignment mask for the type of section.
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001871 size_t alignment_mask;
jeffhao10037c82012-01-23 15:06:23 -08001872 switch (type) {
1873 case DexFile::kDexTypeClassDataItem:
1874 alignment_mask = sizeof(uint8_t) - 1;
1875 break;
1876 default:
1877 alignment_mask = sizeof(uint32_t) - 1;
1878 break;
1879 }
1880
1881 // Iterate through the items in the section.
1882 previous_item_ = NULL;
1883 for (uint32_t i = 0; i < count; i++) {
1884 uint32_t new_offset = (offset + alignment_mask) & ~alignment_mask;
Ian Rogers30fab402012-01-23 15:43:46 -08001885 ptr_ = begin_ + new_offset;
jeffhao10037c82012-01-23 15:06:23 -08001886 const byte* prev_ptr = ptr_;
1887
1888 // Check depending on the section type.
1889 switch (type) {
1890 case DexFile::kDexTypeStringIdItem: {
1891 if (!CheckInterStringIdItem()) {
1892 return false;
1893 }
1894 break;
1895 }
1896 case DexFile::kDexTypeTypeIdItem: {
1897 if (!CheckInterTypeIdItem()) {
1898 return false;
1899 }
1900 break;
1901 }
1902 case DexFile::kDexTypeProtoIdItem: {
1903 if (!CheckInterProtoIdItem()) {
1904 return false;
1905 }
1906 break;
1907 }
1908 case DexFile::kDexTypeFieldIdItem: {
1909 if (!CheckInterFieldIdItem()) {
1910 return false;
1911 }
1912 break;
1913 }
1914 case DexFile::kDexTypeMethodIdItem: {
1915 if (!CheckInterMethodIdItem()) {
1916 return false;
1917 }
1918 break;
1919 }
1920 case DexFile::kDexTypeClassDefItem: {
1921 if (!CheckInterClassDefItem()) {
1922 return false;
1923 }
1924 break;
1925 }
1926 case DexFile::kDexTypeAnnotationSetRefList: {
1927 if (!CheckInterAnnotationSetRefList()) {
1928 return false;
1929 }
1930 break;
1931 }
1932 case DexFile::kDexTypeAnnotationSetItem: {
1933 if (!CheckInterAnnotationSetItem()) {
1934 return false;
1935 }
1936 break;
1937 }
1938 case DexFile::kDexTypeClassDataItem: {
1939 if (!CheckInterClassDataItem()) {
1940 return false;
1941 }
1942 break;
1943 }
1944 case DexFile::kDexTypeAnnotationsDirectoryItem: {
1945 if (!CheckInterAnnotationsDirectoryItem()) {
1946 return false;
1947 }
1948 break;
1949 }
1950 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001951 ErrorStringPrintf("Unknown map item type %x", type);
jeffhao10037c82012-01-23 15:06:23 -08001952 return false;
1953 }
1954
1955 previous_item_ = prev_ptr;
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001956 offset = ptr_ - begin_;
jeffhao10037c82012-01-23 15:06:23 -08001957 }
1958
1959 return true;
1960}
1961
1962bool DexFileVerifier::CheckInterSection() {
Ian Rogers30fab402012-01-23 15:43:46 -08001963 const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ + header_->map_off_);
jeffhao10037c82012-01-23 15:06:23 -08001964 const DexFile::MapItem* item = map->list_;
1965 uint32_t count = map->size_;
1966
1967 // Cross check the items listed in the map.
1968 while (count--) {
1969 uint32_t section_offset = item->offset_;
1970 uint32_t section_count = item->size_;
1971 uint16_t type = item->type_;
1972
1973 switch (type) {
1974 case DexFile::kDexTypeHeaderItem:
1975 case DexFile::kDexTypeMapList:
1976 case DexFile::kDexTypeTypeList:
1977 case DexFile::kDexTypeCodeItem:
1978 case DexFile::kDexTypeStringDataItem:
1979 case DexFile::kDexTypeDebugInfoItem:
1980 case DexFile::kDexTypeAnnotationItem:
1981 case DexFile::kDexTypeEncodedArrayItem:
1982 break;
1983 case DexFile::kDexTypeStringIdItem:
1984 case DexFile::kDexTypeTypeIdItem:
1985 case DexFile::kDexTypeProtoIdItem:
1986 case DexFile::kDexTypeFieldIdItem:
1987 case DexFile::kDexTypeMethodIdItem:
1988 case DexFile::kDexTypeClassDefItem:
1989 case DexFile::kDexTypeAnnotationSetRefList:
1990 case DexFile::kDexTypeAnnotationSetItem:
1991 case DexFile::kDexTypeClassDataItem:
1992 case DexFile::kDexTypeAnnotationsDirectoryItem: {
1993 if (!CheckInterSectionIterate(section_offset, section_count, type)) {
1994 return false;
1995 }
1996 break;
1997 }
1998 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001999 ErrorStringPrintf("Unknown map item type %x", type);
jeffhao10037c82012-01-23 15:06:23 -08002000 return false;
2001 }
2002
2003 item++;
2004 }
2005
2006 return true;
2007}
2008
2009bool DexFileVerifier::Verify() {
2010 // Check the header.
2011 if (!CheckHeader()) {
2012 return false;
2013 }
2014
2015 // Check the map section.
2016 if (!CheckMap()) {
2017 return false;
2018 }
2019
2020 // Check structure within remaining sections.
2021 if (!CheckIntraSection()) {
2022 return false;
2023 }
2024
2025 // Check references from one section to another.
2026 if (!CheckInterSection()) {
2027 return false;
2028 }
2029
2030 return true;
2031}
2032
Ian Rogers8d31bbd2013-10-13 10:44:14 -07002033void DexFileVerifier::ErrorStringPrintf(const char* fmt, ...) {
2034 va_list ap;
2035 va_start(ap, fmt);
2036 DCHECK(failure_reason_.empty()) << failure_reason_;
2037 failure_reason_ = StringPrintf("Failure to verify dex file '%s': ", location_);
2038 StringAppendV(&failure_reason_, fmt, ap);
2039 va_end(ap);
2040}
2041
jeffhao10037c82012-01-23 15:06:23 -08002042} // namespace art