< prev index next >

src/hotspot/share/cds/classListWriter.cpp

Print this page

  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/classListWriter.hpp"
 28 #include "cds/lambdaFormInvokers.inline.hpp"
 29 #include "classfile/classFileStream.hpp"
 30 #include "classfile/classLoader.hpp"
 31 #include "classfile/classLoaderData.hpp"
 32 #include "classfile/classLoaderDataGraph.hpp"
 33 #include "classfile/moduleEntry.hpp"

 34 #include "classfile/systemDictionaryShared.hpp"
 35 #include "memory/resourceArea.hpp"
 36 #include "oops/constantPool.inline.hpp"
 37 #include "oops/instanceKlass.hpp"

 38 #include "runtime/mutexLocker.hpp"
 39 
 40 fileStream* ClassListWriter::_classlist_file = nullptr;
 41 
 42 void ClassListWriter::init() {
 43   // For -XX:DumpLoadedClassList=<file> option
 44   if (DumpLoadedClassList != nullptr) {
 45     const char* list_name = make_log_name(DumpLoadedClassList, nullptr);
 46     _classlist_file = new(mtInternal)
 47                          fileStream(list_name);
 48     _classlist_file->print_cr("# NOTE: Do not modify this file.");
 49     _classlist_file->print_cr("#");
 50     _classlist_file->print_cr("# This file is generated via the -XX:DumpLoadedClassList=<class_list_file> option");
 51     _classlist_file->print_cr("# and is used at CDS archive dump time (see -Xshare:dump).");
 52     _classlist_file->print_cr("#");
 53     FREE_C_HEAP_ARRAY(char, list_name);
 54   }
 55 }
 56 
 57 void ClassListWriter::write(const InstanceKlass* k, const ClassFileStream* cfs) {

191 #endif
192   }
193 
194   stream->cr();
195   stream->flush();
196 }
197 
198 void ClassListWriter::delete_classlist() {
199   if (_classlist_file != nullptr) {
200     delete _classlist_file;
201   }
202 }
203 
204 class ClassListWriter::WriteResolveConstantsCLDClosure : public CLDClosure {
205 public:
206   void do_cld(ClassLoaderData* cld) {
207     for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
208       if (klass->is_instance_klass()) {
209         InstanceKlass* ik = InstanceKlass::cast(klass);
210         write_resolved_constants_for(ik);

211       }
212     }
213   }
214 };
215 












216 void ClassListWriter::write_resolved_constants() {
217   if (!is_enabled()) {
218     return;
219   }
220   MutexLocker lock(ClassLoaderDataGraph_lock);
221   MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag);
222 
223   WriteResolveConstantsCLDClosure closure;
224   ClassLoaderDataGraph::loaded_cld_do(&closure);
225 }
226 



























227 void ClassListWriter::write_resolved_constants_for(InstanceKlass* ik) {
228   if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data()) ||
229       ik->is_hidden()) {
230     return;
231   }
232   if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) {
233     return;
234   }
235   if (ik->name()->equals("jdk/internal/module/SystemModules$all")) {
236     // This class is regenerated during JDK build process, so the classlist
237     // may not match the version that's in the real jdk image.
238     return;
239   }
240 
241   if (!has_id(ik)) { // do not resolve CP for classes loaded by custom loaders.
242     return;
243   }
244 
245   ResourceMark rm;
246   ConstantPool* cp = ik->constants();

