1 /* 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/javaClasses.inline.hpp" 27 #include "classfile/systemDictionary.hpp" 28 #include "classfile/vmSymbols.hpp" 29 #include "code/codeCache.hpp" 30 #include "compiler/compileBroker.hpp" 31 #include "compiler/disassembler.hpp" 32 #include "gc/shared/collectedHeap.hpp" 33 #include "interpreter/interpreter.hpp" 34 #include "interpreter/interpreterRuntime.hpp" 35 #include "interpreter/linkResolver.hpp" 36 #include "interpreter/templateTable.hpp" 37 #include "logging/log.hpp" 38 #include "memory/oopFactory.hpp" 39 #include "memory/resourceArea.hpp" 40 #include "memory/universe.inline.hpp" 41 #include "memory/vtBuffer.hpp" 42 #include "oops/constantPool.hpp" 43 #include "oops/instanceKlass.hpp" 44 #include "oops/methodData.hpp" 45 #include "oops/objArrayKlass.hpp" 46 #include "oops/objArrayOop.inline.hpp" 47 #include "oops/oop.inline.hpp" 48 #include "oops/symbol.hpp" 49 #include "oops/valueKlass.hpp" 50 #include "oops/valueArrayKlass.hpp" 51 #include "oops/valueArrayOop.hpp" 52 #include "prims/jvmtiExport.hpp" 53 #include "prims/nativeLookup.hpp" 54 #include "runtime/atomic.hpp" 55 #include "runtime/biasedLocking.hpp" 56 #include "runtime/compilationPolicy.hpp" 57 #include "runtime/deoptimization.hpp" 58 #include "runtime/fieldDescriptor.hpp" 59 #include "runtime/handles.inline.hpp" 60 #include "runtime/icache.hpp" 61 #include "runtime/interfaceSupport.hpp" 62 #include "runtime/java.hpp" 63 #include "runtime/jfieldIDWorkaround.hpp" 64 #include "runtime/osThread.hpp" 65 #include "runtime/sharedRuntime.hpp" 66 #include "runtime/stubRoutines.hpp" 67 #include "runtime/synchronizer.hpp" 68 #include "runtime/threadCritical.hpp" 69 #include "utilities/align.hpp" 70 #include "utilities/events.hpp" 71 #include "utilities/globalDefinitions.hpp" 72 #ifdef COMPILER2 73 #include "opto/runtime.hpp" 74 #endif 75 76 class UnlockFlagSaver { 77 private: 78 JavaThread* _thread; 79 bool _do_not_unlock; 80 public: 81 UnlockFlagSaver(JavaThread* t) { 82 _thread = t; 83 _do_not_unlock = t->do_not_unlock_if_synchronized(); 84 t->set_do_not_unlock_if_synchronized(false); 85 } 86 ~UnlockFlagSaver() { 87 _thread->set_do_not_unlock_if_synchronized(_do_not_unlock); 88 } 89 }; 90 91 //------------------------------------------------------------------------------------------------------------------------ 92 // State accessors 93 94 void InterpreterRuntime::set_bcp_and_mdp(address bcp, JavaThread *thread) { 95 last_frame(thread).interpreter_frame_set_bcp(bcp); 96 if (ProfileInterpreter) { 97 // ProfileTraps uses MDOs independently of ProfileInterpreter. 98 // That is why we must check both ProfileInterpreter and mdo != NULL. 99 MethodData* mdo = last_frame(thread).interpreter_frame_method()->method_data(); 100 if (mdo != NULL) { 101 NEEDS_CLEANUP; 102 last_frame(thread).interpreter_frame_set_mdp(mdo->bci_to_dp(last_frame(thread).interpreter_frame_bci())); 103 } 104 } 105 } 106 107 //------------------------------------------------------------------------------------------------------------------------ 108 // Constants 109 110 111 IRT_ENTRY(void, InterpreterRuntime::ldc(JavaThread* thread, bool wide)) 112 // access constant pool 113 ConstantPool* pool = method(thread)->constants(); 114 int index = wide ? get_index_u2(thread, Bytecodes::_ldc_w) : get_index_u1(thread, Bytecodes::_ldc); 115 constantTag tag = pool->tag_at(index); 116 117 assert ((tag.is_unresolved_klass() || tag.is_klass() || 118 tag.is_unresolved_value_type() || tag.is_value_type()), 119 "wrong ldc call"); 120 Klass* klass = pool->klass_at(index, CHECK); 121 oop java_class = klass->java_mirror(); 122 thread->set_vm_result(java_class); 123 IRT_END 124 125 IRT_ENTRY(void, InterpreterRuntime::resolve_ldc(JavaThread* thread, Bytecodes::Code bytecode)) { 126 assert(bytecode == Bytecodes::_fast_aldc || 127 bytecode == Bytecodes::_fast_aldc_w, "wrong bc"); 128 ResourceMark rm(thread); 129 methodHandle m (thread, method(thread)); 130 Bytecode_loadconstant ldc(m, bci(thread)); 131 oop result = ldc.resolve_constant(CHECK); 132 #ifdef ASSERT 133 { 134 // The bytecode wrappers aren't GC-safe so construct a new one 135 Bytecode_loadconstant ldc2(m, bci(thread)); 136 oop coop = m->constants()->resolved_references()->obj_at(ldc2.cache_index()); 137 assert(result == coop, "expected result for assembly code"); 138 } 139 #endif 140 thread->set_vm_result(result); 141 } 142 IRT_END 143 144 145 //------------------------------------------------------------------------------------------------------------------------ 146 // Allocation 147 148 IRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* thread, ConstantPool* pool, int index)) 149 Klass* k = pool->klass_at(index, CHECK); 150 InstanceKlass* klass = InstanceKlass::cast(k); 151 152 // Make sure we are not instantiating an abstract klass 153 klass->check_valid_for_instantiation(true, CHECK); 154 155 // Make sure klass is initialized 156 klass->initialize(CHECK); 157 158 // At this point the class may not be fully initialized 159 // because of recursive initialization. If it is fully 160 // initialized & has_finalized is not set, we rewrite 161 // it into its fast version (Note: no locking is needed 162 // here since this is an atomic byte write and can be 163 // done more than once). 164 // 165 // Note: In case of classes with has_finalized we don't 166 // rewrite since that saves us an extra check in 167 // the fast version which then would call the 168 // slow version anyway (and do a call back into 169 // Java). 170 // If we have a breakpoint, then we don't rewrite 171 // because the _breakpoint bytecode would be lost. 172 oop obj = klass->allocate_instance(CHECK); 173 thread->set_vm_result(obj); 174 IRT_END 175 176 void copy_primitive_argument(intptr_t* addr, Handle instance, int offset, BasicType type) { 177 switch (type) { 178 case T_BOOLEAN: 179 instance()->bool_field_put(offset, (jboolean)*((int*)addr)); 180 break; 181 case T_CHAR: 182 instance()->char_field_put(offset, (jchar) *((int*)addr)); 183 break; 184 case T_FLOAT: 185 instance()->float_field_put(offset, (jfloat)*((float*)addr)); 186 break; 187 case T_DOUBLE: 188 instance()->double_field_put(offset, (jdouble)*((double*)addr)); 189 break; 190 case T_BYTE: 191 instance()->byte_field_put(offset, (jbyte)*((int*)addr)); 192 break; 193 case T_SHORT: 194 instance()->short_field_put(offset, (jshort)*((int*)addr)); 195 break; 196 case T_INT: 197 instance()->int_field_put(offset, (jint)*((int*)addr)); 198 break; 199 case T_LONG: 200 instance()->long_field_put(offset, (jlong)*((long long*)addr)); 201 break; 202 case T_OBJECT: 203 case T_ARRAY: 204 case T_VALUETYPE: 205 fatal("Should not be handled with this method"); 206 break; 207 default: 208 fatal("Unsupported BasicType"); 209 } 210 } 211 212 IRT_ENTRY(void, InterpreterRuntime::vdefault(JavaThread* thread, ConstantPool* pool, int index)) 213 // Getting the ValueKlass 214 Klass* k = pool->klass_at(index, CHECK); 215 assert(k->is_value(), "vdefault argument must be the value type class"); 216 ValueKlass* vklass = ValueKlass::cast(k); 217 218 vklass->initialize(THREAD); 219 220 // Creating value 221 bool in_heap; 222 instanceOop value = vklass->allocate_buffered_or_heap_instance(&in_heap, CHECK); 223 Handle value_h = Handle(THREAD, value); 224 225 thread->set_vm_result(value_h()); 226 IRT_END 227 228 IRT_ENTRY(int, InterpreterRuntime::vwithfield(JavaThread* thread, ConstantPoolCache* cp_cache)) 229 // Getting the ValueKlass 230 int index = ConstantPool::decode_cpcache_index(get_index_u2_cpcache(thread, Bytecodes::_vwithfield)); 231 ConstantPoolCacheEntry* cp_entry = cp_cache->entry_at(index); 232 assert(cp_entry->is_resolved(Bytecodes::_vwithfield), "Should have been resolved"); 233 Klass* klass = cp_entry->f1_as_klass(); 234 assert(klass->is_value(), "vwithfield only applies to value types"); 235 ValueKlass* vklass = ValueKlass::cast(klass); 236 237 // Getting Field information 238 int offset = cp_entry->f2_as_index(); 239 int field_index = cp_entry->field_index(); 240 int field_offset = cp_entry->f2_as_offset(); 241 Symbol* field_signature = vklass->field_signature(field_index); 242 ResourceMark rm(THREAD); 243 const char* signature = (const char *) field_signature->as_utf8(); 244 BasicType field_type = char2type(signature[0]); 245 246 // Getting old value 247 frame f = last_frame(thread); 248 jint tos_idx = f.interpreter_frame_expression_stack_size() - 1; 249 int vt_offset = type2size[field_type]; 250 oop old_value = *(oop*)f.interpreter_frame_expression_stack_at(tos_idx - vt_offset); 251 assert(old_value != NULL && oopDesc::is_oop(old_value) && old_value->is_value(),"Verifying receiver"); 252 Handle old_value_h(THREAD, old_value); 253 254 // Creating new value by copying the one passed in argument 255 bool in_heap; 256 instanceOop new_value = vklass->allocate_buffered_or_heap_instance(&in_heap, 257 CHECK_((type2size[field_type]) * AbstractInterpreter::stackElementSize)); 258 Handle new_value_h = Handle(THREAD, new_value); 259 int first_offset = vklass->first_field_offset(); 260 vklass->value_store(vklass->data_for_oop(old_value_h()), 261 vklass->data_for_oop(new_value_h()), in_heap, false); 262 263 // Updating the field specified in arguments 264 if (field_type == T_OBJECT || field_type == T_ARRAY) { 265 oop aoop = *(oop*)f.interpreter_frame_expression_stack_at(tos_idx); 266 assert(aoop == NULL || (oopDesc::is_oop(aoop) && (!aoop->is_value())),"argument must be a reference type"); 267 new_value_h()->obj_field_put(field_offset, aoop); 268 } else if (field_type == T_VALUETYPE) { 269 if (cp_entry->is_flatten()) { 270 Klass* field_k = vklass->get_value_field_klass(field_index); 271 ValueKlass* field_vk = ValueKlass::cast(field_k); 272 oop vt_oop = *(oop*)f.interpreter_frame_expression_stack_at(tos_idx); 273 assert(vt_oop != NULL && oopDesc::is_oop(vt_oop) && vt_oop->is_value(),"argument must be a value type"); 274 assert(field_vk == vt_oop->klass(), "Must match"); 275 field_vk->value_store(field_vk->data_for_oop(vt_oop), 276 ((char*)(oopDesc*)new_value_h()) + field_offset, true, false); 277 } else { 278 oop voop = *(oop*)f.interpreter_frame_expression_stack_at(tos_idx); 279 assert(voop != NULL || (oopDesc::is_oop(voop) && (voop->is_value())),"argument must be a value type"); 280 new_value_h()->obj_field_put(field_offset, voop); 281 } 282 } else { 283 intptr_t* addr = f.interpreter_frame_expression_stack_at(tos_idx); 284 copy_primitive_argument(addr, new_value_h, field_offset, field_type); 285 } 286 287 // returning result 288 thread->set_vm_result(new_value_h()); 289 return (type2size[field_type] + type2size[T_VALUETYPE]) * AbstractInterpreter::stackElementSize; 290 IRT_END 291 292 IRT_ENTRY(void, InterpreterRuntime::vbox(JavaThread* thread, ConstantPool* pool, int index, oopDesc* value)) 293 assert(EnableMVT, "vbox is supported only when the MVT programming model is enabled"); 294 if (value == NULL) { 295 THROW(vmSymbols::java_lang_NullPointerException()); 296 } 297 298 // Since the verifier is probably disabled, a few extra type check 299 Klass* target_klass = pool->klass_at(index, CHECK); 300 if (target_klass->is_value()) { 301 THROW_MSG(vmSymbols::java_lang_ClassCastException(), "vbox target is value type"); 302 } 303 Klass* klass = value->klass(); 304 if (!klass->is_value()) { 305 THROW_MSG(vmSymbols::java_lang_ClassCastException(), "vbox not from value type"); 306 } 307 ValueKlass* vtklass = ValueKlass::cast(klass); 308 if (vtklass->get_vcc_klass() != target_klass) { 309 THROW_MSG(vmSymbols::java_lang_ClassCastException(), "vbox target is not derive value type box"); 310 } 311 oop box = vtklass->box(Handle(THREAD, value), 312 InstanceKlass::cast(target_klass), 313 CHECK); 314 thread->set_vm_result(box); 315 IRT_END 316 317 IRT_ENTRY(void, InterpreterRuntime::vunbox(JavaThread* thread, ConstantPool* pool, int index, oopDesc* obj)) 318 assert(EnableMVT, "vunbox is supported only when the MVT programming model is enabled"); 319 if (obj == NULL) { 320 THROW(vmSymbols::java_lang_NullPointerException()); 321 } 322 Klass* target_klass = pool->klass_at(index, CHECK); 323 if (!target_klass->is_value()) { 324 THROW_MSG(vmSymbols::java_lang_ClassCastException(), "vunbox target is not value type"); 325 } 326 Klass* klass = obj->klass(); 327 if ((!klass->is_instance_klass()) || klass->is_value()) { 328 THROW_MSG(vmSymbols::java_lang_ClassCastException(), "vunbox source is not an instance"); 329 } 330 if (klass != InstanceKlass::cast(target_klass)->get_vcc_klass()) { 331 THROW_MSG(vmSymbols::java_lang_ClassCastException(), "vunbox target is not derive value type"); 332 } 333 oop value = ValueKlass::cast(target_klass)->unbox(Handle(THREAD, obj), 334 InstanceKlass::cast(target_klass), 335 CHECK); 336 thread->set_vm_result(value); 337 IRT_END 338 339 IRT_ENTRY(void, InterpreterRuntime::qgetfield(JavaThread* thread, oopDesc* obj, int index, Klass* field_holder)) 340 Handle value_h(THREAD, obj); 341 InstanceKlass* klass = InstanceKlass::cast(field_holder); 342 343 Klass* field_k = klass->get_value_field_klass(index); 344 ValueKlass* field_vklass = ValueKlass::cast(field_k); 345 field_vklass->initialize(THREAD); 346 347 instanceOop res; 348 bool in_heap; 349 if (klass->is_field_flatten(index)) { 350 // allocate instance 351 res = field_vklass->allocate_buffered_or_heap_instance(&in_heap, CHECK); 352 instanceHandle res_h(THREAD, res); 353 // copy value 354 field_vklass->value_store(((char*)(oopDesc*)value_h()) + klass->field_offset(index), 355 field_vklass->data_for_oop(res), in_heap, false); 356 thread->set_vm_result(res_h()); 357 } else { 358 oop res = value_h()->obj_field_acquire(klass->field_offset(index)); 359 if (res == NULL) { 360 res = field_vklass->allocate_buffered_or_heap_instance(&in_heap, CHECK); 361 } 362 thread->set_vm_result(res); 363 } 364 IRT_END 365 366 IRT_ENTRY(void, InterpreterRuntime::initialize_static_value_field(JavaThread* thread, oopDesc* mirror, int index)) 367 instanceHandle mirror_h(THREAD, (instanceOop)mirror); 368 InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror)); 369 int offset = klass->field_offset(index); 370 assert(mirror->obj_field(offset) == NULL,"Field must not be initialized twice"); 371 372 Klass* field_k = klass->get_value_field_klass(index); 373 ValueKlass* field_vklass = ValueKlass::cast(field_k); 374 // allocate instance, because it is going to be assigned to a static field 375 // it must not be a buffered value 376 instanceOop res = field_vklass->allocate_instance(CHECK); 377 instanceHandle res_h(THREAD, res); 378 mirror_h()->obj_field_put(offset, res_h()); 379 thread->set_vm_result(res_h()); 380 IRT_END 381 382 IRT_ENTRY(void, InterpreterRuntime::qputfield(JavaThread* thread, oopDesc* obj, oopDesc* value, ConstantPoolCache* cp_cache)) 383 Handle value_h(THREAD, value); 384 Handle obj_h(THREAD, obj); 385 assert(!obj_h()->klass()->is_value(), "obj must be an object"); 386 assert(value_h()->klass()->is_value(), "value must be an value type"); 387 388 int idx = ConstantPool::decode_cpcache_index(get_index_u2_cpcache(thread, Bytecodes::_putfield)); 389 ConstantPoolCacheEntry* cp_entry = cp_cache->entry_at(idx); 390 391 int index = cp_entry->field_index(); 392 bool flatten = cp_entry->is_flatten(); 393 394 InstanceKlass* klass = InstanceKlass::cast(cp_entry->f1_as_klass()); 395 Klass* field_k = klass->get_value_field_klass(index); 396 ValueKlass* field_vklass = ValueKlass::cast(value->klass()); 397 assert(field_k == field_vklass, "Field descriptor and argument must match"); 398 if (flatten) { 399 // copy value 400 field_vklass->value_store(field_vklass->data_for_oop(value_h()), 401 ((char*)(oopDesc*)obj_h()) + klass->field_offset(index), true, false); 402 } else { 403 if (Universe::heap()->is_in_reserved(value_h())) { 404 obj_h()->obj_field_put(klass->field_offset(index), value_h()); 405 } else { 406 // allocate heap instance 407 instanceOop val = field_vklass->allocate_instance(CHECK); 408 instanceHandle res_h(THREAD, val); 409 // copy value 410 field_vklass->value_store(field_vklass->data_for_oop(value_h()), 411 field_vklass->data_for_oop(res_h()), true, false); 412 413 414 obj_h()->obj_field_put(klass->field_offset(index), res_h()); 415 } 416 } 417 IRT_END 418 419 IRT_ENTRY(void, InterpreterRuntime::qputstatic(JavaThread* thread, oopDesc* value, int offset, oopDesc* mirror)) 420 instanceHandle value_h(THREAD, (instanceOop)value); 421 assert(value_h()->is_value(), "qputstatic only deals with value arguments"); 422 if (Universe::heap()->is_in_reserved(value_h())) { 423 mirror->obj_field_put(offset, value_h()); 424 } else { 425 // The argument is a buffered value, a copy must be created in the Java heap 426 // because a static field cannot point to a thread-local buffered value 427 ValueKlass* field_vklass = ValueKlass::cast(value_h()->klass()); 428 Handle mirror_h(THREAD, mirror); 429 // allocate heap instance 430 instanceOop res = field_vklass->allocate_instance(CHECK); 431 assert(Universe::heap()->is_in_reserved(res), "Must be in the Java heap"); 432 instanceHandle res_h(THREAD, res); 433 // copy value 434 field_vklass->value_store(field_vklass->data_for_oop(value_h()), 435 field_vklass->data_for_oop(res), true, false); 436 // writing static field 437 mirror_h->obj_field_put(offset, res_h()); 438 assert(mirror_h->obj_field(offset) != NULL,"Sanity check"); 439 } 440 IRT_END 441 442 IRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* thread, BasicType type, jint size)) 443 oop obj = oopFactory::new_typeArray(type, size, CHECK); 444 thread->set_vm_result(obj); 445 IRT_END 446 447 448 IRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* thread, ConstantPool* pool, int index, jint size)) 449 Klass* klass = pool->klass_at(index, CHECK); 450 if (klass->is_value()) { // Logically creates elements, ensure klass init 451 klass->initialize(CHECK); 452 } 453 arrayOop obj = oopFactory::new_array(klass, size, CHECK); 454 thread->set_vm_result(obj); 455 IRT_END 456 457 IRT_ENTRY(void, InterpreterRuntime::value_array_load(JavaThread* thread, arrayOopDesc* array, int index)) 458 Klass* klass = array->klass(); 459 assert(klass->is_valueArray_klass() || klass->is_objArray_klass(), "expected value or object array oop"); 460 461 if (klass->is_objArray_klass()) { 462 thread->set_vm_result(((objArrayOop) array)->obj_at(index)); 463 } 464 else { 465 ValueArrayKlass* vaklass = ValueArrayKlass::cast(klass); 466 ValueKlass* vklass = vaklass->element_klass(); 467 arrayHandle ah(THREAD, array); 468 bool in_heap; 469 instanceOop value_holder = vklass->allocate_buffered_or_heap_instance(&in_heap, CHECK); 470 void* src = ((valueArrayOop)ah())->value_at_addr(index, vaklass->layout_helper()); 471 vklass->value_store(src, vklass->data_for_oop(value_holder), 472 vaklass->element_byte_size(), in_heap, false); 473 thread->set_vm_result(value_holder); 474 } 475 IRT_END 476 477 IRT_ENTRY(void, InterpreterRuntime::value_array_store(JavaThread* thread, arrayOopDesc* array, int index, void* val)) 478 Klass* klass = array->klass(); 479 assert(klass->is_valueArray_klass() || klass->is_objArray_klass(), "expected value or object array oop"); 480 481 if (ArrayKlass::cast(klass)->element_klass() != ((oop)val)->klass()) { 482 THROW(vmSymbols::java_lang_ArrayStoreException()); 483 } 484 if (klass->is_objArray_klass()) { 485 if(!Universe::heap()->is_in_reserved(val)) { 486 // A Java heap allocated copy must be made because an array cannot 487 // reference a thread-local buffered value 488 Handle val_h(THREAD, (oop)val); 489 ObjArrayKlass* aklass = ObjArrayKlass::cast(klass); 490 Klass* eklass = aklass->element_klass(); 491 assert(eklass->is_value(), "Sanity check"); 492 assert(eklass == ((oop)val)->klass(), "Sanity check"); 493 ValueKlass* vklass = ValueKlass::cast(eklass); 494 // allocate heap instance 495 instanceOop res = vklass->allocate_instance(CHECK); 496 Handle res_h(THREAD, res); 497 // copy value 498 vklass->value_store(((char*)(oopDesc*)val_h()) + vklass->first_field_offset(), 499 ((char*)(oopDesc*)res_h()) + vklass->first_field_offset(),true, false); 500 val = res_h(); 501 } 502 ((objArrayOop) array)->obj_at_put(index, (oop)val); 503 } else { 504 valueArrayOop varray = (valueArrayOop)array; 505 ValueArrayKlass* vaklass = ValueArrayKlass::cast(klass); 506 ValueKlass* vklass = vaklass->element_klass(); 507 const int lh = vaklass->layout_helper(); 508 vklass->value_store(vklass->data_for_oop((oop)val), varray->value_at_addr(index, lh), 509 vaklass->element_byte_size(), true, false); 510 } 511 IRT_END 512 513 IRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* thread, jint* first_size_address)) 514 // We may want to pass in more arguments - could make this slightly faster 515 ConstantPool* constants = method(thread)->constants(); 516 int i = get_index_u2(thread, Bytecodes::_multianewarray); 517 Klass* klass = constants->klass_at(i, CHECK); 518 int nof_dims = number_of_dimensions(thread); 519 assert(klass->is_klass(), "not a class"); 520 assert(nof_dims >= 1, "multianewarray rank must be nonzero"); 521 522 if (klass->is_value()) { // Logically creates elements, ensure klass init 523 klass->initialize(CHECK); 524 } 525 526 // We must create an array of jints to pass to multi_allocate. 527 ResourceMark rm(thread); 528 const int small_dims = 10; 529 jint dim_array[small_dims]; 530 jint *dims = &dim_array[0]; 531 if (nof_dims > small_dims) { 532 dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims); 533 } 534 for (int index = 0; index < nof_dims; index++) { 535 // offset from first_size_address is addressed as local[index] 536 int n = Interpreter::local_offset_in_bytes(index)/jintSize; 537 dims[index] = first_size_address[n]; 538 } 539 oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK); 540 thread->set_vm_result(obj); 541 IRT_END 542 543 IRT_ENTRY(void, InterpreterRuntime::recycle_vtbuffer(JavaThread* thread)) 544 VTBuffer::recycle_vtbuffer(thread, last_frame(thread)); 545 IRT_END 546 547 IRT_ENTRY(void, InterpreterRuntime::recycle_buffered_values(JavaThread* thread)) 548 frame f = thread->last_frame(); 549 assert(f.is_interpreted_frame(), "recycling can only be triggered from interpreted frames"); 550 VTBuffer::recycle_vt_in_frame(thread, &f); 551 IRT_END 552 553 IRT_ENTRY(void, InterpreterRuntime::fix_frame_vt_alloc_ptr(JavaThread* thread)) 554 frame f = thread->last_frame(); 555 VTBuffer::fix_frame_vt_alloc_ptr(f, VTBufferChunk::chunk(thread->vt_alloc_ptr())); 556 IRT_END 557 558 IRT_ENTRY(void, InterpreterRuntime::return_value(JavaThread* thread, oopDesc* obj)) 559 if (Universe::heap()->is_in_reserved(obj)) { 560 thread->set_vm_result(obj); 561 return; 562 } 563 assert(obj->klass()->is_value(), "Sanity check"); 564 ValueKlass* vk = ValueKlass::cast(obj->klass()); 565 RegisterMap reg_map(thread, false); 566 frame current_frame = last_frame(thread); 567 frame caller_frame = current_frame.sender(®_map); 568 if (!caller_frame.is_interpreted_frame()) { 569 // caller is not an interpreted frame, creating a new value in Java heap 570 Handle obj_h(THREAD, obj); 571 instanceOop res = vk->allocate_instance(CHECK); 572 Handle res_h(THREAD, res); 573 // copy value 574 vk->value_store(vk->data_for_oop(obj_h()), 575 vk->data_for_oop(res_h()), true, false); 576 thread->set_vm_result(res_h()); 577 return; 578 } else { 579 oop dest = VTBuffer::relocate_return_value(thread, current_frame, obj); 580 thread->set_vm_result(dest); 581 } 582 IRT_END 583 584 IRT_ENTRY(void, InterpreterRuntime::check_areturn(JavaThread* thread, oopDesc* obj)) 585 if (obj != NULL) { 586 Klass* k = obj->klass(); 587 if (k->is_value()) { 588 ResourceMark rm(thread); 589 tty->print_cr("areturn used on a value from %s", k->name()->as_C_string()); 590 } 591 assert(!k->is_value(), "areturn should never be used on values"); 592 } 593 thread->set_vm_result(obj); 594 IRT_END 595 596 IRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* thread, oopDesc* obj)) 597 assert(oopDesc::is_oop(obj), "must be a valid oop"); 598 assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise"); 599 InstanceKlass::register_finalizer(instanceOop(obj), CHECK); 600 IRT_END 601 602 603 // Quicken instance-of and check-cast bytecodes 604 IRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* thread)) 605 // Force resolving; quicken the bytecode 606 int which = get_index_u2(thread, Bytecodes::_checkcast); 607 ConstantPool* cpool = method(thread)->constants(); 608 // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded 609 // program we might have seen an unquick'd bytecode in the interpreter but have another 610 // thread quicken the bytecode before we get here. 611 // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" ); 612 Klass* klass = cpool->klass_at(which, CHECK); 613 thread->set_vm_result_2(klass); 614 IRT_END 615 616 617 //------------------------------------------------------------------------------------------------------------------------ 618 // Exceptions 619 620 void InterpreterRuntime::note_trap_inner(JavaThread* thread, int reason, 621 const methodHandle& trap_method, int trap_bci, TRAPS) { 622 if (trap_method.not_null()) { 623 MethodData* trap_mdo = trap_method->method_data(); 624 if (trap_mdo == NULL) { 625 Method::build_interpreter_method_data(trap_method, THREAD); 626 if (HAS_PENDING_EXCEPTION) { 627 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), 628 "we expect only an OOM error here"); 629 CLEAR_PENDING_EXCEPTION; 630 } 631 trap_mdo = trap_method->method_data(); 632 // and fall through... 633 } 634 if (trap_mdo != NULL) { 635 // Update per-method count of trap events. The interpreter 636 // is updating the MDO to simulate the effect of compiler traps. 637 Deoptimization::update_method_data_from_interpreter(trap_mdo, trap_bci, reason); 638 } 639 } 640 } 641 642 // Assume the compiler is (or will be) interested in this event. 643 // If necessary, create an MDO to hold the information, and record it. 644 void InterpreterRuntime::note_trap(JavaThread* thread, int reason, TRAPS) { 645 assert(ProfileTraps, "call me only if profiling"); 646 methodHandle trap_method(thread, method(thread)); 647 int trap_bci = trap_method->bci_from(bcp(thread)); 648 note_trap_inner(thread, reason, trap_method, trap_bci, THREAD); 649 } 650 651 #ifdef CC_INTERP 652 // As legacy note_trap, but we have more arguments. 653 IRT_ENTRY(void, InterpreterRuntime::note_trap(JavaThread* thread, int reason, Method *method, int trap_bci)) 654 methodHandle trap_method(method); 655 note_trap_inner(thread, reason, trap_method, trap_bci, THREAD); 656 IRT_END 657 658 // Class Deoptimization is not visible in BytecodeInterpreter, so we need a wrapper 659 // for each exception. 660 void InterpreterRuntime::note_nullCheck_trap(JavaThread* thread, Method *method, int trap_bci) 661 { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_null_check, method, trap_bci); } 662 void InterpreterRuntime::note_div0Check_trap(JavaThread* thread, Method *method, int trap_bci) 663 { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_div0_check, method, trap_bci); } 664 void InterpreterRuntime::note_rangeCheck_trap(JavaThread* thread, Method *method, int trap_bci) 665 { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_range_check, method, trap_bci); } 666 void InterpreterRuntime::note_classCheck_trap(JavaThread* thread, Method *method, int trap_bci) 667 { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_class_check, method, trap_bci); } 668 void InterpreterRuntime::note_arrayCheck_trap(JavaThread* thread, Method *method, int trap_bci) 669 { if (ProfileTraps) note_trap(thread, Deoptimization::Reason_array_check, method, trap_bci); } 670 #endif // CC_INTERP 671 672 673 static Handle get_preinitialized_exception(Klass* k, TRAPS) { 674 // get klass 675 InstanceKlass* klass = InstanceKlass::cast(k); 676 assert(klass->is_initialized(), 677 "this klass should have been initialized during VM initialization"); 678 // create instance - do not call constructor since we may have no 679 // (java) stack space left (should assert constructor is empty) 680 Handle exception; 681 oop exception_oop = klass->allocate_instance(CHECK_(exception)); 682 exception = Handle(THREAD, exception_oop); 683 if (StackTraceInThrowable) { 684 java_lang_Throwable::fill_in_stack_trace(exception); 685 } 686 return exception; 687 } 688 689 // Special handling for stack overflow: since we don't have any (java) stack 690 // space left we use the pre-allocated & pre-initialized StackOverflowError 691 // klass to create an stack overflow error instance. We do not call its 692 // constructor for the same reason (it is empty, anyway). 693 IRT_ENTRY(void, InterpreterRuntime::throw_StackOverflowError(JavaThread* thread)) 694 Handle exception = get_preinitialized_exception( 695 SystemDictionary::StackOverflowError_klass(), 696 CHECK); 697 // Increment counter for hs_err file reporting 698 Atomic::inc(&Exceptions::_stack_overflow_errors); 699 THROW_HANDLE(exception); 700 IRT_END 701 702 IRT_ENTRY(void, InterpreterRuntime::throw_delayed_StackOverflowError(JavaThread* thread)) 703 Handle exception = get_preinitialized_exception( 704 SystemDictionary::StackOverflowError_klass(), 705 CHECK); 706 java_lang_Throwable::set_message(exception(), 707 Universe::delayed_stack_overflow_error_message()); 708 // Increment counter for hs_err file reporting 709 Atomic::inc(&Exceptions::_stack_overflow_errors); 710 THROW_HANDLE(exception); 711 IRT_END 712 713 IRT_ENTRY(void, InterpreterRuntime::create_exception(JavaThread* thread, char* name, char* message)) 714 // lookup exception klass 715 TempNewSymbol s = SymbolTable::new_symbol(name, CHECK); 716 if (ProfileTraps) { 717 if (s == vmSymbols::java_lang_ArithmeticException()) { 718 note_trap(thread, Deoptimization::Reason_div0_check, CHECK); 719 } else if (s == vmSymbols::java_lang_NullPointerException()) { 720 note_trap(thread, Deoptimization::Reason_null_check, CHECK); 721 } 722 } 723 // create exception 724 Handle exception = Exceptions::new_exception(thread, s, message); 725 thread->set_vm_result(exception()); 726 IRT_END 727 728 729 IRT_ENTRY(void, InterpreterRuntime::create_klass_exception(JavaThread* thread, char* name, oopDesc* obj)) 730 ResourceMark rm(thread); 731 const char* klass_name = obj->klass()->external_name(); 732 // lookup exception klass 733 TempNewSymbol s = SymbolTable::new_symbol(name, CHECK); 734 if (ProfileTraps) { 735 note_trap(thread, Deoptimization::Reason_class_check, CHECK); 736 } 737 // create exception, with klass name as detail message 738 Handle exception = Exceptions::new_exception(thread, s, klass_name); 739 thread->set_vm_result(exception()); 740 IRT_END 741 742 743 IRT_ENTRY(void, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException(JavaThread* thread, char* name, jint index)) 744 char message[jintAsStringSize]; 745 // lookup exception klass 746 TempNewSymbol s = SymbolTable::new_symbol(name, CHECK); 747 if (ProfileTraps) { 748 note_trap(thread, Deoptimization::Reason_range_check, CHECK); 749 } 750 // create exception 751 sprintf(message, "%d", index); 752 THROW_MSG(s, message); 753 IRT_END 754 755 IRT_ENTRY(void, InterpreterRuntime::throw_ClassCastException( 756 JavaThread* thread, oopDesc* obj)) 757 758 ResourceMark rm(thread); 759 char* message = SharedRuntime::generate_class_cast_message( 760 thread, obj->klass()); 761 762 if (ProfileTraps) { 763 note_trap(thread, Deoptimization::Reason_class_check, CHECK); 764 } 765 766 // create exception 767 THROW_MSG(vmSymbols::java_lang_ClassCastException(), message); 768 IRT_END 769 770 // exception_handler_for_exception(...) returns the continuation address, 771 // the exception oop (via TLS) and sets the bci/bcp for the continuation. 772 // The exception oop is returned to make sure it is preserved over GC (it 773 // is only on the stack if the exception was thrown explicitly via athrow). 774 // During this operation, the expression stack contains the values for the 775 // bci where the exception happened. If the exception was propagated back 776 // from a call, the expression stack contains the values for the bci at the 777 // invoke w/o arguments (i.e., as if one were inside the call). 778 IRT_ENTRY(address, InterpreterRuntime::exception_handler_for_exception(JavaThread* thread, oopDesc* exception)) 779 780 Handle h_exception(thread, exception); 781 methodHandle h_method (thread, method(thread)); 782 constantPoolHandle h_constants(thread, h_method->constants()); 783 bool should_repeat; 784 int handler_bci; 785 int current_bci = bci(thread); 786 787 if (thread->frames_to_pop_failed_realloc() > 0) { 788 // Allocation of scalar replaced object used in this frame 789 // failed. Unconditionally pop the frame. 790 thread->dec_frames_to_pop_failed_realloc(); 791 thread->set_vm_result(h_exception()); 792 // If the method is synchronized we already unlocked the monitor 793 // during deoptimization so the interpreter needs to skip it when 794 // the frame is popped. 795 thread->set_do_not_unlock_if_synchronized(true); 796 #ifdef CC_INTERP 797 return (address) -1; 798 #else 799 return Interpreter::remove_activation_entry(); 800 #endif 801 } 802 803 // Need to do this check first since when _do_not_unlock_if_synchronized 804 // is set, we don't want to trigger any classloading which may make calls 805 // into java, or surprisingly find a matching exception handler for bci 0 806 // since at this moment the method hasn't been "officially" entered yet. 807 if (thread->do_not_unlock_if_synchronized()) { 808 ResourceMark rm; 809 assert(current_bci == 0, "bci isn't zero for do_not_unlock_if_synchronized"); 810 thread->set_vm_result(exception); 811 #ifdef CC_INTERP 812 return (address) -1; 813 #else 814 return Interpreter::remove_activation_entry(); 815 #endif 816 } 817 818 do { 819 should_repeat = false; 820 821 // assertions 822 #ifdef ASSERT 823 assert(h_exception.not_null(), "NULL exceptions should be handled by athrow"); 824 // Check that exception is a subclass of Throwable, otherwise we have a VerifyError 825 if (!(h_exception->is_a(SystemDictionary::Throwable_klass()))) { 826 if (ExitVMOnVerifyError) vm_exit(-1); 827 ShouldNotReachHere(); 828 } 829 #endif 830 831 // tracing 832 if (log_is_enabled(Info, exceptions)) { 833 ResourceMark rm(thread); 834 stringStream tempst; 835 tempst.print("interpreter method <%s>\n" 836 " at bci %d for thread " INTPTR_FORMAT, 837 h_method->print_value_string(), current_bci, p2i(thread)); 838 Exceptions::log_exception(h_exception, tempst); 839 } 840 // Don't go paging in something which won't be used. 841 // else if (extable->length() == 0) { 842 // // disabled for now - interpreter is not using shortcut yet 843 // // (shortcut is not to call runtime if we have no exception handlers) 844 // // warning("performance bug: should not call runtime if method has no exception handlers"); 845 // } 846 // for AbortVMOnException flag 847 Exceptions::debug_check_abort(h_exception); 848 849 // exception handler lookup 850 Klass* klass = h_exception->klass(); 851 handler_bci = Method::fast_exception_handler_bci_for(h_method, klass, current_bci, THREAD); 852 if (HAS_PENDING_EXCEPTION) { 853 // We threw an exception while trying to find the exception handler. 854 // Transfer the new exception to the exception handle which will 855 // be set into thread local storage, and do another lookup for an 856 // exception handler for this exception, this time starting at the 857 // BCI of the exception handler which caused the exception to be 858 // thrown (bug 4307310). 859 h_exception = Handle(THREAD, PENDING_EXCEPTION); 860 CLEAR_PENDING_EXCEPTION; 861 if (handler_bci >= 0) { 862 current_bci = handler_bci; 863 should_repeat = true; 864 } 865 } 866 } while (should_repeat == true); 867 868 #if INCLUDE_JVMCI 869 if (EnableJVMCI && h_method->method_data() != NULL) { 870 ResourceMark rm(thread); 871 ProfileData* pdata = h_method->method_data()->allocate_bci_to_data(current_bci, NULL); 872 if (pdata != NULL && pdata->is_BitData()) { 873 BitData* bit_data = (BitData*) pdata; 874 bit_data->set_exception_seen(); 875 } 876 } 877 #endif 878 879 // notify JVMTI of an exception throw; JVMTI will detect if this is a first 880 // time throw or a stack unwinding throw and accordingly notify the debugger 881 if (JvmtiExport::can_post_on_exceptions()) { 882 JvmtiExport::post_exception_throw(thread, h_method(), bcp(thread), h_exception()); 883 } 884 885 #ifdef CC_INTERP 886 address continuation = (address)(intptr_t) handler_bci; 887 #else 888 address continuation = NULL; 889 #endif 890 address handler_pc = NULL; 891 if (handler_bci < 0 || !thread->reguard_stack((address) &continuation)) { 892 // Forward exception to callee (leaving bci/bcp untouched) because (a) no 893 // handler in this method, or (b) after a stack overflow there is not yet 894 // enough stack space available to reprotect the stack. 895 #ifndef CC_INTERP 896 continuation = Interpreter::remove_activation_entry(); 897 #endif 898 #if COMPILER2_OR_JVMCI 899 // Count this for compilation purposes 900 h_method->interpreter_throwout_increment(THREAD); 901 #endif 902 } else { 903 // handler in this method => change bci/bcp to handler bci/bcp and continue there 904 handler_pc = h_method->code_base() + handler_bci; 905 #ifndef CC_INTERP 906 set_bcp_and_mdp(handler_pc, thread); 907 continuation = Interpreter::dispatch_table(vtos)[*handler_pc]; 908 #endif 909 } 910 // notify debugger of an exception catch 911 // (this is good for exceptions caught in native methods as well) 912 if (JvmtiExport::can_post_on_exceptions()) { 913 JvmtiExport::notice_unwind_due_to_exception(thread, h_method(), handler_pc, h_exception(), (handler_pc != NULL)); 914 } 915 916 thread->set_vm_result(h_exception()); 917 return continuation; 918 IRT_END 919 920 921 IRT_ENTRY(void, InterpreterRuntime::throw_pending_exception(JavaThread* thread)) 922 assert(thread->has_pending_exception(), "must only ne called if there's an exception pending"); 923 // nothing to do - eventually we should remove this code entirely (see comments @ call sites) 924 IRT_END 925 926 927 IRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodError(JavaThread* thread)) 928 THROW(vmSymbols::java_lang_AbstractMethodError()); 929 IRT_END 930 931 932 IRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* thread)) 933 THROW(vmSymbols::java_lang_IncompatibleClassChangeError()); 934 IRT_END 935 936 937 //------------------------------------------------------------------------------------------------------------------------ 938 // Fields 939 // 940 941 void InterpreterRuntime::resolve_get_put(JavaThread* thread, Bytecodes::Code bytecode) { 942 Thread* THREAD = thread; 943 // resolve field 944 fieldDescriptor info; 945 constantPoolHandle pool(thread, method(thread)->constants()); 946 methodHandle m(thread, method(thread)); 947 bool is_put = (bytecode == Bytecodes::_putfield || bytecode == Bytecodes::_nofast_putfield || 948 bytecode == Bytecodes::_putstatic || bytecode == Bytecodes::_vwithfield); 949 bool is_static = (bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic); 950 bool is_value = bytecode == Bytecodes::_vwithfield; 951 952 { 953 JvmtiHideSingleStepping jhss(thread); 954 LinkResolver::resolve_field_access(info, pool, get_index_u2_cpcache(thread, bytecode), 955 m, bytecode, CHECK); 956 } // end JvmtiHideSingleStepping 957 958 // check if link resolution caused cpCache to be updated 959 ConstantPoolCacheEntry* cp_cache_entry = cache_entry(thread); 960 if (cp_cache_entry->is_resolved(bytecode)) return; 961 962 // compute auxiliary field attributes 963 TosState state = as_TosState(info.field_type()); 964 965 // Resolution of put instructions on final fields is delayed. That is required so that 966 // exceptions are thrown at the correct place (when the instruction is actually invoked). 967 // If we do not resolve an instruction in the current pass, leaving the put_code 968 // set to zero will cause the next put instruction to the same field to reresolve. 969 970 // Resolution of put instructions to final instance fields with invalid updates (i.e., 971 // to final instance fields with updates originating from a method different than <init>) 972 // is inhibited. A putfield instruction targeting an instance final field must throw 973 // an IllegalAccessError if the instruction is not in an instance 974 // initializer method <init>. If resolution were not inhibited, a putfield 975 // in an initializer method could be resolved in the initializer. Subsequent 976 // putfield instructions to the same field would then use cached information. 977 // As a result, those instructions would not pass through the VM. That is, 978 // checks in resolve_field_access() would not be executed for those instructions 979 // and the required IllegalAccessError would not be thrown. 980 // 981 // Also, we need to delay resolving getstatic and putstatic instructions until the 982 // class is initialized. This is required so that access to the static 983 // field will call the initialization function every time until the class 984 // is completely initialized ala. in 2.17.5 in JVM Specification. 985 InstanceKlass* klass = InstanceKlass::cast(info.field_holder()); 986 bool uninitialized_static = is_static && !klass->is_initialized(); 987 bool has_initialized_final_update = info.field_holder()->major_version() >= 53 && 988 info.has_initialized_final_update(); 989 assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final"); 990 991 Bytecodes::Code get_code = (Bytecodes::Code)0; 992 Bytecodes::Code put_code = (Bytecodes::Code)0; 993 if (!uninitialized_static) { 994 if (is_static) { 995 get_code = Bytecodes::_getstatic; 996 } else { 997 get_code = Bytecodes::_getfield; 998 } 999 if (is_put && is_value) { 1000 put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_vwithfield); 1001 } else if ((is_put && !has_initialized_final_update) || !info.access_flags().is_final()) { 1002 put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield); 1003 } 1004 } 1005 1006 cp_cache_entry->set_field( 1007 get_code, 1008 put_code, 1009 info.field_holder(), 1010 info.index(), 1011 info.offset(), 1012 state, 1013 info.access_flags().is_final(), 1014 info.access_flags().is_volatile(), 1015 info.is_flatten(), 1016 pool->pool_holder() 1017 ); 1018 } 1019 1020 1021 //------------------------------------------------------------------------------------------------------------------------ 1022 // Synchronization 1023 // 1024 // The interpreter's synchronization code is factored out so that it can 1025 // be shared by method invocation and synchronized blocks. 1026 //%note synchronization_3 1027 1028 //%note monitor_1 1029 IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* thread, BasicObjectLock* elem)) 1030 #ifdef ASSERT 1031 thread->last_frame().interpreter_frame_verify_monitor(elem); 1032 #endif 1033 if (PrintBiasedLockingStatistics) { 1034 Atomic::inc(BiasedLocking::slow_path_entry_count_addr()); 1035 } 1036 Handle h_obj(thread, elem->obj()); 1037 assert(Universe::heap()->is_in_reserved_or_null(h_obj()), 1038 "must be NULL or an object"); 1039 if (UseBiasedLocking) { 1040 // Retry fast entry if bias is revoked to avoid unnecessary inflation 1041 ObjectSynchronizer::fast_enter(h_obj, elem->lock(), true, CHECK); 1042 } else { 1043 ObjectSynchronizer::slow_enter(h_obj, elem->lock(), CHECK); 1044 } 1045 assert(Universe::heap()->is_in_reserved_or_null(elem->obj()), 1046 "must be NULL or an object"); 1047 #ifdef ASSERT 1048 thread->last_frame().interpreter_frame_verify_monitor(elem); 1049 #endif 1050 IRT_END 1051 1052 1053 //%note monitor_1 1054 IRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorexit(JavaThread* thread, BasicObjectLock* elem)) 1055 #ifdef ASSERT 1056 thread->last_frame().interpreter_frame_verify_monitor(elem); 1057 #endif 1058 Handle h_obj(thread, elem->obj()); 1059 assert(Universe::heap()->is_in_reserved_or_null(h_obj()), 1060 "must be NULL or an object"); 1061 if (elem == NULL || h_obj()->is_unlocked()) { 1062 THROW(vmSymbols::java_lang_IllegalMonitorStateException()); 1063 } 1064 ObjectSynchronizer::slow_exit(h_obj(), elem->lock(), thread); 1065 // Free entry. This must be done here, since a pending exception might be installed on 1066 // exit. If it is not cleared, the exception handling code will try to unlock the monitor again. 1067 elem->set_obj(NULL); 1068 #ifdef ASSERT 1069 thread->last_frame().interpreter_frame_verify_monitor(elem); 1070 #endif 1071 IRT_END 1072 1073 1074 IRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* thread)) 1075 THROW(vmSymbols::java_lang_IllegalMonitorStateException()); 1076 IRT_END 1077 1078 1079 IRT_ENTRY(void, InterpreterRuntime::new_illegal_monitor_state_exception(JavaThread* thread)) 1080 // Returns an illegal exception to install into the current thread. The 1081 // pending_exception flag is cleared so normal exception handling does not 1082 // trigger. Any current installed exception will be overwritten. This 1083 // method will be called during an exception unwind. 1084 1085 assert(!HAS_PENDING_EXCEPTION, "no pending exception"); 1086 Handle exception(thread, thread->vm_result()); 1087 assert(exception() != NULL, "vm result should be set"); 1088 thread->set_vm_result(NULL); // clear vm result before continuing (may cause memory leaks and assert failures) 1089 if (!exception->is_a(SystemDictionary::ThreadDeath_klass())) { 1090 exception = get_preinitialized_exception( 1091 SystemDictionary::IllegalMonitorStateException_klass(), 1092 CATCH); 1093 } 1094 thread->set_vm_result(exception()); 1095 IRT_END 1096 1097 1098 //------------------------------------------------------------------------------------------------------------------------ 1099 // Invokes 1100 1101 IRT_ENTRY(Bytecodes::Code, InterpreterRuntime::get_original_bytecode_at(JavaThread* thread, Method* method, address bcp)) 1102 return method->orig_bytecode_at(method->bci_from(bcp)); 1103 IRT_END 1104 1105 IRT_ENTRY(void, InterpreterRuntime::set_original_bytecode_at(JavaThread* thread, Method* method, address bcp, Bytecodes::Code new_code)) 1106 method->set_orig_bytecode_at(method->bci_from(bcp), new_code); 1107 IRT_END 1108 1109 IRT_ENTRY(void, InterpreterRuntime::_breakpoint(JavaThread* thread, Method* method, address bcp)) 1110 JvmtiExport::post_raw_breakpoint(thread, method, bcp); 1111 IRT_END 1112 1113 void InterpreterRuntime::resolve_invoke(JavaThread* thread, Bytecodes::Code bytecode) { 1114 Thread* THREAD = thread; 1115 // extract receiver from the outgoing argument list if necessary 1116 Handle receiver(thread, NULL); 1117 if (bytecode == Bytecodes::_invokevirtual || bytecode == Bytecodes::_invokeinterface || 1118 bytecode == Bytecodes::_invokespecial) { 1119 ResourceMark rm(thread); 1120 methodHandle m (thread, method(thread)); 1121 Bytecode_invoke call(m, bci(thread)); 1122 Symbol* signature = call.signature(); 1123 receiver = Handle(thread, 1124 thread->last_frame().interpreter_callee_receiver(signature)); 1125 assert(Universe::heap()->is_in_reserved_or_null(receiver()) 1126 || VTBuffer::is_in_vt_buffer(receiver()), 1127 "sanity check"); 1128 assert(receiver.is_null() || 1129 !Universe::heap()->is_in_reserved(receiver->klass()), 1130 "sanity check"); 1131 } 1132 1133 // resolve method 1134 CallInfo info; 1135 constantPoolHandle pool(thread, method(thread)->constants()); 1136 1137 { 1138 JvmtiHideSingleStepping jhss(thread); 1139 LinkResolver::resolve_invoke(info, receiver, pool, 1140 get_index_u2_cpcache(thread, bytecode), bytecode, 1141 CHECK); 1142 if (JvmtiExport::can_hotswap_or_post_breakpoint()) { 1143 int retry_count = 0; 1144 while (info.resolved_method()->is_old()) { 1145 // It is very unlikely that method is redefined more than 100 times 1146 // in the middle of resolve. If it is looping here more than 100 times 1147 // means then there could be a bug here. 1148 guarantee((retry_count++ < 100), 1149 "Could not resolve to latest version of redefined method"); 1150 // method is redefined in the middle of resolve so re-try. 1151 LinkResolver::resolve_invoke(info, receiver, pool, 1152 get_index_u2_cpcache(thread, bytecode), bytecode, 1153 CHECK); 1154 } 1155 } 1156 } // end JvmtiHideSingleStepping 1157 1158 // check if link resolution caused cpCache to be updated 1159 ConstantPoolCacheEntry* cp_cache_entry = cache_entry(thread); 1160 if (cp_cache_entry->is_resolved(bytecode)) return; 1161 1162 #ifdef ASSERT 1163 if (bytecode == Bytecodes::_invokeinterface) { 1164 if (info.resolved_method()->method_holder() == 1165 SystemDictionary::Object_klass()) { 1166 // NOTE: THIS IS A FIX FOR A CORNER CASE in the JVM spec 1167 // (see also CallInfo::set_interface for details) 1168 assert(info.call_kind() == CallInfo::vtable_call || 1169 info.call_kind() == CallInfo::direct_call, ""); 1170 methodHandle rm = info.resolved_method(); 1171 assert(rm->is_final() || info.has_vtable_index(), 1172 "should have been set already"); 1173 } else if (!info.resolved_method()->has_itable_index()) { 1174 // Resolved something like CharSequence.toString. Use vtable not itable. 1175 assert(info.call_kind() != CallInfo::itable_call, ""); 1176 } else { 1177 // Setup itable entry 1178 assert(info.call_kind() == CallInfo::itable_call, ""); 1179 int index = info.resolved_method()->itable_index(); 1180 assert(info.itable_index() == index, ""); 1181 } 1182 } else if (bytecode == Bytecodes::_invokespecial) { 1183 assert(info.call_kind() == CallInfo::direct_call, "must be direct call"); 1184 } else { 1185 assert(info.call_kind() == CallInfo::direct_call || 1186 info.call_kind() == CallInfo::vtable_call, ""); 1187 } 1188 #endif 1189 // Get sender or sender's host_klass, and only set cpCache entry to resolved if 1190 // it is not an interface. The receiver for invokespecial calls within interface 1191 // methods must be checked for every call. 1192 InstanceKlass* sender = pool->pool_holder(); 1193 sender = sender->is_anonymous() ? sender->host_klass() : sender; 1194 1195 switch (info.call_kind()) { 1196 case CallInfo::direct_call: 1197 cp_cache_entry->set_direct_call( 1198 bytecode, 1199 info.resolved_method(), 1200 sender->is_interface()); 1201 break; 1202 case CallInfo::vtable_call: 1203 cp_cache_entry->set_vtable_call( 1204 bytecode, 1205 info.resolved_method(), 1206 info.vtable_index()); 1207 break; 1208 case CallInfo::itable_call: 1209 cp_cache_entry->set_itable_call( 1210 bytecode, 1211 info.resolved_method(), 1212 info.itable_index()); 1213 break; 1214 default: ShouldNotReachHere(); 1215 } 1216 } 1217 1218 1219 // First time execution: Resolve symbols, create a permanent MethodType object. 1220 void InterpreterRuntime::resolve_invokehandle(JavaThread* thread) { 1221 Thread* THREAD = thread; 1222 const Bytecodes::Code bytecode = Bytecodes::_invokehandle; 1223 1224 // resolve method 1225 CallInfo info; 1226 constantPoolHandle pool(thread, method(thread)->constants()); 1227 { 1228 JvmtiHideSingleStepping jhss(thread); 1229 LinkResolver::resolve_invoke(info, Handle(), pool, 1230 get_index_u2_cpcache(thread, bytecode), bytecode, 1231 CHECK); 1232 } // end JvmtiHideSingleStepping 1233 1234 ConstantPoolCacheEntry* cp_cache_entry = cache_entry(thread); 1235 cp_cache_entry->set_method_handle(pool, info); 1236 } 1237 1238 // First time execution: Resolve symbols, create a permanent CallSite object. 1239 void InterpreterRuntime::resolve_invokedynamic(JavaThread* thread) { 1240 Thread* THREAD = thread; 1241 const Bytecodes::Code bytecode = Bytecodes::_invokedynamic; 1242 1243 //TO DO: consider passing BCI to Java. 1244 // int caller_bci = method(thread)->bci_from(bcp(thread)); 1245 1246 // resolve method 1247 CallInfo info; 1248 constantPoolHandle pool(thread, method(thread)->constants()); 1249 int index = get_index_u4(thread, bytecode); 1250 { 1251 JvmtiHideSingleStepping jhss(thread); 1252 LinkResolver::resolve_invoke(info, Handle(), pool, 1253 index, bytecode, CHECK); 1254 } // end JvmtiHideSingleStepping 1255 1256 ConstantPoolCacheEntry* cp_cache_entry = pool->invokedynamic_cp_cache_entry_at(index); 1257 cp_cache_entry->set_dynamic_call(pool, info); 1258 } 1259 1260 // This function is the interface to the assembly code. It returns the resolved 1261 // cpCache entry. This doesn't safepoint, but the helper routines safepoint. 1262 // This function will check for redefinition! 1263 IRT_ENTRY(void, InterpreterRuntime::resolve_from_cache(JavaThread* thread, Bytecodes::Code bytecode)) { 1264 switch (bytecode) { 1265 case Bytecodes::_getstatic: 1266 case Bytecodes::_putstatic: 1267 case Bytecodes::_getfield: 1268 case Bytecodes::_putfield: 1269 case Bytecodes::_vwithfield: 1270 resolve_get_put(thread, bytecode); 1271 break; 1272 case Bytecodes::_invokevirtual: 1273 case Bytecodes::_invokespecial: 1274 case Bytecodes::_invokestatic: 1275 case Bytecodes::_invokeinterface: 1276 resolve_invoke(thread, bytecode); 1277 break; 1278 case Bytecodes::_invokehandle: 1279 resolve_invokehandle(thread); 1280 break; 1281 case Bytecodes::_invokedynamic: 1282 resolve_invokedynamic(thread); 1283 break; 1284 default: 1285 fatal("unexpected bytecode: %s", Bytecodes::name(bytecode)); 1286 break; 1287 } 1288 } 1289 IRT_END 1290 1291 //------------------------------------------------------------------------------------------------------------------------ 1292 // Miscellaneous 1293 1294 1295 nmethod* InterpreterRuntime::frequency_counter_overflow(JavaThread* thread, address branch_bcp) { 1296 nmethod* nm = frequency_counter_overflow_inner(thread, branch_bcp); 1297 assert(branch_bcp != NULL || nm == NULL, "always returns null for non OSR requests"); 1298 if (branch_bcp != NULL && nm != NULL) { 1299 // This was a successful request for an OSR nmethod. Because 1300 // frequency_counter_overflow_inner ends with a safepoint check, 1301 // nm could have been unloaded so look it up again. It's unsafe 1302 // to examine nm directly since it might have been freed and used 1303 // for something else. 1304 frame fr = thread->last_frame(); 1305 Method* method = fr.interpreter_frame_method(); 1306 int bci = method->bci_from(fr.interpreter_frame_bcp()); 1307 nm = method->lookup_osr_nmethod_for(bci, CompLevel_none, false); 1308 } 1309 #ifndef PRODUCT 1310 if (TraceOnStackReplacement) { 1311 if (nm != NULL) { 1312 tty->print("OSR entry @ pc: " INTPTR_FORMAT ": ", p2i(nm->osr_entry())); 1313 nm->print(); 1314 } 1315 } 1316 #endif 1317 return nm; 1318 } 1319 1320 IRT_ENTRY(nmethod*, 1321 InterpreterRuntime::frequency_counter_overflow_inner(JavaThread* thread, address branch_bcp)) 1322 // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized 1323 // flag, in case this method triggers classloading which will call into Java. 1324 UnlockFlagSaver fs(thread); 1325 1326 frame fr = thread->last_frame(); 1327 assert(fr.is_interpreted_frame(), "must come from interpreter"); 1328 methodHandle method(thread, fr.interpreter_frame_method()); 1329 const int branch_bci = branch_bcp != NULL ? method->bci_from(branch_bcp) : InvocationEntryBci; 1330 const int bci = branch_bcp != NULL ? method->bci_from(fr.interpreter_frame_bcp()) : InvocationEntryBci; 1331 1332 assert(!HAS_PENDING_EXCEPTION, "Should not have any exceptions pending"); 1333 nmethod* osr_nm = CompilationPolicy::policy()->event(method, method, branch_bci, bci, CompLevel_none, NULL, thread); 1334 assert(!HAS_PENDING_EXCEPTION, "Event handler should not throw any exceptions"); 1335 1336 if (osr_nm != NULL) { 1337 // We may need to do on-stack replacement which requires that no 1338 // monitors in the activation are biased because their 1339 // BasicObjectLocks will need to migrate during OSR. Force 1340 // unbiasing of all monitors in the activation now (even though 1341 // the OSR nmethod might be invalidated) because we don't have a 1342 // safepoint opportunity later once the migration begins. 1343 if (UseBiasedLocking) { 1344 ResourceMark rm; 1345 GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>(); 1346 for( BasicObjectLock *kptr = fr.interpreter_frame_monitor_end(); 1347 kptr < fr.interpreter_frame_monitor_begin(); 1348 kptr = fr.next_monitor_in_interpreter_frame(kptr) ) { 1349 if( kptr->obj() != NULL ) { 1350 objects_to_revoke->append(Handle(THREAD, kptr->obj())); 1351 } 1352 } 1353 BiasedLocking::revoke(objects_to_revoke); 1354 } 1355 } 1356 return osr_nm; 1357 IRT_END 1358 1359 IRT_LEAF(jint, InterpreterRuntime::bcp_to_di(Method* method, address cur_bcp)) 1360 assert(ProfileInterpreter, "must be profiling interpreter"); 1361 int bci = method->bci_from(cur_bcp); 1362 MethodData* mdo = method->method_data(); 1363 if (mdo == NULL) return 0; 1364 return mdo->bci_to_di(bci); 1365 IRT_END 1366 1367 IRT_ENTRY(void, InterpreterRuntime::profile_method(JavaThread* thread)) 1368 // use UnlockFlagSaver to clear and restore the _do_not_unlock_if_synchronized 1369 // flag, in case this method triggers classloading which will call into Java. 1370 UnlockFlagSaver fs(thread); 1371 1372 assert(ProfileInterpreter, "must be profiling interpreter"); 1373 frame fr = thread->last_frame(); 1374 assert(fr.is_interpreted_frame(), "must come from interpreter"); 1375 methodHandle method(thread, fr.interpreter_frame_method()); 1376 Method::build_interpreter_method_data(method, THREAD); 1377 if (HAS_PENDING_EXCEPTION) { 1378 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here"); 1379 CLEAR_PENDING_EXCEPTION; 1380 // and fall through... 1381 } 1382 IRT_END 1383 1384 1385 #ifdef ASSERT 1386 IRT_LEAF(void, InterpreterRuntime::verify_mdp(Method* method, address bcp, address mdp)) 1387 assert(ProfileInterpreter, "must be profiling interpreter"); 1388 1389 MethodData* mdo = method->method_data(); 1390 assert(mdo != NULL, "must not be null"); 1391 1392 int bci = method->bci_from(bcp); 1393 1394 address mdp2 = mdo->bci_to_dp(bci); 1395 if (mdp != mdp2) { 1396 ResourceMark rm; 1397 ResetNoHandleMark rnm; // In a LEAF entry. 1398 HandleMark hm; 1399 tty->print_cr("FAILED verify : actual mdp %p expected mdp %p @ bci %d", mdp, mdp2, bci); 1400 int current_di = mdo->dp_to_di(mdp); 1401 int expected_di = mdo->dp_to_di(mdp2); 1402 tty->print_cr(" actual di %d expected di %d", current_di, expected_di); 1403 int expected_approx_bci = mdo->data_at(expected_di)->bci(); 1404 int approx_bci = -1; 1405 if (current_di >= 0) { 1406 approx_bci = mdo->data_at(current_di)->bci(); 1407 } 1408 tty->print_cr(" actual bci is %d expected bci %d", approx_bci, expected_approx_bci); 1409 mdo->print_on(tty); 1410 method->print_codes(); 1411 } 1412 assert(mdp == mdp2, "wrong mdp"); 1413 IRT_END 1414 #endif // ASSERT 1415 1416 IRT_ENTRY(void, InterpreterRuntime::update_mdp_for_ret(JavaThread* thread, int return_bci)) 1417 assert(ProfileInterpreter, "must be profiling interpreter"); 1418 ResourceMark rm(thread); 1419 HandleMark hm(thread); 1420 frame fr = thread->last_frame(); 1421 assert(fr.is_interpreted_frame(), "must come from interpreter"); 1422 MethodData* h_mdo = fr.interpreter_frame_method()->method_data(); 1423 1424 // Grab a lock to ensure atomic access to setting the return bci and 1425 // the displacement. This can block and GC, invalidating all naked oops. 1426 MutexLocker ml(RetData_lock); 1427 1428 // ProfileData is essentially a wrapper around a derived oop, so we 1429 // need to take the lock before making any ProfileData structures. 1430 ProfileData* data = h_mdo->data_at(h_mdo->dp_to_di(fr.interpreter_frame_mdp())); 1431 guarantee(data != NULL, "profile data must be valid"); 1432 RetData* rdata = data->as_RetData(); 1433 address new_mdp = rdata->fixup_ret(return_bci, h_mdo); 1434 fr.interpreter_frame_set_mdp(new_mdp); 1435 IRT_END 1436 1437 IRT_ENTRY(MethodCounters*, InterpreterRuntime::build_method_counters(JavaThread* thread, Method* m)) 1438 MethodCounters* mcs = Method::build_method_counters(m, thread); 1439 if (HAS_PENDING_EXCEPTION) { 1440 assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here"); 1441 CLEAR_PENDING_EXCEPTION; 1442 } 1443 return mcs; 1444 IRT_END 1445 1446 1447 IRT_ENTRY(void, InterpreterRuntime::at_safepoint(JavaThread* thread)) 1448 // We used to need an explict preserve_arguments here for invoke bytecodes. However, 1449 // stack traversal automatically takes care of preserving arguments for invoke, so 1450 // this is no longer needed. 1451 1452 // IRT_END does an implicit safepoint check, hence we are guaranteed to block 1453 // if this is called during a safepoint 1454 1455 if (JvmtiExport::should_post_single_step()) { 1456 // We are called during regular safepoints and when the VM is 1457 // single stepping. If any thread is marked for single stepping, 1458 // then we may have JVMTI work to do. 1459 JvmtiExport::at_single_stepping_point(thread, method(thread), bcp(thread)); 1460 } 1461 IRT_END 1462 1463 IRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread *thread, oopDesc* obj, 1464 ConstantPoolCacheEntry *cp_entry)) 1465 1466 // check the access_flags for the field in the klass 1467 1468 InstanceKlass* ik = InstanceKlass::cast(cp_entry->f1_as_klass()); 1469 int index = cp_entry->field_index(); 1470 if ((ik->field_access_flags(index) & JVM_ACC_FIELD_ACCESS_WATCHED) == 0) return; 1471 1472 bool is_static = (obj == NULL); 1473 HandleMark hm(thread); 1474 1475 Handle h_obj; 1476 if (!is_static) { 1477 // non-static field accessors have an object, but we need a handle 1478 h_obj = Handle(thread, obj); 1479 } 1480 InstanceKlass* cp_entry_f1 = InstanceKlass::cast(cp_entry->f1_as_klass()); 1481 jfieldID fid = jfieldIDWorkaround::to_jfieldID(cp_entry_f1, cp_entry->f2_as_index(), is_static); 1482 JvmtiExport::post_field_access(thread, method(thread), bcp(thread), cp_entry_f1, h_obj, fid); 1483 IRT_END 1484 1485 IRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread *thread, 1486 oopDesc* obj, ConstantPoolCacheEntry *cp_entry, jvalue *value)) 1487 1488 Klass* k = cp_entry->f1_as_klass(); 1489 1490 // check the access_flags for the field in the klass 1491 InstanceKlass* ik = InstanceKlass::cast(k); 1492 int index = cp_entry->field_index(); 1493 // bail out if field modifications are not watched 1494 if ((ik->field_access_flags(index) & JVM_ACC_FIELD_MODIFICATION_WATCHED) == 0) return; 1495 1496 char sig_type = '\0'; 1497 1498 switch(cp_entry->flag_state()) { 1499 case btos: sig_type = 'B'; break; 1500 case ztos: sig_type = 'Z'; break; 1501 case ctos: sig_type = 'C'; break; 1502 case stos: sig_type = 'S'; break; 1503 case itos: sig_type = 'I'; break; 1504 case ftos: sig_type = 'F'; break; 1505 case atos: sig_type = 'L'; break; 1506 case ltos: sig_type = 'J'; break; 1507 case dtos: sig_type = 'D'; break; 1508 default: ShouldNotReachHere(); return; 1509 } 1510 bool is_static = (obj == NULL); 1511 1512 HandleMark hm(thread); 1513 jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, cp_entry->f2_as_index(), is_static); 1514 jvalue fvalue; 1515 #ifdef _LP64 1516 fvalue = *value; 1517 #else 1518 // Long/double values are stored unaligned and also noncontiguously with 1519 // tagged stacks. We can't just do a simple assignment even in the non- 1520 // J/D cases because a C++ compiler is allowed to assume that a jvalue is 1521 // 8-byte aligned, and interpreter stack slots are only 4-byte aligned. 1522 // We assume that the two halves of longs/doubles are stored in interpreter 1523 // stack slots in platform-endian order. 1524 jlong_accessor u; 1525 jint* newval = (jint*)value; 1526 u.words[0] = newval[0]; 1527 u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag 1528 fvalue.j = u.long_value; 1529 #endif // _LP64 1530 1531 Handle h_obj; 1532 if (!is_static) { 1533 // non-static field accessors have an object, but we need a handle 1534 h_obj = Handle(thread, obj); 1535 } 1536 1537 JvmtiExport::post_raw_field_modification(thread, method(thread), bcp(thread), ik, h_obj, 1538 fid, sig_type, &fvalue); 1539 IRT_END 1540 1541 IRT_ENTRY(void, InterpreterRuntime::post_method_entry(JavaThread *thread)) 1542 JvmtiExport::post_method_entry(thread, InterpreterRuntime::method(thread), InterpreterRuntime::last_frame(thread)); 1543 IRT_END 1544 1545 1546 IRT_ENTRY(void, InterpreterRuntime::post_method_exit(JavaThread *thread)) 1547 JvmtiExport::post_method_exit(thread, InterpreterRuntime::method(thread), InterpreterRuntime::last_frame(thread)); 1548 IRT_END 1549 1550 IRT_LEAF(int, InterpreterRuntime::interpreter_contains(address pc)) 1551 { 1552 return (Interpreter::contains(pc) ? 1 : 0); 1553 } 1554 IRT_END 1555 1556 1557 // Implementation of SignatureHandlerLibrary 1558 1559 #ifndef SHARING_FAST_NATIVE_FINGERPRINTS 1560 // Dummy definition (else normalization method is defined in CPU 1561 // dependant code) 1562 uint64_t InterpreterRuntime::normalize_fast_native_fingerprint(uint64_t fingerprint) { 1563 return fingerprint; 1564 } 1565 #endif 1566 1567 address SignatureHandlerLibrary::set_handler_blob() { 1568 BufferBlob* handler_blob = BufferBlob::create("native signature handlers", blob_size); 1569 if (handler_blob == NULL) { 1570 return NULL; 1571 } 1572 address handler = handler_blob->code_begin(); 1573 _handler_blob = handler_blob; 1574 _handler = handler; 1575 return handler; 1576 } 1577 1578 void SignatureHandlerLibrary::initialize() { 1579 if (_fingerprints != NULL) { 1580 return; 1581 } 1582 if (set_handler_blob() == NULL) { 1583 vm_exit_out_of_memory(blob_size, OOM_MALLOC_ERROR, "native signature handlers"); 1584 } 1585 1586 BufferBlob* bb = BufferBlob::create("Signature Handler Temp Buffer", 1587 SignatureHandlerLibrary::buffer_size); 1588 _buffer = bb->code_begin(); 1589 1590 _fingerprints = new(ResourceObj::C_HEAP, mtCode)GrowableArray<uint64_t>(32, true); 1591 _handlers = new(ResourceObj::C_HEAP, mtCode)GrowableArray<address>(32, true); 1592 } 1593 1594 address SignatureHandlerLibrary::set_handler(CodeBuffer* buffer) { 1595 address handler = _handler; 1596 int insts_size = buffer->pure_insts_size(); 1597 if (handler + insts_size > _handler_blob->code_end()) { 1598 // get a new handler blob 1599 handler = set_handler_blob(); 1600 } 1601 if (handler != NULL) { 1602 memcpy(handler, buffer->insts_begin(), insts_size); 1603 pd_set_handler(handler); 1604 ICache::invalidate_range(handler, insts_size); 1605 _handler = handler + insts_size; 1606 } 1607 return handler; 1608 } 1609 1610 void SignatureHandlerLibrary::add(const methodHandle& method) { 1611 if (method->signature_handler() == NULL) { 1612 // use slow signature handler if we can't do better 1613 int handler_index = -1; 1614 // check if we can use customized (fast) signature handler 1615 if (UseFastSignatureHandlers && method->size_of_parameters() <= Fingerprinter::max_size_of_parameters) { 1616 // use customized signature handler 1617 MutexLocker mu(SignatureHandlerLibrary_lock); 1618 // make sure data structure is initialized 1619 initialize(); 1620 // lookup method signature's fingerprint 1621 uint64_t fingerprint = Fingerprinter(method).fingerprint(); 1622 // allow CPU dependant code to optimize the fingerprints for the fast handler 1623 fingerprint = InterpreterRuntime::normalize_fast_native_fingerprint(fingerprint); 1624 handler_index = _fingerprints->find(fingerprint); 1625 // create handler if necessary 1626 if (handler_index < 0) { 1627 ResourceMark rm; 1628 ptrdiff_t align_offset = align_up(_buffer, CodeEntryAlignment) - (address)_buffer; 1629 CodeBuffer buffer((address)(_buffer + align_offset), 1630 SignatureHandlerLibrary::buffer_size - align_offset); 1631 InterpreterRuntime::SignatureHandlerGenerator(method, &buffer).generate(fingerprint); 1632 // copy into code heap 1633 address handler = set_handler(&buffer); 1634 if (handler == NULL) { 1635 // use slow signature handler (without memorizing it in the fingerprints) 1636 } else { 1637 // debugging suppport 1638 if (PrintSignatureHandlers && (handler != Interpreter::slow_signature_handler())) { 1639 ttyLocker ttyl; 1640 tty->cr(); 1641 tty->print_cr("argument handler #%d for: %s %s (fingerprint = " UINT64_FORMAT ", %d bytes generated)", 1642 _handlers->length(), 1643 (method->is_static() ? "static" : "receiver"), 1644 method->name_and_sig_as_C_string(), 1645 fingerprint, 1646 buffer.insts_size()); 1647 if (buffer.insts_size() > 0) { 1648 Disassembler::decode(handler, handler + buffer.insts_size()); 1649 } 1650 #ifndef PRODUCT 1651 address rh_begin = Interpreter::result_handler(method()->result_type()); 1652 if (CodeCache::contains(rh_begin)) { 1653 // else it might be special platform dependent values 1654 tty->print_cr(" --- associated result handler ---"); 1655 address rh_end = rh_begin; 1656 while (*(int*)rh_end != 0) { 1657 rh_end += sizeof(int); 1658 } 1659 Disassembler::decode(rh_begin, rh_end); 1660 } else { 1661 tty->print_cr(" associated result handler: " PTR_FORMAT, p2i(rh_begin)); 1662 } 1663 #endif 1664 } 1665 // add handler to library 1666 _fingerprints->append(fingerprint); 1667 _handlers->append(handler); 1668 // set handler index 1669 assert(_fingerprints->length() == _handlers->length(), "sanity check"); 1670 handler_index = _fingerprints->length() - 1; 1671 } 1672 } 1673 // Set handler under SignatureHandlerLibrary_lock 1674 if (handler_index < 0) { 1675 // use generic signature handler 1676 method->set_signature_handler(Interpreter::slow_signature_handler()); 1677 } else { 1678 // set handler 1679 method->set_signature_handler(_handlers->at(handler_index)); 1680 } 1681 } else { 1682 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); 1683 // use generic signature handler 1684 method->set_signature_handler(Interpreter::slow_signature_handler()); 1685 } 1686 } 1687 #ifdef ASSERT 1688 int handler_index = -1; 1689 int fingerprint_index = -2; 1690 { 1691 // '_handlers' and '_fingerprints' are 'GrowableArray's and are NOT synchronized 1692 // in any way if accessed from multiple threads. To avoid races with another 1693 // thread which may change the arrays in the above, mutex protected block, we 1694 // have to protect this read access here with the same mutex as well! 1695 MutexLocker mu(SignatureHandlerLibrary_lock); 1696 if (_handlers != NULL) { 1697 handler_index = _handlers->find(method->signature_handler()); 1698 uint64_t fingerprint = Fingerprinter(method).fingerprint(); 1699 fingerprint = InterpreterRuntime::normalize_fast_native_fingerprint(fingerprint); 1700 fingerprint_index = _fingerprints->find(fingerprint); 1701 } 1702 } 1703 assert(method->signature_handler() == Interpreter::slow_signature_handler() || 1704 handler_index == fingerprint_index, "sanity check"); 1705 #endif // ASSERT 1706 } 1707 1708 void SignatureHandlerLibrary::add(uint64_t fingerprint, address handler) { 1709 int handler_index = -1; 1710 // use customized signature handler 1711 MutexLocker mu(SignatureHandlerLibrary_lock); 1712 // make sure data structure is initialized 1713 initialize(); 1714 fingerprint = InterpreterRuntime::normalize_fast_native_fingerprint(fingerprint); 1715 handler_index = _fingerprints->find(fingerprint); 1716 // create handler if necessary 1717 if (handler_index < 0) { 1718 if (PrintSignatureHandlers && (handler != Interpreter::slow_signature_handler())) { 1719 tty->cr(); 1720 tty->print_cr("argument handler #%d at " PTR_FORMAT " for fingerprint " UINT64_FORMAT, 1721 _handlers->length(), 1722 p2i(handler), 1723 fingerprint); 1724 } 1725 _fingerprints->append(fingerprint); 1726 _handlers->append(handler); 1727 } else { 1728 if (PrintSignatureHandlers) { 1729 tty->cr(); 1730 tty->print_cr("duplicate argument handler #%d for fingerprint " UINT64_FORMAT "(old: " PTR_FORMAT ", new : " PTR_FORMAT ")", 1731 _handlers->length(), 1732 fingerprint, 1733 p2i(_handlers->at(handler_index)), 1734 p2i(handler)); 1735 } 1736 } 1737 } 1738 1739 1740 BufferBlob* SignatureHandlerLibrary::_handler_blob = NULL; 1741 address SignatureHandlerLibrary::_handler = NULL; 1742 GrowableArray<uint64_t>* SignatureHandlerLibrary::_fingerprints = NULL; 1743 GrowableArray<address>* SignatureHandlerLibrary::_handlers = NULL; 1744 address SignatureHandlerLibrary::_buffer = NULL; 1745 1746 1747 IRT_ENTRY(void, InterpreterRuntime::prepare_native_call(JavaThread* thread, Method* method)) 1748 methodHandle m(thread, method); 1749 assert(m->is_native(), "sanity check"); 1750 // lookup native function entry point if it doesn't exist 1751 bool in_base_library; 1752 if (!m->has_native_function()) { 1753 NativeLookup::lookup(m, in_base_library, CHECK); 1754 } 1755 // make sure signature handler is installed 1756 SignatureHandlerLibrary::add(m); 1757 // The interpreter entry point checks the signature handler first, 1758 // before trying to fetch the native entry point and klass mirror. 1759 // We must set the signature handler last, so that multiple processors 1760 // preparing the same method will be sure to see non-null entry & mirror. 1761 IRT_END 1762 1763 #if defined(IA32) || defined(AMD64) || defined(ARM) 1764 IRT_LEAF(void, InterpreterRuntime::popframe_move_outgoing_args(JavaThread* thread, void* src_address, void* dest_address)) 1765 if (src_address == dest_address) { 1766 return; 1767 } 1768 ResetNoHandleMark rnm; // In a LEAF entry. 1769 HandleMark hm; 1770 ResourceMark rm; 1771 frame fr = thread->last_frame(); 1772 assert(fr.is_interpreted_frame(), ""); 1773 jint bci = fr.interpreter_frame_bci(); 1774 methodHandle mh(thread, fr.interpreter_frame_method()); 1775 Bytecode_invoke invoke(mh, bci); 1776 ArgumentSizeComputer asc(invoke.signature()); 1777 int size_of_arguments = (asc.size() + (invoke.has_receiver() ? 1 : 0)); // receiver 1778 Copy::conjoint_jbytes(src_address, dest_address, 1779 size_of_arguments * Interpreter::stackElementSize); 1780 IRT_END 1781 #endif 1782 1783 #if INCLUDE_JVMTI 1784 // This is a support of the JVMTI PopFrame interface. 1785 // Make sure it is an invokestatic of a polymorphic intrinsic that has a member_name argument 1786 // and return it as a vm_result so that it can be reloaded in the list of invokestatic parameters. 1787 // The member_name argument is a saved reference (in local#0) to the member_name. 1788 // For backward compatibility with some JDK versions (7, 8) it can also be a direct method handle. 1789 // FIXME: remove DMH case after j.l.i.InvokerBytecodeGenerator code shape is updated. 1790 IRT_ENTRY(void, InterpreterRuntime::member_name_arg_or_null(JavaThread* thread, address member_name, 1791 Method* method, address bcp)) 1792 Bytecodes::Code code = Bytecodes::code_at(method, bcp); 1793 if (code != Bytecodes::_invokestatic) { 1794 return; 1795 } 1796 ConstantPool* cpool = method->constants(); 1797 int cp_index = Bytes::get_native_u2(bcp + 1) + ConstantPool::CPCACHE_INDEX_TAG; 1798 Symbol* cname = cpool->klass_name_at(cpool->klass_ref_index_at(cp_index)); 1799 Symbol* mname = cpool->name_ref_at(cp_index); 1800 1801 if (MethodHandles::has_member_arg(cname, mname)) { 1802 oop member_name_oop = (oop) member_name; 1803 if (java_lang_invoke_DirectMethodHandle::is_instance(member_name_oop)) { 1804 // FIXME: remove after j.l.i.InvokerBytecodeGenerator code shape is updated. 1805 member_name_oop = java_lang_invoke_DirectMethodHandle::member(member_name_oop); 1806 } 1807 thread->set_vm_result(member_name_oop); 1808 } else { 1809 thread->set_vm_result(NULL); 1810 } 1811 IRT_END 1812 #endif // INCLUDE_JVMTI 1813 1814 #ifndef PRODUCT 1815 // This must be a IRT_LEAF function because the interpreter must save registers on x86 to 1816 // call this, which changes rsp and makes the interpreter's expression stack not walkable. 1817 // The generated code still uses call_VM because that will set up the frame pointer for 1818 // bcp and method. 1819 IRT_LEAF(intptr_t, InterpreterRuntime::trace_bytecode(JavaThread* thread, intptr_t preserve_this_value, intptr_t tos, intptr_t tos2)) 1820 const frame f = thread->last_frame(); 1821 assert(f.is_interpreted_frame(), "must be an interpreted frame"); 1822 methodHandle mh(thread, f.interpreter_frame_method()); 1823 BytecodeTracer::trace(mh, f.interpreter_frame_bcp(), tos, tos2); 1824 return preserve_this_value; 1825 IRT_END 1826 #endif // !PRODUCT