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