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