276 
277     Array<ResolvedFieldEntry>* field_entries = cp->cache()->resolved_field_entries();
278     if (field_entries != nullptr) {
279       for (int i = 0; i < field_entries->length(); i++) {
280         ResolvedFieldEntry* rfe = field_entries->adr_at(i);
281         if (rfe->is_resolved(Bytecodes::_getfield) ||
282             rfe->is_resolved(Bytecodes::_putfield)) {
283           list.at_put(rfe->constant_pool_index(), true);
284           print = true;
285         }
286       }
287     }
288 
289     Array<ResolvedMethodEntry>* method_entries = cp->cache()->resolved_method_entries();
290     if (method_entries != nullptr) {
291       for (int i = 0; i < method_entries->length(); i++) {
292         ResolvedMethodEntry* rme = method_entries->adr_at(i);
293         if (rme->is_resolved(Bytecodes::_invokevirtual) ||
294             rme->is_resolved(Bytecodes::_invokespecial) ||
295             rme->is_resolved(Bytecodes::_invokeinterface) ||

296             rme->is_resolved(Bytecodes::_invokehandle)) {
297           list.at_put(rme->constant_pool_index(), true);
298           print = true;
299         }
300       }
301     }
302   }
303 
304   if (print) {
305     outputStream* stream = _classlist_file;
306     stream->print("@cp %s", ik->name()->as_C_string());
307     for (int i = 0; i < list.length(); i++) {
308       if (list.at(i)) {
309         constantTag cp_tag = cp->tag_at(i).value();
310         assert(cp_tag.value() == JVM_CONSTANT_Class ||
311                cp_tag.value() == JVM_CONSTANT_Fieldref ||
312                cp_tag.value() == JVM_CONSTANT_Methodref||
313                cp_tag.value() == JVM_CONSTANT_InterfaceMethodref ||
314                cp_tag.value() == JVM_CONSTANT_InvokeDynamic, "sanity");
315         stream->print(" %d", i);
316       }
317     }
318     stream->cr();
319   }
320 }
















































  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/aotConstantPoolResolver.hpp"
 27 #include "cds/cds_globals.hpp"
 28 #include "cds/classListParser.hpp"
 29 #include "cds/classListWriter.hpp"
 30 #include "cds/lambdaFormInvokers.inline.hpp"
 31 #include "classfile/classFileStream.hpp"
 32 #include "classfile/classLoader.hpp"
 33 #include "classfile/classLoaderData.hpp"
 34 #include "classfile/classLoaderDataGraph.hpp"
 35 #include "classfile/moduleEntry.hpp"
 36 #include "classfile/symbolTable.hpp"
 37 #include "classfile/systemDictionaryShared.hpp"
 38 #include "memory/resourceArea.hpp"
 39 #include "oops/constantPool.inline.hpp"
 40 #include "oops/instanceKlass.hpp"
 41 #include "runtime/javaCalls.hpp"
 42 #include "runtime/mutexLocker.hpp"
 43 
 44 fileStream* ClassListWriter::_classlist_file = nullptr;
 45 
 46 void ClassListWriter::init() {
 47   // For -XX:DumpLoadedClassList=<file> option
 48   if (DumpLoadedClassList != nullptr) {
 49     const char* list_name = make_log_name(DumpLoadedClassList, nullptr);
 50     _classlist_file = new(mtInternal)
 51                          fileStream(list_name);
 52     _classlist_file->print_cr("# NOTE: Do not modify this file.");
 53     _classlist_file->print_cr("#");
 54     _classlist_file->print_cr("# This file is generated via the -XX:DumpLoadedClassList=<class_list_file> option");
 55     _classlist_file->print_cr("# and is used at CDS archive dump time (see -Xshare:dump).");
 56     _classlist_file->print_cr("#");
 57     FREE_C_HEAP_ARRAY(char, list_name);
 58   }
 59 }
 60 
 61 void ClassListWriter::write(const InstanceKlass* k, const ClassFileStream* cfs) {

195 #endif
196   }
197 
198   stream->cr();
199   stream->flush();
200 }
201 
202 void ClassListWriter::delete_classlist() {
203   if (_classlist_file != nullptr) {
204     delete _classlist_file;
205   }
206 }
207 
208 class ClassListWriter::WriteResolveConstantsCLDClosure : public CLDClosure {
209 public:
210   void do_cld(ClassLoaderData* cld) {
211     for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
212       if (klass->is_instance_klass()) {
213         InstanceKlass* ik = InstanceKlass::cast(klass);
214         write_resolved_constants_for(ik);
215         write_array_info_for(ik); // FIXME: piggybacking on WriteResolveConstantsCLDClosure is misleading
216       }
217     }
218   }
219 };
220 
221 void ClassListWriter::write_array_info_for(InstanceKlass* ik) {
222   ObjArrayKlass* oak = ik->array_klasses();
223   if (oak != nullptr) {
224     while (oak->higher_dimension() != nullptr) {
225       oak = oak->higher_dimension();
226     }
227     ResourceMark rm;
228     outputStream* stream = _classlist_file;
229     stream->print_cr("%s %s %d", ClassListParser::ARRAY_TAG, ik->name()->as_C_string(), oak->dimension());
230   }
231 }
232 
233 void ClassListWriter::write_resolved_constants() {
234   if (!is_enabled()) {
235     return;
236   }
237   MutexLocker lock(ClassLoaderDataGraph_lock);
238   MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag);
239 
240   WriteResolveConstantsCLDClosure closure;
241   ClassLoaderDataGraph::loaded_cld_do(&closure);
242 }
243 
244 void ClassListWriter::write_reflection_data() {
245   if (!is_enabled()) {
246     return;
247   }
248   auto collector = [&] (const InstanceKlass* ik, int id) {
249     write_reflection_data_for(const_cast<InstanceKlass*>(ik));
250   };
251   _id_table->iterate_all(collector);
252 }
253 
254 void ClassListWriter::write_reflection_data_for(InstanceKlass* ik) {
255   ResourceMark rm;
256   outputStream* stream = _classlist_file;
257   if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data()) || ik->is_hidden()) {
258     return; // ignore
259   }
260   if (java_lang_Class::has_reflection_data(ik->java_mirror())) {
261     EXCEPTION_MARK;
262     int rd_flags = AOTConstantPoolResolver::class_reflection_data_flags(ik, THREAD);
263     if (!HAS_PENDING_EXCEPTION) {
264       // We can't hold the lock when doing the upcall inside class_reflection_data_flags()
265       MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag);
266       stream->print_cr("%s %s %d", ClassListParser::CLASS_REFLECTION_DATA_TAG, ik->name()->as_C_string(), rd_flags);
267     }
268   }
269 }
270 
271 void ClassListWriter::write_resolved_constants_for(InstanceKlass* ik) {
272   if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data()) ||
273       ik->is_hidden()) {
274     return;
275   }
276   if (LambdaFormInvokers::may_be_regenerated_class(ik->name())) {
277     return;
278   }
279   if (ik->name()->equals("jdk/internal/module/SystemModules$all")) {
280     // This class is regenerated during JDK build process, so the classlist
281     // may not match the version that's in the real jdk image.
282     return;
283   }
284 
285   if (!has_id(ik)) { // do not resolve CP for classes loaded by custom loaders.
286     return;
287   }
288 
289   ResourceMark rm;
290   ConstantPool* cp = ik->constants();

