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: |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 29 | static bool Verify(const DexFile* dex_file, |
| 30 | const uint8_t* begin, |
| 31 | size_t size, |
| 32 | const char* location, |
| 33 | bool verify_checksum, |
| 34 | std::string* error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 35 | |
| 36 | const std::string& FailureReason() const { |
| 37 | return failure_reason_; |
| 38 | } |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 39 | |
| 40 | private: |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 41 | DexFileVerifier(const DexFile* dex_file, |
| 42 | const uint8_t* begin, |
| 43 | size_t size, |
| 44 | const char* location, |
| 45 | bool verify_checksum) |
| 46 | : dex_file_(dex_file), |
| 47 | begin_(begin), |
| 48 | size_(size), |
| 49 | location_(location), |
| 50 | verify_checksum_(verify_checksum), |
| 51 | header_(&dex_file->GetHeader()), |
| 52 | ptr_(nullptr), |
| 53 | previous_item_(nullptr) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | bool Verify(); |
| 57 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 58 | bool CheckShortyDescriptorMatch(char shorty_char, const char* descriptor, bool is_return_type); |
Andreas Gampe | 50d1bc1 | 2014-07-17 21:49:24 -0700 | [diff] [blame] | 59 | 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] | 60 | // Check a list. The head is assumed to be at *ptr, and elements to be of size element_size. If |
| 61 | // 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] | 62 | 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] | 63 | // Checks whether the offset is zero (when size is zero) or that the offset falls within the area |
| 64 | // claimed by the file. |
Andreas Gampe | b512c0e | 2016-02-19 19:45:34 -0800 | [diff] [blame] | 65 | bool CheckValidOffsetAndSize(uint32_t offset, uint32_t size, size_t alignment, const char* label); |
Vladimir Marko | 0ca8add | 2016-05-03 17:17:50 +0100 | [diff] [blame] | 66 | // Checks whether the size is less than the limit. |
| 67 | bool CheckSizeLimit(uint32_t size, uint32_t limit, const char* label); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 68 | bool CheckIndex(uint32_t field, uint32_t limit, const char* label); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 69 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 70 | bool CheckHeader(); |
| 71 | bool CheckMap(); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 72 | |
| 73 | uint32_t ReadUnsignedLittleEndian(uint32_t size); |
| 74 | bool CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 75 | uint32_t* handler_offsets, uint32_t handlers_size); |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 76 | bool CheckClassDataItemField(uint32_t idx, |
| 77 | uint32_t access_flags, |
| 78 | uint32_t class_access_flags, |
Andreas Gampe | 1a97357 | 2015-09-10 20:09:11 -0700 | [diff] [blame] | 79 | uint16_t class_type_index, |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 80 | bool expect_static); |
| 81 | bool CheckClassDataItemMethod(uint32_t idx, |
| 82 | uint32_t access_flags, |
| 83 | uint32_t class_access_flags, |
Andreas Gampe | 1a97357 | 2015-09-10 20:09:11 -0700 | [diff] [blame] | 84 | uint16_t class_type_index, |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 85 | uint32_t code_offset, |
| 86 | std::unordered_set<uint32_t>* direct_method_indexes, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 87 | bool expect_direct); |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 88 | bool CheckOrderAndGetClassFlags(bool is_field, |
| 89 | const char* type_descr, |
| 90 | uint32_t curr_index, |
| 91 | uint32_t prev_index, |
| 92 | bool* have_class, |
| 93 | uint16_t* class_type_index, |
| 94 | uint32_t* class_access_flags); |
| 95 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 96 | bool CheckPadding(size_t offset, uint32_t aligned_offset); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 97 | bool CheckEncodedValue(); |
| 98 | bool CheckEncodedArray(); |
| 99 | bool CheckEncodedAnnotation(); |
| 100 | |
| 101 | bool CheckIntraClassDataItem(); |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 102 | // Check all fields of the given type from the given iterator. Load the class data from the first |
| 103 | // field, if necessary (and return it), or use the given values. |
| 104 | template <bool kStatic> |
| 105 | bool CheckIntraClassDataItemFields(ClassDataItemIterator* it, |
| 106 | bool* have_class, |
| 107 | uint16_t* class_type_index, |
| 108 | uint32_t* class_access_flags); |
| 109 | // Check all methods of the given type from the given iterator. Load the class data from the first |
| 110 | // method, if necessary (and return it), or use the given values. |
| 111 | template <bool kDirect> |
| 112 | bool CheckIntraClassDataItemMethods(ClassDataItemIterator* it, |
| 113 | std::unordered_set<uint32_t>* direct_method_indexes, |
| 114 | bool* have_class, |
| 115 | uint16_t* class_type_index, |
| 116 | uint32_t* class_access_flags); |
| 117 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 118 | bool CheckIntraCodeItem(); |
| 119 | bool CheckIntraStringDataItem(); |
| 120 | bool CheckIntraDebugInfoItem(); |
| 121 | bool CheckIntraAnnotationItem(); |
| 122 | bool CheckIntraAnnotationsDirectoryItem(); |
| 123 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 124 | bool CheckIntraSectionIterate(size_t offset, uint32_t count, uint16_t type); |
| 125 | bool CheckIntraIdSection(size_t offset, uint32_t count, uint16_t type); |
| 126 | bool CheckIntraDataSection(size_t offset, uint32_t count, uint16_t type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 127 | bool CheckIntraSection(); |
| 128 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 129 | bool CheckOffsetToTypeMap(size_t offset, uint16_t type); |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 130 | |
Andreas Gampe | 5e31dda | 2014-06-13 11:35:12 -0700 | [diff] [blame] | 131 | // Note: as sometimes kDexNoIndex16, being 0xFFFF, is a valid return value, we need an |
| 132 | // additional out parameter to signal any errors loading an index. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 133 | uint16_t FindFirstClassDataDefiner(const uint8_t* ptr, bool* success); |
| 134 | uint16_t FindFirstAnnotationsDirectoryDefiner(const uint8_t* ptr, bool* success); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 135 | |
| 136 | bool CheckInterStringIdItem(); |
| 137 | bool CheckInterTypeIdItem(); |
| 138 | bool CheckInterProtoIdItem(); |
| 139 | bool CheckInterFieldIdItem(); |
| 140 | bool CheckInterMethodIdItem(); |
| 141 | bool CheckInterClassDefItem(); |
| 142 | bool CheckInterAnnotationSetRefList(); |
| 143 | bool CheckInterAnnotationSetItem(); |
| 144 | bool CheckInterClassDataItem(); |
| 145 | bool CheckInterAnnotationsDirectoryItem(); |
| 146 | |
Ian Rogers | 8a6bbfc | 2014-01-23 13:29:07 -0800 | [diff] [blame] | 147 | bool CheckInterSectionIterate(size_t offset, uint32_t count, uint16_t type); |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 148 | bool CheckInterSection(); |
| 149 | |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 150 | // 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] | 151 | // not. If there is an error, null is returned. |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 152 | const char* CheckLoadStringByIdx(uint32_t idx, const char* error_fmt); |
| 153 | const char* CheckLoadStringByTypeIdx(uint32_t type_idx, const char* error_fmt); |
| 154 | |
| 155 | // 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] | 156 | // not. If there is an error, null is returned. |
Andreas Gampe | e09269c | 2014-06-06 18:45:35 -0700 | [diff] [blame] | 157 | const DexFile::FieldId* CheckLoadFieldId(uint32_t idx, const char* error_fmt); |
| 158 | const DexFile::MethodId* CheckLoadMethodId(uint32_t idx, const char* error_fmt); |
| 159 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 160 | void ErrorStringPrintf(const char* fmt, ...) |
| 161 | __attribute__((__format__(__printf__, 2, 3))) COLD_ATTR; |
| 162 | |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 163 | // Retrieve class index and class access flag from the given member. index is the member index, |
| 164 | // which is taken as either a field or a method index (as designated by is_field). The result, |
| 165 | // if the member and declaring class could be found, is stored in class_type_index and |
| 166 | // class_access_flags. |
| 167 | // This is an expensive lookup, as we have to find the class-def by type index, which is a |
| 168 | // linear search. The output values should thus be cached by the caller. |
| 169 | bool FindClassFlags(uint32_t index, |
| 170 | bool is_field, |
| 171 | uint16_t* class_type_index, |
| 172 | uint32_t* class_access_flags); |
| 173 | |
| 174 | // Check validity of the given access flags, interpreted for a field in the context of a class |
| 175 | // with the given second access flags. |
Andreas Gampe | c9f0ba1 | 2016-02-09 09:21:04 -0800 | [diff] [blame] | 176 | bool CheckFieldAccessFlags(uint32_t idx, |
| 177 | uint32_t field_access_flags, |
| 178 | uint32_t class_access_flags, |
| 179 | std::string* error_msg); |
Andreas Gampe | e6215c0 | 2015-08-31 18:54:38 -0700 | [diff] [blame] | 180 | // Check validity of the given method and access flags, in the context of a class with the given |
| 181 | // second access flags. |
| 182 | bool CheckMethodAccessFlags(uint32_t method_index, |
| 183 | uint32_t method_access_flags, |
| 184 | uint32_t class_access_flags, |
| 185 | bool has_code, |
| 186 | bool expect_direct, |
| 187 | std::string* error_msg); |
| 188 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 189 | const DexFile* const dex_file_; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 190 | const uint8_t* const begin_; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 191 | const size_t size_; |
| 192 | const char* const location_; |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 193 | const bool verify_checksum_; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 194 | const DexFile::Header* const header_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 195 | |
Mathieu Chartier | 0f8e072 | 2015-10-26 14:52:42 -0700 | [diff] [blame] | 196 | struct OffsetTypeMapEmptyFn { |
| 197 | // Make a hash map slot empty by making the offset 0. Offset 0 is a valid dex file offset that |
| 198 | // is in the offset of the dex file header. However, we only store data section items in the |
| 199 | // map, and these are after the header. |
| 200 | void MakeEmpty(std::pair<uint32_t, uint16_t>& pair) const { |
| 201 | pair.first = 0u; |
| 202 | } |
| 203 | // Check if a hash map slot is empty. |
| 204 | bool IsEmpty(const std::pair<uint32_t, uint16_t>& pair) const { |
| 205 | return pair.first == 0; |
| 206 | } |
| 207 | }; |
| 208 | struct OffsetTypeMapHashCompareFn { |
| 209 | // Hash function for offset. |
| 210 | size_t operator()(const uint32_t key) const { |
| 211 | return key; |
| 212 | } |
| 213 | // std::equal function for offset. |
| 214 | bool operator()(const uint32_t a, const uint32_t b) const { |
| 215 | return a == b; |
| 216 | } |
| 217 | }; |
| 218 | // Map from offset to dex file type, HashMap for performance reasons. |
| 219 | AllocationTrackingHashMap<uint32_t, |
| 220 | uint16_t, |
| 221 | OffsetTypeMapEmptyFn, |
| 222 | kAllocatorTagDexFileVerifier, |
| 223 | OffsetTypeMapHashCompareFn, |
| 224 | OffsetTypeMapHashCompareFn> offset_to_type_map_; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 225 | const uint8_t* ptr_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 226 | const void* previous_item_; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 227 | |
| 228 | std::string failure_reason_; |
Andreas Gampe | 0ba238d | 2014-07-29 01:22:07 -0700 | [diff] [blame] | 229 | |
| 230 | // Set of type ids for which there are ClassDef elements in the dex file. |
| 231 | std::unordered_set<decltype(DexFile::ClassDef::class_idx_)> defined_classes_; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | } // namespace art |
| 235 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 236 | #endif // ART_RUNTIME_DEX_FILE_VERIFIER_H_ |