Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [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_OBJECT_UTILS_H_ |
| 18 | #define ART_RUNTIME_OBJECT_UTILS_H_ |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 19 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 20 | #include "class_linker.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 21 | #include "dex_file.h" |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 22 | #include "monitor.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 23 | #include "mirror/art_field.h" |
| 24 | #include "mirror/art_method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "mirror/class.h" |
| 26 | #include "mirror/dex_cache.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/iftable.h" |
| 28 | #include "mirror/string.h" |
| 29 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 30 | #include "runtime.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 31 | #include "handle_scope-inl.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 32 | |
| 33 | #include <string> |
| 34 | |
| 35 | namespace art { |
| 36 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 37 | template <typename T> |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 38 | class ObjectLock { |
| 39 | public: |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 40 | ObjectLock(Thread* self, Handle<T> object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 41 | : self_(self), obj_(object) { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 42 | CHECK(object.Get() != nullptr); |
| 43 | obj_->MonitorEnter(self_); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 46 | ~ObjectLock() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 47 | obj_->MonitorExit(self_); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 50 | void WaitIgnoringInterrupts() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 51 | Monitor::Wait(self_, obj_.Get(), 0, 0, false, kWaiting); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 54 | void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 55 | obj_->Notify(self_); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 58 | void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 59 | obj_->NotifyAll(self_); |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | private: |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 63 | Thread* const self_; |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 64 | Handle<T> const obj_; |
Ian Rogers | 672f520 | 2012-01-12 18:06:40 -0800 | [diff] [blame] | 65 | DISALLOW_COPY_AND_ASSIGN(ObjectLock); |
| 66 | }; |
| 67 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 68 | class FieldHelper { |
| 69 | public: |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 70 | explicit FieldHelper(Handle<mirror::ArtField> f) : field_(f) {} |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 71 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 72 | void ChangeField(mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 73 | DCHECK(new_f != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 74 | field_.Assign(new_f); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 75 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 76 | |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 77 | mirror::ArtField* GetField() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 78 | return field_.Get(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 79 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 80 | |
Ian Rogers | 50239c7 | 2013-06-17 14:53:22 -0700 | [diff] [blame] | 81 | mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 82 | uint32_t field_index = field_->GetDexFieldIndex(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 83 | if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 84 | return Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(), |
| 85 | field_->GetTypeDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 86 | } |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 87 | const DexFile* dex_file = field_->GetDexFile(); |
| 88 | const DexFile::FieldId& field_id = dex_file->GetFieldId(field_index); |
| 89 | mirror::Class* type = field_->GetDexCache()->GetResolvedType(field_id.type_idx_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 90 | if (resolve && (type == nullptr)) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 91 | type = Runtime::Current()->GetClassLinker()->ResolveType(field_id.type_idx_, field_.Get()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 92 | CHECK(type != nullptr || Thread::Current()->IsExceptionPending()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 93 | } |
| 94 | return type; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 95 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 96 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 97 | // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper. |
| 98 | // If you need it longer, copy it into a std::string. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 99 | const char* GetDeclaringClassDescriptor() |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 100 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 101 | uint32_t field_index = field_->GetDexFieldIndex(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 102 | if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 103 | DCHECK(field_->IsStatic()); |
| 104 | DCHECK_LT(field_index, 2U); |
| 105 | // 0 == Class[] interfaces; 1 == Class[][] throws; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 106 | declaring_class_descriptor_ = field_->GetDeclaringClass()->GetDescriptor(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 107 | return declaring_class_descriptor_.c_str(); |
| 108 | } |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 109 | const DexFile* dex_file = field_->GetDexFile(); |
| 110 | const DexFile::FieldId& field_id = dex_file->GetFieldId(field_index); |
| 111 | return dex_file->GetFieldDeclaringClassDescriptor(field_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | private: |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 115 | Handle<mirror::ArtField> field_; |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 116 | std::string declaring_class_descriptor_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 117 | |
| 118 | DISALLOW_COPY_AND_ASSIGN(FieldHelper); |
| 119 | }; |
| 120 | |
| 121 | class MethodHelper { |
| 122 | public: |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 123 | explicit MethodHelper(Handle<mirror::ArtMethod> m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 124 | : method_(m), shorty_(nullptr), shorty_len_(0) { |
| 125 | SetMethod(m.Get()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 128 | void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 129 | DCHECK(new_m != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 130 | SetMethod(new_m); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 131 | shorty_ = nullptr; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 132 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 133 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 134 | mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 135 | return method_->GetInterfaceMethodIfProxy(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 138 | mirror::String* GetNameAsString(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 139 | const DexFile* dex_file = method_->GetDexFile(); |
| 140 | mirror::ArtMethod* method = method_->GetInterfaceMethodIfProxy(); |
| 141 | uint32_t dex_method_idx = method->GetDexMethodIndex(); |
| 142 | const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx); |
| 143 | StackHandleScope<1> hs(self); |
| 144 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); |
| 145 | return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_, |
| 146 | dex_cache); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 147 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 148 | |
Jeff Hao | 9a916d3 | 2013-06-27 18:45:37 -0700 | [diff] [blame] | 149 | const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 150 | const char* result = shorty_; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 151 | if (result == nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 152 | result = method_->GetShorty(&shorty_len_); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 153 | shorty_ = result; |
| 154 | } |
| 155 | return result; |
| 156 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 157 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 158 | uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 159 | if (shorty_ == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 160 | GetShorty(); |
| 161 | } |
| 162 | return shorty_len_; |
| 163 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 164 | |
Andreas Gampe | 36fea8d | 2014-03-10 13:37:40 -0700 | [diff] [blame] | 165 | // Counts the number of references in the parameter list of the corresponding method. |
| 166 | // Note: Thus does _not_ include "this" for non-static methods. |
| 167 | uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 168 | const char* shorty = GetShorty(); |
| 169 | uint32_t refs = 0; |
| 170 | for (uint32_t i = 1; i < shorty_len_ ; ++i) { |
| 171 | if (shorty[i] == 'L') { |
| 172 | refs++; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return refs; |
| 177 | } |
| 178 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 179 | // May cause thread suspension due to GetClassFromTypeIdx calling ResolveType this caused a large |
| 180 | // number of bugs at call sites. |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 181 | mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 182 | mirror::ArtMethod* method = GetMethod(); |
| 183 | const DexFile* dex_file = method->GetDexFile(); |
| 184 | const DexFile::MethodId& method_id = dex_file->GetMethodId(method->GetDexMethodIndex()); |
| 185 | const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 186 | uint16_t return_type_idx = proto_id.return_type_idx_; |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 187 | return GetClassFromTypeIdx(return_type_idx, resolve); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 188 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 189 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 190 | size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 191 | // "1 +" because the first in Args is the receiver. |
| 192 | // "- 1" because we don't count the return type. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 193 | return (method_->IsStatic() ? 0 : 1) + GetShortyLength() - 1; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 194 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 195 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 196 | // Get the primitive type associated with the given parameter. |
| 197 | Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 198 | CHECK_LT(param, NumArgs()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 199 | if (GetMethod()->IsStatic()) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 200 | param++; // 0th argument must skip return value at start of the shorty |
| 201 | } else if (param == 0) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 202 | return Primitive::kPrimNot; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 203 | } |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 204 | return Primitive::GetType(GetShorty()[param]); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 205 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 206 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 207 | // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods. |
| 208 | bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 209 | Primitive::Type type = GetParamPrimitiveType(param); |
| 210 | return type == Primitive::kPrimLong || type == Primitive::kPrimDouble; |
| 211 | } |
| 212 | |
| 213 | // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 214 | bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 215 | return GetParamPrimitiveType(param) == Primitive::kPrimNot; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 216 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 217 | |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 218 | bool HasSameNameAndSignature(MethodHelper* other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 219 | const DexFile* dex_file = method_->GetDexFile(); |
| 220 | const DexFile::MethodId& mid = dex_file->GetMethodId(GetMethod()->GetDexMethodIndex()); |
| 221 | if (method_->GetDexCache() == other->method_->GetDexCache()) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 222 | const DexFile::MethodId& other_mid = |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 223 | dex_file->GetMethodId(other->GetMethod()->GetDexMethodIndex()); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 224 | return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 225 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 226 | const DexFile* other_dex_file = other->method_->GetDexFile(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 227 | const DexFile::MethodId& other_mid = |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 228 | other_dex_file->GetMethodId(other->GetMethod()->GetDexMethodIndex()); |
| 229 | if (!DexFileStringEquals(dex_file, mid.name_idx_, other_dex_file, other_mid.name_idx_)) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 230 | return false; // Name mismatch. |
| 231 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 232 | return dex_file->GetMethodSignature(mid) == other_dex_file->GetMethodSignature(other_mid); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 233 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 234 | |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 235 | bool HasSameSignatureWithDifferentClassLoaders(MethodHelper* other) |
| 236 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 237 | if (UNLIKELY(GetReturnType() != other->GetReturnType())) { |
| 238 | return false; |
| 239 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 240 | const DexFile::TypeList* types = method_->GetParameterTypeList(); |
| 241 | const DexFile::TypeList* other_types = other->method_->GetParameterTypeList(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 242 | if (types == nullptr) { |
| 243 | return (other_types == nullptr) || (other_types->Size() == 0); |
| 244 | } else if (UNLIKELY(other_types == nullptr)) { |
| 245 | return types->Size() == 0; |
| 246 | } |
| 247 | uint32_t num_types = types->Size(); |
| 248 | if (UNLIKELY(num_types != other_types->Size())) { |
| 249 | return false; |
| 250 | } |
| 251 | for (uint32_t i = 0; i < num_types; ++i) { |
| 252 | mirror::Class* param_type = GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_); |
| 253 | mirror::Class* other_param_type = |
| 254 | other->GetClassFromTypeIdx(other_types->GetTypeItem(i).type_idx_); |
| 255 | if (UNLIKELY(param_type != other_param_type)) { |
| 256 | return false; |
| 257 | } |
| 258 | } |
| 259 | return true; |
| 260 | } |
| 261 | |
Mathieu Chartier | eae2fb2 | 2014-01-14 14:31:25 -0800 | [diff] [blame] | 262 | mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 263 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 264 | mirror::ArtMethod* method = GetMethod(); |
| 265 | mirror::Class* type = method->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 266 | if (type == nullptr && resolve) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 267 | type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 268 | CHECK(type != nullptr || Thread::Current()->IsExceptionPending()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 269 | } |
| 270 | return type; |
| 271 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 272 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 273 | mirror::Class* GetDexCacheResolvedType(uint16_t type_idx) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 274 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 275 | return GetMethod()->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 276 | } |
Elliott Hughes | a21039c | 2012-06-21 12:09:25 -0700 | [diff] [blame] | 277 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 278 | mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 279 | mirror::ArtMethod* method = GetMethod(); |
| 280 | mirror::String* s = method->GetDexCacheStrings()->Get(string_idx); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 281 | if (UNLIKELY(s == nullptr)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 282 | StackHandleScope<1> hs(Thread::Current()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 283 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); |
| 284 | s = Runtime::Current()->GetClassLinker()->ResolveString(*method->GetDexFile(), string_idx, |
| 285 | dex_cache); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 286 | } |
| 287 | return s; |
| 288 | } |
| 289 | |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 290 | uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile) |
| 291 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 292 | mirror::ArtMethod* method = GetMethod(); |
| 293 | const DexFile* dexfile = method->GetDexFile(); |
| 294 | if (dexfile == &other_dexfile) { |
| 295 | return method->GetDexMethodIndex(); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 296 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 297 | const DexFile::MethodId& mid = dexfile->GetMethodId(method->GetDexMethodIndex()); |
| 298 | const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 299 | const DexFile::StringId* other_descriptor = |
| 300 | other_dexfile.FindStringId(mid_declaring_class_descriptor); |
| 301 | if (other_descriptor != nullptr) { |
| 302 | const DexFile::TypeId* other_type_id = |
| 303 | other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor)); |
| 304 | if (other_type_id != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 305 | const char* mid_name = dexfile->GetMethodName(mid); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 306 | const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name); |
| 307 | if (other_name != nullptr) { |
| 308 | uint16_t other_return_type_idx; |
| 309 | std::vector<uint16_t> other_param_type_idxs; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 310 | bool success = other_dexfile.CreateTypeList( |
| 311 | dexfile->GetMethodSignature(mid).ToString(), &other_return_type_idx, |
| 312 | &other_param_type_idxs); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 313 | if (success) { |
| 314 | const DexFile::ProtoId* other_sig = |
| 315 | other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs); |
| 316 | if (other_sig != nullptr) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 317 | const DexFile::MethodId* other_mid = other_dexfile.FindMethodId( |
| 318 | *other_type_id, *other_name, *other_sig); |
Ian Rogers | 83883d7 | 2013-10-21 21:07:24 -0700 | [diff] [blame] | 319 | if (other_mid != nullptr) { |
| 320 | return other_dexfile.GetIndexForMethodId(*other_mid); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | return DexFile::kDexNoIndex; |
| 328 | } |
| 329 | |
Vladimir Marko | bbcc0c0 | 2014-02-03 14:08:42 +0000 | [diff] [blame] | 330 | // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the |
| 331 | // other_dexfile, such as the method index used to resolve this method in the other_dexfile. |
| 332 | uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile, |
| 333 | uint32_t name_and_signature_idx) |
| 334 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 335 | mirror::ArtMethod* method = GetMethod(); |
| 336 | const DexFile* dexfile = method->GetDexFile(); |
| 337 | const uint32_t dex_method_idx = method->GetDexMethodIndex(); |
| 338 | const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx); |
Vladimir Marko | bbcc0c0 | 2014-02-03 14:08:42 +0000 | [diff] [blame] | 339 | const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 340 | DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid)); |
| 341 | DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid)); |
| 342 | if (dexfile == &other_dexfile) { |
| 343 | return dex_method_idx; |
Vladimir Marko | bbcc0c0 | 2014-02-03 14:08:42 +0000 | [diff] [blame] | 344 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 345 | const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_); |
Vladimir Marko | bbcc0c0 | 2014-02-03 14:08:42 +0000 | [diff] [blame] | 346 | const DexFile::StringId* other_descriptor = |
| 347 | other_dexfile.FindStringId(mid_declaring_class_descriptor); |
| 348 | if (other_descriptor != nullptr) { |
| 349 | const DexFile::TypeId* other_type_id = |
| 350 | other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor)); |
| 351 | if (other_type_id != nullptr) { |
| 352 | const DexFile::MethodId* other_mid = other_dexfile.FindMethodId( |
| 353 | *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_), |
| 354 | other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_)); |
| 355 | if (other_mid != nullptr) { |
| 356 | return other_dexfile.GetIndexForMethodId(*other_mid); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | return DexFile::kDexNoIndex; |
| 361 | } |
| 362 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 363 | private: |
| 364 | // Set the method_ field, for proxy methods looking up the interface method via the resolved |
| 365 | // methods table. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 366 | void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 367 | method_.Assign(method); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 368 | } |
Ian Rogers | ad0b3a3 | 2012-04-16 14:50:24 -0700 | [diff] [blame] | 369 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 370 | Handle<mirror::ArtMethod> method_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 371 | const char* shorty_; |
Elliott Hughes | 45651fd | 2012-02-21 15:48:20 -0800 | [diff] [blame] | 372 | uint32_t shorty_len_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 373 | |
| 374 | DISALLOW_COPY_AND_ASSIGN(MethodHelper); |
| 375 | }; |
| 376 | |
| 377 | } // namespace art |
| 378 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 379 | #endif // ART_RUNTIME_OBJECT_UTILS_H_ |