< prev index next >

src/hotspot/share/prims/jni.cpp

Print this page

   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #include "precompiled.hpp"
  28 #include "jni.h"

  29 #include "jvm.h"
  30 #include "ci/ciReplay.hpp"
  31 #include "classfile/altHashing.hpp"
  32 #include "classfile/classFileStream.hpp"
  33 #include "classfile/classLoader.hpp"
  34 #include "classfile/classLoadInfo.hpp"
  35 #include "classfile/javaClasses.hpp"
  36 #include "classfile/javaClasses.inline.hpp"
  37 #include "classfile/javaThreadStatus.hpp"
  38 #include "classfile/moduleEntry.hpp"
  39 #include "classfile/modules.hpp"
  40 #include "classfile/symbolTable.hpp"
  41 #include "classfile/systemDictionary.hpp"
  42 #include "classfile/vmClasses.hpp"
  43 #include "classfile/vmSymbols.hpp"
  44 #include "compiler/compiler_globals.hpp"
  45 #include "gc/shared/collectedHeap.hpp"
  46 #include "gc/shared/gcLocker.inline.hpp"
  47 #include "gc/shared/stringdedup/stringDedup.hpp"
  48 #include "interpreter/linkResolver.hpp"
  49 #include "jfr/jfrEvents.hpp"
  50 #include "jfr/support/jfrThreadId.hpp"
  51 #include "logging/log.hpp"
  52 #include "memory/allocation.hpp"
  53 #include "memory/allocation.inline.hpp"
  54 #include "memory/oopFactory.hpp"
  55 #include "memory/resourceArea.hpp"
  56 #include "memory/universe.hpp"
  57 #include "oops/access.inline.hpp"
  58 #include "oops/arrayOop.hpp"


  59 #include "oops/instanceKlass.inline.hpp"
  60 #include "oops/instanceOop.hpp"
  61 #include "oops/klass.inline.hpp"
  62 #include "oops/markWord.hpp"
  63 #include "oops/method.hpp"
  64 #include "oops/objArrayKlass.hpp"
  65 #include "oops/objArrayOop.inline.hpp"
  66 #include "oops/oop.inline.hpp"
  67 #include "oops/symbol.hpp"
  68 #include "oops/typeArrayKlass.hpp"
  69 #include "oops/typeArrayOop.inline.hpp"
  70 #include "prims/jniCheck.hpp"
  71 #include "prims/jniExport.hpp"
  72 #include "prims/jniFastGetField.hpp"
  73 #include "prims/jvm_misc.hpp"
  74 #include "prims/jvmtiExport.hpp"
  75 #include "prims/jvmtiThreadState.hpp"
  76 #include "runtime/atomic.hpp"
  77 #include "runtime/fieldDescriptor.inline.hpp"
  78 #include "runtime/handles.inline.hpp"

 405   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
 406 
 407   // Make sure class is initialized before handing id's out to fields
 408   k1->initialize(CHECK_NULL);
 409 
 410   // First check if this is a static field
 411   if (modifiers & JVM_ACC_STATIC) {
 412     intptr_t offset = InstanceKlass::cast(k1)->field_offset( slot );
 413     JNIid* id = InstanceKlass::cast(k1)->jni_id_for(offset);
 414     assert(id != NULL, "corrupt Field object");
 415     debug_only(id->set_is_static_field_id();)
 416     // A jfieldID for a static field is a JNIid specifying the field holder and the offset within the Klass*
 417     ret = jfieldIDWorkaround::to_static_jfieldID(id);
 418     return ret;
 419   }
 420 
 421   // The slot is the index of the field description in the field-array
 422   // The jfieldID is the offset of the field within the object
 423   // It may also have hash bits for k, if VerifyJNIFields is turned on.
 424   intptr_t offset = InstanceKlass::cast(k1)->field_offset( slot );

 425   assert(InstanceKlass::cast(k1)->contains_field_offset(offset), "stay within object");
 426   ret = jfieldIDWorkaround::to_instance_jfieldID(k1, offset);
 427   return ret;
 428 JNI_END
 429 
 430 
 431 DT_RETURN_MARK_DECL(ToReflectedMethod, jobject
 432                     , HOTSPOT_JNI_TOREFLECTEDMETHOD_RETURN(_ret_ref));
 433 
 434 JNI_ENTRY(jobject, jni_ToReflectedMethod(JNIEnv *env, jclass cls, jmethodID method_id, jboolean isStatic))
 435   HOTSPOT_JNI_TOREFLECTEDMETHOD_ENTRY(env, cls, (uintptr_t) method_id, isStatic);
 436 
 437   jobject ret = NULL;
 438   DT_RETURN_MARK(ToReflectedMethod, jobject, (const jobject&)ret);
 439 
 440   methodHandle m (THREAD, Method::resolve_jmethod_id(method_id));
 441   assert(m->is_static() == (isStatic != 0), "jni_ToReflectedMethod access flags doesn't match");
 442   oop reflection_method;
 443   if (m->is_initializer()) {
 444     reflection_method = Reflection::new_constructor(m, CHECK_NULL);
 445   } else {
 446     reflection_method = Reflection::new_method(m, false, CHECK_NULL);
 447   }
 448   ret = JNIHandles::make_local(THREAD, reflection_method);
 449   return ret;
 450 JNI_END
 451 
 452 DT_RETURN_MARK_DECL(GetSuperclass, jclass
 453                     , HOTSPOT_JNI_GETSUPERCLASS_RETURN(_ret_ref));
 454 
 455 JNI_ENTRY(jclass, jni_GetSuperclass(JNIEnv *env, jclass sub))
 456   HOTSPOT_JNI_GETSUPERCLASS_ENTRY(env, sub);
 457 
 458   jclass obj = NULL;
 459   DT_RETURN_MARK(GetSuperclass, jclass, (const jclass&)obj);
 460 
 461   oop mirror = JNIHandles::resolve_non_null(sub);
 462   // primitive classes return NULL
 463   if (java_lang_Class::is_primitive(mirror)) return NULL;

 479          "java_super computation depends on interface, array, other super");
 480   obj = (super == NULL) ? NULL : (jclass) JNIHandles::make_local(THREAD, super->java_mirror());
 481   return obj;
 482 JNI_END
 483 
 484 JNI_ENTRY_NO_PRESERVE(jboolean, jni_IsAssignableFrom(JNIEnv *env, jclass sub, jclass super))
 485   HOTSPOT_JNI_ISASSIGNABLEFROM_ENTRY(env, sub, super);
 486 
 487   oop sub_mirror   = JNIHandles::resolve_non_null(sub);
 488   oop super_mirror = JNIHandles::resolve_non_null(super);
 489   if (java_lang_Class::is_primitive(sub_mirror) ||
 490       java_lang_Class::is_primitive(super_mirror)) {
 491     jboolean ret = (sub_mirror == super_mirror);
 492 
 493     HOTSPOT_JNI_ISASSIGNABLEFROM_RETURN(ret);
 494     return ret;
 495   }
 496   Klass* sub_klass   = java_lang_Class::as_Klass(sub_mirror);
 497   Klass* super_klass = java_lang_Class::as_Klass(super_mirror);
 498   assert(sub_klass != NULL && super_klass != NULL, "invalid arguments to jni_IsAssignableFrom");
 499   jboolean ret = sub_klass->is_subtype_of(super_klass) ?
 500                    JNI_TRUE : JNI_FALSE;
 501 









 502   HOTSPOT_JNI_ISASSIGNABLEFROM_RETURN(ret);
 503   return ret;
 504 JNI_END
 505 
 506 
 507 DT_RETURN_MARK_DECL(Throw, jint
 508                     , HOTSPOT_JNI_THROW_RETURN(_ret_ref));
 509 
 510 JNI_ENTRY(jint, jni_Throw(JNIEnv *env, jthrowable obj))
 511   HOTSPOT_JNI_THROW_ENTRY(env, obj);
 512 
 513   jint ret = JNI_OK;
 514   DT_RETURN_MARK(Throw, jint, (const jint&)ret);
 515 
 516   THROW_OOP_(JNIHandles::resolve(obj), JNI_OK);
 517   ShouldNotReachHere();
 518   return 0;  // Mute compiler.
 519 JNI_END
 520 
 521 

 786   }
 787 
 788   friend class SignatureIterator;  // so do_parameters_on can call do_type
 789   void do_type(BasicType type) {
 790     switch (type) {
 791     // these are coerced to int when using va_arg
 792     case T_BYTE:
 793     case T_CHAR:
 794     case T_SHORT:
 795     case T_INT:         push_int(va_arg(_ap, jint)); break;
 796     case T_BOOLEAN:     push_boolean((jboolean) va_arg(_ap, jint)); break;
 797 
 798     // each of these paths is exercised by the various jck Call[Static,Nonvirtual,][Void,Int,..]Method[A,V,] tests
 799 
 800     case T_LONG:        push_long(va_arg(_ap, jlong)); break;
 801     // float is coerced to double w/ va_arg
 802     case T_FLOAT:       push_float((jfloat) va_arg(_ap, jdouble)); break;
 803     case T_DOUBLE:      push_double(va_arg(_ap, jdouble)); break;
 804 
 805     case T_ARRAY:
 806     case T_OBJECT:      push_object(va_arg(_ap, jobject)); break;

 807     default:            ShouldNotReachHere();
 808     }
 809   }
 810 
 811  public:
 812   JNI_ArgumentPusherVaArg(jmethodID method_id, va_list rap)
 813       : JNI_ArgumentPusher(Method::resolve_jmethod_id(method_id)) {
 814     set_ap(rap);
 815   }
 816 
 817   ~JNI_ArgumentPusherVaArg() {
 818     va_end(_ap);
 819   }
 820 
 821   virtual void push_arguments_on(JavaCallArguments* arguments) {
 822     _arguments = arguments;
 823     do_parameters_on(this);
 824   }
 825 };
 826 
 827 
 828 class JNI_ArgumentPusherArray : public JNI_ArgumentPusher {
 829  protected:
 830   const jvalue *_ap;
 831 
 832   inline void set_ap(const jvalue *rap) { _ap = rap; }
 833 
 834   friend class SignatureIterator;  // so do_parameters_on can call do_type
 835   void do_type(BasicType type) {
 836     switch (type) {
 837     case T_CHAR:        push_int((_ap++)->c); break;
 838     case T_SHORT:       push_int((_ap++)->s); break;
 839     case T_BYTE:        push_int((_ap++)->b); break;
 840     case T_INT:         push_int((_ap++)->i); break;
 841     case T_BOOLEAN:     push_boolean((_ap++)->z); break;
 842     case T_LONG:        push_long((_ap++)->j); break;
 843     case T_FLOAT:       push_float((_ap++)->f); break;
 844     case T_DOUBLE:      push_double((_ap++)->d); break;
 845     case T_ARRAY:
 846     case T_OBJECT:      push_object((_ap++)->l); break;

 847     default:            ShouldNotReachHere();
 848     }
 849   }
 850 
 851  public:
 852   JNI_ArgumentPusherArray(jmethodID method_id, const jvalue *rap)
 853       : JNI_ArgumentPusher(Method::resolve_jmethod_id(method_id)) {
 854     set_ap(rap);
 855   }
 856 
 857   virtual void push_arguments_on(JavaCallArguments* arguments) {
 858     _arguments = arguments;
 859     do_parameters_on(this);
 860   }
 861 };
 862 
 863 
 864 enum JNICallType {
 865   JNI_STATIC,
 866   JNI_VIRTUAL,

 962 JNI_ENTRY(jobject, jni_AllocObject(JNIEnv *env, jclass clazz))
 963   HOTSPOT_JNI_ALLOCOBJECT_ENTRY(env, clazz);
 964 
 965   jobject ret = NULL;
 966   DT_RETURN_MARK(AllocObject, jobject, (const jobject&)ret);
 967 
 968   instanceOop i = InstanceKlass::allocate_instance(JNIHandles::resolve_non_null(clazz), CHECK_NULL);
 969   ret = JNIHandles::make_local(THREAD, i);
 970   return ret;
 971 JNI_END
 972 
 973 DT_RETURN_MARK_DECL(NewObjectA, jobject
 974                     , HOTSPOT_JNI_NEWOBJECTA_RETURN(_ret_ref));
 975 
 976 JNI_ENTRY(jobject, jni_NewObjectA(JNIEnv *env, jclass clazz, jmethodID methodID, const jvalue *args))
 977   HOTSPOT_JNI_NEWOBJECTA_ENTRY(env, clazz, (uintptr_t) methodID);
 978 
 979   jobject obj = NULL;
 980   DT_RETURN_MARK(NewObjectA, jobject, (const jobject&)obj);
 981 
 982   instanceOop i = InstanceKlass::allocate_instance(JNIHandles::resolve_non_null(clazz), CHECK_NULL);
 983   obj = JNIHandles::make_local(THREAD, i);
 984   JavaValue jvalue(T_VOID);
 985   JNI_ArgumentPusherArray ap(methodID, args);
 986   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);














 987   return obj;
 988 JNI_END
 989 
 990 
 991 DT_RETURN_MARK_DECL(NewObjectV, jobject
 992                     , HOTSPOT_JNI_NEWOBJECTV_RETURN(_ret_ref));
 993 
 994 JNI_ENTRY(jobject, jni_NewObjectV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args))
 995   HOTSPOT_JNI_NEWOBJECTV_ENTRY(env, clazz, (uintptr_t) methodID);
 996 
 997   jobject obj = NULL;
 998   DT_RETURN_MARK(NewObjectV, jobject, (const jobject&)obj);
 999 
