interpreter_common.cc revision 4e30541a92381fb280cd0be9a1763b713ee4d64c
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" 18#include "mirror/array-inl.h" 19 20namespace art { 21namespace interpreter { 22 23static 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_); 27 28// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame. 29static 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_) { 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); 34 mirror::Object* o = shadow_frame.GetVRegReference<kVerifyNone>(src_reg); 35 if (src_value == reinterpret_cast<intptr_t>(o)) { 36 new_shadow_frame->SetVRegReference(dest_reg, o); 37 } else { 38 new_shadow_frame->SetVReg(dest_reg, src_value); 39 } 40} 41 42template<bool is_range, bool do_assignability_check> 43bool DoCall(ArtMethod* method, Thread* self, ShadowFrame& shadow_frame, 44 const Instruction* inst, uint16_t inst_data, JValue* result) { 45 // Compute method information. 46 MethodHelper mh(method); 47 const DexFile::CodeItem* code_item = mh.GetCodeItem(); 48 const uint16_t num_ins = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data); 49 uint16_t num_regs; 50 if (LIKELY(code_item != NULL)) { 51 num_regs = code_item->registers_size_; 52 DCHECK_EQ(num_ins, code_item->ins_size_); 53 } else { 54 DCHECK(method->IsNative() || method->IsProxyMethod()); 55 num_regs = num_ins; 56 } 57 58 // Allocate shadow frame on the stack. 59 const char* old_cause = self->StartAssertNoThreadSuspension("DoCall"); 60 void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); 61 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, method, 0, memory)); 62 63 // Initialize new shadow frame. 64 const size_t first_dest_reg = num_regs - num_ins; 65 if (do_assignability_check) { 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 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 } 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 } 89 for (uint32_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) { 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()); 99 self->EndAssertNoThreadSuspension(old_cause); 100 return false; 101 } 102 if (!o->VerifierInstanceOf(arg_type)) { 103 self->EndAssertNoThreadSuspension(old_cause); 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 } 113 } 114 new_shadow_frame->SetVRegReference(dest_reg, o); 115 break; 116 } 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; 128 } 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) { 136 AssignRegister(new_shadow_frame, shadow_frame, dest_reg, src_reg); 137 } 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) { 143 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + 4U, (inst_data >> 8) & 0x0f); 144 --count; 145 } 146 for (size_t arg_index = 0; arg_index < count; ++arg_index, regList >>= 4) { 147 AssignRegister(new_shadow_frame, shadow_frame, first_dest_reg + arg_index, regList & 0x0f); 148 } 149 } 150 } 151 self->EndAssertNoThreadSuspension(old_cause); 152 153 // Do the call now. 154 if (LIKELY(Runtime::Current()->IsStarted())) { 155 if (kIsDebugBuild && method->GetEntryPointFromInterpreter() == nullptr) { 156 LOG(FATAL) << "Attempt to invoke non-executable method: " << PrettyMethod(method); 157 } 158 (method->GetEntryPointFromInterpreter())(self, mh, code_item, new_shadow_frame, result); 159 } else { 160 UnstartedRuntimeInvoke(self, mh, code_item, new_shadow_frame, result, first_dest_reg); 161 } 162 return !self->IsExceptionPending(); 163} 164 165template <bool is_range, bool do_access_check, bool transaction_active> 166bool 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;", 195 "Found type %s; filled-new-array not implemented for anything but 'int'", 196 PrettyDescriptor(componentClass).c_str()); 197 } 198 return false; 199 } 200 Object* newArray = Array::Alloc<true>(self, arrayClass, length); 201 if (UNLIKELY(newArray == NULL)) { 202 DCHECK(self->IsExceptionPending()); 203 return false; 204 } 205 uint32_t arg[5]; // only used in filled-new-array. 206 uint32_t vregC; // only used in filled-new-array-range. 207 if (is_range) { 208 vregC = inst->VRegC_3rc(); 209 } else { 210 inst->GetArgs(arg); 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) { 216 newArray->AsIntArray()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVReg(src_reg)); 217 } else { 218 newArray->AsObjectArray<Object>()->SetWithoutChecks<transaction_active>(i, shadow_frame.GetVRegReference(src_reg)); 219 } 220 } 221 222 result->SetL(newArray); 223 return true; 224} 225 226// TODO fix thread analysis: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_). 227template<typename T> 228static 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 236void 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 270static void UnstartedRuntimeInvoke(Thread* self, MethodHelper& mh, 271 const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame, 272 JValue* result, size_t arg_offset) { 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())); 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. 280 std::string descriptor(DotToDescriptor(shadow_frame->GetVRegReference(arg_offset)->AsString()->ToModifiedUtf8().c_str())); 281 282 SirtRef<ClassLoader> class_loader(self, nullptr); // shadow_frame.GetMethod()->GetDeclaringClass()->GetClassLoader(); 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); 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); 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(); 334 SirtRef<Object> field(self, jlr_Field->AllocNonMovableObject(self)); 335 CHECK(field.get() != NULL); 336 ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V"); 337 uint32_t args[1]; 338 args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue(); 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 375// Explicit DoCall template function declarations. 376#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ 377 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ 378 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Thread* self, \ 379 ShadowFrame& shadow_frame, \ 380 const Instruction* inst, uint16_t inst_data, \ 381 JValue* result) 382EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); 383EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); 384EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); 385EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); 386#undef EXPLICIT_DO_CALL_TEMPLATE_DECL 387 388// Explicit DoFilledNewArray template function declarations. 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) 399EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(false); 400EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL(true); 401#undef EXPLICIT_DO_FILLED_NEW_ARRAY_ALL_TEMPLATE_DECL 402#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL 403 404} // namespace interpreter 405} // namespace art 406