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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_DEX_FILE_VERIFIER_H_ |
| 18 | #define ART_RUNTIME_DEX_FILE_VERIFIER_H_ |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 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: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 27 | static bool Verify(const DexFile* dex_file, const byte* begin, size_t size, |
| 28 | const char* location, std::string* error_msg); |
| 29 | |
| 30 | const std::string& FailureReason() const { |
| 31 | return failure_reason_; |
| 32 | } |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 33 | |
| 34 | private: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 35 | DexFileVerifier(const DexFile* dex_file, const byte* begin, size_t size, const char* location) |
| 36 | : dex_file_(dex_file), begin_(begin), size_(size), location_(location), |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 37 | header_(&dex_file->GetHeader()), ptr_(NULL), previous_item_(NULL) { |
| 38 | } |
| 39 | |
| 40 | bool Verify(); |
| 41 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 42 | bool CheckShortyDescriptorMatch(char shorty_char, const char* descriptor, bool is_return_type); |
| 43 | bool CheckPointerRange(const void* start, const void* end, const char* label); |
| 44 | bool CheckListSize(const void* start, uint32_t count, uint32_t element_size, const char* label); |
| 45 | bool CheckIndex(uint32_t field, uint32_t limit, const char* label); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 46 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 47 | bool CheckHeader(); |
| 48 | bool CheckMap(); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 49 | |
| 50 | uint32_t ReadUnsignedLittleEndian(uint32_t size); |
| 51 | bool CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 52 | uint32_t* handler_offsets, uint32_t handlers_size); |
| 53 | bool CheckClassDataItemField(uint32_t idx, uint32_t access_flags, bool expect_static); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 54 | bool CheckClassDataItemMethod(uint32_t idx, uint32_t access_flags, uint32_t code_offset, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 55 | bool expect_direct); |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 56 | bool CheckPadding(size_t offset, uint32_t aligned_offset); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 57 | bool CheckEncodedValue(); |
| 58 | bool CheckEncodedArray(); |
| 59 | bool CheckEncodedAnnotation(); |
| 60 | |
| 61 | bool CheckIntraClassDataItem(); |
| 62 | bool CheckIntraCodeItem(); |
| 63 | bool CheckIntraStringDataItem(); |
| 64 | bool CheckIntraDebugInfoItem(); |
| 65 | bool CheckIntraAnnotationItem(); |
| 66 | bool CheckIntraAnnotationsDirectoryItem(); |
| 67 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 68 | bool CheckIntraSectionIterate(size_t offset, uint32_t count, uint16_t type); |
| 69 | bool CheckIntraIdSection(size_t offset, uint32_t count, uint16_t type); |
| 70 | bool CheckIntraDataSection(size_t offset, uint32_t count, uint16_t type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 71 | bool CheckIntraSection(); |
| 72 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 73 | bool CheckOffsetToTypeMap(size_t offset, uint16_t type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 74 | uint16_t FindFirstClassDataDefiner(const byte* ptr) const; |
| 75 | uint16_t FindFirstAnnotationsDirectoryDefiner(const byte* ptr) const; |
| 76 | |
| 77 | bool CheckInterStringIdItem(); |
| 78 | bool CheckInterTypeIdItem(); |
| 79 | bool CheckInterProtoIdItem(); |
| 80 | bool CheckInterFieldIdItem(); |
| 81 | bool CheckInterMethodIdItem(); |
| 82 | bool CheckInterClassDefItem(); |
| 83 | bool CheckInterAnnotationSetRefList(); |
| 84 | bool CheckInterAnnotationSetItem(); |
| 85 | bool CheckInterClassDataItem(); |
| 86 | bool CheckInterAnnotationsDirectoryItem(); |
| 87 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 88 | bool CheckInterSectionIterate(size_t offset, uint32_t count, uint16_t type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 89 | bool CheckInterSection(); |
| 90 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 91 | void ErrorStringPrintf(const char* fmt, ...) |
| 92 | __attribute__((__format__(__printf__, 2, 3))) COLD_ATTR; |
| 93 | |
| 94 | const DexFile* const dex_file_; |
| 95 | const byte* const begin_; |
| 96 | const size_t size_; |
| 97 | const char* const location_; |
| 98 | const DexFile::Header* const header_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 99 | |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 100 | SafeMap<uint32_t, uint16_t> offset_to_type_map_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 101 | const byte* ptr_; |
| 102 | const void* previous_item_; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 103 | |
| 104 | std::string failure_reason_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace art |
| 108 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 109 | #endif // ART_RUNTIME_DEX_FILE_VERIFIER_H_ |