Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "interpreter_common.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 18 | #include "mirror/array-inl.h" |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 19 | |
| 20 | namespace art { |
| 21 | namespace interpreter { |
| 22 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 23 | static void UnstartedRuntimeInvoke(Thread* self, MethodHelper& mh, |
| 24 | const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame, |
| 25 | JValue* result, size_t arg_offset) |
| 26 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 27 | |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 28 | // Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 29 | static inline void AssignRegister(ShadowFrame* new_shadow_frame, const ShadowFrame& shadow_frame, |
| 30 | size_t dest_reg, size_t src_reg) |
| 31 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 32 | // If both register locations contains the same value, the register probably holds a reference. |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 33 | // Uint required, so that sign extension does not make this wrong on 64b systems |
| 34 | uint32_t src_value = shadow_frame.GetVReg(src_reg); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 35 | mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg); |
Andreas Gampe | 7104cbf | 2014-03-21 11:44:43 -0700 | [diff] [blame] | 36 | if (src_value == reinterpret_cast<uintptr_t>(o)) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 37 | new_shadow_frame->SetVRegReference(dest_reg, o); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 38 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 39 | new_shadow_frame->SetVReg(dest_reg, src_value); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Mathieu Chartier | b2c7ead | 2014-04-29 11:13:16 -0700 | [diff] [blame] | 43 | void AbortTransaction(Thread* self, const char* fmt, ...) { |
| 44 | CHECK(Runtime::Current()->IsActiveTransaction()); |
| 45 | // Throw an exception so we can abort the transaction and undo every change. |
| 46 | va_list args; |
| 47 | va_start(args, fmt); |
| 48 | self->ThrowNewExceptionV(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;", fmt, |
| 49 | args); |
| 50 | va_end(args); |
| 51 | } |
| 52 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 53 | template<bool is_range, bool do_assignability_check> |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 54 | bool DoCall(ArtMethod* method, Thread* self, ShadowFrame& shadow_frame, |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 55 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 56 | // Compute method information. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 57 | MethodHelper mh(method); |
| 58 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 59 | const uint16_t num_ins = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 60 | uint16_t num_regs; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 61 | if (LIKELY(code_item != NULL)) { |
| 62 | num_regs = code_item->registers_size_; |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 63 | DCHECK_EQ(num_ins, code_item->ins_size_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 64 | } else { |
| 65 | DCHECK(method->IsNative() || method->IsProxyMethod()); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 66 | num_regs = num_ins; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 69 | // Allocate shadow frame on the stack. |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 70 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCall"); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 71 | void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); |
| 72 | ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, method, 0, memory)); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 73 | |
| 74 | // Initialize new shadow frame. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 75 | const size_t first_dest_reg = num_regs - num_ins; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 76 | if (do_assignability_check) { |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 77 | // Slow path. |
| 78 | // We might need to do class loading, which incurs a thread state change to kNative. So |
| 79 | // register the shadow frame as under construction and allow suspension again. |
| 80 | self->SetShadowFrameUnderConstruction(new_shadow_frame); |
| 81 | self->EndAssertNoThreadSuspension(old_cause); |
| 82 | |
| 83 | // We need to do runtime check on reference assignment. We need to load the shorty |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 84 | // to get the exact type of each reference argument. |
| 85 | const DexFile::TypeList* params = mh.GetParameterTypeList(); |
| 86 | const char* shorty = mh.GetShorty(); |
| 87 | |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 88 | // TODO: find a cleaner way to separate non-range and range information without duplicating code. |
| 89 | uint32_t arg[5]; // only used in invoke-XXX. |
| 90 | uint32_t vregC; // only used in invoke-XXX-range. |
| 91 | if (is_range) { |
| 92 | vregC = inst->VRegC_3rc(); |
| 93 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 94 | inst->GetVarArgs(arg, inst_data); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 95 | } |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 96 | |
| 97 | // Handle receiver apart since it's not part of the shorty. |
| 98 | size_t dest_reg = first_dest_reg; |
| 99 | size_t arg_offset = 0; |
| 100 | if (!method->IsStatic()) { |
| 101 | size_t receiver_reg = (is_range) ? vregC : arg[0]; |
| 102 | new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg)); |
| 103 | ++dest_reg; |
| 104 | ++arg_offset; |
| 105 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 106 | for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) { |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 107 | DCHECK_LT(shorty_pos + 1, mh.GetShortyLength()); |
| 108 | const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset]; |
| 109 | switch (shorty[shorty_pos + 1]) { |
| 110 | case 'L': { |
| 111 | Object* o = shadow_frame.GetVRegReference(src_reg); |
| 112 | if (do_assignability_check && o != NULL) { |
| 113 | Class* arg_type = mh.GetClassFromTypeIdx(params->GetTypeItem(shorty_pos).type_idx_); |
| 114 | if (arg_type == NULL) { |
| 115 | CHECK(self->IsExceptionPending()); |
| 116 | return false; |
| 117 | } |
| 118 | if (!o->VerifierInstanceOf(arg_type)) { |
| 119 | // This should never happen. |
| 120 | self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), |
| 121 | "Ljava/lang/VirtualMachineError;", |
| 122 | "Invoking %s with bad arg %d, type '%s' not instance of '%s'", |
| 123 | mh.GetName(), shorty_pos, |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 124 | o->GetClass()->GetDescriptor().c_str(), |
| 125 | arg_type->GetDescriptor().c_str()); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 126 | return false; |
| 127 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 128 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 129 | new_shadow_frame->SetVRegReference(dest_reg, o); |
| 130 | break; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 131 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 132 | case 'J': case 'D': { |
| 133 | uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) | |
| 134 | static_cast<uint32_t>(shadow_frame.GetVReg(src_reg)); |
| 135 | new_shadow_frame->SetVRegLong(dest_reg, wide_value); |
| 136 | ++dest_reg; |
| 137 | ++arg_offset; |
| 138 | break; |
| 139 | } |
| 140 | default: |
| 141 | new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg)); |
| 142 | break; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 143 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 144 | } |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 145 | // We're done with the construction. |
| 146 | self->ClearShadowFrameUnderConstruction(); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 147 | } else { |
| 148 | // Fast path: no extra checks. |
| 149 | if (is_range) { |
| 150 | const uint16_t first_src_reg = inst->VRegC_3rc(); |
| 151 | for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs; |
| 152 | ++dest_reg, ++src_reg) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 153 | AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 154 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 155 | } else { |
| 156 | DCHECK_LE(num_ins, 5U); |
| 157 | uint16_t regList = inst->Fetch16(2); |
| 158 | uint16_t count = num_ins; |
| 159 | if (count == 5) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 160 | AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U, (inst_data >> 8) & 0x0f); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 161 | --count; |
| 162 | } |
| 163 | for (size_t arg_index = 0; arg_index < count; ++arg_index, regList >>= 4) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 164 | AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, regList & 0x0f); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 165 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 166 | } |
Andreas Gampe | 2a0d4ec | 2014-06-02 22:05:22 -0700 | [diff] [blame] | 167 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 170 | // Do the call now. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 171 | if (LIKELY(Runtime::Current()->IsStarted())) { |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 172 | if (kIsDebugBuild && method->GetEntryPointFromInterpreter() == nullptr) { |
| 173 | LOG(FATAL) << "Attempt to invoke non-executable method: " << PrettyMethod(method); |
| 174 | } |
Hiroshi Yamauchi | 563b47c | 2014-02-28 17:18:37 -0800 | [diff] [blame] | 175 | if (kIsDebugBuild && Runtime::Current()->GetInstrumentation()->IsForcedInterpretOnly() && |
| 176 | !method->IsNative() && !method->IsProxyMethod() && |
| 177 | method->GetEntryPointFromInterpreter() == artInterpreterToCompiledCodeBridge) { |
| 178 | LOG(FATAL) << "Attempt to call compiled code when -Xint: " << PrettyMethod(method); |
| 179 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 180 | (method->GetEntryPointFromInterpreter())(self, mh, code_item, new_shadow_frame, result); |
| 181 | } else { |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 182 | UnstartedRuntimeInvoke(self, mh, code_item, new_shadow_frame, result, first_dest_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 183 | } |
| 184 | return !self->IsExceptionPending(); |
| 185 | } |
| 186 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 187 | template <bool is_range, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 188 | bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 189 | Thread* self, JValue* result) { |
| 190 | DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY || |
| 191 | inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE); |
| 192 | const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 193 | if (!is_range) { |
| 194 | // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments. |
| 195 | CHECK_LE(length, 5); |
| 196 | } |
| 197 | if (UNLIKELY(length < 0)) { |
| 198 | ThrowNegativeArraySizeException(length); |
| 199 | return false; |
| 200 | } |
| 201 | uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 202 | Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(), |
| 203 | self, false, do_access_check); |
| 204 | if (UNLIKELY(arrayClass == NULL)) { |
| 205 | DCHECK(self->IsExceptionPending()); |
| 206 | return false; |
| 207 | } |
| 208 | CHECK(arrayClass->IsArrayClass()); |
| 209 | Class* componentClass = arrayClass->GetComponentType(); |
| 210 | if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) { |
| 211 | if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) { |
| 212 | ThrowRuntimeException("Bad filled array request for type %s", |
| 213 | PrettyDescriptor(componentClass).c_str()); |
| 214 | } else { |
| 215 | self->ThrowNewExceptionF(shadow_frame.GetCurrentLocationForThrow(), |
| 216 | "Ljava/lang/InternalError;", |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 217 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 218 | PrettyDescriptor(componentClass).c_str()); |
| 219 | } |
| 220 | return false; |
| 221 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 222 | Object* newArray = Array::Alloc<true>(self, arrayClass, length, arrayClass->GetComponentSize(), |
| 223 | Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 224 | if (UNLIKELY(newArray == NULL)) { |
| 225 | DCHECK(self->IsExceptionPending()); |
| 226 | return false; |
| 227 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 228 | uint32_t arg[5]; // only used in filled-new-array. |
| 229 | uint32_t vregC; // only used in filled-new-array-range. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 230 | if (is_range) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 231 | vregC = inst->VRegC_3rc(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 232 | } else { |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 233 | inst->GetVarArgs(arg); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 234 | } |
| 235 | const bool is_primitive_int_component = componentClass->IsPrimitiveInt(); |
| 236 | for (int32_t i = 0; i < length; ++i) { |
| 237 | size_t src_reg = is_range ? vregC + i : arg[i]; |
| 238 | if (is_primitive_int_component) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 239 | newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 240 | } else { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 241 | newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | result->SetL(newArray); |
| 246 | return true; |
| 247 | } |
| 248 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 249 | // TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_). |
| 250 | template<typename T> |
| 251 | static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count) |
| 252 | NO_THREAD_SAFETY_ANALYSIS { |
| 253 | Runtime* runtime = Runtime::Current(); |
| 254 | for (int32_t i = 0; i < count; ++i) { |
| 255 | runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i)); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count) |
| 260 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 261 | DCHECK(Runtime::Current()->IsActiveTransaction()); |
| 262 | DCHECK(array != nullptr); |
| 263 | DCHECK_LE(count, array->GetLength()); |
| 264 | Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType(); |
| 265 | switch (primitive_component_type) { |
| 266 | case Primitive::kPrimBoolean: |
| 267 | RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count); |
| 268 | break; |
| 269 | case Primitive::kPrimByte: |
| 270 | RecordArrayElementsInTransactionImpl(array->AsByteArray(), count); |
| 271 | break; |
| 272 | case Primitive::kPrimChar: |
| 273 | RecordArrayElementsInTransactionImpl(array->AsCharArray(), count); |
| 274 | break; |
| 275 | case Primitive::kPrimShort: |
| 276 | RecordArrayElementsInTransactionImpl(array->AsShortArray(), count); |
| 277 | break; |
| 278 | case Primitive::kPrimInt: |
| 279 | case Primitive::kPrimFloat: |
| 280 | RecordArrayElementsInTransactionImpl(array->AsIntArray(), count); |
| 281 | break; |
| 282 | case Primitive::kPrimLong: |
| 283 | case Primitive::kPrimDouble: |
| 284 | RecordArrayElementsInTransactionImpl(array->AsLongArray(), count); |
| 285 | break; |
| 286 | default: |
| 287 | LOG(FATAL) << "Unsupported primitive type " << primitive_component_type |
| 288 | << " in fill-array-data"; |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 293 | static void UnstartedRuntimeInvoke(Thread* self, MethodHelper& mh, |
| 294 | const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame, |
| 295 | JValue* result, size_t arg_offset) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 296 | // In a runtime that's not started we intercept certain methods to avoid complicated dependency |
| 297 | // problems in core libraries. |
| 298 | std::string name(PrettyMethod(shadow_frame->GetMethod())); |
Kenny Root | fa31b3c | 2013-12-09 13:51:32 -0800 | [diff] [blame] | 299 | if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)" |
| 300 | || name == "java.lang.Class java.lang.VMClassLoader.loadClass(java.lang.String, boolean)") { |
| 301 | // TODO Class#forName should actually call Class::EnsureInitialized always. Support for the |
| 302 | // other variants that take more arguments should also be added. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 303 | std::string descriptor(DotToDescriptor(shadow_frame->GetVRegReference(arg_offset)->AsString()->ToModifiedUtf8().c_str())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 304 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 305 | // shadow_frame.GetMethod()->GetDeclaringClass()->GetClassLoader(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 306 | Class* found = Runtime::Current()->GetClassLinker()->FindClass( |
| 307 | self, descriptor.c_str(), NullHandle<mirror::ClassLoader>()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 308 | CHECK(found != NULL) << "Class.forName failed in un-started runtime for class: " |
| 309 | << PrettyDescriptor(descriptor); |
| 310 | result->SetL(found); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 311 | } else if (name == "java.lang.Class java.lang.Void.lookupType()") { |
| 312 | result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V')); |
Kenny Root | fa31b3c | 2013-12-09 13:51:32 -0800 | [diff] [blame] | 313 | } else if (name == "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)") { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 314 | StackHandleScope<1> hs(self); |
| 315 | Handle<ClassLoader> class_loader( |
| 316 | hs.NewHandle(down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset)))); |
Kenny Root | fa31b3c | 2013-12-09 13:51:32 -0800 | [diff] [blame] | 317 | std::string descriptor(DotToDescriptor(shadow_frame->GetVRegReference(arg_offset + 1)->AsString()->ToModifiedUtf8().c_str())); |
| 318 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 319 | Class* found = Runtime::Current()->GetClassLinker()->FindClass(self, descriptor.c_str(), |
Kenny Root | fa31b3c | 2013-12-09 13:51:32 -0800 | [diff] [blame] | 320 | class_loader); |
| 321 | result->SetL(found); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 322 | } else if (name == "java.lang.Object java.lang.Class.newInstance()") { |
| 323 | Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
| 324 | ArtMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V"); |
| 325 | CHECK(c != NULL); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 326 | StackHandleScope<1> hs(self); |
| 327 | Handle<Object> obj(hs.NewHandle(klass->AllocObject(self))); |
| 328 | CHECK(obj.Get() != NULL); |
| 329 | EnterInterpreterFromInvoke(self, c, obj.Get(), NULL, NULL); |
| 330 | result->SetL(obj.Get()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 331 | } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") { |
| 332 | // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail |
| 333 | // going the reflective Dex way. |
| 334 | Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
| 335 | String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
| 336 | ArtField* found = NULL; |
| 337 | FieldHelper fh; |
| 338 | ObjectArray<ArtField>* fields = klass->GetIFields(); |
| 339 | for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) { |
| 340 | ArtField* f = fields->Get(i); |
| 341 | fh.ChangeField(f); |
| 342 | if (name->Equals(fh.GetName())) { |
| 343 | found = f; |
| 344 | } |
| 345 | } |
| 346 | if (found == NULL) { |
| 347 | fields = klass->GetSFields(); |
| 348 | for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) { |
| 349 | ArtField* f = fields->Get(i); |
| 350 | fh.ChangeField(f); |
| 351 | if (name->Equals(fh.GetName())) { |
| 352 | found = f; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | CHECK(found != NULL) |
| 357 | << "Failed to find field in Class.getDeclaredField in un-started runtime. name=" |
| 358 | << name->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass); |
| 359 | // TODO: getDeclaredField calls GetType once the field is found to ensure a |
| 360 | // NoClassDefFoundError is thrown if the field's type cannot be resolved. |
| 361 | Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 362 | StackHandleScope<1> hs(self); |
| 363 | Handle<Object> field(hs.NewHandle(jlr_Field->AllocNonMovableObject(self))); |
| 364 | CHECK(field.Get() != NULL); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 365 | ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V"); |
| 366 | uint32_t args[1]; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 367 | args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 368 | EnterInterpreterFromInvoke(self, c, field.Get(), args, NULL); |
| 369 | result->SetL(field.Get()); |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 370 | } else if (name == "int java.lang.Object.hashCode()") { |
| 371 | Object* obj = shadow_frame->GetVRegReference(arg_offset); |
| 372 | result->SetI(obj->IdentityHashCode()); |
| 373 | } else if (name == "java.lang.String java.lang.reflect.ArtMethod.getMethodName(java.lang.reflect.ArtMethod)") { |
| 374 | ArtMethod* method = shadow_frame->GetVRegReference(arg_offset)->AsArtMethod(); |
| 375 | result->SetL(MethodHelper(method).GetNameAsString()); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 376 | } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" || |
| 377 | name == "void java.lang.System.arraycopy(char[], int, char[], int, int)") { |
| 378 | // Special case array copying without initializing System. |
| 379 | Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType(); |
| 380 | jint srcPos = shadow_frame->GetVReg(arg_offset + 1); |
| 381 | jint dstPos = shadow_frame->GetVReg(arg_offset + 3); |
| 382 | jint length = shadow_frame->GetVReg(arg_offset + 4); |
| 383 | if (!ctype->IsPrimitive()) { |
| 384 | ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>(); |
| 385 | ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>(); |
| 386 | for (jint i = 0; i < length; ++i) { |
| 387 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 388 | } |
| 389 | } else if (ctype->IsPrimitiveChar()) { |
| 390 | CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray(); |
| 391 | CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray(); |
| 392 | for (jint i = 0; i < length; ++i) { |
| 393 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 394 | } |
| 395 | } else if (ctype->IsPrimitiveInt()) { |
| 396 | IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray(); |
| 397 | IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray(); |
| 398 | for (jint i = 0; i < length; ++i) { |
| 399 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 400 | } |
| 401 | } else { |
Ian Rogers | c45b8b5 | 2014-05-03 01:39:59 -0700 | [diff] [blame] | 402 | self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;", |
| 403 | "Unimplemented System.arraycopy for type '%s'", |
| 404 | PrettyDescriptor(ctype).c_str()); |
| 405 | } |
| 406 | } else if (name == "java.lang.Object java.lang.ThreadLocal.get()") { |
| 407 | std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod())); |
| 408 | if (caller == "java.lang.String java.lang.IntegralToString.convertInt(java.lang.AbstractStringBuilder, int)") { |
| 409 | // Allocate non-threadlocal buffer. |
| 410 | result->SetL(mirror::CharArray::Alloc(self, 11)); |
| 411 | } else { |
| 412 | self->ThrowNewException(self->GetCurrentLocationForThrow(), "Ljava/lang/InternalError;", |
| 413 | "Unimplemented ThreadLocal.get"); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 414 | } |
| 415 | } else { |
| 416 | // Not special, continue with regular interpreter execution. |
| 417 | artInterpreterToInterpreterBridge(self, mh, code_item, shadow_frame, result); |
| 418 | } |
| 419 | } |
| 420 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 421 | // Explicit DoCall template function declarations. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 422 | #define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
| 423 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 424 | bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 425 | ShadowFrame& shadow_frame, \ |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 426 | const Instruction* inst, uint16_t inst_data, \ |
| 427 | JValue* result) |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 428 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); |
| 429 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); |
| 430 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); |
| 431 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); |
| 432 | #undef EXPLICIT_DO_CALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 433 | |
| 434 | // Explicit DoFilledNewArray template function declarations. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 435 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \ |
| 436 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
| 437 | bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \ |
| 438 | const ShadowFrame& shadow_frame, \ |
| 439 | Thread* self, JValue* result) |
| 440 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \ |
| 441 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \ |
| 442 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \ |
| 443 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \ |
| 444 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active) |
| 445 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false); |
| 446 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true); |
| 447 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 448 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL |
| 449 | |
| 450 | } // namespace interpreter |
| 451 | } // namespace art |