blob: 5fa58f754f83ca327b04f8ba9da5c5c4f0a5e234 [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
17#include "dex_file_verifier.h"
18
Narayan Kamath92572be2013-11-28 14:06:24 +000019#include <zlib.h>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Narayan Kamath92572be2013-11-28 14:06:24 +000021
Elliott Hughese222ee02012-12-13 14:41:43 -080022#include "base/stringprintf.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070023#include "dex_file-inl.h"
jeffhao10037c82012-01-23 15:06:23 -080024#include "leb128.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070025#include "safe_map.h"
Ian Rogersa6724902013-09-23 09:23:37 -070026#include "utf-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "utils.h"
jeffhao10037c82012-01-23 15:06:23 -080028
29namespace art {
30
31static uint32_t MapTypeToBitMask(uint32_t map_type) {
32 switch (map_type) {
33 case DexFile::kDexTypeHeaderItem: return 1 << 0;
34 case DexFile::kDexTypeStringIdItem: return 1 << 1;
35 case DexFile::kDexTypeTypeIdItem: return 1 << 2;
36 case DexFile::kDexTypeProtoIdItem: return 1 << 3;
37 case DexFile::kDexTypeFieldIdItem: return 1 << 4;
38 case DexFile::kDexTypeMethodIdItem: return 1 << 5;
39 case DexFile::kDexTypeClassDefItem: return 1 << 6;
40 case DexFile::kDexTypeMapList: return 1 << 7;
41 case DexFile::kDexTypeTypeList: return 1 << 8;
42 case DexFile::kDexTypeAnnotationSetRefList: return 1 << 9;
43 case DexFile::kDexTypeAnnotationSetItem: return 1 << 10;
44 case DexFile::kDexTypeClassDataItem: return 1 << 11;
45 case DexFile::kDexTypeCodeItem: return 1 << 12;
46 case DexFile::kDexTypeStringDataItem: return 1 << 13;
47 case DexFile::kDexTypeDebugInfoItem: return 1 << 14;
48 case DexFile::kDexTypeAnnotationItem: return 1 << 15;
49 case DexFile::kDexTypeEncodedArrayItem: return 1 << 16;
50 case DexFile::kDexTypeAnnotationsDirectoryItem: return 1 << 17;
51 }
52 return 0;
53}
54
55static bool IsDataSectionType(uint32_t map_type) {
56 switch (map_type) {
57 case DexFile::kDexTypeHeaderItem:
58 case DexFile::kDexTypeStringIdItem:
59 case DexFile::kDexTypeTypeIdItem:
60 case DexFile::kDexTypeProtoIdItem:
61 case DexFile::kDexTypeFieldIdItem:
62 case DexFile::kDexTypeMethodIdItem:
63 case DexFile::kDexTypeClassDefItem:
64 return false;
65 }
66 return true;
67}
68
Andreas Gampee09269c2014-06-06 18:45:35 -070069const char* DexFileVerifier::CheckLoadStringByIdx(uint32_t idx, const char* error_string) {
Andreas Gampedf10b322014-06-11 21:46:05 -070070 if (UNLIKELY(!CheckIndex(idx, dex_file_->NumStringIds(), error_string))) {
Andreas Gampee09269c2014-06-06 18:45:35 -070071 return nullptr;
72 }
73 return dex_file_->StringDataByIdx(idx);
74}
75
76const char* DexFileVerifier::CheckLoadStringByTypeIdx(uint32_t type_idx, const char* error_string) {
Andreas Gampedf10b322014-06-11 21:46:05 -070077 if (UNLIKELY(!CheckIndex(type_idx, dex_file_->NumTypeIds(), error_string))) {
Andreas Gampee09269c2014-06-06 18:45:35 -070078 return nullptr;
79 }
80 const DexFile::TypeId& type_id = dex_file_->GetTypeId(type_idx);
81 uint32_t idx = type_id.descriptor_idx_;
82 return CheckLoadStringByIdx(idx, error_string);
83}
84
85const DexFile::FieldId* DexFileVerifier::CheckLoadFieldId(uint32_t idx, const char* error_string) {
Andreas Gampedf10b322014-06-11 21:46:05 -070086 if (UNLIKELY(!CheckIndex(idx, dex_file_->NumFieldIds(), error_string))) {
Andreas Gampee09269c2014-06-06 18:45:35 -070087 return nullptr;
88 }
89 return &dex_file_->GetFieldId(idx);
90}
91
92const DexFile::MethodId* DexFileVerifier::CheckLoadMethodId(uint32_t idx, const char* err_string) {
Andreas Gampedf10b322014-06-11 21:46:05 -070093 if (UNLIKELY(!CheckIndex(idx, dex_file_->NumMethodIds(), err_string))) {
Andreas Gampee09269c2014-06-06 18:45:35 -070094 return nullptr;
95 }
96 return &dex_file_->GetMethodId(idx);
97}
98
99// Helper macro to load string and return false on error.
100#define LOAD_STRING(var, idx, error) \
101 const char* var = CheckLoadStringByIdx(idx, error); \
Andreas Gampedf10b322014-06-11 21:46:05 -0700102 if (UNLIKELY(var == nullptr)) { \
Andreas Gampee09269c2014-06-06 18:45:35 -0700103 return false; \
104 }
105
106// Helper macro to load string by type idx and return false on error.
107#define LOAD_STRING_BY_TYPE(var, type_idx, error) \
108 const char* var = CheckLoadStringByTypeIdx(type_idx, error); \
Andreas Gampedf10b322014-06-11 21:46:05 -0700109 if (UNLIKELY(var == nullptr)) { \
Andreas Gampee09269c2014-06-06 18:45:35 -0700110 return false; \
111 }
112
113// Helper macro to load method id. Return last parameter on error.
Andreas Gampe5e31dda2014-06-13 11:35:12 -0700114#define LOAD_METHOD(var, idx, error_string, error_stmt) \
Andreas Gampee09269c2014-06-06 18:45:35 -0700115 const DexFile::MethodId* var = CheckLoadMethodId(idx, error_string); \
Andreas Gampedf10b322014-06-11 21:46:05 -0700116 if (UNLIKELY(var == nullptr)) { \
Andreas Gampe5e31dda2014-06-13 11:35:12 -0700117 error_stmt; \
Andreas Gampee09269c2014-06-06 18:45:35 -0700118 }
119
120// Helper macro to load method id. Return last parameter on error.
Andreas Gampe5e31dda2014-06-13 11:35:12 -0700121#define LOAD_FIELD(var, idx, fmt, error_stmt) \
Andreas Gampee09269c2014-06-06 18:45:35 -0700122 const DexFile::FieldId* var = CheckLoadFieldId(idx, fmt); \
Andreas Gampedf10b322014-06-11 21:46:05 -0700123 if (UNLIKELY(var == nullptr)) { \
Andreas Gampe5e31dda2014-06-13 11:35:12 -0700124 error_stmt; \
Andreas Gampee09269c2014-06-06 18:45:35 -0700125 }
126
Ian Rogers13735952014-10-08 12:43:28 -0700127bool DexFileVerifier::Verify(const DexFile* dex_file, const uint8_t* begin, size_t size,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700128 const char* location, std::string* error_msg) {
Ian Rogers700a4022014-05-19 16:49:03 -0700129 std::unique_ptr<DexFileVerifier> verifier(new DexFileVerifier(dex_file, begin, size, location));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700130 if (!verifier->Verify()) {
131 *error_msg = verifier->FailureReason();
132 return false;
133 }
134 return true;
135}
136
137bool DexFileVerifier::CheckShortyDescriptorMatch(char shorty_char, const char* descriptor,
138 bool is_return_type) {
jeffhao10037c82012-01-23 15:06:23 -0800139 switch (shorty_char) {
140 case 'V':
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700141 if (UNLIKELY(!is_return_type)) {
142 ErrorStringPrintf("Invalid use of void");
jeffhao10037c82012-01-23 15:06:23 -0800143 return false;
144 }
Ian Rogersfc787ec2014-10-09 21:56:44 -0700145 FALLTHROUGH_INTENDED;
jeffhao10037c82012-01-23 15:06:23 -0800146 case 'B':
147 case 'C':
148 case 'D':
149 case 'F':
150 case 'I':
151 case 'J':
152 case 'S':
153 case 'Z':
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700154 if (UNLIKELY((descriptor[0] != shorty_char) || (descriptor[1] != '\0'))) {
155 ErrorStringPrintf("Shorty vs. primitive type mismatch: '%c', '%s'",
156 shorty_char, descriptor);
jeffhao10037c82012-01-23 15:06:23 -0800157 return false;
158 }
159 break;
160 case 'L':
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700161 if (UNLIKELY((descriptor[0] != 'L') && (descriptor[0] != '['))) {
162 ErrorStringPrintf("Shorty vs. type mismatch: '%c', '%s'", shorty_char, descriptor);
jeffhao10037c82012-01-23 15:06:23 -0800163 return false;
164 }
165 break;
166 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700167 ErrorStringPrintf("Bad shorty character: '%c'", shorty_char);
jeffhao10037c82012-01-23 15:06:23 -0800168 return false;
169 }
170 return true;
171}
172
Andreas Gampe50d1bc12014-07-17 21:49:24 -0700173bool DexFileVerifier::CheckListSize(const void* start, size_t count, size_t elem_size,
Andreas Gamped4ae41f2014-09-02 11:17:34 -0700174 const char* label) {
Andreas Gampe50d1bc12014-07-17 21:49:24 -0700175 // Check that size is not 0.
176 CHECK_NE(elem_size, 0U);
177
Ian Rogers13735952014-10-08 12:43:28 -0700178 const uint8_t* range_start = reinterpret_cast<const uint8_t*>(start);
179 const uint8_t* file_start = reinterpret_cast<const uint8_t*>(begin_);
Andreas Gampe50d1bc12014-07-17 21:49:24 -0700180
181 // Check for overflow.
182 uintptr_t max = 0 - 1;
183 size_t available_bytes_till_end_of_mem = max - reinterpret_cast<uintptr_t>(start);
184 size_t max_count = available_bytes_till_end_of_mem / elem_size;
185 if (max_count < count) {
186 ErrorStringPrintf("Overflow in range for %s: %zx for %zu@%zu", label,
187 static_cast<size_t>(range_start - file_start),
188 count, elem_size);
189 return false;
190 }
191
Ian Rogers13735952014-10-08 12:43:28 -0700192 const uint8_t* range_end = range_start + count * elem_size;
193 const uint8_t* file_end = file_start + size_;
Andreas Gampe50d1bc12014-07-17 21:49:24 -0700194 if (UNLIKELY((range_start < file_start) || (range_end > file_end))) {
195 // Note: these two tests are enough as we make sure above that there's no overflow.
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800196 ErrorStringPrintf("Bad range for %s: %zx to %zx", label,
Ian Rogerse3d55812014-06-11 13:00:44 -0700197 static_cast<size_t>(range_start - file_start),
198 static_cast<size_t>(range_end - file_start));
jeffhao10037c82012-01-23 15:06:23 -0800199 return false;
200 }
201 return true;
202}
203
Ian Rogers13735952014-10-08 12:43:28 -0700204bool DexFileVerifier::CheckList(size_t element_size, const char* label, const uint8_t* *ptr) {
Andreas Gamped4ae41f2014-09-02 11:17:34 -0700205 // Check that the list is available. The first 4B are the count.
206 if (!CheckListSize(*ptr, 1, 4U, label)) {
207 return false;
208 }
209
210 uint32_t count = *reinterpret_cast<const uint32_t*>(*ptr);
211 if (count > 0) {
212 if (!CheckListSize(*ptr + 4, count, element_size, label)) {
213 return false;
214 }
215 }
216
217 *ptr += 4 + count * element_size;
218 return true;
219}
220
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700221bool DexFileVerifier::CheckIndex(uint32_t field, uint32_t limit, const char* label) {
222 if (UNLIKELY(field >= limit)) {
223 ErrorStringPrintf("Bad index for %s: %x >= %x", label, field, limit);
jeffhao10037c82012-01-23 15:06:23 -0800224 return false;
225 }
226 return true;
227}
228
Andreas Gamped4ae41f2014-09-02 11:17:34 -0700229bool DexFileVerifier::CheckValidOffsetAndSize(uint32_t offset, uint32_t size, const char* label) {
230 if (size == 0) {
231 if (offset != 0) {
232 ErrorStringPrintf("Offset(%d) should be zero when size is zero for %s.", offset, label);
233 return false;
234 }
235 }
236 if (size_ <= offset) {
237 ErrorStringPrintf("Offset(%d) should be within file size(%zu) for %s.", offset, size_, label);
238 return false;
239 }
240 return true;
241}
242
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700243bool DexFileVerifier::CheckHeader() {
jeffhaof6174e82012-01-31 16:14:17 -0800244 // Check file size from the header.
245 uint32_t expected_size = header_->file_size_;
246 if (size_ != expected_size) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700247 ErrorStringPrintf("Bad file size (%zd, expected %ud)", size_, expected_size);
jeffhao10037c82012-01-23 15:06:23 -0800248 return false;
249 }
250
251 // Compute and verify the checksum in the header.
252 uint32_t adler_checksum = adler32(0L, Z_NULL, 0);
253 const uint32_t non_sum = sizeof(header_->magic_) + sizeof(header_->checksum_);
Ian Rogers13735952014-10-08 12:43:28 -0700254 const uint8_t* non_sum_ptr = reinterpret_cast<const uint8_t*>(header_) + non_sum;
jeffhaof6174e82012-01-31 16:14:17 -0800255 adler_checksum = adler32(adler_checksum, non_sum_ptr, expected_size - non_sum);
jeffhao10037c82012-01-23 15:06:23 -0800256 if (adler_checksum != header_->checksum_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700257 ErrorStringPrintf("Bad checksum (%08x, expected %08x)", adler_checksum, header_->checksum_);
jeffhao10037c82012-01-23 15:06:23 -0800258 return false;
259 }
260
261 // Check the contents of the header.
262 if (header_->endian_tag_ != DexFile::kDexEndianConstant) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700263 ErrorStringPrintf("Unexpected endian_tag: %x", header_->endian_tag_);
jeffhao10037c82012-01-23 15:06:23 -0800264 return false;
265 }
266
267 if (header_->header_size_ != sizeof(DexFile::Header)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700268 ErrorStringPrintf("Bad header size: %ud", header_->header_size_);
jeffhao10037c82012-01-23 15:06:23 -0800269 return false;
270 }
271
Andreas Gamped4ae41f2014-09-02 11:17:34 -0700272 // Check that all offsets are inside the file.
273 bool result =
274 CheckValidOffsetAndSize(header_->link_off_, header_->link_size_, "link") &&
275 CheckValidOffsetAndSize(header_->map_off_, header_->map_off_, "map") &&
276 CheckValidOffsetAndSize(header_->string_ids_off_, header_->string_ids_size_, "string-ids") &&
277 CheckValidOffsetAndSize(header_->type_ids_off_, header_->type_ids_size_, "type-ids") &&
278 CheckValidOffsetAndSize(header_->proto_ids_off_, header_->proto_ids_size_, "proto-ids") &&
279 CheckValidOffsetAndSize(header_->field_ids_off_, header_->field_ids_size_, "field-ids") &&
280 CheckValidOffsetAndSize(header_->method_ids_off_, header_->method_ids_size_, "method-ids") &&
281 CheckValidOffsetAndSize(header_->class_defs_off_, header_->class_defs_size_, "class-defs") &&
282 CheckValidOffsetAndSize(header_->data_off_, header_->data_size_, "data");
283
284 return result;
jeffhao10037c82012-01-23 15:06:23 -0800285}
286
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700287bool DexFileVerifier::CheckMap() {
Andreas Gamped4ae41f2014-09-02 11:17:34 -0700288 const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ +
289 header_->map_off_);
290 // Check that map list content is available.
291 if (!CheckListSize(map, 1, sizeof(DexFile::MapList), "maplist content")) {
292 return false;
293 }
294
jeffhao10037c82012-01-23 15:06:23 -0800295 const DexFile::MapItem* item = map->list_;
296
297 uint32_t count = map->size_;
298 uint32_t last_offset = 0;
299 uint32_t data_item_count = 0;
300 uint32_t data_items_left = header_->data_size_;
301 uint32_t used_bits = 0;
302
303 // Sanity check the size of the map list.
304 if (!CheckListSize(item, count, sizeof(DexFile::MapItem), "map size")) {
305 return false;
306 }
307
308 // Check the items listed in the map.
309 for (uint32_t i = 0; i < count; i++) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700310 if (UNLIKELY(last_offset >= item->offset_ && i != 0)) {
311 ErrorStringPrintf("Out of order map item: %x then %x", last_offset, item->offset_);
jeffhao10037c82012-01-23 15:06:23 -0800312 return false;
313 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700314 if (UNLIKELY(item->offset_ >= header_->file_size_)) {
315 ErrorStringPrintf("Map item after end of file: %x, size %x",
316 item->offset_, header_->file_size_);
jeffhao10037c82012-01-23 15:06:23 -0800317 return false;
318 }
319
320 if (IsDataSectionType(item->type_)) {
321 uint32_t icount = item->size_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700322 if (UNLIKELY(icount > data_items_left)) {
323 ErrorStringPrintf("Too many items in data section: %ud", data_item_count + icount);
jeffhao10037c82012-01-23 15:06:23 -0800324 return false;
325 }
326 data_items_left -= icount;
327 data_item_count += icount;
328 }
329
330 uint32_t bit = MapTypeToBitMask(item->type_);
331
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700332 if (UNLIKELY(bit == 0)) {
333 ErrorStringPrintf("Unknown map section type %x", item->type_);
jeffhao10037c82012-01-23 15:06:23 -0800334 return false;
335 }
336
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700337 if (UNLIKELY((used_bits & bit) != 0)) {
338 ErrorStringPrintf("Duplicate map section of type %x", item->type_);
jeffhao10037c82012-01-23 15:06:23 -0800339 return false;
340 }
341
342 used_bits |= bit;
343 last_offset = item->offset_;
344 item++;
345 }
346
347 // Check for missing sections in the map.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700348 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeHeaderItem)) == 0)) {
349 ErrorStringPrintf("Map is missing header entry");
jeffhao10037c82012-01-23 15:06:23 -0800350 return false;
351 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700352 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeMapList)) == 0)) {
353 ErrorStringPrintf("Map is missing map_list entry");
jeffhao10037c82012-01-23 15:06:23 -0800354 return false;
355 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700356 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeStringIdItem)) == 0 &&
357 ((header_->string_ids_off_ != 0) || (header_->string_ids_size_ != 0)))) {
358 ErrorStringPrintf("Map is missing string_ids entry");
jeffhao10037c82012-01-23 15:06:23 -0800359 return false;
360 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700361 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeTypeIdItem)) == 0 &&
362 ((header_->type_ids_off_ != 0) || (header_->type_ids_size_ != 0)))) {
363 ErrorStringPrintf("Map is missing type_ids entry");
jeffhao10037c82012-01-23 15:06:23 -0800364 return false;
365 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700366 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeProtoIdItem)) == 0 &&
367 ((header_->proto_ids_off_ != 0) || (header_->proto_ids_size_ != 0)))) {
368 ErrorStringPrintf("Map is missing proto_ids entry");
jeffhao10037c82012-01-23 15:06:23 -0800369 return false;
370 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700371 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeFieldIdItem)) == 0 &&
372 ((header_->field_ids_off_ != 0) || (header_->field_ids_size_ != 0)))) {
373 ErrorStringPrintf("Map is missing field_ids entry");
jeffhao10037c82012-01-23 15:06:23 -0800374 return false;
375 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700376 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeMethodIdItem)) == 0 &&
377 ((header_->method_ids_off_ != 0) || (header_->method_ids_size_ != 0)))) {
378 ErrorStringPrintf("Map is missing method_ids entry");
jeffhao10037c82012-01-23 15:06:23 -0800379 return false;
380 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700381 if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeClassDefItem)) == 0 &&
382 ((header_->class_defs_off_ != 0) || (header_->class_defs_size_ != 0)))) {
383 ErrorStringPrintf("Map is missing class_defs entry");
jeffhao10037c82012-01-23 15:06:23 -0800384 return false;
385 }
jeffhao10037c82012-01-23 15:06:23 -0800386 return true;
387}
388
389uint32_t DexFileVerifier::ReadUnsignedLittleEndian(uint32_t size) {
390 uint32_t result = 0;
Ian Rogers13735952014-10-08 12:43:28 -0700391 if (LIKELY(CheckListSize(ptr_, size, sizeof(uint8_t), "encoded_value"))) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700392 for (uint32_t i = 0; i < size; i++) {
393 result |= ((uint32_t) *(ptr_++)) << (i * 8);
394 }
jeffhao10037c82012-01-23 15:06:23 -0800395 }
jeffhao10037c82012-01-23 15:06:23 -0800396 return result;
397}
398
399bool DexFileVerifier::CheckAndGetHandlerOffsets(const DexFile::CodeItem* code_item,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700400 uint32_t* handler_offsets, uint32_t handlers_size) {
Ian Rogers13735952014-10-08 12:43:28 -0700401 const uint8_t* handlers_base = DexFile::GetCatchHandlerData(*code_item, 0);
jeffhao10037c82012-01-23 15:06:23 -0800402
403 for (uint32_t i = 0; i < handlers_size; i++) {
404 bool catch_all;
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800405 size_t offset = ptr_ - handlers_base;
jeffhao10037c82012-01-23 15:06:23 -0800406 int32_t size = DecodeSignedLeb128(&ptr_);
407
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700408 if (UNLIKELY((size < -65536) || (size > 65536))) {
409 ErrorStringPrintf("Invalid exception handler size: %d", size);
jeffhao10037c82012-01-23 15:06:23 -0800410 return false;
411 }
412
413 if (size <= 0) {
414 catch_all = true;
415 size = -size;
416 } else {
417 catch_all = false;
418 }
419
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800420 handler_offsets[i] = static_cast<uint32_t>(offset);
jeffhao10037c82012-01-23 15:06:23 -0800421
422 while (size-- > 0) {
423 uint32_t type_idx = DecodeUnsignedLeb128(&ptr_);
424 if (!CheckIndex(type_idx, header_->type_ids_size_, "handler type_idx")) {
425 return false;
426 }
427
428 uint32_t addr = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700429 if (UNLIKELY(addr >= code_item->insns_size_in_code_units_)) {
430 ErrorStringPrintf("Invalid handler addr: %x", addr);
jeffhao10037c82012-01-23 15:06:23 -0800431 return false;
432 }
433 }
434
435 if (catch_all) {
436 uint32_t addr = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700437 if (UNLIKELY(addr >= code_item->insns_size_in_code_units_)) {
438 ErrorStringPrintf("Invalid handler catch_all_addr: %x", addr);
jeffhao10037c82012-01-23 15:06:23 -0800439 return false;
440 }
441 }
442 }
443
444 return true;
445}
446
447bool DexFileVerifier::CheckClassDataItemField(uint32_t idx, uint32_t access_flags,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700448 bool expect_static) {
jeffhao10037c82012-01-23 15:06:23 -0800449 if (!CheckIndex(idx, header_->field_ids_size_, "class_data_item field_idx")) {
450 return false;
451 }
452
453 bool is_static = (access_flags & kAccStatic) != 0;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700454 if (UNLIKELY(is_static != expect_static)) {
455 ErrorStringPrintf("Static/instance field not in expected list");
jeffhao10037c82012-01-23 15:06:23 -0800456 return false;
457 }
458
Andreas Gampe51829322014-08-25 15:05:04 -0700459 if (UNLIKELY((access_flags & ~kAccJavaFlagsMask) != 0)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700460 ErrorStringPrintf("Bad class_data_item field access_flags %x", access_flags);
jeffhao10037c82012-01-23 15:06:23 -0800461 return false;
462 }
463
464 return true;
465}
466
467bool DexFileVerifier::CheckClassDataItemMethod(uint32_t idx, uint32_t access_flags,
Jeff Haoa574b0e2015-06-04 18:12:26 -0700468 uint32_t code_offset,
469 std::unordered_set<uint32_t>& direct_method_indexes,
470 bool expect_direct) {
jeffhao10037c82012-01-23 15:06:23 -0800471 if (!CheckIndex(idx, header_->method_ids_size_, "class_data_item method_idx")) {
472 return false;
473 }
474
475 bool is_direct = (access_flags & (kAccStatic | kAccPrivate | kAccConstructor)) != 0;
476 bool expect_code = (access_flags & (kAccNative | kAccAbstract)) == 0;
477 bool is_synchronized = (access_flags & kAccSynchronized) != 0;
478 bool allow_synchronized = (access_flags & kAccNative) != 0;
479
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700480 if (UNLIKELY(is_direct != expect_direct)) {
481 ErrorStringPrintf("Direct/virtual method not in expected list");
jeffhao10037c82012-01-23 15:06:23 -0800482 return false;
483 }
484
Jeff Haoa574b0e2015-06-04 18:12:26 -0700485 if (expect_direct) {
486 direct_method_indexes.insert(idx);
487 } else if (direct_method_indexes.find(idx) != direct_method_indexes.end()) {
488 ErrorStringPrintf("Found virtual method with same index as direct method: %d", idx);
489 return false;
490 }
491
Andreas Gampe51829322014-08-25 15:05:04 -0700492 constexpr uint32_t access_method_mask = kAccJavaFlagsMask | kAccConstructor |
493 kAccDeclaredSynchronized;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700494 if (UNLIKELY(((access_flags & ~access_method_mask) != 0) ||
495 (is_synchronized && !allow_synchronized))) {
496 ErrorStringPrintf("Bad class_data_item method access_flags %x", access_flags);
jeffhao10037c82012-01-23 15:06:23 -0800497 return false;
498 }
499
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700500 if (UNLIKELY(expect_code && (code_offset == 0))) {
501 ErrorStringPrintf("Unexpected zero value for class_data_item method code_off with access "
502 "flags %x", access_flags);
jeffhao10037c82012-01-23 15:06:23 -0800503 return false;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700504 } else if (UNLIKELY(!expect_code && (code_offset != 0))) {
505 ErrorStringPrintf("Unexpected non-zero value %x for class_data_item method code_off"
506 " with access flags %x", code_offset, access_flags);
jeffhao10037c82012-01-23 15:06:23 -0800507 return false;
508 }
509
510 return true;
511}
512
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800513bool DexFileVerifier::CheckPadding(size_t offset, uint32_t aligned_offset) {
jeffhao10037c82012-01-23 15:06:23 -0800514 if (offset < aligned_offset) {
Ian Rogers13735952014-10-08 12:43:28 -0700515 if (!CheckListSize(begin_ + offset, aligned_offset - offset, sizeof(uint8_t), "section")) {
jeffhao10037c82012-01-23 15:06:23 -0800516 return false;
517 }
518 while (offset < aligned_offset) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700519 if (UNLIKELY(*ptr_ != '\0')) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800520 ErrorStringPrintf("Non-zero padding %x before section start at %zx", *ptr_, offset);
jeffhao10037c82012-01-23 15:06:23 -0800521 return false;
522 }
523 ptr_++;
524 offset++;
525 }
526 }
527 return true;
528}
529
530bool DexFileVerifier::CheckEncodedValue() {
Ian Rogers13735952014-10-08 12:43:28 -0700531 if (!CheckListSize(ptr_, 1, sizeof(uint8_t), "encoded_value header")) {
jeffhao10037c82012-01-23 15:06:23 -0800532 return false;
533 }
534
535 uint8_t header_byte = *(ptr_++);
536 uint32_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
537 uint32_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
538
539 switch (value_type) {
540 case DexFile::kDexAnnotationByte:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700541 if (UNLIKELY(value_arg != 0)) {
542 ErrorStringPrintf("Bad encoded_value byte size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800543 return false;
544 }
545 ptr_++;
546 break;
547 case DexFile::kDexAnnotationShort:
548 case DexFile::kDexAnnotationChar:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700549 if (UNLIKELY(value_arg > 1)) {
550 ErrorStringPrintf("Bad encoded_value char/short size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800551 return false;
552 }
553 ptr_ += value_arg + 1;
554 break;
555 case DexFile::kDexAnnotationInt:
556 case DexFile::kDexAnnotationFloat:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700557 if (UNLIKELY(value_arg > 3)) {
558 ErrorStringPrintf("Bad encoded_value int/float size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800559 return false;
560 }
561 ptr_ += value_arg + 1;
562 break;
563 case DexFile::kDexAnnotationLong:
564 case DexFile::kDexAnnotationDouble:
565 ptr_ += value_arg + 1;
566 break;
567 case DexFile::kDexAnnotationString: {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700568 if (UNLIKELY(value_arg > 3)) {
569 ErrorStringPrintf("Bad encoded_value string size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800570 return false;
571 }
572 uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
573 if (!CheckIndex(idx, header_->string_ids_size_, "encoded_value string")) {
574 return false;
575 }
576 break;
577 }
578 case DexFile::kDexAnnotationType: {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700579 if (UNLIKELY(value_arg > 3)) {
580 ErrorStringPrintf("Bad encoded_value type size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800581 return false;
582 }
583 uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
584 if (!CheckIndex(idx, header_->type_ids_size_, "encoded_value type")) {
585 return false;
586 }
587 break;
588 }
589 case DexFile::kDexAnnotationField:
590 case DexFile::kDexAnnotationEnum: {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700591 if (UNLIKELY(value_arg > 3)) {
592 ErrorStringPrintf("Bad encoded_value field/enum size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800593 return false;
594 }
595 uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
596 if (!CheckIndex(idx, header_->field_ids_size_, "encoded_value field")) {
597 return false;
598 }
599 break;
600 }
601 case DexFile::kDexAnnotationMethod: {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700602 if (UNLIKELY(value_arg > 3)) {
603 ErrorStringPrintf("Bad encoded_value method size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800604 return false;
605 }
606 uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
607 if (!CheckIndex(idx, header_->method_ids_size_, "encoded_value method")) {
608 return false;
609 }
610 break;
611 }
612 case DexFile::kDexAnnotationArray:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700613 if (UNLIKELY(value_arg != 0)) {
614 ErrorStringPrintf("Bad encoded_value array value_arg %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800615 return false;
616 }
617 if (!CheckEncodedArray()) {
618 return false;
619 }
620 break;
621 case DexFile::kDexAnnotationAnnotation:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700622 if (UNLIKELY(value_arg != 0)) {
623 ErrorStringPrintf("Bad encoded_value annotation value_arg %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800624 return false;
625 }
626 if (!CheckEncodedAnnotation()) {
627 return false;
628 }
629 break;
630 case DexFile::kDexAnnotationNull:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700631 if (UNLIKELY(value_arg != 0)) {
632 ErrorStringPrintf("Bad encoded_value null value_arg %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800633 return false;
634 }
635 break;
636 case DexFile::kDexAnnotationBoolean:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700637 if (UNLIKELY(value_arg > 1)) {
638 ErrorStringPrintf("Bad encoded_value boolean size %x", value_arg);
jeffhao10037c82012-01-23 15:06:23 -0800639 return false;
640 }
641 break;
642 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700643 ErrorStringPrintf("Bogus encoded_value value_type %x", value_type);
jeffhao10037c82012-01-23 15:06:23 -0800644 return false;
645 }
646
647 return true;
648}
649
650bool DexFileVerifier::CheckEncodedArray() {
651 uint32_t size = DecodeUnsignedLeb128(&ptr_);
652
653 while (size--) {
654 if (!CheckEncodedValue()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700655 failure_reason_ = StringPrintf("Bad encoded_array value: %s", failure_reason_.c_str());
jeffhao10037c82012-01-23 15:06:23 -0800656 return false;
657 }
658 }
659 return true;
660}
661
662bool DexFileVerifier::CheckEncodedAnnotation() {
663 uint32_t idx = DecodeUnsignedLeb128(&ptr_);
664 if (!CheckIndex(idx, header_->type_ids_size_, "encoded_annotation type_idx")) {
665 return false;
666 }
667
668 uint32_t size = DecodeUnsignedLeb128(&ptr_);
669 uint32_t last_idx = 0;
670
671 for (uint32_t i = 0; i < size; i++) {
672 idx = DecodeUnsignedLeb128(&ptr_);
673 if (!CheckIndex(idx, header_->string_ids_size_, "annotation_element name_idx")) {
674 return false;
675 }
676
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700677 if (UNLIKELY(last_idx >= idx && i != 0)) {
678 ErrorStringPrintf("Out-of-order annotation_element name_idx: %x then %x",
679 last_idx, idx);
jeffhao10037c82012-01-23 15:06:23 -0800680 return false;
681 }
682
683 if (!CheckEncodedValue()) {
684 return false;
685 }
686
687 last_idx = idx;
688 }
689 return true;
690}
691
692bool DexFileVerifier::CheckIntraClassDataItem() {
693 ClassDataItemIterator it(*dex_file_, ptr_);
Jeff Haoa574b0e2015-06-04 18:12:26 -0700694 std::unordered_set<uint32_t> direct_method_indexes;
jeffhao10037c82012-01-23 15:06:23 -0800695
Andreas Gampe51829322014-08-25 15:05:04 -0700696 // These calls use the raw access flags to check whether the whole dex field is valid.
697
jeffhao10037c82012-01-23 15:06:23 -0800698 for (; it.HasNextStaticField(); it.Next()) {
Andreas Gampe51829322014-08-25 15:05:04 -0700699 if (!CheckClassDataItemField(it.GetMemberIndex(), it.GetRawMemberAccessFlags(), true)) {
jeffhao10037c82012-01-23 15:06:23 -0800700 return false;
701 }
702 }
703 for (; it.HasNextInstanceField(); it.Next()) {
Andreas Gampe51829322014-08-25 15:05:04 -0700704 if (!CheckClassDataItemField(it.GetMemberIndex(), it.GetRawMemberAccessFlags(), false)) {
jeffhao10037c82012-01-23 15:06:23 -0800705 return false;
706 }
707 }
708 for (; it.HasNextDirectMethod(); it.Next()) {
Andreas Gampe51829322014-08-25 15:05:04 -0700709 if (!CheckClassDataItemMethod(it.GetMemberIndex(), it.GetRawMemberAccessFlags(),
Jeff Haoa574b0e2015-06-04 18:12:26 -0700710 it.GetMethodCodeItemOffset(), direct_method_indexes, true)) {
jeffhao10037c82012-01-23 15:06:23 -0800711 return false;
712 }
713 }
714 for (; it.HasNextVirtualMethod(); it.Next()) {
Andreas Gampe51829322014-08-25 15:05:04 -0700715 if (!CheckClassDataItemMethod(it.GetMemberIndex(), it.GetRawMemberAccessFlags(),
Jeff Haoa574b0e2015-06-04 18:12:26 -0700716 it.GetMethodCodeItemOffset(), direct_method_indexes, false)) {
jeffhao10037c82012-01-23 15:06:23 -0800717 return false;
718 }
719 }
720
721 ptr_ = it.EndDataPointer();
722 return true;
723}
724
725bool DexFileVerifier::CheckIntraCodeItem() {
726 const DexFile::CodeItem* code_item = reinterpret_cast<const DexFile::CodeItem*>(ptr_);
Andreas Gampe50d1bc12014-07-17 21:49:24 -0700727 if (!CheckListSize(code_item, 1, sizeof(DexFile::CodeItem), "code")) {
jeffhao10037c82012-01-23 15:06:23 -0800728 return false;
729 }
730
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700731 if (UNLIKELY(code_item->ins_size_ > code_item->registers_size_)) {
732 ErrorStringPrintf("ins_size (%ud) > registers_size (%ud)",
733 code_item->ins_size_, code_item->registers_size_);
jeffhao10037c82012-01-23 15:06:23 -0800734 return false;
735 }
736
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700737 if (UNLIKELY((code_item->outs_size_ > 5) &&
738 (code_item->outs_size_ > code_item->registers_size_))) {
jeffhao10037c82012-01-23 15:06:23 -0800739 /*
740 * outs_size can be up to 5, even if registers_size is smaller, since the
741 * short forms of method invocation allow repetitions of a register multiple
742 * times within a single parameter list. However, longer parameter lists
743 * need to be represented in-order in the register file.
744 */
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700745 ErrorStringPrintf("outs_size (%ud) > registers_size (%ud)",
746 code_item->outs_size_, code_item->registers_size_);
jeffhao10037c82012-01-23 15:06:23 -0800747 return false;
748 }
749
750 const uint16_t* insns = code_item->insns_;
751 uint32_t insns_size = code_item->insns_size_in_code_units_;
752 if (!CheckListSize(insns, insns_size, sizeof(uint16_t), "insns size")) {
753 return false;
754 }
755
756 // Grab the end of the insns if there are no try_items.
757 uint32_t try_items_size = code_item->tries_size_;
758 if (try_items_size == 0) {
Ian Rogers13735952014-10-08 12:43:28 -0700759 ptr_ = reinterpret_cast<const uint8_t*>(&insns[insns_size]);
jeffhao10037c82012-01-23 15:06:23 -0800760 return true;
761 }
762
763 // try_items are 4-byte aligned. Verify the spacer is 0.
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800764 if (((reinterpret_cast<uintptr_t>(&insns[insns_size]) & 3) != 0) && (insns[insns_size] != 0)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700765 ErrorStringPrintf("Non-zero padding: %x", insns[insns_size]);
jeffhao10037c82012-01-23 15:06:23 -0800766 return false;
767 }
768
769 const DexFile::TryItem* try_items = DexFile::GetTryItems(*code_item, 0);
770 ptr_ = DexFile::GetCatchHandlerData(*code_item, 0);
771 uint32_t handlers_size = DecodeUnsignedLeb128(&ptr_);
772
773 if (!CheckListSize(try_items, try_items_size, sizeof(DexFile::TryItem), "try_items size")) {
774 return false;
775 }
776
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700777 if (UNLIKELY((handlers_size == 0) || (handlers_size >= 65536))) {
778 ErrorStringPrintf("Invalid handlers_size: %ud", handlers_size);
jeffhao10037c82012-01-23 15:06:23 -0800779 return false;
780 }
781
Ian Rogers700a4022014-05-19 16:49:03 -0700782 std::unique_ptr<uint32_t[]> handler_offsets(new uint32_t[handlers_size]);
Elliott Hughesee0fa762012-03-26 17:12:41 -0700783 if (!CheckAndGetHandlerOffsets(code_item, &handler_offsets[0], handlers_size)) {
jeffhao10037c82012-01-23 15:06:23 -0800784 return false;
785 }
786
787 uint32_t last_addr = 0;
788 while (try_items_size--) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700789 if (UNLIKELY(try_items->start_addr_ < last_addr)) {
790 ErrorStringPrintf("Out-of_order try_item with start_addr: %x", try_items->start_addr_);
jeffhao10037c82012-01-23 15:06:23 -0800791 return false;
792 }
793
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700794 if (UNLIKELY(try_items->start_addr_ >= insns_size)) {
795 ErrorStringPrintf("Invalid try_item start_addr: %x", try_items->start_addr_);
jeffhao10037c82012-01-23 15:06:23 -0800796 return false;
797 }
798
799 uint32_t i;
800 for (i = 0; i < handlers_size; i++) {
801 if (try_items->handler_off_ == handler_offsets[i]) {
802 break;
803 }
804 }
805
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700806 if (UNLIKELY(i == handlers_size)) {
807 ErrorStringPrintf("Bogus handler offset: %x", try_items->handler_off_);
jeffhao10037c82012-01-23 15:06:23 -0800808 return false;
809 }
810
811 last_addr = try_items->start_addr_ + try_items->insn_count_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700812 if (UNLIKELY(last_addr > insns_size)) {
813 ErrorStringPrintf("Invalid try_item insn_count: %x", try_items->insn_count_);
jeffhao10037c82012-01-23 15:06:23 -0800814 return false;
815 }
816
817 try_items++;
818 }
819
820 return true;
821}
822
823bool DexFileVerifier::CheckIntraStringDataItem() {
824 uint32_t size = DecodeUnsignedLeb128(&ptr_);
Ian Rogers13735952014-10-08 12:43:28 -0700825 const uint8_t* file_end = begin_ + size_;
jeffhao10037c82012-01-23 15:06:23 -0800826
827 for (uint32_t i = 0; i < size; i++) {
Brian Carlstromc6475642014-05-27 11:14:12 -0700828 CHECK_LT(i, size); // b/15014252 Prevents hitting the impossible case below
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700829 if (UNLIKELY(ptr_ >= file_end)) {
830 ErrorStringPrintf("String data would go beyond end-of-file");
jeffhao10037c82012-01-23 15:06:23 -0800831 return false;
832 }
833
834 uint8_t byte = *(ptr_++);
835
836 // Switch on the high 4 bits.
837 switch (byte >> 4) {
838 case 0x00:
839 // Special case of bit pattern 0xxx.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700840 if (UNLIKELY(byte == 0)) {
Brian Carlstromc6475642014-05-27 11:14:12 -0700841 CHECK_LT(i, size); // b/15014252 Actually hit this impossible case with clang
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700842 ErrorStringPrintf("String data shorter than indicated utf16_size %x", size);
jeffhao10037c82012-01-23 15:06:23 -0800843 return false;
844 }
845 break;
846 case 0x01:
847 case 0x02:
848 case 0x03:
849 case 0x04:
850 case 0x05:
851 case 0x06:
852 case 0x07:
853 // No extra checks necessary for bit pattern 0xxx.
854 break;
855 case 0x08:
856 case 0x09:
857 case 0x0a:
858 case 0x0b:
859 case 0x0f:
860 // Illegal bit patterns 10xx or 1111.
861 // Note: 1111 is valid for normal UTF-8, but not here.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700862 ErrorStringPrintf("Illegal start byte %x in string data", byte);
jeffhao10037c82012-01-23 15:06:23 -0800863 return false;
864 case 0x0c:
865 case 0x0d: {
866 // Bit pattern 110x has an additional byte.
867 uint8_t byte2 = *(ptr_++);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700868 if (UNLIKELY((byte2 & 0xc0) != 0x80)) {
869 ErrorStringPrintf("Illegal continuation byte %x in string data", byte2);
jeffhao10037c82012-01-23 15:06:23 -0800870 return false;
871 }
872 uint16_t value = ((byte & 0x1f) << 6) | (byte2 & 0x3f);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700873 if (UNLIKELY((value != 0) && (value < 0x80))) {
874 ErrorStringPrintf("Illegal representation for value %x in string data", value);
jeffhao10037c82012-01-23 15:06:23 -0800875 return false;
876 }
877 break;
878 }
879 case 0x0e: {
880 // Bit pattern 1110 has 2 additional bytes.
881 uint8_t byte2 = *(ptr_++);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700882 if (UNLIKELY((byte2 & 0xc0) != 0x80)) {
883 ErrorStringPrintf("Illegal continuation byte %x in string data", byte2);
jeffhao10037c82012-01-23 15:06:23 -0800884 return false;
885 }
886 uint8_t byte3 = *(ptr_++);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700887 if (UNLIKELY((byte3 & 0xc0) != 0x80)) {
888 ErrorStringPrintf("Illegal continuation byte %x in string data", byte3);
jeffhao10037c82012-01-23 15:06:23 -0800889 return false;
890 }
891 uint16_t value = ((byte & 0x0f) << 12) | ((byte2 & 0x3f) << 6) | (byte3 & 0x3f);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700892 if (UNLIKELY(value < 0x800)) {
893 ErrorStringPrintf("Illegal representation for value %x in string data", value);
jeffhao10037c82012-01-23 15:06:23 -0800894 return false;
895 }
896 break;
897 }
898 }
899 }
900
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700901 if (UNLIKELY(*(ptr_++) != '\0')) {
902 ErrorStringPrintf("String longer than indicated size %x", size);
jeffhao10037c82012-01-23 15:06:23 -0800903 return false;
904 }
905
906 return true;
907}
908
909bool DexFileVerifier::CheckIntraDebugInfoItem() {
910 DecodeUnsignedLeb128(&ptr_);
911 uint32_t parameters_size = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700912 if (UNLIKELY(parameters_size > 65536)) {
913 ErrorStringPrintf("Invalid parameters_size: %x", parameters_size);
jeffhao10037c82012-01-23 15:06:23 -0800914 return false;
915 }
916
917 for (uint32_t j = 0; j < parameters_size; j++) {
918 uint32_t parameter_name = DecodeUnsignedLeb128(&ptr_);
919 if (parameter_name != 0) {
920 parameter_name--;
921 if (!CheckIndex(parameter_name, header_->string_ids_size_, "debug_info_item parameter_name")) {
922 return false;
923 }
924 }
925 }
926
927 while (true) {
928 uint8_t opcode = *(ptr_++);
929 switch (opcode) {
930 case DexFile::DBG_END_SEQUENCE: {
931 return true;
932 }
933 case DexFile::DBG_ADVANCE_PC: {
934 DecodeUnsignedLeb128(&ptr_);
935 break;
936 }
937 case DexFile::DBG_ADVANCE_LINE: {
938 DecodeSignedLeb128(&ptr_);
939 break;
940 }
941 case DexFile::DBG_START_LOCAL: {
942 uint32_t reg_num = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700943 if (UNLIKELY(reg_num >= 65536)) {
944 ErrorStringPrintf("Bad reg_num for opcode %x", opcode);
jeffhao10037c82012-01-23 15:06:23 -0800945 return false;
946 }
947 uint32_t name_idx = DecodeUnsignedLeb128(&ptr_);
948 if (name_idx != 0) {
949 name_idx--;
950 if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_START_LOCAL name_idx")) {
951 return false;
952 }
953 }
954 uint32_t type_idx = DecodeUnsignedLeb128(&ptr_);
955 if (type_idx != 0) {
956 type_idx--;
Logan Chiendd3208d2015-04-19 23:27:52 +0800957 if (!CheckIndex(type_idx, header_->type_ids_size_, "DBG_START_LOCAL type_idx")) {
jeffhao10037c82012-01-23 15:06:23 -0800958 return false;
959 }
960 }
961 break;
962 }
963 case DexFile::DBG_END_LOCAL:
964 case DexFile::DBG_RESTART_LOCAL: {
965 uint32_t reg_num = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700966 if (UNLIKELY(reg_num >= 65536)) {
967 ErrorStringPrintf("Bad reg_num for opcode %x", opcode);
jeffhao10037c82012-01-23 15:06:23 -0800968 return false;
969 }
970 break;
971 }
972 case DexFile::DBG_START_LOCAL_EXTENDED: {
973 uint32_t reg_num = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700974 if (UNLIKELY(reg_num >= 65536)) {
975 ErrorStringPrintf("Bad reg_num for opcode %x", opcode);
jeffhao10037c82012-01-23 15:06:23 -0800976 return false;
977 }
978 uint32_t name_idx = DecodeUnsignedLeb128(&ptr_);
979 if (name_idx != 0) {
980 name_idx--;
981 if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED name_idx")) {
982 return false;
983 }
984 }
985 uint32_t type_idx = DecodeUnsignedLeb128(&ptr_);
986 if (type_idx != 0) {
987 type_idx--;
Logan Chiendd3208d2015-04-19 23:27:52 +0800988 if (!CheckIndex(type_idx, header_->type_ids_size_, "DBG_START_LOCAL_EXTENDED type_idx")) {
jeffhao10037c82012-01-23 15:06:23 -0800989 return false;
990 }
991 }
992 uint32_t sig_idx = DecodeUnsignedLeb128(&ptr_);
993 if (sig_idx != 0) {
994 sig_idx--;
995 if (!CheckIndex(sig_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED sig_idx")) {
996 return false;
997 }
998 }
999 break;
1000 }
1001 case DexFile::DBG_SET_FILE: {
1002 uint32_t name_idx = DecodeUnsignedLeb128(&ptr_);
1003 if (name_idx != 0) {
1004 name_idx--;
1005 if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_SET_FILE name_idx")) {
1006 return false;
1007 }
1008 }
1009 break;
1010 }
1011 }
1012 }
1013}
1014
1015bool DexFileVerifier::CheckIntraAnnotationItem() {
Ian Rogers13735952014-10-08 12:43:28 -07001016 if (!CheckListSize(ptr_, 1, sizeof(uint8_t), "annotation visibility")) {
jeffhao10037c82012-01-23 15:06:23 -08001017 return false;
1018 }
1019
1020 // Check visibility
1021 switch (*(ptr_++)) {
1022 case DexFile::kDexVisibilityBuild:
1023 case DexFile::kDexVisibilityRuntime:
1024 case DexFile::kDexVisibilitySystem:
1025 break;
1026 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001027 ErrorStringPrintf("Bad annotation visibility: %x", *ptr_);
jeffhao10037c82012-01-23 15:06:23 -08001028 return false;
1029 }
1030
1031 if (!CheckEncodedAnnotation()) {
1032 return false;
1033 }
1034
1035 return true;
1036}
1037
1038bool DexFileVerifier::CheckIntraAnnotationsDirectoryItem() {
1039 const DexFile::AnnotationsDirectoryItem* item =
1040 reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr_);
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001041 if (!CheckListSize(item, 1, sizeof(DexFile::AnnotationsDirectoryItem), "annotations_directory")) {
jeffhao10037c82012-01-23 15:06:23 -08001042 return false;
1043 }
1044
1045 // Field annotations follow immediately after the annotations directory.
1046 const DexFile::FieldAnnotationsItem* field_item =
1047 reinterpret_cast<const DexFile::FieldAnnotationsItem*>(item + 1);
1048 uint32_t field_count = item->fields_size_;
1049 if (!CheckListSize(field_item, field_count, sizeof(DexFile::FieldAnnotationsItem), "field_annotations list")) {
1050 return false;
1051 }
1052
1053 uint32_t last_idx = 0;
1054 for (uint32_t i = 0; i < field_count; i++) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001055 if (UNLIKELY(last_idx >= field_item->field_idx_ && i != 0)) {
1056 ErrorStringPrintf("Out-of-order field_idx for annotation: %x then %x", last_idx, field_item->field_idx_);
jeffhao10037c82012-01-23 15:06:23 -08001057 return false;
1058 }
1059 last_idx = field_item->field_idx_;
1060 field_item++;
1061 }
1062
1063 // Method annotations follow immediately after field annotations.
1064 const DexFile::MethodAnnotationsItem* method_item =
1065 reinterpret_cast<const DexFile::MethodAnnotationsItem*>(field_item);
1066 uint32_t method_count = item->methods_size_;
1067 if (!CheckListSize(method_item, method_count, sizeof(DexFile::MethodAnnotationsItem), "method_annotations list")) {
1068 return false;
1069 }
1070
1071 last_idx = 0;
1072 for (uint32_t i = 0; i < method_count; i++) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001073 if (UNLIKELY(last_idx >= method_item->method_idx_ && i != 0)) {
1074 ErrorStringPrintf("Out-of-order method_idx for annotation: %x then %x",
1075 last_idx, method_item->method_idx_);
jeffhao10037c82012-01-23 15:06:23 -08001076 return false;
1077 }
1078 last_idx = method_item->method_idx_;
1079 method_item++;
1080 }
1081
1082 // Parameter annotations follow immediately after method annotations.
1083 const DexFile::ParameterAnnotationsItem* parameter_item =
1084 reinterpret_cast<const DexFile::ParameterAnnotationsItem*>(method_item);
1085 uint32_t parameter_count = item->parameters_size_;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001086 if (!CheckListSize(parameter_item, parameter_count, sizeof(DexFile::ParameterAnnotationsItem),
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001087 "parameter_annotations list")) {
jeffhao10037c82012-01-23 15:06:23 -08001088 return false;
1089 }
1090
1091 last_idx = 0;
1092 for (uint32_t i = 0; i < parameter_count; i++) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001093 if (UNLIKELY(last_idx >= parameter_item->method_idx_ && i != 0)) {
1094 ErrorStringPrintf("Out-of-order method_idx for annotation: %x then %x",
1095 last_idx, parameter_item->method_idx_);
jeffhao10037c82012-01-23 15:06:23 -08001096 return false;
1097 }
1098 last_idx = parameter_item->method_idx_;
1099 parameter_item++;
1100 }
1101
1102 // Return a pointer to the end of the annotations.
Ian Rogers13735952014-10-08 12:43:28 -07001103 ptr_ = reinterpret_cast<const uint8_t*>(parameter_item);
jeffhao10037c82012-01-23 15:06:23 -08001104 return true;
1105}
1106
Andreas Gampeb061cc12014-09-02 10:22:20 -07001107bool DexFileVerifier::CheckIntraSectionIterate(size_t offset, uint32_t section_count,
1108 uint16_t type) {
jeffhao10037c82012-01-23 15:06:23 -08001109 // Get the right alignment mask for the type of section.
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001110 size_t alignment_mask;
jeffhao10037c82012-01-23 15:06:23 -08001111 switch (type) {
1112 case DexFile::kDexTypeClassDataItem:
1113 case DexFile::kDexTypeStringDataItem:
1114 case DexFile::kDexTypeDebugInfoItem:
1115 case DexFile::kDexTypeAnnotationItem:
1116 case DexFile::kDexTypeEncodedArrayItem:
1117 alignment_mask = sizeof(uint8_t) - 1;
1118 break;
1119 default:
1120 alignment_mask = sizeof(uint32_t) - 1;
1121 break;
1122 }
1123
1124 // Iterate through the items in the section.
Andreas Gampeb061cc12014-09-02 10:22:20 -07001125 for (uint32_t i = 0; i < section_count; i++) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001126 size_t aligned_offset = (offset + alignment_mask) & ~alignment_mask;
jeffhao10037c82012-01-23 15:06:23 -08001127
1128 // Check the padding between items.
1129 if (!CheckPadding(offset, aligned_offset)) {
1130 return false;
1131 }
1132
1133 // Check depending on the section type.
1134 switch (type) {
1135 case DexFile::kDexTypeStringIdItem: {
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001136 if (!CheckListSize(ptr_, 1, sizeof(DexFile::StringId), "string_ids")) {
jeffhao10037c82012-01-23 15:06:23 -08001137 return false;
1138 }
1139 ptr_ += sizeof(DexFile::StringId);
1140 break;
1141 }
1142 case DexFile::kDexTypeTypeIdItem: {
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001143 if (!CheckListSize(ptr_, 1, sizeof(DexFile::TypeId), "type_ids")) {
jeffhao10037c82012-01-23 15:06:23 -08001144 return false;
1145 }
1146 ptr_ += sizeof(DexFile::TypeId);
1147 break;
1148 }
1149 case DexFile::kDexTypeProtoIdItem: {
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001150 if (!CheckListSize(ptr_, 1, sizeof(DexFile::ProtoId), "proto_ids")) {
jeffhao10037c82012-01-23 15:06:23 -08001151 return false;
1152 }
1153 ptr_ += sizeof(DexFile::ProtoId);
1154 break;
1155 }
1156 case DexFile::kDexTypeFieldIdItem: {
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001157 if (!CheckListSize(ptr_, 1, sizeof(DexFile::FieldId), "field_ids")) {
jeffhao10037c82012-01-23 15:06:23 -08001158 return false;
1159 }
1160 ptr_ += sizeof(DexFile::FieldId);
1161 break;
1162 }
1163 case DexFile::kDexTypeMethodIdItem: {
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001164 if (!CheckListSize(ptr_, 1, sizeof(DexFile::MethodId), "method_ids")) {
jeffhao10037c82012-01-23 15:06:23 -08001165 return false;
1166 }
1167 ptr_ += sizeof(DexFile::MethodId);
1168 break;
1169 }
1170 case DexFile::kDexTypeClassDefItem: {
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001171 if (!CheckListSize(ptr_, 1, sizeof(DexFile::ClassDef), "class_defs")) {
jeffhao10037c82012-01-23 15:06:23 -08001172 return false;
1173 }
1174 ptr_ += sizeof(DexFile::ClassDef);
1175 break;
1176 }
1177 case DexFile::kDexTypeTypeList: {
Andreas Gamped4ae41f2014-09-02 11:17:34 -07001178 if (!CheckList(sizeof(DexFile::TypeItem), "type_list", &ptr_)) {
jeffhao10037c82012-01-23 15:06:23 -08001179 return false;
1180 }
jeffhao10037c82012-01-23 15:06:23 -08001181 break;
1182 }
1183 case DexFile::kDexTypeAnnotationSetRefList: {
Andreas Gamped4ae41f2014-09-02 11:17:34 -07001184 if (!CheckList(sizeof(DexFile::AnnotationSetRefItem), "annotation_set_ref_list", &ptr_)) {
jeffhao10037c82012-01-23 15:06:23 -08001185 return false;
1186 }
jeffhao10037c82012-01-23 15:06:23 -08001187 break;
1188 }
1189 case DexFile::kDexTypeAnnotationSetItem: {
Andreas Gamped4ae41f2014-09-02 11:17:34 -07001190 if (!CheckList(sizeof(uint32_t), "annotation_set_item", &ptr_)) {
jeffhao10037c82012-01-23 15:06:23 -08001191 return false;
1192 }
jeffhao10037c82012-01-23 15:06:23 -08001193 break;
1194 }
1195 case DexFile::kDexTypeClassDataItem: {
1196 if (!CheckIntraClassDataItem()) {
1197 return false;
1198 }
1199 break;
1200 }
1201 case DexFile::kDexTypeCodeItem: {
1202 if (!CheckIntraCodeItem()) {
1203 return false;
1204 }
1205 break;
1206 }
1207 case DexFile::kDexTypeStringDataItem: {
1208 if (!CheckIntraStringDataItem()) {
1209 return false;
1210 }
1211 break;
1212 }
1213 case DexFile::kDexTypeDebugInfoItem: {
1214 if (!CheckIntraDebugInfoItem()) {
1215 return false;
1216 }
1217 break;
1218 }
1219 case DexFile::kDexTypeAnnotationItem: {
1220 if (!CheckIntraAnnotationItem()) {
1221 return false;
1222 }
1223 break;
1224 }
1225 case DexFile::kDexTypeEncodedArrayItem: {
1226 if (!CheckEncodedArray()) {
1227 return false;
1228 }
1229 break;
1230 }
1231 case DexFile::kDexTypeAnnotationsDirectoryItem: {
1232 if (!CheckIntraAnnotationsDirectoryItem()) {
1233 return false;
1234 }
1235 break;
1236 }
1237 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001238 ErrorStringPrintf("Unknown map item type %x", type);
jeffhao10037c82012-01-23 15:06:23 -08001239 return false;
1240 }
1241
1242 if (IsDataSectionType(type)) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001243 offset_to_type_map_.Put(aligned_offset, type);
jeffhao10037c82012-01-23 15:06:23 -08001244 }
1245
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001246 aligned_offset = ptr_ - begin_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001247 if (UNLIKELY(aligned_offset > size_)) {
1248 ErrorStringPrintf("Item %d at ends out of bounds", i);
jeffhao10037c82012-01-23 15:06:23 -08001249 return false;
1250 }
1251
1252 offset = aligned_offset;
1253 }
1254
1255 return true;
1256}
1257
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001258bool DexFileVerifier::CheckIntraIdSection(size_t offset, uint32_t count, uint16_t type) {
jeffhao10037c82012-01-23 15:06:23 -08001259 uint32_t expected_offset;
1260 uint32_t expected_size;
1261
1262 // Get the expected offset and size from the header.
1263 switch (type) {
1264 case DexFile::kDexTypeStringIdItem:
1265 expected_offset = header_->string_ids_off_;
1266 expected_size = header_->string_ids_size_;
1267 break;
1268 case DexFile::kDexTypeTypeIdItem:
1269 expected_offset = header_->type_ids_off_;
1270 expected_size = header_->type_ids_size_;
1271 break;
1272 case DexFile::kDexTypeProtoIdItem:
1273 expected_offset = header_->proto_ids_off_;
1274 expected_size = header_->proto_ids_size_;
1275 break;
1276 case DexFile::kDexTypeFieldIdItem:
1277 expected_offset = header_->field_ids_off_;
1278 expected_size = header_->field_ids_size_;
1279 break;
1280 case DexFile::kDexTypeMethodIdItem:
1281 expected_offset = header_->method_ids_off_;
1282 expected_size = header_->method_ids_size_;
1283 break;
1284 case DexFile::kDexTypeClassDefItem:
1285 expected_offset = header_->class_defs_off_;
1286 expected_size = header_->class_defs_size_;
1287 break;
1288 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001289 ErrorStringPrintf("Bad type for id section: %x", type);
jeffhao10037c82012-01-23 15:06:23 -08001290 return false;
1291 }
1292
1293 // Check that the offset and size are what were expected from the header.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001294 if (UNLIKELY(offset != expected_offset)) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001295 ErrorStringPrintf("Bad offset for section: got %zx, expected %x", offset, expected_offset);
jeffhao10037c82012-01-23 15:06:23 -08001296 return false;
1297 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001298 if (UNLIKELY(count != expected_size)) {
1299 ErrorStringPrintf("Bad size for section: got %x, expected %x", count, expected_size);
jeffhao10037c82012-01-23 15:06:23 -08001300 return false;
1301 }
1302
1303 return CheckIntraSectionIterate(offset, count, type);
1304}
1305
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001306bool DexFileVerifier::CheckIntraDataSection(size_t offset, uint32_t count, uint16_t type) {
1307 size_t data_start = header_->data_off_;
1308 size_t data_end = data_start + header_->data_size_;
jeffhao10037c82012-01-23 15:06:23 -08001309
1310 // Sanity check the offset of the section.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001311 if (UNLIKELY((offset < data_start) || (offset > data_end))) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001312 ErrorStringPrintf("Bad offset for data subsection: %zx", offset);
jeffhao10037c82012-01-23 15:06:23 -08001313 return false;
1314 }
1315
1316 if (!CheckIntraSectionIterate(offset, count, type)) {
1317 return false;
1318 }
1319
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001320 size_t next_offset = ptr_ - begin_;
jeffhao10037c82012-01-23 15:06:23 -08001321 if (next_offset > data_end) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001322 ErrorStringPrintf("Out-of-bounds end of data subsection: %zx", next_offset);
jeffhao10037c82012-01-23 15:06:23 -08001323 return false;
1324 }
1325
1326 return true;
1327}
1328
1329bool DexFileVerifier::CheckIntraSection() {
Ian Rogers30fab402012-01-23 15:43:46 -08001330 const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ + header_->map_off_);
jeffhao10037c82012-01-23 15:06:23 -08001331 const DexFile::MapItem* item = map->list_;
1332
1333 uint32_t count = map->size_;
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001334 size_t offset = 0;
Ian Rogers30fab402012-01-23 15:43:46 -08001335 ptr_ = begin_;
jeffhao10037c82012-01-23 15:06:23 -08001336
1337 // Check the items listed in the map.
1338 while (count--) {
1339 uint32_t section_offset = item->offset_;
1340 uint32_t section_count = item->size_;
1341 uint16_t type = item->type_;
1342
1343 // Check for padding and overlap between items.
1344 if (!CheckPadding(offset, section_offset)) {
1345 return false;
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001346 } else if (UNLIKELY(offset > section_offset)) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001347 ErrorStringPrintf("Section overlap or out-of-order map: %zx, %x", offset, section_offset);
jeffhao10037c82012-01-23 15:06:23 -08001348 return false;
1349 }
1350
1351 // Check each item based on its type.
1352 switch (type) {
1353 case DexFile::kDexTypeHeaderItem:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001354 if (UNLIKELY(section_count != 1)) {
1355 ErrorStringPrintf("Multiple header items");
jeffhao10037c82012-01-23 15:06:23 -08001356 return false;
1357 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001358 if (UNLIKELY(section_offset != 0)) {
1359 ErrorStringPrintf("Header at %x, not at start of file", section_offset);
jeffhao10037c82012-01-23 15:06:23 -08001360 return false;
1361 }
Ian Rogers30fab402012-01-23 15:43:46 -08001362 ptr_ = begin_ + header_->header_size_;
jeffhao10037c82012-01-23 15:06:23 -08001363 offset = header_->header_size_;
1364 break;
1365 case DexFile::kDexTypeStringIdItem:
1366 case DexFile::kDexTypeTypeIdItem:
1367 case DexFile::kDexTypeProtoIdItem:
1368 case DexFile::kDexTypeFieldIdItem:
1369 case DexFile::kDexTypeMethodIdItem:
1370 case DexFile::kDexTypeClassDefItem:
1371 if (!CheckIntraIdSection(section_offset, section_count, type)) {
1372 return false;
1373 }
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001374 offset = ptr_ - begin_;
jeffhao10037c82012-01-23 15:06:23 -08001375 break;
1376 case DexFile::kDexTypeMapList:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001377 if (UNLIKELY(section_count != 1)) {
1378 ErrorStringPrintf("Multiple map list items");
jeffhao10037c82012-01-23 15:06:23 -08001379 return false;
1380 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001381 if (UNLIKELY(section_offset != header_->map_off_)) {
1382 ErrorStringPrintf("Map not at header-defined offset: %x, expected %x",
1383 section_offset, header_->map_off_);
jeffhao10037c82012-01-23 15:06:23 -08001384 return false;
1385 }
1386 ptr_ += sizeof(uint32_t) + (map->size_ * sizeof(DexFile::MapItem));
1387 offset = section_offset + sizeof(uint32_t) + (map->size_ * sizeof(DexFile::MapItem));
1388 break;
1389 case DexFile::kDexTypeTypeList:
1390 case DexFile::kDexTypeAnnotationSetRefList:
1391 case DexFile::kDexTypeAnnotationSetItem:
1392 case DexFile::kDexTypeClassDataItem:
1393 case DexFile::kDexTypeCodeItem:
1394 case DexFile::kDexTypeStringDataItem:
1395 case DexFile::kDexTypeDebugInfoItem:
1396 case DexFile::kDexTypeAnnotationItem:
1397 case DexFile::kDexTypeEncodedArrayItem:
1398 case DexFile::kDexTypeAnnotationsDirectoryItem:
1399 if (!CheckIntraDataSection(section_offset, section_count, type)) {
1400 return false;
1401 }
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001402 offset = ptr_ - begin_;
jeffhao10037c82012-01-23 15:06:23 -08001403 break;
1404 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001405 ErrorStringPrintf("Unknown map item type %x", type);
jeffhao10037c82012-01-23 15:06:23 -08001406 return false;
1407 }
1408
1409 item++;
1410 }
1411
1412 return true;
1413}
1414
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001415bool DexFileVerifier::CheckOffsetToTypeMap(size_t offset, uint16_t type) {
Mathieu Chartier02e25112013-08-14 16:14:24 -07001416 auto it = offset_to_type_map_.find(offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001417 if (UNLIKELY(it == offset_to_type_map_.end())) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001418 ErrorStringPrintf("No data map entry found @ %zx; expected %x", offset, type);
jeffhao10037c82012-01-23 15:06:23 -08001419 return false;
1420 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001421 if (UNLIKELY(it->second != type)) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001422 ErrorStringPrintf("Unexpected data map entry @ %zx; expected %x, found %x",
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001423 offset, type, it->second);
jeffhao10037c82012-01-23 15:06:23 -08001424 return false;
1425 }
1426 return true;
1427}
1428
Ian Rogers13735952014-10-08 12:43:28 -07001429uint16_t DexFileVerifier::FindFirstClassDataDefiner(const uint8_t* ptr, bool* success) {
jeffhao10037c82012-01-23 15:06:23 -08001430 ClassDataItemIterator it(*dex_file_, ptr);
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001431 *success = true;
jeffhao10037c82012-01-23 15:06:23 -08001432
1433 if (it.HasNextStaticField() || it.HasNextInstanceField()) {
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001434 LOAD_FIELD(field, it.GetMemberIndex(), "first_class_data_definer field_id",
1435 *success = false; return DexFile::kDexNoIndex16)
Andreas Gampee09269c2014-06-06 18:45:35 -07001436 return field->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001437 }
1438
1439 if (it.HasNextDirectMethod() || it.HasNextVirtualMethod()) {
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001440 LOAD_METHOD(method, it.GetMemberIndex(), "first_class_data_definer method_id",
1441 *success = false; return DexFile::kDexNoIndex16)
Andreas Gampee09269c2014-06-06 18:45:35 -07001442 return method->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001443 }
1444
1445 return DexFile::kDexNoIndex16;
1446}
1447
Ian Rogers13735952014-10-08 12:43:28 -07001448uint16_t DexFileVerifier::FindFirstAnnotationsDirectoryDefiner(const uint8_t* ptr, bool* success) {
jeffhao10037c82012-01-23 15:06:23 -08001449 const DexFile::AnnotationsDirectoryItem* item =
1450 reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr);
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001451 *success = true;
1452
jeffhao10037c82012-01-23 15:06:23 -08001453 if (item->fields_size_ != 0) {
1454 DexFile::FieldAnnotationsItem* field_items = (DexFile::FieldAnnotationsItem*) (item + 1);
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001455 LOAD_FIELD(field, field_items[0].field_idx_, "first_annotations_dir_definer field_id",
1456 *success = false; return DexFile::kDexNoIndex16)
Andreas Gampee09269c2014-06-06 18:45:35 -07001457 return field->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001458 }
1459
1460 if (item->methods_size_ != 0) {
1461 DexFile::MethodAnnotationsItem* method_items = (DexFile::MethodAnnotationsItem*) (item + 1);
Andreas Gampee09269c2014-06-06 18:45:35 -07001462 LOAD_METHOD(method, method_items[0].method_idx_, "first_annotations_dir_definer method id",
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001463 *success = false; return DexFile::kDexNoIndex16)
Andreas Gampee09269c2014-06-06 18:45:35 -07001464 return method->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001465 }
1466
1467 if (item->parameters_size_ != 0) {
1468 DexFile::ParameterAnnotationsItem* parameter_items = (DexFile::ParameterAnnotationsItem*) (item + 1);
Andreas Gampee09269c2014-06-06 18:45:35 -07001469 LOAD_METHOD(method, parameter_items[0].method_idx_, "first_annotations_dir_definer method id",
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001470 *success = false; return DexFile::kDexNoIndex16)
Andreas Gampee09269c2014-06-06 18:45:35 -07001471 return method->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001472 }
1473
1474 return DexFile::kDexNoIndex16;
1475}
1476
1477bool DexFileVerifier::CheckInterStringIdItem() {
1478 const DexFile::StringId* item = reinterpret_cast<const DexFile::StringId*>(ptr_);
1479
1480 // Check the map to make sure it has the right offset->type.
1481 if (!CheckOffsetToTypeMap(item->string_data_off_, DexFile::kDexTypeStringDataItem)) {
1482 return false;
1483 }
1484
1485 // Check ordering between items.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001486 if (previous_item_ != nullptr) {
jeffhao10037c82012-01-23 15:06:23 -08001487 const DexFile::StringId* prev_item = reinterpret_cast<const DexFile::StringId*>(previous_item_);
1488 const char* prev_str = dex_file_->GetStringData(*prev_item);
1489 const char* str = dex_file_->GetStringData(*item);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001490 if (UNLIKELY(CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(prev_str, str) >= 0)) {
1491 ErrorStringPrintf("Out-of-order string_ids: '%s' then '%s'", prev_str, str);
jeffhao10037c82012-01-23 15:06:23 -08001492 return false;
1493 }
1494 }
1495
1496 ptr_ += sizeof(DexFile::StringId);
1497 return true;
1498}
1499
1500bool DexFileVerifier::CheckInterTypeIdItem() {
1501 const DexFile::TypeId* item = reinterpret_cast<const DexFile::TypeId*>(ptr_);
Andreas Gampee09269c2014-06-06 18:45:35 -07001502
1503 LOAD_STRING(descriptor, item->descriptor_idx_, "inter_type_id_item descriptor_idx")
jeffhao10037c82012-01-23 15:06:23 -08001504
1505 // Check that the descriptor is a valid type.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001506 if (UNLIKELY(!IsValidDescriptor(descriptor))) {
1507 ErrorStringPrintf("Invalid type descriptor: '%s'", descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001508 return false;
1509 }
1510
1511 // Check ordering between items.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001512 if (previous_item_ != nullptr) {
jeffhao10037c82012-01-23 15:06:23 -08001513 const DexFile::TypeId* prev_item = reinterpret_cast<const DexFile::TypeId*>(previous_item_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001514 if (UNLIKELY(prev_item->descriptor_idx_ >= item->descriptor_idx_)) {
1515 ErrorStringPrintf("Out-of-order type_ids: %x then %x",
1516 prev_item->descriptor_idx_, item->descriptor_idx_);
jeffhao10037c82012-01-23 15:06:23 -08001517 return false;
1518 }
1519 }
1520
1521 ptr_ += sizeof(DexFile::TypeId);
1522 return true;
1523}
1524
1525bool DexFileVerifier::CheckInterProtoIdItem() {
1526 const DexFile::ProtoId* item = reinterpret_cast<const DexFile::ProtoId*>(ptr_);
Andreas Gampee09269c2014-06-06 18:45:35 -07001527
1528 LOAD_STRING(shorty, item->shorty_idx_, "inter_proto_id_item shorty_idx")
1529
jeffhao10037c82012-01-23 15:06:23 -08001530 if (item->parameters_off_ != 0 &&
1531 !CheckOffsetToTypeMap(item->parameters_off_, DexFile::kDexTypeTypeList)) {
1532 return false;
1533 }
1534
1535 // Check the return type and advance the shorty.
Andreas Gampee09269c2014-06-06 18:45:35 -07001536 LOAD_STRING_BY_TYPE(return_type, item->return_type_idx_, "inter_proto_id_item return_type_idx")
1537 if (!CheckShortyDescriptorMatch(*shorty, return_type, true)) {
jeffhao10037c82012-01-23 15:06:23 -08001538 return false;
1539 }
1540 shorty++;
1541
1542 DexFileParameterIterator it(*dex_file_, *item);
1543 while (it.HasNext() && *shorty != '\0') {
Andreas Gampebb836e12014-06-13 15:31:40 -07001544 if (!CheckIndex(it.GetTypeIdx(), dex_file_->NumTypeIds(),
1545 "inter_proto_id_item shorty type_idx")) {
1546 return false;
1547 }
jeffhao10037c82012-01-23 15:06:23 -08001548 const char* descriptor = it.GetDescriptor();
1549 if (!CheckShortyDescriptorMatch(*shorty, descriptor, false)) {
1550 return false;
1551 }
1552 it.Next();
1553 shorty++;
1554 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001555 if (UNLIKELY(it.HasNext() || *shorty != '\0')) {
1556 ErrorStringPrintf("Mismatched length for parameters and shorty");
jeffhao10037c82012-01-23 15:06:23 -08001557 return false;
1558 }
1559
1560 // Check ordering between items. This relies on type_ids being in order.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001561 if (previous_item_ != nullptr) {
jeffhao10037c82012-01-23 15:06:23 -08001562 const DexFile::ProtoId* prev = reinterpret_cast<const DexFile::ProtoId*>(previous_item_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001563 if (UNLIKELY(prev->return_type_idx_ > item->return_type_idx_)) {
1564 ErrorStringPrintf("Out-of-order proto_id return types");
jeffhao10037c82012-01-23 15:06:23 -08001565 return false;
1566 } else if (prev->return_type_idx_ == item->return_type_idx_) {
1567 DexFileParameterIterator curr_it(*dex_file_, *item);
1568 DexFileParameterIterator prev_it(*dex_file_, *prev);
1569
1570 while (curr_it.HasNext() && prev_it.HasNext()) {
1571 uint16_t prev_idx = prev_it.GetTypeIdx();
1572 uint16_t curr_idx = curr_it.GetTypeIdx();
1573 if (prev_idx == DexFile::kDexNoIndex16) {
1574 break;
1575 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001576 if (UNLIKELY(curr_idx == DexFile::kDexNoIndex16)) {
1577 ErrorStringPrintf("Out-of-order proto_id arguments");
jeffhao10037c82012-01-23 15:06:23 -08001578 return false;
1579 }
1580
1581 if (prev_idx < curr_idx) {
1582 break;
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001583 } else if (UNLIKELY(prev_idx > curr_idx)) {
1584 ErrorStringPrintf("Out-of-order proto_id arguments");
jeffhao10037c82012-01-23 15:06:23 -08001585 return false;
1586 }
1587
1588 prev_it.Next();
1589 curr_it.Next();
1590 }
1591 }
1592 }
1593
1594 ptr_ += sizeof(DexFile::ProtoId);
1595 return true;
1596}
1597
1598bool DexFileVerifier::CheckInterFieldIdItem() {
1599 const DexFile::FieldId* item = reinterpret_cast<const DexFile::FieldId*>(ptr_);
1600
1601 // Check that the class descriptor is valid.
Andreas Gampee09269c2014-06-06 18:45:35 -07001602 LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_field_id_item class_idx")
1603 if (UNLIKELY(!IsValidDescriptor(class_descriptor) || class_descriptor[0] != 'L')) {
1604 ErrorStringPrintf("Invalid descriptor for class_idx: '%s'", class_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001605 return false;
1606 }
1607
1608 // Check that the type descriptor is a valid field name.
Andreas Gampee09269c2014-06-06 18:45:35 -07001609 LOAD_STRING_BY_TYPE(type_descriptor, item->type_idx_, "inter_field_id_item type_idx")
1610 if (UNLIKELY(!IsValidDescriptor(type_descriptor) || type_descriptor[0] == 'V')) {
1611 ErrorStringPrintf("Invalid descriptor for type_idx: '%s'", type_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001612 return false;
1613 }
1614
1615 // Check that the name is valid.
Andreas Gampee09269c2014-06-06 18:45:35 -07001616 LOAD_STRING(descriptor, item->name_idx_, "inter_field_id_item name_idx")
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001617 if (UNLIKELY(!IsValidMemberName(descriptor))) {
1618 ErrorStringPrintf("Invalid field name: '%s'", descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001619 return false;
1620 }
1621
1622 // Check ordering between items. This relies on the other sections being in order.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001623 if (previous_item_ != nullptr) {
jeffhao10037c82012-01-23 15:06:23 -08001624 const DexFile::FieldId* prev_item = reinterpret_cast<const DexFile::FieldId*>(previous_item_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001625 if (UNLIKELY(prev_item->class_idx_ > item->class_idx_)) {
1626 ErrorStringPrintf("Out-of-order field_ids");
jeffhao10037c82012-01-23 15:06:23 -08001627 return false;
1628 } else if (prev_item->class_idx_ == item->class_idx_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001629 if (UNLIKELY(prev_item->name_idx_ > item->name_idx_)) {
1630 ErrorStringPrintf("Out-of-order field_ids");
jeffhao10037c82012-01-23 15:06:23 -08001631 return false;
1632 } else if (prev_item->name_idx_ == item->name_idx_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001633 if (UNLIKELY(prev_item->type_idx_ >= item->type_idx_)) {
1634 ErrorStringPrintf("Out-of-order field_ids");
jeffhao10037c82012-01-23 15:06:23 -08001635 return false;
1636 }
1637 }
1638 }
1639 }
1640
1641 ptr_ += sizeof(DexFile::FieldId);
1642 return true;
1643}
1644
1645bool DexFileVerifier::CheckInterMethodIdItem() {
1646 const DexFile::MethodId* item = reinterpret_cast<const DexFile::MethodId*>(ptr_);
1647
1648 // Check that the class descriptor is a valid reference name.
Andreas Gampee09269c2014-06-06 18:45:35 -07001649 LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_method_id_item class_idx")
1650 if (UNLIKELY(!IsValidDescriptor(class_descriptor) || (class_descriptor[0] != 'L' &&
1651 class_descriptor[0] != '['))) {
1652 ErrorStringPrintf("Invalid descriptor for class_idx: '%s'", class_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001653 return false;
1654 }
1655
1656 // Check that the name is valid.
Andreas Gampedf10b322014-06-11 21:46:05 -07001657 LOAD_STRING(descriptor, item->name_idx_, "inter_method_id_item name_idx")
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001658 if (UNLIKELY(!IsValidMemberName(descriptor))) {
1659 ErrorStringPrintf("Invalid method name: '%s'", descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001660 return false;
1661 }
1662
Andreas Gampedf10b322014-06-11 21:46:05 -07001663 // Check that the proto id is valid.
1664 if (UNLIKELY(!CheckIndex(item->proto_idx_, dex_file_->NumProtoIds(),
1665 "inter_method_id_item proto_idx"))) {
1666 return false;
1667 }
1668
jeffhao10037c82012-01-23 15:06:23 -08001669 // Check ordering between items. This relies on the other sections being in order.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001670 if (previous_item_ != nullptr) {
jeffhao10037c82012-01-23 15:06:23 -08001671 const DexFile::MethodId* prev_item = reinterpret_cast<const DexFile::MethodId*>(previous_item_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001672 if (UNLIKELY(prev_item->class_idx_ > item->class_idx_)) {
1673 ErrorStringPrintf("Out-of-order method_ids");
jeffhao10037c82012-01-23 15:06:23 -08001674 return false;
1675 } else if (prev_item->class_idx_ == item->class_idx_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001676 if (UNLIKELY(prev_item->name_idx_ > item->name_idx_)) {
1677 ErrorStringPrintf("Out-of-order method_ids");
jeffhao10037c82012-01-23 15:06:23 -08001678 return false;
1679 } else if (prev_item->name_idx_ == item->name_idx_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001680 if (UNLIKELY(prev_item->proto_idx_ >= item->proto_idx_)) {
1681 ErrorStringPrintf("Out-of-order method_ids");
jeffhao10037c82012-01-23 15:06:23 -08001682 return false;
1683 }
1684 }
1685 }
1686 }
1687
1688 ptr_ += sizeof(DexFile::MethodId);
1689 return true;
1690}
1691
1692bool DexFileVerifier::CheckInterClassDefItem() {
1693 const DexFile::ClassDef* item = reinterpret_cast<const DexFile::ClassDef*>(ptr_);
jeffhao10037c82012-01-23 15:06:23 -08001694
Andreas Gampe0ba238d2014-07-29 01:22:07 -07001695 // Check for duplicate class def.
1696 if (defined_classes_.find(item->class_idx_) != defined_classes_.end()) {
1697 ErrorStringPrintf("Redefinition of class with type idx: '%d'", item->class_idx_);
1698 return false;
1699 }
1700 defined_classes_.insert(item->class_idx_);
1701
Andreas Gampee09269c2014-06-06 18:45:35 -07001702 LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_class_def_item class_idx")
1703 if (UNLIKELY(!IsValidDescriptor(class_descriptor) || class_descriptor[0] != 'L')) {
1704 ErrorStringPrintf("Invalid class descriptor: '%s'", class_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001705 return false;
1706 }
1707
Andreas Gampeacc2bb62014-07-17 19:26:50 -07001708 // Only allow non-runtime modifiers.
1709 if ((item->access_flags_ & ~kAccJavaFlagsMask) != 0) {
1710 ErrorStringPrintf("Invalid class flags: '%d'", item->access_flags_);
1711 return false;
1712 }
1713
jeffhao10037c82012-01-23 15:06:23 -08001714 if (item->interfaces_off_ != 0 &&
1715 !CheckOffsetToTypeMap(item->interfaces_off_, DexFile::kDexTypeTypeList)) {
1716 return false;
1717 }
1718 if (item->annotations_off_ != 0 &&
1719 !CheckOffsetToTypeMap(item->annotations_off_, DexFile::kDexTypeAnnotationsDirectoryItem)) {
1720 return false;
1721 }
1722 if (item->class_data_off_ != 0 &&
1723 !CheckOffsetToTypeMap(item->class_data_off_, DexFile::kDexTypeClassDataItem)) {
1724 return false;
1725 }
1726 if (item->static_values_off_ != 0 &&
1727 !CheckOffsetToTypeMap(item->static_values_off_, DexFile::kDexTypeEncodedArrayItem)) {
1728 return false;
1729 }
1730
1731 if (item->superclass_idx_ != DexFile::kDexNoIndex16) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001732 LOAD_STRING_BY_TYPE(superclass_descriptor, item->superclass_idx_,
1733 "inter_class_def_item superclass_idx")
1734 if (UNLIKELY(!IsValidDescriptor(superclass_descriptor) || superclass_descriptor[0] != 'L')) {
1735 ErrorStringPrintf("Invalid superclass: '%s'", superclass_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001736 return false;
1737 }
1738 }
1739
1740 const DexFile::TypeList* interfaces = dex_file_->GetInterfacesList(*item);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001741 if (interfaces != nullptr) {
jeffhao10037c82012-01-23 15:06:23 -08001742 uint32_t size = interfaces->Size();
1743
1744 // Ensure that all interfaces refer to classes (not arrays or primitives).
1745 for (uint32_t i = 0; i < size; i++) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001746 LOAD_STRING_BY_TYPE(inf_descriptor, interfaces->GetTypeItem(i).type_idx_,
1747 "inter_class_def_item interface type_idx")
1748 if (UNLIKELY(!IsValidDescriptor(inf_descriptor) || inf_descriptor[0] != 'L')) {
1749 ErrorStringPrintf("Invalid interface: '%s'", inf_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001750 return false;
1751 }
1752 }
1753
1754 /*
1755 * Ensure that there are no duplicates. This is an O(N^2) test, but in
1756 * practice the number of interfaces implemented by any given class is low.
1757 */
1758 for (uint32_t i = 1; i < size; i++) {
1759 uint32_t idx1 = interfaces->GetTypeItem(i).type_idx_;
1760 for (uint32_t j =0; j < i; j++) {
1761 uint32_t idx2 = interfaces->GetTypeItem(j).type_idx_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001762 if (UNLIKELY(idx1 == idx2)) {
1763 ErrorStringPrintf("Duplicate interface: '%s'", dex_file_->StringByTypeIdx(idx1));
jeffhao10037c82012-01-23 15:06:23 -08001764 return false;
1765 }
1766 }
1767 }
1768 }
1769
1770 // Check that references in class_data_item are to the right class.
1771 if (item->class_data_off_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07001772 const uint8_t* data = begin_ + item->class_data_off_;
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001773 bool success;
1774 uint16_t data_definer = FindFirstClassDataDefiner(data, &success);
1775 if (!success) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001776 return false;
1777 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001778 if (UNLIKELY((data_definer != item->class_idx_) && (data_definer != DexFile::kDexNoIndex16))) {
1779 ErrorStringPrintf("Invalid class_data_item");
jeffhao10037c82012-01-23 15:06:23 -08001780 return false;
1781 }
1782 }
1783
1784 // Check that references in annotations_directory_item are to right class.
1785 if (item->annotations_off_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07001786 const uint8_t* data = begin_ + item->annotations_off_;
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001787 bool success;
1788 uint16_t annotations_definer = FindFirstAnnotationsDirectoryDefiner(data, &success);
1789 if (!success) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001790 return false;
1791 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001792 if (UNLIKELY((annotations_definer != item->class_idx_) &&
1793 (annotations_definer != DexFile::kDexNoIndex16))) {
1794 ErrorStringPrintf("Invalid annotations_directory_item");
jeffhao10037c82012-01-23 15:06:23 -08001795 return false;
1796 }
1797 }
1798
1799 ptr_ += sizeof(DexFile::ClassDef);
1800 return true;
1801}
1802
1803bool DexFileVerifier::CheckInterAnnotationSetRefList() {
1804 const DexFile::AnnotationSetRefList* list =
1805 reinterpret_cast<const DexFile::AnnotationSetRefList*>(ptr_);
1806 const DexFile::AnnotationSetRefItem* item = list->list_;
1807 uint32_t count = list->size_;
1808
1809 while (count--) {
1810 if (item->annotations_off_ != 0 &&
1811 !CheckOffsetToTypeMap(item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
1812 return false;
1813 }
1814 item++;
1815 }
1816
Ian Rogers13735952014-10-08 12:43:28 -07001817 ptr_ = reinterpret_cast<const uint8_t*>(item);
jeffhao10037c82012-01-23 15:06:23 -08001818 return true;
1819}
1820
1821bool DexFileVerifier::CheckInterAnnotationSetItem() {
1822 const DexFile::AnnotationSetItem* set = reinterpret_cast<const DexFile::AnnotationSetItem*>(ptr_);
1823 const uint32_t* offsets = set->entries_;
1824 uint32_t count = set->size_;
1825 uint32_t last_idx = 0;
1826
1827 for (uint32_t i = 0; i < count; i++) {
1828 if (*offsets != 0 && !CheckOffsetToTypeMap(*offsets, DexFile::kDexTypeAnnotationItem)) {
1829 return false;
1830 }
1831
1832 // Get the annotation from the offset and the type index for the annotation.
1833 const DexFile::AnnotationItem* annotation =
Ian Rogers30fab402012-01-23 15:43:46 -08001834 reinterpret_cast<const DexFile::AnnotationItem*>(begin_ + *offsets);
jeffhao10037c82012-01-23 15:06:23 -08001835 const uint8_t* data = annotation->annotation_;
1836 uint32_t idx = DecodeUnsignedLeb128(&data);
1837
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001838 if (UNLIKELY(last_idx >= idx && i != 0)) {
1839 ErrorStringPrintf("Out-of-order entry types: %x then %x", last_idx, idx);
jeffhao10037c82012-01-23 15:06:23 -08001840 return false;
1841 }
1842
1843 last_idx = idx;
1844 offsets++;
1845 }
1846
Ian Rogers13735952014-10-08 12:43:28 -07001847 ptr_ = reinterpret_cast<const uint8_t*>(offsets);
jeffhao10037c82012-01-23 15:06:23 -08001848 return true;
1849}
1850
1851bool DexFileVerifier::CheckInterClassDataItem() {
1852 ClassDataItemIterator it(*dex_file_, ptr_);
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001853 bool success;
1854 uint16_t defining_class = FindFirstClassDataDefiner(ptr_, &success);
1855 if (!success) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001856 return false;
1857 }
jeffhao10037c82012-01-23 15:06:23 -08001858
1859 for (; it.HasNextStaticField() || it.HasNextInstanceField(); it.Next()) {
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001860 LOAD_FIELD(field, it.GetMemberIndex(), "inter_class_data_item field_id", return false)
Andreas Gampee09269c2014-06-06 18:45:35 -07001861 if (UNLIKELY(field->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001862 ErrorStringPrintf("Mismatched defining class for class_data_item field");
jeffhao10037c82012-01-23 15:06:23 -08001863 return false;
1864 }
1865 }
1866 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
1867 uint32_t code_off = it.GetMethodCodeItemOffset();
1868 if (code_off != 0 && !CheckOffsetToTypeMap(code_off, DexFile::kDexTypeCodeItem)) {
1869 return false;
1870 }
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001871 LOAD_METHOD(method, it.GetMemberIndex(), "inter_class_data_item method_id", return false)
Andreas Gampee09269c2014-06-06 18:45:35 -07001872 if (UNLIKELY(method->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001873 ErrorStringPrintf("Mismatched defining class for class_data_item method");
jeffhao10037c82012-01-23 15:06:23 -08001874 return false;
1875 }
1876 }
1877
1878 ptr_ = it.EndDataPointer();
1879 return true;
1880}
1881
1882bool DexFileVerifier::CheckInterAnnotationsDirectoryItem() {
1883 const DexFile::AnnotationsDirectoryItem* item =
1884 reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr_);
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001885 bool success;
1886 uint16_t defining_class = FindFirstAnnotationsDirectoryDefiner(ptr_, &success);
1887 if (!success) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001888 return false;
1889 }
jeffhao10037c82012-01-23 15:06:23 -08001890
1891 if (item->class_annotations_off_ != 0 &&
1892 !CheckOffsetToTypeMap(item->class_annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
1893 return false;
1894 }
1895
1896 // Field annotations follow immediately after the annotations directory.
1897 const DexFile::FieldAnnotationsItem* field_item =
1898 reinterpret_cast<const DexFile::FieldAnnotationsItem*>(item + 1);
1899 uint32_t field_count = item->fields_size_;
1900 for (uint32_t i = 0; i < field_count; i++) {
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001901 LOAD_FIELD(field, field_item->field_idx_, "inter_annotations_directory_item field_id",
1902 return false)
Andreas Gampee09269c2014-06-06 18:45:35 -07001903 if (UNLIKELY(field->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001904 ErrorStringPrintf("Mismatched defining class for field_annotation");
jeffhao10037c82012-01-23 15:06:23 -08001905 return false;
1906 }
1907 if (!CheckOffsetToTypeMap(field_item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
1908 return false;
1909 }
1910 field_item++;
1911 }
1912
1913 // Method annotations follow immediately after field annotations.
1914 const DexFile::MethodAnnotationsItem* method_item =
1915 reinterpret_cast<const DexFile::MethodAnnotationsItem*>(field_item);
1916 uint32_t method_count = item->methods_size_;
1917 for (uint32_t i = 0; i < method_count; i++) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001918 LOAD_METHOD(method, method_item->method_idx_, "inter_annotations_directory_item method_id",
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001919 return false)
Andreas Gampee09269c2014-06-06 18:45:35 -07001920 if (UNLIKELY(method->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001921 ErrorStringPrintf("Mismatched defining class for method_annotation");
jeffhao10037c82012-01-23 15:06:23 -08001922 return false;
1923 }
1924 if (!CheckOffsetToTypeMap(method_item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
1925 return false;
1926 }
1927 method_item++;
1928 }
1929
1930 // Parameter annotations follow immediately after method annotations.
1931 const DexFile::ParameterAnnotationsItem* parameter_item =
1932 reinterpret_cast<const DexFile::ParameterAnnotationsItem*>(method_item);
1933 uint32_t parameter_count = item->parameters_size_;
1934 for (uint32_t i = 0; i < parameter_count; i++) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001935 LOAD_METHOD(parameter_method, parameter_item->method_idx_,
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001936 "inter_annotations_directory_item parameter method_id", return false)
Andreas Gampee09269c2014-06-06 18:45:35 -07001937 if (UNLIKELY(parameter_method->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001938 ErrorStringPrintf("Mismatched defining class for parameter_annotation");
jeffhao10037c82012-01-23 15:06:23 -08001939 return false;
1940 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001941 if (!CheckOffsetToTypeMap(parameter_item->annotations_off_,
1942 DexFile::kDexTypeAnnotationSetRefList)) {
jeffhao10037c82012-01-23 15:06:23 -08001943 return false;
1944 }
1945 parameter_item++;
1946 }
1947
Ian Rogers13735952014-10-08 12:43:28 -07001948 ptr_ = reinterpret_cast<const uint8_t*>(parameter_item);
jeffhao10037c82012-01-23 15:06:23 -08001949 return true;
1950}
1951
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001952bool DexFileVerifier::CheckInterSectionIterate(size_t offset, uint32_t count, uint16_t type) {
jeffhao10037c82012-01-23 15:06:23 -08001953 // Get the right alignment mask for the type of section.
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001954 size_t alignment_mask;
jeffhao10037c82012-01-23 15:06:23 -08001955 switch (type) {
1956 case DexFile::kDexTypeClassDataItem:
1957 alignment_mask = sizeof(uint8_t) - 1;
1958 break;
1959 default:
1960 alignment_mask = sizeof(uint32_t) - 1;
1961 break;
1962 }
1963
1964 // Iterate through the items in the section.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001965 previous_item_ = nullptr;
jeffhao10037c82012-01-23 15:06:23 -08001966 for (uint32_t i = 0; i < count; i++) {
1967 uint32_t new_offset = (offset + alignment_mask) & ~alignment_mask;
Ian Rogers30fab402012-01-23 15:43:46 -08001968 ptr_ = begin_ + new_offset;
Ian Rogers13735952014-10-08 12:43:28 -07001969 const uint8_t* prev_ptr = ptr_;
jeffhao10037c82012-01-23 15:06:23 -08001970
1971 // Check depending on the section type.
1972 switch (type) {
1973 case DexFile::kDexTypeStringIdItem: {
1974 if (!CheckInterStringIdItem()) {
1975 return false;
1976 }
1977 break;
1978 }
1979 case DexFile::kDexTypeTypeIdItem: {
1980 if (!CheckInterTypeIdItem()) {
1981 return false;
1982 }
1983 break;
1984 }
1985 case DexFile::kDexTypeProtoIdItem: {
1986 if (!CheckInterProtoIdItem()) {
1987 return false;
1988 }
1989 break;
1990 }
1991 case DexFile::kDexTypeFieldIdItem: {
1992 if (!CheckInterFieldIdItem()) {
1993 return false;
1994 }
1995 break;
1996 }
1997 case DexFile::kDexTypeMethodIdItem: {
1998 if (!CheckInterMethodIdItem()) {
1999 return false;
2000 }
2001 break;
2002 }
2003 case DexFile::kDexTypeClassDefItem: {
2004 if (!CheckInterClassDefItem()) {
2005 return false;
2006 }
2007 break;
2008 }
2009 case DexFile::kDexTypeAnnotationSetRefList: {
2010 if (!CheckInterAnnotationSetRefList()) {
2011 return false;
2012 }
2013 break;
2014 }
2015 case DexFile::kDexTypeAnnotationSetItem: {
2016 if (!CheckInterAnnotationSetItem()) {
2017 return false;
2018 }
2019 break;
2020 }
2021 case DexFile::kDexTypeClassDataItem: {
2022 if (!CheckInterClassDataItem()) {
2023 return false;
2024 }
2025 break;
2026 }
2027 case DexFile::kDexTypeAnnotationsDirectoryItem: {
2028 if (!CheckInterAnnotationsDirectoryItem()) {
2029 return false;
2030 }
2031 break;
2032 }
2033 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07002034 ErrorStringPrintf("Unknown map item type %x", type);
jeffhao10037c82012-01-23 15:06:23 -08002035 return false;
2036 }
2037
2038 previous_item_ = prev_ptr;
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08002039 offset = ptr_ - begin_;
jeffhao10037c82012-01-23 15:06:23 -08002040 }
2041
2042 return true;
2043}
2044
2045bool DexFileVerifier::CheckInterSection() {
Ian Rogers30fab402012-01-23 15:43:46 -08002046 const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ + header_->map_off_);
jeffhao10037c82012-01-23 15:06:23 -08002047 const DexFile::MapItem* item = map->list_;
2048 uint32_t count = map->size_;
2049
2050 // Cross check the items listed in the map.
2051 while (count--) {
2052 uint32_t section_offset = item->offset_;
2053 uint32_t section_count = item->size_;
2054 uint16_t type = item->type_;
2055
2056 switch (type) {
2057 case DexFile::kDexTypeHeaderItem:
2058 case DexFile::kDexTypeMapList:
2059 case DexFile::kDexTypeTypeList:
2060 case DexFile::kDexTypeCodeItem:
2061 case DexFile::kDexTypeStringDataItem:
2062 case DexFile::kDexTypeDebugInfoItem:
2063 case DexFile::kDexTypeAnnotationItem:
2064 case DexFile::kDexTypeEncodedArrayItem:
2065 break;
2066 case DexFile::kDexTypeStringIdItem:
2067 case DexFile::kDexTypeTypeIdItem:
2068 case DexFile::kDexTypeProtoIdItem:
2069 case DexFile::kDexTypeFieldIdItem:
2070 case DexFile::kDexTypeMethodIdItem:
2071 case DexFile::kDexTypeClassDefItem:
2072 case DexFile::kDexTypeAnnotationSetRefList:
2073 case DexFile::kDexTypeAnnotationSetItem:
2074 case DexFile::kDexTypeClassDataItem:
2075 case DexFile::kDexTypeAnnotationsDirectoryItem: {
2076 if (!CheckInterSectionIterate(section_offset, section_count, type)) {
2077 return false;
2078 }
2079 break;
2080 }
2081 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07002082 ErrorStringPrintf("Unknown map item type %x", type);
jeffhao10037c82012-01-23 15:06:23 -08002083 return false;
2084 }
2085
2086 item++;
2087 }
2088
2089 return true;
2090}
2091
2092bool DexFileVerifier::Verify() {
2093 // Check the header.
2094 if (!CheckHeader()) {
2095 return false;
2096 }
2097
2098 // Check the map section.
2099 if (!CheckMap()) {
2100 return false;
2101 }
2102
2103 // Check structure within remaining sections.
2104 if (!CheckIntraSection()) {
2105 return false;
2106 }
2107
2108 // Check references from one section to another.
2109 if (!CheckInterSection()) {
2110 return false;
2111 }
2112
2113 return true;
2114}
2115
Ian Rogers8d31bbd2013-10-13 10:44:14 -07002116void DexFileVerifier::ErrorStringPrintf(const char* fmt, ...) {
2117 va_list ap;
2118 va_start(ap, fmt);
2119 DCHECK(failure_reason_.empty()) << failure_reason_;
2120 failure_reason_ = StringPrintf("Failure to verify dex file '%s': ", location_);
2121 StringAppendV(&failure_reason_, fmt, ap);
2122 va_end(ap);
2123}
2124
jeffhao10037c82012-01-23 15:06:23 -08002125} // namespace art