50 Array<Method*>* VM_RedefineClasses::_new_methods = NULL;
51 Method** VM_RedefineClasses::_matching_old_methods = NULL;
52 Method** VM_RedefineClasses::_matching_new_methods = NULL;
53 Method** VM_RedefineClasses::_deleted_methods = NULL;
54 Method** VM_RedefineClasses::_added_methods = NULL;
55 int VM_RedefineClasses::_matching_methods_length = 0;
56 int VM_RedefineClasses::_deleted_methods_length = 0;
57 int VM_RedefineClasses::_added_methods_length = 0;
58 Klass* VM_RedefineClasses::_the_class_oop = NULL;
59
60
61 VM_RedefineClasses::VM_RedefineClasses(jint class_count,
62 const jvmtiClassDefinition *class_defs,
63 JvmtiClassLoadKind class_load_kind) {
64 _class_count = class_count;
65 _class_defs = class_defs;
66 _class_load_kind = class_load_kind;
67 _res = JVMTI_ERROR_NONE;
68 }
69
70 bool VM_RedefineClasses::doit_prologue() {
71 if (_class_count == 0) {
72 _res = JVMTI_ERROR_NONE;
73 return false;
74 }
75 if (_class_defs == NULL) {
76 _res = JVMTI_ERROR_NULL_POINTER;
77 return false;
78 }
79 for (int i = 0; i < _class_count; i++) {
80 if (_class_defs[i].klass == NULL) {
81 _res = JVMTI_ERROR_INVALID_CLASS;
82 return false;
83 }
84 if (_class_defs[i].class_byte_count == 0) {
85 _res = JVMTI_ERROR_INVALID_CLASS_FORMAT;
86 return false;
87 }
88 if (_class_defs[i].class_bytes == NULL) {
89 _res = JVMTI_ERROR_NULL_POINTER;
90 return false;
91 }
92 }
93
94 // Start timer after all the sanity checks; not quite accurate, but
95 // better than adding a bunch of stop() calls.
96 RC_TIMER_START(_timer_vm_op_prologue);
97
98 // We first load new class versions in the prologue, because somewhere down the
99 // call chain it is required that the current thread is a Java thread.
100 _res = load_new_class_versions(Thread::current());
101 if (_res != JVMTI_ERROR_NONE) {
102 // free any successfully created classes, since none are redefined
103 for (int i = 0; i < _class_count; i++) {
104 if (_scratch_classes[i] != NULL) {
105 ClassLoaderData* cld = _scratch_classes[i]->class_loader_data();
106 // Free the memory for this class at class unloading time. Not before
107 // because CMS might think this is still live.
108 cld->add_to_deallocate_list((InstanceKlass*)_scratch_classes[i]);
109 }
110 }
111 // Free os::malloc allocated memory in load_new_class_version.
112 os::free(_scratch_classes);
113 RC_TIMER_STOP(_timer_vm_op_prologue);
114 return false;
115 }
116
117 RC_TIMER_STOP(_timer_vm_op_prologue);
118 return true;
119 }
120
121 void VM_RedefineClasses::doit() {
122 Thread *thread = Thread::current();
123
124 if (UseSharedSpaces) {
125 // Sharing is enabled so we remap the shared readonly space to
126 // shared readwrite, private just in case we need to redefine
127 // a shared class. We do the remap during the doit() phase of
3963 // internally as an InstanceKlass. These special instanceKlasses
3964 // share the constant pool of the class that "implements" the
3965 // interface. By sharing the constant pool, the method holder of a
3966 // miranda method is the class that "implements" the interface. In a
3967 // non-redefine situation, the subtype check works fine. However, if
3968 // the old constant pool's pool holder is modified, then the check
3969 // fails because there is no class hierarchy relationship between the
3970 // vtable's class and "the new class".
3971
3972 old_constants->set_pool_holder(scratch_class());
3973 #endif
3974
3975 // track number of methods that are EMCP for add_previous_version() call below
3976 int emcp_method_count = check_methods_and_mark_as_obsolete();
3977 transfer_old_native_function_registrations(the_class);
3978
3979 // The class file bytes from before any retransformable agents mucked
3980 // with them was cached on the scratch class, move to the_class.
3981 // Note: we still want to do this if nothing needed caching since it
3982 // should get cleared in the_class too.
3983 if (the_class->get_cached_class_file_bytes() == 0) {
3984 // the_class doesn't have a cache yet so copy it
3985 the_class->set_cached_class_file(scratch_class->get_cached_class_file());
3986 }
3987 else if (scratch_class->get_cached_class_file_bytes() !=
3988 the_class->get_cached_class_file_bytes()) {
3989 // The same class can be present twice in the scratch classes list or there
3990 // are multiple concurrent RetransformClasses calls on different threads.
3991 // In such cases we have to deallocate scratch_class cached_class_file.
3992 os::free(scratch_class->get_cached_class_file());
3993 }
3994
3995 // NULL out in scratch class to not delete twice. The class to be redefined
3996 // always owns these bytes.
3997 scratch_class->set_cached_class_file(NULL);
3998
3999 // Replace inner_classes
4000 Array<u2>* old_inner_classes = the_class->inner_classes();
4001 the_class->set_inner_classes(scratch_class->inner_classes());
4002 scratch_class->set_inner_classes(old_inner_classes);
4003
4004 // Initialize the vtable and interface table after
4005 // methods have been rewritten
4006 {
4007 ResourceMark rm(THREAD);
4008 // no exception should happen here since we explicitly
|
50 Array<Method*>* VM_RedefineClasses::_new_methods = NULL;
51 Method** VM_RedefineClasses::_matching_old_methods = NULL;
52 Method** VM_RedefineClasses::_matching_new_methods = NULL;
53 Method** VM_RedefineClasses::_deleted_methods = NULL;
54 Method** VM_RedefineClasses::_added_methods = NULL;
55 int VM_RedefineClasses::_matching_methods_length = 0;
56 int VM_RedefineClasses::_deleted_methods_length = 0;
57 int VM_RedefineClasses::_added_methods_length = 0;
58 Klass* VM_RedefineClasses::_the_class_oop = NULL;
59
60
61 VM_RedefineClasses::VM_RedefineClasses(jint class_count,
62 const jvmtiClassDefinition *class_defs,
63 JvmtiClassLoadKind class_load_kind) {
64 _class_count = class_count;
65 _class_defs = class_defs;
66 _class_load_kind = class_load_kind;
67 _res = JVMTI_ERROR_NONE;
68 }
69
70 static inline InstanceKlass* get_ik(jclass def) {
71 oop mirror = JNIHandles::resolve_non_null(def);
72 return InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
73 }
74
75 bool VM_RedefineClasses::doit_prologue() {
76 if (_class_count == 0) {
77 _res = JVMTI_ERROR_NONE;
78 return false;
79 }
80 if (_class_defs == NULL) {
81 _res = JVMTI_ERROR_NULL_POINTER;
82 return false;
83 }
84 for (int i = 0; i < _class_count; i++) {
85 if (_class_defs[i].klass == NULL) {
86 _res = JVMTI_ERROR_INVALID_CLASS;
87 return false;
88 }
89 if (_class_defs[i].class_byte_count == 0) {
90 _res = JVMTI_ERROR_INVALID_CLASS_FORMAT;
91 return false;
92 }
93 if (_class_defs[i].class_bytes == NULL) {
94 _res = JVMTI_ERROR_NULL_POINTER;
95 return false;
96 }
97 }
98
99 // Start timer after all the sanity checks; not quite accurate, but
100 // better than adding a bunch of stop() calls.
101 RC_TIMER_START(_timer_vm_op_prologue);
102
103 // We first load new class versions in the prologue, because somewhere down the
104 // call chain it is required that the current thread is a Java thread.
105 _res = load_new_class_versions(Thread::current());
106 if (_res != JVMTI_ERROR_NONE) {
107 // free any successfully created classes, since none are redefined
108 for (int i = 0; i < _class_count; i++) {
109 if (_scratch_classes[i] != NULL) {
110 ClassLoaderData* cld = _scratch_classes[i]->class_loader_data();
111 // Free the memory for this class at class unloading time. Not before
112 // because CMS might think this is still live.
113 InstanceKlass* ik = get_ik(_class_defs[i].klass);
114 if (ik->get_cached_class_file() == ((InstanceKlass*)_scratch_classes[i])->get_cached_class_file()) {
115 // Don't double-free cached_class_file copied from the original class if error.
116 ((InstanceKlass*)_scratch_classes[i])->set_cached_class_file(NULL);
117 }
118 cld->add_to_deallocate_list((InstanceKlass*)_scratch_classes[i]);
119 }
120 }
121 // Free os::malloc allocated memory in load_new_class_version.
122 os::free(_scratch_classes);
123 RC_TIMER_STOP(_timer_vm_op_prologue);
124 return false;
125 }
126
127 RC_TIMER_STOP(_timer_vm_op_prologue);
128 return true;
129 }
130
131 void VM_RedefineClasses::doit() {
132 Thread *thread = Thread::current();
133
134 if (UseSharedSpaces) {
135 // Sharing is enabled so we remap the shared readonly space to
136 // shared readwrite, private just in case we need to redefine
137 // a shared class. We do the remap during the doit() phase of
3973 // internally as an InstanceKlass. These special instanceKlasses
3974 // share the constant pool of the class that "implements" the
3975 // interface. By sharing the constant pool, the method holder of a
3976 // miranda method is the class that "implements" the interface. In a
3977 // non-redefine situation, the subtype check works fine. However, if
3978 // the old constant pool's pool holder is modified, then the check
3979 // fails because there is no class hierarchy relationship between the
3980 // vtable's class and "the new class".
3981
3982 old_constants->set_pool_holder(scratch_class());
3983 #endif
3984
3985 // track number of methods that are EMCP for add_previous_version() call below
3986 int emcp_method_count = check_methods_and_mark_as_obsolete();
3987 transfer_old_native_function_registrations(the_class);
3988
3989 // The class file bytes from before any retransformable agents mucked
3990 // with them was cached on the scratch class, move to the_class.
3991 // Note: we still want to do this if nothing needed caching since it
3992 // should get cleared in the_class too.
3993 if (the_class->get_cached_class_file() == 0) {
3994 // the_class doesn't have a cache yet so copy it
3995 the_class->set_cached_class_file(scratch_class->get_cached_class_file());
3996 }
3997 else if (scratch_class->get_cached_class_file() !=
3998 the_class->get_cached_class_file()) {
3999 // The same class can be present twice in the scratch classes list or there
4000 // are multiple concurrent RetransformClasses calls on different threads.
4001 // In such cases we have to deallocate scratch_class cached_class_file.
4002 os::free(scratch_class->get_cached_class_file());
4003 }
4004
4005 // NULL out in scratch class to not delete twice. The class to be redefined
4006 // always owns these bytes.
4007 scratch_class->set_cached_class_file(NULL);
4008
4009 // Replace inner_classes
4010 Array<u2>* old_inner_classes = the_class->inner_classes();
4011 the_class->set_inner_classes(scratch_class->inner_classes());
4012 scratch_class->set_inner_classes(old_inner_classes);
4013
4014 // Initialize the vtable and interface table after
4015 // methods have been rewritten
4016 {
4017 ResourceMark rm(THREAD);
4018 // no exception should happen here since we explicitly
|