1 /* 2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/classFileParser.hpp" 27 #include "classfile/classFileStream.hpp" 28 #include "classfile/classLoader.hpp" 29 #include "classfile/classLoaderData.hpp" 30 #include "classfile/classLoaderData.inline.hpp" 31 #include "classfile/klassFactory.hpp" 32 #include "classfile/sharedClassUtil.hpp" 33 #include "memory/metaspaceShared.hpp" 34 #include "memory/resourceArea.hpp" 35 #include "prims/jvmtiEnvBase.hpp" 36 #include "prims/jvmtiRedefineClasses.hpp" 37 #include "trace/traceMacros.hpp" 38 39 // called during initial loading of a shared class 40 InstanceKlass* KlassFactory::check_shared_class_file_load_hook( 41 InstanceKlass* ik, 42 Symbol* class_name, 43 Handle class_loader, 44 Handle protection_domain, TRAPS) { 45 #if INCLUDE_CDS && INCLUDE_JVMTI 46 assert(ik != NULL, "sanity"); 47 assert(ik->is_shared(), "expecting a shared class"); 48 49 if (JvmtiExport::should_post_class_file_load_hook()) { 50 assert(THREAD->is_Java_thread(), "must be JavaThread"); 51 52 // Post the CFLH 53 JvmtiCachedClassFileData* cached_class_file = NULL; 54 JvmtiCachedClassFileData* archived_class_data = ik->get_archived_class_data(); 55 assert(archived_class_data != NULL, "shared class has no archived class data"); 56 unsigned char* ptr = 57 VM_RedefineClasses::get_cached_class_file_bytes(archived_class_data); 58 unsigned char* end_ptr = 59 ptr + VM_RedefineClasses::get_cached_class_file_len(archived_class_data); 60 unsigned char* old_ptr = ptr; 61 JvmtiExport::post_class_file_load_hook(class_name, 62 class_loader, 63 protection_domain, 64 &ptr, 65 &end_ptr, 66 &cached_class_file); 67 if (old_ptr != ptr) { 68 // JVMTI agent has modified class file data. 69 // Set new class file stream using JVMTI agent modified class file data. 70 ClassLoaderData* loader_data = 71 ClassLoaderData::class_loader_data(class_loader()); 72 int path_index = ik->shared_classpath_index(); 73 const char* pathname; 74 if (path_index < 0) { 75 // shared classes loaded by user defined class loader 76 // do not have shared_classpath_index 77 ModuleEntry* mod_entry = ik->module(); 78 if (mod_entry != NULL && (mod_entry->location() != NULL)) { 79 ResourceMark rm; 80 pathname = (const char*)(mod_entry->location()->as_C_string()); 81 } else { 82 pathname = ""; 83 } 84 } else { 85 SharedClassPathEntry* ent = 86 (SharedClassPathEntry*)FileMapInfo::shared_classpath(path_index); 87 pathname = ent == NULL ? NULL : ent->name(); 88 } 89 ClassFileStream* stream = new ClassFileStream(ptr, 90 end_ptr - ptr, 91 pathname, 92 ClassFileStream::verify); 93 ClassFileParser parser(stream, 94 class_name, 95 loader_data, 96 protection_domain, 97 NULL, 98 NULL, 99 ClassFileParser::BROADCAST, // publicity level 100 CHECK_NULL); 101 InstanceKlass* new_ik = parser.create_instance_klass(true /* changed_by_loadhook */, 102 CHECK_NULL); 103 if (cached_class_file != NULL) { 104 new_ik->set_cached_class_file(cached_class_file); 105 } 106 107 if (class_loader.is_null()) { 108 ResourceMark rm; 109 ClassLoader::add_package(class_name->as_C_string(), path_index, THREAD); 110 } 111 112 return new_ik; 113 } 114 } 115 #endif 116 117 return NULL; 118 } 119 120 121 static ClassFileStream* check_class_file_load_hook(ClassFileStream* stream, 122 Symbol* name, 123 ClassLoaderData* loader_data, 124 Handle protection_domain, 125 JvmtiCachedClassFileData** cached_class_file, 126 TRAPS) { 127 128 assert(stream != NULL, "invariant"); 129 130 if (JvmtiExport::should_post_class_file_load_hook()) { 131 assert(THREAD->is_Java_thread(), "must be a JavaThread"); 132 const JavaThread* jt = (JavaThread*)THREAD; 133 134 Handle class_loader(THREAD, loader_data->class_loader()); 135 136 // Get the cached class file bytes (if any) from the class that 137 // is being redefined or retransformed. We use jvmti_thread_state() 138 // instead of JvmtiThreadState::state_for(jt) so we don't allocate 139 // a JvmtiThreadState any earlier than necessary. This will help 140 // avoid the bug described by 7126851. 141 142 JvmtiThreadState* state = jt->jvmti_thread_state(); 143 144 if (state != NULL) { 145 Klass* k = state->get_class_being_redefined(); 146 147 if (k != NULL) { 148 InstanceKlass* class_being_redefined = InstanceKlass::cast(k); 149 *cached_class_file = class_being_redefined->get_cached_class_file(); 150 } 151 } 152 153 unsigned char* ptr = const_cast<unsigned char*>(stream->buffer()); 154 unsigned char* end_ptr = ptr + stream->length(); 155 156 JvmtiExport::post_class_file_load_hook(name, 157 class_loader, 158 protection_domain, 159 &ptr, 160 &end_ptr, 161 cached_class_file); 162 163 if (ptr != stream->buffer()) { 164 // JVMTI agent has modified class file data. 165 // Set new class file stream using JVMTI agent modified class file data. 166 stream = new ClassFileStream(ptr, 167 end_ptr - ptr, 168 stream->source(), 169 stream->need_verify()); 170 } 171 } 172 173 return stream; 174 } 175 176 177 InstanceKlass* KlassFactory::create_from_stream(ClassFileStream* stream, 178 Symbol* name, 179 ClassLoaderData* loader_data, 180 Handle protection_domain, 181 const InstanceKlass* host_klass, 182 GrowableArray<Handle>* cp_patches, 183 TRAPS) { 184 assert(stream != NULL, "invariant"); 185 assert(loader_data != NULL, "invariant"); 186 assert(THREAD->is_Java_thread(), "must be a JavaThread"); 187 188 ResourceMark rm; 189 HandleMark hm; 190 191 JvmtiCachedClassFileData* cached_class_file = NULL; 192 193 ClassFileStream* old_stream = stream; 194 195 // Skip this processing for VM anonymous classes 196 if (host_klass == NULL) { 197 stream = check_class_file_load_hook(stream, 198 name, 199 loader_data, 200 protection_domain, 201 &cached_class_file, 202 CHECK_NULL); 203 } 204 205 ClassFileParser parser(stream, 206 name, 207 loader_data, 208 protection_domain, 209 host_klass, 210 cp_patches, 211 ClassFileParser::BROADCAST, // publicity level 212 CHECK_NULL); 213 214 InstanceKlass* result = parser.create_instance_klass(old_stream != stream, CHECK_NULL); 215 assert(result == parser.create_instance_klass(old_stream != stream, THREAD), "invariant"); 216 217 if (result == NULL) { 218 return NULL; 219 } 220 221 if (cached_class_file != NULL) { 222 // JVMTI: we have an InstanceKlass now, tell it about the cached bytes 223 result->set_cached_class_file(cached_class_file); 224 } 225 226 if (result->should_store_fingerprint()) { 227 result->store_fingerprint(stream->compute_fingerprint()); 228 } 229 230 TRACE_KLASS_CREATION(result, parser, THREAD); 231 232 #if INCLUDE_CDS 233 if (DumpSharedSpaces) { 234 ClassLoader::record_shared_class_loader_type(result, stream); 235 #if INCLUDE_JVMTI 236 assert(cached_class_file == NULL, "Sanity"); 237 // Archive the class stream data into the optional data section 238 JvmtiCachedClassFileData *p; 239 int len; 240 const unsigned char *bytes; 241 // event based tracing might set cached_class_file 242 if ((bytes = result->get_cached_class_file_bytes()) != NULL) { 243 len = result->get_cached_class_file_len(); 244 } else { 245 len = stream->length(); 246 bytes = stream->buffer(); 247 } 248 p = (JvmtiCachedClassFileData*)os::malloc(offset_of(JvmtiCachedClassFileData, data) + len, mtInternal); 249 p->length = len; 250 memcpy(p->data, bytes, len); 251 result->set_archived_class_data(p); 252 #endif // INCLUDE_JVMTI 253 } 254 #endif // INCLUDE_CDS 255 256 return result; 257 }