320 
321     Array<ResolvedFieldEntry>* field_entries = cp->cache()->resolved_field_entries();
322     if (field_entries != nullptr) {
323       for (int i = 0; i < field_entries->length(); i++) {
324         ResolvedFieldEntry* rfe = field_entries->adr_at(i);
325         if (rfe->is_resolved(Bytecodes::_getfield) ||
326             rfe->is_resolved(Bytecodes::_putfield)) {
327           list.at_put(rfe->constant_pool_index(), true);
328           print = true;
329         }
330       }
331     }
332 
333     Array<ResolvedMethodEntry>* method_entries = cp->cache()->resolved_method_entries();
334     if (method_entries != nullptr) {
335       for (int i = 0; i < method_entries->length(); i++) {
336         ResolvedMethodEntry* rme = method_entries->adr_at(i);
337         if (rme->is_resolved(Bytecodes::_invokevirtual) ||
338             rme->is_resolved(Bytecodes::_invokespecial) ||
339             rme->is_resolved(Bytecodes::_invokeinterface) ||
340             rme->is_resolved(Bytecodes::_invokestatic) ||
341             rme->is_resolved(Bytecodes::_invokehandle)) {
342           list.at_put(rme->constant_pool_index(), true);
343           print = true;
344         }
345       }
346     }
347   }
348 
349   if (print) {
350     outputStream* stream = _classlist_file;
351     stream->print("@cp %s", ik->name()->as_C_string());
352     for (int i = 0; i < list.length(); i++) {
353       if (list.at(i)) {
354         constantTag cp_tag = cp->tag_at(i).value();
355         assert(cp_tag.value() == JVM_CONSTANT_Class ||
356                cp_tag.value() == JVM_CONSTANT_Fieldref ||
357                cp_tag.value() == JVM_CONSTANT_Methodref||
358                cp_tag.value() == JVM_CONSTANT_InterfaceMethodref ||
359                cp_tag.value() == JVM_CONSTANT_InvokeDynamic, "sanity");
360         stream->print(" %d", i);
361       }
362     }
363     stream->cr();
364   }
365 }
366 
367 void ClassListWriter::write_loader_negative_lookup_cache_for(oop loader, const char* loader_type) {
368   TempNewSymbol method = SymbolTable::new_symbol("negativeLookupCacheContents");
369   TempNewSymbol signature = SymbolTable::new_symbol("()Ljava/lang/String;");
370 
371   EXCEPTION_MARK;
372   HandleMark hm(THREAD);
373 
374   JavaValue result(T_OBJECT);
375   JavaCalls::call_virtual(&result,
376                           Handle(THREAD, loader),
377                           loader->klass(),
378                           method,
379                           signature,
380                           CHECK);
381 
382   if (HAS_PENDING_EXCEPTION) {
383     log_warning(cds)("Error during BuiltinClassLoader::negativeLookupCacheContents() call for %s loader", loader_type);
384     CLEAR_PENDING_EXCEPTION;
385     return;
386   } else if (result.get_oop() == nullptr) {
387     return;
388   }
389 
390   ResourceMark rm;
391   const char* cache_contents = java_lang_String::as_utf8_string(result.get_oop());
392   log_debug(cds)("%s loader negative cache: %s", loader_type, cache_contents);
393 
394   outputStream* stream = _classlist_file;
395   const size_t buffer_size = strlen(ClassListParser::LOADER_NEGATIVE_CACHE_TAG) + 1 /* for space*/
396                     + strlen(loader_type) + 1 /* for space */ + strlen(cache_contents) + 1 /* for null character */;
397   char* buffer = NEW_C_HEAP_ARRAY(char, buffer_size, mtInternal);
398   _classlist_file->set_scratch_buffer(buffer, buffer_size);
399   MutexLocker lock2(ClassListFile_lock, Mutex::_no_safepoint_check_flag);
400   stream->print("%s %s %s", ClassListParser::LOADER_NEGATIVE_CACHE_TAG, loader_type, cache_contents);
401   stream->cr();
402   _classlist_file->set_scratch_buffer(nullptr, 0);
403 }
404 
405 void ClassListWriter::write_loader_negative_lookup_cache() {
406   if (!is_enabled()) {
407     return;
408   }
409 
410   write_loader_negative_lookup_cache_for(SystemDictionary::java_platform_loader(), "platform");
411   write_loader_negative_lookup_cache_for(SystemDictionary::java_system_loader(), "app");
412 }
< prev index next >