Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_ART_METHOD_INL_H_ |
| 18 | #define ART_RUNTIME_ART_METHOD_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 20 | #include "art_method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 22 | #include "base/callee_save_type.h" |
Hiroshi Yamauchi | 0037082 | 2015-08-18 14:47:25 -0700 | [diff] [blame] | 23 | #include "class_linker-inl.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 24 | #include "common_throws.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 25 | #include "dex/code_item_accessors-inl.h" |
| 26 | #include "dex/dex_file-inl.h" |
| 27 | #include "dex/dex_file_annotations.h" |
| 28 | #include "dex/dex_file_types.h" |
David Sehr | 8c0961f | 2018-01-23 16:11:38 -0800 | [diff] [blame] | 29 | #include "dex/invoke_type.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 30 | #include "dex/primitive.h" |
Andreas Gampe | b79674c | 2019-05-14 16:16:46 -0700 | [diff] [blame] | 31 | #include "dex/signature.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 32 | #include "gc_root-inl.h" |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 33 | #include "imtable-inl.h" |
Vladimir Marko | 1001c2e | 2024-01-30 16:54:20 +0000 | [diff] [blame] | 34 | #include "jit/jit_options.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 35 | #include "mirror/class-inl.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 36 | #include "mirror/dex_cache-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 37 | #include "mirror/object-inl.h" |
| 38 | #include "mirror/object_array.h" |
Nicolas Geoffray | 4c0b4bc | 2017-03-17 13:08:26 +0000 | [diff] [blame] | 39 | #include "mirror/string.h" |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 40 | #include "obj_ptr-inl.h" |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 41 | #include "quick/quick_method_frame_info.h" |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 42 | #include "read_barrier-inl.h" |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 43 | #include "runtime-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 44 | #include "thread-current-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 45 | |
Dmitrii Ishcheikin | 91d4f8c | 2024-01-17 12:01:01 +0000 | [diff] [blame] | 46 | namespace art HIDDEN { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | |
Vladimir Marko | a3749b2 | 2022-11-02 13:42:54 +0000 | [diff] [blame] | 48 | namespace detail { |
| 49 | |
| 50 | template <> struct ShortyTraits<'V'> { |
| 51 | using Type = void; |
Stefano Cianciulli | 78f3c72 | 2023-05-16 10:32:54 +0000 | [diff] [blame] | 52 | static Type Get([[maybe_unused]] const JValue& value) {} |
Vladimir Marko | a3749b2 | 2022-11-02 13:42:54 +0000 | [diff] [blame] | 53 | // `kVRegCount` and `Set()` are not defined. |
| 54 | }; |
| 55 | |
| 56 | template <> struct ShortyTraits<'Z'> { |
Vladimir Marko | ad65983 | 2022-11-21 15:08:14 +0000 | [diff] [blame] | 57 | // Despite using `uint8_t` for `boolean` in `JValue`, we shall use `bool` here. |
| 58 | using Type = bool; |
| 59 | static Type Get(const JValue& value) { return value.GetZ() != 0u; } |
Vladimir Marko | a3749b2 | 2022-11-02 13:42:54 +0000 | [diff] [blame] | 60 | static constexpr size_t kVRegCount = 1u; |
Vladimir Marko | ad65983 | 2022-11-21 15:08:14 +0000 | [diff] [blame] | 61 | static void Set(uint32_t* args, Type value) { args[0] = static_cast<uint32_t>(value ? 1u : 0u); } |
Vladimir Marko | a3749b2 | 2022-11-02 13:42:54 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | template <> struct ShortyTraits<'B'> { |
| 65 | using Type = int8_t; |
| 66 | static Type Get(const JValue& value) { return value.GetB(); } |
| 67 | static constexpr size_t kVRegCount = 1u; |
| 68 | static void Set(uint32_t* args, Type value) { args[0] = static_cast<uint32_t>(value); } |
| 69 | }; |
| 70 | |
| 71 | template <> struct ShortyTraits<'C'> { |
| 72 | using Type = uint16_t; |
| 73 | static Type Get(const JValue& value) { return value.GetC(); } |
| 74 | static constexpr size_t kVRegCount = 1u; |
| 75 | static void Set(uint32_t* args, Type value) { args[0] = static_cast<uint32_t>(value); } |
| 76 | }; |
| 77 | |
| 78 | template <> struct ShortyTraits<'S'> { |
| 79 | using Type = int16_t; |
| 80 | static Type Get(const JValue& value) { return value.GetS(); } |
| 81 | static constexpr size_t kVRegCount = 1u; |
| 82 | static void Set(uint32_t* args, Type value) { args[0] = static_cast<uint32_t>(value); } |
| 83 | }; |
| 84 | |
| 85 | template <> struct ShortyTraits<'I'> { |
| 86 | using Type = int32_t; |
| 87 | static Type Get(const JValue& value) { return value.GetI(); } |
| 88 | static constexpr size_t kVRegCount = 1u; |
| 89 | static void Set(uint32_t* args, Type value) { args[0] = static_cast<uint32_t>(value); } |
| 90 | }; |
| 91 | |
| 92 | template <> struct ShortyTraits<'J'> { |
| 93 | using Type = int64_t; |
| 94 | static Type Get(const JValue& value) { return value.GetJ(); } |
| 95 | static constexpr size_t kVRegCount = 2u; |
| 96 | static void Set(uint32_t* args, Type value) { |
| 97 | // Little-endian representation. |
| 98 | args[0] = static_cast<uint32_t>(value); |
| 99 | args[1] = static_cast<uint32_t>(static_cast<uint64_t>(value) >> 32); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | template <> struct ShortyTraits<'F'> { |
| 104 | using Type = float; |
| 105 | static Type Get(const JValue& value) { return value.GetF(); } |
| 106 | static constexpr size_t kVRegCount = 1u; |
| 107 | static void Set(uint32_t* args, Type value) { args[0] = bit_cast<uint32_t>(value); } |
| 108 | }; |
| 109 | |
| 110 | template <> struct ShortyTraits<'D'> { |
| 111 | using Type = double; |
| 112 | static Type Get(const JValue& value) { return value.GetD(); } |
| 113 | static constexpr size_t kVRegCount = 2u; |
| 114 | static void Set(uint32_t* args, Type value) { |
| 115 | // Little-endian representation. |
| 116 | uint64_t v = bit_cast<uint64_t>(value); |
| 117 | args[0] = static_cast<uint32_t>(v); |
| 118 | args[1] = static_cast<uint32_t>(v >> 32); |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | template <> struct ShortyTraits<'L'> { |
| 123 | using Type = ObjPtr<mirror::Object>; |
| 124 | static Type Get(const JValue& value) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 125 | return value.GetL(); |
| 126 | } |
| 127 | static constexpr size_t kVRegCount = 1u; |
| 128 | static void Set(uint32_t* args, Type value) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 129 | args[0] = StackReference<mirror::Object>::FromMirrorPtr(value.Ptr()).AsVRegValue(); |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | template <char... Shorty> |
| 134 | constexpr auto MaterializeShorty() { |
| 135 | constexpr size_t kSize = std::size({Shorty...}) + 1u; |
| 136 | return std::array<char, kSize>{Shorty..., '\0'}; |
| 137 | } |
| 138 | |
| 139 | template <char... ArgType> |
| 140 | constexpr size_t NumberOfVRegs() { |
| 141 | constexpr size_t kArgVRegCount[] = { |
| 142 | ShortyTraits<ArgType>::kVRegCount... |
| 143 | }; |
| 144 | size_t sum = 0u; |
| 145 | for (size_t count : kArgVRegCount) { |
| 146 | sum += count; |
| 147 | } |
| 148 | return sum; |
| 149 | } |
| 150 | |
| 151 | template <char... ArgType> |
Stefano Cianciulli | 78f3c72 | 2023-05-16 10:32:54 +0000 | [diff] [blame] | 152 | inline ALWAYS_INLINE void FillVRegs([[maybe_unused]] uint32_t* vregs, |
| 153 | [[maybe_unused]] typename ShortyTraits<ArgType>::Type... args) |
Vladimir Marko | a3749b2 | 2022-11-02 13:42:54 +0000 | [diff] [blame] | 154 | REQUIRES_SHARED(Locks::mutator_lock_) {} |
| 155 | |
| 156 | template <char FirstArgType, char... ArgType> |
| 157 | inline ALWAYS_INLINE void FillVRegs(uint32_t* vregs, |
| 158 | typename ShortyTraits<FirstArgType>::Type first_arg, |
| 159 | typename ShortyTraits<ArgType>::Type... args) |
| 160 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 161 | ShortyTraits<FirstArgType>::Set(vregs, first_arg); |
| 162 | FillVRegs<ArgType...>(vregs + ShortyTraits<FirstArgType>::kVRegCount, args...); |
| 163 | } |
| 164 | |
| 165 | template <char... ArgType> |
| 166 | inline ALWAYS_INLINE auto MaterializeVRegs(typename ShortyTraits<ArgType>::Type... args) |
| 167 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 168 | constexpr size_t kNumVRegs = NumberOfVRegs<ArgType...>(); |
| 169 | std::array<uint32_t, kNumVRegs> vregs; |
| 170 | FillVRegs<ArgType...>(vregs.data(), args...); |
| 171 | return vregs; |
| 172 | } |
| 173 | |
| 174 | } // namespace detail |
| 175 | |
| 176 | template <char ReturnType, char... ArgType> |
| 177 | inline typename detail::ShortyTraits<ReturnType>::Type |
| 178 | ArtMethod::InvokeStatic(Thread* self, typename detail::ShortyTraits<ArgType>::Type... args) { |
| 179 | DCHECK(IsStatic()); |
Vladimir Marko | b9c73f8 | 2022-11-22 15:18:43 +0000 | [diff] [blame] | 180 | DCHECK(GetDeclaringClass()->IsInitialized()); // Used only for initialized well-known classes. |
Vladimir Marko | a3749b2 | 2022-11-02 13:42:54 +0000 | [diff] [blame] | 181 | JValue result; |
| 182 | constexpr auto shorty = detail::MaterializeShorty<ReturnType, ArgType...>(); |
| 183 | auto vregs = detail::MaterializeVRegs<ArgType...>(args...); |
Ryan Prichard | 2c0caed | 2023-07-27 00:26:46 -0700 | [diff] [blame] | 184 | Invoke(self, |
| 185 | vregs.empty() ? nullptr : vregs.data(), |
| 186 | vregs.size() * sizeof(typename decltype(vregs)::value_type), |
| 187 | &result, |
| 188 | shorty.data()); |
Vladimir Marko | a3749b2 | 2022-11-02 13:42:54 +0000 | [diff] [blame] | 189 | return detail::ShortyTraits<ReturnType>::Get(result); |
| 190 | } |
| 191 | |
| 192 | template <char ReturnType, char... ArgType> |
| 193 | typename detail::ShortyTraits<ReturnType>::Type |
| 194 | ArtMethod::InvokeInstance(Thread* self, |
| 195 | ObjPtr<mirror::Object> receiver, |
| 196 | typename detail::ShortyTraits<ArgType>::Type... args) { |
Vladimir Marko | f7bd1fa | 2022-11-18 10:55:05 +0000 | [diff] [blame] | 197 | DCHECK(!GetDeclaringClass()->IsInterface()); |
Vladimir Marko | a3749b2 | 2022-11-02 13:42:54 +0000 | [diff] [blame] | 198 | DCHECK(!IsStatic()); |
| 199 | JValue result; |
| 200 | constexpr auto shorty = detail::MaterializeShorty<ReturnType, ArgType...>(); |
| 201 | auto vregs = detail::MaterializeVRegs<'L', ArgType...>(receiver, args...); |
Ryan Prichard | 2c0caed | 2023-07-27 00:26:46 -0700 | [diff] [blame] | 202 | Invoke(self, |
| 203 | vregs.data(), |
| 204 | vregs.size() * sizeof(typename decltype(vregs)::value_type), |
| 205 | &result, |
| 206 | shorty.data()); |
Vladimir Marko | a3749b2 | 2022-11-02 13:42:54 +0000 | [diff] [blame] | 207 | return detail::ShortyTraits<ReturnType>::Get(result); |
| 208 | } |
| 209 | |
Vladimir Marko | f7bd1fa | 2022-11-18 10:55:05 +0000 | [diff] [blame] | 210 | template <char ReturnType, char... ArgType> |
| 211 | typename detail::ShortyTraits<ReturnType>::Type |
| 212 | ArtMethod::InvokeFinal(Thread* self, |
| 213 | ObjPtr<mirror::Object> receiver, |
| 214 | typename detail::ShortyTraits<ArgType>::Type... args) { |
| 215 | DCHECK(!GetDeclaringClass()->IsInterface()); |
| 216 | DCHECK(!IsStatic()); |
Vladimir Marko | ad65983 | 2022-11-21 15:08:14 +0000 | [diff] [blame] | 217 | DCHECK(IsFinal() || GetDeclaringClass()->IsFinal()); |
Vladimir Marko | f7bd1fa | 2022-11-18 10:55:05 +0000 | [diff] [blame] | 218 | DCHECK(receiver != nullptr); |
| 219 | return InvokeInstance<ReturnType, ArgType...>(self, receiver, args...); |
| 220 | } |
| 221 | |
| 222 | template <char ReturnType, char... ArgType> |
| 223 | typename detail::ShortyTraits<ReturnType>::Type |
| 224 | ArtMethod::InvokeVirtual(Thread* self, |
| 225 | ObjPtr<mirror::Object> receiver, |
| 226 | typename detail::ShortyTraits<ArgType>::Type... args) { |
| 227 | DCHECK(!GetDeclaringClass()->IsInterface()); |
| 228 | DCHECK(!IsStatic()); |
| 229 | DCHECK(!IsFinal()); |
| 230 | DCHECK(receiver != nullptr); |
| 231 | ArtMethod* target_method = |
| 232 | receiver->GetClass()->FindVirtualMethodForVirtual(this, kRuntimePointerSize); |
| 233 | DCHECK(target_method != nullptr); |
| 234 | return target_method->InvokeInstance<ReturnType, ArgType...>(self, receiver, args...); |
| 235 | } |
| 236 | |
| 237 | template <char ReturnType, char... ArgType> |
| 238 | typename detail::ShortyTraits<ReturnType>::Type |
| 239 | ArtMethod::InvokeInterface(Thread* self, |
| 240 | ObjPtr<mirror::Object> receiver, |
| 241 | typename detail::ShortyTraits<ArgType>::Type... args) { |
| 242 | DCHECK(GetDeclaringClass()->IsInterface()); |
| 243 | DCHECK(!IsStatic()); |
| 244 | DCHECK(receiver != nullptr); |
| 245 | ArtMethod* target_method = |
| 246 | receiver->GetClass()->FindVirtualMethodForInterface(this, kRuntimePointerSize); |
| 247 | DCHECK(target_method != nullptr); |
| 248 | return target_method->InvokeInstance<ReturnType, ArgType...>(self, receiver, args...); |
| 249 | } |
| 250 | |
Mathieu Chartier | e7f75f3 | 2016-02-01 16:08:15 -0800 | [diff] [blame] | 251 | template <ReadBarrierOption kReadBarrierOption> |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 252 | inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClassUnchecked() { |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 253 | GcRootSource gc_root_source(this); |
Mathieu Chartier | e7f75f3 | 2016-02-01 16:08:15 -0800 | [diff] [blame] | 254 | return declaring_class_.Read<kReadBarrierOption>(&gc_root_source); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Mathieu Chartier | e7f75f3 | 2016-02-01 16:08:15 -0800 | [diff] [blame] | 257 | template <ReadBarrierOption kReadBarrierOption> |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 258 | inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClass() { |
| 259 | ObjPtr<mirror::Class> result = GetDeclaringClassUnchecked<kReadBarrierOption>(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 260 | if (kIsDebugBuild) { |
| 261 | if (!IsRuntimeMethod()) { |
| 262 | CHECK(result != nullptr) << this; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 263 | } else { |
| 264 | CHECK(result == nullptr) << this; |
| 265 | } |
| 266 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 267 | return result; |
| 268 | } |
| 269 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 270 | inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 271 | declaring_class_ = GcRoot<mirror::Class>(new_declaring_class); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 274 | inline bool ArtMethod::CASDeclaringClass(ObjPtr<mirror::Class> expected_class, |
| 275 | ObjPtr<mirror::Class> desired_class) { |
Mathieu Chartier | 10e5ea9 | 2015-08-13 12:56:31 -0700 | [diff] [blame] | 276 | GcRoot<mirror::Class> expected_root(expected_class); |
| 277 | GcRoot<mirror::Class> desired_root(desired_class); |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 278 | auto atomic_root_class = reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_); |
| 279 | return atomic_root_class->CompareAndSetStrongSequentiallyConsistent(expected_root, desired_root); |
Mathieu Chartier | 10e5ea9 | 2015-08-13 12:56:31 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 282 | inline uint16_t ArtMethod::GetMethodIndex() { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 283 | DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 284 | return method_index_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 287 | inline uint16_t ArtMethod::GetMethodIndexDuringLinking() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 288 | return method_index_; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 291 | inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedClassFromTypeIndex(dex::TypeIndex type_idx) { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 292 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 293 | ObjPtr<mirror::Class> type = |
| 294 | Runtime::Current()->GetClassLinker()->LookupResolvedType(type_idx, this); |
| 295 | DCHECK(!Thread::Current()->IsExceptionPending()); |
| 296 | return type; |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | inline ObjPtr<mirror::Class> ArtMethod::ResolveClassFromTypeIndex(dex::TypeIndex type_idx) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 300 | ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this); |
| 301 | DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending()); |
| 302 | return type; |
Ian Rogers | a048560 | 2014-12-02 15:48:04 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Vladimir Marko | 7267e1e | 2022-11-07 12:37:32 +0100 | [diff] [blame] | 305 | inline bool ArtMethod::IsStringConstructor() { |
| 306 | uint32_t access_flags = GetAccessFlags(); |
| 307 | DCHECK(!IsClassInitializer(access_flags)); |
| 308 | return IsConstructor(access_flags) && |
| 309 | // No read barrier needed for reading a constant reference only to read |
| 310 | // a constant string class flag. See `ReadBarrierOption`. |
| 311 | GetDeclaringClass<kWithoutReadBarrier>()->IsStringClass(); |
| 312 | } |
| 313 | |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 314 | inline bool ArtMethod::IsOverridableByDefaultMethod() { |
| 315 | // It is safe to avoid the read barrier here since the constant interface flag |
| 316 | // in the `Class` object is stored before creating the `ArtMethod` and storing |
| 317 | // the declaring class reference. See `ReadBarrierOption`. |
| 318 | return GetDeclaringClass<kWithoutReadBarrier>()->IsInterface(); |
| 319 | } |
| 320 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 321 | inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 322 | switch (type) { |
| 323 | case kStatic: |
| 324 | return !IsStatic(); |
| 325 | case kDirect: |
| 326 | return !IsDirect() || IsStatic(); |
| 327 | case kVirtual: { |
Alex Light | d6e0fa9 | 2016-10-17 13:02:39 -0700 | [diff] [blame] | 328 | // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface |
| 329 | // method. |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 330 | ObjPtr<mirror::Class> methods_class = GetDeclaringClass(); |
Alex Light | d6e0fa9 | 2016-10-17 13:02:39 -0700 | [diff] [blame] | 331 | return IsDirect() || (methods_class->IsInterface() && !IsCopied()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 332 | } |
| 333 | case kSuper: |
Andreas Gampe | 8f252e6 | 2014-08-25 20:46:31 -0700 | [diff] [blame] | 334 | // Constructors and static methods are called with invoke-direct. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 335 | return IsConstructor() || IsStatic(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 336 | case kInterface: { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 337 | ObjPtr<mirror::Class> methods_class = GetDeclaringClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 338 | return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass()); |
| 339 | } |
Andra Danciu | a0130e8 | 2020-07-23 12:34:56 +0000 | [diff] [blame] | 340 | case kPolymorphic: |
Orion Hodson | 81daf3d | 2020-09-03 14:01:51 +0100 | [diff] [blame] | 341 | return !IsSignaturePolymorphic(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 342 | default: |
| 343 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 344 | UNREACHABLE(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 348 | inline bool ArtMethod::IsCalleeSaveMethod() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 349 | if (!IsRuntimeMethod()) { |
| 350 | return false; |
| 351 | } |
| 352 | Runtime* runtime = Runtime::Current(); |
| 353 | bool result = false; |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 354 | for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); i++) { |
| 355 | if (this == runtime->GetCalleeSaveMethod(CalleeSaveType(i))) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 356 | result = true; |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | return result; |
| 361 | } |
| 362 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 363 | inline bool ArtMethod::IsResolutionMethod() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 364 | bool result = this == Runtime::Current()->GetResolutionMethod(); |
| 365 | // Check that if we do think it is phony it looks like the resolution method. |
Santiago Aboy Solanes | 6cdabe1 | 2022-02-18 15:27:43 +0000 | [diff] [blame] | 366 | DCHECK_IMPLIES(result, IsRuntimeMethod()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 367 | return result; |
| 368 | } |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 369 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 370 | inline bool ArtMethod::IsImtUnimplementedMethod() { |
| 371 | bool result = this == Runtime::Current()->GetImtUnimplementedMethod(); |
| 372 | // Check that if we do think it is phony it looks like the imt unimplemented method. |
Santiago Aboy Solanes | 6cdabe1 | 2022-02-18 15:27:43 +0000 | [diff] [blame] | 373 | DCHECK_IMPLIES(result, IsRuntimeMethod()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 374 | return result; |
| 375 | } |
| 376 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 377 | inline const DexFile* ArtMethod::GetDexFile() { |
Mathieu Chartier | 137cdfa | 2017-01-26 14:03:11 -0800 | [diff] [blame] | 378 | // It is safe to avoid the read barrier here since the dex file is constant, so if we read the |
| 379 | // from-space dex file pointer it will be equal to the to-space copy. |
| 380 | return GetDexCache<kWithoutReadBarrier>()->GetDexFile(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | inline const char* ArtMethod::GetDeclaringClassDescriptor() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 384 | uint32_t dex_method_idx = GetDexMethodIndex(); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 385 | if (UNLIKELY(dex_method_idx == dex::kDexNoIndex)) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 386 | return "<runtime method>"; |
| 387 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 388 | DCHECK(!IsProxyMethod()); |
| 389 | const DexFile* dex_file = GetDexFile(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 390 | return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx)); |
| 391 | } |
| 392 | |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 393 | inline const char* ArtMethod::GetShorty() { |
| 394 | uint32_t unused_length; |
| 395 | return GetShorty(&unused_length); |
| 396 | } |
| 397 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 398 | inline const char* ArtMethod::GetShorty(uint32_t* out_length) { |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 399 | DCHECK(!IsProxyMethod()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 400 | const DexFile* dex_file = GetDexFile(); |
Vladimir Marko | c945e0d | 2018-07-18 17:26:45 +0100 | [diff] [blame] | 401 | return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Vladimir Marko | 473c5a01 | 2023-08-01 10:55:27 +0200 | [diff] [blame] | 404 | inline std::string_view ArtMethod::GetShortyView() { |
| 405 | DCHECK(!IsProxyMethod()); |
| 406 | const DexFile* dex_file = GetDexFile(); |
| 407 | return dex_file->GetMethodShortyView(dex_file->GetMethodId(GetDexMethodIndex())); |
| 408 | } |
| 409 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 410 | inline const Signature ArtMethod::GetSignature() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 411 | uint32_t dex_method_idx = GetDexMethodIndex(); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 412 | if (dex_method_idx != dex::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 413 | DCHECK(!IsProxyMethod()); |
| 414 | const DexFile* dex_file = GetDexFile(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 415 | return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx)); |
| 416 | } |
| 417 | return Signature::NoSignature(); |
| 418 | } |
| 419 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 420 | inline const char* ArtMethod::GetName() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 421 | uint32_t dex_method_idx = GetDexMethodIndex(); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 422 | if (LIKELY(dex_method_idx != dex::kDexNoIndex)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 423 | DCHECK(!IsProxyMethod()); |
| 424 | const DexFile* dex_file = GetDexFile(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 425 | return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx)); |
| 426 | } |
Eric Holk | abdb459 | 2019-05-16 08:33:12 -0700 | [diff] [blame] | 427 | return GetRuntimeMethodName(); |
| 428 | } |
| 429 | |
| 430 | inline std::string_view ArtMethod::GetNameView() { |
| 431 | uint32_t dex_method_idx = GetDexMethodIndex(); |
| 432 | if (LIKELY(dex_method_idx != dex::kDexNoIndex)) { |
| 433 | DCHECK(!IsProxyMethod()); |
| 434 | const DexFile* dex_file = GetDexFile(); |
Vladimir Marko | 4573be3 | 2021-06-07 11:07:05 +0100 | [diff] [blame] | 435 | return dex_file->GetMethodNameView(dex_method_idx); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 436 | } |
Eric Holk | abdb459 | 2019-05-16 08:33:12 -0700 | [diff] [blame] | 437 | return GetRuntimeMethodName(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 440 | inline ObjPtr<mirror::String> ArtMethod::ResolveNameString() { |
| 441 | DCHECK(!IsProxyMethod()); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 442 | const dex::MethodId& method_id = GetDexFile()->GetMethodId(GetDexMethodIndex()); |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 443 | return Runtime::Current()->GetClassLinker()->ResolveString(method_id.name_idx_, this); |
| 444 | } |
| 445 | |
Vladimir Marko | d4e07aa | 2021-10-04 12:56:13 +0100 | [diff] [blame] | 446 | inline bool ArtMethod::NameEquals(ObjPtr<mirror::String> name) { |
| 447 | DCHECK(!IsProxyMethod()); |
| 448 | const DexFile* dex_file = GetDexFile(); |
| 449 | const dex::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex()); |
| 450 | const dex::StringIndex name_idx = method_id.name_idx_; |
| 451 | uint32_t utf16_length; |
| 452 | const char* utf8_name = dex_file->StringDataAndUtf16LengthByIdx(name_idx, &utf16_length); |
| 453 | return dchecked_integral_cast<uint32_t>(name->GetLength()) == utf16_length && |
| 454 | name->Equals(utf8_name); |
| 455 | } |
| 456 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 457 | inline const dex::CodeItem* ArtMethod::GetCodeItem() { |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 458 | if (!HasCodeItem()) { |
| 459 | return nullptr; |
| 460 | } |
| 461 | Runtime* runtime = Runtime::Current(); |
| 462 | PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize(); |
| 463 | return runtime->IsAotCompiler() |
| 464 | ? GetDexFile()->GetCodeItem(reinterpret_cast32<uint32_t>(GetDataPtrSize(pointer_size))) |
Nicolas Geoffray | e1d2dce | 2020-09-21 10:06:31 +0100 | [diff] [blame] | 465 | : reinterpret_cast<const dex::CodeItem*>( |
| 466 | reinterpret_cast<uintptr_t>(GetDataPtrSize(pointer_size)) & ~1); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 469 | inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 470 | DCHECK(!IsProxyMethod()); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 471 | return LookupResolvedClassFromTypeIndex(type_idx) != nullptr; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 475 | DCHECK(!IsProxyMethod()); |
Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 476 | if (dex_pc == dex::kDexNoIndex) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 477 | return IsNative() ? -2 : -1; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 478 | } |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 479 | return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 482 | inline const dex::ProtoId& ArtMethod::GetPrototype() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 483 | DCHECK(!IsProxyMethod()); |
| 484 | const DexFile* dex_file = GetDexFile(); |
| 485 | return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex())); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 488 | inline const dex::TypeList* ArtMethod::GetParameterTypeList() { |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 489 | DCHECK(!IsProxyMethod()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 490 | const DexFile* dex_file = GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 491 | const dex::ProtoId& proto = dex_file->GetMethodPrototype( |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 492 | dex_file->GetMethodId(GetDexMethodIndex())); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 493 | return dex_file->GetProtoParameters(proto); |
| 494 | } |
| 495 | |
| 496 | inline const char* ArtMethod::GetDeclaringClassSourceFile() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 497 | DCHECK(!IsProxyMethod()); |
| 498 | return GetDeclaringClass()->GetSourceFile(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | inline uint16_t ArtMethod::GetClassDefIndex() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 502 | DCHECK(!IsProxyMethod()); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 503 | if (LIKELY(!IsObsolete())) { |
| 504 | return GetDeclaringClass()->GetDexClassDefIndex(); |
| 505 | } else { |
| 506 | return FindObsoleteDexClassDefIndex(); |
| 507 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 510 | inline const dex::ClassDef& ArtMethod::GetClassDef() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 511 | DCHECK(!IsProxyMethod()); |
| 512 | return GetDexFile()->GetClassDef(GetClassDefIndex()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Orion Hodson | 58143d2 | 2018-02-20 08:44:20 +0000 | [diff] [blame] | 515 | inline size_t ArtMethod::GetNumberOfParameters() { |
| 516 | constexpr size_t return_type_count = 1u; |
Jared Duke | 968c813 | 2021-11-02 16:47:54 -0700 | [diff] [blame] | 517 | uint32_t shorty_length; |
| 518 | GetShorty(&shorty_length); |
| 519 | return shorty_length - return_type_count; |
Orion Hodson | 58143d2 | 2018-02-20 08:44:20 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 522 | inline const char* ArtMethod::GetReturnTypeDescriptor() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 523 | DCHECK(!IsProxyMethod()); |
| 524 | const DexFile* dex_file = GetDexFile(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 525 | return dex_file->GetTypeDescriptor(dex_file->GetTypeId(GetReturnTypeIndex())); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Alex Light | d766158 | 2017-05-01 13:48:16 -0700 | [diff] [blame] | 528 | inline Primitive::Type ArtMethod::GetReturnTypePrimitive() { |
| 529 | return Primitive::GetType(GetReturnTypeDescriptor()[0]); |
| 530 | } |
| 531 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 532 | inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 533 | DCHECK(!IsProxyMethod()); |
| 534 | const DexFile* dex_file = GetDexFile(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 535 | return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx)); |
| 536 | } |
| 537 | |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 538 | inline ObjPtr<mirror::ClassLoader> ArtMethod::GetClassLoader() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 539 | DCHECK(!IsProxyMethod()); |
| 540 | return GetDeclaringClass()->GetClassLoader(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Mathieu Chartier | 137cdfa | 2017-01-26 14:03:11 -0800 | [diff] [blame] | 543 | template <ReadBarrierOption kReadBarrierOption> |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 544 | inline ObjPtr<mirror::DexCache> ArtMethod::GetDexCache() { |
Vladimir Marko | c945e0d | 2018-07-18 17:26:45 +0100 | [diff] [blame] | 545 | if (LIKELY(!IsObsolete())) { |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 546 | ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>(); |
Mathieu Chartier | 137cdfa | 2017-01-26 14:03:11 -0800 | [diff] [blame] | 547 | return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>(); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 548 | } else { |
| 549 | DCHECK(!IsProxyMethod()); |
Vladimir Marko | 5f3c7eb | 2022-12-06 13:55:03 +0000 | [diff] [blame] | 550 | return GetObsoleteDexCache<kReadBarrierOption>(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 551 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 554 | inline bool ArtMethod::IsProxyMethod() { |
Roland Levillain | fa854e4 | 2018-02-07 13:09:55 +0000 | [diff] [blame] | 555 | DCHECK(!IsRuntimeMethod()) << "ArtMethod::IsProxyMethod called on a runtime method"; |
Vladimir Marko | 415ac85 | 2019-03-21 12:49:10 +0000 | [diff] [blame] | 556 | // No read barrier needed, we're reading the constant declaring class only to read |
| 557 | // the constant proxy flag. See ReadBarrierOption. |
Mathieu Chartier | 90c5a9b | 2017-02-01 13:10:06 -0800 | [diff] [blame] | 558 | return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 561 | inline ArtMethod* ArtMethod::GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size) { |
| 562 | DCHECK(IsProxyMethod()); |
| 563 | // Do not check IsAssignableFrom() here as it relies on raw reference comparison |
| 564 | // which may give false negatives while visiting references for a non-CC moving GC. |
| 565 | return reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size)); |
| 566 | } |
| 567 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 568 | inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) { |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 569 | if (LIKELY(!IsProxyMethod())) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 570 | return this; |
| 571 | } |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 572 | ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size); |
| 573 | // We can check that the proxy class implements the interface only if the proxy class |
| 574 | // is resolved, otherwise the interface table is not yet initialized. |
Santiago Aboy Solanes | 6cdabe1 | 2022-02-18 15:27:43 +0000 | [diff] [blame] | 575 | DCHECK_IMPLIES(GetDeclaringClass()->IsResolved(), |
| 576 | interface_method->GetDeclaringClass()->IsAssignableFrom(GetDeclaringClass())); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 577 | return interface_method; |
| 578 | } |
| 579 | |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 580 | inline dex::TypeIndex ArtMethod::GetReturnTypeIndex() { |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 581 | DCHECK(!IsProxyMethod()); |
| 582 | const DexFile* dex_file = GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 583 | const dex::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex()); |
| 584 | const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 585 | return proto_id.return_type_idx_; |
| 586 | } |
| 587 | |
| 588 | inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedReturnType() { |
| 589 | return LookupResolvedClassFromTypeIndex(GetReturnTypeIndex()); |
| 590 | } |
| 591 | |
| 592 | inline ObjPtr<mirror::Class> ArtMethod::ResolveReturnType() { |
| 593 | return ResolveClassFromTypeIndex(GetReturnTypeIndex()); |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 596 | inline bool ArtMethod::HasSingleImplementation() { |
Vladimir Marko | 3292d10 | 2022-12-09 14:22:25 +0000 | [diff] [blame] | 597 | // No read barrier needed for reading a constant reference only to read |
| 598 | // a constant final class flag. See `ReadBarrierOption`. |
| 599 | if (IsFinal() || GetDeclaringClass<kWithoutReadBarrier>()->IsFinal()) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 600 | // We don't set kAccSingleImplementation for these cases since intrinsic |
| 601 | // can use the flag also. |
| 602 | return true; |
| 603 | } |
Vladimir Marko | c945e0d | 2018-07-18 17:26:45 +0100 | [diff] [blame] | 604 | return (GetAccessFlags() & kAccSingleImplementation) != 0; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Lokesh Gidra | b7607c2 | 2022-01-28 12:30:31 -0800 | [diff] [blame] | 607 | template<ReadBarrierOption kReadBarrierOption, bool kVisitProxyMethod, typename RootVisitorType> |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 608 | void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) { |
Hiroshi Yamauchi | 7a62e67 | 2016-06-10 17:22:48 -0700 | [diff] [blame] | 609 | if (LIKELY(!declaring_class_.IsNull())) { |
| 610 | visitor.VisitRoot(declaring_class_.AddressWithoutBarrier()); |
Lokesh Gidra | b7607c2 | 2022-01-28 12:30:31 -0800 | [diff] [blame] | 611 | if (kVisitProxyMethod) { |
| 612 | ObjPtr<mirror::Class> klass = declaring_class_.Read<kReadBarrierOption>(); |
| 613 | if (UNLIKELY(klass->IsProxyClass())) { |
| 614 | // For normal methods, dex cache shortcuts will be visited through the declaring class. |
| 615 | // However, for proxies we need to keep the interface method alive, so we visit its roots. |
| 616 | ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size); |
| 617 | DCHECK(interface_method != nullptr); |
| 618 | interface_method->VisitRoots<kReadBarrierOption, kVisitProxyMethod>(visitor, pointer_size); |
| 619 | } |
Nicolas Geoffray | dec3a12 | 2016-02-13 12:38:36 +0000 | [diff] [blame] | 620 | } |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 621 | } |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 624 | template<typename RootVisitorType> |
| 625 | void ArtMethod::VisitRoots(RootVisitorType& visitor, |
| 626 | uint8_t* start_boundary, |
| 627 | uint8_t* end_boundary, |
| 628 | ArtMethod* method) { |
| 629 | mirror::CompressedReference<mirror::Object>* cls_ptr = |
| 630 | reinterpret_cast<mirror::CompressedReference<mirror::Object>*>( |
| 631 | reinterpret_cast<uint8_t*>(method) + DeclaringClassOffset().Int32Value()); |
| 632 | if (reinterpret_cast<uint8_t*>(cls_ptr) >= start_boundary |
| 633 | && reinterpret_cast<uint8_t*>(cls_ptr) < end_boundary) { |
| 634 | visitor.VisitRootIfNonNull(cls_ptr); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | template<PointerSize kPointerSize, typename RootVisitorType> |
| 639 | void ArtMethod::VisitArrayRoots(RootVisitorType& visitor, |
| 640 | uint8_t* start_boundary, |
| 641 | uint8_t* end_boundary, |
| 642 | LengthPrefixedArray<ArtMethod>* array) { |
| 643 | DCHECK_LE(start_boundary, end_boundary); |
| 644 | DCHECK_NE(array->size(), 0u); |
| 645 | static constexpr size_t kMethodSize = ArtMethod::Size(kPointerSize); |
| 646 | ArtMethod* first_method = &array->At(0, kMethodSize, ArtMethod::Alignment(kPointerSize)); |
| 647 | DCHECK_LE(static_cast<void*>(end_boundary), |
| 648 | static_cast<void*>(reinterpret_cast<uint8_t*>(first_method) |
| 649 | + array->size() * kMethodSize)); |
| 650 | uint8_t* declaring_class = |
| 651 | reinterpret_cast<uint8_t*>(first_method) + DeclaringClassOffset().Int32Value(); |
| 652 | // Jump to the first class to visit. |
| 653 | if (declaring_class < start_boundary) { |
| 654 | size_t remainder = (start_boundary - declaring_class) % kMethodSize; |
| 655 | declaring_class = start_boundary; |
| 656 | if (remainder > 0) { |
| 657 | declaring_class += kMethodSize - remainder; |
| 658 | } |
| 659 | } |
| 660 | while (declaring_class < end_boundary) { |
| 661 | visitor.VisitRootIfNonNull( |
| 662 | reinterpret_cast<mirror::CompressedReference<mirror::Object>*>(declaring_class)); |
| 663 | declaring_class += kMethodSize; |
| 664 | } |
| 665 | } |
| 666 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 667 | template <typename Visitor> |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 668 | inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) { |
Vladimir Marko | c945e0d | 2018-07-18 17:26:45 +0100 | [diff] [blame] | 669 | if (IsNative()) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 670 | const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 671 | const void* new_native_code = visitor(old_native_code); |
| 672 | if (old_native_code != new_native_code) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 673 | SetEntryPointFromJniPtrSize(new_native_code, pointer_size); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 674 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 675 | } |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 676 | const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 677 | const void* new_code = visitor(old_code); |
| 678 | if (old_code != new_code) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 679 | SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
Vladimir Marko | a20ec9b | 2022-12-12 09:12:21 +0000 | [diff] [blame] | 683 | template <ReadBarrierOption kReadBarrierOption> |
| 684 | inline bool ArtMethod::StillNeedsClinitCheck() { |
| 685 | if (!NeedsClinitCheckBeforeCall()) { |
| 686 | return false; |
| 687 | } |
| 688 | ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>(); |
| 689 | return !klass->IsVisiblyInitialized(); |
| 690 | } |
| 691 | |
| 692 | inline bool ArtMethod::StillNeedsClinitCheckMayBeDead() { |
| 693 | if (!NeedsClinitCheckBeforeCall()) { |
| 694 | return false; |
| 695 | } |
Vladimir Marko | d44adb6 | 2022-12-12 16:28:25 +0000 | [diff] [blame] | 696 | ObjPtr<mirror::Class> klass = GetDeclaringClassMayBeDead(); |
| 697 | return !klass->IsVisiblyInitialized(); |
| 698 | } |
| 699 | |
| 700 | inline bool ArtMethod::IsDeclaringClassVerifiedMayBeDead() { |
| 701 | ObjPtr<mirror::Class> klass = GetDeclaringClassMayBeDead(); |
| 702 | return klass->IsVerified(); |
| 703 | } |
| 704 | |
| 705 | inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClassMayBeDead() { |
| 706 | // Helper method for checking the status of the declaring class which may be dead. |
| 707 | // |
Vladimir Marko | a20ec9b | 2022-12-12 09:12:21 +0000 | [diff] [blame] | 708 | // To avoid resurrecting an unreachable object, or crashing the GC in some GC phases, |
| 709 | // we must not use a full read barrier. Therefore we read the declaring class without |
| 710 | // a read barrier and check if it's already marked. If yes, we check the status of the |
| 711 | // to-space class object as intended. Otherwise, there is no to-space object and the |
| 712 | // from-space class object contains the most recent value of the status field; even if |
| 713 | // this races with another thread doing a read barrier and updating the status, that's |
| 714 | // no different from a race with a thread that just updates the status. |
| 715 | ObjPtr<mirror::Class> klass = GetDeclaringClass<kWithoutReadBarrier>(); |
| 716 | ObjPtr<mirror::Class> marked = ReadBarrier::IsMarked(klass.Ptr()); |
Vladimir Marko | d44adb6 | 2022-12-12 16:28:25 +0000 | [diff] [blame] | 717 | return (marked != nullptr) ? marked : klass; |
Vladimir Marko | a20ec9b | 2022-12-12 09:12:21 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 720 | inline CodeItemInstructionAccessor ArtMethod::DexInstructions() { |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 721 | return CodeItemInstructionAccessor(*GetDexFile(), GetCodeItem()); |
| 722 | } |
| 723 | |
| 724 | inline CodeItemDataAccessor ArtMethod::DexInstructionData() { |
| 725 | return CodeItemDataAccessor(*GetDexFile(), GetCodeItem()); |
| 726 | } |
| 727 | |
| 728 | inline CodeItemDebugInfoAccessor ArtMethod::DexInstructionDebugInfo() { |
| 729 | return CodeItemDebugInfoAccessor(*GetDexFile(), GetCodeItem(), GetDexMethodIndex()); |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 732 | inline bool ArtMethod::CounterHasChanged(uint16_t threshold) { |
Vladimir Marko | dd446b1 | 2021-05-20 14:35:51 +0100 | [diff] [blame] | 733 | DCHECK(!IsAbstract()); |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 734 | DCHECK_EQ(threshold, Runtime::Current()->GetJITOptions()->GetWarmupThreshold()); |
| 735 | return hotness_count_ != threshold; |
Nicolas Geoffray | 61673dc | 2021-11-06 13:58:31 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 738 | inline void ArtMethod::ResetCounter(uint16_t new_value) { |
| 739 | if (IsAbstract()) { |
| 740 | return; |
| 741 | } |
Nicolas Geoffray | f9ae8e3 | 2022-02-15 22:54:11 +0000 | [diff] [blame] | 742 | if (IsMemorySharedMethod()) { |
| 743 | return; |
| 744 | } |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 745 | DCHECK_EQ(new_value, Runtime::Current()->GetJITOptions()->GetWarmupThreshold()); |
Nicolas Geoffray | 61673dc | 2021-11-06 13:58:31 +0000 | [diff] [blame] | 746 | // Avoid dirtying the value if possible. |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 747 | if (hotness_count_ != new_value) { |
| 748 | hotness_count_ = new_value; |
Nicolas Geoffray | 61673dc | 2021-11-06 13:58:31 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
| 752 | inline void ArtMethod::SetHotCounter() { |
| 753 | DCHECK(!IsAbstract()); |
| 754 | // Avoid dirtying the value if possible. |
| 755 | if (hotness_count_ != 0) { |
| 756 | hotness_count_ = 0; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | inline void ArtMethod::UpdateCounter(int32_t new_samples) { |
| 761 | DCHECK(!IsAbstract()); |
| 762 | DCHECK_GT(new_samples, 0); |
| 763 | DCHECK_LE(new_samples, std::numeric_limits<uint16_t>::max()); |
Nicolas Geoffray | f9ae8e3 | 2022-02-15 22:54:11 +0000 | [diff] [blame] | 764 | if (IsMemorySharedMethod()) { |
| 765 | return; |
| 766 | } |
Nicolas Geoffray | 61673dc | 2021-11-06 13:58:31 +0000 | [diff] [blame] | 767 | uint16_t old_hotness_count = hotness_count_; |
| 768 | uint16_t new_count = (old_hotness_count <= new_samples) ? 0u : old_hotness_count - new_samples; |
| 769 | // Avoid dirtying the value if possible. |
| 770 | if (old_hotness_count != new_count) { |
| 771 | hotness_count_ = new_count; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | inline bool ArtMethod::CounterIsHot() { |
| 776 | DCHECK(!IsAbstract()); |
| 777 | return hotness_count_ == 0; |
| 778 | } |
| 779 | |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 780 | inline bool ArtMethod::CounterHasReached(uint16_t samples, uint16_t threshold) { |
Nicolas Geoffray | 61673dc | 2021-11-06 13:58:31 +0000 | [diff] [blame] | 781 | DCHECK(!IsAbstract()); |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 782 | DCHECK_EQ(threshold, Runtime::Current()->GetJITOptions()->GetWarmupThreshold()); |
| 783 | DCHECK_LE(samples, threshold); |
| 784 | return hotness_count_ <= (threshold - samples); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | inline uint16_t ArtMethod::GetCounter() { |
Vladimir Marko | dd446b1 | 2021-05-20 14:35:51 +0100 | [diff] [blame] | 788 | DCHECK(!IsAbstract()); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 789 | return hotness_count_; |
| 790 | } |
| 791 | |
| 792 | inline uint32_t ArtMethod::GetImtIndex() { |
Nicolas Geoffray | d5a8695 | 2021-01-19 10:35:54 +0000 | [diff] [blame] | 793 | if (LIKELY(IsAbstract())) { |
| 794 | return imt_index_; |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 795 | } else { |
| 796 | return ImTable::GetImtIndex(this); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | inline void ArtMethod::CalculateAndSetImtIndex() { |
| 801 | DCHECK(IsAbstract()) << PrettyMethod(); |
Nicolas Geoffray | d5a8695 | 2021-01-19 10:35:54 +0000 | [diff] [blame] | 802 | imt_index_ = ImTable::GetImtIndex(this); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 805 | } // namespace art |
| 806 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 807 | #endif // ART_RUNTIME_ART_METHOD_INL_H_ |