| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | #include "interpreter_common.h" |
| Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 18 | |
| Andreas Gampe | f0e128a | 2015-02-27 20:08:34 -0800 | [diff] [blame] | 19 | #include <cmath> |
| 20 | |
| Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 21 | #include "base/casts.h" |
| Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 22 | #include "base/enums.h" |
| Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 23 | #include "class_root-inl.h" |
| Daniel Mihalyi | eb07669 | 2014-08-22 17:33:31 +0200 | [diff] [blame] | 24 | #include "debugger.h" |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 25 | #include "dex/dex_file_types.h" |
| Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 26 | #include "entrypoints/runtime_asm_entrypoints.h" |
| Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 27 | #include "handle.h" |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 28 | #include "intrinsics_enum.h" |
| Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 29 | #include "jit/jit.h" |
| Andreas Gampe | c5b7564 | 2018-05-16 15:12:11 -0700 | [diff] [blame] | 30 | #include "jvalue-inl.h" |
| Narayan Kamath | 208f857 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 31 | #include "method_handles-inl.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 32 | #include "method_handles.h" |
| Andreas Gampe | 8e0f043 | 2018-10-24 13:38:03 -0700 | [diff] [blame] | 33 | #include "mirror/array-alloc-inl.h" |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 34 | #include "mirror/array-inl.h" |
| Vladimir Marko | 621c880 | 2019-03-27 16:18:18 +0000 | [diff] [blame] | 35 | #include "mirror/call_site-inl.h" |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 36 | #include "mirror/class.h" |
| Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 37 | #include "mirror/emulated_stack_frame.h" |
| Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 38 | #include "mirror/method_handle_impl-inl.h" |
| Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 39 | #include "mirror/method_type-inl.h" |
| Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 40 | #include "mirror/object_array-alloc-inl.h" |
| 41 | #include "mirror/object_array-inl.h" |
| Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 42 | #include "mirror/var_handle.h" |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 43 | #include "reflection-inl.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 44 | #include "reflection.h" |
| Vladimir Marko | 6ec2a1b | 2018-05-22 15:33:48 +0100 | [diff] [blame] | 45 | #include "shadow_frame-inl.h" |
| Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 46 | #include "stack.h" |
| Andreas Gampe | 513061a | 2017-06-01 09:17:34 -0700 | [diff] [blame] | 47 | #include "thread-inl.h" |
| Chang Xing | bd208d8 | 2017-07-12 14:53:17 -0700 | [diff] [blame] | 48 | #include "transaction.h" |
| Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 49 | #include "var_handles.h" |
| Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 50 | #include "well_known_classes.h" |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 51 | |
| 52 | namespace art { |
| 53 | namespace interpreter { |
| 54 | |
| Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 55 | void ThrowNullPointerExceptionFromInterpreter() { |
| 56 | ThrowNullPointerExceptionFromDexPC(); |
| Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| David Srbecky | 960327b | 2018-10-25 10:11:59 +0000 | [diff] [blame] | 59 | bool CheckStackOverflow(Thread* self, size_t frame_size) |
| 60 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 61 | bool implicit_check = !Runtime::Current()->ExplicitStackOverflowChecks(); |
| 62 | uint8_t* stack_end = self->GetStackEndForInterpreter(implicit_check); |
| 63 | if (UNLIKELY(__builtin_frame_address(0) < stack_end + frame_size)) { |
| 64 | ThrowStackOverflowError(self); |
| 65 | return false; |
| 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | |
| David Srbecky | 9581e61 | 2018-10-30 14:29:43 +0000 | [diff] [blame] | 70 | bool UseFastInterpreterToInterpreterInvoke(ArtMethod* method) { |
| 71 | Runtime* runtime = Runtime::Current(); |
| 72 | const void* quick_code = method->GetEntryPointFromQuickCompiledCode(); |
| 73 | if (!runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) { |
| 74 | return false; |
| 75 | } |
| 76 | if (!method->SkipAccessChecks() || method->IsNative() || method->IsProxyMethod()) { |
| 77 | return false; |
| 78 | } |
| 79 | if (method->IsIntrinsic()) { |
| 80 | return false; |
| 81 | } |
| 82 | if (method->GetDeclaringClass()->IsStringClass() && method->IsConstructor()) { |
| 83 | return false; |
| 84 | } |
| Vladimir Marko | baa81b5 | 2019-08-02 10:14:04 +0100 | [diff] [blame] | 85 | if (method->IsStatic() && !method->GetDeclaringClass()->IsVisiblyInitialized()) { |
| David Srbecky | 9581e61 | 2018-10-30 14:29:43 +0000 | [diff] [blame] | 86 | return false; |
| 87 | } |
| David Srbecky | 9581e61 | 2018-10-30 14:29:43 +0000 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | |
| Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 91 | template <typename T> |
| 92 | bool SendMethodExitEvents(Thread* self, |
| 93 | const instrumentation::Instrumentation* instrumentation, |
| 94 | ShadowFrame& frame, |
| 95 | ObjPtr<mirror::Object> thiz, |
| 96 | ArtMethod* method, |
| 97 | uint32_t dex_pc, |
| 98 | T& result) { |
| 99 | bool had_event = false; |
| 100 | // We can get additional ForcePopFrame requests during handling of these events. We should |
| 101 | // respect these and send additional instrumentation events. |
| 102 | StackHandleScope<1> hs(self); |
| 103 | Handle<mirror::Object> h_thiz(hs.NewHandle(thiz)); |
| 104 | do { |
| 105 | frame.SetForcePopFrame(false); |
| 106 | if (UNLIKELY(instrumentation->HasMethodExitListeners() && !frame.GetSkipMethodExitEvents())) { |
| 107 | had_event = true; |
| 108 | instrumentation->MethodExitEvent( |
| 109 | self, h_thiz.Get(), method, dex_pc, instrumentation::OptionalFrame{ frame }, result); |
| 110 | } |
| 111 | // We don't send method-exit if it's a pop-frame. We still send frame_popped though. |
| 112 | if (UNLIKELY(frame.NeedsNotifyPop() && instrumentation->HasWatchedFramePopListeners())) { |
| 113 | had_event = true; |
| 114 | instrumentation->WatchedFramePopped(self, frame); |
| 115 | } |
| 116 | } while (UNLIKELY(frame.GetForcePopFrame())); |
| 117 | if (UNLIKELY(had_event)) { |
| 118 | return !self->IsExceptionPending(); |
| 119 | } else { |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | template |
| 125 | bool SendMethodExitEvents(Thread* self, |
| 126 | const instrumentation::Instrumentation* instrumentation, |
| 127 | ShadowFrame& frame, |
| 128 | ObjPtr<mirror::Object> thiz, |
| 129 | ArtMethod* method, |
| 130 | uint32_t dex_pc, |
| 131 | MutableHandle<mirror::Object>& result); |
| 132 | |
| 133 | template |
| 134 | bool SendMethodExitEvents(Thread* self, |
| 135 | const instrumentation::Instrumentation* instrumentation, |
| 136 | ShadowFrame& frame, |
| 137 | ObjPtr<mirror::Object> thiz, |
| 138 | ArtMethod* method, |
| 139 | uint32_t dex_pc, |
| 140 | JValue& result); |
| 141 | |
| Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 142 | // We execute any instrumentation events that are triggered by this exception and change the |
| 143 | // shadow_frame's dex_pc to that of the exception handler if there is one in the current method. |
| 144 | // Return true if we should continue executing in the current method and false if we need to go up |
| 145 | // the stack to find an exception handler. |
| Sebastien Hertz | 520633b | 2015-09-08 17:03:36 +0200 | [diff] [blame] | 146 | // We accept a null Instrumentation* meaning we must not report anything to the instrumentation. |
| Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 147 | // TODO We should have a better way to skip instrumentation reporting or possibly rethink that |
| 148 | // behavior. |
| 149 | bool MoveToExceptionHandler(Thread* self, |
| 150 | ShadowFrame& shadow_frame, |
| 151 | const instrumentation::Instrumentation* instrumentation) { |
| Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 152 | self->VerifyStack(); |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 153 | StackHandleScope<2> hs(self); |
| Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 154 | Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException())); |
| Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 155 | if (instrumentation != nullptr && |
| 156 | instrumentation->HasExceptionThrownListeners() && |
| 157 | self->IsExceptionThrownByCurrentMethod(exception.Get())) { |
| 158 | // See b/65049545 for why we don't need to check to see if the exception has changed. |
| Alex Light | 6e1607e | 2017-08-23 10:06:18 -0700 | [diff] [blame] | 159 | instrumentation->ExceptionThrownEvent(self, exception.Get()); |
| Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 160 | if (shadow_frame.GetForcePopFrame()) { |
| 161 | // We will check in the caller for GetForcePopFrame again. We need to bail out early to |
| 162 | // prevent an ExceptionHandledEvent from also being sent before popping. |
| 163 | return true; |
| 164 | } |
| Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 165 | } |
| Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 166 | bool clear_exception = false; |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 167 | uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock( |
| Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 168 | hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception); |
| Andreas Gampe | e2abbc6 | 2017-09-15 11:59:26 -0700 | [diff] [blame] | 169 | if (found_dex_pc == dex::kDexNoIndex) { |
| Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 170 | if (instrumentation != nullptr) { |
| 171 | if (shadow_frame.NeedsNotifyPop()) { |
| 172 | instrumentation->WatchedFramePopped(self, shadow_frame); |
| Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 173 | if (shadow_frame.GetForcePopFrame()) { |
| 174 | // We will check in the caller for GetForcePopFrame again. We need to bail out early to |
| 175 | // prevent an ExceptionHandledEvent from also being sent before popping and to ensure we |
| 176 | // handle other types of non-standard-exits. |
| 177 | return true; |
| 178 | } |
| Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 179 | } |
| 180 | // Exception is not caught by the current method. We will unwind to the |
| 181 | // caller. Notify any instrumentation listener. |
| 182 | instrumentation->MethodUnwindEvent(self, |
| 183 | shadow_frame.GetThisObject(), |
| 184 | shadow_frame.GetMethod(), |
| 185 | shadow_frame.GetDexPC()); |
| Alex Light | e814f9d | 2017-07-31 16:14:39 -0700 | [diff] [blame] | 186 | } |
| Alex Light | b7c640d | 2019-03-20 15:52:13 -0700 | [diff] [blame] | 187 | return shadow_frame.GetForcePopFrame(); |
| Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 188 | } else { |
| Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 189 | shadow_frame.SetDexPC(found_dex_pc); |
| 190 | if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) { |
| 191 | self->ClearException(); |
| 192 | instrumentation->ExceptionHandledEvent(self, exception.Get()); |
| 193 | if (UNLIKELY(self->IsExceptionPending())) { |
| 194 | // Exception handled event threw an exception. Try to find the handler for this one. |
| 195 | return MoveToExceptionHandler(self, shadow_frame, instrumentation); |
| 196 | } else if (!clear_exception) { |
| 197 | self->SetException(exception.Get()); |
| 198 | } |
| 199 | } else if (clear_exception) { |
| Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 200 | self->ClearException(); |
| 201 | } |
| Alex Light | 9fb1ab1 | 2017-09-05 09:32:49 -0700 | [diff] [blame] | 202 | return true; |
| Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 203 | } |
| Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 206 | void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) { |
| 207 | LOG(FATAL) << "Unexpected instruction: " |
| 208 | << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()); |
| 209 | UNREACHABLE(); |
| Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 212 | void AbortTransactionF(Thread* self, const char* fmt, ...) { |
| Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 213 | va_list args; |
| 214 | va_start(args, fmt); |
| Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 215 | AbortTransactionV(self, fmt, args); |
| 216 | va_end(args); |
| 217 | } |
| 218 | |
| 219 | void AbortTransactionV(Thread* self, const char* fmt, va_list args) { |
| 220 | CHECK(Runtime::Current()->IsActiveTransaction()); |
| 221 | // Constructs abort message. |
| Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 222 | std::string abort_msg; |
| Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 223 | android::base::StringAppendV(&abort_msg, fmt, args); |
| Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 224 | // Throws an exception so we can abort the transaction and rollback every change. |
| Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 225 | Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg); |
| Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 228 | // START DECLARATIONS : |
| 229 | // |
| 230 | // These additional declarations are required because clang complains |
| 231 | // about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions. |
| 232 | // |
| 233 | |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 234 | template <bool is_range, bool do_assignability_check> |
| Vladimir Marko | d16363a | 2017-02-01 14:09:13 +0000 | [diff] [blame] | 235 | static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method, |
| 236 | Thread* self, |
| 237 | ShadowFrame& shadow_frame, |
| 238 | JValue* result, |
| 239 | uint16_t number_of_inputs, |
| 240 | uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| 241 | uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_); |
| Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 242 | |
| 243 | template <bool is_range> |
| Narayan Kamath | 2cb856c | 2016-11-02 12:01:26 +0000 | [diff] [blame] | 244 | ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame, |
| 245 | ShadowFrame* callee_frame, |
| 246 | const uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| 247 | const size_t first_src_reg, |
| 248 | const size_t first_dest_reg, |
| 249 | const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_); |
| Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 250 | |
| 251 | // END DECLARATIONS. |
| 252 | |
| Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 253 | void ArtInterpreterToCompiledCodeBridge(Thread* self, |
| Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 254 | ArtMethod* caller, |
| Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 255 | ShadowFrame* shadow_frame, |
| Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 256 | uint16_t arg_offset, |
| Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 257 | JValue* result) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 258 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 259 | ArtMethod* method = shadow_frame->GetMethod(); |
| 260 | // Ensure static methods are initialized. |
| 261 | if (method->IsStatic()) { |
| Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 262 | ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass(); |
| Vladimir Marko | baa81b5 | 2019-08-02 10:14:04 +0100 | [diff] [blame] | 263 | if (UNLIKELY(!declaringClass->IsVisiblyInitialized())) { |
| Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 264 | self->PushShadowFrame(shadow_frame); |
| 265 | StackHandleScope<1> hs(self); |
| 266 | Handle<mirror::Class> h_class(hs.NewHandle(declaringClass)); |
| Vladimir Marko | baa81b5 | 2019-08-02 10:14:04 +0100 | [diff] [blame] | 267 | if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized( |
| 268 | self, h_class, /*can_init_fields=*/ true, /*can_init_parents=*/ true))) { |
| Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 269 | self->PopShadowFrame(); |
| 270 | DCHECK(self->IsExceptionPending()); |
| 271 | return; |
| 272 | } |
| 273 | self->PopShadowFrame(); |
| Vladimir Marko | baa81b5 | 2019-08-02 10:14:04 +0100 | [diff] [blame] | 274 | DCHECK(h_class->IsInitializing()); |
| Nicolas Geoffray | 0182229 | 2017-03-09 09:03:19 +0000 | [diff] [blame] | 275 | // Reload from shadow frame in case the method moved, this is faster than adding a handle. |
| 276 | method = shadow_frame->GetMethod(); |
| Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 279 | // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise, |
| 280 | // check that the arg_offset isn't greater than the number of registers. A stronger check is |
| 281 | // difficult since the frame may contain space for all the registers in the method, or only enough |
| 282 | // space for the arguments. |
| 283 | if (kIsDebugBuild) { |
| 284 | if (method->GetCodeItem() == nullptr) { |
| 285 | DCHECK_EQ(0u, arg_offset) << method->PrettyMethod(); |
| 286 | } else { |
| 287 | DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs()); |
| 288 | } |
| 289 | } |
| Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 290 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 291 | if (jit != nullptr && caller != nullptr) { |
| 292 | jit->NotifyInterpreterToCompiledCodeTransition(self, caller); |
| 293 | } |
| Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 294 | method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset), |
| 295 | (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t), |
| Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 296 | result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty()); |
| Andreas Gampe | 3cfa4d0 | 2015-10-06 17:04:01 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 299 | void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame, |
| 300 | uint16_t this_obj_vreg, |
| 301 | JValue result) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 302 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 303 | ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg); |
| Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 304 | if (existing == nullptr) { |
| 305 | // If it's null, we come from compiled code that was deoptimized. Nothing to do, |
| 306 | // as the compiler verified there was no alias. |
| 307 | // Set the new string result of the StringFactory. |
| 308 | shadow_frame->SetVRegReference(this_obj_vreg, result.GetL()); |
| 309 | return; |
| 310 | } |
| 311 | // Set the string init result into all aliases. |
| 312 | for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) { |
| 313 | if (shadow_frame->GetVRegReference(i) == existing) { |
| 314 | DCHECK_EQ(shadow_frame->GetVRegReference(i), |
| Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 315 | reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i))); |
| Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 316 | shadow_frame->SetVRegReference(i, result.GetL()); |
| 317 | DCHECK_EQ(shadow_frame->GetVRegReference(i), |
| Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 318 | reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i))); |
| Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 323 | template<bool is_range> |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 324 | static bool DoMethodHandleInvokeCommon(Thread* self, |
| 325 | ShadowFrame& shadow_frame, |
| 326 | bool invoke_exact, |
| 327 | const Instruction* inst, |
| 328 | uint16_t inst_data, |
| 329 | JValue* result) |
| Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 330 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Alex Light | 848574c | 2017-09-25 16:59:39 -0700 | [diff] [blame] | 331 | // Make sure to check for async exceptions |
| 332 | if (UNLIKELY(self->ObserveAsyncException())) { |
| 333 | return false; |
| 334 | } |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 335 | // Invoke-polymorphic instructions always take a receiver. i.e, they are never static. |
| 336 | const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc(); |
| Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 337 | const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc(); |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 338 | |
| Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 339 | // Initialize |result| to 0 as this is the default return value for |
| 340 | // polymorphic invocations of method handle types with void return |
| Orion Hodson | aacf977 | 2020-07-22 21:51:00 +0100 | [diff] [blame] | 341 | // and provides a sensible return result in error cases. |
| Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 342 | result->SetJ(0); |
| 343 | |
| Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 344 | // The invoke_method_idx here is the name of the signature polymorphic method that |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 345 | // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact) |
| 346 | // and not the method that we'll dispatch to in the end. |
| Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 347 | StackHandleScope<2> hs(self); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 348 | Handle<mirror::MethodHandle> method_handle(hs.NewHandle( |
| Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 349 | ObjPtr<mirror::MethodHandle>::DownCast(shadow_frame.GetVRegReference(vRegC)))); |
| Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 350 | if (UNLIKELY(method_handle == nullptr)) { |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 351 | // Note that the invoke type is kVirtual here because a call to a signature |
| 352 | // polymorphic method is shaped like a virtual call at the bytecode level. |
| Orion Hodson | 3d617ac | 2016-10-19 14:00:46 +0100 | [diff] [blame] | 353 | ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual); |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 354 | return false; |
| 355 | } |
| 356 | |
| 357 | // The vRegH value gives the index of the proto_id associated with this |
| Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 358 | // signature polymorphic call site. |
| Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 359 | const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc(); |
| 360 | const dex::ProtoIndex callsite_proto_id(vRegH); |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 361 | |
| 362 | // Call through to the classlinker and ask it to resolve the static type associated |
| 363 | // with the callsite. This information is stored in the dex cache so it's |
| 364 | // guaranteed to be fast after the first resolution. |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 365 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| Orion Hodson | e7732be | 2017-10-11 14:35:20 +0100 | [diff] [blame] | 366 | Handle<mirror::MethodType> callsite_type(hs.NewHandle( |
| 367 | class_linker->ResolveMethodType(self, callsite_proto_id, shadow_frame.GetMethod()))); |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 368 | |
| 369 | // This implies we couldn't resolve one or more types in this method handle. |
| Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 370 | if (UNLIKELY(callsite_type == nullptr)) { |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 371 | CHECK(self->IsExceptionPending()); |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 372 | return false; |
| 373 | } |
| 374 | |
| Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 375 | // There is a common dispatch method for method handles that takes |
| 376 | // arguments either from a range or an array of arguments depending |
| 377 | // on whether the DEX instruction is invoke-polymorphic/range or |
| 378 | // invoke-polymorphic. The array here is for the latter. |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 379 | if (UNLIKELY(is_range)) { |
| Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 380 | // VRegC is the register holding the method handle. Arguments passed |
| 381 | // to the method handle's target do not include the method handle. |
| Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 382 | RangeInstructionOperands operands(inst->VRegC_4rcc() + 1, inst->VRegA_4rcc() - 1); |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 383 | if (invoke_exact) { |
| Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 384 | return MethodHandleInvokeExact(self, |
| 385 | shadow_frame, |
| 386 | method_handle, |
| 387 | callsite_type, |
| 388 | &operands, |
| 389 | result); |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 390 | } else { |
| Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 391 | return MethodHandleInvoke(self, |
| 392 | shadow_frame, |
| 393 | method_handle, |
| 394 | callsite_type, |
| 395 | &operands, |
| 396 | result); |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 397 | } |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 398 | } else { |
| Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 399 | // Get the register arguments for the invoke. |
| Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 400 | uint32_t args[Instruction::kMaxVarArgRegs] = {}; |
| Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 401 | inst->GetVarArgs(args, inst_data); |
| 402 | // Drop the first register which is the method handle performing the invoke. |
| Alexey Frunze | 8631a46 | 2017-01-19 19:07:37 -0800 | [diff] [blame] | 403 | memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1)); |
| Orion Hodson | 811bd5f | 2016-12-07 11:35:37 +0000 | [diff] [blame] | 404 | args[Instruction::kMaxVarArgRegs - 1] = 0; |
| Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 405 | VarArgsInstructionOperands operands(args, inst->VRegA_45cc() - 1); |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 406 | if (invoke_exact) { |
| Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 407 | return MethodHandleInvokeExact(self, |
| 408 | shadow_frame, |
| 409 | method_handle, |
| 410 | callsite_type, |
| 411 | &operands, |
| 412 | result); |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 413 | } else { |
| Orion Hodson | 960d4f7 | 2017-11-10 15:32:38 +0000 | [diff] [blame] | 414 | return MethodHandleInvoke(self, |
| 415 | shadow_frame, |
| 416 | method_handle, |
| 417 | callsite_type, |
| 418 | &operands, |
| 419 | result); |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | bool DoMethodHandleInvokeExact(Thread* self, |
| 425 | ShadowFrame& shadow_frame, |
| 426 | const Instruction* inst, |
| 427 | uint16_t inst_data, |
| 428 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 429 | if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) { |
| 430 | static const bool kIsRange = false; |
| 431 | return DoMethodHandleInvokeCommon<kIsRange>( |
| Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 432 | self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result); |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 433 | } else { |
| 434 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE); |
| 435 | static const bool kIsRange = true; |
| 436 | return DoMethodHandleInvokeCommon<kIsRange>( |
| Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 437 | self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result); |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | bool DoMethodHandleInvoke(Thread* self, |
| 442 | ShadowFrame& shadow_frame, |
| 443 | const Instruction* inst, |
| 444 | uint16_t inst_data, |
| 445 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 446 | if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) { |
| 447 | static const bool kIsRange = false; |
| 448 | return DoMethodHandleInvokeCommon<kIsRange>( |
| Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 449 | self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result); |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 450 | } else { |
| 451 | DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE); |
| 452 | static const bool kIsRange = true; |
| 453 | return DoMethodHandleInvokeCommon<kIsRange>( |
| Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 454 | self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result); |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
| Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 458 | static bool DoVarHandleInvokeCommon(Thread* self, |
| 459 | ShadowFrame& shadow_frame, |
| 460 | const Instruction* inst, |
| 461 | uint16_t inst_data, |
| 462 | JValue* result, |
| 463 | mirror::VarHandle::AccessMode access_mode) |
| 464 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 465 | // Make sure to check for async exceptions |
| 466 | if (UNLIKELY(self->ObserveAsyncException())) { |
| 467 | return false; |
| 468 | } |
| 469 | |
| Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 470 | StackHandleScope<2> hs(self); |
| Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 471 | bool is_var_args = inst->HasVarArgs(); |
| Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 472 | const uint16_t vRegH = is_var_args ? inst->VRegH_45cc() : inst->VRegH_4rcc(); |
| Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 473 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 474 | Handle<mirror::MethodType> callsite_type(hs.NewHandle( |
| Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 475 | class_linker->ResolveMethodType(self, dex::ProtoIndex(vRegH), shadow_frame.GetMethod()))); |
| Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 476 | // This implies we couldn't resolve one or more types in this VarHandle. |
| 477 | if (UNLIKELY(callsite_type == nullptr)) { |
| 478 | CHECK(self->IsExceptionPending()); |
| 479 | return false; |
| 480 | } |
| 481 | |
| Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 482 | const uint32_t vRegC = is_var_args ? inst->VRegC_45cc() : inst->VRegC_4rcc(); |
| 483 | ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(vRegC)); |
| Vladimir Marko | 179b7c6 | 2019-03-22 13:38:57 +0000 | [diff] [blame] | 484 | Handle<mirror::VarHandle> var_handle(hs.NewHandle(ObjPtr<mirror::VarHandle>::DownCast(receiver))); |
| Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 485 | if (is_var_args) { |
| 486 | uint32_t args[Instruction::kMaxVarArgRegs]; |
| 487 | inst->GetVarArgs(args, inst_data); |
| 488 | VarArgsInstructionOperands all_operands(args, inst->VRegA_45cc()); |
| 489 | NoReceiverInstructionOperands operands(&all_operands); |
| Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 490 | return VarHandleInvokeAccessor(self, |
| 491 | shadow_frame, |
| 492 | var_handle, |
| 493 | callsite_type, |
| 494 | access_mode, |
| 495 | &operands, |
| 496 | result); |
| Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 497 | } else { |
| 498 | RangeInstructionOperands all_operands(inst->VRegC_4rcc(), inst->VRegA_4rcc()); |
| 499 | NoReceiverInstructionOperands operands(&all_operands); |
| Orion Hodson | 537a4fe | 2018-05-15 13:57:58 +0100 | [diff] [blame] | 500 | return VarHandleInvokeAccessor(self, |
| 501 | shadow_frame, |
| 502 | var_handle, |
| 503 | callsite_type, |
| 504 | access_mode, |
| 505 | &operands, |
| 506 | result); |
| Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 507 | } |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 508 | } |
| 509 | |
| Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 510 | #define DO_VAR_HANDLE_ACCESSOR(_access_mode) \ |
| 511 | bool DoVarHandle ## _access_mode(Thread* self, \ |
| 512 | ShadowFrame& shadow_frame, \ |
| 513 | const Instruction* inst, \ |
| 514 | uint16_t inst_data, \ |
| 515 | JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { \ |
| 516 | const auto access_mode = mirror::VarHandle::AccessMode::k ## _access_mode; \ |
| 517 | return DoVarHandleInvokeCommon(self, shadow_frame, inst, inst_data, result, access_mode); \ |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 518 | } |
| 519 | |
| Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 520 | DO_VAR_HANDLE_ACCESSOR(CompareAndExchange) |
| 521 | DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeAcquire) |
| 522 | DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeRelease) |
| 523 | DO_VAR_HANDLE_ACCESSOR(CompareAndSet) |
| 524 | DO_VAR_HANDLE_ACCESSOR(Get) |
| 525 | DO_VAR_HANDLE_ACCESSOR(GetAcquire) |
| 526 | DO_VAR_HANDLE_ACCESSOR(GetAndAdd) |
| 527 | DO_VAR_HANDLE_ACCESSOR(GetAndAddAcquire) |
| 528 | DO_VAR_HANDLE_ACCESSOR(GetAndAddRelease) |
| 529 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAnd) |
| 530 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndAcquire) |
| 531 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndRelease) |
| 532 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOr) |
| 533 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrAcquire) |
| 534 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrRelease) |
| 535 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXor) |
| 536 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorAcquire) |
| 537 | DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorRelease) |
| 538 | DO_VAR_HANDLE_ACCESSOR(GetAndSet) |
| 539 | DO_VAR_HANDLE_ACCESSOR(GetAndSetAcquire) |
| 540 | DO_VAR_HANDLE_ACCESSOR(GetAndSetRelease) |
| 541 | DO_VAR_HANDLE_ACCESSOR(GetOpaque) |
| 542 | DO_VAR_HANDLE_ACCESSOR(GetVolatile) |
| 543 | DO_VAR_HANDLE_ACCESSOR(Set) |
| 544 | DO_VAR_HANDLE_ACCESSOR(SetOpaque) |
| 545 | DO_VAR_HANDLE_ACCESSOR(SetRelease) |
| 546 | DO_VAR_HANDLE_ACCESSOR(SetVolatile) |
| 547 | DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSet) |
| 548 | DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetAcquire) |
| 549 | DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetPlain) |
| 550 | DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetRelease) |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 551 | |
| Orion Hodson | 928033d | 2018-02-07 05:30:54 +0000 | [diff] [blame] | 552 | #undef DO_VAR_HANDLE_ACCESSOR |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 553 | |
| 554 | template<bool is_range> |
| 555 | bool DoInvokePolymorphic(Thread* self, |
| 556 | ShadowFrame& shadow_frame, |
| 557 | const Instruction* inst, |
| 558 | uint16_t inst_data, |
| 559 | JValue* result) { |
| 560 | const int invoke_method_idx = inst->VRegB(); |
| 561 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 562 | ArtMethod* invoke_method = |
| 563 | class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>( |
| Orion Hodson | 098faa8 | 2020-07-27 21:25:17 +0100 | [diff] [blame] | 564 | self, invoke_method_idx, shadow_frame.GetMethod(), kPolymorphic); |
| Orion Hodson | 43f0cdb | 2017-10-10 14:47:32 +0100 | [diff] [blame] | 565 | |
| 566 | // Ensure intrinsic identifiers are initialized. |
| 567 | DCHECK(invoke_method->IsIntrinsic()); |
| 568 | |
| 569 | // Dispatch based on intrinsic identifier associated with method. |
| 570 | switch (static_cast<art::Intrinsics>(invoke_method->GetIntrinsic())) { |
| 571 | #define CASE_SIGNATURE_POLYMORPHIC_INTRINSIC(Name, ...) \ |
| 572 | case Intrinsics::k##Name: \ |
| 573 | return Do ## Name(self, shadow_frame, inst, inst_data, result); |
| 574 | #include "intrinsics_list.h" |
| 575 | SIGNATURE_POLYMORPHIC_INTRINSICS_LIST(CASE_SIGNATURE_POLYMORPHIC_INTRINSIC) |
| 576 | #undef INTRINSICS_LIST |
| 577 | #undef SIGNATURE_POLYMORPHIC_INTRINSICS_LIST |
| 578 | #undef CASE_SIGNATURE_POLYMORPHIC_INTRINSIC |
| 579 | default: |
| 580 | LOG(FATAL) << "Unreachable: " << invoke_method->GetIntrinsic(); |
| 581 | UNREACHABLE(); |
| 582 | return false; |
| Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 586 | static JValue ConvertScalarBootstrapArgument(jvalue value) { |
| 587 | // value either contains a primitive scalar value if it corresponds |
| 588 | // to a primitive type, or it contains an integer value if it |
| 589 | // corresponds to an object instance reference id (e.g. a string id). |
| 590 | return JValue::FromPrimitive(value.j); |
| 591 | } |
| 592 | |
| 593 | static ObjPtr<mirror::Class> GetClassForBootstrapArgument(EncodedArrayValueIterator::ValueType type) |
| 594 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 595 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 596 | ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots(); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 597 | switch (type) { |
| 598 | case EncodedArrayValueIterator::ValueType::kBoolean: |
| 599 | case EncodedArrayValueIterator::ValueType::kByte: |
| 600 | case EncodedArrayValueIterator::ValueType::kChar: |
| 601 | case EncodedArrayValueIterator::ValueType::kShort: |
| 602 | // These types are disallowed by JVMS. Treat as integers. This |
| 603 | // will result in CCE's being raised if the BSM has one of these |
| 604 | // types. |
| 605 | case EncodedArrayValueIterator::ValueType::kInt: |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 606 | return GetClassRoot(ClassRoot::kPrimitiveInt, class_roots); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 607 | case EncodedArrayValueIterator::ValueType::kLong: |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 608 | return GetClassRoot(ClassRoot::kPrimitiveLong, class_roots); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 609 | case EncodedArrayValueIterator::ValueType::kFloat: |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 610 | return GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 611 | case EncodedArrayValueIterator::ValueType::kDouble: |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 612 | return GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 613 | case EncodedArrayValueIterator::ValueType::kMethodType: |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 614 | return GetClassRoot<mirror::MethodType>(class_roots); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 615 | case EncodedArrayValueIterator::ValueType::kMethodHandle: |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 616 | return GetClassRoot<mirror::MethodHandle>(class_roots); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 617 | case EncodedArrayValueIterator::ValueType::kString: |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 618 | return GetClassRoot<mirror::String>(); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 619 | case EncodedArrayValueIterator::ValueType::kType: |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 620 | return GetClassRoot<mirror::Class>(); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 621 | case EncodedArrayValueIterator::ValueType::kField: |
| 622 | case EncodedArrayValueIterator::ValueType::kMethod: |
| 623 | case EncodedArrayValueIterator::ValueType::kEnum: |
| 624 | case EncodedArrayValueIterator::ValueType::kArray: |
| 625 | case EncodedArrayValueIterator::ValueType::kAnnotation: |
| 626 | case EncodedArrayValueIterator::ValueType::kNull: |
| 627 | return nullptr; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | static bool GetArgumentForBootstrapMethod(Thread* self, |
| 632 | ArtMethod* referrer, |
| 633 | EncodedArrayValueIterator::ValueType type, |
| 634 | const JValue* encoded_value, |
| 635 | JValue* decoded_value) |
| 636 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 637 | // The encoded_value contains either a scalar value (IJDF) or a |
| 638 | // scalar DEX file index to a reference type to be materialized. |
| 639 | switch (type) { |
| 640 | case EncodedArrayValueIterator::ValueType::kInt: |
| 641 | case EncodedArrayValueIterator::ValueType::kFloat: |
| 642 | decoded_value->SetI(encoded_value->GetI()); |
| 643 | return true; |
| 644 | case EncodedArrayValueIterator::ValueType::kLong: |
| 645 | case EncodedArrayValueIterator::ValueType::kDouble: |
| 646 | decoded_value->SetJ(encoded_value->GetJ()); |
| 647 | return true; |
| 648 | case EncodedArrayValueIterator::ValueType::kMethodType: { |
| 649 | StackHandleScope<2> hs(self); |
| 650 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
| 651 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 652 | dex::ProtoIndex proto_idx(encoded_value->GetC()); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 653 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 654 | ObjPtr<mirror::MethodType> o = |
| 655 | cl->ResolveMethodType(self, proto_idx, dex_cache, class_loader); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 656 | if (UNLIKELY(o.IsNull())) { |
| 657 | DCHECK(self->IsExceptionPending()); |
| 658 | return false; |
| 659 | } |
| 660 | decoded_value->SetL(o); |
| 661 | return true; |
| 662 | } |
| 663 | case EncodedArrayValueIterator::ValueType::kMethodHandle: { |
| 664 | uint32_t index = static_cast<uint32_t>(encoded_value->GetI()); |
| 665 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| 666 | ObjPtr<mirror::MethodHandle> o = cl->ResolveMethodHandle(self, index, referrer); |
| 667 | if (UNLIKELY(o.IsNull())) { |
| 668 | DCHECK(self->IsExceptionPending()); |
| 669 | return false; |
| 670 | } |
| 671 | decoded_value->SetL(o); |
| 672 | return true; |
| 673 | } |
| 674 | case EncodedArrayValueIterator::ValueType::kString: { |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 675 | dex::StringIndex index(static_cast<uint32_t>(encoded_value->GetI())); |
| 676 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 677 | ObjPtr<mirror::String> o = cl->ResolveString(index, referrer); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 678 | if (UNLIKELY(o.IsNull())) { |
| 679 | DCHECK(self->IsExceptionPending()); |
| 680 | return false; |
| 681 | } |
| 682 | decoded_value->SetL(o); |
| 683 | return true; |
| 684 | } |
| 685 | case EncodedArrayValueIterator::ValueType::kType: { |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 686 | dex::TypeIndex index(static_cast<uint32_t>(encoded_value->GetI())); |
| 687 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 688 | ObjPtr<mirror::Class> o = cl->ResolveType(index, referrer); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 689 | if (UNLIKELY(o.IsNull())) { |
| 690 | DCHECK(self->IsExceptionPending()); |
| 691 | return false; |
| 692 | } |
| 693 | decoded_value->SetL(o); |
| 694 | return true; |
| 695 | } |
| 696 | case EncodedArrayValueIterator::ValueType::kBoolean: |
| 697 | case EncodedArrayValueIterator::ValueType::kByte: |
| 698 | case EncodedArrayValueIterator::ValueType::kChar: |
| 699 | case EncodedArrayValueIterator::ValueType::kShort: |
| 700 | case EncodedArrayValueIterator::ValueType::kField: |
| 701 | case EncodedArrayValueIterator::ValueType::kMethod: |
| 702 | case EncodedArrayValueIterator::ValueType::kEnum: |
| 703 | case EncodedArrayValueIterator::ValueType::kArray: |
| 704 | case EncodedArrayValueIterator::ValueType::kAnnotation: |
| 705 | case EncodedArrayValueIterator::ValueType::kNull: |
| 706 | // Unreachable - unsupported types that have been checked when |
| 707 | // determining the effect call site type based on the bootstrap |
| 708 | // argument types. |
| 709 | UNREACHABLE(); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | static bool PackArgumentForBootstrapMethod(Thread* self, |
| 714 | ArtMethod* referrer, |
| 715 | CallSiteArrayValueIterator* it, |
| 716 | ShadowFrameSetter* setter) |
| 717 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 718 | auto type = it->GetValueType(); |
| 719 | const JValue encoded_value = ConvertScalarBootstrapArgument(it->GetJavaValue()); |
| 720 | JValue decoded_value; |
| 721 | if (!GetArgumentForBootstrapMethod(self, referrer, type, &encoded_value, &decoded_value)) { |
| 722 | return false; |
| 723 | } |
| 724 | switch (it->GetValueType()) { |
| 725 | case EncodedArrayValueIterator::ValueType::kInt: |
| 726 | case EncodedArrayValueIterator::ValueType::kFloat: |
| 727 | setter->Set(static_cast<uint32_t>(decoded_value.GetI())); |
| 728 | return true; |
| 729 | case EncodedArrayValueIterator::ValueType::kLong: |
| 730 | case EncodedArrayValueIterator::ValueType::kDouble: |
| 731 | setter->SetLong(decoded_value.GetJ()); |
| 732 | return true; |
| 733 | case EncodedArrayValueIterator::ValueType::kMethodType: |
| 734 | case EncodedArrayValueIterator::ValueType::kMethodHandle: |
| 735 | case EncodedArrayValueIterator::ValueType::kString: |
| 736 | case EncodedArrayValueIterator::ValueType::kType: |
| 737 | setter->SetReference(decoded_value.GetL()); |
| 738 | return true; |
| 739 | case EncodedArrayValueIterator::ValueType::kBoolean: |
| 740 | case EncodedArrayValueIterator::ValueType::kByte: |
| 741 | case EncodedArrayValueIterator::ValueType::kChar: |
| 742 | case EncodedArrayValueIterator::ValueType::kShort: |
| 743 | case EncodedArrayValueIterator::ValueType::kField: |
| 744 | case EncodedArrayValueIterator::ValueType::kMethod: |
| 745 | case EncodedArrayValueIterator::ValueType::kEnum: |
| 746 | case EncodedArrayValueIterator::ValueType::kArray: |
| 747 | case EncodedArrayValueIterator::ValueType::kAnnotation: |
| 748 | case EncodedArrayValueIterator::ValueType::kNull: |
| 749 | // Unreachable - unsupported types that have been checked when |
| 750 | // determining the effect call site type based on the bootstrap |
| 751 | // argument types. |
| 752 | UNREACHABLE(); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | static bool PackCollectorArrayForBootstrapMethod(Thread* self, |
| 757 | ArtMethod* referrer, |
| 758 | ObjPtr<mirror::Class> array_type, |
| 759 | int32_t array_length, |
| 760 | CallSiteArrayValueIterator* it, |
| 761 | ShadowFrameSetter* setter) |
| 762 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 763 | StackHandleScope<1> hs(self); |
| 764 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 765 | JValue decoded_value; |
| 766 | |
| 767 | #define COLLECT_PRIMITIVE_ARRAY(Descriptor, Type) \ |
| 768 | Handle<mirror::Type ## Array> array = \ |
| 769 | hs.NewHandle(mirror::Type ## Array::Alloc(self, array_length)); \ |
| 770 | if (array.IsNull()) { \ |
| 771 | return false; \ |
| 772 | } \ |
| 773 | for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \ |
| 774 | auto type = it->GetValueType(); \ |
| 775 | DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \ |
| 776 | const JValue encoded_value = \ |
| 777 | ConvertScalarBootstrapArgument(it->GetJavaValue()); \ |
| 778 | GetArgumentForBootstrapMethod(self, \ |
| 779 | referrer, \ |
| 780 | type, \ |
| 781 | &encoded_value, \ |
| 782 | &decoded_value); \ |
| 783 | array->Set(i, decoded_value.Get ## Descriptor()); \ |
| 784 | } \ |
| 785 | setter->SetReference(array.Get()); \ |
| 786 | return true; |
| 787 | |
| 788 | #define COLLECT_REFERENCE_ARRAY(T, Type) \ |
| Andreas Gampe | 584771b | 2018-10-18 13:22:23 -0700 | [diff] [blame] | 789 | Handle<mirror::ObjectArray<T>> array = /* NOLINT */ \ |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 790 | hs.NewHandle(mirror::ObjectArray<T>::Alloc(self, \ |
| 791 | array_type, \ |
| 792 | array_length)); \ |
| 793 | if (array.IsNull()) { \ |
| 794 | return false; \ |
| 795 | } \ |
| 796 | for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \ |
| 797 | auto type = it->GetValueType(); \ |
| 798 | DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \ |
| 799 | const JValue encoded_value = \ |
| 800 | ConvertScalarBootstrapArgument(it->GetJavaValue()); \ |
| 801 | if (!GetArgumentForBootstrapMethod(self, \ |
| 802 | referrer, \ |
| 803 | type, \ |
| 804 | &encoded_value, \ |
| 805 | &decoded_value)) { \ |
| 806 | return false; \ |
| 807 | } \ |
| 808 | ObjPtr<mirror::Object> o = decoded_value.GetL(); \ |
| 809 | if (Runtime::Current()->IsActiveTransaction()) { \ |
| 810 | array->Set<true>(i, ObjPtr<T>::DownCast(o)); \ |
| 811 | } else { \ |
| 812 | array->Set<false>(i, ObjPtr<T>::DownCast(o)); \ |
| 813 | } \ |
| 814 | } \ |
| 815 | setter->SetReference(array.Get()); \ |
| 816 | return true; |
| 817 | |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 818 | ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots(); |
| 819 | ObjPtr<mirror::Class> component_type = array_type->GetComponentType(); |
| 820 | if (component_type == GetClassRoot(ClassRoot::kPrimitiveInt, class_roots)) { |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 821 | COLLECT_PRIMITIVE_ARRAY(I, Int); |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 822 | } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveLong, class_roots)) { |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 823 | COLLECT_PRIMITIVE_ARRAY(J, Long); |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 824 | } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots)) { |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 825 | COLLECT_PRIMITIVE_ARRAY(F, Float); |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 826 | } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots)) { |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 827 | COLLECT_PRIMITIVE_ARRAY(D, Double); |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 828 | } else if (component_type == GetClassRoot<mirror::MethodType>()) { |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 829 | COLLECT_REFERENCE_ARRAY(mirror::MethodType, MethodType); |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 830 | } else if (component_type == GetClassRoot<mirror::MethodHandle>()) { |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 831 | COLLECT_REFERENCE_ARRAY(mirror::MethodHandle, MethodHandle); |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 832 | } else if (component_type == GetClassRoot<mirror::String>(class_roots)) { |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 833 | COLLECT_REFERENCE_ARRAY(mirror::String, String); |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 834 | } else if (component_type == GetClassRoot<mirror::Class>()) { |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 835 | COLLECT_REFERENCE_ARRAY(mirror::Class, Type); |
| 836 | } else { |
| 837 | UNREACHABLE(); |
| 838 | } |
| 839 | #undef COLLECT_PRIMITIVE_ARRAY |
| 840 | #undef COLLECT_REFERENCE_ARRAY |
| 841 | } |
| 842 | |
| 843 | static ObjPtr<mirror::MethodType> BuildCallSiteForBootstrapMethod(Thread* self, |
| 844 | const DexFile* dex_file, |
| 845 | uint32_t call_site_idx) |
| 846 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 847 | const dex::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 848 | CallSiteArrayValueIterator it(*dex_file, csi); |
| 849 | DCHECK_GE(it.Size(), 1u); |
| 850 | |
| 851 | StackHandleScope<2> hs(self); |
| 852 | // Create array for parameter types. |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 853 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 854 | ObjPtr<mirror::Class> class_array_type = |
| 855 | GetClassRoot<mirror::ObjectArray<mirror::Class>>(class_linker); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 856 | Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle( |
| 857 | mirror::ObjectArray<mirror::Class>::Alloc(self, |
| 858 | class_array_type, |
| 859 | static_cast<int>(it.Size()))); |
| 860 | if (ptypes.IsNull()) { |
| 861 | DCHECK(self->IsExceptionPending()); |
| 862 | return nullptr; |
| 863 | } |
| 864 | |
| 865 | // Populate the first argument with an instance of j.l.i.MethodHandles.Lookup |
| 866 | // that the runtime will construct. |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 867 | ptypes->Set(0, GetClassRoot<mirror::MethodHandlesLookup>(class_linker)); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 868 | it.Next(); |
| 869 | |
| 870 | // The remaining parameter types are derived from the types of |
| 871 | // arguments present in the DEX file. |
| 872 | int index = 1; |
| 873 | while (it.HasNext()) { |
| 874 | ObjPtr<mirror::Class> ptype = GetClassForBootstrapArgument(it.GetValueType()); |
| 875 | if (ptype.IsNull()) { |
| 876 | ThrowClassCastException("Unsupported bootstrap argument type"); |
| 877 | return nullptr; |
| 878 | } |
| 879 | ptypes->Set(index, ptype); |
| 880 | index++; |
| 881 | it.Next(); |
| 882 | } |
| 883 | DCHECK_EQ(static_cast<size_t>(index), it.Size()); |
| 884 | |
| 885 | // By definition, the return type is always a j.l.i.CallSite. |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 886 | Handle<mirror::Class> rtype = hs.NewHandle(GetClassRoot<mirror::CallSite>()); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 887 | return mirror::MethodType::Create(self, rtype, ptypes); |
| 888 | } |
| 889 | |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 890 | static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self, |
| 891 | ShadowFrame& shadow_frame, |
| 892 | uint32_t call_site_idx) |
| 893 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 894 | StackHandleScope<5> hs(self); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 895 | // There are three mandatory arguments expected from the call site |
| 896 | // value array in the DEX file: the bootstrap method handle, the |
| 897 | // method name to pass to the bootstrap method, and the method type |
| 898 | // to pass to the bootstrap method. |
| 899 | static constexpr size_t kMandatoryArgumentsCount = 3; |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 900 | ArtMethod* referrer = shadow_frame.GetMethod(); |
| 901 | const DexFile* dex_file = referrer->GetDexFile(); |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 902 | const dex::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 903 | CallSiteArrayValueIterator it(*dex_file, csi); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 904 | if (it.Size() < kMandatoryArgumentsCount) { |
| 905 | ThrowBootstrapMethodError("Truncated bootstrap arguments (%zu < %zu)", |
| 906 | it.Size(), kMandatoryArgumentsCount); |
| 907 | return nullptr; |
| 908 | } |
| 909 | |
| 910 | if (it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodHandle) { |
| 911 | ThrowBootstrapMethodError("First bootstrap argument is not a method handle"); |
| 912 | return nullptr; |
| 913 | } |
| 914 | |
| 915 | uint32_t bsm_index = static_cast<uint32_t>(it.GetJavaValue().i); |
| 916 | it.Next(); |
| 917 | |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 918 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 919 | Handle<mirror::MethodHandle> bsm = |
| 920 | hs.NewHandle(class_linker->ResolveMethodHandle(self, bsm_index, referrer)); |
| 921 | if (bsm.IsNull()) { |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 922 | DCHECK(self->IsExceptionPending()); |
| 923 | return nullptr; |
| 924 | } |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 925 | |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 926 | if (bsm->GetHandleKind() != mirror::MethodHandle::Kind::kInvokeStatic) { |
| 927 | // JLS suggests also accepting constructors. This is currently |
| 928 | // hard as constructor invocations happen via transformers in ART |
| 929 | // today. The constructor would need to be a class derived from java.lang.invoke.CallSite. |
| 930 | ThrowBootstrapMethodError("Unsupported bootstrap method invocation kind"); |
| 931 | return nullptr; |
| 932 | } |
| 933 | |
| 934 | // Construct the local call site type information based on the 3 |
| 935 | // mandatory arguments provided by the runtime and the static arguments |
| 936 | // in the DEX file. We will use these arguments to build a shadow frame. |
| 937 | MutableHandle<mirror::MethodType> call_site_type = |
| 938 | hs.NewHandle(BuildCallSiteForBootstrapMethod(self, dex_file, call_site_idx)); |
| 939 | if (call_site_type.IsNull()) { |
| 940 | DCHECK(self->IsExceptionPending()); |
| 941 | return nullptr; |
| 942 | } |
| 943 | |
| 944 | // Check if this BSM is targeting a variable arity method. If so, |
| 945 | // we'll need to collect the trailing arguments into an array. |
| 946 | Handle<mirror::Array> collector_arguments; |
| 947 | int32_t collector_arguments_length; |
| 948 | if (bsm->GetTargetMethod()->IsVarargs()) { |
| 949 | int number_of_bsm_parameters = bsm->GetMethodType()->GetNumberOfPTypes(); |
| 950 | if (number_of_bsm_parameters == 0) { |
| 951 | ThrowBootstrapMethodError("Variable arity BSM does not have any arguments"); |
| 952 | return nullptr; |
| 953 | } |
| 954 | Handle<mirror::Class> collector_array_class = |
| 955 | hs.NewHandle(bsm->GetMethodType()->GetPTypes()->Get(number_of_bsm_parameters - 1)); |
| 956 | if (!collector_array_class->IsArrayClass()) { |
| 957 | ThrowBootstrapMethodError("Variable arity BSM does not have array as final argument"); |
| 958 | return nullptr; |
| 959 | } |
| 960 | // The call site may include no arguments to be collected. In this |
| 961 | // case the number of arguments must be at least the number of BSM |
| 962 | // parameters less the collector array. |
| 963 | if (call_site_type->GetNumberOfPTypes() < number_of_bsm_parameters - 1) { |
| 964 | ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get()); |
| 965 | return nullptr; |
| 966 | } |
| 967 | // Check all the arguments to be collected match the collector array component type. |
| 968 | for (int i = number_of_bsm_parameters - 1; i < call_site_type->GetNumberOfPTypes(); ++i) { |
| 969 | if (call_site_type->GetPTypes()->Get(i) != collector_array_class->GetComponentType()) { |
| 970 | ThrowClassCastException(collector_array_class->GetComponentType(), |
| 971 | call_site_type->GetPTypes()->Get(i)); |
| 972 | return nullptr; |
| 973 | } |
| 974 | } |
| 975 | // Update the call site method type so it now includes the collector array. |
| 976 | int32_t collector_arguments_start = number_of_bsm_parameters - 1; |
| 977 | collector_arguments_length = call_site_type->GetNumberOfPTypes() - number_of_bsm_parameters + 1; |
| 978 | call_site_type.Assign( |
| 979 | mirror::MethodType::CollectTrailingArguments(self, |
| 980 | call_site_type.Get(), |
| 981 | collector_array_class.Get(), |
| 982 | collector_arguments_start)); |
| 983 | if (call_site_type.IsNull()) { |
| 984 | DCHECK(self->IsExceptionPending()); |
| 985 | return nullptr; |
| 986 | } |
| 987 | } else { |
| 988 | collector_arguments_length = 0; |
| 989 | } |
| 990 | |
| 991 | if (call_site_type->GetNumberOfPTypes() != bsm->GetMethodType()->GetNumberOfPTypes()) { |
| 992 | ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get()); |
| 993 | return nullptr; |
| 994 | } |
| 995 | |
| 996 | // BSM invocation has a different set of exceptions that |
| 997 | // j.l.i.MethodHandle.invoke(). Scan arguments looking for CCE |
| 998 | // "opportunities". Unfortunately we cannot just leave this to the |
| 999 | // method handle invocation as this might generate a WMTE. |
| 1000 | for (int32_t i = 0; i < call_site_type->GetNumberOfPTypes(); ++i) { |
| 1001 | ObjPtr<mirror::Class> from = call_site_type->GetPTypes()->Get(i); |
| 1002 | ObjPtr<mirror::Class> to = bsm->GetMethodType()->GetPTypes()->Get(i); |
| 1003 | if (!IsParameterTypeConvertible(from, to)) { |
| 1004 | ThrowClassCastException(from, to); |
| 1005 | return nullptr; |
| 1006 | } |
| 1007 | } |
| 1008 | if (!IsReturnTypeConvertible(call_site_type->GetRType(), bsm->GetMethodType()->GetRType())) { |
| 1009 | ThrowClassCastException(bsm->GetMethodType()->GetRType(), call_site_type->GetRType()); |
| 1010 | return nullptr; |
| 1011 | } |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1012 | |
| 1013 | // Set-up a shadow frame for invoking the bootstrap method handle. |
| 1014 | ShadowFrameAllocaUniquePtr bootstrap_frame = |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1015 | CREATE_SHADOW_FRAME(call_site_type->NumberOfVRegs(), |
| 1016 | nullptr, |
| 1017 | referrer, |
| 1018 | shadow_frame.GetDexPC()); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1019 | ScopedStackedShadowFramePusher pusher( |
| 1020 | self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction); |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1021 | ShadowFrameSetter setter(bootstrap_frame.get(), 0u); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1022 | |
| 1023 | // The first parameter is a MethodHandles lookup instance. |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1024 | Handle<mirror::Class> lookup_class = |
| 1025 | hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass()); |
| 1026 | ObjPtr<mirror::MethodHandlesLookup> lookup = |
| 1027 | mirror::MethodHandlesLookup::Create(self, lookup_class); |
| 1028 | if (lookup.IsNull()) { |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1029 | DCHECK(self->IsExceptionPending()); |
| 1030 | return nullptr; |
| 1031 | } |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1032 | setter.SetReference(lookup); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1033 | |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1034 | // Pack the remaining arguments into the frame. |
| 1035 | int number_of_arguments = call_site_type->GetNumberOfPTypes(); |
| 1036 | int argument_index; |
| 1037 | for (argument_index = 1; argument_index < number_of_arguments; ++argument_index) { |
| 1038 | if (argument_index == number_of_arguments - 1 && |
| 1039 | call_site_type->GetPTypes()->Get(argument_index)->IsArrayClass()) { |
| 1040 | ObjPtr<mirror::Class> array_type = call_site_type->GetPTypes()->Get(argument_index); |
| 1041 | if (!PackCollectorArrayForBootstrapMethod(self, |
| 1042 | referrer, |
| 1043 | array_type, |
| 1044 | collector_arguments_length, |
| 1045 | &it, |
| 1046 | &setter)) { |
| 1047 | DCHECK(self->IsExceptionPending()); |
| 1048 | return nullptr; |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1049 | } |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1050 | } else if (!PackArgumentForBootstrapMethod(self, referrer, &it, &setter)) { |
| 1051 | DCHECK(self->IsExceptionPending()); |
| 1052 | return nullptr; |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1053 | } |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1054 | it.Next(); |
| 1055 | } |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1056 | DCHECK(!it.HasNext()); |
| 1057 | DCHECK(setter.Done()); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1058 | |
| 1059 | // Invoke the bootstrap method handle. |
| 1060 | JValue result; |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1061 | RangeInstructionOperands operands(0, bootstrap_frame->NumberOfVRegs()); |
| 1062 | bool invoke_success = MethodHandleInvoke(self, |
| 1063 | *bootstrap_frame, |
| 1064 | bsm, |
| 1065 | call_site_type, |
| 1066 | &operands, |
| 1067 | &result); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1068 | if (!invoke_success) { |
| 1069 | DCHECK(self->IsExceptionPending()); |
| 1070 | return nullptr; |
| 1071 | } |
| 1072 | |
| 1073 | Handle<mirror::Object> object(hs.NewHandle(result.GetL())); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1074 | if (UNLIKELY(object.IsNull())) { |
| Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 1075 | // This will typically be for LambdaMetafactory which is not supported. |
| Orion Hodson | 76e6adb | 2018-02-23 13:15:55 +0000 | [diff] [blame] | 1076 | ThrowClassCastException("Bootstrap method returned null"); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1077 | return nullptr; |
| 1078 | } |
| 1079 | |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1080 | // Check the result type is a subclass of j.l.i.CallSite. |
| Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 1081 | ObjPtr<mirror::Class> call_site_class = GetClassRoot<mirror::CallSite>(class_linker); |
| 1082 | if (UNLIKELY(!object->InstanceOf(call_site_class))) { |
| 1083 | ThrowClassCastException(object->GetClass(), call_site_class); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1084 | return nullptr; |
| 1085 | } |
| 1086 | |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1087 | // Check the call site target is not null as we're going to invoke it. |
| Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 1088 | ObjPtr<mirror::CallSite> call_site = ObjPtr<mirror::CallSite>::DownCast(result.GetL()); |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 1089 | ObjPtr<mirror::MethodHandle> target = call_site->GetTarget(); |
| 1090 | if (UNLIKELY(target == nullptr)) { |
| Orion Hodson | a5dca52 | 2018-02-27 12:42:11 +0000 | [diff] [blame] | 1091 | ThrowClassCastException("Bootstrap method returned a CallSite with a null target"); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1092 | return nullptr; |
| 1093 | } |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 1094 | return call_site; |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 1097 | namespace { |
| 1098 | |
| 1099 | ObjPtr<mirror::CallSite> DoResolveCallSite(Thread* self, |
| 1100 | ShadowFrame& shadow_frame, |
| 1101 | uint32_t call_site_idx) |
| 1102 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1103 | StackHandleScope<1> hs(self); |
| 1104 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache())); |
| 1105 | |
| 1106 | // Get the call site from the DexCache if present. |
| 1107 | ObjPtr<mirror::CallSite> call_site = dex_cache->GetResolvedCallSite(call_site_idx); |
| 1108 | if (LIKELY(call_site != nullptr)) { |
| 1109 | return call_site; |
| 1110 | } |
| 1111 | |
| 1112 | // Invoke the bootstrap method to get a candidate call site. |
| 1113 | call_site = InvokeBootstrapMethod(self, shadow_frame, call_site_idx); |
| 1114 | if (UNLIKELY(call_site == nullptr)) { |
| 1115 | if (!self->GetException()->IsError()) { |
| 1116 | // Use a BootstrapMethodError if the exception is not an instance of java.lang.Error. |
| 1117 | ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method", |
| 1118 | call_site_idx); |
| 1119 | } |
| 1120 | return nullptr; |
| 1121 | } |
| 1122 | |
| 1123 | // Attempt to place the candidate call site into the DexCache, return the winning call site. |
| 1124 | return dex_cache->SetResolvedCallSite(call_site_idx, call_site); |
| 1125 | } |
| 1126 | |
| 1127 | } // namespace |
| 1128 | |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1129 | bool DoInvokeCustom(Thread* self, |
| 1130 | ShadowFrame& shadow_frame, |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 1131 | uint32_t call_site_idx, |
| 1132 | const InstructionOperands* operands, |
| 1133 | JValue* result) { |
| Alex Light | 848574c | 2017-09-25 16:59:39 -0700 | [diff] [blame] | 1134 | // Make sure to check for async exceptions |
| 1135 | if (UNLIKELY(self->ObserveAsyncException())) { |
| 1136 | return false; |
| 1137 | } |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 1138 | |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1139 | // invoke-custom is not supported in transactions. In transactions |
| 1140 | // there is a limited set of types supported. invoke-custom allows |
| 1141 | // running arbitrary code and instantiating arbitrary types. |
| 1142 | CHECK(!Runtime::Current()->IsActiveTransaction()); |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 1143 | |
| 1144 | ObjPtr<mirror::CallSite> call_site = DoResolveCallSite(self, shadow_frame, call_site_idx); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1145 | if (call_site.IsNull()) { |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 1146 | DCHECK(self->IsExceptionPending()); |
| 1147 | return false; |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 1150 | StackHandleScope<2> hs(self); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1151 | Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget()); |
| 1152 | Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType()); |
| Orion Hodson | 4c8e12e | 2018-05-18 08:33:20 +0100 | [diff] [blame] | 1153 | DCHECK_EQ(operands->GetNumberOfOperands(), target_method_type->NumberOfVRegs()) |
| 1154 | << " call_site_idx" << call_site_idx; |
| 1155 | return MethodHandleInvokeExact(self, |
| 1156 | shadow_frame, |
| 1157 | target, |
| 1158 | target_method_type, |
| 1159 | operands, |
| 1160 | result); |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| Vladimir Marko | 6ec2a1b | 2018-05-22 15:33:48 +0100 | [diff] [blame] | 1163 | // Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame. |
| 1164 | static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame, |
| 1165 | size_t dest_reg, size_t src_reg) |
| 1166 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1167 | // Uint required, so that sign extension does not make this wrong on 64b systems |
| 1168 | uint32_t src_value = shadow_frame.GetVReg(src_reg); |
| 1169 | ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg); |
| 1170 | |
| 1171 | // If both register locations contains the same value, the register probably holds a reference. |
| 1172 | // Note: As an optimization, non-moving collectors leave a stale reference value |
| 1173 | // in the references array even after the original vreg was overwritten to a non-reference. |
| Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 1174 | if (src_value == reinterpret_cast32<uint32_t>(o.Ptr())) { |
| Vladimir Marko | 6ec2a1b | 2018-05-22 15:33:48 +0100 | [diff] [blame] | 1175 | new_shadow_frame->SetVRegReference(dest_reg, o); |
| 1176 | } else { |
| 1177 | new_shadow_frame->SetVReg(dest_reg, src_value); |
| 1178 | } |
| 1179 | } |
| 1180 | |
| Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1181 | template <bool is_range> |
| 1182 | inline void CopyRegisters(ShadowFrame& caller_frame, |
| 1183 | ShadowFrame* callee_frame, |
| 1184 | const uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| 1185 | const size_t first_src_reg, |
| 1186 | const size_t first_dest_reg, |
| 1187 | const size_t num_regs) { |
| 1188 | if (is_range) { |
| 1189 | const size_t dest_reg_bound = first_dest_reg + num_regs; |
| 1190 | for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound; |
| 1191 | ++dest_reg, ++src_reg) { |
| 1192 | AssignRegister(callee_frame, caller_frame, dest_reg, src_reg); |
| 1193 | } |
| 1194 | } else { |
| 1195 | DCHECK_LE(num_regs, arraysize(arg)); |
| 1196 | |
| 1197 | for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) { |
| 1198 | AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]); |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | |
| Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1203 | template <bool is_range, |
| Narayan Kamath | 370423d | 2016-10-03 16:51:22 +0100 | [diff] [blame] | 1204 | bool do_assignability_check> |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1205 | static inline bool DoCallCommon(ArtMethod* called_method, |
| 1206 | Thread* self, |
| 1207 | ShadowFrame& shadow_frame, |
| 1208 | JValue* result, |
| 1209 | uint16_t number_of_inputs, |
| Narayan Kamath | 370423d | 2016-10-03 16:51:22 +0100 | [diff] [blame] | 1210 | uint32_t (&arg)[Instruction::kMaxVarArgRegs], |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1211 | uint32_t vregC) { |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1212 | bool string_init = false; |
| 1213 | // Replace calls to String.<init> with equivalent StringFactory call. |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1214 | if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass() |
| 1215 | && called_method->IsConstructor())) { |
| Nicolas Geoffray | da079bb | 2016-09-26 17:56:07 +0100 | [diff] [blame] | 1216 | called_method = WellKnownClasses::StringInitToStringFactory(called_method); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1217 | string_init = true; |
| 1218 | } |
| 1219 | |
| Alex Light | daf58c8 | 2016-03-16 23:00:49 +0000 | [diff] [blame] | 1220 | // Compute method information. |
| David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 1221 | CodeItemDataAccessor accessor(called_method->DexInstructionData()); |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1222 | // Number of registers for the callee's call frame. |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1223 | uint16_t num_regs; |
| Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1224 | // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to |
| 1225 | // PerformCall. A deoptimization could occur at any time, and we shouldn't change which |
| 1226 | // entrypoint to use once we start building the shadow frame. |
| Mathieu Chartier | 448bbcf | 2017-07-06 12:05:13 -0700 | [diff] [blame] | 1227 | |
| 1228 | // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are |
| 1229 | // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image |
| 1230 | // pointer size here and this may case an overflow if it is called from the compiler. b/62402160 |
| 1231 | const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() || |
| 1232 | ClassLinker::ShouldUseInterpreterEntrypoint( |
| 1233 | called_method, |
| 1234 | called_method->GetEntryPointFromQuickCompiledCode()); |
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1235 | if (LIKELY(accessor.HasCodeItem())) { |
| Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1236 | // When transitioning to compiled code, space only needs to be reserved for the input registers. |
| 1237 | // The rest of the frame gets discarded. This also prevents accessing the called method's code |
| 1238 | // item, saving memory by keeping code items of compiled code untouched. |
| Mathieu Chartier | 448bbcf | 2017-07-06 12:05:13 -0700 | [diff] [blame] | 1239 | if (!use_interpreter_entrypoint) { |
| 1240 | DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint"; |
| Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1241 | num_regs = number_of_inputs; |
| 1242 | } else { |
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1243 | num_regs = accessor.RegistersSize(); |
| 1244 | DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, accessor.InsSize()); |
| Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1245 | } |
| Nicolas Geoffray | 0182229 | 2017-03-09 09:03:19 +0000 | [diff] [blame] | 1246 | } else { |
| 1247 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
| 1248 | num_regs = number_of_inputs; |
| Jeff Hao | df79ddb | 2017-02-27 14:47:06 -0800 | [diff] [blame] | 1249 | } |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1250 | |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1251 | // Hack for String init: |
| 1252 | // |
| 1253 | // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into: |
| 1254 | // invoke-x StringFactory(a, b, c, ...) |
| 1255 | // by effectively dropping the first virtual register from the invoke. |
| 1256 | // |
| 1257 | // (at this point the ArtMethod has already been replaced, |
| 1258 | // so we just need to fix-up the arguments) |
| David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 1259 | // |
| 1260 | // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased |
| 1261 | // to handle the compiler optimization of replacing `this` with null without |
| 1262 | // throwing NullPointerException. |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1263 | uint32_t string_init_vreg_this = is_range ? vregC : arg[0]; |
| Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1264 | if (UNLIKELY(string_init)) { |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1265 | DCHECK_GT(num_regs, 0u); // As the method is an instance method, there should be at least 1. |
| Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1266 | |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1267 | // The new StringFactory call is static and has one fewer argument. |
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1268 | if (!accessor.HasCodeItem()) { |
| Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1269 | DCHECK(called_method->IsNative() || called_method->IsProxyMethod()); |
| 1270 | num_regs--; |
| 1271 | } // else ... don't need to change num_regs since it comes up from the string_init's code item |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1272 | number_of_inputs--; |
| 1273 | |
| 1274 | // Rewrite the var-args, dropping the 0th argument ("this") |
| Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1275 | for (uint32_t i = 1; i < arraysize(arg); ++i) { |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1276 | arg[i - 1] = arg[i]; |
| 1277 | } |
| Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1278 | arg[arraysize(arg) - 1] = 0; |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1279 | |
| 1280 | // Rewrite the non-var-arg case |
| 1281 | vregC++; // Skips the 0th vreg in the range ("this"). |
| 1282 | } |
| 1283 | |
| 1284 | // Parameter registers go at the end of the shadow frame. |
| 1285 | DCHECK_GE(num_regs, number_of_inputs); |
| 1286 | size_t first_dest_reg = num_regs - number_of_inputs; |
| 1287 | DCHECK_NE(first_dest_reg, (size_t)-1); |
| 1288 | |
| Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1289 | // Allocate shadow frame on the stack. |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1290 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon"); |
| Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 1291 | ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr = |
| Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame] | 1292 | CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0); |
| Andreas Gampe | b302592 | 2015-09-01 14:45:00 -0700 | [diff] [blame] | 1293 | ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get(); |
| Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1294 | |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1295 | // Initialize new shadow frame by copying the registers from the callee shadow frame. |
| Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1296 | if (do_assignability_check) { |
| Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1297 | // Slow path. |
| 1298 | // We might need to do class loading, which incurs a thread state change to kNative. So |
| 1299 | // register the shadow frame as under construction and allow suspension again. |
| Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 1300 | ScopedStackedShadowFramePusher pusher( |
| Sebastien Hertz | f795869 | 2015-06-09 14:09:14 +0200 | [diff] [blame] | 1301 | self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction); |
| Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1302 | self->EndAssertNoThreadSuspension(old_cause); |
| 1303 | |
| Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1304 | // ArtMethod here is needed to check type information of the call site against the callee. |
| 1305 | // Type information is retrieved from a DexFile/DexCache for that respective declared method. |
| 1306 | // |
| 1307 | // As a special case for proxy methods, which are not dex-backed, |
| 1308 | // we have to retrieve type information from the proxy's method |
| 1309 | // interface method instead (which is dex backed since proxies are never interfaces). |
| Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1310 | ArtMethod* method = |
| 1311 | new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize); |
| Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1312 | |
| Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1313 | // We need to do runtime check on reference assignment. We need to load the shorty |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1314 | // to get the exact type of each reference argument. |
| Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1315 | const dex::TypeList* params = method->GetParameterTypeList(); |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1316 | uint32_t shorty_len = 0; |
| Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1317 | const char* shorty = method->GetShorty(&shorty_len); |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1318 | |
| Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1319 | // Handle receiver apart since it's not part of the shorty. |
| 1320 | size_t dest_reg = first_dest_reg; |
| 1321 | size_t arg_offset = 0; |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1322 | |
| Igor Murashkin | 9f95ba7 | 2016-02-01 14:21:25 -0800 | [diff] [blame] | 1323 | if (!method->IsStatic()) { |
| Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 1324 | size_t receiver_reg = is_range ? vregC : arg[0]; |
| Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1325 | new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg)); |
| 1326 | ++dest_reg; |
| 1327 | ++arg_offset; |
| Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1328 | DCHECK(!string_init); // All StringFactory methods are static. |
| Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1329 | } |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1330 | |
| 1331 | // Copy the caller's invoke-* arguments into the callee's parameter registers. |
| Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1332 | for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) { |
| Igor Murashkin | a06b49b | 2015-06-25 15:18:12 -0700 | [diff] [blame] | 1333 | // Skip the 0th 'shorty' type since it represents the return type. |
| 1334 | DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'"; |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1335 | const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset]; |
| 1336 | switch (shorty[shorty_pos + 1]) { |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1337 | // Handle Object references. 1 virtual register slot. |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1338 | case 'L': { |
| Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1339 | ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1340 | if (do_assignability_check && o != nullptr) { |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1341 | const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_; |
| Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 1342 | ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx); |
| Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1343 | if (arg_type == nullptr) { |
| Mathieu Chartier | e22305b | 2016-10-26 21:04:58 -0700 | [diff] [blame] | 1344 | StackHandleScope<1> hs(self); |
| 1345 | // Preserve o since it is used below and GetClassFromTypeIndex may cause thread |
| 1346 | // suspension. |
| 1347 | HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o); |
| Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 1348 | arg_type = method->ResolveClassFromTypeIndex(type_idx); |
| Mathieu Chartier | e22305b | 2016-10-26 21:04:58 -0700 | [diff] [blame] | 1349 | if (arg_type == nullptr) { |
| 1350 | CHECK(self->IsExceptionPending()); |
| 1351 | return false; |
| 1352 | } |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1353 | } |
| 1354 | if (!o->VerifierInstanceOf(arg_type)) { |
| 1355 | // This should never happen. |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1356 | std::string temp1, temp2; |
| Orion Hodson | fef0664 | 2016-11-25 16:07:11 +0000 | [diff] [blame] | 1357 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1358 | "Invoking %s with bad arg %d, type '%s' not instance of '%s'", |
| Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 1359 | new_shadow_frame->GetMethod()->GetName(), shorty_pos, |
| Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1360 | o->GetClass()->GetDescriptor(&temp1), |
| 1361 | arg_type->GetDescriptor(&temp2)); |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1362 | return false; |
| 1363 | } |
| Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1364 | } |
| Vladimir Marko | 6ec2a1b | 2018-05-22 15:33:48 +0100 | [diff] [blame] | 1365 | new_shadow_frame->SetVRegReference(dest_reg, o); |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1366 | break; |
| Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 1367 | } |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1368 | // Handle doubles and longs. 2 consecutive virtual register slots. |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1369 | case 'J': case 'D': { |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1370 | uint64_t wide_value = |
| 1371 | (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) | |
| 1372 | static_cast<uint32_t>(shadow_frame.GetVReg(src_reg)); |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1373 | new_shadow_frame->SetVRegLong(dest_reg, wide_value); |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1374 | // Skip the next virtual register slot since we already used it. |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1375 | ++dest_reg; |
| 1376 | ++arg_offset; |
| 1377 | break; |
| 1378 | } |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1379 | // Handle all other primitives that are always 1 virtual register slot. |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1380 | default: |
| 1381 | new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg)); |
| 1382 | break; |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1383 | } |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1384 | } |
| 1385 | } else { |
| Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 1386 | if (is_range) { |
| Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1387 | DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs); |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1388 | } |
| Narayan Kamath | c3b7f1a | 2016-10-19 11:05:04 +0100 | [diff] [blame] | 1389 | |
| 1390 | CopyRegisters<is_range>(shadow_frame, |
| 1391 | new_shadow_frame, |
| 1392 | arg, |
| 1393 | vregC, |
| 1394 | first_dest_reg, |
| 1395 | number_of_inputs); |
| Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 1396 | self->EndAssertNoThreadSuspension(old_cause); |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1397 | } |
| 1398 | |
| Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1399 | PerformCall(self, |
| Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 1400 | accessor, |
| Jeff Hao | 5ea8413 | 2017-05-05 16:59:29 -0700 | [diff] [blame] | 1401 | shadow_frame.GetMethod(), |
| 1402 | first_dest_reg, |
| 1403 | new_shadow_frame, |
| 1404 | result, |
| 1405 | use_interpreter_entrypoint); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1406 | |
| 1407 | if (string_init && !self->IsExceptionPending()) { |
| Mingyao Yang | ffedec5 | 2016-05-19 10:48:40 -0700 | [diff] [blame] | 1408 | SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result); |
| Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 1409 | } |
| 1410 | |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1411 | return !self->IsExceptionPending(); |
| 1412 | } |
| 1413 | |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1414 | template<bool is_range, bool do_assignability_check> |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1415 | bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
| 1416 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 1417 | // Argument word count. |
| Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 1418 | const uint16_t number_of_inputs = |
| 1419 | (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data); |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1420 | |
| 1421 | // TODO: find a cleaner way to separate non-range and range information without duplicating |
| 1422 | // code. |
| Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1423 | uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX. |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1424 | uint32_t vregC = 0; |
| 1425 | if (is_range) { |
| 1426 | vregC = inst->VRegC_3rc(); |
| 1427 | } else { |
| 1428 | vregC = inst->VRegC_35c(); |
| 1429 | inst->GetVarArgs(arg, inst_data); |
| 1430 | } |
| 1431 | |
| 1432 | return DoCallCommon<is_range, do_assignability_check>( |
| 1433 | called_method, self, shadow_frame, |
| 1434 | result, number_of_inputs, arg, vregC); |
| 1435 | } |
| 1436 | |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1437 | template <bool is_range, bool do_access_check, bool transaction_active> |
| Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1438 | bool DoFilledNewArray(const Instruction* inst, |
| 1439 | const ShadowFrame& shadow_frame, |
| 1440 | Thread* self, |
| 1441 | JValue* result) { |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1442 | DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY || |
| 1443 | inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE); |
| 1444 | const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 1445 | if (!is_range) { |
| 1446 | // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments. |
| 1447 | CHECK_LE(length, 5); |
| 1448 | } |
| 1449 | if (UNLIKELY(length < 0)) { |
| 1450 | ThrowNegativeArraySizeException(length); |
| 1451 | return false; |
| 1452 | } |
| 1453 | uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1454 | ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx), |
| Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1455 | shadow_frame.GetMethod(), |
| 1456 | self, |
| 1457 | false, |
| 1458 | do_access_check); |
| Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1459 | if (UNLIKELY(array_class == nullptr)) { |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1460 | DCHECK(self->IsExceptionPending()); |
| 1461 | return false; |
| 1462 | } |
| Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1463 | CHECK(array_class->IsArrayClass()); |
| Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1464 | ObjPtr<mirror::Class> component_class = array_class->GetComponentType(); |
| Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1465 | const bool is_primitive_int_component = component_class->IsPrimitiveInt(); |
| 1466 | if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) { |
| 1467 | if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) { |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1468 | ThrowRuntimeException("Bad filled array request for type %s", |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1469 | component_class->PrettyDescriptor().c_str()); |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1470 | } else { |
| Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 1471 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 1472 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
| David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1473 | component_class->PrettyDescriptor().c_str()); |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1474 | } |
| 1475 | return false; |
| 1476 | } |
| Vladimir Marko | 9b81ac3 | 2019-05-16 16:47:08 +0100 | [diff] [blame] | 1477 | ObjPtr<mirror::Object> new_array = mirror::Array::Alloc( |
| Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1478 | self, |
| 1479 | array_class, |
| 1480 | length, |
| 1481 | array_class->GetComponentSizeShift(), |
| 1482 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1483 | if (UNLIKELY(new_array == nullptr)) { |
| 1484 | self->AssertPendingOOMException(); |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1485 | return false; |
| 1486 | } |
| Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1487 | uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array. |
| 1488 | uint32_t vregC = 0; // only used in filled-new-array-range. |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1489 | if (is_range) { |
| Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1490 | vregC = inst->VRegC_3rc(); |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1491 | } else { |
| Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 1492 | inst->GetVarArgs(arg); |
| Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1493 | } |
| Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1494 | for (int32_t i = 0; i < length; ++i) { |
| 1495 | size_t src_reg = is_range ? vregC + i : arg[i]; |
| 1496 | if (is_primitive_int_component) { |
| Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1497 | new_array->AsIntArray()->SetWithoutChecks<transaction_active>( |
| 1498 | i, shadow_frame.GetVReg(src_reg)); |
| Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 1499 | } else { |
| Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1500 | new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>( |
| Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1501 | i, shadow_frame.GetVRegReference(src_reg)); |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | |
| Mathieu Chartier | 52ea33b | 2015-06-18 16:48:52 -0700 | [diff] [blame] | 1505 | result->SetL(new_array); |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1506 | return true; |
| 1507 | } |
| 1508 | |
| Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1509 | // TODO: Use ObjPtr here. |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1510 | template<typename T> |
| Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 1511 | static void RecordArrayElementsInTransactionImpl(ObjPtr<mirror::PrimitiveArray<T>> array, |
| Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1512 | int32_t count) |
| 1513 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1514 | Runtime* runtime = Runtime::Current(); |
| 1515 | for (int32_t i = 0; i < count; ++i) { |
| Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 1516 | runtime->RecordWriteArray(array.Ptr(), i, array->GetWithoutChecks(i)); |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | |
| Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 1520 | void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count) |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1521 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1522 | DCHECK(Runtime::Current()->IsActiveTransaction()); |
| 1523 | DCHECK(array != nullptr); |
| 1524 | DCHECK_LE(count, array->GetLength()); |
| 1525 | Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType(); |
| 1526 | switch (primitive_component_type) { |
| 1527 | case Primitive::kPrimBoolean: |
| 1528 | RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count); |
| 1529 | break; |
| 1530 | case Primitive::kPrimByte: |
| 1531 | RecordArrayElementsInTransactionImpl(array->AsByteArray(), count); |
| 1532 | break; |
| 1533 | case Primitive::kPrimChar: |
| 1534 | RecordArrayElementsInTransactionImpl(array->AsCharArray(), count); |
| 1535 | break; |
| 1536 | case Primitive::kPrimShort: |
| 1537 | RecordArrayElementsInTransactionImpl(array->AsShortArray(), count); |
| 1538 | break; |
| 1539 | case Primitive::kPrimInt: |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1540 | RecordArrayElementsInTransactionImpl(array->AsIntArray(), count); |
| 1541 | break; |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1542 | case Primitive::kPrimFloat: |
| 1543 | RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count); |
| 1544 | break; |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1545 | case Primitive::kPrimLong: |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1546 | RecordArrayElementsInTransactionImpl(array->AsLongArray(), count); |
| 1547 | break; |
| Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1548 | case Primitive::kPrimDouble: |
| 1549 | RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count); |
| 1550 | break; |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1551 | default: |
| 1552 | LOG(FATAL) << "Unsupported primitive type " << primitive_component_type |
| 1553 | << " in fill-array-data"; |
| Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 1554 | UNREACHABLE(); |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1555 | } |
| 1556 | } |
| 1557 | |
| Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1558 | // Explicit DoCall template function declarations. |
| Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 1559 | #define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1560 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
| Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 1561 | bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 1562 | ShadowFrame& shadow_frame, \ |
| Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 1563 | const Instruction* inst, uint16_t inst_data, \ |
| 1564 | JValue* result) |
| Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 1565 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); |
| 1566 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); |
| 1567 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); |
| 1568 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); |
| 1569 | #undef EXPLICIT_DO_CALL_TEMPLATE_DECL |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1570 | |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1571 | // Explicit DoInvokePolymorphic template function declarations. |
| 1572 | #define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \ |
| 1573 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
| 1574 | bool DoInvokePolymorphic<_is_range>( \ |
| 1575 | Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 1576 | uint16_t inst_data, JValue* result) |
| 1577 | EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false); |
| 1578 | EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true); |
| Narayan Kamath | 9823e78 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1579 | #undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL |
| 1580 | |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1581 | // Explicit DoFilledNewArray template function declarations. |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1582 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \ |
| Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1583 | template REQUIRES_SHARED(Locks::mutator_lock_) \ |
| Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1584 | bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \ |
| 1585 | const ShadowFrame& shadow_frame, \ |
| 1586 | Thread* self, JValue* result) |
| 1587 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \ |
| 1588 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \ |
| 1589 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \ |
| 1590 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \ |
| 1591 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active) |
| 1592 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false); |
| 1593 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true); |
| 1594 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL |
| Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1595 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL |
| 1596 | |
| 1597 | } // namespace interpreter |
| 1598 | } // namespace art |