< 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/continuation.hpp"
  59 #include "runtime/deoptimization.hpp"
  60 #include "runtime/fieldDescriptor.inline.hpp"
  61 #include "runtime/frame.inline.hpp"
  62 #include "runtime/handles.inline.hpp"
  63 #include "runtime/icache.hpp"
  64 #include "runtime/interfaceSupport.inline.hpp"
  65 #include "runtime/java.hpp"
  66 #include "runtime/javaCalls.hpp"
  67 #include "runtime/jfieldIDWorkaround.hpp"
  68 #include "runtime/osThread.hpp"
  69 #include "runtime/sharedRuntime.hpp"
  70 #include "runtime/stackWatermarkSet.hpp"
  71 #include "runtime/stubRoutines.hpp"
  72 #include "runtime/synchronizer.hpp"
  73 #include "utilities/align.hpp"
  74 #include "utilities/checkedCast.hpp"
  75 #include "utilities/copy.hpp"
  76 #include "utilities/events.hpp"
  77 #include "utilities/exceptions.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_preemptable(CHECK_AND_CLEAR_PREEMPTED);
 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());
 643 JRT_END

 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->fill_in(info, checked_cast<u1>(state),
 715                  static_cast<u1>(get_code), static_cast<u1>(put_code));
 716 }
 717 
 718 
 719 //------------------------------------------------------------------------------------------------------------------------
 720 // Synchronization
 721 //
 722 // The interpreter's synchronization code is factored out so that it can
 723 // be shared by method invocation and synchronized blocks.
 724 //%note synchronization_3
 725 
 726 //%note monitor_1

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















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

1178   JFR_ONLY(Jfr::check_and_process_sample_request(current);)
1179   // This function is called by the interpreter when the return poll found a reason
1180   // to call the VM. The reason could be that we are returning into a not yet safe
1181   // to access frame. We handle that below.
1182   // Note that this path does not check for single stepping, because we do not want
1183   // to single step when unwinding frames for an exception being thrown. Instead,
1184   // such single stepping code will use the safepoint table, which will use the
1185   // InterpreterRuntime::at_safepoint callback.
1186   StackWatermarkSet::before_unwind(current);
1187 JRT_END
1188 
1189 JRT_ENTRY(void, InterpreterRuntime::post_field_access(JavaThread* current, oopDesc* obj,
1190                                                       ResolvedFieldEntry* entry))
1191 
1192   // check the access_flags for the field in the klass
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   // check the access_flags for the field in the klass
1215   InstanceKlass* ik = entry->field_holder();
1216   int index = entry->field_index();
1217   // bail out if field modifications are not watched
1218   if (!ik->field_status(index).is_modification_watched()) return;
1219 
1220   char sig_type = '\0';
1221 
1222   switch((TosState)entry->tos_state()) {
1223     case btos: sig_type = JVM_SIGNATURE_BYTE;    break;
1224     case ztos: sig_type = JVM_SIGNATURE_BOOLEAN; break;
1225     case ctos: sig_type = JVM_SIGNATURE_CHAR;    break;
1226     case stos: sig_type = JVM_SIGNATURE_SHORT;   break;
1227     case itos: sig_type = JVM_SIGNATURE_INT;     break;
1228     case ftos: sig_type = JVM_SIGNATURE_FLOAT;   break;
1229     case atos: sig_type = JVM_SIGNATURE_CLASS;   break;
1230     case ltos: sig_type = JVM_SIGNATURE_LONG;    break;
1231     case dtos: sig_type = JVM_SIGNATURE_DOUBLE;  break;
1232     default:  ShouldNotReachHere(); return;
1233   }

1234   bool is_static = (obj == nullptr);