1000   instanceOop i = InstanceKlass::allocate_instance(JNIHandles::resolve_non_null(clazz), CHECK_NULL);
1001   obj = JNIHandles::make_local(THREAD, i);
1002   JavaValue jvalue(T_VOID);
1003   JNI_ArgumentPusherVaArg ap(methodID, args);
1004   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);














1005   return obj;
1006 JNI_END
1007 
1008 
1009 DT_RETURN_MARK_DECL(NewObject, jobject
1010                     , HOTSPOT_JNI_NEWOBJECT_RETURN(_ret_ref));
1011 
1012 JNI_ENTRY(jobject, jni_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...))
1013   HOTSPOT_JNI_NEWOBJECT_ENTRY(env, clazz, (uintptr_t) methodID);
1014 
1015   jobject obj = NULL;
1016   DT_RETURN_MARK(NewObject, jobject, (const jobject&)obj);
1017 
1018   instanceOop i = InstanceKlass::allocate_instance(JNIHandles::resolve_non_null(clazz), CHECK_NULL);
1019   obj = JNIHandles::make_local(THREAD, i);
1020   va_list args;
1021   va_start(args, methodID);
1022   JavaValue jvalue(T_VOID);
1023   JNI_ArgumentPusherVaArg ap(methodID, args);
1024   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
1025   va_end(args);

















