Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #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 Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 24 | #include <iostream> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 25 | #include <sstream> |
| 26 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 27 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 28 | #include "art_method-inl.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 29 | #include "base/logging.h" |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 30 | #include "base/macros.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 31 | #include "class_linker-inl.h" |
| 32 | #include "common_throws.h" |
| 33 | #include "dex_file-inl.h" |
| 34 | #include "dex_instruction-inl.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 35 | #include "entrypoints/entrypoint_utils-inl.h" |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 36 | #include "handle_scope-inl.h" |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 37 | #include "lambda/art_lambda_method.h" |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 38 | #include "lambda/box_table.h" |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 39 | #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 Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 43 | #include "mirror/class-inl.h" |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 44 | #include "mirror/method.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 45 | #include "mirror/object-inl.h" |
| 46 | #include "mirror/object_array-inl.h" |
Douglas Leung | 4965c02 | 2014-06-11 11:41:11 -0700 | [diff] [blame] | 47 | #include "mirror/string-inl.h" |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame^] | 48 | #include "stack.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 49 | #include "thread.h" |
| 50 | #include "well_known_classes.h" |
| 51 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 52 | using ::art::ArtMethod; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 53 | using ::art::mirror::Array; |
| 54 | using ::art::mirror::BooleanArray; |
| 55 | using ::art::mirror::ByteArray; |
| 56 | using ::art::mirror::CharArray; |
| 57 | using ::art::mirror::Class; |
| 58 | using ::art::mirror::ClassLoader; |
| 59 | using ::art::mirror::IntArray; |
| 60 | using ::art::mirror::LongArray; |
| 61 | using ::art::mirror::Object; |
| 62 | using ::art::mirror::ObjectArray; |
| 63 | using ::art::mirror::ShortArray; |
| 64 | using ::art::mirror::String; |
| 65 | using ::art::mirror::Throwable; |
| 66 | |
| 67 | namespace art { |
| 68 | namespace interpreter { |
| 69 | |
| 70 | // External references to both interpreter implementations. |
| 71 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 72 | template<bool do_access_check, bool transaction_active> |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 73 | extern JValue ExecuteSwitchImpl(Thread* self, const DexFile::CodeItem* code_item, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 74 | ShadowFrame& shadow_frame, JValue result_register); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 75 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 76 | template<bool do_access_check, bool transaction_active> |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 77 | extern JValue ExecuteGotoImpl(Thread* self, const DexFile::CodeItem* code_item, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 78 | ShadowFrame& shadow_frame, JValue result_register); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 79 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 80 | void ThrowNullPointerExceptionFromInterpreter() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 81 | SHARED_REQUIRES(Locks::mutator_lock_); |
Sebastien Hertz | da843e1 | 2014-05-28 19:28:31 +0200 | [diff] [blame] | 82 | |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame^] | 83 | template <bool kMonitorCounting> |
| 84 | static inline void DoMonitorEnter(Thread* self, |
| 85 | ShadowFrame* frame, |
| 86 | Object* ref) NO_THREAD_SAFETY_ANALYSIS { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 87 | ref->MonitorEnter(self); |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame^] | 88 | frame->GetLockCountData().AddMonitor<kMonitorCounting>(self, ref); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame^] | 91 | template <bool kMonitorCounting> |
| 92 | static inline void DoMonitorExit(Thread* self, |
| 93 | ShadowFrame* frame, |
| 94 | Object* ref) NO_THREAD_SAFETY_ANALYSIS { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 95 | ref->MonitorExit(self); |
Andreas Gampe | 03ec930 | 2015-08-27 17:41:47 -0700 | [diff] [blame^] | 96 | frame->GetLockCountData().RemoveMonitorOrThrow<kMonitorCounting>(self, ref); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 99 | void AbortTransactionF(Thread* self, const char* fmt, ...) |
| 100 | __attribute__((__format__(__printf__, 2, 3))) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 101 | SHARED_REQUIRES(Locks::mutator_lock_); |
Sebastien Hertz | 45b1597 | 2015-04-03 16:07:05 +0200 | [diff] [blame] | 102 | |
| 103 | void AbortTransactionV(Thread* self, const char* fmt, va_list args) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 104 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 105 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 106 | void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 107 | SHARED_REQUIRES(Locks::mutator_lock_); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 108 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 109 | // Invokes the given method. This is part of the invocation support and is used by DoInvoke and |
| 110 | // DoInvokeVirtualQuick functions. |
| 111 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 112 | template<bool is_range, bool do_assignability_check> |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 113 | bool DoCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 114 | const Instruction* inst, uint16_t inst_data, JValue* result); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 115 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 116 | // Invokes the given lambda closure. This is part of the invocation support and is used by |
| 117 | // DoLambdaInvoke functions. |
| 118 | // Returns true on success, otherwise throws an exception and returns false. |
| 119 | template<bool is_range, bool do_assignability_check> |
| 120 | bool DoLambdaCall(ArtMethod* called_method, Thread* self, ShadowFrame& shadow_frame, |
| 121 | const Instruction* inst, uint16_t inst_data, JValue* result); |
| 122 | |
| 123 | // Validates that the art method corresponding to a lambda method target |
| 124 | // is semantically valid: |
| 125 | // |
| 126 | // Must be ACC_STATIC and ACC_LAMBDA. Must be a concrete managed implementation |
| 127 | // (i.e. not native, not proxy, not abstract, ...). |
| 128 | // |
| 129 | // If the validation fails, return false and raise an exception. |
| 130 | static inline bool IsValidLambdaTargetOrThrow(ArtMethod* called_method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 131 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 132 | bool success = false; |
| 133 | |
| 134 | if (UNLIKELY(called_method == nullptr)) { |
| 135 | // The shadow frame should already be pushed, so we don't need to update it. |
| 136 | } else if (UNLIKELY(called_method->IsAbstract())) { |
| 137 | ThrowAbstractMethodError(called_method); |
| 138 | // TODO(iam): Also handle the case when the method is non-static, what error do we throw? |
| 139 | // TODO(iam): Also make sure that ACC_LAMBDA is set. |
| 140 | } else if (UNLIKELY(called_method->GetCodeItem() == nullptr)) { |
| 141 | // Method could be native, proxy method, etc. Lambda targets have to be concrete impls, |
| 142 | // so don't allow this. |
| 143 | } else { |
| 144 | success = true; |
| 145 | } |
| 146 | |
| 147 | return success; |
| 148 | } |
| 149 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 150 | // Write out the 'Closure*' into vreg and vreg+1, as if it was a jlong. |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 151 | static inline void WriteLambdaClosureIntoVRegs(ShadowFrame& shadow_frame, |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 152 | const lambda::Closure* lambda_closure, |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 153 | uint32_t vreg) { |
| 154 | // Split the method into a lo and hi 32 bits so we can encode them into 2 virtual registers. |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 155 | uint32_t closure_lo = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(lambda_closure)); |
| 156 | uint32_t closure_hi = static_cast<uint32_t>(reinterpret_cast<uint64_t>(lambda_closure) |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 157 | >> BitSizeOf<uint32_t>()); |
| 158 | // Use uint64_t instead of uintptr_t to allow shifting past the max on 32-bit. |
| 159 | static_assert(sizeof(uint64_t) >= sizeof(uintptr_t), "Impossible"); |
| 160 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 161 | DCHECK_NE(closure_lo | closure_hi, 0u); |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 162 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 163 | shadow_frame.SetVReg(vreg, closure_lo); |
| 164 | shadow_frame.SetVReg(vreg + 1, closure_hi); |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 167 | // Handles create-lambda instructions. |
| 168 | // Returns true on success, otherwise throws an exception and returns false. |
| 169 | // (Exceptions are thrown by creating a new exception and then being put in the thread TLS) |
| 170 | // |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 171 | // The closure must be allocated big enough to hold the data, and should not be |
| 172 | // pre-initialized. It is initialized with the actual captured variables as a side-effect, |
| 173 | // although this should be unimportant to the caller since this function also handles storing it to |
| 174 | // the ShadowFrame. |
| 175 | // |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 176 | // As a work-in-progress implementation, this shoves the ArtMethod object corresponding |
| 177 | // to the target dex method index into the target register vA and vA + 1. |
| 178 | template<bool do_access_check> |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 179 | static inline bool DoCreateLambda(Thread* self, |
| 180 | const Instruction* inst, |
| 181 | /*inout*/ShadowFrame& shadow_frame, |
| 182 | /*inout*/lambda::ClosureBuilder* closure_builder, |
| 183 | /*inout*/lambda::Closure* uninitialized_closure) { |
| 184 | DCHECK(closure_builder != nullptr); |
| 185 | DCHECK(uninitialized_closure != nullptr); |
| 186 | DCHECK_ALIGNED(uninitialized_closure, alignof(lambda::Closure)); |
| 187 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 188 | /* |
| 189 | * create-lambda is opcode 0x21c |
| 190 | * - vA is the target register where the closure will be stored into |
| 191 | * (also stores into vA + 1) |
| 192 | * - vB is the method index which will be the target for a later invoke-lambda |
| 193 | */ |
| 194 | const uint32_t method_idx = inst->VRegB_21c(); |
| 195 | mirror::Object* receiver = nullptr; // Always static. (see 'kStatic') |
| 196 | ArtMethod* sf_method = shadow_frame.GetMethod(); |
| 197 | ArtMethod* const called_method = FindMethodFromCode<kStatic, do_access_check>( |
Andreas Gampe | 3a35714 | 2015-08-07 17:20:11 -0700 | [diff] [blame] | 198 | method_idx, &receiver, sf_method, self); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 199 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 200 | uint32_t vreg_dest_closure = inst->VRegA_21c(); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 201 | |
| 202 | if (UNLIKELY(!IsValidLambdaTargetOrThrow(called_method))) { |
| 203 | CHECK(self->IsExceptionPending()); |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 204 | shadow_frame.SetVReg(vreg_dest_closure, 0u); |
| 205 | shadow_frame.SetVReg(vreg_dest_closure + 1, 0u); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 209 | lambda::ArtLambdaMethod* initialized_lambda_method; |
| 210 | // Initialize the ArtLambdaMethod with the right data. |
| 211 | { |
| 212 | lambda::ArtLambdaMethod* uninitialized_lambda_method = |
| 213 | reinterpret_cast<lambda::ArtLambdaMethod*>( |
| 214 | lambda::LeakingAllocator::AllocateMemory(self, sizeof(lambda::ArtLambdaMethod))); |
| 215 | |
| 216 | std::string captured_variables_shorty = closure_builder->GetCapturedVariableShortyTypes(); |
| 217 | std::string captured_variables_long_type_desc; |
| 218 | |
| 219 | // Synthesize a long type descriptor from the short one. |
| 220 | for (char shorty : captured_variables_shorty) { |
| 221 | lambda::ShortyFieldType shorty_field_type(shorty); |
| 222 | if (shorty_field_type.IsObject()) { |
| 223 | // Not the true type, but good enough until we implement verifier support. |
| 224 | captured_variables_long_type_desc += "Ljava/lang/Object;"; |
| 225 | UNIMPLEMENTED(FATAL) << "create-lambda with an object captured variable"; |
| 226 | } else if (shorty_field_type.IsLambda()) { |
| 227 | // Not the true type, but good enough until we implement verifier support. |
| 228 | captured_variables_long_type_desc += "Ljava/lang/Runnable;"; |
| 229 | UNIMPLEMENTED(FATAL) << "create-lambda with a lambda captured variable"; |
| 230 | } else { |
| 231 | // The primitive types have the same length shorty or not, so this is always correct. |
| 232 | DCHECK(shorty_field_type.IsPrimitive()); |
| 233 | captured_variables_long_type_desc += shorty_field_type; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // Copy strings to dynamically allocated storage. This leaks, but that's ok. Fix it later. |
| 238 | // TODO: Strings need to come from the DexFile, so they won't need their own allocations. |
| 239 | char* captured_variables_type_desc = lambda::LeakingAllocator::MakeFlexibleInstance<char>( |
| 240 | self, |
| 241 | captured_variables_long_type_desc.size() + 1); |
| 242 | strcpy(captured_variables_type_desc, captured_variables_long_type_desc.c_str()); |
| 243 | char* captured_variables_shorty_copy = lambda::LeakingAllocator::MakeFlexibleInstance<char>( |
| 244 | self, |
| 245 | captured_variables_shorty.size() + 1); |
| 246 | strcpy(captured_variables_shorty_copy, captured_variables_shorty.c_str()); |
| 247 | |
| 248 | new (uninitialized_lambda_method) lambda::ArtLambdaMethod(called_method, |
| 249 | captured_variables_type_desc, |
| 250 | captured_variables_shorty_copy, |
| 251 | true); // innate lambda |
| 252 | initialized_lambda_method = uninitialized_lambda_method; |
| 253 | } |
| 254 | |
| 255 | // Write all the closure captured variables and the closure header into the closure. |
| 256 | lambda::Closure* initialized_closure; |
| 257 | { |
| 258 | initialized_closure = |
| 259 | closure_builder->CreateInPlace(uninitialized_closure, initialized_lambda_method); |
| 260 | } |
| 261 | |
| 262 | WriteLambdaClosureIntoVRegs(/*inout*/shadow_frame, initialized_closure, vreg_dest_closure); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 263 | return true; |
| 264 | } |
| 265 | |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 266 | // Reads out the 'ArtMethod*' stored inside of vreg and vreg+1 |
| 267 | // |
| 268 | // Validates that the art method points to a valid lambda function, otherwise throws |
| 269 | // an exception and returns null. |
| 270 | // (Exceptions are thrown by creating a new exception and then being put in the thread TLS) |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 271 | static inline lambda::Closure* ReadLambdaClosureFromVRegsOrThrow(ShadowFrame& shadow_frame, |
| 272 | uint32_t vreg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 273 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 274 | // Lambda closures take up a consecutive pair of 2 virtual registers. |
| 275 | // On 32-bit the high bits are always 0. |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 276 | uint32_t vc_value_lo = shadow_frame.GetVReg(vreg); |
| 277 | uint32_t vc_value_hi = shadow_frame.GetVReg(vreg + 1); |
| 278 | |
| 279 | uint64_t vc_value_ptr = (static_cast<uint64_t>(vc_value_hi) << BitSizeOf<uint32_t>()) |
| 280 | | vc_value_lo; |
| 281 | |
| 282 | // Use uint64_t instead of uintptr_t to allow left-shifting past the max on 32-bit. |
| 283 | static_assert(sizeof(uint64_t) >= sizeof(uintptr_t), "Impossible"); |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 284 | lambda::Closure* const lambda_closure = reinterpret_cast<lambda::Closure*>(vc_value_ptr); |
| 285 | DCHECK_ALIGNED(lambda_closure, alignof(lambda::Closure)); |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 286 | |
| 287 | // Guard against the user passing a null closure, which is odd but (sadly) semantically valid. |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 288 | if (UNLIKELY(lambda_closure == nullptr)) { |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 289 | ThrowNullPointerExceptionFromInterpreter(); |
| 290 | return nullptr; |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 291 | } else if (UNLIKELY(!IsValidLambdaTargetOrThrow(lambda_closure->GetTargetMethod()))) { |
| 292 | // Sanity check against data corruption. |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 293 | return nullptr; |
| 294 | } |
| 295 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 296 | return lambda_closure; |
| 297 | } |
| 298 | |
| 299 | // Forward declaration for lock annotations. See below for documentation. |
| 300 | template <bool do_access_check> |
| 301 | static inline const char* GetStringDataByDexStringIndexOrThrow(ShadowFrame& shadow_frame, |
| 302 | uint32_t string_idx) |
| 303 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 304 | |
| 305 | // Find the c-string data corresponding to a dex file's string index. |
| 306 | // Otherwise, returns null if not found and throws a VerifyError. |
| 307 | // |
| 308 | // Note that with do_access_check=false, we never return null because the verifier |
| 309 | // must guard against invalid string indices. |
| 310 | // (Exceptions are thrown by creating a new exception and then being put in the thread TLS) |
| 311 | template <bool do_access_check> |
| 312 | static inline const char* GetStringDataByDexStringIndexOrThrow(ShadowFrame& shadow_frame, |
| 313 | uint32_t string_idx) { |
| 314 | ArtMethod* method = shadow_frame.GetMethod(); |
| 315 | const DexFile* dex_file = method->GetDexFile(); |
| 316 | |
| 317 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
| 318 | if (!do_access_check) { |
| 319 | // MethodVerifier refuses methods with string_idx out of bounds. |
| 320 | DCHECK_LT(string_idx, declaring_class->GetDexCache()->NumStrings()); |
| 321 | } else { |
| 322 | // Access checks enabled: perform string index bounds ourselves. |
| 323 | if (string_idx >= dex_file->GetHeader().string_ids_size_) { |
| 324 | ThrowVerifyError(declaring_class, "String index '%" PRIu32 "' out of bounds", |
| 325 | string_idx); |
| 326 | return nullptr; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | const char* type_string = dex_file->StringDataByIdx(string_idx); |
| 331 | |
| 332 | if (UNLIKELY(type_string == nullptr)) { |
| 333 | CHECK_EQ(false, do_access_check) |
| 334 | << " verifier should've caught invalid string index " << string_idx; |
| 335 | CHECK_EQ(true, do_access_check) |
| 336 | << " string idx size check should've caught invalid string index " << string_idx; |
| 337 | } |
| 338 | |
| 339 | return type_string; |
| 340 | } |
| 341 | |
| 342 | // Handles capture-variable instructions. |
| 343 | // Returns true on success, otherwise throws an exception and returns false. |
| 344 | // (Exceptions are thrown by creating a new exception and then being put in the thread TLS) |
| 345 | template<bool do_access_check> |
| 346 | static inline bool DoCaptureVariable(Thread* self, |
| 347 | const Instruction* inst, |
| 348 | /*inout*/ShadowFrame& shadow_frame, |
| 349 | /*inout*/lambda::ClosureBuilder* closure_builder) { |
| 350 | DCHECK(closure_builder != nullptr); |
| 351 | using lambda::ShortyFieldType; |
| 352 | /* |
| 353 | * capture-variable is opcode 0xf6, fmt 0x21c |
| 354 | * - vA is the source register of the variable that will be captured |
| 355 | * - vB is the string ID of the variable's type that will be captured |
| 356 | */ |
| 357 | const uint32_t source_vreg = inst->VRegA_21c(); |
| 358 | const uint32_t string_idx = inst->VRegB_21c(); |
| 359 | // TODO: this should be a proper [type id] instead of a [string ID] pointing to a type. |
| 360 | |
| 361 | const char* type_string = GetStringDataByDexStringIndexOrThrow<do_access_check>(shadow_frame, |
| 362 | string_idx); |
| 363 | if (UNLIKELY(type_string == nullptr)) { |
| 364 | CHECK(self->IsExceptionPending()); |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | char type_first_letter = type_string[0]; |
| 369 | ShortyFieldType shorty_type; |
| 370 | if (do_access_check && |
| 371 | UNLIKELY(!ShortyFieldType::MaybeCreate(type_first_letter, /*out*/&shorty_type))) { // NOLINT: [whitespace/comma] [3] |
| 372 | ThrowVerifyError(shadow_frame.GetMethod()->GetDeclaringClass(), |
| 373 | "capture-variable vB must be a valid type"); |
| 374 | return false; |
| 375 | } else { |
| 376 | // Already verified that the type is valid. |
| 377 | shorty_type = ShortyFieldType(type_first_letter); |
| 378 | } |
| 379 | |
| 380 | const size_t captured_variable_count = closure_builder->GetCaptureCount(); |
| 381 | |
| 382 | // Note: types are specified explicitly so that the closure is packed tightly. |
| 383 | switch (shorty_type) { |
| 384 | case ShortyFieldType::kBoolean: { |
| 385 | uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg); |
| 386 | closure_builder->CaptureVariablePrimitive<bool>(primitive_narrow_value); |
| 387 | break; |
| 388 | } |
| 389 | case ShortyFieldType::kByte: { |
| 390 | uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg); |
| 391 | closure_builder->CaptureVariablePrimitive<int8_t>(primitive_narrow_value); |
| 392 | break; |
| 393 | } |
| 394 | case ShortyFieldType::kChar: { |
| 395 | uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg); |
| 396 | closure_builder->CaptureVariablePrimitive<uint16_t>(primitive_narrow_value); |
| 397 | break; |
| 398 | } |
| 399 | case ShortyFieldType::kShort: { |
| 400 | uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg); |
| 401 | closure_builder->CaptureVariablePrimitive<int16_t>(primitive_narrow_value); |
| 402 | break; |
| 403 | } |
| 404 | case ShortyFieldType::kInt: { |
| 405 | uint32_t primitive_narrow_value = shadow_frame.GetVReg(source_vreg); |
| 406 | closure_builder->CaptureVariablePrimitive<int32_t>(primitive_narrow_value); |
| 407 | break; |
| 408 | } |
| 409 | case ShortyFieldType::kDouble: { |
| 410 | closure_builder->CaptureVariablePrimitive(shadow_frame.GetVRegDouble(source_vreg)); |
| 411 | break; |
| 412 | } |
| 413 | case ShortyFieldType::kFloat: { |
| 414 | closure_builder->CaptureVariablePrimitive(shadow_frame.GetVRegFloat(source_vreg)); |
| 415 | break; |
| 416 | } |
| 417 | case ShortyFieldType::kLambda: { |
| 418 | UNIMPLEMENTED(FATAL) << " capture-variable with type kLambda"; |
| 419 | // TODO: Capturing lambdas recursively will be done at a later time. |
| 420 | UNREACHABLE(); |
| 421 | } |
| 422 | case ShortyFieldType::kLong: { |
| 423 | closure_builder->CaptureVariablePrimitive(shadow_frame.GetVRegLong(source_vreg)); |
| 424 | break; |
| 425 | } |
| 426 | case ShortyFieldType::kObject: { |
| 427 | closure_builder->CaptureVariableObject(shadow_frame.GetVRegReference(source_vreg)); |
| 428 | UNIMPLEMENTED(FATAL) << " capture-variable with type kObject"; |
| 429 | // TODO: finish implementing this. disabled for now since we can't track lambda refs for GC. |
| 430 | UNREACHABLE(); |
| 431 | } |
| 432 | |
| 433 | default: |
| 434 | LOG(FATAL) << "Invalid shorty type value " << shorty_type; |
| 435 | UNREACHABLE(); |
| 436 | } |
| 437 | |
| 438 | DCHECK_EQ(captured_variable_count + 1, closure_builder->GetCaptureCount()); |
| 439 | |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | // Handles capture-variable instructions. |
| 444 | // Returns true on success, otherwise throws an exception and returns false. |
| 445 | // (Exceptions are thrown by creating a new exception and then being put in the thread TLS) |
| 446 | template<bool do_access_check> |
| 447 | static inline bool DoLiberateVariable(Thread* self, |
| 448 | const Instruction* inst, |
| 449 | size_t captured_variable_index, |
| 450 | /*inout*/ShadowFrame& shadow_frame) { |
| 451 | using lambda::ShortyFieldType; |
| 452 | /* |
| 453 | * liberate-variable is opcode 0xf7, fmt 0x22c |
| 454 | * - vA is the destination register |
| 455 | * - vB is the register with the lambda closure in it |
| 456 | * - vC is the string ID which needs to be a valid field type descriptor |
| 457 | */ |
| 458 | |
| 459 | const uint32_t dest_vreg = inst->VRegA_22c(); |
| 460 | const uint32_t closure_vreg = inst->VRegB_22c(); |
| 461 | const uint32_t string_idx = inst->VRegC_22c(); |
| 462 | // TODO: this should be a proper [type id] instead of a [string ID] pointing to a type. |
| 463 | |
| 464 | |
| 465 | // Synthesize a long type descriptor from a shorty type descriptor list. |
| 466 | // TODO: Fix the dex encoding to contain the long and short type descriptors. |
| 467 | const char* type_string = GetStringDataByDexStringIndexOrThrow<do_access_check>(shadow_frame, |
| 468 | string_idx); |
| 469 | if (UNLIKELY(do_access_check && type_string == nullptr)) { |
| 470 | CHECK(self->IsExceptionPending()); |
| 471 | shadow_frame.SetVReg(dest_vreg, 0); |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | char type_first_letter = type_string[0]; |
| 476 | ShortyFieldType shorty_type; |
| 477 | if (do_access_check && |
| 478 | UNLIKELY(!ShortyFieldType::MaybeCreate(type_first_letter, /*out*/&shorty_type))) { // NOLINT: [whitespace/comma] [3] |
| 479 | ThrowVerifyError(shadow_frame.GetMethod()->GetDeclaringClass(), |
| 480 | "liberate-variable vC must be a valid type"); |
| 481 | shadow_frame.SetVReg(dest_vreg, 0); |
| 482 | return false; |
| 483 | } else { |
| 484 | // Already verified that the type is valid. |
| 485 | shorty_type = ShortyFieldType(type_first_letter); |
| 486 | } |
| 487 | |
| 488 | // Check for closure being null *after* the type check. |
| 489 | // This way we can access the type info in case we fail later, to know how many vregs to clear. |
| 490 | const lambda::Closure* lambda_closure = |
| 491 | ReadLambdaClosureFromVRegsOrThrow(/*inout*/shadow_frame, closure_vreg); |
| 492 | |
| 493 | // Failed lambda target runtime check, an exception was raised. |
| 494 | if (UNLIKELY(lambda_closure == nullptr)) { |
| 495 | CHECK(self->IsExceptionPending()); |
| 496 | |
| 497 | // Clear the destination vreg(s) to be safe. |
| 498 | shadow_frame.SetVReg(dest_vreg, 0); |
| 499 | if (shorty_type.IsPrimitiveWide() || shorty_type.IsLambda()) { |
| 500 | shadow_frame.SetVReg(dest_vreg + 1, 0); |
| 501 | } |
| 502 | return false; |
| 503 | } |
| 504 | |
| 505 | if (do_access_check && |
| 506 | UNLIKELY(captured_variable_index >= lambda_closure->GetNumberOfCapturedVariables())) { |
| 507 | ThrowVerifyError(shadow_frame.GetMethod()->GetDeclaringClass(), |
| 508 | "liberate-variable captured variable index %zu out of bounds", |
| 509 | lambda_closure->GetNumberOfCapturedVariables()); |
| 510 | // Clear the destination vreg(s) to be safe. |
| 511 | shadow_frame.SetVReg(dest_vreg, 0); |
| 512 | if (shorty_type.IsPrimitiveWide() || shorty_type.IsLambda()) { |
| 513 | shadow_frame.SetVReg(dest_vreg + 1, 0); |
| 514 | } |
| 515 | return false; |
| 516 | } |
| 517 | |
| 518 | // Verify that the runtime type of the captured-variable matches the requested dex type. |
| 519 | if (do_access_check) { |
| 520 | ShortyFieldType actual_type = lambda_closure->GetCapturedShortyType(captured_variable_index); |
| 521 | if (actual_type != shorty_type) { |
| 522 | ThrowVerifyError(shadow_frame.GetMethod()->GetDeclaringClass(), |
| 523 | "cannot liberate-variable of runtime type '%c' to dex type '%c'", |
| 524 | static_cast<char>(actual_type), |
| 525 | static_cast<char>(shorty_type)); |
| 526 | |
| 527 | shadow_frame.SetVReg(dest_vreg, 0); |
| 528 | if (shorty_type.IsPrimitiveWide() || shorty_type.IsLambda()) { |
| 529 | shadow_frame.SetVReg(dest_vreg + 1, 0); |
| 530 | } |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | if (actual_type.IsLambda() || actual_type.IsObject()) { |
| 535 | UNIMPLEMENTED(FATAL) << "liberate-variable type checks needs to " |
| 536 | << "parse full type descriptor for objects and lambdas"; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | // Unpack the captured variable from the closure into the correct type, then save it to the vreg. |
| 541 | if (shorty_type.IsPrimitiveNarrow()) { |
| 542 | uint32_t primitive_narrow_value = |
| 543 | lambda_closure->GetCapturedPrimitiveNarrow(captured_variable_index); |
| 544 | shadow_frame.SetVReg(dest_vreg, primitive_narrow_value); |
| 545 | } else if (shorty_type.IsPrimitiveWide()) { |
| 546 | uint64_t primitive_wide_value = |
| 547 | lambda_closure->GetCapturedPrimitiveWide(captured_variable_index); |
| 548 | shadow_frame.SetVRegLong(dest_vreg, static_cast<int64_t>(primitive_wide_value)); |
| 549 | } else if (shorty_type.IsObject()) { |
| 550 | mirror::Object* unpacked_object = |
| 551 | lambda_closure->GetCapturedObject(captured_variable_index); |
| 552 | shadow_frame.SetVRegReference(dest_vreg, unpacked_object); |
| 553 | |
| 554 | UNIMPLEMENTED(FATAL) << "liberate-variable cannot unpack objects yet"; |
| 555 | } else if (shorty_type.IsLambda()) { |
| 556 | UNIMPLEMENTED(FATAL) << "liberate-variable cannot unpack lambdas yet"; |
| 557 | } else { |
| 558 | LOG(FATAL) << "unreachable"; |
| 559 | UNREACHABLE(); |
| 560 | } |
| 561 | |
| 562 | return true; |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 565 | template<bool do_access_check> |
| 566 | static inline bool DoInvokeLambda(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
| 567 | uint16_t inst_data, JValue* result) { |
| 568 | /* |
| 569 | * invoke-lambda is opcode 0x25 |
| 570 | * |
| 571 | * - vC is the closure register (both vC and vC + 1 will be used to store the closure). |
| 572 | * - vB is the number of additional registers up to |{vD,vE,vF,vG}| (4) |
| 573 | * - the rest of the registers are always var-args |
| 574 | * |
| 575 | * - reading var-args for 0x25 gets us vD,vE,vF,vG (but not vB) |
| 576 | */ |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 577 | uint32_t vreg_closure = inst->VRegC_25x(); |
| 578 | const lambda::Closure* lambda_closure = |
| 579 | ReadLambdaClosureFromVRegsOrThrow(shadow_frame, vreg_closure); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 580 | |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 581 | // Failed lambda target runtime check, an exception was raised. |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 582 | if (UNLIKELY(lambda_closure == nullptr)) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 583 | CHECK(self->IsExceptionPending()); |
| 584 | result->SetJ(0); |
| 585 | return false; |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 586 | } |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 587 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 588 | ArtMethod* const called_method = lambda_closure->GetTargetMethod(); |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 589 | // Invoke a non-range lambda |
| 590 | return DoLambdaCall<false, do_access_check>(called_method, self, shadow_frame, inst, inst_data, |
| 591 | result); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 594 | // Handles invoke-XXX/range instructions (other than invoke-lambda[-range]). |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 595 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 596 | template<InvokeType type, bool is_range, bool do_access_check> |
| 597 | static inline bool DoInvoke(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
| 598 | uint16_t inst_data, JValue* result) { |
| 599 | const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 600 | const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c(); |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 601 | Object* receiver = (type == kStatic) ? nullptr : shadow_frame.GetVRegReference(vregC); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 602 | ArtMethod* sf_method = shadow_frame.GetMethod(); |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 603 | ArtMethod* const called_method = FindMethodFromCode<type, do_access_check>( |
Andreas Gampe | 3a35714 | 2015-08-07 17:20:11 -0700 | [diff] [blame] | 604 | method_idx, &receiver, sf_method, self); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 605 | // The shadow frame should already be pushed, so we don't need to update it. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 606 | if (UNLIKELY(called_method == nullptr)) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 607 | CHECK(self->IsExceptionPending()); |
| 608 | result->SetJ(0); |
| 609 | return false; |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 610 | } else if (UNLIKELY(called_method->IsAbstract())) { |
| 611 | ThrowAbstractMethodError(called_method); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 612 | result->SetJ(0); |
| 613 | return false; |
| 614 | } else { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 615 | if (type == kVirtual || type == kInterface) { |
| 616 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 617 | if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) { |
| 618 | instrumentation->InvokeVirtualOrInterface( |
| 619 | self, receiver, sf_method, shadow_frame.GetDexPC(), called_method); |
| 620 | } |
| 621 | } |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 622 | return DoCall<is_range, do_access_check>(called_method, self, shadow_frame, inst, inst_data, |
| 623 | result); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 624 | } |
| 625 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 626 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 627 | // Handles invoke-virtual-quick and invoke-virtual-quick-range instructions. |
| 628 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 629 | template<bool is_range> |
| 630 | static inline bool DoInvokeVirtualQuick(Thread* self, ShadowFrame& shadow_frame, |
| 631 | const Instruction* inst, uint16_t inst_data, |
| 632 | JValue* result) { |
| 633 | const uint32_t vregC = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c(); |
| 634 | Object* const receiver = shadow_frame.GetVRegReference(vregC); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 635 | if (UNLIKELY(receiver == nullptr)) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 636 | // We lost the reference to the method index so we cannot get a more |
| 637 | // precised exception message. |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 638 | ThrowNullPointerExceptionFromDexPC(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 639 | return false; |
| 640 | } |
| 641 | const uint32_t vtable_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 642 | CHECK(receiver->GetClass()->ShouldHaveEmbeddedImtAndVTable()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 643 | ArtMethod* const called_method = receiver->GetClass()->GetEmbeddedVTableEntry( |
| 644 | vtable_idx, sizeof(void*)); |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 645 | if (UNLIKELY(called_method == nullptr)) { |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 646 | CHECK(self->IsExceptionPending()); |
| 647 | result->SetJ(0); |
| 648 | return false; |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 649 | } else if (UNLIKELY(called_method->IsAbstract())) { |
| 650 | ThrowAbstractMethodError(called_method); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 651 | result->SetJ(0); |
| 652 | return false; |
| 653 | } else { |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 654 | instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation(); |
| 655 | if (UNLIKELY(instrumentation->HasInvokeVirtualOrInterfaceListeners())) { |
| 656 | instrumentation->InvokeVirtualOrInterface( |
| 657 | self, receiver, shadow_frame.GetMethod(), shadow_frame.GetDexPC(), called_method); |
| 658 | } |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 659 | // No need to check since we've been quickened. |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 660 | return DoCall<is_range, false>(called_method, self, shadow_frame, inst, inst_data, result); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 661 | } |
| 662 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 663 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 664 | // Handles iget-XXX and sget-XXX instructions. |
| 665 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 666 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check> |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 667 | bool DoFieldGet(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 668 | uint16_t inst_data) SHARED_REQUIRES(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 669 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 670 | // Handles iget-quick, iget-wide-quick and iget-object-quick instructions. |
| 671 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 672 | template<Primitive::Type field_type> |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 673 | bool DoIGetQuick(ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 674 | SHARED_REQUIRES(Locks::mutator_lock_); |
Sebastien Hertz | 479fc1e | 2014-04-04 17:51:34 +0200 | [diff] [blame] | 675 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 676 | // Handles iput-XXX and sput-XXX instructions. |
| 677 | // Returns true on success, otherwise throws an exception and returns false. |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 678 | template<FindFieldType find_type, Primitive::Type field_type, bool do_access_check, |
| 679 | bool transaction_active> |
| 680 | bool DoFieldPut(Thread* self, const ShadowFrame& shadow_frame, const Instruction* inst, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 681 | uint16_t inst_data) SHARED_REQUIRES(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 682 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 683 | // Handles iput-quick, iput-wide-quick and iput-object-quick instructions. |
| 684 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 685 | template<Primitive::Type field_type, bool transaction_active> |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 686 | bool DoIPutQuick(const ShadowFrame& shadow_frame, const Instruction* inst, uint16_t inst_data) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 687 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 688 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 689 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 690 | // Handles string resolution for const-string and const-string-jumbo instructions. Also ensures the |
| 691 | // java.lang.String class is initialized. |
Ian Rogers | 6786a58 | 2014-10-28 12:49:06 -0700 | [diff] [blame] | 692 | static inline String* ResolveString(Thread* self, ShadowFrame& shadow_frame, uint32_t string_idx) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 693 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 694 | Class* java_lang_string_class = String::GetJavaLangString(); |
| 695 | if (UNLIKELY(!java_lang_string_class->IsInitialized())) { |
| 696 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 697 | StackHandleScope<1> hs(self); |
| 698 | Handle<mirror::Class> h_class(hs.NewHandle(java_lang_string_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 699 | if (UNLIKELY(!class_linker->EnsureInitialized(self, h_class, true, true))) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 700 | DCHECK(self->IsExceptionPending()); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 701 | return nullptr; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 702 | } |
| 703 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 704 | ArtMethod* method = shadow_frame.GetMethod(); |
Mathieu Chartier | eace458 | 2014-11-24 18:29:54 -0800 | [diff] [blame] | 705 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 706 | // MethodVerifier refuses methods with string_idx out of bounds. |
| 707 | DCHECK_LT(string_idx, declaring_class->GetDexCache()->NumStrings()); |
| 708 | mirror::String* s = declaring_class->GetDexCacheStrings()[string_idx].Read(); |
Ian Rogers | 6786a58 | 2014-10-28 12:49:06 -0700 | [diff] [blame] | 709 | if (UNLIKELY(s == nullptr)) { |
| 710 | StackHandleScope<1> hs(self); |
Mathieu Chartier | eace458 | 2014-11-24 18:29:54 -0800 | [diff] [blame] | 711 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache())); |
Ian Rogers | 6786a58 | 2014-10-28 12:49:06 -0700 | [diff] [blame] | 712 | s = Runtime::Current()->GetClassLinker()->ResolveString(*method->GetDexFile(), string_idx, |
| 713 | dex_cache); |
| 714 | } |
| 715 | return s; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 718 | // Handles div-int, div-int/2addr, div-int/li16 and div-int/lit8 instructions. |
| 719 | // Returns true on success, otherwise throws a java.lang.ArithmeticException and return false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 720 | static inline bool DoIntDivide(ShadowFrame& shadow_frame, size_t result_reg, |
| 721 | int32_t dividend, int32_t divisor) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 722 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 723 | constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 724 | if (UNLIKELY(divisor == 0)) { |
| 725 | ThrowArithmeticExceptionDivideByZero(); |
| 726 | return false; |
| 727 | } |
| 728 | if (UNLIKELY(dividend == kMinInt && divisor == -1)) { |
| 729 | shadow_frame.SetVReg(result_reg, kMinInt); |
| 730 | } else { |
| 731 | shadow_frame.SetVReg(result_reg, dividend / divisor); |
| 732 | } |
| 733 | return true; |
| 734 | } |
| 735 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 736 | // Handles rem-int, rem-int/2addr, rem-int/li16 and rem-int/lit8 instructions. |
| 737 | // Returns true on success, otherwise throws a java.lang.ArithmeticException and return false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 738 | static inline bool DoIntRemainder(ShadowFrame& shadow_frame, size_t result_reg, |
| 739 | int32_t dividend, int32_t divisor) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 740 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | f72a11d | 2014-10-30 15:41:08 -0700 | [diff] [blame] | 741 | constexpr int32_t kMinInt = std::numeric_limits<int32_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 742 | if (UNLIKELY(divisor == 0)) { |
| 743 | ThrowArithmeticExceptionDivideByZero(); |
| 744 | return false; |
| 745 | } |
| 746 | if (UNLIKELY(dividend == kMinInt && divisor == -1)) { |
| 747 | shadow_frame.SetVReg(result_reg, 0); |
| 748 | } else { |
| 749 | shadow_frame.SetVReg(result_reg, dividend % divisor); |
| 750 | } |
| 751 | return true; |
| 752 | } |
| 753 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 754 | // Handles div-long and div-long-2addr instructions. |
| 755 | // Returns true on success, otherwise throws a java.lang.ArithmeticException and return false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 756 | static inline bool DoLongDivide(ShadowFrame& shadow_frame, size_t result_reg, |
| 757 | int64_t dividend, int64_t divisor) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 758 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 759 | const int64_t kMinLong = std::numeric_limits<int64_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 760 | if (UNLIKELY(divisor == 0)) { |
| 761 | ThrowArithmeticExceptionDivideByZero(); |
| 762 | return false; |
| 763 | } |
| 764 | if (UNLIKELY(dividend == kMinLong && divisor == -1)) { |
| 765 | shadow_frame.SetVRegLong(result_reg, kMinLong); |
| 766 | } else { |
| 767 | shadow_frame.SetVRegLong(result_reg, dividend / divisor); |
| 768 | } |
| 769 | return true; |
| 770 | } |
| 771 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 772 | // Handles rem-long and rem-long-2addr instructions. |
| 773 | // Returns true on success, otherwise throws a java.lang.ArithmeticException and return false. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 774 | static inline bool DoLongRemainder(ShadowFrame& shadow_frame, size_t result_reg, |
| 775 | int64_t dividend, int64_t divisor) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 776 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 2e2deeb | 2013-09-23 11:58:57 -0700 | [diff] [blame] | 777 | const int64_t kMinLong = std::numeric_limits<int64_t>::min(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 778 | if (UNLIKELY(divisor == 0)) { |
| 779 | ThrowArithmeticExceptionDivideByZero(); |
| 780 | return false; |
| 781 | } |
| 782 | if (UNLIKELY(dividend == kMinLong && divisor == -1)) { |
| 783 | shadow_frame.SetVRegLong(result_reg, 0); |
| 784 | } else { |
| 785 | shadow_frame.SetVRegLong(result_reg, dividend % divisor); |
| 786 | } |
| 787 | return true; |
| 788 | } |
| 789 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 790 | // Handles filled-new-array and filled-new-array-range instructions. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 791 | // Returns true on success, otherwise throws an exception and returns false. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 792 | template <bool is_range, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 793 | bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 794 | Thread* self, JValue* result); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 795 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 796 | // Handles packed-switch instruction. |
| 797 | // Returns the branch offset to the next instruction to execute. |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 798 | static inline int32_t DoPackedSwitch(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 799 | uint16_t inst_data) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 800 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 801 | DCHECK(inst->Opcode() == Instruction::PACKED_SWITCH); |
| 802 | const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t(); |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 803 | int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 804 | DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature)); |
| 805 | uint16_t size = switch_data[1]; |
David Brazdil | 2ef645b | 2015-06-17 18:20:52 +0100 | [diff] [blame] | 806 | if (size == 0) { |
| 807 | // Empty packed switch, move forward by 3 (size of PACKED_SWITCH). |
| 808 | return 3; |
| 809 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 810 | const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 811 | DCHECK_ALIGNED(keys, 4); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 812 | int32_t first_key = keys[0]; |
| 813 | const int32_t* targets = reinterpret_cast<const int32_t*>(&switch_data[4]); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 814 | DCHECK_ALIGNED(targets, 4); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 815 | int32_t index = test_val - first_key; |
| 816 | if (index >= 0 && index < size) { |
| 817 | return targets[index]; |
| 818 | } else { |
| 819 | // No corresponding value: move forward by 3 (size of PACKED_SWITCH). |
| 820 | return 3; |
| 821 | } |
| 822 | } |
| 823 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 824 | // Handles sparse-switch instruction. |
| 825 | // Returns the branch offset to the next instruction to execute. |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 826 | static inline int32_t DoSparseSwitch(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 827 | uint16_t inst_data) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 828 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 829 | DCHECK(inst->Opcode() == Instruction::SPARSE_SWITCH); |
| 830 | const uint16_t* switch_data = reinterpret_cast<const uint16_t*>(inst) + inst->VRegB_31t(); |
Sebastien Hertz | 3b588e0 | 2013-09-11 14:33:18 +0200 | [diff] [blame] | 831 | int32_t test_val = shadow_frame.GetVReg(inst->VRegA_31t(inst_data)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 832 | DCHECK_EQ(switch_data[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature)); |
| 833 | uint16_t size = switch_data[1]; |
Jeff Hao | 935e01a | 2015-03-20 19:44:35 -0700 | [diff] [blame] | 834 | // Return length of SPARSE_SWITCH if size is 0. |
| 835 | if (size == 0) { |
| 836 | return 3; |
| 837 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 838 | const int32_t* keys = reinterpret_cast<const int32_t*>(&switch_data[2]); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 839 | DCHECK_ALIGNED(keys, 4); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 840 | const int32_t* entries = keys + size; |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 841 | DCHECK_ALIGNED(entries, 4); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 842 | int lo = 0; |
| 843 | int hi = size - 1; |
| 844 | while (lo <= hi) { |
| 845 | int mid = (lo + hi) / 2; |
| 846 | int32_t foundVal = keys[mid]; |
| 847 | if (test_val < foundVal) { |
| 848 | hi = mid - 1; |
| 849 | } else if (test_val > foundVal) { |
| 850 | lo = mid + 1; |
| 851 | } else { |
| 852 | return entries[mid]; |
| 853 | } |
| 854 | } |
| 855 | // No corresponding value: move forward by 3 (size of SPARSE_SWITCH). |
| 856 | return 3; |
| 857 | } |
| 858 | |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 859 | template <bool _do_check> |
| 860 | static inline bool DoBoxLambda(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 861 | uint16_t inst_data) SHARED_REQUIRES(Locks::mutator_lock_) { |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 862 | /* |
| 863 | * box-lambda vA, vB /// opcode 0xf8, format 22x |
| 864 | * - vA is the target register where the Object representation of the closure will be stored into |
| 865 | * - vB is a closure (made by create-lambda) |
| 866 | * (also reads vB + 1) |
| 867 | */ |
| 868 | uint32_t vreg_target_object = inst->VRegA_22x(inst_data); |
| 869 | uint32_t vreg_source_closure = inst->VRegB_22x(); |
| 870 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 871 | lambda::Closure* lambda_closure = ReadLambdaClosureFromVRegsOrThrow(shadow_frame, |
| 872 | vreg_source_closure); |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 873 | |
| 874 | // Failed lambda target runtime check, an exception was raised. |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 875 | if (UNLIKELY(lambda_closure == nullptr)) { |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 876 | CHECK(self->IsExceptionPending()); |
| 877 | return false; |
| 878 | } |
| 879 | |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 880 | mirror::Object* closure_as_object = |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 881 | Runtime::Current()->GetLambdaBoxTable()->BoxLambda(lambda_closure); |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 882 | |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 883 | // Failed to box the lambda, an exception was raised. |
| 884 | if (UNLIKELY(closure_as_object == nullptr)) { |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 885 | CHECK(self->IsExceptionPending()); |
| 886 | return false; |
| 887 | } |
| 888 | |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 889 | shadow_frame.SetVRegReference(vreg_target_object, closure_as_object); |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 890 | return true; |
| 891 | } |
| 892 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 893 | template <bool _do_check> SHARED_REQUIRES(Locks::mutator_lock_) |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 894 | static inline bool DoUnboxLambda(Thread* self, |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 895 | ShadowFrame& shadow_frame, |
| 896 | const Instruction* inst, |
| 897 | uint16_t inst_data) { |
| 898 | /* |
| 899 | * unbox-lambda vA, vB, [type id] /// opcode 0xf9, format 22c |
| 900 | * - vA is the target register where the closure will be written into |
| 901 | * (also writes vA + 1) |
| 902 | * - vB is the Object representation of the closure (made by box-lambda) |
| 903 | */ |
| 904 | uint32_t vreg_target_closure = inst->VRegA_22c(inst_data); |
| 905 | uint32_t vreg_source_object = inst->VRegB_22c(); |
| 906 | |
| 907 | // Raise NullPointerException if object is null |
| 908 | mirror::Object* boxed_closure_object = shadow_frame.GetVRegReference(vreg_source_object); |
| 909 | if (UNLIKELY(boxed_closure_object == nullptr)) { |
| 910 | ThrowNullPointerExceptionFromInterpreter(); |
| 911 | return false; |
| 912 | } |
| 913 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 914 | lambda::Closure* unboxed_closure = nullptr; |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 915 | // Raise an exception if unboxing fails. |
| 916 | if (!Runtime::Current()->GetLambdaBoxTable()->UnboxLambda(boxed_closure_object, |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 917 | /*out*/&unboxed_closure)) { |
Igor Murashkin | e2facc5 | 2015-07-10 13:49:08 -0700 | [diff] [blame] | 918 | CHECK(self->IsExceptionPending()); |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 919 | return false; |
| 920 | } |
| 921 | |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 922 | DCHECK(unboxed_closure != nullptr); |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 923 | WriteLambdaClosureIntoVRegs(/*inout*/shadow_frame, unboxed_closure, vreg_target_closure); |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 924 | return true; |
| 925 | } |
| 926 | |
Ian Rogers | 5487494 | 2014-06-10 16:31:03 -0700 | [diff] [blame] | 927 | uint32_t FindNextInstructionFollowingException(Thread* self, ShadowFrame& shadow_frame, |
Sebastien Hertz | 9f10203 | 2014-05-23 08:59:42 +0200 | [diff] [blame] | 928 | uint32_t dex_pc, const instrumentation::Instrumentation* instrumentation) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 929 | SHARED_REQUIRES(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 930 | |
Andreas Gampe | 794ad76 | 2015-02-23 08:12:24 -0800 | [diff] [blame] | 931 | NO_RETURN void UnexpectedOpcode(const Instruction* inst, const ShadowFrame& shadow_frame) |
| 932 | __attribute__((cold)) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 933 | SHARED_REQUIRES(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 934 | |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 935 | static inline void TraceExecution(const ShadowFrame& shadow_frame, const Instruction* inst, |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 936 | const uint32_t dex_pc) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 937 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 938 | constexpr bool kTracing = false; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 939 | if (kTracing) { |
| 940 | #define TRACE_LOG std::cerr |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 941 | std::ostringstream oss; |
| 942 | oss << PrettyMethod(shadow_frame.GetMethod()) |
| 943 | << StringPrintf("\n0x%x: ", dex_pc) |
Ian Rogers | e94652f | 2014-12-02 11:13:19 -0800 | [diff] [blame] | 944 | << inst->DumpString(shadow_frame.GetMethod()->GetDexFile()) << "\n"; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 945 | for (uint32_t i = 0; i < shadow_frame.NumberOfVRegs(); ++i) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 946 | uint32_t raw_value = shadow_frame.GetVReg(i); |
| 947 | Object* ref_value = shadow_frame.GetVRegReference(i); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 948 | oss << StringPrintf(" vreg%u=0x%08X", i, raw_value); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 949 | if (ref_value != nullptr) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 950 | if (ref_value->GetClass()->IsStringClass() && |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 951 | ref_value->AsString()->GetValue() != nullptr) { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 952 | oss << "/java.lang.String \"" << ref_value->AsString()->ToModifiedUtf8() << "\""; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 953 | } else { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 954 | oss << "/" << PrettyTypeOf(ref_value); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | } |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 958 | TRACE_LOG << oss.str() << "\n"; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 959 | #undef TRACE_LOG |
| 960 | } |
| 961 | } |
| 962 | |
Sebastien Hertz | 1eda226 | 2013-09-09 16:53:14 +0200 | [diff] [blame] | 963 | static inline bool IsBackwardBranch(int32_t branch_offset) { |
| 964 | return branch_offset <= 0; |
| 965 | } |
| 966 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 967 | // Explicitly instantiate all DoInvoke functions. |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 968 | #define EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, _is_range, _do_check) \ |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 969 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 970 | bool DoInvoke<_type, _is_range, _do_check>(Thread* self, ShadowFrame& shadow_frame, \ |
| 971 | const Instruction* inst, uint16_t inst_data, \ |
| 972 | JValue* result) |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 973 | |
| 974 | #define EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(_type) \ |
| 975 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, false); \ |
| 976 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, false, true); \ |
| 977 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, false); \ |
| 978 | EXPLICIT_DO_INVOKE_TEMPLATE_DECL(_type, true, true); |
| 979 | |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 980 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kStatic) // invoke-static/range. |
| 981 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kDirect) // invoke-direct/range. |
| 982 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kVirtual) // invoke-virtual/range. |
| 983 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kSuper) // invoke-super/range. |
| 984 | EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL(kInterface) // invoke-interface/range. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 985 | #undef EXPLICIT_DO_INVOKE_ALL_TEMPLATE_DECL |
| 986 | #undef EXPLICIT_DO_INVOKE_TEMPLATE_DECL |
| 987 | |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 988 | // Explicitly instantiate all DoInvokeVirtualQuick functions. |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 989 | #define EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(_is_range) \ |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 990 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
Bernhard Rosenkränzer | 4605362 | 2013-12-12 02:15:52 +0100 | [diff] [blame] | 991 | bool DoInvokeVirtualQuick<_is_range>(Thread* self, ShadowFrame& shadow_frame, \ |
| 992 | const Instruction* inst, uint16_t inst_data, \ |
| 993 | JValue* result) |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 994 | |
| 995 | EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(false); // invoke-virtual-quick. |
| 996 | EXPLICIT_DO_INVOKE_VIRTUAL_QUICK_TEMPLATE_DECL(true); // invoke-virtual-quick-range. |
| 997 | #undef EXPLICIT_INSTANTIATION_DO_INVOKE_VIRTUAL_QUICK |
| 998 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 999 | // Explicitly instantiate all DoCreateLambda functions. |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1000 | #define EXPLICIT_DO_CREATE_LAMBDA_DECL(_do_check) \ |
| 1001 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
| 1002 | bool DoCreateLambda<_do_check>(Thread* self, \ |
| 1003 | const Instruction* inst, \ |
| 1004 | /*inout*/ShadowFrame& shadow_frame, \ |
| 1005 | /*inout*/lambda::ClosureBuilder* closure_builder, \ |
| 1006 | /*inout*/lambda::Closure* uninitialized_closure); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1007 | |
| 1008 | EXPLICIT_DO_CREATE_LAMBDA_DECL(false); // create-lambda |
| 1009 | EXPLICIT_DO_CREATE_LAMBDA_DECL(true); // create-lambda |
| 1010 | #undef EXPLICIT_DO_CREATE_LAMBDA_DECL |
| 1011 | |
| 1012 | // Explicitly instantiate all DoInvokeLambda functions. |
| 1013 | #define EXPLICIT_DO_INVOKE_LAMBDA_DECL(_do_check) \ |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1014 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 1015 | bool DoInvokeLambda<_do_check>(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 1016 | uint16_t inst_data, JValue* result); |
| 1017 | |
| 1018 | EXPLICIT_DO_INVOKE_LAMBDA_DECL(false); // invoke-lambda |
| 1019 | EXPLICIT_DO_INVOKE_LAMBDA_DECL(true); // invoke-lambda |
| 1020 | #undef EXPLICIT_DO_INVOKE_LAMBDA_DECL |
| 1021 | |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 1022 | // Explicitly instantiate all DoBoxLambda functions. |
| 1023 | #define EXPLICIT_DO_BOX_LAMBDA_DECL(_do_check) \ |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1024 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 1025 | bool DoBoxLambda<_do_check>(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 1026 | uint16_t inst_data); |
| 1027 | |
| 1028 | EXPLICIT_DO_BOX_LAMBDA_DECL(false); // box-lambda |
| 1029 | EXPLICIT_DO_BOX_LAMBDA_DECL(true); // box-lambda |
| 1030 | #undef EXPLICIT_DO_BOX_LAMBDA_DECL |
| 1031 | |
| 1032 | // Explicitly instantiate all DoUnBoxLambda functions. |
| 1033 | #define EXPLICIT_DO_UNBOX_LAMBDA_DECL(_do_check) \ |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1034 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
Igor Murashkin | 2ee54e2 | 2015-06-18 10:05:11 -0700 | [diff] [blame] | 1035 | bool DoUnboxLambda<_do_check>(Thread* self, ShadowFrame& shadow_frame, const Instruction* inst, \ |
| 1036 | uint16_t inst_data); |
| 1037 | |
| 1038 | EXPLICIT_DO_UNBOX_LAMBDA_DECL(false); // unbox-lambda |
| 1039 | EXPLICIT_DO_UNBOX_LAMBDA_DECL(true); // unbox-lambda |
| 1040 | #undef EXPLICIT_DO_BOX_LAMBDA_DECL |
| 1041 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1042 | // Explicitly instantiate all DoCaptureVariable functions. |
| 1043 | #define EXPLICIT_DO_CAPTURE_VARIABLE_DECL(_do_check) \ |
| 1044 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
| 1045 | bool DoCaptureVariable<_do_check>(Thread* self, \ |
| 1046 | const Instruction* inst, \ |
| 1047 | ShadowFrame& shadow_frame, \ |
| 1048 | lambda::ClosureBuilder* closure_builder); |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 1049 | |
Igor Murashkin | 6918bf1 | 2015-09-27 19:19:06 -0700 | [diff] [blame] | 1050 | EXPLICIT_DO_CAPTURE_VARIABLE_DECL(false); // capture-variable |
| 1051 | EXPLICIT_DO_CAPTURE_VARIABLE_DECL(true); // capture-variable |
| 1052 | #undef EXPLICIT_DO_CREATE_LAMBDA_DECL |
| 1053 | |
| 1054 | // Explicitly instantiate all DoLiberateVariable functions. |
| 1055 | #define EXPLICIT_DO_LIBERATE_VARIABLE_DECL(_do_check) \ |
| 1056 | template SHARED_REQUIRES(Locks::mutator_lock_) \ |
| 1057 | bool DoLiberateVariable<_do_check>(Thread* self, \ |
| 1058 | const Instruction* inst, \ |
| 1059 | size_t captured_variable_index, \ |
| 1060 | ShadowFrame& shadow_frame); \ |
| 1061 | |
| 1062 | EXPLICIT_DO_LIBERATE_VARIABLE_DECL(false); // liberate-variable |
| 1063 | EXPLICIT_DO_LIBERATE_VARIABLE_DECL(true); // liberate-variable |
| 1064 | #undef EXPLICIT_DO_LIBERATE_LAMBDA_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1065 | } // namespace interpreter |
| 1066 | } // namespace art |
| 1067 | |
| 1068 | #endif // ART_RUNTIME_INTERPRETER_INTERPRETER_COMMON_H_ |