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