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