blob: eec49839ef5f599b456531dfecc198db1b776bab [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.
Jeff Haoec969232015-06-22 20:20:30 -0700697 uint32_t prev_index = 0;
jeffhao10037c82012-01-23 15:06:23 -0800698 for (; it.HasNextStaticField(); it.Next()) {
Jeff Haoec969232015-06-22 20:20:30 -0700699 uint32_t curr_index = it.GetMemberIndex();
700 if (curr_index < prev_index) {
701 ErrorStringPrintf("out-of-order static field indexes %d and %d", prev_index, curr_index);
702 return false;
703 }
704 prev_index = curr_index;
705 if (!CheckClassDataItemField(curr_index, it.GetRawMemberAccessFlags(), true)) {
jeffhao10037c82012-01-23 15:06:23 -0800706 return false;
707 }
708 }
Jeff Haoec969232015-06-22 20:20:30 -0700709 prev_index = 0;
jeffhao10037c82012-01-23 15:06:23 -0800710 for (; it.HasNextInstanceField(); it.Next()) {
Jeff Haoec969232015-06-22 20:20:30 -0700711 uint32_t curr_index = it.GetMemberIndex();
712 if (curr_index < prev_index) {
713 ErrorStringPrintf("out-of-order instance field indexes %d and %d", prev_index, curr_index);
714 return false;
715 }
716 prev_index = curr_index;
717 if (!CheckClassDataItemField(curr_index, it.GetRawMemberAccessFlags(), false)) {
jeffhao10037c82012-01-23 15:06:23 -0800718 return false;
719 }
720 }
Jeff Haoec969232015-06-22 20:20:30 -0700721 prev_index = 0;
jeffhao10037c82012-01-23 15:06:23 -0800722 for (; it.HasNextDirectMethod(); it.Next()) {
Jeff Haoec969232015-06-22 20:20:30 -0700723 uint32_t curr_index = it.GetMemberIndex();
724 if (curr_index < prev_index) {
725 ErrorStringPrintf("out-of-order direct method indexes %d and %d", prev_index, curr_index);
726 return false;
727 }
728 prev_index = curr_index;
729 if (!CheckClassDataItemMethod(curr_index, it.GetRawMemberAccessFlags(),
Jeff Haoa574b0e2015-06-04 18:12:26 -0700730 it.GetMethodCodeItemOffset(), direct_method_indexes, true)) {
jeffhao10037c82012-01-23 15:06:23 -0800731 return false;
732 }
733 }
Jeff Haoec969232015-06-22 20:20:30 -0700734 prev_index = 0;
jeffhao10037c82012-01-23 15:06:23 -0800735 for (; it.HasNextVirtualMethod(); it.Next()) {
Jeff Haoec969232015-06-22 20:20:30 -0700736 uint32_t curr_index = it.GetMemberIndex();
737 if (curr_index < prev_index) {
738 ErrorStringPrintf("out-of-order virtual method indexes %d and %d", prev_index, curr_index);
739 return false;
740 }
741 prev_index = curr_index;
742 if (!CheckClassDataItemMethod(curr_index, it.GetRawMemberAccessFlags(),
Jeff Haoa574b0e2015-06-04 18:12:26 -0700743 it.GetMethodCodeItemOffset(), direct_method_indexes, false)) {
jeffhao10037c82012-01-23 15:06:23 -0800744 return false;
745 }
746 }
747
748 ptr_ = it.EndDataPointer();
749 return true;
750}
751
752bool DexFileVerifier::CheckIntraCodeItem() {
753 const DexFile::CodeItem* code_item = reinterpret_cast<const DexFile::CodeItem*>(ptr_);
Andreas Gampe50d1bc12014-07-17 21:49:24 -0700754 if (!CheckListSize(code_item, 1, sizeof(DexFile::CodeItem), "code")) {
jeffhao10037c82012-01-23 15:06:23 -0800755 return false;
756 }
757
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700758 if (UNLIKELY(code_item->ins_size_ > code_item->registers_size_)) {
759 ErrorStringPrintf("ins_size (%ud) > registers_size (%ud)",
760 code_item->ins_size_, code_item->registers_size_);
jeffhao10037c82012-01-23 15:06:23 -0800761 return false;
762 }
763
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700764 if (UNLIKELY((code_item->outs_size_ > 5) &&
765 (code_item->outs_size_ > code_item->registers_size_))) {
jeffhao10037c82012-01-23 15:06:23 -0800766 /*
767 * outs_size can be up to 5, even if registers_size is smaller, since the
768 * short forms of method invocation allow repetitions of a register multiple
769 * times within a single parameter list. However, longer parameter lists
770 * need to be represented in-order in the register file.
771 */
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700772 ErrorStringPrintf("outs_size (%ud) > registers_size (%ud)",
773 code_item->outs_size_, code_item->registers_size_);
jeffhao10037c82012-01-23 15:06:23 -0800774 return false;
775 }
776
777 const uint16_t* insns = code_item->insns_;
778 uint32_t insns_size = code_item->insns_size_in_code_units_;
779 if (!CheckListSize(insns, insns_size, sizeof(uint16_t), "insns size")) {
780 return false;
781 }
782
783 // Grab the end of the insns if there are no try_items.
784 uint32_t try_items_size = code_item->tries_size_;
785 if (try_items_size == 0) {
Ian Rogers13735952014-10-08 12:43:28 -0700786 ptr_ = reinterpret_cast<const uint8_t*>(&insns[insns_size]);
jeffhao10037c82012-01-23 15:06:23 -0800787 return true;
788 }
789
790 // try_items are 4-byte aligned. Verify the spacer is 0.
Ian Rogers8a6bbfc2014-01-23 13:29:07 -0800791 if (((reinterpret_cast<uintptr_t>(&insns[insns_size]) & 3) != 0) && (insns[insns_size] != 0)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700792 ErrorStringPrintf("Non-zero padding: %x", insns[insns_size]);
jeffhao10037c82012-01-23 15:06:23 -0800793 return false;
794 }
795
796 const DexFile::TryItem* try_items = DexFile::GetTryItems(*code_item, 0);
jeffhao10037c82012-01-23 15:06:23 -0800797 if (!CheckListSize(try_items, try_items_size, sizeof(DexFile::TryItem), "try_items size")) {
798 return false;
799 }
800
Anestis Bechtsoudis6a8df532015-07-12 12:51:35 -0500801 ptr_ = DexFile::GetCatchHandlerData(*code_item, 0);
802 uint32_t handlers_size = DecodeUnsignedLeb128(&ptr_);
803
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700804 if (UNLIKELY((handlers_size == 0) || (handlers_size >= 65536))) {
805 ErrorStringPrintf("Invalid handlers_size: %ud", handlers_size);
jeffhao10037c82012-01-23 15:06:23 -0800806 return false;
807 }
808
Ian Rogers700a4022014-05-19 16:49:03 -0700809 std::unique_ptr<uint32_t[]> handler_offsets(new uint32_t[handlers_size]);
Elliott Hughesee0fa762012-03-26 17:12:41 -0700810 if (!CheckAndGetHandlerOffsets(code_item, &handler_offsets[0], handlers_size)) {
jeffhao10037c82012-01-23 15:06:23 -0800811 return false;
812 }
813
814 uint32_t last_addr = 0;
815 while (try_items_size--) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700816 if (UNLIKELY(try_items->start_addr_ < last_addr)) {
817 ErrorStringPrintf("Out-of_order try_item with start_addr: %x", try_items->start_addr_);
jeffhao10037c82012-01-23 15:06:23 -0800818 return false;
819 }
820
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700821 if (UNLIKELY(try_items->start_addr_ >= insns_size)) {
822 ErrorStringPrintf("Invalid try_item start_addr: %x", try_items->start_addr_);
jeffhao10037c82012-01-23 15:06:23 -0800823 return false;
824 }
825
826 uint32_t i;
827 for (i = 0; i < handlers_size; i++) {
828 if (try_items->handler_off_ == handler_offsets[i]) {
829 break;
830 }
831 }
832
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700833 if (UNLIKELY(i == handlers_size)) {
834 ErrorStringPrintf("Bogus handler offset: %x", try_items->handler_off_);
jeffhao10037c82012-01-23 15:06:23 -0800835 return false;
836 }
837
838 last_addr = try_items->start_addr_ + try_items->insn_count_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700839 if (UNLIKELY(last_addr > insns_size)) {
840 ErrorStringPrintf("Invalid try_item insn_count: %x", try_items->insn_count_);
jeffhao10037c82012-01-23 15:06:23 -0800841 return false;
842 }
843
844 try_items++;
845 }
846
847 return true;
848}
849
850bool DexFileVerifier::CheckIntraStringDataItem() {
851 uint32_t size = DecodeUnsignedLeb128(&ptr_);
Ian Rogers13735952014-10-08 12:43:28 -0700852 const uint8_t* file_end = begin_ + size_;
jeffhao10037c82012-01-23 15:06:23 -0800853
854 for (uint32_t i = 0; i < size; i++) {
Brian Carlstromc6475642014-05-27 11:14:12 -0700855 CHECK_LT(i, size); // b/15014252 Prevents hitting the impossible case below
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700856 if (UNLIKELY(ptr_ >= file_end)) {
857 ErrorStringPrintf("String data would go beyond end-of-file");
jeffhao10037c82012-01-23 15:06:23 -0800858 return false;
859 }
860
861 uint8_t byte = *(ptr_++);
862
863 // Switch on the high 4 bits.
864 switch (byte >> 4) {
865 case 0x00:
866 // Special case of bit pattern 0xxx.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700867 if (UNLIKELY(byte == 0)) {
Brian Carlstromc6475642014-05-27 11:14:12 -0700868 CHECK_LT(i, size); // b/15014252 Actually hit this impossible case with clang
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700869 ErrorStringPrintf("String data shorter than indicated utf16_size %x", size);
jeffhao10037c82012-01-23 15:06:23 -0800870 return false;
871 }
872 break;
873 case 0x01:
874 case 0x02:
875 case 0x03:
876 case 0x04:
877 case 0x05:
878 case 0x06:
879 case 0x07:
880 // No extra checks necessary for bit pattern 0xxx.
881 break;
882 case 0x08:
883 case 0x09:
884 case 0x0a:
885 case 0x0b:
886 case 0x0f:
887 // Illegal bit patterns 10xx or 1111.
888 // Note: 1111 is valid for normal UTF-8, but not here.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700889 ErrorStringPrintf("Illegal start byte %x in string data", byte);
jeffhao10037c82012-01-23 15:06:23 -0800890 return false;
891 case 0x0c:
892 case 0x0d: {
893 // Bit pattern 110x has an additional byte.
894 uint8_t byte2 = *(ptr_++);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700895 if (UNLIKELY((byte2 & 0xc0) != 0x80)) {
896 ErrorStringPrintf("Illegal continuation byte %x in string data", byte2);
jeffhao10037c82012-01-23 15:06:23 -0800897 return false;
898 }
899 uint16_t value = ((byte & 0x1f) << 6) | (byte2 & 0x3f);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700900 if (UNLIKELY((value != 0) && (value < 0x80))) {
901 ErrorStringPrintf("Illegal representation for value %x in string data", value);
jeffhao10037c82012-01-23 15:06:23 -0800902 return false;
903 }
904 break;
905 }
906 case 0x0e: {
907 // Bit pattern 1110 has 2 additional bytes.
908 uint8_t byte2 = *(ptr_++);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700909 if (UNLIKELY((byte2 & 0xc0) != 0x80)) {
910 ErrorStringPrintf("Illegal continuation byte %x in string data", byte2);
jeffhao10037c82012-01-23 15:06:23 -0800911 return false;
912 }
913 uint8_t byte3 = *(ptr_++);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700914 if (UNLIKELY((byte3 & 0xc0) != 0x80)) {
915 ErrorStringPrintf("Illegal continuation byte %x in string data", byte3);
jeffhao10037c82012-01-23 15:06:23 -0800916 return false;
917 }
918 uint16_t value = ((byte & 0x0f) << 12) | ((byte2 & 0x3f) << 6) | (byte3 & 0x3f);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700919 if (UNLIKELY(value < 0x800)) {
920 ErrorStringPrintf("Illegal representation for value %x in string data", value);
jeffhao10037c82012-01-23 15:06:23 -0800921 return false;
922 }
923 break;
924 }
925 }
926 }
927
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700928 if (UNLIKELY(*(ptr_++) != '\0')) {
929 ErrorStringPrintf("String longer than indicated size %x", size);
jeffhao10037c82012-01-23 15:06:23 -0800930 return false;
931 }
932
933 return true;
934}
935
936bool DexFileVerifier::CheckIntraDebugInfoItem() {
937 DecodeUnsignedLeb128(&ptr_);
938 uint32_t parameters_size = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700939 if (UNLIKELY(parameters_size > 65536)) {
940 ErrorStringPrintf("Invalid parameters_size: %x", parameters_size);
jeffhao10037c82012-01-23 15:06:23 -0800941 return false;
942 }
943
944 for (uint32_t j = 0; j < parameters_size; j++) {
945 uint32_t parameter_name = DecodeUnsignedLeb128(&ptr_);
946 if (parameter_name != 0) {
947 parameter_name--;
948 if (!CheckIndex(parameter_name, header_->string_ids_size_, "debug_info_item parameter_name")) {
949 return false;
950 }
951 }
952 }
953
954 while (true) {
955 uint8_t opcode = *(ptr_++);
956 switch (opcode) {
957 case DexFile::DBG_END_SEQUENCE: {
958 return true;
959 }
960 case DexFile::DBG_ADVANCE_PC: {
961 DecodeUnsignedLeb128(&ptr_);
962 break;
963 }
964 case DexFile::DBG_ADVANCE_LINE: {
965 DecodeSignedLeb128(&ptr_);
966 break;
967 }
968 case DexFile::DBG_START_LOCAL: {
969 uint32_t reg_num = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700970 if (UNLIKELY(reg_num >= 65536)) {
971 ErrorStringPrintf("Bad reg_num for opcode %x", opcode);
jeffhao10037c82012-01-23 15:06:23 -0800972 return false;
973 }
974 uint32_t name_idx = DecodeUnsignedLeb128(&ptr_);
975 if (name_idx != 0) {
976 name_idx--;
977 if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_START_LOCAL name_idx")) {
978 return false;
979 }
980 }
981 uint32_t type_idx = DecodeUnsignedLeb128(&ptr_);
982 if (type_idx != 0) {
983 type_idx--;
Logan Chiendd3208d2015-04-19 23:27:52 +0800984 if (!CheckIndex(type_idx, header_->type_ids_size_, "DBG_START_LOCAL type_idx")) {
jeffhao10037c82012-01-23 15:06:23 -0800985 return false;
986 }
987 }
988 break;
989 }
990 case DexFile::DBG_END_LOCAL:
991 case DexFile::DBG_RESTART_LOCAL: {
992 uint32_t reg_num = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700993 if (UNLIKELY(reg_num >= 65536)) {
994 ErrorStringPrintf("Bad reg_num for opcode %x", opcode);
jeffhao10037c82012-01-23 15:06:23 -0800995 return false;
996 }
997 break;
998 }
999 case DexFile::DBG_START_LOCAL_EXTENDED: {
1000 uint32_t reg_num = DecodeUnsignedLeb128(&ptr_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001001 if (UNLIKELY(reg_num >= 65536)) {
1002 ErrorStringPrintf("Bad reg_num for opcode %x", opcode);
jeffhao10037c82012-01-23 15:06:23 -08001003 return false;
1004 }
1005 uint32_t name_idx = DecodeUnsignedLeb128(&ptr_);
1006 if (name_idx != 0) {
1007 name_idx--;
1008 if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED name_idx")) {
1009 return false;
1010 }
1011 }
1012 uint32_t type_idx = DecodeUnsignedLeb128(&ptr_);
1013 if (type_idx != 0) {
1014 type_idx--;
Logan Chiendd3208d2015-04-19 23:27:52 +08001015 if (!CheckIndex(type_idx, header_->type_ids_size_, "DBG_START_LOCAL_EXTENDED type_idx")) {
jeffhao10037c82012-01-23 15:06:23 -08001016 return false;
1017 }
1018 }
1019 uint32_t sig_idx = DecodeUnsignedLeb128(&ptr_);
1020 if (sig_idx != 0) {
1021 sig_idx--;
1022 if (!CheckIndex(sig_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED sig_idx")) {
1023 return false;
1024 }
1025 }
1026 break;
1027 }
1028 case DexFile::DBG_SET_FILE: {
1029 uint32_t name_idx = DecodeUnsignedLeb128(&ptr_);
1030 if (name_idx != 0) {
1031 name_idx--;
1032 if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_SET_FILE name_idx")) {
1033 return false;
1034 }
1035 }
1036 break;
1037 }
1038 }
1039 }
1040}
1041
1042bool DexFileVerifier::CheckIntraAnnotationItem() {
Ian Rogers13735952014-10-08 12:43:28 -07001043 if (!CheckListSize(ptr_, 1, sizeof(uint8_t), "annotation visibility")) {
jeffhao10037c82012-01-23 15:06:23 -08001044 return false;
1045 }
1046
1047 // Check visibility
1048 switch (*(ptr_++)) {
1049 case DexFile::kDexVisibilityBuild:
1050 case DexFile::kDexVisibilityRuntime:
1051 case DexFile::kDexVisibilitySystem:
1052 break;
1053 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001054 ErrorStringPrintf("Bad annotation visibility: %x", *ptr_);
jeffhao10037c82012-01-23 15:06:23 -08001055 return false;
1056 }
1057
1058 if (!CheckEncodedAnnotation()) {
1059 return false;
1060 }
1061
1062 return true;
1063}
1064
1065bool DexFileVerifier::CheckIntraAnnotationsDirectoryItem() {
1066 const DexFile::AnnotationsDirectoryItem* item =
1067 reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr_);
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001068 if (!CheckListSize(item, 1, sizeof(DexFile::AnnotationsDirectoryItem), "annotations_directory")) {
jeffhao10037c82012-01-23 15:06:23 -08001069 return false;
1070 }
1071
1072 // Field annotations follow immediately after the annotations directory.
1073 const DexFile::FieldAnnotationsItem* field_item =
1074 reinterpret_cast<const DexFile::FieldAnnotationsItem*>(item + 1);
1075 uint32_t field_count = item->fields_size_;
1076 if (!CheckListSize(field_item, field_count, sizeof(DexFile::FieldAnnotationsItem), "field_annotations list")) {
1077 return false;
1078 }
1079
1080 uint32_t last_idx = 0;
1081 for (uint32_t i = 0; i < field_count; i++) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001082 if (UNLIKELY(last_idx >= field_item->field_idx_ && i != 0)) {
1083 ErrorStringPrintf("Out-of-order field_idx for annotation: %x then %x", last_idx, field_item->field_idx_);
jeffhao10037c82012-01-23 15:06:23 -08001084 return false;
1085 }
1086 last_idx = field_item->field_idx_;
1087 field_item++;
1088 }
1089
1090 // Method annotations follow immediately after field annotations.
1091 const DexFile::MethodAnnotationsItem* method_item =
1092 reinterpret_cast<const DexFile::MethodAnnotationsItem*>(field_item);
1093 uint32_t method_count = item->methods_size_;
1094 if (!CheckListSize(method_item, method_count, sizeof(DexFile::MethodAnnotationsItem), "method_annotations list")) {
1095 return false;
1096 }
1097
1098 last_idx = 0;
1099 for (uint32_t i = 0; i < method_count; i++) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001100 if (UNLIKELY(last_idx >= method_item->method_idx_ && i != 0)) {
1101 ErrorStringPrintf("Out-of-order method_idx for annotation: %x then %x",
1102 last_idx, method_item->method_idx_);
jeffhao10037c82012-01-23 15:06:23 -08001103 return false;
1104 }
1105 last_idx = method_item->method_idx_;
1106 method_item++;
1107 }
1108
1109 // Parameter annotations follow immediately after method annotations.
1110 const DexFile::ParameterAnnotationsItem* parameter_item =
1111 reinterpret_cast<const DexFile::ParameterAnnotationsItem*>(method_item);
1112 uint32_t parameter_count = item->parameters_size_;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001113 if (!CheckListSize(parameter_item, parameter_count, sizeof(DexFile::ParameterAnnotationsItem),
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001114 "parameter_annotations list")) {
jeffhao10037c82012-01-23 15:06:23 -08001115 return false;
1116 }
1117
1118 last_idx = 0;
1119 for (uint32_t i = 0; i < parameter_count; i++) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001120 if (UNLIKELY(last_idx >= parameter_item->method_idx_ && i != 0)) {
1121 ErrorStringPrintf("Out-of-order method_idx for annotation: %x then %x",
1122 last_idx, parameter_item->method_idx_);
jeffhao10037c82012-01-23 15:06:23 -08001123 return false;
1124 }
1125 last_idx = parameter_item->method_idx_;
1126 parameter_item++;
1127 }
1128
1129 // Return a pointer to the end of the annotations.
Ian Rogers13735952014-10-08 12:43:28 -07001130 ptr_ = reinterpret_cast<const uint8_t*>(parameter_item);
jeffhao10037c82012-01-23 15:06:23 -08001131 return true;
1132}
1133
Andreas Gampeb061cc12014-09-02 10:22:20 -07001134bool DexFileVerifier::CheckIntraSectionIterate(size_t offset, uint32_t section_count,
1135 uint16_t type) {
jeffhao10037c82012-01-23 15:06:23 -08001136 // Get the right alignment mask for the type of section.
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001137 size_t alignment_mask;
jeffhao10037c82012-01-23 15:06:23 -08001138 switch (type) {
1139 case DexFile::kDexTypeClassDataItem:
1140 case DexFile::kDexTypeStringDataItem:
1141 case DexFile::kDexTypeDebugInfoItem:
1142 case DexFile::kDexTypeAnnotationItem:
1143 case DexFile::kDexTypeEncodedArrayItem:
1144 alignment_mask = sizeof(uint8_t) - 1;
1145 break;
1146 default:
1147 alignment_mask = sizeof(uint32_t) - 1;
1148 break;
1149 }
1150
1151 // Iterate through the items in the section.
Andreas Gampeb061cc12014-09-02 10:22:20 -07001152 for (uint32_t i = 0; i < section_count; i++) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001153 size_t aligned_offset = (offset + alignment_mask) & ~alignment_mask;
jeffhao10037c82012-01-23 15:06:23 -08001154
1155 // Check the padding between items.
1156 if (!CheckPadding(offset, aligned_offset)) {
1157 return false;
1158 }
1159
1160 // Check depending on the section type.
1161 switch (type) {
1162 case DexFile::kDexTypeStringIdItem: {
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001163 if (!CheckListSize(ptr_, 1, sizeof(DexFile::StringId), "string_ids")) {
jeffhao10037c82012-01-23 15:06:23 -08001164 return false;
1165 }
1166 ptr_ += sizeof(DexFile::StringId);
1167 break;
1168 }
1169 case DexFile::kDexTypeTypeIdItem: {
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001170 if (!CheckListSize(ptr_, 1, sizeof(DexFile::TypeId), "type_ids")) {
jeffhao10037c82012-01-23 15:06:23 -08001171 return false;
1172 }
1173 ptr_ += sizeof(DexFile::TypeId);
1174 break;
1175 }
1176 case DexFile::kDexTypeProtoIdItem: {
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001177 if (!CheckListSize(ptr_, 1, sizeof(DexFile::ProtoId), "proto_ids")) {
jeffhao10037c82012-01-23 15:06:23 -08001178 return false;
1179 }
1180 ptr_ += sizeof(DexFile::ProtoId);
1181 break;
1182 }
1183 case DexFile::kDexTypeFieldIdItem: {
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001184 if (!CheckListSize(ptr_, 1, sizeof(DexFile::FieldId), "field_ids")) {
jeffhao10037c82012-01-23 15:06:23 -08001185 return false;
1186 }
1187 ptr_ += sizeof(DexFile::FieldId);
1188 break;
1189 }
1190 case DexFile::kDexTypeMethodIdItem: {
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001191 if (!CheckListSize(ptr_, 1, sizeof(DexFile::MethodId), "method_ids")) {
jeffhao10037c82012-01-23 15:06:23 -08001192 return false;
1193 }
1194 ptr_ += sizeof(DexFile::MethodId);
1195 break;
1196 }
1197 case DexFile::kDexTypeClassDefItem: {
Andreas Gampe50d1bc12014-07-17 21:49:24 -07001198 if (!CheckListSize(ptr_, 1, sizeof(DexFile::ClassDef), "class_defs")) {
jeffhao10037c82012-01-23 15:06:23 -08001199 return false;
1200 }
1201 ptr_ += sizeof(DexFile::ClassDef);
1202 break;
1203 }
1204 case DexFile::kDexTypeTypeList: {
Andreas Gamped4ae41f2014-09-02 11:17:34 -07001205 if (!CheckList(sizeof(DexFile::TypeItem), "type_list", &ptr_)) {
jeffhao10037c82012-01-23 15:06:23 -08001206 return false;
1207 }
jeffhao10037c82012-01-23 15:06:23 -08001208 break;
1209 }
1210 case DexFile::kDexTypeAnnotationSetRefList: {
Andreas Gamped4ae41f2014-09-02 11:17:34 -07001211 if (!CheckList(sizeof(DexFile::AnnotationSetRefItem), "annotation_set_ref_list", &ptr_)) {
jeffhao10037c82012-01-23 15:06:23 -08001212 return false;
1213 }
jeffhao10037c82012-01-23 15:06:23 -08001214 break;
1215 }
1216 case DexFile::kDexTypeAnnotationSetItem: {
Andreas Gamped4ae41f2014-09-02 11:17:34 -07001217 if (!CheckList(sizeof(uint32_t), "annotation_set_item", &ptr_)) {
jeffhao10037c82012-01-23 15:06:23 -08001218 return false;
1219 }
jeffhao10037c82012-01-23 15:06:23 -08001220 break;
1221 }
1222 case DexFile::kDexTypeClassDataItem: {
1223 if (!CheckIntraClassDataItem()) {
1224 return false;
1225 }
1226 break;
1227 }
1228 case DexFile::kDexTypeCodeItem: {
1229 if (!CheckIntraCodeItem()) {
1230 return false;
1231 }
1232 break;
1233 }
1234 case DexFile::kDexTypeStringDataItem: {
1235 if (!CheckIntraStringDataItem()) {
1236 return false;
1237 }
1238 break;
1239 }
1240 case DexFile::kDexTypeDebugInfoItem: {
1241 if (!CheckIntraDebugInfoItem()) {
1242 return false;
1243 }
1244 break;
1245 }
1246 case DexFile::kDexTypeAnnotationItem: {
1247 if (!CheckIntraAnnotationItem()) {
1248 return false;
1249 }
1250 break;
1251 }
1252 case DexFile::kDexTypeEncodedArrayItem: {
1253 if (!CheckEncodedArray()) {
1254 return false;
1255 }
1256 break;
1257 }
1258 case DexFile::kDexTypeAnnotationsDirectoryItem: {
1259 if (!CheckIntraAnnotationsDirectoryItem()) {
1260 return false;
1261 }
1262 break;
1263 }
1264 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001265 ErrorStringPrintf("Unknown map item type %x", type);
jeffhao10037c82012-01-23 15:06:23 -08001266 return false;
1267 }
1268
1269 if (IsDataSectionType(type)) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001270 offset_to_type_map_.Put(aligned_offset, type);
jeffhao10037c82012-01-23 15:06:23 -08001271 }
1272
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001273 aligned_offset = ptr_ - begin_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001274 if (UNLIKELY(aligned_offset > size_)) {
1275 ErrorStringPrintf("Item %d at ends out of bounds", i);
jeffhao10037c82012-01-23 15:06:23 -08001276 return false;
1277 }
1278
1279 offset = aligned_offset;
1280 }
1281
1282 return true;
1283}
1284
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001285bool DexFileVerifier::CheckIntraIdSection(size_t offset, uint32_t count, uint16_t type) {
jeffhao10037c82012-01-23 15:06:23 -08001286 uint32_t expected_offset;
1287 uint32_t expected_size;
1288
1289 // Get the expected offset and size from the header.
1290 switch (type) {
1291 case DexFile::kDexTypeStringIdItem:
1292 expected_offset = header_->string_ids_off_;
1293 expected_size = header_->string_ids_size_;
1294 break;
1295 case DexFile::kDexTypeTypeIdItem:
1296 expected_offset = header_->type_ids_off_;
1297 expected_size = header_->type_ids_size_;
1298 break;
1299 case DexFile::kDexTypeProtoIdItem:
1300 expected_offset = header_->proto_ids_off_;
1301 expected_size = header_->proto_ids_size_;
1302 break;
1303 case DexFile::kDexTypeFieldIdItem:
1304 expected_offset = header_->field_ids_off_;
1305 expected_size = header_->field_ids_size_;
1306 break;
1307 case DexFile::kDexTypeMethodIdItem:
1308 expected_offset = header_->method_ids_off_;
1309 expected_size = header_->method_ids_size_;
1310 break;
1311 case DexFile::kDexTypeClassDefItem:
1312 expected_offset = header_->class_defs_off_;
1313 expected_size = header_->class_defs_size_;
1314 break;
1315 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001316 ErrorStringPrintf("Bad type for id section: %x", type);
jeffhao10037c82012-01-23 15:06:23 -08001317 return false;
1318 }
1319
1320 // Check that the offset and size are what were expected from the header.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001321 if (UNLIKELY(offset != expected_offset)) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001322 ErrorStringPrintf("Bad offset for section: got %zx, expected %x", offset, expected_offset);
jeffhao10037c82012-01-23 15:06:23 -08001323 return false;
1324 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001325 if (UNLIKELY(count != expected_size)) {
1326 ErrorStringPrintf("Bad size for section: got %x, expected %x", count, expected_size);
jeffhao10037c82012-01-23 15:06:23 -08001327 return false;
1328 }
1329
1330 return CheckIntraSectionIterate(offset, count, type);
1331}
1332
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001333bool DexFileVerifier::CheckIntraDataSection(size_t offset, uint32_t count, uint16_t type) {
1334 size_t data_start = header_->data_off_;
1335 size_t data_end = data_start + header_->data_size_;
jeffhao10037c82012-01-23 15:06:23 -08001336
1337 // Sanity check the offset of the section.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001338 if (UNLIKELY((offset < data_start) || (offset > data_end))) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001339 ErrorStringPrintf("Bad offset for data subsection: %zx", offset);
jeffhao10037c82012-01-23 15:06:23 -08001340 return false;
1341 }
1342
1343 if (!CheckIntraSectionIterate(offset, count, type)) {
1344 return false;
1345 }
1346
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001347 size_t next_offset = ptr_ - begin_;
jeffhao10037c82012-01-23 15:06:23 -08001348 if (next_offset > data_end) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001349 ErrorStringPrintf("Out-of-bounds end of data subsection: %zx", next_offset);
jeffhao10037c82012-01-23 15:06:23 -08001350 return false;
1351 }
1352
1353 return true;
1354}
1355
1356bool DexFileVerifier::CheckIntraSection() {
Ian Rogers30fab402012-01-23 15:43:46 -08001357 const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ + header_->map_off_);
jeffhao10037c82012-01-23 15:06:23 -08001358 const DexFile::MapItem* item = map->list_;
1359
1360 uint32_t count = map->size_;
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001361 size_t offset = 0;
Ian Rogers30fab402012-01-23 15:43:46 -08001362 ptr_ = begin_;
jeffhao10037c82012-01-23 15:06:23 -08001363
1364 // Check the items listed in the map.
1365 while (count--) {
1366 uint32_t section_offset = item->offset_;
1367 uint32_t section_count = item->size_;
1368 uint16_t type = item->type_;
1369
1370 // Check for padding and overlap between items.
1371 if (!CheckPadding(offset, section_offset)) {
1372 return false;
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001373 } else if (UNLIKELY(offset > section_offset)) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001374 ErrorStringPrintf("Section overlap or out-of-order map: %zx, %x", offset, section_offset);
jeffhao10037c82012-01-23 15:06:23 -08001375 return false;
1376 }
1377
1378 // Check each item based on its type.
1379 switch (type) {
1380 case DexFile::kDexTypeHeaderItem:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001381 if (UNLIKELY(section_count != 1)) {
1382 ErrorStringPrintf("Multiple header items");
jeffhao10037c82012-01-23 15:06:23 -08001383 return false;
1384 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001385 if (UNLIKELY(section_offset != 0)) {
1386 ErrorStringPrintf("Header at %x, not at start of file", section_offset);
jeffhao10037c82012-01-23 15:06:23 -08001387 return false;
1388 }
Ian Rogers30fab402012-01-23 15:43:46 -08001389 ptr_ = begin_ + header_->header_size_;
jeffhao10037c82012-01-23 15:06:23 -08001390 offset = header_->header_size_;
1391 break;
1392 case DexFile::kDexTypeStringIdItem:
1393 case DexFile::kDexTypeTypeIdItem:
1394 case DexFile::kDexTypeProtoIdItem:
1395 case DexFile::kDexTypeFieldIdItem:
1396 case DexFile::kDexTypeMethodIdItem:
1397 case DexFile::kDexTypeClassDefItem:
1398 if (!CheckIntraIdSection(section_offset, section_count, type)) {
1399 return false;
1400 }
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001401 offset = ptr_ - begin_;
jeffhao10037c82012-01-23 15:06:23 -08001402 break;
1403 case DexFile::kDexTypeMapList:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001404 if (UNLIKELY(section_count != 1)) {
1405 ErrorStringPrintf("Multiple map list items");
jeffhao10037c82012-01-23 15:06:23 -08001406 return false;
1407 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001408 if (UNLIKELY(section_offset != header_->map_off_)) {
1409 ErrorStringPrintf("Map not at header-defined offset: %x, expected %x",
1410 section_offset, header_->map_off_);
jeffhao10037c82012-01-23 15:06:23 -08001411 return false;
1412 }
1413 ptr_ += sizeof(uint32_t) + (map->size_ * sizeof(DexFile::MapItem));
1414 offset = section_offset + sizeof(uint32_t) + (map->size_ * sizeof(DexFile::MapItem));
1415 break;
1416 case DexFile::kDexTypeTypeList:
1417 case DexFile::kDexTypeAnnotationSetRefList:
1418 case DexFile::kDexTypeAnnotationSetItem:
1419 case DexFile::kDexTypeClassDataItem:
1420 case DexFile::kDexTypeCodeItem:
1421 case DexFile::kDexTypeStringDataItem:
1422 case DexFile::kDexTypeDebugInfoItem:
1423 case DexFile::kDexTypeAnnotationItem:
1424 case DexFile::kDexTypeEncodedArrayItem:
1425 case DexFile::kDexTypeAnnotationsDirectoryItem:
1426 if (!CheckIntraDataSection(section_offset, section_count, type)) {
1427 return false;
1428 }
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001429 offset = ptr_ - begin_;
jeffhao10037c82012-01-23 15:06:23 -08001430 break;
1431 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001432 ErrorStringPrintf("Unknown map item type %x", type);
jeffhao10037c82012-01-23 15:06:23 -08001433 return false;
1434 }
1435
1436 item++;
1437 }
1438
1439 return true;
1440}
1441
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001442bool DexFileVerifier::CheckOffsetToTypeMap(size_t offset, uint16_t type) {
Mathieu Chartier02e25112013-08-14 16:14:24 -07001443 auto it = offset_to_type_map_.find(offset);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001444 if (UNLIKELY(it == offset_to_type_map_.end())) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001445 ErrorStringPrintf("No data map entry found @ %zx; expected %x", offset, type);
jeffhao10037c82012-01-23 15:06:23 -08001446 return false;
1447 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001448 if (UNLIKELY(it->second != type)) {
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001449 ErrorStringPrintf("Unexpected data map entry @ %zx; expected %x, found %x",
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001450 offset, type, it->second);
jeffhao10037c82012-01-23 15:06:23 -08001451 return false;
1452 }
1453 return true;
1454}
1455
Ian Rogers13735952014-10-08 12:43:28 -07001456uint16_t DexFileVerifier::FindFirstClassDataDefiner(const uint8_t* ptr, bool* success) {
jeffhao10037c82012-01-23 15:06:23 -08001457 ClassDataItemIterator it(*dex_file_, ptr);
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001458 *success = true;
jeffhao10037c82012-01-23 15:06:23 -08001459
1460 if (it.HasNextStaticField() || it.HasNextInstanceField()) {
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001461 LOAD_FIELD(field, it.GetMemberIndex(), "first_class_data_definer field_id",
1462 *success = false; return DexFile::kDexNoIndex16)
Andreas Gampee09269c2014-06-06 18:45:35 -07001463 return field->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001464 }
1465
1466 if (it.HasNextDirectMethod() || it.HasNextVirtualMethod()) {
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001467 LOAD_METHOD(method, it.GetMemberIndex(), "first_class_data_definer method_id",
1468 *success = false; return DexFile::kDexNoIndex16)
Andreas Gampee09269c2014-06-06 18:45:35 -07001469 return method->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001470 }
1471
1472 return DexFile::kDexNoIndex16;
1473}
1474
Ian Rogers13735952014-10-08 12:43:28 -07001475uint16_t DexFileVerifier::FindFirstAnnotationsDirectoryDefiner(const uint8_t* ptr, bool* success) {
jeffhao10037c82012-01-23 15:06:23 -08001476 const DexFile::AnnotationsDirectoryItem* item =
1477 reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr);
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001478 *success = true;
1479
jeffhao10037c82012-01-23 15:06:23 -08001480 if (item->fields_size_ != 0) {
1481 DexFile::FieldAnnotationsItem* field_items = (DexFile::FieldAnnotationsItem*) (item + 1);
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001482 LOAD_FIELD(field, field_items[0].field_idx_, "first_annotations_dir_definer field_id",
1483 *success = false; return DexFile::kDexNoIndex16)
Andreas Gampee09269c2014-06-06 18:45:35 -07001484 return field->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001485 }
1486
1487 if (item->methods_size_ != 0) {
1488 DexFile::MethodAnnotationsItem* method_items = (DexFile::MethodAnnotationsItem*) (item + 1);
Andreas Gampee09269c2014-06-06 18:45:35 -07001489 LOAD_METHOD(method, method_items[0].method_idx_, "first_annotations_dir_definer method id",
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001490 *success = false; return DexFile::kDexNoIndex16)
Andreas Gampee09269c2014-06-06 18:45:35 -07001491 return method->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001492 }
1493
1494 if (item->parameters_size_ != 0) {
1495 DexFile::ParameterAnnotationsItem* parameter_items = (DexFile::ParameterAnnotationsItem*) (item + 1);
Andreas Gampee09269c2014-06-06 18:45:35 -07001496 LOAD_METHOD(method, parameter_items[0].method_idx_, "first_annotations_dir_definer method id",
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001497 *success = false; return DexFile::kDexNoIndex16)
Andreas Gampee09269c2014-06-06 18:45:35 -07001498 return method->class_idx_;
jeffhao10037c82012-01-23 15:06:23 -08001499 }
1500
1501 return DexFile::kDexNoIndex16;
1502}
1503
1504bool DexFileVerifier::CheckInterStringIdItem() {
1505 const DexFile::StringId* item = reinterpret_cast<const DexFile::StringId*>(ptr_);
1506
1507 // Check the map to make sure it has the right offset->type.
1508 if (!CheckOffsetToTypeMap(item->string_data_off_, DexFile::kDexTypeStringDataItem)) {
1509 return false;
1510 }
1511
1512 // Check ordering between items.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001513 if (previous_item_ != nullptr) {
jeffhao10037c82012-01-23 15:06:23 -08001514 const DexFile::StringId* prev_item = reinterpret_cast<const DexFile::StringId*>(previous_item_);
1515 const char* prev_str = dex_file_->GetStringData(*prev_item);
1516 const char* str = dex_file_->GetStringData(*item);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001517 if (UNLIKELY(CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(prev_str, str) >= 0)) {
1518 ErrorStringPrintf("Out-of-order string_ids: '%s' then '%s'", prev_str, str);
jeffhao10037c82012-01-23 15:06:23 -08001519 return false;
1520 }
1521 }
1522
1523 ptr_ += sizeof(DexFile::StringId);
1524 return true;
1525}
1526
1527bool DexFileVerifier::CheckInterTypeIdItem() {
1528 const DexFile::TypeId* item = reinterpret_cast<const DexFile::TypeId*>(ptr_);
Andreas Gampee09269c2014-06-06 18:45:35 -07001529
1530 LOAD_STRING(descriptor, item->descriptor_idx_, "inter_type_id_item descriptor_idx")
jeffhao10037c82012-01-23 15:06:23 -08001531
1532 // Check that the descriptor is a valid type.
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001533 if (UNLIKELY(!IsValidDescriptor(descriptor))) {
1534 ErrorStringPrintf("Invalid type descriptor: '%s'", descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001535 return false;
1536 }
1537
1538 // Check ordering between items.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001539 if (previous_item_ != nullptr) {
jeffhao10037c82012-01-23 15:06:23 -08001540 const DexFile::TypeId* prev_item = reinterpret_cast<const DexFile::TypeId*>(previous_item_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001541 if (UNLIKELY(prev_item->descriptor_idx_ >= item->descriptor_idx_)) {
1542 ErrorStringPrintf("Out-of-order type_ids: %x then %x",
1543 prev_item->descriptor_idx_, item->descriptor_idx_);
jeffhao10037c82012-01-23 15:06:23 -08001544 return false;
1545 }
1546 }
1547
1548 ptr_ += sizeof(DexFile::TypeId);
1549 return true;
1550}
1551
1552bool DexFileVerifier::CheckInterProtoIdItem() {
1553 const DexFile::ProtoId* item = reinterpret_cast<const DexFile::ProtoId*>(ptr_);
Andreas Gampee09269c2014-06-06 18:45:35 -07001554
1555 LOAD_STRING(shorty, item->shorty_idx_, "inter_proto_id_item shorty_idx")
1556
jeffhao10037c82012-01-23 15:06:23 -08001557 if (item->parameters_off_ != 0 &&
1558 !CheckOffsetToTypeMap(item->parameters_off_, DexFile::kDexTypeTypeList)) {
1559 return false;
1560 }
1561
1562 // Check the return type and advance the shorty.
Andreas Gampee09269c2014-06-06 18:45:35 -07001563 LOAD_STRING_BY_TYPE(return_type, item->return_type_idx_, "inter_proto_id_item return_type_idx")
1564 if (!CheckShortyDescriptorMatch(*shorty, return_type, true)) {
jeffhao10037c82012-01-23 15:06:23 -08001565 return false;
1566 }
1567 shorty++;
1568
1569 DexFileParameterIterator it(*dex_file_, *item);
1570 while (it.HasNext() && *shorty != '\0') {
Andreas Gampebb836e12014-06-13 15:31:40 -07001571 if (!CheckIndex(it.GetTypeIdx(), dex_file_->NumTypeIds(),
1572 "inter_proto_id_item shorty type_idx")) {
1573 return false;
1574 }
jeffhao10037c82012-01-23 15:06:23 -08001575 const char* descriptor = it.GetDescriptor();
1576 if (!CheckShortyDescriptorMatch(*shorty, descriptor, false)) {
1577 return false;
1578 }
1579 it.Next();
1580 shorty++;
1581 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001582 if (UNLIKELY(it.HasNext() || *shorty != '\0')) {
1583 ErrorStringPrintf("Mismatched length for parameters and shorty");
jeffhao10037c82012-01-23 15:06:23 -08001584 return false;
1585 }
1586
1587 // Check ordering between items. This relies on type_ids being in order.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001588 if (previous_item_ != nullptr) {
jeffhao10037c82012-01-23 15:06:23 -08001589 const DexFile::ProtoId* prev = reinterpret_cast<const DexFile::ProtoId*>(previous_item_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001590 if (UNLIKELY(prev->return_type_idx_ > item->return_type_idx_)) {
1591 ErrorStringPrintf("Out-of-order proto_id return types");
jeffhao10037c82012-01-23 15:06:23 -08001592 return false;
1593 } else if (prev->return_type_idx_ == item->return_type_idx_) {
1594 DexFileParameterIterator curr_it(*dex_file_, *item);
1595 DexFileParameterIterator prev_it(*dex_file_, *prev);
1596
1597 while (curr_it.HasNext() && prev_it.HasNext()) {
1598 uint16_t prev_idx = prev_it.GetTypeIdx();
1599 uint16_t curr_idx = curr_it.GetTypeIdx();
1600 if (prev_idx == DexFile::kDexNoIndex16) {
1601 break;
1602 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001603 if (UNLIKELY(curr_idx == DexFile::kDexNoIndex16)) {
1604 ErrorStringPrintf("Out-of-order proto_id arguments");
jeffhao10037c82012-01-23 15:06:23 -08001605 return false;
1606 }
1607
1608 if (prev_idx < curr_idx) {
1609 break;
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001610 } else if (UNLIKELY(prev_idx > curr_idx)) {
1611 ErrorStringPrintf("Out-of-order proto_id arguments");
jeffhao10037c82012-01-23 15:06:23 -08001612 return false;
1613 }
1614
1615 prev_it.Next();
1616 curr_it.Next();
1617 }
1618 }
1619 }
1620
1621 ptr_ += sizeof(DexFile::ProtoId);
1622 return true;
1623}
1624
1625bool DexFileVerifier::CheckInterFieldIdItem() {
1626 const DexFile::FieldId* item = reinterpret_cast<const DexFile::FieldId*>(ptr_);
1627
1628 // Check that the class descriptor is valid.
Andreas Gampee09269c2014-06-06 18:45:35 -07001629 LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_field_id_item class_idx")
1630 if (UNLIKELY(!IsValidDescriptor(class_descriptor) || class_descriptor[0] != 'L')) {
1631 ErrorStringPrintf("Invalid descriptor for class_idx: '%s'", class_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001632 return false;
1633 }
1634
1635 // Check that the type descriptor is a valid field name.
Andreas Gampee09269c2014-06-06 18:45:35 -07001636 LOAD_STRING_BY_TYPE(type_descriptor, item->type_idx_, "inter_field_id_item type_idx")
1637 if (UNLIKELY(!IsValidDescriptor(type_descriptor) || type_descriptor[0] == 'V')) {
1638 ErrorStringPrintf("Invalid descriptor for type_idx: '%s'", type_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001639 return false;
1640 }
1641
1642 // Check that the name is valid.
Andreas Gampee09269c2014-06-06 18:45:35 -07001643 LOAD_STRING(descriptor, item->name_idx_, "inter_field_id_item name_idx")
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001644 if (UNLIKELY(!IsValidMemberName(descriptor))) {
1645 ErrorStringPrintf("Invalid field name: '%s'", descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001646 return false;
1647 }
1648
1649 // Check ordering between items. This relies on the other sections being in order.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001650 if (previous_item_ != nullptr) {
jeffhao10037c82012-01-23 15:06:23 -08001651 const DexFile::FieldId* prev_item = reinterpret_cast<const DexFile::FieldId*>(previous_item_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001652 if (UNLIKELY(prev_item->class_idx_ > item->class_idx_)) {
1653 ErrorStringPrintf("Out-of-order field_ids");
jeffhao10037c82012-01-23 15:06:23 -08001654 return false;
1655 } else if (prev_item->class_idx_ == item->class_idx_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001656 if (UNLIKELY(prev_item->name_idx_ > item->name_idx_)) {
1657 ErrorStringPrintf("Out-of-order field_ids");
jeffhao10037c82012-01-23 15:06:23 -08001658 return false;
1659 } else if (prev_item->name_idx_ == item->name_idx_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001660 if (UNLIKELY(prev_item->type_idx_ >= item->type_idx_)) {
1661 ErrorStringPrintf("Out-of-order field_ids");
jeffhao10037c82012-01-23 15:06:23 -08001662 return false;
1663 }
1664 }
1665 }
1666 }
1667
1668 ptr_ += sizeof(DexFile::FieldId);
1669 return true;
1670}
1671
1672bool DexFileVerifier::CheckInterMethodIdItem() {
1673 const DexFile::MethodId* item = reinterpret_cast<const DexFile::MethodId*>(ptr_);
1674
1675 // Check that the class descriptor is a valid reference name.
Andreas Gampee09269c2014-06-06 18:45:35 -07001676 LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_method_id_item class_idx")
1677 if (UNLIKELY(!IsValidDescriptor(class_descriptor) || (class_descriptor[0] != 'L' &&
1678 class_descriptor[0] != '['))) {
1679 ErrorStringPrintf("Invalid descriptor for class_idx: '%s'", class_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001680 return false;
1681 }
1682
1683 // Check that the name is valid.
Andreas Gampedf10b322014-06-11 21:46:05 -07001684 LOAD_STRING(descriptor, item->name_idx_, "inter_method_id_item name_idx")
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001685 if (UNLIKELY(!IsValidMemberName(descriptor))) {
1686 ErrorStringPrintf("Invalid method name: '%s'", descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001687 return false;
1688 }
1689
Andreas Gampedf10b322014-06-11 21:46:05 -07001690 // Check that the proto id is valid.
1691 if (UNLIKELY(!CheckIndex(item->proto_idx_, dex_file_->NumProtoIds(),
1692 "inter_method_id_item proto_idx"))) {
1693 return false;
1694 }
1695
jeffhao10037c82012-01-23 15:06:23 -08001696 // Check ordering between items. This relies on the other sections being in order.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001697 if (previous_item_ != nullptr) {
jeffhao10037c82012-01-23 15:06:23 -08001698 const DexFile::MethodId* prev_item = reinterpret_cast<const DexFile::MethodId*>(previous_item_);
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001699 if (UNLIKELY(prev_item->class_idx_ > item->class_idx_)) {
1700 ErrorStringPrintf("Out-of-order method_ids");
jeffhao10037c82012-01-23 15:06:23 -08001701 return false;
1702 } else if (prev_item->class_idx_ == item->class_idx_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001703 if (UNLIKELY(prev_item->name_idx_ > item->name_idx_)) {
1704 ErrorStringPrintf("Out-of-order method_ids");
jeffhao10037c82012-01-23 15:06:23 -08001705 return false;
1706 } else if (prev_item->name_idx_ == item->name_idx_) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001707 if (UNLIKELY(prev_item->proto_idx_ >= item->proto_idx_)) {
1708 ErrorStringPrintf("Out-of-order method_ids");
jeffhao10037c82012-01-23 15:06:23 -08001709 return false;
1710 }
1711 }
1712 }
1713 }
1714
1715 ptr_ += sizeof(DexFile::MethodId);
1716 return true;
1717}
1718
1719bool DexFileVerifier::CheckInterClassDefItem() {
1720 const DexFile::ClassDef* item = reinterpret_cast<const DexFile::ClassDef*>(ptr_);
jeffhao10037c82012-01-23 15:06:23 -08001721
Andreas Gampe0ba238d2014-07-29 01:22:07 -07001722 // Check for duplicate class def.
1723 if (defined_classes_.find(item->class_idx_) != defined_classes_.end()) {
1724 ErrorStringPrintf("Redefinition of class with type idx: '%d'", item->class_idx_);
1725 return false;
1726 }
1727 defined_classes_.insert(item->class_idx_);
1728
Andreas Gampee09269c2014-06-06 18:45:35 -07001729 LOAD_STRING_BY_TYPE(class_descriptor, item->class_idx_, "inter_class_def_item class_idx")
1730 if (UNLIKELY(!IsValidDescriptor(class_descriptor) || class_descriptor[0] != 'L')) {
1731 ErrorStringPrintf("Invalid class descriptor: '%s'", class_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001732 return false;
1733 }
1734
Andreas Gampeacc2bb62014-07-17 19:26:50 -07001735 // Only allow non-runtime modifiers.
1736 if ((item->access_flags_ & ~kAccJavaFlagsMask) != 0) {
1737 ErrorStringPrintf("Invalid class flags: '%d'", item->access_flags_);
1738 return false;
1739 }
1740
jeffhao10037c82012-01-23 15:06:23 -08001741 if (item->interfaces_off_ != 0 &&
1742 !CheckOffsetToTypeMap(item->interfaces_off_, DexFile::kDexTypeTypeList)) {
1743 return false;
1744 }
1745 if (item->annotations_off_ != 0 &&
1746 !CheckOffsetToTypeMap(item->annotations_off_, DexFile::kDexTypeAnnotationsDirectoryItem)) {
1747 return false;
1748 }
1749 if (item->class_data_off_ != 0 &&
1750 !CheckOffsetToTypeMap(item->class_data_off_, DexFile::kDexTypeClassDataItem)) {
1751 return false;
1752 }
1753 if (item->static_values_off_ != 0 &&
1754 !CheckOffsetToTypeMap(item->static_values_off_, DexFile::kDexTypeEncodedArrayItem)) {
1755 return false;
1756 }
1757
1758 if (item->superclass_idx_ != DexFile::kDexNoIndex16) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001759 LOAD_STRING_BY_TYPE(superclass_descriptor, item->superclass_idx_,
1760 "inter_class_def_item superclass_idx")
1761 if (UNLIKELY(!IsValidDescriptor(superclass_descriptor) || superclass_descriptor[0] != 'L')) {
1762 ErrorStringPrintf("Invalid superclass: '%s'", superclass_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001763 return false;
1764 }
1765 }
1766
1767 const DexFile::TypeList* interfaces = dex_file_->GetInterfacesList(*item);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001768 if (interfaces != nullptr) {
jeffhao10037c82012-01-23 15:06:23 -08001769 uint32_t size = interfaces->Size();
1770
1771 // Ensure that all interfaces refer to classes (not arrays or primitives).
1772 for (uint32_t i = 0; i < size; i++) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001773 LOAD_STRING_BY_TYPE(inf_descriptor, interfaces->GetTypeItem(i).type_idx_,
1774 "inter_class_def_item interface type_idx")
1775 if (UNLIKELY(!IsValidDescriptor(inf_descriptor) || inf_descriptor[0] != 'L')) {
1776 ErrorStringPrintf("Invalid interface: '%s'", inf_descriptor);
jeffhao10037c82012-01-23 15:06:23 -08001777 return false;
1778 }
1779 }
1780
1781 /*
1782 * Ensure that there are no duplicates. This is an O(N^2) test, but in
1783 * practice the number of interfaces implemented by any given class is low.
1784 */
1785 for (uint32_t i = 1; i < size; i++) {
1786 uint32_t idx1 = interfaces->GetTypeItem(i).type_idx_;
1787 for (uint32_t j =0; j < i; j++) {
1788 uint32_t idx2 = interfaces->GetTypeItem(j).type_idx_;
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001789 if (UNLIKELY(idx1 == idx2)) {
1790 ErrorStringPrintf("Duplicate interface: '%s'", dex_file_->StringByTypeIdx(idx1));
jeffhao10037c82012-01-23 15:06:23 -08001791 return false;
1792 }
1793 }
1794 }
1795 }
1796
1797 // Check that references in class_data_item are to the right class.
1798 if (item->class_data_off_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07001799 const uint8_t* data = begin_ + item->class_data_off_;
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001800 bool success;
1801 uint16_t data_definer = FindFirstClassDataDefiner(data, &success);
1802 if (!success) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001803 return false;
1804 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001805 if (UNLIKELY((data_definer != item->class_idx_) && (data_definer != DexFile::kDexNoIndex16))) {
1806 ErrorStringPrintf("Invalid class_data_item");
jeffhao10037c82012-01-23 15:06:23 -08001807 return false;
1808 }
1809 }
1810
1811 // Check that references in annotations_directory_item are to right class.
1812 if (item->annotations_off_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07001813 const uint8_t* data = begin_ + item->annotations_off_;
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001814 bool success;
1815 uint16_t annotations_definer = FindFirstAnnotationsDirectoryDefiner(data, &success);
1816 if (!success) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001817 return false;
1818 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001819 if (UNLIKELY((annotations_definer != item->class_idx_) &&
1820 (annotations_definer != DexFile::kDexNoIndex16))) {
1821 ErrorStringPrintf("Invalid annotations_directory_item");
jeffhao10037c82012-01-23 15:06:23 -08001822 return false;
1823 }
1824 }
1825
1826 ptr_ += sizeof(DexFile::ClassDef);
1827 return true;
1828}
1829
1830bool DexFileVerifier::CheckInterAnnotationSetRefList() {
1831 const DexFile::AnnotationSetRefList* list =
1832 reinterpret_cast<const DexFile::AnnotationSetRefList*>(ptr_);
1833 const DexFile::AnnotationSetRefItem* item = list->list_;
1834 uint32_t count = list->size_;
1835
1836 while (count--) {
1837 if (item->annotations_off_ != 0 &&
1838 !CheckOffsetToTypeMap(item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
1839 return false;
1840 }
1841 item++;
1842 }
1843
Ian Rogers13735952014-10-08 12:43:28 -07001844 ptr_ = reinterpret_cast<const uint8_t*>(item);
jeffhao10037c82012-01-23 15:06:23 -08001845 return true;
1846}
1847
1848bool DexFileVerifier::CheckInterAnnotationSetItem() {
1849 const DexFile::AnnotationSetItem* set = reinterpret_cast<const DexFile::AnnotationSetItem*>(ptr_);
1850 const uint32_t* offsets = set->entries_;
1851 uint32_t count = set->size_;
1852 uint32_t last_idx = 0;
1853
1854 for (uint32_t i = 0; i < count; i++) {
1855 if (*offsets != 0 && !CheckOffsetToTypeMap(*offsets, DexFile::kDexTypeAnnotationItem)) {
1856 return false;
1857 }
1858
1859 // Get the annotation from the offset and the type index for the annotation.
1860 const DexFile::AnnotationItem* annotation =
Ian Rogers30fab402012-01-23 15:43:46 -08001861 reinterpret_cast<const DexFile::AnnotationItem*>(begin_ + *offsets);
jeffhao10037c82012-01-23 15:06:23 -08001862 const uint8_t* data = annotation->annotation_;
1863 uint32_t idx = DecodeUnsignedLeb128(&data);
1864
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001865 if (UNLIKELY(last_idx >= idx && i != 0)) {
1866 ErrorStringPrintf("Out-of-order entry types: %x then %x", last_idx, idx);
jeffhao10037c82012-01-23 15:06:23 -08001867 return false;
1868 }
1869
1870 last_idx = idx;
1871 offsets++;
1872 }
1873
Ian Rogers13735952014-10-08 12:43:28 -07001874 ptr_ = reinterpret_cast<const uint8_t*>(offsets);
jeffhao10037c82012-01-23 15:06:23 -08001875 return true;
1876}
1877
1878bool DexFileVerifier::CheckInterClassDataItem() {
1879 ClassDataItemIterator it(*dex_file_, ptr_);
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001880 bool success;
1881 uint16_t defining_class = FindFirstClassDataDefiner(ptr_, &success);
1882 if (!success) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001883 return false;
1884 }
jeffhao10037c82012-01-23 15:06:23 -08001885
1886 for (; it.HasNextStaticField() || it.HasNextInstanceField(); it.Next()) {
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001887 LOAD_FIELD(field, it.GetMemberIndex(), "inter_class_data_item field_id", return false)
Andreas Gampee09269c2014-06-06 18:45:35 -07001888 if (UNLIKELY(field->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001889 ErrorStringPrintf("Mismatched defining class for class_data_item field");
jeffhao10037c82012-01-23 15:06:23 -08001890 return false;
1891 }
1892 }
1893 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
1894 uint32_t code_off = it.GetMethodCodeItemOffset();
1895 if (code_off != 0 && !CheckOffsetToTypeMap(code_off, DexFile::kDexTypeCodeItem)) {
1896 return false;
1897 }
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001898 LOAD_METHOD(method, it.GetMemberIndex(), "inter_class_data_item method_id", return false)
Andreas Gampee09269c2014-06-06 18:45:35 -07001899 if (UNLIKELY(method->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001900 ErrorStringPrintf("Mismatched defining class for class_data_item method");
jeffhao10037c82012-01-23 15:06:23 -08001901 return false;
1902 }
1903 }
1904
1905 ptr_ = it.EndDataPointer();
1906 return true;
1907}
1908
1909bool DexFileVerifier::CheckInterAnnotationsDirectoryItem() {
1910 const DexFile::AnnotationsDirectoryItem* item =
1911 reinterpret_cast<const DexFile::AnnotationsDirectoryItem*>(ptr_);
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001912 bool success;
1913 uint16_t defining_class = FindFirstAnnotationsDirectoryDefiner(ptr_, &success);
1914 if (!success) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001915 return false;
1916 }
jeffhao10037c82012-01-23 15:06:23 -08001917
1918 if (item->class_annotations_off_ != 0 &&
1919 !CheckOffsetToTypeMap(item->class_annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
1920 return false;
1921 }
1922
1923 // Field annotations follow immediately after the annotations directory.
1924 const DexFile::FieldAnnotationsItem* field_item =
1925 reinterpret_cast<const DexFile::FieldAnnotationsItem*>(item + 1);
1926 uint32_t field_count = item->fields_size_;
1927 for (uint32_t i = 0; i < field_count; i++) {
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001928 LOAD_FIELD(field, field_item->field_idx_, "inter_annotations_directory_item field_id",
1929 return false)
Andreas Gampee09269c2014-06-06 18:45:35 -07001930 if (UNLIKELY(field->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001931 ErrorStringPrintf("Mismatched defining class for field_annotation");
jeffhao10037c82012-01-23 15:06:23 -08001932 return false;
1933 }
1934 if (!CheckOffsetToTypeMap(field_item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
1935 return false;
1936 }
1937 field_item++;
1938 }
1939
1940 // Method annotations follow immediately after field annotations.
1941 const DexFile::MethodAnnotationsItem* method_item =
1942 reinterpret_cast<const DexFile::MethodAnnotationsItem*>(field_item);
1943 uint32_t method_count = item->methods_size_;
1944 for (uint32_t i = 0; i < method_count; i++) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001945 LOAD_METHOD(method, method_item->method_idx_, "inter_annotations_directory_item method_id",
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001946 return false)
Andreas Gampee09269c2014-06-06 18:45:35 -07001947 if (UNLIKELY(method->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001948 ErrorStringPrintf("Mismatched defining class for method_annotation");
jeffhao10037c82012-01-23 15:06:23 -08001949 return false;
1950 }
1951 if (!CheckOffsetToTypeMap(method_item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
1952 return false;
1953 }
1954 method_item++;
1955 }
1956
1957 // Parameter annotations follow immediately after method annotations.
1958 const DexFile::ParameterAnnotationsItem* parameter_item =
1959 reinterpret_cast<const DexFile::ParameterAnnotationsItem*>(method_item);
1960 uint32_t parameter_count = item->parameters_size_;
1961 for (uint32_t i = 0; i < parameter_count; i++) {
Andreas Gampee09269c2014-06-06 18:45:35 -07001962 LOAD_METHOD(parameter_method, parameter_item->method_idx_,
Andreas Gampe5e31dda2014-06-13 11:35:12 -07001963 "inter_annotations_directory_item parameter method_id", return false)
Andreas Gampee09269c2014-06-06 18:45:35 -07001964 if (UNLIKELY(parameter_method->class_idx_ != defining_class)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001965 ErrorStringPrintf("Mismatched defining class for parameter_annotation");
jeffhao10037c82012-01-23 15:06:23 -08001966 return false;
1967 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001968 if (!CheckOffsetToTypeMap(parameter_item->annotations_off_,
1969 DexFile::kDexTypeAnnotationSetRefList)) {
jeffhao10037c82012-01-23 15:06:23 -08001970 return false;
1971 }
1972 parameter_item++;
1973 }
1974
Ian Rogers13735952014-10-08 12:43:28 -07001975 ptr_ = reinterpret_cast<const uint8_t*>(parameter_item);
jeffhao10037c82012-01-23 15:06:23 -08001976 return true;
1977}
1978
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001979bool DexFileVerifier::CheckInterSectionIterate(size_t offset, uint32_t count, uint16_t type) {
jeffhao10037c82012-01-23 15:06:23 -08001980 // Get the right alignment mask for the type of section.
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08001981 size_t alignment_mask;
jeffhao10037c82012-01-23 15:06:23 -08001982 switch (type) {
1983 case DexFile::kDexTypeClassDataItem:
1984 alignment_mask = sizeof(uint8_t) - 1;
1985 break;
1986 default:
1987 alignment_mask = sizeof(uint32_t) - 1;
1988 break;
1989 }
1990
1991 // Iterate through the items in the section.
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001992 previous_item_ = nullptr;
jeffhao10037c82012-01-23 15:06:23 -08001993 for (uint32_t i = 0; i < count; i++) {
1994 uint32_t new_offset = (offset + alignment_mask) & ~alignment_mask;
Ian Rogers30fab402012-01-23 15:43:46 -08001995 ptr_ = begin_ + new_offset;
Ian Rogers13735952014-10-08 12:43:28 -07001996 const uint8_t* prev_ptr = ptr_;
jeffhao10037c82012-01-23 15:06:23 -08001997
1998 // Check depending on the section type.
1999 switch (type) {
2000 case DexFile::kDexTypeStringIdItem: {
2001 if (!CheckInterStringIdItem()) {
2002 return false;
2003 }
2004 break;
2005 }
2006 case DexFile::kDexTypeTypeIdItem: {
2007 if (!CheckInterTypeIdItem()) {
2008 return false;
2009 }
2010 break;
2011 }
2012 case DexFile::kDexTypeProtoIdItem: {
2013 if (!CheckInterProtoIdItem()) {
2014 return false;
2015 }
2016 break;
2017 }
2018 case DexFile::kDexTypeFieldIdItem: {
2019 if (!CheckInterFieldIdItem()) {
2020 return false;
2021 }
2022 break;
2023 }
2024 case DexFile::kDexTypeMethodIdItem: {
2025 if (!CheckInterMethodIdItem()) {
2026 return false;
2027 }
2028 break;
2029 }
2030 case DexFile::kDexTypeClassDefItem: {
2031 if (!CheckInterClassDefItem()) {
2032 return false;
2033 }
2034 break;
2035 }
2036 case DexFile::kDexTypeAnnotationSetRefList: {
2037 if (!CheckInterAnnotationSetRefList()) {
2038 return false;
2039 }
2040 break;
2041 }
2042 case DexFile::kDexTypeAnnotationSetItem: {
2043 if (!CheckInterAnnotationSetItem()) {
2044 return false;
2045 }
2046 break;
2047 }
2048 case DexFile::kDexTypeClassDataItem: {
2049 if (!CheckInterClassDataItem()) {
2050 return false;
2051 }
2052 break;
2053 }
2054 case DexFile::kDexTypeAnnotationsDirectoryItem: {
2055 if (!CheckInterAnnotationsDirectoryItem()) {
2056 return false;
2057 }
2058 break;
2059 }
2060 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07002061 ErrorStringPrintf("Unknown map item type %x", type);
jeffhao10037c82012-01-23 15:06:23 -08002062 return false;
2063 }
2064
2065 previous_item_ = prev_ptr;
Ian Rogers8a6bbfc2014-01-23 13:29:07 -08002066 offset = ptr_ - begin_;
jeffhao10037c82012-01-23 15:06:23 -08002067 }
2068
2069 return true;
2070}
2071
2072bool DexFileVerifier::CheckInterSection() {
Ian Rogers30fab402012-01-23 15:43:46 -08002073 const DexFile::MapList* map = reinterpret_cast<const DexFile::MapList*>(begin_ + header_->map_off_);
jeffhao10037c82012-01-23 15:06:23 -08002074 const DexFile::MapItem* item = map->list_;
2075 uint32_t count = map->size_;
2076
2077 // Cross check the items listed in the map.
2078 while (count--) {
2079 uint32_t section_offset = item->offset_;
2080 uint32_t section_count = item->size_;
2081 uint16_t type = item->type_;
2082
2083 switch (type) {
2084 case DexFile::kDexTypeHeaderItem:
2085 case DexFile::kDexTypeMapList:
2086 case DexFile::kDexTypeTypeList:
2087 case DexFile::kDexTypeCodeItem:
2088 case DexFile::kDexTypeStringDataItem:
2089 case DexFile::kDexTypeDebugInfoItem:
2090 case DexFile::kDexTypeAnnotationItem:
2091 case DexFile::kDexTypeEncodedArrayItem:
2092 break;
2093 case DexFile::kDexTypeStringIdItem:
2094 case DexFile::kDexTypeTypeIdItem:
2095 case DexFile::kDexTypeProtoIdItem:
2096 case DexFile::kDexTypeFieldIdItem:
2097 case DexFile::kDexTypeMethodIdItem:
2098 case DexFile::kDexTypeClassDefItem:
2099 case DexFile::kDexTypeAnnotationSetRefList:
2100 case DexFile::kDexTypeAnnotationSetItem:
2101 case DexFile::kDexTypeClassDataItem:
2102 case DexFile::kDexTypeAnnotationsDirectoryItem: {
2103 if (!CheckInterSectionIterate(section_offset, section_count, type)) {
2104 return false;
2105 }
2106 break;
2107 }
2108 default:
Ian Rogers8d31bbd2013-10-13 10:44:14 -07002109 ErrorStringPrintf("Unknown map item type %x", type);
jeffhao10037c82012-01-23 15:06:23 -08002110 return false;
2111 }
2112
2113 item++;
2114 }
2115
2116 return true;
2117}
2118
2119bool DexFileVerifier::Verify() {
2120 // Check the header.
2121 if (!CheckHeader()) {
2122 return false;
2123 }
2124
2125 // Check the map section.
2126 if (!CheckMap()) {
2127 return false;
2128 }
2129
2130 // Check structure within remaining sections.
2131 if (!CheckIntraSection()) {
2132 return false;
2133 }
2134
2135 // Check references from one section to another.
2136 if (!CheckInterSection()) {
2137 return false;
2138 }
2139
2140 return true;
2141}
2142
Ian Rogers8d31bbd2013-10-13 10:44:14 -07002143void DexFileVerifier::ErrorStringPrintf(const char* fmt, ...) {
2144 va_list ap;
2145 va_start(ap, fmt);
2146 DCHECK(failure_reason_.empty()) << failure_reason_;
2147 failure_reason_ = StringPrintf("Failure to verify dex file '%s': ", location_);
2148 StringAppendV(&failure_reason_, fmt, ap);
2149 va_end(ap);
2150}
2151
jeffhao10037c82012-01-23 15:06:23 -08002152} // namespace art