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 | |
| 20 | #include <map> |
| 21 | |
| 22 | #include "dex_file.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | class DexFileVerifier { |
| 27 | public: |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame^] | 28 | static bool Verify(const DexFile* dex_file, const byte* begin, size_t size); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 29 | |
| 30 | private: |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame^] | 31 | DexFileVerifier(const DexFile* dex_file, const byte* begin, size_t size) |
| 32 | : dex_file_(dex_file), begin_(begin), size_(size), |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 33 | header_(&dex_file->GetHeader()), ptr_(NULL), previous_item_(NULL) { |
| 34 | } |
| 35 | |
| 36 | bool Verify(); |
| 37 | |
| 38 | bool CheckPointerRange(const void* start, const void* end, const char* label) const; |
| 39 | bool CheckListSize(const void* start, uint32_t count, uint32_t element_size, const char* label) const; |
| 40 | bool CheckIndex(uint32_t field, uint32_t limit, const char* label) const; |
| 41 | |
| 42 | bool CheckHeader() const; |
| 43 | bool CheckMap() const; |
| 44 | |
| 45 | uint32_t ReadUnsignedLittleEndian(uint32_t size); |
| 46 | bool CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item, |
| 47 | uint32_t* handler_offsets, uint32_t handlers_size); |
| 48 | bool CheckClassDataItemField(uint32_t idx, uint32_t access_flags, bool expect_static) const; |
| 49 | bool CheckClassDataItemMethod(uint32_t idx, uint32_t access_flags, uint32_t code_offset, |
| 50 | bool expect_direct) const; |
| 51 | bool CheckPadding(uint32_t offset, uint32_t aligned_offset); |
| 52 | bool CheckEncodedValue(); |
| 53 | bool CheckEncodedArray(); |
| 54 | bool CheckEncodedAnnotation(); |
| 55 | |
| 56 | bool CheckIntraClassDataItem(); |
| 57 | bool CheckIntraCodeItem(); |
| 58 | bool CheckIntraStringDataItem(); |
| 59 | bool CheckIntraDebugInfoItem(); |
| 60 | bool CheckIntraAnnotationItem(); |
| 61 | bool CheckIntraAnnotationsDirectoryItem(); |
| 62 | |
| 63 | bool CheckIntraSectionIterate(uint32_t offset, uint32_t count, uint16_t type); |
| 64 | bool CheckIntraIdSection(uint32_t offset, uint32_t count, uint16_t type); |
| 65 | bool CheckIntraDataSection(uint32_t offset, uint32_t count, uint16_t type); |
| 66 | bool CheckIntraSection(); |
| 67 | |
| 68 | bool CheckOffsetToTypeMap(uint32_t offset, uint16_t type); |
| 69 | uint16_t FindFirstClassDataDefiner(const byte* ptr) const; |
| 70 | uint16_t FindFirstAnnotationsDirectoryDefiner(const byte* ptr) const; |
| 71 | |
| 72 | bool CheckInterStringIdItem(); |
| 73 | bool CheckInterTypeIdItem(); |
| 74 | bool CheckInterProtoIdItem(); |
| 75 | bool CheckInterFieldIdItem(); |
| 76 | bool CheckInterMethodIdItem(); |
| 77 | bool CheckInterClassDefItem(); |
| 78 | bool CheckInterAnnotationSetRefList(); |
| 79 | bool CheckInterAnnotationSetItem(); |
| 80 | bool CheckInterClassDataItem(); |
| 81 | bool CheckInterAnnotationsDirectoryItem(); |
| 82 | |
| 83 | bool CheckInterSectionIterate(uint32_t offset, uint32_t count, uint16_t type); |
| 84 | bool CheckInterSection(); |
| 85 | |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame^] | 86 | const DexFile* dex_file_; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 87 | const byte* begin_; |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame^] | 88 | size_t size_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 89 | const DexFile::Header* header_; |
| 90 | |
| 91 | std::map<uint32_t, uint16_t> offset_to_type_map_; |
| 92 | const byte* ptr_; |
| 93 | const void* previous_item_; |
| 94 | }; |
| 95 | |
| 96 | } // namespace art |
| 97 | |
| 98 | #endif // ART_SRC_DEX_FILE_VERIFIER_H_ |