1026   return obj;
1027 JNI_END
1028 
1029 
1030 JNI_ENTRY(jclass, jni_GetObjectClass(JNIEnv *env, jobject obj))
1031   HOTSPOT_JNI_GETOBJECTCLASS_ENTRY(env, obj);
1032 
1033   Klass* k = JNIHandles::resolve_non_null(obj)->klass();
1034   jclass ret =
1035     (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
1036 
1037   HOTSPOT_JNI_GETOBJECTCLASS_RETURN(ret);
1038   return ret;
1039 JNI_END
1040 
1041 JNI_ENTRY_NO_PRESERVE(jboolean, jni_IsInstanceOf(JNIEnv *env, jobject obj, jclass clazz))
1042   HOTSPOT_JNI_ISINSTANCEOF_ENTRY(env, obj, clazz);
1043 
1044   jboolean ret = JNI_TRUE;
1045   if (obj != NULL) {

1756   // table.  If they're not there, the field doesn't exist.
1757   TempNewSymbol fieldname = SymbolTable::probe(name, (int)strlen(name));
1758   TempNewSymbol signame = SymbolTable::probe(sig, (int)strlen(sig));
1759   if (fieldname == NULL || signame == NULL) {
1760     ResourceMark rm;
1761     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), err_msg("%s.%s %s", k->external_name(), name, sig));
1762   }
1763 
1764   // Make sure class is initialized before handing id's out to fields
1765   k->initialize(CHECK_NULL);
1766 
1767   fieldDescriptor fd;
1768   if (!k->is_instance_klass() ||
1769       !InstanceKlass::cast(k)->find_field(fieldname, signame, false, &fd)) {
1770     ResourceMark rm;
1771     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), err_msg("%s.%s %s", k->external_name(), name, sig));
1772   }
1773 
1774   // A jfieldID for a non-static field is simply the offset of the field within the instanceOop
1775   // It may also have hash bits for k, if VerifyJNIFields is turned on.
1776   ret = jfieldIDWorkaround::to_instance_jfieldID(k, fd.offset());
1777   return ret;
1778 JNI_END
1779 
1780 
1781 JNI_ENTRY(jobject, jni_GetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID))
1782   HOTSPOT_JNI_GETOBJECTFIELD_ENTRY(env, obj, (uintptr_t) fieldID);
1783   oop o = JNIHandles::resolve_non_null(obj);
1784   Klass* k = o->klass();
1785   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);

1786   // Keep JVMTI addition small and only check enabled flag here.
1787   // jni_GetField_probe() assumes that is okay to create handles.
1788   if (JvmtiExport::should_post_field_access()) {
1789     o = JvmtiExport::jni_GetField_probe(thread, obj, o, k, fieldID, false);
1790   }
1791   oop loaded_obj = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_load_at(o, offset);
1792   jobject ret = JNIHandles::make_local(THREAD, loaded_obj);










1793   HOTSPOT_JNI_GETOBJECTFIELD_RETURN(ret);
1794   return ret;
1795 JNI_END
1796 
1797 
1798 
1799 #define DEFINE_GETFIELD(Return,Fieldname,Result \
1800   , EntryProbe, ReturnProbe) \
1801 \
1802   DT_RETURN_MARK_DECL_FOR(Result, Get##Result##Field, Return \
1803   , ReturnProbe); \
1804 \
1805 JNI_ENTRY_NO_PRESERVE(Return, jni_Get##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID)) \
1806 \
1807   EntryProbe; \
1808   Return ret = 0;\
1809   DT_RETURN_MARK_FOR(Result, Get##Result##Field, Return, (const Return&)ret);\
1810 \
1811   oop o = JNIHandles::resolve_non_null(obj); \
1812   Klass* k = o->klass(); \

1864   return (address)jni_GetLongField;
1865 }
1866 address jni_GetFloatField_addr() {
1867   return (address)jni_GetFloatField;
1868 }
1869 address jni_GetDoubleField_addr() {
1870   return (address)jni_GetDoubleField;
1871 }
1872 
1873 JNI_ENTRY_NO_PRESERVE(void, jni_SetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID, jobject value))
1874   HOTSPOT_JNI_SETOBJECTFIELD_ENTRY(env, obj, (uintptr_t) fieldID, value);
1875   oop o = JNIHandles::resolve_non_null(obj);
1876   Klass* k = o->klass();
1877   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
1878   // Keep JVMTI addition small and only check enabled flag here.
1879   if (JvmtiExport::should_post_field_modification()) {
1880     jvalue field_value;
1881     field_value.l = value;
1882     o = JvmtiExport::jni_SetField_probe(thread, obj, o, k, fieldID, false, JVM_SIGNATURE_CLASS, (jvalue *)&field_value);
1883   }
1884   HeapAccess<ON_UNKNOWN_OOP_REF>::oop_store_at(o, offset, JNIHandles::resolve(value));











1885   HOTSPOT_JNI_SETOBJECTFIELD_RETURN();
1886 JNI_END
1887 
1888 
1889 #define DEFINE_SETFIELD(Argument,Fieldname,Result,SigType,unionType \
1890                         , EntryProbe, ReturnProbe) \
1891 \
1892 JNI_ENTRY_NO_PRESERVE(void, jni_Set##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID, Argument value)) \
1893 \
1894   EntryProbe; \
1895 \
1896   oop o = JNIHandles::resolve_non_null(obj); \
1897   Klass* k = o->klass(); \
1898   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);  \
1899   /* Keep JVMTI addition small and only check enabled flag here.       */ \
1900   if (JvmtiExport::should_post_field_modification()) { \
1901     jvalue field_value; \
1902     field_value.unionType = value; \
1903     o = JvmtiExport::jni_SetField_probe(thread, obj, o, k, fieldID, false, SigType, (jvalue *)&field_value); \
1904   } \

2281   Klass* ak = ek->array_klass(CHECK_NULL);
2282   ObjArrayKlass::cast(ak)->initialize(CHECK_NULL);
2283   objArrayOop result = ObjArrayKlass::cast(ak)->allocate(length, CHECK_NULL);
2284   oop initial_value = JNIHandles::resolve(initialElement);
2285   if (initial_value != NULL) {  // array already initialized with NULL
2286     for (int index = 0; index < length; index++) {
2287       result->obj_at_put(index, initial_value);
2288     }
2289   }
2290   ret = (jobjectArray) JNIHandles::make_local(THREAD, result);
2291   return ret;
2292 JNI_END
2293 
2294 DT_RETURN_MARK_DECL(GetObjectArrayElement, jobject
2295                     , HOTSPOT_JNI_GETOBJECTARRAYELEMENT_RETURN(_ret_ref));
2296 
2297 JNI_ENTRY(jobject, jni_GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index))
2298  HOTSPOT_JNI_GETOBJECTARRAYELEMENT_ENTRY(env, array, index);
2299   jobject ret = NULL;
2300   DT_RETURN_MARK(GetObjectArrayElement, jobject, (const jobject&)ret);
2301   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2302   if (a->is_within_bounds(index)) {
2303     ret = JNIHandles::make_local(THREAD, a->obj_at(index));
2304     return ret;









2305   } else {
2306     ResourceMark rm(THREAD);
2307     stringStream ss;
2308     ss.print("Index %d out of bounds for length %d", index, a->length());
2309     THROW_MSG_0(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2310   }


2311 JNI_END
2312 
2313 DT_VOID_RETURN_MARK_DECL(SetObjectArrayElement
2314                          , HOTSPOT_JNI_SETOBJECTARRAYELEMENT_RETURN());
2315 
2316 JNI_ENTRY(void, jni_SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject value))
2317  HOTSPOT_JNI_SETOBJECTARRAYELEMENT_ENTRY(env, array, index, value);
2318   DT_VOID_RETURN_MARK(SetObjectArrayElement);
2319 
2320   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2321   oop v = JNIHandles::resolve(value);
2322   if (a->is_within_bounds(index)) {
2323     if (v == NULL || v->is_a(ObjArrayKlass::cast(a->klass())->element_klass())) {
2324       a->obj_at_put(index, v);
2325     } else {
2326       ResourceMark rm(THREAD);
2327       stringStream ss;
2328       Klass *bottom_kl = ObjArrayKlass::cast(a->klass())->bottom_klass();
2329       ss.print("type mismatch: can not store %s to %s[%d]",
2330                v->klass()->external_name(),
2331                bottom_kl->is_typeArray_klass() ? type2name_tab[ArrayKlass::cast(bottom_kl)->element_type()] : bottom_kl->external_name(),
2332                index);
2333       for (int dims = ArrayKlass::cast(a->klass())->dimension(); dims > 1; --dims) {
2334         ss.print("[]");
2335       }
2336       THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string());
2337     }
2338   } else {
2339     ResourceMark rm(THREAD);
2340     stringStream ss;
2341     ss.print("Index %d out of bounds for length %d", index, a->length());
2342     THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2343   }



























