1 /* 2 * Copyright (c) 2015, 2025, 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 "cds/cds_globals.hpp" 26 #include "cds/cdsConfig.hpp" 27 #include "cds/filemap.hpp" 28 #include "classfile/classFileParser.hpp" 29 #include "classfile/classFileStream.hpp" 30 #include "classfile/classLoader.hpp" 31 #include "classfile/classLoaderData.hpp" 32 #include "classfile/classLoaderData.inline.hpp" 33 #include "classfile/classLoadInfo.hpp" 34 #include "classfile/klassFactory.hpp" 35 #include "classfile/systemDictionaryShared.hpp" 36 #include "memory/resourceArea.hpp" 37 #include "prims/jvmtiEnvBase.hpp" 38 #include "prims/jvmtiRedefineClasses.hpp" 39 #include "runtime/handles.inline.hpp" 40 #include "utilities/macros.hpp" 41 #if INCLUDE_JFR 42 #include "jfr/support/jfrKlassExtension.hpp" 43 #endif 44 45 46 // called during initial loading of a shared class 47 InstanceKlass* KlassFactory::check_shared_class_file_load_hook( 48 InstanceKlass* ik, 49 Symbol* class_name, 50 Handle class_loader, 51 Handle protection_domain, 52 const ClassFileStream *cfs, 53 TRAPS) { 54 #if INCLUDE_CDS && INCLUDE_JVMTI 55 assert(ik != nullptr, "sanity"); 56 assert(ik->is_shared(), "expecting a shared class"); 57 if (JvmtiExport::should_post_class_file_load_hook()) { 58 ResourceMark rm(THREAD); 59 // Post the CFLH 60 JvmtiCachedClassFileData* cached_class_file = nullptr; 61 if (cfs == nullptr) { 62 cfs = FileMapInfo::open_stream_for_jvmti(ik, class_loader, CHECK_NULL); 63 } 64 unsigned char* ptr = (unsigned char*)cfs->buffer(); 65 unsigned char* end_ptr = ptr + cfs->length(); 66 unsigned char* old_ptr = ptr; 67 JvmtiExport::post_class_file_load_hook(class_name, 68 class_loader, 69 protection_domain, 70 &ptr, 71 &end_ptr, 72 &cached_class_file); 73 if (old_ptr != ptr) { 74 // JVMTI agent has modified class file data. 75 // Set new class file stream using JVMTI agent modified class file data. 76 ClassLoaderData* loader_data = 77 ClassLoaderData::class_loader_data(class_loader()); 78 s2 path_index = ik->shared_classpath_index(); 79 ClassFileStream* stream = new ClassFileStream(ptr, 80 pointer_delta_as_int(end_ptr, ptr), 81 cfs->source(), 82 /* from_boot_loader_modules_image */ false, 83 /* from_class_file_load_hook */ true); 84 ClassLoadInfo cl_info(protection_domain); 85 ClassFileParser parser(stream, 86 class_name, 87 loader_data, 88 &cl_info, 89 ClassFileParser::BROADCAST, // publicity level 90 CHECK_NULL); 91 const ClassInstanceInfo* cl_inst_info = cl_info.class_hidden_info_ptr(); 92 InstanceKlass* new_ik = parser.create_instance_klass(true, // changed_by_loadhook 93 *cl_inst_info, // dynamic_nest_host and classData 94 CHECK_NULL); 95 96 if (cached_class_file != nullptr) { 97 new_ik->set_cached_class_file(cached_class_file); 98 } 99 100 if (class_loader.is_null()) { 101 new_ik->set_classpath_index(path_index); 102 } 103 104 return new_ik; 105 } 106 } 107 #endif 108 109 return nullptr; 110 } 111 112 113 static ClassFileStream* check_class_file_load_hook(ClassFileStream* stream, 114 Symbol* name, 115 ClassLoaderData* loader_data, 116 Handle protection_domain, 117 JvmtiCachedClassFileData** cached_class_file, 118 TRAPS) { 119 120 assert(stream != nullptr, "invariant"); 121 122 if (JvmtiExport::should_post_class_file_load_hook()) { 123 const JavaThread* jt = THREAD; 124 125 Handle class_loader(THREAD, loader_data->class_loader()); 126 127 // Get the cached class file bytes (if any) from the class that 128 // is being retransformed. If class file load hook provides 129 // modified class data during class loading or redefinition, 130 // new cached class file buffer should be allocated. 131 // We use jvmti_thread_state() 132 // instead of JvmtiThreadState::state_for(jt) so we don't allocate 133 // a JvmtiThreadState any earlier than necessary. This will help 134 // avoid the bug described by 7126851. 135 136 JvmtiThreadState* state = jt->jvmti_thread_state(); 137 138 if (state != nullptr) { 139 Klass* k = state->get_class_being_redefined(); 140 if (k != nullptr && state->get_class_load_kind() == jvmti_class_load_kind_retransform) { 141 InstanceKlass* class_being_redefined = InstanceKlass::cast(k); 142 *cached_class_file = class_being_redefined->get_cached_class_file(); 143 } 144 } 145 146 unsigned char* ptr = const_cast<unsigned char*>(stream->buffer()); 147 unsigned char* end_ptr = ptr + stream->length(); 148 149 JvmtiExport::post_class_file_load_hook(name, 150 class_loader, 151 protection_domain, 152 &ptr, 153 &end_ptr, 154 cached_class_file); 155 156 if (ptr != stream->buffer()) { 157 // JVMTI agent has modified class file data. 158 // Set new class file stream using JVMTI agent modified class file data. 159 stream = new ClassFileStream(ptr, 160 pointer_delta_as_int(end_ptr, ptr), 161 stream->source(), 162 /* from_boot_loader_modules_image */ false, 163 /* from_class_file_load_hook */ true); 164 } 165 } 166 167 return stream; 168 } 169 170 171 InstanceKlass* KlassFactory::create_from_stream(ClassFileStream* stream, 172 Symbol* name, 173 ClassLoaderData* loader_data, 174 const ClassLoadInfo& cl_info, 175 TRAPS) { 176 assert(stream != nullptr, "invariant"); 177 assert(loader_data != nullptr, "invariant"); 178 179 ResourceMark rm(THREAD); 180 HandleMark hm(THREAD); 181 182 JvmtiCachedClassFileData* cached_class_file = nullptr; 183 184 ClassFileStream* old_stream = stream; 185 186 // increment counter 187 THREAD->statistical_info().incr_define_class_count(); 188 189 // Skip this processing for VM hidden classes 190 if (!cl_info.is_hidden()) { 191 stream = check_class_file_load_hook(stream, 192 name, 193 loader_data, 194 cl_info.protection_domain(), 195 &cached_class_file, 196 CHECK_NULL); 197 } 198 199 ClassFileParser parser(stream, 200 name, 201 loader_data, 202 &cl_info, 203 ClassFileParser::BROADCAST, // publicity level 204 CHECK_NULL); 205 206 const ClassInstanceInfo* cl_inst_info = cl_info.class_hidden_info_ptr(); 207 InstanceKlass* result = parser.create_instance_klass(old_stream != stream, *cl_inst_info, CHECK_NULL); 208 assert(result != nullptr, "result cannot be null with no pending exception"); 209 if (CDSConfig::is_dumping_archive() && stream->from_class_file_load_hook()) { 210 SystemDictionaryShared::set_from_class_file_load_hook(result); 211 } 212 213 if (cached_class_file != nullptr) { 214 // JVMTI: we have an InstanceKlass now, tell it about the cached bytes 215 result->set_cached_class_file(cached_class_file); 216 } 217 218 JFR_ONLY(ON_KLASS_CREATION(result, parser, THREAD);) 219 220 #if INCLUDE_CDS 221 if (CDSConfig::is_dumping_archive()) { 222 ClassLoader::record_result(THREAD, result, stream, old_stream != stream); 223 } 224 #endif // INCLUDE_CDS 225 226 return result; 227 }