blob: 4d964f1f3b2bcc7c59b50e6bf195d263ea1700bb [file] [log] [blame]
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001/*
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 Rogers22d5e732014-07-15 22:23:51 -070018
Andreas Gampef0e128a2015-02-27 20:08:34 -080019#include <cmath>
20
Vladimir Marko78baed52018-10-11 10:44:58 +010021#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Vladimir Markoc7aa87e2018-05-24 15:19:52 +010023#include "class_root.h"
Daniel Mihalyieb076692014-08-22 17:33:31 +020024#include "debugger.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/dex_file_types.h"
Nicolas Geoffray7bf2b4f2015-07-08 10:11:59 +000026#include "entrypoints/runtime_asm_entrypoints.h"
Alex Lightb7c640d2019-03-20 15:52:13 -070027#include "handle.h"
Orion Hodson43f0cdb2017-10-10 14:47:32 +010028#include "intrinsics_enum.h"
Tamas Berghammerdd5e5e92016-02-12 16:29:00 +000029#include "jit/jit.h"
Andreas Gampec5b75642018-05-16 15:12:11 -070030#include "jvalue-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010031#include "method_handles-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "method_handles.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070033#include "mirror/array-alloc-inl.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010034#include "mirror/array-inl.h"
Vladimir Marko621c8802019-03-27 16:18:18 +000035#include "mirror/call_site-inl.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010036#include "mirror/class.h"
Narayan Kamath000e1882016-10-24 17:14:25 +010037#include "mirror/emulated_stack_frame.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080038#include "mirror/method_handle_impl-inl.h"
Vladimir Marko5aead702019-03-27 11:00:36 +000039#include "mirror/method_type-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070040#include "mirror/object_array-alloc-inl.h"
41#include "mirror/object_array-inl.h"
Orion Hodson928033d2018-02-07 05:30:54 +000042#include "mirror/var_handle.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010043#include "reflection-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070044#include "reflection.h"
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010045#include "shadow_frame-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070046#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070047#include "thread-inl.h"
Chang Xingbd208d82017-07-12 14:53:17 -070048#include "transaction.h"
Orion Hodson537a4fe2018-05-15 13:57:58 +010049#include "var_handles.h"
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010050#include "well_known_classes.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020051
52namespace art {
53namespace interpreter {
54
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000055void ThrowNullPointerExceptionFromInterpreter() {
56 ThrowNullPointerExceptionFromDexPC();
Ian Rogers54874942014-06-10 16:31:03 -070057}
58
David Srbecky960327b2018-10-25 10:11:59 +000059bool 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 Srbecky9581e612018-10-30 14:29:43 +000070bool 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 Markobaa81b52019-08-02 10:14:04 +010085 if (method->IsStatic() && !method->GetDeclaringClass()->IsVisiblyInitialized()) {
David Srbecky9581e612018-10-30 14:29:43 +000086 return false;
87 }
88 ProfilingInfo* profiling_info = method->GetProfilingInfo(kRuntimePointerSize);
89 if ((profiling_info != nullptr) && (profiling_info->GetSavedEntryPoint() != nullptr)) {
90 return false;
91 }
92 return true;
93}
94
Alex Lightb7c640d2019-03-20 15:52:13 -070095template <typename T>
96bool SendMethodExitEvents(Thread* self,
97 const instrumentation::Instrumentation* instrumentation,
98 ShadowFrame& frame,
99 ObjPtr<mirror::Object> thiz,
100 ArtMethod* method,
101 uint32_t dex_pc,
102 T& result) {
103 bool had_event = false;
104 // We can get additional ForcePopFrame requests during handling of these events. We should
105 // respect these and send additional instrumentation events.
106 StackHandleScope<1> hs(self);
107 Handle<mirror::Object> h_thiz(hs.NewHandle(thiz));
108 do {
109 frame.SetForcePopFrame(false);
110 if (UNLIKELY(instrumentation->HasMethodExitListeners() && !frame.GetSkipMethodExitEvents())) {
111 had_event = true;
112 instrumentation->MethodExitEvent(
113 self, h_thiz.Get(), method, dex_pc, instrumentation::OptionalFrame{ frame }, result);
114 }
115 // We don't send method-exit if it's a pop-frame. We still send frame_popped though.
116 if (UNLIKELY(frame.NeedsNotifyPop() && instrumentation->HasWatchedFramePopListeners())) {
117 had_event = true;
118 instrumentation->WatchedFramePopped(self, frame);
119 }
120 } while (UNLIKELY(frame.GetForcePopFrame()));
121 if (UNLIKELY(had_event)) {
122 return !self->IsExceptionPending();
123 } else {
124 return true;
125 }
126}
127
128template
129bool SendMethodExitEvents(Thread* self,
130 const instrumentation::Instrumentation* instrumentation,
131 ShadowFrame& frame,
132 ObjPtr<mirror::Object> thiz,
133 ArtMethod* method,
134 uint32_t dex_pc,
135 MutableHandle<mirror::Object>& result);
136
137template
138bool SendMethodExitEvents(Thread* self,
139 const instrumentation::Instrumentation* instrumentation,
140 ShadowFrame& frame,
141 ObjPtr<mirror::Object> thiz,
142 ArtMethod* method,
143 uint32_t dex_pc,
144 JValue& result);
145
Alex Light9fb1ab12017-09-05 09:32:49 -0700146// We execute any instrumentation events that are triggered by this exception and change the
147// shadow_frame's dex_pc to that of the exception handler if there is one in the current method.
148// Return true if we should continue executing in the current method and false if we need to go up
149// the stack to find an exception handler.
Sebastien Hertz520633b2015-09-08 17:03:36 +0200150// We accept a null Instrumentation* meaning we must not report anything to the instrumentation.
Alex Light9fb1ab12017-09-05 09:32:49 -0700151// TODO We should have a better way to skip instrumentation reporting or possibly rethink that
152// behavior.
153bool MoveToExceptionHandler(Thread* self,
154 ShadowFrame& shadow_frame,
155 const instrumentation::Instrumentation* instrumentation) {
Ian Rogers54874942014-06-10 16:31:03 -0700156 self->VerifyStack();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700157 StackHandleScope<2> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000158 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Alex Light9fb1ab12017-09-05 09:32:49 -0700159 if (instrumentation != nullptr &&
160 instrumentation->HasExceptionThrownListeners() &&
161 self->IsExceptionThrownByCurrentMethod(exception.Get())) {
162 // See b/65049545 for why we don't need to check to see if the exception has changed.
Alex Light6e1607e2017-08-23 10:06:18 -0700163 instrumentation->ExceptionThrownEvent(self, exception.Get());
Alex Light0aa7a5a2018-10-10 15:58:14 +0000164 if (shadow_frame.GetForcePopFrame()) {
165 // We will check in the caller for GetForcePopFrame again. We need to bail out early to
166 // prevent an ExceptionHandledEvent from also being sent before popping.
167 return true;
168 }
Sebastien Hertz9f102032014-05-23 08:59:42 +0200169 }
Ian Rogers54874942014-06-10 16:31:03 -0700170 bool clear_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700171 uint32_t found_dex_pc = shadow_frame.GetMethod()->FindCatchBlock(
Alex Light9fb1ab12017-09-05 09:32:49 -0700172 hs.NewHandle(exception->GetClass()), shadow_frame.GetDexPC(), &clear_exception);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700173 if (found_dex_pc == dex::kDexNoIndex) {
Alex Light9fb1ab12017-09-05 09:32:49 -0700174 if (instrumentation != nullptr) {
175 if (shadow_frame.NeedsNotifyPop()) {
176 instrumentation->WatchedFramePopped(self, shadow_frame);
Alex Lightb7c640d2019-03-20 15:52:13 -0700177 if (shadow_frame.GetForcePopFrame()) {
178 // We will check in the caller for GetForcePopFrame again. We need to bail out early to
179 // prevent an ExceptionHandledEvent from also being sent before popping and to ensure we
180 // handle other types of non-standard-exits.
181 return true;
182 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700183 }
184 // Exception is not caught by the current method. We will unwind to the
185 // caller. Notify any instrumentation listener.
186 instrumentation->MethodUnwindEvent(self,
187 shadow_frame.GetThisObject(),
188 shadow_frame.GetMethod(),
189 shadow_frame.GetDexPC());
Alex Lighte814f9d2017-07-31 16:14:39 -0700190 }
Alex Lightb7c640d2019-03-20 15:52:13 -0700191 return shadow_frame.GetForcePopFrame();
Ian Rogers54874942014-06-10 16:31:03 -0700192 } else {
Alex Light9fb1ab12017-09-05 09:32:49 -0700193 shadow_frame.SetDexPC(found_dex_pc);
194 if (instrumentation != nullptr && instrumentation->HasExceptionHandledListeners()) {
195 self->ClearException();
196 instrumentation->ExceptionHandledEvent(self, exception.Get());
197 if (UNLIKELY(self->IsExceptionPending())) {
198 // Exception handled event threw an exception. Try to find the handler for this one.
199 return MoveToExceptionHandler(self, shadow_frame, instrumentation);
200 } else if (!clear_exception) {
201 self->SetException(exception.Get());
202 }
203 } else if (clear_exception) {
Ian Rogers54874942014-06-10 16:31:03 -0700204 self->ClearException();
205 }
Alex Light9fb1ab12017-09-05 09:32:49 -0700206 return true;
Ian Rogers54874942014-06-10 16:31:03 -0700207 }
Ian Rogers54874942014-06-10 16:31:03 -0700208}
209
Ian Rogerse94652f2014-12-02 11:13:19 -0800210void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) {
211 LOG(FATAL) << "Unexpected instruction: "
212 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile());
213 UNREACHABLE();
Ian Rogers54874942014-06-10 16:31:03 -0700214}
215
Sebastien Hertz45b15972015-04-03 16:07:05 +0200216void AbortTransactionF(Thread* self, const char* fmt, ...) {
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700217 va_list args;
218 va_start(args, fmt);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200219 AbortTransactionV(self, fmt, args);
220 va_end(args);
221}
222
223void AbortTransactionV(Thread* self, const char* fmt, va_list args) {
224 CHECK(Runtime::Current()->IsActiveTransaction());
225 // Constructs abort message.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100226 std::string abort_msg;
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800227 android::base::StringAppendV(&abort_msg, fmt, args);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100228 // Throws an exception so we can abort the transaction and rollback every change.
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200229 Runtime::Current()->AbortTransactionAndThrowAbortError(self, abort_msg);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700230}
231
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100232// START DECLARATIONS :
233//
234// These additional declarations are required because clang complains
235// about ALWAYS_INLINE (-Werror, -Wgcc-compat) in definitions.
236//
237
Narayan Kamath9823e782016-08-03 12:46:58 +0100238template <bool is_range, bool do_assignability_check>
Vladimir Markod16363a2017-02-01 14:09:13 +0000239static ALWAYS_INLINE bool DoCallCommon(ArtMethod* called_method,
240 Thread* self,
241 ShadowFrame& shadow_frame,
242 JValue* result,
243 uint16_t number_of_inputs,
244 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
245 uint32_t vregC) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100246
247template <bool is_range>
Narayan Kamath2cb856c2016-11-02 12:01:26 +0000248ALWAYS_INLINE void CopyRegisters(ShadowFrame& caller_frame,
249 ShadowFrame* callee_frame,
250 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
251 const size_t first_src_reg,
252 const size_t first_dest_reg,
253 const size_t num_regs) REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100254
255// END DECLARATIONS.
256
Siva Chandra05d24152016-01-05 17:43:17 -0800257void ArtInterpreterToCompiledCodeBridge(Thread* self,
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100258 ArtMethod* caller,
Siva Chandra05d24152016-01-05 17:43:17 -0800259 ShadowFrame* shadow_frame,
Jeff Hao5ea84132017-05-05 16:59:29 -0700260 uint16_t arg_offset,
Siva Chandra05d24152016-01-05 17:43:17 -0800261 JValue* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700262 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700263 ArtMethod* method = shadow_frame->GetMethod();
264 // Ensure static methods are initialized.
265 if (method->IsStatic()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700266 ObjPtr<mirror::Class> declaringClass = method->GetDeclaringClass();
Vladimir Markobaa81b52019-08-02 10:14:04 +0100267 if (UNLIKELY(!declaringClass->IsVisiblyInitialized())) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700268 self->PushShadowFrame(shadow_frame);
269 StackHandleScope<1> hs(self);
270 Handle<mirror::Class> h_class(hs.NewHandle(declaringClass));
Vladimir Markobaa81b52019-08-02 10:14:04 +0100271 if (UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
272 self, h_class, /*can_init_fields=*/ true, /*can_init_parents=*/ true))) {
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700273 self->PopShadowFrame();
274 DCHECK(self->IsExceptionPending());
275 return;
276 }
277 self->PopShadowFrame();
Vladimir Markobaa81b52019-08-02 10:14:04 +0100278 DCHECK(h_class->IsInitializing());
Nicolas Geoffray01822292017-03-09 09:03:19 +0000279 // Reload from shadow frame in case the method moved, this is faster than adding a handle.
280 method = shadow_frame->GetMethod();
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700281 }
282 }
Jeff Hao5ea84132017-05-05 16:59:29 -0700283 // Basic checks for the arg_offset. If there's no code item, the arg_offset must be 0. Otherwise,
284 // check that the arg_offset isn't greater than the number of registers. A stronger check is
285 // difficult since the frame may contain space for all the registers in the method, or only enough
286 // space for the arguments.
287 if (kIsDebugBuild) {
288 if (method->GetCodeItem() == nullptr) {
289 DCHECK_EQ(0u, arg_offset) << method->PrettyMethod();
290 } else {
291 DCHECK_LE(arg_offset, shadow_frame->NumberOfVRegs());
292 }
293 }
Nicolas Geoffray71cd50f2016-04-14 15:00:33 +0100294 jit::Jit* jit = Runtime::Current()->GetJit();
295 if (jit != nullptr && caller != nullptr) {
296 jit->NotifyInterpreterToCompiledCodeTransition(self, caller);
297 }
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700298 method->Invoke(self, shadow_frame->GetVRegArgs(arg_offset),
299 (shadow_frame->NumberOfVRegs() - arg_offset) * sizeof(uint32_t),
Andreas Gampe542451c2016-07-26 09:02:02 -0700300 result, method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty());
Andreas Gampe3cfa4d02015-10-06 17:04:01 -0700301}
302
Mingyao Yangffedec52016-05-19 10:48:40 -0700303void SetStringInitValueToAllAliases(ShadowFrame* shadow_frame,
304 uint16_t this_obj_vreg,
305 JValue result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700306 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700307 ObjPtr<mirror::Object> existing = shadow_frame->GetVRegReference(this_obj_vreg);
Mingyao Yangffedec52016-05-19 10:48:40 -0700308 if (existing == nullptr) {
309 // If it's null, we come from compiled code that was deoptimized. Nothing to do,
310 // as the compiler verified there was no alias.
311 // Set the new string result of the StringFactory.
312 shadow_frame->SetVRegReference(this_obj_vreg, result.GetL());
313 return;
314 }
315 // Set the string init result into all aliases.
316 for (uint32_t i = 0, e = shadow_frame->NumberOfVRegs(); i < e; ++i) {
317 if (shadow_frame->GetVRegReference(i) == existing) {
318 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100319 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700320 shadow_frame->SetVRegReference(i, result.GetL());
321 DCHECK_EQ(shadow_frame->GetVRegReference(i),
Vladimir Marko78baed52018-10-11 10:44:58 +0100322 reinterpret_cast32<mirror::Object*>(shadow_frame->GetVReg(i)));
Mingyao Yangffedec52016-05-19 10:48:40 -0700323 }
324 }
325}
326
Orion Hodsonc069a302017-01-18 09:23:12 +0000327template<bool is_range>
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100328static bool DoMethodHandleInvokeCommon(Thread* self,
329 ShadowFrame& shadow_frame,
330 bool invoke_exact,
331 const Instruction* inst,
332 uint16_t inst_data,
333 JValue* result)
Orion Hodsonba28f9f2016-10-26 10:56:25 +0100334 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Light848574c2017-09-25 16:59:39 -0700335 // Make sure to check for async exceptions
336 if (UNLIKELY(self->ObserveAsyncException())) {
337 return false;
338 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100339 // Invoke-polymorphic instructions always take a receiver. i.e, they are never static.
340 const uint32_t vRegC = (is_range) ? inst->VRegC_4rcc() : inst->VRegC_45cc();
Orion Hodson3d617ac2016-10-19 14:00:46 +0100341 const int invoke_method_idx = (is_range) ? inst->VRegB_4rcc() : inst->VRegB_45cc();
Narayan Kamath9823e782016-08-03 12:46:58 +0100342
Orion Hodson1a06f9f2016-11-09 08:32:42 +0000343 // Initialize |result| to 0 as this is the default return value for
344 // polymorphic invocations of method handle types with void return
345 // and provides sane return result in error cases.
346 result->SetJ(0);
347
Orion Hodson3d617ac2016-10-19 14:00:46 +0100348 // The invoke_method_idx here is the name of the signature polymorphic method that
Narayan Kamath9823e782016-08-03 12:46:58 +0100349 // was symbolically invoked in bytecode (say MethodHandle.invoke or MethodHandle.invokeExact)
350 // and not the method that we'll dispatch to in the end.
Orion Hodsone7732be2017-10-11 14:35:20 +0100351 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +0000352 Handle<mirror::MethodHandle> method_handle(hs.NewHandle(
Vladimir Markod7e9bbf2019-03-28 13:18:57 +0000353 ObjPtr<mirror::MethodHandle>::DownCast(shadow_frame.GetVRegReference(vRegC))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800354 if (UNLIKELY(method_handle == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100355 // Note that the invoke type is kVirtual here because a call to a signature
356 // polymorphic method is shaped like a virtual call at the bytecode level.
Orion Hodson3d617ac2016-10-19 14:00:46 +0100357 ThrowNullPointerExceptionForMethodAccess(invoke_method_idx, InvokeType::kVirtual);
Narayan Kamath9823e782016-08-03 12:46:58 +0100358 return false;
359 }
360
361 // The vRegH value gives the index of the proto_id associated with this
Orion Hodson811bd5f2016-12-07 11:35:37 +0000362 // signature polymorphic call site.
Orion Hodson06d10a72018-05-14 08:53:38 +0100363 const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
364 const dex::ProtoIndex callsite_proto_id(vRegH);
Narayan Kamath9823e782016-08-03 12:46:58 +0100365
366 // Call through to the classlinker and ask it to resolve the static type associated
367 // with the callsite. This information is stored in the dex cache so it's
368 // guaranteed to be fast after the first resolution.
Narayan Kamath9823e782016-08-03 12:46:58 +0100369 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsone7732be2017-10-11 14:35:20 +0100370 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
371 class_linker->ResolveMethodType(self, callsite_proto_id, shadow_frame.GetMethod())));
Narayan Kamath9823e782016-08-03 12:46:58 +0100372
373 // This implies we couldn't resolve one or more types in this method handle.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800374 if (UNLIKELY(callsite_type == nullptr)) {
Narayan Kamath9823e782016-08-03 12:46:58 +0100375 CHECK(self->IsExceptionPending());
Narayan Kamath9823e782016-08-03 12:46:58 +0100376 return false;
377 }
378
Orion Hodson811bd5f2016-12-07 11:35:37 +0000379 // There is a common dispatch method for method handles that takes
380 // arguments either from a range or an array of arguments depending
381 // on whether the DEX instruction is invoke-polymorphic/range or
382 // invoke-polymorphic. The array here is for the latter.
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100383 if (UNLIKELY(is_range)) {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000384 // VRegC is the register holding the method handle. Arguments passed
385 // to the method handle's target do not include the method handle.
Orion Hodson960d4f72017-11-10 15:32:38 +0000386 RangeInstructionOperands operands(inst->VRegC_4rcc() + 1, inst->VRegA_4rcc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100387 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000388 return MethodHandleInvokeExact(self,
389 shadow_frame,
390 method_handle,
391 callsite_type,
392 &operands,
393 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100394 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000395 return MethodHandleInvoke(self,
396 shadow_frame,
397 method_handle,
398 callsite_type,
399 &operands,
400 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100401 }
Narayan Kamath9823e782016-08-03 12:46:58 +0100402 } else {
Orion Hodson811bd5f2016-12-07 11:35:37 +0000403 // Get the register arguments for the invoke.
Orion Hodson960d4f72017-11-10 15:32:38 +0000404 uint32_t args[Instruction::kMaxVarArgRegs] = {};
Orion Hodson811bd5f2016-12-07 11:35:37 +0000405 inst->GetVarArgs(args, inst_data);
406 // Drop the first register which is the method handle performing the invoke.
Alexey Frunze8631a462017-01-19 19:07:37 -0800407 memmove(args, args + 1, sizeof(args[0]) * (Instruction::kMaxVarArgRegs - 1));
Orion Hodson811bd5f2016-12-07 11:35:37 +0000408 args[Instruction::kMaxVarArgRegs - 1] = 0;
Orion Hodson960d4f72017-11-10 15:32:38 +0000409 VarArgsInstructionOperands operands(args, inst->VRegA_45cc() - 1);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100410 if (invoke_exact) {
Orion Hodson960d4f72017-11-10 15:32:38 +0000411 return MethodHandleInvokeExact(self,
412 shadow_frame,
413 method_handle,
414 callsite_type,
415 &operands,
416 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100417 } else {
Orion Hodson960d4f72017-11-10 15:32:38 +0000418 return MethodHandleInvoke(self,
419 shadow_frame,
420 method_handle,
421 callsite_type,
422 &operands,
423 result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100424 }
425 }
426}
427
428bool DoMethodHandleInvokeExact(Thread* self,
429 ShadowFrame& shadow_frame,
430 const Instruction* inst,
431 uint16_t inst_data,
432 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
433 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
434 static const bool kIsRange = false;
435 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700436 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100437 } else {
438 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
439 static const bool kIsRange = true;
440 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700441 self, shadow_frame, /* invoke_exact= */ true, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100442 }
443}
444
445bool DoMethodHandleInvoke(Thread* self,
446 ShadowFrame& shadow_frame,
447 const Instruction* inst,
448 uint16_t inst_data,
449 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) {
450 if (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC) {
451 static const bool kIsRange = false;
452 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700453 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100454 } else {
455 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_POLYMORPHIC_RANGE);
456 static const bool kIsRange = true;
457 return DoMethodHandleInvokeCommon<kIsRange>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700458 self, shadow_frame, /* invoke_exact= */ false, inst, inst_data, result);
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100459 }
460}
461
Orion Hodson928033d2018-02-07 05:30:54 +0000462static bool DoVarHandleInvokeCommon(Thread* self,
463 ShadowFrame& shadow_frame,
464 const Instruction* inst,
465 uint16_t inst_data,
466 JValue* result,
467 mirror::VarHandle::AccessMode access_mode)
468 REQUIRES_SHARED(Locks::mutator_lock_) {
469 // Make sure to check for async exceptions
470 if (UNLIKELY(self->ObserveAsyncException())) {
471 return false;
472 }
473
Orion Hodson928033d2018-02-07 05:30:54 +0000474 StackHandleScope<2> hs(self);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100475 bool is_var_args = inst->HasVarArgs();
Orion Hodson06d10a72018-05-14 08:53:38 +0100476 const uint16_t vRegH = is_var_args ? inst->VRegH_45cc() : inst->VRegH_4rcc();
Orion Hodson928033d2018-02-07 05:30:54 +0000477 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
478 Handle<mirror::MethodType> callsite_type(hs.NewHandle(
Orion Hodson06d10a72018-05-14 08:53:38 +0100479 class_linker->ResolveMethodType(self, dex::ProtoIndex(vRegH), shadow_frame.GetMethod())));
Orion Hodson928033d2018-02-07 05:30:54 +0000480 // This implies we couldn't resolve one or more types in this VarHandle.
481 if (UNLIKELY(callsite_type == nullptr)) {
482 CHECK(self->IsExceptionPending());
483 return false;
484 }
485
Orion Hodson537a4fe2018-05-15 13:57:58 +0100486 const uint32_t vRegC = is_var_args ? inst->VRegC_45cc() : inst->VRegC_4rcc();
487 ObjPtr<mirror::Object> receiver(shadow_frame.GetVRegReference(vRegC));
Vladimir Marko179b7c62019-03-22 13:38:57 +0000488 Handle<mirror::VarHandle> var_handle(hs.NewHandle(ObjPtr<mirror::VarHandle>::DownCast(receiver)));
Orion Hodson928033d2018-02-07 05:30:54 +0000489 if (is_var_args) {
490 uint32_t args[Instruction::kMaxVarArgRegs];
491 inst->GetVarArgs(args, inst_data);
492 VarArgsInstructionOperands all_operands(args, inst->VRegA_45cc());
493 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100494 return VarHandleInvokeAccessor(self,
495 shadow_frame,
496 var_handle,
497 callsite_type,
498 access_mode,
499 &operands,
500 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000501 } else {
502 RangeInstructionOperands all_operands(inst->VRegC_4rcc(), inst->VRegA_4rcc());
503 NoReceiverInstructionOperands operands(&all_operands);
Orion Hodson537a4fe2018-05-15 13:57:58 +0100504 return VarHandleInvokeAccessor(self,
505 shadow_frame,
506 var_handle,
507 callsite_type,
508 access_mode,
509 &operands,
510 result);
Orion Hodson928033d2018-02-07 05:30:54 +0000511 }
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100512}
513
Orion Hodson928033d2018-02-07 05:30:54 +0000514#define DO_VAR_HANDLE_ACCESSOR(_access_mode) \
515bool DoVarHandle ## _access_mode(Thread* self, \
516 ShadowFrame& shadow_frame, \
517 const Instruction* inst, \
518 uint16_t inst_data, \
519 JValue* result) REQUIRES_SHARED(Locks::mutator_lock_) { \
520 const auto access_mode = mirror::VarHandle::AccessMode::k ## _access_mode; \
521 return DoVarHandleInvokeCommon(self, shadow_frame, inst, inst_data, result, access_mode); \
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100522}
523
Orion Hodson928033d2018-02-07 05:30:54 +0000524DO_VAR_HANDLE_ACCESSOR(CompareAndExchange)
525DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeAcquire)
526DO_VAR_HANDLE_ACCESSOR(CompareAndExchangeRelease)
527DO_VAR_HANDLE_ACCESSOR(CompareAndSet)
528DO_VAR_HANDLE_ACCESSOR(Get)
529DO_VAR_HANDLE_ACCESSOR(GetAcquire)
530DO_VAR_HANDLE_ACCESSOR(GetAndAdd)
531DO_VAR_HANDLE_ACCESSOR(GetAndAddAcquire)
532DO_VAR_HANDLE_ACCESSOR(GetAndAddRelease)
533DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAnd)
534DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndAcquire)
535DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseAndRelease)
536DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOr)
537DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrAcquire)
538DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseOrRelease)
539DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXor)
540DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorAcquire)
541DO_VAR_HANDLE_ACCESSOR(GetAndBitwiseXorRelease)
542DO_VAR_HANDLE_ACCESSOR(GetAndSet)
543DO_VAR_HANDLE_ACCESSOR(GetAndSetAcquire)
544DO_VAR_HANDLE_ACCESSOR(GetAndSetRelease)
545DO_VAR_HANDLE_ACCESSOR(GetOpaque)
546DO_VAR_HANDLE_ACCESSOR(GetVolatile)
547DO_VAR_HANDLE_ACCESSOR(Set)
548DO_VAR_HANDLE_ACCESSOR(SetOpaque)
549DO_VAR_HANDLE_ACCESSOR(SetRelease)
550DO_VAR_HANDLE_ACCESSOR(SetVolatile)
551DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSet)
552DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetAcquire)
553DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetPlain)
554DO_VAR_HANDLE_ACCESSOR(WeakCompareAndSetRelease)
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100555
Orion Hodson928033d2018-02-07 05:30:54 +0000556#undef DO_VAR_HANDLE_ACCESSOR
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100557
558template<bool is_range>
559bool DoInvokePolymorphic(Thread* self,
560 ShadowFrame& shadow_frame,
561 const Instruction* inst,
562 uint16_t inst_data,
563 JValue* result) {
564 const int invoke_method_idx = inst->VRegB();
565 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
566 ArtMethod* invoke_method =
567 class_linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
568 self, invoke_method_idx, shadow_frame.GetMethod(), kVirtual);
569
570 // Ensure intrinsic identifiers are initialized.
571 DCHECK(invoke_method->IsIntrinsic());
572
573 // Dispatch based on intrinsic identifier associated with method.
574 switch (static_cast<art::Intrinsics>(invoke_method->GetIntrinsic())) {
575#define CASE_SIGNATURE_POLYMORPHIC_INTRINSIC(Name, ...) \
576 case Intrinsics::k##Name: \
577 return Do ## Name(self, shadow_frame, inst, inst_data, result);
578#include "intrinsics_list.h"
579 SIGNATURE_POLYMORPHIC_INTRINSICS_LIST(CASE_SIGNATURE_POLYMORPHIC_INTRINSIC)
580#undef INTRINSICS_LIST
581#undef SIGNATURE_POLYMORPHIC_INTRINSICS_LIST
582#undef CASE_SIGNATURE_POLYMORPHIC_INTRINSIC
583 default:
584 LOG(FATAL) << "Unreachable: " << invoke_method->GetIntrinsic();
585 UNREACHABLE();
586 return false;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100587 }
588}
589
Orion Hodsona5dca522018-02-27 12:42:11 +0000590static JValue ConvertScalarBootstrapArgument(jvalue value) {
591 // value either contains a primitive scalar value if it corresponds
592 // to a primitive type, or it contains an integer value if it
593 // corresponds to an object instance reference id (e.g. a string id).
594 return JValue::FromPrimitive(value.j);
595}
596
597static ObjPtr<mirror::Class> GetClassForBootstrapArgument(EncodedArrayValueIterator::ValueType type)
598 REQUIRES_SHARED(Locks::mutator_lock_) {
599 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100600 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
Orion Hodsona5dca522018-02-27 12:42:11 +0000601 switch (type) {
602 case EncodedArrayValueIterator::ValueType::kBoolean:
603 case EncodedArrayValueIterator::ValueType::kByte:
604 case EncodedArrayValueIterator::ValueType::kChar:
605 case EncodedArrayValueIterator::ValueType::kShort:
606 // These types are disallowed by JVMS. Treat as integers. This
607 // will result in CCE's being raised if the BSM has one of these
608 // types.
609 case EncodedArrayValueIterator::ValueType::kInt:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100610 return GetClassRoot(ClassRoot::kPrimitiveInt, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000611 case EncodedArrayValueIterator::ValueType::kLong:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100612 return GetClassRoot(ClassRoot::kPrimitiveLong, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000613 case EncodedArrayValueIterator::ValueType::kFloat:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100614 return GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000615 case EncodedArrayValueIterator::ValueType::kDouble:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100616 return GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000617 case EncodedArrayValueIterator::ValueType::kMethodType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100618 return GetClassRoot<mirror::MethodType>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000619 case EncodedArrayValueIterator::ValueType::kMethodHandle:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100620 return GetClassRoot<mirror::MethodHandle>(class_roots);
Orion Hodsona5dca522018-02-27 12:42:11 +0000621 case EncodedArrayValueIterator::ValueType::kString:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100622 return GetClassRoot<mirror::String>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000623 case EncodedArrayValueIterator::ValueType::kType:
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100624 return GetClassRoot<mirror::Class>();
Orion Hodsona5dca522018-02-27 12:42:11 +0000625 case EncodedArrayValueIterator::ValueType::kField:
626 case EncodedArrayValueIterator::ValueType::kMethod:
627 case EncodedArrayValueIterator::ValueType::kEnum:
628 case EncodedArrayValueIterator::ValueType::kArray:
629 case EncodedArrayValueIterator::ValueType::kAnnotation:
630 case EncodedArrayValueIterator::ValueType::kNull:
631 return nullptr;
632 }
633}
634
635static bool GetArgumentForBootstrapMethod(Thread* self,
636 ArtMethod* referrer,
637 EncodedArrayValueIterator::ValueType type,
638 const JValue* encoded_value,
639 JValue* decoded_value)
640 REQUIRES_SHARED(Locks::mutator_lock_) {
641 // The encoded_value contains either a scalar value (IJDF) or a
642 // scalar DEX file index to a reference type to be materialized.
643 switch (type) {
644 case EncodedArrayValueIterator::ValueType::kInt:
645 case EncodedArrayValueIterator::ValueType::kFloat:
646 decoded_value->SetI(encoded_value->GetI());
647 return true;
648 case EncodedArrayValueIterator::ValueType::kLong:
649 case EncodedArrayValueIterator::ValueType::kDouble:
650 decoded_value->SetJ(encoded_value->GetJ());
651 return true;
652 case EncodedArrayValueIterator::ValueType::kMethodType: {
653 StackHandleScope<2> hs(self);
654 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
655 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
Orion Hodson06d10a72018-05-14 08:53:38 +0100656 dex::ProtoIndex proto_idx(encoded_value->GetC());
Orion Hodsona5dca522018-02-27 12:42:11 +0000657 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Orion Hodson06d10a72018-05-14 08:53:38 +0100658 ObjPtr<mirror::MethodType> o =
659 cl->ResolveMethodType(self, proto_idx, dex_cache, class_loader);
Orion Hodsona5dca522018-02-27 12:42:11 +0000660 if (UNLIKELY(o.IsNull())) {
661 DCHECK(self->IsExceptionPending());
662 return false;
663 }
664 decoded_value->SetL(o);
665 return true;
666 }
667 case EncodedArrayValueIterator::ValueType::kMethodHandle: {
668 uint32_t index = static_cast<uint32_t>(encoded_value->GetI());
669 ClassLinker* cl = Runtime::Current()->GetClassLinker();
670 ObjPtr<mirror::MethodHandle> o = cl->ResolveMethodHandle(self, index, referrer);
671 if (UNLIKELY(o.IsNull())) {
672 DCHECK(self->IsExceptionPending());
673 return false;
674 }
675 decoded_value->SetL(o);
676 return true;
677 }
678 case EncodedArrayValueIterator::ValueType::kString: {
Orion Hodsona5dca522018-02-27 12:42:11 +0000679 dex::StringIndex index(static_cast<uint32_t>(encoded_value->GetI()));
680 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko18090d12018-06-01 16:53:12 +0100681 ObjPtr<mirror::String> o = cl->ResolveString(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +0000682 if (UNLIKELY(o.IsNull())) {
683 DCHECK(self->IsExceptionPending());
684 return false;
685 }
686 decoded_value->SetL(o);
687 return true;
688 }
689 case EncodedArrayValueIterator::ValueType::kType: {
Orion Hodsona5dca522018-02-27 12:42:11 +0000690 dex::TypeIndex index(static_cast<uint32_t>(encoded_value->GetI()));
691 ClassLinker* cl = Runtime::Current()->GetClassLinker();
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100692 ObjPtr<mirror::Class> o = cl->ResolveType(index, referrer);
Orion Hodsona5dca522018-02-27 12:42:11 +0000693 if (UNLIKELY(o.IsNull())) {
694 DCHECK(self->IsExceptionPending());
695 return false;
696 }
697 decoded_value->SetL(o);
698 return true;
699 }
700 case EncodedArrayValueIterator::ValueType::kBoolean:
701 case EncodedArrayValueIterator::ValueType::kByte:
702 case EncodedArrayValueIterator::ValueType::kChar:
703 case EncodedArrayValueIterator::ValueType::kShort:
704 case EncodedArrayValueIterator::ValueType::kField:
705 case EncodedArrayValueIterator::ValueType::kMethod:
706 case EncodedArrayValueIterator::ValueType::kEnum:
707 case EncodedArrayValueIterator::ValueType::kArray:
708 case EncodedArrayValueIterator::ValueType::kAnnotation:
709 case EncodedArrayValueIterator::ValueType::kNull:
710 // Unreachable - unsupported types that have been checked when
711 // determining the effect call site type based on the bootstrap
712 // argument types.
713 UNREACHABLE();
714 }
715}
716
717static bool PackArgumentForBootstrapMethod(Thread* self,
718 ArtMethod* referrer,
719 CallSiteArrayValueIterator* it,
720 ShadowFrameSetter* setter)
721 REQUIRES_SHARED(Locks::mutator_lock_) {
722 auto type = it->GetValueType();
723 const JValue encoded_value = ConvertScalarBootstrapArgument(it->GetJavaValue());
724 JValue decoded_value;
725 if (!GetArgumentForBootstrapMethod(self, referrer, type, &encoded_value, &decoded_value)) {
726 return false;
727 }
728 switch (it->GetValueType()) {
729 case EncodedArrayValueIterator::ValueType::kInt:
730 case EncodedArrayValueIterator::ValueType::kFloat:
731 setter->Set(static_cast<uint32_t>(decoded_value.GetI()));
732 return true;
733 case EncodedArrayValueIterator::ValueType::kLong:
734 case EncodedArrayValueIterator::ValueType::kDouble:
735 setter->SetLong(decoded_value.GetJ());
736 return true;
737 case EncodedArrayValueIterator::ValueType::kMethodType:
738 case EncodedArrayValueIterator::ValueType::kMethodHandle:
739 case EncodedArrayValueIterator::ValueType::kString:
740 case EncodedArrayValueIterator::ValueType::kType:
741 setter->SetReference(decoded_value.GetL());
742 return true;
743 case EncodedArrayValueIterator::ValueType::kBoolean:
744 case EncodedArrayValueIterator::ValueType::kByte:
745 case EncodedArrayValueIterator::ValueType::kChar:
746 case EncodedArrayValueIterator::ValueType::kShort:
747 case EncodedArrayValueIterator::ValueType::kField:
748 case EncodedArrayValueIterator::ValueType::kMethod:
749 case EncodedArrayValueIterator::ValueType::kEnum:
750 case EncodedArrayValueIterator::ValueType::kArray:
751 case EncodedArrayValueIterator::ValueType::kAnnotation:
752 case EncodedArrayValueIterator::ValueType::kNull:
753 // Unreachable - unsupported types that have been checked when
754 // determining the effect call site type based on the bootstrap
755 // argument types.
756 UNREACHABLE();
757 }
758}
759
760static bool PackCollectorArrayForBootstrapMethod(Thread* self,
761 ArtMethod* referrer,
762 ObjPtr<mirror::Class> array_type,
763 int32_t array_length,
764 CallSiteArrayValueIterator* it,
765 ShadowFrameSetter* setter)
766 REQUIRES_SHARED(Locks::mutator_lock_) {
767 StackHandleScope<1> hs(self);
768 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
769 JValue decoded_value;
770
771#define COLLECT_PRIMITIVE_ARRAY(Descriptor, Type) \
772 Handle<mirror::Type ## Array> array = \
773 hs.NewHandle(mirror::Type ## Array::Alloc(self, array_length)); \
774 if (array.IsNull()) { \
775 return false; \
776 } \
777 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
778 auto type = it->GetValueType(); \
779 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
780 const JValue encoded_value = \
781 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
782 GetArgumentForBootstrapMethod(self, \
783 referrer, \
784 type, \
785 &encoded_value, \
786 &decoded_value); \
787 array->Set(i, decoded_value.Get ## Descriptor()); \
788 } \
789 setter->SetReference(array.Get()); \
790 return true;
791
792#define COLLECT_REFERENCE_ARRAY(T, Type) \
Andreas Gampe584771b2018-10-18 13:22:23 -0700793 Handle<mirror::ObjectArray<T>> array = /* NOLINT */ \
Orion Hodsona5dca522018-02-27 12:42:11 +0000794 hs.NewHandle(mirror::ObjectArray<T>::Alloc(self, \
795 array_type, \
796 array_length)); \
797 if (array.IsNull()) { \
798 return false; \
799 } \
800 for (int32_t i = 0; it->HasNext(); it->Next(), ++i) { \
801 auto type = it->GetValueType(); \
802 DCHECK_EQ(type, EncodedArrayValueIterator::ValueType::k ## Type); \
803 const JValue encoded_value = \
804 ConvertScalarBootstrapArgument(it->GetJavaValue()); \
805 if (!GetArgumentForBootstrapMethod(self, \
806 referrer, \
807 type, \
808 &encoded_value, \
809 &decoded_value)) { \
810 return false; \
811 } \
812 ObjPtr<mirror::Object> o = decoded_value.GetL(); \
813 if (Runtime::Current()->IsActiveTransaction()) { \
814 array->Set<true>(i, ObjPtr<T>::DownCast(o)); \
815 } else { \
816 array->Set<false>(i, ObjPtr<T>::DownCast(o)); \
817 } \
818 } \
819 setter->SetReference(array.Get()); \
820 return true;
821
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100822 ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = class_linker->GetClassRoots();
823 ObjPtr<mirror::Class> component_type = array_type->GetComponentType();
824 if (component_type == GetClassRoot(ClassRoot::kPrimitiveInt, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +0000825 COLLECT_PRIMITIVE_ARRAY(I, Int);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100826 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveLong, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +0000827 COLLECT_PRIMITIVE_ARRAY(J, Long);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100828 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +0000829 COLLECT_PRIMITIVE_ARRAY(F, Float);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100830 } else if (component_type == GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +0000831 COLLECT_PRIMITIVE_ARRAY(D, Double);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100832 } else if (component_type == GetClassRoot<mirror::MethodType>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +0000833 COLLECT_REFERENCE_ARRAY(mirror::MethodType, MethodType);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100834 } else if (component_type == GetClassRoot<mirror::MethodHandle>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +0000835 COLLECT_REFERENCE_ARRAY(mirror::MethodHandle, MethodHandle);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100836 } else if (component_type == GetClassRoot<mirror::String>(class_roots)) {
Orion Hodsona5dca522018-02-27 12:42:11 +0000837 COLLECT_REFERENCE_ARRAY(mirror::String, String);
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100838 } else if (component_type == GetClassRoot<mirror::Class>()) {
Orion Hodsona5dca522018-02-27 12:42:11 +0000839 COLLECT_REFERENCE_ARRAY(mirror::Class, Type);
840 } else {
841 UNREACHABLE();
842 }
843 #undef COLLECT_PRIMITIVE_ARRAY
844 #undef COLLECT_REFERENCE_ARRAY
845}
846
847static ObjPtr<mirror::MethodType> BuildCallSiteForBootstrapMethod(Thread* self,
848 const DexFile* dex_file,
849 uint32_t call_site_idx)
850 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800851 const dex::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsona5dca522018-02-27 12:42:11 +0000852 CallSiteArrayValueIterator it(*dex_file, csi);
853 DCHECK_GE(it.Size(), 1u);
854
855 StackHandleScope<2> hs(self);
856 // Create array for parameter types.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100857 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100858 ObjPtr<mirror::Class> class_array_type =
859 GetClassRoot<mirror::ObjectArray<mirror::Class>>(class_linker);
Orion Hodsona5dca522018-02-27 12:42:11 +0000860 Handle<mirror::ObjectArray<mirror::Class>> ptypes = hs.NewHandle(
861 mirror::ObjectArray<mirror::Class>::Alloc(self,
862 class_array_type,
863 static_cast<int>(it.Size())));
864 if (ptypes.IsNull()) {
865 DCHECK(self->IsExceptionPending());
866 return nullptr;
867 }
868
869 // Populate the first argument with an instance of j.l.i.MethodHandles.Lookup
870 // that the runtime will construct.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100871 ptypes->Set(0, GetClassRoot<mirror::MethodHandlesLookup>(class_linker));
Orion Hodsona5dca522018-02-27 12:42:11 +0000872 it.Next();
873
874 // The remaining parameter types are derived from the types of
875 // arguments present in the DEX file.
876 int index = 1;
877 while (it.HasNext()) {
878 ObjPtr<mirror::Class> ptype = GetClassForBootstrapArgument(it.GetValueType());
879 if (ptype.IsNull()) {
880 ThrowClassCastException("Unsupported bootstrap argument type");
881 return nullptr;
882 }
883 ptypes->Set(index, ptype);
884 index++;
885 it.Next();
886 }
887 DCHECK_EQ(static_cast<size_t>(index), it.Size());
888
889 // By definition, the return type is always a j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +0100890 Handle<mirror::Class> rtype = hs.NewHandle(GetClassRoot<mirror::CallSite>());
Orion Hodsona5dca522018-02-27 12:42:11 +0000891 return mirror::MethodType::Create(self, rtype, ptypes);
892}
893
Orion Hodsonc069a302017-01-18 09:23:12 +0000894static ObjPtr<mirror::CallSite> InvokeBootstrapMethod(Thread* self,
895 ShadowFrame& shadow_frame,
896 uint32_t call_site_idx)
897 REQUIRES_SHARED(Locks::mutator_lock_) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +0100898 StackHandleScope<5> hs(self);
Orion Hodsona5dca522018-02-27 12:42:11 +0000899 // There are three mandatory arguments expected from the call site
900 // value array in the DEX file: the bootstrap method handle, the
901 // method name to pass to the bootstrap method, and the method type
902 // to pass to the bootstrap method.
903 static constexpr size_t kMandatoryArgumentsCount = 3;
Orion Hodsonc069a302017-01-18 09:23:12 +0000904 ArtMethod* referrer = shadow_frame.GetMethod();
905 const DexFile* dex_file = referrer->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800906 const dex::CallSiteIdItem& csi = dex_file->GetCallSiteId(call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +0000907 CallSiteArrayValueIterator it(*dex_file, csi);
Orion Hodsona5dca522018-02-27 12:42:11 +0000908 if (it.Size() < kMandatoryArgumentsCount) {
909 ThrowBootstrapMethodError("Truncated bootstrap arguments (%zu < %zu)",
910 it.Size(), kMandatoryArgumentsCount);
911 return nullptr;
912 }
913
914 if (it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodHandle) {
915 ThrowBootstrapMethodError("First bootstrap argument is not a method handle");
916 return nullptr;
917 }
918
919 uint32_t bsm_index = static_cast<uint32_t>(it.GetJavaValue().i);
920 it.Next();
921
Orion Hodsonc069a302017-01-18 09:23:12 +0000922 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Orion Hodsona5dca522018-02-27 12:42:11 +0000923 Handle<mirror::MethodHandle> bsm =
924 hs.NewHandle(class_linker->ResolveMethodHandle(self, bsm_index, referrer));
925 if (bsm.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +0000926 DCHECK(self->IsExceptionPending());
927 return nullptr;
928 }
Orion Hodsonc069a302017-01-18 09:23:12 +0000929
Orion Hodsona5dca522018-02-27 12:42:11 +0000930 if (bsm->GetHandleKind() != mirror::MethodHandle::Kind::kInvokeStatic) {
931 // JLS suggests also accepting constructors. This is currently
932 // hard as constructor invocations happen via transformers in ART
933 // today. The constructor would need to be a class derived from java.lang.invoke.CallSite.
934 ThrowBootstrapMethodError("Unsupported bootstrap method invocation kind");
935 return nullptr;
936 }
937
938 // Construct the local call site type information based on the 3
939 // mandatory arguments provided by the runtime and the static arguments
940 // in the DEX file. We will use these arguments to build a shadow frame.
941 MutableHandle<mirror::MethodType> call_site_type =
942 hs.NewHandle(BuildCallSiteForBootstrapMethod(self, dex_file, call_site_idx));
943 if (call_site_type.IsNull()) {
944 DCHECK(self->IsExceptionPending());
945 return nullptr;
946 }
947
948 // Check if this BSM is targeting a variable arity method. If so,
949 // we'll need to collect the trailing arguments into an array.
950 Handle<mirror::Array> collector_arguments;
951 int32_t collector_arguments_length;
952 if (bsm->GetTargetMethod()->IsVarargs()) {
953 int number_of_bsm_parameters = bsm->GetMethodType()->GetNumberOfPTypes();
954 if (number_of_bsm_parameters == 0) {
955 ThrowBootstrapMethodError("Variable arity BSM does not have any arguments");
956 return nullptr;
957 }
958 Handle<mirror::Class> collector_array_class =
959 hs.NewHandle(bsm->GetMethodType()->GetPTypes()->Get(number_of_bsm_parameters - 1));
960 if (!collector_array_class->IsArrayClass()) {
961 ThrowBootstrapMethodError("Variable arity BSM does not have array as final argument");
962 return nullptr;
963 }
964 // The call site may include no arguments to be collected. In this
965 // case the number of arguments must be at least the number of BSM
966 // parameters less the collector array.
967 if (call_site_type->GetNumberOfPTypes() < number_of_bsm_parameters - 1) {
968 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
969 return nullptr;
970 }
971 // Check all the arguments to be collected match the collector array component type.
972 for (int i = number_of_bsm_parameters - 1; i < call_site_type->GetNumberOfPTypes(); ++i) {
973 if (call_site_type->GetPTypes()->Get(i) != collector_array_class->GetComponentType()) {
974 ThrowClassCastException(collector_array_class->GetComponentType(),
975 call_site_type->GetPTypes()->Get(i));
976 return nullptr;
977 }
978 }
979 // Update the call site method type so it now includes the collector array.
980 int32_t collector_arguments_start = number_of_bsm_parameters - 1;
981 collector_arguments_length = call_site_type->GetNumberOfPTypes() - number_of_bsm_parameters + 1;
982 call_site_type.Assign(
983 mirror::MethodType::CollectTrailingArguments(self,
984 call_site_type.Get(),
985 collector_array_class.Get(),
986 collector_arguments_start));
987 if (call_site_type.IsNull()) {
988 DCHECK(self->IsExceptionPending());
989 return nullptr;
990 }
991 } else {
992 collector_arguments_length = 0;
993 }
994
995 if (call_site_type->GetNumberOfPTypes() != bsm->GetMethodType()->GetNumberOfPTypes()) {
996 ThrowWrongMethodTypeException(bsm->GetMethodType(), call_site_type.Get());
997 return nullptr;
998 }
999
1000 // BSM invocation has a different set of exceptions that
1001 // j.l.i.MethodHandle.invoke(). Scan arguments looking for CCE
1002 // "opportunities". Unfortunately we cannot just leave this to the
1003 // method handle invocation as this might generate a WMTE.
1004 for (int32_t i = 0; i < call_site_type->GetNumberOfPTypes(); ++i) {
1005 ObjPtr<mirror::Class> from = call_site_type->GetPTypes()->Get(i);
1006 ObjPtr<mirror::Class> to = bsm->GetMethodType()->GetPTypes()->Get(i);
1007 if (!IsParameterTypeConvertible(from, to)) {
1008 ThrowClassCastException(from, to);
1009 return nullptr;
1010 }
1011 }
1012 if (!IsReturnTypeConvertible(call_site_type->GetRType(), bsm->GetMethodType()->GetRType())) {
1013 ThrowClassCastException(bsm->GetMethodType()->GetRType(), call_site_type->GetRType());
1014 return nullptr;
1015 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001016
1017 // Set-up a shadow frame for invoking the bootstrap method handle.
1018 ShadowFrameAllocaUniquePtr bootstrap_frame =
Orion Hodsona5dca522018-02-27 12:42:11 +00001019 CREATE_SHADOW_FRAME(call_site_type->NumberOfVRegs(),
1020 nullptr,
1021 referrer,
1022 shadow_frame.GetDexPC());
Orion Hodsonc069a302017-01-18 09:23:12 +00001023 ScopedStackedShadowFramePusher pusher(
1024 self, bootstrap_frame.get(), StackedShadowFrameType::kShadowFrameUnderConstruction);
Orion Hodsona5dca522018-02-27 12:42:11 +00001025 ShadowFrameSetter setter(bootstrap_frame.get(), 0u);
Orion Hodsonc069a302017-01-18 09:23:12 +00001026
1027 // The first parameter is a MethodHandles lookup instance.
Orion Hodsona5dca522018-02-27 12:42:11 +00001028 Handle<mirror::Class> lookup_class =
1029 hs.NewHandle(shadow_frame.GetMethod()->GetDeclaringClass());
1030 ObjPtr<mirror::MethodHandlesLookup> lookup =
1031 mirror::MethodHandlesLookup::Create(self, lookup_class);
1032 if (lookup.IsNull()) {
Orion Hodsonc069a302017-01-18 09:23:12 +00001033 DCHECK(self->IsExceptionPending());
1034 return nullptr;
1035 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001036 setter.SetReference(lookup);
Orion Hodsonc069a302017-01-18 09:23:12 +00001037
Orion Hodsona5dca522018-02-27 12:42:11 +00001038 // Pack the remaining arguments into the frame.
1039 int number_of_arguments = call_site_type->GetNumberOfPTypes();
1040 int argument_index;
1041 for (argument_index = 1; argument_index < number_of_arguments; ++argument_index) {
1042 if (argument_index == number_of_arguments - 1 &&
1043 call_site_type->GetPTypes()->Get(argument_index)->IsArrayClass()) {
1044 ObjPtr<mirror::Class> array_type = call_site_type->GetPTypes()->Get(argument_index);
1045 if (!PackCollectorArrayForBootstrapMethod(self,
1046 referrer,
1047 array_type,
1048 collector_arguments_length,
1049 &it,
1050 &setter)) {
1051 DCHECK(self->IsExceptionPending());
1052 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001053 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001054 } else if (!PackArgumentForBootstrapMethod(self, referrer, &it, &setter)) {
1055 DCHECK(self->IsExceptionPending());
1056 return nullptr;
Orion Hodsonc069a302017-01-18 09:23:12 +00001057 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001058 it.Next();
1059 }
Orion Hodsona5dca522018-02-27 12:42:11 +00001060 DCHECK(!it.HasNext());
1061 DCHECK(setter.Done());
Orion Hodsonc069a302017-01-18 09:23:12 +00001062
1063 // Invoke the bootstrap method handle.
1064 JValue result;
Orion Hodsona5dca522018-02-27 12:42:11 +00001065 RangeInstructionOperands operands(0, bootstrap_frame->NumberOfVRegs());
1066 bool invoke_success = MethodHandleInvoke(self,
1067 *bootstrap_frame,
1068 bsm,
1069 call_site_type,
1070 &operands,
1071 &result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001072 if (!invoke_success) {
1073 DCHECK(self->IsExceptionPending());
1074 return nullptr;
1075 }
1076
1077 Handle<mirror::Object> object(hs.NewHandle(result.GetL()));
Orion Hodsonc069a302017-01-18 09:23:12 +00001078 if (UNLIKELY(object.IsNull())) {
Orion Hodsonda1cdd02018-01-31 18:08:28 +00001079 // This will typically be for LambdaMetafactory which is not supported.
Orion Hodson76e6adb2018-02-23 13:15:55 +00001080 ThrowClassCastException("Bootstrap method returned null");
Orion Hodsonc069a302017-01-18 09:23:12 +00001081 return nullptr;
1082 }
1083
Orion Hodsona5dca522018-02-27 12:42:11 +00001084 // Check the result type is a subclass of j.l.i.CallSite.
Vladimir Markoc7aa87e2018-05-24 15:19:52 +01001085 ObjPtr<mirror::Class> call_site_class = GetClassRoot<mirror::CallSite>(class_linker);
1086 if (UNLIKELY(!object->InstanceOf(call_site_class))) {
1087 ThrowClassCastException(object->GetClass(), call_site_class);
Orion Hodsonc069a302017-01-18 09:23:12 +00001088 return nullptr;
1089 }
1090
Orion Hodsona5dca522018-02-27 12:42:11 +00001091 // Check the call site target is not null as we're going to invoke it.
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001092 ObjPtr<mirror::CallSite> call_site = ObjPtr<mirror::CallSite>::DownCast(result.GetL());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001093 ObjPtr<mirror::MethodHandle> target = call_site->GetTarget();
1094 if (UNLIKELY(target == nullptr)) {
Orion Hodsona5dca522018-02-27 12:42:11 +00001095 ThrowClassCastException("Bootstrap method returned a CallSite with a null target");
Orion Hodsonc069a302017-01-18 09:23:12 +00001096 return nullptr;
1097 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001098 return call_site;
Orion Hodsonc069a302017-01-18 09:23:12 +00001099}
1100
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001101namespace {
1102
1103ObjPtr<mirror::CallSite> DoResolveCallSite(Thread* self,
1104 ShadowFrame& shadow_frame,
1105 uint32_t call_site_idx)
1106 REQUIRES_SHARED(Locks::mutator_lock_) {
1107 StackHandleScope<1> hs(self);
1108 Handle<mirror::DexCache> dex_cache(hs.NewHandle(shadow_frame.GetMethod()->GetDexCache()));
1109
1110 // Get the call site from the DexCache if present.
1111 ObjPtr<mirror::CallSite> call_site = dex_cache->GetResolvedCallSite(call_site_idx);
1112 if (LIKELY(call_site != nullptr)) {
1113 return call_site;
1114 }
1115
1116 // Invoke the bootstrap method to get a candidate call site.
1117 call_site = InvokeBootstrapMethod(self, shadow_frame, call_site_idx);
1118 if (UNLIKELY(call_site == nullptr)) {
1119 if (!self->GetException()->IsError()) {
1120 // Use a BootstrapMethodError if the exception is not an instance of java.lang.Error.
1121 ThrowWrappedBootstrapMethodError("Exception from call site #%u bootstrap method",
1122 call_site_idx);
1123 }
1124 return nullptr;
1125 }
1126
1127 // Attempt to place the candidate call site into the DexCache, return the winning call site.
1128 return dex_cache->SetResolvedCallSite(call_site_idx, call_site);
1129}
1130
1131} // namespace
1132
Orion Hodsonc069a302017-01-18 09:23:12 +00001133bool DoInvokeCustom(Thread* self,
1134 ShadowFrame& shadow_frame,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001135 uint32_t call_site_idx,
1136 const InstructionOperands* operands,
1137 JValue* result) {
Alex Light848574c2017-09-25 16:59:39 -07001138 // Make sure to check for async exceptions
1139 if (UNLIKELY(self->ObserveAsyncException())) {
1140 return false;
1141 }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001142
Orion Hodsonc069a302017-01-18 09:23:12 +00001143 // invoke-custom is not supported in transactions. In transactions
1144 // there is a limited set of types supported. invoke-custom allows
1145 // running arbitrary code and instantiating arbitrary types.
1146 CHECK(!Runtime::Current()->IsActiveTransaction());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001147
1148 ObjPtr<mirror::CallSite> call_site = DoResolveCallSite(self, shadow_frame, call_site_idx);
Orion Hodsonc069a302017-01-18 09:23:12 +00001149 if (call_site.IsNull()) {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001150 DCHECK(self->IsExceptionPending());
1151 return false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001152 }
1153
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001154 StackHandleScope<2> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00001155 Handle<mirror::MethodHandle> target = hs.NewHandle(call_site->GetTarget());
1156 Handle<mirror::MethodType> target_method_type = hs.NewHandle(target->GetMethodType());
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001157 DCHECK_EQ(operands->GetNumberOfOperands(), target_method_type->NumberOfVRegs())
1158 << " call_site_idx" << call_site_idx;
1159 return MethodHandleInvokeExact(self,
1160 shadow_frame,
1161 target,
1162 target_method_type,
1163 operands,
1164 result);
Orion Hodsonc069a302017-01-18 09:23:12 +00001165}
1166
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001167// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame.
1168static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame,
1169 size_t dest_reg, size_t src_reg)
1170 REQUIRES_SHARED(Locks::mutator_lock_) {
1171 // Uint required, so that sign extension does not make this wrong on 64b systems
1172 uint32_t src_value = shadow_frame.GetVReg(src_reg);
1173 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg);
1174
1175 // If both register locations contains the same value, the register probably holds a reference.
1176 // Note: As an optimization, non-moving collectors leave a stale reference value
1177 // in the references array even after the original vreg was overwritten to a non-reference.
Vladimir Marko78baed52018-10-11 10:44:58 +01001178 if (src_value == reinterpret_cast32<uint32_t>(o.Ptr())) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001179 new_shadow_frame->SetVRegReference(dest_reg, o);
1180 } else {
1181 new_shadow_frame->SetVReg(dest_reg, src_value);
1182 }
1183}
1184
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001185template <bool is_range>
1186inline void CopyRegisters(ShadowFrame& caller_frame,
1187 ShadowFrame* callee_frame,
1188 const uint32_t (&arg)[Instruction::kMaxVarArgRegs],
1189 const size_t first_src_reg,
1190 const size_t first_dest_reg,
1191 const size_t num_regs) {
1192 if (is_range) {
1193 const size_t dest_reg_bound = first_dest_reg + num_regs;
1194 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < dest_reg_bound;
1195 ++dest_reg, ++src_reg) {
1196 AssignRegister(callee_frame, caller_frame, dest_reg, src_reg);
1197 }
1198 } else {
1199 DCHECK_LE(num_regs, arraysize(arg));
1200
1201 for (size_t arg_index = 0; arg_index < num_regs; ++arg_index) {
1202 AssignRegister(callee_frame, caller_frame, first_dest_reg + arg_index, arg[arg_index]);
1203 }
1204 }
1205}
1206
Igor Murashkin6918bf12015-09-27 19:19:06 -07001207template <bool is_range,
Narayan Kamath370423d2016-10-03 16:51:22 +01001208 bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001209static inline bool DoCallCommon(ArtMethod* called_method,
1210 Thread* self,
1211 ShadowFrame& shadow_frame,
1212 JValue* result,
1213 uint16_t number_of_inputs,
Narayan Kamath370423d2016-10-03 16:51:22 +01001214 uint32_t (&arg)[Instruction::kMaxVarArgRegs],
Igor Murashkin158f35c2015-06-10 15:55:30 -07001215 uint32_t vregC) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001216 bool string_init = false;
1217 // Replace calls to String.<init> with equivalent StringFactory call.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001218 if (UNLIKELY(called_method->GetDeclaringClass()->IsStringClass()
1219 && called_method->IsConstructor())) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01001220 called_method = WellKnownClasses::StringInitToStringFactory(called_method);
Jeff Hao848f70a2014-01-15 13:49:50 -08001221 string_init = true;
1222 }
1223
Alex Lightdaf58c82016-03-16 23:00:49 +00001224 // Compute method information.
David Sehr0225f8e2018-01-31 08:52:24 +00001225 CodeItemDataAccessor accessor(called_method->DexInstructionData());
Igor Murashkin158f35c2015-06-10 15:55:30 -07001226 // Number of registers for the callee's call frame.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001227 uint16_t num_regs;
Jeff Hao5ea84132017-05-05 16:59:29 -07001228 // Test whether to use the interpreter or compiler entrypoint, and save that result to pass to
1229 // PerformCall. A deoptimization could occur at any time, and we shouldn't change which
1230 // entrypoint to use once we start building the shadow frame.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001231
1232 // For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where we are
1233 // doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use the image
1234 // pointer size here and this may case an overflow if it is called from the compiler. b/62402160
1235 const bool use_interpreter_entrypoint = !Runtime::Current()->IsStarted() ||
1236 ClassLinker::ShouldUseInterpreterEntrypoint(
1237 called_method,
1238 called_method->GetEntryPointFromQuickCompiledCode());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001239 if (LIKELY(accessor.HasCodeItem())) {
Jeff Hao5ea84132017-05-05 16:59:29 -07001240 // When transitioning to compiled code, space only needs to be reserved for the input registers.
1241 // The rest of the frame gets discarded. This also prevents accessing the called method's code
1242 // item, saving memory by keeping code items of compiled code untouched.
Mathieu Chartier448bbcf2017-07-06 12:05:13 -07001243 if (!use_interpreter_entrypoint) {
1244 DCHECK(!Runtime::Current()->IsAotCompiler()) << "Compiler should use interpreter entrypoint";
Jeff Hao5ea84132017-05-05 16:59:29 -07001245 num_regs = number_of_inputs;
1246 } else {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001247 num_regs = accessor.RegistersSize();
1248 DCHECK_EQ(string_init ? number_of_inputs - 1 : number_of_inputs, accessor.InsSize());
Jeff Hao5ea84132017-05-05 16:59:29 -07001249 }
Nicolas Geoffray01822292017-03-09 09:03:19 +00001250 } else {
1251 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1252 num_regs = number_of_inputs;
Jeff Haodf79ddb2017-02-27 14:47:06 -08001253 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001254
Igor Murashkin158f35c2015-06-10 15:55:30 -07001255 // Hack for String init:
1256 //
1257 // Rewrite invoke-x java.lang.String.<init>(this, a, b, c, ...) into:
1258 // invoke-x StringFactory(a, b, c, ...)
1259 // by effectively dropping the first virtual register from the invoke.
1260 //
1261 // (at this point the ArtMethod has already been replaced,
1262 // so we just need to fix-up the arguments)
David Brazdil65902e82016-01-15 09:35:13 +00001263 //
1264 // Note that FindMethodFromCode in entrypoint_utils-inl.h was also special-cased
1265 // to handle the compiler optimization of replacing `this` with null without
1266 // throwing NullPointerException.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001267 uint32_t string_init_vreg_this = is_range ? vregC : arg[0];
Igor Murashkina06b49b2015-06-25 15:18:12 -07001268 if (UNLIKELY(string_init)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001269 DCHECK_GT(num_regs, 0u); // As the method is an instance method, there should be at least 1.
Igor Murashkina06b49b2015-06-25 15:18:12 -07001270
Igor Murashkin158f35c2015-06-10 15:55:30 -07001271 // The new StringFactory call is static and has one fewer argument.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001272 if (!accessor.HasCodeItem()) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001273 DCHECK(called_method->IsNative() || called_method->IsProxyMethod());
1274 num_regs--;
1275 } // else ... don't need to change num_regs since it comes up from the string_init's code item
Igor Murashkin158f35c2015-06-10 15:55:30 -07001276 number_of_inputs--;
1277
1278 // Rewrite the var-args, dropping the 0th argument ("this")
Igor Murashkin6918bf12015-09-27 19:19:06 -07001279 for (uint32_t i = 1; i < arraysize(arg); ++i) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001280 arg[i - 1] = arg[i];
1281 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07001282 arg[arraysize(arg) - 1] = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001283
1284 // Rewrite the non-var-arg case
1285 vregC++; // Skips the 0th vreg in the range ("this").
1286 }
1287
1288 // Parameter registers go at the end of the shadow frame.
1289 DCHECK_GE(num_regs, number_of_inputs);
1290 size_t first_dest_reg = num_regs - number_of_inputs;
1291 DCHECK_NE(first_dest_reg, (size_t)-1);
1292
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001293 // Allocate shadow frame on the stack.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001294 const char* old_cause = self->StartAssertNoThreadSuspension("DoCallCommon");
Andreas Gampeb3025922015-09-01 14:45:00 -07001295 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -07001296 CREATE_SHADOW_FRAME(num_regs, &shadow_frame, called_method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -07001297 ShadowFrame* new_shadow_frame = shadow_frame_unique_ptr.get();
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001298
Igor Murashkin158f35c2015-06-10 15:55:30 -07001299 // Initialize new shadow frame by copying the registers from the callee shadow frame.
Jeff Haoa3faaf42013-09-03 19:07:00 -07001300 if (do_assignability_check) {
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001301 // Slow path.
1302 // We might need to do class loading, which incurs a thread state change to kNative. So
1303 // register the shadow frame as under construction and allow suspension again.
Mingyao Yang1f2d3ba2015-05-18 12:12:50 -07001304 ScopedStackedShadowFramePusher pusher(
Sebastien Hertzf7958692015-06-09 14:09:14 +02001305 self, new_shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001306 self->EndAssertNoThreadSuspension(old_cause);
1307
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001308 // ArtMethod here is needed to check type information of the call site against the callee.
1309 // Type information is retrieved from a DexFile/DexCache for that respective declared method.
1310 //
1311 // As a special case for proxy methods, which are not dex-backed,
1312 // we have to retrieve type information from the proxy's method
1313 // interface method instead (which is dex backed since proxies are never interfaces).
Andreas Gampe542451c2016-07-26 09:02:02 -07001314 ArtMethod* method =
1315 new_shadow_frame->GetMethod()->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001316
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001317 // We need to do runtime check on reference assignment. We need to load the shorty
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001318 // to get the exact type of each reference argument.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001319 const dex::TypeList* params = method->GetParameterTypeList();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001320 uint32_t shorty_len = 0;
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001321 const char* shorty = method->GetShorty(&shorty_len);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001322
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001323 // Handle receiver apart since it's not part of the shorty.
1324 size_t dest_reg = first_dest_reg;
1325 size_t arg_offset = 0;
Igor Murashkin158f35c2015-06-10 15:55:30 -07001326
Igor Murashkin9f95ba72016-02-01 14:21:25 -08001327 if (!method->IsStatic()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001328 size_t receiver_reg = is_range ? vregC : arg[0];
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001329 new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg));
1330 ++dest_reg;
1331 ++arg_offset;
Igor Murashkina06b49b2015-06-25 15:18:12 -07001332 DCHECK(!string_init); // All StringFactory methods are static.
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001333 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001334
1335 // Copy the caller's invoke-* arguments into the callee's parameter registers.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001336 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) {
Igor Murashkina06b49b2015-06-25 15:18:12 -07001337 // Skip the 0th 'shorty' type since it represents the return type.
1338 DCHECK_LT(shorty_pos + 1, shorty_len) << "for shorty '" << shorty << "'";
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001339 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset];
1340 switch (shorty[shorty_pos + 1]) {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001341 // Handle Object references. 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001342 case 'L': {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001343 ObjPtr<mirror::Object> o = shadow_frame.GetVRegReference(src_reg);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001344 if (do_assignability_check && o != nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001345 const dex::TypeIndex type_idx = params->GetTypeItem(shorty_pos).type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +00001346 ObjPtr<mirror::Class> arg_type = method->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001347 if (arg_type == nullptr) {
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001348 StackHandleScope<1> hs(self);
1349 // Preserve o since it is used below and GetClassFromTypeIndex may cause thread
1350 // suspension.
1351 HandleWrapperObjPtr<mirror::Object> h = hs.NewHandleWrapper(&o);
Vladimir Markob45528c2017-07-27 14:14:28 +01001352 arg_type = method->ResolveClassFromTypeIndex(type_idx);
Mathieu Chartiere22305b2016-10-26 21:04:58 -07001353 if (arg_type == nullptr) {
1354 CHECK(self->IsExceptionPending());
1355 return false;
1356 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001357 }
1358 if (!o->VerifierInstanceOf(arg_type)) {
1359 // This should never happen.
Ian Rogers1ff3c982014-08-12 02:30:58 -07001360 std::string temp1, temp2;
Orion Hodsonfef06642016-11-25 16:07:11 +00001361 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001362 "Invoking %s with bad arg %d, type '%s' not instance of '%s'",
Ian Rogerse94652f2014-12-02 11:13:19 -08001363 new_shadow_frame->GetMethod()->GetName(), shorty_pos,
Ian Rogers1ff3c982014-08-12 02:30:58 -07001364 o->GetClass()->GetDescriptor(&temp1),
1365 arg_type->GetDescriptor(&temp2));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001366 return false;
1367 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001368 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +01001369 new_shadow_frame->SetVRegReference(dest_reg, o);
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001370 break;
Jeff Haoa3faaf42013-09-03 19:07:00 -07001371 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001372 // Handle doubles and longs. 2 consecutive virtual register slots.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001373 case 'J': case 'D': {
Igor Murashkin158f35c2015-06-10 15:55:30 -07001374 uint64_t wide_value =
1375 (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << BitSizeOf<uint32_t>()) |
1376 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg));
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001377 new_shadow_frame->SetVRegLong(dest_reg, wide_value);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001378 // Skip the next virtual register slot since we already used it.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001379 ++dest_reg;
1380 ++arg_offset;
1381 break;
1382 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001383 // Handle all other primitives that are always 1 virtual register slot.
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001384 default:
1385 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg));
1386 break;
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001387 }
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001388 }
1389 } else {
Sebastien Hertz9ace87b2013-09-27 11:48:09 +02001390 if (is_range) {
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001391 DCHECK_EQ(num_regs, first_dest_reg + number_of_inputs);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001392 }
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +01001393
1394 CopyRegisters<is_range>(shadow_frame,
1395 new_shadow_frame,
1396 arg,
1397 vregC,
1398 first_dest_reg,
1399 number_of_inputs);
Andreas Gampe2a0d4ec2014-06-02 22:05:22 -07001400 self->EndAssertNoThreadSuspension(old_cause);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001401 }
1402
Jeff Hao5ea84132017-05-05 16:59:29 -07001403 PerformCall(self,
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001404 accessor,
Jeff Hao5ea84132017-05-05 16:59:29 -07001405 shadow_frame.GetMethod(),
1406 first_dest_reg,
1407 new_shadow_frame,
1408 result,
1409 use_interpreter_entrypoint);
Jeff Hao848f70a2014-01-15 13:49:50 -08001410
1411 if (string_init && !self->IsExceptionPending()) {
Mingyao Yangffedec52016-05-19 10:48:40 -07001412 SetStringInitValueToAllAliases(&shadow_frame, string_init_vreg_this, *result);
Jeff Hao848f70a2014-01-15 13:49:50 -08001413 }
1414
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001415 return !self->IsExceptionPending();
1416}
1417
Igor Murashkin158f35c2015-06-10 15:55:30 -07001418template<bool is_range, bool do_assignability_check>
Igor Murashkin158f35c2015-06-10 15:55:30 -07001419bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
1420 const Instruction* inst, uint16_t inst_data, JValue* result) {
1421 // Argument word count.
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001422 const uint16_t number_of_inputs =
1423 (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001424
1425 // TODO: find a cleaner way to separate non-range and range information without duplicating
1426 // code.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001427 uint32_t arg[Instruction::kMaxVarArgRegs] = {}; // only used in invoke-XXX.
Igor Murashkin158f35c2015-06-10 15:55:30 -07001428 uint32_t vregC = 0;
1429 if (is_range) {
1430 vregC = inst->VRegC_3rc();
1431 } else {
1432 vregC = inst->VRegC_35c();
1433 inst->GetVarArgs(arg, inst_data);
1434 }
1435
1436 return DoCallCommon<is_range, do_assignability_check>(
1437 called_method, self, shadow_frame,
1438 result, number_of_inputs, arg, vregC);
1439}
1440
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001441template <bool is_range, bool do_access_check, bool transaction_active>
Mathieu Chartieref41db72016-10-25 15:08:01 -07001442bool DoFilledNewArray(const Instruction* inst,
1443 const ShadowFrame& shadow_frame,
1444 Thread* self,
1445 JValue* result) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001446 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY ||
1447 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE);
1448 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c();
1449 if (!is_range) {
1450 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments.
1451 CHECK_LE(length, 5);
1452 }
1453 if (UNLIKELY(length < 0)) {
1454 ThrowNegativeArraySizeException(length);
1455 return false;
1456 }
1457 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampea5b09a62016-11-17 15:21:22 -08001458 ObjPtr<mirror::Class> array_class = ResolveVerifyAndClinit(dex::TypeIndex(type_idx),
Mathieu Chartieref41db72016-10-25 15:08:01 -07001459 shadow_frame.GetMethod(),
1460 self,
1461 false,
1462 do_access_check);
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001463 if (UNLIKELY(array_class == nullptr)) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001464 DCHECK(self->IsExceptionPending());
1465 return false;
1466 }
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001467 CHECK(array_class->IsArrayClass());
Mathieu Chartieref41db72016-10-25 15:08:01 -07001468 ObjPtr<mirror::Class> component_class = array_class->GetComponentType();
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001469 const bool is_primitive_int_component = component_class->IsPrimitiveInt();
1470 if (UNLIKELY(component_class->IsPrimitive() && !is_primitive_int_component)) {
1471 if (component_class->IsPrimitiveLong() || component_class->IsPrimitiveDouble()) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001472 ThrowRuntimeException("Bad filled array request for type %s",
David Sehr709b0702016-10-13 09:12:37 -07001473 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001474 } else {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001475 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
Brian Carlstrom4fa0bcd2013-12-10 11:24:21 -08001476 "Found type %s; filled-new-array not implemented for anything but 'int'",
David Sehr709b0702016-10-13 09:12:37 -07001477 component_class->PrettyDescriptor().c_str());
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001478 }
1479 return false;
1480 }
Vladimir Marko9b81ac32019-05-16 16:47:08 +01001481 ObjPtr<mirror::Object> new_array = mirror::Array::Alloc(
Mathieu Chartieref41db72016-10-25 15:08:01 -07001482 self,
1483 array_class,
1484 length,
1485 array_class->GetComponentSizeShift(),
1486 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001487 if (UNLIKELY(new_array == nullptr)) {
1488 self->AssertPendingOOMException();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001489 return false;
1490 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07001491 uint32_t arg[Instruction::kMaxVarArgRegs]; // only used in filled-new-array.
1492 uint32_t vregC = 0; // only used in filled-new-array-range.
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001493 if (is_range) {
Sebastien Hertzabff6432014-01-27 18:01:39 +01001494 vregC = inst->VRegC_3rc();
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001495 } else {
Ian Rogers29a26482014-05-02 15:27:29 -07001496 inst->GetVarArgs(arg);
Sebastien Hertzabff6432014-01-27 18:01:39 +01001497 }
Sebastien Hertzabff6432014-01-27 18:01:39 +01001498 for (int32_t i = 0; i < length; ++i) {
1499 size_t src_reg = is_range ? vregC + i : arg[i];
1500 if (is_primitive_int_component) {
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001501 new_array->AsIntArray()->SetWithoutChecks<transaction_active>(
1502 i, shadow_frame.GetVReg(src_reg));
Sebastien Hertzabff6432014-01-27 18:01:39 +01001503 } else {
Mathieu Chartieref41db72016-10-25 15:08:01 -07001504 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<transaction_active>(
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001505 i, shadow_frame.GetVRegReference(src_reg));
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001506 }
1507 }
1508
Mathieu Chartier52ea33b2015-06-18 16:48:52 -07001509 result->SetL(new_array);
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001510 return true;
1511}
1512
Mathieu Chartieref41db72016-10-25 15:08:01 -07001513// TODO: Use ObjPtr here.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001514template<typename T>
Vladimir Marko4617d582019-03-28 13:48:31 +00001515static void RecordArrayElementsInTransactionImpl(ObjPtr<mirror::PrimitiveArray<T>> array,
Mathieu Chartieref41db72016-10-25 15:08:01 -07001516 int32_t count)
1517 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001518 Runtime* runtime = Runtime::Current();
1519 for (int32_t i = 0; i < count; ++i) {
Vladimir Marko4617d582019-03-28 13:48:31 +00001520 runtime->RecordWriteArray(array.Ptr(), i, array->GetWithoutChecks(i));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001521 }
1522}
1523
Mathieu Chartieref41db72016-10-25 15:08:01 -07001524void RecordArrayElementsInTransaction(ObjPtr<mirror::Array> array, int32_t count)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001525 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001526 DCHECK(Runtime::Current()->IsActiveTransaction());
1527 DCHECK(array != nullptr);
1528 DCHECK_LE(count, array->GetLength());
1529 Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType();
1530 switch (primitive_component_type) {
1531 case Primitive::kPrimBoolean:
1532 RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count);
1533 break;
1534 case Primitive::kPrimByte:
1535 RecordArrayElementsInTransactionImpl(array->AsByteArray(), count);
1536 break;
1537 case Primitive::kPrimChar:
1538 RecordArrayElementsInTransactionImpl(array->AsCharArray(), count);
1539 break;
1540 case Primitive::kPrimShort:
1541 RecordArrayElementsInTransactionImpl(array->AsShortArray(), count);
1542 break;
1543 case Primitive::kPrimInt:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001544 RecordArrayElementsInTransactionImpl(array->AsIntArray(), count);
1545 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001546 case Primitive::kPrimFloat:
1547 RecordArrayElementsInTransactionImpl(array->AsFloatArray(), count);
1548 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001549 case Primitive::kPrimLong:
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001550 RecordArrayElementsInTransactionImpl(array->AsLongArray(), count);
1551 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001552 case Primitive::kPrimDouble:
1553 RecordArrayElementsInTransactionImpl(array->AsDoubleArray(), count);
1554 break;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001555 default:
1556 LOG(FATAL) << "Unsupported primitive type " << primitive_component_type
1557 << " in fill-array-data";
Elliott Hughesc1896c92018-11-29 11:33:18 -08001558 UNREACHABLE();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001559 }
1560}
1561
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001562// Explicit DoCall template function declarations.
Sebastien Hertzc6714852013-09-30 16:42:32 +02001563#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001564 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertz9119c5f2013-12-16 11:31:45 +01001565 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \
1566 ShadowFrame& shadow_frame, \
Sebastien Hertzc6714852013-09-30 16:42:32 +02001567 const Instruction* inst, uint16_t inst_data, \
1568 JValue* result)
Sebastien Hertzc61124b2013-09-10 11:44:19 +02001569EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false);
1570EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true);
1571EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false);
1572EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true);
1573#undef EXPLICIT_DO_CALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001574
Orion Hodsonc069a302017-01-18 09:23:12 +00001575// Explicit DoInvokePolymorphic template function declarations.
1576#define EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(_is_range) \
1577 template REQUIRES_SHARED(Locks::mutator_lock_) \
1578 bool DoInvokePolymorphic<_is_range>( \
1579 Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1580 uint16_t inst_data, JValue* result)
1581EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(false);
1582EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL(true);
Narayan Kamath9823e782016-08-03 12:46:58 +01001583#undef EXPLICIT_DO_INVOKE_POLYMORPHIC_TEMPLATE_DECL
1584
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001585// Explicit DoFilledNewArray template function declarations.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001586#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001587 template REQUIRES_SHARED(Locks::mutator_lock_) \
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001588 bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \
1589 const ShadowFrame& shadow_frame, \
1590 Thread* self, JValue* result)
1591#define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \
1592 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \
1593 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \
1594 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \
1595 EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active)
1596EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false);
1597EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true);
1598#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001599#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL
1600
1601} // namespace interpreter
1602} // namespace art