Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_DEX_FILE_INL_H_ |
| 18 | #define ART_RUNTIME_DEX_FILE_INL_H_ |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 19 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 20 | #include "base/bit_utils.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 21 | #include "base/logging.h" |
Brian Carlstrom | a280655 | 2014-02-27 12:29:32 -0800 | [diff] [blame] | 22 | #include "base/stringpiece.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 23 | #include "dex_file.h" |
| 24 | #include "leb128.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | inline int32_t DexFile::GetStringLength(const StringId& string_id) const { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 29 | const uint8_t* ptr = begin_ + string_id.string_data_off_; |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 30 | return DecodeUnsignedLeb128(&ptr); |
| 31 | } |
| 32 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 33 | inline const char* DexFile::GetStringDataAndUtf16Length(const StringId& string_id, |
| 34 | uint32_t* utf16_length) const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 35 | DCHECK(utf16_length != nullptr) << GetLocation(); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 36 | const uint8_t* ptr = begin_ + string_id.string_data_off_; |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 37 | *utf16_length = DecodeUnsignedLeb128(&ptr); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 38 | return reinterpret_cast<const char*>(ptr); |
| 39 | } |
| 40 | |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 41 | inline const char* DexFile::GetStringData(const StringId& string_id) const { |
| 42 | uint32_t ignored; |
| 43 | return GetStringDataAndUtf16Length(string_id, &ignored); |
| 44 | } |
| 45 | |
| 46 | inline const char* DexFile::StringDataAndUtf16LengthByIdx(uint32_t idx, |
| 47 | uint32_t* utf16_length) const { |
| 48 | if (idx == kDexNoIndex) { |
| 49 | *utf16_length = 0; |
| 50 | return nullptr; |
| 51 | } |
| 52 | const StringId& string_id = GetStringId(idx); |
| 53 | return GetStringDataAndUtf16Length(string_id, utf16_length); |
| 54 | } |
| 55 | |
| 56 | inline const char* DexFile::StringDataByIdx(uint32_t idx) const { |
| 57 | uint32_t unicode_length; |
| 58 | return StringDataAndUtf16LengthByIdx(idx, &unicode_length); |
| 59 | } |
| 60 | |
| 61 | inline const char* DexFile::StringByTypeIdx(uint32_t idx, uint32_t* unicode_length) const { |
| 62 | const TypeId& type_id = GetTypeId(idx); |
| 63 | return StringDataAndUtf16LengthByIdx(type_id.descriptor_idx_, unicode_length); |
| 64 | } |
| 65 | |
| 66 | inline const char* DexFile::StringByTypeIdx(uint32_t idx) const { |
| 67 | const TypeId& type_id = GetTypeId(idx); |
| 68 | return StringDataByIdx(type_id.descriptor_idx_); |
| 69 | } |
| 70 | |
| 71 | inline const char* DexFile::GetTypeDescriptor(const TypeId& type_id) const { |
| 72 | return StringDataByIdx(type_id.descriptor_idx_); |
| 73 | } |
| 74 | |
| 75 | inline const char* DexFile::GetFieldTypeDescriptor(const FieldId& field_id) const { |
| 76 | const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_); |
| 77 | return GetTypeDescriptor(type_id); |
| 78 | } |
| 79 | |
| 80 | inline const char* DexFile::GetFieldName(const FieldId& field_id) const { |
| 81 | return StringDataByIdx(field_id.name_idx_); |
| 82 | } |
| 83 | |
| 84 | inline const char* DexFile::GetMethodDeclaringClassDescriptor(const MethodId& method_id) const { |
| 85 | const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_); |
| 86 | return GetTypeDescriptor(type_id); |
| 87 | } |
| 88 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 89 | inline const Signature DexFile::GetMethodSignature(const MethodId& method_id) const { |
| 90 | return Signature(this, GetProtoId(method_id.proto_idx_)); |
| 91 | } |
| 92 | |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 93 | inline const Signature DexFile::GetProtoSignature(const ProtoId& proto_id) const { |
| 94 | return Signature(this, proto_id); |
| 95 | } |
| 96 | |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 97 | inline const char* DexFile::GetMethodName(const MethodId& method_id) const { |
| 98 | return StringDataByIdx(method_id.name_idx_); |
| 99 | } |
| 100 | |
| 101 | inline const char* DexFile::GetMethodShorty(uint32_t idx) const { |
| 102 | return StringDataByIdx(GetProtoId(GetMethodId(idx).proto_idx_).shorty_idx_); |
| 103 | } |
| 104 | |
| 105 | inline const char* DexFile::GetMethodShorty(const MethodId& method_id) const { |
| 106 | return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_); |
| 107 | } |
| 108 | |
| 109 | inline const char* DexFile::GetMethodShorty(const MethodId& method_id, uint32_t* length) const { |
| 110 | // Using the UTF16 length is safe here as shorties are guaranteed to be ASCII characters. |
| 111 | return StringDataAndUtf16LengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length); |
| 112 | } |
| 113 | |
| 114 | inline const char* DexFile::GetClassDescriptor(const ClassDef& class_def) const { |
| 115 | return StringByTypeIdx(class_def.class_idx_); |
| 116 | } |
| 117 | |
| 118 | inline const char* DexFile::GetReturnTypeDescriptor(const ProtoId& proto_id) const { |
| 119 | return StringByTypeIdx(proto_id.return_type_idx_); |
| 120 | } |
| 121 | |
| 122 | inline const char* DexFile::GetShorty(uint32_t proto_idx) const { |
| 123 | const ProtoId& proto_id = GetProtoId(proto_idx); |
| 124 | return StringDataByIdx(proto_id.shorty_idx_); |
| 125 | } |
| 126 | |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 127 | inline const DexFile::TryItem* DexFile::GetTryItems(const CodeItem& code_item, uint32_t offset) { |
| 128 | const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_in_code_units_]; |
| 129 | return reinterpret_cast<const TryItem*> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 130 | (RoundUp(reinterpret_cast<uintptr_t>(insns_end_), 4)) + offset; |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 133 | static inline bool DexFileStringEquals(const DexFile* df1, uint32_t sidx1, |
| 134 | const DexFile* df2, uint32_t sidx2) { |
| 135 | uint32_t s1_len; // Note: utf16 length != mutf8 length. |
| 136 | const char* s1_data = df1->StringDataAndUtf16LengthByIdx(sidx1, &s1_len); |
| 137 | uint32_t s2_len; |
| 138 | const char* s2_data = df2->StringDataAndUtf16LengthByIdx(sidx2, &s2_len); |
| 139 | return (s1_len == s2_len) && (strcmp(s1_data, s2_data) == 0); |
| 140 | } |
| 141 | |
| 142 | inline bool Signature::operator==(const Signature& rhs) const { |
| 143 | if (dex_file_ == nullptr) { |
| 144 | return rhs.dex_file_ == nullptr; |
| 145 | } |
| 146 | if (rhs.dex_file_ == nullptr) { |
| 147 | return false; |
| 148 | } |
| 149 | if (dex_file_ == rhs.dex_file_) { |
| 150 | return proto_id_ == rhs.proto_id_; |
| 151 | } |
| 152 | uint32_t lhs_shorty_len; // For a shorty utf16 length == mutf8 length. |
| 153 | const char* lhs_shorty_data = dex_file_->StringDataAndUtf16LengthByIdx(proto_id_->shorty_idx_, |
| 154 | &lhs_shorty_len); |
| 155 | StringPiece lhs_shorty(lhs_shorty_data, lhs_shorty_len); |
| 156 | { |
| 157 | uint32_t rhs_shorty_len; |
| 158 | const char* rhs_shorty_data = |
| 159 | rhs.dex_file_->StringDataAndUtf16LengthByIdx(rhs.proto_id_->shorty_idx_, |
| 160 | &rhs_shorty_len); |
| 161 | StringPiece rhs_shorty(rhs_shorty_data, rhs_shorty_len); |
| 162 | if (lhs_shorty != rhs_shorty) { |
| 163 | return false; // Shorty mismatch. |
| 164 | } |
| 165 | } |
| 166 | if (lhs_shorty[0] == 'L') { |
| 167 | const DexFile::TypeId& return_type_id = dex_file_->GetTypeId(proto_id_->return_type_idx_); |
| 168 | const DexFile::TypeId& rhs_return_type_id = |
| 169 | rhs.dex_file_->GetTypeId(rhs.proto_id_->return_type_idx_); |
| 170 | if (!DexFileStringEquals(dex_file_, return_type_id.descriptor_idx_, |
| 171 | rhs.dex_file_, rhs_return_type_id.descriptor_idx_)) { |
| 172 | return false; // Return type mismatch. |
| 173 | } |
| 174 | } |
| 175 | if (lhs_shorty.find('L', 1) != StringPiece::npos) { |
| 176 | const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_); |
| 177 | const DexFile::TypeList* rhs_params = rhs.dex_file_->GetProtoParameters(*rhs.proto_id_); |
| 178 | // Both lists are empty or have contents, or else shorty is broken. |
| 179 | DCHECK_EQ(params == nullptr, rhs_params == nullptr); |
| 180 | if (params != nullptr) { |
| 181 | uint32_t params_size = params->Size(); |
| 182 | DCHECK_EQ(params_size, rhs_params->Size()); // Parameter list size must match. |
| 183 | for (uint32_t i = 0; i < params_size; ++i) { |
| 184 | const DexFile::TypeId& param_id = dex_file_->GetTypeId(params->GetTypeItem(i).type_idx_); |
| 185 | const DexFile::TypeId& rhs_param_id = |
| 186 | rhs.dex_file_->GetTypeId(rhs_params->GetTypeItem(i).type_idx_); |
| 187 | if (!DexFileStringEquals(dex_file_, param_id.descriptor_idx_, |
| 188 | rhs.dex_file_, rhs_param_id.descriptor_idx_)) { |
| 189 | return false; // Parameter type mismatch. |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 198 | } // namespace art |
| 199 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 200 | #endif // ART_RUNTIME_DEX_FILE_INL_H_ |