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