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
119 ADD_EXCL("jdk/internal/loader/BootLoader", "UNNAMED_MODULE"); // A
120 ADD_EXCL("jdk/internal/loader/BuiltinClassLoader", "packageToModule"); // A
121 ADD_EXCL("jdk/internal/loader/ClassLoaders", "BOOT_LOADER", // A
122 "APP_LOADER", // A
129 ADD_EXCL("jdk/internal/module/ServicesCatalog", "CLV"); // A
130
131 // This just points to an empty Map
132 ADD_EXCL("jdk/internal/reflect/Reflection", "methodFilterMap"); // E
133
134 // Integer for 0 and 1 are in java/lang/Integer$IntegerCache and are archived
135 ADD_EXCL("sun/invoke/util/ValueConversions", "ONE_INT", // E
136 "ZERO_INT"); // E
137
138 if (CDSConfig::is_dumping_method_handles()) {
139 ADD_EXCL("java/lang/invoke/InvokerBytecodeGenerator", "MEMBERNAME_FACTORY", // D
140 "CD_Object_array", // E same as <...>ConstantUtils.CD_Object_array::CD_Object
141 "INVOKER_SUPER_DESC"); // E same as java.lang.constant.ConstantDescs::CD_Object
142
143 ADD_EXCL("java/lang/runtime/ObjectMethods", "CLASS_IS_INSTANCE", // D
144 "FALSE", // D
145 "TRUE", // D
146 "ZERO"); // D
147 }
148
149 # undef ADD_EXCL
150
151 ClassLoaderDataGraph::classes_do(this);
152 }
153
154 CDSHeapVerifier::~CDSHeapVerifier() {
155 if (_problems > 0) {
156 log_error(aot, heap)("Scanned %d objects. Found %d case(s) where "
157 "an object points to a static field that "
158 "may hold a different value at runtime.", _archived_objs, _problems);
159 log_error(aot, heap)("Please see cdsHeapVerifier.cpp and aotClassInitializer.cpp for details");
160 MetaspaceShared::unrecoverable_writing_error();
161 }
162 }
163
164 class CDSHeapVerifier::CheckStaticFields : public FieldClosure {
165 CDSHeapVerifier* _verifier;
166 InstanceKlass* _ik; // The class whose static fields are being checked.
167 const char** _exclusions;
168 public:
256 }
257
258 if (ArchiveUtils::has_aot_initialized_mirror(ik)) {
259 // ik's <clinit> won't be executed at runtime, the static fields in
260 // ik will carry their values to runtime.
261 return;
262 }
263
264 CheckStaticFields csf(this, ik);
265 ik->do_local_static_fields(&csf);
266 }
267 }
268
269 void CDSHeapVerifier::add_static_obj_field(InstanceKlass* ik, oop field, Symbol* name) {
270 StaticFieldInfo info = {ik, name};
271 _table.put(field, info);
272 }
273
274 // This function is called once for every archived heap object. Warn if this object is referenced by
275 // a static field of a class that's not aot-initialized.
276 inline bool CDSHeapVerifier::do_entry(oop& orig_obj, HeapShared::CachedOopInfo& value) {
277 _archived_objs++;
278
279 if (java_lang_String::is_instance(orig_obj) && HeapShared::is_dumped_interned_string(orig_obj)) {
280 // It's quite often for static fields to have interned strings. These are most likely not
281 // problematic (and are hard to filter). So we will ignore them.
282 return true; /* keep on iterating */
283 }
284
285 StaticFieldInfo* info = _table.get(orig_obj);
286 if (info != nullptr) {
287 ResourceMark rm;
288 char* class_name = info->_holder->name()->as_C_string();
289 char* field_name = info->_name->as_C_string();
290 LogStream ls(Log(aot, heap)::warning());
291 ls.print_cr("Archive heap points to a static field that may hold a different value at runtime:");
292 ls.print_cr("Field: %s::%s", class_name, field_name);
293 ls.print("Value: ");
294 orig_obj->print_on(&ls);
295 ls.print_cr("--- trace begin ---");
296 trace_to_root(&ls, orig_obj, nullptr, &value);
306 oop _orig_obj;
307 oop _orig_field;
308 outputStream* _st;
309
310 public:
311 TraceFields(oop orig_obj, oop orig_field, outputStream* st)
312 : _orig_obj(orig_obj), _orig_field(orig_field), _st(st) {}
313
314 void do_field(fieldDescriptor* fd) {
315 if (fd->field_type() == T_OBJECT || fd->field_type() == T_ARRAY) {
316 oop obj_field = _orig_obj->obj_field(fd->offset());
317 if (obj_field == _orig_field) {
318 _st->print("::%s (offset = %d)", fd->name()->as_C_string(), fd->offset());
319 }
320 }
321 }
322 };
323
324 // Call this function (from gdb, etc) if you want to know why an object is archived.
325 void CDSHeapVerifier::trace_to_root(outputStream* st, oop orig_obj) {
326 HeapShared::CachedOopInfo* info = HeapShared::archived_object_cache()->get(orig_obj);
327 if (info != nullptr) {
328 trace_to_root(st, orig_obj, nullptr, info);
329 } else {
330 st->print_cr("Not an archived object??");
331 }
332 }
333
334 const char* static_field_name(oop mirror, oop field) {
335 Klass* k = java_lang_Class::as_Klass(mirror);
336 if (k->is_instance_klass()) {
337 for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) {
338 if (fs.access_flags().is_static()) {
339 fieldDescriptor& fd = fs.field_descriptor();
340 switch (fd.field_type()) {
341 case T_OBJECT:
342 case T_ARRAY:
343 if (mirror->obj_field(fd.offset()) == field) {
344 return fs.name()->as_C_string();
345 }
346 break;
347 default:
348 break;
349 }
350 }
351 }
352 }
353
354 return "<unknown>";
355 }
356
357 int CDSHeapVerifier::trace_to_root(outputStream* st, oop orig_obj, oop orig_field, HeapShared::CachedOopInfo* info) {
358 int level = 0;
359 if (info->orig_referrer() != nullptr) {
360 HeapShared::CachedOopInfo* ref = HeapShared::archived_object_cache()->get(info->orig_referrer());
361 assert(ref != nullptr, "sanity");
362 level = trace_to_root(st, info->orig_referrer(), orig_obj, ref) + 1;
363 } else if (java_lang_String::is_instance(orig_obj)) {
364 st->print_cr("[%2d] (shared string table)", level++);
365 }
366 Klass* k = orig_obj->klass();
367 ResourceMark rm;
368 st->print("[%2d] ", level);
369 orig_obj->print_address_on(st);
370 st->print(" %s", k->internal_name());
371 if (java_lang_Class::is_instance(orig_obj)) {
372 st->print(" (%s::%s)", java_lang_Class::as_Klass(orig_obj)->external_name(), static_field_name(orig_obj, orig_field));
373 }
374 if (orig_field != nullptr) {
375 if (k->is_instance_klass()) {
376 TraceFields clo(orig_obj, orig_field, st);
377 InstanceKlass::cast(k)->do_nonstatic_fields(&clo);
378 } else {
379 assert(orig_obj->is_objArray(), "must be");
380 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
120 ADD_EXCL("jdk/internal/loader/BootLoader", "UNNAMED_MODULE"); // A
121 ADD_EXCL("jdk/internal/loader/BuiltinClassLoader", "packageToModule"); // A
122 ADD_EXCL("jdk/internal/loader/ClassLoaders", "BOOT_LOADER", // A
123 "APP_LOADER", // A
130 ADD_EXCL("jdk/internal/module/ServicesCatalog", "CLV"); // A
131
132 // This just points to an empty Map
133 ADD_EXCL("jdk/internal/reflect/Reflection", "methodFilterMap"); // E
134
135 // Integer for 0 and 1 are in java/lang/Integer$IntegerCache and are archived
136 ADD_EXCL("sun/invoke/util/ValueConversions", "ONE_INT", // E
137 "ZERO_INT"); // E
138
139 if (CDSConfig::is_dumping_method_handles()) {
140 ADD_EXCL("java/lang/invoke/InvokerBytecodeGenerator", "MEMBERNAME_FACTORY", // D
141 "CD_Object_array", // E same as <...>ConstantUtils.CD_Object_array::CD_Object
142 "INVOKER_SUPER_DESC"); // E same as java.lang.constant.ConstantDescs::CD_Object
143
144 ADD_EXCL("java/lang/runtime/ObjectMethods", "CLASS_IS_INSTANCE", // D
145 "FALSE", // D
146 "TRUE", // D
147 "ZERO"); // D
148 }
149
150 if (CDSConfig::is_dumping_packages()) {
151 ADD_EXCL("java/lang/Package$VersionInfo", "NULL_VERSION_INFO"); // D
152 }
153
154 if (CDSConfig::is_dumping_dynamic_proxies()) {
155 ADD_EXCL("java/lang/reflect/ProxyGenerator", "CD_Object_array"); // D
156 }
157
158 // These are used by BuiltinClassLoader::negativeLookupCache, etc but seem to be
159 // OK. TODO - we should completely disable the caching unless ArchiveLoaderLookupCache
160 // is enabled
161 ADD_EXCL("java/lang/Boolean", "TRUE", // E
162 "FALSE"); // E
163
164 # undef ADD_EXCL
165
166 ClassLoaderDataGraph::classes_do(this);
167 }
168
169 CDSHeapVerifier::~CDSHeapVerifier() {
170 if (_problems > 0) {
171 log_error(aot, heap)("Scanned %d objects. Found %d case(s) where "
172 "an object points to a static field that "
173 "may hold a different value at runtime.", _archived_objs, _problems);
174 log_error(aot, heap)("Please see cdsHeapVerifier.cpp and aotClassInitializer.cpp for details");
175 MetaspaceShared::unrecoverable_writing_error();
176 }
177 }
178
179 class CDSHeapVerifier::CheckStaticFields : public FieldClosure {
180 CDSHeapVerifier* _verifier;
181 InstanceKlass* _ik; // The class whose static fields are being checked.
182 const char** _exclusions;
183 public:
271 }
272
273 if (ArchiveUtils::has_aot_initialized_mirror(ik)) {
274 // ik's <clinit> won't be executed at runtime, the static fields in
275 // ik will carry their values to runtime.
276 return;
277 }
278
279 CheckStaticFields csf(this, ik);
280 ik->do_local_static_fields(&csf);
281 }
282 }
283
284 void CDSHeapVerifier::add_static_obj_field(InstanceKlass* ik, oop field, Symbol* name) {
285 StaticFieldInfo info = {ik, name};
286 _table.put(field, info);
287 }
288
289 // This function is called once for every archived heap object. Warn if this object is referenced by
290 // a static field of a class that's not aot-initialized.
291 inline bool CDSHeapVerifier::do_entry(OopHandle& orig_obj_handle, HeapShared::CachedOopInfo& value) {
292 oop orig_obj = orig_obj_handle.resolve();
293 _archived_objs++;
294
295 if (java_lang_String::is_instance(orig_obj) && HeapShared::is_dumped_interned_string(orig_obj)) {
296 // It's quite often for static fields to have interned strings. These are most likely not
297 // problematic (and are hard to filter). So we will ignore them.
298 return true; /* keep on iterating */
299 }
300
301 StaticFieldInfo* info = _table.get(orig_obj);
302 if (info != nullptr) {
303 ResourceMark rm;
304 char* class_name = info->_holder->name()->as_C_string();
305 char* field_name = info->_name->as_C_string();
306 LogStream ls(Log(aot, heap)::warning());
307 ls.print_cr("Archive heap points to a static field that may hold a different value at runtime:");
308 ls.print_cr("Field: %s::%s", class_name, field_name);
309 ls.print("Value: ");
310 orig_obj->print_on(&ls);
311 ls.print_cr("--- trace begin ---");
312 trace_to_root(&ls, orig_obj, nullptr, &value);
322 oop _orig_obj;
323 oop _orig_field;
324 outputStream* _st;
325
326 public:
327 TraceFields(oop orig_obj, oop orig_field, outputStream* st)
328 : _orig_obj(orig_obj), _orig_field(orig_field), _st(st) {}
329
330 void do_field(fieldDescriptor* fd) {
331 if (fd->field_type() == T_OBJECT || fd->field_type() == T_ARRAY) {
332 oop obj_field = _orig_obj->obj_field(fd->offset());
333 if (obj_field == _orig_field) {
334 _st->print("::%s (offset = %d)", fd->name()->as_C_string(), fd->offset());
335 }
336 }
337 }
338 };
339
340 // Call this function (from gdb, etc) if you want to know why an object is archived.
341 void CDSHeapVerifier::trace_to_root(outputStream* st, oop orig_obj) {
342 HeapShared::CachedOopInfo* info = HeapShared::get_cached_oop_info(orig_obj);
343 if (info != nullptr) {
344 trace_to_root(st, orig_obj, nullptr, info);
345 } else {
346 st->print_cr("Not an archived object??");
347 }
348 }
349
350 const char* static_field_name(oop mirror, oop field) {
351 Klass* k = java_lang_Class::as_Klass(mirror);
352 if (k->is_instance_klass()) {
353 for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) {
354 if (fs.access_flags().is_static()) {
355 fieldDescriptor& fd = fs.field_descriptor();
356 switch (fd.field_type()) {
357 case T_OBJECT:
358 case T_ARRAY:
359 if (mirror->obj_field(fd.offset()) == field) {
360 return fs.name()->as_C_string();
361 }
362 break;
363 default:
364 break;
365 }
366 }
367 }
368 }
369
370 return "<unknown>";
371 }
372
373 int CDSHeapVerifier::trace_to_root(outputStream* st, oop orig_obj, oop orig_field, HeapShared::CachedOopInfo* info) {
374 int level = 0;
375 if (info->orig_referrer() != nullptr) {
376 HeapShared::CachedOopInfo* ref = HeapShared::get_cached_oop_info(info->orig_referrer());
377 assert(ref != nullptr, "sanity");
378 level = trace_to_root(st, info->orig_referrer(), orig_obj, ref) + 1;
379 } else if (java_lang_String::is_instance(orig_obj)) {
380 st->print_cr("[%2d] (shared string table)", level++);
381 }
382 Klass* k = orig_obj->klass();
383 ResourceMark rm;
384 st->print("[%2d] ", level);
385 orig_obj->print_address_on(st);
386 st->print(" %s", k->internal_name());
387 if (java_lang_Class::is_instance(orig_obj)) {
388 st->print(" (%s::%s)", java_lang_Class::as_Klass(orig_obj)->external_name(), static_field_name(orig_obj, orig_field));
389 }
390 if (orig_field != nullptr) {
391 if (k->is_instance_klass()) {
392 TraceFields clo(orig_obj, orig_field, st);
393 InstanceKlass::cast(k)->do_nonstatic_fields(&clo);
394 } else {
395 assert(orig_obj->is_objArray(), "must be");
396 objArrayOop array = (objArrayOop)orig_obj;
|