1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "dex_file.h"
18
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <zlib.h>
24
25 #include <memory>
26 #include <ostream>
27 #include <sstream>
28 #include <type_traits>
29
30 #include "android-base/stringprintf.h"
31
32 #include "base/enums.h"
33 #include "base/hiddenapi_domain.h"
34 #include "base/leb128.h"
35 #include "base/stl_util.h"
36 #include "class_accessor-inl.h"
37 #include "descriptors_names.h"
38 #include "dex_file-inl.h"
39 #include "standard_dex_file.h"
40 #include "utf-inl.h"
41
42 namespace art {
43
44 using android::base::StringPrintf;
45
46 using dex::CallSiteIdItem;
47 using dex::ClassDef;
48 using dex::FieldId;
49 using dex::MapList;
50 using dex::MapItem;
51 using dex::MethodHandleItem;
52 using dex::MethodId;
53 using dex::ProtoId;
54 using dex::StringId;
55 using dex::TryItem;
56 using dex::TypeId;
57 using dex::TypeList;
58
59 static_assert(sizeof(dex::StringIndex) == sizeof(uint32_t), "StringIndex size is wrong");
60 static_assert(std::is_trivially_copyable<dex::StringIndex>::value, "StringIndex not trivial");
61 static_assert(sizeof(dex::TypeIndex) == sizeof(uint16_t), "TypeIndex size is wrong");
62 static_assert(std::is_trivially_copyable<dex::TypeIndex>::value, "TypeIndex not trivial");
63
CalculateChecksum() const64 uint32_t DexFile::CalculateChecksum() const {
65 return CalculateChecksum(Begin(), Size());
66 }
67
CalculateChecksum(const uint8_t * begin,size_t size)68 uint32_t DexFile::CalculateChecksum(const uint8_t* begin, size_t size) {
69 const uint32_t non_sum_bytes = OFFSETOF_MEMBER(DexFile::Header, signature_);
70 return ChecksumMemoryRange(begin + non_sum_bytes, size - non_sum_bytes);
71 }
72
ChecksumMemoryRange(const uint8_t * begin,size_t size)73 uint32_t DexFile::ChecksumMemoryRange(const uint8_t* begin, size_t size) {
74 return adler32(adler32(0L, Z_NULL, 0), begin, size);
75 }
76
GetPermissions() const77 int DexFile::GetPermissions() const {
78 CHECK(container_.get() != nullptr);
79 return container_->GetPermissions();
80 }
81
IsReadOnly() const82 bool DexFile::IsReadOnly() const {
83 CHECK(container_.get() != nullptr);
84 return container_->IsReadOnly();
85 }
86
EnableWrite() const87 bool DexFile::EnableWrite() const {
88 CHECK(container_.get() != nullptr);
89 return container_->EnableWrite();
90 }
91
DisableWrite() const92 bool DexFile::DisableWrite() const {
93 CHECK(container_.get() != nullptr);
94 return container_->DisableWrite();
95 }
96
DexFile(const uint8_t * base,size_t size,const uint8_t * data_begin,size_t data_size,const std::string & location,uint32_t location_checksum,const OatDexFile * oat_dex_file,std::unique_ptr<DexFileContainer> container,bool is_compact_dex)97 DexFile::DexFile(const uint8_t* base,
98 size_t size,
99 const uint8_t* data_begin,
100 size_t data_size,
101 const std::string& location,
102 uint32_t location_checksum,
103 const OatDexFile* oat_dex_file,
104 std::unique_ptr<DexFileContainer> container,
105 bool is_compact_dex)
106 : begin_(base),
107 size_(size),
108 data_begin_(data_begin),
109 data_size_(data_size),
110 location_(location),
111 location_checksum_(location_checksum),
112 header_(reinterpret_cast<const Header*>(base)),
113 string_ids_(reinterpret_cast<const StringId*>(base + header_->string_ids_off_)),
114 type_ids_(reinterpret_cast<const TypeId*>(base + header_->type_ids_off_)),
115 field_ids_(reinterpret_cast<const FieldId*>(base + header_->field_ids_off_)),
116 method_ids_(reinterpret_cast<const MethodId*>(base + header_->method_ids_off_)),
117 proto_ids_(reinterpret_cast<const ProtoId*>(base + header_->proto_ids_off_)),
118 class_defs_(reinterpret_cast<const ClassDef*>(base + header_->class_defs_off_)),
119 method_handles_(nullptr),
120 num_method_handles_(0),
121 call_site_ids_(nullptr),
122 num_call_site_ids_(0),
123 hiddenapi_class_data_(nullptr),
124 oat_dex_file_(oat_dex_file),
125 container_(std::move(container)),
126 is_compact_dex_(is_compact_dex),
127 hiddenapi_domain_(hiddenapi::Domain::kApplication) {
128 CHECK(begin_ != nullptr) << GetLocation();
129 CHECK_GT(size_, 0U) << GetLocation();
130 // Check base (=header) alignment.
131 // Must be 4-byte aligned to avoid undefined behavior when accessing
132 // any of the sections via a pointer.
133 CHECK_ALIGNED(begin_, alignof(Header));
134
135 InitializeSectionsFromMapList();
136 }
137
~DexFile()138 DexFile::~DexFile() {
139 // We don't call DeleteGlobalRef on dex_object_ because we're only called by DestroyJavaVM, and
140 // that's only called after DetachCurrentThread, which means there's no JNIEnv. We could
141 // re-attach, but cleaning up these global references is not obviously useful. It's not as if
142 // the global reference table is otherwise empty!
143 }
144
Init(std::string * error_msg)145 bool DexFile::Init(std::string* error_msg) {
146 if (!CheckMagicAndVersion(error_msg)) {
147 return false;
148 }
149 return true;
150 }
151
CheckMagicAndVersion(std::string * error_msg) const152 bool DexFile::CheckMagicAndVersion(std::string* error_msg) const {
153 if (!IsMagicValid()) {
154 std::ostringstream oss;
155 oss << "Unrecognized magic number in " << GetLocation() << ":"
156 << " " << header_->magic_[0]
157 << " " << header_->magic_[1]
158 << " " << header_->magic_[2]
159 << " " << header_->magic_[3];
160 *error_msg = oss.str();
161 return false;
162 }
163 if (!IsVersionValid()) {
164 std::ostringstream oss;
165 oss << "Unrecognized version number in " << GetLocation() << ":"
166 << " " << header_->magic_[4]
167 << " " << header_->magic_[5]
168 << " " << header_->magic_[6]
169 << " " << header_->magic_[7];
170 *error_msg = oss.str();
171 return false;
172 }
173 return true;
174 }
175
InitializeSectionsFromMapList()176 void DexFile::InitializeSectionsFromMapList() {
177 const MapList* map_list = reinterpret_cast<const MapList*>(DataBegin() + header_->map_off_);
178 if (header_->map_off_ == 0 || header_->map_off_ > DataSize()) {
179 // Bad offset. The dex file verifier runs after this method and will reject the file.
180 return;
181 }
182 const size_t count = map_list->size_;
183
184 size_t map_limit = header_->map_off_ + count * sizeof(MapItem);
185 if (header_->map_off_ >= map_limit || map_limit > DataSize()) {
186 // Overflow or out out of bounds. The dex file verifier runs after
187 // this method and will reject the file as it is malformed.
188 return;
189 }
190
191 for (size_t i = 0; i < count; ++i) {
192 const MapItem& map_item = map_list->list_[i];
193 if (map_item.type_ == kDexTypeMethodHandleItem) {
194 method_handles_ = reinterpret_cast<const MethodHandleItem*>(Begin() + map_item.offset_);
195 num_method_handles_ = map_item.size_;
196 } else if (map_item.type_ == kDexTypeCallSiteIdItem) {
197 call_site_ids_ = reinterpret_cast<const CallSiteIdItem*>(Begin() + map_item.offset_);
198 num_call_site_ids_ = map_item.size_;
199 } else if (map_item.type_ == kDexTypeHiddenapiClassData) {
200 hiddenapi_class_data_ = GetHiddenapiClassDataAtOffset(map_item.offset_);
201 } else {
202 // Pointers to other sections are not necessary to retain in the DexFile struct.
203 // Other items have pointers directly into their data.
204 }
205 }
206 }
207
GetVersion() const208 uint32_t DexFile::Header::GetVersion() const {
209 const char* version = reinterpret_cast<const char*>(&magic_[kDexMagicSize]);
210 return atoi(version);
211 }
212
FindClassDef(dex::TypeIndex type_idx) const213 const ClassDef* DexFile::FindClassDef(dex::TypeIndex type_idx) const {
214 size_t num_class_defs = NumClassDefs();
215 // Fast path for rare no class defs case.
216 if (num_class_defs == 0) {
217 return nullptr;
218 }
219 for (size_t i = 0; i < num_class_defs; ++i) {
220 const ClassDef& class_def = GetClassDef(i);
221 if (class_def.class_idx_ == type_idx) {
222 return &class_def;
223 }
224 }
225 return nullptr;
226 }
227
FindCodeItemOffset(const ClassDef & class_def,uint32_t method_idx) const228 uint32_t DexFile::FindCodeItemOffset(const ClassDef& class_def, uint32_t method_idx) const {
229 ClassAccessor accessor(*this, class_def);
230 CHECK(accessor.HasClassData());
231 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
232 if (method.GetIndex() == method_idx) {
233 return method.GetCodeItemOffset();
234 }
235 }
236 LOG(FATAL) << "Unable to find method " << method_idx;
237 UNREACHABLE();
238 }
239
FindFieldId(const TypeId & declaring_klass,const StringId & name,const TypeId & type) const240 const FieldId* DexFile::FindFieldId(const TypeId& declaring_klass,
241 const StringId& name,
242 const TypeId& type) const {
243 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
244 const dex::TypeIndex class_idx = GetIndexForTypeId(declaring_klass);
245 const dex::StringIndex name_idx = GetIndexForStringId(name);
246 const dex::TypeIndex type_idx = GetIndexForTypeId(type);
247 int32_t lo = 0;
248 int32_t hi = NumFieldIds() - 1;
249 while (hi >= lo) {
250 int32_t mid = (hi + lo) / 2;
251 const FieldId& field = GetFieldId(mid);
252 if (class_idx > field.class_idx_) {
253 lo = mid + 1;
254 } else if (class_idx < field.class_idx_) {
255 hi = mid - 1;
256 } else {
257 if (name_idx > field.name_idx_) {
258 lo = mid + 1;
259 } else if (name_idx < field.name_idx_) {
260 hi = mid - 1;
261 } else {
262 if (type_idx > field.type_idx_) {
263 lo = mid + 1;
264 } else if (type_idx < field.type_idx_) {
265 hi = mid - 1;
266 } else {
267 return &field;
268 }
269 }
270 }
271 }
272 return nullptr;
273 }
274
FindMethodId(const TypeId & declaring_klass,const StringId & name,const ProtoId & signature) const275 const MethodId* DexFile::FindMethodId(const TypeId& declaring_klass,
276 const StringId& name,
277 const ProtoId& signature) const {
278 // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
279 const dex::TypeIndex class_idx = GetIndexForTypeId(declaring_klass);
280 const dex::StringIndex name_idx = GetIndexForStringId(name);
281 const dex::ProtoIndex proto_idx = GetIndexForProtoId(signature);
282 int32_t lo = 0;
283 int32_t hi = NumMethodIds() - 1;
284 while (hi >= lo) {
285 int32_t mid = (hi + lo) / 2;
286 const MethodId& method = GetMethodId(mid);
287 if (class_idx > method.class_idx_) {
288 lo = mid + 1;
289 } else if (class_idx < method.class_idx_) {
290 hi = mid - 1;
291 } else {
292 if (name_idx > method.name_idx_) {
293 lo = mid + 1;
294 } else if (name_idx < method.name_idx_) {
295 hi = mid - 1;
296 } else {
297 if (proto_idx > method.proto_idx_) {
298 lo = mid + 1;
299 } else if (proto_idx < method.proto_idx_) {
300 hi = mid - 1;
301 } else {
302 return &method;
303 }
304 }
305 }
306 }
307 return nullptr;
308 }
309
FindStringId(const char * string) const310 const StringId* DexFile::FindStringId(const char* string) const {
311 int32_t lo = 0;
312 int32_t hi = NumStringIds() - 1;
313 while (hi >= lo) {
314 int32_t mid = (hi + lo) / 2;
315 const StringId& str_id = GetStringId(dex::StringIndex(mid));
316 const char* str = GetStringData(str_id);
317 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
318 if (compare > 0) {
319 lo = mid + 1;
320 } else if (compare < 0) {
321 hi = mid - 1;
322 } else {
323 return &str_id;
324 }
325 }
326 return nullptr;
327 }
328
FindTypeId(const char * string) const329 const TypeId* DexFile::FindTypeId(const char* string) const {
330 int32_t lo = 0;
331 int32_t hi = NumTypeIds() - 1;
332 while (hi >= lo) {
333 int32_t mid = (hi + lo) / 2;
334 const TypeId& type_id = GetTypeId(dex::TypeIndex(mid));
335 const StringId& str_id = GetStringId(type_id.descriptor_idx_);
336 const char* str = GetStringData(str_id);
337 int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
338 if (compare > 0) {
339 lo = mid + 1;
340 } else if (compare < 0) {
341 hi = mid - 1;
342 } else {
343 return &type_id;
344 }
345 }
346 return nullptr;
347 }
348
FindTypeId(dex::StringIndex string_idx) const349 const TypeId* DexFile::FindTypeId(dex::StringIndex string_idx) const {
350 int32_t lo = 0;
351 int32_t hi = NumTypeIds() - 1;
352 while (hi >= lo) {
353 int32_t mid = (hi + lo) / 2;
354 const TypeId& type_id = GetTypeId(dex::TypeIndex(mid));
355 if (string_idx > type_id.descriptor_idx_) {
356 lo = mid + 1;
357 } else if (string_idx < type_id.descriptor_idx_) {
358 hi = mid - 1;
359 } else {
360 return &type_id;
361 }
362 }
363 return nullptr;
364 }
365
FindProtoId(dex::TypeIndex return_type_idx,const dex::TypeIndex * signature_type_idxs,uint32_t signature_length) const366 const ProtoId* DexFile::FindProtoId(dex::TypeIndex return_type_idx,
367 const dex::TypeIndex* signature_type_idxs,
368 uint32_t signature_length) const {
369 int32_t lo = 0;
370 int32_t hi = NumProtoIds() - 1;
371 while (hi >= lo) {
372 int32_t mid = (hi + lo) / 2;
373 const dex::ProtoIndex proto_idx = static_cast<dex::ProtoIndex>(mid);
374 const ProtoId& proto = GetProtoId(proto_idx);
375 int compare = return_type_idx.index_ - proto.return_type_idx_.index_;
376 if (compare == 0) {
377 DexFileParameterIterator it(*this, proto);
378 size_t i = 0;
379 while (it.HasNext() && i < signature_length && compare == 0) {
380 compare = signature_type_idxs[i].index_ - it.GetTypeIdx().index_;
381 it.Next();
382 i++;
383 }
384 if (compare == 0) {
385 if (it.HasNext()) {
386 compare = -1;
387 } else if (i < signature_length) {
388 compare = 1;
389 }
390 }
391 }
392 if (compare > 0) {
393 lo = mid + 1;
394 } else if (compare < 0) {
395 hi = mid - 1;
396 } else {
397 return &proto;
398 }
399 }
400 return nullptr;
401 }
402
403 // Given a signature place the type ids into the given vector
CreateTypeList(std::string_view signature,dex::TypeIndex * return_type_idx,std::vector<dex::TypeIndex> * param_type_idxs) const404 bool DexFile::CreateTypeList(std::string_view signature,
405 dex::TypeIndex* return_type_idx,
406 std::vector<dex::TypeIndex>* param_type_idxs) const {
407 if (signature[0] != '(') {
408 return false;
409 }
410 size_t offset = 1;
411 size_t end = signature.size();
412 bool process_return = false;
413 while (offset < end) {
414 size_t start_offset = offset;
415 char c = signature[offset];
416 offset++;
417 if (c == ')') {
418 process_return = true;
419 continue;
420 }
421 while (c == '[') { // process array prefix
422 if (offset >= end) { // expect some descriptor following [
423 return false;
424 }
425 c = signature[offset];
426 offset++;
427 }
428 if (c == 'L') { // process type descriptors
429 do {
430 if (offset >= end) { // unexpected early termination of descriptor
431 return false;
432 }
433 c = signature[offset];
434 offset++;
435 } while (c != ';');
436 }
437 // TODO: avoid creating a std::string just to get a 0-terminated char array
438 std::string descriptor(signature.data() + start_offset, offset - start_offset);
439 const TypeId* type_id = FindTypeId(descriptor.c_str());
440 if (type_id == nullptr) {
441 return false;
442 }
443 dex::TypeIndex type_idx = GetIndexForTypeId(*type_id);
444 if (!process_return) {
445 param_type_idxs->push_back(type_idx);
446 } else {
447 *return_type_idx = type_idx;
448 return offset == end; // return true if the signature had reached a sensible end
449 }
450 }
451 return false; // failed to correctly parse return type
452 }
453
FindTryItem(const TryItem * try_items,uint32_t tries_size,uint32_t address)454 int32_t DexFile::FindTryItem(const TryItem* try_items, uint32_t tries_size, uint32_t address) {
455 uint32_t min = 0;
456 uint32_t max = tries_size;
457 while (min < max) {
458 const uint32_t mid = (min + max) / 2;
459
460 const TryItem& ti = try_items[mid];
461 const uint32_t start = ti.start_addr_;
462 const uint32_t end = start + ti.insn_count_;
463
464 if (address < start) {
465 max = mid;
466 } else if (address >= end) {
467 min = mid + 1;
468 } else { // We have a winner!
469 return mid;
470 }
471 }
472 // No match.
473 return -1;
474 }
475
476 // Read a signed integer. "zwidth" is the zero-based byte count.
ReadSignedInt(const uint8_t * ptr,int zwidth)477 int32_t DexFile::ReadSignedInt(const uint8_t* ptr, int zwidth) {
478 int32_t val = 0;
479 for (int i = zwidth; i >= 0; --i) {
480 val = ((uint32_t)val >> 8) | (((int32_t)*ptr++) << 24);
481 }
482 val >>= (3 - zwidth) * 8;
483 return val;
484 }
485
486 // Read an unsigned integer. "zwidth" is the zero-based byte count,
487 // "fill_on_right" indicates which side we want to zero-fill from.
ReadUnsignedInt(const uint8_t * ptr,int zwidth,bool fill_on_right)488 uint32_t DexFile::ReadUnsignedInt(const uint8_t* ptr, int zwidth, bool fill_on_right) {
489 uint32_t val = 0;
490 for (int i = zwidth; i >= 0; --i) {
491 val = (val >> 8) | (((uint32_t)*ptr++) << 24);
492 }
493 if (!fill_on_right) {
494 val >>= (3 - zwidth) * 8;
495 }
496 return val;
497 }
498
499 // Read a signed long. "zwidth" is the zero-based byte count.
ReadSignedLong(const uint8_t * ptr,int zwidth)500 int64_t DexFile::ReadSignedLong(const uint8_t* ptr, int zwidth) {
501 int64_t val = 0;
502 for (int i = zwidth; i >= 0; --i) {
503 val = ((uint64_t)val >> 8) | (((int64_t)*ptr++) << 56);
504 }
505 val >>= (7 - zwidth) * 8;
506 return val;
507 }
508
509 // Read an unsigned long. "zwidth" is the zero-based byte count,
510 // "fill_on_right" indicates which side we want to zero-fill from.
ReadUnsignedLong(const uint8_t * ptr,int zwidth,bool fill_on_right)511 uint64_t DexFile::ReadUnsignedLong(const uint8_t* ptr, int zwidth, bool fill_on_right) {
512 uint64_t val = 0;
513 for (int i = zwidth; i >= 0; --i) {
514 val = (val >> 8) | (((uint64_t)*ptr++) << 56);
515 }
516 if (!fill_on_right) {
517 val >>= (7 - zwidth) * 8;
518 }
519 return val;
520 }
521
PrettyMethod(uint32_t method_idx,bool with_signature) const522 std::string DexFile::PrettyMethod(uint32_t method_idx, bool with_signature) const {
523 if (method_idx >= NumMethodIds()) {
524 return StringPrintf("<<invalid-method-idx-%d>>", method_idx);
525 }
526 const MethodId& method_id = GetMethodId(method_idx);
527 std::string result;
528 const ProtoId* proto_id = with_signature ? &GetProtoId(method_id.proto_idx_) : nullptr;
529 if (with_signature) {
530 AppendPrettyDescriptor(StringByTypeIdx(proto_id->return_type_idx_), &result);
531 result += ' ';
532 }
533 AppendPrettyDescriptor(GetMethodDeclaringClassDescriptor(method_id), &result);
534 result += '.';
535 result += GetMethodName(method_id);
536 if (with_signature) {
537 result += '(';
538 const TypeList* params = GetProtoParameters(*proto_id);
539 if (params != nullptr) {
540 const char* separator = "";
541 for (uint32_t i = 0u, size = params->Size(); i != size; ++i) {
542 result += separator;
543 separator = ", ";
544 AppendPrettyDescriptor(StringByTypeIdx(params->GetTypeItem(i).type_idx_), &result);
545 }
546 }
547 result += ')';
548 }
549 return result;
550 }
551
PrettyField(uint32_t field_idx,bool with_type) const552 std::string DexFile::PrettyField(uint32_t field_idx, bool with_type) const {
553 if (field_idx >= NumFieldIds()) {
554 return StringPrintf("<<invalid-field-idx-%d>>", field_idx);
555 }
556 const FieldId& field_id = GetFieldId(field_idx);
557 std::string result;
558 if (with_type) {
559 result += GetFieldTypeDescriptor(field_id);
560 result += ' ';
561 }
562 AppendPrettyDescriptor(GetFieldDeclaringClassDescriptor(field_id), &result);
563 result += '.';
564 result += GetFieldName(field_id);
565 return result;
566 }
567
PrettyType(dex::TypeIndex type_idx) const568 std::string DexFile::PrettyType(dex::TypeIndex type_idx) const {
569 if (type_idx.index_ >= NumTypeIds()) {
570 return StringPrintf("<<invalid-type-idx-%d>>", type_idx.index_);
571 }
572 const TypeId& type_id = GetTypeId(type_idx);
573 return PrettyDescriptor(GetTypeDescriptor(type_id));
574 }
575
GetProtoIndexForCallSite(uint32_t call_site_idx) const576 dex::ProtoIndex DexFile::GetProtoIndexForCallSite(uint32_t call_site_idx) const {
577 const CallSiteIdItem& csi = GetCallSiteId(call_site_idx);
578 CallSiteArrayValueIterator it(*this, csi);
579 it.Next();
580 it.Next();
581 DCHECK_EQ(EncodedArrayValueIterator::ValueType::kMethodType, it.GetValueType());
582 return dex::ProtoIndex(it.GetJavaValue().i);
583 }
584
585 // Checks that visibility is as expected. Includes special behavior for M and
586 // before to allow runtime and build visibility when expecting runtime.
operator <<(std::ostream & os,const DexFile & dex_file)587 std::ostream& operator<<(std::ostream& os, const DexFile& dex_file) {
588 os << StringPrintf("[DexFile: %s dex-checksum=%08x location-checksum=%08x %p-%p]",
589 dex_file.GetLocation().c_str(),
590 dex_file.GetHeader().checksum_, dex_file.GetLocationChecksum(),
591 dex_file.Begin(), dex_file.Begin() + dex_file.Size());
592 return os;
593 }
594
EncodedArrayValueIterator(const DexFile & dex_file,const uint8_t * array_data)595 EncodedArrayValueIterator::EncodedArrayValueIterator(const DexFile& dex_file,
596 const uint8_t* array_data)
597 : dex_file_(dex_file),
598 array_size_(),
599 pos_(-1),
600 ptr_(array_data),
601 type_(kByte) {
602 array_size_ = (ptr_ != nullptr) ? DecodeUnsignedLeb128(&ptr_) : 0;
603 if (array_size_ > 0) {
604 Next();
605 }
606 }
607
Next()608 void EncodedArrayValueIterator::Next() {
609 pos_++;
610 if (pos_ >= array_size_) {
611 return;
612 }
613 uint8_t value_type = *ptr_++;
614 uint8_t value_arg = value_type >> kEncodedValueArgShift;
615 size_t width = value_arg + 1; // assume and correct later
616 type_ = static_cast<ValueType>(value_type & kEncodedValueTypeMask);
617 switch (type_) {
618 case kBoolean:
619 jval_.i = (value_arg != 0) ? 1 : 0;
620 width = 0;
621 break;
622 case kByte:
623 jval_.i = DexFile::ReadSignedInt(ptr_, value_arg);
624 CHECK(IsInt<8>(jval_.i));
625 break;
626 case kShort:
627 jval_.i = DexFile::ReadSignedInt(ptr_, value_arg);
628 CHECK(IsInt<16>(jval_.i));
629 break;
630 case kChar:
631 jval_.i = DexFile::ReadUnsignedInt(ptr_, value_arg, false);
632 CHECK(IsUint<16>(jval_.i));
633 break;
634 case kInt:
635 jval_.i = DexFile::ReadSignedInt(ptr_, value_arg);
636 break;
637 case kLong:
638 jval_.j = DexFile::ReadSignedLong(ptr_, value_arg);
639 break;
640 case kFloat:
641 jval_.i = DexFile::ReadUnsignedInt(ptr_, value_arg, true);
642 break;
643 case kDouble:
644 jval_.j = DexFile::ReadUnsignedLong(ptr_, value_arg, true);
645 break;
646 case kString:
647 case kType:
648 case kMethodType:
649 case kMethodHandle:
650 jval_.i = DexFile::ReadUnsignedInt(ptr_, value_arg, false);
651 break;
652 case kField:
653 case kMethod:
654 case kEnum:
655 case kArray:
656 case kAnnotation:
657 UNIMPLEMENTED(FATAL) << ": type " << type_;
658 UNREACHABLE();
659 case kNull:
660 jval_.l = nullptr;
661 width = 0;
662 break;
663 default:
664 LOG(FATAL) << "Unreached";
665 UNREACHABLE();
666 }
667 ptr_ += width;
668 }
669
670 namespace dex {
671
operator <<(std::ostream & os,const ProtoIndex & index)672 std::ostream& operator<<(std::ostream& os, const ProtoIndex& index) {
673 os << "ProtoIndex[" << index.index_ << "]";
674 return os;
675 }
676
operator <<(std::ostream & os,const StringIndex & index)677 std::ostream& operator<<(std::ostream& os, const StringIndex& index) {
678 os << "StringIndex[" << index.index_ << "]";
679 return os;
680 }
681
operator <<(std::ostream & os,const TypeIndex & index)682 std::ostream& operator<<(std::ostream& os, const TypeIndex& index) {
683 os << "TypeIndex[" << index.index_ << "]";
684 return os;
685 }
686
687 } // namespace dex
688
689 } // namespace art
690