2344 JNI_END
2345 
2346 
2347 
2348 #define DEFINE_NEWSCALARARRAY(Return,Allocator,Result \
2349                               ,EntryProbe,ReturnProbe)  \
2350 \
2351   DT_RETURN_MARK_DECL(New##Result##Array, Return \
2352                       , ReturnProbe); \
2353 \
2354 JNI_ENTRY(Return, \
2355           jni_New##Result##Array(JNIEnv *env, jsize len)) \
2356   EntryProbe; \
2357   Return ret = NULL;\
2358   DT_RETURN_MARK(New##Result##Array, Return, (const Return&)ret);\
2359 \
2360   oop obj= oopFactory::Allocator(len, CHECK_NULL); \
2361   ret = (Return) JNIHandles::make_local(THREAD, obj); \
2362   return ret;\
2363 JNI_END

3119 JNI_LEAF(jint, jni_GetVersion(JNIEnv *env))
3120   HOTSPOT_JNI_GETVERSION_ENTRY(env);
3121   HOTSPOT_JNI_GETVERSION_RETURN(CurrentVersion);
3122   return CurrentVersion;
3123 JNI_END
3124 
3125 extern struct JavaVM_ main_vm;
3126 
3127 JNI_LEAF(jint, jni_GetJavaVM(JNIEnv *env, JavaVM **vm))
3128   HOTSPOT_JNI_GETJAVAVM_ENTRY(env, (void **) vm);
3129   *vm  = (JavaVM *)(&main_vm);
3130   HOTSPOT_JNI_GETJAVAVM_RETURN(JNI_OK);
3131   return JNI_OK;
3132 JNI_END
3133 
3134 
3135 JNI_ENTRY(jobject, jni_GetModule(JNIEnv* env, jclass clazz))
3136   return Modules::get_module(clazz, THREAD);
3137 JNI_END
3138 
3139 
3140 // Structure containing all jni functions
3141 struct JNINativeInterface_ jni_NativeInterface = {
3142     NULL,
3143     NULL,
3144     NULL,
3145 
3146     NULL,
3147 
3148     jni_GetVersion,
3149 
3150     jni_DefineClass,
3151     jni_FindClass,
3152 
3153     jni_FromReflectedMethod,
3154     jni_FromReflectedField,
3155 
3156     jni_ToReflectedMethod,
3157 
3158     jni_GetSuperclass,
3159     jni_IsAssignableFrom,

3403     jni_ReleasePrimitiveArrayCritical,
3404 
3405     jni_GetStringCritical,
3406     jni_ReleaseStringCritical,
3407 
3408     jni_NewWeakGlobalRef,
3409     jni_DeleteWeakGlobalRef,
3410 
3411     jni_ExceptionCheck,
3412 
3413     jni_NewDirectByteBuffer,
3414     jni_GetDirectBufferAddress,
3415     jni_GetDirectBufferCapacity,
3416 
3417     // New 1_6 features
3418 
3419     jni_GetObjectRefType,
3420 
3421     // Module features
3422 
3423     jni_GetModule

3424 };
3425 
3426 
3427 // For jvmti use to modify jni function table.
3428 // Java threads in native contiues to run until it is transitioned
3429 // to VM at safepoint. Before the transition or before it is blocked
3430 // for safepoint it may access jni function table. VM could crash if
3431 // any java thread access the jni function table in the middle of memcpy.
3432 // To avoid this each function pointers are copied automically.
3433 void copy_jni_function_table(const struct JNINativeInterface_ *new_jni_NativeInterface) {
3434   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
3435   intptr_t *a = (intptr_t *) jni_functions();
3436   intptr_t *b = (intptr_t *) new_jni_NativeInterface;
3437   for (uint i=0; i <  sizeof(struct JNINativeInterface_)/sizeof(void *); i++) {
3438     Atomic::store(a++, *b++);
3439   }
3440 }
3441 
3442 void quicken_jni_functions() {
3443   // Replace Get<Primitive>Field with fast versions

   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #include "precompiled.hpp"
  28 #include "jni.h"
  29 #include "jni.h"
  30 #include "jvm.h"
  31 #include "ci/ciReplay.hpp"
  32 #include "classfile/altHashing.hpp"
  33 #include "classfile/classFileStream.hpp"
  34 #include "classfile/classLoader.hpp"
  35 #include "classfile/classLoadInfo.hpp"
  36 #include "classfile/javaClasses.hpp"
  37 #include "classfile/javaClasses.inline.hpp"
  38 #include "classfile/javaThreadStatus.hpp"
  39 #include "classfile/moduleEntry.hpp"
  40 #include "classfile/modules.hpp"
  41 #include "classfile/symbolTable.hpp"
  42 #include "classfile/systemDictionary.hpp"
  43 #include "classfile/vmClasses.hpp"
  44 #include "classfile/vmSymbols.hpp"
  45 #include "compiler/compiler_globals.hpp"
  46 #include "gc/shared/collectedHeap.hpp"
  47 #include "gc/shared/gcLocker.inline.hpp"
  48 #include "gc/shared/stringdedup/stringDedup.hpp"
  49 #include "interpreter/linkResolver.hpp"
  50 #include "jfr/jfrEvents.hpp"
  51 #include "jfr/support/jfrThreadId.hpp"
  52 #include "logging/log.hpp"
  53 #include "memory/allocation.hpp"
  54 #include "memory/allocation.inline.hpp"
  55 #include "memory/oopFactory.hpp"
  56 #include "memory/resourceArea.hpp"
  57 #include "memory/universe.hpp"
  58 #include "oops/access.inline.hpp"
  59 #include "oops/arrayOop.hpp"
  60 #include "oops/flatArrayOop.inline.hpp"
  61 #include "oops/inlineKlass.inline.hpp"
  62 #include "oops/instanceKlass.inline.hpp"
  63 #include "oops/instanceOop.hpp"
  64 #include "oops/klass.inline.hpp"
  65 #include "oops/markWord.hpp"
  66 #include "oops/method.hpp"
  67 #include "oops/objArrayKlass.hpp"
  68 #include "oops/objArrayOop.inline.hpp"
  69 #include "oops/oop.inline.hpp"
  70 #include "oops/symbol.hpp"
  71 #include "oops/typeArrayKlass.hpp"
  72 #include "oops/typeArrayOop.inline.hpp"
  73 #include "prims/jniCheck.hpp"
  74 #include "prims/jniExport.hpp"
  75 #include "prims/jniFastGetField.hpp"
  76 #include "prims/jvm_misc.hpp"
  77 #include "prims/jvmtiExport.hpp"
  78 #include "prims/jvmtiThreadState.hpp"
  79 #include "runtime/atomic.hpp"
  80 #include "runtime/fieldDescriptor.inline.hpp"
  81 #include "runtime/handles.inline.hpp"

 408   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
 409 
 410   // Make sure class is initialized before handing id's out to fields
 411   k1->initialize(CHECK_NULL);
 412 
 413   // First check if this is a static field
 414   if (modifiers & JVM_ACC_STATIC) {
 415     intptr_t offset = InstanceKlass::cast(k1)->field_offset( slot );
 416     JNIid* id = InstanceKlass::cast(k1)->jni_id_for(offset);
 417     assert(id != NULL, "corrupt Field object");
 418     debug_only(id->set_is_static_field_id();)
 419     // A jfieldID for a static field is a JNIid specifying the field holder and the offset within the Klass*
 420     ret = jfieldIDWorkaround::to_static_jfieldID(id);
 421     return ret;
 422   }
 423 
 424   // The slot is the index of the field description in the field-array
 425   // The jfieldID is the offset of the field within the object
 426   // It may also have hash bits for k, if VerifyJNIFields is turned on.
 427   intptr_t offset = InstanceKlass::cast(k1)->field_offset( slot );
 428   bool is_inlined = InstanceKlass::cast(k1)->field_is_inlined(slot);
 429   assert(InstanceKlass::cast(k1)->contains_field_offset(offset), "stay within object");
 430   ret = jfieldIDWorkaround::to_instance_jfieldID(k1, offset, is_inlined);
 431   return ret;
 432 JNI_END
 433 
 434 
 435 DT_RETURN_MARK_DECL(ToReflectedMethod, jobject
 436                     , HOTSPOT_JNI_TOREFLECTEDMETHOD_RETURN(_ret_ref));
 437 
 438 JNI_ENTRY(jobject, jni_ToReflectedMethod(JNIEnv *env, jclass cls, jmethodID method_id, jboolean isStatic))
 439   HOTSPOT_JNI_TOREFLECTEDMETHOD_ENTRY(env, cls, (uintptr_t) method_id, isStatic);
 440 
 441   jobject ret = NULL;
 442   DT_RETURN_MARK(ToReflectedMethod, jobject, (const jobject&)ret);
 443 
 444   methodHandle m (THREAD, Method::resolve_jmethod_id(method_id));
 445   assert(m->is_static() == (isStatic != 0), "jni_ToReflectedMethod access flags doesn't match");
 446   oop reflection_method;
 447   if (m->is_object_constructor() || m->is_static_init_factory()) {
 448     reflection_method = Reflection::new_constructor(m, CHECK_NULL);
 449   } else {
 450     reflection_method = Reflection::new_method(m, false, CHECK_NULL);
 451   }
 452   ret = JNIHandles::make_local(THREAD, reflection_method);
 453   return ret;
 454 JNI_END
 455 
 456 DT_RETURN_MARK_DECL(GetSuperclass, jclass
 457                     , HOTSPOT_JNI_GETSUPERCLASS_RETURN(_ret_ref));
 458 
 459 JNI_ENTRY(jclass, jni_GetSuperclass(JNIEnv *env, jclass sub))
 460   HOTSPOT_JNI_GETSUPERCLASS_ENTRY(env, sub);
 461 
 462   jclass obj = NULL;
 463   DT_RETURN_MARK(GetSuperclass, jclass, (const jclass&)obj);
 464 
 465   oop mirror = JNIHandles::resolve_non_null(sub);
 466   // primitive classes return NULL
 467   if (java_lang_Class::is_primitive(mirror)) return NULL;

 483          "java_super computation depends on interface, array, other super");
 484   obj = (super == NULL) ? NULL : (jclass) JNIHandles::make_local(THREAD, super->java_mirror());
 485   return obj;
 486 JNI_END
 487 
 488 JNI_ENTRY_NO_PRESERVE(jboolean, jni_IsAssignableFrom(JNIEnv *env, jclass sub, jclass super))
 489   HOTSPOT_JNI_ISASSIGNABLEFROM_ENTRY(env, sub, super);
 490 
 491   oop sub_mirror   = JNIHandles::resolve_non_null(sub);
 492   oop super_mirror = JNIHandles::resolve_non_null(super);
 493   if (java_lang_Class::is_primitive(sub_mirror) ||
 494       java_lang_Class::is_primitive(super_mirror)) {
 495     jboolean ret = (sub_mirror == super_mirror);
 496 
 497     HOTSPOT_JNI_ISASSIGNABLEFROM_RETURN(ret);
 498     return ret;
 499   }
 500   Klass* sub_klass   = java_lang_Class::as_Klass(sub_mirror);
 501   Klass* super_klass = java_lang_Class::as_Klass(super_mirror);
 502   assert(sub_klass != NULL && super_klass != NULL, "invalid arguments to jni_IsAssignableFrom");
 503   jboolean ret;
 504   if (sub_klass == super_klass && sub_klass->is_inline_klass()) {
 505     // val type is a subtype of ref type
 506     InlineKlass* ik = InlineKlass::cast(sub_klass);
 507     if (sub_mirror == super_mirror || (ik->val_mirror() == sub_mirror && ik->ref_mirror() == super_mirror)) {
 508       ret = JNI_TRUE;
 509     } else {
 510       ret = JNI_FALSE;
 511     }
 512   } else {
 513     ret = sub_klass->is_subtype_of(super_klass) ? JNI_TRUE : JNI_FALSE;
 514   }
 515   HOTSPOT_JNI_ISASSIGNABLEFROM_RETURN(ret);
 516   return ret;
 517 JNI_END
 518 
 519 
 520 DT_RETURN_MARK_DECL(Throw, jint
 521                     , HOTSPOT_JNI_THROW_RETURN(_ret_ref));
 522 
 523 JNI_ENTRY(jint, jni_Throw(JNIEnv *env, jthrowable obj))
 524   HOTSPOT_JNI_THROW_ENTRY(env, obj);
 525 
 526   jint ret = JNI_OK;
 527   DT_RETURN_MARK(Throw, jint, (const jint&)ret);
 528 
 529   THROW_OOP_(JNIHandles::resolve(obj), JNI_OK);
 530   ShouldNotReachHere();
 531   return 0;  // Mute compiler.
 532 JNI_END
 533 
 534 

 799   }
 800 
 801   friend class SignatureIterator;  // so do_parameters_on can call do_type
 802   void do_type(BasicType type) {
 803     switch (type) {
 804     // these are coerced to int when using va_arg
 805     case T_BYTE:
 806     case T_CHAR:
 807     case T_SHORT:
 808     case T_INT:         push_int(va_arg(_ap, jint)); break;
 809     case T_BOOLEAN:     push_boolean((jboolean) va_arg(_ap, jint)); break;
 810 
 811     // each of these paths is exercised by the various jck Call[Static,Nonvirtual,][Void,Int,..]Method[A,V,] tests
 812 
 813     case T_LONG:        push_long(va_arg(_ap, jlong)); break;
 814     // float is coerced to double w/ va_arg
 815     case T_FLOAT:       push_float((jfloat) va_arg(_ap, jdouble)); break;
 816     case T_DOUBLE:      push_double(va_arg(_ap, jdouble)); break;
 817 
 818     case T_ARRAY:
 819     case T_OBJECT:
 820     case T_INLINE_TYPE: push_object(va_arg(_ap, jobject)); break;
 821     default:            ShouldNotReachHere();
 822     }
 823   }
 824 
 825  public:
 826   JNI_ArgumentPusherVaArg(jmethodID method_id, va_list rap)
 827       : JNI_ArgumentPusher(Method::resolve_jmethod_id(method_id)) {
 828     set_ap(rap);
 829   }
 830 
 831   ~JNI_ArgumentPusherVaArg() {
 832     va_end(_ap);
 833   }
 834 
 835   virtual void push_arguments_on(JavaCallArguments* arguments) {
 836     _arguments = arguments;
 837     do_parameters_on(this);
 838   }
 839 };
 840 
 841 
 842 class JNI_ArgumentPusherArray : public JNI_ArgumentPusher {
 843  protected:
 844   const jvalue *_ap;
 845 
 846   inline void set_ap(const jvalue *rap) { _ap = rap; }
 847 
 848   friend class SignatureIterator;  // so do_parameters_on can call do_type
 849   void do_type(BasicType type) {
 850     switch (type) {
 851     case T_CHAR:        push_int((_ap++)->c); break;
 852     case T_SHORT:       push_int((_ap++)->s); break;
 853     case T_BYTE:        push_int((_ap++)->b); break;
 854     case T_INT:         push_int((_ap++)->i); break;
 855     case T_BOOLEAN:     push_boolean((_ap++)->z); break;
 856     case T_LONG:        push_long((_ap++)->j); break;
 857     case T_FLOAT:       push_float((_ap++)->f); break;
 858     case T_DOUBLE:      push_double((_ap++)->d); break;
 859     case T_ARRAY:
 860     case T_OBJECT:
 861     case T_INLINE_TYPE: push_object((_ap++)->l); break;
 862     default:            ShouldNotReachHere();
 863     }
 864   }
 865 
 866  public:
 867   JNI_ArgumentPusherArray(jmethodID method_id, const jvalue *rap)
 868       : JNI_ArgumentPusher(Method::resolve_jmethod_id(method_id)) {
 869     set_ap(rap);
 870   }
 871 
 872   virtual void push_arguments_on(JavaCallArguments* arguments) {
 873     _arguments = arguments;
 874     do_parameters_on(this);
 875   }
 876 };
 877 
 878 
 879 enum JNICallType {
 880   JNI_STATIC,
 881   JNI_VIRTUAL,

 977 JNI_ENTRY(jobject, jni_AllocObject(JNIEnv *env, jclass clazz))
 978   HOTSPOT_JNI_ALLOCOBJECT_ENTRY(env, clazz);
 979 
 980   jobject ret = NULL;
 981   DT_RETURN_MARK(AllocObject, jobject, (const jobject&)ret);
 982 
 983   instanceOop i = InstanceKlass::allocate_instance(JNIHandles::resolve_non_null(clazz), CHECK_NULL);
 984   ret = JNIHandles::make_local(THREAD, i);
 985   return ret;
 986 JNI_END
 987 
 988 DT_RETURN_MARK_DECL(NewObjectA, jobject
 989                     , HOTSPOT_JNI_NEWOBJECTA_RETURN(_ret_ref));
 990 
 991 JNI_ENTRY(jobject, jni_NewObjectA(JNIEnv *env, jclass clazz, jmethodID methodID, const jvalue *args))
 992   HOTSPOT_JNI_NEWOBJECTA_ENTRY(env, clazz, (uintptr_t) methodID);
 993 
 994   jobject obj = NULL;
 995   DT_RETURN_MARK(NewObjectA, jobject, (const jobject&)obj);
 996 
 997   oop clazzoop = JNIHandles::resolve_non_null(clazz);
 998   Klass* k = java_lang_Class::as_Klass(clazzoop);
 999   if (k == NULL) {
1000     ResourceMark rm(THREAD);
1001     THROW_(vmSymbols::java_lang_InstantiationException(), NULL);
1002   }
1003 
1004   if (!k->is_inline_klass()) {
1005     instanceOop i = InstanceKlass::allocate_instance(clazzoop, CHECK_NULL);
1006     obj = JNIHandles::make_local(THREAD, i);
1007     JavaValue jvalue(T_VOID);
1008     JNI_ArgumentPusherArray ap(methodID, args);
1009     jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
1010   } else {
1011     JavaValue jvalue(T_INLINE_TYPE);
1012     JNI_ArgumentPusherArray ap(methodID, args);
1013     jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_NULL);
1014     obj = jvalue.get_jobject();
1015   }
1016   return obj;
1017   JNI_END
1018 
1019 
1020 DT_RETURN_MARK_DECL(NewObjectV, jobject
1021                     , HOTSPOT_JNI_NEWOBJECTV_RETURN(_ret_ref));
1022 
1023 JNI_ENTRY(jobject, jni_NewObjectV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args))
1024   HOTSPOT_JNI_NEWOBJECTV_ENTRY(env, clazz, (uintptr_t) methodID);
1025 
1026   jobject obj = NULL;
1027   DT_RETURN_MARK(NewObjectV, jobject, (const jobject&)obj);
1028 
1029   oop clazzoop = JNIHandles::resolve_non_null(clazz);
1030   Klass* k = java_lang_Class::as_Klass(clazzoop);
1031   if (k == NULL) {
1032     ResourceMark rm(THREAD);
1033     THROW_(vmSymbols::java_lang_InstantiationException(), NULL);
1034   }
1035 
1036   if (!k->is_inline_klass()) {
1037     instanceOop i = InstanceKlass::allocate_instance(clazzoop, CHECK_NULL);
1038     obj = JNIHandles::make_local(THREAD, i);
1039     JavaValue jvalue(T_VOID);
1040     JNI_ArgumentPusherVaArg ap(methodID, args);
1041     jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
1042   } else {
1043     JavaValue jvalue(T_INLINE_TYPE);
1044     JNI_ArgumentPusherVaArg ap(methodID, args);
1045     jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_NULL);
1046     obj = jvalue.get_jobject();
1047   }
1048   return obj;
1049 JNI_END
1050 
1051 
1052 DT_RETURN_MARK_DECL(NewObject, jobject
1053                     , HOTSPOT_JNI_NEWOBJECT_RETURN(_ret_ref));
1054 
1055 JNI_ENTRY(jobject, jni_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...))
1056   HOTSPOT_JNI_NEWOBJECT_ENTRY(env, clazz, (uintptr_t) methodID);
1057 
1058   jobject obj = NULL;
1059   DT_RETURN_MARK(NewObject, jobject, (const jobject&)obj);
1060 
1061   oop clazzoop = JNIHandles::resolve_non_null(clazz);
1062   Klass* k = java_lang_Class::as_Klass(clazzoop);
1063   if (k == NULL) {
1064     ResourceMark rm(THREAD);
1065     THROW_(vmSymbols::java_lang_InstantiationException(), NULL);
1066   }
1067 
1068   if (!k->is_inline_klass()) {
1069     instanceOop i = InstanceKlass::allocate_instance(clazzoop, CHECK_NULL);
1070     obj = JNIHandles::make_local(THREAD, i);
1071     va_list args;
1072     va_start(args, methodID);
1073     JavaValue jvalue(T_VOID);
1074     JNI_ArgumentPusherVaArg ap(methodID, args);
1075     jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
1076     va_end(args);
1077   } else {
1078     va_list args;
1079     va_start(args, methodID);
1080     JavaValue jvalue(T_INLINE_TYPE);
1081     JNI_ArgumentPusherVaArg ap(methodID, args);
1082     jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_NULL);
1083     va_end(args);
1084     obj = jvalue.get_jobject();
1085   }
1086   return obj;
1087 JNI_END
1088 
1089 
1090 JNI_ENTRY(jclass, jni_GetObjectClass(JNIEnv *env, jobject obj))
1091   HOTSPOT_JNI_GETOBJECTCLASS_ENTRY(env, obj);
1092 
1093   Klass* k = JNIHandles::resolve_non_null(obj)->klass();
1094   jclass ret =
1095     (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
1096 
1097   HOTSPOT_JNI_GETOBJECTCLASS_RETURN(ret);
1098   return ret;
1099 JNI_END
1100 
1101 JNI_ENTRY_NO_PRESERVE(jboolean, jni_IsInstanceOf(JNIEnv *env, jobject obj, jclass clazz))
1102   HOTSPOT_JNI_ISINSTANCEOF_ENTRY(env, obj, clazz);
1103 
1104   jboolean ret = JNI_TRUE;
1105   if (obj != NULL) {

1816   // table.  If they're not there, the field doesn't exist.
1817   TempNewSymbol fieldname = SymbolTable::probe(name, (int)strlen(name));
1818   TempNewSymbol signame = SymbolTable::probe(sig, (int)strlen(sig));
1819   if (fieldname == NULL || signame == NULL) {
1820     ResourceMark rm;
1821     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), err_msg("%s.%s %s", k->external_name(), name, sig));
1822   }
1823 
1824   // Make sure class is initialized before handing id's out to fields
1825   k->initialize(CHECK_NULL);
1826 
1827   fieldDescriptor fd;
1828   if (!k->is_instance_klass() ||
1829       !InstanceKlass::cast(k)->find_field(fieldname, signame, false, &fd)) {
1830     ResourceMark rm;
1831     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), err_msg("%s.%s %s", k->external_name(), name, sig));
1832   }
1833 
1834   // A jfieldID for a non-static field is simply the offset of the field within the instanceOop
1835   // It may also have hash bits for k, if VerifyJNIFields is turned on.
1836   ret = jfieldIDWorkaround::to_instance_jfieldID(k, fd.offset(), fd.is_inlined());
1837   return ret;
1838 JNI_END
1839 
1840 
1841 JNI_ENTRY(jobject, jni_GetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID))
1842   HOTSPOT_JNI_GETOBJECTFIELD_ENTRY(env, obj, (uintptr_t) fieldID);
1843   oop o = JNIHandles::resolve_non_null(obj);
1844   Klass* k = o->klass();
1845   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
1846   oop res = NULL;
1847   // Keep JVMTI addition small and only check enabled flag here.
1848   // jni_GetField_probe() assumes that is okay to create handles.
1849   if (JvmtiExport::should_post_field_access()) {
1850     o = JvmtiExport::jni_GetField_probe(thread, obj, o, k, fieldID, false);
1851   }
1852   if (!jfieldIDWorkaround::is_inlined_jfieldID(fieldID)) {
1853     res = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_load_at(o, offset);
1854   } else {
1855     assert(k->is_instance_klass(), "Only instance can have inlined fields");
1856     InstanceKlass* ik = InstanceKlass::cast(k);
1857     fieldDescriptor fd;
1858     ik->find_field_from_offset(offset, false, &fd);  // performance bottleneck
1859     InstanceKlass* holder = fd.field_holder();
1860     InlineKlass* field_vklass = InlineKlass::cast(holder->get_inline_type_field_klass(fd.index()));
1861     res = field_vklass->read_inlined_field(o, ik->field_offset(fd.index()), CHECK_NULL);
1862   }
1863   jobject ret = JNIHandles::make_local(THREAD, res);
1864   HOTSPOT_JNI_GETOBJECTFIELD_RETURN(ret);
1865   return ret;
1866 JNI_END
1867 
1868 
1869 
1870 #define DEFINE_GETFIELD(Return,Fieldname,Result \
1871   , EntryProbe, ReturnProbe) \
1872 \
1873   DT_RETURN_MARK_DECL_FOR(Result, Get##Result##Field, Return \
1874   , ReturnProbe); \
1875 \
1876 JNI_ENTRY_NO_PRESERVE(Return, jni_Get##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID)) \
1877 \
1878   EntryProbe; \
1879   Return ret = 0;\
1880   DT_RETURN_MARK_FOR(Result, Get##Result##Field, Return, (const Return&)ret);\
1881 \
1882   oop o = JNIHandles::resolve_non_null(obj); \
1883   Klass* k = o->klass(); \

1935   return (address)jni_GetLongField;
1936 }
1937 address jni_GetFloatField_addr() {
1938   return (address)jni_GetFloatField;
1939 }
1940 address jni_GetDoubleField_addr() {
1941   return (address)jni_GetDoubleField;
1942 }
1943 
1944 JNI_ENTRY_NO_PRESERVE(void, jni_SetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID, jobject value))
1945   HOTSPOT_JNI_SETOBJECTFIELD_ENTRY(env, obj, (uintptr_t) fieldID, value);
1946   oop o = JNIHandles::resolve_non_null(obj);
1947   Klass* k = o->klass();
1948   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
1949   // Keep JVMTI addition small and only check enabled flag here.
1950   if (JvmtiExport::should_post_field_modification()) {
1951     jvalue field_value;
1952     field_value.l = value;
1953     o = JvmtiExport::jni_SetField_probe(thread, obj, o, k, fieldID, false, JVM_SIGNATURE_CLASS, (jvalue *)&field_value);
1954   }
1955   if (!jfieldIDWorkaround::is_inlined_jfieldID(fieldID)) {
1956     HeapAccess<ON_UNKNOWN_OOP_REF>::oop_store_at(o, offset, JNIHandles::resolve(value));
1957   } else {
1958     assert(k->is_instance_klass(), "Only instances can have inlined fields");
1959     InstanceKlass* ik = InstanceKlass::cast(k);
1960     fieldDescriptor fd;
1961     ik->find_field_from_offset(offset, false, &fd);
1962     InstanceKlass* holder = fd.field_holder();
1963     InlineKlass* vklass = InlineKlass::cast(holder->get_inline_type_field_klass(fd.index()));
1964     oop v = JNIHandles::resolve_non_null(value);
1965     vklass->write_inlined_field(o, offset, v, CHECK);
1966   }
1967   HOTSPOT_JNI_SETOBJECTFIELD_RETURN();
1968 JNI_END
1969 
1970 
1971 #define DEFINE_SETFIELD(Argument,Fieldname,Result,SigType,unionType \
1972                         , EntryProbe, ReturnProbe) \
1973 \
1974 JNI_ENTRY_NO_PRESERVE(void, jni_Set##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID, Argument value)) \
1975 \
1976   EntryProbe; \
1977 \
1978   oop o = JNIHandles::resolve_non_null(obj); \
1979   Klass* k = o->klass(); \
1980   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);  \
1981   /* Keep JVMTI addition small and only check enabled flag here.       */ \
1982   if (JvmtiExport::should_post_field_modification()) { \
1983     jvalue field_value; \
1984     field_value.unionType = value; \
1985     o = JvmtiExport::jni_SetField_probe(thread, obj, o, k, fieldID, false, SigType, (jvalue *)&field_value); \
1986   } \

