Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_DEX_FILE_VERIFIER_H_ |
| 18 | #define ART_SRC_DEX_FILE_VERIFIER_H_ |
| 19 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 20 | #include "dex_file.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 21 | #include "safe_map.h" |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | class DexFileVerifier { |
| 26 | public: |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 27 | static bool Verify(const DexFile* dex_file, const byte* begin, size_t size); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 28 | |
| 29 | private: |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 30 | DexFileVerifier(const DexFile* dex_file, const byte* begin, size_t size) |
| 31 | : dex_file_(dex_file), begin_(begin), size_(size), |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 32 | header_(&dex_file->GetHeader()), ptr_(NULL), previous_item_(NULL) { |
| 33 | } |
| 34 | |
| 35 | bool Verify(); |
| 36 | |
| 37 | bool CheckPointerRange(const void* start, const void* end, const char* label) const; |
| 38 | bool CheckListSize(const void* start, uint32_t count, uint32_t element_size, const char* label) const; |
| 39 | bool CheckIndex(uint32_t field, uint32_t limit, const char* label) const; |
| 40 | |
| 41 | bool CheckHeader() const; |
| 42 | bool CheckMap() const; |
| 43 | |
| 44 | uint32_t ReadUnsignedLittleEndian(uint32_t size); |
| 45 | bool CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item, |
| 46 | uint32_t* handler_offsets, uint32_t handlers_size); |
| 47 | bool CheckClassDataItemField(uint32_t idx, uint32_t access_flags, bool expect_static) const; |
| 48 | bool CheckClassDataItemMethod(uint32_t idx, uint32_t access_flags, uint32_t code_offset, |
| 49 | bool expect_direct) const; |
| 50 | bool CheckPadding(uint32_t offset, uint32_t aligned_offset); |
| 51 | bool CheckEncodedValue(); |
| 52 | bool CheckEncodedArray(); |
| 53 | bool CheckEncodedAnnotation(); |
| 54 | |
| 55 | bool CheckIntraClassDataItem(); |
| 56 | bool CheckIntraCodeItem(); |
| 57 | bool CheckIntraStringDataItem(); |
| 58 | bool CheckIntraDebugInfoItem(); |
| 59 | bool CheckIntraAnnotationItem(); |
| 60 | bool CheckIntraAnnotationsDirectoryItem(); |
| 61 | |
| 62 | bool CheckIntraSectionIterate(uint32_t offset, uint32_t count, uint16_t type); |
| 63 | bool CheckIntraIdSection(uint32_t offset, uint32_t count, uint16_t type); |
| 64 | bool CheckIntraDataSection(uint32_t offset, uint32_t count, uint16_t type); |
| 65 | bool CheckIntraSection(); |
| 66 | |
| 67 | bool CheckOffsetToTypeMap(uint32_t offset, uint16_t type); |
| 68 | uint16_t FindFirstClassDataDefiner(const byte* ptr) const; |
| 69 | uint16_t FindFirstAnnotationsDirectoryDefiner(const byte* ptr) const; |
| 70 | |
| 71 | bool CheckInterStringIdItem(); |
| 72 | bool CheckInterTypeIdItem(); |
| 73 | bool CheckInterProtoIdItem(); |
| 74 | bool CheckInterFieldIdItem(); |
| 75 | bool CheckInterMethodIdItem(); |
| 76 | bool CheckInterClassDefItem(); |
| 77 | bool CheckInterAnnotationSetRefList(); |
| 78 | bool CheckInterAnnotationSetItem(); |
| 79 | bool CheckInterClassDataItem(); |
| 80 | bool CheckInterAnnotationsDirectoryItem(); |
| 81 | |
| 82 | bool CheckInterSectionIterate(uint32_t offset, uint32_t count, uint16_t type); |
| 83 | bool CheckInterSection(); |
| 84 | |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 85 | const DexFile* dex_file_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 86 | const byte* begin_; |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 87 | size_t size_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 88 | const DexFile::Header* header_; |
| 89 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 90 | SafeMap<uint32_t, uint16_t> offset_to_type_map_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 91 | const byte* ptr_; |
| 92 | const void* previous_item_; |
| 93 | }; |
| 94 | |
| 95 | } // namespace art |
| 96 | |
| 97 | #endif // ART_SRC_DEX_FILE_VERIFIER_H_ |