blob: 8c495fc7bba535db596a976ca22c4bfb2efbac85 [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#ifndef ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_
18#define ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_
19
20#include "interpreter.h"
21
22#include <math.h>
23
Ian Rogerscf7f1912014-10-22 22:06:39 -070024#include <iostream>
Ian Rogersc7dd2952014-10-21 23:31:19 -070025#include <sstream>
26
Mathieu Chartierc7853442015-03-27 14:35:38 -070027#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "art_method-inl.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020029#include "base/logging.h"
Andreas Gampe794ad762015-02-23 08:12:24 -080030#include "base/macros.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020031#include "class_linker-inl.h"
32#include "common_throws.h"
33#include "dex_file-inl.h"
34#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070035#include "entrypoints/entrypoint_utils-inl.h"
Mathieu Chartier0cd81352014-05-22 16:48:55 -070036#include "handle_scope-inl.h"
Igor Murashkin6918bf12015-09-27 19:19:06 -070037#include "lambda/art_lambda_method.h"
Igor Murashkine2facc52015-07-10 13:49:08 -070038#include "lambda/box_table.h"
Igor Murashkin6918bf12015-09-27 19:19:06 -070039#include "lambda/closure.h"
40#include "lambda/closure_builder-inl.h"
41#include "lambda/leaking_allocator.h"
42#include "lambda/shorty_field_type.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020043#include "mirror/class-inl.h"
Igor Murashkin2ee54e22015-06-18 10:05:11 -070044#include "mirror/method.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020045#include "mirror/object-inl.h"
46#include "mirror/object_array-inl.h"
Douglas Leung4965c022014-06-11 11:41:11 -070047#include "mirror/string-inl.h"
Andreas Gampe03ec9302015-08-27 17:41:47 -070048#include "stack.h"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020049#include "thread.h"
50#include "well_known_classes.h"
51
Mathieu Chartiere401d142015-04-22 13:56:20 -070052using ::art::ArtMethod;
Sebastien Hertz8ece0502013-08-07 11:26:41 +020053using ::art::mirror::Array;
54using ::art::mirror::BooleanArray;
55using ::art::mirror::ByteArray;
56using ::art::mirror::CharArray;
57using ::art::mirror::Class;
58using ::art::mirror::ClassLoader;
59using ::art::mirror::IntArray;
60using ::art::mirror::LongArray;
61using ::art::mirror::Object;
62using ::art::mirror::ObjectArray;
63using ::art::mirror::ShortArray;
64using ::art::mirror::String;
65using ::art::mirror::Throwable;
66
67namespace art {
68namespace interpreter {
69
70// External references to both interpreter implementations.
71
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010072template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -080073extern JValue ExecuteSwitchImpl(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzc6714852013-09-30 16:42:32 +020074 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020075
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010076template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -080077extern JValue ExecuteGotoImpl(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzc6714852013-09-30 16:42:32 +020078 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020079
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000080void ThrowNullPointerExceptionFromInterpreter()
Mathieu Chartier90443472015-07-16 20:32:27 -070081 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzda843e12014-05-28 19:28:31 +020082
Andreas Gampe03ec9302015-08-27 17:41:47 -070083template <bool kMonitorCounting>
84static inline void DoMonitorEnter(Thread* self,
85 ShadowFrame* frame,
Mathieu Chartier2d096c92015-10-12 16:18:20 -070086 Object* ref)
87 NO_THREAD_SAFETY_ANALYSIS
88 REQUIRES(!Roles::uninterruptible_) {
89 StackHandleScope<1> hs(self);
90 Handle<Object> h_ref(hs.NewHandle(ref));
91 h_ref->MonitorEnter(self);
92 frame->GetLockCountData().AddMonitor<kMonitorCounting>(self, h_ref.Get());
Sebastien Hertz8ece0502013-08-07 11:26:41 +020093}
94
Andreas Gampe03ec9302015-08-27 17:41:47 -070095template <bool kMonitorCounting>
96static inline void DoMonitorExit(Thread* self,
97 ShadowFrame* frame,
Mathieu Chartier2d096c92015-10-12 16:18:20 -070098 Object* ref)
99 NO_THREAD_SAFETY_ANALYSIS
100 REQUIRES(!Roles::uninterruptible_) {
101 StackHandleScope<1> hs(self);
102 Handle<Object> h_ref(hs.NewHandle(ref));
103 h_ref->MonitorExit(self);
104 frame->GetLockCountData().RemoveMonitorOrThrow<kMonitorCounting>(self, h_ref.Get());
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200105}
106
Sebastien Hertz45b15972015-04-03 16:07:05 +0200107void AbortTransactionF(Thread* self, const char* fmt, ...)
108 __attribute__((__format__(__printf__, 2, 3)))
Mathieu Chartier90443472015-07-16 20:32:27 -0700109 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +0200110
111void AbortTransactionV(Thread* self, const char* fmt, va_list args)
Mathieu Chartier90443472015-07-16 20:32:27 -0700112 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -0700113
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100114void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -0700115 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100116
Sebastien Hertzc6714852013-09-30 16:42:32 +0200117// Invokes the given method. This is part of the invocation support and is used by DoInvoke and
118// DoInvokeVirtualQuick functions.
119// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200120template<bool is_range, bool do_assignability_check>
Ian Rogerse94652f2014-12-02 11:13:19 -0800121bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200122 const Instruction* inst, uint16_t inst_data, JValue* result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200123
Igor Murashkin158f35c2015-06-10 15:55:30 -0700124// Invokes the given lambda closure. This is part of the invocation support and is used by
125// DoLambdaInvoke functions.
126// Returns true on success, otherwise throws an exception and returns false.
127template<bool is_range, bool do_assignability_check>
128bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
129 const Instruction* inst, uint16_t inst_data, JValue* result);
130
131// Validates that the art method corresponding to a lambda method target
132// is semantically valid:
133//
134// Must be ACC_STATIC and ACC_LAMBDA. Must be a concrete managed implementation
135// (i.e. not native, not proxy, not abstract, ...).
136//
137// If the validation fails, return false and raise an exception.
138static inline bool IsValidLambdaTargetOrThrow(ArtMethod* called_method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700139 SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700140 bool success = false;
141
142 if (UNLIKELY(called_method == nullptr)) {
143 // The shadow frame should already be pushed, so we don't need to update it.
144 } else if (UNLIKELY(called_method->IsAbstract())) {
145 ThrowAbstractMethodError(called_method);
146 // TODO(iam): Also handle the case when the method is non-static, what error do we throw?
147 // TODO(iam): Also make sure that ACC_LAMBDA is set.
148 } else if (UNLIKELY(called_method->GetCodeItem() == nullptr)) {
149 // Method could be native, proxy method, etc. Lambda targets have to be concrete impls,
150 // so don't allow this.
151 } else {
152 success = true;
153 }
154
155 return success;
156}
157
Igor Murashkin6918bf12015-09-27 19:19:06 -0700158// Write out the 'Closure*' into vreg and vreg+1, as if it was a jlong.
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700159static inline void WriteLambdaClosureIntoVRegs(ShadowFrame& shadow_frame,
Igor Murashkin6918bf12015-09-27 19:19:06 -0700160 const lambda::Closure* lambda_closure,
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700161 uint32_t vreg) {
162 // Split the method into a lo and hi 32 bits so we can encode them into 2 virtual registers.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700163 uint32_t closure_lo = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(lambda_closure));
164 uint32_t closure_hi = static_cast<uint32_t>(reinterpret_cast<uint64_t>(lambda_closure)
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700165 >> BitSizeOf<uint32_t>());
166 // Use uint64_t instead of uintptr_t to allow shifting past the max on 32-bit.
167 static_assert(sizeof(uint64_t) >= sizeof(uintptr_t), "Impossible");
168
Igor Murashkin6918bf12015-09-27 19:19:06 -0700169 DCHECK_NE(closure_lo | closure_hi, 0u);
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700170
Igor Murashkin6918bf12015-09-27 19:19:06 -0700171 shadow_frame.SetVReg(vreg, closure_lo);
172 shadow_frame.SetVReg(vreg + 1, closure_hi);
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700173}
174
Igor Murashkin158f35c2015-06-10 15:55:30 -0700175// Handles create-lambda instructions.
176// Returns true on success, otherwise throws an exception and returns false.
177// (Exceptions are thrown by creating a new exception and then being put in the thread TLS)
178//
Igor Murashkin6918bf12015-09-27 19:19:06 -0700179// The closure must be allocated big enough to hold the data, and should not be
180// pre-initialized. It is initialized with the actual captured variables as a side-effect,
181// although this should be unimportant to the caller since this function also handles storing it to
182// the ShadowFrame.
183//
Igor Murashkin158f35c2015-06-10 15:55:30 -0700184// As a work-in-progress implementation, this shoves the ArtMethod object corresponding
185// to the target dex method index into the target register vA and vA + 1.
186template<bool do_access_check>
Igor Murashkin6918bf12015-09-27 19:19:06 -0700187static inline bool DoCreateLambda(Thread* self,
188 const Instruction* inst,
189 /*inout*/ShadowFrame& shadow_frame,
190 /*inout*/lambda::ClosureBuilder* closure_builder,
191 /*inout*/lambda::Closure* uninitialized_closure) {
192 DCHECK(closure_builder != nullptr);
193 DCHECK(uninitialized_closure != nullptr);
194 DCHECK_ALIGNED(uninitialized_closure, alignof(lambda::Closure));
195
Igor Murashkin158f35c2015-06-10 15:55:30 -0700196 /*
197 * create-lambda is opcode 0x21c
198 * - vA is the target register where the closure will be stored into
199 * (also stores into vA + 1)
200 * - vB is the method index which will be the target for a later invoke-lambda
201 */
202 const uint32_t method_idx = inst->VRegB_21c();
203 mirror::Object* receiver = nullptr; // Always static. (see 'kStatic')
204 ArtMethod* sf_method = shadow_frame.GetMethod();
205 ArtMethod* const called_method = FindMethodFromCode<kStatic, do_access_check>(
Andreas Gampe3a357142015-08-07 17:20:11 -0700206 method_idx, &receiver, sf_method, self);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700207
Igor Murashkin6918bf12015-09-27 19:19:06 -0700208 uint32_t vreg_dest_closure = inst->VRegA_21c();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700209
210 if (UNLIKELY(!IsValidLambdaTargetOrThrow(called_method))) {
211 CHECK(self->IsExceptionPending());
Igor Murashkin6918bf12015-09-27 19:19:06 -0700212 shadow_frame.SetVReg(vreg_dest_closure, 0u);
213 shadow_frame.SetVReg(vreg_dest_closure + 1, 0u);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700214 return false;
215 }
216
Igor Murashkin6918bf12015-09-27 19:19:06 -0700217 lambda::ArtLambdaMethod* initialized_lambda_method;
218 // Initialize the ArtLambdaMethod with the right data.
219 {
220 lambda::ArtLambdaMethod* uninitialized_lambda_method =
221 reinterpret_cast<lambda::ArtLambdaMethod*>(
222 lambda::LeakingAllocator::AllocateMemory(self, sizeof(lambda::ArtLambdaMethod)));
223
224 std::string captured_variables_shorty = closure_builder->GetCapturedVariableShortyTypes();
225 std::string captured_variables_long_type_desc;
226
227 // Synthesize a long type descriptor from the short one.
228 for (char shorty : captured_variables_shorty) {
229 lambda::ShortyFieldType shorty_field_type(shorty);
230 if (shorty_field_type.IsObject()) {
231 // Not the true type, but good enough until we implement verifier support.
232 captured_variables_long_type_desc += "Ljava/lang/Object;";
233 UNIMPLEMENTED(FATAL) << "create-lambda with an object captured variable";
234 } else if (shorty_field_type.IsLambda()) {
235 // Not the true type, but good enough until we implement verifier support.
236 captured_variables_long_type_desc += "Ljava/lang/Runnable;";
237 UNIMPLEMENTED(FATAL) << "create-lambda with a lambda captured variable";
238 } else {
239 // The primitive types have the same length shorty or not, so this is always correct.
240 DCHECK(shorty_field_type.IsPrimitive());
241 captured_variables_long_type_desc += shorty_field_type;
242 }
243 }
244
245 // Copy strings to dynamically allocated storage. This leaks, but that's ok. Fix it later.
246 // TODO: Strings need to come from the DexFile, so they won't need their own allocations.
247 char* captured_variables_type_desc = lambda::LeakingAllocator::MakeFlexibleInstance<char>(
248 self,
249 captured_variables_long_type_desc.size() + 1);
250 strcpy(captured_variables_type_desc, captured_variables_long_type_desc.c_str());
251 char* captured_variables_shorty_copy = lambda::LeakingAllocator::MakeFlexibleInstance<char>(
252 self,
253 captured_variables_shorty.size() + 1);
254 strcpy(captured_variables_shorty_copy, captured_variables_shorty.c_str());
255
256 new (uninitialized_lambda_method) lambda::ArtLambdaMethod(called_method,
257 captured_variables_type_desc,
258 captured_variables_shorty_copy,
259 true); // innate lambda
260 initialized_lambda_method = uninitialized_lambda_method;
261 }
262
263 // Write all the closure captured variables and the closure header into the closure.
264 lambda::Closure* initialized_closure;
265 {
266 initialized_closure =
267 closure_builder->CreateInPlace(uninitialized_closure, initialized_lambda_method);
268 }
269
270 WriteLambdaClosureIntoVRegs(/*inout*/shadow_frame, initialized_closure, vreg_dest_closure);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700271 return true;
272}
273
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700274// Reads out the 'ArtMethod*' stored inside of vreg and vreg+1
275//
276// Validates that the art method points to a valid lambda function, otherwise throws
277// an exception and returns null.
278// (Exceptions are thrown by creating a new exception and then being put in the thread TLS)
Igor Murashkin6918bf12015-09-27 19:19:06 -0700279static inline lambda::Closure* ReadLambdaClosureFromVRegsOrThrow(ShadowFrame& shadow_frame,
280 uint32_t vreg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700281 SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin6918bf12015-09-27 19:19:06 -0700282 // Lambda closures take up a consecutive pair of 2 virtual registers.
283 // On 32-bit the high bits are always 0.
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700284 uint32_t vc_value_lo = shadow_frame.GetVReg(vreg);
285 uint32_t vc_value_hi = shadow_frame.GetVReg(vreg + 1);
286
287 uint64_t vc_value_ptr = (static_cast<uint64_t>(vc_value_hi) << BitSizeOf<uint32_t>())
288 | vc_value_lo;
289
290 // Use uint64_t instead of uintptr_t to allow left-shifting past the max on 32-bit.
291 static_assert(sizeof(uint64_t) >= sizeof(uintptr_t), "Impossible");
Igor Murashkin6918bf12015-09-27 19:19:06 -0700292 lambda::Closure* const lambda_closure = reinterpret_cast<lambda::Closure*>(vc_value_ptr);
293 DCHECK_ALIGNED(lambda_closure, alignof(lambda::Closure));
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700294
295 // Guard against the user passing a null closure, which is odd but (sadly) semantically valid.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700296 if (UNLIKELY(lambda_closure == nullptr)) {
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700297 ThrowNullPointerExceptionFromInterpreter();
298 return nullptr;
Igor Murashkin6918bf12015-09-27 19:19:06 -0700299 } else if (UNLIKELY(!IsValidLambdaTargetOrThrow(lambda_closure->GetTargetMethod()))) {
300 // Sanity check against data corruption.
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700301 return nullptr;
302 }
303
Igor Murashkin6918bf12015-09-27 19:19:06 -0700304 return lambda_closure;
305}
306
307// Forward declaration for lock annotations. See below for documentation.
308template <bool do_access_check>
309static inline const char* GetStringDataByDexStringIndexOrThrow(ShadowFrame& shadow_frame,
310 uint32_t string_idx)
311 SHARED_REQUIRES(Locks::mutator_lock_);
312
313// Find the c-string data corresponding to a dex file's string index.
314// Otherwise, returns null if not found and throws a VerifyError.
315//
316// Note that with do_access_check=false, we never return null because the verifier
317// must guard against invalid string indices.
318// (Exceptions are thrown by creating a new exception and then being put in the thread TLS)
319template <bool do_access_check>
320static inline const char* GetStringDataByDexStringIndexOrThrow(ShadowFrame& shadow_frame,
321 uint32_t string_idx) {
322 ArtMethod* method = shadow_frame.GetMethod();
323 const DexFile* dex_file = method->GetDexFile();
324
325 mirror::Class* declaring_class = method->GetDeclaringClass();
326 if (!do_access_check) {
327 // MethodVerifier refuses methods with string_idx out of bounds.
328 DCHECK_LT(string_idx, declaring_class->GetDexCache()->NumStrings());
329 } else {
330 // Access checks enabled: perform string index bounds ourselves.
331 if (string_idx >= dex_file->GetHeader().string_ids_size_) {
332 ThrowVerifyError(declaring_class, "String index '%" PRIu32 "' out of bounds",
333 string_idx);
334 return nullptr;
335 }
336 }
337
338 const char* type_string = dex_file->StringDataByIdx(string_idx);
339
340 if (UNLIKELY(type_string == nullptr)) {
341 CHECK_EQ(false, do_access_check)
342 << " verifier should've caught invalid string index " << string_idx;
343 CHECK_EQ(true, do_access_check)
344 << " string idx size check should've caught invalid string index " << string_idx;
345 }
346
347 return type_string;
348}
349
350// Handles capture-variable instructions.
351// Returns true on success, otherwise throws an exception and returns false.
352// (Exceptions are thrown by creating a new exception and then being put in the thread TLS)
353template<bool do_access_check>
354static inline bool DoCaptureVariable(Thread* self,
355 const Instruction* inst,
356 /*inout*/ShadowFrame& shadow_frame,
357 /*inout*/lambda::ClosureBuilder* closure_builder) {
358 DCHECK(closure_builder != nullptr);
359 using lambda::ShortyFieldType;
360 /*
361 * capture-variable is opcode 0xf6, fmt 0x21c
362 * - vA is the source register of the variable that will be captured
363 * - vB is the string ID of the variable's type that will be captured
364 */
365 const uint32_t source_vreg = inst->VRegA_21c();
366 const uint32_t string_idx = inst->VRegB_21c();
367 // TODO: this should be a proper [type id] instead of a [string ID] pointing to a type.
368
369 const char* type_string = GetStringDataByDexStringIndexOrThrow<do_access_check>(shadow_frame,
370 string_idx);
371 if (UNLIKELY(type_string == nullptr)) {
372 CHECK(self->IsExceptionPending());
373 return false;
374 }
375
376 char type_first_letter = type_string[0];
377 ShortyFieldType shorty_type;
378 if (do_access_check &&
379 UNLIKELY(!ShortyFieldType::MaybeCreate(type_first_letter, /*out*/&shorty_type))) { // NOLINT: [whitespace/comma] [3]
380 ThrowVerifyError(shadow_frame.GetMethod()->GetDeclaringClass(),
381 "capture-variable vB must be a valid type");
382 return false;
383 } else {
384 // Already verified that the type is valid.
385 shorty_type = ShortyFieldType(type_first_letter);
386 }
387
388 const size_t captured_variable_count = closure_builder->GetCaptureCount();
389
390 // Note: types are specified explicitly so that the closure is packed tightly.
391 switch (shorty_type) {
392 case ShortyFieldType::kBoolean: {
393 uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg);
394 closure_builder->CaptureVariablePrimitive<bool>(primitive_narrow_value);
395 break;
396 }
397 case ShortyFieldType::kByte: {
398 uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg);
399 closure_builder->CaptureVariablePrimitive<int8_t>(primitive_narrow_value);
400 break;
401 }
402 case ShortyFieldType::kChar: {
403 uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg);
404 closure_builder->CaptureVariablePrimitive<uint16_t>(primitive_narrow_value);
405 break;
406 }
407 case ShortyFieldType::kShort: {
408 uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg);
409 closure_builder->CaptureVariablePrimitive<int16_t>(primitive_narrow_value);
410 break;
411 }
412 case ShortyFieldType::kInt: {
413 uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg);
414 closure_builder->CaptureVariablePrimitive<int32_t>(primitive_narrow_value);
415 break;
416 }
417 case ShortyFieldType::kDouble: {
418 closure_builder->CaptureVariablePrimitive(shadow_frame.GetVRegDouble(source_vreg));
419 break;
420 }
421 case ShortyFieldType::kFloat: {
422 closure_builder->CaptureVariablePrimitive(shadow_frame.GetVRegFloat(source_vreg));
423 break;
424 }
425 case ShortyFieldType::kLambda: {
426 UNIMPLEMENTED(FATAL) << " capture-variable with type kLambda";
427 // TODO: Capturing lambdas recursively will be done at a later time.
428 UNREACHABLE();
429 }
430 case ShortyFieldType::kLong: {
431 closure_builder->CaptureVariablePrimitive(shadow_frame.GetVRegLong(source_vreg));
432 break;
433 }
434 case ShortyFieldType::kObject: {
435 closure_builder->CaptureVariableObject(shadow_frame.GetVRegReference(source_vreg));
436 UNIMPLEMENTED(FATAL) << " capture-variable with type kObject";
437 // TODO: finish implementing this. disabled for now since we can't track lambda refs for GC.
438 UNREACHABLE();
439 }
440
441 default:
442 LOG(FATAL) << "Invalid shorty type value " << shorty_type;
443 UNREACHABLE();
444 }
445
446 DCHECK_EQ(captured_variable_count + 1, closure_builder->GetCaptureCount());
447
448 return true;
449}
450
451// Handles capture-variable instructions.
452// Returns true on success, otherwise throws an exception and returns false.
453// (Exceptions are thrown by creating a new exception and then being put in the thread TLS)
454template<bool do_access_check>
455static inline bool DoLiberateVariable(Thread* self,
456 const Instruction* inst,
457 size_t captured_variable_index,
458 /*inout*/ShadowFrame& shadow_frame) {
459 using lambda::ShortyFieldType;
460 /*
461 * liberate-variable is opcode 0xf7, fmt 0x22c
462 * - vA is the destination register
463 * - vB is the register with the lambda closure in it
464 * - vC is the string ID which needs to be a valid field type descriptor
465 */
466
467 const uint32_t dest_vreg = inst->VRegA_22c();
468 const uint32_t closure_vreg = inst->VRegB_22c();
469 const uint32_t string_idx = inst->VRegC_22c();
470 // TODO: this should be a proper [type id] instead of a [string ID] pointing to a type.
471
472
473 // Synthesize a long type descriptor from a shorty type descriptor list.
474 // TODO: Fix the dex encoding to contain the long and short type descriptors.
475 const char* type_string = GetStringDataByDexStringIndexOrThrow<do_access_check>(shadow_frame,
476 string_idx);
477 if (UNLIKELY(do_access_check && type_string == nullptr)) {
478 CHECK(self->IsExceptionPending());
479 shadow_frame.SetVReg(dest_vreg, 0);
480 return false;
481 }
482
483 char type_first_letter = type_string[0];
484 ShortyFieldType shorty_type;
485 if (do_access_check &&
486 UNLIKELY(!ShortyFieldType::MaybeCreate(type_first_letter, /*out*/&shorty_type))) { // NOLINT: [whitespace/comma] [3]
487 ThrowVerifyError(shadow_frame.GetMethod()->GetDeclaringClass(),
488 "liberate-variable vC must be a valid type");
489 shadow_frame.SetVReg(dest_vreg, 0);
490 return false;
491 } else {
492 // Already verified that the type is valid.
493 shorty_type = ShortyFieldType(type_first_letter);
494 }
495
496 // Check for closure being null *after* the type check.
497 // This way we can access the type info in case we fail later, to know how many vregs to clear.
498 const lambda::Closure* lambda_closure =
499 ReadLambdaClosureFromVRegsOrThrow(/*inout*/shadow_frame, closure_vreg);
500
501 // Failed lambda target runtime check, an exception was raised.
502 if (UNLIKELY(lambda_closure == nullptr)) {
503 CHECK(self->IsExceptionPending());
504
505 // Clear the destination vreg(s) to be safe.
506 shadow_frame.SetVReg(dest_vreg, 0);
507 if (shorty_type.IsPrimitiveWide() || shorty_type.IsLambda()) {
508 shadow_frame.SetVReg(dest_vreg + 1, 0);
509 }
510 return false;
511 }
512
513 if (do_access_check &&
514 UNLIKELY(captured_variable_index >= lambda_closure->GetNumberOfCapturedVariables())) {
515 ThrowVerifyError(shadow_frame.GetMethod()->GetDeclaringClass(),
516 "liberate-variable captured variable index %zu out of bounds",
517 lambda_closure->GetNumberOfCapturedVariables());
518 // Clear the destination vreg(s) to be safe.
519 shadow_frame.SetVReg(dest_vreg, 0);
520 if (shorty_type.IsPrimitiveWide() || shorty_type.IsLambda()) {
521 shadow_frame.SetVReg(dest_vreg + 1, 0);
522 }
523 return false;
524 }
525
526 // Verify that the runtime type of the captured-variable matches the requested dex type.
527 if (do_access_check) {
528 ShortyFieldType actual_type = lambda_closure->GetCapturedShortyType(captured_variable_index);
529 if (actual_type != shorty_type) {
530 ThrowVerifyError(shadow_frame.GetMethod()->GetDeclaringClass(),
531 "cannot liberate-variable of runtime type '%c' to dex type '%c'",
532 static_cast<char>(actual_type),
533 static_cast<char>(shorty_type));
534
535 shadow_frame.SetVReg(dest_vreg, 0);
536 if (shorty_type.IsPrimitiveWide() || shorty_type.IsLambda()) {
537 shadow_frame.SetVReg(dest_vreg + 1, 0);
538 }
539 return false;
540 }
541
542 if (actual_type.IsLambda() || actual_type.IsObject()) {
543 UNIMPLEMENTED(FATAL) << "liberate-variable type checks needs to "
544 << "parse full type descriptor for objects and lambdas";
545 }
546 }
547
548 // Unpack the captured variable from the closure into the correct type, then save it to the vreg.
549 if (shorty_type.IsPrimitiveNarrow()) {
550 uint32_t primitive_narrow_value =
551 lambda_closure->GetCapturedPrimitiveNarrow(captured_variable_index);
552 shadow_frame.SetVReg(dest_vreg, primitive_narrow_value);
553 } else if (shorty_type.IsPrimitiveWide()) {
554 uint64_t primitive_wide_value =
555 lambda_closure->GetCapturedPrimitiveWide(captured_variable_index);
556 shadow_frame.SetVRegLong(dest_vreg, static_cast<int64_t>(primitive_wide_value));
557 } else if (shorty_type.IsObject()) {
558 mirror::Object* unpacked_object =
559 lambda_closure->GetCapturedObject(captured_variable_index);
560 shadow_frame.SetVRegReference(dest_vreg, unpacked_object);
561
562 UNIMPLEMENTED(FATAL) << "liberate-variable cannot unpack objects yet";
563 } else if (shorty_type.IsLambda()) {
564 UNIMPLEMENTED(FATAL) << "liberate-variable cannot unpack lambdas yet";
565 } else {
566 LOG(FATAL) << "unreachable";
567 UNREACHABLE();
568 }
569
570 return true;
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700571}
572
Igor Murashkin158f35c2015-06-10 15:55:30 -0700573template<bool do_access_check>
574static inline bool DoInvokeLambda(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
575 uint16_t inst_data, JValue* result) {
576 /*
577 * invoke-lambda is opcode 0x25
578 *
579 * - vC is the closure register (both vC and vC + 1 will be used to store the closure).
580 * - vB is the number of additional registers up to |{vD,vE,vF,vG}| (4)
581 * - the rest of the registers are always var-args
582 *
583 * - reading var-args for 0x25 gets us vD,vE,vF,vG (but not vB)
584 */
Igor Murashkin6918bf12015-09-27 19:19:06 -0700585 uint32_t vreg_closure = inst->VRegC_25x();
586 const lambda::Closure* lambda_closure =
587 ReadLambdaClosureFromVRegsOrThrow(shadow_frame, vreg_closure);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700588
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700589 // Failed lambda target runtime check, an exception was raised.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700590 if (UNLIKELY(lambda_closure == nullptr)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700591 CHECK(self->IsExceptionPending());
592 result->SetJ(0);
593 return false;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700594 }
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700595
Igor Murashkin6918bf12015-09-27 19:19:06 -0700596 ArtMethod* const called_method = lambda_closure->GetTargetMethod();
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700597 // Invoke a non-range lambda
598 return DoLambdaCall<false, do_access_check>(called_method, self, shadow_frame, inst, inst_data,
599 result);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700600}
601
Igor Murashkin6918bf12015-09-27 19:19:06 -0700602// Handles invoke-XXX/range instructions (other than invoke-lambda[-range]).
Sebastien Hertzc6714852013-09-30 16:42:32 +0200603// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200604template<InvokeType type, bool is_range, bool do_access_check>
605static inline bool DoInvoke(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
606 uint16_t inst_data, JValue* result) {
607 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
608 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700609 Object* receiver = (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700610 ArtMethod* sf_method = shadow_frame.GetMethod();
Ian Rogerse94652f2014-12-02 11:13:19 -0800611 ArtMethod* const called_method = FindMethodFromCode<type, do_access_check>(
Andreas Gampe3a357142015-08-07 17:20:11 -0700612 method_idx, &receiver, sf_method, self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700613 // The shadow frame should already be pushed, so we don't need to update it.
Ian Rogerse94652f2014-12-02 11:13:19 -0800614 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200615 CHECK(self->IsExceptionPending());
616 result->SetJ(0);
617 return false;
Ian Rogerse94652f2014-12-02 11:13:19 -0800618 } else if (UNLIKELY(called_method->IsAbstract())) {
619 ThrowAbstractMethodError(called_method);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200620 result->SetJ(0);
621 return false;
622 } else {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100623 if (type == kVirtual || type == kInterface) {
624 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
625 if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) {
626 instrumentation->InvokeVirtualOrInterface(
627 self, receiver, sf_method, shadow_frame.GetDexPC(), called_method);
628 }
629 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800630 return DoCall<is_range, do_access_check>(called_method, self, shadow_frame, inst, inst_data,
631 result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200632 }
633}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200634
Sebastien Hertzc6714852013-09-30 16:42:32 +0200635// Handles invoke-virtual-quick and invoke-virtual-quick-range instructions.
636// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200637template<bool is_range>
638static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame,
639 const Instruction* inst, uint16_t inst_data,
640 JValue* result) {
641 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
642 Object* const receiver = shadow_frame.GetVRegReference(vregC);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200643 if (UNLIKELY(receiver == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200644 // We lost the reference to the method index so we cannot get a more
645 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000646 ThrowNullPointerExceptionFromDexPC();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200647 return false;
648 }
649 const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700650 CHECK(receiver->GetClass()->ShouldHaveEmbeddedImtAndVTable());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700651 ArtMethod* const called_method = receiver->GetClass()->GetEmbeddedVTableEntry(
652 vtable_idx, sizeof(void*));
Ian Rogerse94652f2014-12-02 11:13:19 -0800653 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200654 CHECK(self->IsExceptionPending());
655 result->SetJ(0);
656 return false;
Ian Rogerse94652f2014-12-02 11:13:19 -0800657 } else if (UNLIKELY(called_method->IsAbstract())) {
658 ThrowAbstractMethodError(called_method);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200659 result->SetJ(0);
660 return false;
661 } else {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100662 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
663 if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) {
664 instrumentation->InvokeVirtualOrInterface(
665 self, receiver, shadow_frame.GetMethod(), shadow_frame.GetDexPC(), called_method);
666 }
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200667 // No need to check since we've been quickened.
Ian Rogerse94652f2014-12-02 11:13:19 -0800668 return DoCall<is_range, false>(called_method, self, shadow_frame, inst, inst_data, result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200669 }
670}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200671
Sebastien Hertzc6714852013-09-30 16:42:32 +0200672// Handles iget-XXX and sget-XXX instructions.
673// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200674template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
Ian Rogers54874942014-06-10 16:31:03 -0700675bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
Mathieu Chartier90443472015-07-16 20:32:27 -0700676 uint16_t inst_data) SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200677
Sebastien Hertzc6714852013-09-30 16:42:32 +0200678// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
679// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200680template<Primitive::Type field_type>
Ian Rogers54874942014-06-10 16:31:03 -0700681bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
Mathieu Chartier90443472015-07-16 20:32:27 -0700682 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200683
Sebastien Hertzc6714852013-09-30 16:42:32 +0200684// Handles iput-XXX and sput-XXX instructions.
685// Returns true on success, otherwise throws an exception and returns false.
Ian Rogers54874942014-06-10 16:31:03 -0700686template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
687 bool transaction_active>
688bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
Mathieu Chartier90443472015-07-16 20:32:27 -0700689 uint16_t inst_data) SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200690
Sebastien Hertzc6714852013-09-30 16:42:32 +0200691// Handles iput-quick, iput-wide-quick and iput-object-quick instructions.
692// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100693template<Primitive::Type field_type, bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -0700694bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
Mathieu Chartier90443472015-07-16 20:32:27 -0700695 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers54874942014-06-10 16:31:03 -0700696
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200697
Sebastien Hertzc6714852013-09-30 16:42:32 +0200698// Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the
699// java.lang.String class is initialized.
Ian Rogers6786a582014-10-28 12:49:06 -0700700static inline String* ResolveString(Thread* self, ShadowFrame& shadow_frame, uint32_t string_idx)
Mathieu Chartier90443472015-07-16 20:32:27 -0700701 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200702 Class* java_lang_string_class = String::GetJavaLangString();
703 if (UNLIKELY(!java_lang_string_class->IsInitialized())) {
704 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700705 StackHandleScope<1> hs(self);
706 Handle<mirror::Class> h_class(hs.NewHandle(java_lang_string_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700707 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200708 DCHECK(self->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800709 return nullptr;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200710 }
711 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700712 ArtMethod* method = shadow_frame.GetMethod();
Mathieu Chartiereace4582014-11-24 18:29:54 -0800713 mirror::Class* declaring_class = method->GetDeclaringClass();
Vladimir Marko05792b92015-08-03 11:56:49 +0100714 // MethodVerifier refuses methods with string_idx out of bounds.
715 DCHECK_LT(string_idx, declaring_class->GetDexCache()->NumStrings());
716 mirror::String* s = declaring_class->GetDexCacheStrings()[string_idx].Read();
Ian Rogers6786a582014-10-28 12:49:06 -0700717 if (UNLIKELY(s == nullptr)) {
718 StackHandleScope<1> hs(self);
Mathieu Chartiereace4582014-11-24 18:29:54 -0800719 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
Ian Rogers6786a582014-10-28 12:49:06 -0700720 s = Runtime::Current()->GetClassLinker()->ResolveString(*method->GetDexFile(), string_idx,
721 dex_cache);
722 }
723 return s;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200724}
725
Sebastien Hertzc6714852013-09-30 16:42:32 +0200726// Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions.
727// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200728static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg,
729 int32_t dividend, int32_t divisor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700730 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700731 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200732 if (UNLIKELY(divisor == 0)) {
733 ThrowArithmeticExceptionDivideByZero();
734 return false;
735 }
736 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
737 shadow_frame.SetVReg(result_reg, kMinInt);
738 } else {
739 shadow_frame.SetVReg(result_reg, dividend / divisor);
740 }
741 return true;
742}
743
Sebastien Hertzc6714852013-09-30 16:42:32 +0200744// Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions.
745// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200746static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg,
747 int32_t dividend, int32_t divisor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700748 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700749 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200750 if (UNLIKELY(divisor == 0)) {
751 ThrowArithmeticExceptionDivideByZero();
752 return false;
753 }
754 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
755 shadow_frame.SetVReg(result_reg, 0);
756 } else {
757 shadow_frame.SetVReg(result_reg, dividend % divisor);
758 }
759 return true;
760}
761
Sebastien Hertzc6714852013-09-30 16:42:32 +0200762// Handles div-long and div-long-2addr instructions.
763// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200764static inline bool DoLongDivide(ShadowFrame& shadow_frame, size_t result_reg,
765 int64_t dividend, int64_t divisor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700766 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700767 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200768 if (UNLIKELY(divisor == 0)) {
769 ThrowArithmeticExceptionDivideByZero();
770 return false;
771 }
772 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
773 shadow_frame.SetVRegLong(result_reg, kMinLong);
774 } else {
775 shadow_frame.SetVRegLong(result_reg, dividend / divisor);
776 }
777 return true;
778}
779
Sebastien Hertzc6714852013-09-30 16:42:32 +0200780// Handles rem-long and rem-long-2addr instructions.
781// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200782static inline bool DoLongRemainder(ShadowFrame& shadow_frame, size_t result_reg,
783 int64_t dividend, int64_t divisor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700784 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700785 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200786 if (UNLIKELY(divisor == 0)) {
787 ThrowArithmeticExceptionDivideByZero();
788 return false;
789 }
790 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
791 shadow_frame.SetVRegLong(result_reg, 0);
792 } else {
793 shadow_frame.SetVRegLong(result_reg, dividend % divisor);
794 }
795 return true;
796}
797
Sebastien Hertzc6714852013-09-30 16:42:32 +0200798// Handles filled-new-array and filled-new-array-range instructions.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200799// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100800template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200801bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200802 Thread* self, JValue* result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200803
Sebastien Hertzc6714852013-09-30 16:42:32 +0200804// Handles packed-switch instruction.
805// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200806static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
807 uint16_t inst_data)
Mathieu Chartier90443472015-07-16 20:32:27 -0700808 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200809 DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH);
810 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200811 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200812 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
813 uint16_t size = switch_data[1];
David Brazdil2ef645b2015-06-17 18:20:52 +0100814 if (size == 0) {
815 // Empty packed switch, move forward by 3 (size of PACKED_SWITCH).
816 return 3;
817 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200818 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Roland Levillain14d90572015-07-16 10:52:26 +0100819 DCHECK_ALIGNED(keys, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200820 int32_t first_key = keys[0];
821 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
Roland Levillain14d90572015-07-16 10:52:26 +0100822 DCHECK_ALIGNED(targets, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200823 int32_t index = test_val - first_key;
824 if (index >= 0 && index < size) {
825 return targets[index];
826 } else {
827 // No corresponding value: move forward by 3 (size of PACKED_SWITCH).
828 return 3;
829 }
830}
831
Sebastien Hertzc6714852013-09-30 16:42:32 +0200832// Handles sparse-switch instruction.
833// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200834static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
835 uint16_t inst_data)
Mathieu Chartier90443472015-07-16 20:32:27 -0700836 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200837 DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH);
838 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200839 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200840 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
841 uint16_t size = switch_data[1];
Jeff Hao935e01a2015-03-20 19:44:35 -0700842 // Return length of SPARSE_SWITCH if size is 0.
843 if (size == 0) {
844 return 3;
845 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200846 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Roland Levillain14d90572015-07-16 10:52:26 +0100847 DCHECK_ALIGNED(keys, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200848 const int32_t* entries = keys + size;
Roland Levillain14d90572015-07-16 10:52:26 +0100849 DCHECK_ALIGNED(entries, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200850 int lo = 0;
851 int hi = size - 1;
852 while (lo <= hi) {
853 int mid = (lo + hi) / 2;
854 int32_t foundVal = keys[mid];
855 if (test_val < foundVal) {
856 hi = mid - 1;
857 } else if (test_val > foundVal) {
858 lo = mid + 1;
859 } else {
860 return entries[mid];
861 }
862 }
863 // No corresponding value: move forward by 3 (size of SPARSE_SWITCH).
864 return 3;
865}
866
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700867template <bool _do_check>
868static inline bool DoBoxLambda(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
Mathieu Chartier90443472015-07-16 20:32:27 -0700869 uint16_t inst_data) SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700870 /*
871 * box-lambda vA, vB /// opcode 0xf8, format 22x
872 * - vA is the target register where the Object representation of the closure will be stored into
873 * - vB is a closure (made by create-lambda)
874 * (also reads vB + 1)
875 */
876 uint32_t vreg_target_object = inst->VRegA_22x(inst_data);
877 uint32_t vreg_source_closure = inst->VRegB_22x();
878
Igor Murashkin6918bf12015-09-27 19:19:06 -0700879 lambda::Closure* lambda_closure = ReadLambdaClosureFromVRegsOrThrow(shadow_frame,
880 vreg_source_closure);
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700881
882 // Failed lambda target runtime check, an exception was raised.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700883 if (UNLIKELY(lambda_closure == nullptr)) {
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700884 CHECK(self->IsExceptionPending());
885 return false;
886 }
887
Igor Murashkine2facc52015-07-10 13:49:08 -0700888 mirror::Object* closure_as_object =
Igor Murashkin6918bf12015-09-27 19:19:06 -0700889 Runtime::Current()->GetLambdaBoxTable()->BoxLambda(lambda_closure);
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700890
Igor Murashkine2facc52015-07-10 13:49:08 -0700891 // Failed to box the lambda, an exception was raised.
892 if (UNLIKELY(closure_as_object == nullptr)) {
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700893 CHECK(self->IsExceptionPending());
894 return false;
895 }
896
Igor Murashkine2facc52015-07-10 13:49:08 -0700897 shadow_frame.SetVRegReference(vreg_target_object, closure_as_object);
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700898 return true;
899}
900
Mathieu Chartier90443472015-07-16 20:32:27 -0700901template <bool _do_check> SHARED_REQUIRES(Locks::mutator_lock_)
Igor Murashkine2facc52015-07-10 13:49:08 -0700902static inline bool DoUnboxLambda(Thread* self,
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700903 ShadowFrame& shadow_frame,
904 const Instruction* inst,
905 uint16_t inst_data) {
906 /*
907 * unbox-lambda vA, vB, [type id] /// opcode 0xf9, format 22c
908 * - vA is the target register where the closure will be written into
909 * (also writes vA + 1)
910 * - vB is the Object representation of the closure (made by box-lambda)
911 */
912 uint32_t vreg_target_closure = inst->VRegA_22c(inst_data);
913 uint32_t vreg_source_object = inst->VRegB_22c();
914
915 // Raise NullPointerException if object is null
916 mirror::Object* boxed_closure_object = shadow_frame.GetVRegReference(vreg_source_object);
917 if (UNLIKELY(boxed_closure_object == nullptr)) {
918 ThrowNullPointerExceptionFromInterpreter();
919 return false;
920 }
921
Igor Murashkin6918bf12015-09-27 19:19:06 -0700922 lambda::Closure* unboxed_closure = nullptr;
Igor Murashkine2facc52015-07-10 13:49:08 -0700923 // Raise an exception if unboxing fails.
924 if (!Runtime::Current()->GetLambdaBoxTable()->UnboxLambda(boxed_closure_object,
Igor Murashkin6918bf12015-09-27 19:19:06 -0700925 /*out*/&unboxed_closure)) {
Igor Murashkine2facc52015-07-10 13:49:08 -0700926 CHECK(self->IsExceptionPending());
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700927 return false;
928 }
929
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700930 DCHECK(unboxed_closure != nullptr);
Igor Murashkin6918bf12015-09-27 19:19:06 -0700931 WriteLambdaClosureIntoVRegs(/*inout*/shadow_frame, unboxed_closure, vreg_target_closure);
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700932 return true;
933}
934
Ian Rogers54874942014-06-10 16:31:03 -0700935uint32_t FindNextInstructionFollowingException(Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertz9f102032014-05-23 08:59:42 +0200936 uint32_t dex_pc, const instrumentation::Instrumentation* instrumentation)
Mathieu Chartier90443472015-07-16 20:32:27 -0700937 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200938
Andreas Gampe794ad762015-02-23 08:12:24 -0800939NO_RETURN void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame)
940 __attribute__((cold))
Mathieu Chartier90443472015-07-16 20:32:27 -0700941 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200942
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200943static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst,
Ian Rogerse94652f2014-12-02 11:13:19 -0800944 const uint32_t dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700945 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700946 constexpr bool kTracing = false;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200947 if (kTracing) {
948#define TRACE_LOG std::cerr
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700949 std::ostringstream oss;
950 oss << PrettyMethod(shadow_frame.GetMethod())
951 << StringPrintf("\n0x%x: ", dex_pc)
Ian Rogerse94652f2014-12-02 11:13:19 -0800952 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()) << "\n";
Ian Rogersef7d42f2014-01-06 12:55:46 -0800953 for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200954 uint32_t raw_value = shadow_frame.GetVReg(i);
955 Object* ref_value = shadow_frame.GetVRegReference(i);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800956 oss << StringPrintf(" vreg%u=0x%08X", i, raw_value);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700957 if (ref_value != nullptr) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200958 if (ref_value->GetClass()->IsStringClass() &&
Jeff Hao848f70a2014-01-15 13:49:50 -0800959 ref_value->AsString()->GetValue() != nullptr) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700960 oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\"";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200961 } else {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700962 oss << "/" << PrettyTypeOf(ref_value);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200963 }
964 }
965 }
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700966 TRACE_LOG << oss.str() << "\n";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200967#undef TRACE_LOG
968 }
969}
970
Sebastien Hertz1eda2262013-09-09 16:53:14 +0200971static inline bool IsBackwardBranch(int32_t branch_offset) {
972 return branch_offset <= 0;
973}
974
Sebastien Hertzc6714852013-09-30 16:42:32 +0200975// Explicitly instantiate all DoInvoke functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100976#define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700977 template SHARED_REQUIRES(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100978 bool DoInvoke<_type, _is_range, _do_check>(Thread* self, ShadowFrame& shadow_frame, \
979 const Instruction* inst, uint16_t inst_data, \
980 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200981
982#define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \
983 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \
984 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \
985 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \
986 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true);
987
Andreas Gampec8ccf682014-09-29 20:07:43 -0700988EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic) // invoke-static/range.
989EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect) // invoke-direct/range.
990EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual) // invoke-virtual/range.
991EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper) // invoke-super/range.
992EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface) // invoke-interface/range.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200993#undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL
994#undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL
995
Sebastien Hertzc6714852013-09-30 16:42:32 +0200996// Explicitly instantiate all DoInvokeVirtualQuick functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100997#define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700998 template SHARED_REQUIRES(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100999 bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \
1000 const Instruction* inst, uint16_t inst_data, \
1001 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +02001002
1003EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick.
1004EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range.
1005#undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK
1006
Igor Murashkin158f35c2015-06-10 15:55:30 -07001007// Explicitly instantiate all DoCreateLambda functions.
Igor Murashkin6918bf12015-09-27 19:19:06 -07001008#define EXPLICIT_DO_CREATE_LAMBDA_DECL(_do_check) \
1009template SHARED_REQUIRES(Locks::mutator_lock_) \
1010bool DoCreateLambda<_do_check>(Thread* self, \
1011 const Instruction* inst, \
1012 /*inout*/ShadowFrame& shadow_frame, \
1013 /*inout*/lambda::ClosureBuilder* closure_builder, \
1014 /*inout*/lambda::Closure* uninitialized_closure);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001015
1016EXPLICIT_DO_CREATE_LAMBDA_DECL(false); // create-lambda
1017EXPLICIT_DO_CREATE_LAMBDA_DECL(true); // create-lambda
1018#undef EXPLICIT_DO_CREATE_LAMBDA_DECL
1019
1020// Explicitly instantiate all DoInvokeLambda functions.
1021#define EXPLICIT_DO_INVOKE_LAMBDA_DECL(_do_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -07001022template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin158f35c2015-06-10 15:55:30 -07001023bool DoInvokeLambda<_do_check>(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1024 uint16_t inst_data, JValue* result);
1025
1026EXPLICIT_DO_INVOKE_LAMBDA_DECL(false); // invoke-lambda
1027EXPLICIT_DO_INVOKE_LAMBDA_DECL(true); // invoke-lambda
1028#undef EXPLICIT_DO_INVOKE_LAMBDA_DECL
1029
Igor Murashkin2ee54e22015-06-18 10:05:11 -07001030// Explicitly instantiate all DoBoxLambda functions.
1031#define EXPLICIT_DO_BOX_LAMBDA_DECL(_do_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -07001032template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin2ee54e22015-06-18 10:05:11 -07001033bool DoBoxLambda<_do_check>(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1034 uint16_t inst_data);
1035
1036EXPLICIT_DO_BOX_LAMBDA_DECL(false); // box-lambda
1037EXPLICIT_DO_BOX_LAMBDA_DECL(true); // box-lambda
1038#undef EXPLICIT_DO_BOX_LAMBDA_DECL
1039
1040// Explicitly instantiate all DoUnBoxLambda functions.
1041#define EXPLICIT_DO_UNBOX_LAMBDA_DECL(_do_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -07001042template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin2ee54e22015-06-18 10:05:11 -07001043bool DoUnboxLambda<_do_check>(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1044 uint16_t inst_data);
1045
1046EXPLICIT_DO_UNBOX_LAMBDA_DECL(false); // unbox-lambda
1047EXPLICIT_DO_UNBOX_LAMBDA_DECL(true); // unbox-lambda
1048#undef EXPLICIT_DO_BOX_LAMBDA_DECL
1049
Igor Murashkin6918bf12015-09-27 19:19:06 -07001050// Explicitly instantiate all DoCaptureVariable functions.
1051#define EXPLICIT_DO_CAPTURE_VARIABLE_DECL(_do_check) \
1052template SHARED_REQUIRES(Locks::mutator_lock_) \
1053bool DoCaptureVariable<_do_check>(Thread* self, \
1054 const Instruction* inst, \
1055 ShadowFrame& shadow_frame, \
1056 lambda::ClosureBuilder* closure_builder);
Sebastien Hertzc6714852013-09-30 16:42:32 +02001057
Igor Murashkin6918bf12015-09-27 19:19:06 -07001058EXPLICIT_DO_CAPTURE_VARIABLE_DECL(false); // capture-variable
1059EXPLICIT_DO_CAPTURE_VARIABLE_DECL(true); // capture-variable
1060#undef EXPLICIT_DO_CREATE_LAMBDA_DECL
1061
1062// Explicitly instantiate all DoLiberateVariable functions.
1063#define EXPLICIT_DO_LIBERATE_VARIABLE_DECL(_do_check) \
1064template SHARED_REQUIRES(Locks::mutator_lock_) \
1065bool DoLiberateVariable<_do_check>(Thread* self, \
1066 const Instruction* inst, \
1067 size_t captured_variable_index, \
1068 ShadowFrame& shadow_frame); \
1069
1070EXPLICIT_DO_LIBERATE_VARIABLE_DECL(false); // liberate-variable
1071EXPLICIT_DO_LIBERATE_VARIABLE_DECL(true); // liberate-variable
1072#undef EXPLICIT_DO_LIBERATE_LAMBDA_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001073} // namespace interpreter
1074} // namespace art
1075
1076#endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_