2363   Klass* ak = ek->array_klass(CHECK_NULL);
2364   ObjArrayKlass::cast(ak)->initialize(CHECK_NULL);
2365   objArrayOop result = ObjArrayKlass::cast(ak)->allocate(length, CHECK_NULL);
2366   oop initial_value = JNIHandles::resolve(initialElement);
2367   if (initial_value != NULL) {  // array already initialized with NULL
2368     for (int index = 0; index < length; index++) {
2369       result->obj_at_put(index, initial_value);
2370     }
2371   }
2372   ret = (jobjectArray) JNIHandles::make_local(THREAD, result);
2373   return ret;
2374 JNI_END
2375 
2376 DT_RETURN_MARK_DECL(GetObjectArrayElement, jobject
2377                     , HOTSPOT_JNI_GETOBJECTARRAYELEMENT_RETURN(_ret_ref));
2378 
2379 JNI_ENTRY(jobject, jni_GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index))
2380  HOTSPOT_JNI_GETOBJECTARRAYELEMENT_ENTRY(env, array, index);
2381   jobject ret = NULL;
2382   DT_RETURN_MARK(GetObjectArrayElement, jobject, (const jobject&)ret);
2383   oop res = NULL;
2384   arrayOop arr((arrayOop)JNIHandles::resolve_non_null(array));
2385   if (arr->is_within_bounds(index)) {
2386     if (arr->is_flatArray()) {
2387       flatArrayOop a = flatArrayOop(JNIHandles::resolve_non_null(array));
2388       flatArrayHandle vah(thread, a);
2389       res = flatArrayOopDesc::value_alloc_copy_from_index(vah, index, CHECK_NULL);
2390       assert(res != NULL, "Must be set in one of two paths above");
2391     } else {
2392       assert(arr->is_objArray(), "If not a valueArray. must be an objArray");
2393       objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2394       res = a->obj_at(index);
2395     }
2396   } else {
2397     ResourceMark rm(THREAD);
2398     stringStream ss;
2399     ss.print("Index %d out of bounds for length %d", index,arr->length());
2400     THROW_MSG_0(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2401   }
2402   ret = JNIHandles::make_local(THREAD, res);
2403   return ret;
2404 JNI_END
2405 
2406 DT_VOID_RETURN_MARK_DECL(SetObjectArrayElement
2407                          , HOTSPOT_JNI_SETOBJECTARRAYELEMENT_RETURN());
2408 
2409 JNI_ENTRY(void, jni_SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject value))
2410  HOTSPOT_JNI_SETOBJECTARRAYELEMENT_ENTRY(env, array, index, value);
2411   DT_VOID_RETURN_MARK(SetObjectArrayElement);
2412 
2413    bool oob = false;
2414    int length = -1;
2415    oop res = NULL;
2416    arrayOop arr((arrayOop)JNIHandles::resolve_non_null(array));
2417    if (arr->is_within_bounds(index)) {
2418      if (arr->is_flatArray()) {
2419        flatArrayOop a = flatArrayOop(JNIHandles::resolve_non_null(array));
2420        oop v = JNIHandles::resolve(value);
2421        FlatArrayKlass* vaklass = FlatArrayKlass::cast(a->klass());
2422        InlineKlass* element_vklass = vaklass->element_klass();
2423        if (v != NULL && v->is_a(element_vklass)) {
2424          a->value_copy_to_index(v, index);
2425        } else {
2426          ResourceMark rm(THREAD);
2427          stringStream ss;
2428          Klass *kl = FlatArrayKlass::cast(a->klass());
2429          ss.print("type mismatch: can not store %s to %s[%d]",
2430              v->klass()->external_name(),
2431              kl->external_name(),
2432              index);
2433          for (int dims = ArrayKlass::cast(a->klass())->dimension(); dims > 1; --dims) {
2434            ss.print("[]");
2435          }
2436          THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string());
2437        }
2438      } else {
2439        assert(arr->is_objArray(), "If not a valueArray. must be an objArray");
2440        objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2441        oop v = JNIHandles::resolve(value);
2442        if (v == NULL || v->is_a(ObjArrayKlass::cast(a->klass())->element_klass())) {
2443          a->obj_at_put(index, v);
2444        } else {
2445          ResourceMark rm(THREAD);
2446          stringStream ss;
2447          Klass *bottom_kl = ObjArrayKlass::cast(a->klass())->bottom_klass();
2448          ss.print("type mismatch: can not store %s to %s[%d]",
2449              v->klass()->external_name(),
2450              bottom_kl->is_typeArray_klass() ? type2name_tab[ArrayKlass::cast(bottom_kl)->element_type()] : bottom_kl->external_name(),
2451                  index);
2452          for (int dims = ArrayKlass::cast(a->klass())->dimension(); dims > 1; --dims) {
2453            ss.print("[]");
2454          }
2455          THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string());
2456        }
2457      }
2458    } else {
2459      ResourceMark rm(THREAD);
2460      stringStream ss;
2461      ss.print("Index %d out of bounds for length %d", index, arr->length());
2462      THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2463    }
2464 JNI_END
2465 
2466 
2467 
2468 #define DEFINE_NEWSCALARARRAY(Return,Allocator,Result \
2469                               ,EntryProbe,ReturnProbe)  \
2470 \
2471   DT_RETURN_MARK_DECL(New##Result##Array, Return \
2472                       , ReturnProbe); \
2473 \
2474 JNI_ENTRY(Return, \
2475           jni_New##Result##Array(JNIEnv *env, jsize len)) \
2476   EntryProbe; \
2477   Return ret = NULL;\
2478   DT_RETURN_MARK(New##Result##Array, Return, (const Return&)ret);\
2479 \
2480   oop obj= oopFactory::Allocator(len, CHECK_NULL); \
2481   ret = (Return) JNIHandles::make_local(THREAD, obj); \
2482   return ret;\
2483 JNI_END

3239 JNI_LEAF(jint, jni_GetVersion(JNIEnv *env))
3240   HOTSPOT_JNI_GETVERSION_ENTRY(env);
3241   HOTSPOT_JNI_GETVERSION_RETURN(CurrentVersion);
3242   return CurrentVersion;
3243 JNI_END
3244 
3245 extern struct JavaVM_ main_vm;
3246 
3247 JNI_LEAF(jint, jni_GetJavaVM(JNIEnv *env, JavaVM **vm))
3248   HOTSPOT_JNI_GETJAVAVM_ENTRY(env, (void **) vm);
3249   *vm  = (JavaVM *)(&main_vm);
3250   HOTSPOT_JNI_GETJAVAVM_RETURN(JNI_OK);
3251   return JNI_OK;
3252 JNI_END
3253 
3254 
3255 JNI_ENTRY(jobject, jni_GetModule(JNIEnv* env, jclass clazz))
3256   return Modules::get_module(clazz, THREAD);
3257 JNI_END
3258 

3259 // Structure containing all jni functions
3260 struct JNINativeInterface_ jni_NativeInterface = {
3261     NULL,
3262     NULL,
3263     NULL,
3264 
3265     NULL,
3266 
3267     jni_GetVersion,
3268 
3269     jni_DefineClass,
3270     jni_FindClass,
3271 
3272     jni_FromReflectedMethod,
3273     jni_FromReflectedField,
3274 
3275     jni_ToReflectedMethod,
3276 
3277     jni_GetSuperclass,
3278     jni_IsAssignableFrom,

3522     jni_ReleasePrimitiveArrayCritical,
3523 
3524     jni_GetStringCritical,
3525     jni_ReleaseStringCritical,
3526 
3527     jni_NewWeakGlobalRef,
3528     jni_DeleteWeakGlobalRef,
3529 
3530     jni_ExceptionCheck,
3531 
3532     jni_NewDirectByteBuffer,
3533     jni_GetDirectBufferAddress,
3534     jni_GetDirectBufferCapacity,
3535 
3536     // New 1_6 features
3537 
3538     jni_GetObjectRefType,
3539 
3540     // Module features
3541 
3542     jni_GetModule,
3543 
3544 };
3545 
3546 
3547 // For jvmti use to modify jni function table.
3548 // Java threads in native contiues to run until it is transitioned
3549 // to VM at safepoint. Before the transition or before it is blocked
3550 // for safepoint it may access jni function table. VM could crash if
3551 // any java thread access the jni function table in the middle of memcpy.
3552 // To avoid this each function pointers are copied automically.
3553 void copy_jni_function_table(const struct JNINativeInterface_ *new_jni_NativeInterface) {
3554   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
3555   intptr_t *a = (intptr_t *) jni_functions();
3556   intptr_t *b = (intptr_t *) new_jni_NativeInterface;
3557   for (uint i=0; i <  sizeof(struct JNINativeInterface_)/sizeof(void *); i++) {
3558     Atomic::store(a++, *b++);
3559   }
3560 }
3561 
3562 void quicken_jni_functions() {
3563   // Replace Get<Primitive>Field with fast versions
< prev index next >