1235 
1236   HandleMark hm(current);
1237   jfieldID fid = jfieldIDWorkaround::to_jfieldID(ik, entry->field_offset(), is_static);
1238   jvalue fvalue;
1239 #ifdef _LP64
1240   fvalue = *value;
1241 #else
1242   // Long/double values are stored unaligned and also noncontiguously with
1243   // tagged stacks.  We can't just do a simple assignment even in the non-
1244   // J/D cases because a C++ compiler is allowed to assume that a jvalue is
1245   // 8-byte aligned, and interpreter stack slots are only 4-byte aligned.
1246   // We assume that the two halves of longs/doubles are stored in interpreter
1247   // stack slots in platform-endian order.
1248   jlong_accessor u;
1249   jint* newval = (jint*)value;
1250   u.words[0] = newval[0];
1251   u.words[1] = newval[Interpreter::stackElementWords]; // skip if tag
1252   fvalue.j = u.long_value;
1253 #endif // _LP64
1254 
1255   Handle h_obj;
1256   if (!is_static) {
1257     // 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/oopsHierarchy.hpp"
  59 #include "oops/symbol.hpp"
  60 #include "oops/valuePayload.inline.hpp"
  61 #include "prims/jvmtiExport.hpp"
  62 #include "prims/methodHandles.hpp"
  63 #include "prims/nativeLookup.hpp"
  64 #include "runtime/continuation.hpp"
  65 #include "runtime/deoptimization.hpp"
  66 #include "runtime/fieldDescriptor.inline.hpp"
  67 #include "runtime/frame.inline.hpp"
  68 #include "runtime/handles.inline.hpp"
  69 #include "runtime/icache.hpp"
  70 #include "runtime/interfaceSupport.inline.hpp"
  71 #include "runtime/java.hpp"
  72 #include "runtime/javaCalls.hpp"
  73 #include "runtime/jfieldIDWorkaround.hpp"
  74 #include "runtime/osThread.hpp"
  75 #include "runtime/sharedRuntime.hpp"
  76 #include "runtime/stackWatermarkSet.hpp"
  77 #include "runtime/stubRoutines.hpp"
  78 #include "runtime/synchronizer.hpp"
  79 #include "utilities/align.hpp"
  80 #include "utilities/checkedCast.hpp"
  81 #include "utilities/copy.hpp"
  82 #include "utilities/events.hpp"
  83 #include "utilities/exceptions.hpp"
  84 #include "utilities/globalDefinitions.hpp"
  85 #if INCLUDE_JFR
  86 #include "jfr/jfr.inline.hpp"
  87 #endif
  88 
  89 // Helper class to access current interpreter state
  90 class LastFrameAccessor : public StackObj {
  91   frame _last_frame;
  92 public:
  93   LastFrameAccessor(JavaThread* current) {
  94     assert(current == Thread::current(), "sanity");
  95     _last_frame = current->last_frame();
  96   }
  97   bool is_interpreted_frame() const              { return _last_frame.is_interpreted_frame(); }
  98   Method*   method() const                       { return _last_frame.interpreter_frame_method(); }
  99   address   bcp() const                          { return _last_frame.interpreter_frame_bcp(); }
 100   int       bci() const                          { return _last_frame.interpreter_frame_bci(); }
 101   address   mdp() const                          { return _last_frame.interpreter_frame_mdp(); }
 102 
 103   void      set_bcp(address bcp)                 { _last_frame.interpreter_frame_set_bcp(bcp); }
 104   void      set_mdp(address dp)                  { _last_frame.interpreter_frame_set_mdp(dp); }

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

 668 // and therefore we don't have the receiver object at our fingertips. (Though,
 669 // on some platforms the receiver still resides in a register...). Thus,
 670 // we have no choice but print an error message not containing the receiver
 671 // type.
 672 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorWithMethod(JavaThread* current,
 673                                                                         Method* missingMethod))
 674   ResourceMark rm(current);
 675   assert(missingMethod != nullptr, "sanity");
 676   methodHandle m(current, missingMethod);
 677   LinkResolver::throw_abstract_method_error(m, THREAD);
 678 JRT_END
 679 
 680 JRT_ENTRY(void, InterpreterRuntime::throw_AbstractMethodErrorVerbose(JavaThread* current,
 681                                                                      Klass* recvKlass,
 682                                                                      Method* missingMethod))
 683   ResourceMark rm(current);
 684   methodHandle mh = methodHandle(current, missingMethod);
 685   LinkResolver::throw_abstract_method_error(mh, recvKlass, THREAD);
 686 JRT_END
 687 

 688 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeError(JavaThread* current))
 689   THROW(vmSymbols::java_lang_IncompatibleClassChangeError());
 690 JRT_END
 691 
 692 JRT_ENTRY(void, InterpreterRuntime::throw_IncompatibleClassChangeErrorVerbose(JavaThread* current,
 693                                                                               Klass* recvKlass,
 694                                                                               Klass* interfaceKlass))
 695   ResourceMark rm(current);
 696   char buf[1000];
 697   buf[0] = '\0';
 698   jio_snprintf(buf, sizeof(buf),
 699                "Class %s does not implement the requested interface %s",
 700                recvKlass ? recvKlass->external_name() : "nullptr",
 701                interfaceKlass ? interfaceKlass->external_name() : "nullptr");
 702   THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 703 JRT_END
 704 
 705 JRT_ENTRY(void, InterpreterRuntime::throw_NullPointerException(JavaThread* current))
 706   THROW(vmSymbols::java_lang_NullPointerException());
 707 JRT_END

 746 
 747   // Resolution of put instructions to final instance fields with invalid updates (i.e.,
 748   // to final instance fields with updates originating from a method different than <init>)
 749   // is inhibited. A putfield instruction targeting an instance final field must throw
 750   // an IllegalAccessError if the instruction is not in an instance
 751   // initializer method <init>. If resolution were not inhibited, a putfield
 752   // in an initializer method could be resolved in the initializer. Subsequent
 753   // putfield instructions to the same field would then use cached information.
 754   // As a result, those instructions would not pass through the VM. That is,
 755   // checks in resolve_field_access() would not be executed for those instructions
 756   // and the required IllegalAccessError would not be thrown.
 757   //
 758   // Also, we need to delay resolving getstatic and putstatic instructions until the
 759   // class is initialized.  This is required so that access to the static
 760   // field will call the initialization function every time until the class
 761   // is completely initialized ala. in 2.17.5 in JVM Specification.
 762   InstanceKlass* klass = info.field_holder();
 763   bool uninitialized_static = is_static && !klass->is_initialized();
 764   bool has_initialized_final_update = info.field_holder()->major_version() >= 53 &&
 765                                       info.has_initialized_final_update();
 766   bool strict_static_final = info.is_strict() && info.is_static() && info.is_final();
 767   assert(!(has_initialized_final_update && !info.access_flags().is_final()), "Fields with initialized final updates must be final");
 768 
 769   Bytecodes::Code get_code = (Bytecodes::Code)0;
 770   Bytecodes::Code put_code = (Bytecodes::Code)0;
 771   if (uninitialized_static && (info.is_strict_static_unset() || strict_static_final)) {
 772     // During <clinit>, closely track the state of strict statics.
 773     // 1. if we are reading an uninitialized strict static, throw
 774     // 2. if we are writing one, clear the "unset" flag
 775     //
 776     // Note: If we were handling an attempted write of a null to a
 777     // null-restricted strict static, we would NOT clear the "unset"
 778     // flag.
 779     assert(klass->is_being_initialized(), "else should have thrown");
 780     assert(klass->is_reentrant_initialization(THREAD),
 781       "<clinit> must be running in current thread");
 782     klass->notify_strict_static_access(info.index(), is_put, CHECK);
 783     assert(!info.is_strict_static_unset(), "after initialization, no unset flags");
 784   } else if (!uninitialized_static || VM_Version::supports_fast_class_init_checks()) {
 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   }
 790 
 791   ResolvedFieldEntry* entry = pool->resolved_field_entry_at(field_index);
 792   entry->fill_in(info, checked_cast<u1>(state),
 793                  static_cast<u1>(get_code), static_cast<u1>(put_code));
 794 }
 795 
 796 
 797 //------------------------------------------------------------------------------------------------------------------------
 798 // Synchronization
 799 //
 800 // The interpreter's synchronization code is factored out so that it can
 801 // be shared by method invocation and synchronized blocks.
 802 //%note synchronization_3
 803 
 804 //%note monitor_1

 817 #endif
 818 JRT_END
 819 
 820 JRT_LEAF(void, InterpreterRuntime::monitorexit(BasicObjectLock* elem))
 821   oop obj = elem->obj();
 822   assert(Universe::heap()->is_in(obj), "must be an object");
 823   // The object could become unlocked through a JNI call, which we have no other checks for.
 824   // Give a fatal message if CheckJNICalls. Otherwise we ignore it.
 825   if (obj->is_unlocked()) {
 826     if (CheckJNICalls) {
 827       fatal("Object has been unlocked by JNI");
 828     }
 829     return;
 830   }
 831   ObjectSynchronizer::exit(obj, elem->lock(), JavaThread::current());
 832   // Free entry. If it is not cleared, the exception handling code will try to unlock the monitor
 833   // again at method exit or in the case of an exception.
 834   elem->set_obj(nullptr);
 835 JRT_END
 836 

 837 JRT_ENTRY(void, InterpreterRuntime::throw_illegal_monitor_state_exception(JavaThread* current))
 838   THROW(vmSymbols::java_lang_IllegalMonitorStateException());
 839 JRT_END
 840 

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

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