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 
StringViewFromUtf16Length(const char * utf8_data,size_t utf16_length)35 inline std::string_view StringViewFromUtf16Length(const char* utf8_data, size_t utf16_length) {
36   size_t utf8_length = LIKELY(utf8_data[utf16_length] == 0)  // Is ASCII?
37                            ? utf16_length
38                            : utf16_length + strlen(utf8_data + utf16_length);
39   return std::string_view(utf8_data, utf8_length);
40 }
41 
GetStringLength(const dex::StringId & string_id)42 inline int32_t DexFile::GetStringLength(const dex::StringId& string_id) const {
43   const uint8_t* ptr = DataBegin() + string_id.string_data_off_;
44   return DecodeUnsignedLeb128(&ptr);
45 }
46 
GetStringDataAndUtf16Length(const dex::StringId & string_id,uint32_t * utf16_length)47 inline const char* DexFile::GetStringDataAndUtf16Length(const dex::StringId& string_id,
48                                                         uint32_t* utf16_length) const {
49   DCHECK(utf16_length != nullptr) << GetLocation();
50   const uint8_t* ptr = DataBegin() + string_id.string_data_off_;
51   *utf16_length = DecodeUnsignedLeb128(&ptr);
52   return reinterpret_cast<const char*>(ptr);
53 }
54 
GetStringData(const dex::StringId & string_id)55 inline const char* DexFile::GetStringData(const dex::StringId& string_id) const {
56   uint32_t ignored;
57   return GetStringDataAndUtf16Length(string_id, &ignored);
58 }
59 
StringDataAndUtf16LengthByIdx(dex::StringIndex idx,uint32_t * utf16_length)60 inline const char* DexFile::StringDataAndUtf16LengthByIdx(dex::StringIndex idx,
61                                                           uint32_t* utf16_length) const {
62   if (!idx.IsValid()) {
63     *utf16_length = 0;
64     return nullptr;
65   }
66   const dex::StringId& string_id = GetStringId(idx);
67   return GetStringDataAndUtf16Length(string_id, utf16_length);
68 }
69 
StringDataByIdx(dex::StringIndex idx)70 inline const char* DexFile::StringDataByIdx(dex::StringIndex idx) const {
71   uint32_t unicode_length;
72   return StringDataAndUtf16LengthByIdx(idx, &unicode_length);
73 }
74 
StringViewByIdx(dex::StringIndex idx)75 inline std::string_view DexFile::StringViewByIdx(dex::StringIndex idx) const {
76   uint32_t unicode_length;
77   const char* data = StringDataAndUtf16LengthByIdx(idx, &unicode_length);
78   return data != nullptr ? StringViewFromUtf16Length(data, unicode_length) : std::string_view("");
79 }
80 
StringByTypeIdx(dex::TypeIndex idx,uint32_t * unicode_length)81 inline const char* DexFile::StringByTypeIdx(dex::TypeIndex idx, uint32_t* unicode_length) const {
82   if (!idx.IsValid()) {
83     return nullptr;
84   }
85   const dex::TypeId& type_id = GetTypeId(idx);
86   return StringDataAndUtf16LengthByIdx(type_id.descriptor_idx_, unicode_length);
87 }
88 
StringByTypeIdx(dex::TypeIndex idx)89 inline const char* DexFile::StringByTypeIdx(dex::TypeIndex idx) const {
90   if (!idx.IsValid()) {
91     return nullptr;
92   }
93   const dex::TypeId& type_id = GetTypeId(idx);
94   return StringDataByIdx(type_id.descriptor_idx_);
95 }
96 
GetTypeDescriptor(const dex::TypeId & type_id)97 inline const char* DexFile::GetTypeDescriptor(const dex::TypeId& type_id) const {
98   return StringDataByIdx(type_id.descriptor_idx_);
99 }
100 
GetFieldTypeDescriptor(const dex::FieldId & field_id)101 inline const char* DexFile::GetFieldTypeDescriptor(const dex::FieldId& field_id) const {
102   const dex::TypeId& type_id = GetTypeId(field_id.type_idx_);
103   return GetTypeDescriptor(type_id);
104 }
105 
GetFieldName(const dex::FieldId & field_id)106 inline const char* DexFile::GetFieldName(const dex::FieldId& field_id) const {
107   return StringDataByIdx(field_id.name_idx_);
108 }
109 
GetMethodDeclaringClassDescriptor(const dex::MethodId & method_id)110 inline const char* DexFile::GetMethodDeclaringClassDescriptor(const dex::MethodId& method_id)
111     const {
112   const dex::TypeId& type_id = GetTypeId(method_id.class_idx_);
113   return GetTypeDescriptor(type_id);
114 }
115 
GetMethodSignature(const dex::MethodId & method_id)116 inline const Signature DexFile::GetMethodSignature(const dex::MethodId& method_id) const {
117   return Signature(this, GetProtoId(method_id.proto_idx_));
118 }
119 
GetProtoSignature(const dex::ProtoId & proto_id)120 inline const Signature DexFile::GetProtoSignature(const dex::ProtoId& proto_id) const {
121   return Signature(this, proto_id);
122 }
123 
GetMethodName(const dex::MethodId & method_id)124 inline const char* DexFile::GetMethodName(const dex::MethodId& method_id) const {
125   return StringDataByIdx(method_id.name_idx_);
126 }
127 
GetMethodName(const dex::MethodId & method_id,uint32_t * utf_length)128 inline const char* DexFile::GetMethodName(const dex::MethodId& method_id, uint32_t* utf_length)
129     const {
130   return StringDataAndUtf16LengthByIdx(method_id.name_idx_, utf_length);
131 }
132 
GetMethodName(uint32_t idx)133 inline const char* DexFile::GetMethodName(uint32_t idx) const {
134   return StringDataByIdx(GetMethodId(idx).name_idx_);
135 }
136 
GetMethodName(uint32_t idx,uint32_t * utf_length)137 inline const char* DexFile::GetMethodName(uint32_t idx, uint32_t* utf_length) const {
138   return StringDataAndUtf16LengthByIdx(GetMethodId(idx).name_idx_, utf_length);
139 }
140 
GetMethodShorty(uint32_t idx)141 inline const char* DexFile::GetMethodShorty(uint32_t idx) const {
142   return StringDataByIdx(GetProtoId(GetMethodId(idx).proto_idx_).shorty_idx_);
143 }
144 
GetMethodShorty(const dex::MethodId & method_id)145 inline const char* DexFile::GetMethodShorty(const dex::MethodId& method_id) const {
146   return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_);
147 }
148 
GetMethodShorty(const dex::MethodId & method_id,uint32_t * length)149 inline const char* DexFile::GetMethodShorty(const dex::MethodId& method_id, uint32_t* length)
150     const {
151   // Using the UTF16 length is safe here as shorties are guaranteed to be ASCII characters.
152   return StringDataAndUtf16LengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length);
153 }
154 
GetClassDescriptor(const dex::ClassDef & class_def)155 inline const char* DexFile::GetClassDescriptor(const dex::ClassDef& class_def) const {
156   return StringByTypeIdx(class_def.class_idx_);
157 }
158 
GetReturnTypeDescriptor(const dex::ProtoId & proto_id)159 inline const char* DexFile::GetReturnTypeDescriptor(const dex::ProtoId& proto_id) const {
160   return StringByTypeIdx(proto_id.return_type_idx_);
161 }
162 
GetShorty(dex::ProtoIndex proto_idx)163 inline const char* DexFile::GetShorty(dex::ProtoIndex proto_idx) const {
164   const dex::ProtoId& proto_id = GetProtoId(proto_idx);
165   return StringDataByIdx(proto_id.shorty_idx_);
166 }
167 
GetTryItems(const DexInstructionIterator & code_item_end,uint32_t offset)168 inline const dex::TryItem* DexFile::GetTryItems(const DexInstructionIterator& code_item_end,
169                                                 uint32_t offset) {
170   return reinterpret_cast<const dex::TryItem*>
171       (RoundUp(reinterpret_cast<uintptr_t>(&code_item_end.Inst()), dex::TryItem::kAlignment)) +
172           offset;
173 }
174 
StringEquals(const DexFile * df1,dex::StringIndex sidx1,const DexFile * df2,dex::StringIndex sidx2)175 inline bool DexFile::StringEquals(const DexFile* df1, dex::StringIndex sidx1,
176                                   const DexFile* df2, dex::StringIndex sidx2) {
177   uint32_t s1_len;  // Note: utf16 length != mutf8 length.
178   const char* s1_data = df1->StringDataAndUtf16LengthByIdx(sidx1, &s1_len);
179   uint32_t s2_len;
180   const char* s2_data = df2->StringDataAndUtf16LengthByIdx(sidx2, &s2_len);
181   return (s1_len == s2_len) && (strcmp(s1_data, s2_data) == 0);
182 }
183 
184 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)185 bool DexFile::DecodeDebugLocalInfo(const uint8_t* stream,
186                                    const std::string& location,
187                                    const char* declaring_class_descriptor,
188                                    const std::vector<const char*>& arg_descriptors,
189                                    const std::string& method_name,
190                                    bool is_static,
191                                    uint16_t registers_size,
192                                    uint16_t ins_size,
193                                    uint16_t insns_size_in_code_units,
194                                    const IndexToStringData& index_to_string_data,
195                                    const TypeIndexToStringData& type_index_to_string_data,
196                                    const NewLocalCallback& new_local_callback) {
197   if (stream == nullptr) {
198     return false;
199   }
200   std::vector<LocalInfo> local_in_reg(registers_size);
201 
202   uint16_t arg_reg = registers_size - ins_size;
203   if (!is_static) {
204     const char* descriptor = declaring_class_descriptor;
205     local_in_reg[arg_reg].name_ = "this";
206     local_in_reg[arg_reg].descriptor_ = descriptor;
207     local_in_reg[arg_reg].signature_ = nullptr;
208     local_in_reg[arg_reg].start_address_ = 0;
209     local_in_reg[arg_reg].reg_ = arg_reg;
210     local_in_reg[arg_reg].is_live_ = true;
211     arg_reg++;
212   }
213 
214   DecodeUnsignedLeb128(&stream);  // Line.
215   uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
216   uint32_t i;
217   if (parameters_size != arg_descriptors.size()) {
218     LOG(ERROR) << "invalid stream - problem with parameter iterator in " << location
219                << " for method " << method_name;
220     return false;
221   }
222   for (i = 0; i < parameters_size && i < arg_descriptors.size(); ++i) {
223     if (arg_reg >= registers_size) {
224       LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg
225                  << " >= " << registers_size << ") in " << location;
226       return false;
227     }
228     uint32_t name_idx = DecodeUnsignedLeb128P1(&stream);
229     const char* descriptor = arg_descriptors[i];
230     local_in_reg[arg_reg].name_ = index_to_string_data(name_idx);
231     local_in_reg[arg_reg].descriptor_ = descriptor;
232     local_in_reg[arg_reg].signature_ = nullptr;
233     local_in_reg[arg_reg].start_address_ = 0;
234     local_in_reg[arg_reg].reg_ = arg_reg;
235     local_in_reg[arg_reg].is_live_ = true;
236     switch (*descriptor) {
237       case 'D':
238       case 'J':
239         arg_reg += 2;
240         break;
241       default:
242         arg_reg += 1;
243         break;
244     }
245   }
246 
247   uint32_t address = 0;
248   for (;;)  {
249     uint8_t opcode = *stream++;
250     switch (opcode) {
251       case DBG_END_SEQUENCE:
252         // Emit all variables which are still alive at the end of the method.
253         for (uint16_t reg = 0; reg < registers_size; reg++) {
254           if (local_in_reg[reg].is_live_) {
255             local_in_reg[reg].end_address_ = insns_size_in_code_units;
256             new_local_callback(local_in_reg[reg]);
257           }
258         }
259         return true;
260       case DBG_ADVANCE_PC:
261         address += DecodeUnsignedLeb128(&stream);
262         break;
263       case DBG_ADVANCE_LINE:
264         DecodeSignedLeb128(&stream);  // Line.
265         break;
266       case DBG_START_LOCAL:
267       case DBG_START_LOCAL_EXTENDED: {
268         uint16_t reg = DecodeUnsignedLeb128(&stream);
269         if (reg >= registers_size) {
270           LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= "
271                      << registers_size << ") in " << location;
272           return false;
273         }
274 
275         uint32_t name_idx = DecodeUnsignedLeb128P1(&stream);
276         uint16_t descriptor_idx = DecodeUnsignedLeb128P1(&stream);
277         uint32_t signature_idx = dex::kDexNoIndex;
278         if (opcode == DBG_START_LOCAL_EXTENDED) {
279           signature_idx = DecodeUnsignedLeb128P1(&stream);
280         }
281 
282         // Emit what was previously there, if anything
283         if (local_in_reg[reg].is_live_) {
284           local_in_reg[reg].end_address_ = address;
285           new_local_callback(local_in_reg[reg]);
286         }
287 
288         local_in_reg[reg].name_ = index_to_string_data(name_idx);
289         local_in_reg[reg].descriptor_ = type_index_to_string_data(descriptor_idx);;
290         local_in_reg[reg].signature_ = index_to_string_data(signature_idx);
291         local_in_reg[reg].start_address_ = address;
292         local_in_reg[reg].reg_ = reg;
293         local_in_reg[reg].is_live_ = true;
294         break;
295       }
296       case DBG_END_LOCAL: {
297         uint16_t reg = DecodeUnsignedLeb128(&stream);
298         if (reg >= registers_size) {
299           LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= "
300                      << registers_size << ") in " << location;
301           return false;
302         }
303         // If the register is live, close it properly. Otherwise, closing an already
304         // closed register is sloppy, but harmless if no further action is taken.
305         if (local_in_reg[reg].is_live_) {
306           local_in_reg[reg].end_address_ = address;
307           new_local_callback(local_in_reg[reg]);
308           local_in_reg[reg].is_live_ = false;
309         }
310         break;
311       }
312       case DBG_RESTART_LOCAL: {
313         uint16_t reg = DecodeUnsignedLeb128(&stream);
314         if (reg >= registers_size) {
315           LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= "
316                      << registers_size << ") in " << location;
317           return false;
318         }
319         // If the register is live, the "restart" is superfluous,
320         // and we don't want to mess with the existing start address.
321         if (!local_in_reg[reg].is_live_) {
322           local_in_reg[reg].start_address_ = address;
323           local_in_reg[reg].is_live_ = true;
324         }
325         break;
326       }
327       case DBG_SET_PROLOGUE_END:
328       case DBG_SET_EPILOGUE_BEGIN:
329         break;
330       case DBG_SET_FILE:
331         DecodeUnsignedLeb128P1(&stream);  // name.
332         break;
333       default:
334         address += (opcode - DBG_FIRST_SPECIAL) / DBG_LINE_RANGE;
335         break;
336     }
337   }
338 }
339 
340 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)341 bool DexFile::DecodeDebugLocalInfo(uint32_t registers_size,
342                                    uint32_t ins_size,
343                                    uint32_t insns_size_in_code_units,
344                                    uint32_t debug_info_offset,
345                                    bool is_static,
346                                    uint32_t method_idx,
347                                    const NewLocalCallback& new_local_callback) const {
348   const uint8_t* const stream = GetDebugInfoStream(debug_info_offset);
349   if (stream == nullptr) {
350     return false;
351   }
352   std::vector<const char*> arg_descriptors;
353   DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx)));
354   for (; it.HasNext(); it.Next()) {
355     arg_descriptors.push_back(it.GetDescriptor());
356   }
357   return DecodeDebugLocalInfo(stream,
358                               GetLocation(),
359                               GetMethodDeclaringClassDescriptor(GetMethodId(method_idx)),
360                               arg_descriptors,
361                               this->PrettyMethod(method_idx),
362                               is_static,
363                               registers_size,
364                               ins_size,
365                               insns_size_in_code_units,
366                               [this](uint32_t idx) {
367                                 return StringDataByIdx(dex::StringIndex(idx));
368                               },
369                               [this](uint32_t idx) {
370                                 return StringByTypeIdx(dex::TypeIndex(
371                                     dchecked_integral_cast<uint16_t>(idx)));
372                               },
373                               new_local_callback);
374 }
375 
376 template<typename DexDebugNewPosition, typename IndexToStringData>
DecodeDebugPositionInfo(const uint8_t * stream,const IndexToStringData & index_to_string_data,const DexDebugNewPosition & position_functor)377 bool DexFile::DecodeDebugPositionInfo(const uint8_t* stream,
378                                       const IndexToStringData& index_to_string_data,
379                                       const DexDebugNewPosition& position_functor) {
380   if (stream == nullptr) {
381     return false;
382   }
383 
384   PositionInfo entry;
385   entry.line_ = DecodeDebugInfoParameterNames(&stream, VoidFunctor());
386 
387   for (;;)  {
388     uint8_t opcode = *stream++;
389     switch (opcode) {
390       case DBG_END_SEQUENCE:
391         return true;  // end of stream.
392       case DBG_ADVANCE_PC:
393         entry.address_ += DecodeUnsignedLeb128(&stream);
394         break;
395       case DBG_ADVANCE_LINE:
396         entry.line_ += DecodeSignedLeb128(&stream);
397         break;
398       case DBG_START_LOCAL:
399         DecodeUnsignedLeb128(&stream);  // reg.
400         DecodeUnsignedLeb128P1(&stream);  // name.
401         DecodeUnsignedLeb128P1(&stream);  // descriptor.
402         break;
403       case DBG_START_LOCAL_EXTENDED:
404         DecodeUnsignedLeb128(&stream);  // reg.
405         DecodeUnsignedLeb128P1(&stream);  // name.
406         DecodeUnsignedLeb128P1(&stream);  // descriptor.
407         DecodeUnsignedLeb128P1(&stream);  // signature.
408         break;
409       case DBG_END_LOCAL:
410       case DBG_RESTART_LOCAL:
411         DecodeUnsignedLeb128(&stream);  // reg.
412         break;
413       case DBG_SET_PROLOGUE_END:
414         entry.prologue_end_ = true;
415         break;
416       case DBG_SET_EPILOGUE_BEGIN:
417         entry.epilogue_begin_ = true;
418         break;
419       case DBG_SET_FILE: {
420         uint32_t name_idx = DecodeUnsignedLeb128P1(&stream);
421         entry.source_file_ = index_to_string_data(name_idx);
422         break;
423       }
424       default: {
425         int adjopcode = opcode - DBG_FIRST_SPECIAL;
426         entry.address_ += adjopcode / DBG_LINE_RANGE;
427         entry.line_ += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
428         if (position_functor(entry)) {
429           return true;  // early exit.
430         }
431         entry.prologue_end_ = false;
432         entry.epilogue_begin_ = false;
433         break;
434       }
435     }
436   }
437 }
438 
AsCompactDexFile()439 inline const CompactDexFile* DexFile::AsCompactDexFile() const {
440   DCHECK(IsCompactDexFile());
441   return down_cast<const CompactDexFile*>(this);
442 }
443 
AsStandardDexFile()444 inline const StandardDexFile* DexFile::AsStandardDexFile() const {
445   DCHECK(IsStandardDexFile());
446   return down_cast<const StandardDexFile*>(this);
447 }
448 
449 // Get the base of the encoded data for the given DexCode.
GetCatchHandlerData(const DexInstructionIterator & code_item_end,uint32_t tries_size,uint32_t offset)450 inline const uint8_t* DexFile::GetCatchHandlerData(const DexInstructionIterator& code_item_end,
451                                                    uint32_t tries_size,
452                                                    uint32_t offset) {
453   const uint8_t* handler_data =
454       reinterpret_cast<const uint8_t*>(GetTryItems(code_item_end, tries_size));
455   return handler_data + offset;
456 }
457 
GetClasses()458 inline IterationRange<ClassIterator> DexFile::GetClasses() const {
459   return { ClassIterator(*this, 0u), ClassIterator(*this, NumClassDefs()) };
460 }
461 
462 // Returns the line number
463 template <typename Visitor>
DecodeDebugInfoParameterNames(const uint8_t ** debug_info,const Visitor & visitor)464 inline uint32_t DexFile::DecodeDebugInfoParameterNames(const uint8_t** debug_info,
465                                                        const Visitor& visitor) {
466   uint32_t line = DecodeUnsignedLeb128(debug_info);
467   const uint32_t parameters_size = DecodeUnsignedLeb128(debug_info);
468   for (uint32_t i = 0; i < parameters_size; ++i) {
469     visitor(dex::StringIndex(DecodeUnsignedLeb128P1(debug_info)));
470   }
471   return line;
472 }
473 
474 }  // namespace art
475 
476 #endif  // ART_LIBDEXFILE_DEX_DEX_FILE_INL_H_
477