xref: /aosp_15_r20/art/libdexfile/dex/dex_file-inl.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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 #ifndef ART_LIBDEXFILE_DEX_DEX_FILE_INL_H_
18 #define ART_LIBDEXFILE_DEX_DEX_FILE_INL_H_
19 
20 #include "dex_file.h"
21 
22 #include "base/casts.h"
23 #include "base/iteration_range.h"
24 #include "base/leb128.h"
25 #include "base/utils.h"
26 #include "class_iterator.h"
27 #include "compact_dex_file.h"
28 #include "dex_instruction_iterator.h"
29 #include "invoke_type.h"
30 #include "signature.h"
31 #include "standard_dex_file.h"
32 
33 namespace art {
34 
CompareDescriptors(std::string_view lhs,std::string_view rhs)35 inline int DexFile::CompareDescriptors(std::string_view lhs, std::string_view rhs) {
36   // Note: `std::string_view::compare()` uses lexicographical comparison and treats the `char`
37   // as unsigned; for Modified-UTF-8 without embedded nulls this is consistent with the
38   // `CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues()` ordering.
39   return lhs.compare(rhs);
40 }
41 
CompareMemberNames(std::string_view lhs,std::string_view rhs)42 inline int DexFile::CompareMemberNames(std::string_view lhs, std::string_view rhs) {
43   // Note: `std::string_view::compare()` uses lexicographical comparison and treats the `char`
44   // as unsigned; for Modified-UTF-8 without embedded nulls this is consistent with the
45   // `CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues()` ordering.
46   return lhs.compare(rhs);
47 }
48 
Utf8Length(const char * utf8_data,size_t utf16_length)49 inline size_t DexFile::Utf8Length(const char* utf8_data, size_t utf16_length) {
50   return LIKELY(utf8_data[utf16_length] == 0)  // Is ASCII?
51              ? utf16_length
52              : utf16_length + strlen(utf8_data + utf16_length);
53 }
54 
StringViewFromUtf16Length(const char * utf8_data,size_t utf16_length)55 inline std::string_view DexFile::StringViewFromUtf16Length(const char* utf8_data,
56                                                            size_t utf16_length) {
57   return std::string_view(utf8_data, Utf8Length(utf8_data, utf16_length));
58 }
59 
60 ALWAYS_INLINE
GetStringDataAndUtf16Length(const dex::StringId & string_id,uint32_t * utf16_length)61 inline const char* DexFile::GetStringDataAndUtf16Length(const dex::StringId& string_id,
62                                                         uint32_t* utf16_length) const {
63   DCHECK(utf16_length != nullptr) << GetLocation();
64   const uint8_t* ptr = DataBegin() + string_id.string_data_off_;
65   *utf16_length = DecodeUnsignedLeb128(&ptr);
66   return reinterpret_cast<const char*>(ptr);
67 }
68 
69 ALWAYS_INLINE
GetStringDataAndUtf16Length(const dex::StringIndex string_idx,uint32_t * utf16_length)70 inline const char* DexFile::GetStringDataAndUtf16Length(const dex::StringIndex string_idx,
71                                                         uint32_t* utf16_length) const {
72   return GetStringDataAndUtf16Length(GetStringId(string_idx), utf16_length);
73 }
74 
75 ALWAYS_INLINE
GetStringUtf16Length(const dex::StringId & string_id)76 inline uint32_t DexFile::GetStringUtf16Length(const dex::StringId& string_id) const {
77   uint32_t utf16_length;
78   GetStringDataAndUtf16Length(string_id, &utf16_length);
79   return utf16_length;
80 }
81 
82 ALWAYS_INLINE
GetStringData(const dex::StringId & string_id)83 inline const char* DexFile::GetStringData(const dex::StringId& string_id) const {
84   uint32_t ignored;
85   return GetStringDataAndUtf16Length(string_id, &ignored);
86 }
87 
88 ALWAYS_INLINE
GetStringData(dex::StringIndex string_idx)89 inline const char* DexFile::GetStringData(dex::StringIndex string_idx) const {
90   return GetStringData(GetStringId(string_idx));
91 }
92 
93 ALWAYS_INLINE
GetStringView(const dex::StringId & string_id)94 inline std::string_view DexFile::GetStringView(const dex::StringId& string_id) const {
95   uint32_t utf16_length;
96   const char* data = GetStringDataAndUtf16Length(string_id, &utf16_length);
97   return StringViewFromUtf16Length(data, utf16_length);
98 }
99 
100 ALWAYS_INLINE
GetStringView(dex::StringIndex string_idx)101 inline std::string_view DexFile::GetStringView(dex::StringIndex string_idx) const {
102   return GetStringView(GetStringId(string_idx));
103 }
104 
GetTypeDescriptor(const dex::TypeId & type_id)105 inline const char* DexFile::GetTypeDescriptor(const dex::TypeId& type_id) const {
106   return GetStringData(type_id.descriptor_idx_);
107 }
108 
GetTypeDescriptor(dex::TypeIndex type_idx)109 inline const char* DexFile::GetTypeDescriptor(dex::TypeIndex type_idx) const {
110   return GetTypeDescriptor(GetTypeId(type_idx));
111 }
112 
GetTypeDescriptorView(const dex::TypeId & type_id)113 inline std::string_view DexFile::GetTypeDescriptorView(const dex::TypeId& type_id) const {
114   return GetStringView(type_id.descriptor_idx_);
115 }
116 
GetTypeDescriptorView(dex::TypeIndex type_idx)117 inline std::string_view DexFile::GetTypeDescriptorView(dex::TypeIndex type_idx) const {
118   return GetTypeDescriptorView(GetTypeId(type_idx));
119 }
120 
GetFieldDeclaringClassDescriptor(const dex::FieldId & field_id)121 inline const char* DexFile::GetFieldDeclaringClassDescriptor(const dex::FieldId& field_id) const {
122   return GetTypeDescriptor(field_id.class_idx_);
123 }
124 
GetFieldDeclaringClassDescriptor(uint32_t field_idx)125 inline const char* DexFile::GetFieldDeclaringClassDescriptor(uint32_t field_idx) const {
126   return GetFieldDeclaringClassDescriptor(GetFieldId(field_idx));
127 }
128 
GetFieldDeclaringClassDescriptorView(const dex::FieldId & field_id)129 inline std::string_view DexFile::GetFieldDeclaringClassDescriptorView(
130     const dex::FieldId& field_id) const {
131   return GetTypeDescriptorView(field_id.class_idx_);
132 }
133 
GetFieldDeclaringClassDescriptorView(uint32_t field_idx)134 inline std::string_view DexFile::GetFieldDeclaringClassDescriptorView(uint32_t field_idx) const {
135   return GetFieldDeclaringClassDescriptorView(GetFieldId(field_idx));
136 }
137 
GetFieldTypeDescriptor(const dex::FieldId & field_id)138 inline const char* DexFile::GetFieldTypeDescriptor(const dex::FieldId& field_id) const {
139   return GetTypeDescriptor(field_id.type_idx_);
140 }
141 
GetFieldTypeDescriptor(uint32_t field_idx)142 inline const char* DexFile::GetFieldTypeDescriptor(uint32_t field_idx) const {
143   return GetFieldTypeDescriptor(GetFieldId(field_idx));
144 }
145 
GetFieldTypeDescriptorView(const dex::FieldId & field_id)146 inline std::string_view DexFile::GetFieldTypeDescriptorView(const dex::FieldId& field_id) const {
147   return GetTypeDescriptorView(field_id.type_idx_);
148 }
149 
GetFieldTypeDescriptorView(uint32_t field_idx)150 inline std::string_view DexFile::GetFieldTypeDescriptorView(uint32_t field_idx) const {
151   return GetFieldTypeDescriptorView(GetFieldId(field_idx));
152 }
153 
GetFieldName(const dex::FieldId & field_id)154 inline const char* DexFile::GetFieldName(const dex::FieldId& field_id) const {
155   return GetStringData(field_id.name_idx_);
156 }
157 
GetFieldName(uint32_t field_idx)158 inline const char* DexFile::GetFieldName(uint32_t field_idx) const {
159   return GetFieldName(GetFieldId(field_idx));
160 }
161 
GetFieldNameView(const dex::FieldId & field_id)162 inline std::string_view DexFile::GetFieldNameView(const dex::FieldId& field_id) const {
163   return GetStringView(field_id.name_idx_);
164 }
165 
GetFieldNameView(uint32_t field_idx)166 inline std::string_view DexFile::GetFieldNameView(uint32_t field_idx) const {
167   return GetFieldNameView(GetFieldId(field_idx));
168 }
169 
GetMethodDeclaringClassDescriptor(const dex::MethodId & method_id)170 inline const char* DexFile::GetMethodDeclaringClassDescriptor(
171     const dex::MethodId& method_id) const {
172   return GetTypeDescriptor(method_id.class_idx_);
173 }
174 
GetMethodDeclaringClassDescriptor(uint32_t method_idx)175 inline const char* DexFile::GetMethodDeclaringClassDescriptor(uint32_t method_idx) const {
176   return GetMethodDeclaringClassDescriptor(GetMethodId(method_idx));
177 }
178 
GetMethodDeclaringClassDescriptorView(const dex::MethodId & method_id)179 inline std::string_view DexFile::GetMethodDeclaringClassDescriptorView(
180     const dex::MethodId& method_id) const {
181   return GetTypeDescriptorView(method_id.class_idx_);
182 }
183 
GetMethodDeclaringClassDescriptorView(uint32_t method_idx)184 inline std::string_view DexFile::GetMethodDeclaringClassDescriptorView(uint32_t method_idx) const {
185   return GetMethodDeclaringClassDescriptorView(GetMethodId(method_idx));
186 }
187 
GetMethodSignature(const dex::MethodId & method_id)188 inline const Signature DexFile::GetMethodSignature(const dex::MethodId& method_id) const {
189   return Signature(this, GetProtoId(method_id.proto_idx_));
190 }
191 
GetProtoSignature(const dex::ProtoId & proto_id)192 inline const Signature DexFile::GetProtoSignature(const dex::ProtoId& proto_id) const {
193   return Signature(this, proto_id);
194 }
195 
GetMethodName(const dex::MethodId & method_id)196 inline const char* DexFile::GetMethodName(const dex::MethodId& method_id) const {
197   return GetStringData(method_id.name_idx_);
198 }
199 
GetMethodName(const dex::MethodId & method_id,uint32_t * utf_length)200 inline const char* DexFile::GetMethodName(const dex::MethodId& method_id, uint32_t* utf_length)
201     const {
202   return GetStringDataAndUtf16Length(method_id.name_idx_, utf_length);
203 }
204 
GetMethodName(uint32_t method_idx)205 inline const char* DexFile::GetMethodName(uint32_t method_idx) const {
206   return GetStringData(GetMethodId(method_idx).name_idx_);
207 }
208 
GetMethodName(uint32_t idx,uint32_t * utf_length)209 inline const char* DexFile::GetMethodName(uint32_t idx, uint32_t* utf_length) const {
210   return GetStringDataAndUtf16Length(GetMethodId(idx).name_idx_, utf_length);
211 }
212 
213 ALWAYS_INLINE
GetMethodNameView(const dex::MethodId & method_id)214 inline std::string_view DexFile::GetMethodNameView(const dex::MethodId& method_id) const {
215   return GetStringView(method_id.name_idx_);
216 }
217 
218 ALWAYS_INLINE
GetMethodNameView(uint32_t method_idx)219 inline std::string_view DexFile::GetMethodNameView(uint32_t method_idx) const {
220   return GetMethodNameView(GetMethodId(method_idx));
221 }
222 
GetMethodShorty(uint32_t idx)223 inline const char* DexFile::GetMethodShorty(uint32_t idx) const {
224   return GetMethodShorty(GetMethodId(idx));
225 }
226 
GetMethodShortyView(uint32_t idx)227 inline std::string_view DexFile::GetMethodShortyView(uint32_t idx) const {
228   return GetMethodShortyView(GetMethodId(idx));
229 }
230 
GetMethodShorty(const dex::MethodId & method_id)231 inline const char* DexFile::GetMethodShorty(const dex::MethodId& method_id) const {
232   return GetStringData(GetProtoId(method_id.proto_idx_).shorty_idx_);
233 }
234 
GetMethodShorty(const dex::MethodId & method_id,uint32_t * length)235 inline const char* DexFile::GetMethodShorty(const dex::MethodId& method_id, uint32_t* length)
236     const {
237   // Using the UTF16 length is safe here as shorties are guaranteed to be ASCII characters.
238   return GetStringDataAndUtf16Length(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
239 }
240 
GetMethodShortyView(const dex::MethodId & method_id)241 inline std::string_view DexFile::GetMethodShortyView(const dex::MethodId& method_id) const {
242   return GetShortyView(method_id.proto_idx_);
243 }
244 
GetClassDescriptor(const dex::ClassDef & class_def)245 inline const char* DexFile::GetClassDescriptor(const dex::ClassDef& class_def) const {
246   return GetTypeDescriptor(class_def.class_idx_);
247 }
248 
GetReturnTypeDescriptor(const dex::ProtoId & proto_id)249 inline const char* DexFile::GetReturnTypeDescriptor(const dex::ProtoId& proto_id) const {
250   return GetTypeDescriptor(proto_id.return_type_idx_);
251 }
252 
GetShorty(dex::ProtoIndex proto_idx)253 inline const char* DexFile::GetShorty(dex::ProtoIndex proto_idx) const {
254   const dex::ProtoId& proto_id = GetProtoId(proto_idx);
255   return GetStringData(proto_id.shorty_idx_);
256 }
257 
258 ALWAYS_INLINE
GetShortyView(dex::ProtoIndex proto_idx)259 inline std::string_view DexFile::GetShortyView(dex::ProtoIndex proto_idx) const {
260   return GetShortyView(GetProtoId(proto_idx));
261 }
262 
263 ALWAYS_INLINE
GetShortyView(const dex::ProtoId & proto_id)264 inline std::string_view DexFile::GetShortyView(const dex::ProtoId& proto_id) const {
265   uint32_t shorty_len;
266   const char* shorty_data = GetStringDataAndUtf16Length(proto_id.shorty_idx_, &shorty_len);
267   DCHECK_EQ(shorty_data[shorty_len], '\0');  // For a shorty utf16 length == mutf8 length.
268   return std::string_view(shorty_data, shorty_len);
269 }
270 
GetTryItems(const DexInstructionIterator & code_item_end,uint32_t offset)271 inline const dex::TryItem* DexFile::GetTryItems(const DexInstructionIterator& code_item_end,
272                                                 uint32_t offset) {
273   return reinterpret_cast<const dex::TryItem*>
274       (RoundUp(reinterpret_cast<uintptr_t>(&code_item_end.Inst()), dex::TryItem::kAlignment)) +
275           offset;
276 }
277 
StringEquals(const DexFile * df1,dex::StringIndex sidx1,const DexFile * df2,dex::StringIndex sidx2)278 inline bool DexFile::StringEquals(const DexFile* df1, dex::StringIndex sidx1,
279                                   const DexFile* df2, dex::StringIndex sidx2) {
280   uint32_t s1_len;  // Note: utf16 length != mutf8 length.
281   const char* s1_data = df1->GetStringDataAndUtf16Length(sidx1, &s1_len);
282   uint32_t s2_len;
283   const char* s2_data = df2->GetStringDataAndUtf16Length(sidx2, &s2_len);
284   return (s1_len == s2_len) && (strcmp(s1_data, s2_data) == 0);
285 }
286 
287 template<typename NewLocalCallback, typename IndexToStringData, typename TypeIndexToStringData>
DecodeDebugLocalInfo(const uint8_t * stream,const std::string & location,const char * declaring_class_descriptor,const std::vector<const char * > & arg_descriptors,const std::string & method_name,bool is_static,uint16_t registers_size,uint16_t ins_size,uint16_t insns_size_in_code_units,const IndexToStringData & index_to_string_data,const TypeIndexToStringData & type_index_to_string_data,const NewLocalCallback & new_local_callback)288 bool DexFile::DecodeDebugLocalInfo(const uint8_t* stream,
289                                    const std::string& location,
290                                    const char* declaring_class_descriptor,
291                                    const std::vector<const char*>& arg_descriptors,
292                                    const std::string& method_name,
293                                    bool is_static,
294                                    uint16_t registers_size,
295                                    uint16_t ins_size,
296                                    uint16_t insns_size_in_code_units,
297                                    const IndexToStringData& index_to_string_data,
298                                    const TypeIndexToStringData& type_index_to_string_data,
299                                    const NewLocalCallback& new_local_callback) {
300   if (stream == nullptr) {
301     return false;
302   }
303   std::vector<LocalInfo> local_in_reg(registers_size);
304 
305   uint16_t arg_reg = registers_size - ins_size;
306   if (!is_static) {
307     const char* descriptor = declaring_class_descriptor;
308     local_in_reg[arg_reg].name_ = "this";
309     local_in_reg[arg_reg].descriptor_ = descriptor;
310     local_in_reg[arg_reg].signature_ = nullptr;
311     local_in_reg[arg_reg].start_address_ = 0;
312     local_in_reg[arg_reg].reg_ = arg_reg;
313     local_in_reg[arg_reg].is_live_ = true;
314     arg_reg++;
315   }
316 
317   DecodeUnsignedLeb128(&stream);  // Line.
318   uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
319   uint32_t i;
320   if (parameters_size != arg_descriptors.size()) {
321     LOG(ERROR) << "invalid stream - problem with parameter iterator in " << location
322                << " for method " << method_name;
323     return false;
324   }
325   for (i = 0; i < parameters_size && i < arg_descriptors.size(); ++i) {
326     if (arg_reg >= registers_size) {
327       LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
328                  << " >= " << registers_size << ") in " << location;
329       return false;
330     }
331     uint32_t name_idx = DecodeUnsignedLeb128P1(&stream);
332     const char* descriptor = arg_descriptors[i];
333     local_in_reg[arg_reg].name_ = index_to_string_data(name_idx);
334     local_in_reg[arg_reg].descriptor_ = descriptor;
335     local_in_reg[arg_reg].signature_ = nullptr;
336     local_in_reg[arg_reg].start_address_ = 0;
337     local_in_reg[arg_reg].reg_ = arg_reg;
338     local_in_reg[arg_reg].is_live_ = true;
339     switch (*descriptor) {
340       case 'D':
341       case 'J':
342         arg_reg += 2;
343         break;
344       default:
345         arg_reg += 1;
346         break;
347     }
348   }
349 
350   uint32_t address = 0;
351   for (;;)  {
352     uint8_t opcode = *stream++;
353     switch (opcode) {
354       case DBG_END_SEQUENCE:
355         // Emit all variables which are still alive at the end of the method.
356         for (uint16_t reg = 0; reg < registers_size; reg++) {
357           if (local_in_reg[reg].is_live_) {
358             local_in_reg[reg].end_address_ = insns_size_in_code_units;
359             new_local_callback(local_in_reg[reg]);
360           }
361         }
362         return true;
363       case DBG_ADVANCE_PC:
364         address += DecodeUnsignedLeb128(&stream);
365         break;
366       case DBG_ADVANCE_LINE:
367         DecodeSignedLeb128(&stream);  // Line.
368         break;
369       case DBG_START_LOCAL:
370       case DBG_START_LOCAL_EXTENDED: {
371         uint16_t reg = DecodeUnsignedLeb128(&stream);
372         if (reg >= registers_size) {
373           LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= "
374                      << registers_size << ") in " << location;
375           return false;
376         }
377 
378         uint32_t name_idx = DecodeUnsignedLeb128P1(&stream);
379         uint16_t descriptor_idx = DecodeUnsignedLeb128P1(&stream);
380         uint32_t signature_idx = dex::kDexNoIndex;
381         if (opcode == DBG_START_LOCAL_EXTENDED) {
382           signature_idx = DecodeUnsignedLeb128P1(&stream);
383         }
384 
385         // Emit what was previously there, if anything
386         if (local_in_reg[reg].is_live_) {
387           local_in_reg[reg].end_address_ = address;
388           // Parameters with generic types cannot be encoded in the debug_info_item header. So d8
389           // encodes it as null in the header with start and end address as 0. There will be a
390           // START_LOCAL_EXTENDED that will declare the parameter with correct signature
391           // Debuggers get confused when they see empty ranges. So don't emit them.
392           // See b/297843934 for more details.
393           if (local_in_reg[reg].end_address_ != 0) {
394             new_local_callback(local_in_reg[reg]);
395           }
396         }
397 
398         local_in_reg[reg].name_ = index_to_string_data(name_idx);
399         local_in_reg[reg].descriptor_ = type_index_to_string_data(descriptor_idx);
400         local_in_reg[reg].signature_ = index_to_string_data(signature_idx);
401         local_in_reg[reg].start_address_ = address;
402         local_in_reg[reg].reg_ = reg;
403         local_in_reg[reg].is_live_ = true;
404         break;
405       }
406       case DBG_END_LOCAL: {
407         uint16_t reg = DecodeUnsignedLeb128(&stream);
408         if (reg >= registers_size) {
409           LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= "
410                      << registers_size << ") in " << location;
411           return false;
412         }
413         // If the register is live, close it properly. Otherwise, closing an already
414         // closed register is sloppy, but harmless if no further action is taken.
415         if (local_in_reg[reg].is_live_) {
416           local_in_reg[reg].end_address_ = address;
417           new_local_callback(local_in_reg[reg]);
418           local_in_reg[reg].is_live_ = false;
419         }
420         break;
421       }
422       case DBG_RESTART_LOCAL: {
423         uint16_t reg = DecodeUnsignedLeb128(&stream);
424         if (reg >= registers_size) {
425           LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= "
426                      << registers_size << ") in " << location;
427           return false;
428         }
429         // If the register is live, the "restart" is superfluous,
430         // and we don't want to mess with the existing start address.
431         if (!local_in_reg[reg].is_live_) {
432           local_in_reg[reg].start_address_ = address;
433           local_in_reg[reg].is_live_ = true;
434         }
435         break;
436       }
437       case DBG_SET_PROLOGUE_END:
438       case DBG_SET_EPILOGUE_BEGIN:
439         break;
440       case DBG_SET_FILE:
441         DecodeUnsignedLeb128P1(&stream);  // name.
442         break;
443       default:
444         address += (opcode - DBG_FIRST_SPECIAL) / DBG_LINE_RANGE;
445         break;
446     }
447   }
448 }
449 
450 template<typename NewLocalCallback>
DecodeDebugLocalInfo(uint32_t registers_size,uint32_t ins_size,uint32_t insns_size_in_code_units,uint32_t debug_info_offset,bool is_static,uint32_t method_idx,const NewLocalCallback & new_local_callback)451 bool DexFile::DecodeDebugLocalInfo(uint32_t registers_size,
452                                    uint32_t ins_size,
453                                    uint32_t insns_size_in_code_units,
454                                    uint32_t debug_info_offset,
455                                    bool is_static,
456                                    uint32_t method_idx,
457                                    const NewLocalCallback& new_local_callback) const {
458   const uint8_t* const stream = GetDebugInfoStream(debug_info_offset);
459   if (stream == nullptr) {
460     return false;
461   }
462   std::vector<const char*> arg_descriptors;
463   DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
464   for (; it.HasNext(); it.Next()) {
465     arg_descriptors.push_back(it.GetDescriptor());
466   }
467   return DecodeDebugLocalInfo(stream,
468                               GetLocation(),
469                               GetMethodDeclaringClassDescriptor(GetMethodId(method_idx)),
470                               arg_descriptors,
471                               this->PrettyMethod(method_idx),
472                               is_static,
473                               registers_size,
474                               ins_size,
475                               insns_size_in_code_units,
476                               [this](uint32_t idx) {
477                                 dex::StringIndex string_idx(idx);
478                                 return string_idx.IsValid() ? GetStringData(string_idx) : nullptr;
479                               },
480                               [this](uint16_t idx) {
481                                 dex::TypeIndex type_idx(idx);
482                                 return type_idx.IsValid() ? GetTypeDescriptor(type_idx) : nullptr;
483                               },
484                               new_local_callback);
485 }
486 
487 template<typename DexDebugNewPosition, typename IndexToStringData>
DecodeDebugPositionInfo(const uint8_t * stream,IndexToStringData && index_to_string_data,DexDebugNewPosition && position_functor)488 bool DexFile::DecodeDebugPositionInfo(const uint8_t* stream,
489                                       IndexToStringData&& index_to_string_data,
490                                       DexDebugNewPosition&& position_functor) {
491   if (stream == nullptr) {
492     return false;
493   }
494 
495   PositionInfo entry;
496   entry.line_ = DecodeDebugInfoParameterNames(&stream, VoidFunctor());
497 
498   for (;;)  {
499     uint8_t opcode = *stream++;
500     switch (opcode) {
501       case DBG_END_SEQUENCE:
502         return true;  // end of stream.
503       case DBG_ADVANCE_PC:
504         entry.address_ += DecodeUnsignedLeb128(&stream);
505         break;
506       case DBG_ADVANCE_LINE:
507         entry.line_ += DecodeSignedLeb128(&stream);
508         break;
509       case DBG_START_LOCAL:
510         DecodeUnsignedLeb128(&stream);  // reg.
511         DecodeUnsignedLeb128P1(&stream);  // name.
512         DecodeUnsignedLeb128P1(&stream);  // descriptor.
513         break;
514       case DBG_START_LOCAL_EXTENDED:
515         DecodeUnsignedLeb128(&stream);  // reg.
516         DecodeUnsignedLeb128P1(&stream);  // name.
517         DecodeUnsignedLeb128P1(&stream);  // descriptor.
518         DecodeUnsignedLeb128P1(&stream);  // signature.
519         break;
520       case DBG_END_LOCAL:
521       case DBG_RESTART_LOCAL:
522         DecodeUnsignedLeb128(&stream);  // reg.
523         break;
524       case DBG_SET_PROLOGUE_END:
525         entry.prologue_end_ = true;
526         break;
527       case DBG_SET_EPILOGUE_BEGIN:
528         entry.epilogue_begin_ = true;
529         break;
530       case DBG_SET_FILE: {
531         uint32_t name_idx = DecodeUnsignedLeb128P1(&stream);
532         entry.source_file_ = index_to_string_data(name_idx);
533         break;
534       }
535       default: {
536         int adjopcode = opcode - DBG_FIRST_SPECIAL;
537         entry.address_ += adjopcode / DBG_LINE_RANGE;
538         entry.line_ += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
539         if (position_functor(entry)) {
540           return true;  // early exit.
541         }
542         entry.prologue_end_ = false;
543         entry.epilogue_begin_ = false;
544         break;
545       }
546     }
547   }
548 }
549 
AsCompactDexFile()550 inline const CompactDexFile* DexFile::AsCompactDexFile() const {
551   DCHECK(IsCompactDexFile());
552   return down_cast<const CompactDexFile*>(this);
553 }
554 
AsStandardDexFile()555 inline const StandardDexFile* DexFile::AsStandardDexFile() const {
556   DCHECK(IsStandardDexFile());
557   return down_cast<const StandardDexFile*>(this);
558 }
559 
560 // Get the base of the encoded data for the given DexCode.
GetCatchHandlerData(const DexInstructionIterator & code_item_end,uint32_t tries_size,uint32_t offset)561 inline const uint8_t* DexFile::GetCatchHandlerData(const DexInstructionIterator& code_item_end,
562                                                    uint32_t tries_size,
563                                                    uint32_t offset) {
564   const uint8_t* handler_data =
565       reinterpret_cast<const uint8_t*>(GetTryItems(code_item_end, tries_size));
566   return handler_data + offset;
567 }
568 
GetClasses()569 inline IterationRange<ClassIterator> DexFile::GetClasses() const {
570   return { ClassIterator(*this, 0u), ClassIterator(*this, NumClassDefs()) };
571 }
572 
573 // Returns the line number
574 template <typename Visitor>
DecodeDebugInfoParameterNames(const uint8_t ** debug_info,Visitor && visitor)575 inline uint32_t DexFile::DecodeDebugInfoParameterNames(const uint8_t** debug_info,
576                                                        Visitor&& visitor) {
577   uint32_t line = DecodeUnsignedLeb128(debug_info);
578   const uint32_t parameters_size = DecodeUnsignedLeb128(debug_info);
579   for (uint32_t i = 0; i < parameters_size; ++i) {
580     visitor(dex::StringIndex(DecodeUnsignedLeb128P1(debug_info)));
581   }
582   return line;
583 }
584 
585 }  // namespace art
586 
587 #endif  // ART_LIBDEXFILE_DEX_DEX_FILE_INL_H_
588