< prev index next >

src/hotspot/share/cds/cdsHeapVerifier.cpp

Print this page

 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/aotClassInitializer.hpp"
 26 #include "cds/archiveBuilder.hpp"
 27 #include "cds/cdsHeapVerifier.hpp"
 28 #include "classfile/classLoaderDataGraph.hpp"
 29 #include "classfile/javaClasses.inline.hpp"
 30 #include "classfile/moduleEntry.hpp"
 31 #include "classfile/systemDictionaryShared.hpp"
 32 #include "classfile/vmSymbols.hpp"
 33 #include "logging/log.hpp"
 34 #include "logging/logStream.hpp"
 35 #include "memory/resourceArea.hpp"
 36 #include "oops/fieldStreams.inline.hpp"
 37 #include "oops/klass.inline.hpp"
 38 #include "oops/oop.inline.hpp"

 39 #include "runtime/fieldDescriptor.inline.hpp"
 40 
 41 #if INCLUDE_CDS_JAVA_HEAP
 42 
 43 // CDSHeapVerifier is used to check for problems where an archived object references a
 44 // static field that may be get a different value at runtime.
 45 //
 46 // *Please see comments in aotClassInitializer.cpp for how to avoid such problems*,
 47 //
 48 // In the following example,
 49 //      Foo.get.test()
 50 // correctly returns true when CDS disabled, but incorrectly returns false when CDS is enabled,
 51 // because the archived archivedFoo.bar value is different than Bar.bar.
 52 //
 53 // class Foo {
 54 //     static final Foo archivedFoo; // this field is archived by CDS
 55 //     Bar bar;
 56 //     static {
 57 //         CDS.initializeFromArchive(Foo.class);
 58 //         if (archivedFoo == null) {

 82 // [A] In most of the cases, the module bootstrap code will update the static field
 83 //     to point to part of the archived module graph. E.g.,
 84 //     - java/lang/System::bootLayer
 85 //     - jdk/internal/loader/ClassLoaders::BOOT_LOADER
 86 // [B] A final static String that's explicitly initialized inside <clinit>, but
 87 //     its value is deterministic and is always the same string literal.
 88 // [C] A non-final static string that is assigned a string literal during class
 89 //     initialization; this string is never changed during -Xshare:dump.
 90 // [D] Simple caches whose value doesn't matter.
 91 // [E] Other cases (see comments in-line below).
 92 
 93 CDSHeapVerifier::CDSHeapVerifier() : _archived_objs(0), _problems(0)
 94 {
 95 # define ADD_EXCL(...) { static const char* e[] = {__VA_ARGS__, nullptr}; add_exclusion(e); }
 96 
 97   // Unfortunately this needs to be manually maintained. If
 98   // test/hotspot/jtreg/runtime/cds/appcds/cacheObject/ArchivedEnumTest.java fails,
 99   // you might need to fix the core library code, or fix the ADD_EXCL entries below.
100   //
101   //       class                                         field                     type
102   ADD_EXCL("java/lang/ClassLoader",                      "scl");                   // A
103   ADD_EXCL("java/lang/Module",                           "ALL_UNNAMED_MODULE",     // A
104                                                          "ALL_UNNAMED_MODULE_SET", // A
105                                                          "EVERYONE_MODULE",        // A
106                                                          "EVERYONE_SET");          // A
107 
108   // This is the same as java/util/ImmutableCollections::EMPTY_SET, which is archived
109   ADD_EXCL("java/lang/reflect/AccessFlag$Location",      "EMPTY_SET");             // E
110 
111   ADD_EXCL("java/lang/System",                           "bootLayer");             // A
112 
113   ADD_EXCL("java/util/Collections",                      "EMPTY_LIST");           // E
114 
115   // A dummy object used by HashSet. The value doesn't matter and it's never
116   // tested for equality.
117   ADD_EXCL("java/util/HashSet",                          "PRESENT");               // E
118   ADD_EXCL("jdk/internal/loader/BuiltinClassLoader",     "packageToModule");       // A
119   ADD_EXCL("jdk/internal/loader/ClassLoaders",           "BOOT_LOADER",            // A
120                                                          "APP_LOADER",             // A
121                                                          "PLATFORM_LOADER");       // A
122   ADD_EXCL("jdk/internal/module/Builder",                "cachedVersion");         // D

127   ADD_EXCL("jdk/internal/module/ServicesCatalog",        "CLV");                   // A
128 
129   // This just points to an empty Map
130   ADD_EXCL("jdk/internal/reflect/Reflection",            "methodFilterMap");       // E
131 
132   // Integer for 0 and 1 are in java/lang/Integer$IntegerCache and are archived
133   ADD_EXCL("sun/invoke/util/ValueConversions",           "ONE_INT",                // E
134                                                          "ZERO_INT");              // E
135 
136   if (CDSConfig::is_dumping_method_handles()) {
137     ADD_EXCL("java/lang/invoke/InvokerBytecodeGenerator", "MEMBERNAME_FACTORY",    // D
138                                                           "CD_Object_array",       // E same as <...>ConstantUtils.CD_Object_array::CD_Object
139                                                           "INVOKER_SUPER_DESC");   // E same as java.lang.constant.ConstantDescs::CD_Object
140 
141     ADD_EXCL("java/lang/runtime/ObjectMethods",           "CLASS_IS_INSTANCE",     // D
142                                                           "FALSE",                 // D
143                                                           "TRUE",                  // D
144                                                           "ZERO");                 // D
145   }
146 














147 # undef ADD_EXCL
148 
149   ClassLoaderDataGraph::classes_do(this);
150 }
151 
152 CDSHeapVerifier::~CDSHeapVerifier() {
153   if (_problems > 0) {
154     log_error(aot, heap)("Scanned %d objects. Found %d case(s) where "
155                          "an object points to a static field that "
156                          "may hold a different value at runtime.", _archived_objs, _problems);
157     log_error(aot, heap)("Please see cdsHeapVerifier.cpp and aotClassInitializer.cpp for details");
158     MetaspaceShared::unrecoverable_writing_error();
159   }
160 }
161 
162 class CDSHeapVerifier::CheckStaticFields : public FieldClosure {
163   CDSHeapVerifier* _verifier;
164   InstanceKlass* _ik; // The class whose static fields are being checked.
165   const char** _exclusions;
166 public:

254     }
255 
256     if (ArchiveUtils::has_aot_initialized_mirror(ik)) {
257       // ik's <clinit> won't be executed at runtime, the static fields in
258       // ik will carry their values to runtime.
259       return;
260     }
261 
262     CheckStaticFields csf(this, ik);
263     ik->do_local_static_fields(&csf);
264   }
265 }
266 
267 void CDSHeapVerifier::add_static_obj_field(InstanceKlass* ik, oop field, Symbol* name) {
268   StaticFieldInfo info = {ik, name};
269   _table.put(field, info);
270 }
271 
272 // This function is called once for every archived heap object. Warn if this object is referenced by
273 // a static field of a class that's not aot-initialized.
274 inline bool CDSHeapVerifier::do_entry(oop& orig_obj, HeapShared::CachedOopInfo& value) {

275   _archived_objs++;
276 
277   if (java_lang_String::is_instance(orig_obj) && HeapShared::is_dumped_interned_string(orig_obj)) {
278     // It's quite often for static fields to have interned strings. These are most likely not
279     // problematic (and are hard to filter). So we will ignore them.
280     return true; /* keep on iterating */
281   }
282 
283   StaticFieldInfo* info = _table.get(orig_obj);
284   if (info != nullptr) {
285     ResourceMark rm;
286     char* class_name = info->_holder->name()->as_C_string();
287     char* field_name = info->_name->as_C_string();
288     LogStream ls(Log(aot, heap)::warning());
289     ls.print_cr("Archive heap points to a static field that may hold a different value at runtime:");
290     ls.print_cr("Field: %s::%s", class_name, field_name);
291     ls.print("Value: ");
292     orig_obj->print_on(&ls);
293     ls.print_cr("--- trace begin ---");
294     trace_to_root(&ls, orig_obj, nullptr, &value);

304   oop _orig_obj;
305   oop _orig_field;
306   outputStream* _st;
307 
308 public:
309   TraceFields(oop orig_obj, oop orig_field, outputStream* st)
310     : _orig_obj(orig_obj), _orig_field(orig_field), _st(st) {}
311 
312   void do_field(fieldDescriptor* fd) {
313     if (fd->field_type() == T_OBJECT || fd->field_type() == T_ARRAY) {
314       oop obj_field = _orig_obj->obj_field(fd->offset());
315       if (obj_field == _orig_field) {
316         _st->print("::%s (offset = %d)", fd->name()->as_C_string(), fd->offset());
317       }
318     }
319   }
320 };
321 
322 // Call this function (from gdb, etc) if you want to know why an object is archived.
323 void CDSHeapVerifier::trace_to_root(outputStream* st, oop orig_obj) {
324   HeapShared::CachedOopInfo* info = HeapShared::archived_object_cache()->get(orig_obj);
325   if (info != nullptr) {
326     trace_to_root(st, orig_obj, nullptr, info);
327   } else {
328     st->print_cr("Not an archived object??");
329   }
330 }
331 
332 const char* static_field_name(oop mirror, oop field) {
333   Klass* k = java_lang_Class::as_Klass(mirror);
334   if (k->is_instance_klass()) {
335     for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) {
336       if (fs.access_flags().is_static()) {
337         fieldDescriptor& fd = fs.field_descriptor();
338         switch (fd.field_type()) {
339         case T_OBJECT:
340         case T_ARRAY:
341           if (mirror->obj_field(fd.offset()) == field) {
342             return fs.name()->as_C_string();
343           }
344           break;
345         default:
346           break;
347         }
348       }
349     }
350   }
351 
352   return "<unknown>";
353 }
354 
355 int CDSHeapVerifier::trace_to_root(outputStream* st, oop orig_obj, oop orig_field, HeapShared::CachedOopInfo* info) {
356   int level = 0;
357   if (info->orig_referrer() != nullptr) {
358     HeapShared::CachedOopInfo* ref = HeapShared::archived_object_cache()->get(info->orig_referrer());
359     assert(ref != nullptr, "sanity");
360     level = trace_to_root(st, info->orig_referrer(), orig_obj, ref) + 1;
361   } else if (java_lang_String::is_instance(orig_obj)) {
362     st->print_cr("[%2d] (shared string table)", level++);
363   }
364   Klass* k = orig_obj->klass();
365   ResourceMark rm;
366   st->print("[%2d] ", level);
367   orig_obj->print_address_on(st);
368   st->print(" %s", k->internal_name());
369   if (java_lang_Class::is_instance(orig_obj)) {
370     st->print(" (%s::%s)", java_lang_Class::as_Klass(orig_obj)->external_name(), static_field_name(orig_obj, orig_field));
371   }
372   if (orig_field != nullptr) {
373     if (k->is_instance_klass()) {
374       TraceFields clo(orig_obj, orig_field, st);
375       InstanceKlass::cast(k)->do_nonstatic_fields(&clo);
376     } else {
377       assert(orig_obj->is_objArray(), "must be");
378       objArrayOop array = (objArrayOop)orig_obj;

 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/aotClassInitializer.hpp"
 26 #include "cds/archiveBuilder.hpp"
 27 #include "cds/cdsHeapVerifier.hpp"
 28 #include "classfile/classLoaderDataGraph.hpp"
 29 #include "classfile/javaClasses.inline.hpp"
 30 #include "classfile/moduleEntry.hpp"
 31 #include "classfile/systemDictionaryShared.hpp"
 32 #include "classfile/vmSymbols.hpp"
 33 #include "logging/log.hpp"
 34 #include "logging/logStream.hpp"
 35 #include "memory/resourceArea.hpp"
 36 #include "oops/fieldStreams.inline.hpp"
 37 #include "oops/klass.inline.hpp"
 38 #include "oops/oop.inline.hpp"
 39 #include "oops/oopHandle.inline.hpp"
 40 #include "runtime/fieldDescriptor.inline.hpp"
 41 
 42 #if INCLUDE_CDS_JAVA_HEAP
 43 
 44 // CDSHeapVerifier is used to check for problems where an archived object references a
 45 // static field that may be get a different value at runtime.
 46 //
 47 // *Please see comments in aotClassInitializer.cpp for how to avoid such problems*,
 48 //
 49 // In the following example,
 50 //      Foo.get.test()
 51 // correctly returns true when CDS disabled, but incorrectly returns false when CDS is enabled,
 52 // because the archived archivedFoo.bar value is different than Bar.bar.
 53 //
 54 // class Foo {
 55 //     static final Foo archivedFoo; // this field is archived by CDS
 56 //     Bar bar;
 57 //     static {
 58 //         CDS.initializeFromArchive(Foo.class);
 59 //         if (archivedFoo == null) {

 83 // [A] In most of the cases, the module bootstrap code will update the static field
 84 //     to point to part of the archived module graph. E.g.,
 85 //     - java/lang/System::bootLayer
 86 //     - jdk/internal/loader/ClassLoaders::BOOT_LOADER
 87 // [B] A final static String that's explicitly initialized inside <clinit>, but
 88 //     its value is deterministic and is always the same string literal.
 89 // [C] A non-final static string that is assigned a string literal during class
 90 //     initialization; this string is never changed during -Xshare:dump.
 91 // [D] Simple caches whose value doesn't matter.
 92 // [E] Other cases (see comments in-line below).
 93 
 94 CDSHeapVerifier::CDSHeapVerifier() : _archived_objs(0), _problems(0)
 95 {
 96 # define ADD_EXCL(...) { static const char* e[] = {__VA_ARGS__, nullptr}; add_exclusion(e); }
 97 
 98   // Unfortunately this needs to be manually maintained. If
 99   // test/hotspot/jtreg/runtime/cds/appcds/cacheObject/ArchivedEnumTest.java fails,
100   // you might need to fix the core library code, or fix the ADD_EXCL entries below.
101   //
102   //       class                                         field                     type
103   ADD_EXCL("java/lang/ClassLoader$Holder",               "scl");                   // A
104   ADD_EXCL("java/lang/Module",                           "ALL_UNNAMED_MODULE",     // A
105                                                          "ALL_UNNAMED_MODULE_SET", // A
106                                                          "EVERYONE_MODULE",        // A
107                                                          "EVERYONE_SET");          // A
108 
109   // This is the same as java/util/ImmutableCollections::EMPTY_SET, which is archived
110   ADD_EXCL("java/lang/reflect/AccessFlag$Location",      "EMPTY_SET");             // E
111 
112   ADD_EXCL("java/lang/System",                           "bootLayer");             // A
113 
114   ADD_EXCL("java/util/Collections",                      "EMPTY_LIST");           // E
115 
116   // A dummy object used by HashSet. The value doesn't matter and it's never
117   // tested for equality.
118   ADD_EXCL("java/util/HashSet",                          "PRESENT");               // E
119   ADD_EXCL("jdk/internal/loader/BuiltinClassLoader",     "packageToModule");       // A
120   ADD_EXCL("jdk/internal/loader/ClassLoaders",           "BOOT_LOADER",            // A
121                                                          "APP_LOADER",             // A
122                                                          "PLATFORM_LOADER");       // A
123   ADD_EXCL("jdk/internal/module/Builder",                "cachedVersion");         // D

128   ADD_EXCL("jdk/internal/module/ServicesCatalog",        "CLV");                   // A
129 
130   // This just points to an empty Map
131   ADD_EXCL("jdk/internal/reflect/Reflection",            "methodFilterMap");       // E
132 
133   // Integer for 0 and 1 are in java/lang/Integer$IntegerCache and are archived
134   ADD_EXCL("sun/invoke/util/ValueConversions",           "ONE_INT",                // E
135                                                          "ZERO_INT");              // E
136 
137   if (CDSConfig::is_dumping_method_handles()) {
138     ADD_EXCL("java/lang/invoke/InvokerBytecodeGenerator", "MEMBERNAME_FACTORY",    // D
139                                                           "CD_Object_array",       // E same as <...>ConstantUtils.CD_Object_array::CD_Object
140                                                           "INVOKER_SUPER_DESC");   // E same as java.lang.constant.ConstantDescs::CD_Object
141 
142     ADD_EXCL("java/lang/runtime/ObjectMethods",           "CLASS_IS_INSTANCE",     // D
143                                                           "FALSE",                 // D
144                                                           "TRUE",                  // D
145                                                           "ZERO");                 // D
146   }
147 
148   if (CDSConfig::is_dumping_packages()) {
149     ADD_EXCL("java/lang/Package$VersionInfo",             "NULL_VERSION_INFO");    // D
150   }
151 
152   if (CDSConfig::is_dumping_dynamic_proxies()) {
153     ADD_EXCL("java/lang/reflect/ProxyGenerator",          "CD_Object_array");      // D
154   }
155 
156   // These are used by BuiltinClassLoader::negativeLookupCache, etc but seem to be
157   // OK. TODO - we should completely disable the caching unless ArchiveLoaderLookupCache
158   // is enabled
159   ADD_EXCL("java/lang/Boolean",                           "TRUE",                  // E
160                                                           "FALSE");                // E
161 
162 # undef ADD_EXCL
163 
164   ClassLoaderDataGraph::classes_do(this);
165 }
166 
167 CDSHeapVerifier::~CDSHeapVerifier() {
168   if (_problems > 0) {
169     log_error(aot, heap)("Scanned %d objects. Found %d case(s) where "
170                          "an object points to a static field that "
171                          "may hold a different value at runtime.", _archived_objs, _problems);
172     log_error(aot, heap)("Please see cdsHeapVerifier.cpp and aotClassInitializer.cpp for details");
173     MetaspaceShared::unrecoverable_writing_error();
174   }
175 }
176 
177 class CDSHeapVerifier::CheckStaticFields : public FieldClosure {
178   CDSHeapVerifier* _verifier;
179   InstanceKlass* _ik; // The class whose static fields are being checked.
180   const char** _exclusions;
181 public:

269     }
270 
271     if (ArchiveUtils::has_aot_initialized_mirror(ik)) {
272       // ik's <clinit> won't be executed at runtime, the static fields in
273       // ik will carry their values to runtime.
274       return;
275     }
276 
277     CheckStaticFields csf(this, ik);
278     ik->do_local_static_fields(&csf);
279   }
280 }
281 
282 void CDSHeapVerifier::add_static_obj_field(InstanceKlass* ik, oop field, Symbol* name) {
283   StaticFieldInfo info = {ik, name};
284   _table.put(field, info);
285 }
286 
287 // This function is called once for every archived heap object. Warn if this object is referenced by
288 // a static field of a class that's not aot-initialized.
289 inline bool CDSHeapVerifier::do_entry(OopHandle& orig_obj_handle, HeapShared::CachedOopInfo& value) {
290   oop orig_obj = orig_obj_handle.resolve();
291   _archived_objs++;
292 
293   if (java_lang_String::is_instance(orig_obj) && HeapShared::is_dumped_interned_string(orig_obj)) {
294     // It's quite often for static fields to have interned strings. These are most likely not
295     // problematic (and are hard to filter). So we will ignore them.
296     return true; /* keep on iterating */
297   }
298 
299   StaticFieldInfo* info = _table.get(orig_obj);
300   if (info != nullptr) {
301     ResourceMark rm;
302     char* class_name = info->_holder->name()->as_C_string();
303     char* field_name = info->_name->as_C_string();
304     LogStream ls(Log(aot, heap)::warning());
305     ls.print_cr("Archive heap points to a static field that may hold a different value at runtime:");
306     ls.print_cr("Field: %s::%s", class_name, field_name);
307     ls.print("Value: ");
308     orig_obj->print_on(&ls);
309     ls.print_cr("--- trace begin ---");
310     trace_to_root(&ls, orig_obj, nullptr, &value);

320   oop _orig_obj;
321   oop _orig_field;
322   outputStream* _st;
323 
324 public:
325   TraceFields(oop orig_obj, oop orig_field, outputStream* st)
326     : _orig_obj(orig_obj), _orig_field(orig_field), _st(st) {}
327 
328   void do_field(fieldDescriptor* fd) {
329     if (fd->field_type() == T_OBJECT || fd->field_type() == T_ARRAY) {
330       oop obj_field = _orig_obj->obj_field(fd->offset());
331       if (obj_field == _orig_field) {
332         _st->print("::%s (offset = %d)", fd->name()->as_C_string(), fd->offset());
333       }
334     }
335   }
336 };
337 
338 // Call this function (from gdb, etc) if you want to know why an object is archived.
339 void CDSHeapVerifier::trace_to_root(outputStream* st, oop orig_obj) {
340   HeapShared::CachedOopInfo* info = HeapShared::get_cached_oop_info(orig_obj);
341   if (info != nullptr) {
342     trace_to_root(st, orig_obj, nullptr, info);
343   } else {
344     st->print_cr("Not an archived object??");
345   }
346 }
347 
348 const char* static_field_name(oop mirror, oop field) {
349   Klass* k = java_lang_Class::as_Klass(mirror);
350   if (k->is_instance_klass()) {
351     for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) {
352       if (fs.access_flags().is_static()) {
353         fieldDescriptor& fd = fs.field_descriptor();
354         switch (fd.field_type()) {
355         case T_OBJECT:
356         case T_ARRAY:
357           if (mirror->obj_field(fd.offset()) == field) {
358             return fs.name()->as_C_string();
359           }
360           break;
361         default:
362           break;
363         }
364       }
365     }
366   }
367 
368   return "<unknown>";
369 }
370 
371 int CDSHeapVerifier::trace_to_root(outputStream* st, oop orig_obj, oop orig_field, HeapShared::CachedOopInfo* info) {
372   int level = 0;
373   if (info->orig_referrer() != nullptr) {
374     HeapShared::CachedOopInfo* ref = HeapShared::get_cached_oop_info(info->orig_referrer());
375     assert(ref != nullptr, "sanity");
376     level = trace_to_root(st, info->orig_referrer(), orig_obj, ref) + 1;
377   } else if (java_lang_String::is_instance(orig_obj)) {
378     st->print_cr("[%2d] (shared string table)", level++);
379   }
380   Klass* k = orig_obj->klass();
381   ResourceMark rm;
382   st->print("[%2d] ", level);
383   orig_obj->print_address_on(st);
384   st->print(" %s", k->internal_name());
385   if (java_lang_Class::is_instance(orig_obj)) {
386     st->print(" (%s::%s)", java_lang_Class::as_Klass(orig_obj)->external_name(), static_field_name(orig_obj, orig_field));
387   }
388   if (orig_field != nullptr) {
389     if (k->is_instance_klass()) {
390       TraceFields clo(orig_obj, orig_field, st);
391       InstanceKlass::cast(k)->do_nonstatic_fields(&clo);
392     } else {
393       assert(orig_obj->is_objArray(), "must be");
394       objArrayOop array = (objArrayOop)orig_obj;
< prev index next >