1 /* 2 * Copyright (c) 2014, 2020, 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/classFileStream.hpp" 27 #include "classfile/classListParser.hpp" 28 #include "classfile/classLoader.hpp" 29 #include "classfile/classLoaderData.inline.hpp" 30 #include "classfile/classLoaderDataGraph.hpp" 31 #include "classfile/classLoaderExt.hpp" 32 #include "classfile/dictionary.hpp" 33 #include "classfile/javaClasses.hpp" 34 #include "classfile/symbolTable.hpp" 35 #include "classfile/systemDictionary.hpp" 36 #include "classfile/systemDictionaryShared.hpp" 37 #include "classfile/verificationType.hpp" 38 #include "classfile/vmSymbols.hpp" 39 #include "gc/shared/oopStorageSet.hpp" 40 #include "jfr/jfrEvents.hpp" 41 #include "logging/log.hpp" 42 #include "memory/allocation.hpp" 43 #include "memory/archiveUtils.hpp" 44 #include "memory/dynamicArchive.hpp" 45 #include "memory/filemap.hpp" 46 #include "memory/heapShared.hpp" 47 #include "memory/metadataFactory.hpp" 48 #include "memory/metaspaceClosure.hpp" 49 #include "memory/oopFactory.hpp" 50 #include "memory/resourceArea.hpp" 51 #include "memory/universe.hpp" 52 #include "oops/instanceKlass.hpp" 53 #include "oops/klass.inline.hpp" 54 #include "oops/objArrayOop.inline.hpp" 55 #include "oops/oop.inline.hpp" 56 #include "oops/typeArrayOop.inline.hpp" 57 #include "runtime/handles.inline.hpp" 58 #include "runtime/java.hpp" 59 #include "runtime/javaCalls.hpp" 60 #include "runtime/mutexLocker.hpp" 61 #include "utilities/hashtable.inline.hpp" 62 #include "utilities/resourceHash.hpp" 63 #include "utilities/stringUtils.hpp" 64 65 66 OopHandle SystemDictionaryShared::_shared_protection_domains; 67 OopHandle SystemDictionaryShared::_shared_jar_urls; 68 OopHandle SystemDictionaryShared::_shared_jar_manifests; 69 DEBUG_ONLY(bool SystemDictionaryShared::_no_class_loading_should_happen = false;) 70 71 class DumpTimeSharedClassInfo: public CHeapObj<mtClass> { 72 bool _excluded; 73 public: 74 struct DTLoaderConstraint { 75 Symbol* _name; 76 char _loader_type1; 77 char _loader_type2; 78 DTLoaderConstraint(Symbol* name, char l1, char l2) : _name(name), _loader_type1(l1), _loader_type2(l2) { 79 _name->increment_refcount(); 80 } 81 DTLoaderConstraint() : _name(NULL), _loader_type1('0'), _loader_type2('0') {} 82 bool equals(const DTLoaderConstraint& t) { 83 return t._name == _name && 84 ((t._loader_type1 == _loader_type1 && t._loader_type2 == _loader_type2) || 85 (t._loader_type2 == _loader_type1 && t._loader_type1 == _loader_type2)); 86 } 87 }; 88 89 struct DTVerifierConstraint { 90 Symbol* _name; 91 Symbol* _from_name; 92 DTVerifierConstraint() : _name(NULL), _from_name(NULL) {} 93 DTVerifierConstraint(Symbol* n, Symbol* fn) : _name(n), _from_name(fn) { 94 _name->increment_refcount(); 95 _from_name->increment_refcount(); 96 } 97 }; 98 99 InstanceKlass* _klass; 100 bool _failed_verification; 101 bool _is_archived_lambda_proxy; 102 int _id; 103 int _clsfile_size; 104 int _clsfile_crc32; 105 GrowableArray<DTVerifierConstraint>* _verifier_constraints; 106 GrowableArray<char>* _verifier_constraint_flags; 107 GrowableArray<DTLoaderConstraint>* _loader_constraints; 108 109 DumpTimeSharedClassInfo() { 110 _klass = NULL; 111 _failed_verification = false; 112 _is_archived_lambda_proxy = false; 113 _id = -1; 114 _clsfile_size = -1; 115 _clsfile_crc32 = -1; 116 _excluded = false; 117 _verifier_constraints = NULL; 118 _verifier_constraint_flags = NULL; 119 _loader_constraints = NULL; 120 } 121 122 void add_verification_constraint(InstanceKlass* k, Symbol* name, 123 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object); 124 void record_linking_constraint(Symbol* name, Handle loader1, Handle loader2); 125 126 bool is_builtin() { 127 return SystemDictionaryShared::is_builtin(_klass); 128 } 129 130 int num_verifier_constraints() { 131 if (_verifier_constraint_flags != NULL) { 132 return _verifier_constraint_flags->length(); 133 } else { 134 return 0; 135 } 136 } 137 138 int num_loader_constraints() { 139 if (_loader_constraints != NULL) { 140 return _loader_constraints->length(); 141 } else { 142 return 0; 143 } 144 } 145 146 void metaspace_pointers_do(MetaspaceClosure* it) { 147 it->push(&_klass); 148 if (_verifier_constraints != NULL) { 149 for (int i = 0; i < _verifier_constraints->length(); i++) { 150 DTVerifierConstraint* cons = _verifier_constraints->adr_at(i); 151 it->push(&cons->_name); 152 it->push(&cons->_from_name); 153 } 154 } 155 if (_loader_constraints != NULL) { 156 for (int i = 0; i < _loader_constraints->length(); i++) { 157 DTLoaderConstraint* lc = _loader_constraints->adr_at(i); 158 it->push(&lc->_name); 159 } 160 } 161 } 162 163 void set_excluded() { 164 _excluded = true; 165 } 166 167 bool is_excluded() { 168 // _klass may become NULL due to DynamicArchiveBuilder::set_to_null 169 return _excluded || _failed_verification || _klass == NULL; 170 } 171 172 void set_failed_verification() { 173 _failed_verification = true; 174 } 175 176 bool failed_verification() { 177 return _failed_verification; 178 } 179 }; 180 181 inline unsigned DumpTimeSharedClassTable_hash(InstanceKlass* const& k) { 182 if (DumpSharedSpaces) { 183 // Deterministic archive contents 184 uintx delta = k->name() - MetaspaceShared::symbol_rs_base(); 185 return primitive_hash<uintx>(delta); 186 } else { 187 // Deterministic archive is not possible because classes can be loaded 188 // in multiple threads. 189 return primitive_hash<InstanceKlass*>(k); 190 } 191 } 192 193 class DumpTimeSharedClassTable: public ResourceHashtable< 194 InstanceKlass*, 195 DumpTimeSharedClassInfo, 196 &DumpTimeSharedClassTable_hash, 197 primitive_equals<InstanceKlass*>, 198 15889, // prime number 199 ResourceObj::C_HEAP> 200 { 201 int _builtin_count; 202 int _unregistered_count; 203 public: 204 DumpTimeSharedClassInfo* find_or_allocate_info_for(InstanceKlass* k) { 205 bool created = false; 206 DumpTimeSharedClassInfo* p = put_if_absent(k, &created); 207 if (created) { 208 assert(!SystemDictionaryShared::no_class_loading_should_happen(), 209 "no new classes can be loaded while dumping archive"); 210 p->_klass = k; 211 } else { 212 assert(p->_klass == k, "Sanity"); 213 } 214 return p; 215 } 216 217 class CountClassByCategory : StackObj { 218 DumpTimeSharedClassTable* _table; 219 public: 220 CountClassByCategory(DumpTimeSharedClassTable* table) : _table(table) {} 221 bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) { 222 if (!info.is_excluded()) { 223 if (info.is_builtin()) { 224 ++ _table->_builtin_count; 225 } else { 226 ++ _table->_unregistered_count; 227 } 228 } 229 return true; // keep on iterating 230 } 231 }; 232 233 void update_counts() { 234 _builtin_count = 0; 235 _unregistered_count = 0; 236 CountClassByCategory counter(this); 237 iterate(&counter); 238 } 239 240 int count_of(bool is_builtin) const { 241 if (is_builtin) { 242 return _builtin_count; 243 } else { 244 return _unregistered_count; 245 } 246 } 247 }; 248 249 class LambdaProxyClassKey { 250 template <typename T> static void original_to_target(T& field) { 251 if (field != NULL) { 252 field = DynamicArchive::original_to_target(field); 253 ArchivePtrMarker::mark_pointer(&field); 254 } 255 } 256 257 InstanceKlass* _caller_ik; 258 Symbol* _invoked_name; 259 Symbol* _invoked_type; 260 Symbol* _method_type; 261 Method* _member_method; 262 Symbol* _instantiated_method_type; 263 264 public: 265 LambdaProxyClassKey(InstanceKlass* caller_ik, 266 Symbol* invoked_name, 267 Symbol* invoked_type, 268 Symbol* method_type, 269 Method* member_method, 270 Symbol* instantiated_method_type) : 271 _caller_ik(caller_ik), 272 _invoked_name(invoked_name), 273 _invoked_type(invoked_type), 274 _method_type(method_type), 275 _member_method(member_method), 276 _instantiated_method_type(instantiated_method_type) {} 277 278 void original_to_target() { 279 original_to_target(_caller_ik); 280 original_to_target(_instantiated_method_type); 281 original_to_target(_invoked_name); 282 original_to_target(_invoked_type); 283 original_to_target(_member_method); 284 original_to_target(_method_type); 285 } 286 287 bool equals(LambdaProxyClassKey const& other) const { 288 return _caller_ik == other._caller_ik && 289 _invoked_name == other._invoked_name && 290 _invoked_type == other._invoked_type && 291 _method_type == other._method_type && 292 _member_method == other._member_method && 293 _instantiated_method_type == other._instantiated_method_type; 294 } 295 296 unsigned int hash() const { 297 return SystemDictionaryShared::hash_for_shared_dictionary(_caller_ik) + 298 SystemDictionaryShared::hash_for_shared_dictionary(_invoked_name) + 299 SystemDictionaryShared::hash_for_shared_dictionary(_invoked_type) + 300 SystemDictionaryShared::hash_for_shared_dictionary(_method_type) + 301 SystemDictionaryShared::hash_for_shared_dictionary(_instantiated_method_type); 302 } 303 304 unsigned int dumptime_hash() const { 305 return primitive_hash<InstanceKlass*>(_caller_ik) + 306 primitive_hash<Symbol*>(_invoked_name) + 307 primitive_hash<Symbol*>(_invoked_type) + 308 primitive_hash<Symbol*>(_method_type) + 309 primitive_hash<Symbol*>(_instantiated_method_type); 310 } 311 312 static inline unsigned int DUMPTIME_HASH(LambdaProxyClassKey const& key) { 313 return (key.dumptime_hash()); 314 } 315 316 static inline bool DUMPTIME_EQUALS( 317 LambdaProxyClassKey const& k1, LambdaProxyClassKey const& k2) { 318 return (k1.equals(k2)); 319 } 320 }; 321 322 323 class DumpTimeLambdaProxyClassInfo { 324 public: 325 GrowableArray<InstanceKlass*>* _proxy_klasses; 326 DumpTimeLambdaProxyClassInfo() : _proxy_klasses(NULL) {} 327 void add_proxy_klass(InstanceKlass* proxy_klass) { 328 if (_proxy_klasses == NULL) { 329 _proxy_klasses = new (ResourceObj::C_HEAP, mtClassShared)GrowableArray<InstanceKlass*>(5, mtClassShared); 330 } 331 assert(_proxy_klasses != NULL, "sanity"); 332 _proxy_klasses->append(proxy_klass); 333 } 334 }; 335 336 class RunTimeLambdaProxyClassInfo { 337 LambdaProxyClassKey _key; 338 InstanceKlass* _proxy_klass_head; 339 public: 340 RunTimeLambdaProxyClassInfo(LambdaProxyClassKey key, InstanceKlass* proxy_klass) : 341 _key(key), _proxy_klass_head(proxy_klass) {} 342 343 InstanceKlass* proxy_klass_head() const { return _proxy_klass_head; } 344 345 // Used by LambdaProxyClassDictionary to implement OffsetCompactHashtable::EQUALS 346 static inline bool EQUALS( 347 const RunTimeLambdaProxyClassInfo* value, LambdaProxyClassKey* key, int len_unused) { 348 return (value->_key.equals(*key)); 349 } 350 void init(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) { 351 _key = key; 352 _key.original_to_target(); 353 _proxy_klass_head = DynamicArchive::original_to_target(info._proxy_klasses->at(0)); 354 ArchivePtrMarker::mark_pointer(&_proxy_klass_head); 355 } 356 357 unsigned int hash() const { 358 return _key.hash(); 359 } 360 }; 361 362 class LambdaProxyClassDictionary : public OffsetCompactHashtable< 363 LambdaProxyClassKey*, 364 const RunTimeLambdaProxyClassInfo*, 365 RunTimeLambdaProxyClassInfo::EQUALS> {}; 366 367 LambdaProxyClassDictionary _lambda_proxy_class_dictionary; 368 369 class DumpTimeLambdaProxyClassDictionary 370 : public ResourceHashtable<LambdaProxyClassKey, 371 DumpTimeLambdaProxyClassInfo, 372 LambdaProxyClassKey::DUMPTIME_HASH, 373 LambdaProxyClassKey::DUMPTIME_EQUALS, 374 137, // prime number 375 ResourceObj::C_HEAP> { 376 public: 377 int _count; 378 }; 379 380 DumpTimeLambdaProxyClassDictionary* _dumptime_lambda_proxy_class_dictionary = NULL; 381 382 static void add_to_dump_time_lambda_proxy_class_dictionary(LambdaProxyClassKey key, 383 InstanceKlass* proxy_klass) { 384 assert(DumpTimeTable_lock->owned_by_self(), "sanity"); 385 if (_dumptime_lambda_proxy_class_dictionary == NULL) { 386 _dumptime_lambda_proxy_class_dictionary = 387 new (ResourceObj::C_HEAP, mtClass)DumpTimeLambdaProxyClassDictionary(); 388 } 389 DumpTimeLambdaProxyClassInfo* lambda_info = _dumptime_lambda_proxy_class_dictionary->get(key); 390 if (lambda_info == NULL) { 391 DumpTimeLambdaProxyClassInfo info; 392 info.add_proxy_klass(proxy_klass); 393 _dumptime_lambda_proxy_class_dictionary->put(key, info); 394 //lambda_info = _dumptime_lambda_proxy_class_dictionary->get(key); 395 //assert(lambda_info->_proxy_klass == proxy_klass, "must be"); // debug only -- remove 396 ++_dumptime_lambda_proxy_class_dictionary->_count; 397 } else { 398 lambda_info->add_proxy_klass(proxy_klass); 399 } 400 } 401 402 class RunTimeSharedClassInfo { 403 public: 404 struct CrcInfo { 405 int _clsfile_size; 406 int _clsfile_crc32; 407 }; 408 409 // This is different than DumpTimeSharedClassInfo::DTVerifierConstraint. We use 410 // u4 instead of Symbol* to save space on 64-bit CPU. 411 struct RTVerifierConstraint { 412 u4 _name; 413 u4 _from_name; 414 Symbol* name() { return (Symbol*)(SharedBaseAddress + _name);} 415 Symbol* from_name() { return (Symbol*)(SharedBaseAddress + _from_name); } 416 }; 417 418 struct RTLoaderConstraint { 419 u4 _name; 420 char _loader_type1; 421 char _loader_type2; 422 Symbol* constraint_name() { 423 return (Symbol*)(SharedBaseAddress + _name); 424 } 425 }; 426 427 InstanceKlass* _klass; 428 int _num_verifier_constraints; 429 int _num_loader_constraints; 430 431 // optional CrcInfo _crc; (only for UNREGISTERED classes) 432 // optional InstanceKlass* _nest_host 433 // optional RTLoaderConstraint _loader_constraint_types[_num_loader_constraints] 434 // optional RTVerifierConstraint _verifier_constraints[_num_verifier_constraints] 435 // optional char _verifier_constraint_flags[_num_verifier_constraints] 436 437 private: 438 static size_t header_size_size() { 439 return sizeof(RunTimeSharedClassInfo); 440 } 441 static size_t crc_size(InstanceKlass* klass) { 442 if (!SystemDictionaryShared::is_builtin(klass)) { 443 return sizeof(CrcInfo); 444 } else { 445 return 0; 446 } 447 } 448 static size_t verifier_constraints_size(int num_verifier_constraints) { 449 return sizeof(RTVerifierConstraint) * num_verifier_constraints; 450 } 451 static size_t verifier_constraint_flags_size(int num_verifier_constraints) { 452 return sizeof(char) * num_verifier_constraints; 453 } 454 static size_t loader_constraints_size(int num_loader_constraints) { 455 return sizeof(RTLoaderConstraint) * num_loader_constraints; 456 } 457 static size_t nest_host_size(InstanceKlass* klass) { 458 if (klass->is_hidden()) { 459 return sizeof(InstanceKlass*); 460 } else { 461 return 0; 462 } 463 } 464 465 public: 466 static size_t byte_size(InstanceKlass* klass, int num_verifier_constraints, int num_loader_constraints) { 467 return header_size_size() + 468 crc_size(klass) + 469 nest_host_size(klass) + 470 loader_constraints_size(num_loader_constraints) + 471 verifier_constraints_size(num_verifier_constraints) + 472 verifier_constraint_flags_size(num_verifier_constraints); 473 } 474 475 private: 476 size_t crc_offset() const { 477 return header_size_size(); 478 } 479 480 size_t nest_host_offset() const { 481 return crc_offset() + crc_size(_klass); 482 } 483 484 size_t loader_constraints_offset() const { 485 return nest_host_offset() + nest_host_size(_klass); 486 } 487 size_t verifier_constraints_offset() const { 488 return loader_constraints_offset() + loader_constraints_size(_num_loader_constraints); 489 } 490 size_t verifier_constraint_flags_offset() const { 491 return verifier_constraints_offset() + verifier_constraints_size(_num_verifier_constraints); 492 } 493 494 void check_verifier_constraint_offset(int i) const { 495 assert(0 <= i && i < _num_verifier_constraints, "sanity"); 496 } 497 498 void check_loader_constraint_offset(int i) const { 499 assert(0 <= i && i < _num_loader_constraints, "sanity"); 500 } 501 502 public: 503 CrcInfo* crc() const { 504 assert(crc_size(_klass) > 0, "must be"); 505 return (CrcInfo*)(address(this) + crc_offset()); 506 } 507 RTVerifierConstraint* verifier_constraints() { 508 assert(_num_verifier_constraints > 0, "sanity"); 509 return (RTVerifierConstraint*)(address(this) + verifier_constraints_offset()); 510 } 511 RTVerifierConstraint* verifier_constraint_at(int i) { 512 check_verifier_constraint_offset(i); 513 return verifier_constraints() + i; 514 } 515 516 char* verifier_constraint_flags() { 517 assert(_num_verifier_constraints > 0, "sanity"); 518 return (char*)(address(this) + verifier_constraint_flags_offset()); 519 } 520 521 InstanceKlass** nest_host_addr() { 522 assert(_klass->is_hidden(), "sanity"); 523 return (InstanceKlass**)(address(this) + nest_host_offset()); 524 } 525 InstanceKlass* nest_host() { 526 return *nest_host_addr(); 527 } 528 void set_nest_host(InstanceKlass* k) { 529 *nest_host_addr() = k; 530 ArchivePtrMarker::mark_pointer((address*)nest_host_addr()); 531 } 532 533 RTLoaderConstraint* loader_constraints() { 534 assert(_num_loader_constraints > 0, "sanity"); 535 return (RTLoaderConstraint*)(address(this) + loader_constraints_offset()); 536 } 537 538 RTLoaderConstraint* loader_constraint_at(int i) { 539 check_loader_constraint_offset(i); 540 return loader_constraints() + i; 541 } 542 543 static u4 object_delta_u4(Symbol* sym) { 544 if (DynamicDumpSharedSpaces) { 545 sym = DynamicArchive::original_to_target(sym); 546 } 547 return MetaspaceShared::object_delta_u4(sym); 548 } 549 550 void init(DumpTimeSharedClassInfo& info) { 551 _klass = info._klass; 552 if (!SystemDictionaryShared::is_builtin(_klass)) { 553 CrcInfo* c = crc(); 554 c->_clsfile_size = info._clsfile_size; 555 c->_clsfile_crc32 = info._clsfile_crc32; 556 } 557 _num_verifier_constraints = info.num_verifier_constraints(); 558 _num_loader_constraints = info.num_loader_constraints(); 559 int i; 560 if (_num_verifier_constraints > 0) { 561 RTVerifierConstraint* vf_constraints = verifier_constraints(); 562 char* flags = verifier_constraint_flags(); 563 for (i = 0; i < _num_verifier_constraints; i++) { 564 vf_constraints[i]._name = object_delta_u4(info._verifier_constraints->at(i)._name); 565 vf_constraints[i]._from_name = object_delta_u4(info._verifier_constraints->at(i)._from_name); 566 } 567 for (i = 0; i < _num_verifier_constraints; i++) { 568 flags[i] = info._verifier_constraint_flags->at(i); 569 } 570 } 571 572 if (_num_loader_constraints > 0) { 573 RTLoaderConstraint* ld_constraints = loader_constraints(); 574 for (i = 0; i < _num_loader_constraints; i++) { 575 ld_constraints[i]._name = object_delta_u4(info._loader_constraints->at(i)._name); 576 ld_constraints[i]._loader_type1 = info._loader_constraints->at(i)._loader_type1; 577 ld_constraints[i]._loader_type2 = info._loader_constraints->at(i)._loader_type2; 578 } 579 } 580 if (DynamicDumpSharedSpaces) { 581 if (_klass->is_hidden()) { 582 Thread* THREAD = Thread::current(); 583 InstanceKlass* n_h = _klass->nest_host(THREAD); 584 n_h = DynamicArchive::original_to_target(n_h); 585 set_nest_host(n_h); 586 } 587 _klass = DynamicArchive::original_to_target(info._klass); 588 } 589 ArchivePtrMarker::mark_pointer(&_klass); 590 } 591 592 bool matches(int clsfile_size, int clsfile_crc32) const { 593 return crc()->_clsfile_size == clsfile_size && 594 crc()->_clsfile_crc32 == clsfile_crc32; 595 } 596 597 char verifier_constraint_flag(int i) { 598 check_verifier_constraint_offset(i); 599 return verifier_constraint_flags()[i]; 600 } 601 602 private: 603 // ArchiveCompactor::allocate() has reserved a pointer immediately before 604 // archived InstanceKlasses. We can use this slot to do a quick 605 // lookup of InstanceKlass* -> RunTimeSharedClassInfo* without 606 // building a new hashtable. 607 // 608 // info_pointer_addr(klass) --> 0x0100 RunTimeSharedClassInfo* 609 // InstanceKlass* klass --> 0x0108 <C++ vtbl> 610 // 0x0110 fields from Klass ... 611 static RunTimeSharedClassInfo** info_pointer_addr(InstanceKlass* klass) { 612 return &((RunTimeSharedClassInfo**)klass)[-1]; 613 } 614 615 public: 616 static RunTimeSharedClassInfo* get_for(InstanceKlass* klass) { 617 assert(klass->is_shared(), "don't call for non-shared class"); 618 return *info_pointer_addr(klass); 619 } 620 static void set_for(InstanceKlass* klass, RunTimeSharedClassInfo* record) { 621 if (DynamicDumpSharedSpaces) { 622 klass = DynamicArchive::original_to_buffer(klass); 623 *info_pointer_addr(klass) = DynamicArchive::buffer_to_target(record); 624 } else { 625 *info_pointer_addr(klass) = record; 626 } 627 628 ArchivePtrMarker::mark_pointer(info_pointer_addr(klass)); 629 } 630 631 // Used by RunTimeSharedDictionary to implement OffsetCompactHashtable::EQUALS 632 static inline bool EQUALS( 633 const RunTimeSharedClassInfo* value, Symbol* key, int len_unused) { 634 return (value->_klass->name() == key); 635 } 636 }; 637 638 class RunTimeSharedDictionary : public OffsetCompactHashtable< 639 Symbol*, 640 const RunTimeSharedClassInfo*, 641 RunTimeSharedClassInfo::EQUALS> {}; 642 643 static DumpTimeSharedClassTable* _dumptime_table = NULL; 644 // SystemDictionaries in the base layer static archive 645 static RunTimeSharedDictionary _builtin_dictionary; 646 static RunTimeSharedDictionary _unregistered_dictionary; 647 // SystemDictionaries in the top layer dynamic archive 648 static RunTimeSharedDictionary _dynamic_builtin_dictionary; 649 static RunTimeSharedDictionary _dynamic_unregistered_dictionary; 650 651 oop SystemDictionaryShared::shared_protection_domain(int index) { 652 return ((objArrayOop)_shared_protection_domains.resolve())->obj_at(index); 653 } 654 655 oop SystemDictionaryShared::shared_jar_url(int index) { 656 return ((objArrayOop)_shared_jar_urls.resolve())->obj_at(index); 657 } 658 659 oop SystemDictionaryShared::shared_jar_manifest(int index) { 660 return ((objArrayOop)_shared_jar_manifests.resolve())->obj_at(index); 661 } 662 663 Handle SystemDictionaryShared::get_shared_jar_manifest(int shared_path_index, TRAPS) { 664 Handle manifest ; 665 if (shared_jar_manifest(shared_path_index) == NULL) { 666 SharedClassPathEntry* ent = FileMapInfo::shared_path(shared_path_index); 667 long size = ent->manifest_size(); 668 if (size <= 0) { 669 return Handle(); 670 } 671 672 // ByteArrayInputStream bais = new ByteArrayInputStream(buf); 673 const char* src = ent->manifest(); 674 assert(src != NULL, "No Manifest data"); 675 typeArrayOop buf = oopFactory::new_byteArray(size, CHECK_NH); 676 typeArrayHandle bufhandle(THREAD, buf); 677 ArrayAccess<>::arraycopy_from_native(reinterpret_cast<const jbyte*>(src), 678 buf, typeArrayOopDesc::element_offset<jbyte>(0), size); 679 680 Handle bais = JavaCalls::construct_new_instance(SystemDictionary::ByteArrayInputStream_klass(), 681 vmSymbols::byte_array_void_signature(), 682 bufhandle, CHECK_NH); 683 684 // manifest = new Manifest(bais) 685 manifest = JavaCalls::construct_new_instance(SystemDictionary::Jar_Manifest_klass(), 686 vmSymbols::input_stream_void_signature(), 687 bais, CHECK_NH); 688 atomic_set_shared_jar_manifest(shared_path_index, manifest()); 689 } 690 691 manifest = Handle(THREAD, shared_jar_manifest(shared_path_index)); 692 assert(manifest.not_null(), "sanity"); 693 return manifest; 694 } 695 696 Handle SystemDictionaryShared::get_shared_jar_url(int shared_path_index, TRAPS) { 697 Handle url_h; 698 if (shared_jar_url(shared_path_index) == NULL) { 699 JavaValue result(T_OBJECT); 700 const char* path = FileMapInfo::shared_path_name(shared_path_index); 701 Handle path_string = java_lang_String::create_from_str(path, CHECK_(url_h)); 702 Klass* classLoaders_klass = 703 SystemDictionary::jdk_internal_loader_ClassLoaders_klass(); 704 JavaCalls::call_static(&result, classLoaders_klass, 705 vmSymbols::toFileURL_name(), 706 vmSymbols::toFileURL_signature(), 707 path_string, CHECK_(url_h)); 708 709 atomic_set_shared_jar_url(shared_path_index, (oop)result.get_jobject()); 710 } 711 712 url_h = Handle(THREAD, shared_jar_url(shared_path_index)); 713 assert(url_h.not_null(), "sanity"); 714 return url_h; 715 } 716 717 Handle SystemDictionaryShared::get_package_name(Symbol* class_name, TRAPS) { 718 ResourceMark rm(THREAD); 719 Handle pkgname_string; 720 Symbol* pkg = ClassLoader::package_from_class_name(class_name); 721 if (pkg != NULL) { // Package prefix found 722 const char* pkgname = pkg->as_klass_external_name(); 723 pkgname_string = java_lang_String::create_from_str(pkgname, 724 CHECK_(pkgname_string)); 725 } 726 return pkgname_string; 727 } 728 729 // Define Package for shared app classes from JAR file and also checks for 730 // package sealing (all done in Java code) 731 // See http://docs.oracle.com/javase/tutorial/deployment/jar/sealman.html 732 void SystemDictionaryShared::define_shared_package(Symbol* class_name, 733 Handle class_loader, 734 Handle manifest, 735 Handle url, 736 TRAPS) { 737 assert(SystemDictionary::is_system_class_loader(class_loader()), "unexpected class loader"); 738 // get_package_name() returns a NULL handle if the class is in unnamed package 739 Handle pkgname_string = get_package_name(class_name, CHECK); 740 if (pkgname_string.not_null()) { 741 Klass* app_classLoader_klass = SystemDictionary::jdk_internal_loader_ClassLoaders_AppClassLoader_klass(); 742 JavaValue result(T_OBJECT); 743 JavaCallArguments args(3); 744 args.set_receiver(class_loader); 745 args.push_oop(pkgname_string); 746 args.push_oop(manifest); 747 args.push_oop(url); 748 JavaCalls::call_virtual(&result, app_classLoader_klass, 749 vmSymbols::defineOrCheckPackage_name(), 750 vmSymbols::defineOrCheckPackage_signature(), 751 &args, 752 CHECK); 753 } 754 } 755 756 // Get the ProtectionDomain associated with the CodeSource from the classloader. 757 Handle SystemDictionaryShared::get_protection_domain_from_classloader(Handle class_loader, 758 Handle url, TRAPS) { 759 // CodeSource cs = new CodeSource(url, null); 760 Handle cs = JavaCalls::construct_new_instance(SystemDictionary::CodeSource_klass(), 761 vmSymbols::url_code_signer_array_void_signature(), 762 url, Handle(), CHECK_NH); 763 764 // protection_domain = SecureClassLoader.getProtectionDomain(cs); 765 Klass* secureClassLoader_klass = SystemDictionary::SecureClassLoader_klass(); 766 JavaValue obj_result(T_OBJECT); 767 JavaCalls::call_virtual(&obj_result, class_loader, secureClassLoader_klass, 768 vmSymbols::getProtectionDomain_name(), 769 vmSymbols::getProtectionDomain_signature(), 770 cs, CHECK_NH); 771 return Handle(THREAD, (oop)obj_result.get_jobject()); 772 } 773 774 // Returns the ProtectionDomain associated with the JAR file identified by the url. 775 Handle SystemDictionaryShared::get_shared_protection_domain(Handle class_loader, 776 int shared_path_index, 777 Handle url, 778 TRAPS) { 779 Handle protection_domain; 780 if (shared_protection_domain(shared_path_index) == NULL) { 781 Handle pd = get_protection_domain_from_classloader(class_loader, url, THREAD); 782 atomic_set_shared_protection_domain(shared_path_index, pd()); 783 } 784 785 // Acquire from the cache because if another thread beats the current one to 786 // set the shared protection_domain and the atomic_set fails, the current thread 787 // needs to get the updated protection_domain from the cache. 788 protection_domain = Handle(THREAD, shared_protection_domain(shared_path_index)); 789 assert(protection_domain.not_null(), "sanity"); 790 return protection_domain; 791 } 792 793 // Returns the ProtectionDomain associated with the moduleEntry. 794 Handle SystemDictionaryShared::get_shared_protection_domain(Handle class_loader, 795 ModuleEntry* mod, TRAPS) { 796 ClassLoaderData *loader_data = mod->loader_data(); 797 if (mod->shared_protection_domain() == NULL) { 798 Symbol* location = mod->location(); 799 if (location != NULL) { 800 Handle location_string = java_lang_String::create_from_symbol( 801 location, CHECK_NH); 802 Handle url; 803 JavaValue result(T_OBJECT); 804 if (location->starts_with("jrt:/")) { 805 url = JavaCalls::construct_new_instance(SystemDictionary::URL_klass(), 806 vmSymbols::string_void_signature(), 807 location_string, CHECK_NH); 808 } else { 809 Klass* classLoaders_klass = 810 SystemDictionary::jdk_internal_loader_ClassLoaders_klass(); 811 JavaCalls::call_static(&result, classLoaders_klass, vmSymbols::toFileURL_name(), 812 vmSymbols::toFileURL_signature(), 813 location_string, CHECK_NH); 814 url = Handle(THREAD, (oop)result.get_jobject()); 815 } 816 817 Handle pd = get_protection_domain_from_classloader(class_loader, url, 818 CHECK_NH); 819 mod->set_shared_protection_domain(loader_data, pd); 820 } 821 } 822 823 Handle protection_domain(THREAD, mod->shared_protection_domain()); 824 assert(protection_domain.not_null(), "sanity"); 825 return protection_domain; 826 } 827 828 // Initializes the java.lang.Package and java.security.ProtectionDomain objects associated with 829 // the given InstanceKlass. 830 // Returns the ProtectionDomain for the InstanceKlass. 831 Handle SystemDictionaryShared::init_security_info(Handle class_loader, InstanceKlass* ik, PackageEntry* pkg_entry, TRAPS) { 832 Handle pd; 833 834 if (ik != NULL) { 835 int index = ik->shared_classpath_index(); 836 assert(index >= 0, "Sanity"); 837 SharedClassPathEntry* ent = FileMapInfo::shared_path(index); 838 Symbol* class_name = ik->name(); 839 840 if (ent->is_modules_image()) { 841 // For shared app/platform classes originated from the run-time image: 842 // The ProtectionDomains are cached in the corresponding ModuleEntries 843 // for fast access by the VM. 844 // all packages from module image are already created during VM bootstrap in 845 // Modules::define_module(). 846 assert(pkg_entry != NULL, "archived class in module image cannot be from unnamed package"); 847 ModuleEntry* mod_entry = pkg_entry->module(); 848 pd = get_shared_protection_domain(class_loader, mod_entry, THREAD); 849 } else { 850 // For shared app/platform classes originated from JAR files on the class path: 851 // Each of the 3 SystemDictionaryShared::_shared_xxx arrays has the same length 852 // as the shared classpath table in the shared archive (see 853 // FileMap::_shared_path_table in filemap.hpp for details). 854 // 855 // If a shared InstanceKlass k is loaded from the class path, let 856 // 857 // index = k->shared_classpath_index(): 858 // 859 // FileMap::_shared_path_table[index] identifies the JAR file that contains k. 860 // 861 // k's protection domain is: 862 // 863 // ProtectionDomain pd = _shared_protection_domains[index]; 864 // 865 // and k's Package is initialized using 866 // 867 // manifest = _shared_jar_manifests[index]; 868 // url = _shared_jar_urls[index]; 869 // define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd)); 870 // 871 // Note that if an element of these 3 _shared_xxx arrays is NULL, it will be initialized by 872 // the corresponding SystemDictionaryShared::get_shared_xxx() function. 873 Handle manifest = get_shared_jar_manifest(index, CHECK_(pd)); 874 Handle url = get_shared_jar_url(index, CHECK_(pd)); 875 int index_offset = index - ClassLoaderExt::app_class_paths_start_index(); 876 if (index_offset < PackageEntry::max_index_for_defined_in_class_path()) { 877 if (pkg_entry == NULL || !pkg_entry->is_defined_by_cds_in_class_path(index_offset)) { 878 // define_shared_package only needs to be called once for each package in a jar specified 879 // in the shared class path. 880 define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd)); 881 if (pkg_entry != NULL) { 882 pkg_entry->set_defined_by_cds_in_class_path(index_offset); 883 } 884 } 885 } else { 886 define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd)); 887 } 888 pd = get_shared_protection_domain(class_loader, index, url, CHECK_(pd)); 889 } 890 } 891 return pd; 892 } 893 894 bool SystemDictionaryShared::is_sharing_possible(ClassLoaderData* loader_data) { 895 oop class_loader = loader_data->class_loader(); 896 return (class_loader == NULL || 897 SystemDictionary::is_system_class_loader(class_loader) || 898 SystemDictionary::is_platform_class_loader(class_loader)); 899 } 900 901 bool SystemDictionaryShared::has_platform_or_app_classes() { 902 if (FileMapInfo::current_info()->has_platform_or_app_classes()) { 903 return true; 904 } 905 if (DynamicArchive::is_mapped() && 906 FileMapInfo::dynamic_info()->has_platform_or_app_classes()) { 907 return true; 908 } 909 return false; 910 } 911 912 // The following stack shows how this code is reached: 913 // 914 // [0] SystemDictionaryShared::find_or_load_shared_class() 915 // [1] JVM_FindLoadedClass 916 // [2] java.lang.ClassLoader.findLoadedClass0() 917 // [3] java.lang.ClassLoader.findLoadedClass() 918 // [4] jdk.internal.loader.BuiltinClassLoader.loadClassOrNull() 919 // [5] jdk.internal.loader.BuiltinClassLoader.loadClass() 920 // [6] jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(), or 921 // jdk.internal.loader.ClassLoaders$PlatformClassLoader.loadClass() 922 // 923 // AppCDS supports fast class loading for these 2 built-in class loaders: 924 // jdk.internal.loader.ClassLoaders$PlatformClassLoader 925 // jdk.internal.loader.ClassLoaders$AppClassLoader 926 // with the following assumptions (based on the JDK core library source code): 927 // 928 // [a] these two loaders use the BuiltinClassLoader.loadClassOrNull() to 929 // load the named class. 930 // [b] BuiltinClassLoader.loadClassOrNull() first calls findLoadedClass(name). 931 // [c] At this point, if we can find the named class inside the 932 // shared_dictionary, we can perform further checks (see 933 // SystemDictionary::is_shared_class_visible) to ensure that this class 934 // was loaded by the same class loader during dump time. 935 // 936 // Given these assumptions, we intercept the findLoadedClass() call to invoke 937 // SystemDictionaryShared::find_or_load_shared_class() to load the shared class from 938 // the archive for the 2 built-in class loaders. This way, 939 // we can improve start-up because we avoid decoding the classfile, 940 // and avoid delegating to the parent loader. 941 // 942 // NOTE: there's a lot of assumption about the Java code. If any of that change, this 943 // needs to be redesigned. 944 945 InstanceKlass* SystemDictionaryShared::find_or_load_shared_class( 946 Symbol* name, Handle class_loader, TRAPS) { 947 InstanceKlass* k = NULL; 948 if (UseSharedSpaces) { 949 if (!has_platform_or_app_classes()) { 950 return NULL; 951 } 952 953 if (SystemDictionary::is_system_class_loader(class_loader()) || 954 SystemDictionary::is_platform_class_loader(class_loader())) { 955 // Fix for 4474172; see evaluation for more details 956 class_loader = Handle( 957 THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader())); 958 ClassLoaderData *loader_data = register_loader(class_loader); 959 Dictionary* dictionary = loader_data->dictionary(); 960 961 unsigned int d_hash = dictionary->compute_hash(name); 962 963 bool DoObjectLock = true; 964 if (is_parallelCapable(class_loader)) { 965 DoObjectLock = false; 966 } 967 968 // Make sure we are synchronized on the class loader before we proceed 969 // 970 // Note: currently, find_or_load_shared_class is called only from 971 // JVM_FindLoadedClass and used for PlatformClassLoader and AppClassLoader, 972 // which are parallel-capable loaders, so this lock is NOT taken. 973 Handle lockObject = compute_loader_lock_object(class_loader, THREAD); 974 check_loader_lock_contention(lockObject, THREAD); 975 ObjectLocker ol(lockObject, THREAD, DoObjectLock); 976 977 { 978 MutexLocker mu(THREAD, SystemDictionary_lock); 979 InstanceKlass* check = find_class(d_hash, name, dictionary); 980 if (check != NULL) { 981 return check; 982 } 983 } 984 985 k = load_shared_class_for_builtin_loader(name, class_loader, THREAD); 986 if (k != NULL) { 987 define_instance_class(k, CHECK_NULL); 988 } 989 } 990 } 991 return k; 992 } 993 994 PackageEntry* SystemDictionaryShared::get_package_entry_from_class_name(Handle class_loader, Symbol* class_name) { 995 PackageEntry* pkg_entry = NULL; 996 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name); 997 if (pkg_name != NULL) { 998 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name); 999 } 1000 return pkg_entry; 1001 } 1002 1003 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader( 1004 Symbol* class_name, Handle class_loader, TRAPS) { 1005 assert(UseSharedSpaces, "must be"); 1006 InstanceKlass* ik = find_builtin_class(class_name); 1007 1008 if (ik != NULL) { 1009 if ((ik->is_shared_app_class() && 1010 SystemDictionary::is_system_class_loader(class_loader())) || 1011 (ik->is_shared_platform_class() && 1012 SystemDictionary::is_platform_class_loader(class_loader()))) { 1013 PackageEntry* pkg_entry = get_package_entry_from_class_name(class_loader, class_name); 1014 Handle protection_domain = 1015 SystemDictionaryShared::init_security_info(class_loader, ik, pkg_entry, CHECK_NULL); 1016 return load_shared_class(ik, class_loader, protection_domain, NULL, pkg_entry, THREAD); 1017 } 1018 } 1019 return NULL; 1020 } 1021 1022 void SystemDictionaryShared::allocate_shared_protection_domain_array(int size, TRAPS) { 1023 if (_shared_protection_domains.resolve() == NULL) { 1024 oop spd = oopFactory::new_objArray( 1025 SystemDictionary::ProtectionDomain_klass(), size, CHECK); 1026 _shared_protection_domains = OopHandle(OopStorageSet::vm_global(), spd); 1027 } 1028 } 1029 1030 void SystemDictionaryShared::allocate_shared_jar_url_array(int size, TRAPS) { 1031 if (_shared_jar_urls.resolve() == NULL) { 1032 oop sju = oopFactory::new_objArray( 1033 SystemDictionary::URL_klass(), size, CHECK); 1034 _shared_jar_urls = OopHandle(OopStorageSet::vm_global(), sju); 1035 } 1036 } 1037 1038 void SystemDictionaryShared::allocate_shared_jar_manifest_array(int size, TRAPS) { 1039 if (_shared_jar_manifests.resolve() == NULL) { 1040 oop sjm = oopFactory::new_objArray( 1041 SystemDictionary::Jar_Manifest_klass(), size, CHECK); 1042 _shared_jar_manifests = OopHandle(OopStorageSet::vm_global(), sjm); 1043 } 1044 } 1045 1046 void SystemDictionaryShared::allocate_shared_data_arrays(int size, TRAPS) { 1047 allocate_shared_protection_domain_array(size, CHECK); 1048 allocate_shared_jar_url_array(size, CHECK); 1049 allocate_shared_jar_manifest_array(size, CHECK); 1050 } 1051 1052 // This function is called for loading only UNREGISTERED classes 1053 InstanceKlass* SystemDictionaryShared::lookup_from_stream(Symbol* class_name, 1054 Handle class_loader, 1055 Handle protection_domain, 1056 const ClassFileStream* cfs, 1057 TRAPS) { 1058 if (!UseSharedSpaces) { 1059 return NULL; 1060 } 1061 if (class_name == NULL) { // don't do this for hidden and unsafe anonymous classes 1062 return NULL; 1063 } 1064 if (class_loader.is_null() || 1065 SystemDictionary::is_system_class_loader(class_loader()) || 1066 SystemDictionary::is_platform_class_loader(class_loader())) { 1067 // Do nothing for the BUILTIN loaders. 1068 return NULL; 1069 } 1070 1071 const RunTimeSharedClassInfo* record = find_record(&_unregistered_dictionary, &_dynamic_unregistered_dictionary, class_name); 1072 if (record == NULL) { 1073 return NULL; 1074 } 1075 1076 int clsfile_size = cfs->length(); 1077 int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 1078 1079 if (!record->matches(clsfile_size, clsfile_crc32)) { 1080 return NULL; 1081 } 1082 1083 return acquire_class_for_current_thread(record->_klass, class_loader, 1084 protection_domain, cfs, 1085 THREAD); 1086 } 1087 1088 InstanceKlass* SystemDictionaryShared::acquire_class_for_current_thread( 1089 InstanceKlass *ik, 1090 Handle class_loader, 1091 Handle protection_domain, 1092 const ClassFileStream *cfs, 1093 TRAPS) { 1094 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader()); 1095 1096 { 1097 MutexLocker mu(THREAD, SharedDictionary_lock); 1098 if (ik->class_loader_data() != NULL) { 1099 // ik is already loaded (by this loader or by a different loader) 1100 // or ik is being loaded by a different thread (by this loader or by a different loader) 1101 return NULL; 1102 } 1103 1104 // No other thread has acquired this yet, so give it to *this thread* 1105 ik->set_class_loader_data(loader_data); 1106 } 1107 1108 // No longer holding SharedDictionary_lock 1109 // No need to lock, as <ik> can be held only by a single thread. 1110 loader_data->add_class(ik); 1111 1112 // Get the package entry. 1113 PackageEntry* pkg_entry = get_package_entry_from_class_name(class_loader, ik->name()); 1114 1115 // Load and check super/interfaces, restore unsharable info 1116 InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain, 1117 cfs, pkg_entry, THREAD); 1118 if (shared_klass == NULL || HAS_PENDING_EXCEPTION) { 1119 // TODO: clean up <ik> so it can be used again 1120 return NULL; 1121 } 1122 1123 return shared_klass; 1124 } 1125 1126 static ResourceHashtable< 1127 Symbol*, bool, 1128 primitive_hash<Symbol*>, 1129 primitive_equals<Symbol*>, 1130 6661, // prime number 1131 ResourceObj::C_HEAP> _loaded_unregistered_classes; 1132 1133 bool SystemDictionaryShared::add_unregistered_class(InstanceKlass* k, TRAPS) { 1134 // We don't allow duplicated unregistered classes of the same name. 1135 assert(DumpSharedSpaces, "only when dumping"); 1136 Symbol* name = k->name(); 1137 bool created = false; 1138 _loaded_unregistered_classes.put_if_absent(name, true, &created); 1139 if (created) { 1140 MutexLocker mu_r(THREAD, Compile_lock); // add_to_hierarchy asserts this. 1141 SystemDictionary::add_to_hierarchy(k, CHECK_false); 1142 } 1143 return created; 1144 } 1145 1146 // This function is called to resolve the super/interfaces of shared classes for 1147 // non-built-in loaders. E.g., ChildClass in the below example 1148 // where "super:" (and optionally "interface:") have been specified. 1149 // 1150 // java/lang/Object id: 0 1151 // Interface id: 2 super: 0 source: cust.jar 1152 // ChildClass id: 4 super: 0 interfaces: 2 source: cust.jar 1153 InstanceKlass* SystemDictionaryShared::dump_time_resolve_super_or_fail( 1154 Symbol* child_name, Symbol* class_name, Handle class_loader, 1155 Handle protection_domain, bool is_superclass, TRAPS) { 1156 1157 assert(DumpSharedSpaces, "only when dumping"); 1158 1159 ClassListParser* parser = ClassListParser::instance(); 1160 if (parser == NULL) { 1161 // We're still loading the well-known classes, before the ClassListParser is created. 1162 return NULL; 1163 } 1164 if (child_name->equals(parser->current_class_name())) { 1165 // When this function is called, all the numbered super and interface types 1166 // must have already been loaded. Hence this function is never recursively called. 1167 if (is_superclass) { 1168 return parser->lookup_super_for_current_class(class_name); 1169 } else { 1170 return parser->lookup_interface_for_current_class(class_name); 1171 } 1172 } else { 1173 // The VM is not trying to resolve a super type of parser->current_class_name(). 1174 // Instead, it's resolving an error class (because parser->current_class_name() has 1175 // failed parsing or verification). Don't do anything here. 1176 return NULL; 1177 } 1178 } 1179 1180 DumpTimeSharedClassInfo* SystemDictionaryShared::find_or_allocate_info_for(InstanceKlass* k) { 1181 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 1182 if (_dumptime_table == NULL) { 1183 _dumptime_table = new (ResourceObj::C_HEAP, mtClass)DumpTimeSharedClassTable(); 1184 } 1185 return _dumptime_table->find_or_allocate_info_for(k); 1186 } 1187 1188 void SystemDictionaryShared::set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs) { 1189 Arguments::assert_is_dumping_archive(); 1190 assert(!is_builtin(k), "must be unregistered class"); 1191 DumpTimeSharedClassInfo* info = find_or_allocate_info_for(k); 1192 info->_clsfile_size = cfs->length(); 1193 info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 1194 } 1195 1196 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) { 1197 (void)find_or_allocate_info_for(k); 1198 } 1199 1200 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) { 1201 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 1202 DumpTimeSharedClassInfo* p = _dumptime_table->get(k); 1203 if (p == NULL) { 1204 return; 1205 } 1206 if (p->_verifier_constraints != NULL) { 1207 for (int i = 0; i < p->_verifier_constraints->length(); i++) { 1208 DumpTimeSharedClassInfo::DTVerifierConstraint constraint = p->_verifier_constraints->at(i); 1209 if (constraint._name != NULL ) { 1210 constraint._name->decrement_refcount(); 1211 } 1212 if (constraint._from_name != NULL ) { 1213 constraint._from_name->decrement_refcount(); 1214 } 1215 } 1216 FREE_C_HEAP_ARRAY(DumpTimeSharedClassInfo::DTVerifierConstraint, p->_verifier_constraints); 1217 p->_verifier_constraints = NULL; 1218 FREE_C_HEAP_ARRAY(char, p->_verifier_constraint_flags); 1219 p->_verifier_constraint_flags = NULL; 1220 } 1221 if (p->_loader_constraints != NULL) { 1222 for (int i = 0; i < p->_loader_constraints->length(); i++) { 1223 DumpTimeSharedClassInfo::DTLoaderConstraint ld = p->_loader_constraints->at(i); 1224 if (ld._name != NULL) { 1225 ld._name->decrement_refcount(); 1226 } 1227 } 1228 FREE_C_HEAP_ARRAY(DumpTimeSharedClassInfo::DTLoaderConstraint, p->_loader_constraints); 1229 p->_loader_constraints = NULL; 1230 } 1231 _dumptime_table->remove(k); 1232 } 1233 1234 bool SystemDictionaryShared::is_jfr_event_class(InstanceKlass *k) { 1235 while (k) { 1236 if (k->name()->equals("jdk/internal/event/Event")) { 1237 return true; 1238 } 1239 k = k->java_super(); 1240 } 1241 return false; 1242 } 1243 1244 bool SystemDictionaryShared::is_registered_lambda_proxy_class(InstanceKlass* ik) { 1245 DumpTimeSharedClassInfo* info = _dumptime_table->get(ik); 1246 return (info != NULL) ? info->_is_archived_lambda_proxy : false; 1247 } 1248 1249 bool SystemDictionaryShared::is_hidden_lambda_proxy(InstanceKlass* ik) { 1250 assert(ik->is_shared(), "applicable to only a shared class"); 1251 if (ik->is_hidden()) { 1252 return true; 1253 } else { 1254 return false; 1255 } 1256 } 1257 1258 void SystemDictionaryShared::warn_excluded(InstanceKlass* k, const char* reason) { 1259 ResourceMark rm; 1260 log_warning(cds)("Skipping %s: %s", k->name()->as_C_string(), reason); 1261 } 1262 1263 bool SystemDictionaryShared::should_be_excluded(InstanceKlass* k) { 1264 1265 if (k->is_unsafe_anonymous()) { 1266 warn_excluded(k, "Unsafe anonymous class"); 1267 return true; // unsafe anonymous classes are not archived, skip 1268 } 1269 1270 if (k->is_in_error_state()) { 1271 warn_excluded(k, "In error state"); 1272 return true; 1273 } 1274 if (k->has_been_redefined()) { 1275 warn_excluded(k, "Has been redefined"); 1276 return true; 1277 } 1278 if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) { 1279 // These are classes loaded from unsupported locations (such as those loaded by JVMTI native 1280 // agent during dump time). 1281 warn_excluded(k, "Unsupported location"); 1282 return true; 1283 } 1284 if (k->signers() != NULL) { 1285 // We cannot include signed classes in the archive because the certificates 1286 // used during dump time may be different than those used during 1287 // runtime (due to expiration, etc). 1288 warn_excluded(k, "Signed JAR"); 1289 return true; 1290 } 1291 if (is_jfr_event_class(k)) { 1292 // We cannot include JFR event classes because they need runtime-specific 1293 // instrumentation in order to work with -XX:FlightRecorderOptions=retransform=false. 1294 // There are only a small number of these classes, so it's not worthwhile to 1295 // support them and make CDS more complicated. 1296 warn_excluded(k, "JFR event class"); 1297 return true; 1298 } 1299 if (k->init_state() < InstanceKlass::linked) { 1300 // In CDS dumping, we will attempt to link all classes. Those that fail to link will 1301 // be recorded in DumpTimeSharedClassInfo. 1302 Arguments::assert_is_dumping_archive(); 1303 1304 // TODO -- rethink how this can be handled. 1305 // We should try to link ik, however, we can't do it here because 1306 // 1. We are at VM exit 1307 // 2. linking a class may cause other classes to be loaded, which means 1308 // a custom ClassLoader.loadClass() may be called, at a point where the 1309 // class loader doesn't expect it. 1310 if (has_class_failed_verification(k)) { 1311 warn_excluded(k, "Failed verification"); 1312 } else { 1313 warn_excluded(k, "Not linked"); 1314 } 1315 return true; 1316 } 1317 if (k->major_version() < 50 /*JAVA_6_VERSION*/) { 1318 ResourceMark rm; 1319 log_warning(cds)("Pre JDK 6 class not supported by CDS: %u.%u %s", 1320 k->major_version(), k->minor_version(), k->name()->as_C_string()); 1321 return true; 1322 } 1323 1324 InstanceKlass* super = k->java_super(); 1325 if (super != NULL && should_be_excluded(super)) { 1326 ResourceMark rm; 1327 log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string()); 1328 return true; 1329 } 1330 1331 if (k->is_hidden() && !is_registered_lambda_proxy_class(k)) { 1332 warn_excluded(k, "Hidden class"); 1333 return true; 1334 } 1335 1336 Array<InstanceKlass*>* interfaces = k->local_interfaces(); 1337 int len = interfaces->length(); 1338 for (int i = 0; i < len; i++) { 1339 InstanceKlass* intf = interfaces->at(i); 1340 if (should_be_excluded(intf)) { 1341 log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string()); 1342 return true; 1343 } 1344 } 1345 1346 return false; 1347 } 1348 1349 // k is a class before relocating by ArchiveCompactor 1350 void SystemDictionaryShared::validate_before_archiving(InstanceKlass* k) { 1351 ResourceMark rm; 1352 const char* name = k->name()->as_C_string(); 1353 DumpTimeSharedClassInfo* info = _dumptime_table->get(k); 1354 assert(_no_class_loading_should_happen, "class loading must be disabled"); 1355 guarantee(info != NULL, "Class %s must be entered into _dumptime_table", name); 1356 guarantee(!info->is_excluded(), "Should not attempt to archive excluded class %s", name); 1357 if (is_builtin(k)) { 1358 if (k->is_hidden()) { 1359 assert(is_registered_lambda_proxy_class(k), "unexpected hidden class %s", name); 1360 } 1361 guarantee(!k->is_shared_unregistered_class(), 1362 "Class loader type must be set for BUILTIN class %s", name); 1363 1364 } else { 1365 guarantee(k->is_shared_unregistered_class(), 1366 "Class loader type must not be set for UNREGISTERED class %s", name); 1367 } 1368 } 1369 1370 class ExcludeDumpTimeSharedClasses : StackObj { 1371 public: 1372 bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) { 1373 if (SystemDictionaryShared::should_be_excluded(k)) { 1374 info.set_excluded(); 1375 } 1376 return true; // keep on iterating 1377 } 1378 }; 1379 1380 void SystemDictionaryShared::check_excluded_classes() { 1381 ExcludeDumpTimeSharedClasses excl; 1382 _dumptime_table->iterate(&excl); 1383 _dumptime_table->update_counts(); 1384 } 1385 1386 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) { 1387 assert(_no_class_loading_should_happen, "sanity"); 1388 Arguments::assert_is_dumping_archive(); 1389 return find_or_allocate_info_for(k)->is_excluded(); 1390 } 1391 1392 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) { 1393 Arguments::assert_is_dumping_archive(); 1394 find_or_allocate_info_for(ik)->set_failed_verification(); 1395 } 1396 1397 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) { 1398 Arguments::assert_is_dumping_archive(); 1399 if (_dumptime_table == NULL) { 1400 assert(DynamicDumpSharedSpaces, "sanity"); 1401 assert(ik->is_shared(), "must be a shared class in the static archive"); 1402 return false; 1403 } 1404 DumpTimeSharedClassInfo* p = _dumptime_table->get(ik); 1405 return (p == NULL) ? false : p->failed_verification(); 1406 } 1407 1408 class IterateDumpTimeSharedClassTable : StackObj { 1409 MetaspaceClosure *_it; 1410 public: 1411 IterateDumpTimeSharedClassTable(MetaspaceClosure* it) : _it(it) {} 1412 1413 bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) { 1414 if (!info.is_excluded()) { 1415 info.metaspace_pointers_do(_it); 1416 } 1417 return true; // keep on iterating 1418 } 1419 }; 1420 1421 void SystemDictionaryShared::dumptime_classes_do(class MetaspaceClosure* it) { 1422 IterateDumpTimeSharedClassTable iter(it); 1423 _dumptime_table->iterate(&iter); 1424 } 1425 1426 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name, 1427 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) { 1428 Arguments::assert_is_dumping_archive(); 1429 DumpTimeSharedClassInfo* info = find_or_allocate_info_for(k); 1430 info->add_verification_constraint(k, name, from_name, from_field_is_protected, 1431 from_is_array, from_is_object); 1432 1433 if (DynamicDumpSharedSpaces) { 1434 // For dynamic dumping, we can resolve all the constraint classes for all class loaders during 1435 // the initial run prior to creating the archive before vm exit. We will also perform verification 1436 // check when running with the archive. 1437 return false; 1438 } else { 1439 if (is_builtin(k)) { 1440 // For builtin class loaders, we can try to complete the verification check at dump time, 1441 // because we can resolve all the constraint classes. We will also perform verification check 1442 // when running with the archive. 1443 return false; 1444 } else { 1445 // For non-builtin class loaders, we cannot complete the verification check at dump time, 1446 // because at dump time we don't know how to resolve classes for such loaders. 1447 return true; 1448 } 1449 } 1450 } 1451 1452 void DumpTimeSharedClassInfo::add_verification_constraint(InstanceKlass* k, Symbol* name, 1453 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) { 1454 if (_verifier_constraints == NULL) { 1455 _verifier_constraints = new(ResourceObj::C_HEAP, mtClass) GrowableArray<DTVerifierConstraint>(4, mtClass); 1456 } 1457 if (_verifier_constraint_flags == NULL) { 1458 _verifier_constraint_flags = new(ResourceObj::C_HEAP, mtClass) GrowableArray<char>(4, mtClass); 1459 } 1460 GrowableArray<DTVerifierConstraint>* vc_array = _verifier_constraints; 1461 for (int i = 0; i < vc_array->length(); i++) { 1462 DTVerifierConstraint* p = vc_array->adr_at(i); 1463 if (name == p->_name && from_name == p->_from_name) { 1464 return; 1465 } 1466 } 1467 DTVerifierConstraint cons(name, from_name); 1468 vc_array->append(cons); 1469 1470 GrowableArray<char>* vcflags_array = _verifier_constraint_flags; 1471 char c = 0; 1472 c |= from_field_is_protected ? SystemDictionaryShared::FROM_FIELD_IS_PROTECTED : 0; 1473 c |= from_is_array ? SystemDictionaryShared::FROM_IS_ARRAY : 0; 1474 c |= from_is_object ? SystemDictionaryShared::FROM_IS_OBJECT : 0; 1475 vcflags_array->append(c); 1476 1477 if (log_is_enabled(Trace, cds, verification)) { 1478 ResourceMark rm; 1479 log_trace(cds, verification)("add_verification_constraint: %s: %s must be subclass of %s [0x%x] array len %d flags len %d", 1480 k->external_name(), from_name->as_klass_external_name(), 1481 name->as_klass_external_name(), c, vc_array->length(), vcflags_array->length()); 1482 } 1483 } 1484 1485 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik, 1486 InstanceKlass* lambda_ik, 1487 Symbol* invoked_name, 1488 Symbol* invoked_type, 1489 Symbol* method_type, 1490 Method* member_method, 1491 Symbol* instantiated_method_type) { 1492 1493 assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader"); 1494 assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data"); 1495 assert(java_lang_Class::class_data(lambda_ik->java_mirror()) == NULL, "must not have class data"); 1496 1497 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 1498 1499 lambda_ik->assign_class_loader_type(); 1500 lambda_ik->set_shared_classpath_index(caller_ik->shared_classpath_index()); 1501 1502 DumpTimeSharedClassInfo* info = _dumptime_table->get(lambda_ik); 1503 if (info != NULL && !lambda_ik->is_non_strong_hidden() && is_builtin(lambda_ik) && is_builtin(caller_ik)) { 1504 // Set _is_archived_lambda_proxy in DumpTimeSharedClassInfo so that the lambda_ik 1505 // won't be excluded during dumping of shared archive. See ExcludeDumpTimeSharedClasses. 1506 info->_is_archived_lambda_proxy = true; 1507 1508 LambdaProxyClassKey key(caller_ik, 1509 invoked_name, 1510 invoked_type, 1511 method_type, 1512 member_method, 1513 instantiated_method_type); 1514 add_to_dump_time_lambda_proxy_class_dictionary(key, lambda_ik); 1515 } 1516 } 1517 1518 InstanceKlass* SystemDictionaryShared::get_shared_lambda_proxy_class(InstanceKlass* caller_ik, 1519 Symbol* invoked_name, 1520 Symbol* invoked_type, 1521 Symbol* method_type, 1522 Method* member_method, 1523 Symbol* instantiated_method_type) { 1524 MutexLocker ml(CDSLambda_lock, Mutex::_no_safepoint_check_flag); 1525 LambdaProxyClassKey key(caller_ik, invoked_name, invoked_type, 1526 method_type, member_method, instantiated_method_type); 1527 const RunTimeLambdaProxyClassInfo* info = _lambda_proxy_class_dictionary.lookup(&key, key.hash(), 0); 1528 InstanceKlass* proxy_klass = NULL; 1529 if (info != NULL) { 1530 InstanceKlass* curr_klass = info->proxy_klass_head(); 1531 InstanceKlass* prev_klass = curr_klass; 1532 if (curr_klass->lambda_proxy_is_available()) { 1533 while (curr_klass->next_link() != NULL) { 1534 prev_klass = curr_klass; 1535 curr_klass = InstanceKlass::cast(curr_klass->next_link()); 1536 } 1537 assert(curr_klass->is_hidden(), "must be"); 1538 assert(curr_klass->lambda_proxy_is_available(), "must be"); 1539 1540 prev_klass->set_next_link(NULL); 1541 proxy_klass = curr_klass; 1542 proxy_klass->clear_lambda_proxy_is_available(); 1543 if (log_is_enabled(Debug, cds)) { 1544 ResourceMark rm; 1545 log_debug(cds)("Loaded lambda proxy: %s", proxy_klass->external_name()); 1546 } 1547 } else { 1548 if (log_is_enabled(Debug, cds)) { 1549 ResourceMark rm; 1550 log_debug(cds)("Used all archived lambda proxy classes for: %s %s%s", 1551 caller_ik->external_name(), invoked_name->as_C_string(), invoked_type->as_C_string()); 1552 } 1553 } 1554 } 1555 return proxy_klass; 1556 } 1557 1558 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) { 1559 assert(!DumpSharedSpaces && UseSharedSpaces, "called at run time with CDS enabled only"); 1560 RunTimeSharedClassInfo* record = RunTimeSharedClassInfo::get_for(lambda_ik); 1561 return record->nest_host(); 1562 } 1563 1564 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik, 1565 InstanceKlass* caller_ik, 1566 bool initialize, TRAPS) { 1567 Handle class_loader(THREAD, caller_ik->class_loader()); 1568 Handle protection_domain; 1569 PackageEntry* pkg_entry = get_package_entry_from_class_name(class_loader, caller_ik->name()); 1570 if (caller_ik->class_loader() != NULL) { 1571 protection_domain = SystemDictionaryShared::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL); 1572 } 1573 1574 InstanceKlass* shared_nest_host = get_shared_nest_host(lambda_ik); 1575 assert(shared_nest_host != NULL, "unexpected NULL _nest_host"); 1576 1577 InstanceKlass* loaded_lambda = 1578 SystemDictionary::load_shared_lambda_proxy_class(lambda_ik, class_loader, protection_domain, pkg_entry, CHECK_NULL); 1579 1580 // Ensures the nest host is the same as the lambda proxy's 1581 // nest host recorded at dump time. 1582 InstanceKlass* nest_host = caller_ik->nest_host(THREAD); 1583 assert(nest_host == shared_nest_host, "mismatched nest host"); 1584 1585 EventClassLoad class_load_start_event; 1586 { 1587 MutexLocker mu_r(THREAD, Compile_lock); 1588 1589 // Add to class hierarchy, initialize vtables, and do possible 1590 // deoptimizations. 1591 SystemDictionary::add_to_hierarchy(loaded_lambda, CHECK_NULL); // No exception, but can block 1592 // But, do not add to dictionary. 1593 } 1594 loaded_lambda->link_class(CHECK_NULL); 1595 // notify jvmti 1596 if (JvmtiExport::should_post_class_load()) { 1597 assert(THREAD->is_Java_thread(), "thread->is_Java_thread()"); 1598 JvmtiExport::post_class_load((JavaThread *) THREAD, loaded_lambda); 1599 } 1600 if (class_load_start_event.should_commit()) { 1601 SystemDictionary::post_class_load_event(&class_load_start_event, loaded_lambda, ClassLoaderData::class_loader_data(class_loader())); 1602 } 1603 1604 if (initialize) { 1605 loaded_lambda->initialize(CHECK_NULL); 1606 } 1607 1608 return loaded_lambda; 1609 } 1610 1611 static char get_loader_type_by(oop loader) { 1612 assert(SystemDictionary::is_builtin_class_loader(loader), "Must be built-in loader"); 1613 if (SystemDictionary::is_boot_class_loader(loader)) { 1614 return (char)ClassLoader::BOOT_LOADER; 1615 } else if (SystemDictionary::is_platform_class_loader(loader)) { 1616 return (char)ClassLoader::PLATFORM_LOADER; 1617 } else { 1618 assert(SystemDictionary::is_system_class_loader(loader), "Class loader mismatch"); 1619 return (char)ClassLoader::APP_LOADER; 1620 } 1621 } 1622 1623 static oop get_class_loader_by(char type) { 1624 if (type == (char)ClassLoader::BOOT_LOADER) { 1625 return (oop)NULL; 1626 } else if (type == (char)ClassLoader::PLATFORM_LOADER) { 1627 return SystemDictionary::java_platform_loader(); 1628 } else { 1629 assert (type == (char)ClassLoader::APP_LOADER, "Sanity"); 1630 return SystemDictionary::java_system_loader(); 1631 } 1632 } 1633 1634 void DumpTimeSharedClassInfo::record_linking_constraint(Symbol* name, Handle loader1, Handle loader2) { 1635 assert(loader1 != loader2, "sanity"); 1636 LogTarget(Info, class, loader, constraints) log; 1637 if (_loader_constraints == NULL) { 1638 _loader_constraints = new (ResourceObj::C_HEAP, mtClass) GrowableArray<DTLoaderConstraint>(4, mtClass); 1639 } 1640 char lt1 = get_loader_type_by(loader1()); 1641 char lt2 = get_loader_type_by(loader2()); 1642 DTLoaderConstraint lc(name, lt1, lt2); 1643 for (int i = 0; i < _loader_constraints->length(); i++) { 1644 DTLoaderConstraint dt = _loader_constraints->at(i); 1645 if (lc.equals(dt)) { 1646 if (log.is_enabled()) { 1647 ResourceMark rm; 1648 // Use loader[0]/loader[1] to be consistent with the logs in loaderConstraints.cpp 1649 log.print("[CDS record loader constraint for class: %s constraint_name: %s loader[0]: %s loader[1]: %s already added]", 1650 _klass->external_name(), name->as_C_string(), 1651 ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(), 1652 ClassLoaderData::class_loader_data(loader2())->loader_name_and_id()); 1653 } 1654 return; 1655 } 1656 } 1657 _loader_constraints->append(lc); 1658 if (log.is_enabled()) { 1659 ResourceMark rm; 1660 // Use loader[0]/loader[1] to be consistent with the logs in loaderConstraints.cpp 1661 log.print("[CDS record loader constraint for class: %s constraint_name: %s loader[0]: %s loader[1]: %s total %d]", 1662 _klass->external_name(), name->as_C_string(), 1663 ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(), 1664 ClassLoaderData::class_loader_data(loader2())->loader_name_and_id(), 1665 _loader_constraints->length()); 1666 } 1667 } 1668 1669 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass, 1670 TRAPS) { 1671 assert(!DumpSharedSpaces && UseSharedSpaces, "called at run time with CDS enabled only"); 1672 RunTimeSharedClassInfo* record = RunTimeSharedClassInfo::get_for(klass); 1673 1674 int length = record->_num_verifier_constraints; 1675 if (length > 0) { 1676 for (int i = 0; i < length; i++) { 1677 RunTimeSharedClassInfo::RTVerifierConstraint* vc = record->verifier_constraint_at(i); 1678 Symbol* name = vc->name(); 1679 Symbol* from_name = vc->from_name(); 1680 char c = record->verifier_constraint_flag(i); 1681 1682 if (log_is_enabled(Trace, cds, verification)) { 1683 ResourceMark rm(THREAD); 1684 log_trace(cds, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]", 1685 klass->external_name(), from_name->as_klass_external_name(), 1686 name->as_klass_external_name(), c); 1687 } 1688 1689 bool from_field_is_protected = (c & SystemDictionaryShared::FROM_FIELD_IS_PROTECTED) ? true : false; 1690 bool from_is_array = (c & SystemDictionaryShared::FROM_IS_ARRAY) ? true : false; 1691 bool from_is_object = (c & SystemDictionaryShared::FROM_IS_OBJECT) ? true : false; 1692 1693 bool ok = VerificationType::resolve_and_check_assignability(klass, name, 1694 from_name, from_field_is_protected, from_is_array, from_is_object, CHECK); 1695 if (!ok) { 1696 ResourceMark rm(THREAD); 1697 stringStream ss; 1698 1699 ss.print_cr("Bad type on operand stack"); 1700 ss.print_cr("Exception Details:"); 1701 ss.print_cr(" Location:\n %s", klass->name()->as_C_string()); 1702 ss.print_cr(" Reason:\n Type '%s' is not assignable to '%s'", 1703 from_name->as_quoted_ascii(), name->as_quoted_ascii()); 1704 THROW_MSG(vmSymbols::java_lang_VerifyError(), ss.as_string()); 1705 } 1706 } 1707 } 1708 } 1709 1710 // Record class loader constraints that are checked inside 1711 // InstanceKlass::link_class(), so that these can be checked quickly 1712 // at runtime without laying out the vtable/itables. 1713 void SystemDictionaryShared::record_linking_constraint(Symbol* name, InstanceKlass* klass, 1714 Handle loader1, Handle loader2, TRAPS) { 1715 // A linking constraint check is executed when: 1716 // - klass extends or implements type S 1717 // - klass overrides method S.M(...) with X.M 1718 // - If klass defines the method M, X is 1719 // the same as klass. 1720 // - If klass does not define the method M, 1721 // X must be a supertype of klass and X.M is 1722 // a default method defined by X. 1723 // - loader1 = X->class_loader() 1724 // - loader2 = S->class_loader() 1725 // - loader1 != loader2 1726 // - M's paramater(s) include an object type T 1727 // We require that 1728 // - whenever loader1 and loader2 try to 1729 // resolve the type T, they must always resolve to 1730 // the same InstanceKlass. 1731 // NOTE: type T may or may not be currently resolved in 1732 // either of these two loaders. The check itself does not 1733 // try to resolve T. 1734 oop klass_loader = klass->class_loader(); 1735 assert(klass_loader != NULL, "should not be called for boot loader"); 1736 assert(loader1 != loader2, "must be"); 1737 1738 if (!is_system_class_loader(klass_loader) && 1739 !is_platform_class_loader(klass_loader)) { 1740 // If klass is loaded by system/platform loaders, we can 1741 // guarantee that klass and S must be loaded by the same 1742 // respective loader between dump time and run time, and 1743 // the exact same check on (name, loader1, loader2) will 1744 // be executed. Hence, we can cache this check and execute 1745 // it at runtime without walking the vtable/itables. 1746 // 1747 // This cannot be guaranteed for classes loaded by other 1748 // loaders, so we bail. 1749 return; 1750 } 1751 1752 if (THREAD->is_VM_thread()) { 1753 assert(DynamicDumpSharedSpaces, "must be"); 1754 // We are re-laying out the vtable/itables of the *copy* of 1755 // a class during the final stage of dynamic dumping. The 1756 // linking constraints for this class has already been recorded. 1757 return; 1758 } 1759 Arguments::assert_is_dumping_archive(); 1760 DumpTimeSharedClassInfo* info = find_or_allocate_info_for(klass); 1761 info->record_linking_constraint(name, loader1, loader2); 1762 } 1763 1764 // returns true IFF there's no need to re-initialize the i/v-tables for klass for 1765 // the purpose of checking class loader constraints. 1766 bool SystemDictionaryShared::check_linking_constraints(InstanceKlass* klass, TRAPS) { 1767 assert(!DumpSharedSpaces && UseSharedSpaces, "called at run time with CDS enabled only"); 1768 LogTarget(Info, class, loader, constraints) log; 1769 if (klass->is_shared_boot_class()) { 1770 // No class loader constraint check performed for boot classes. 1771 return true; 1772 } 1773 if (klass->is_shared_platform_class() || klass->is_shared_app_class()) { 1774 RunTimeSharedClassInfo* info = RunTimeSharedClassInfo::get_for(klass); 1775 assert(info != NULL, "Sanity"); 1776 if (info->_num_loader_constraints > 0) { 1777 HandleMark hm; 1778 for (int i = 0; i < info->_num_loader_constraints; i++) { 1779 RunTimeSharedClassInfo::RTLoaderConstraint* lc = info->loader_constraint_at(i); 1780 Symbol* name = lc->constraint_name(); 1781 Handle loader1(THREAD, get_class_loader_by(lc->_loader_type1)); 1782 Handle loader2(THREAD, get_class_loader_by(lc->_loader_type2)); 1783 if (log.is_enabled()) { 1784 ResourceMark rm(THREAD); 1785 log.print("[CDS add loader constraint for class %s symbol %s loader[0] %s loader[1] %s", 1786 klass->external_name(), name->as_C_string(), 1787 ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(), 1788 ClassLoaderData::class_loader_data(loader2())->loader_name_and_id()); 1789 } 1790 if (!SystemDictionary::add_loader_constraint(name, klass, loader1, loader2, THREAD)) { 1791 // Loader constraint violation has been found. The caller 1792 // will re-layout the vtable/itables to produce the correct 1793 // exception. 1794 if (log.is_enabled()) { 1795 log.print(" failed]"); 1796 } 1797 return false; 1798 } 1799 if (log.is_enabled()) { 1800 log.print(" succeeded]"); 1801 } 1802 } 1803 return true; // for all recorded constraints added successully. 1804 } 1805 } 1806 if (log.is_enabled()) { 1807 ResourceMark rm(THREAD); 1808 log.print("[CDS has not recorded loader constraint for class %s]", klass->external_name()); 1809 } 1810 return false; 1811 } 1812 1813 class EstimateSizeForArchive : StackObj { 1814 size_t _shared_class_info_size; 1815 int _num_builtin_klasses; 1816 int _num_unregistered_klasses; 1817 1818 public: 1819 EstimateSizeForArchive() { 1820 _shared_class_info_size = 0; 1821 _num_builtin_klasses = 0; 1822 _num_unregistered_klasses = 0; 1823 } 1824 1825 bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) { 1826 if (!info.is_excluded()) { 1827 size_t byte_size = RunTimeSharedClassInfo::byte_size(info._klass, info.num_verifier_constraints(), info.num_loader_constraints()); 1828 _shared_class_info_size += align_up(byte_size, BytesPerWord); 1829 } 1830 return true; // keep on iterating 1831 } 1832 1833 size_t total() { 1834 return _shared_class_info_size; 1835 } 1836 }; 1837 1838 size_t SystemDictionaryShared::estimate_size_for_archive() { 1839 EstimateSizeForArchive est; 1840 _dumptime_table->iterate(&est); 1841 size_t total_size = est.total() + 1842 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) + 1843 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false)); 1844 if (_dumptime_lambda_proxy_class_dictionary != NULL) { 1845 total_size += 1846 (sizeof(RunTimeLambdaProxyClassInfo) * _dumptime_lambda_proxy_class_dictionary->_count) + 1847 CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count); 1848 } else { 1849 total_size += CompactHashtableWriter::estimate_size(0); 1850 } 1851 return total_size; 1852 } 1853 1854 class CopyLambdaProxyClassInfoToArchive : StackObj { 1855 CompactHashtableWriter* _writer; 1856 public: 1857 CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer) 1858 : _writer(writer) {} 1859 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) { 1860 if (SystemDictionaryShared::is_excluded_class(info._proxy_klasses->at(0))) { 1861 return true; 1862 } 1863 ResourceMark rm; 1864 log_info(cds,dynamic)("Archiving hidden %s", info._proxy_klasses->at(0)->external_name()); 1865 size_t byte_size = sizeof(RunTimeLambdaProxyClassInfo); 1866 RunTimeLambdaProxyClassInfo* runtime_info = 1867 (RunTimeLambdaProxyClassInfo*)MetaspaceShared::read_only_space_alloc(byte_size); 1868 runtime_info->init(key, info); 1869 unsigned int hash = runtime_info->hash(); // Fields in runtime_info->_key already point to target space. 1870 u4 delta = MetaspaceShared::object_delta_u4(DynamicArchive::buffer_to_target(runtime_info)); 1871 _writer->add(hash, delta); 1872 return true; 1873 } 1874 }; 1875 1876 class AdjustLambdaProxyClassInfo : StackObj { 1877 public: 1878 AdjustLambdaProxyClassInfo() {} 1879 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) { 1880 if (SystemDictionaryShared::is_excluded_class(info._proxy_klasses->at(0))) { 1881 return true; 1882 } 1883 int len = info._proxy_klasses->length(); 1884 if (len > 1) { 1885 for (int i = 0; i < len-1; i++) { 1886 InstanceKlass* ok0 = info._proxy_klasses->at(i+0); // this is original klass 1887 InstanceKlass* ok1 = info._proxy_klasses->at(i+1); // this is original klass 1888 InstanceKlass* bk0 = DynamicArchive::original_to_buffer(ok0); 1889 InstanceKlass* bk1 = DynamicArchive::original_to_buffer(ok1); 1890 assert(bk0->next_link() == 0, "must be called after Klass::remove_unshareable_info()"); 1891 assert(bk1->next_link() == 0, "must be called after Klass::remove_unshareable_info()"); 1892 bk0->set_next_link(bk1); 1893 bk1->set_lambda_proxy_is_available(); 1894 ArchivePtrMarker::mark_pointer(bk0->next_link_addr()); 1895 } 1896 } 1897 DynamicArchive::original_to_buffer(info._proxy_klasses->at(0))->set_lambda_proxy_is_available(); 1898 return true; 1899 } 1900 }; 1901 1902 class CopySharedClassInfoToArchive : StackObj { 1903 CompactHashtableWriter* _writer; 1904 bool _is_builtin; 1905 public: 1906 CopySharedClassInfoToArchive(CompactHashtableWriter* writer, 1907 bool is_builtin, 1908 bool is_static_archive) 1909 : _writer(writer), _is_builtin(is_builtin) {} 1910 1911 bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) { 1912 if (!info.is_excluded() && info.is_builtin() == _is_builtin) { 1913 size_t byte_size = RunTimeSharedClassInfo::byte_size(info._klass, info.num_verifier_constraints(), info.num_loader_constraints()); 1914 RunTimeSharedClassInfo* record; 1915 record = (RunTimeSharedClassInfo*)MetaspaceShared::read_only_space_alloc(byte_size); 1916 record->init(info); 1917 1918 unsigned int hash; 1919 Symbol* name = info._klass->name(); 1920 if (DynamicDumpSharedSpaces) { 1921 name = DynamicArchive::original_to_target(name); 1922 } 1923 hash = SystemDictionaryShared::hash_for_shared_dictionary(name); 1924 u4 delta; 1925 if (DynamicDumpSharedSpaces) { 1926 delta = MetaspaceShared::object_delta_u4(DynamicArchive::buffer_to_target(record)); 1927 } else { 1928 delta = MetaspaceShared::object_delta_u4(record); 1929 } 1930 if (_is_builtin && info._klass->is_hidden()) { 1931 // skip 1932 } else { 1933 _writer->add(hash, delta); 1934 } 1935 if (log_is_enabled(Trace, cds, hashtables)) { 1936 ResourceMark rm; 1937 log_trace(cds,hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name()); 1938 } 1939 1940 // Save this for quick runtime lookup of InstanceKlass* -> RunTimeSharedClassInfo* 1941 RunTimeSharedClassInfo::set_for(info._klass, record); 1942 } 1943 return true; // keep on iterating 1944 } 1945 }; 1946 1947 void SystemDictionaryShared::write_lambda_proxy_class_dictionary(LambdaProxyClassDictionary *dictionary) { 1948 CompactHashtableStats stats; 1949 dictionary->reset(); 1950 CompactHashtableWriter writer(_dumptime_lambda_proxy_class_dictionary->_count, &stats); 1951 CopyLambdaProxyClassInfoToArchive copy(&writer); 1952 _dumptime_lambda_proxy_class_dictionary->iterate(©); 1953 writer.dump(dictionary, "lambda proxy class dictionary"); 1954 } 1955 1956 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary, 1957 bool is_builtin, 1958 bool is_static_archive) { 1959 CompactHashtableStats stats; 1960 dictionary->reset(); 1961 CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats); 1962 CopySharedClassInfoToArchive copy(&writer, is_builtin, is_static_archive); 1963 _dumptime_table->iterate(©); 1964 writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary"); 1965 } 1966 1967 void SystemDictionaryShared::write_to_archive(bool is_static_archive) { 1968 if (is_static_archive) { 1969 write_dictionary(&_builtin_dictionary, true); 1970 write_dictionary(&_unregistered_dictionary, false); 1971 } else { 1972 write_dictionary(&_dynamic_builtin_dictionary, true); 1973 write_dictionary(&_dynamic_unregistered_dictionary, false); 1974 } 1975 if (_dumptime_lambda_proxy_class_dictionary != NULL) { 1976 write_lambda_proxy_class_dictionary(&_lambda_proxy_class_dictionary); 1977 } 1978 } 1979 1980 void SystemDictionaryShared::adjust_lambda_proxy_class_dictionary() { 1981 if (_dumptime_lambda_proxy_class_dictionary != NULL) { 1982 AdjustLambdaProxyClassInfo adjuster; 1983 _dumptime_lambda_proxy_class_dictionary->iterate(&adjuster); 1984 } 1985 } 1986 1987 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc, 1988 bool is_static_archive) { 1989 if (is_static_archive) { 1990 _builtin_dictionary.serialize_header(soc); 1991 _unregistered_dictionary.serialize_header(soc); 1992 } else { 1993 _dynamic_builtin_dictionary.serialize_header(soc); 1994 _dynamic_unregistered_dictionary.serialize_header(soc); 1995 _lambda_proxy_class_dictionary.serialize_header(soc); 1996 } 1997 } 1998 1999 void SystemDictionaryShared::serialize_well_known_klasses(SerializeClosure* soc) { 2000 for (int i = FIRST_WKID; i < WKID_LIMIT; i++) { 2001 soc->do_ptr((void**)&_well_known_klasses[i]); 2002 } 2003 } 2004 2005 const RunTimeSharedClassInfo* 2006 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) { 2007 if (!UseSharedSpaces || !name->is_shared()) { 2008 // The names of all shared classes must also be a shared Symbol. 2009 return NULL; 2010 } 2011 2012 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary(name); 2013 const RunTimeSharedClassInfo* record = NULL; 2014 if (!MetaspaceShared::is_shared_dynamic(name)) { 2015 // The names of all shared classes in the static dict must also be in the 2016 // static archive 2017 record = static_dict->lookup(name, hash, 0); 2018 } 2019 2020 if (record == NULL && DynamicArchive::is_mapped()) { 2021 record = dynamic_dict->lookup(name, hash, 0); 2022 } 2023 2024 return record; 2025 } 2026 2027 InstanceKlass* SystemDictionaryShared::find_builtin_class(Symbol* name) { 2028 const RunTimeSharedClassInfo* record = find_record(&_builtin_dictionary, &_dynamic_builtin_dictionary, name); 2029 if (record != NULL) { 2030 assert(!record->_klass->is_hidden(), "hidden class cannot be looked up by name"); 2031 return record->_klass; 2032 } else { 2033 return NULL; 2034 } 2035 } 2036 2037 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) { 2038 assert(DumpSharedSpaces, "supported only when dumping"); 2039 DumpTimeSharedClassInfo* info = find_or_allocate_info_for(k); 2040 info->_id = id; 2041 } 2042 2043 class SharedDictionaryPrinter : StackObj { 2044 outputStream* _st; 2045 int _index; 2046 public: 2047 SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {} 2048 2049 void do_value(const RunTimeSharedClassInfo* record) { 2050 ResourceMark rm; 2051 _st->print_cr("%4d: %s", (_index++), record->_klass->external_name()); 2052 } 2053 }; 2054 2055 class SharedLambdaDictionaryPrinter : StackObj { 2056 outputStream* _st; 2057 int _index; 2058 public: 2059 SharedLambdaDictionaryPrinter(outputStream* st) : _st(st), _index(0) {} 2060 2061 void do_value(const RunTimeLambdaProxyClassInfo* record) { 2062 ResourceMark rm; 2063 _st->print_cr("%4d: %s", (_index++), record->proxy_klass_head()->external_name()); 2064 Klass* k = record->proxy_klass_head()->next_link(); 2065 while (k != NULL) { 2066 _st->print_cr("%4d: %s", (_index++), k->external_name()); 2067 k = k->next_link(); 2068 } 2069 } 2070 }; 2071 2072 void SystemDictionaryShared::print_on(outputStream* st) { 2073 if (UseSharedSpaces) { 2074 st->print_cr("Shared Dictionary"); 2075 SharedDictionaryPrinter p(st); 2076 _builtin_dictionary.iterate(&p); 2077 _unregistered_dictionary.iterate(&p); 2078 if (DynamicArchive::is_mapped()) { 2079 _dynamic_builtin_dictionary.iterate(&p); 2080 _unregistered_dictionary.iterate(&p); 2081 if (!_lambda_proxy_class_dictionary.empty()) { 2082 st->print_cr("Shared Lambda Dictionary"); 2083 SharedLambdaDictionaryPrinter ldp(st); 2084 _lambda_proxy_class_dictionary.iterate(&ldp); 2085 } 2086 } 2087 } 2088 } 2089 2090 void SystemDictionaryShared::print_table_statistics(outputStream* st) { 2091 if (UseSharedSpaces) { 2092 _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary"); 2093 _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary"); 2094 if (DynamicArchive::is_mapped()) { 2095 _dynamic_builtin_dictionary.print_table_statistics(st, "Dynamic Builtin Shared Dictionary"); 2096 _dynamic_unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary"); 2097 _lambda_proxy_class_dictionary.print_table_statistics(st, "Lambda Shared Dictionary"); 2098 } 2099 } 2100 } 2101 2102 bool SystemDictionaryShared::empty_dumptime_table() { 2103 if (_dumptime_table == NULL) { 2104 return true; 2105 } 2106 _dumptime_table->update_counts(); 2107 if (_dumptime_table->count_of(true) == 0 && _dumptime_table->count_of(false) == 0){ 2108 return true; 2109 } 2110 return false; 2111 } 2112 2113 #if INCLUDE_CDS_JAVA_HEAP 2114 2115 class ArchivedMirrorPatcher { 2116 static void update(Klass* k) { 2117 if (k->has_raw_archived_mirror()) { 2118 oop m = HeapShared::materialize_archived_object(k->archived_java_mirror_raw_narrow()); 2119 if (m != NULL) { 2120 java_lang_Class::update_archived_mirror_native_pointers(m); 2121 } 2122 } 2123 } 2124 2125 public: 2126 static void update_array_klasses(Klass* ak) { 2127 while (ak != NULL) { 2128 update(ak); 2129 ak = ArrayKlass::cast(ak)->higher_dimension(); 2130 } 2131 } 2132 2133 void do_value(const RunTimeSharedClassInfo* info) { 2134 InstanceKlass* ik = info->_klass; 2135 update(ik); 2136 update_array_klasses(ik->array_klasses()); 2137 } 2138 }; 2139 2140 void SystemDictionaryShared::update_archived_mirror_native_pointers_for(RunTimeSharedDictionary* dict) { 2141 ArchivedMirrorPatcher patcher; 2142 dict->iterate(&patcher); 2143 } 2144 2145 void SystemDictionaryShared::update_archived_mirror_native_pointers() { 2146 if (!HeapShared::open_archive_heap_region_mapped()) { 2147 return; 2148 } 2149 if (MetaspaceShared::relocation_delta() == 0) { 2150 return; 2151 } 2152 update_archived_mirror_native_pointers_for(&_builtin_dictionary); 2153 update_archived_mirror_native_pointers_for(&_unregistered_dictionary); 2154 2155 for (int t = T_BOOLEAN; t <= T_LONG; t++) { 2156 Klass* k = Universe::typeArrayKlassObj((BasicType)t); 2157 ArchivedMirrorPatcher::update_array_klasses(k); 2158 } 2159 } 2160 #endif