blob: 5db8cf79a3a177a0badcaa910ed818ec84115769 [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"
Sebastien Hertz8ece0502013-08-07 11:26:41 +020048#include "thread.h"
49#include "well_known_classes.h"
50
Mathieu Chartiere401d142015-04-22 13:56:20 -070051using ::art::ArtMethod;
Sebastien Hertz8ece0502013-08-07 11:26:41 +020052using ::art::mirror::Array;
53using ::art::mirror::BooleanArray;
54using ::art::mirror::ByteArray;
55using ::art::mirror::CharArray;
56using ::art::mirror::Class;
57using ::art::mirror::ClassLoader;
58using ::art::mirror::IntArray;
59using ::art::mirror::LongArray;
60using ::art::mirror::Object;
61using ::art::mirror::ObjectArray;
62using ::art::mirror::ShortArray;
63using ::art::mirror::String;
64using ::art::mirror::Throwable;
65
66namespace art {
67namespace interpreter {
68
69// External references to both interpreter implementations.
70
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010071template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -080072extern JValue ExecuteSwitchImpl(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzc6714852013-09-30 16:42:32 +020073 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020074
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010075template<bool do_access_check, bool transaction_active>
Ian Rogerse94652f2014-12-02 11:13:19 -080076extern JValue ExecuteGotoImpl(Thread* self, const DexFile::CodeItem* code_item,
Sebastien Hertzc6714852013-09-30 16:42:32 +020077 ShadowFrame& shadow_frame, JValue result_register);
Sebastien Hertz8ece0502013-08-07 11:26:41 +020078
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000079void ThrowNullPointerExceptionFromInterpreter()
Mathieu Chartier90443472015-07-16 20:32:27 -070080 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzda843e12014-05-28 19:28:31 +020081
Sebastien Hertz8ece0502013-08-07 11:26:41 +020082static inline void DoMonitorEnter(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
83 ref->MonitorEnter(self);
84}
85
86static inline void DoMonitorExit(Thread* self, Object* ref) NO_THREAD_SAFETY_ANALYSIS {
87 ref->MonitorExit(self);
88}
89
Sebastien Hertz45b15972015-04-03 16:07:05 +020090void AbortTransactionF(Thread* self, const char* fmt, ...)
91 __attribute__((__format__(__printf__, 2, 3)))
Mathieu Chartier90443472015-07-16 20:32:27 -070092 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020093
94void AbortTransactionV(Thread* self, const char* fmt, va_list args)
Mathieu Chartier90443472015-07-16 20:32:27 -070095 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierb2c7ead2014-04-29 11:13:16 -070096
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010097void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count)
Mathieu Chartier90443472015-07-16 20:32:27 -070098 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010099
Sebastien Hertzc6714852013-09-30 16:42:32 +0200100// Invokes the given method. This is part of the invocation support and is used by DoInvoke and
101// DoInvokeVirtualQuick functions.
102// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200103template<bool is_range, bool do_assignability_check>
Ian Rogerse94652f2014-12-02 11:13:19 -0800104bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200105 const Instruction* inst, uint16_t inst_data, JValue* result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200106
Igor Murashkin158f35c2015-06-10 15:55:30 -0700107// Invokes the given lambda closure. This is part of the invocation support and is used by
108// DoLambdaInvoke functions.
109// Returns true on success, otherwise throws an exception and returns false.
110template<bool is_range, bool do_assignability_check>
111bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame,
112 const Instruction* inst, uint16_t inst_data, JValue* result);
113
114// Validates that the art method corresponding to a lambda method target
115// is semantically valid:
116//
117// Must be ACC_STATIC and ACC_LAMBDA. Must be a concrete managed implementation
118// (i.e. not native, not proxy, not abstract, ...).
119//
120// If the validation fails, return false and raise an exception.
121static inline bool IsValidLambdaTargetOrThrow(ArtMethod* called_method)
Mathieu Chartier90443472015-07-16 20:32:27 -0700122 SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700123 bool success = false;
124
125 if (UNLIKELY(called_method == nullptr)) {
126 // The shadow frame should already be pushed, so we don't need to update it.
127 } else if (UNLIKELY(called_method->IsAbstract())) {
128 ThrowAbstractMethodError(called_method);
129 // TODO(iam): Also handle the case when the method is non-static, what error do we throw?
130 // TODO(iam): Also make sure that ACC_LAMBDA is set.
131 } else if (UNLIKELY(called_method->GetCodeItem() == nullptr)) {
132 // Method could be native, proxy method, etc. Lambda targets have to be concrete impls,
133 // so don't allow this.
134 } else {
135 success = true;
136 }
137
138 return success;
139}
140
Igor Murashkin6918bf12015-09-27 19:19:06 -0700141// Write out the 'Closure*' into vreg and vreg+1, as if it was a jlong.
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700142static inline void WriteLambdaClosureIntoVRegs(ShadowFrame& shadow_frame,
Igor Murashkin30c475a2015-10-06 13:59:43 -0700143 const lambda::Closure& lambda_closure,
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700144 uint32_t vreg) {
145 // Split the method into a lo and hi 32 bits so we can encode them into 2 virtual registers.
Igor Murashkin30c475a2015-10-06 13:59:43 -0700146 uint32_t closure_lo = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&lambda_closure));
147 uint32_t closure_hi = static_cast<uint32_t>(reinterpret_cast<uint64_t>(&lambda_closure)
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700148 >> BitSizeOf<uint32_t>());
149 // Use uint64_t instead of uintptr_t to allow shifting past the max on 32-bit.
150 static_assert(sizeof(uint64_t) >= sizeof(uintptr_t), "Impossible");
151
Igor Murashkin6918bf12015-09-27 19:19:06 -0700152 DCHECK_NE(closure_lo | closure_hi, 0u);
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700153
Igor Murashkin6918bf12015-09-27 19:19:06 -0700154 shadow_frame.SetVReg(vreg, closure_lo);
155 shadow_frame.SetVReg(vreg + 1, closure_hi);
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700156}
157
Igor Murashkin158f35c2015-06-10 15:55:30 -0700158// Handles create-lambda instructions.
159// Returns true on success, otherwise throws an exception and returns false.
160// (Exceptions are thrown by creating a new exception and then being put in the thread TLS)
161//
Igor Murashkin6918bf12015-09-27 19:19:06 -0700162// The closure must be allocated big enough to hold the data, and should not be
163// pre-initialized. It is initialized with the actual captured variables as a side-effect,
164// although this should be unimportant to the caller since this function also handles storing it to
165// the ShadowFrame.
166//
Igor Murashkin158f35c2015-06-10 15:55:30 -0700167// As a work-in-progress implementation, this shoves the ArtMethod object corresponding
168// to the target dex method index into the target register vA and vA + 1.
169template<bool do_access_check>
Igor Murashkin6918bf12015-09-27 19:19:06 -0700170static inline bool DoCreateLambda(Thread* self,
171 const Instruction* inst,
172 /*inout*/ShadowFrame& shadow_frame,
173 /*inout*/lambda::ClosureBuilder* closure_builder,
174 /*inout*/lambda::Closure* uninitialized_closure) {
175 DCHECK(closure_builder != nullptr);
176 DCHECK(uninitialized_closure != nullptr);
177 DCHECK_ALIGNED(uninitialized_closure, alignof(lambda::Closure));
178
Igor Murashkin30c475a2015-10-06 13:59:43 -0700179 using lambda::ArtLambdaMethod;
180 using lambda::LeakingAllocator;
181
Igor Murashkin158f35c2015-06-10 15:55:30 -0700182 /*
183 * create-lambda is opcode 0x21c
184 * - vA is the target register where the closure will be stored into
185 * (also stores into vA + 1)
186 * - vB is the method index which will be the target for a later invoke-lambda
187 */
188 const uint32_t method_idx = inst->VRegB_21c();
189 mirror::Object* receiver = nullptr; // Always static. (see 'kStatic')
190 ArtMethod* sf_method = shadow_frame.GetMethod();
191 ArtMethod* const called_method = FindMethodFromCode<kStatic, do_access_check>(
Andreas Gampe3a357142015-08-07 17:20:11 -0700192 method_idx, &receiver, sf_method, self);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700193
Igor Murashkin6918bf12015-09-27 19:19:06 -0700194 uint32_t vreg_dest_closure = inst->VRegA_21c();
Igor Murashkin158f35c2015-06-10 15:55:30 -0700195
196 if (UNLIKELY(!IsValidLambdaTargetOrThrow(called_method))) {
197 CHECK(self->IsExceptionPending());
Igor Murashkin6918bf12015-09-27 19:19:06 -0700198 shadow_frame.SetVReg(vreg_dest_closure, 0u);
199 shadow_frame.SetVReg(vreg_dest_closure + 1, 0u);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700200 return false;
201 }
202
Igor Murashkin30c475a2015-10-06 13:59:43 -0700203 ArtLambdaMethod* initialized_lambda_method;
Igor Murashkin6918bf12015-09-27 19:19:06 -0700204 // Initialize the ArtLambdaMethod with the right data.
205 {
Igor Murashkin30c475a2015-10-06 13:59:43 -0700206 // Allocate enough memory to store a well-aligned ArtLambdaMethod.
207 // This is not the final type yet since the data starts out uninitialized.
208 LeakingAllocator::AlignedMemoryStorage<ArtLambdaMethod>* uninitialized_lambda_method =
209 LeakingAllocator::AllocateMemory<ArtLambdaMethod>(self);
Igor Murashkin6918bf12015-09-27 19:19:06 -0700210
211 std::string captured_variables_shorty = closure_builder->GetCapturedVariableShortyTypes();
212 std::string captured_variables_long_type_desc;
213
214 // Synthesize a long type descriptor from the short one.
215 for (char shorty : captured_variables_shorty) {
216 lambda::ShortyFieldType shorty_field_type(shorty);
217 if (shorty_field_type.IsObject()) {
218 // Not the true type, but good enough until we implement verifier support.
219 captured_variables_long_type_desc += "Ljava/lang/Object;";
220 UNIMPLEMENTED(FATAL) << "create-lambda with an object captured variable";
221 } else if (shorty_field_type.IsLambda()) {
222 // Not the true type, but good enough until we implement verifier support.
223 captured_variables_long_type_desc += "Ljava/lang/Runnable;";
224 UNIMPLEMENTED(FATAL) << "create-lambda with a lambda captured variable";
225 } else {
226 // The primitive types have the same length shorty or not, so this is always correct.
227 DCHECK(shorty_field_type.IsPrimitive());
228 captured_variables_long_type_desc += shorty_field_type;
229 }
230 }
231
232 // Copy strings to dynamically allocated storage. This leaks, but that's ok. Fix it later.
233 // TODO: Strings need to come from the DexFile, so they won't need their own allocations.
Igor Murashkin30c475a2015-10-06 13:59:43 -0700234 char* captured_variables_type_desc = LeakingAllocator::MakeFlexibleInstance<char>(
Igor Murashkin6918bf12015-09-27 19:19:06 -0700235 self,
236 captured_variables_long_type_desc.size() + 1);
237 strcpy(captured_variables_type_desc, captured_variables_long_type_desc.c_str());
Igor Murashkin30c475a2015-10-06 13:59:43 -0700238 char* captured_variables_shorty_copy = LeakingAllocator::MakeFlexibleInstance<char>(
Igor Murashkin6918bf12015-09-27 19:19:06 -0700239 self,
240 captured_variables_shorty.size() + 1);
241 strcpy(captured_variables_shorty_copy, captured_variables_shorty.c_str());
242
Igor Murashkin30c475a2015-10-06 13:59:43 -0700243 // After initialization, the object at the storage is well-typed. Use strong type going forward.
244 initialized_lambda_method =
245 new (uninitialized_lambda_method) ArtLambdaMethod(called_method,
246 captured_variables_type_desc,
247 captured_variables_shorty_copy,
248 true); // innate lambda
Igor Murashkin6918bf12015-09-27 19:19:06 -0700249 }
250
251 // Write all the closure captured variables and the closure header into the closure.
Igor Murashkin30c475a2015-10-06 13:59:43 -0700252 lambda::Closure* initialized_closure =
253 closure_builder->CreateInPlace(uninitialized_closure, initialized_lambda_method);
Igor Murashkin6918bf12015-09-27 19:19:06 -0700254
Igor Murashkin30c475a2015-10-06 13:59:43 -0700255 WriteLambdaClosureIntoVRegs(/*inout*/shadow_frame, *initialized_closure, vreg_dest_closure);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700256 return true;
257}
258
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700259// Reads out the 'ArtMethod*' stored inside of vreg and vreg+1
260//
261// Validates that the art method points to a valid lambda function, otherwise throws
262// an exception and returns null.
263// (Exceptions are thrown by creating a new exception and then being put in the thread TLS)
Igor Murashkin6918bf12015-09-27 19:19:06 -0700264static inline lambda::Closure* ReadLambdaClosureFromVRegsOrThrow(ShadowFrame& shadow_frame,
265 uint32_t vreg)
Mathieu Chartier90443472015-07-16 20:32:27 -0700266 SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin6918bf12015-09-27 19:19:06 -0700267 // Lambda closures take up a consecutive pair of 2 virtual registers.
268 // On 32-bit the high bits are always 0.
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700269 uint32_t vc_value_lo = shadow_frame.GetVReg(vreg);
270 uint32_t vc_value_hi = shadow_frame.GetVReg(vreg + 1);
271
272 uint64_t vc_value_ptr = (static_cast<uint64_t>(vc_value_hi) << BitSizeOf<uint32_t>())
273 | vc_value_lo;
274
275 // Use uint64_t instead of uintptr_t to allow left-shifting past the max on 32-bit.
276 static_assert(sizeof(uint64_t) >= sizeof(uintptr_t), "Impossible");
Igor Murashkin6918bf12015-09-27 19:19:06 -0700277 lambda::Closure* const lambda_closure = reinterpret_cast<lambda::Closure*>(vc_value_ptr);
278 DCHECK_ALIGNED(lambda_closure, alignof(lambda::Closure));
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700279
280 // Guard against the user passing a null closure, which is odd but (sadly) semantically valid.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700281 if (UNLIKELY(lambda_closure == nullptr)) {
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700282 ThrowNullPointerExceptionFromInterpreter();
283 return nullptr;
Igor Murashkin6918bf12015-09-27 19:19:06 -0700284 } else if (UNLIKELY(!IsValidLambdaTargetOrThrow(lambda_closure->GetTargetMethod()))) {
285 // Sanity check against data corruption.
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700286 return nullptr;
287 }
288
Igor Murashkin6918bf12015-09-27 19:19:06 -0700289 return lambda_closure;
290}
291
292// Forward declaration for lock annotations. See below for documentation.
293template <bool do_access_check>
294static inline const char* GetStringDataByDexStringIndexOrThrow(ShadowFrame& shadow_frame,
295 uint32_t string_idx)
296 SHARED_REQUIRES(Locks::mutator_lock_);
297
298// Find the c-string data corresponding to a dex file's string index.
299// Otherwise, returns null if not found and throws a VerifyError.
300//
301// Note that with do_access_check=false, we never return null because the verifier
302// must guard against invalid string indices.
303// (Exceptions are thrown by creating a new exception and then being put in the thread TLS)
304template <bool do_access_check>
305static inline const char* GetStringDataByDexStringIndexOrThrow(ShadowFrame& shadow_frame,
306 uint32_t string_idx) {
307 ArtMethod* method = shadow_frame.GetMethod();
308 const DexFile* dex_file = method->GetDexFile();
309
310 mirror::Class* declaring_class = method->GetDeclaringClass();
311 if (!do_access_check) {
312 // MethodVerifier refuses methods with string_idx out of bounds.
313 DCHECK_LT(string_idx, declaring_class->GetDexCache()->NumStrings());
314 } else {
315 // Access checks enabled: perform string index bounds ourselves.
316 if (string_idx >= dex_file->GetHeader().string_ids_size_) {
317 ThrowVerifyError(declaring_class, "String index '%" PRIu32 "' out of bounds",
318 string_idx);
319 return nullptr;
320 }
321 }
322
323 const char* type_string = dex_file->StringDataByIdx(string_idx);
324
325 if (UNLIKELY(type_string == nullptr)) {
326 CHECK_EQ(false, do_access_check)
327 << " verifier should've caught invalid string index " << string_idx;
328 CHECK_EQ(true, do_access_check)
329 << " string idx size check should've caught invalid string index " << string_idx;
330 }
331
332 return type_string;
333}
334
335// Handles capture-variable instructions.
336// Returns true on success, otherwise throws an exception and returns false.
337// (Exceptions are thrown by creating a new exception and then being put in the thread TLS)
338template<bool do_access_check>
339static inline bool DoCaptureVariable(Thread* self,
340 const Instruction* inst,
341 /*inout*/ShadowFrame& shadow_frame,
342 /*inout*/lambda::ClosureBuilder* closure_builder) {
343 DCHECK(closure_builder != nullptr);
344 using lambda::ShortyFieldType;
345 /*
346 * capture-variable is opcode 0xf6, fmt 0x21c
347 * - vA is the source register of the variable that will be captured
348 * - vB is the string ID of the variable's type that will be captured
349 */
350 const uint32_t source_vreg = inst->VRegA_21c();
351 const uint32_t string_idx = inst->VRegB_21c();
352 // TODO: this should be a proper [type id] instead of a [string ID] pointing to a type.
353
354 const char* type_string = GetStringDataByDexStringIndexOrThrow<do_access_check>(shadow_frame,
355 string_idx);
356 if (UNLIKELY(type_string == nullptr)) {
357 CHECK(self->IsExceptionPending());
358 return false;
359 }
360
361 char type_first_letter = type_string[0];
362 ShortyFieldType shorty_type;
363 if (do_access_check &&
364 UNLIKELY(!ShortyFieldType::MaybeCreate(type_first_letter, /*out*/&shorty_type))) { // NOLINT: [whitespace/comma] [3]
365 ThrowVerifyError(shadow_frame.GetMethod()->GetDeclaringClass(),
366 "capture-variable vB must be a valid type");
367 return false;
368 } else {
369 // Already verified that the type is valid.
370 shorty_type = ShortyFieldType(type_first_letter);
371 }
372
373 const size_t captured_variable_count = closure_builder->GetCaptureCount();
374
375 // Note: types are specified explicitly so that the closure is packed tightly.
376 switch (shorty_type) {
377 case ShortyFieldType::kBoolean: {
378 uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg);
379 closure_builder->CaptureVariablePrimitive<bool>(primitive_narrow_value);
380 break;
381 }
382 case ShortyFieldType::kByte: {
383 uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg);
384 closure_builder->CaptureVariablePrimitive<int8_t>(primitive_narrow_value);
385 break;
386 }
387 case ShortyFieldType::kChar: {
388 uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg);
389 closure_builder->CaptureVariablePrimitive<uint16_t>(primitive_narrow_value);
390 break;
391 }
392 case ShortyFieldType::kShort: {
393 uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg);
394 closure_builder->CaptureVariablePrimitive<int16_t>(primitive_narrow_value);
395 break;
396 }
397 case ShortyFieldType::kInt: {
398 uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg);
399 closure_builder->CaptureVariablePrimitive<int32_t>(primitive_narrow_value);
400 break;
401 }
402 case ShortyFieldType::kDouble: {
403 closure_builder->CaptureVariablePrimitive(shadow_frame.GetVRegDouble(source_vreg));
404 break;
405 }
406 case ShortyFieldType::kFloat: {
407 closure_builder->CaptureVariablePrimitive(shadow_frame.GetVRegFloat(source_vreg));
408 break;
409 }
410 case ShortyFieldType::kLambda: {
411 UNIMPLEMENTED(FATAL) << " capture-variable with type kLambda";
412 // TODO: Capturing lambdas recursively will be done at a later time.
413 UNREACHABLE();
414 }
415 case ShortyFieldType::kLong: {
416 closure_builder->CaptureVariablePrimitive(shadow_frame.GetVRegLong(source_vreg));
417 break;
418 }
419 case ShortyFieldType::kObject: {
420 closure_builder->CaptureVariableObject(shadow_frame.GetVRegReference(source_vreg));
421 UNIMPLEMENTED(FATAL) << " capture-variable with type kObject";
422 // TODO: finish implementing this. disabled for now since we can't track lambda refs for GC.
423 UNREACHABLE();
424 }
425
426 default:
427 LOG(FATAL) << "Invalid shorty type value " << shorty_type;
428 UNREACHABLE();
429 }
430
431 DCHECK_EQ(captured_variable_count + 1, closure_builder->GetCaptureCount());
432
433 return true;
434}
435
436// Handles capture-variable instructions.
437// Returns true on success, otherwise throws an exception and returns false.
438// (Exceptions are thrown by creating a new exception and then being put in the thread TLS)
439template<bool do_access_check>
440static inline bool DoLiberateVariable(Thread* self,
441 const Instruction* inst,
442 size_t captured_variable_index,
443 /*inout*/ShadowFrame& shadow_frame) {
444 using lambda::ShortyFieldType;
445 /*
446 * liberate-variable is opcode 0xf7, fmt 0x22c
447 * - vA is the destination register
448 * - vB is the register with the lambda closure in it
449 * - vC is the string ID which needs to be a valid field type descriptor
450 */
451
452 const uint32_t dest_vreg = inst->VRegA_22c();
453 const uint32_t closure_vreg = inst->VRegB_22c();
454 const uint32_t string_idx = inst->VRegC_22c();
455 // TODO: this should be a proper [type id] instead of a [string ID] pointing to a type.
456
457
458 // Synthesize a long type descriptor from a shorty type descriptor list.
459 // TODO: Fix the dex encoding to contain the long and short type descriptors.
460 const char* type_string = GetStringDataByDexStringIndexOrThrow<do_access_check>(shadow_frame,
461 string_idx);
462 if (UNLIKELY(do_access_check && type_string == nullptr)) {
463 CHECK(self->IsExceptionPending());
464 shadow_frame.SetVReg(dest_vreg, 0);
465 return false;
466 }
467
468 char type_first_letter = type_string[0];
469 ShortyFieldType shorty_type;
470 if (do_access_check &&
471 UNLIKELY(!ShortyFieldType::MaybeCreate(type_first_letter, /*out*/&shorty_type))) { // NOLINT: [whitespace/comma] [3]
472 ThrowVerifyError(shadow_frame.GetMethod()->GetDeclaringClass(),
473 "liberate-variable vC must be a valid type");
474 shadow_frame.SetVReg(dest_vreg, 0);
475 return false;
476 } else {
477 // Already verified that the type is valid.
478 shorty_type = ShortyFieldType(type_first_letter);
479 }
480
481 // Check for closure being null *after* the type check.
482 // This way we can access the type info in case we fail later, to know how many vregs to clear.
483 const lambda::Closure* lambda_closure =
484 ReadLambdaClosureFromVRegsOrThrow(/*inout*/shadow_frame, closure_vreg);
485
486 // Failed lambda target runtime check, an exception was raised.
487 if (UNLIKELY(lambda_closure == nullptr)) {
488 CHECK(self->IsExceptionPending());
489
490 // Clear the destination vreg(s) to be safe.
491 shadow_frame.SetVReg(dest_vreg, 0);
492 if (shorty_type.IsPrimitiveWide() || shorty_type.IsLambda()) {
493 shadow_frame.SetVReg(dest_vreg + 1, 0);
494 }
495 return false;
496 }
497
498 if (do_access_check &&
499 UNLIKELY(captured_variable_index >= lambda_closure->GetNumberOfCapturedVariables())) {
500 ThrowVerifyError(shadow_frame.GetMethod()->GetDeclaringClass(),
501 "liberate-variable captured variable index %zu out of bounds",
502 lambda_closure->GetNumberOfCapturedVariables());
503 // Clear the destination vreg(s) to be safe.
504 shadow_frame.SetVReg(dest_vreg, 0);
505 if (shorty_type.IsPrimitiveWide() || shorty_type.IsLambda()) {
506 shadow_frame.SetVReg(dest_vreg + 1, 0);
507 }
508 return false;
509 }
510
511 // Verify that the runtime type of the captured-variable matches the requested dex type.
512 if (do_access_check) {
513 ShortyFieldType actual_type = lambda_closure->GetCapturedShortyType(captured_variable_index);
514 if (actual_type != shorty_type) {
515 ThrowVerifyError(shadow_frame.GetMethod()->GetDeclaringClass(),
516 "cannot liberate-variable of runtime type '%c' to dex type '%c'",
517 static_cast<char>(actual_type),
518 static_cast<char>(shorty_type));
519
520 shadow_frame.SetVReg(dest_vreg, 0);
521 if (shorty_type.IsPrimitiveWide() || shorty_type.IsLambda()) {
522 shadow_frame.SetVReg(dest_vreg + 1, 0);
523 }
524 return false;
525 }
526
527 if (actual_type.IsLambda() || actual_type.IsObject()) {
528 UNIMPLEMENTED(FATAL) << "liberate-variable type checks needs to "
529 << "parse full type descriptor for objects and lambdas";
530 }
531 }
532
533 // Unpack the captured variable from the closure into the correct type, then save it to the vreg.
534 if (shorty_type.IsPrimitiveNarrow()) {
535 uint32_t primitive_narrow_value =
536 lambda_closure->GetCapturedPrimitiveNarrow(captured_variable_index);
537 shadow_frame.SetVReg(dest_vreg, primitive_narrow_value);
538 } else if (shorty_type.IsPrimitiveWide()) {
539 uint64_t primitive_wide_value =
540 lambda_closure->GetCapturedPrimitiveWide(captured_variable_index);
541 shadow_frame.SetVRegLong(dest_vreg, static_cast<int64_t>(primitive_wide_value));
542 } else if (shorty_type.IsObject()) {
543 mirror::Object* unpacked_object =
544 lambda_closure->GetCapturedObject(captured_variable_index);
545 shadow_frame.SetVRegReference(dest_vreg, unpacked_object);
546
547 UNIMPLEMENTED(FATAL) << "liberate-variable cannot unpack objects yet";
548 } else if (shorty_type.IsLambda()) {
549 UNIMPLEMENTED(FATAL) << "liberate-variable cannot unpack lambdas yet";
550 } else {
551 LOG(FATAL) << "unreachable";
552 UNREACHABLE();
553 }
554
555 return true;
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700556}
557
Igor Murashkin158f35c2015-06-10 15:55:30 -0700558template<bool do_access_check>
559static inline bool DoInvokeLambda(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
560 uint16_t inst_data, JValue* result) {
561 /*
562 * invoke-lambda is opcode 0x25
563 *
564 * - vC is the closure register (both vC and vC + 1 will be used to store the closure).
565 * - vB is the number of additional registers up to |{vD,vE,vF,vG}| (4)
566 * - the rest of the registers are always var-args
567 *
568 * - reading var-args for 0x25 gets us vD,vE,vF,vG (but not vB)
569 */
Igor Murashkin6918bf12015-09-27 19:19:06 -0700570 uint32_t vreg_closure = inst->VRegC_25x();
571 const lambda::Closure* lambda_closure =
572 ReadLambdaClosureFromVRegsOrThrow(shadow_frame, vreg_closure);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700573
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700574 // Failed lambda target runtime check, an exception was raised.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700575 if (UNLIKELY(lambda_closure == nullptr)) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700576 CHECK(self->IsExceptionPending());
577 result->SetJ(0);
578 return false;
Igor Murashkin158f35c2015-06-10 15:55:30 -0700579 }
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700580
Igor Murashkin6918bf12015-09-27 19:19:06 -0700581 ArtMethod* const called_method = lambda_closure->GetTargetMethod();
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700582 // Invoke a non-range lambda
583 return DoLambdaCall<false, do_access_check>(called_method, self, shadow_frame, inst, inst_data,
584 result);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700585}
586
Igor Murashkin6918bf12015-09-27 19:19:06 -0700587// Handles invoke-XXX/range instructions (other than invoke-lambda[-range]).
Sebastien Hertzc6714852013-09-30 16:42:32 +0200588// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200589template<InvokeType type, bool is_range, bool do_access_check>
590static inline bool DoInvoke(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
591 uint16_t inst_data, JValue* result) {
592 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
593 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700594 Object* receiver = (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700595 ArtMethod* sf_method = shadow_frame.GetMethod();
Ian Rogerse94652f2014-12-02 11:13:19 -0800596 ArtMethod* const called_method = FindMethodFromCode<type, do_access_check>(
Andreas Gampe3a357142015-08-07 17:20:11 -0700597 method_idx, &receiver, sf_method, self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700598 // The shadow frame should already be pushed, so we don't need to update it.
Ian Rogerse94652f2014-12-02 11:13:19 -0800599 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200600 CHECK(self->IsExceptionPending());
601 result->SetJ(0);
602 return false;
Ian Rogerse94652f2014-12-02 11:13:19 -0800603 } else if (UNLIKELY(called_method->IsAbstract())) {
604 ThrowAbstractMethodError(called_method);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200605 result->SetJ(0);
606 return false;
607 } else {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100608 if (type == kVirtual || type == kInterface) {
609 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
610 if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) {
611 instrumentation->InvokeVirtualOrInterface(
612 self, receiver, sf_method, shadow_frame.GetDexPC(), called_method);
613 }
614 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800615 return DoCall<is_range, do_access_check>(called_method, self, shadow_frame, inst, inst_data,
616 result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200617 }
618}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200619
Sebastien Hertzc6714852013-09-30 16:42:32 +0200620// Handles invoke-virtual-quick and invoke-virtual-quick-range instructions.
621// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200622template<bool is_range>
623static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame,
624 const Instruction* inst, uint16_t inst_data,
625 JValue* result) {
626 const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
627 Object* const receiver = shadow_frame.GetVRegReference(vregC);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200628 if (UNLIKELY(receiver == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200629 // We lost the reference to the method index so we cannot get a more
630 // precised exception message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000631 ThrowNullPointerExceptionFromDexPC();
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200632 return false;
633 }
634 const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700635 CHECK(receiver->GetClass()->ShouldHaveEmbeddedImtAndVTable());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700636 ArtMethod* const called_method = receiver->GetClass()->GetEmbeddedVTableEntry(
637 vtable_idx, sizeof(void*));
Ian Rogerse94652f2014-12-02 11:13:19 -0800638 if (UNLIKELY(called_method == nullptr)) {
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200639 CHECK(self->IsExceptionPending());
640 result->SetJ(0);
641 return false;
Ian Rogerse94652f2014-12-02 11:13:19 -0800642 } else if (UNLIKELY(called_method->IsAbstract())) {
643 ThrowAbstractMethodError(called_method);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200644 result->SetJ(0);
645 return false;
646 } else {
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100647 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
648 if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) {
649 instrumentation->InvokeVirtualOrInterface(
650 self, receiver, shadow_frame.GetMethod(), shadow_frame.GetDexPC(), called_method);
651 }
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200652 // No need to check since we've been quickened.
Ian Rogerse94652f2014-12-02 11:13:19 -0800653 return DoCall<is_range, false>(called_method, self, shadow_frame, inst, inst_data, result);
Sebastien Hertzc61124b2013-09-10 11:44:19 +0200654 }
655}
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200656
Sebastien Hertzc6714852013-09-30 16:42:32 +0200657// Handles iget-XXX and sget-XXX instructions.
658// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200659template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check>
Ian Rogers54874942014-06-10 16:31:03 -0700660bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
Mathieu Chartier90443472015-07-16 20:32:27 -0700661 uint16_t inst_data) SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200662
Sebastien Hertzc6714852013-09-30 16:42:32 +0200663// Handles iget-quick, iget-wide-quick and iget-object-quick instructions.
664// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200665template<Primitive::Type field_type>
Ian Rogers54874942014-06-10 16:31:03 -0700666bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
Mathieu Chartier90443472015-07-16 20:32:27 -0700667 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +0200668
Sebastien Hertzc6714852013-09-30 16:42:32 +0200669// Handles iput-XXX and sput-XXX instructions.
670// Returns true on success, otherwise throws an exception and returns false.
Ian Rogers54874942014-06-10 16:31:03 -0700671template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check,
672 bool transaction_active>
673bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst,
Mathieu Chartier90443472015-07-16 20:32:27 -0700674 uint16_t inst_data) SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200675
Sebastien Hertzc6714852013-09-30 16:42:32 +0200676// Handles iput-quick, iput-wide-quick and iput-object-quick instructions.
677// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100678template<Primitive::Type field_type, bool transaction_active>
Ian Rogers54874942014-06-10 16:31:03 -0700679bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data)
Mathieu Chartier90443472015-07-16 20:32:27 -0700680 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers54874942014-06-10 16:31:03 -0700681
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200682
Sebastien Hertzc6714852013-09-30 16:42:32 +0200683// Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the
684// java.lang.String class is initialized.
Ian Rogers6786a582014-10-28 12:49:06 -0700685static inline String* ResolveString(Thread* self, ShadowFrame& shadow_frame, uint32_t string_idx)
Mathieu Chartier90443472015-07-16 20:32:27 -0700686 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200687 Class* java_lang_string_class = String::GetJavaLangString();
688 if (UNLIKELY(!java_lang_string_class->IsInitialized())) {
689 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700690 StackHandleScope<1> hs(self);
691 Handle<mirror::Class> h_class(hs.NewHandle(java_lang_string_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700692 if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200693 DCHECK(self->IsExceptionPending());
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800694 return nullptr;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200695 }
696 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700697 ArtMethod* method = shadow_frame.GetMethod();
Mathieu Chartiereace4582014-11-24 18:29:54 -0800698 mirror::Class* declaring_class = method->GetDeclaringClass();
Vladimir Marko05792b92015-08-03 11:56:49 +0100699 // MethodVerifier refuses methods with string_idx out of bounds.
700 DCHECK_LT(string_idx, declaring_class->GetDexCache()->NumStrings());
701 mirror::String* s = declaring_class->GetDexCacheStrings()[string_idx].Read();
Ian Rogers6786a582014-10-28 12:49:06 -0700702 if (UNLIKELY(s == nullptr)) {
703 StackHandleScope<1> hs(self);
Mathieu Chartiereace4582014-11-24 18:29:54 -0800704 Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
Ian Rogers6786a582014-10-28 12:49:06 -0700705 s = Runtime::Current()->GetClassLinker()->ResolveString(*method->GetDexFile(), string_idx,
706 dex_cache);
707 }
708 return s;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200709}
710
Sebastien Hertzc6714852013-09-30 16:42:32 +0200711// Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions.
712// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200713static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg,
714 int32_t dividend, int32_t divisor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700715 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700716 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200717 if (UNLIKELY(divisor == 0)) {
718 ThrowArithmeticExceptionDivideByZero();
719 return false;
720 }
721 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
722 shadow_frame.SetVReg(result_reg, kMinInt);
723 } else {
724 shadow_frame.SetVReg(result_reg, dividend / divisor);
725 }
726 return true;
727}
728
Sebastien Hertzc6714852013-09-30 16:42:32 +0200729// Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions.
730// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200731static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg,
732 int32_t dividend, int32_t divisor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700733 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersf72a11d2014-10-30 15:41:08 -0700734 constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200735 if (UNLIKELY(divisor == 0)) {
736 ThrowArithmeticExceptionDivideByZero();
737 return false;
738 }
739 if (UNLIKELY(dividend == kMinInt && divisor == -1)) {
740 shadow_frame.SetVReg(result_reg, 0);
741 } else {
742 shadow_frame.SetVReg(result_reg, dividend % divisor);
743 }
744 return true;
745}
746
Sebastien Hertzc6714852013-09-30 16:42:32 +0200747// Handles div-long and div-long-2addr instructions.
748// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200749static inline bool DoLongDivide(ShadowFrame& shadow_frame, size_t result_reg,
750 int64_t dividend, int64_t divisor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700751 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700752 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200753 if (UNLIKELY(divisor == 0)) {
754 ThrowArithmeticExceptionDivideByZero();
755 return false;
756 }
757 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
758 shadow_frame.SetVRegLong(result_reg, kMinLong);
759 } else {
760 shadow_frame.SetVRegLong(result_reg, dividend / divisor);
761 }
762 return true;
763}
764
Sebastien Hertzc6714852013-09-30 16:42:32 +0200765// Handles rem-long and rem-long-2addr instructions.
766// Returns true on success, otherwise throws a java.lang.ArithmeticException and return false.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200767static inline bool DoLongRemainder(ShadowFrame& shadow_frame, size_t result_reg,
768 int64_t dividend, int64_t divisor)
Mathieu Chartier90443472015-07-16 20:32:27 -0700769 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2e2deeb2013-09-23 11:58:57 -0700770 const int64_t kMinLong = std::numeric_limits<int64_t>::min();
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200771 if (UNLIKELY(divisor == 0)) {
772 ThrowArithmeticExceptionDivideByZero();
773 return false;
774 }
775 if (UNLIKELY(dividend == kMinLong && divisor == -1)) {
776 shadow_frame.SetVRegLong(result_reg, 0);
777 } else {
778 shadow_frame.SetVRegLong(result_reg, dividend % divisor);
779 }
780 return true;
781}
782
Sebastien Hertzc6714852013-09-30 16:42:32 +0200783// Handles filled-new-array and filled-new-array-range instructions.
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200784// Returns true on success, otherwise throws an exception and returns false.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100785template <bool is_range, bool do_access_check, bool transaction_active>
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200786bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame,
Sebastien Hertzc6714852013-09-30 16:42:32 +0200787 Thread* self, JValue* result);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200788
Sebastien Hertzc6714852013-09-30 16:42:32 +0200789// Handles packed-switch instruction.
790// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200791static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
792 uint16_t inst_data)
Mathieu Chartier90443472015-07-16 20:32:27 -0700793 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200794 DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH);
795 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200796 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200797 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature));
798 uint16_t size = switch_data[1];
David Brazdil2ef645b2015-06-17 18:20:52 +0100799 if (size == 0) {
800 // Empty packed switch, move forward by 3 (size of PACKED_SWITCH).
801 return 3;
802 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200803 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Roland Levillain14d90572015-07-16 10:52:26 +0100804 DCHECK_ALIGNED(keys, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200805 int32_t first_key = keys[0];
806 const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]);
Roland Levillain14d90572015-07-16 10:52:26 +0100807 DCHECK_ALIGNED(targets, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200808 int32_t index = test_val - first_key;
809 if (index >= 0 && index < size) {
810 return targets[index];
811 } else {
812 // No corresponding value: move forward by 3 (size of PACKED_SWITCH).
813 return 3;
814 }
815}
816
Sebastien Hertzc6714852013-09-30 16:42:32 +0200817// Handles sparse-switch instruction.
818// Returns the branch offset to the next instruction to execute.
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200819static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame,
820 uint16_t inst_data)
Mathieu Chartier90443472015-07-16 20:32:27 -0700821 SHARED_REQUIRES(Locks::mutator_lock_) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200822 DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH);
823 const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t();
Sebastien Hertz3b588e02013-09-11 14:33:18 +0200824 int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data));
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200825 DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature));
826 uint16_t size = switch_data[1];
Jeff Hao935e01a2015-03-20 19:44:35 -0700827 // Return length of SPARSE_SWITCH if size is 0.
828 if (size == 0) {
829 return 3;
830 }
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200831 const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]);
Roland Levillain14d90572015-07-16 10:52:26 +0100832 DCHECK_ALIGNED(keys, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200833 const int32_t* entries = keys + size;
Roland Levillain14d90572015-07-16 10:52:26 +0100834 DCHECK_ALIGNED(entries, 4);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200835 int lo = 0;
836 int hi = size - 1;
837 while (lo <= hi) {
838 int mid = (lo + hi) / 2;
839 int32_t foundVal = keys[mid];
840 if (test_val < foundVal) {
841 hi = mid - 1;
842 } else if (test_val > foundVal) {
843 lo = mid + 1;
844 } else {
845 return entries[mid];
846 }
847 }
848 // No corresponding value: move forward by 3 (size of SPARSE_SWITCH).
849 return 3;
850}
851
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700852template <bool _do_check>
853static inline bool DoBoxLambda(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst,
Mathieu Chartier90443472015-07-16 20:32:27 -0700854 uint16_t inst_data) SHARED_REQUIRES(Locks::mutator_lock_) {
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700855 /*
856 * box-lambda vA, vB /// opcode 0xf8, format 22x
857 * - vA is the target register where the Object representation of the closure will be stored into
858 * - vB is a closure (made by create-lambda)
859 * (also reads vB + 1)
860 */
861 uint32_t vreg_target_object = inst->VRegA_22x(inst_data);
862 uint32_t vreg_source_closure = inst->VRegB_22x();
863
Igor Murashkin6918bf12015-09-27 19:19:06 -0700864 lambda::Closure* lambda_closure = ReadLambdaClosureFromVRegsOrThrow(shadow_frame,
865 vreg_source_closure);
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700866
867 // Failed lambda target runtime check, an exception was raised.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700868 if (UNLIKELY(lambda_closure == nullptr)) {
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700869 CHECK(self->IsExceptionPending());
870 return false;
871 }
872
Igor Murashkine2facc52015-07-10 13:49:08 -0700873 mirror::Object* closure_as_object =
Igor Murashkin6918bf12015-09-27 19:19:06 -0700874 Runtime::Current()->GetLambdaBoxTable()->BoxLambda(lambda_closure);
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700875
Igor Murashkine2facc52015-07-10 13:49:08 -0700876 // Failed to box the lambda, an exception was raised.
877 if (UNLIKELY(closure_as_object == nullptr)) {
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700878 CHECK(self->IsExceptionPending());
879 return false;
880 }
881
Igor Murashkine2facc52015-07-10 13:49:08 -0700882 shadow_frame.SetVRegReference(vreg_target_object, closure_as_object);
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700883 return true;
884}
885
Mathieu Chartier90443472015-07-16 20:32:27 -0700886template <bool _do_check> SHARED_REQUIRES(Locks::mutator_lock_)
Igor Murashkine2facc52015-07-10 13:49:08 -0700887static inline bool DoUnboxLambda(Thread* self,
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700888 ShadowFrame& shadow_frame,
889 const Instruction* inst,
890 uint16_t inst_data) {
891 /*
892 * unbox-lambda vA, vB, [type id] /// opcode 0xf9, format 22c
893 * - vA is the target register where the closure will be written into
894 * (also writes vA + 1)
895 * - vB is the Object representation of the closure (made by box-lambda)
896 */
897 uint32_t vreg_target_closure = inst->VRegA_22c(inst_data);
898 uint32_t vreg_source_object = inst->VRegB_22c();
899
900 // Raise NullPointerException if object is null
901 mirror::Object* boxed_closure_object = shadow_frame.GetVRegReference(vreg_source_object);
902 if (UNLIKELY(boxed_closure_object == nullptr)) {
903 ThrowNullPointerExceptionFromInterpreter();
904 return false;
905 }
906
Igor Murashkin6918bf12015-09-27 19:19:06 -0700907 lambda::Closure* unboxed_closure = nullptr;
Igor Murashkine2facc52015-07-10 13:49:08 -0700908 // Raise an exception if unboxing fails.
909 if (!Runtime::Current()->GetLambdaBoxTable()->UnboxLambda(boxed_closure_object,
Igor Murashkin6918bf12015-09-27 19:19:06 -0700910 /*out*/&unboxed_closure)) {
Igor Murashkine2facc52015-07-10 13:49:08 -0700911 CHECK(self->IsExceptionPending());
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700912 return false;
913 }
914
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700915 DCHECK(unboxed_closure != nullptr);
Igor Murashkin30c475a2015-10-06 13:59:43 -0700916 WriteLambdaClosureIntoVRegs(/*inout*/shadow_frame, *unboxed_closure, vreg_target_closure);
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700917 return true;
918}
919
Ian Rogers54874942014-06-10 16:31:03 -0700920uint32_t FindNextInstructionFollowingException(Thread* self, ShadowFrame& shadow_frame,
Sebastien Hertz9f102032014-05-23 08:59:42 +0200921 uint32_t dex_pc, const instrumentation::Instrumentation* instrumentation)
Mathieu Chartier90443472015-07-16 20:32:27 -0700922 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200923
Andreas Gampe794ad762015-02-23 08:12:24 -0800924NO_RETURN void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame)
925 __attribute__((cold))
Mathieu Chartier90443472015-07-16 20:32:27 -0700926 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200927
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200928static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst,
Ian Rogerse94652f2014-12-02 11:13:19 -0800929 const uint32_t dex_pc)
Mathieu Chartier90443472015-07-16 20:32:27 -0700930 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700931 constexpr bool kTracing = false;
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200932 if (kTracing) {
933#define TRACE_LOG std::cerr
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700934 std::ostringstream oss;
935 oss << PrettyMethod(shadow_frame.GetMethod())
936 << StringPrintf("\n0x%x: ", dex_pc)
Ian Rogerse94652f2014-12-02 11:13:19 -0800937 << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()) << "\n";
Ian Rogersef7d42f2014-01-06 12:55:46 -0800938 for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200939 uint32_t raw_value = shadow_frame.GetVReg(i);
940 Object* ref_value = shadow_frame.GetVRegReference(i);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800941 oss << StringPrintf(" vreg%u=0x%08X", i, raw_value);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700942 if (ref_value != nullptr) {
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200943 if (ref_value->GetClass()->IsStringClass() &&
Jeff Hao848f70a2014-01-15 13:49:50 -0800944 ref_value->AsString()->GetValue() != nullptr) {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700945 oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\"";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200946 } else {
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700947 oss << "/" << PrettyTypeOf(ref_value);
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200948 }
949 }
950 }
Mathieu Chartiere861ebd2013-10-09 15:01:21 -0700951 TRACE_LOG << oss.str() << "\n";
Sebastien Hertz8ece0502013-08-07 11:26:41 +0200952#undef TRACE_LOG
953 }
954}
955
Sebastien Hertz1eda2262013-09-09 16:53:14 +0200956static inline bool IsBackwardBranch(int32_t branch_offset) {
957 return branch_offset <= 0;
958}
959
Sebastien Hertzc6714852013-09-30 16:42:32 +0200960// Explicitly instantiate all DoInvoke functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100961#define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700962 template SHARED_REQUIRES(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100963 bool DoInvoke<_type, _is_range, _do_check>(Thread* self, ShadowFrame& shadow_frame, \
964 const Instruction* inst, uint16_t inst_data, \
965 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200966
967#define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \
968 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \
969 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \
970 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \
971 EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true);
972
Andreas Gampec8ccf682014-09-29 20:07:43 -0700973EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic) // invoke-static/range.
974EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect) // invoke-direct/range.
975EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual) // invoke-virtual/range.
976EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper) // invoke-super/range.
977EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface) // invoke-interface/range.
Sebastien Hertzc6714852013-09-30 16:42:32 +0200978#undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL
979#undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL
980
Sebastien Hertzc6714852013-09-30 16:42:32 +0200981// Explicitly instantiate all DoInvokeVirtualQuick functions.
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100982#define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700983 template SHARED_REQUIRES(Locks::mutator_lock_) \
Bernhard Rosenkränzer46053622013-12-12 02:15:52 +0100984 bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \
985 const Instruction* inst, uint16_t inst_data, \
986 JValue* result)
Sebastien Hertzc6714852013-09-30 16:42:32 +0200987
988EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick.
989EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range.
990#undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK
991
Igor Murashkin158f35c2015-06-10 15:55:30 -0700992// Explicitly instantiate all DoCreateLambda functions.
Igor Murashkin6918bf12015-09-27 19:19:06 -0700993#define EXPLICIT_DO_CREATE_LAMBDA_DECL(_do_check) \
994template SHARED_REQUIRES(Locks::mutator_lock_) \
995bool DoCreateLambda<_do_check>(Thread* self, \
996 const Instruction* inst, \
997 /*inout*/ShadowFrame& shadow_frame, \
998 /*inout*/lambda::ClosureBuilder* closure_builder, \
999 /*inout*/lambda::Closure* uninitialized_closure);
Igor Murashkin158f35c2015-06-10 15:55:30 -07001000
1001EXPLICIT_DO_CREATE_LAMBDA_DECL(false); // create-lambda
1002EXPLICIT_DO_CREATE_LAMBDA_DECL(true); // create-lambda
1003#undef EXPLICIT_DO_CREATE_LAMBDA_DECL
1004
1005// Explicitly instantiate all DoInvokeLambda functions.
1006#define EXPLICIT_DO_INVOKE_LAMBDA_DECL(_do_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -07001007template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin158f35c2015-06-10 15:55:30 -07001008bool DoInvokeLambda<_do_check>(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1009 uint16_t inst_data, JValue* result);
1010
1011EXPLICIT_DO_INVOKE_LAMBDA_DECL(false); // invoke-lambda
1012EXPLICIT_DO_INVOKE_LAMBDA_DECL(true); // invoke-lambda
1013#undef EXPLICIT_DO_INVOKE_LAMBDA_DECL
1014
Igor Murashkin2ee54e22015-06-18 10:05:11 -07001015// Explicitly instantiate all DoBoxLambda functions.
1016#define EXPLICIT_DO_BOX_LAMBDA_DECL(_do_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -07001017template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin2ee54e22015-06-18 10:05:11 -07001018bool DoBoxLambda<_do_check>(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1019 uint16_t inst_data);
1020
1021EXPLICIT_DO_BOX_LAMBDA_DECL(false); // box-lambda
1022EXPLICIT_DO_BOX_LAMBDA_DECL(true); // box-lambda
1023#undef EXPLICIT_DO_BOX_LAMBDA_DECL
1024
1025// Explicitly instantiate all DoUnBoxLambda functions.
1026#define EXPLICIT_DO_UNBOX_LAMBDA_DECL(_do_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -07001027template SHARED_REQUIRES(Locks::mutator_lock_) \
Igor Murashkin2ee54e22015-06-18 10:05:11 -07001028bool DoUnboxLambda<_do_check>(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \
1029 uint16_t inst_data);
1030
1031EXPLICIT_DO_UNBOX_LAMBDA_DECL(false); // unbox-lambda
1032EXPLICIT_DO_UNBOX_LAMBDA_DECL(true); // unbox-lambda
1033#undef EXPLICIT_DO_BOX_LAMBDA_DECL
1034
Igor Murashkin6918bf12015-09-27 19:19:06 -07001035// Explicitly instantiate all DoCaptureVariable functions.
1036#define EXPLICIT_DO_CAPTURE_VARIABLE_DECL(_do_check) \
1037template SHARED_REQUIRES(Locks::mutator_lock_) \
1038bool DoCaptureVariable<_do_check>(Thread* self, \
1039 const Instruction* inst, \
1040 ShadowFrame& shadow_frame, \
1041 lambda::ClosureBuilder* closure_builder);
Sebastien Hertzc6714852013-09-30 16:42:32 +02001042
Igor Murashkin6918bf12015-09-27 19:19:06 -07001043EXPLICIT_DO_CAPTURE_VARIABLE_DECL(false); // capture-variable
1044EXPLICIT_DO_CAPTURE_VARIABLE_DECL(true); // capture-variable
1045#undef EXPLICIT_DO_CREATE_LAMBDA_DECL
1046
1047// Explicitly instantiate all DoLiberateVariable functions.
1048#define EXPLICIT_DO_LIBERATE_VARIABLE_DECL(_do_check) \
1049template SHARED_REQUIRES(Locks::mutator_lock_) \
1050bool DoLiberateVariable<_do_check>(Thread* self, \
1051 const Instruction* inst, \
1052 size_t captured_variable_index, \
1053 ShadowFrame& shadow_frame); \
1054
1055EXPLICIT_DO_LIBERATE_VARIABLE_DECL(false); // liberate-variable
1056EXPLICIT_DO_LIBERATE_VARIABLE_DECL(true); // liberate-variable
1057#undef EXPLICIT_DO_LIBERATE_LAMBDA_DECL
Sebastien Hertz8ece0502013-08-07 11:26:41 +02001058} // namespace interpreter
1059} // namespace art
1060
1061#endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_