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