< prev index next >

src/hotspot/share/interpreter/interpreterRuntime.cpp

Print this page

   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 "classfile/javaClasses.inline.hpp"
  26 #include "classfile/symbolTable.hpp"

  27 #include "classfile/vmClasses.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "code/codeCache.hpp"
  30 #include "compiler/compilationPolicy.hpp"
  31 #include "compiler/compileBroker.hpp"
  32 #include "compiler/disassembler.hpp"
  33 #include "gc/shared/barrierSetNMethod.hpp"
  34 #include "gc/shared/collectedHeap.hpp"
  35 #include "interpreter/bytecodeTracer.hpp"
  36 #include "interpreter/interpreter.hpp"
  37 #include "interpreter/interpreterRuntime.hpp"
  38 #include "interpreter/linkResolver.hpp"
  39 #include "interpreter/templateTable.hpp"
  40 #include "jvm_io.h"
  41 #include "logging/log.hpp"
  42 #include "memory/oopFactory.hpp"
  43 #include "memory/resourceArea.hpp"
  44 #include "memory/universe.hpp"
  45 #include "oops/constantPool.inline.hpp"
  46 #include "oops/cpCache.inline.hpp"



  47 #include "oops/instanceKlass.inline.hpp"
  48 #include "oops/klass.inline.hpp"
  49 #include "oops/method.inline.hpp"
  50 #include "oops/methodData.hpp"
  51 #include "oops/objArrayKlass.hpp"
  52 #include "oops/objArrayOop.inline.hpp"
  53 #include "oops/oop.inline.hpp"
  54 #include "oops/symbol.hpp"
  55 #include "prims/jvmtiExport.hpp"
  56 #include "prims/methodHandles.hpp"
  57 #include "prims/nativeLookup.hpp"
  58 #include "runtime/atomicAccess.hpp"
  59 #include "runtime/continuation.hpp"
  60 #include "runtime/deoptimization.hpp"
  61 #include "runtime/fieldDescriptor.inline.hpp"
  62 #include "runtime/frame.inline.hpp"
  63 #include "runtime/handles.inline.hpp"
  64 #include "runtime/icache.hpp"
  65 #include "runtime/interfaceSupport.inline.hpp"
  66 #include "runtime/java.hpp"
  67 #include "runtime/javaCalls.hpp"
  68 #include "runtime/jfieldIDWorkaround.hpp"
  69 #include "runtime/osThread.hpp"
  70 #include "runtime/sharedRuntime.hpp"
  71 #include "runtime/stackWatermarkSet.hpp"
  72 #include "runtime/stubRoutines.hpp"
  73 #include "runtime/synchronizer.inline.hpp"
  74 #include "utilities/align.hpp"
  75 #include "utilities/checkedCast.hpp"
  76 #include "utilities/copy.hpp"
  77 #include "utilities/events.hpp"

  78 #if INCLUDE_JFR
  79 #include "jfr/jfr.inline.hpp"
  80 #endif
  81 
  82 // Helper class to access current interpreter state
  83 class LastFrameAccessor : public StackObj {
  84   frame _last_frame;
  85 public:
  86   LastFrameAccessor(JavaThread* current) {
  87     assert(current == Thread::current(), "sanity");
  88     _last_frame = current->last_frame();
  89   }
  90   bool is_interpreted_frame() const              { return _last_frame.is_interpreted_frame(); }
  91   Method*   method() const                       { return _last_frame.interpreter_frame_method(); }
  92   address   bcp() const                          { return _last_frame.interpreter_frame_bcp(); }
  93   int       bci() const                          { return _last_frame.interpreter_frame_bci(); }
  94   address   mdp() const                          { return _last_frame.interpreter_frame_mdp(); }
  95 
  96   void      set_bcp(address bcp)                 { _last_frame.interpreter_frame_set_bcp(bcp); }
  97   void      set_mdp(address dp)                  { _last_frame.interpreter_frame_set_mdp(dp); }

 208 JRT_END
 209 
 210 
 211 //------------------------------------------------------------------------------------------------------------------------
 212 // Allocation
 213 
 214 JRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* current, ConstantPool* pool, int index))
 215   Klass* k = pool->klass_at(index, CHECK);
 216   InstanceKlass* klass = InstanceKlass::cast(k);
 217 
 218   // Make sure we are not instantiating an abstract klass
 219   klass->check_valid_for_instantiation(true, CHECK);
 220 
 221   // Make sure klass is initialized
 222   klass->initialize(CHECK);
 223 
 224   oop obj = klass->allocate_instance(CHECK);
 225   current->set_vm_result_oop(obj);
 226 JRT_END
 227 
































 228 
 229 JRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* current, BasicType type, jint size))
 230   oop obj = oopFactory::new_typeArray(type, size, CHECK);
 231   current->set_vm_result_oop(obj);
 232 JRT_END
 233 
 234 
 235 JRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* current, ConstantPool* pool, int index, jint size))
 236   Klass*    klass = pool->klass_at(index, CHECK);
 237   objArrayOop obj = oopFactory::new_objArray(klass, size, CHECK);
 238   current->set_vm_result_oop(obj);
 239 JRT_END
 240 












 241 
 242 JRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* current, jint* first_size_address))
 243   // We may want to pass in more arguments - could make this slightly faster
 244   LastFrameAccessor last_frame(current);
 245   ConstantPool* constants = last_frame.method()->constants();
 246   int          i = last_frame.get_index_u2(Bytecodes::_multianewarray);
 247   Klass* klass   = constants->klass_at(i, CHECK);
 248   int   nof_dims = last_frame.number_of_dimensions();
 249   assert(klass->is_klass(), "not a class");
 250   assert(nof_dims >= 1, "multianewarray rank must be nonzero");
 251 
 252   // We must create an array of jints to pass to multi_allocate.
 253   ResourceMark rm(current);
 254   const int small_dims = 10;
 255   jint dim_array[small_dims];
 256   jint *dims = &dim_array[0];
 257   if (nof_dims > small_dims) {
 258     dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims);
 259   }
 260   for (int index = 0; index < nof_dims; index++) {
 261     // offset from first_size_address is addressed as local[index]
 262     int n = Interpreter::local_offset_in_bytes(index)/jintSize;
 263     dims[index] = first_size_address[n];
 264   }
 265   oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK);
 266   current->set_vm_result_oop(obj);
 267 JRT_END
 268 
 269 
 270 JRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* current, oopDesc* obj))
 271   assert(oopDesc::is_oop(obj), "must be a valid oop");
 272   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
 273   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
 274 JRT_END
 275 
























 276 
 277 // Quicken instance-of and check-cast bytecodes
 278 JRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* current))
 279   // Force resolving; quicken the bytecode
 280   LastFrameAccessor last_frame(current);
 281   int which = last_frame.get_index_u2(Bytecodes::_checkcast);
 282   ConstantPool* cpool = last_frame.method()->constants();
 283   // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded
 284   // program we might have seen an unquick'd bytecode in the interpreter but have another
 285   // thread quicken the bytecode before we get here.
 286   // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" );
 287   Klass* klass = cpool->klass_at(which, CHECK);
 288   current->set_vm_result_metadata(klass);
 289 JRT_END
 290 
 291 
 292 //------------------------------------------------------------------------------------------------------------------------
 293 // Exceptions
 294 
 295 void InterpreterRuntime::note_trap_inner(JavaThread* current, int reason,

 603 // and therefore we don't have the receiver object at our fingertips. (Though,
 604 // on some platforms the receiver still resides in a register...). Thus,
 605 // we have no choice but print an error message not containing the receiver
 606 // type.
 607 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorWithMethod(JavaThread* current,
 608                                                                         Method* missingMethod))
 609   ResourceMark rm(current);
 610   assert(missingMethod != nullptr, "sanity");
 611   methodHandle m(current, missingMethod);
 612   LinkResolver::throw_abstract_method_error(m, THREAD);
 613 JRT_END
 614 
 615 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorVerbose(JavaThread* current,
 616                                                                      Klass* recvKlass,
 617                                                                      Method* missingMethod))
 618   ResourceMark rm(current);
 619   methodHandle mh = methodHandle(current, missingMethod);
 620   LinkResolver::throw_abstract_method_error(mh, recvKlass, THREAD);
 621 JRT_END
 622 




 623 
 624 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* current))
 625   THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
 626 JRT_END
 627 
 628 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose(JavaThread* current,
 629                                                                               Klass* recvKlass,
 630                                                                               Klass* interfaceKlass))
 631   ResourceMark rm(current);
 632   char buf[1000];
 633   buf[0] = '\0';
 634   jio_snprintf(buf, sizeof(buf),
 635                "Class %s does not implement the requested interface %s",
 636                recvKlass ? recvKlass->external_name() : "nullptr",
 637                interfaceKlass ? interfaceKlass->external_name() : "nullptr");
 638   THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 639 JRT_END
 640 
 641 JRT_ENTRY(void, InterpreterRuntime::throw_NullPointerException(JavaThread* current))
 642   THROW(vmSymbols::java_lang_NullPointerException());

 682 
 683   // Resolution of put instructions to final instance fields with invalid updates (i.e.,
 684   // to final instance fields with updates originating from a method different than <init>)
 685   // is inhibited. A putfield instruction targeting an instance final field must throw
 686   // an IllegalAccessError if the instruction is not in an instance
 687   // initializer method <init>. If resolution were not inhibited, a putfield
 688   // in an initializer method could be resolved in the initializer. Subsequent
 689   // putfield instructions to the same field would then use cached information.
 690   // As a result, those instructions would not pass through the VM. That is,
 691   // checks in resolve_field_access() would not be executed for those instructions
 692   // and the required IllegalAccessError would not be thrown.
 693   //
 694   // Also, we need to delay resolving getstatic and putstatic instructions until the
 695   // class is initialized.  This is required so that access to the static
 696   // field will call the initialization function every time until the class
 697   // is completely initialized ala. in 2.17.5 in JVM Specification.
 698   InstanceKlass* klass = info.field_holder();
 699   bool uninitialized_static = is_static && !klass->is_initialized();
 700   bool has_initialized_final_update = info.field_holder()->major_version() >= 53 &&
 701                                       info.has_initialized_final_update();

 702   assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final");
 703 
 704   Bytecodes::Code get_code = (Bytecodes::Code)0;
 705   Bytecodes::Code put_code = (Bytecodes::Code)0;
 706   if (!uninitialized_static || VM_Version::supports_fast_class_init_checks()) {
 707     get_code = ((is_static) ? Bytecodes::_getstatic : Bytecodes::_getfield);
 708     if ((is_put && !has_initialized_final_update) || !info.access_flags().is_final()) {
 709       put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
 710     }













 711   }
 712 
 713   ResolvedFieldEntry* entry = pool->resolved_field_entry_at(field_index);
 714   entry->set_flags(info.access_flags().is_final(), info.access_flags().is_volatile());



 715   entry->fill_in(info.field_holder(), info.offset(),
 716                  checked_cast<u2>(info.index()), checked_cast<u1>(state),
 717                  static_cast<u1>(get_code), static_cast<u1>(put_code));
 718 }
 719 
 720 
 721 //------------------------------------------------------------------------------------------------------------------------
 722 // Synchronization
 723 //
 724 // The interpreter's synchronization code is factored out so that it can
 725 // be shared by method invocation and synchronized blocks.
 726 //%note synchronization_3
 727 
 728 //%note monitor_1
 729 JRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* current, BasicObjectLock* elem))
 730 #ifdef ASSERT
 731   current->last_frame().interpreter_frame_verify_monitor(elem);
 732 #endif
 733   Handle h_obj(current, elem->obj());
 734   assert(Universe::heap()->is_in_or_null(h_obj()),

 741 #endif
 742 JRT_END
 743 
 744 JRT_LEAF(void, InterpreterRuntime::monitorexit(BasicObjectLock* elem))
 745   oop obj = elem->obj();
 746   assert(Universe::heap()->is_in(obj), "must be an object");
 747   // The object could become unlocked through a JNI call, which we have no other checks for.
 748   // Give a fatal message if CheckJNICalls. Otherwise we ignore it.
 749   if (obj->is_unlocked()) {
 750     if (CheckJNICalls) {
 751       fatal("Object has been unlocked by JNI");
 752     }
 753     return;
 754   }
 755   ObjectSynchronizer::exit(obj, elem->lock(), JavaThread::current());
 756   // Free entry. If it is not cleared, the exception handling code will try to unlock the monitor
 757   // again at method exit or in the case of an exception.
 758   elem->set_obj(nullptr);
 759 JRT_END
 760 
 761 
 762 JRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* current))
 763   THROW(vmSymbols::java_lang_IllegalMonitorStateException());
 764 JRT_END
 765 
 766 
 767 JRT_ENTRY(void, InterpreterRuntime::new_illegal_monitor_state_exception(JavaThread* current))
 768   // Returns an illegal exception to install into the current thread. The
 769   // pending_exception flag is cleared so normal exception handling does not
 770   // trigger. Any current installed exception will be overwritten. This
 771   // method will be called during an exception unwind.
 772 
 773   assert(!HAS_PENDING_EXCEPTION, "no pending exception");
 774   Handle exception(current, current->vm_result_oop());
 775   assert(exception() != nullptr, "vm result should be set");
 776   current->set_vm_result_oop(nullptr); // clear vm result before continuing (may cause memory leaks and assert failures)
 777   exception = get_preinitialized_exception(vmClasses::IllegalMonitorStateException_klass(), CATCH);
 778   current->set_vm_result_oop(exception());
 779 JRT_END
 780 















 781 
 782 //------------------------------------------------------------------------------------------------------------------------
 783 // Invokes
 784 
 785 JRT_ENTRY(Bytecodes::Code, InterpreterRuntime::get_original_bytecode_at(JavaThread* current, Method* method, address bcp))
 786   return method->orig_bytecode_at(method->bci_from(bcp));
 787 JRT_END
 788 
 789 JRT_ENTRY(void, InterpreterRuntime::set_original_bytecode_at(JavaThread* current, Method* method, address bcp, Bytecodes::Code new_code))
 790   method->set_orig_bytecode_at(method->bci_from(bcp), new_code);
 791 JRT_END
 792 
 793 JRT_ENTRY(void, InterpreterRuntime::_breakpoint(JavaThread* current, Method* method, address bcp))
 794   JvmtiExport::post_raw_breakpoint(current, method, bcp);
 795 JRT_END
 796 
 797 void InterpreterRuntime::resolve_invoke(JavaThread* current, Bytecodes::Code bytecode) {
 798   LastFrameAccessor last_frame(current);
 799   // extract receiver from the outgoing argument list if necessary
 800   Handle receiver(current, nullptr);

1171     JvmtiExport::at_single_stepping_point(current, last_frame.method(), last_frame.bcp());
1172   }
1173 JRT_END
1174 
1175 JRT_LEAF(void, InterpreterRuntime::at_unwind(JavaThread* current))
1176   assert(current == JavaThread::current(), "pre-condition");
1177   JFR_ONLY(Jfr::check_and_process_sample_request(current);)
1178   // This function is called by the interpreter when the return poll found a reason
1179   // to call the VM. The reason could be that we are returning into a not yet safe
1180   // to access frame. We handle that below.
1181   // Note that this path does not check for single stepping, because we do not want
1182   // to single step when unwinding frames for an exception being thrown. Instead,
1183   // such single stepping code will use the safepoint table, which will use the
1184   // InterpreterRuntime::at_safepoint callback.
1185   StackWatermarkSet::before_unwind(current);
1186 JRT_END
1187 
1188 JRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread* current, oopDesc* obj,
1189                                                       ResolvedFieldEntry *entry))
1190 

