1 /* 2 * Copyright (c) 2022, 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/aotClassInitializer.hpp" 27 #include "cds/archiveBuilder.hpp" 28 #include "cds/cdsHeapVerifier.hpp" 29 #include "classfile/classLoaderDataGraph.hpp" 30 #include "classfile/javaClasses.inline.hpp" 31 #include "classfile/moduleEntry.hpp" 32 #include "classfile/systemDictionaryShared.hpp" 33 #include "classfile/vmSymbols.hpp" 34 #include "logging/log.hpp" 35 #include "logging/logStream.hpp" 36 #include "memory/resourceArea.hpp" 37 #include "oops/fieldStreams.inline.hpp" 38 #include "oops/klass.inline.hpp" 39 #include "oops/oop.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. In the following example, 46 // Foo.get.test() 47 // correctly returns true when CDS disabled, but incorrectly returns false when CDS is enabled, 48 // because the archived archivedFoo.bar value is different than Bar.bar. 49 // 50 // class Foo { 51 // static final Foo archivedFoo; // this field is archived by CDS 52 // Bar bar; 53 // static { 54 // CDS.initializeFromArchive(Foo.class); 55 // if (archivedFoo == null) { 56 // archivedFoo = new Foo(); 57 // archivedFoo.bar = Bar.bar; 58 // } 59 // } 60 // static Foo get() { return archivedFoo; } 61 // boolean test() { 62 // return bar == Bar.bar; 63 // } 64 // } 65 // 66 // class Bar { 67 // // this field is initialized in both CDS dump time and runtime. 68 // static final Bar bar = new Bar(); 69 // } 70 // 71 // The check itself is simple: 72 // [1] CDSHeapVerifier::do_klass() collects all static fields 73 // [2] CDSHeapVerifier::do_entry() checks all the archived objects. None of them 74 // should be in [1] 75 // 76 // However, it's legal for *some* static fields to be referenced. The reasons are explained 77 // in the table of ADD_EXCL below. 78 // 79 // [A] In most of the cases, the module bootstrap code will update the static field 80 // to point to part of the archived module graph. E.g., 81 // - java/lang/System::bootLayer 82 // - jdk/internal/loader/ClassLoaders::BOOT_LOADER 83 // [B] A final static String that's explicitly initialized inside <clinit>, but 84 // its value is deterministic and is always the same string literal. 85 // [C] A non-final static string that is assigned a string literal during class 86 // initialization; this string is never changed during -Xshare:dump. 87 // [D] Simple caches whose value doesn't matter. 88 // [E] Other cases (see comments in-line below). 89 90 CDSHeapVerifier::CDSHeapVerifier() : _archived_objs(0), _problems(0) 91 { 92 # define ADD_EXCL(...) { static const char* e[] = {__VA_ARGS__, nullptr}; add_exclusion(e); } 93 94 // Unfortunately this needs to be manually maintained. If 95 // test/hotspot/jtreg/runtime/cds/appcds/cacheObject/ArchivedEnumTest.java fails, 96 // you might need to fix the core library code, or fix the ADD_EXCL entries below. 97 // 98 // class field type 99 ADD_EXCL("java/lang/ClassLoader", "scl"); // A 100 ADD_EXCL("java/lang/Module", "ALL_UNNAMED_MODULE", // A 101 "ALL_UNNAMED_MODULE_SET", // A 102 "EVERYONE_MODULE", // A 103 "EVERYONE_SET"); // A 104 105 // This is the same as java/util/ImmutableCollections::EMPTY_SET, which is archived 106 ADD_EXCL("java/lang/reflect/AccessFlag$Location", "EMPTY_SET"); // E 107 108 ADD_EXCL("java/lang/System", "bootLayer"); // A 109 110 // A dummy object used by HashSet. The value doesn't matter and it's never 111 // tested for equality. 112 ADD_EXCL("java/util/HashSet", "PRESENT"); // E 113 ADD_EXCL("jdk/internal/loader/BuiltinClassLoader", "packageToModule"); // A 114 ADD_EXCL("jdk/internal/loader/ClassLoaders", "BOOT_LOADER", // A 115 "APP_LOADER", // A 116 "PLATFORM_LOADER"); // A 117 ADD_EXCL("jdk/internal/module/Builder", "cachedVersion"); // D 118 ADD_EXCL("jdk/internal/module/ModuleLoaderMap$Mapper", "APP_CLASSLOADER", // A 119 "APP_LOADER_INDEX", // A 120 "PLATFORM_CLASSLOADER", // A 121 "PLATFORM_LOADER_INDEX"); // A 122 ADD_EXCL("jdk/internal/module/ServicesCatalog", "CLV"); // A 123 124 // This just points to an empty Map 125 ADD_EXCL("jdk/internal/reflect/Reflection", "methodFilterMap"); // E 126 127 // Integer for 0 and 1 are in java/lang/Integer$IntegerCache and are archived 128 ADD_EXCL("sun/invoke/util/ValueConversions", "ONE_INT", // E 129 "ZERO_INT"); // E 130 131 if (CDSConfig::is_dumping_invokedynamic()) { 132 ADD_EXCL("java/lang/invoke/InvokerBytecodeGenerator", "MEMBERNAME_FACTORY", // D 133 "CD_Object_array", // E same as <...>ConstantUtils.CD_Object_array::CD_Object 134 "INVOKER_SUPER_DESC"); // E same as java.lang.constant.ConstantDescs::CD_Object 135 } 136 137 // These are used by BuiltinClassLoader::negativeLookupCache, etc but seem to be 138 // OK. TODO - we should completely disable the caching unless ArchiveLoaderLookupCache 139 // is enabled 140 ADD_EXCL("java/lang/Boolean", "TRUE", // E 141 "FALSE"); // E 142 143 # undef ADD_EXCL 144 145 ClassLoaderDataGraph::classes_do(this); 146 } 147 148 CDSHeapVerifier::~CDSHeapVerifier() { 149 if (_problems > 0) { 150 log_error(cds, heap)("Scanned %d objects. Found %d case(s) where " 151 "an object points to a static field that " 152 "may hold a different value at runtime.", _archived_objs, _problems); 153 //MetaspaceShared::unrecoverable_writing_error(); // FIXME -- leyden+JEP483 merge 154 } 155 } 156 157 class CDSHeapVerifier::CheckStaticFields : public FieldClosure { 158 CDSHeapVerifier* _verifier; 159 InstanceKlass* _ik; // The class whose static fields are being checked. 160 const char** _exclusions; 161 public: 162 CheckStaticFields(CDSHeapVerifier* verifier, InstanceKlass* ik) 163 : _verifier(verifier), _ik(ik) { 164 _exclusions = _verifier->find_exclusion(_ik); 165 } 166 167 void do_field(fieldDescriptor* fd) { 168 if (fd->field_type() != T_OBJECT) { 169 return; 170 } 171 172 if (fd->signature()->equals("Ljdk/internal/access/JavaLangAccess;")) { 173 // A few classes have static fields that point to SharedSecrets.getJavaLangAccess(). 174 // This object carries no state and we can create a new one in the production run. 175 return; 176 } 177 oop static_obj_field = _ik->java_mirror()->obj_field(fd->offset()); 178 if (static_obj_field != nullptr) { 179 Klass* field_type = static_obj_field->klass(); 180 if (_exclusions != nullptr) { 181 for (const char** p = _exclusions; *p != nullptr; p++) { 182 if (fd->name()->equals(*p)) { 183 return; 184 } 185 } 186 } 187 188 if (fd->is_final() && java_lang_String::is_instance(static_obj_field) && fd->has_initial_value()) { 189 // This field looks like like this in the Java source: 190 // static final SOME_STRING = "a string literal"; 191 // This string literal has been stored in the shared string table, so it's OK 192 // for the archived objects to refer to it. 193 return; 194 } 195 if (fd->is_final() && java_lang_Class::is_instance(static_obj_field)) { 196 // This field points to an archived mirror. 197 return; 198 } 199 200 if (field_type->is_instance_klass()) { 201 InstanceKlass* field_ik = InstanceKlass::cast(field_type); 202 if (field_ik->is_enum_subclass()) { 203 if (field_ik->has_archived_enum_objs() || ArchiveUtils::has_aot_initialized_mirror(field_ik)) { 204 // This field is an Enum. If any instance of this Enum has been archived, we will archive 205 // all static fields of this Enum as well. 206 return; 207 } 208 } 209 210 if (field_ik->is_hidden() && ArchiveUtils::has_aot_initialized_mirror(field_ik)) { 211 // We have a static field in a core-library class that points to a method reference, which 212 // are safe to archive. 213 guarantee(_ik->module()->name() == vmSymbols::java_base(), "sanity"); 214 return; 215 } 216 217 if (field_ik == vmClasses::MethodType_klass()) { 218 // The identity of MethodTypes are preserved between assembly phase and production runs 219 // (by MethodType::AOTHolder::archivedMethodTypes). No need to check. 220 return; 221 } 222 223 if (field_ik == vmClasses::internal_Unsafe_klass() && ArchiveUtils::has_aot_initialized_mirror(field_ik)) { 224 // There's only a single instance of jdk/internal/misc/Unsafe, so all references will 225 // be pointing to this singleton, which has been archived. 226 return; 227 } 228 } 229 230 // This field *may* be initialized to a different value at runtime. Remember it 231 // and check later if it appears in the archived object graph. 232 _verifier->add_static_obj_field(_ik, static_obj_field, fd->name()); 233 } 234 } 235 }; 236 237 // Remember all the static object fields of every class that are currently 238 // loaded. Later, we will check if any archived objects reference one of 239 // these fields. 240 void CDSHeapVerifier::do_klass(Klass* k) { 241 if (k->is_instance_klass()) { 242 InstanceKlass* ik = InstanceKlass::cast(k); 243 244 if (HeapShared::is_subgraph_root_class(ik)) { 245 // ik is inside one of the ArchivableStaticFieldInfo tables 246 // in heapShared.cpp. We assume such classes are programmed to 247 // update their static fields correctly at runtime. 248 return; 249 } 250 251 if (ArchiveUtils::has_aot_initialized_mirror(ik)) { 252 // ik's <clinit> won't be executed at runtime, the static fields in 253 // ik will carry their values to runtime. 254 return; 255 } 256 257 CheckStaticFields csf(this, ik); 258 ik->do_local_static_fields(&csf); 259 } 260 } 261 262 void CDSHeapVerifier::add_static_obj_field(InstanceKlass* ik, oop field, Symbol* name) { 263 StaticFieldInfo info = {ik, name}; 264 _table.put(field, info); 265 } 266 267 inline bool CDSHeapVerifier::do_entry(oop& orig_obj, HeapShared::CachedOopInfo& value) { 268 _archived_objs++; 269 270 StaticFieldInfo* info = _table.get(orig_obj); 271 if (info != nullptr) { 272 if (value.orig_referrer() == nullptr && java_lang_String::is_instance(orig_obj)) { 273 // This string object is not referenced by any of the archived object graphs. It's archived 274 // only because it's in the interned string table. So we are not in a condition that 275 // should be flagged by CDSHeapVerifier. 276 return true; /* keep on iterating */ 277 } 278 if (info->_holder->is_hidden()) { 279 return true; 280 } 281 ResourceMark rm; 282 char* class_name = info->_holder->name()->as_C_string(); 283 char* field_name = info->_name->as_C_string(); 284 LogStream ls(Log(cds, heap)::warning()); 285 ls.print_cr("Archive heap points to a static field that may hold a different value at runtime:"); 286 ls.print_cr("Field: %s::%s", class_name, field_name); 287 ls.print("Value: "); 288 orig_obj->print_on(&ls); 289 ls.print_cr("--- trace begin ---"); 290 trace_to_root(&ls, orig_obj, nullptr, &value); 291 ls.print_cr("--- trace end ---"); 292 ls.cr(); 293 _problems ++; 294 } 295 296 return true; /* keep on iterating */ 297 } 298 299 class CDSHeapVerifier::TraceFields : public FieldClosure { 300 oop _orig_obj; 301 oop _orig_field; 302 outputStream* _st; 303 304 public: 305 TraceFields(oop orig_obj, oop orig_field, outputStream* st) 306 : _orig_obj(orig_obj), _orig_field(orig_field), _st(st) {} 307 308 void do_field(fieldDescriptor* fd) { 309 if (fd->field_type() == T_OBJECT || fd->field_type() == T_ARRAY) { 310 oop obj_field = _orig_obj->obj_field(fd->offset()); 311 if (obj_field == _orig_field) { 312 _st->print("::%s (offset = %d)", fd->name()->as_C_string(), fd->offset()); 313 } 314 } 315 } 316 }; 317 318 // Call this function (from gdb, etc) if you want to know why an object is archived. 319 void CDSHeapVerifier::trace_to_root(outputStream* st, oop orig_obj) { 320 HeapShared::CachedOopInfo* info = HeapShared::archived_object_cache()->get(orig_obj); 321 if (info != nullptr) { 322 trace_to_root(st, orig_obj, nullptr, info); 323 } else { 324 st->print_cr("Not an archived object??"); 325 } 326 } 327 328 const char* static_field_name(oop mirror, oop field) { 329 Klass* k = java_lang_Class::as_Klass(mirror); 330 if (k->is_instance_klass()) { 331 for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) { 332 if (fs.access_flags().is_static()) { 333 fieldDescriptor& fd = fs.field_descriptor(); 334 switch (fd.field_type()) { 335 case T_OBJECT: 336 case T_ARRAY: 337 if (mirror->obj_field(fd.offset()) == field) { 338 return fs.name()->as_C_string(); 339 } 340 break; 341 default: 342 break; 343 } 344 } 345 } 346 } 347 348 return "<unknown>"; 349 } 350 351 int CDSHeapVerifier::trace_to_root(outputStream* st, oop orig_obj, oop orig_field, HeapShared::CachedOopInfo* info) { 352 int level = 0; 353 if (info->orig_referrer() != nullptr) { 354 HeapShared::CachedOopInfo* ref = HeapShared::archived_object_cache()->get(info->orig_referrer()); 355 assert(ref != nullptr, "sanity"); 356 level = trace_to_root(st, info->orig_referrer(), orig_obj, ref) + 1; 357 } else if (java_lang_String::is_instance(orig_obj)) { 358 st->print_cr("[%2d] (shared string table)", level++); 359 } 360 Klass* k = orig_obj->klass(); 361 ResourceMark rm; 362 st->print("[%2d] ", level); 363 orig_obj->print_address_on(st); 364 st->print(" %s", k->internal_name()); 365 if (java_lang_Class::is_instance(orig_obj)) { 366 st->print(" (%s::%s)", java_lang_Class::as_Klass(orig_obj)->external_name(), static_field_name(orig_obj, orig_field)); 367 } 368 if (orig_field != nullptr) { 369 if (k->is_instance_klass()) { 370 TraceFields clo(orig_obj, orig_field, st); 371 InstanceKlass::cast(k)->do_nonstatic_fields(&clo); 372 } else { 373 assert(orig_obj->is_objArray(), "must be"); 374 objArrayOop array = (objArrayOop)orig_obj; 375 for (int i = 0; i < array->length(); i++) { 376 if (array->obj_at(i) == orig_field) { 377 st->print(" @[%d]", i); 378 break; 379 } 380 } 381 } 382 } 383 st->cr(); 384 385 return level; 386 } 387 388 void CDSHeapVerifier::verify() { 389 CDSHeapVerifier verf; 390 HeapShared::archived_object_cache()->iterate(&verf); 391 } 392 393 #endif // INCLUDE_CDS_JAVA_HEAP