blob: ddfeea2305b4de0320b152cc678aa9e7f90fa471 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
jeffhao10037c82012-01-23 15:06:23 -080016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_DEX_FILE_VERIFIER_H_
18#define ART_RUNTIME_DEX_FILE_VERIFIER_H_
jeffhao10037c82012-01-23 15:06:23 -080019
Andreas Gampe0ba238d2014-07-29 01:22:07 -070020#include <unordered_set>
21
jeffhao10037c82012-01-23 15:06:23 -080022#include "dex_file.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070023#include "safe_map.h"
jeffhao10037c82012-01-23 15:06:23 -080024
25namespace art {
26
27class DexFileVerifier {
28 public:
Ian Rogers13735952014-10-08 12:43:28 -070029 static bool Verify(const DexFile* dex_file, const uint8_t* begin, size_t size,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070030 const char* location, std::string* error_msg);
31
32 const std::string& FailureReason() const {
33 return failure_reason_;
34 }
jeffhao10037c82012-01-23 15:06:23 -080035
36 private:
Ian Rogers13735952014-10-08 12:43:28 -070037 DexFileVerifier(const DexFile* dex_file, const uint8_t* begin, size_t size, const char* location)
Ian Rogers8d31bbd2013-10-13 10:44:14 -070038 : dex_file_(dex_file), begin_(begin), size_(size), location_(location),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070039 header_(&dex_file->GetHeader()), ptr_(nullptr), previous_item_(nullptr) {
jeffhao10037c82012-01-23 15:06:23 -080040 }
41
42 bool Verify();
43
Ian Rogers8d31bbd2013-10-13 10:44:14 -070044 bool CheckShortyDescriptorMatch(char shorty_char, const char* descriptor, bool is_return_type);
Andreas Gampe50d1bc12014-07-17 21:49:24 -070045 bool CheckListSize(const void* start, size_t count, size_t element_size, const char* label);
Andreas Gamped4ae41f2014-09-02 11:17:34 -070046 // 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 Rogers13735952014-10-08 12:43:28 -070048 bool CheckList(size_t element_size, const char* label, const uint8_t* *ptr);
Andreas Gamped4ae41f2014-09-02 11:17:34 -070049 // 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 Rogers8d31bbd2013-10-13 10:44:14 -070052 bool CheckIndex(uint32_t field, uint32_t limit, const char* label);
jeffhao10037c82012-01-23 15:06:23 -080053
Ian Rogers8d31bbd2013-10-13 10:44:14 -070054 bool CheckHeader();
55 bool CheckMap();
jeffhao10037c82012-01-23 15:06:23 -080056
57 uint32_t ReadUnsignedLittleEndian(uint32_t size);
58 bool CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070059 uint32_t* handler_offsets, uint32_t handlers_size);
Andreas Gampee6215c02015-08-31 18:54:38 -070060 bool CheckClassDataItemField(uint32_t idx,
61 uint32_t access_flags,
62 uint32_t class_access_flags,
Andreas Gampe1a973572015-09-10 20:09:11 -070063 uint16_t class_type_index,
Andreas Gampee6215c02015-08-31 18:54:38 -070064 bool expect_static);
65 bool CheckClassDataItemMethod(uint32_t idx,
66 uint32_t access_flags,
67 uint32_t class_access_flags,
Andreas Gampe1a973572015-09-10 20:09:11 -070068 uint16_t class_type_index,
Andreas Gampee6215c02015-08-31 18:54:38 -070069 uint32_t code_offset,
70 std::unordered_set<uint32_t>* direct_method_indexes,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070071 bool expect_direct);
Andreas Gampee6215c02015-08-31 18:54:38 -070072 bool CheckOrderAndGetClassFlags(bool is_field,
73 const char* type_descr,
74 uint32_t curr_index,
75 uint32_t prev_index,
76 bool* have_class,
77 uint16_t* class_type_index,
78 uint32_t* class_access_flags);
79
Ian Rogers8a6bbfc2014-01-23 13:29:07 -080080 bool CheckPadding(size_t offset, uint32_t aligned_offset);
jeffhao10037c82012-01-23 15:06:23 -080081 bool CheckEncodedValue();
82 bool CheckEncodedArray();
83 bool CheckEncodedAnnotation();
84
85 bool CheckIntraClassDataItem();
Andreas Gampee6215c02015-08-31 18:54:38 -070086 // Check all fields of the given type from the given iterator. Load the class data from the first
87 // field, if necessary (and return it), or use the given values.
88 template <bool kStatic>
89 bool CheckIntraClassDataItemFields(ClassDataItemIterator* it,
90 bool* have_class,
91 uint16_t* class_type_index,
92 uint32_t* class_access_flags);
93 // Check all methods of the given type from the given iterator. Load the class data from the first
94 // method, if necessary (and return it), or use the given values.
95 template <bool kDirect>
96 bool CheckIntraClassDataItemMethods(ClassDataItemIterator* it,
97 std::unordered_set<uint32_t>* direct_method_indexes,
98 bool* have_class,
99 uint16_t* class_type_index,
100 uint32_t* class_access_flags);
101
jeffhao10037c82012-01-23 15:06:23 -0800102 bool CheckIntraCodeItem();
103 bool CheckIntraStringDataItem();
104 bool CheckIntraDebugInfoItem();
105 bool CheckIntraAnnotationItem();
106 bool CheckIntraAnnotationsDirectoryItem();
107
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800108 bool CheckIntraSectionIterate(size_t offset, uint32_t count, uint16_t type);
109 bool CheckIntraIdSection(size_t offset, uint32_t count, uint16_t type);
110 bool CheckIntraDataSection(size_t offset, uint32_t count, uint16_t type);
jeffhao10037c82012-01-23 15:06:23 -0800111 bool CheckIntraSection();
112
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800113 bool CheckOffsetToTypeMap(size_t offset, uint16_t type);
Andreas Gampee09269c2014-06-06 18:45:35 -0700114
Andreas Gampe5e31dda2014-06-13 11:35:12 -0700115 // Note: as sometimes kDexNoIndex16, being 0xFFFF, is a valid return value, we need an
116 // additional out parameter to signal any errors loading an index.
Ian Rogers13735952014-10-08 12:43:28 -0700117 uint16_t FindFirstClassDataDefiner(const uint8_t* ptr, bool* success);
118 uint16_t FindFirstAnnotationsDirectoryDefiner(const uint8_t* ptr, bool* success);
jeffhao10037c82012-01-23 15:06:23 -0800119
120 bool CheckInterStringIdItem();
121 bool CheckInterTypeIdItem();
122 bool CheckInterProtoIdItem();
123 bool CheckInterFieldIdItem();
124 bool CheckInterMethodIdItem();
125 bool CheckInterClassDefItem();
126 bool CheckInterAnnotationSetRefList();
127 bool CheckInterAnnotationSetItem();
128 bool CheckInterClassDataItem();
129 bool CheckInterAnnotationsDirectoryItem();
130
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800131 bool CheckInterSectionIterate(size_t offset, uint32_t count, uint16_t type);
jeffhao10037c82012-01-23 15:06:23 -0800132 bool CheckInterSection();
133
Andreas Gampee09269c2014-06-06 18:45:35 -0700134 // Load a string by (type) index. Checks whether the index is in bounds, printing the error if
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700135 // not. If there is an error, null is returned.
Andreas Gampee09269c2014-06-06 18:45:35 -0700136 const char* CheckLoadStringByIdx(uint32_t idx, const char* error_fmt);
137 const char* CheckLoadStringByTypeIdx(uint32_t type_idx, const char* error_fmt);
138
139 // Load a field/method Id by index. Checks whether the index is in bounds, printing the error if
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700140 // not. If there is an error, null is returned.
Andreas Gampee09269c2014-06-06 18:45:35 -0700141 const DexFile::FieldId* CheckLoadFieldId(uint32_t idx, const char* error_fmt);
142 const DexFile::MethodId* CheckLoadMethodId(uint32_t idx, const char* error_fmt);
143
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700144 void ErrorStringPrintf(const char* fmt, ...)
145 __attribute__((__format__(__printf__, 2, 3))) COLD_ATTR;
146
Andreas Gampee6215c02015-08-31 18:54:38 -0700147 // Retrieve class index and class access flag from the given member. index is the member index,
148 // which is taken as either a field or a method index (as designated by is_field). The result,
149 // if the member and declaring class could be found, is stored in class_type_index and
150 // class_access_flags.
151 // This is an expensive lookup, as we have to find the class-def by type index, which is a
152 // linear search. The output values should thus be cached by the caller.
153 bool FindClassFlags(uint32_t index,
154 bool is_field,
155 uint16_t* class_type_index,
156 uint32_t* class_access_flags);
157
158 // Check validity of the given access flags, interpreted for a field in the context of a class
159 // with the given second access flags.
Andreas Gampec9f0ba12016-02-09 09:21:04 -0800160 bool CheckFieldAccessFlags(uint32_t idx,
161 uint32_t field_access_flags,
162 uint32_t class_access_flags,
163 std::string* error_msg);
Andreas Gampee6215c02015-08-31 18:54:38 -0700164 // Check validity of the given method and access flags, in the context of a class with the given
165 // second access flags.
166 bool CheckMethodAccessFlags(uint32_t method_index,
167 uint32_t method_access_flags,
168 uint32_t class_access_flags,
169 bool has_code,
170 bool expect_direct,
171 std::string* error_msg);
172
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700173 const DexFile* const dex_file_;
Ian Rogers13735952014-10-08 12:43:28 -0700174 const uint8_t* const begin_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700175 const size_t size_;
176 const char* const location_;
177 const DexFile::Header* const header_;
jeffhao10037c82012-01-23 15:06:23 -0800178
Mathieu Chartier0f8e0722015-10-26 14:52:42 -0700179 struct OffsetTypeMapEmptyFn {
180 // Make a hash map slot empty by making the offset 0. Offset 0 is a valid dex file offset that
181 // is in the offset of the dex file header. However, we only store data section items in the
182 // map, and these are after the header.
183 void MakeEmpty(std::pair<uint32_t, uint16_t>& pair) const {
184 pair.first = 0u;
185 }
186 // Check if a hash map slot is empty.
187 bool IsEmpty(const std::pair<uint32_t, uint16_t>& pair) const {
188 return pair.first == 0;
189 }
190 };
191 struct OffsetTypeMapHashCompareFn {
192 // Hash function for offset.
193 size_t operator()(const uint32_t key) const {
194 return key;
195 }
196 // std::equal function for offset.
197 bool operator()(const uint32_t a, const uint32_t b) const {
198 return a == b;
199 }
200 };
201 // Map from offset to dex file type, HashMap for performance reasons.
202 AllocationTrackingHashMap<uint32_t,
203 uint16_t,
204 OffsetTypeMapEmptyFn,
205 kAllocatorTagDexFileVerifier,
206 OffsetTypeMapHashCompareFn,
207 OffsetTypeMapHashCompareFn> offset_to_type_map_;
Ian Rogers13735952014-10-08 12:43:28 -0700208 const uint8_t* ptr_;
jeffhao10037c82012-01-23 15:06:23 -0800209 const void* previous_item_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700210
211 std::string failure_reason_;
Andreas Gampe0ba238d2014-07-29 01:22:07 -0700212
213 // Set of type ids for which there are ClassDef elements in the dex file.
214 std::unordered_set<decltype(DexFile::ClassDef::class_idx_)> defined_classes_;
jeffhao10037c82012-01-23 15:06:23 -0800215};
216
217} // namespace art
218
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700219#endif // ART_RUNTIME_DEX_FILE_VERIFIER_H_