1 /*
  2 * Copyright (c) 2015, 2024, 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 "cds/cds_globals.hpp"
 27 #include "cds/cdsConfig.hpp"
 28 #include "cds/filemap.hpp"
 29 #include "classfile/classFileParser.hpp"
 30 #include "classfile/classFileStream.hpp"
 31 #include "classfile/classLoader.hpp"
 32 #include "classfile/classLoaderData.hpp"
 33 #include "classfile/classLoaderData.inline.hpp"
 34 #include "classfile/classLoadInfo.hpp"
 35 #include "classfile/klassFactory.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       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         new_ik->assign_class_loader_type();
101       }
102 
103       return new_ik;
104     }
105   }
106 #endif
107 
108   return nullptr;
109 }
110 
111 
112 static ClassFileStream* check_class_file_load_hook(ClassFileStream* stream,
113                                                    Symbol* name,
114                                                    ClassLoaderData* loader_data,
115                                                    Handle protection_domain,
116                                                    JvmtiCachedClassFileData** cached_class_file,
117                                                    TRAPS) {
118 
119   assert(stream != nullptr, "invariant");
120 
121   if (JvmtiExport::should_post_class_file_load_hook()) {
122     const JavaThread* jt = THREAD;
123 
124     Handle class_loader(THREAD, loader_data->class_loader());
125 
126     // Get the cached class file bytes (if any) from the class that
127     // is being retransformed. If class file load hook provides
128     // modified class data during class loading or redefinition,
129     // new cached class file buffer should be allocated.
130     // We use jvmti_thread_state()
131     // instead of JvmtiThreadState::state_for(jt) so we don't allocate
132     // a JvmtiThreadState any earlier than necessary. This will help
133     // avoid the bug described by 7126851.
134 
135     JvmtiThreadState* state = jt->jvmti_thread_state();
136 
137     if (state != nullptr) {
138       Klass* k = state->get_class_being_redefined();
139       if (k != nullptr && state->get_class_load_kind() == jvmti_class_load_kind_retransform) {
140         InstanceKlass* class_being_redefined = InstanceKlass::cast(k);
141         *cached_class_file = class_being_redefined->get_cached_class_file();
142       }
143     }
144 
145     unsigned char* ptr = const_cast<unsigned char*>(stream->buffer());
146     unsigned char* end_ptr = ptr + stream->length();
147 
148     JvmtiExport::post_class_file_load_hook(name,
149                                            class_loader,
150                                            protection_domain,
151                                            &ptr,
152                                            &end_ptr,
153                                            cached_class_file);
154 
155     if (ptr != stream->buffer()) {
156       // JVMTI agent has modified class file data.
157       // Set new class file stream using JVMTI agent modified class file data.
158       stream = new ClassFileStream(ptr,
159                                    pointer_delta_as_int(end_ptr, ptr),
160                                    stream->source());
161     }
162   }
163 
164   return stream;
165 }
166 
167 
168 InstanceKlass* KlassFactory::create_from_stream(ClassFileStream* stream,
169                                                 Symbol* name,
170                                                 ClassLoaderData* loader_data,
171                                                 const ClassLoadInfo& cl_info,
172                                                 TRAPS) {
173   assert(stream != nullptr, "invariant");
174   assert(loader_data != nullptr, "invariant");
175 
176   ResourceMark rm(THREAD);
177   HandleMark hm(THREAD);
178 
179   JvmtiCachedClassFileData* cached_class_file = nullptr;
180 
181   ClassFileStream* old_stream = stream;
182 
183   // increment counter
184   THREAD->statistical_info().incr_define_class_count();
185 
186   // Skip this processing for VM hidden classes
187   if (!cl_info.is_hidden()) {
188     stream = check_class_file_load_hook(stream,
189                                         name,
190                                         loader_data,
191                                         cl_info.protection_domain(),
192                                         &cached_class_file,
193                                         CHECK_NULL);
194   }
195 
196   ClassFileParser parser(stream,
197                          name,
198                          loader_data,
199                          &cl_info,
200                          ClassFileParser::BROADCAST, // publicity level
201                          CHECK_NULL);
202 
203   const ClassInstanceInfo* cl_inst_info = cl_info.class_hidden_info_ptr();
204   InstanceKlass* result = parser.create_instance_klass(old_stream != stream, *cl_inst_info, CHECK_NULL);
205   assert(result != nullptr, "result cannot be null with no pending exception");
206 
207   if (cached_class_file != nullptr) {
208     // JVMTI: we have an InstanceKlass now, tell it about the cached bytes
209     result->set_cached_class_file(cached_class_file);
210   }
211 
212   JFR_ONLY(ON_KLASS_CREATION(result, parser, THREAD);)
213 
214 #if INCLUDE_CDS
215   if (CDSConfig::is_dumping_archive()) {
216     ClassLoader::record_result(THREAD, result, stream, old_stream != stream);
217   }
218 #endif // INCLUDE_CDS
219 
220   return result;
221 }