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 | |
Andreas Gampe | 0ba238d | 2014-07-29 01:22:07 -0700 | [diff] [blame] | 20 | #include <unordered_set> |
| 21 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 22 | #include "dex_file.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 23 | #include "safe_map.h" |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class DexFileVerifier { |
| 28 | public: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 29 | static bool Verify(const DexFile* dex_file, const uint8_t* begin, size_t size, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 30 | const char* location, std::string* error_msg); |
| 31 | |
| 32 | const std::string& FailureReason() const { |
| 33 | return failure_reason_; |
| 34 | } |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 35 | |
| 36 | private: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 37 | DexFileVerifier(const DexFile* dex_file, const uint8_t* begin, size_t size, const char* location) |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 38 | : dex_file_(dex_file), begin_(begin), size_(size), location_(location), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 39 | header_(&dex_file->GetHeader()), ptr_(nullptr), previous_item_(nullptr) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | bool Verify(); |
| 43 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 44 | bool CheckShortyDescriptorMatch(char shorty_char, const char* descriptor, bool is_return_type); |
Andreas Gampe | 50d1bc1 | 2014-07-17 21:49:24 -0700 | [diff] [blame] | 45 | bool CheckListSize(const void* start, size_t count, size_t element_size, const char* label); |
Andreas Gampe | d4ae41f | 2014-09-02 11:17:34 -0700 | [diff] [blame] | 46 | // Check a list. The head is assumed to be at *ptr, and elements to be of size element_size. If |
| 47 | // successful, the ptr will be moved forward the amount covered by the list. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 48 | bool CheckList(size_t element_size, const char* label, const uint8_t* *ptr); |
Andreas Gampe | d4ae41f | 2014-09-02 11:17:34 -0700 | [diff] [blame] | 49 | // Checks whether the offset is zero (when size is zero) or that the offset falls within the area |
| 50 | // claimed by the file. |
| 51 | bool CheckValidOffsetAndSize(uint32_t offset, uint32_t size, const char* label); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 52 | bool CheckIndex(uint32_t field, uint32_t limit, const char* label); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 53 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 54 | bool CheckHeader(); |
| 55 | bool CheckMap(); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 56 | |
| 57 | uint32_t ReadUnsignedLittleEndian(uint32_t size); |
| 58 | bool CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 59 | uint32_t* handler_offsets, uint32_t handlers_size); |
| 60 | bool CheckClassDataItemField(uint32_t idx, uint32_t access_flags, bool expect_static); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 61 | 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] | 62 | bool expect_direct); |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 63 | bool CheckPadding(size_t offset, uint32_t aligned_offset); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 64 | bool CheckEncodedValue(); |
| 65 | bool CheckEncodedArray(); |
| 66 | bool CheckEncodedAnnotation(); |
| 67 | |
| 68 | bool CheckIntraClassDataItem(); |
| 69 | bool CheckIntraCodeItem(); |
| 70 | bool CheckIntraStringDataItem(); |
| 71 | bool CheckIntraDebugInfoItem(); |
| 72 | bool CheckIntraAnnotationItem(); |
| 73 | bool CheckIntraAnnotationsDirectoryItem(); |
| 74 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 75 | bool CheckIntraSectionIterate(size_t offset, uint32_t count, uint16_t type); |
| 76 | bool CheckIntraIdSection(size_t offset, uint32_t count, uint16_t type); |
| 77 | bool CheckIntraDataSection(size_t offset, uint32_t count, uint16_t type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 78 | bool CheckIntraSection(); |
| 79 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 80 | bool CheckOffsetToTypeMap(size_t offset, uint16_t type); |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 81 | |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 82 | // Note: as sometimes kDexNoIndex16, being 0xFFFF, is a valid return value, we need an |
| 83 | // additional out parameter to signal any errors loading an index. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 84 | uint16_t FindFirstClassDataDefiner(const uint8_t* ptr, bool* success); |
| 85 | uint16_t FindFirstAnnotationsDirectoryDefiner(const uint8_t* ptr, bool* success); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 86 | |
| 87 | bool CheckInterStringIdItem(); |
| 88 | bool CheckInterTypeIdItem(); |
| 89 | bool CheckInterProtoIdItem(); |
| 90 | bool CheckInterFieldIdItem(); |
| 91 | bool CheckInterMethodIdItem(); |
| 92 | bool CheckInterClassDefItem(); |
| 93 | bool CheckInterAnnotationSetRefList(); |
| 94 | bool CheckInterAnnotationSetItem(); |
| 95 | bool CheckInterClassDataItem(); |
| 96 | bool CheckInterAnnotationsDirectoryItem(); |
| 97 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 98 | bool CheckInterSectionIterate(size_t offset, uint32_t count, uint16_t type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 99 | bool CheckInterSection(); |
| 100 | |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 101 | // Load a string by (type) index. Checks whether the index is in bounds, printing the error if |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 102 | // not. If there is an error, null is returned. |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 103 | const char* CheckLoadStringByIdx(uint32_t idx, const char* error_fmt); |
| 104 | const char* CheckLoadStringByTypeIdx(uint32_t type_idx, const char* error_fmt); |
| 105 | |
| 106 | // Load a field/method Id by index. Checks whether the index is in bounds, printing the error if |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 107 | // not. If there is an error, null is returned. |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 108 | const DexFile::FieldId* CheckLoadFieldId(uint32_t idx, const char* error_fmt); |
| 109 | const DexFile::MethodId* CheckLoadMethodId(uint32_t idx, const char* error_fmt); |
| 110 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 111 | void ErrorStringPrintf(const char* fmt, ...) |
| 112 | __attribute__((__format__(__printf__, 2, 3))) COLD_ATTR; |
| 113 | |
| 114 | const DexFile* const dex_file_; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 115 | const uint8_t* const begin_; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 116 | const size_t size_; |
| 117 | const char* const location_; |
| 118 | const DexFile::Header* const header_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 119 | |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 120 | AllocationTrackingSafeMap<uint32_t, uint16_t, kAllocatorTagDexFileVerifier> offset_to_type_map_; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 121 | const uint8_t* ptr_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 122 | const void* previous_item_; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 123 | |
| 124 | std::string failure_reason_; |
Andreas Gampe | 0ba238d | 2014-07-29 01:22:07 -0700 | [diff] [blame] | 125 | |
| 126 | // Set of type ids for which there are ClassDef elements in the dex file. |
| 127 | std::unordered_set<decltype(DexFile::ClassDef::class_idx_)> defined_classes_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | } // namespace art |
| 131 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 132 | #endif // ART_RUNTIME_DEX_FILE_VERIFIER_H_ |