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