1191   // check the access_flags for the field in the klass
1192 
1193   InstanceKlass* ik = entry->field_holder();
1194   int index = entry->field_index();
1195   if (!ik->field_status(index).is_access_watched()) return;
1196 
1197   bool is_static = (obj == nullptr);

1198   HandleMark hm(current);
1199 
1200   Handle h_obj;
1201   if (!is_static) {
1202     // non-static field accessors have an object, but we need a handle
1203     h_obj = Handle(current, obj);
1204   }
1205   InstanceKlass* field_holder = entry->field_holder(); // HERE
1206   jfieldID fid = jfieldIDWorkaround::to_jfieldID(field_holder, entry->field_offset(), is_static);
1207   LastFrameAccessor last_frame(current);
1208   JvmtiExport::post_field_access(current, last_frame.method(), last_frame.bcp(), field_holder, h_obj, fid);
1209 JRT_END
1210 
1211 JRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread* current, oopDesc* obj,
1212                                                             ResolvedFieldEntry *entry, jvalue *value))
1213 

1214   InstanceKlass* ik = entry->field_holder();
1215 
1216   // check the access_flags for the field in the klass
1217   int index = entry->field_index();
1218   // bail out if field modifications are not watched
1219   if (!ik->field_status(index).is_modification_watched()) return;
1220 
1221   char sig_type = '\0';
1222 
1223   switch((TosState)entry->tos_state()) {
1224     case btos: sig_type = JVM_SIGNATURE_BYTE;    break;
1225     case ztos: sig_type = JVM_SIGNATURE_BOOLEAN; break;
1226     case ctos: sig_type = JVM_SIGNATURE_CHAR;    break;
1227     case stos: sig_type = JVM_SIGNATURE_SHORT;   break;
1228     case itos: sig_type = JVM_SIGNATURE_INT;     break;
1229     case ftos: sig_type = JVM_SIGNATURE_FLOAT;   break;
1230     case atos: sig_type = JVM_SIGNATURE_CLASS;   break;
1231     case ltos: sig_type = JVM_SIGNATURE_LONG;    break;
1232     case dtos: sig_type = JVM_SIGNATURE_DOUBLE;  break;
1233     default:  ShouldNotReachHere(); return;
1234   }

