1 /* 2 * Copyright (c) 2018, 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 "classfile/javaClasses.inline.hpp" 27 #include "classfile/symbolTable.hpp" 28 #include "classfile/vmSymbols.hpp" 29 #include "logging/log.hpp" 30 #include "logging/logMessage.hpp" 31 #include "logging/logStream.hpp" 32 #include "memory/heapShared.hpp" 33 #include "memory/iterator.inline.hpp" 34 #include "memory/metadataFactory.hpp" 35 #include "memory/metaspaceClosure.hpp" 36 #include "memory/metaspaceShared.hpp" 37 #include "memory/resourceArea.hpp" 38 #include "oops/compressedOops.inline.hpp" 39 #include "oops/oop.inline.hpp" 40 #include "runtime/fieldDescriptor.hpp" 41 42 #if INCLUDE_CDS_JAVA_HEAP 43 KlassSubGraphInfo* HeapShared::_subgraph_info_list = NULL; 44 int HeapShared::_num_archived_subgraph_info_records = 0; 45 Array<ArchivedKlassSubGraphInfoRecord>* HeapShared::_archived_subgraph_info_records = NULL; 46 47 // Currently there is only one class mirror (ArchivedModuleGraph) with archived 48 // sub-graphs. 49 KlassSubGraphInfo* HeapShared::find_subgraph_info(Klass* k) { 50 KlassSubGraphInfo* info = _subgraph_info_list; 51 while (info != NULL) { 52 if (info->klass() == k) { 53 return info; 54 } 55 info = info->next(); 56 } 57 return NULL; 58 } 59 60 // Get the subgraph_info for Klass k. A new subgraph_info is created if 61 // there is no existing one for k. The subgraph_info records the relocated 62 // Klass* of the original k. 63 KlassSubGraphInfo* HeapShared::get_subgraph_info(Klass* k) { 64 Klass* relocated_k = MetaspaceShared::get_relocated_klass(k); 65 KlassSubGraphInfo* info = find_subgraph_info(relocated_k); 66 if (info != NULL) { 67 return info; 68 } 69 70 info = new KlassSubGraphInfo(relocated_k, _subgraph_info_list); 71 _subgraph_info_list = info; 72 return info; 73 } 74 75 int HeapShared::num_of_subgraph_infos() { 76 int num = 0; 77 KlassSubGraphInfo* info = _subgraph_info_list; 78 while (info != NULL) { 79 num ++; 80 info = info->next(); 81 } 82 return num; 83 } 84 85 // Add an entry field to the current KlassSubGraphInfo. 86 void KlassSubGraphInfo::add_subgraph_entry_field(int static_field_offset, oop v) { 87 assert(DumpSharedSpaces, "dump time only"); 88 if (_subgraph_entry_fields == NULL) { 89 _subgraph_entry_fields = 90 new(ResourceObj::C_HEAP, mtClass) GrowableArray<juint>(10, true); 91 } 92 _subgraph_entry_fields->append((juint)static_field_offset); 93 _subgraph_entry_fields->append(CompressedOops::encode(v)); 94 } 95 96 // Add the Klass* for an object in the current KlassSubGraphInfo's subgraphs. 97 // Only objects of boot classes can be included in sub-graph. 98 void KlassSubGraphInfo::add_subgraph_object_klass(Klass* orig_k, Klass *relocated_k) { 99 assert(DumpSharedSpaces, "dump time only"); 100 assert(relocated_k == MetaspaceShared::get_relocated_klass(orig_k), 101 "must be the relocated Klass in the shared space"); 102 103 if (_subgraph_object_klasses == NULL) { 104 _subgraph_object_klasses = 105 new(ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(50, true); 106 } 107 108 assert(relocated_k->is_shared(), "must be a shared class"); 109 110 if (_k == relocated_k) { 111 // Don't add the Klass containing the sub-graph to it's own klass 112 // initialization list. 113 return; 114 } 115 116 if (relocated_k->is_instance_klass()) { 117 assert(InstanceKlass::cast(relocated_k)->is_shared_boot_class(), 118 "must be boot class"); 119 // SystemDictionary::xxx_klass() are not updated, need to check 120 // the original Klass* 121 if (orig_k == SystemDictionary::String_klass() || 122 orig_k == SystemDictionary::Object_klass()) { 123 // Initialized early during VM initialization. No need to be added 124 // to the sub-graph object class list. 125 return; 126 } 127 } else if (relocated_k->is_objArray_klass()) { 128 Klass* abk = ObjArrayKlass::cast(relocated_k)->bottom_klass(); 129 if (abk->is_instance_klass()) { 130 assert(InstanceKlass::cast(abk)->is_shared_boot_class(), 131 "must be boot class"); 132 } 133 if (relocated_k == Universe::objectArrayKlassObj()) { 134 // Initialized early during Universe::genesis. No need to be added 135 // to the list. 136 return; 137 } 138 } else { 139 assert(relocated_k->is_typeArray_klass(), "must be"); 140 // Primitive type arrays are created early during Universe::genesis. 141 return; 142 } 143 144 _subgraph_object_klasses->append_if_missing(relocated_k); 145 } 146 147 // Initialize an archived subgraph_info_record from the given KlassSubGraphInfo. 148 void ArchivedKlassSubGraphInfoRecord::init(KlassSubGraphInfo* info) { 149 _k = info->klass(); 150 _next = NULL; 151 _entry_field_records = NULL; 152 _subgraph_klasses = NULL; 153 154 // populate the entry fields 155 GrowableArray<juint>* entry_fields = info->subgraph_entry_fields(); 156 if (entry_fields != NULL) { 157 int num_entry_fields = entry_fields->length(); 158 assert(num_entry_fields % 2 == 0, "sanity"); 159 _entry_field_records = 160 MetaspaceShared::new_ro_array<juint>(num_entry_fields); 161 for (int i = 0 ; i < num_entry_fields; i++) { 162 _entry_field_records->at_put(i, entry_fields->at(i)); 163 } 164 } 165 166 // the Klasses of the objects in the sub-graphs 167 GrowableArray<Klass*>* subgraph_klasses = info->subgraph_object_klasses(); 168 if (subgraph_klasses != NULL) { 169 int num_subgraphs_klasses = subgraph_klasses->length(); 170 _subgraph_klasses = 171 MetaspaceShared::new_ro_array<Klass*>(num_subgraphs_klasses); 172 for (int i = 0; i < num_subgraphs_klasses; i++) { 173 Klass* subgraph_k = subgraph_klasses->at(i); 174 if (log_is_enabled(Info, cds, heap)) { 175 ResourceMark rm; 176 log_info(cds, heap)( 177 "Archived object klass (%d): %s in %s sub-graphs", 178 i, subgraph_k->external_name(), _k->external_name()); 179 } 180 _subgraph_klasses->at_put(i, subgraph_k); 181 } 182 } 183 } 184 185 // Build the records of archived subgraph infos, which include: 186 // - Entry points to all subgraphs from the containing class mirror. The entry 187 // points are static fields in the mirror. For each entry point, the field 188 // offset and value are recorded in the sub-graph info. The value are stored 189 // back to the corresponding field at runtime. 190 // - A list of klasses that need to be loaded/initialized before archived 191 // java object sub-graph can be accessed at runtime. 192 // 193 // The records are saved in the archive file and reloaded at runtime. Currently 194 // there is only one class mirror (ArchivedModuleGraph) with archived sub-graphs. 195 // 196 // Layout of the archived subgraph info records: 197 // 198 // records_size | num_records | records* 199 // ArchivedKlassSubGraphInfoRecord | entry_fields | subgraph_object_klasses 200 size_t HeapShared::build_archived_subgraph_info_records(int num_records) { 201 // remember the start address 202 char* start_p = MetaspaceShared::read_only_space_top(); 203 204 // now populate the archived subgraph infos, which will be saved in the 205 // archive file 206 _archived_subgraph_info_records = 207 MetaspaceShared::new_ro_array<ArchivedKlassSubGraphInfoRecord>(num_records); 208 KlassSubGraphInfo* info = _subgraph_info_list; 209 int i = 0; 210 while (info != NULL) { 211 assert(i < _archived_subgraph_info_records->length(), "sanity"); 212 ArchivedKlassSubGraphInfoRecord* record = 213 _archived_subgraph_info_records->adr_at(i); 214 record->init(info); 215 info = info->next(); 216 i ++; 217 } 218 219 // _subgraph_info_list is no longer needed 220 delete _subgraph_info_list; 221 _subgraph_info_list = NULL; 222 223 char* end_p = MetaspaceShared::read_only_space_top(); 224 size_t records_size = end_p - start_p; 225 return records_size; 226 } 227 228 // Write the subgraph info records in the shared _ro region 229 void HeapShared::write_archived_subgraph_infos() { 230 assert(DumpSharedSpaces, "dump time only"); 231 232 Array<intptr_t>* records_header = MetaspaceShared::new_ro_array<intptr_t>(3); 233 234 _num_archived_subgraph_info_records = num_of_subgraph_infos(); 235 size_t records_size = build_archived_subgraph_info_records( 236 _num_archived_subgraph_info_records); 237 238 // Now write the header information: 239 // records_size, num_records, _archived_subgraph_info_records 240 assert(records_header != NULL, "sanity"); 241 intptr_t* p = (intptr_t*)(records_header->data()); 242 *p = (intptr_t)records_size; 243 p ++; 244 *p = (intptr_t)_num_archived_subgraph_info_records; 245 p ++; 246 *p = (intptr_t)_archived_subgraph_info_records; 247 } 248 249 char* HeapShared::read_archived_subgraph_infos(char* buffer) { 250 Array<intptr_t>* records_header = (Array<intptr_t>*)buffer; 251 intptr_t* p = (intptr_t*)(records_header->data()); 252 size_t records_size = (size_t)(*p); 253 p ++; 254 _num_archived_subgraph_info_records = *p; 255 p ++; 256 _archived_subgraph_info_records = 257 (Array<ArchivedKlassSubGraphInfoRecord>*)(*p); 258 259 buffer = (char*)_archived_subgraph_info_records + records_size; 260 return buffer; 261 } 262 263 void HeapShared::initialize_from_archived_subgraph(Klass* k) { 264 if (!MetaspaceShared::open_archive_heap_region_mapped()) { 265 return; // nothing to do 266 } 267 268 if (_num_archived_subgraph_info_records == 0) { 269 return; // no subgraph info records 270 } 271 272 // Initialize from archived data. Currently only ArchivedModuleGraph 273 // has archived object subgraphs, which is used during VM initialization 274 // time when bootstraping the system modules. No lock is needed. 275 Thread* THREAD = Thread::current(); 276 for (int i = 0; i < _archived_subgraph_info_records->length(); i++) { 277 ArchivedKlassSubGraphInfoRecord* record = _archived_subgraph_info_records->adr_at(i); 278 if (record->klass() == k) { 279 int i; 280 // Found the archived subgraph info record for the requesting klass. 281 // Load/link/initialize the klasses of the objects in the subgraph. 282 // NULL class loader is used. 283 Array<Klass*>* klasses = record->subgraph_klasses(); 284 if (klasses != NULL) { 285 for (i = 0; i < klasses->length(); i++) { 286 Klass* obj_k = klasses->at(i); 287 Klass* resolved_k = SystemDictionary::resolve_or_null( 288 (obj_k)->name(), THREAD); 289 if (resolved_k != obj_k) { 290 return; 291 } 292 if ((obj_k)->is_instance_klass()) { 293 InstanceKlass* ik = InstanceKlass::cast(obj_k); 294 ik->initialize(THREAD); 295 } else if ((obj_k)->is_objArray_klass()) { 296 ObjArrayKlass* oak = ObjArrayKlass::cast(obj_k); 297 oak->initialize(THREAD); 298 } 299 } 300 } 301 302 if (HAS_PENDING_EXCEPTION) { 303 CLEAR_PENDING_EXCEPTION; 304 // None of the field value will be set if there was an exception. 305 // The java code will not see any of the archived objects in the 306 // subgraphs referenced from k in this case. 307 return; 308 } 309 310 // Load the subgraph entry fields from the record and store them back to 311 // the corresponding fields within the mirror. 312 oop m = k->java_mirror(); 313 Array<juint>* entry_field_records = record->entry_field_records(); 314 if (entry_field_records != NULL) { 315 int efr_len = entry_field_records->length(); 316 assert(efr_len % 2 == 0, "sanity"); 317 for (i = 0; i < efr_len;) { 318 int field_offset = entry_field_records->at(i); 319 // The object refereced by the field becomes 'known' by GC from this 320 // point. All objects in the subgraph reachable from the object are 321 // also 'known' by GC. 322 oop v = MetaspaceShared::materialize_archived_object( 323 CompressedOops::decode(entry_field_records->at(i+1))); 324 m->obj_field_put(field_offset, v); 325 i += 2; 326 } 327 } 328 329 // Done. Java code can see the archived sub-graphs referenced from k's 330 // mirror after this point. 331 return; 332 } 333 } 334 } 335 336 class WalkOopAndArchiveClosure: public BasicOopIterateClosure { 337 int _level; 338 KlassSubGraphInfo* _subgraph_info; 339 oop _orig_referencing_obj; 340 oop _archived_referencing_obj; 341 public: 342 WalkOopAndArchiveClosure(int level, KlassSubGraphInfo* subgraph_info, 343 oop orig, oop archived) : _level(level), 344 _subgraph_info(subgraph_info), 345 _orig_referencing_obj(orig), 346 _archived_referencing_obj(archived) {} 347 void do_oop(narrowOop *p) { WalkOopAndArchiveClosure::do_oop_work(p); } 348 void do_oop( oop *p) { WalkOopAndArchiveClosure::do_oop_work(p); } 349 350 protected: 351 template <class T> void do_oop_work(T *p) { 352 oop obj = RawAccess<>::oop_load(p); 353 if (!CompressedOops::is_null(obj)) { 354 // A java.lang.Class instance can not be included in an archived 355 // object sub-graph. 356 if (java_lang_Class::is_instance(obj)) { 357 tty->print("Unknown java.lang.Class object is in the archived sub-graph\n"); 358 vm_exit(1); 359 } 360 361 LogTarget(Debug, cds, heap) log; 362 LogStream ls(log); 363 outputStream* out = &ls; 364 { 365 ResourceMark rm; 366 log.print("(%d) %s <--- referenced from: %s", 367 _level, obj->klass()->external_name(), 368 CompressedOops::is_null(_orig_referencing_obj) ? 369 "" : _orig_referencing_obj->klass()->external_name()); 370 obj->print_on(out); 371 } 372 373 if (MetaspaceShared::is_archive_object(obj)) { 374 // The current oop is an archived oop, nothing needs to be done 375 log.print("--- object is already archived ---"); 376 return; 377 } 378 379 size_t field_delta = pointer_delta( 380 p, _orig_referencing_obj, sizeof(char)); 381 T* new_p = (T*)(address(_archived_referencing_obj) + field_delta); 382 oop archived = MetaspaceShared::find_archived_heap_object(obj); 383 if (archived != NULL) { 384 // There is an archived copy existing, update reference to point 385 // to the archived copy 386 RawAccess<IS_NOT_NULL>::oop_store(new_p, archived); 387 log.print( 388 "--- found existing archived copy, store archived " PTR_FORMAT " in " PTR_FORMAT, 389 p2i(archived), p2i(new_p)); 390 return; 391 } 392 393 int l = _level + 1; 394 Thread* THREAD = Thread::current(); 395 // Archive the current oop before iterating through its references 396 archived = MetaspaceShared::archive_heap_object(obj, THREAD); 397 assert(MetaspaceShared::is_archive_object(archived), "must be archived"); 398 log.print("=== archiving oop " PTR_FORMAT " ==> " PTR_FORMAT, 399 p2i(obj), p2i(archived)); 400 401 // Following the references in the current oop and archive any 402 // encountered objects during the process 403 WalkOopAndArchiveClosure walker(l, _subgraph_info, obj, archived); 404 obj->oop_iterate(&walker); 405 406 // Update the reference in the archived copy of the referencing object 407 RawAccess<IS_NOT_NULL>::oop_store(new_p, archived); 408 log.print("=== store archived " PTR_FORMAT " in " PTR_FORMAT, 409 p2i(archived), p2i(new_p)); 410 411 // Add the klass to the list of classes that need to be loaded before 412 // module system initialization 413 Klass *orig_k = obj->klass(); 414 Klass *relocated_k = archived->klass(); 415 _subgraph_info->add_subgraph_object_klass(orig_k, relocated_k); 416 } 417 } 418 }; 419 420 // 421 // Start from the given static field in a java mirror and archive the 422 // complete sub-graph of java heap objects that are reached directly 423 // or indirectly from the starting object by following references. 424 // Currently, only ArchivedModuleGraph class instance (mirror) has archived 425 // object subgraphs. Sub-graph archiving restrictions (current): 426 // 427 // - All classes of objects in the archived sub-graph (including the 428 // entry class) must be boot class only. 429 // - No java.lang.Class instance (java mirror) can be included inside 430 // an archived sub-graph. Mirror can only be the sub-graph entry object. 431 // 432 // The Java heap object sub-graph archiving process (see 433 // WalkOopAndArchiveClosure): 434 // 435 // 1) Java object sub-graph archiving starts from a given static field 436 // within a Class instance (java mirror). If the static field is a 437 // refererence field and points to a non-null java object, proceed to 438 // the next step. 439 // 440 // 2) Archives the referenced java object. If an archived copy of the 441 // current object already exists, updates the pointer in the archived 442 // copy of the referencing object to point to the current archived object. 443 // Otherwise, proceed to the next step. 444 // 445 // 3) Follows all references within the current java object and recursively 446 // archive the sub-graph of objects starting from each reference. 447 // 448 // 4) Updates the pointer in the archived copy of referencing object to 449 // point to the current archived object. 450 // 451 // 5) The Klass of the current java object is added to the list of Klasses 452 // for loading and initialzing before any object in the archived graph can 453 // be accessed at runtime. 454 // 455 void HeapShared::archive_reachable_objects_from_static_field(Klass *k, 456 int field_offset, 457 BasicType field_type, 458 TRAPS) { 459 assert(DumpSharedSpaces, "dump time only"); 460 assert(k->is_instance_klass(), "sanity"); 461 assert(InstanceKlass::cast(k)->is_shared_boot_class(), 462 "must be boot class"); 463 464 oop m = k->java_mirror(); 465 oop archived_m = MetaspaceShared::find_archived_heap_object(m); 466 if (CompressedOops::is_null(archived_m)) { 467 return; 468 } 469 470 if (field_type == T_OBJECT || field_type == T_ARRAY) { 471 // obtain k's subGraph Info 472 KlassSubGraphInfo* subgraph_info = get_subgraph_info(k); 473 474 // get the object referenced by the field 475 oop f = m->obj_field(field_offset); 476 if (!CompressedOops::is_null(f)) { 477 LogTarget(Debug, cds, heap) log; 478 LogStream ls(log); 479 outputStream* out = &ls; 480 log.print("Start from: "); 481 f->print_on(out); 482 483 // get the archived copy of the field referenced object 484 oop af = MetaspaceShared::archive_heap_object(f, THREAD); 485 if (!MetaspaceShared::is_archive_object(f)) { 486 WalkOopAndArchiveClosure walker(1, subgraph_info, f, af); 487 f->oop_iterate(&walker); 488 } 489 490 // The field value is not preserved in the archived mirror. 491 // Record the field as a new subGraph entry point. The recorded 492 // information is restored from the archive at runtime. 493 subgraph_info->add_subgraph_entry_field(field_offset, af); 494 Klass *relocated_k = af->klass(); 495 Klass *orig_k = f->klass(); 496 subgraph_info->add_subgraph_object_klass(orig_k, relocated_k); 497 } else { 498 // The field contains null, we still need to record the entry point, 499 // so it can be restored at runtime. 500 subgraph_info->add_subgraph_entry_field(field_offset, NULL); 501 } 502 } else { 503 ShouldNotReachHere(); 504 } 505 } 506 507 struct ArchivableStaticFieldInfo { 508 const char* class_name; 509 const char* field_name; 510 InstanceKlass* klass; 511 int offset; 512 BasicType type; 513 }; 514 515 // If you add new entries to this table, you should know what you're doing! 516 static ArchivableStaticFieldInfo archivable_static_fields[] = { 517 {"jdk/internal/module/ArchivedModuleGraph", "archivedSystemModules"}, 518 {"jdk/internal/module/ArchivedModuleGraph", "archivedModuleFinder"}, 519 {"jdk/internal/module/ArchivedModuleGraph", "archivedMainModule"}, 520 {"jdk/internal/module/ArchivedModuleGraph", "archivedConfiguration"}, 521 {"java/util/ImmutableCollections$ListN", "EMPTY_LIST"}, 522 {"java/util/ImmutableCollections$MapN", "EMPTY_MAP"}, 523 {"java/util/ImmutableCollections$SetN", "EMPTY_SET"}, 524 {"java/lang/Integer$IntegerCache", "archivedCache"}, 525 {"java/lang/module/Configuration", "EMPTY_CONFIGURATION"}, 526 }; 527 528 const static int num_archivable_static_fields = sizeof(archivable_static_fields) / sizeof(ArchivableStaticFieldInfo); 529 530 class ArchivableStaticFieldFinder: public FieldClosure { 531 InstanceKlass* _ik; 532 Symbol* _field_name; 533 bool _found; 534 int _offset; 535 BasicType _type; 536 public: 537 ArchivableStaticFieldFinder(InstanceKlass* ik, Symbol* field_name) : 538 _ik(ik), _field_name(field_name), _found(false), _offset(-1), _type(T_ILLEGAL) {} 539 540 virtual void do_field(fieldDescriptor* fd) { 541 if (fd->name() == _field_name) { 542 assert(!_found, "fields cannot be overloaded"); 543 _found = true; 544 _offset = fd->offset(); 545 _type = fd->field_type(); 546 assert(_type == T_OBJECT || _type == T_ARRAY, "can archive only obj or array fields"); 547 } 548 } 549 bool found() { return _found; } 550 int offset() { return _offset; } 551 BasicType type() { return _type; } 552 }; 553 554 void HeapShared::init_archivable_static_fields(Thread* THREAD) { 555 for (int i = 0; i < num_archivable_static_fields; i++) { 556 ArchivableStaticFieldInfo* info = &archivable_static_fields[i]; 557 TempNewSymbol class_name = SymbolTable::new_symbol(info->class_name, THREAD); 558 TempNewSymbol field_name = SymbolTable::new_symbol(info->field_name, THREAD); 559 560 Klass* k = SystemDictionary::resolve_or_null(class_name, THREAD); 561 assert(k != NULL && !HAS_PENDING_EXCEPTION, "class must exist"); 562 InstanceKlass* ik = InstanceKlass::cast(k); 563 564 ArchivableStaticFieldFinder finder(ik, field_name); 565 ik->do_local_static_fields(&finder); 566 assert(finder.found(), "field must exist"); 567 568 info->klass = ik; 569 info->offset = finder.offset(); 570 info->type = finder.type(); 571 } 572 } 573 574 void HeapShared::archive_module_graph_objects(Thread* THREAD) { 575 for (int i = 0; i < num_archivable_static_fields; i++) { 576 ArchivableStaticFieldInfo* info = &archivable_static_fields[i]; 577 archive_reachable_objects_from_static_field(info->klass, info->offset, info->type, CHECK); 578 } 579 } 580 #endif // INCLUDE_CDS_JAVA_HEAP