< prev index next >

src/share/vm/prims/unsafe.cpp

Print this page




 828 // The bytecodes for U2 should refer to U1 by a symbolic name (doesn't matter what the name is).
 829 // The CONSTANT_Class entry for that name can be patched to refer directly to U1.
 830 
 831 // This allows, for example, U2 to use U1 as a superclass or super-interface, or as
 832 // an outer class (so that U2 is an anonymous inner class of anonymous U1).
 833 // It is not possible for a named class, or an older anonymous class, to refer by
 834 // name (via its CP) to a newer anonymous class.
 835 
 836 // CP patching may also be used to modify (i.e., hack) the names of methods, classes,
 837 // or type descriptors used in the loaded anonymous class.
 838 
 839 // Finally, CP patching may be used to introduce "live" objects into the constant pool,
 840 // instead of "dead" strings.  A compiled statement like println((Object)"hello") can
 841 // be changed to println(greeting), where greeting is an arbitrary object created before
 842 // the anonymous class is loaded.  This is useful in dynamic languages, in which
 843 // various kinds of metaobjects must be introduced as constants into bytecode.
 844 // Note the cast (Object), which tells the verifier to expect an arbitrary object,
 845 // not just a literal string.  For such ldc instructions, the verifier uses the
 846 // type Object instead of String, if the loaded constant is not in fact a String.
 847 
 848 static instanceKlassHandle
 849 Unsafe_DefineAnonymousClass_impl(JNIEnv *env,
 850                                  jclass host_class, jbyteArray data, jobjectArray cp_patches_jh,
 851                                  u1** temp_alloc,
 852                                  TRAPS) {
 853   assert(host_class != NULL, "host_class must not be NULL");
 854   assert(data != NULL, "data must not be NULL");
 855 
 856   if (UsePerfData) {
 857     ClassLoader::unsafe_defineClassCallCounter()->inc();
 858   }
 859 
 860   jint length = typeArrayOop(JNIHandles::resolve_non_null(data))->length();
 861   assert(length >= 0, "class_bytes_length must not be negative: %d", length);
 862 
 863   int class_bytes_length = (int) length;
 864 
 865   u1* class_bytes = NEW_C_HEAP_ARRAY(u1, length, mtInternal);
 866   if (class_bytes == NULL) {
 867     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 868   }


 915 
 916         cp_patches->at_put(i, patch);
 917       }
 918     }
 919   }
 920 
 921   ClassFileStream st(class_bytes, class_bytes_length, host_source, ClassFileStream::verify);
 922 
 923   Symbol* no_class_name = NULL;
 924   Klass* anonk = SystemDictionary::parse_stream(no_class_name,
 925                                                 host_loader,
 926                                                 host_domain,
 927                                                 &st,
 928                                                 InstanceKlass::cast(host_klass),
 929                                                 cp_patches,
 930                                                 CHECK_NULL);
 931   if (anonk == NULL) {
 932     return NULL;
 933   }
 934 
 935   return instanceKlassHandle(THREAD, anonk);
 936 }
 937 
 938 UNSAFE_ENTRY(jclass, Unsafe_DefineAnonymousClass0(JNIEnv *env, jobject unsafe, jclass host_class, jbyteArray data, jobjectArray cp_patches_jh)) {
 939   ResourceMark rm(THREAD);
 940 
 941   instanceKlassHandle anon_klass;
 942   jobject res_jh = NULL;
 943   u1* temp_alloc = NULL;
 944 
 945   anon_klass = Unsafe_DefineAnonymousClass_impl(env, host_class, data, cp_patches_jh, &temp_alloc, THREAD);
 946   if (anon_klass() != NULL) {
 947     res_jh = JNIHandles::make_local(env, anon_klass->java_mirror());
 948   }
 949 
 950   // try/finally clause:
 951   if (temp_alloc != NULL) {
 952     FREE_C_HEAP_ARRAY(u1, temp_alloc);
 953   }
 954 
 955   // The anonymous class loader data has been artificially been kept alive to
 956   // this point.   The mirror and any instances of this class have to keep
 957   // it alive afterwards.
 958   if (anon_klass() != NULL) {
 959     anon_klass->class_loader_data()->dec_keep_alive();
 960   }
 961 
 962   // let caller initialize it as needed...
 963 
 964   return (jclass) res_jh;
 965 } UNSAFE_END
 966 
 967 
 968 
 969 UNSAFE_ENTRY(void, Unsafe_ThrowException(JNIEnv *env, jobject unsafe, jthrowable thr)) {
 970   ThreadToNativeFromVM ttnfv(thread);
 971   env->Throw(thr);
 972 } UNSAFE_END
 973 
 974 // JSR166 ------------------------------------------------------------------
 975 
 976 UNSAFE_ENTRY(jobject, Unsafe_CompareAndExchangeObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) {
 977   oop x = JNIHandles::resolve(x_h);
 978   oop e = JNIHandles::resolve(e_h);




 828 // The bytecodes for U2 should refer to U1 by a symbolic name (doesn't matter what the name is).
 829 // The CONSTANT_Class entry for that name can be patched to refer directly to U1.
 830 
 831 // This allows, for example, U2 to use U1 as a superclass or super-interface, or as
 832 // an outer class (so that U2 is an anonymous inner class of anonymous U1).
 833 // It is not possible for a named class, or an older anonymous class, to refer by
 834 // name (via its CP) to a newer anonymous class.
 835 
 836 // CP patching may also be used to modify (i.e., hack) the names of methods, classes,
 837 // or type descriptors used in the loaded anonymous class.
 838 
 839 // Finally, CP patching may be used to introduce "live" objects into the constant pool,
 840 // instead of "dead" strings.  A compiled statement like println((Object)"hello") can
 841 // be changed to println(greeting), where greeting is an arbitrary object created before
 842 // the anonymous class is loaded.  This is useful in dynamic languages, in which
 843 // various kinds of metaobjects must be introduced as constants into bytecode.
 844 // Note the cast (Object), which tells the verifier to expect an arbitrary object,
 845 // not just a literal string.  For such ldc instructions, the verifier uses the
 846 // type Object instead of String, if the loaded constant is not in fact a String.
 847 
 848 static InstanceKlass*
 849 Unsafe_DefineAnonymousClass_impl(JNIEnv *env,
 850                                  jclass host_class, jbyteArray data, jobjectArray cp_patches_jh,
 851                                  u1** temp_alloc,
 852                                  TRAPS) {
 853   assert(host_class != NULL, "host_class must not be NULL");
 854   assert(data != NULL, "data must not be NULL");
 855 
 856   if (UsePerfData) {
 857     ClassLoader::unsafe_defineClassCallCounter()->inc();
 858   }
 859 
 860   jint length = typeArrayOop(JNIHandles::resolve_non_null(data))->length();
 861   assert(length >= 0, "class_bytes_length must not be negative: %d", length);
 862 
 863   int class_bytes_length = (int) length;
 864 
 865   u1* class_bytes = NEW_C_HEAP_ARRAY(u1, length, mtInternal);
 866   if (class_bytes == NULL) {
 867     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 868   }


 915 
 916         cp_patches->at_put(i, patch);
 917       }
 918     }
 919   }
 920 
 921   ClassFileStream st(class_bytes, class_bytes_length, host_source, ClassFileStream::verify);
 922 
 923   Symbol* no_class_name = NULL;
 924   Klass* anonk = SystemDictionary::parse_stream(no_class_name,
 925                                                 host_loader,
 926                                                 host_domain,
 927                                                 &st,
 928                                                 InstanceKlass::cast(host_klass),
 929                                                 cp_patches,
 930                                                 CHECK_NULL);
 931   if (anonk == NULL) {
 932     return NULL;
 933   }
 934 
 935   return InstanceKlass::cast(anonk);
 936 }
 937 
 938 UNSAFE_ENTRY(jclass, Unsafe_DefineAnonymousClass0(JNIEnv *env, jobject unsafe, jclass host_class, jbyteArray data, jobjectArray cp_patches_jh)) {
 939   ResourceMark rm(THREAD);
 940 

 941   jobject res_jh = NULL;
 942   u1* temp_alloc = NULL;
 943 
 944   InstanceKlass* anon_klass = Unsafe_DefineAnonymousClass_impl(env, host_class, data, cp_patches_jh, &temp_alloc, THREAD);
 945   if (anon_klass != NULL) {
 946     res_jh = JNIHandles::make_local(env, anon_klass->java_mirror());
 947   }
 948 
 949   // try/finally clause:
 950   if (temp_alloc != NULL) {
 951     FREE_C_HEAP_ARRAY(u1, temp_alloc);
 952   }
 953 
 954   // The anonymous class loader data has been artificially been kept alive to
 955   // this point.   The mirror and any instances of this class have to keep
 956   // it alive afterwards.
 957   if (anon_klass != NULL) {
 958     anon_klass->class_loader_data()->dec_keep_alive();
 959   }
 960 
 961   // let caller initialize it as needed...
 962 
 963   return (jclass) res_jh;
 964 } UNSAFE_END
 965 
 966 
 967 
 968 UNSAFE_ENTRY(void, Unsafe_ThrowException(JNIEnv *env, jobject unsafe, jthrowable thr)) {
 969   ThreadToNativeFromVM ttnfv(thread);
 970   env->Throw(thr);
 971 } UNSAFE_END
 972 
 973 // JSR166 ------------------------------------------------------------------
 974 
 975 UNSAFE_ENTRY(jobject, Unsafe_CompareAndExchangeObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) {
 976   oop x = JNIHandles::resolve(x_h);
 977   oop e = JNIHandles::resolve(e_h);


< prev index next >