1235   bool is_static = (obj == nullptr);

1236 
1237   HandleMark hm(current);
1238   jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, entry->field_offset(), is_static);
1239   jvalue fvalue;
1240 #ifdef _LP64
1241   fvalue = *value;
1242 #else
1243   // Long/double values are stored unaligned and also noncontiguously with
1244   // tagged stacks.  We can't just do a simple assignment even in the non-
1245   // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1246   // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1247   // We assume that the two halves of longs/doubles are stored in interpreter
1248   // stack slots in platform-endian order.
1249   jlong_accessor u;
1250   jint* newval = (jint*)value;
1251   u.words[0] = newval[0];
1252   u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1253   fvalue.j = u.long_value;
1254 #endif // _LP64
1255 
1256   Handle h_obj;
1257   if (!is_static) {
1258     // non-static field accessors have an object, but we need a handle

   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 "classfile/javaClasses.inline.hpp"
  26 #include "classfile/symbolTable.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmClasses.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "code/codeCache.hpp"
  31 #include "compiler/compilationPolicy.hpp"
  32 #include "compiler/compileBroker.hpp"
  33 #include "compiler/disassembler.hpp"
  34 #include "gc/shared/barrierSetNMethod.hpp"
  35 #include "gc/shared/collectedHeap.hpp"
  36 #include "interpreter/bytecodeTracer.hpp"
  37 #include "interpreter/interpreter.hpp"
  38 #include "interpreter/interpreterRuntime.hpp"
  39 #include "interpreter/linkResolver.hpp"
  40 #include "interpreter/templateTable.hpp"
  41 #include "jvm_io.h"
  42 #include "logging/log.hpp"
  43 #include "memory/oopFactory.hpp"
  44 #include "memory/resourceArea.hpp"
  45 #include "memory/universe.hpp"
  46 #include "oops/constantPool.inline.hpp"
  47 #include "oops/cpCache.inline.hpp"
  48 #include "oops/flatArrayKlass.hpp"
  49 #include "oops/flatArrayOop.inline.hpp"
  50 #include "oops/inlineKlass.inline.hpp"
  51 #include "oops/instanceKlass.inline.hpp"
  52 #include "oops/klass.inline.hpp"
  53 #include "oops/method.inline.hpp"
  54 #include "oops/methodData.hpp"
  55 #include "oops/objArrayKlass.hpp"
  56 #include "oops/objArrayOop.inline.hpp"
  57 #include "oops/oop.inline.hpp"
  58 #include "oops/symbol.hpp"
  59 #include "prims/jvmtiExport.hpp"
  60 #include "prims/methodHandles.hpp"
  61 #include "prims/nativeLookup.hpp"
  62 #include "runtime/atomicAccess.hpp"
  63 #include "runtime/continuation.hpp"
  64 #include "runtime/deoptimization.hpp"
  65 #include "runtime/fieldDescriptor.inline.hpp"
  66 #include "runtime/frame.inline.hpp"
  67 #include "runtime/handles.inline.hpp"
  68 #include "runtime/icache.hpp"
  69 #include "runtime/interfaceSupport.inline.hpp"
  70 #include "runtime/java.hpp"
  71 #include "runtime/javaCalls.hpp"
  72 #include "runtime/jfieldIDWorkaround.hpp"
  73 #include "runtime/osThread.hpp"
  74 #include "runtime/sharedRuntime.hpp"
  75 #include "runtime/stackWatermarkSet.hpp"
  76 #include "runtime/stubRoutines.hpp"
  77 #include "runtime/synchronizer.inline.hpp"
  78 #include "utilities/align.hpp"
  79 #include "utilities/checkedCast.hpp"
  80 #include "utilities/copy.hpp"
  81 #include "utilities/events.hpp"
  82 #include "utilities/globalDefinitions.hpp"
  83 #if INCLUDE_JFR
  84 #include "jfr/jfr.inline.hpp"
  85 #endif
  86 
  87 // Helper class to access current interpreter state
  88 class LastFrameAccessor : public StackObj {
  89   frame _last_frame;
  90 public:
  91   LastFrameAccessor(JavaThread* current) {
  92     assert(current == Thread::current(), "sanity");
  93     _last_frame = current->last_frame();
  94   }
  95   bool is_interpreted_frame() const              { return _last_frame.is_interpreted_frame(); }
  96   Method*   method() const                       { return _last_frame.interpreter_frame_method(); }
  97   address   bcp() const                          { return _last_frame.interpreter_frame_bcp(); }
  98   int       bci() const                          { return _last_frame.interpreter_frame_bci(); }
  99   address   mdp() const                          { return _last_frame.interpreter_frame_mdp(); }
 100 
 101   void      set_bcp(address bcp)                 { _last_frame.interpreter_frame_set_bcp(bcp); }
 102   void      set_mdp(address dp)                  { _last_frame.interpreter_frame_set_mdp(dp); }

 213 JRT_END
 214 
 215 
 216 //------------------------------------------------------------------------------------------------------------------------
 217 // Allocation
 218 
 219 JRT_ENTRY(void, InterpreterRuntime::_new(JavaThread* current, ConstantPool* pool, int index))
 220   Klass* k = pool->klass_at(index, CHECK);
 221   InstanceKlass* klass = InstanceKlass::cast(k);
 222 
 223   // Make sure we are not instantiating an abstract klass
 224   klass->check_valid_for_instantiation(true, CHECK);
 225 
 226   // Make sure klass is initialized
 227   klass->initialize(CHECK);
 228 
 229   oop obj = klass->allocate_instance(CHECK);
 230   current->set_vm_result_oop(obj);
 231 JRT_END
 232 
 233 JRT_ENTRY(void, InterpreterRuntime::read_flat_field(JavaThread* current, oopDesc* obj, ResolvedFieldEntry* entry))
 234   assert(oopDesc::is_oop(obj), "Sanity check");
 235   Handle obj_h(THREAD, obj);
 236 
 237   InstanceKlass* holder = InstanceKlass::cast(entry->field_holder());
 238   assert(entry->field_holder()->field_is_flat(entry->field_index()), "Sanity check");
 239 
 240   InlineLayoutInfo* layout_info = holder->inline_layout_info_adr(entry->field_index());
 241   InlineKlass* field_vklass = layout_info->klass();
 242 
 243 #ifdef ASSERT
 244   fieldDescriptor fd;
 245   bool found = holder->find_field_from_offset(entry->field_offset(), false, &fd);
 246   assert(found, "Field not found");
 247   assert(fd.is_flat(), "Field must be flat");
 248 #endif // ASSERT
 249 
 250   oop res = field_vklass->read_payload_from_addr(obj_h(), entry->field_offset(), layout_info->kind(), CHECK);
 251   current->set_vm_result_oop(res);
 252 JRT_END
 253 
 254 JRT_ENTRY(void, InterpreterRuntime::write_flat_field(JavaThread* current, oopDesc* obj, oopDesc* value, ResolvedFieldEntry* entry))
 255   assert(oopDesc::is_oop(obj), "Sanity check");
 256   Handle obj_h(THREAD, obj);
 257   assert(value == nullptr || oopDesc::is_oop(value), "Sanity check");
 258   Handle val_h(THREAD, value);
 259 
 260   InstanceKlass* holder = entry->field_holder();
 261   InlineLayoutInfo* li = holder->inline_layout_info_adr(entry->field_index());
 262   InlineKlass* vk = li->klass();
 263   vk->write_value_to_addr(val_h(), ((char*)(oopDesc*)obj_h()) + entry->field_offset(), li->kind(), true, CHECK);
 264 JRT_END
 265 
 266 JRT_ENTRY(void, InterpreterRuntime::newarray(JavaThread* current, BasicType type, jint size))
 267   oop obj = oopFactory::new_typeArray(type, size, CHECK);
 268   current->set_vm_result_oop(obj);
 269 JRT_END
 270 
 271 
 272 JRT_ENTRY(void, InterpreterRuntime::anewarray(JavaThread* current, ConstantPool* pool, int index, jint size))
 273   Klass*    klass = pool->klass_at(index, CHECK);
 274   arrayOop obj = oopFactory::new_objArray(klass, size, CHECK);
 275   current->set_vm_result_oop(obj);
 276 JRT_END
 277 
 278 JRT_ENTRY(void, InterpreterRuntime::flat_array_load(JavaThread* current, arrayOopDesc* array, int index))
 279   assert(array->is_flatArray(), "Must be");
 280   flatArrayOop farray = (flatArrayOop)array;
 281   oop res = farray->obj_at(index, CHECK);
 282   current->set_vm_result_oop(res);
 283 JRT_END
 284 
 285 JRT_ENTRY(void, InterpreterRuntime::flat_array_store(JavaThread* current, oopDesc* val, arrayOopDesc* array, int index))
 286   assert(array->is_flatArray(), "Must be");
 287   flatArrayOop farray = (flatArrayOop)array;
 288   farray->obj_at_put(index, val, CHECK);
 289 JRT_END
 290 
 291 JRT_ENTRY(void, InterpreterRuntime::multianewarray(JavaThread* current, jint* first_size_address))
 292   // We may want to pass in more arguments - could make this slightly faster
 293   LastFrameAccessor last_frame(current);
 294   ConstantPool* constants = last_frame.method()->constants();
 295   int i = last_frame.get_index_u2(Bytecodes::_multianewarray);
 296   Klass* klass = constants->klass_at(i, CHECK);
 297   int   nof_dims = last_frame.number_of_dimensions();
 298   assert(klass->is_klass(), "not a class");
 299   assert(nof_dims >= 1, "multianewarray rank must be nonzero");
 300 
 301   // We must create an array of jints to pass to multi_allocate.
 302   ResourceMark rm(current);
 303   const int small_dims = 10;
 304   jint dim_array[small_dims];
 305   jint *dims = &dim_array[0];
 306   if (nof_dims > small_dims) {
 307     dims = (jint*) NEW_RESOURCE_ARRAY(jint, nof_dims);
 308   }
 309   for (int index = 0; index < nof_dims; index++) {
 310     // offset from first_size_address is addressed as local[index]
 311     int n = Interpreter::local_offset_in_bytes(index)/jintSize;
 312     dims[index] = first_size_address[n];
 313   }
 314   oop obj = ArrayKlass::cast(klass)->multi_allocate(nof_dims, dims, CHECK);
 315   current->set_vm_result_oop(obj);
 316 JRT_END
 317 
 318 
 319 JRT_ENTRY(void, InterpreterRuntime::register_finalizer(JavaThread* current, oopDesc* obj))
 320   assert(oopDesc::is_oop(obj), "must be a valid oop");
 321   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
 322   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
 323 JRT_END
 324 
 325 JRT_ENTRY(jboolean, InterpreterRuntime::is_substitutable(JavaThread* current, oopDesc* aobj, oopDesc* bobj))
 326   assert(oopDesc::is_oop(aobj) && oopDesc::is_oop(bobj), "must be valid oops");
 327 
 328   Handle ha(THREAD, aobj);
 329   Handle hb(THREAD, bobj);
 330   JavaValue result(T_BOOLEAN);
 331   JavaCallArguments args;
 332   args.push_oop(ha);
 333   args.push_oop(hb);
 334   methodHandle method(current, UseAltSubstitutabilityMethod ?  Universe::is_substitutableAlt_method() : Universe::is_substitutable_method());
 335   method->method_holder()->initialize(CHECK_false); // Ensure class ValueObjectMethods is initialized
 336   JavaCalls::call(&result, method, &args, THREAD);
 337   if (HAS_PENDING_EXCEPTION) {
 338     // Something really bad happened because isSubstitutable() should not throw exceptions
 339     // If it is an error, just let it propagate
 340     // If it is an exception, wrap it into an InternalError
 341     if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
 342       Handle e(THREAD, PENDING_EXCEPTION);
 343       CLEAR_PENDING_EXCEPTION;
 344       THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in substitutability test", e, false);
 345     }
 346   }
 347   return result.get_jboolean();
 348 JRT_END
 349 
 350 // Quicken instance-of and check-cast bytecodes
 351 JRT_ENTRY(void, InterpreterRuntime::quicken_io_cc(JavaThread* current))
 352   // Force resolving; quicken the bytecode
 353   LastFrameAccessor last_frame(current);
 354   int which = last_frame.get_index_u2(Bytecodes::_checkcast);
 355   ConstantPool* cpool = last_frame.method()->constants();
 356   // We'd expect to assert that we're only here to quicken bytecodes, but in a multithreaded
 357   // program we might have seen an unquick'd bytecode in the interpreter but have another
 358   // thread quicken the bytecode before we get here.
 359   // assert( cpool->tag_at(which).is_unresolved_klass(), "should only come here to quicken bytecodes" );
 360   Klass* klass = cpool->klass_at(which, CHECK);
 361   current->set_vm_result_metadata(klass);
 362 JRT_END
 363 
 364 
 365 //------------------------------------------------------------------------------------------------------------------------
 366 // Exceptions
 367 
 368 void InterpreterRuntime::note_trap_inner(JavaThread* current, int reason,

 676 // and therefore we don't have the receiver object at our fingertips. (Though,
 677 // on some platforms the receiver still resides in a register...). Thus,
 678 // we have no choice but print an error message not containing the receiver
 679 // type.
 680 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorWithMethod(JavaThread* current,
 681                                                                         Method* missingMethod))
 682   ResourceMark rm(current);
 683   assert(missingMethod != nullptr, "sanity");
 684   methodHandle m(current, missingMethod);
 685   LinkResolver::throw_abstract_method_error(m, THREAD);
 686 JRT_END
 687 
 688 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorVerbose(JavaThread* current,
 689                                                                      Klass* recvKlass,
 690                                                                      Method* missingMethod))
 691   ResourceMark rm(current);
 692   methodHandle mh = methodHandle(current, missingMethod);
 693   LinkResolver::throw_abstract_method_error(mh, recvKlass, THREAD);
 694 JRT_END
 695 
 696 JRT_ENTRY(void, InterpreterRuntime::throw_InstantiationError(JavaThread* current))
 697   THROW(vmSymbols::java_lang_InstantiationError());
 698 JRT_END
 699 
 700 
 701 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* current))
 702   THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
 703 JRT_END
 704 
 705 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose(JavaThread* current,
 706                                                                               Klass* recvKlass,
 707                                                                               Klass* interfaceKlass))
 708   ResourceMark rm(current);
 709   char buf[1000];
 710   buf[0] = '\0';
 711   jio_snprintf(buf, sizeof(buf),
 712                "Class %s does not implement the requested interface %s",
 713                recvKlass ? recvKlass->external_name() : "nullptr",
 714                interfaceKlass ? interfaceKlass->external_name() : "nullptr");
 715   THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 716 JRT_END
 717 
 718 JRT_ENTRY(void, InterpreterRuntime::throw_NullPointerException(JavaThread* current))
 719   THROW(vmSymbols::java_lang_NullPointerException());

 759 
 760   // Resolution of put instructions to final instance fields with invalid updates (i.e.,
 761   // to final instance fields with updates originating from a method different than <init>)
 762   // is inhibited. A putfield instruction targeting an instance final field must throw
 763   // an IllegalAccessError if the instruction is not in an instance
 764   // initializer method <init>. If resolution were not inhibited, a putfield
 765   // in an initializer method could be resolved in the initializer. Subsequent
 766   // putfield instructions to the same field would then use cached information.
 767   // As a result, those instructions would not pass through the VM. That is,
 768   // checks in resolve_field_access() would not be executed for those instructions
 769   // and the required IllegalAccessError would not be thrown.
 770   //
 771   // Also, we need to delay resolving getstatic and putstatic instructions until the
 772   // class is initialized.  This is required so that access to the static
 773   // field will call the initialization function every time until the class
 774   // is completely initialized ala. in 2.17.5 in JVM Specification.
 775   InstanceKlass* klass = info.field_holder();
 776   bool uninitialized_static = is_static && !klass->is_initialized();
 777   bool has_initialized_final_update = info.field_holder()->major_version() >= 53 &&
 778                                       info.has_initialized_final_update();
 779   bool strict_static_final = info.is_strict() && info.is_static() && info.is_final();
 780   assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final");
 781 
 782   Bytecodes::Code get_code = (Bytecodes::Code)0;
 783   Bytecodes::Code put_code = (Bytecodes::Code)0;
 784   if (!uninitialized_static) {
 785     get_code = ((is_static) ? Bytecodes::_getstatic : Bytecodes::_getfield);
 786     if ((is_put && !has_initialized_final_update) || !info.access_flags().is_final()) {
 787       put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
 788     }
 789     assert(!info.is_strict_static_unset(), "after initialization, no unset flags");
 790   } else if (is_static && (info.is_strict_static_unset() || strict_static_final)) {
 791     // During <clinit>, closely track the state of strict statics.
 792     // 1. if we are reading an uninitialized strict static, throw
 793     // 2. if we are writing one, clear the "unset" flag
 794     //
 795     // Note: If we were handling an attempted write of a null to a
 796     // null-restricted strict static, we would NOT clear the "unset"
 797     // flag.
 798     assert(klass->is_being_initialized(), "else should have thrown");
 799     assert(klass->is_reentrant_initialization(THREAD),
 800       "<clinit> must be running in current thread");
 801     klass->notify_strict_static_access(info.index(), is_put, CHECK);
 802   }
 803 
 804   ResolvedFieldEntry* entry = pool->resolved_field_entry_at(field_index);
 805   entry->set_flags(info.access_flags().is_final(), info.access_flags().is_volatile(),
 806                    info.is_flat(), info.is_null_free_inline_type(),
 807                    info.has_null_marker());
 808 
 809   entry->fill_in(info.field_holder(), info.offset(),
 810                  checked_cast<u2>(info.index()), checked_cast<u1>(state),
 811                  static_cast<u1>(get_code), static_cast<u1>(put_code));
 812 }
 813 
 814 
 815 //------------------------------------------------------------------------------------------------------------------------
 816 // Synchronization
 817 //
 818 // The interpreter's synchronization code is factored out so that it can
 819 // be shared by method invocation and synchronized blocks.
 820 //%note synchronization_3
 821 
 822 //%note monitor_1
 823 JRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* current, BasicObjectLock* elem))
 824 #ifdef ASSERT
 825   current->last_frame().interpreter_frame_verify_monitor(elem);
 826 #endif
 827   Handle h_obj(current, elem->obj());
 828   assert(Universe::heap()->is_in_or_null(h_obj()),

 835 #endif
 836 JRT_END
 837 
 838 JRT_LEAF(void, InterpreterRuntime::monitorexit(BasicObjectLock* elem))
 839   oop obj = elem->obj();
 840   assert(Universe::heap()->is_in(obj), "must be an object");
 841   // The object could become unlocked through a JNI call, which we have no other checks for.
 842   // Give a fatal message if CheckJNICalls. Otherwise we ignore it.
 843   if (obj->is_unlocked()) {
 844     if (CheckJNICalls) {
 845       fatal("Object has been unlocked by JNI");
 846     }
 847     return;
 848   }
 849   ObjectSynchronizer::exit(obj, elem->lock(), JavaThread::current());
 850   // Free entry. If it is not cleared, the exception handling code will try to unlock the monitor
 851   // again at method exit or in the case of an exception.
 852   elem->set_obj(nullptr);
 853 JRT_END
 854 

 855 JRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* current))
 856   THROW(vmSymbols::java_lang_IllegalMonitorStateException());
 857 JRT_END
 858 

 859 JRT_ENTRY(void, InterpreterRuntime::new_illegal_monitor_state_exception(JavaThread* current))
 860   // Returns an illegal exception to install into the current thread. The
 861   // pending_exception flag is cleared so normal exception handling does not
 862   // trigger. Any current installed exception will be overwritten. This
 863   // method will be called during an exception unwind.
 864 
 865   assert(!HAS_PENDING_EXCEPTION, "no pending exception");
 866   Handle exception(current, current->vm_result_oop());
 867   assert(exception() != nullptr, "vm result should be set");
 868   current->set_vm_result_oop(nullptr); // clear vm result before continuing (may cause memory leaks and assert failures)
 869   exception = get_preinitialized_exception(vmClasses::IllegalMonitorStateException_klass(), CATCH);
 870   current->set_vm_result_oop(exception());
 871 JRT_END
 872 
 873 JRT_ENTRY(void, InterpreterRuntime::throw_identity_exception(JavaThread* current, oopDesc* obj))
 874   Klass* klass = cast_to_oop(obj)->klass();
 875   ResourceMark rm(THREAD);
 876   const char* desc = "Cannot synchronize on an instance of value class ";
 877   const char* className = klass->external_name();
 878   size_t msglen = strlen(desc) + strlen(className) + 1;
 879   char* message = NEW_RESOURCE_ARRAY(char, msglen);
 880   if (nullptr == message) {
 881     // Out of memory: can't create detailed error message
 882     THROW_MSG(vmSymbols::java_lang_IdentityException(), className);
 883   } else {
 884     jio_snprintf(message, msglen, "%s%s", desc, className);
 885     THROW_MSG(vmSymbols::java_lang_IdentityException(), message);
 886   }
 887 JRT_END
 888 
 889 //------------------------------------------------------------------------------------------------------------------------
 890 // Invokes
 891 
 892 JRT_ENTRY(Bytecodes::Code, InterpreterRuntime::get_original_bytecode_at(JavaThread* current, Method* method, address bcp))
 893   return method->orig_bytecode_at(method->bci_from(bcp));
 894 JRT_END
 895 
 896 JRT_ENTRY(void, InterpreterRuntime::set_original_bytecode_at(JavaThread* current, Method* method, address bcp, Bytecodes::Code new_code))
 897   method->set_orig_bytecode_at(method->bci_from(bcp), new_code);
 898 JRT_END
 899 
 900 JRT_ENTRY(void, InterpreterRuntime::_breakpoint(JavaThread* current, Method* method, address bcp))
 901   JvmtiExport::post_raw_breakpoint(current, method, bcp);
 902 JRT_END
 903 
 904 void InterpreterRuntime::resolve_invoke(JavaThread* current, Bytecodes::Code bytecode) {
 905   LastFrameAccessor last_frame(current);
 906   // extract receiver from the outgoing argument list if necessary
 907   Handle receiver(current, nullptr);

1278     JvmtiExport::at_single_stepping_point(current, last_frame.method(), last_frame.bcp());
1279   }
1280 JRT_END
1281 
1282 JRT_LEAF(void, InterpreterRuntime::at_unwind(JavaThread* current))
1283   assert(current == JavaThread::current(), "pre-condition");
1284   JFR_ONLY(Jfr::check_and_process_sample_request(current);)
1285   // This function is called by the interpreter when the return poll found a reason
1286   // to call the VM. The reason could be that we are returning into a not yet safe
1287   // to access frame. We handle that below.
1288   // Note that this path does not check for single stepping, because we do not want
1289   // to single step when unwinding frames for an exception being thrown. Instead,
1290   // such single stepping code will use the safepoint table, which will use the
1291   // InterpreterRuntime::at_safepoint callback.
1292   StackWatermarkSet::before_unwind(current);
1293 JRT_END
1294 
1295 JRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread* current, oopDesc* obj,
1296                                                       ResolvedFieldEntry *entry))
1297 
1298   assert(entry->is_valid(), "Invalid ResolvedFieldEntry");
1299   // check the access_flags for the field in the klass
1300 
1301   InstanceKlass* ik = entry->field_holder();
1302   int index = entry->field_index();
1303   if (!ik->field_status(index).is_access_watched()) return;
1304 
1305   bool is_static = (obj == nullptr);
1306   bool is_flat = entry->is_flat();
1307   HandleMark hm(current);
1308 
1309   Handle h_obj;
1310   if (!is_static) {
1311     // non-static field accessors have an object, but we need a handle
1312     h_obj = Handle(current, obj);
1313   }
1314   InstanceKlass* field_holder = entry->field_holder(); // HERE
1315   jfieldID fid = jfieldIDWorkaround::to_jfieldID(field_holder, entry->field_offset(), is_static, is_flat);
1316   LastFrameAccessor last_frame(current);
1317   JvmtiExport::post_field_access(current, last_frame.method(), last_frame.bcp(), field_holder, h_obj, fid);
1318 JRT_END
1319 
1320 JRT_ENTRY(void, InterpreterRuntime::post_field_modification(JavaThread* current, oopDesc* obj,
1321                                                             ResolvedFieldEntry *entry, jvalue *value))
1322 
1323   assert(entry->is_valid(), "Invalid ResolvedFieldEntry");
1324   InstanceKlass* ik = entry->field_holder();
1325 
1326   // check the access_flags for the field in the klass
1327   int index = entry->field_index();
1328   // bail out if field modifications are not watched
1329   if (!ik->field_status(index).is_modification_watched()) return;
1330 
1331   char sig_type = '\0';
1332 
1333   switch((TosState)entry->tos_state()) {
1334     case btos: sig_type = JVM_SIGNATURE_BYTE;    break;
1335     case ztos: sig_type = JVM_SIGNATURE_BOOLEAN; break;
1336     case ctos: sig_type = JVM_SIGNATURE_CHAR;    break;
1337     case stos: sig_type = JVM_SIGNATURE_SHORT;   break;
1338     case itos: sig_type = JVM_SIGNATURE_INT;     break;
1339     case ftos: sig_type = JVM_SIGNATURE_FLOAT;   break;
1340     case atos: sig_type = JVM_SIGNATURE_CLASS;   break;
1341     case ltos: sig_type = JVM_SIGNATURE_LONG;    break;
1342     case dtos: sig_type = JVM_SIGNATURE_DOUBLE;  break;
1343     default:  ShouldNotReachHere(); return;
1344   }
1345 
1346   bool is_static = (obj == nullptr);
1347   bool is_flat = entry->is_flat();
1348 
1349   HandleMark hm(current);
1350   jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, entry->field_offset(), is_static, is_flat);
1351   jvalue fvalue;
1352 #ifdef _LP64
1353   fvalue = *value;
1354 #else
1355   // Long/double values are stored unaligned and also noncontiguously with
1356   // tagged stacks.  We can't just do a simple assignment even in the non-
1357   // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1358   // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1359   // We assume that the two halves of longs/doubles are stored in interpreter
1360   // stack slots in platform-endian order.
1361   jlong_accessor u;
1362   jint* newval = (jint*)value;
1363   u.words[0] = newval[0];
1364   u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1365   fvalue.j = u.long_value;
1366 #endif // _LP64
1367 
1368   Handle h_obj;
1369   if (!is_static) {
1370     // non-static field accessors have an object, but we need a handle
< prev index next >