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. |
| 33 | int32_t src_value = shadow_frame.GetVReg(src_reg); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 34 | mirror::Object* o = shadow_frame.GetVRegReference<false>(src_reg); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 35 | if (src_value == reinterpret_cast<intptr_t>(o)) { |
| 36 | new_shadow_frame->SetVRegReference(dest_reg, o); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 37 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 38 | new_shadow_frame->SetVReg(dest_reg, src_value); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 42 | template<bool is_range, bool do_assignability_check> |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 43 | bool DoCall(ArtMethod* method, Thread* self, ShadowFrame& shadow_frame, |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 44 | const Instruction* inst, uint16_t inst_data, JValue* result) { |
| 45 | // Compute method information. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 46 | MethodHelper mh(method); |
| 47 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 48 | 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] | 49 | uint16_t num_regs; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 50 | if (LIKELY(code_item != NULL)) { |
| 51 | num_regs = code_item->registers_size_; |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 52 | DCHECK_EQ(num_ins, code_item->ins_size_); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 53 | } else { |
| 54 | DCHECK(method->IsNative() || method->IsProxyMethod()); |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 55 | num_regs = num_ins; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 58 | // Allocate shadow frame on the stack. |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 59 | const char* old_cause = self->StartAssertNoThreadSuspension("DoCall"); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 60 | void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); |
| 61 | 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] | 62 | |
| 63 | // Initialize new shadow frame. |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 64 | const size_t first_dest_reg = num_regs - num_ins; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 65 | if (do_assignability_check) { |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 66 | // Slow path: we need to do runtime check on reference assignment. We need to load the shorty |
| 67 | // to get the exact type of each reference argument. |
| 68 | const DexFile::TypeList* params = mh.GetParameterTypeList(); |
| 69 | const char* shorty = mh.GetShorty(); |
| 70 | |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 71 | // TODO: find a cleaner way to separate non-range and range information without duplicating code. |
| 72 | uint32_t arg[5]; // only used in invoke-XXX. |
| 73 | uint32_t vregC; // only used in invoke-XXX-range. |
| 74 | if (is_range) { |
| 75 | vregC = inst->VRegC_3rc(); |
| 76 | } else { |
| 77 | inst->GetArgs(arg, inst_data); |
| 78 | } |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 79 | |
| 80 | // Handle receiver apart since it's not part of the shorty. |
| 81 | size_t dest_reg = first_dest_reg; |
| 82 | size_t arg_offset = 0; |
| 83 | if (!method->IsStatic()) { |
| 84 | size_t receiver_reg = (is_range) ? vregC : arg[0]; |
| 85 | new_shadow_frame->SetVRegReference(dest_reg, shadow_frame.GetVRegReference(receiver_reg)); |
| 86 | ++dest_reg; |
| 87 | ++arg_offset; |
| 88 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 89 | 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] | 90 | DCHECK_LT(shorty_pos + 1, mh.GetShortyLength()); |
| 91 | const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset]; |
| 92 | switch (shorty[shorty_pos + 1]) { |
| 93 | case 'L': { |
| 94 | Object* o = shadow_frame.GetVRegReference(src_reg); |
| 95 | if (do_assignability_check && o != NULL) { |
| 96 | Class* arg_type = mh.GetClassFromTypeIdx(params->GetTypeItem(shorty_pos).type_idx_); |
| 97 | if (arg_type == NULL) { |
| 98 | CHECK(self->IsExceptionPending()); |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 99 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | if (!o->VerifierInstanceOf(arg_type)) { |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 103 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 104 | // This should never happen. |
| 105 | self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), |
| 106 | "Ljava/lang/VirtualMachineError;", |
| 107 | "Invoking %s with bad arg %d, type '%s' not instance of '%s'", |
| 108 | mh.GetName(), shorty_pos, |
| 109 | ClassHelper(o->GetClass()).GetDescriptor(), |
| 110 | ClassHelper(arg_type).GetDescriptor()); |
| 111 | return false; |
| 112 | } |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 113 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 114 | new_shadow_frame->SetVRegReference(dest_reg, o); |
| 115 | break; |
Jeff Hao | a3faaf4 | 2013-09-03 19:07:00 -0700 | [diff] [blame] | 116 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 117 | case 'J': case 'D': { |
| 118 | uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) | |
| 119 | static_cast<uint32_t>(shadow_frame.GetVReg(src_reg)); |
| 120 | new_shadow_frame->SetVRegLong(dest_reg, wide_value); |
| 121 | ++dest_reg; |
| 122 | ++arg_offset; |
| 123 | break; |
| 124 | } |
| 125 | default: |
| 126 | new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg)); |
| 127 | break; |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 128 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 129 | } |
| 130 | } else { |
| 131 | // Fast path: no extra checks. |
| 132 | if (is_range) { |
| 133 | const uint16_t first_src_reg = inst->VRegC_3rc(); |
| 134 | for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs; |
| 135 | ++dest_reg, ++src_reg) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 136 | AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 137 | } |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 138 | } else { |
| 139 | DCHECK_LE(num_ins, 5U); |
| 140 | uint16_t regList = inst->Fetch16(2); |
| 141 | uint16_t count = num_ins; |
| 142 | if (count == 5) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 143 | 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] | 144 | --count; |
| 145 | } |
| 146 | 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] | 147 | 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] | 148 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 149 | } |
| 150 | } |
Mathieu Chartier | e861ebd | 2013-10-09 15:01:21 -0700 | [diff] [blame] | 151 | self->EndAssertNoThreadSuspension(old_cause); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 152 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 153 | // Do the call now. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 154 | if (LIKELY(Runtime::Current()->IsStarted())) { |
Ian Rogers | 1d99e45 | 2014-01-02 17:36:41 -0800 | [diff] [blame] | 155 | if (kIsDebugBuild && method->GetEntryPointFromInterpreter() == nullptr) { |
| 156 | LOG(FATAL) << "Attempt to invoke non-executable method: " << PrettyMethod(method); |
| 157 | } |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 158 | (method->GetEntryPointFromInterpreter())(self, mh, code_item, new_shadow_frame, result); |
| 159 | } else { |
Sebastien Hertz | 9ace87b | 2013-09-27 11:48:09 +0200 | [diff] [blame] | 160 | UnstartedRuntimeInvoke(self, mh, code_item, new_shadow_frame, result, first_dest_reg); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 161 | } |
| 162 | return !self->IsExceptionPending(); |
| 163 | } |
| 164 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 165 | template <bool is_range, bool do_access_check, bool transaction_active> |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 166 | bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, |
| 167 | Thread* self, JValue* result) { |
| 168 | DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY || |
| 169 | inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE); |
| 170 | const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c(); |
| 171 | if (!is_range) { |
| 172 | // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments. |
| 173 | CHECK_LE(length, 5); |
| 174 | } |
| 175 | if (UNLIKELY(length < 0)) { |
| 176 | ThrowNegativeArraySizeException(length); |
| 177 | return false; |
| 178 | } |
| 179 | uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); |
| 180 | Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(), |
| 181 | self, false, do_access_check); |
| 182 | if (UNLIKELY(arrayClass == NULL)) { |
| 183 | DCHECK(self->IsExceptionPending()); |
| 184 | return false; |
| 185 | } |
| 186 | CHECK(arrayClass->IsArrayClass()); |
| 187 | Class* componentClass = arrayClass->GetComponentType(); |
| 188 | if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) { |
| 189 | if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) { |
| 190 | ThrowRuntimeException("Bad filled array request for type %s", |
| 191 | PrettyDescriptor(componentClass).c_str()); |
| 192 | } else { |
| 193 | self->ThrowNewExceptionF(shadow_frame.GetCurrentLocationForThrow(), |
| 194 | "Ljava/lang/InternalError;", |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 195 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 196 | PrettyDescriptor(componentClass).c_str()); |
| 197 | } |
| 198 | return false; |
| 199 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 200 | Object* newArray = Array::Alloc<true>(self, arrayClass, length); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 201 | if (UNLIKELY(newArray == NULL)) { |
| 202 | DCHECK(self->IsExceptionPending()); |
| 203 | return false; |
| 204 | } |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 205 | uint32_t arg[5]; // only used in filled-new-array. |
| 206 | uint32_t vregC; // only used in filled-new-array-range. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 207 | if (is_range) { |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 208 | vregC = inst->VRegC_3rc(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 209 | } else { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 210 | inst->GetArgs(arg); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 211 | } |
| 212 | const bool is_primitive_int_component = componentClass->IsPrimitiveInt(); |
| 213 | for (int32_t i = 0; i < length; ++i) { |
| 214 | size_t src_reg = is_range ? vregC + i : arg[i]; |
| 215 | if (is_primitive_int_component) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 216 | newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg)); |
Sebastien Hertz | abff643 | 2014-01-27 18:01:39 +0100 | [diff] [blame] | 217 | } else { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 218 | newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | result->SetL(newArray); |
| 223 | return true; |
| 224 | } |
| 225 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 226 | // TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_). |
| 227 | template<typename T> |
| 228 | static void RecordArrayElementsInTransactionImpl(mirror::PrimitiveArray<T>* array, int32_t count) |
| 229 | NO_THREAD_SAFETY_ANALYSIS { |
| 230 | Runtime* runtime = Runtime::Current(); |
| 231 | for (int32_t i = 0; i < count; ++i) { |
| 232 | runtime->RecordWriteArray(array, i, array->GetWithoutChecks(i)); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void RecordArrayElementsInTransaction(mirror::Array* array, int32_t count) |
| 237 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 238 | DCHECK(Runtime::Current()->IsActiveTransaction()); |
| 239 | DCHECK(array != nullptr); |
| 240 | DCHECK_LE(count, array->GetLength()); |
| 241 | Primitive::Type primitive_component_type = array->GetClass()->GetComponentType()->GetPrimitiveType(); |
| 242 | switch (primitive_component_type) { |
| 243 | case Primitive::kPrimBoolean: |
| 244 | RecordArrayElementsInTransactionImpl(array->AsBooleanArray(), count); |
| 245 | break; |
| 246 | case Primitive::kPrimByte: |
| 247 | RecordArrayElementsInTransactionImpl(array->AsByteArray(), count); |
| 248 | break; |
| 249 | case Primitive::kPrimChar: |
| 250 | RecordArrayElementsInTransactionImpl(array->AsCharArray(), count); |
| 251 | break; |
| 252 | case Primitive::kPrimShort: |
| 253 | RecordArrayElementsInTransactionImpl(array->AsShortArray(), count); |
| 254 | break; |
| 255 | case Primitive::kPrimInt: |
| 256 | case Primitive::kPrimFloat: |
| 257 | RecordArrayElementsInTransactionImpl(array->AsIntArray(), count); |
| 258 | break; |
| 259 | case Primitive::kPrimLong: |
| 260 | case Primitive::kPrimDouble: |
| 261 | RecordArrayElementsInTransactionImpl(array->AsLongArray(), count); |
| 262 | break; |
| 263 | default: |
| 264 | LOG(FATAL) << "Unsupported primitive type " << primitive_component_type |
| 265 | << " in fill-array-data"; |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 270 | static void UnstartedRuntimeInvoke(Thread* self, MethodHelper& mh, |
| 271 | const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame, |
| 272 | JValue* result, size_t arg_offset) { |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 273 | // In a runtime that's not started we intercept certain methods to avoid complicated dependency |
| 274 | // problems in core libraries. |
| 275 | std::string name(PrettyMethod(shadow_frame->GetMethod())); |
Kenny Root | fa31b3c | 2013-12-09 13:51:32 -0800 | [diff] [blame] | 276 | if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)" |
| 277 | || name == "java.lang.Class java.lang.VMClassLoader.loadClass(java.lang.String, boolean)") { |
| 278 | // TODO Class#forName should actually call Class::EnsureInitialized always. Support for the |
| 279 | // other variants that take more arguments should also be added. |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 280 | 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] | 281 | |
| 282 | SirtRef<ClassLoader> class_loader(self, nullptr); // shadow_frame.GetMethod()->GetDeclaringClass()->GetClassLoader(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 283 | Class* found = Runtime::Current()->GetClassLinker()->FindClass(descriptor.c_str(), |
| 284 | class_loader); |
| 285 | CHECK(found != NULL) << "Class.forName failed in un-started runtime for class: " |
| 286 | << PrettyDescriptor(descriptor); |
| 287 | result->SetL(found); |
Kenny Root | fa31b3c | 2013-12-09 13:51:32 -0800 | [diff] [blame] | 288 | } else if (name == "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)") { |
| 289 | SirtRef<ClassLoader> class_loader(self, down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset))); |
| 290 | std::string descriptor(DotToDescriptor(shadow_frame->GetVRegReference(arg_offset + 1)->AsString()->ToModifiedUtf8().c_str())); |
| 291 | |
| 292 | Class* found = Runtime::Current()->GetClassLinker()->FindClass(descriptor.c_str(), |
| 293 | class_loader); |
| 294 | result->SetL(found); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 295 | } else if (name == "java.lang.Object java.lang.Class.newInstance()") { |
| 296 | Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
| 297 | ArtMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V"); |
| 298 | CHECK(c != NULL); |
| 299 | SirtRef<Object> obj(self, klass->AllocObject(self)); |
| 300 | CHECK(obj.get() != NULL); |
| 301 | EnterInterpreterFromInvoke(self, c, obj.get(), NULL, NULL); |
| 302 | result->SetL(obj.get()); |
| 303 | } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") { |
| 304 | // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail |
| 305 | // going the reflective Dex way. |
| 306 | Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
| 307 | String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
| 308 | ArtField* found = NULL; |
| 309 | FieldHelper fh; |
| 310 | ObjectArray<ArtField>* fields = klass->GetIFields(); |
| 311 | for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) { |
| 312 | ArtField* f = fields->Get(i); |
| 313 | fh.ChangeField(f); |
| 314 | if (name->Equals(fh.GetName())) { |
| 315 | found = f; |
| 316 | } |
| 317 | } |
| 318 | if (found == NULL) { |
| 319 | fields = klass->GetSFields(); |
| 320 | for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) { |
| 321 | ArtField* f = fields->Get(i); |
| 322 | fh.ChangeField(f); |
| 323 | if (name->Equals(fh.GetName())) { |
| 324 | found = f; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | CHECK(found != NULL) |
| 329 | << "Failed to find field in Class.getDeclaredField in un-started runtime. name=" |
| 330 | << name->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass); |
| 331 | // TODO: getDeclaredField calls GetType once the field is found to ensure a |
| 332 | // NoClassDefFoundError is thrown if the field's type cannot be resolved. |
| 333 | Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass(); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 334 | SirtRef<Object> field(self, jlr_Field->AllocNonMovableObject(self)); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 335 | CHECK(field.get() != NULL); |
| 336 | ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V"); |
| 337 | uint32_t args[1]; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 338 | args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue(); |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 339 | EnterInterpreterFromInvoke(self, c, field.get(), args, NULL); |
| 340 | result->SetL(field.get()); |
| 341 | } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" || |
| 342 | name == "void java.lang.System.arraycopy(char[], int, char[], int, int)") { |
| 343 | // Special case array copying without initializing System. |
| 344 | Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType(); |
| 345 | jint srcPos = shadow_frame->GetVReg(arg_offset + 1); |
| 346 | jint dstPos = shadow_frame->GetVReg(arg_offset + 3); |
| 347 | jint length = shadow_frame->GetVReg(arg_offset + 4); |
| 348 | if (!ctype->IsPrimitive()) { |
| 349 | ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>(); |
| 350 | ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>(); |
| 351 | for (jint i = 0; i < length; ++i) { |
| 352 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 353 | } |
| 354 | } else if (ctype->IsPrimitiveChar()) { |
| 355 | CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray(); |
| 356 | CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray(); |
| 357 | for (jint i = 0; i < length; ++i) { |
| 358 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 359 | } |
| 360 | } else if (ctype->IsPrimitiveInt()) { |
| 361 | IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray(); |
| 362 | IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray(); |
| 363 | for (jint i = 0; i < length; ++i) { |
| 364 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 365 | } |
| 366 | } else { |
| 367 | UNIMPLEMENTED(FATAL) << "System.arraycopy of unexpected type: " << PrettyDescriptor(ctype); |
| 368 | } |
| 369 | } else { |
| 370 | // Not special, continue with regular interpreter execution. |
| 371 | artInterpreterToInterpreterBridge(self, mh, code_item, shadow_frame, result); |
| 372 | } |
| 373 | } |
| 374 | |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 375 | // Explicit DoCall template function declarations. |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 376 | #define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ |
| 377 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
Sebastien Hertz | 9119c5f | 2013-12-16 11:31:45 +0100 | [diff] [blame] | 378 | bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ |
| 379 | ShadowFrame& shadow_frame, \ |
Sebastien Hertz | c671485 | 2013-09-30 16:42:32 +0200 | [diff] [blame] | 380 | const Instruction* inst, uint16_t inst_data, \ |
| 381 | JValue* result) |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 382 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); |
| 383 | EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); |
| 384 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); |
| 385 | EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); |
| 386 | #undef EXPLICIT_DO_CALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 387 | |
| 388 | // Explicit DoFilledNewArray template function declarations. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 389 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check, _transaction_active) \ |
| 390 | template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ |
| 391 | bool DoFilledNewArray<_is_range_, _check, _transaction_active>(const Instruction* inst, \ |
| 392 | const ShadowFrame& shadow_frame, \ |
| 393 | Thread* self, JValue* result) |
| 394 | #define EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(_transaction_active) \ |
| 395 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false, _transaction_active); \ |
| 396 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true, _transaction_active); \ |
| 397 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false, _transaction_active); \ |
| 398 | EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true, _transaction_active) |
| 399 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false); |
| 400 | EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true); |
| 401 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL |
Sebastien Hertz | 8ece050 | 2013-08-07 11:26:41 +0200 | [diff] [blame] | 402 | #undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL |
| 403 | |
| 404 | } // namespace interpreter |
| 405 | } // namespace art |