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_RUNTIME_DEX_FILE_INL_H_
18 #define ART_RUNTIME_DEX_FILE_INL_H_
19 
20 #include "base/bit_utils.h"
21 #include "base/logging.h"
22 #include "base/stringpiece.h"
23 #include "dex_file.h"
24 #include "leb128.h"
25 
26 namespace art {
27 
GetStringLength(const StringId & string_id)28 inline int32_t DexFile::GetStringLength(const StringId& string_id) const {
29   const uint8_t* ptr = begin_ + string_id.string_data_off_;
30   return DecodeUnsignedLeb128(&ptr);
31 }
32 
GetStringDataAndUtf16Length(const StringId & string_id,uint32_t * utf16_length)33 inline const char* DexFile::GetStringDataAndUtf16Length(const StringId& string_id,
34                                                         uint32_t* utf16_length) const {
35   DCHECK(utf16_length != nullptr) << GetLocation();
36   const uint8_t* ptr = begin_ + string_id.string_data_off_;
37   *utf16_length = DecodeUnsignedLeb128(&ptr);
38   return reinterpret_cast<const char*>(ptr);
39 }
40 
GetMethodSignature(const MethodId & method_id)41 inline const Signature DexFile::GetMethodSignature(const MethodId& method_id) const {
42   return Signature(this, GetProtoId(method_id.proto_idx_));
43 }
44 
GetTryItems(const CodeItem & code_item,uint32_t offset)45 inline const DexFile::TryItem* DexFile::GetTryItems(const CodeItem& code_item, uint32_t offset) {
46   const uint16_t* insns_end_ = &code_item.insns_[code_item.insns_size_in_code_units_];
47   return reinterpret_cast<const TryItem*>
48       (RoundUp(reinterpret_cast<uintptr_t>(insns_end_), 4)) + offset;
49 }
50 
DexFileStringEquals(const DexFile * df1,uint32_t sidx1,const DexFile * df2,uint32_t sidx2)51 static inline bool DexFileStringEquals(const DexFile* df1, uint32_t sidx1,
52                                        const DexFile* df2, uint32_t sidx2) {
53   uint32_t s1_len;  // Note: utf16 length != mutf8 length.
54   const char* s1_data = df1->StringDataAndUtf16LengthByIdx(sidx1, &s1_len);
55   uint32_t s2_len;
56   const char* s2_data = df2->StringDataAndUtf16LengthByIdx(sidx2, &s2_len);
57   return (s1_len == s2_len) && (strcmp(s1_data, s2_data) == 0);
58 }
59 
60 inline bool Signature::operator==(const Signature& rhs) const {
61   if (dex_file_ == nullptr) {
62     return rhs.dex_file_ == nullptr;
63   }
64   if (rhs.dex_file_ == nullptr) {
65     return false;
66   }
67   if (dex_file_ == rhs.dex_file_) {
68     return proto_id_ == rhs.proto_id_;
69   }
70   uint32_t lhs_shorty_len;  // For a shorty utf16 length == mutf8 length.
71   const char* lhs_shorty_data = dex_file_->StringDataAndUtf16LengthByIdx(proto_id_->shorty_idx_,
72                                                                          &lhs_shorty_len);
73   StringPiece lhs_shorty(lhs_shorty_data, lhs_shorty_len);
74   {
75     uint32_t rhs_shorty_len;
76     const char* rhs_shorty_data =
77         rhs.dex_file_->StringDataAndUtf16LengthByIdx(rhs.proto_id_->shorty_idx_,
78                                                      &rhs_shorty_len);
79     StringPiece rhs_shorty(rhs_shorty_data, rhs_shorty_len);
80     if (lhs_shorty != rhs_shorty) {
81       return false;  // Shorty mismatch.
82     }
83   }
84   if (lhs_shorty[0] == 'L') {
85     const DexFile::TypeId& return_type_id = dex_file_->GetTypeId(proto_id_->return_type_idx_);
86     const DexFile::TypeId& rhs_return_type_id =
87         rhs.dex_file_->GetTypeId(rhs.proto_id_->return_type_idx_);
88     if (!DexFileStringEquals(dex_file_, return_type_id.descriptor_idx_,
89                              rhs.dex_file_, rhs_return_type_id.descriptor_idx_)) {
90       return false;  // Return type mismatch.
91     }
92   }
93   if (lhs_shorty.find('L', 1) != StringPiece::npos) {
94     const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
95     const DexFile::TypeList* rhs_params = rhs.dex_file_->GetProtoParameters(*rhs.proto_id_);
96     // Both lists are empty or have contents, or else shorty is broken.
97     DCHECK_EQ(params == nullptr, rhs_params == nullptr);
98     if (params != nullptr) {
99       uint32_t params_size = params->Size();
100       DCHECK_EQ(params_size, rhs_params->Size());  // Parameter list size must match.
101       for (uint32_t i = 0; i < params_size; ++i) {
102         const DexFile::TypeId& param_id = dex_file_->GetTypeId(params->GetTypeItem(i).type_idx_);
103         const DexFile::TypeId& rhs_param_id =
104             rhs.dex_file_->GetTypeId(rhs_params->GetTypeItem(i).type_idx_);
105         if (!DexFileStringEquals(dex_file_, param_id.descriptor_idx_,
106                                  rhs.dex_file_, rhs_param_id.descriptor_idx_)) {
107           return false;  // Parameter type mismatch.
108         }
109       }
110     }
111   }
112   return true;
113 }
114 
115 
116 }  // namespace art
117 
118 #endif  // ART_RUNTIME_DEX_FILE_INL_H_
119