src/share/vm/prims/jvm.cpp

Wed, 25 Mar 2009 13:09:28 -0400

author
acorn
date
Wed, 25 Mar 2009 13:09:28 -0400
changeset 1092
715dceaa89b7
parent 1014
0fbdb4381b99
child 1111
d3676b4cb78c
permissions
-rw-r--r--

6603316: Improve instrumentation for classes loaded at startup
Reviewed-by: xlu, mchung

     1 /*
     2  * Copyright 1997-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 #include "incls/_precompiled.incl"
    26 #include "incls/_jvm.cpp.incl"
    27 #include <errno.h>
    29 /*
    30   NOTE about use of any ctor or function call that can trigger a safepoint/GC:
    31   such ctors and calls MUST NOT come between an oop declaration/init and its
    32   usage because if objects are move this may cause various memory stomps, bus
    33   errors and segfaults. Here is a cookbook for causing so called "naked oop
    34   failures":
    36       JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields<etc> {
    37           JVMWrapper("JVM_GetClassDeclaredFields");
    39           // Object address to be held directly in mirror & not visible to GC
    40           oop mirror = JNIHandles::resolve_non_null(ofClass);
    42           // If this ctor can hit a safepoint, moving objects around, then
    43           ComplexConstructor foo;
    45           // Boom! mirror may point to JUNK instead of the intended object
    46           (some dereference of mirror)
    48           // Here's another call that may block for GC, making mirror stale
    49           MutexLocker ml(some_lock);
    51           // And here's an initializer that can result in a stale oop
    52           // all in one step.
    53           oop o = call_that_can_throw_exception(TRAPS);
    56   The solution is to keep the oop declaration BELOW the ctor or function
    57   call that might cause a GC, do another resolve to reassign the oop, or
    58   consider use of a Handle instead of an oop so there is immunity from object
    59   motion. But note that the "QUICK" entries below do not have a handlemark
    60   and thus can only support use of handles passed in.
    61 */
    63 static void trace_class_resolution_impl(klassOop to_class, TRAPS) {
    64   ResourceMark rm;
    65   int line_number = -1;
    66   const char * source_file = NULL;
    67   const char * trace = "explicit";
    68   klassOop caller = NULL;
    69   JavaThread* jthread = JavaThread::current();
    70   if (jthread->has_last_Java_frame()) {
    71     vframeStream vfst(jthread);
    73     // scan up the stack skipping ClassLoader, AccessController and PrivilegedAction frames
    74     symbolHandle access_controller = oopFactory::new_symbol_handle("java/security/AccessController", CHECK);
    75     klassOop access_controller_klass = SystemDictionary::resolve_or_fail(access_controller, false, CHECK);
    76     symbolHandle privileged_action = oopFactory::new_symbol_handle("java/security/PrivilegedAction", CHECK);
    77     klassOop privileged_action_klass = SystemDictionary::resolve_or_fail(privileged_action, false, CHECK);
    79     methodOop last_caller = NULL;
    81     while (!vfst.at_end()) {
    82       methodOop m = vfst.method();
    83       if (!vfst.method()->method_holder()->klass_part()->is_subclass_of(SystemDictionary::classloader_klass())&&
    84           !vfst.method()->method_holder()->klass_part()->is_subclass_of(access_controller_klass) &&
    85           !vfst.method()->method_holder()->klass_part()->is_subclass_of(privileged_action_klass)) {
    86         break;
    87       }
    88       last_caller = m;
    89       vfst.next();
    90     }
    91     // if this is called from Class.forName0 and that is called from Class.forName,
    92     // then print the caller of Class.forName.  If this is Class.loadClass, then print
    93     // that caller, otherwise keep quiet since this should be picked up elsewhere.
    94     bool found_it = false;
    95     if (!vfst.at_end() &&
    96         instanceKlass::cast(vfst.method()->method_holder())->name() == vmSymbols::java_lang_Class() &&
    97         vfst.method()->name() == vmSymbols::forName0_name()) {
    98       vfst.next();
    99       if (!vfst.at_end() &&
   100           instanceKlass::cast(vfst.method()->method_holder())->name() == vmSymbols::java_lang_Class() &&
   101           vfst.method()->name() == vmSymbols::forName_name()) {
   102         vfst.next();
   103         found_it = true;
   104       }
   105     } else if (last_caller != NULL &&
   106                instanceKlass::cast(last_caller->method_holder())->name() ==
   107                vmSymbols::java_lang_ClassLoader() &&
   108                (last_caller->name() == vmSymbols::loadClassInternal_name() ||
   109                 last_caller->name() == vmSymbols::loadClass_name())) {
   110       found_it = true;
   111     } else if (!vfst.at_end()) {
   112       if (vfst.method()->is_native()) {
   113         // JNI call
   114         found_it = true;
   115       }
   116     }
   117     if (found_it && !vfst.at_end()) {
   118       // found the caller
   119       caller = vfst.method()->method_holder();
   120       line_number = vfst.method()->line_number_from_bci(vfst.bci());
   121       if (line_number == -1) {
   122         // show method name if it's a native method
   123         trace = vfst.method()->name_and_sig_as_C_string();
   124       }
   125       symbolOop s = instanceKlass::cast(caller)->source_file_name();
   126       if (s != NULL) {
   127         source_file = s->as_C_string();
   128       }
   129     }
   130   }
   131   if (caller != NULL) {
   132     if (to_class != caller) {
   133       const char * from = Klass::cast(caller)->external_name();
   134       const char * to = Klass::cast(to_class)->external_name();
   135       // print in a single call to reduce interleaving between threads
   136       if (source_file != NULL) {
   137         tty->print("RESOLVE %s %s %s:%d (%s)\n", from, to, source_file, line_number, trace);
   138       } else {
   139         tty->print("RESOLVE %s %s (%s)\n", from, to, trace);
   140       }
   141     }
   142   }
   143 }
   145 void trace_class_resolution(klassOop to_class) {
   146   EXCEPTION_MARK;
   147   trace_class_resolution_impl(to_class, THREAD);
   148   if (HAS_PENDING_EXCEPTION) {
   149     CLEAR_PENDING_EXCEPTION;
   150   }
   151 }
   153 // Wrapper to trace JVM functions
   155 #ifdef ASSERT
   156   class JVMTraceWrapper : public StackObj {
   157    public:
   158     JVMTraceWrapper(const char* format, ...) {
   159       if (TraceJVMCalls) {
   160         va_list ap;
   161         va_start(ap, format);
   162         tty->print("JVM ");
   163         tty->vprint_cr(format, ap);
   164         va_end(ap);
   165       }
   166     }
   167   };
   169   Histogram* JVMHistogram;
   170   volatile jint JVMHistogram_lock = 0;
   172   class JVMHistogramElement : public HistogramElement {
   173     public:
   174      JVMHistogramElement(const char* name);
   175   };
   177   JVMHistogramElement::JVMHistogramElement(const char* elementName) {
   178     _name = elementName;
   179     uintx count = 0;
   181     while (Atomic::cmpxchg(1, &JVMHistogram_lock, 0) != 0) {
   182       while (OrderAccess::load_acquire(&JVMHistogram_lock) != 0) {
   183         count +=1;
   184         if ( (WarnOnStalledSpinLock > 0)
   185           && (count % WarnOnStalledSpinLock == 0)) {
   186           warning("JVMHistogram_lock seems to be stalled");
   187         }
   188       }
   189      }
   191     if(JVMHistogram == NULL)
   192       JVMHistogram = new Histogram("JVM Call Counts",100);
   194     JVMHistogram->add_element(this);
   195     Atomic::dec(&JVMHistogram_lock);
   196   }
   198   #define JVMCountWrapper(arg) \
   199       static JVMHistogramElement* e = new JVMHistogramElement(arg); \
   200       if (e != NULL) e->increment_count();  // Due to bug in VC++, we need a NULL check here eventhough it should never happen!
   202   #define JVMWrapper(arg1)                    JVMCountWrapper(arg1); JVMTraceWrapper(arg1)
   203   #define JVMWrapper2(arg1, arg2)             JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2)
   204   #define JVMWrapper3(arg1, arg2, arg3)       JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2, arg3)
   205   #define JVMWrapper4(arg1, arg2, arg3, arg4) JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2, arg3, arg4)
   206 #else
   207   #define JVMWrapper(arg1)
   208   #define JVMWrapper2(arg1, arg2)
   209   #define JVMWrapper3(arg1, arg2, arg3)
   210   #define JVMWrapper4(arg1, arg2, arg3, arg4)
   211 #endif
   214 // Interface version /////////////////////////////////////////////////////////////////////
   217 JVM_LEAF(jint, JVM_GetInterfaceVersion())
   218   return JVM_INTERFACE_VERSION;
   219 JVM_END
   222 // java.lang.System //////////////////////////////////////////////////////////////////////
   225 JVM_LEAF(jlong, JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored))
   226   JVMWrapper("JVM_CurrentTimeMillis");
   227   return os::javaTimeMillis();
   228 JVM_END
   230 JVM_LEAF(jlong, JVM_NanoTime(JNIEnv *env, jclass ignored))
   231   JVMWrapper("JVM_NanoTime");
   232   return os::javaTimeNanos();
   233 JVM_END
   236 JVM_ENTRY(void, JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos,
   237                                jobject dst, jint dst_pos, jint length))
   238   JVMWrapper("JVM_ArrayCopy");
   239   // Check if we have null pointers
   240   if (src == NULL || dst == NULL) {
   241     THROW(vmSymbols::java_lang_NullPointerException());
   242   }
   243   arrayOop s = arrayOop(JNIHandles::resolve_non_null(src));
   244   arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst));
   245   assert(s->is_oop(), "JVM_ArrayCopy: src not an oop");
   246   assert(d->is_oop(), "JVM_ArrayCopy: dst not an oop");
   247   // Do copy
   248   Klass::cast(s->klass())->copy_array(s, src_pos, d, dst_pos, length, thread);
   249 JVM_END
   252 static void set_property(Handle props, const char* key, const char* value, TRAPS) {
   253   JavaValue r(T_OBJECT);
   254   // public synchronized Object put(Object key, Object value);
   255   HandleMark hm(THREAD);
   256   Handle key_str    = java_lang_String::create_from_platform_dependent_str(key, CHECK);
   257   Handle value_str  = java_lang_String::create_from_platform_dependent_str((value != NULL ? value : ""), CHECK);
   258   JavaCalls::call_virtual(&r,
   259                           props,
   260                           KlassHandle(THREAD, SystemDictionary::properties_klass()),
   261                           vmSymbolHandles::put_name(),
   262                           vmSymbolHandles::object_object_object_signature(),
   263                           key_str,
   264                           value_str,
   265                           THREAD);
   266 }
   269 #define PUTPROP(props, name, value) set_property((props), (name), (value), CHECK_(properties));
   272 JVM_ENTRY(jobject, JVM_InitProperties(JNIEnv *env, jobject properties))
   273   JVMWrapper("JVM_InitProperties");
   274   ResourceMark rm;
   276   Handle props(THREAD, JNIHandles::resolve_non_null(properties));
   278   // System property list includes both user set via -D option and
   279   // jvm system specific properties.
   280   for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
   281     PUTPROP(props, p->key(), p->value());
   282   }
   284   // Convert the -XX:MaxDirectMemorySize= command line flag
   285   // to the sun.nio.MaxDirectMemorySize property.
   286   // Do this after setting user properties to prevent people
   287   // from setting the value with a -D option, as requested.
   288   {
   289     char as_chars[256];
   290     jio_snprintf(as_chars, sizeof(as_chars), INTX_FORMAT, MaxDirectMemorySize);
   291     PUTPROP(props, "sun.nio.MaxDirectMemorySize", as_chars);
   292   }
   294   // JVM monitoring and management support
   295   // Add the sun.management.compiler property for the compiler's name
   296   {
   297 #undef CSIZE
   298 #if defined(_LP64) || defined(_WIN64)
   299   #define CSIZE "64-Bit "
   300 #else
   301   #define CSIZE
   302 #endif // 64bit
   304 #ifdef TIERED
   305     const char* compiler_name = "HotSpot " CSIZE "Tiered Compilers";
   306 #else
   307 #if defined(COMPILER1)
   308     const char* compiler_name = "HotSpot " CSIZE "Client Compiler";
   309 #elif defined(COMPILER2)
   310     const char* compiler_name = "HotSpot " CSIZE "Server Compiler";
   311 #else
   312     const char* compiler_name = "";
   313 #endif // compilers
   314 #endif // TIERED
   316     if (*compiler_name != '\0' &&
   317         (Arguments::mode() != Arguments::_int)) {
   318       PUTPROP(props, "sun.management.compiler", compiler_name);
   319     }
   320   }
   322   return properties;
   323 JVM_END
   326 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
   328 extern volatile jint vm_created;
   330 JVM_ENTRY_NO_ENV(void, JVM_Exit(jint code))
   331   if (vm_created != 0 && (code == 0)) {
   332     // The VM is about to exit. We call back into Java to check whether finalizers should be run
   333     Universe::run_finalizers_on_exit();
   334   }
   335   before_exit(thread);
   336   vm_exit(code);
   337 JVM_END
   340 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
   341   before_exit(thread);
   342   vm_exit(code);
   343 JVM_END
   346 JVM_LEAF(void, JVM_OnExit(void (*func)(void)))
   347   register_on_exit_function(func);
   348 JVM_END
   351 JVM_ENTRY_NO_ENV(void, JVM_GC(void))
   352   JVMWrapper("JVM_GC");
   353   if (!DisableExplicitGC) {
   354     Universe::heap()->collect(GCCause::_java_lang_system_gc);
   355   }
   356 JVM_END
   359 JVM_LEAF(jlong, JVM_MaxObjectInspectionAge(void))
   360   JVMWrapper("JVM_MaxObjectInspectionAge");
   361   return Universe::heap()->millis_since_last_gc();
   362 JVM_END
   365 JVM_LEAF(void, JVM_TraceInstructions(jboolean on))
   366   if (PrintJVMWarnings) warning("JVM_TraceInstructions not supported");
   367 JVM_END
   370 JVM_LEAF(void, JVM_TraceMethodCalls(jboolean on))
   371   if (PrintJVMWarnings) warning("JVM_TraceMethodCalls not supported");
   372 JVM_END
   374 static inline jlong convert_size_t_to_jlong(size_t val) {
   375   // In the 64-bit vm, a size_t can overflow a jlong (which is signed).
   376   NOT_LP64 (return (jlong)val;)
   377   LP64_ONLY(return (jlong)MIN2(val, (size_t)max_jlong);)
   378 }
   380 JVM_ENTRY_NO_ENV(jlong, JVM_TotalMemory(void))
   381   JVMWrapper("JVM_TotalMemory");
   382   size_t n = Universe::heap()->capacity();
   383   return convert_size_t_to_jlong(n);
   384 JVM_END
   387 JVM_ENTRY_NO_ENV(jlong, JVM_FreeMemory(void))
   388   JVMWrapper("JVM_FreeMemory");
   389   CollectedHeap* ch = Universe::heap();
   390   size_t n;
   391   {
   392      MutexLocker x(Heap_lock);
   393      n = ch->capacity() - ch->used();
   394   }
   395   return convert_size_t_to_jlong(n);
   396 JVM_END
   399 JVM_ENTRY_NO_ENV(jlong, JVM_MaxMemory(void))
   400   JVMWrapper("JVM_MaxMemory");
   401   size_t n = Universe::heap()->max_capacity();
   402   return convert_size_t_to_jlong(n);
   403 JVM_END
   406 JVM_ENTRY_NO_ENV(jint, JVM_ActiveProcessorCount(void))
   407   JVMWrapper("JVM_ActiveProcessorCount");
   408   return os::active_processor_count();
   409 JVM_END
   413 // java.lang.Throwable //////////////////////////////////////////////////////
   416 JVM_ENTRY(void, JVM_FillInStackTrace(JNIEnv *env, jobject receiver))
   417   JVMWrapper("JVM_FillInStackTrace");
   418   Handle exception(thread, JNIHandles::resolve_non_null(receiver));
   419   java_lang_Throwable::fill_in_stack_trace(exception);
   420 JVM_END
   423 JVM_ENTRY(void, JVM_PrintStackTrace(JNIEnv *env, jobject receiver, jobject printable))
   424   JVMWrapper("JVM_PrintStackTrace");
   425   // Note: This is no longer used in Merlin, but we still support it for compatibility.
   426   oop exception = JNIHandles::resolve_non_null(receiver);
   427   oop stream    = JNIHandles::resolve_non_null(printable);
   428   java_lang_Throwable::print_stack_trace(exception, stream);
   429 JVM_END
   432 JVM_ENTRY(jint, JVM_GetStackTraceDepth(JNIEnv *env, jobject throwable))
   433   JVMWrapper("JVM_GetStackTraceDepth");
   434   oop exception = JNIHandles::resolve(throwable);
   435   return java_lang_Throwable::get_stack_trace_depth(exception, THREAD);
   436 JVM_END
   439 JVM_ENTRY(jobject, JVM_GetStackTraceElement(JNIEnv *env, jobject throwable, jint index))
   440   JVMWrapper("JVM_GetStackTraceElement");
   441   JvmtiVMObjectAllocEventCollector oam; // This ctor (throughout this module) may trigger a safepoint/GC
   442   oop exception = JNIHandles::resolve(throwable);
   443   oop element = java_lang_Throwable::get_stack_trace_element(exception, index, CHECK_NULL);
   444   return JNIHandles::make_local(env, element);
   445 JVM_END
   448 // java.lang.Object ///////////////////////////////////////////////
   451 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
   452   JVMWrapper("JVM_IHashCode");
   453   // as implemented in the classic virtual machine; return 0 if object is NULL
   454   return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ;
   455 JVM_END
   458 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
   459   JVMWrapper("JVM_MonitorWait");
   460   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
   461   assert(obj->is_instance() || obj->is_array(), "JVM_MonitorWait must apply to an object");
   462   JavaThreadInObjectWaitState jtiows(thread, ms != 0);
   463   if (JvmtiExport::should_post_monitor_wait()) {
   464     JvmtiExport::post_monitor_wait((JavaThread *)THREAD, (oop)obj(), ms);
   465   }
   466   ObjectSynchronizer::wait(obj, ms, CHECK);
   467 JVM_END
   470 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
   471   JVMWrapper("JVM_MonitorNotify");
   472   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
   473   assert(obj->is_instance() || obj->is_array(), "JVM_MonitorNotify must apply to an object");
   474   ObjectSynchronizer::notify(obj, CHECK);
   475 JVM_END
   478 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
   479   JVMWrapper("JVM_MonitorNotifyAll");
   480   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
   481   assert(obj->is_instance() || obj->is_array(), "JVM_MonitorNotifyAll must apply to an object");
   482   ObjectSynchronizer::notifyall(obj, CHECK);
   483 JVM_END
   486 JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
   487   JVMWrapper("JVM_Clone");
   488   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
   489   const KlassHandle klass (THREAD, obj->klass());
   490   JvmtiVMObjectAllocEventCollector oam;
   492 #ifdef ASSERT
   493   // Just checking that the cloneable flag is set correct
   494   if (obj->is_javaArray()) {
   495     guarantee(klass->is_cloneable(), "all arrays are cloneable");
   496   } else {
   497     guarantee(obj->is_instance(), "should be instanceOop");
   498     bool cloneable = klass->is_subtype_of(SystemDictionary::cloneable_klass());
   499     guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
   500   }
   501 #endif
   503   // Check if class of obj supports the Cloneable interface.
   504   // All arrays are considered to be cloneable (See JLS 20.1.5)
   505   if (!klass->is_cloneable()) {
   506     ResourceMark rm(THREAD);
   507     THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
   508   }
   510   // Make shallow object copy
   511   const int size = obj->size();
   512   oop new_obj = NULL;
   513   if (obj->is_javaArray()) {
   514     const int length = ((arrayOop)obj())->length();
   515     new_obj = CollectedHeap::array_allocate(klass, size, length, CHECK_NULL);
   516   } else {
   517     new_obj = CollectedHeap::obj_allocate(klass, size, CHECK_NULL);
   518   }
   519   // 4839641 (4840070): We must do an oop-atomic copy, because if another thread
   520   // is modifying a reference field in the clonee, a non-oop-atomic copy might
   521   // be suspended in the middle of copying the pointer and end up with parts
   522   // of two different pointers in the field.  Subsequent dereferences will crash.
   523   // 4846409: an oop-copy of objects with long or double fields or arrays of same
   524   // won't copy the longs/doubles atomically in 32-bit vm's, so we copy jlongs instead
   525   // of oops.  We know objects are aligned on a minimum of an jlong boundary.
   526   // The same is true of StubRoutines::object_copy and the various oop_copy
   527   // variants, and of the code generated by the inline_native_clone intrinsic.
   528   assert(MinObjAlignmentInBytes >= BytesPerLong, "objects misaligned");
   529   Copy::conjoint_jlongs_atomic((jlong*)obj(), (jlong*)new_obj,
   530                                (size_t)align_object_size(size) / HeapWordsPerLong);
   531   // Clear the header
   532   new_obj->init_mark();
   534   // Store check (mark entire object and let gc sort it out)
   535   BarrierSet* bs = Universe::heap()->barrier_set();
   536   assert(bs->has_write_region_opt(), "Barrier set does not have write_region");
   537   bs->write_region(MemRegion((HeapWord*)new_obj, size));
   539   // Caution: this involves a java upcall, so the clone should be
   540   // "gc-robust" by this stage.
   541   if (klass->has_finalizer()) {
   542     assert(obj->is_instance(), "should be instanceOop");
   543     new_obj = instanceKlass::register_finalizer(instanceOop(new_obj), CHECK_NULL);
   544   }
   546   return JNIHandles::make_local(env, oop(new_obj));
   547 JVM_END
   549 // java.lang.Compiler ////////////////////////////////////////////////////
   551 // The initial cuts of the HotSpot VM will not support JITs, and all existing
   552 // JITs would need extensive changes to work with HotSpot.  The JIT-related JVM
   553 // functions are all silently ignored unless JVM warnings are printed.
   555 JVM_LEAF(void, JVM_InitializeCompiler (JNIEnv *env, jclass compCls))
   556   if (PrintJVMWarnings) warning("JVM_InitializeCompiler not supported");
   557 JVM_END
   560 JVM_LEAF(jboolean, JVM_IsSilentCompiler(JNIEnv *env, jclass compCls))
   561   if (PrintJVMWarnings) warning("JVM_IsSilentCompiler not supported");
   562   return JNI_FALSE;
   563 JVM_END
   566 JVM_LEAF(jboolean, JVM_CompileClass(JNIEnv *env, jclass compCls, jclass cls))
   567   if (PrintJVMWarnings) warning("JVM_CompileClass not supported");
   568   return JNI_FALSE;
   569 JVM_END
   572 JVM_LEAF(jboolean, JVM_CompileClasses(JNIEnv *env, jclass cls, jstring jname))
   573   if (PrintJVMWarnings) warning("JVM_CompileClasses not supported");
   574   return JNI_FALSE;
   575 JVM_END
   578 JVM_LEAF(jobject, JVM_CompilerCommand(JNIEnv *env, jclass compCls, jobject arg))
   579   if (PrintJVMWarnings) warning("JVM_CompilerCommand not supported");
   580   return NULL;
   581 JVM_END
   584 JVM_LEAF(void, JVM_EnableCompiler(JNIEnv *env, jclass compCls))
   585   if (PrintJVMWarnings) warning("JVM_EnableCompiler not supported");
   586 JVM_END
   589 JVM_LEAF(void, JVM_DisableCompiler(JNIEnv *env, jclass compCls))
   590   if (PrintJVMWarnings) warning("JVM_DisableCompiler not supported");
   591 JVM_END
   595 // Error message support //////////////////////////////////////////////////////
   597 JVM_LEAF(jint, JVM_GetLastErrorString(char *buf, int len))
   598   JVMWrapper("JVM_GetLastErrorString");
   599   return hpi::lasterror(buf, len);
   600 JVM_END
   603 // java.io.File ///////////////////////////////////////////////////////////////
   605 JVM_LEAF(char*, JVM_NativePath(char* path))
   606   JVMWrapper2("JVM_NativePath (%s)", path);
   607   return hpi::native_path(path);
   608 JVM_END
   611 // Misc. class handling ///////////////////////////////////////////////////////////
   614 JVM_ENTRY(jclass, JVM_GetCallerClass(JNIEnv* env, int depth))
   615   JVMWrapper("JVM_GetCallerClass");
   616   klassOop k = thread->security_get_caller_class(depth);
   617   return (k == NULL) ? NULL : (jclass) JNIHandles::make_local(env, Klass::cast(k)->java_mirror());
   618 JVM_END
   621 JVM_ENTRY(jclass, JVM_FindPrimitiveClass(JNIEnv* env, const char* utf))
   622   JVMWrapper("JVM_FindPrimitiveClass");
   623   oop mirror = NULL;
   624   BasicType t = name2type(utf);
   625   if (t != T_ILLEGAL && t != T_OBJECT && t != T_ARRAY) {
   626     mirror = Universe::java_mirror(t);
   627   }
   628   if (mirror == NULL) {
   629     THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), (char*) utf);
   630   } else {
   631     return (jclass) JNIHandles::make_local(env, mirror);
   632   }
   633 JVM_END
   636 JVM_ENTRY(void, JVM_ResolveClass(JNIEnv* env, jclass cls))
   637   JVMWrapper("JVM_ResolveClass");
   638   if (PrintJVMWarnings) warning("JVM_ResolveClass not implemented");
   639 JVM_END
   641 // Common implementation for JVM_FindClassFromBootLoader and
   642 // JVM_FindClassFromLoader
   643 static jclass jvm_find_class_from_class_loader(JNIEnv* env, const char* name,
   644                                   jboolean init, jobject loader,
   645                                   jboolean throwError, TRAPS) {
   646   // Java libraries should ensure that name is never null...
   647   if (name == NULL || (int)strlen(name) > symbolOopDesc::max_length()) {
   648     // It's impossible to create this class;  the name cannot fit
   649     // into the constant pool.
   650     if (throwError) {
   651       THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
   652     } else {
   653       THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), name);
   654     }
   655   }
   656   symbolHandle h_name = oopFactory::new_symbol_handle(name, CHECK_NULL);
   657   Handle h_loader(THREAD, JNIHandles::resolve(loader));
   658   jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
   659                                                Handle(), throwError, THREAD);
   661   if (TraceClassResolution && result != NULL) {
   662     trace_class_resolution(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(result)));
   663   }
   664   return result;
   665 }
   667 // Rationale behind JVM_FindClassFromBootLoader
   668 // a> JVM_FindClassFromClassLoader was never exported in the export tables.
   669 // b> because of (a) java.dll has a direct dependecy on the  unexported
   670 //    private symbol "_JVM_FindClassFromClassLoader@20".
   671 // c> the launcher cannot use the private symbol as it dynamically opens
   672 //    the entry point, so if something changes, the launcher will fail
   673 //    unexpectedly at runtime, it is safest for the launcher to dlopen a
   674 //    stable exported interface.
   675 // d> re-exporting JVM_FindClassFromClassLoader as public, will cause its
   676 //    signature to change from _JVM_FindClassFromClassLoader@20 to
   677 //    JVM_FindClassFromClassLoader and will not be backward compatible
   678 //    with older JDKs.
   679 // Thus a public/stable exported entry point is the right solution,
   680 // public here means public in linker semantics, and is exported only
   681 // to the JDK, and is not intended to be a public API.
   683 JVM_ENTRY(jclass, JVM_FindClassFromBootLoader(JNIEnv* env,
   684                                               const char* name,
   685                                               jboolean throwError))
   686   JVMWrapper3("JVM_FindClassFromBootLoader %s throw %s", name,
   687               throwError ? "error" : "exception");
   688   return jvm_find_class_from_class_loader(env, name, JNI_FALSE,
   689                                           (jobject)NULL, throwError, THREAD);
   690 JVM_END
   692 JVM_ENTRY(jclass, JVM_FindClassFromClassLoader(JNIEnv* env, const char* name,
   693                                                jboolean init, jobject loader,
   694                                                jboolean throwError))
   695   JVMWrapper3("JVM_FindClassFromClassLoader %s throw %s", name,
   696                throwError ? "error" : "exception");
   697   return jvm_find_class_from_class_loader(env, name, init, loader,
   698                                           throwError, THREAD);
   699 JVM_END
   702 JVM_ENTRY(jclass, JVM_FindClassFromClass(JNIEnv *env, const char *name,
   703                                          jboolean init, jclass from))
   704   JVMWrapper2("JVM_FindClassFromClass %s", name);
   705   if (name == NULL || (int)strlen(name) > symbolOopDesc::max_length()) {
   706     // It's impossible to create this class;  the name cannot fit
   707     // into the constant pool.
   708     THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
   709   }
   710   symbolHandle h_name = oopFactory::new_symbol_handle(name, CHECK_NULL);
   711   oop from_class_oop = JNIHandles::resolve(from);
   712   klassOop from_class = (from_class_oop == NULL)
   713                            ? (klassOop)NULL
   714                            : java_lang_Class::as_klassOop(from_class_oop);
   715   oop class_loader = NULL;
   716   oop protection_domain = NULL;
   717   if (from_class != NULL) {
   718     class_loader = Klass::cast(from_class)->class_loader();
   719     protection_domain = Klass::cast(from_class)->protection_domain();
   720   }
   721   Handle h_loader(THREAD, class_loader);
   722   Handle h_prot  (THREAD, protection_domain);
   723   jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
   724                                                h_prot, true, thread);
   726   if (TraceClassResolution && result != NULL) {
   727     // this function is generally only used for class loading during verification.
   728     ResourceMark rm;
   729     oop from_mirror = JNIHandles::resolve_non_null(from);
   730     klassOop from_class = java_lang_Class::as_klassOop(from_mirror);
   731     const char * from_name = Klass::cast(from_class)->external_name();
   733     oop mirror = JNIHandles::resolve_non_null(result);
   734     klassOop to_class = java_lang_Class::as_klassOop(mirror);
   735     const char * to = Klass::cast(to_class)->external_name();
   736     tty->print("RESOLVE %s %s (verification)\n", from_name, to);
   737   }
   739   return result;
   740 JVM_END
   742 static void is_lock_held_by_thread(Handle loader, PerfCounter* counter, TRAPS) {
   743   if (loader.is_null()) {
   744     return;
   745   }
   747   // check whether the current caller thread holds the lock or not.
   748   // If not, increment the corresponding counter
   749   if (ObjectSynchronizer::query_lock_ownership((JavaThread*)THREAD, loader) !=
   750       ObjectSynchronizer::owner_self) {
   751     counter->inc();
   752   }
   753 }
   755 // common code for JVM_DefineClass() and JVM_DefineClassWithSource()
   756 static jclass jvm_define_class_common(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source, TRAPS) {
   757   if (source == NULL)  source = "__JVM_DefineClass__";
   759   // Since exceptions can be thrown, class initialization can take place
   760   // if name is NULL no check for class name in .class stream has to be made.
   761   symbolHandle class_name;
   762   if (name != NULL) {
   763     const int str_len = (int)strlen(name);
   764     if (str_len > symbolOopDesc::max_length()) {
   765       // It's impossible to create this class;  the name cannot fit
   766       // into the constant pool.
   767       THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
   768     }
   769     class_name = oopFactory::new_symbol_handle(name, str_len, CHECK_NULL);
   770   }
   772   ResourceMark rm(THREAD);
   773   ClassFileStream st((u1*) buf, len, (char *)source);
   774   Handle class_loader (THREAD, JNIHandles::resolve(loader));
   775   if (UsePerfData) {
   776     is_lock_held_by_thread(class_loader,
   777                            ClassLoader::sync_JVMDefineClassLockFreeCounter(),
   778                            THREAD);
   779   }
   780   Handle protection_domain (THREAD, JNIHandles::resolve(pd));
   781   klassOop k = SystemDictionary::resolve_from_stream(class_name, class_loader,
   782                                                      protection_domain, &st,
   783                                                      CHECK_NULL);
   785   if (TraceClassResolution && k != NULL) {
   786     trace_class_resolution(k);
   787   }
   789   return (jclass) JNIHandles::make_local(env, Klass::cast(k)->java_mirror());
   790 }
   793 JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd))
   794   JVMWrapper2("JVM_DefineClass %s", name);
   796   return jvm_define_class_common(env, name, loader, buf, len, pd, NULL, THREAD);
   797 JVM_END
   800 JVM_ENTRY(jclass, JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source))
   801   JVMWrapper2("JVM_DefineClassWithSource %s", name);
   803   return jvm_define_class_common(env, name, loader, buf, len, pd, source, THREAD);
   804 JVM_END
   807 JVM_ENTRY(jclass, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name))
   808   JVMWrapper("JVM_FindLoadedClass");
   809   ResourceMark rm(THREAD);
   811   Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
   812   Handle string = java_lang_String::internalize_classname(h_name, CHECK_NULL);
   814   const char* str   = java_lang_String::as_utf8_string(string());
   815   // Sanity check, don't expect null
   816   if (str == NULL) return NULL;
   818   const int str_len = (int)strlen(str);
   819   if (str_len > symbolOopDesc::max_length()) {
   820     // It's impossible to create this class;  the name cannot fit
   821     // into the constant pool.
   822     return NULL;
   823   }
   824   symbolHandle klass_name = oopFactory::new_symbol_handle(str, str_len,CHECK_NULL);
   826   // Security Note:
   827   //   The Java level wrapper will perform the necessary security check allowing
   828   //   us to pass the NULL as the initiating class loader.
   829   Handle h_loader(THREAD, JNIHandles::resolve(loader));
   830   if (UsePerfData) {
   831     is_lock_held_by_thread(h_loader,
   832                            ClassLoader::sync_JVMFindLoadedClassLockFreeCounter(),
   833                            THREAD);
   834   }
   836   klassOop k = SystemDictionary::find_instance_or_array_klass(klass_name,
   837                                                               h_loader,
   838                                                               Handle(),
   839                                                               CHECK_NULL);
   841   return (k == NULL) ? NULL :
   842             (jclass) JNIHandles::make_local(env, Klass::cast(k)->java_mirror());
   843 JVM_END
   846 // Reflection support //////////////////////////////////////////////////////////////////////////////
   848 JVM_ENTRY(jstring, JVM_GetClassName(JNIEnv *env, jclass cls))
   849   assert (cls != NULL, "illegal class");
   850   JVMWrapper("JVM_GetClassName");
   851   JvmtiVMObjectAllocEventCollector oam;
   852   ResourceMark rm(THREAD);
   853   const char* name;
   854   if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
   855     name = type2name(java_lang_Class::primitive_type(JNIHandles::resolve(cls)));
   856   } else {
   857     // Consider caching interned string in Klass
   858     klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
   859     assert(k->is_klass(), "just checking");
   860     name = Klass::cast(k)->external_name();
   861   }
   862   oop result = StringTable::intern((char*) name, CHECK_NULL);
   863   return (jstring) JNIHandles::make_local(env, result);
   864 JVM_END
   867 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
   868   JVMWrapper("JVM_GetClassInterfaces");
   869   JvmtiVMObjectAllocEventCollector oam;
   870   oop mirror = JNIHandles::resolve_non_null(cls);
   872   // Special handling for primitive objects
   873   if (java_lang_Class::is_primitive(mirror)) {
   874     // Primitive objects does not have any interfaces
   875     objArrayOop r = oopFactory::new_objArray(SystemDictionary::class_klass(), 0, CHECK_NULL);
   876     return (jobjectArray) JNIHandles::make_local(env, r);
   877   }
   879   KlassHandle klass(thread, java_lang_Class::as_klassOop(mirror));
   880   // Figure size of result array
   881   int size;
   882   if (klass->oop_is_instance()) {
   883     size = instanceKlass::cast(klass())->local_interfaces()->length();
   884   } else {
   885     assert(klass->oop_is_objArray() || klass->oop_is_typeArray(), "Illegal mirror klass");
   886     size = 2;
   887   }
   889   // Allocate result array
   890   objArrayOop r = oopFactory::new_objArray(SystemDictionary::class_klass(), size, CHECK_NULL);
   891   objArrayHandle result (THREAD, r);
   892   // Fill in result
   893   if (klass->oop_is_instance()) {
   894     // Regular instance klass, fill in all local interfaces
   895     for (int index = 0; index < size; index++) {
   896       klassOop k = klassOop(instanceKlass::cast(klass())->local_interfaces()->obj_at(index));
   897       result->obj_at_put(index, Klass::cast(k)->java_mirror());
   898     }
   899   } else {
   900     // All arrays implement java.lang.Cloneable and java.io.Serializable
   901     result->obj_at_put(0, Klass::cast(SystemDictionary::cloneable_klass())->java_mirror());
   902     result->obj_at_put(1, Klass::cast(SystemDictionary::serializable_klass())->java_mirror());
   903   }
   904   return (jobjectArray) JNIHandles::make_local(env, result());
   905 JVM_END
   908 JVM_ENTRY(jobject, JVM_GetClassLoader(JNIEnv *env, jclass cls))
   909   JVMWrapper("JVM_GetClassLoader");
   910   if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
   911     return NULL;
   912   }
   913   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
   914   oop loader = Klass::cast(k)->class_loader();
   915   return JNIHandles::make_local(env, loader);
   916 JVM_END
   919 JVM_QUICK_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
   920   JVMWrapper("JVM_IsInterface");
   921   oop mirror = JNIHandles::resolve_non_null(cls);
   922   if (java_lang_Class::is_primitive(mirror)) {
   923     return JNI_FALSE;
   924   }
   925   klassOop k = java_lang_Class::as_klassOop(mirror);
   926   jboolean result = Klass::cast(k)->is_interface();
   927   assert(!result || Klass::cast(k)->oop_is_instance(),
   928          "all interfaces are instance types");
   929   // The compiler intrinsic for isInterface tests the
   930   // Klass::_access_flags bits in the same way.
   931   return result;
   932 JVM_END
   935 JVM_ENTRY(jobjectArray, JVM_GetClassSigners(JNIEnv *env, jclass cls))
   936   JVMWrapper("JVM_GetClassSigners");
   937   JvmtiVMObjectAllocEventCollector oam;
   938   if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
   939     // There are no signers for primitive types
   940     return NULL;
   941   }
   943   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
   944   objArrayOop signers = NULL;
   945   if (Klass::cast(k)->oop_is_instance()) {
   946     signers = instanceKlass::cast(k)->signers();
   947   }
   949   // If there are no signers set in the class, or if the class
   950   // is an array, return NULL.
   951   if (signers == NULL) return NULL;
   953   // copy of the signers array
   954   klassOop element = objArrayKlass::cast(signers->klass())->element_klass();
   955   objArrayOop signers_copy = oopFactory::new_objArray(element, signers->length(), CHECK_NULL);
   956   for (int index = 0; index < signers->length(); index++) {
   957     signers_copy->obj_at_put(index, signers->obj_at(index));
   958   }
   960   // return the copy
   961   return (jobjectArray) JNIHandles::make_local(env, signers_copy);
   962 JVM_END
   965 JVM_ENTRY(void, JVM_SetClassSigners(JNIEnv *env, jclass cls, jobjectArray signers))
   966   JVMWrapper("JVM_SetClassSigners");
   967   if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
   968     // This call is ignored for primitive types and arrays.
   969     // Signers are only set once, ClassLoader.java, and thus shouldn't
   970     // be called with an array.  Only the bootstrap loader creates arrays.
   971     klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
   972     if (Klass::cast(k)->oop_is_instance()) {
   973       instanceKlass::cast(k)->set_signers(objArrayOop(JNIHandles::resolve(signers)));
   974     }
   975   }
   976 JVM_END
   979 JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls))
   980   JVMWrapper("JVM_GetProtectionDomain");
   981   if (JNIHandles::resolve(cls) == NULL) {
   982     THROW_(vmSymbols::java_lang_NullPointerException(), NULL);
   983   }
   985   if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
   986     // Primitive types does not have a protection domain.
   987     return NULL;
   988   }
   990   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
   991   return (jobject) JNIHandles::make_local(env, Klass::cast(k)->protection_domain());
   992 JVM_END
   995 // Obsolete since 1.2 (Class.setProtectionDomain removed), although
   996 // still defined in core libraries as of 1.5.
   997 JVM_ENTRY(void, JVM_SetProtectionDomain(JNIEnv *env, jclass cls, jobject protection_domain))
   998   JVMWrapper("JVM_SetProtectionDomain");
   999   if (JNIHandles::resolve(cls) == NULL) {
  1000     THROW(vmSymbols::java_lang_NullPointerException());
  1002   if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
  1003     // Call is ignored for primitive types
  1004     klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
  1006     // cls won't be an array, as this called only from ClassLoader.defineClass
  1007     if (Klass::cast(k)->oop_is_instance()) {
  1008       oop pd = JNIHandles::resolve(protection_domain);
  1009       assert(pd == NULL || pd->is_oop(), "just checking");
  1010       instanceKlass::cast(k)->set_protection_domain(pd);
  1013 JVM_END
  1016 JVM_ENTRY(jobject, JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject action, jobject context, jboolean wrapException))
  1017   JVMWrapper("JVM_DoPrivileged");
  1019   if (action == NULL) {
  1020     THROW_MSG_0(vmSymbols::java_lang_NullPointerException(), "Null action");
  1023   // Stack allocated list of privileged stack elements
  1024   PrivilegedElement pi;
  1026   // Check that action object understands "Object run()"
  1027   Handle object (THREAD, JNIHandles::resolve(action));
  1029   // get run() method
  1030   methodOop m_oop = Klass::cast(object->klass())->uncached_lookup_method(
  1031                                            vmSymbols::run_method_name(),
  1032                                            vmSymbols::void_object_signature());
  1033   methodHandle m (THREAD, m_oop);
  1034   if (m.is_null() || !m->is_method() || !methodOop(m())->is_public() || methodOop(m())->is_static()) {
  1035     THROW_MSG_0(vmSymbols::java_lang_InternalError(), "No run method");
  1038   // Compute the frame initiating the do privileged operation and setup the privileged stack
  1039   vframeStream vfst(thread);
  1040   vfst.security_get_caller_frame(1);
  1042   if (!vfst.at_end()) {
  1043     pi.initialize(&vfst, JNIHandles::resolve(context), thread->privileged_stack_top(), CHECK_NULL);
  1044     thread->set_privileged_stack_top(&pi);
  1048   // invoke the Object run() in the action object. We cannot use call_interface here, since the static type
  1049   // is not really known - it is either java.security.PrivilegedAction or java.security.PrivilegedExceptionAction
  1050   Handle pending_exception;
  1051   JavaValue result(T_OBJECT);
  1052   JavaCallArguments args(object);
  1053   JavaCalls::call(&result, m, &args, THREAD);
  1055   // done with action, remove ourselves from the list
  1056   if (!vfst.at_end()) {
  1057     assert(thread->privileged_stack_top() != NULL && thread->privileged_stack_top() == &pi, "wrong top element");
  1058     thread->set_privileged_stack_top(thread->privileged_stack_top()->next());
  1061   if (HAS_PENDING_EXCEPTION) {
  1062     pending_exception = Handle(THREAD, PENDING_EXCEPTION);
  1063     CLEAR_PENDING_EXCEPTION;
  1065     if ( pending_exception->is_a(SystemDictionary::exception_klass()) &&
  1066         !pending_exception->is_a(SystemDictionary::runtime_exception_klass())) {
  1067       // Throw a java.security.PrivilegedActionException(Exception e) exception
  1068       JavaCallArguments args(pending_exception);
  1069       THROW_ARG_0(vmSymbolHandles::java_security_PrivilegedActionException(),
  1070                   vmSymbolHandles::exception_void_signature(),
  1071                   &args);
  1075   if (pending_exception.not_null()) THROW_OOP_0(pending_exception());
  1076   return JNIHandles::make_local(env, (oop) result.get_jobject());
  1077 JVM_END
  1080 // Returns the inherited_access_control_context field of the running thread.
  1081 JVM_ENTRY(jobject, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls))
  1082   JVMWrapper("JVM_GetInheritedAccessControlContext");
  1083   oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj());
  1084   return JNIHandles::make_local(env, result);
  1085 JVM_END
  1087 class RegisterArrayForGC {
  1088  private:
  1089   JavaThread *_thread;
  1090  public:
  1091   RegisterArrayForGC(JavaThread *thread, GrowableArray<oop>* array)  {
  1092     _thread = thread;
  1093     _thread->register_array_for_gc(array);
  1096   ~RegisterArrayForGC() {
  1097     _thread->register_array_for_gc(NULL);
  1099 };
  1102 JVM_ENTRY(jobject, JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls))
  1103   JVMWrapper("JVM_GetStackAccessControlContext");
  1104   if (!UsePrivilegedStack) return NULL;
  1106   ResourceMark rm(THREAD);
  1107   GrowableArray<oop>* local_array = new GrowableArray<oop>(12);
  1108   JvmtiVMObjectAllocEventCollector oam;
  1110   // count the protection domains on the execution stack. We collapse
  1111   // duplicate consecutive protection domains into a single one, as
  1112   // well as stopping when we hit a privileged frame.
  1114   // Use vframeStream to iterate through Java frames
  1115   vframeStream vfst(thread);
  1117   oop previous_protection_domain = NULL;
  1118   Handle privileged_context(thread, NULL);
  1119   bool is_privileged = false;
  1120   oop protection_domain = NULL;
  1122   for(; !vfst.at_end(); vfst.next()) {
  1123     // get method of frame
  1124     methodOop method = vfst.method();
  1125     intptr_t* frame_id   = vfst.frame_id();
  1127     // check the privileged frames to see if we have a match
  1128     if (thread->privileged_stack_top() && thread->privileged_stack_top()->frame_id() == frame_id) {
  1129       // this frame is privileged
  1130       is_privileged = true;
  1131       privileged_context = Handle(thread, thread->privileged_stack_top()->privileged_context());
  1132       protection_domain  = thread->privileged_stack_top()->protection_domain();
  1133     } else {
  1134       protection_domain = instanceKlass::cast(method->method_holder())->protection_domain();
  1137     if ((previous_protection_domain != protection_domain) && (protection_domain != NULL)) {
  1138       local_array->push(protection_domain);
  1139       previous_protection_domain = protection_domain;
  1142     if (is_privileged) break;
  1146   // either all the domains on the stack were system domains, or
  1147   // we had a privileged system domain
  1148   if (local_array->is_empty()) {
  1149     if (is_privileged && privileged_context.is_null()) return NULL;
  1151     oop result = java_security_AccessControlContext::create(objArrayHandle(), is_privileged, privileged_context, CHECK_NULL);
  1152     return JNIHandles::make_local(env, result);
  1155   // the resource area must be registered in case of a gc
  1156   RegisterArrayForGC ragc(thread, local_array);
  1157   objArrayOop context = oopFactory::new_objArray(SystemDictionary::protectionDomain_klass(),
  1158                                                  local_array->length(), CHECK_NULL);
  1159   objArrayHandle h_context(thread, context);
  1160   for (int index = 0; index < local_array->length(); index++) {
  1161     h_context->obj_at_put(index, local_array->at(index));
  1164   oop result = java_security_AccessControlContext::create(h_context, is_privileged, privileged_context, CHECK_NULL);
  1166   return JNIHandles::make_local(env, result);
  1167 JVM_END
  1170 JVM_QUICK_ENTRY(jboolean, JVM_IsArrayClass(JNIEnv *env, jclass cls))
  1171   JVMWrapper("JVM_IsArrayClass");
  1172   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  1173   return (k != NULL) && Klass::cast(k)->oop_is_javaArray() ? true : false;
  1174 JVM_END
  1177 JVM_QUICK_ENTRY(jboolean, JVM_IsPrimitiveClass(JNIEnv *env, jclass cls))
  1178   JVMWrapper("JVM_IsPrimitiveClass");
  1179   oop mirror = JNIHandles::resolve_non_null(cls);
  1180   return (jboolean) java_lang_Class::is_primitive(mirror);
  1181 JVM_END
  1184 JVM_ENTRY(jclass, JVM_GetComponentType(JNIEnv *env, jclass cls))
  1185   JVMWrapper("JVM_GetComponentType");
  1186   oop mirror = JNIHandles::resolve_non_null(cls);
  1187   oop result = Reflection::array_component_type(mirror, CHECK_NULL);
  1188   return (jclass) JNIHandles::make_local(env, result);
  1189 JVM_END
  1192 JVM_ENTRY(jint, JVM_GetClassModifiers(JNIEnv *env, jclass cls))
  1193   JVMWrapper("JVM_GetClassModifiers");
  1194   if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
  1195     // Primitive type
  1196     return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
  1199   Klass* k = Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls)));
  1200   debug_only(int computed_modifiers = k->compute_modifier_flags(CHECK_0));
  1201   assert(k->modifier_flags() == computed_modifiers, "modifiers cache is OK");
  1202   return k->modifier_flags();
  1203 JVM_END
  1206 // Inner class reflection ///////////////////////////////////////////////////////////////////////////////
  1208 JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass))
  1209   const int inner_class_info_index = 0;
  1210   const int outer_class_info_index = 1;
  1212   JvmtiVMObjectAllocEventCollector oam;
  1213   // ofClass is a reference to a java_lang_Class object. The mirror object
  1214   // of an instanceKlass
  1216   if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
  1217       ! Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_instance()) {
  1218     oop result = oopFactory::new_objArray(SystemDictionary::class_klass(), 0, CHECK_NULL);
  1219     return (jobjectArray)JNIHandles::make_local(env, result);
  1222   instanceKlassHandle k(thread, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
  1224   if (k->inner_classes()->length() == 0) {
  1225     // Neither an inner nor outer class
  1226     oop result = oopFactory::new_objArray(SystemDictionary::class_klass(), 0, CHECK_NULL);
  1227     return (jobjectArray)JNIHandles::make_local(env, result);
  1230   // find inner class info
  1231   typeArrayHandle    icls(thread, k->inner_classes());
  1232   constantPoolHandle cp(thread, k->constants());
  1233   int length = icls->length();
  1235   // Allocate temp. result array
  1236   objArrayOop r = oopFactory::new_objArray(SystemDictionary::class_klass(), length/4, CHECK_NULL);
  1237   objArrayHandle result (THREAD, r);
  1238   int members = 0;
  1240   for(int i = 0; i < length; i += 4) {
  1241     int ioff = icls->ushort_at(i + inner_class_info_index);
  1242     int ooff = icls->ushort_at(i + outer_class_info_index);
  1244     if (ioff != 0 && ooff != 0) {
  1245       // Check to see if the name matches the class we're looking for
  1246       // before attempting to find the class.
  1247       if (cp->klass_name_at_matches(k, ooff)) {
  1248         klassOop outer_klass = cp->klass_at(ooff, CHECK_NULL);
  1249         if (outer_klass == k()) {
  1250            klassOop ik = cp->klass_at(ioff, CHECK_NULL);
  1251            instanceKlassHandle inner_klass (THREAD, ik);
  1253            // Throws an exception if outer klass has not declared k as
  1254            // an inner klass
  1255            Reflection::check_for_inner_class(k, inner_klass, CHECK_NULL);
  1257            result->obj_at_put(members, inner_klass->java_mirror());
  1258            members++;
  1264   if (members != length) {
  1265     // Return array of right length
  1266     objArrayOop res = oopFactory::new_objArray(SystemDictionary::class_klass(), members, CHECK_NULL);
  1267     for(int i = 0; i < members; i++) {
  1268       res->obj_at_put(i, result->obj_at(i));
  1270     return (jobjectArray)JNIHandles::make_local(env, res);
  1273   return (jobjectArray)JNIHandles::make_local(env, result());
  1274 JVM_END
  1277 JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass))
  1278   const int inner_class_info_index = 0;
  1279   const int outer_class_info_index = 1;
  1281   // ofClass is a reference to a java_lang_Class object.
  1282   if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
  1283       ! Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_instance()) {
  1284     return NULL;
  1287   instanceKlassHandle k(thread, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
  1289   if (k->inner_classes()->length() == 0) {
  1290     // No inner class info => no declaring class
  1291     return NULL;
  1294   typeArrayHandle i_icls(thread, k->inner_classes());
  1295   constantPoolHandle i_cp(thread, k->constants());
  1296   int i_length = i_icls->length();
  1298   bool found = false;
  1299   klassOop ok;
  1300   instanceKlassHandle outer_klass;
  1302   // Find inner_klass attribute
  1303   for(int i = 0; i < i_length && !found; i+= 4) {
  1304     int ioff = i_icls->ushort_at(i + inner_class_info_index);
  1305     int ooff = i_icls->ushort_at(i + outer_class_info_index);
  1307     if (ioff != 0 && ooff != 0) {
  1308       // Check to see if the name matches the class we're looking for
  1309       // before attempting to find the class.
  1310       if (i_cp->klass_name_at_matches(k, ioff)) {
  1311         klassOop inner_klass = i_cp->klass_at(ioff, CHECK_NULL);
  1312         if (k() == inner_klass) {
  1313           found = true;
  1314           ok = i_cp->klass_at(ooff, CHECK_NULL);
  1315           outer_klass = instanceKlassHandle(thread, ok);
  1321   // If no inner class attribute found for this class.
  1322   if (!found) return NULL;
  1324   // Throws an exception if outer klass has not declared k as an inner klass
  1325   Reflection::check_for_inner_class(outer_klass, k, CHECK_NULL);
  1327   return (jclass)JNIHandles::make_local(env, outer_klass->java_mirror());
  1328 JVM_END
  1331 JVM_ENTRY(jstring, JVM_GetClassSignature(JNIEnv *env, jclass cls))
  1332   assert (cls != NULL, "illegal class");
  1333   JVMWrapper("JVM_GetClassSignature");
  1334   JvmtiVMObjectAllocEventCollector oam;
  1335   ResourceMark rm(THREAD);
  1336   // Return null for arrays and primatives
  1337   if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
  1338     klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
  1339     if (Klass::cast(k)->oop_is_instance()) {
  1340       symbolHandle sym = symbolHandle(THREAD, instanceKlass::cast(k)->generic_signature());
  1341       if (sym.is_null()) return NULL;
  1342       Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
  1343       return (jstring) JNIHandles::make_local(env, str());
  1346   return NULL;
  1347 JVM_END
  1350 JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls))
  1351   assert (cls != NULL, "illegal class");
  1352   JVMWrapper("JVM_GetClassAnnotations");
  1353   ResourceMark rm(THREAD);
  1354   // Return null for arrays and primitives
  1355   if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
  1356     klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
  1357     if (Klass::cast(k)->oop_is_instance()) {
  1358       return (jbyteArray) JNIHandles::make_local(env,
  1359                                   instanceKlass::cast(k)->class_annotations());
  1362   return NULL;
  1363 JVM_END
  1366 JVM_ENTRY(jbyteArray, JVM_GetFieldAnnotations(JNIEnv *env, jobject field))
  1367   assert(field != NULL, "illegal field");
  1368   JVMWrapper("JVM_GetFieldAnnotations");
  1370   // some of this code was adapted from from jni_FromReflectedField
  1372   // field is a handle to a java.lang.reflect.Field object
  1373   oop reflected = JNIHandles::resolve_non_null(field);
  1374   oop mirror    = java_lang_reflect_Field::clazz(reflected);
  1375   klassOop k    = java_lang_Class::as_klassOop(mirror);
  1376   int slot      = java_lang_reflect_Field::slot(reflected);
  1377   int modifiers = java_lang_reflect_Field::modifiers(reflected);
  1379   fieldDescriptor fd;
  1380   KlassHandle kh(THREAD, k);
  1381   intptr_t offset = instanceKlass::cast(kh())->offset_from_fields(slot);
  1383   if (modifiers & JVM_ACC_STATIC) {
  1384     // for static fields we only look in the current class
  1385     if (!instanceKlass::cast(kh())->find_local_field_from_offset(offset,
  1386                                                                  true, &fd)) {
  1387       assert(false, "cannot find static field");
  1388       return NULL;  // robustness
  1390   } else {
  1391     // for instance fields we start with the current class and work
  1392     // our way up through the superclass chain
  1393     if (!instanceKlass::cast(kh())->find_field_from_offset(offset, false,
  1394                                                            &fd)) {
  1395       assert(false, "cannot find instance field");
  1396       return NULL;  // robustness
  1400   return (jbyteArray) JNIHandles::make_local(env, fd.annotations());
  1401 JVM_END
  1404 static methodOop jvm_get_method_common(jobject method, TRAPS) {
  1405   // some of this code was adapted from from jni_FromReflectedMethod
  1407   oop reflected = JNIHandles::resolve_non_null(method);
  1408   oop mirror    = NULL;
  1409   int slot      = 0;
  1411   if (reflected->klass() == SystemDictionary::reflect_constructor_klass()) {
  1412     mirror = java_lang_reflect_Constructor::clazz(reflected);
  1413     slot   = java_lang_reflect_Constructor::slot(reflected);
  1414   } else {
  1415     assert(reflected->klass() == SystemDictionary::reflect_method_klass(),
  1416            "wrong type");
  1417     mirror = java_lang_reflect_Method::clazz(reflected);
  1418     slot   = java_lang_reflect_Method::slot(reflected);
  1420   klassOop k = java_lang_Class::as_klassOop(mirror);
  1422   KlassHandle kh(THREAD, k);
  1423   methodOop m = instanceKlass::cast(kh())->method_with_idnum(slot);
  1424   if (m == NULL) {
  1425     assert(false, "cannot find method");
  1426     return NULL;  // robustness
  1429   return m;
  1433 JVM_ENTRY(jbyteArray, JVM_GetMethodAnnotations(JNIEnv *env, jobject method))
  1434   JVMWrapper("JVM_GetMethodAnnotations");
  1436   // method is a handle to a java.lang.reflect.Method object
  1437   methodOop m = jvm_get_method_common(method, CHECK_NULL);
  1438   return (jbyteArray) JNIHandles::make_local(env, m->annotations());
  1439 JVM_END
  1442 JVM_ENTRY(jbyteArray, JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method))
  1443   JVMWrapper("JVM_GetMethodDefaultAnnotationValue");
  1445   // method is a handle to a java.lang.reflect.Method object
  1446   methodOop m = jvm_get_method_common(method, CHECK_NULL);
  1447   return (jbyteArray) JNIHandles::make_local(env, m->annotation_default());
  1448 JVM_END
  1451 JVM_ENTRY(jbyteArray, JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method))
  1452   JVMWrapper("JVM_GetMethodParameterAnnotations");
  1454   // method is a handle to a java.lang.reflect.Method object
  1455   methodOop m = jvm_get_method_common(method, CHECK_NULL);
  1456   return (jbyteArray) JNIHandles::make_local(env, m->parameter_annotations());
  1457 JVM_END
  1460 // New (JDK 1.4) reflection implementation /////////////////////////////////////
  1462 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly))
  1464   JVMWrapper("JVM_GetClassDeclaredFields");
  1465   JvmtiVMObjectAllocEventCollector oam;
  1467   // Exclude primitive types and array types
  1468   if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
  1469       Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_javaArray()) {
  1470     // Return empty array
  1471     oop res = oopFactory::new_objArray(SystemDictionary::reflect_field_klass(), 0, CHECK_NULL);
  1472     return (jobjectArray) JNIHandles::make_local(env, res);
  1475   instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
  1476   constantPoolHandle cp(THREAD, k->constants());
  1478   // Ensure class is linked
  1479   k->link_class(CHECK_NULL);
  1481   typeArrayHandle fields(THREAD, k->fields());
  1482   int fields_len = fields->length();
  1484   // 4496456 We need to filter out java.lang.Throwable.backtrace
  1485   bool skip_backtrace = false;
  1487   // Allocate result
  1488   int num_fields;
  1490   if (publicOnly) {
  1491     num_fields = 0;
  1492     for (int i = 0, j = 0; i < fields_len; i += instanceKlass::next_offset, j++) {
  1493       int mods = fields->ushort_at(i + instanceKlass::access_flags_offset) & JVM_RECOGNIZED_FIELD_MODIFIERS;
  1494       if (mods & JVM_ACC_PUBLIC) ++num_fields;
  1496   } else {
  1497     num_fields = fields_len / instanceKlass::next_offset;
  1499     if (k() == SystemDictionary::throwable_klass()) {
  1500       num_fields--;
  1501       skip_backtrace = true;
  1505   objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_field_klass(), num_fields, CHECK_NULL);
  1506   objArrayHandle result (THREAD, r);
  1508   int out_idx = 0;
  1509   fieldDescriptor fd;
  1510   for (int i = 0; i < fields_len; i += instanceKlass::next_offset) {
  1511     if (skip_backtrace) {
  1512       // 4496456 skip java.lang.Throwable.backtrace
  1513       int offset = k->offset_from_fields(i);
  1514       if (offset == java_lang_Throwable::get_backtrace_offset()) continue;
  1517     int mods = fields->ushort_at(i + instanceKlass::access_flags_offset) & JVM_RECOGNIZED_FIELD_MODIFIERS;
  1518     if (!publicOnly || (mods & JVM_ACC_PUBLIC)) {
  1519       fd.initialize(k(), i);
  1520       oop field = Reflection::new_field(&fd, UseNewReflection, CHECK_NULL);
  1521       result->obj_at_put(out_idx, field);
  1522       ++out_idx;
  1525   assert(out_idx == num_fields, "just checking");
  1526   return (jobjectArray) JNIHandles::make_local(env, result());
  1528 JVM_END
  1530 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
  1532   JVMWrapper("JVM_GetClassDeclaredMethods");
  1533   JvmtiVMObjectAllocEventCollector oam;
  1535   // Exclude primitive types and array types
  1536   if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass))
  1537       || Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_javaArray()) {
  1538     // Return empty array
  1539     oop res = oopFactory::new_objArray(SystemDictionary::reflect_method_klass(), 0, CHECK_NULL);
  1540     return (jobjectArray) JNIHandles::make_local(env, res);
  1543   instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
  1545   // Ensure class is linked
  1546   k->link_class(CHECK_NULL);
  1548   objArrayHandle methods (THREAD, k->methods());
  1549   int methods_length = methods->length();
  1550   int num_methods = 0;
  1552   int i;
  1553   for (i = 0; i < methods_length; i++) {
  1554     methodHandle method(THREAD, (methodOop) methods->obj_at(i));
  1555     if (!method->is_initializer()) {
  1556       if (!publicOnly || method->is_public()) {
  1557         ++num_methods;
  1562   // Allocate result
  1563   objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_method_klass(), num_methods, CHECK_NULL);
  1564   objArrayHandle result (THREAD, r);
  1566   int out_idx = 0;
  1567   for (i = 0; i < methods_length; i++) {
  1568     methodHandle method(THREAD, (methodOop) methods->obj_at(i));
  1569     if (!method->is_initializer()) {
  1570       if (!publicOnly || method->is_public()) {
  1571         oop m = Reflection::new_method(method, UseNewReflection, false, CHECK_NULL);
  1572         result->obj_at_put(out_idx, m);
  1573         ++out_idx;
  1577   assert(out_idx == num_methods, "just checking");
  1578   return (jobjectArray) JNIHandles::make_local(env, result());
  1580 JVM_END
  1582 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
  1584   JVMWrapper("JVM_GetClassDeclaredConstructors");
  1585   JvmtiVMObjectAllocEventCollector oam;
  1587   // Exclude primitive types and array types
  1588   if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass))
  1589       || Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_javaArray()) {
  1590     // Return empty array
  1591     oop res = oopFactory::new_objArray(SystemDictionary::reflect_constructor_klass(), 0 , CHECK_NULL);
  1592     return (jobjectArray) JNIHandles::make_local(env, res);
  1595   instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
  1597   // Ensure class is linked
  1598   k->link_class(CHECK_NULL);
  1600   objArrayHandle methods (THREAD, k->methods());
  1601   int methods_length = methods->length();
  1602   int num_constructors = 0;
  1604   int i;
  1605   for (i = 0; i < methods_length; i++) {
  1606     methodHandle method(THREAD, (methodOop) methods->obj_at(i));
  1607     if (method->is_initializer() && !method->is_static()) {
  1608       if (!publicOnly || method->is_public()) {
  1609         ++num_constructors;
  1614   // Allocate result
  1615   objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_constructor_klass(), num_constructors, CHECK_NULL);
  1616   objArrayHandle result(THREAD, r);
  1618   int out_idx = 0;
  1619   for (i = 0; i < methods_length; i++) {
  1620     methodHandle method(THREAD, (methodOop) methods->obj_at(i));
  1621     if (method->is_initializer() && !method->is_static()) {
  1622       if (!publicOnly || method->is_public()) {
  1623         oop m = Reflection::new_constructor(method, CHECK_NULL);
  1624         result->obj_at_put(out_idx, m);
  1625         ++out_idx;
  1629   assert(out_idx == num_constructors, "just checking");
  1630   return (jobjectArray) JNIHandles::make_local(env, result());
  1632 JVM_END
  1634 JVM_ENTRY(jint, JVM_GetClassAccessFlags(JNIEnv *env, jclass cls))
  1636   JVMWrapper("JVM_GetClassAccessFlags");
  1637   if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
  1638     // Primitive type
  1639     return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
  1642   Klass* k = Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls)));
  1643   return k->access_flags().as_int() & JVM_ACC_WRITTEN_FLAGS;
  1645 JVM_END
  1648 // Constant pool access //////////////////////////////////////////////////////////
  1650 JVM_ENTRY(jobject, JVM_GetClassConstantPool(JNIEnv *env, jclass cls))
  1652   JVMWrapper("JVM_GetClassConstantPool");
  1653   JvmtiVMObjectAllocEventCollector oam;
  1655   // Return null for primitives and arrays
  1656   if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
  1657     klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  1658     if (Klass::cast(k)->oop_is_instance()) {
  1659       instanceKlassHandle k_h(THREAD, k);
  1660       Handle jcp = sun_reflect_ConstantPool::create(CHECK_NULL);
  1661       sun_reflect_ConstantPool::set_cp_oop(jcp(), k_h->constants());
  1662       return JNIHandles::make_local(jcp());
  1665   return NULL;
  1667 JVM_END
  1670 JVM_ENTRY(jint, JVM_ConstantPoolGetSize(JNIEnv *env, jobject unused, jobject jcpool))
  1672   JVMWrapper("JVM_ConstantPoolGetSize");
  1673   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1674   return cp->length();
  1676 JVM_END
  1679 static void bounds_check(constantPoolHandle cp, jint index, TRAPS) {
  1680   if (!cp->is_within_bounds(index)) {
  1681     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds");
  1686 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
  1688   JVMWrapper("JVM_ConstantPoolGetClassAt");
  1689   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1690   bounds_check(cp, index, CHECK_NULL);
  1691   constantTag tag = cp->tag_at(index);
  1692   if (!tag.is_klass() && !tag.is_unresolved_klass()) {
  1693     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
  1695   klassOop k = cp->klass_at(index, CHECK_NULL);
  1696   return (jclass) JNIHandles::make_local(k->klass_part()->java_mirror());
  1698 JVM_END
  1701 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index))
  1703   JVMWrapper("JVM_ConstantPoolGetClassAtIfLoaded");
  1704   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1705   bounds_check(cp, index, CHECK_NULL);
  1706   constantTag tag = cp->tag_at(index);
  1707   if (!tag.is_klass() && !tag.is_unresolved_klass()) {
  1708     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
  1710   klassOop k = constantPoolOopDesc::klass_at_if_loaded(cp, index);
  1711   if (k == NULL) return NULL;
  1712   return (jclass) JNIHandles::make_local(k->klass_part()->java_mirror());
  1714 JVM_END
  1716 static jobject get_method_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
  1717   constantTag tag = cp->tag_at(index);
  1718   if (!tag.is_method() && !tag.is_interface_method()) {
  1719     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
  1721   int klass_ref  = cp->uncached_klass_ref_index_at(index);
  1722   klassOop k_o;
  1723   if (force_resolution) {
  1724     k_o = cp->klass_at(klass_ref, CHECK_NULL);
  1725   } else {
  1726     k_o = constantPoolOopDesc::klass_at_if_loaded(cp, klass_ref);
  1727     if (k_o == NULL) return NULL;
  1729   instanceKlassHandle k(THREAD, k_o);
  1730   symbolOop name = cp->uncached_name_ref_at(index);
  1731   symbolOop sig  = cp->uncached_signature_ref_at(index);
  1732   methodHandle m (THREAD, k->find_method(name, sig));
  1733   if (m.is_null()) {
  1734     THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
  1736   oop method;
  1737   if (!m->is_initializer() || m->is_static()) {
  1738     method = Reflection::new_method(m, true, true, CHECK_NULL);
  1739   } else {
  1740     method = Reflection::new_constructor(m, CHECK_NULL);
  1742   return JNIHandles::make_local(method);
  1745 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
  1747   JVMWrapper("JVM_ConstantPoolGetMethodAt");
  1748   JvmtiVMObjectAllocEventCollector oam;
  1749   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1750   bounds_check(cp, index, CHECK_NULL);
  1751   jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
  1752   return res;
  1754 JVM_END
  1756 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index))
  1758   JVMWrapper("JVM_ConstantPoolGetMethodAtIfLoaded");
  1759   JvmtiVMObjectAllocEventCollector oam;
  1760   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1761   bounds_check(cp, index, CHECK_NULL);
  1762   jobject res = get_method_at_helper(cp, index, false, CHECK_NULL);
  1763   return res;
  1765 JVM_END
  1767 static jobject get_field_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
  1768   constantTag tag = cp->tag_at(index);
  1769   if (!tag.is_field()) {
  1770     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
  1772   int klass_ref  = cp->uncached_klass_ref_index_at(index);
  1773   klassOop k_o;
  1774   if (force_resolution) {
  1775     k_o = cp->klass_at(klass_ref, CHECK_NULL);
  1776   } else {
  1777     k_o = constantPoolOopDesc::klass_at_if_loaded(cp, klass_ref);
  1778     if (k_o == NULL) return NULL;
  1780   instanceKlassHandle k(THREAD, k_o);
  1781   symbolOop name = cp->uncached_name_ref_at(index);
  1782   symbolOop sig  = cp->uncached_signature_ref_at(index);
  1783   fieldDescriptor fd;
  1784   klassOop target_klass = k->find_field(name, sig, &fd);
  1785   if (target_klass == NULL) {
  1786     THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up field in target class");
  1788   oop field = Reflection::new_field(&fd, true, CHECK_NULL);
  1789   return JNIHandles::make_local(field);
  1792 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
  1794   JVMWrapper("JVM_ConstantPoolGetFieldAt");
  1795   JvmtiVMObjectAllocEventCollector oam;
  1796   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1797   bounds_check(cp, index, CHECK_NULL);
  1798   jobject res = get_field_at_helper(cp, index, true, CHECK_NULL);
  1799   return res;
  1801 JVM_END
  1803 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index))
  1805   JVMWrapper("JVM_ConstantPoolGetFieldAtIfLoaded");
  1806   JvmtiVMObjectAllocEventCollector oam;
  1807   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1808   bounds_check(cp, index, CHECK_NULL);
  1809   jobject res = get_field_at_helper(cp, index, false, CHECK_NULL);
  1810   return res;
  1812 JVM_END
  1814 JVM_ENTRY(jobjectArray, JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
  1816   JVMWrapper("JVM_ConstantPoolGetMemberRefInfoAt");
  1817   JvmtiVMObjectAllocEventCollector oam;
  1818   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1819   bounds_check(cp, index, CHECK_NULL);
  1820   constantTag tag = cp->tag_at(index);
  1821   if (!tag.is_field_or_method()) {
  1822     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
  1824   int klass_ref = cp->uncached_klass_ref_index_at(index);
  1825   symbolHandle klass_name (THREAD, cp->klass_name_at(klass_ref));
  1826   symbolHandle member_name(THREAD, cp->uncached_name_ref_at(index));
  1827   symbolHandle member_sig (THREAD, cp->uncached_signature_ref_at(index));
  1828   objArrayOop  dest_o = oopFactory::new_objArray(SystemDictionary::string_klass(), 3, CHECK_NULL);
  1829   objArrayHandle dest(THREAD, dest_o);
  1830   Handle str = java_lang_String::create_from_symbol(klass_name, CHECK_NULL);
  1831   dest->obj_at_put(0, str());
  1832   str = java_lang_String::create_from_symbol(member_name, CHECK_NULL);
  1833   dest->obj_at_put(1, str());
  1834   str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL);
  1835   dest->obj_at_put(2, str());
  1836   return (jobjectArray) JNIHandles::make_local(dest());
  1838 JVM_END
  1840 JVM_ENTRY(jint, JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
  1842   JVMWrapper("JVM_ConstantPoolGetIntAt");
  1843   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1844   bounds_check(cp, index, CHECK_0);
  1845   constantTag tag = cp->tag_at(index);
  1846   if (!tag.is_int()) {
  1847     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
  1849   return cp->int_at(index);
  1851 JVM_END
  1853 JVM_ENTRY(jlong, JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
  1855   JVMWrapper("JVM_ConstantPoolGetLongAt");
  1856   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1857   bounds_check(cp, index, CHECK_(0L));
  1858   constantTag tag = cp->tag_at(index);
  1859   if (!tag.is_long()) {
  1860     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
  1862   return cp->long_at(index);
  1864 JVM_END
  1866 JVM_ENTRY(jfloat, JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
  1868   JVMWrapper("JVM_ConstantPoolGetFloatAt");
  1869   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1870   bounds_check(cp, index, CHECK_(0.0f));
  1871   constantTag tag = cp->tag_at(index);
  1872   if (!tag.is_float()) {
  1873     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
  1875   return cp->float_at(index);
  1877 JVM_END
  1879 JVM_ENTRY(jdouble, JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
  1881   JVMWrapper("JVM_ConstantPoolGetDoubleAt");
  1882   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1883   bounds_check(cp, index, CHECK_(0.0));
  1884   constantTag tag = cp->tag_at(index);
  1885   if (!tag.is_double()) {
  1886     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
  1888   return cp->double_at(index);
  1890 JVM_END
  1892 JVM_ENTRY(jstring, JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
  1894   JVMWrapper("JVM_ConstantPoolGetStringAt");
  1895   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1896   bounds_check(cp, index, CHECK_NULL);
  1897   constantTag tag = cp->tag_at(index);
  1898   if (!tag.is_string() && !tag.is_unresolved_string()) {
  1899     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
  1901   oop str = cp->string_at(index, CHECK_NULL);
  1902   return (jstring) JNIHandles::make_local(str);
  1904 JVM_END
  1906 JVM_ENTRY(jstring, JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject unused, jobject jcpool, jint index))
  1908   JVMWrapper("JVM_ConstantPoolGetUTF8At");
  1909   JvmtiVMObjectAllocEventCollector oam;
  1910   constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
  1911   bounds_check(cp, index, CHECK_NULL);
  1912   constantTag tag = cp->tag_at(index);
  1913   if (!tag.is_symbol()) {
  1914     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
  1916   symbolOop sym_o = cp->symbol_at(index);
  1917   symbolHandle sym(THREAD, sym_o);
  1918   Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
  1919   return (jstring) JNIHandles::make_local(str());
  1921 JVM_END
  1924 // Assertion support. //////////////////////////////////////////////////////////
  1926 JVM_ENTRY(jboolean, JVM_DesiredAssertionStatus(JNIEnv *env, jclass unused, jclass cls))
  1927   JVMWrapper("JVM_DesiredAssertionStatus");
  1928   assert(cls != NULL, "bad class");
  1930   oop r = JNIHandles::resolve(cls);
  1931   assert(! java_lang_Class::is_primitive(r), "primitive classes not allowed");
  1932   if (java_lang_Class::is_primitive(r)) return false;
  1934   klassOop k = java_lang_Class::as_klassOop(r);
  1935   assert(Klass::cast(k)->oop_is_instance(), "must be an instance klass");
  1936   if (! Klass::cast(k)->oop_is_instance()) return false;
  1938   ResourceMark rm(THREAD);
  1939   const char* name = Klass::cast(k)->name()->as_C_string();
  1940   bool system_class = Klass::cast(k)->class_loader() == NULL;
  1941   return JavaAssertions::enabled(name, system_class);
  1943 JVM_END
  1946 // Return a new AssertionStatusDirectives object with the fields filled in with
  1947 // command-line assertion arguments (i.e., -ea, -da).
  1948 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
  1949   JVMWrapper("JVM_AssertionStatusDirectives");
  1950   JvmtiVMObjectAllocEventCollector oam;
  1951   oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
  1952   return JNIHandles::make_local(env, asd);
  1953 JVM_END
  1955 // Verification ////////////////////////////////////////////////////////////////////////////////
  1957 // Reflection for the verifier /////////////////////////////////////////////////////////////////
  1959 // RedefineClasses support: bug 6214132 caused verification to fail.
  1960 // All functions from this section should call the jvmtiThreadSate function:
  1961 //   klassOop class_to_verify_considering_redefinition(klassOop klass).
  1962 // The function returns a klassOop of the _scratch_class if the verifier
  1963 // was invoked in the middle of the class redefinition.
  1964 // Otherwise it returns its argument value which is the _the_class klassOop.
  1965 // Please, refer to the description in the jvmtiThreadSate.hpp.
  1967 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
  1968   JVMWrapper("JVM_GetClassNameUTF");
  1969   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  1970   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  1971   return Klass::cast(k)->name()->as_utf8();
  1972 JVM_END
  1975 JVM_QUICK_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))
  1976   JVMWrapper("JVM_GetClassCPTypes");
  1977   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  1978   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  1979   // types will have length zero if this is not an instanceKlass
  1980   // (length is determined by call to JVM_GetClassCPEntriesCount)
  1981   if (Klass::cast(k)->oop_is_instance()) {
  1982     constantPoolOop cp = instanceKlass::cast(k)->constants();
  1983     for (int index = cp->length() - 1; index >= 0; index--) {
  1984       constantTag tag = cp->tag_at(index);
  1985       types[index] = (tag.is_unresolved_klass()) ? JVM_CONSTANT_Class :
  1986                      (tag.is_unresolved_string()) ? JVM_CONSTANT_String : tag.value();
  1989 JVM_END
  1992 JVM_QUICK_ENTRY(jint, JVM_GetClassCPEntriesCount(JNIEnv *env, jclass cls))
  1993   JVMWrapper("JVM_GetClassCPEntriesCount");
  1994   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  1995   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  1996   if (!Klass::cast(k)->oop_is_instance())
  1997     return 0;
  1998   return instanceKlass::cast(k)->constants()->length();
  1999 JVM_END
  2002 JVM_QUICK_ENTRY(jint, JVM_GetClassFieldsCount(JNIEnv *env, jclass cls))
  2003   JVMWrapper("JVM_GetClassFieldsCount");
  2004   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2005   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2006   if (!Klass::cast(k)->oop_is_instance())
  2007     return 0;
  2008   return instanceKlass::cast(k)->fields()->length() / instanceKlass::next_offset;
  2009 JVM_END
  2012 JVM_QUICK_ENTRY(jint, JVM_GetClassMethodsCount(JNIEnv *env, jclass cls))
  2013   JVMWrapper("JVM_GetClassMethodsCount");
  2014   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2015   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2016   if (!Klass::cast(k)->oop_is_instance())
  2017     return 0;
  2018   return instanceKlass::cast(k)->methods()->length();
  2019 JVM_END
  2022 // The following methods, used for the verifier, are never called with
  2023 // array klasses, so a direct cast to instanceKlass is safe.
  2024 // Typically, these methods are called in a loop with bounds determined
  2025 // by the results of JVM_GetClass{Fields,Methods}Count, which return
  2026 // zero for arrays.
  2027 JVM_QUICK_ENTRY(void, JVM_GetMethodIxExceptionIndexes(JNIEnv *env, jclass cls, jint method_index, unsigned short *exceptions))
  2028   JVMWrapper("JVM_GetMethodIxExceptionIndexes");
  2029   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2030   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2031   oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
  2032   int length = methodOop(method)->checked_exceptions_length();
  2033   if (length > 0) {
  2034     CheckedExceptionElement* table= methodOop(method)->checked_exceptions_start();
  2035     for (int i = 0; i < length; i++) {
  2036       exceptions[i] = table[i].class_cp_index;
  2039 JVM_END
  2042 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxExceptionsCount(JNIEnv *env, jclass cls, jint method_index))
  2043   JVMWrapper("JVM_GetMethodIxExceptionsCount");
  2044   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2045   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2046   oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
  2047   return methodOop(method)->checked_exceptions_length();
  2048 JVM_END
  2051 JVM_QUICK_ENTRY(void, JVM_GetMethodIxByteCode(JNIEnv *env, jclass cls, jint method_index, unsigned char *code))
  2052   JVMWrapper("JVM_GetMethodIxByteCode");
  2053   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2054   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2055   oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
  2056   memcpy(code, methodOop(method)->code_base(), methodOop(method)->code_size());
  2057 JVM_END
  2060 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxByteCodeLength(JNIEnv *env, jclass cls, jint method_index))
  2061   JVMWrapper("JVM_GetMethodIxByteCodeLength");
  2062   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2063   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2064   oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
  2065   return methodOop(method)->code_size();
  2066 JVM_END
  2069 JVM_QUICK_ENTRY(void, JVM_GetMethodIxExceptionTableEntry(JNIEnv *env, jclass cls, jint method_index, jint entry_index, JVM_ExceptionTableEntryType *entry))
  2070   JVMWrapper("JVM_GetMethodIxExceptionTableEntry");
  2071   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2072   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2073   oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
  2074   typeArrayOop extable = methodOop(method)->exception_table();
  2075   entry->start_pc   = extable->int_at(entry_index * 4);
  2076   entry->end_pc     = extable->int_at(entry_index * 4 + 1);
  2077   entry->handler_pc = extable->int_at(entry_index * 4 + 2);
  2078   entry->catchType  = extable->int_at(entry_index * 4 + 3);
  2079 JVM_END
  2082 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index))
  2083   JVMWrapper("JVM_GetMethodIxExceptionTableLength");
  2084   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2085   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2086   oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
  2087   return methodOop(method)->exception_table()->length() / 4;
  2088 JVM_END
  2091 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index))
  2092   JVMWrapper("JVM_GetMethodIxModifiers");
  2093   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2094   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2095   oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
  2096   return methodOop(method)->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
  2097 JVM_END
  2100 JVM_QUICK_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index))
  2101   JVMWrapper("JVM_GetFieldIxModifiers");
  2102   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2103   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2104   typeArrayOop fields = instanceKlass::cast(k)->fields();
  2105   return fields->ushort_at(field_index * instanceKlass::next_offset + instanceKlass::access_flags_offset) & JVM_RECOGNIZED_FIELD_MODIFIERS;
  2106 JVM_END
  2109 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index))
  2110   JVMWrapper("JVM_GetMethodIxLocalsCount");
  2111   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2112   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2113   oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
  2114   return methodOop(method)->max_locals();
  2115 JVM_END
  2118 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
  2119   JVMWrapper("JVM_GetMethodIxArgsSize");
  2120   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2121   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2122   oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
  2123   return methodOop(method)->size_of_parameters();
  2124 JVM_END
  2127 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
  2128   JVMWrapper("JVM_GetMethodIxMaxStack");
  2129   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2130   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2131   oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
  2132   return methodOop(method)->max_stack();
  2133 JVM_END
  2136 JVM_QUICK_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
  2137   JVMWrapper("JVM_IsConstructorIx");
  2138   ResourceMark rm(THREAD);
  2139   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2140   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2141   oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
  2142   return methodOop(method)->name() == vmSymbols::object_initializer_name();
  2143 JVM_END
  2146 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
  2147   JVMWrapper("JVM_GetMethodIxIxUTF");
  2148   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2149   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2150   oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
  2151   return methodOop(method)->name()->as_utf8();
  2152 JVM_END
  2155 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
  2156   JVMWrapper("JVM_GetMethodIxSignatureUTF");
  2157   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2158   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2159   oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
  2160   return methodOop(method)->signature()->as_utf8();
  2161 JVM_END
  2163 /**
  2164  * All of these JVM_GetCP-xxx methods are used by the old verifier to
  2165  * read entries in the constant pool.  Since the old verifier always
  2166  * works on a copy of the code, it will not see any rewriting that
  2167  * may possibly occur in the middle of verification.  So it is important
  2168  * that nothing it calls tries to use the cpCache instead of the raw
  2169  * constant pool, so we must use cp->uncached_x methods when appropriate.
  2170  */
  2171 JVM_ENTRY(const char*, JVM_GetCPFieldNameUTF(JNIEnv *env, jclass cls, jint cp_index))
  2172   JVMWrapper("JVM_GetCPFieldNameUTF");
  2173   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2174   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2175   constantPoolOop cp = instanceKlass::cast(k)->constants();
  2176   switch (cp->tag_at(cp_index).value()) {
  2177     case JVM_CONSTANT_Fieldref:
  2178       return cp->uncached_name_ref_at(cp_index)->as_utf8();
  2179     default:
  2180       fatal("JVM_GetCPFieldNameUTF: illegal constant");
  2182   ShouldNotReachHere();
  2183   return NULL;
  2184 JVM_END
  2187 JVM_ENTRY(const char*, JVM_GetCPMethodNameUTF(JNIEnv *env, jclass cls, jint cp_index))
  2188   JVMWrapper("JVM_GetCPMethodNameUTF");
  2189   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2190   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2191   constantPoolOop cp = instanceKlass::cast(k)->constants();
  2192   switch (cp->tag_at(cp_index).value()) {
  2193     case JVM_CONSTANT_InterfaceMethodref:
  2194     case JVM_CONSTANT_Methodref:
  2195       return cp->uncached_name_ref_at(cp_index)->as_utf8();
  2196     default:
  2197       fatal("JVM_GetCPMethodNameUTF: illegal constant");
  2199   ShouldNotReachHere();
  2200   return NULL;
  2201 JVM_END
  2204 JVM_ENTRY(const char*, JVM_GetCPMethodSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
  2205   JVMWrapper("JVM_GetCPMethodSignatureUTF");
  2206   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2207   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2208   constantPoolOop cp = instanceKlass::cast(k)->constants();
  2209   switch (cp->tag_at(cp_index).value()) {
  2210     case JVM_CONSTANT_InterfaceMethodref:
  2211     case JVM_CONSTANT_Methodref:
  2212       return cp->uncached_signature_ref_at(cp_index)->as_utf8();
  2213     default:
  2214       fatal("JVM_GetCPMethodSignatureUTF: illegal constant");
  2216   ShouldNotReachHere();
  2217   return NULL;
  2218 JVM_END
  2221 JVM_ENTRY(const char*, JVM_GetCPFieldSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
  2222   JVMWrapper("JVM_GetCPFieldSignatureUTF");
  2223   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2224   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2225   constantPoolOop cp = instanceKlass::cast(k)->constants();
  2226   switch (cp->tag_at(cp_index).value()) {
  2227     case JVM_CONSTANT_Fieldref:
  2228       return cp->uncached_signature_ref_at(cp_index)->as_utf8();
  2229     default:
  2230       fatal("JVM_GetCPFieldSignatureUTF: illegal constant");
  2232   ShouldNotReachHere();
  2233   return NULL;
  2234 JVM_END
  2237 JVM_ENTRY(const char*, JVM_GetCPClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
  2238   JVMWrapper("JVM_GetCPClassNameUTF");
  2239   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2240   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2241   constantPoolOop cp = instanceKlass::cast(k)->constants();
  2242   symbolOop classname = cp->klass_name_at(cp_index);
  2243   return classname->as_utf8();
  2244 JVM_END
  2247 JVM_ENTRY(const char*, JVM_GetCPFieldClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
  2248   JVMWrapper("JVM_GetCPFieldClassNameUTF");
  2249   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2250   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2251   constantPoolOop cp = instanceKlass::cast(k)->constants();
  2252   switch (cp->tag_at(cp_index).value()) {
  2253     case JVM_CONSTANT_Fieldref: {
  2254       int class_index = cp->uncached_klass_ref_index_at(cp_index);
  2255       symbolOop classname = cp->klass_name_at(class_index);
  2256       return classname->as_utf8();
  2258     default:
  2259       fatal("JVM_GetCPFieldClassNameUTF: illegal constant");
  2261   ShouldNotReachHere();
  2262   return NULL;
  2263 JVM_END
  2266 JVM_ENTRY(const char*, JVM_GetCPMethodClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
  2267   JVMWrapper("JVM_GetCPMethodClassNameUTF");
  2268   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2269   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2270   constantPoolOop cp = instanceKlass::cast(k)->constants();
  2271   switch (cp->tag_at(cp_index).value()) {
  2272     case JVM_CONSTANT_Methodref:
  2273     case JVM_CONSTANT_InterfaceMethodref: {
  2274       int class_index = cp->uncached_klass_ref_index_at(cp_index);
  2275       symbolOop classname = cp->klass_name_at(class_index);
  2276       return classname->as_utf8();
  2278     default:
  2279       fatal("JVM_GetCPMethodClassNameUTF: illegal constant");
  2281   ShouldNotReachHere();
  2282   return NULL;
  2283 JVM_END
  2286 JVM_QUICK_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
  2287   JVMWrapper("JVM_GetCPFieldModifiers");
  2288   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2289   klassOop k_called = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(called_cls));
  2290   k        = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2291   k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
  2292   constantPoolOop cp = instanceKlass::cast(k)->constants();
  2293   constantPoolOop cp_called = instanceKlass::cast(k_called)->constants();
  2294   switch (cp->tag_at(cp_index).value()) {
  2295     case JVM_CONSTANT_Fieldref: {
  2296       symbolOop name      = cp->uncached_name_ref_at(cp_index);
  2297       symbolOop signature = cp->uncached_signature_ref_at(cp_index);
  2298       typeArrayOop fields = instanceKlass::cast(k_called)->fields();
  2299       int fields_count = fields->length();
  2300       for (int i = 0; i < fields_count; i += instanceKlass::next_offset) {
  2301         if (cp_called->symbol_at(fields->ushort_at(i + instanceKlass::name_index_offset)) == name &&
  2302             cp_called->symbol_at(fields->ushort_at(i + instanceKlass::signature_index_offset)) == signature) {
  2303           return fields->ushort_at(i + instanceKlass::access_flags_offset) & JVM_RECOGNIZED_FIELD_MODIFIERS;
  2306       return -1;
  2308     default:
  2309       fatal("JVM_GetCPFieldModifiers: illegal constant");
  2311   ShouldNotReachHere();
  2312   return 0;
  2313 JVM_END
  2316 JVM_QUICK_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
  2317   JVMWrapper("JVM_GetCPMethodModifiers");
  2318   klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
  2319   klassOop k_called = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(called_cls));
  2320   k        = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
  2321   k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
  2322   constantPoolOop cp = instanceKlass::cast(k)->constants();
  2323   switch (cp->tag_at(cp_index).value()) {
  2324     case JVM_CONSTANT_Methodref:
  2325     case JVM_CONSTANT_InterfaceMethodref: {
  2326       symbolOop name      = cp->uncached_name_ref_at(cp_index);
  2327       symbolOop signature = cp->uncached_signature_ref_at(cp_index);
  2328       objArrayOop methods = instanceKlass::cast(k_called)->methods();
  2329       int methods_count = methods->length();
  2330       for (int i = 0; i < methods_count; i++) {
  2331         methodOop method = methodOop(methods->obj_at(i));
  2332         if (method->name() == name && method->signature() == signature) {
  2333             return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
  2336       return -1;
  2338     default:
  2339       fatal("JVM_GetCPMethodModifiers: illegal constant");
  2341   ShouldNotReachHere();
  2342   return 0;
  2343 JVM_END
  2346 // Misc //////////////////////////////////////////////////////////////////////////////////////////////
  2348 JVM_LEAF(void, JVM_ReleaseUTF(const char *utf))
  2349   // So long as UTF8::convert_to_utf8 returns resource strings, we don't have to do anything
  2350 JVM_END
  2353 JVM_ENTRY(jboolean, JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2))
  2354   JVMWrapper("JVM_IsSameClassPackage");
  2355   oop class1_mirror = JNIHandles::resolve_non_null(class1);
  2356   oop class2_mirror = JNIHandles::resolve_non_null(class2);
  2357   klassOop klass1 = java_lang_Class::as_klassOop(class1_mirror);
  2358   klassOop klass2 = java_lang_Class::as_klassOop(class2_mirror);
  2359   return (jboolean) Reflection::is_same_class_package(klass1, klass2);
  2360 JVM_END
  2363 // IO functions ////////////////////////////////////////////////////////////////////////////////////////
  2365 JVM_LEAF(jint, JVM_Open(const char *fname, jint flags, jint mode))
  2366   JVMWrapper2("JVM_Open (%s)", fname);
  2368   //%note jvm_r6
  2369   int result = hpi::open(fname, flags, mode);
  2370   if (result >= 0) {
  2371     return result;
  2372   } else {
  2373     switch(errno) {
  2374       case EEXIST:
  2375         return JVM_EEXIST;
  2376       default:
  2377         return -1;
  2380 JVM_END
  2383 JVM_LEAF(jint, JVM_Close(jint fd))
  2384   JVMWrapper2("JVM_Close (0x%x)", fd);
  2385   //%note jvm_r6
  2386   return hpi::close(fd);
  2387 JVM_END
  2390 JVM_LEAF(jint, JVM_Read(jint fd, char *buf, jint nbytes))
  2391   JVMWrapper2("JVM_Read (0x%x)", fd);
  2393   //%note jvm_r6
  2394   return (jint)hpi::read(fd, buf, nbytes);
  2395 JVM_END
  2398 JVM_LEAF(jint, JVM_Write(jint fd, char *buf, jint nbytes))
  2399   JVMWrapper2("JVM_Write (0x%x)", fd);
  2401   //%note jvm_r6
  2402   return (jint)hpi::write(fd, buf, nbytes);
  2403 JVM_END
  2406 JVM_LEAF(jint, JVM_Available(jint fd, jlong *pbytes))
  2407   JVMWrapper2("JVM_Available (0x%x)", fd);
  2408   //%note jvm_r6
  2409   return hpi::available(fd, pbytes);
  2410 JVM_END
  2413 JVM_LEAF(jlong, JVM_Lseek(jint fd, jlong offset, jint whence))
  2414   JVMWrapper4("JVM_Lseek (0x%x, %Ld, %d)", fd, offset, whence);
  2415   //%note jvm_r6
  2416   return hpi::lseek(fd, offset, whence);
  2417 JVM_END
  2420 JVM_LEAF(jint, JVM_SetLength(jint fd, jlong length))
  2421   JVMWrapper3("JVM_SetLength (0x%x, %Ld)", fd, length);
  2422   return hpi::ftruncate(fd, length);
  2423 JVM_END
  2426 JVM_LEAF(jint, JVM_Sync(jint fd))
  2427   JVMWrapper2("JVM_Sync (0x%x)", fd);
  2428   //%note jvm_r6
  2429   return hpi::fsync(fd);
  2430 JVM_END
  2433 // Printing support //////////////////////////////////////////////////
  2434 extern "C" {
  2436 int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) {
  2437   // see bug 4399518, 4417214
  2438   if ((intptr_t)count <= 0) return -1;
  2439   return vsnprintf(str, count, fmt, args);
  2443 int jio_snprintf(char *str, size_t count, const char *fmt, ...) {
  2444   va_list args;
  2445   int len;
  2446   va_start(args, fmt);
  2447   len = jio_vsnprintf(str, count, fmt, args);
  2448   va_end(args);
  2449   return len;
  2453 int jio_fprintf(FILE* f, const char *fmt, ...) {
  2454   int len;
  2455   va_list args;
  2456   va_start(args, fmt);
  2457   len = jio_vfprintf(f, fmt, args);
  2458   va_end(args);
  2459   return len;
  2463 int jio_vfprintf(FILE* f, const char *fmt, va_list args) {
  2464   if (Arguments::vfprintf_hook() != NULL) {
  2465      return Arguments::vfprintf_hook()(f, fmt, args);
  2466   } else {
  2467     return vfprintf(f, fmt, args);
  2472 int jio_printf(const char *fmt, ...) {
  2473   int len;
  2474   va_list args;
  2475   va_start(args, fmt);
  2476   len = jio_vfprintf(defaultStream::output_stream(), fmt, args);
  2477   va_end(args);
  2478   return len;
  2482 // HotSpot specific jio method
  2483 void jio_print(const char* s) {
  2484   // Try to make this function as atomic as possible.
  2485   if (Arguments::vfprintf_hook() != NULL) {
  2486     jio_fprintf(defaultStream::output_stream(), "%s", s);
  2487   } else {
  2488     // Make an unused local variable to avoid warning from gcc 4.x compiler.
  2489     size_t count = ::write(defaultStream::output_fd(), s, (int)strlen(s));
  2493 } // Extern C
  2495 // java.lang.Thread //////////////////////////////////////////////////////////////////////////////
  2497 // In most of the JVM Thread support functions we need to be sure to lock the Threads_lock
  2498 // to prevent the target thread from exiting after we have a pointer to the C++ Thread or
  2499 // OSThread objects.  The exception to this rule is when the target object is the thread
  2500 // doing the operation, in which case we know that the thread won't exit until the
  2501 // operation is done (all exits being voluntary).  There are a few cases where it is
  2502 // rather silly to do operations on yourself, like resuming yourself or asking whether
  2503 // you are alive.  While these can still happen, they are not subject to deadlocks if
  2504 // the lock is held while the operation occurs (this is not the case for suspend, for
  2505 // instance), and are very unlikely.  Because IsAlive needs to be fast and its
  2506 // implementation is local to this file, we always lock Threads_lock for that one.
  2508 static void thread_entry(JavaThread* thread, TRAPS) {
  2509   HandleMark hm(THREAD);
  2510   Handle obj(THREAD, thread->threadObj());
  2511   JavaValue result(T_VOID);
  2512   JavaCalls::call_virtual(&result,
  2513                           obj,
  2514                           KlassHandle(THREAD, SystemDictionary::thread_klass()),
  2515                           vmSymbolHandles::run_method_name(),
  2516                           vmSymbolHandles::void_method_signature(),
  2517                           THREAD);
  2521 JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
  2522   JVMWrapper("JVM_StartThread");
  2523   JavaThread *native_thread = NULL;
  2525   // We cannot hold the Threads_lock when we throw an exception,
  2526   // due to rank ordering issues. Example:  we might need to grab the
  2527   // Heap_lock while we construct the exception.
  2528   bool throw_illegal_thread_state = false;
  2530   // We must release the Threads_lock before we can post a jvmti event
  2531   // in Thread::start.
  2533     // Ensure that the C++ Thread and OSThread structures aren't freed before
  2534     // we operate.
  2535     MutexLocker mu(Threads_lock);
  2537     // Check to see if we're running a thread that's already exited or was
  2538     // stopped (is_stillborn) or is still active (thread is not NULL).
  2539     if (java_lang_Thread::is_stillborn(JNIHandles::resolve_non_null(jthread)) ||
  2540         java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) {
  2541         throw_illegal_thread_state = true;
  2542     } else {
  2543       jlong size =
  2544              java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread));
  2545       // Allocate the C++ Thread structure and create the native thread.  The
  2546       // stack size retrieved from java is signed, but the constructor takes
  2547       // size_t (an unsigned type), so avoid passing negative values which would
  2548       // result in really large stacks.
  2549       size_t sz = size > 0 ? (size_t) size : 0;
  2550       native_thread = new JavaThread(&thread_entry, sz);
  2552       // At this point it may be possible that no osthread was created for the
  2553       // JavaThread due to lack of memory. Check for this situation and throw
  2554       // an exception if necessary. Eventually we may want to change this so
  2555       // that we only grab the lock if the thread was created successfully -
  2556       // then we can also do this check and throw the exception in the
  2557       // JavaThread constructor.
  2558       if (native_thread->osthread() != NULL) {
  2559         // Note: the current thread is not being used within "prepare".
  2560         native_thread->prepare(jthread);
  2565   if (throw_illegal_thread_state) {
  2566     THROW(vmSymbols::java_lang_IllegalThreadStateException());
  2569   assert(native_thread != NULL, "Starting null thread?");
  2571   if (native_thread->osthread() == NULL) {
  2572     // No one should hold a reference to the 'native_thread'.
  2573     delete native_thread;
  2574     if (JvmtiExport::should_post_resource_exhausted()) {
  2575       JvmtiExport::post_resource_exhausted(
  2576         JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS,
  2577         "unable to create new native thread");
  2579     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
  2580               "unable to create new native thread");
  2583   Thread::start(native_thread);
  2585 JVM_END
  2587 // JVM_Stop is implemented using a VM_Operation, so threads are forced to safepoints
  2588 // before the quasi-asynchronous exception is delivered.  This is a little obtrusive,
  2589 // but is thought to be reliable and simple. In the case, where the receiver is the
  2590 // save thread as the sender, no safepoint is needed.
  2591 JVM_ENTRY(void, JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable))
  2592   JVMWrapper("JVM_StopThread");
  2594   oop java_throwable = JNIHandles::resolve(throwable);
  2595   if (java_throwable == NULL) {
  2596     THROW(vmSymbols::java_lang_NullPointerException());
  2598   oop java_thread = JNIHandles::resolve_non_null(jthread);
  2599   JavaThread* receiver = java_lang_Thread::thread(java_thread);
  2600   Events::log("JVM_StopThread thread JavaThread " INTPTR_FORMAT " as oop " INTPTR_FORMAT " [exception " INTPTR_FORMAT "]", receiver, (address)java_thread, throwable);
  2601   // First check if thread already exited
  2602   if (receiver != NULL) {
  2603     // Check if exception is getting thrown at self (use oop equality, since the
  2604     // target object might exit)
  2605     if (java_thread == thread->threadObj()) {
  2606       // This is a change from JDK 1.1, but JDK 1.2 will also do it:
  2607       // NOTE (from JDK 1.2): this is done solely to prevent stopped
  2608       // threads from being restarted.
  2609       // Fix for 4314342, 4145910, perhaps others: it now doesn't have
  2610       // any effect on the "liveness" of a thread; see
  2611       // JVM_IsThreadAlive, below.
  2612       if (java_throwable->is_a(SystemDictionary::threaddeath_klass())) {
  2613         java_lang_Thread::set_stillborn(java_thread);
  2615       THROW_OOP(java_throwable);
  2616     } else {
  2617       // Enques a VM_Operation to stop all threads and then deliver the exception...
  2618       Thread::send_async_exception(java_thread, JNIHandles::resolve(throwable));
  2621 JVM_END
  2624 JVM_ENTRY(jboolean, JVM_IsThreadAlive(JNIEnv* env, jobject jthread))
  2625   JVMWrapper("JVM_IsThreadAlive");
  2627   oop thread_oop = JNIHandles::resolve_non_null(jthread);
  2628   return java_lang_Thread::is_alive(thread_oop);
  2629 JVM_END
  2632 JVM_ENTRY(void, JVM_SuspendThread(JNIEnv* env, jobject jthread))
  2633   JVMWrapper("JVM_SuspendThread");
  2634   oop java_thread = JNIHandles::resolve_non_null(jthread);
  2635   JavaThread* receiver = java_lang_Thread::thread(java_thread);
  2637   if (receiver != NULL) {
  2638     // thread has run and has not exited (still on threads list)
  2641       MutexLockerEx ml(receiver->SR_lock(), Mutex::_no_safepoint_check_flag);
  2642       if (receiver->is_external_suspend()) {
  2643         // Don't allow nested external suspend requests. We can't return
  2644         // an error from this interface so just ignore the problem.
  2645         return;
  2647       if (receiver->is_exiting()) { // thread is in the process of exiting
  2648         return;
  2650       receiver->set_external_suspend();
  2653     // java_suspend() will catch threads in the process of exiting
  2654     // and will ignore them.
  2655     receiver->java_suspend();
  2657     // It would be nice to have the following assertion in all the
  2658     // time, but it is possible for a racing resume request to have
  2659     // resumed this thread right after we suspended it. Temporarily
  2660     // enable this assertion if you are chasing a different kind of
  2661     // bug.
  2662     //
  2663     // assert(java_lang_Thread::thread(receiver->threadObj()) == NULL ||
  2664     //   receiver->is_being_ext_suspended(), "thread is not suspended");
  2666 JVM_END
  2669 JVM_ENTRY(void, JVM_ResumeThread(JNIEnv* env, jobject jthread))
  2670   JVMWrapper("JVM_ResumeThread");
  2671   // Ensure that the C++ Thread and OSThread structures aren't freed before we operate.
  2672   // We need to *always* get the threads lock here, since this operation cannot be allowed during
  2673   // a safepoint. The safepoint code relies on suspending a thread to examine its state. If other
  2674   // threads randomly resumes threads, then a thread might not be suspended when the safepoint code
  2675   // looks at it.
  2676   MutexLocker ml(Threads_lock);
  2677   JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
  2678   if (thr != NULL) {
  2679     // the thread has run and is not in the process of exiting
  2680     thr->java_resume();
  2682 JVM_END
  2685 JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio))
  2686   JVMWrapper("JVM_SetThreadPriority");
  2687   // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
  2688   MutexLocker ml(Threads_lock);
  2689   oop java_thread = JNIHandles::resolve_non_null(jthread);
  2690   java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio);
  2691   JavaThread* thr = java_lang_Thread::thread(java_thread);
  2692   if (thr != NULL) {                  // Thread not yet started; priority pushed down when it is
  2693     Thread::set_priority(thr, (ThreadPriority)prio);
  2695 JVM_END
  2698 JVM_ENTRY(void, JVM_Yield(JNIEnv *env, jclass threadClass))
  2699   JVMWrapper("JVM_Yield");
  2700   if (os::dont_yield()) return;
  2701   // When ConvertYieldToSleep is off (default), this matches the classic VM use of yield.
  2702   // Critical for similar threading behaviour
  2703   if (ConvertYieldToSleep) {
  2704     os::sleep(thread, MinSleepInterval, false);
  2705   } else {
  2706     os::yield();
  2708 JVM_END
  2711 JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis))
  2712   JVMWrapper("JVM_Sleep");
  2714   if (millis < 0) {
  2715     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
  2718   if (Thread::is_interrupted (THREAD, true) && !HAS_PENDING_EXCEPTION) {
  2719     THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
  2722   // Save current thread state and restore it at the end of this block.
  2723   // And set new thread state to SLEEPING.
  2724   JavaThreadSleepState jtss(thread);
  2726   if (millis == 0) {
  2727     // When ConvertSleepToYield is on, this matches the classic VM implementation of
  2728     // JVM_Sleep. Critical for similar threading behaviour (Win32)
  2729     // It appears that in certain GUI contexts, it may be beneficial to do a short sleep
  2730     // for SOLARIS
  2731     if (ConvertSleepToYield) {
  2732       os::yield();
  2733     } else {
  2734       ThreadState old_state = thread->osthread()->get_state();
  2735       thread->osthread()->set_state(SLEEPING);
  2736       os::sleep(thread, MinSleepInterval, false);
  2737       thread->osthread()->set_state(old_state);
  2739   } else {
  2740     ThreadState old_state = thread->osthread()->get_state();
  2741     thread->osthread()->set_state(SLEEPING);
  2742     if (os::sleep(thread, millis, true) == OS_INTRPT) {
  2743       // An asynchronous exception (e.g., ThreadDeathException) could have been thrown on
  2744       // us while we were sleeping. We do not overwrite those.
  2745       if (!HAS_PENDING_EXCEPTION) {
  2746         // TODO-FIXME: THROW_MSG returns which means we will not call set_state()
  2747         // to properly restore the thread state.  That's likely wrong.
  2748         THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
  2751     thread->osthread()->set_state(old_state);
  2753 JVM_END
  2755 JVM_ENTRY(jobject, JVM_CurrentThread(JNIEnv* env, jclass threadClass))
  2756   JVMWrapper("JVM_CurrentThread");
  2757   oop jthread = thread->threadObj();
  2758   assert (thread != NULL, "no current thread!");
  2759   return JNIHandles::make_local(env, jthread);
  2760 JVM_END
  2763 JVM_ENTRY(jint, JVM_CountStackFrames(JNIEnv* env, jobject jthread))
  2764   JVMWrapper("JVM_CountStackFrames");
  2766   // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
  2767   oop java_thread = JNIHandles::resolve_non_null(jthread);
  2768   bool throw_illegal_thread_state = false;
  2769   int count = 0;
  2772     MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
  2773     // We need to re-resolve the java_thread, since a GC might have happened during the
  2774     // acquire of the lock
  2775     JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
  2777     if (thr == NULL) {
  2778       // do nothing
  2779     } else if(! thr->is_external_suspend() || ! thr->frame_anchor()->walkable()) {
  2780       // Check whether this java thread has been suspended already. If not, throws
  2781       // IllegalThreadStateException. We defer to throw that exception until
  2782       // Threads_lock is released since loading exception class has to leave VM.
  2783       // The correct way to test a thread is actually suspended is
  2784       // wait_for_ext_suspend_completion(), but we can't call that while holding
  2785       // the Threads_lock. The above tests are sufficient for our purposes
  2786       // provided the walkability of the stack is stable - which it isn't
  2787       // 100% but close enough for most practical purposes.
  2788       throw_illegal_thread_state = true;
  2789     } else {
  2790       // Count all java activation, i.e., number of vframes
  2791       for(vframeStream vfst(thr); !vfst.at_end(); vfst.next()) {
  2792         // Native frames are not counted
  2793         if (!vfst.method()->is_native()) count++;
  2798   if (throw_illegal_thread_state) {
  2799     THROW_MSG_0(vmSymbols::java_lang_IllegalThreadStateException(),
  2800                 "this thread is not suspended");
  2802   return count;
  2803 JVM_END
  2805 // Consider: A better way to implement JVM_Interrupt() is to acquire
  2806 // Threads_lock to resolve the jthread into a Thread pointer, fetch
  2807 // Thread->platformevent, Thread->native_thr, Thread->parker, etc.,
  2808 // drop Threads_lock, and the perform the unpark() and thr_kill() operations
  2809 // outside the critical section.  Threads_lock is hot so we want to minimize
  2810 // the hold-time.  A cleaner interface would be to decompose interrupt into
  2811 // two steps.  The 1st phase, performed under Threads_lock, would return
  2812 // a closure that'd be invoked after Threads_lock was dropped.
  2813 // This tactic is safe as PlatformEvent and Parkers are type-stable (TSM) and
  2814 // admit spurious wakeups.
  2816 JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
  2817   JVMWrapper("JVM_Interrupt");
  2819   // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
  2820   oop java_thread = JNIHandles::resolve_non_null(jthread);
  2821   MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
  2822   // We need to re-resolve the java_thread, since a GC might have happened during the
  2823   // acquire of the lock
  2824   JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
  2825   if (thr != NULL) {
  2826     Thread::interrupt(thr);
  2828 JVM_END
  2831 JVM_QUICK_ENTRY(jboolean, JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrupted))
  2832   JVMWrapper("JVM_IsInterrupted");
  2834   // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
  2835   oop java_thread = JNIHandles::resolve_non_null(jthread);
  2836   MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
  2837   // We need to re-resolve the java_thread, since a GC might have happened during the
  2838   // acquire of the lock
  2839   JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
  2840   if (thr == NULL) {
  2841     return JNI_FALSE;
  2842   } else {
  2843     return (jboolean) Thread::is_interrupted(thr, clear_interrupted != 0);
  2845 JVM_END
  2848 // Return true iff the current thread has locked the object passed in
  2850 JVM_ENTRY(jboolean, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj))
  2851   JVMWrapper("JVM_HoldsLock");
  2852   assert(THREAD->is_Java_thread(), "sanity check");
  2853   if (obj == NULL) {
  2854     THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
  2856   Handle h_obj(THREAD, JNIHandles::resolve(obj));
  2857   return ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, h_obj);
  2858 JVM_END
  2861 JVM_ENTRY(void, JVM_DumpAllStacks(JNIEnv* env, jclass))
  2862   JVMWrapper("JVM_DumpAllStacks");
  2863   VM_PrintThreads op;
  2864   VMThread::execute(&op);
  2865   if (JvmtiExport::should_post_data_dump()) {
  2866     JvmtiExport::post_data_dump();
  2868 JVM_END
  2871 // java.lang.SecurityManager ///////////////////////////////////////////////////////////////////////
  2873 static bool is_trusted_frame(JavaThread* jthread, vframeStream* vfst) {
  2874   assert(jthread->is_Java_thread(), "must be a Java thread");
  2875   if (jthread->privileged_stack_top() == NULL) return false;
  2876   if (jthread->privileged_stack_top()->frame_id() == vfst->frame_id()) {
  2877     oop loader = jthread->privileged_stack_top()->class_loader();
  2878     if (loader == NULL) return true;
  2879     bool trusted = java_lang_ClassLoader::is_trusted_loader(loader);
  2880     if (trusted) return true;
  2882   return false;
  2885 JVM_ENTRY(jclass, JVM_CurrentLoadedClass(JNIEnv *env))
  2886   JVMWrapper("JVM_CurrentLoadedClass");
  2887   ResourceMark rm(THREAD);
  2889   for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
  2890     // if a method in a class in a trusted loader is in a doPrivileged, return NULL
  2891     bool trusted = is_trusted_frame(thread, &vfst);
  2892     if (trusted) return NULL;
  2894     methodOop m = vfst.method();
  2895     if (!m->is_native()) {
  2896       klassOop holder = m->method_holder();
  2897       oop      loader = instanceKlass::cast(holder)->class_loader();
  2898       if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) {
  2899         return (jclass) JNIHandles::make_local(env, Klass::cast(holder)->java_mirror());
  2903   return NULL;
  2904 JVM_END
  2907 JVM_ENTRY(jobject, JVM_CurrentClassLoader(JNIEnv *env))
  2908   JVMWrapper("JVM_CurrentClassLoader");
  2909   ResourceMark rm(THREAD);
  2911   for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
  2913     // if a method in a class in a trusted loader is in a doPrivileged, return NULL
  2914     bool trusted = is_trusted_frame(thread, &vfst);
  2915     if (trusted) return NULL;
  2917     methodOop m = vfst.method();
  2918     if (!m->is_native()) {
  2919       klassOop holder = m->method_holder();
  2920       assert(holder->is_klass(), "just checking");
  2921       oop loader = instanceKlass::cast(holder)->class_loader();
  2922       if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) {
  2923         return JNIHandles::make_local(env, loader);
  2927   return NULL;
  2928 JVM_END
  2931 // Utility object for collecting method holders walking down the stack
  2932 class KlassLink: public ResourceObj {
  2933  public:
  2934   KlassHandle klass;
  2935   KlassLink*  next;
  2937   KlassLink(KlassHandle k) { klass = k; next = NULL; }
  2938 };
  2941 JVM_ENTRY(jobjectArray, JVM_GetClassContext(JNIEnv *env))
  2942   JVMWrapper("JVM_GetClassContext");
  2943   ResourceMark rm(THREAD);
  2944   JvmtiVMObjectAllocEventCollector oam;
  2945   // Collect linked list of (handles to) method holders
  2946   KlassLink* first = NULL;
  2947   KlassLink* last  = NULL;
  2948   int depth = 0;
  2950   for(vframeStream vfst(thread); !vfst.at_end(); vfst.security_get_caller_frame(1)) {
  2951     // Native frames are not returned
  2952     if (!vfst.method()->is_native()) {
  2953       klassOop holder = vfst.method()->method_holder();
  2954       assert(holder->is_klass(), "just checking");
  2955       depth++;
  2956       KlassLink* l = new KlassLink(KlassHandle(thread, holder));
  2957       if (first == NULL) {
  2958         first = last = l;
  2959       } else {
  2960         last->next = l;
  2961         last = l;
  2966   // Create result array of type [Ljava/lang/Class;
  2967   objArrayOop result = oopFactory::new_objArray(SystemDictionary::class_klass(), depth, CHECK_NULL);
  2968   // Fill in mirrors corresponding to method holders
  2969   int index = 0;
  2970   while (first != NULL) {
  2971     result->obj_at_put(index++, Klass::cast(first->klass())->java_mirror());
  2972     first = first->next;
  2974   assert(index == depth, "just checking");
  2976   return (jobjectArray) JNIHandles::make_local(env, result);
  2977 JVM_END
  2980 JVM_ENTRY(jint, JVM_ClassDepth(JNIEnv *env, jstring name))
  2981   JVMWrapper("JVM_ClassDepth");
  2982   ResourceMark rm(THREAD);
  2983   Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
  2984   Handle class_name_str = java_lang_String::internalize_classname(h_name, CHECK_0);
  2986   const char* str = java_lang_String::as_utf8_string(class_name_str());
  2987   symbolHandle class_name_sym =
  2988                 symbolHandle(THREAD, SymbolTable::probe(str, (int)strlen(str)));
  2989   if (class_name_sym.is_null()) {
  2990     return -1;
  2993   int depth = 0;
  2995   for(vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
  2996     if (!vfst.method()->is_native()) {
  2997       klassOop holder = vfst.method()->method_holder();
  2998       assert(holder->is_klass(), "just checking");
  2999       if (instanceKlass::cast(holder)->name() == class_name_sym()) {
  3000         return depth;
  3002       depth++;
  3005   return -1;
  3006 JVM_END
  3009 JVM_ENTRY(jint, JVM_ClassLoaderDepth(JNIEnv *env))
  3010   JVMWrapper("JVM_ClassLoaderDepth");
  3011   ResourceMark rm(THREAD);
  3012   int depth = 0;
  3013   for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
  3014     // if a method in a class in a trusted loader is in a doPrivileged, return -1
  3015     bool trusted = is_trusted_frame(thread, &vfst);
  3016     if (trusted) return -1;
  3018     methodOop m = vfst.method();
  3019     if (!m->is_native()) {
  3020       klassOop holder = m->method_holder();
  3021       assert(holder->is_klass(), "just checking");
  3022       oop loader = instanceKlass::cast(holder)->class_loader();
  3023       if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) {
  3024         return depth;
  3026       depth++;
  3029   return -1;
  3030 JVM_END
  3033 // java.lang.Package ////////////////////////////////////////////////////////////////
  3036 JVM_ENTRY(jstring, JVM_GetSystemPackage(JNIEnv *env, jstring name))
  3037   JVMWrapper("JVM_GetSystemPackage");
  3038   ResourceMark rm(THREAD);
  3039   JvmtiVMObjectAllocEventCollector oam;
  3040   char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
  3041   oop result = ClassLoader::get_system_package(str, CHECK_NULL);
  3042   return (jstring) JNIHandles::make_local(result);
  3043 JVM_END
  3046 JVM_ENTRY(jobjectArray, JVM_GetSystemPackages(JNIEnv *env))
  3047   JVMWrapper("JVM_GetSystemPackages");
  3048   JvmtiVMObjectAllocEventCollector oam;
  3049   objArrayOop result = ClassLoader::get_system_packages(CHECK_NULL);
  3050   return (jobjectArray) JNIHandles::make_local(result);
  3051 JVM_END
  3054 // ObjectInputStream ///////////////////////////////////////////////////////////////
  3056 bool force_verify_field_access(klassOop current_class, klassOop field_class, AccessFlags access, bool classloader_only) {
  3057   if (current_class == NULL) {
  3058     return true;
  3060   if ((current_class == field_class) || access.is_public()) {
  3061     return true;
  3064   if (access.is_protected()) {
  3065     // See if current_class is a subclass of field_class
  3066     if (Klass::cast(current_class)->is_subclass_of(field_class)) {
  3067       return true;
  3071   return (!access.is_private() && instanceKlass::cast(current_class)->is_same_class_package(field_class));
  3075 // JVM_AllocateNewObject and JVM_AllocateNewArray are unused as of 1.4
  3076 JVM_ENTRY(jobject, JVM_AllocateNewObject(JNIEnv *env, jobject receiver, jclass currClass, jclass initClass))
  3077   JVMWrapper("JVM_AllocateNewObject");
  3078   JvmtiVMObjectAllocEventCollector oam;
  3079   // Receiver is not used
  3080   oop curr_mirror = JNIHandles::resolve_non_null(currClass);
  3081   oop init_mirror = JNIHandles::resolve_non_null(initClass);
  3083   // Cannot instantiate primitive types
  3084   if (java_lang_Class::is_primitive(curr_mirror) || java_lang_Class::is_primitive(init_mirror)) {
  3085     ResourceMark rm(THREAD);
  3086     THROW_0(vmSymbols::java_lang_InvalidClassException());
  3089   // Arrays not allowed here, must use JVM_AllocateNewArray
  3090   if (Klass::cast(java_lang_Class::as_klassOop(curr_mirror))->oop_is_javaArray() ||
  3091       Klass::cast(java_lang_Class::as_klassOop(init_mirror))->oop_is_javaArray()) {
  3092     ResourceMark rm(THREAD);
  3093     THROW_0(vmSymbols::java_lang_InvalidClassException());
  3096   instanceKlassHandle curr_klass (THREAD, java_lang_Class::as_klassOop(curr_mirror));
  3097   instanceKlassHandle init_klass (THREAD, java_lang_Class::as_klassOop(init_mirror));
  3099   assert(curr_klass->is_subclass_of(init_klass()), "just checking");
  3101   // Interfaces, abstract classes, and java.lang.Class classes cannot be instantiated directly.
  3102   curr_klass->check_valid_for_instantiation(false, CHECK_NULL);
  3104   // Make sure klass is initialized, since we are about to instantiate one of them.
  3105   curr_klass->initialize(CHECK_NULL);
  3107  methodHandle m (THREAD,
  3108                  init_klass->find_method(vmSymbols::object_initializer_name(),
  3109                                          vmSymbols::void_method_signature()));
  3110   if (m.is_null()) {
  3111     ResourceMark rm(THREAD);
  3112     THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(),
  3113                 methodOopDesc::name_and_sig_as_C_string(Klass::cast(init_klass()),
  3114                                           vmSymbols::object_initializer_name(),
  3115                                           vmSymbols::void_method_signature()));
  3118   if (curr_klass ==  init_klass && !m->is_public()) {
  3119     // Calling the constructor for class 'curr_klass'.
  3120     // Only allow calls to a public no-arg constructor.
  3121     // This path corresponds to creating an Externalizable object.
  3122     THROW_0(vmSymbols::java_lang_IllegalAccessException());
  3125   if (!force_verify_field_access(curr_klass(), init_klass(), m->access_flags(), false)) {
  3126     // subclass 'curr_klass' does not have access to no-arg constructor of 'initcb'
  3127     THROW_0(vmSymbols::java_lang_IllegalAccessException());
  3130   Handle obj = curr_klass->allocate_instance_handle(CHECK_NULL);
  3131   // Call constructor m. This might call a constructor higher up in the hierachy
  3132   JavaCalls::call_default_constructor(thread, m, obj, CHECK_NULL);
  3134   return JNIHandles::make_local(obj());
  3135 JVM_END
  3138 JVM_ENTRY(jobject, JVM_AllocateNewArray(JNIEnv *env, jobject obj, jclass currClass, jint length))
  3139   JVMWrapper("JVM_AllocateNewArray");
  3140   JvmtiVMObjectAllocEventCollector oam;
  3141   oop mirror = JNIHandles::resolve_non_null(currClass);
  3143   if (java_lang_Class::is_primitive(mirror)) {
  3144     THROW_0(vmSymbols::java_lang_InvalidClassException());
  3146   klassOop k = java_lang_Class::as_klassOop(mirror);
  3147   oop result;
  3149   if (k->klass_part()->oop_is_typeArray()) {
  3150     // typeArray
  3151     result = typeArrayKlass::cast(k)->allocate(length, CHECK_NULL);
  3152   } else if (k->klass_part()->oop_is_objArray()) {
  3153     // objArray
  3154     objArrayKlassHandle oak(THREAD, k);
  3155     oak->initialize(CHECK_NULL); // make sure class is initialized (matches Classic VM behavior)
  3156     result = oak->allocate(length, CHECK_NULL);
  3157   } else {
  3158     THROW_0(vmSymbols::java_lang_InvalidClassException());
  3160   return JNIHandles::make_local(env, result);
  3161 JVM_END
  3164 // Return the first non-null class loader up the execution stack, or null
  3165 // if only code from the null class loader is on the stack.
  3167 JVM_ENTRY(jobject, JVM_LatestUserDefinedLoader(JNIEnv *env))
  3168   for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
  3169     // UseNewReflection
  3170     vfst.skip_reflection_related_frames(); // Only needed for 1.4 reflection
  3171     klassOop holder = vfst.method()->method_holder();
  3172     oop loader = instanceKlass::cast(holder)->class_loader();
  3173     if (loader != NULL) {
  3174       return JNIHandles::make_local(env, loader);
  3177   return NULL;
  3178 JVM_END
  3181 // Load a class relative to the most recent class on the stack  with a non-null
  3182 // classloader.
  3183 // This function has been deprecated and should not be considered part of the
  3184 // specified JVM interface.
  3186 JVM_ENTRY(jclass, JVM_LoadClass0(JNIEnv *env, jobject receiver,
  3187                                  jclass currClass, jstring currClassName))
  3188   JVMWrapper("JVM_LoadClass0");
  3189   // Receiver is not used
  3190   ResourceMark rm(THREAD);
  3192   // Class name argument is not guaranteed to be in internal format
  3193   Handle classname (THREAD, JNIHandles::resolve_non_null(currClassName));
  3194   Handle string = java_lang_String::internalize_classname(classname, CHECK_NULL);
  3196   const char* str = java_lang_String::as_utf8_string(string());
  3198   if (str == NULL || (int)strlen(str) > symbolOopDesc::max_length()) {
  3199     // It's impossible to create this class;  the name cannot fit
  3200     // into the constant pool.
  3201     THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), str);
  3204   symbolHandle name = oopFactory::new_symbol_handle(str, CHECK_NULL);
  3205   Handle curr_klass (THREAD, JNIHandles::resolve(currClass));
  3206   // Find the most recent class on the stack with a non-null classloader
  3207   oop loader = NULL;
  3208   oop protection_domain = NULL;
  3209   if (curr_klass.is_null()) {
  3210     for (vframeStream vfst(thread);
  3211          !vfst.at_end() && loader == NULL;
  3212          vfst.next()) {
  3213       if (!vfst.method()->is_native()) {
  3214         klassOop holder = vfst.method()->method_holder();
  3215         loader             = instanceKlass::cast(holder)->class_loader();
  3216         protection_domain  = instanceKlass::cast(holder)->protection_domain();
  3219   } else {
  3220     klassOop curr_klass_oop = java_lang_Class::as_klassOop(curr_klass());
  3221     loader            = instanceKlass::cast(curr_klass_oop)->class_loader();
  3222     protection_domain = instanceKlass::cast(curr_klass_oop)->protection_domain();
  3224   Handle h_loader(THREAD, loader);
  3225   Handle h_prot  (THREAD, protection_domain);
  3226   jclass result =  find_class_from_class_loader(env, name, true, h_loader, h_prot,
  3227                                                 false, thread);
  3228   if (TraceClassResolution && result != NULL) {
  3229     trace_class_resolution(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(result)));
  3231   return result;
  3232 JVM_END
  3235 // Array ///////////////////////////////////////////////////////////////////////////////////////////
  3238 // resolve array handle and check arguments
  3239 static inline arrayOop check_array(JNIEnv *env, jobject arr, bool type_array_only, TRAPS) {
  3240   if (arr == NULL) {
  3241     THROW_0(vmSymbols::java_lang_NullPointerException());
  3243   oop a = JNIHandles::resolve_non_null(arr);
  3244   if (!a->is_javaArray() || (type_array_only && !a->is_typeArray())) {
  3245     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array");
  3247   return arrayOop(a);
  3251 JVM_ENTRY(jint, JVM_GetArrayLength(JNIEnv *env, jobject arr))
  3252   JVMWrapper("JVM_GetArrayLength");
  3253   arrayOop a = check_array(env, arr, false, CHECK_0);
  3254   return a->length();
  3255 JVM_END
  3258 JVM_ENTRY(jobject, JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index))
  3259   JVMWrapper("JVM_Array_Get");
  3260   JvmtiVMObjectAllocEventCollector oam;
  3261   arrayOop a = check_array(env, arr, false, CHECK_NULL);
  3262   jvalue value;
  3263   BasicType type = Reflection::array_get(&value, a, index, CHECK_NULL);
  3264   oop box = Reflection::box(&value, type, CHECK_NULL);
  3265   return JNIHandles::make_local(env, box);
  3266 JVM_END
  3269 JVM_ENTRY(jvalue, JVM_GetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jint wCode))
  3270   JVMWrapper("JVM_GetPrimitiveArrayElement");
  3271   jvalue value;
  3272   value.i = 0; // to initialize value before getting used in CHECK
  3273   arrayOop a = check_array(env, arr, true, CHECK_(value));
  3274   assert(a->is_typeArray(), "just checking");
  3275   BasicType type = Reflection::array_get(&value, a, index, CHECK_(value));
  3276   BasicType wide_type = (BasicType) wCode;
  3277   if (type != wide_type) {
  3278     Reflection::widen(&value, type, wide_type, CHECK_(value));
  3280   return value;
  3281 JVM_END
  3284 JVM_ENTRY(void, JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val))
  3285   JVMWrapper("JVM_SetArrayElement");
  3286   arrayOop a = check_array(env, arr, false, CHECK);
  3287   oop box = JNIHandles::resolve(val);
  3288   jvalue value;
  3289   value.i = 0; // to initialize value before getting used in CHECK
  3290   BasicType value_type;
  3291   if (a->is_objArray()) {
  3292     // Make sure we do no unbox e.g. java/lang/Integer instances when storing into an object array
  3293     value_type = Reflection::unbox_for_regular_object(box, &value);
  3294   } else {
  3295     value_type = Reflection::unbox_for_primitive(box, &value, CHECK);
  3297   Reflection::array_set(&value, a, index, value_type, CHECK);
  3298 JVM_END
  3301 JVM_ENTRY(void, JVM_SetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jvalue v, unsigned char vCode))
  3302   JVMWrapper("JVM_SetPrimitiveArrayElement");
  3303   arrayOop a = check_array(env, arr, true, CHECK);
  3304   assert(a->is_typeArray(), "just checking");
  3305   BasicType value_type = (BasicType) vCode;
  3306   Reflection::array_set(&v, a, index, value_type, CHECK);
  3307 JVM_END
  3310 JVM_ENTRY(jobject, JVM_NewArray(JNIEnv *env, jclass eltClass, jint length))
  3311   JVMWrapper("JVM_NewArray");
  3312   JvmtiVMObjectAllocEventCollector oam;
  3313   oop element_mirror = JNIHandles::resolve(eltClass);
  3314   oop result = Reflection::reflect_new_array(element_mirror, length, CHECK_NULL);
  3315   return JNIHandles::make_local(env, result);
  3316 JVM_END
  3319 JVM_ENTRY(jobject, JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim))
  3320   JVMWrapper("JVM_NewMultiArray");
  3321   JvmtiVMObjectAllocEventCollector oam;
  3322   arrayOop dim_array = check_array(env, dim, true, CHECK_NULL);
  3323   oop element_mirror = JNIHandles::resolve(eltClass);
  3324   assert(dim_array->is_typeArray(), "just checking");
  3325   oop result = Reflection::reflect_new_multi_array(element_mirror, typeArrayOop(dim_array), CHECK_NULL);
  3326   return JNIHandles::make_local(env, result);
  3327 JVM_END
  3330 // Networking library support ////////////////////////////////////////////////////////////////////
  3332 JVM_LEAF(jint, JVM_InitializeSocketLibrary())
  3333   JVMWrapper("JVM_InitializeSocketLibrary");
  3334   return hpi::initialize_socket_library();
  3335 JVM_END
  3338 JVM_LEAF(jint, JVM_Socket(jint domain, jint type, jint protocol))
  3339   JVMWrapper("JVM_Socket");
  3340   return hpi::socket(domain, type, protocol);
  3341 JVM_END
  3344 JVM_LEAF(jint, JVM_SocketClose(jint fd))
  3345   JVMWrapper2("JVM_SocketClose (0x%x)", fd);
  3346   //%note jvm_r6
  3347   return hpi::socket_close(fd);
  3348 JVM_END
  3351 JVM_LEAF(jint, JVM_SocketShutdown(jint fd, jint howto))
  3352   JVMWrapper2("JVM_SocketShutdown (0x%x)", fd);
  3353   //%note jvm_r6
  3354   return hpi::socket_shutdown(fd, howto);
  3355 JVM_END
  3358 JVM_LEAF(jint, JVM_Recv(jint fd, char *buf, jint nBytes, jint flags))
  3359   JVMWrapper2("JVM_Recv (0x%x)", fd);
  3360   //%note jvm_r6
  3361   return hpi::recv(fd, buf, nBytes, flags);
  3362 JVM_END
  3365 JVM_LEAF(jint, JVM_Send(jint fd, char *buf, jint nBytes, jint flags))
  3366   JVMWrapper2("JVM_Send (0x%x)", fd);
  3367   //%note jvm_r6
  3368   return hpi::send(fd, buf, nBytes, flags);
  3369 JVM_END
  3372 JVM_LEAF(jint, JVM_Timeout(int fd, long timeout))
  3373   JVMWrapper2("JVM_Timeout (0x%x)", fd);
  3374   //%note jvm_r6
  3375   return hpi::timeout(fd, timeout);
  3376 JVM_END
  3379 JVM_LEAF(jint, JVM_Listen(jint fd, jint count))
  3380   JVMWrapper2("JVM_Listen (0x%x)", fd);
  3381   //%note jvm_r6
  3382   return hpi::listen(fd, count);
  3383 JVM_END
  3386 JVM_LEAF(jint, JVM_Connect(jint fd, struct sockaddr *him, jint len))
  3387   JVMWrapper2("JVM_Connect (0x%x)", fd);
  3388   //%note jvm_r6
  3389   return hpi::connect(fd, him, len);
  3390 JVM_END
  3393 JVM_LEAF(jint, JVM_Bind(jint fd, struct sockaddr *him, jint len))
  3394   JVMWrapper2("JVM_Bind (0x%x)", fd);
  3395   //%note jvm_r6
  3396   return hpi::bind(fd, him, len);
  3397 JVM_END
  3400 JVM_LEAF(jint, JVM_Accept(jint fd, struct sockaddr *him, jint *len))
  3401   JVMWrapper2("JVM_Accept (0x%x)", fd);
  3402   //%note jvm_r6
  3403   return hpi::accept(fd, him, (int *)len);
  3404 JVM_END
  3407 JVM_LEAF(jint, JVM_RecvFrom(jint fd, char *buf, int nBytes, int flags, struct sockaddr *from, int *fromlen))
  3408   JVMWrapper2("JVM_RecvFrom (0x%x)", fd);
  3409   //%note jvm_r6
  3410   return hpi::recvfrom(fd, buf, nBytes, flags, from, fromlen);
  3411 JVM_END
  3414 JVM_LEAF(jint, JVM_GetSockName(jint fd, struct sockaddr *him, int *len))
  3415   JVMWrapper2("JVM_GetSockName (0x%x)", fd);
  3416   //%note jvm_r6
  3417   return hpi::get_sock_name(fd, him, len);
  3418 JVM_END
  3421 JVM_LEAF(jint, JVM_SendTo(jint fd, char *buf, int len, int flags, struct sockaddr *to, int tolen))
  3422   JVMWrapper2("JVM_SendTo (0x%x)", fd);
  3423   //%note jvm_r6
  3424   return hpi::sendto(fd, buf, len, flags, to, tolen);
  3425 JVM_END
  3428 JVM_LEAF(jint, JVM_SocketAvailable(jint fd, jint *pbytes))
  3429   JVMWrapper2("JVM_SocketAvailable (0x%x)", fd);
  3430   //%note jvm_r6
  3431   return hpi::socket_available(fd, pbytes);
  3432 JVM_END
  3435 JVM_LEAF(jint, JVM_GetSockOpt(jint fd, int level, int optname, char *optval, int *optlen))
  3436   JVMWrapper2("JVM_GetSockOpt (0x%x)", fd);
  3437   //%note jvm_r6
  3438   return hpi::get_sock_opt(fd, level, optname, optval, optlen);
  3439 JVM_END
  3442 JVM_LEAF(jint, JVM_SetSockOpt(jint fd, int level, int optname, const char *optval, int optlen))
  3443   JVMWrapper2("JVM_GetSockOpt (0x%x)", fd);
  3444   //%note jvm_r6
  3445   return hpi::set_sock_opt(fd, level, optname, optval, optlen);
  3446 JVM_END
  3448 JVM_LEAF(int, JVM_GetHostName(char* name, int namelen))
  3449   JVMWrapper("JVM_GetHostName");
  3450   return hpi::get_host_name(name, namelen);
  3451 JVM_END
  3453 #ifdef _WINDOWS
  3455 JVM_LEAF(struct hostent*, JVM_GetHostByAddr(const char* name, int len, int type))
  3456   JVMWrapper("JVM_GetHostByAddr");
  3457   return hpi::get_host_by_addr(name, len, type);
  3458 JVM_END
  3461 JVM_LEAF(struct hostent*, JVM_GetHostByName(char* name))
  3462   JVMWrapper("JVM_GetHostByName");
  3463   return hpi::get_host_by_name(name);
  3464 JVM_END
  3467 JVM_LEAF(struct protoent*, JVM_GetProtoByName(char* name))
  3468   JVMWrapper("JVM_GetProtoByName");
  3469   return hpi::get_proto_by_name(name);
  3470 JVM_END
  3472 #endif
  3474 // Library support ///////////////////////////////////////////////////////////////////////////
  3476 JVM_ENTRY_NO_ENV(void*, JVM_LoadLibrary(const char* name))
  3477   //%note jvm_ct
  3478   JVMWrapper2("JVM_LoadLibrary (%s)", name);
  3479   char ebuf[1024];
  3480   void *load_result;
  3482     ThreadToNativeFromVM ttnfvm(thread);
  3483     load_result = hpi::dll_load(name, ebuf, sizeof ebuf);
  3485   if (load_result == NULL) {
  3486     char msg[1024];
  3487     jio_snprintf(msg, sizeof msg, "%s: %s", name, ebuf);
  3488     // Since 'ebuf' may contain a string encoded using
  3489     // platform encoding scheme, we need to pass
  3490     // Exceptions::unsafe_to_utf8 to the new_exception method
  3491     // as the last argument. See bug 6367357.
  3492     Handle h_exception =
  3493       Exceptions::new_exception(thread,
  3494                                 vmSymbols::java_lang_UnsatisfiedLinkError(),
  3495                                 msg, Exceptions::unsafe_to_utf8);
  3497     THROW_HANDLE_0(h_exception);
  3499   return load_result;
  3500 JVM_END
  3503 JVM_LEAF(void, JVM_UnloadLibrary(void* handle))
  3504   JVMWrapper("JVM_UnloadLibrary");
  3505   hpi::dll_unload(handle);
  3506 JVM_END
  3509 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
  3510   JVMWrapper2("JVM_FindLibraryEntry (%s)", name);
  3511   return hpi::dll_lookup(handle, name);
  3512 JVM_END
  3514 // Floating point support ////////////////////////////////////////////////////////////////////
  3516 JVM_LEAF(jboolean, JVM_IsNaN(jdouble a))
  3517   JVMWrapper("JVM_IsNaN");
  3518   return g_isnan(a);
  3519 JVM_END
  3523 // JNI version ///////////////////////////////////////////////////////////////////////////////
  3525 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
  3526   JVMWrapper2("JVM_IsSupportedJNIVersion (%d)", version);
  3527   return Threads::is_supported_jni_version_including_1_1(version);
  3528 JVM_END
  3531 // String support ///////////////////////////////////////////////////////////////////////////
  3533 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
  3534   JVMWrapper("JVM_InternString");
  3535   JvmtiVMObjectAllocEventCollector oam;
  3536   if (str == NULL) return NULL;
  3537   oop string = JNIHandles::resolve_non_null(str);
  3538   oop result = StringTable::intern(string, CHECK_NULL);
  3539   return (jstring) JNIHandles::make_local(env, result);
  3540 JVM_END
  3543 // Raw monitor support //////////////////////////////////////////////////////////////////////
  3545 // The lock routine below calls lock_without_safepoint_check in order to get a raw lock
  3546 // without interfering with the safepoint mechanism. The routines are not JVM_LEAF because
  3547 // they might be called by non-java threads. The JVM_LEAF installs a NoHandleMark check
  3548 // that only works with java threads.
  3551 JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void) {
  3552   VM_Exit::block_if_vm_exited();
  3553   JVMWrapper("JVM_RawMonitorCreate");
  3554   return new Mutex(Mutex::native, "JVM_RawMonitorCreate");
  3558 JNIEXPORT void JNICALL  JVM_RawMonitorDestroy(void *mon) {
  3559   VM_Exit::block_if_vm_exited();
  3560   JVMWrapper("JVM_RawMonitorDestroy");
  3561   delete ((Mutex*) mon);
  3565 JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void *mon) {
  3566   VM_Exit::block_if_vm_exited();
  3567   JVMWrapper("JVM_RawMonitorEnter");
  3568   ((Mutex*) mon)->jvm_raw_lock();
  3569   return 0;
  3573 JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon) {
  3574   VM_Exit::block_if_vm_exited();
  3575   JVMWrapper("JVM_RawMonitorExit");
  3576   ((Mutex*) mon)->jvm_raw_unlock();
  3580 // Support for Serialization
  3582 typedef jfloat  (JNICALL *IntBitsToFloatFn  )(JNIEnv* env, jclass cb, jint    value);
  3583 typedef jdouble (JNICALL *LongBitsToDoubleFn)(JNIEnv* env, jclass cb, jlong   value);
  3584 typedef jint    (JNICALL *FloatToIntBitsFn  )(JNIEnv* env, jclass cb, jfloat  value);
  3585 typedef jlong   (JNICALL *DoubleToLongBitsFn)(JNIEnv* env, jclass cb, jdouble value);
  3587 static IntBitsToFloatFn   int_bits_to_float_fn   = NULL;
  3588 static LongBitsToDoubleFn long_bits_to_double_fn = NULL;
  3589 static FloatToIntBitsFn   float_to_int_bits_fn   = NULL;
  3590 static DoubleToLongBitsFn double_to_long_bits_fn = NULL;
  3593 void initialize_converter_functions() {
  3594   if (JDK_Version::is_gte_jdk14x_version()) {
  3595     // These functions only exist for compatibility with 1.3.1 and earlier
  3596     return;
  3599   // called from universe_post_init()
  3600   assert(
  3601     int_bits_to_float_fn   == NULL &&
  3602     long_bits_to_double_fn == NULL &&
  3603     float_to_int_bits_fn   == NULL &&
  3604     double_to_long_bits_fn == NULL ,
  3605     "initialization done twice"
  3606   );
  3607   // initialize
  3608   int_bits_to_float_fn   = CAST_TO_FN_PTR(IntBitsToFloatFn  , NativeLookup::base_library_lookup("java/lang/Float" , "intBitsToFloat"  , "(I)F"));
  3609   long_bits_to_double_fn = CAST_TO_FN_PTR(LongBitsToDoubleFn, NativeLookup::base_library_lookup("java/lang/Double", "longBitsToDouble", "(J)D"));
  3610   float_to_int_bits_fn   = CAST_TO_FN_PTR(FloatToIntBitsFn  , NativeLookup::base_library_lookup("java/lang/Float" , "floatToIntBits"  , "(F)I"));
  3611   double_to_long_bits_fn = CAST_TO_FN_PTR(DoubleToLongBitsFn, NativeLookup::base_library_lookup("java/lang/Double", "doubleToLongBits", "(D)J"));
  3612   // verify
  3613   assert(
  3614     int_bits_to_float_fn   != NULL &&
  3615     long_bits_to_double_fn != NULL &&
  3616     float_to_int_bits_fn   != NULL &&
  3617     double_to_long_bits_fn != NULL ,
  3618     "initialization failed"
  3619   );
  3623 // Serialization
  3624 JVM_ENTRY(void, JVM_SetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj,
  3625                                             jlongArray fieldIDs, jcharArray typecodes, jbyteArray data))
  3626   assert(!JDK_Version::is_gte_jdk14x_version(), "should only be used in 1.3.1 and earlier");
  3628   typeArrayOop tcodes = typeArrayOop(JNIHandles::resolve(typecodes));
  3629   typeArrayOop dbuf   = typeArrayOop(JNIHandles::resolve(data));
  3630   typeArrayOop fids   = typeArrayOop(JNIHandles::resolve(fieldIDs));
  3631   oop          o      = JNIHandles::resolve(obj);
  3633   if (o == NULL || fids == NULL  || dbuf == NULL  || tcodes == NULL) {
  3634     THROW(vmSymbols::java_lang_NullPointerException());
  3637   jsize nfids = fids->length();
  3638   if (nfids == 0) return;
  3640   if (tcodes->length() < nfids) {
  3641     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
  3644   jsize off = 0;
  3645   /* loop through fields, setting values */
  3646   for (jsize i = 0; i < nfids; i++) {
  3647     jfieldID fid = (jfieldID)(intptr_t) fids->long_at(i);
  3648     int field_offset;
  3649     if (fid != NULL) {
  3650       // NULL is a legal value for fid, but retrieving the field offset
  3651       // trigger assertion in that case
  3652       field_offset = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid);
  3655     switch (tcodes->char_at(i)) {
  3656       case 'Z':
  3657         if (fid != NULL) {
  3658           jboolean val = (dbuf->byte_at(off) != 0) ? JNI_TRUE : JNI_FALSE;
  3659           o->bool_field_put(field_offset, val);
  3661         off++;
  3662         break;
  3664       case 'B':
  3665         if (fid != NULL) {
  3666           o->byte_field_put(field_offset, dbuf->byte_at(off));
  3668         off++;
  3669         break;
  3671       case 'C':
  3672         if (fid != NULL) {
  3673           jchar val = ((dbuf->byte_at(off + 0) & 0xFF) << 8)
  3674                     + ((dbuf->byte_at(off + 1) & 0xFF) << 0);
  3675           o->char_field_put(field_offset, val);
  3677         off += 2;
  3678         break;
  3680       case 'S':
  3681         if (fid != NULL) {
  3682           jshort val = ((dbuf->byte_at(off + 0) & 0xFF) << 8)
  3683                      + ((dbuf->byte_at(off + 1) & 0xFF) << 0);
  3684           o->short_field_put(field_offset, val);
  3686         off += 2;
  3687         break;
  3689       case 'I':
  3690         if (fid != NULL) {
  3691           jint ival = ((dbuf->byte_at(off + 0) & 0xFF) << 24)
  3692                     + ((dbuf->byte_at(off + 1) & 0xFF) << 16)
  3693                     + ((dbuf->byte_at(off + 2) & 0xFF) << 8)
  3694                     + ((dbuf->byte_at(off + 3) & 0xFF) << 0);
  3695           o->int_field_put(field_offset, ival);
  3697         off += 4;
  3698         break;
  3700       case 'F':
  3701         if (fid != NULL) {
  3702           jint ival = ((dbuf->byte_at(off + 0) & 0xFF) << 24)
  3703                     + ((dbuf->byte_at(off + 1) & 0xFF) << 16)
  3704                     + ((dbuf->byte_at(off + 2) & 0xFF) << 8)
  3705                     + ((dbuf->byte_at(off + 3) & 0xFF) << 0);
  3706           jfloat fval = (*int_bits_to_float_fn)(env, NULL, ival);
  3707           o->float_field_put(field_offset, fval);
  3709         off += 4;
  3710         break;
  3712       case 'J':
  3713         if (fid != NULL) {
  3714           jlong lval = (((jlong) dbuf->byte_at(off + 0) & 0xFF) << 56)
  3715                      + (((jlong) dbuf->byte_at(off + 1) & 0xFF) << 48)
  3716                      + (((jlong) dbuf->byte_at(off + 2) & 0xFF) << 40)
  3717                      + (((jlong) dbuf->byte_at(off + 3) & 0xFF) << 32)
  3718                      + (((jlong) dbuf->byte_at(off + 4) & 0xFF) << 24)
  3719                      + (((jlong) dbuf->byte_at(off + 5) & 0xFF) << 16)
  3720                      + (((jlong) dbuf->byte_at(off + 6) & 0xFF) << 8)
  3721                      + (((jlong) dbuf->byte_at(off + 7) & 0xFF) << 0);
  3722           o->long_field_put(field_offset, lval);
  3724         off += 8;
  3725         break;
  3727       case 'D':
  3728         if (fid != NULL) {
  3729           jlong lval = (((jlong) dbuf->byte_at(off + 0) & 0xFF) << 56)
  3730                      + (((jlong) dbuf->byte_at(off + 1) & 0xFF) << 48)
  3731                      + (((jlong) dbuf->byte_at(off + 2) & 0xFF) << 40)
  3732                      + (((jlong) dbuf->byte_at(off + 3) & 0xFF) << 32)
  3733                      + (((jlong) dbuf->byte_at(off + 4) & 0xFF) << 24)
  3734                      + (((jlong) dbuf->byte_at(off + 5) & 0xFF) << 16)
  3735                      + (((jlong) dbuf->byte_at(off + 6) & 0xFF) << 8)
  3736                      + (((jlong) dbuf->byte_at(off + 7) & 0xFF) << 0);
  3737           jdouble dval = (*long_bits_to_double_fn)(env, NULL, lval);
  3738           o->double_field_put(field_offset, dval);
  3740         off += 8;
  3741         break;
  3743       default:
  3744         // Illegal typecode
  3745         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "illegal typecode");
  3748 JVM_END
  3751 JVM_ENTRY(void, JVM_GetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj,
  3752                             jlongArray fieldIDs, jcharArray typecodes, jbyteArray data))
  3753   assert(!JDK_Version::is_gte_jdk14x_version(), "should only be used in 1.3.1 and earlier");
  3755   typeArrayOop tcodes = typeArrayOop(JNIHandles::resolve(typecodes));
  3756   typeArrayOop dbuf   = typeArrayOop(JNIHandles::resolve(data));
  3757   typeArrayOop fids   = typeArrayOop(JNIHandles::resolve(fieldIDs));
  3758   oop          o      = JNIHandles::resolve(obj);
  3760   if (o == NULL || fids == NULL  || dbuf == NULL  || tcodes == NULL) {
  3761     THROW(vmSymbols::java_lang_NullPointerException());
  3764   jsize nfids = fids->length();
  3765   if (nfids == 0) return;
  3767   if (tcodes->length() < nfids) {
  3768     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
  3771   /* loop through fields, fetching values */
  3772   jsize off = 0;
  3773   for (jsize i = 0; i < nfids; i++) {
  3774     jfieldID fid = (jfieldID)(intptr_t) fids->long_at(i);
  3775     if (fid == NULL) {
  3776       THROW(vmSymbols::java_lang_NullPointerException());
  3778     int field_offset = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid);
  3780      switch (tcodes->char_at(i)) {
  3781        case 'Z':
  3783            jboolean val = o->bool_field(field_offset);
  3784            dbuf->byte_at_put(off++, (val != 0) ? 1 : 0);
  3786          break;
  3788        case 'B':
  3789          dbuf->byte_at_put(off++, o->byte_field(field_offset));
  3790          break;
  3792        case 'C':
  3794            jchar val = o->char_field(field_offset);
  3795            dbuf->byte_at_put(off++, (val >> 8) & 0xFF);
  3796            dbuf->byte_at_put(off++, (val >> 0) & 0xFF);
  3798          break;
  3800        case 'S':
  3802            jshort val = o->short_field(field_offset);
  3803            dbuf->byte_at_put(off++, (val >> 8) & 0xFF);
  3804            dbuf->byte_at_put(off++, (val >> 0) & 0xFF);
  3806          break;
  3808        case 'I':
  3810            jint val = o->int_field(field_offset);
  3811            dbuf->byte_at_put(off++, (val >> 24) & 0xFF);
  3812            dbuf->byte_at_put(off++, (val >> 16) & 0xFF);
  3813            dbuf->byte_at_put(off++, (val >> 8)  & 0xFF);
  3814            dbuf->byte_at_put(off++, (val >> 0)  & 0xFF);
  3816          break;
  3818        case 'F':
  3820            jfloat fval = o->float_field(field_offset);
  3821            jint ival = (*float_to_int_bits_fn)(env, NULL, fval);
  3822            dbuf->byte_at_put(off++, (ival >> 24) & 0xFF);
  3823            dbuf->byte_at_put(off++, (ival >> 16) & 0xFF);
  3824            dbuf->byte_at_put(off++, (ival >> 8)  & 0xFF);
  3825            dbuf->byte_at_put(off++, (ival >> 0)  & 0xFF);
  3827          break;
  3829        case 'J':
  3831            jlong val = o->long_field(field_offset);
  3832            dbuf->byte_at_put(off++, (val >> 56) & 0xFF);
  3833            dbuf->byte_at_put(off++, (val >> 48) & 0xFF);
  3834            dbuf->byte_at_put(off++, (val >> 40) & 0xFF);
  3835            dbuf->byte_at_put(off++, (val >> 32) & 0xFF);
  3836            dbuf->byte_at_put(off++, (val >> 24) & 0xFF);
  3837            dbuf->byte_at_put(off++, (val >> 16) & 0xFF);
  3838            dbuf->byte_at_put(off++, (val >> 8)  & 0xFF);
  3839            dbuf->byte_at_put(off++, (val >> 0)  & 0xFF);
  3841          break;
  3843        case 'D':
  3845            jdouble dval = o->double_field(field_offset);
  3846            jlong lval = (*double_to_long_bits_fn)(env, NULL, dval);
  3847            dbuf->byte_at_put(off++, (lval >> 56) & 0xFF);
  3848            dbuf->byte_at_put(off++, (lval >> 48) & 0xFF);
  3849            dbuf->byte_at_put(off++, (lval >> 40) & 0xFF);
  3850            dbuf->byte_at_put(off++, (lval >> 32) & 0xFF);
  3851            dbuf->byte_at_put(off++, (lval >> 24) & 0xFF);
  3852            dbuf->byte_at_put(off++, (lval >> 16) & 0xFF);
  3853            dbuf->byte_at_put(off++, (lval >> 8)  & 0xFF);
  3854            dbuf->byte_at_put(off++, (lval >> 0)  & 0xFF);
  3856          break;
  3858        default:
  3859          // Illegal typecode
  3860          THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "illegal typecode");
  3863 JVM_END
  3866 // Shared JNI/JVM entry points //////////////////////////////////////////////////////////////
  3868 jclass find_class_from_class_loader(JNIEnv* env, symbolHandle name, jboolean init, Handle loader, Handle protection_domain, jboolean throwError, TRAPS) {
  3869   // Security Note:
  3870   //   The Java level wrapper will perform the necessary security check allowing
  3871   //   us to pass the NULL as the initiating class loader.
  3872   klassOop klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
  3873   KlassHandle klass_handle(THREAD, klass);
  3874   // Check if we should initialize the class
  3875   if (init && klass_handle->oop_is_instance()) {
  3876     klass_handle->initialize(CHECK_NULL);
  3878   return (jclass) JNIHandles::make_local(env, klass_handle->java_mirror());
  3882 // Internal SQE debugging support ///////////////////////////////////////////////////////////
  3884 #ifndef PRODUCT
  3886 extern "C" {
  3887   JNIEXPORT jboolean JNICALL JVM_AccessVMBooleanFlag(const char* name, jboolean* value, jboolean is_get);
  3888   JNIEXPORT jboolean JNICALL JVM_AccessVMIntFlag(const char* name, jint* value, jboolean is_get);
  3889   JNIEXPORT void JNICALL JVM_VMBreakPoint(JNIEnv *env, jobject obj);
  3892 JVM_LEAF(jboolean, JVM_AccessVMBooleanFlag(const char* name, jboolean* value, jboolean is_get))
  3893   JVMWrapper("JVM_AccessBoolVMFlag");
  3894   return is_get ? CommandLineFlags::boolAt((char*) name, (bool*) value) : CommandLineFlags::boolAtPut((char*) name, (bool*) value, INTERNAL);
  3895 JVM_END
  3897 JVM_LEAF(jboolean, JVM_AccessVMIntFlag(const char* name, jint* value, jboolean is_get))
  3898   JVMWrapper("JVM_AccessVMIntFlag");
  3899   intx v;
  3900   jboolean result = is_get ? CommandLineFlags::intxAt((char*) name, &v) : CommandLineFlags::intxAtPut((char*) name, &v, INTERNAL);
  3901   *value = (jint)v;
  3902   return result;
  3903 JVM_END
  3906 JVM_ENTRY(void, JVM_VMBreakPoint(JNIEnv *env, jobject obj))
  3907   JVMWrapper("JVM_VMBreakPoint");
  3908   oop the_obj = JNIHandles::resolve(obj);
  3909   BREAKPOINT;
  3910 JVM_END
  3913 #endif
  3916 //---------------------------------------------------------------------------
  3917 //
  3918 // Support for old native code-based reflection (pre-JDK 1.4)
  3919 // Disabled by default in the product build.
  3920 //
  3921 // See reflection.hpp for information on SUPPORT_OLD_REFLECTION
  3922 //
  3923 //---------------------------------------------------------------------------
  3925 #ifdef SUPPORT_OLD_REFLECTION
  3927 JVM_ENTRY(jobjectArray, JVM_GetClassFields(JNIEnv *env, jclass cls, jint which))
  3928   JVMWrapper("JVM_GetClassFields");
  3929   JvmtiVMObjectAllocEventCollector oam;
  3930   oop mirror = JNIHandles::resolve_non_null(cls);
  3931   objArrayOop result = Reflection::reflect_fields(mirror, which, CHECK_NULL);
  3932   return (jobjectArray) JNIHandles::make_local(env, result);
  3933 JVM_END
  3936 JVM_ENTRY(jobjectArray, JVM_GetClassMethods(JNIEnv *env, jclass cls, jint which))
  3937   JVMWrapper("JVM_GetClassMethods");
  3938   JvmtiVMObjectAllocEventCollector oam;
  3939   oop mirror = JNIHandles::resolve_non_null(cls);
  3940   objArrayOop result = Reflection::reflect_methods(mirror, which, CHECK_NULL);
  3941   //%note jvm_r4
  3942   return (jobjectArray) JNIHandles::make_local(env, result);
  3943 JVM_END
  3946 JVM_ENTRY(jobjectArray, JVM_GetClassConstructors(JNIEnv *env, jclass cls, jint which))
  3947   JVMWrapper("JVM_GetClassConstructors");
  3948   JvmtiVMObjectAllocEventCollector oam;
  3949   oop mirror = JNIHandles::resolve_non_null(cls);
  3950   objArrayOop result = Reflection::reflect_constructors(mirror, which, CHECK_NULL);
  3951   //%note jvm_r4
  3952   return (jobjectArray) JNIHandles::make_local(env, result);
  3953 JVM_END
  3956 JVM_ENTRY(jobject, JVM_GetClassField(JNIEnv *env, jclass cls, jstring name, jint which))
  3957   JVMWrapper("JVM_GetClassField");
  3958   JvmtiVMObjectAllocEventCollector oam;
  3959   if (name == NULL) return NULL;
  3960   Handle str (THREAD, JNIHandles::resolve_non_null(name));
  3962   const char* cstr = java_lang_String::as_utf8_string(str());
  3963   symbolHandle field_name =
  3964            symbolHandle(THREAD, SymbolTable::probe(cstr, (int)strlen(cstr)));
  3965   if (field_name.is_null()) {
  3966     THROW_0(vmSymbols::java_lang_NoSuchFieldException());
  3969   oop mirror = JNIHandles::resolve_non_null(cls);
  3970   oop result = Reflection::reflect_field(mirror, field_name(), which, CHECK_NULL);
  3971   if (result == NULL) {
  3972     THROW_0(vmSymbols::java_lang_NoSuchFieldException());
  3974   return JNIHandles::make_local(env, result);
  3975 JVM_END
  3978 JVM_ENTRY(jobject, JVM_GetClassMethod(JNIEnv *env, jclass cls, jstring name, jobjectArray types, jint which))
  3979   JVMWrapper("JVM_GetClassMethod");
  3980   JvmtiVMObjectAllocEventCollector oam;
  3981   if (name == NULL) {
  3982     THROW_0(vmSymbols::java_lang_NullPointerException());
  3984   Handle str (THREAD, JNIHandles::resolve_non_null(name));
  3986   const char* cstr = java_lang_String::as_utf8_string(str());
  3987   symbolHandle method_name =
  3988           symbolHandle(THREAD, SymbolTable::probe(cstr, (int)strlen(cstr)));
  3989   if (method_name.is_null()) {
  3990     THROW_0(vmSymbols::java_lang_NoSuchMethodException());
  3993   oop mirror = JNIHandles::resolve_non_null(cls);
  3994   objArrayHandle tarray (THREAD, objArrayOop(JNIHandles::resolve(types)));
  3995   oop result = Reflection::reflect_method(mirror, method_name, tarray,
  3996                                           which, CHECK_NULL);
  3997   if (result == NULL) {
  3998     THROW_0(vmSymbols::java_lang_NoSuchMethodException());
  4000   return JNIHandles::make_local(env, result);
  4001 JVM_END
  4004 JVM_ENTRY(jobject, JVM_GetClassConstructor(JNIEnv *env, jclass cls, jobjectArray types, jint which))
  4005   JVMWrapper("JVM_GetClassConstructor");
  4006   JvmtiVMObjectAllocEventCollector oam;
  4007   oop mirror = JNIHandles::resolve_non_null(cls);
  4008   objArrayHandle tarray (THREAD, objArrayOop(JNIHandles::resolve(types)));
  4009   oop result = Reflection::reflect_constructor(mirror, tarray, which, CHECK_NULL);
  4010   if (result == NULL) {
  4011     THROW_0(vmSymbols::java_lang_NoSuchMethodException());
  4013   return (jobject) JNIHandles::make_local(env, result);
  4014 JVM_END
  4017 // Instantiation ///////////////////////////////////////////////////////////////////////////////
  4019 JVM_ENTRY(jobject, JVM_NewInstance(JNIEnv *env, jclass cls))
  4020   JVMWrapper("JVM_NewInstance");
  4021   Handle mirror(THREAD, JNIHandles::resolve_non_null(cls));
  4023   methodOop resolved_constructor = java_lang_Class::resolved_constructor(mirror());
  4024   if (resolved_constructor == NULL) {
  4025     klassOop k = java_lang_Class::as_klassOop(mirror());
  4026     // The java.lang.Class object caches a resolved constructor if all the checks
  4027     // below were done successfully and a constructor was found.
  4029     // Do class based checks
  4030     if (java_lang_Class::is_primitive(mirror())) {
  4031       const char* msg = "";
  4032       if      (mirror == Universe::bool_mirror())   msg = "java/lang/Boolean";
  4033       else if (mirror == Universe::char_mirror())   msg = "java/lang/Character";
  4034       else if (mirror == Universe::float_mirror())  msg = "java/lang/Float";
  4035       else if (mirror == Universe::double_mirror()) msg = "java/lang/Double";
  4036       else if (mirror == Universe::byte_mirror())   msg = "java/lang/Byte";
  4037       else if (mirror == Universe::short_mirror())  msg = "java/lang/Short";
  4038       else if (mirror == Universe::int_mirror())    msg = "java/lang/Integer";
  4039       else if (mirror == Universe::long_mirror())   msg = "java/lang/Long";
  4040       THROW_MSG_0(vmSymbols::java_lang_NullPointerException(), msg);
  4043     // Check whether we are allowed to instantiate this class
  4044     Klass::cast(k)->check_valid_for_instantiation(false, CHECK_NULL); // Array classes get caught here
  4045     instanceKlassHandle klass(THREAD, k);
  4046     // Make sure class is initialized (also so all methods are rewritten)
  4047     klass->initialize(CHECK_NULL);
  4049     // Lookup default constructor
  4050     resolved_constructor = klass->find_method(vmSymbols::object_initializer_name(), vmSymbols::void_method_signature());
  4051     if (resolved_constructor == NULL) {
  4052       ResourceMark rm(THREAD);
  4053       THROW_MSG_0(vmSymbols::java_lang_InstantiationException(), klass->external_name());
  4056     // Cache result in java.lang.Class object. Does not have to be MT safe.
  4057     java_lang_Class::set_resolved_constructor(mirror(), resolved_constructor);
  4060   assert(resolved_constructor != NULL, "sanity check");
  4061   methodHandle constructor = methodHandle(THREAD, resolved_constructor);
  4063   // We have an initialized instanceKlass with a default constructor
  4064   instanceKlassHandle klass(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls)));
  4065   assert(klass->is_initialized() || klass->is_being_initialized(), "sanity check");
  4067   // Do security check
  4068   klassOop caller_klass = NULL;
  4069   if (UsePrivilegedStack) {
  4070     caller_klass = thread->security_get_caller_class(2);
  4072     if (!Reflection::verify_class_access(caller_klass, klass(), false) ||
  4073         !Reflection::verify_field_access(caller_klass,
  4074                                          klass(),
  4075                                          klass(),
  4076                                          constructor->access_flags(),
  4077                                          false,
  4078                                          true)) {
  4079       ResourceMark rm(THREAD);
  4080       THROW_MSG_0(vmSymbols::java_lang_IllegalAccessException(), klass->external_name());
  4084   // Allocate object and call constructor
  4085   Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
  4086   JavaCalls::call_default_constructor(thread, constructor, receiver, CHECK_NULL);
  4088   jobject res = JNIHandles::make_local(env, receiver());
  4089   if (JvmtiExport::should_post_vm_object_alloc()) {
  4090     JvmtiExport::post_vm_object_alloc(JavaThread::current(), receiver());
  4092   return res;
  4093 JVM_END
  4096 // Field ////////////////////////////////////////////////////////////////////////////////////////////
  4098 JVM_ENTRY(jobject, JVM_GetField(JNIEnv *env, jobject field, jobject obj))
  4099   JVMWrapper("JVM_GetField");
  4100   JvmtiVMObjectAllocEventCollector oam;
  4101   Handle field_mirror(thread, JNIHandles::resolve(field));
  4102   Handle receiver    (thread, JNIHandles::resolve(obj));
  4103   fieldDescriptor fd;
  4104   Reflection::resolve_field(field_mirror, receiver, &fd, false, CHECK_NULL);
  4105   jvalue value;
  4106   BasicType type = Reflection::field_get(&value, &fd, receiver);
  4107   oop box = Reflection::box(&value, type, CHECK_NULL);
  4108   return JNIHandles::make_local(env, box);
  4109 JVM_END
  4112 JVM_ENTRY(jvalue, JVM_GetPrimitiveField(JNIEnv *env, jobject field, jobject obj, unsigned char wCode))
  4113   JVMWrapper("JVM_GetPrimitiveField");
  4114   Handle field_mirror(thread, JNIHandles::resolve(field));
  4115   Handle receiver    (thread, JNIHandles::resolve(obj));
  4116   fieldDescriptor fd;
  4117   jvalue value;
  4118   value.j = 0;
  4119   Reflection::resolve_field(field_mirror, receiver, &fd, false, CHECK_(value));
  4120   BasicType type = Reflection::field_get(&value, &fd, receiver);
  4121   BasicType wide_type = (BasicType) wCode;
  4122   if (type != wide_type) {
  4123     Reflection::widen(&value, type, wide_type, CHECK_(value));
  4125   return value;
  4126 JVM_END // should really be JVM_END, but that doesn't work for union types!
  4129 JVM_ENTRY(void, JVM_SetField(JNIEnv *env, jobject field, jobject obj, jobject val))
  4130   JVMWrapper("JVM_SetField");
  4131   Handle field_mirror(thread, JNIHandles::resolve(field));
  4132   Handle receiver    (thread, JNIHandles::resolve(obj));
  4133   oop box = JNIHandles::resolve(val);
  4134   fieldDescriptor fd;
  4135   Reflection::resolve_field(field_mirror, receiver, &fd, true, CHECK);
  4136   BasicType field_type = fd.field_type();
  4137   jvalue value;
  4138   BasicType value_type;
  4139   if (field_type == T_OBJECT || field_type == T_ARRAY) {
  4140     // Make sure we do no unbox e.g. java/lang/Integer instances when storing into an object array
  4141     value_type = Reflection::unbox_for_regular_object(box, &value);
  4142     Reflection::field_set(&value, &fd, receiver, field_type, CHECK);
  4143   } else {
  4144     value_type = Reflection::unbox_for_primitive(box, &value, CHECK);
  4145     Reflection::field_set(&value, &fd, receiver, value_type, CHECK);
  4147 JVM_END
  4150 JVM_ENTRY(void, JVM_SetPrimitiveField(JNIEnv *env, jobject field, jobject obj, jvalue v, unsigned char vCode))
  4151   JVMWrapper("JVM_SetPrimitiveField");
  4152   Handle field_mirror(thread, JNIHandles::resolve(field));
  4153   Handle receiver    (thread, JNIHandles::resolve(obj));
  4154   fieldDescriptor fd;
  4155   Reflection::resolve_field(field_mirror, receiver, &fd, true, CHECK);
  4156   BasicType value_type = (BasicType) vCode;
  4157   Reflection::field_set(&v, &fd, receiver, value_type, CHECK);
  4158 JVM_END
  4161 // Method ///////////////////////////////////////////////////////////////////////////////////////////
  4163 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
  4164   JVMWrapper("JVM_InvokeMethod");
  4165   Handle method_handle;
  4166   if (thread->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
  4167     method_handle = Handle(THREAD, JNIHandles::resolve(method));
  4168     Handle receiver(THREAD, JNIHandles::resolve(obj));
  4169     objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
  4170     oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
  4171     jobject res = JNIHandles::make_local(env, result);
  4172     if (JvmtiExport::should_post_vm_object_alloc()) {
  4173       oop ret_type = java_lang_reflect_Method::return_type(method_handle());
  4174       assert(ret_type != NULL, "sanity check: ret_type oop must not be NULL!");
  4175       if (java_lang_Class::is_primitive(ret_type)) {
  4176         // Only for primitive type vm allocates memory for java object.
  4177         // See box() method.
  4178         JvmtiExport::post_vm_object_alloc(JavaThread::current(), result);
  4181     return res;
  4182   } else {
  4183     THROW_0(vmSymbols::java_lang_StackOverflowError());
  4185 JVM_END
  4188 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
  4189   JVMWrapper("JVM_NewInstanceFromConstructor");
  4190   oop constructor_mirror = JNIHandles::resolve(c);
  4191   objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
  4192   oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
  4193   jobject res = JNIHandles::make_local(env, result);
  4194   if (JvmtiExport::should_post_vm_object_alloc()) {
  4195     JvmtiExport::post_vm_object_alloc(JavaThread::current(), result);
  4197   return res;
  4198 JVM_END
  4200 #endif /* SUPPORT_OLD_REFLECTION */
  4202 // Atomic ///////////////////////////////////////////////////////////////////////////////////////////
  4204 JVM_LEAF(jboolean, JVM_SupportsCX8())
  4205   JVMWrapper("JVM_SupportsCX8");
  4206   return VM_Version::supports_cx8();
  4207 JVM_END
  4210 JVM_ENTRY(jboolean, JVM_CX8Field(JNIEnv *env, jobject obj, jfieldID fid, jlong oldVal, jlong newVal))
  4211   JVMWrapper("JVM_CX8Field");
  4212   jlong res;
  4213   oop             o       = JNIHandles::resolve(obj);
  4214   intptr_t        fldOffs = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid);
  4215   volatile jlong* addr    = (volatile jlong*)((address)o + fldOffs);
  4217   assert(VM_Version::supports_cx8(), "cx8 not supported");
  4218   res = Atomic::cmpxchg(newVal, addr, oldVal);
  4220   return res == oldVal;
  4221 JVM_END
  4223 // DTrace ///////////////////////////////////////////////////////////////////
  4225 JVM_ENTRY(jint, JVM_DTraceGetVersion(JNIEnv* env))
  4226   JVMWrapper("JVM_DTraceGetVersion");
  4227   return (jint)JVM_TRACING_DTRACE_VERSION;
  4228 JVM_END
  4230 JVM_ENTRY(jlong,JVM_DTraceActivate(
  4231     JNIEnv* env, jint version, jstring module_name, jint providers_count,
  4232     JVM_DTraceProvider* providers))
  4233   JVMWrapper("JVM_DTraceActivate");
  4234   return DTraceJSDT::activate(
  4235     version, module_name, providers_count, providers, CHECK_0);
  4236 JVM_END
  4238 JVM_ENTRY(jboolean,JVM_DTraceIsProbeEnabled(JNIEnv* env, jmethodID method))
  4239   JVMWrapper("JVM_DTraceIsProbeEnabled");
  4240   return DTraceJSDT::is_probe_enabled(method);
  4241 JVM_END
  4243 JVM_ENTRY(void,JVM_DTraceDispose(JNIEnv* env, jlong handle))
  4244   JVMWrapper("JVM_DTraceDispose");
  4245   DTraceJSDT::dispose(handle);
  4246 JVM_END
  4248 JVM_ENTRY(jboolean,JVM_DTraceIsSupported(JNIEnv* env))
  4249   JVMWrapper("JVM_DTraceIsSupported");
  4250   return DTraceJSDT::is_supported();
  4251 JVM_END
  4253 // Returns an array of all live Thread objects (VM internal JavaThreads,
  4254 // jvmti agent threads, and JNI attaching threads  are skipped)
  4255 // See CR 6404306 regarding JNI attaching threads
  4256 JVM_ENTRY(jobjectArray, JVM_GetAllThreads(JNIEnv *env, jclass dummy))
  4257   ResourceMark rm(THREAD);
  4258   ThreadsListEnumerator tle(THREAD, false, false);
  4259   JvmtiVMObjectAllocEventCollector oam;
  4261   int num_threads = tle.num_threads();
  4262   objArrayOop r = oopFactory::new_objArray(SystemDictionary::thread_klass(), num_threads, CHECK_NULL);
  4263   objArrayHandle threads_ah(THREAD, r);
  4265   for (int i = 0; i < num_threads; i++) {
  4266     Handle h = tle.get_threadObj(i);
  4267     threads_ah->obj_at_put(i, h());
  4270   return (jobjectArray) JNIHandles::make_local(env, threads_ah());
  4271 JVM_END
  4274 // Support for java.lang.Thread.getStackTrace() and getAllStackTraces() methods
  4275 // Return StackTraceElement[][], each element is the stack trace of a thread in
  4276 // the corresponding entry in the given threads array
  4277 JVM_ENTRY(jobjectArray, JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads))
  4278   JVMWrapper("JVM_DumpThreads");
  4279   JvmtiVMObjectAllocEventCollector oam;
  4281   // Check if threads is null
  4282   if (threads == NULL) {
  4283     THROW_(vmSymbols::java_lang_NullPointerException(), 0);
  4286   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
  4287   objArrayHandle ah(THREAD, a);
  4288   int num_threads = ah->length();
  4289   // check if threads is non-empty array
  4290   if (num_threads == 0) {
  4291     THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0);
  4294   // check if threads is not an array of objects of Thread class
  4295   klassOop k = objArrayKlass::cast(ah->klass())->element_klass();
  4296   if (k != SystemDictionary::thread_klass()) {
  4297     THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0);
  4300   ResourceMark rm(THREAD);
  4302   GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
  4303   for (int i = 0; i < num_threads; i++) {
  4304     oop thread_obj = ah->obj_at(i);
  4305     instanceHandle h(THREAD, (instanceOop) thread_obj);
  4306     thread_handle_array->append(h);
  4309   Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
  4310   return (jobjectArray)JNIHandles::make_local(env, stacktraces());
  4312 JVM_END
  4314 // JVM monitoring and management support
  4315 JVM_ENTRY_NO_ENV(void*, JVM_GetManagement(jint version))
  4316   return Management::get_jmm_interface(version);
  4317 JVM_END
  4319 // com.sun.tools.attach.VirtualMachine agent properties support
  4320 //
  4321 // Initialize the agent properties with the properties maintained in the VM
  4322 JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties))
  4323   JVMWrapper("JVM_InitAgentProperties");
  4324   ResourceMark rm;
  4326   Handle props(THREAD, JNIHandles::resolve_non_null(properties));
  4328   PUTPROP(props, "sun.java.command", Arguments::java_command());
  4329   PUTPROP(props, "sun.jvm.flags", Arguments::jvm_flags());
  4330   PUTPROP(props, "sun.jvm.args", Arguments::jvm_args());
  4331   return properties;
  4332 JVM_END
  4334 JVM_ENTRY(jobjectArray, JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass))
  4336   JVMWrapper("JVM_GetEnclosingMethodInfo");
  4337   JvmtiVMObjectAllocEventCollector oam;
  4339   if (ofClass == NULL) {
  4340     return NULL;
  4342   Handle mirror(THREAD, JNIHandles::resolve_non_null(ofClass));
  4343   // Special handling for primitive objects
  4344   if (java_lang_Class::is_primitive(mirror())) {
  4345     return NULL;
  4347   klassOop k = java_lang_Class::as_klassOop(mirror());
  4348   if (!Klass::cast(k)->oop_is_instance()) {
  4349     return NULL;
  4351   instanceKlassHandle ik_h(THREAD, k);
  4352   int encl_method_class_idx = ik_h->enclosing_method_class_index();
  4353   if (encl_method_class_idx == 0) {
  4354     return NULL;
  4356   objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::object_klass(), 3, CHECK_NULL);
  4357   objArrayHandle dest(THREAD, dest_o);
  4358   klassOop enc_k = ik_h->constants()->klass_at(encl_method_class_idx, CHECK_NULL);
  4359   dest->obj_at_put(0, Klass::cast(enc_k)->java_mirror());
  4360   int encl_method_method_idx = ik_h->enclosing_method_method_index();
  4361   if (encl_method_method_idx != 0) {
  4362     symbolOop sym_o = ik_h->constants()->symbol_at(
  4363                         extract_low_short_from_int(
  4364                           ik_h->constants()->name_and_type_at(encl_method_method_idx)));
  4365     symbolHandle sym(THREAD, sym_o);
  4366     Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
  4367     dest->obj_at_put(1, str());
  4368     sym_o = ik_h->constants()->symbol_at(
  4369               extract_high_short_from_int(
  4370                 ik_h->constants()->name_and_type_at(encl_method_method_idx)));
  4371     sym = symbolHandle(THREAD, sym_o);
  4372     str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
  4373     dest->obj_at_put(2, str());
  4375   return (jobjectArray) JNIHandles::make_local(dest());
  4377 JVM_END
  4379 JVM_ENTRY(jintArray, JVM_GetThreadStateValues(JNIEnv* env,
  4380                                               jint javaThreadState))
  4382   // If new thread states are added in future JDK and VM versions,
  4383   // this should check if the JDK version is compatible with thread
  4384   // states supported by the VM.  Return NULL if not compatible.
  4385   //
  4386   // This function must map the VM java_lang_Thread::ThreadStatus
  4387   // to the Java thread state that the JDK supports.
  4388   //
  4390   typeArrayHandle values_h;
  4391   switch (javaThreadState) {
  4392     case JAVA_THREAD_STATE_NEW : {
  4393       typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL);
  4394       values_h = typeArrayHandle(THREAD, r);
  4395       values_h->int_at_put(0, java_lang_Thread::NEW);
  4396       break;
  4398     case JAVA_THREAD_STATE_RUNNABLE : {
  4399       typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL);
  4400       values_h = typeArrayHandle(THREAD, r);
  4401       values_h->int_at_put(0, java_lang_Thread::RUNNABLE);
  4402       break;
  4404     case JAVA_THREAD_STATE_BLOCKED : {
  4405       typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL);
  4406       values_h = typeArrayHandle(THREAD, r);
  4407       values_h->int_at_put(0, java_lang_Thread::BLOCKED_ON_MONITOR_ENTER);
  4408       break;
  4410     case JAVA_THREAD_STATE_WAITING : {
  4411       typeArrayOop r = oopFactory::new_typeArray(T_INT, 2, CHECK_NULL);
  4412       values_h = typeArrayHandle(THREAD, r);
  4413       values_h->int_at_put(0, java_lang_Thread::IN_OBJECT_WAIT);
  4414       values_h->int_at_put(1, java_lang_Thread::PARKED);
  4415       break;
  4417     case JAVA_THREAD_STATE_TIMED_WAITING : {
  4418       typeArrayOop r = oopFactory::new_typeArray(T_INT, 3, CHECK_NULL);
  4419       values_h = typeArrayHandle(THREAD, r);
  4420       values_h->int_at_put(0, java_lang_Thread::SLEEPING);
  4421       values_h->int_at_put(1, java_lang_Thread::IN_OBJECT_WAIT_TIMED);
  4422       values_h->int_at_put(2, java_lang_Thread::PARKED_TIMED);
  4423       break;
  4425     case JAVA_THREAD_STATE_TERMINATED : {
  4426       typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL);
  4427       values_h = typeArrayHandle(THREAD, r);
  4428       values_h->int_at_put(0, java_lang_Thread::TERMINATED);
  4429       break;
  4431     default:
  4432       // Unknown state - probably incompatible JDK version
  4433       return NULL;
  4436   return (jintArray) JNIHandles::make_local(env, values_h());
  4438 JVM_END
  4441 JVM_ENTRY(jobjectArray, JVM_GetThreadStateNames(JNIEnv* env,
  4442                                                 jint javaThreadState,
  4443                                                 jintArray values))
  4445   // If new thread states are added in future JDK and VM versions,
  4446   // this should check if the JDK version is compatible with thread
  4447   // states supported by the VM.  Return NULL if not compatible.
  4448   //
  4449   // This function must map the VM java_lang_Thread::ThreadStatus
  4450   // to the Java thread state that the JDK supports.
  4451   //
  4453   ResourceMark rm;
  4455   // Check if threads is null
  4456   if (values == NULL) {
  4457     THROW_(vmSymbols::java_lang_NullPointerException(), 0);
  4460   typeArrayOop v = typeArrayOop(JNIHandles::resolve_non_null(values));
  4461   typeArrayHandle values_h(THREAD, v);
  4463   objArrayHandle names_h;
  4464   switch (javaThreadState) {
  4465     case JAVA_THREAD_STATE_NEW : {
  4466       assert(values_h->length() == 1 &&
  4467                values_h->int_at(0) == java_lang_Thread::NEW,
  4468              "Invalid threadStatus value");
  4470       objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
  4471                                                1, /* only 1 substate */
  4472                                                CHECK_NULL);
  4473       names_h = objArrayHandle(THREAD, r);
  4474       Handle name = java_lang_String::create_from_str("NEW", CHECK_NULL);
  4475       names_h->obj_at_put(0, name());
  4476       break;
  4478     case JAVA_THREAD_STATE_RUNNABLE : {
  4479       assert(values_h->length() == 1 &&
  4480                values_h->int_at(0) == java_lang_Thread::RUNNABLE,
  4481              "Invalid threadStatus value");
  4483       objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
  4484                                                1, /* only 1 substate */
  4485                                                CHECK_NULL);
  4486       names_h = objArrayHandle(THREAD, r);
  4487       Handle name = java_lang_String::create_from_str("RUNNABLE", CHECK_NULL);
  4488       names_h->obj_at_put(0, name());
  4489       break;
  4491     case JAVA_THREAD_STATE_BLOCKED : {
  4492       assert(values_h->length() == 1 &&
  4493                values_h->int_at(0) == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER,
  4494              "Invalid threadStatus value");
  4496       objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
  4497                                                1, /* only 1 substate */
  4498                                                CHECK_NULL);
  4499       names_h = objArrayHandle(THREAD, r);
  4500       Handle name = java_lang_String::create_from_str("BLOCKED", CHECK_NULL);
  4501       names_h->obj_at_put(0, name());
  4502       break;
  4504     case JAVA_THREAD_STATE_WAITING : {
  4505       assert(values_h->length() == 2 &&
  4506                values_h->int_at(0) == java_lang_Thread::IN_OBJECT_WAIT &&
  4507                values_h->int_at(1) == java_lang_Thread::PARKED,
  4508              "Invalid threadStatus value");
  4509       objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
  4510                                                2, /* number of substates */
  4511                                                CHECK_NULL);
  4512       names_h = objArrayHandle(THREAD, r);
  4513       Handle name0 = java_lang_String::create_from_str("WAITING.OBJECT_WAIT",
  4514                                                        CHECK_NULL);
  4515       Handle name1 = java_lang_String::create_from_str("WAITING.PARKED",
  4516                                                        CHECK_NULL);
  4517       names_h->obj_at_put(0, name0());
  4518       names_h->obj_at_put(1, name1());
  4519       break;
  4521     case JAVA_THREAD_STATE_TIMED_WAITING : {
  4522       assert(values_h->length() == 3 &&
  4523                values_h->int_at(0) == java_lang_Thread::SLEEPING &&
  4524                values_h->int_at(1) == java_lang_Thread::IN_OBJECT_WAIT_TIMED &&
  4525                values_h->int_at(2) == java_lang_Thread::PARKED_TIMED,
  4526              "Invalid threadStatus value");
  4527       objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
  4528                                                3, /* number of substates */
  4529                                                CHECK_NULL);
  4530       names_h = objArrayHandle(THREAD, r);
  4531       Handle name0 = java_lang_String::create_from_str("TIMED_WAITING.SLEEPING",
  4532                                                        CHECK_NULL);
  4533       Handle name1 = java_lang_String::create_from_str("TIMED_WAITING.OBJECT_WAIT",
  4534                                                        CHECK_NULL);
  4535       Handle name2 = java_lang_String::create_from_str("TIMED_WAITING.PARKED",
  4536                                                        CHECK_NULL);
  4537       names_h->obj_at_put(0, name0());
  4538       names_h->obj_at_put(1, name1());
  4539       names_h->obj_at_put(2, name2());
  4540       break;
  4542     case JAVA_THREAD_STATE_TERMINATED : {
  4543       assert(values_h->length() == 1 &&
  4544                values_h->int_at(0) == java_lang_Thread::TERMINATED,
  4545              "Invalid threadStatus value");
  4546       objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
  4547                                                1, /* only 1 substate */
  4548                                                CHECK_NULL);
  4549       names_h = objArrayHandle(THREAD, r);
  4550       Handle name = java_lang_String::create_from_str("TERMINATED", CHECK_NULL);
  4551       names_h->obj_at_put(0, name());
  4552       break;
  4554     default:
  4555       // Unknown state - probably incompatible JDK version
  4556       return NULL;
  4558   return (jobjectArray) JNIHandles::make_local(env, names_h());
  4560 JVM_END
  4562 JVM_ENTRY(void, JVM_GetVersionInfo(JNIEnv* env, jvm_version_info* info, size_t info_size))
  4564   memset(info, 0, sizeof(info_size));
  4566   info->jvm_version = Abstract_VM_Version::jvm_version();
  4567   info->update_version = 0;          /* 0 in HotSpot Express VM */
  4568   info->special_update_version = 0;  /* 0 in HotSpot Express VM */
  4570   // when we add a new capability in the jvm_version_info struct, we should also
  4571   // consider to expose this new capability in the sun.rt.jvmCapabilities jvmstat
  4572   // counter defined in runtimeService.cpp.
  4573   info->is_attachable = AttachListener::is_attach_supported();
  4574 #ifdef KERNEL
  4575   info->is_kernel_jvm = 1; // true;
  4576 #else  // KERNEL
  4577   info->is_kernel_jvm = 0; // false;
  4578 #endif // KERNEL
  4580 JVM_END

mercurial