interpreter_common.cc revision cbb2d20bea2861f244da2e2318d8c088300a3710
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 19namespace art { 20namespace interpreter { 21 22static void UnstartedRuntimeInvoke(Thread* self, MethodHelper& mh, 23 const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame, 24 JValue* result, size_t arg_offset) 25 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 26 27// Assign register 'src_reg' from shadow_frame to register 'dest_reg' into new_shadow_frame. 28static inline void AssignRegister(ShadowFrame& new_shadow_frame, const ShadowFrame& shadow_frame, 29 size_t dest_reg, size_t src_reg) { 30 // If both register locations contains the same value, the register probably holds a reference. 31 int32_t src_value = shadow_frame.GetVReg(src_reg); 32 mirror::Object* o = shadow_frame.GetVRegReference<false>(src_reg); 33 if (src_value == reinterpret_cast<int32_t>(o)) { 34 new_shadow_frame.SetVRegReference(dest_reg, o); 35 } else { 36 new_shadow_frame.SetVReg(dest_reg, src_value); 37 } 38} 39 40template<bool is_range, bool do_assignability_check> 41bool DoCall(ArtMethod* method, Object* receiver, Thread* self, ShadowFrame& shadow_frame, 42 const Instruction* inst, uint16_t inst_data, JValue* result) { 43 // Compute method information. 44 MethodHelper mh(method); 45 const DexFile::CodeItem* code_item = mh.GetCodeItem(); 46 const uint16_t num_ins = (is_range) ? inst->VRegA_3rc(inst_data) : inst->VRegA_35c(inst_data); 47 uint16_t num_regs; 48 if (LIKELY(code_item != NULL)) { 49 num_regs = code_item->registers_size_; 50 DCHECK_EQ(num_ins, code_item->ins_size_); 51 } else { 52 DCHECK(method->IsNative() || method->IsProxyMethod()); 53 num_regs = num_ins; 54 } 55 56 // Allocate shadow frame on the stack. 57 const char* old_cause = self->StartAssertNoThreadSuspension("DoCall"); 58 void* memory = alloca(ShadowFrame::ComputeSize(num_regs)); 59 ShadowFrame* new_shadow_frame(ShadowFrame::Create(num_regs, &shadow_frame, method, 0, memory)); 60 61 // Initialize new shadow frame. 62 const size_t first_dest_reg = num_regs - num_ins; 63 if (do_assignability_check) { 64 // Slow path: we need to do runtime check on reference assignment. We need to load the shorty 65 // to get the exact type of each reference argument. 66 const DexFile::TypeList* params = mh.GetParameterTypeList(); 67 const char* shorty = mh.GetShorty(); 68 69 // Handle receiver apart since it's not part of the shorty. 70 size_t dest_reg = first_dest_reg; 71 size_t arg_offset = 0; 72 if (receiver != NULL) { 73 DCHECK(!method->IsStatic()); 74 new_shadow_frame->SetVRegReference(dest_reg, receiver); 75 ++dest_reg; 76 ++arg_offset; 77 } else { 78 DCHECK(method->IsStatic()); 79 } 80 // TODO: find a cleaner way to separate non-range and range information without duplicating code. 81 uint32_t arg[5]; // only used in invoke-XXX. 82 uint32_t vregC; // only used in invoke-XXX-range. 83 if (is_range) { 84 vregC = inst->VRegC_3rc(); 85 } else { 86 inst->GetArgs(arg, inst_data); 87 } 88 for (size_t shorty_pos = 0; dest_reg < num_regs; ++shorty_pos, ++dest_reg, ++arg_offset) { 89 DCHECK_LT(shorty_pos + 1, mh.GetShortyLength()); 90 const size_t src_reg = (is_range) ? vregC + arg_offset : arg[arg_offset]; 91 switch (shorty[shorty_pos + 1]) { 92 case 'L': { 93 Object* o = shadow_frame.GetVRegReference(src_reg); 94 if (do_assignability_check && o != NULL) { 95 Class* arg_type = mh.GetClassFromTypeIdx(params->GetTypeItem(shorty_pos).type_idx_); 96 if (arg_type == NULL) { 97 CHECK(self->IsExceptionPending()); 98 self->EndAssertNoThreadSuspension(old_cause); 99 return false; 100 } 101 if (!o->VerifierInstanceOf(arg_type)) { 102 self->EndAssertNoThreadSuspension(old_cause); 103 // This should never happen. 104 self->ThrowNewExceptionF(self->GetCurrentLocationForThrow(), 105 "Ljava/lang/VirtualMachineError;", 106 "Invoking %s with bad arg %d, type '%s' not instance of '%s'", 107 mh.GetName(), shorty_pos, 108 ClassHelper(o->GetClass()).GetDescriptor(), 109 ClassHelper(arg_type).GetDescriptor()); 110 return false; 111 } 112 } 113 new_shadow_frame->SetVRegReference(dest_reg, o); 114 break; 115 } 116 case 'J': case 'D': { 117 uint64_t wide_value = (static_cast<uint64_t>(shadow_frame.GetVReg(src_reg + 1)) << 32) | 118 static_cast<uint32_t>(shadow_frame.GetVReg(src_reg)); 119 new_shadow_frame->SetVRegLong(dest_reg, wide_value); 120 ++dest_reg; 121 ++arg_offset; 122 break; 123 } 124 default: 125 new_shadow_frame->SetVReg(dest_reg, shadow_frame.GetVReg(src_reg)); 126 break; 127 } 128 } 129 } else { 130 // Fast path: no extra checks. 131 if (is_range) { 132 const uint16_t first_src_reg = inst->VRegC_3rc(); 133 for (size_t src_reg = first_src_reg, dest_reg = first_dest_reg; dest_reg < num_regs; 134 ++dest_reg, ++src_reg) { 135 AssignRegister(*new_shadow_frame, shadow_frame, dest_reg, src_reg); 136 } 137 } else { 138 DCHECK_LE(num_ins, 5U); 139 uint16_t regList = inst->Fetch16(2); 140 uint16_t count = num_ins; 141 if (count == 5) { 142 AssignRegister(*new_shadow_frame, shadow_frame, first_dest_reg + 4U, (inst_data >> 8) & 0x0f); 143 --count; 144 } 145 for (size_t arg_index = 0; arg_index < count; ++arg_index, regList >>= 4) { 146 AssignRegister(*new_shadow_frame, shadow_frame, first_dest_reg + arg_index, regList & 0x0f); 147 } 148 } 149 } 150 self->EndAssertNoThreadSuspension(old_cause); 151 152 // Do the call now. 153 if (LIKELY(Runtime::Current()->IsStarted())) { 154 (method->GetEntryPointFromInterpreter())(self, mh, code_item, new_shadow_frame, result); 155 } else { 156 UnstartedRuntimeInvoke(self, mh, code_item, new_shadow_frame, result, first_dest_reg); 157 } 158 return !self->IsExceptionPending(); 159} 160 161template <bool is_range, bool do_access_check> 162bool DoFilledNewArray(const Instruction* inst, const ShadowFrame& shadow_frame, 163 Thread* self, JValue* result) { 164 DCHECK(inst->Opcode() == Instruction::FILLED_NEW_ARRAY || 165 inst->Opcode() == Instruction::FILLED_NEW_ARRAY_RANGE); 166 const int32_t length = is_range ? inst->VRegA_3rc() : inst->VRegA_35c(); 167 if (!is_range) { 168 // Checks FILLED_NEW_ARRAY's length does not exceed 5 arguments. 169 CHECK_LE(length, 5); 170 } 171 if (UNLIKELY(length < 0)) { 172 ThrowNegativeArraySizeException(length); 173 return false; 174 } 175 uint16_t type_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c(); 176 Class* arrayClass = ResolveVerifyAndClinit(type_idx, shadow_frame.GetMethod(), 177 self, false, do_access_check); 178 if (UNLIKELY(arrayClass == NULL)) { 179 DCHECK(self->IsExceptionPending()); 180 return false; 181 } 182 CHECK(arrayClass->IsArrayClass()); 183 Class* componentClass = arrayClass->GetComponentType(); 184 if (UNLIKELY(componentClass->IsPrimitive() && !componentClass->IsPrimitiveInt())) { 185 if (componentClass->IsPrimitiveLong() || componentClass->IsPrimitiveDouble()) { 186 ThrowRuntimeException("Bad filled array request for type %s", 187 PrettyDescriptor(componentClass).c_str()); 188 } else { 189 self->ThrowNewExceptionF(shadow_frame.GetCurrentLocationForThrow(), 190 "Ljava/lang/InternalError;", 191 "Found type %s; filled-new-array not implemented for anything but \'int\'", 192 PrettyDescriptor(componentClass).c_str()); 193 } 194 return false; 195 } 196 Object* newArray = Array::Alloc<true>(self, arrayClass, length); 197 if (UNLIKELY(newArray == NULL)) { 198 DCHECK(self->IsExceptionPending()); 199 return false; 200 } 201 if (is_range) { 202 uint32_t vregC = inst->VRegC_3rc(); 203 const bool is_primitive_int_component = componentClass->IsPrimitiveInt(); 204 for (int32_t i = 0; i < length; ++i) { 205 if (is_primitive_int_component) { 206 newArray->AsIntArray()->Set(i, shadow_frame.GetVReg(vregC + i)); 207 } else { 208 newArray->AsObjectArray<Object>()->Set(i, shadow_frame.GetVRegReference(vregC + i)); 209 } 210 } 211 } else { 212 uint32_t arg[5]; 213 inst->GetArgs(arg); 214 const bool is_primitive_int_component = componentClass->IsPrimitiveInt(); 215 for (int32_t i = 0; i < length; ++i) { 216 if (is_primitive_int_component) { 217 newArray->AsIntArray()->Set(i, shadow_frame.GetVReg(arg[i])); 218 } else { 219 newArray->AsObjectArray<Object>()->Set(i, shadow_frame.GetVRegReference(arg[i])); 220 } 221 } 222 } 223 224 result->SetL(newArray); 225 return true; 226} 227 228static void UnstartedRuntimeInvoke(Thread* self, MethodHelper& mh, 229 const DexFile::CodeItem* code_item, ShadowFrame* shadow_frame, 230 JValue* result, size_t arg_offset) { 231 // In a runtime that's not started we intercept certain methods to avoid complicated dependency 232 // problems in core libraries. 233 std::string name(PrettyMethod(shadow_frame->GetMethod())); 234 if (name == "java.lang.Class java.lang.Class.forName(java.lang.String)") { 235 std::string descriptor(DotToDescriptor(shadow_frame->GetVRegReference(arg_offset)->AsString()->ToModifiedUtf8().c_str())); 236 237 SirtRef<ClassLoader> class_loader(self, nullptr); // shadow_frame.GetMethod()->GetDeclaringClass()->GetClassLoader(); 238 Class* found = Runtime::Current()->GetClassLinker()->FindClass(descriptor.c_str(), 239 class_loader); 240 CHECK(found != NULL) << "Class.forName failed in un-started runtime for class: " 241 << PrettyDescriptor(descriptor); 242 result->SetL(found); 243 } else if (name == "java.lang.Object java.lang.Class.newInstance()") { 244 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); 245 ArtMethod* c = klass->FindDeclaredDirectMethod("<init>", "()V"); 246 CHECK(c != NULL); 247 SirtRef<Object> obj(self, klass->AllocObject(self)); 248 CHECK(obj.get() != NULL); 249 EnterInterpreterFromInvoke(self, c, obj.get(), NULL, NULL); 250 result->SetL(obj.get()); 251 } else if (name == "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)") { 252 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail 253 // going the reflective Dex way. 254 Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); 255 String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); 256 ArtField* found = NULL; 257 FieldHelper fh; 258 ObjectArray<ArtField>* fields = klass->GetIFields(); 259 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) { 260 ArtField* f = fields->Get(i); 261 fh.ChangeField(f); 262 if (name->Equals(fh.GetName())) { 263 found = f; 264 } 265 } 266 if (found == NULL) { 267 fields = klass->GetSFields(); 268 for (int32_t i = 0; i < fields->GetLength() && found == NULL; ++i) { 269 ArtField* f = fields->Get(i); 270 fh.ChangeField(f); 271 if (name->Equals(fh.GetName())) { 272 found = f; 273 } 274 } 275 } 276 CHECK(found != NULL) 277 << "Failed to find field in Class.getDeclaredField in un-started runtime. name=" 278 << name->ToModifiedUtf8() << " class=" << PrettyDescriptor(klass); 279 // TODO: getDeclaredField calls GetType once the field is found to ensure a 280 // NoClassDefFoundError is thrown if the field's type cannot be resolved. 281 Class* jlr_Field = self->DecodeJObject(WellKnownClasses::java_lang_reflect_Field)->AsClass(); 282 SirtRef<Object> field(self, jlr_Field->AllocNonMovableObject(self)); 283 CHECK(field.get() != NULL); 284 ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", "(Ljava/lang/reflect/ArtField;)V"); 285 uint32_t args[1]; 286 args[0] = reinterpret_cast<uint32_t>(found); 287 EnterInterpreterFromInvoke(self, c, field.get(), args, NULL); 288 result->SetL(field.get()); 289 } else if (name == "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)" || 290 name == "void java.lang.System.arraycopy(char[], int, char[], int, int)") { 291 // Special case array copying without initializing System. 292 Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType(); 293 jint srcPos = shadow_frame->GetVReg(arg_offset + 1); 294 jint dstPos = shadow_frame->GetVReg(arg_offset + 3); 295 jint length = shadow_frame->GetVReg(arg_offset + 4); 296 if (!ctype->IsPrimitive()) { 297 ObjectArray<Object>* src = shadow_frame->GetVRegReference(arg_offset)->AsObjectArray<Object>(); 298 ObjectArray<Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<Object>(); 299 for (jint i = 0; i < length; ++i) { 300 dst->Set(dstPos + i, src->Get(srcPos + i)); 301 } 302 } else if (ctype->IsPrimitiveChar()) { 303 CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray(); 304 CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray(); 305 for (jint i = 0; i < length; ++i) { 306 dst->Set(dstPos + i, src->Get(srcPos + i)); 307 } 308 } else if (ctype->IsPrimitiveInt()) { 309 IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray(); 310 IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray(); 311 for (jint i = 0; i < length; ++i) { 312 dst->Set(dstPos + i, src->Get(srcPos + i)); 313 } 314 } else { 315 UNIMPLEMENTED(FATAL) << "System.arraycopy of unexpected type: " << PrettyDescriptor(ctype); 316 } 317 } else { 318 // Not special, continue with regular interpreter execution. 319 artInterpreterToInterpreterBridge(self, mh, code_item, shadow_frame, result); 320 } 321} 322 323// Explicit DoCall template function declarations. 324#define EXPLICIT_DO_CALL_TEMPLATE_DECL(_is_range, _do_assignability_check) \ 325 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ 326 bool DoCall<_is_range, _do_assignability_check>(ArtMethod* method, Object* receiver, \ 327 Thread* self, ShadowFrame& shadow_frame, \ 328 const Instruction* inst, uint16_t inst_data, \ 329 JValue* result) 330EXPLICIT_DO_CALL_TEMPLATE_DECL(false, false); 331EXPLICIT_DO_CALL_TEMPLATE_DECL(false, true); 332EXPLICIT_DO_CALL_TEMPLATE_DECL(true, false); 333EXPLICIT_DO_CALL_TEMPLATE_DECL(true, true); 334#undef EXPLICIT_DO_CALL_TEMPLATE_DECL 335 336// Explicit DoFilledNewArray template function declarations. 337#define EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(_is_range_, _check) \ 338 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \ 339 bool DoFilledNewArray<_is_range_, _check>(const Instruction* inst, \ 340 const ShadowFrame& shadow_frame, \ 341 Thread* self, JValue* result) 342EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, false); 343EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(false, true); 344EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, false); 345EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL(true, true); 346#undef EXPLICIT_DO_FILLED_NEW_ARRAY_TEMPLATE_DECL 347 348} // namespace interpreter 349} // namespace art 350