< prev index next >

src/hotspot/share/classfile/systemDictionary.cpp

Print this page

  39 #include "classfile/packageEntry.hpp"
  40 #include "classfile/placeholders.hpp"
  41 #include "classfile/protectionDomainCache.hpp"
  42 #include "classfile/resolutionErrors.hpp"
  43 #include "classfile/stringTable.hpp"
  44 #include "classfile/symbolTable.hpp"
  45 #include "classfile/systemDictionary.hpp"
  46 #include "classfile/vmClasses.hpp"
  47 #include "classfile/vmSymbols.hpp"
  48 #include "gc/shared/gcTraceTime.inline.hpp"
  49 #include "interpreter/bootstrapInfo.hpp"
  50 #include "jfr/jfrEvents.hpp"
  51 #include "jvm.h"
  52 #include "logging/log.hpp"
  53 #include "logging/logStream.hpp"
  54 #include "memory/metaspaceClosure.hpp"
  55 #include "memory/oopFactory.hpp"
  56 #include "memory/resourceArea.hpp"
  57 #include "memory/universe.hpp"
  58 #include "oops/access.inline.hpp"

  59 #include "oops/instanceKlass.hpp"
  60 #include "oops/klass.inline.hpp"
  61 #include "oops/method.inline.hpp"
  62 #include "oops/objArrayKlass.hpp"
  63 #include "oops/objArrayOop.inline.hpp"
  64 #include "oops/oop.inline.hpp"
  65 #include "oops/oop.hpp"
  66 #include "oops/oopHandle.hpp"
  67 #include "oops/oopHandle.inline.hpp"
  68 #include "oops/symbol.hpp"
  69 #include "oops/typeArrayKlass.hpp"

  70 #include "prims/jvmtiExport.hpp"
  71 #include "prims/methodHandles.hpp"
  72 #include "runtime/arguments.hpp"
  73 #include "runtime/atomic.hpp"
  74 #include "runtime/handles.inline.hpp"
  75 #include "runtime/java.hpp"
  76 #include "runtime/javaCalls.hpp"
  77 #include "runtime/mutexLocker.hpp"

  78 #include "runtime/sharedRuntime.hpp"
  79 #include "runtime/signature.hpp"
  80 #include "runtime/synchronizer.hpp"
  81 #include "services/classLoadingService.hpp"
  82 #include "services/diagnosticCommand.hpp"
  83 #include "services/finalizerService.hpp"
  84 #include "services/threadService.hpp"
  85 #include "utilities/macros.hpp"
  86 #include "utilities/utf8.hpp"
  87 #if INCLUDE_CDS
  88 #include "classfile/systemDictionaryShared.hpp"
  89 #endif
  90 #if INCLUDE_JFR
  91 #include "jfr/jfr.hpp"
  92 #endif
  93 
  94 class InvokeMethodKey : public StackObj {
  95   private:
  96     Symbol* _symbol;
  97     intptr_t _iid;

 318 
 319 Klass* SystemDictionary::resolve_or_fail(Symbol* class_name, Handle class_loader, Handle protection_domain,
 320                                          bool throw_error, TRAPS) {
 321   Klass* klass = resolve_or_null(class_name, class_loader, protection_domain, THREAD);
 322   // Check for pending exception or null klass, and throw exception
 323   if (HAS_PENDING_EXCEPTION || klass == nullptr) {
 324     handle_resolution_exception(class_name, throw_error, CHECK_NULL);
 325   }
 326   return klass;
 327 }
 328 
 329 // Forwards to resolve_array_class_or_null or resolve_instance_class_or_null
 330 
 331 Klass* SystemDictionary::resolve_or_null(Symbol* class_name, Handle class_loader, Handle protection_domain, TRAPS) {
 332   if (Signature::is_array(class_name)) {
 333     return resolve_array_class_or_null(class_name, class_loader, protection_domain, THREAD);
 334   } else {
 335     assert(class_name != nullptr && !Signature::is_array(class_name), "must be");
 336     if (Signature::has_envelope(class_name)) {
 337       ResourceMark rm(THREAD);
 338       // Ignore wrapping L and ;.
 339       TempNewSymbol name = SymbolTable::new_symbol(class_name->as_C_string() + 1,
 340                                                    class_name->utf8_length() - 2);
 341       return resolve_instance_class_or_null(name, class_loader, protection_domain, THREAD);
 342     } else {
 343       return resolve_instance_class_or_null(class_name, class_loader, protection_domain, THREAD);
 344     }
 345   }
 346 }
 347 
 348 // Forwards to resolve_instance_class_or_null
 349 
 350 Klass* SystemDictionary::resolve_array_class_or_null(Symbol* class_name,
 351                                                      Handle class_loader,
 352                                                      Handle protection_domain,
 353                                                      TRAPS) {
 354   assert(Signature::is_array(class_name), "must be array");
 355   ResourceMark rm(THREAD);
 356   SignatureStream ss(class_name, false);
 357   int ndims = ss.skip_array_prefix();  // skip all '['s
 358   Klass* k = nullptr;

 382     probe->print_on(&ls);
 383     ls.cr();
 384   }
 385 }
 386 
 387 // Must be called for any superclass or superinterface resolution
 388 // during class definition to allow class circularity checking
 389 // superinterface callers:
 390 //    parse_interfaces - from defineClass
 391 // superclass callers:
 392 //   ClassFileParser - from defineClass
 393 //   load_shared_class - while loading a class from shared archive
 394 //   resolve_instance_class_or_null:
 395 //     via: handle_parallel_super_load
 396 //      when resolving a class that has an existing placeholder with
 397 //      a saved superclass [i.e. a defineClass is currently in progress]
 398 //      If another thread is trying to resolve the class, it must do
 399 //      superclass checks on its own thread to catch class circularity and
 400 //      to avoid deadlock.
 401 //
 402 // resolve_super_or_fail adds a LOAD_SUPER placeholder to the placeholder table before calling
 403 // resolve_instance_class_or_null. ClassCircularityError is detected when a LOAD_SUPER or LOAD_INSTANCE
 404 // placeholder for the same thread, class, classloader is found.
 405 // This can be seen with logging option: -Xlog:class+load+placeholders=debug.
 406 //
 407 InstanceKlass* SystemDictionary::resolve_super_or_fail(Symbol* class_name,
 408                                                        Symbol* super_name,
 409                                                        Handle class_loader,
 410                                                        Handle protection_domain,
 411                                                        bool is_superclass,
 412                                                        TRAPS) {
 413 
 414   assert(super_name != nullptr, "null superclass for resolving");
 415   assert(!Signature::is_array(super_name), "invalid superclass name");
 416 #if INCLUDE_CDS
 417   if (CDSConfig::is_dumping_static_archive()) {
 418     // Special processing for handling UNREGISTERED shared classes.
 419     InstanceKlass* k = SystemDictionaryShared::lookup_super_for_unregistered_class(class_name,
 420                            super_name, is_superclass);
 421     if (k) {
 422       return k;
 423     }
 424   }
 425 #endif // INCLUDE_CDS
 426 
 427   // If klass is already loaded, just return the superclass or superinterface.
 428   // Make sure there's a placeholder for the class_name before resolving.
 429   // This is used as a claim that this thread is currently loading superclass/classloader
 430   // and for ClassCircularity checks.
 431 
 432   ClassLoaderData* loader_data = class_loader_data(class_loader);
 433   Dictionary* dictionary = loader_data->dictionary();
 434 
 435   // can't throw error holding a lock
 436   bool throw_circularity_error = false;
 437   {
 438     MutexLocker mu(THREAD, SystemDictionary_lock);
 439     InstanceKlass* klassk = dictionary->find_class(THREAD, class_name);
 440     InstanceKlass* quicksuperk;
 441     // To support parallel loading: if class is done loading, just return the superclass
 442     // if the super_name matches class->super()->name() and if the class loaders match.
 443     // Otherwise, a LinkageError will be thrown later.
 444     if (klassk != nullptr && is_superclass &&
 445         ((quicksuperk = klassk->java_super()) != nullptr) &&
 446          ((quicksuperk->name() == super_name) &&
 447             (quicksuperk->class_loader() == class_loader()))) {
 448            return quicksuperk;
 449     } else {
 450       // Must check ClassCircularity before checking if superclass is already loaded.
 451       PlaceholderEntry* probe = PlaceholderTable::get_entry(class_name, loader_data);
 452       if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER)) {
 453           log_circularity_error(class_name, probe);
 454           throw_circularity_error = true;
 455       }
 456     }
 457 
 458     if (!throw_circularity_error) {
 459       // Be careful not to exit resolve_super without removing this placeholder.
 460       PlaceholderEntry* newprobe = PlaceholderTable::find_and_add(class_name,
 461                                                                   loader_data,
 462                                                                   PlaceholderTable::LOAD_SUPER,
 463                                                                   super_name, THREAD);
 464     }
 465   }
 466 
 467   if (throw_circularity_error) {
 468       ResourceMark rm(THREAD);
 469       THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), class_name->as_C_string());
 470   }
 471 
 472   // Resolve the superclass or superinterface, check results on return
 473   InstanceKlass* superk =
 474     SystemDictionary::resolve_instance_class_or_null(super_name,
 475                                                      class_loader,
 476                                                      protection_domain,
 477                                                      THREAD);
 478 
 479   // Clean up placeholder entry.
 480   {
 481     MutexLocker mu(THREAD, SystemDictionary_lock);
 482     PlaceholderTable::find_and_remove(class_name, loader_data, PlaceholderTable::LOAD_SUPER, THREAD);
 483     SystemDictionary_lock->notify_all();
 484   }
 485 
 486   // Check for pending exception or null superk, and throw exception
 487   if (HAS_PENDING_EXCEPTION || superk == nullptr) {
 488     handle_resolution_exception(super_name, true, CHECK_NULL);
 489   }
 490 
 491   return superk;
 492 }
 493 
 494 // If the class in is in the placeholder table, class loading is in progress.
 495 // For cases where the application changes threads to load classes, it
 496 // is critical to ClassCircularity detection that we try loading
 497 // the superclass on the new thread internally, so we do parallel
 498 // superclass loading here.  This avoids deadlock for ClassCircularity
 499 // detection for parallelCapable class loaders that lock on a per-class lock.
 500 static void handle_parallel_super_load(Symbol* name,
 501                                        Symbol* superclassname,
 502                                        Handle class_loader,
 503                                        Handle protection_domain, TRAPS) {
 504 
 505   // superk is not used; resolve_super_or_fail is called for circularity check only.
 506   Klass* superk = SystemDictionary::resolve_super_or_fail(name,
 507                                                           superclassname,
 508                                                           class_loader,
 509                                                           protection_domain,
 510                                                           true,
 511                                                           CHECK);
 512 }
 513 
 514 // Bootstrap and non-parallel capable class loaders use the LOAD_INSTANCE placeholder to
 515 // wait for parallel class loading and/or to check for circularity error for Xcomp when loading.
 516 static bool needs_load_placeholder(Handle class_loader) {
 517   return class_loader.is_null() || !is_parallelCapable(class_loader);
 518 }
 519 
 520 // Check for other threads loading this class either to throw CCE or wait in the case of the boot loader.
 521 static InstanceKlass* handle_parallel_loading(JavaThread* current,
 522                                               Symbol* name,
 523                                               ClassLoaderData* loader_data,
 524                                               bool must_wait_for_class_loading,
 525                                               bool* throw_circularity_error) {
 526   PlaceholderEntry* oldprobe = PlaceholderTable::get_entry(name, loader_data);
 527   if (oldprobe != nullptr) {
 528     // -Xcomp calls load_signature_classes which might result in loading
 529     // a class that's already in the process of loading, so we detect CCE here also.
 530     // Only need check_seen_thread once, not on each loop
 531     if (oldprobe->check_seen_thread(current, PlaceholderTable::LOAD_INSTANCE)) {
 532       log_circularity_error(name, oldprobe);
 533       *throw_circularity_error = true;
 534       return nullptr;
 535     } else if (must_wait_for_class_loading) {
 536       // Wait until the first thread has finished loading this class. Also wait until all the
 537       // threads trying to load its superclass have removed their placeholders.
 538       while (oldprobe != nullptr &&
 539              (oldprobe->instance_load_in_progress() || oldprobe->super_load_in_progress())) {
 540 
 541         // LOAD_INSTANCE placeholders are used to implement parallel capable class loading
 542         // for the bootclass loader.
 543         SystemDictionary_lock->wait();
 544 
 545         // Check if classloading completed while we were waiting
 546         InstanceKlass* check = loader_data->dictionary()->find_class(current, name);
 547         if (check != nullptr) {
 548           // Klass is already loaded, so just return it
 549           return check;
 550         }
 551         // check if other thread failed to load and cleaned up
 552         oldprobe = PlaceholderTable::get_entry(name, loader_data);
 553       }
 554     }
 555   }
 556   return nullptr;
 557 }
 558 
 559 void SystemDictionary::post_class_load_event(EventClassLoad* event, const InstanceKlass* k, const ClassLoaderData* init_cld) {
 560   assert(event != nullptr, "invariant");
 561   assert(k != nullptr, "invariant");
 562   event->set_loadedClass(k);
 563   event->set_definingClassLoader(k->class_loader_data());
 564   event->set_initiatingClassLoader(init_cld);
 565   event->commit();
 566 }
 567 
 568 // SystemDictionary::resolve_instance_class_or_null is the main function for class name resolution.
 569 // After checking if the InstanceKlass already exists, it checks for ClassCircularityError and
 570 // whether the thread must wait for loading in parallel.  It eventually calls load_instance_class,
 571 // which will load the class via the bootstrap loader or call ClassLoader.loadClass().
 572 // This can return null, an exception or an InstanceKlass.
 573 InstanceKlass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
 574                                                                 Handle class_loader,
 575                                                                 Handle protection_domain,
 576                                                                 TRAPS) {
 577   // name must be in the form of "java/lang/Object" -- cannot be "Ljava/lang/Object;"
 578   assert(name != nullptr && !Signature::is_array(name) &&
 579          !Signature::has_envelope(name), "invalid class name");
 580 
 581   EventClassLoad class_load_start_event;
 582 
 583   HandleMark hm(THREAD);
 584 
 585   // Fix for 4474172; see evaluation for more details
 586   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
 587   ClassLoaderData* loader_data = register_loader(class_loader);
 588   Dictionary* dictionary = loader_data->dictionary();
 589 
 590   // Do lookup to see if class already exists and the protection domain
 591   // has the right access.
 592   // This call uses find which checks protection domain already matches
 593   // All subsequent calls use find_class, and set loaded_class so that
 594   // before we return a result, we call out to java to check for valid protection domain.
 595   InstanceKlass* probe = dictionary->find(THREAD, name, protection_domain);
 596   if (probe != nullptr) return probe;
 597 
 598   // Non-bootstrap class loaders will call out to class loader and
 599   // define via jvm/jni_DefineClass which will acquire the
 600   // class loader object lock to protect against multiple threads
 601   // defining the class in parallel by accident.
 602   // This lock must be acquired here so the waiter will find
 603   // any successful result in the SystemDictionary and not attempt
 604   // the define.
 605   // ParallelCapable class loaders and the bootstrap classloader
 606   // do not acquire lock here.
 607   Handle lockObject = get_loader_lock_or_null(class_loader);
 608   ObjectLocker ol(lockObject, THREAD);
 609 
 610   bool super_load_in_progress  = false;
 611   InstanceKlass* loaded_class = nullptr;
 612   SymbolHandle superclassname; // Keep alive while loading in parallel thread.
 613 
 614   guarantee(THREAD->can_call_java(),
 615          "can not load classes with compiler thread: class=%s, classloader=%s",
 616          name->as_C_string(),
 617          class_loader.is_null() ? "null" : class_loader->klass()->name()->as_C_string());
 618 
 619   // Check again (after locking) if the class already exists in SystemDictionary
 620   {
 621     MutexLocker mu(THREAD, SystemDictionary_lock);
 622     InstanceKlass* check = dictionary->find_class(THREAD, name);
 623     if (check != nullptr) {
 624       // InstanceKlass is already loaded, but we still need to check protection domain below.
 625       loaded_class = check;
 626     } else {
 627       PlaceholderEntry* placeholder = PlaceholderTable::get_entry(name, loader_data);
 628       if (placeholder != nullptr && placeholder->super_load_in_progress()) {
 629          super_load_in_progress = true;
 630          superclassname = placeholder->supername();
 631          assert(superclassname != nullptr, "superclass has to have a name");
 632       }
 633     }
 634   }
 635 
 636   // If the class is in the placeholder table with super_class set,
 637   // handle superclass loading in progress.
 638   if (super_load_in_progress) {
 639     handle_parallel_super_load(name, superclassname,
 640                                class_loader,
 641                                protection_domain,
 642                                CHECK_NULL);
 643   }
 644 
 645   bool throw_circularity_error = false;
 646   if (loaded_class == nullptr) {
 647     bool load_placeholder_added = false;
 648 
 649     // Add placeholder entry to record loading instance class
 650     // case 1. Bootstrap classloader
 651     //    This classloader supports parallelism at the classloader level
 652     //    but only allows a single thread to load a class/classloader pair.
 653     //    The LOAD_INSTANCE placeholder is the mechanism for mutual exclusion.
 654     // case 2. parallelCapable user level classloaders
 655     //    These class loaders lock a per-class object lock when ClassLoader.loadClass()
 656     //    is called. A LOAD_INSTANCE placeholder isn't used for mutual exclusion.
 657     // case 3. traditional classloaders that rely on the classloader object lock
 658     //    There should be no need for need for LOAD_INSTANCE for mutual exclusion,

1035 }
1036 
1037 bool SystemDictionary::check_shared_class_super_type(InstanceKlass* klass, InstanceKlass* super_type,
1038                                                      Handle class_loader,  Handle protection_domain,
1039                                                      bool is_superclass, TRAPS) {
1040   assert(super_type->is_shared(), "must be");
1041 
1042   // Quick check if the super type has been already loaded.
1043   // + Don't do it for unregistered classes -- they can be unloaded so
1044   //   super_type->class_loader_data() could be stale.
1045   // + Don't check if loader data is null, ie. the super_type isn't fully loaded.
1046   if (!super_type->is_shared_unregistered_class() && super_type->class_loader_data() != nullptr) {
1047     // Check if the superclass is loaded by the current class_loader
1048     Symbol* name = super_type->name();
1049     InstanceKlass* check = find_instance_klass(THREAD, name, class_loader, protection_domain);
1050     if (check == super_type) {
1051       return true;
1052     }
1053   }
1054 
1055   Klass *found = resolve_super_or_fail(klass->name(), super_type->name(),
1056                                        class_loader, protection_domain, is_superclass, CHECK_0);
1057   if (found == super_type) {
1058     return true;
1059   } else {
1060     // The dynamically resolved super type is not the same as the one we used during dump time,
1061     // so we cannot use the class.
1062     return false;
1063   }
1064 }
1065 
1066 bool SystemDictionary::check_shared_class_super_types(InstanceKlass* ik, Handle class_loader,
1067                                                       Handle protection_domain, TRAPS) {
1068   // Check the superclass and interfaces. They must be the same
1069   // as in dump time, because the layout of <ik> depends on
1070   // the specific layout of ik->super() and ik->local_interfaces().
1071   //
1072   // If unexpected superclass or interfaces are found, we cannot
1073   // load <ik> from the shared archive.
1074 
1075   if (ik->super() != nullptr &&

1124 
1125 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1126                                                    Handle class_loader,
1127                                                    Handle protection_domain,
1128                                                    const ClassFileStream *cfs,
1129                                                    PackageEntry* pkg_entry,
1130                                                    TRAPS) {
1131   assert(ik != nullptr, "sanity");
1132   assert(!ik->is_unshareable_info_restored(), "shared class can be restored only once");
1133   assert(Atomic::add(&ik->_shared_class_load_count, 1) == 1, "shared class loaded more than once");
1134   Symbol* class_name = ik->name();
1135 
1136   if (!is_shared_class_visible(class_name, ik, pkg_entry, class_loader)) {
1137     return nullptr;
1138   }
1139 
1140   if (!check_shared_class_super_types(ik, class_loader, protection_domain, THREAD)) {
1141     return nullptr;
1142   }
1143 
































1144   InstanceKlass* new_ik = nullptr;
1145   // CFLH check is skipped for VM hidden classes (see KlassFactory::create_from_stream).
1146   // It will be skipped for shared VM hidden lambda proxy classes.
1147   if (!SystemDictionaryShared::is_hidden_lambda_proxy(ik)) {
1148     new_ik = KlassFactory::check_shared_class_file_load_hook(
1149       ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1150   }
1151   if (new_ik != nullptr) {
1152     // The class is changed by CFLH. Return the new class. The shared class is
1153     // not used.
1154     return new_ik;
1155   }
1156 
1157   // Adjust methods to recover missing data.  They need addresses for
1158   // interpreter entry points and their default native method address
1159   // must be reset.
1160 
1161   // Shared classes are all currently loaded by either the bootstrap or
1162   // internal parallel class loaders, so this will never cause a deadlock
1163   // on a custom class loader lock.
1164   // Since this class is already locked with parallel capable class
1165   // loaders, including the bootstrap loader via the placeholder table,
1166   // this lock is currently a nop.
1167 
1168   ClassLoaderData* loader_data = class_loader_data(class_loader);
1169   {
1170     HandleMark hm(THREAD);
1171     Handle lockObject = get_loader_lock_or_null(class_loader);
1172     ObjectLocker ol(lockObject, THREAD);
1173     // prohibited package check assumes all classes loaded from archive call
1174     // restore_unshareable_info which calls ik->set_package()
1175     ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1176   }
1177 
1178   load_shared_class_misc(ik, loader_data);

1179   return ik;
1180 }
1181 
1182 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1183   ik->print_class_load_logging(loader_data, nullptr, nullptr);
1184 
1185   // For boot loader, ensure that GetSystemPackage knows that a class in this
1186   // package was loaded.
1187   if (loader_data->is_the_null_class_loader_data()) {
1188     s2 path_index = ik->shared_classpath_index();
1189     ik->set_classpath_index(path_index);
1190   }
1191 
1192   // notify a class loaded from shared object
1193   ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1194 }
1195 
1196 #endif // INCLUDE_CDS
1197 
1198 InstanceKlass* SystemDictionary::load_instance_class_impl(Symbol* class_name, Handle class_loader, TRAPS) {

  39 #include "classfile/packageEntry.hpp"
  40 #include "classfile/placeholders.hpp"
  41 #include "classfile/protectionDomainCache.hpp"
  42 #include "classfile/resolutionErrors.hpp"
  43 #include "classfile/stringTable.hpp"
  44 #include "classfile/symbolTable.hpp"
  45 #include "classfile/systemDictionary.hpp"
  46 #include "classfile/vmClasses.hpp"
  47 #include "classfile/vmSymbols.hpp"
  48 #include "gc/shared/gcTraceTime.inline.hpp"
  49 #include "interpreter/bootstrapInfo.hpp"
  50 #include "jfr/jfrEvents.hpp"
  51 #include "jvm.h"
  52 #include "logging/log.hpp"
  53 #include "logging/logStream.hpp"
  54 #include "memory/metaspaceClosure.hpp"
  55 #include "memory/oopFactory.hpp"
  56 #include "memory/resourceArea.hpp"
  57 #include "memory/universe.hpp"
  58 #include "oops/access.inline.hpp"
  59 #include "oops/fieldStreams.inline.hpp"
  60 #include "oops/instanceKlass.hpp"
  61 #include "oops/klass.inline.hpp"
  62 #include "oops/method.inline.hpp"
  63 #include "oops/objArrayKlass.hpp"
  64 #include "oops/objArrayOop.inline.hpp"
  65 #include "oops/oop.inline.hpp"
  66 #include "oops/oop.hpp"
  67 #include "oops/oopHandle.hpp"
  68 #include "oops/oopHandle.inline.hpp"
  69 #include "oops/symbol.hpp"
  70 #include "oops/typeArrayKlass.hpp"
  71 #include "oops/inlineKlass.inline.hpp"
  72 #include "prims/jvmtiExport.hpp"
  73 #include "prims/methodHandles.hpp"
  74 #include "runtime/arguments.hpp"
  75 #include "runtime/atomic.hpp"
  76 #include "runtime/handles.inline.hpp"
  77 #include "runtime/java.hpp"
  78 #include "runtime/javaCalls.hpp"
  79 #include "runtime/mutexLocker.hpp"
  80 #include "runtime/os.hpp"
  81 #include "runtime/sharedRuntime.hpp"
  82 #include "runtime/signature.hpp"
  83 #include "runtime/synchronizer.hpp"
  84 #include "services/classLoadingService.hpp"
  85 #include "services/diagnosticCommand.hpp"
  86 #include "services/finalizerService.hpp"
  87 #include "services/threadService.hpp"
  88 #include "utilities/macros.hpp"
  89 #include "utilities/utf8.hpp"
  90 #if INCLUDE_CDS
  91 #include "classfile/systemDictionaryShared.hpp"
  92 #endif
  93 #if INCLUDE_JFR
  94 #include "jfr/jfr.hpp"
  95 #endif
  96 
  97 class InvokeMethodKey : public StackObj {
  98   private:
  99     Symbol* _symbol;
 100     intptr_t _iid;

 321 
 322 Klass* SystemDictionary::resolve_or_fail(Symbol* class_name, Handle class_loader, Handle protection_domain,
 323                                          bool throw_error, TRAPS) {
 324   Klass* klass = resolve_or_null(class_name, class_loader, protection_domain, THREAD);
 325   // Check for pending exception or null klass, and throw exception
 326   if (HAS_PENDING_EXCEPTION || klass == nullptr) {
 327     handle_resolution_exception(class_name, throw_error, CHECK_NULL);
 328   }
 329   return klass;
 330 }
 331 
 332 // Forwards to resolve_array_class_or_null or resolve_instance_class_or_null
 333 
 334 Klass* SystemDictionary::resolve_or_null(Symbol* class_name, Handle class_loader, Handle protection_domain, TRAPS) {
 335   if (Signature::is_array(class_name)) {
 336     return resolve_array_class_or_null(class_name, class_loader, protection_domain, THREAD);
 337   } else {
 338     assert(class_name != nullptr && !Signature::is_array(class_name), "must be");
 339     if (Signature::has_envelope(class_name)) {
 340       ResourceMark rm(THREAD);
 341       // Ignore wrapping L and ; (and Q and ; for value types).
 342       TempNewSymbol name = SymbolTable::new_symbol(class_name->as_C_string() + 1,
 343                                                    class_name->utf8_length() - 2);
 344       return resolve_instance_class_or_null(name, class_loader, protection_domain, THREAD);
 345     } else {
 346       return resolve_instance_class_or_null(class_name, class_loader, protection_domain, THREAD);
 347     }
 348   }
 349 }
 350 
 351 // Forwards to resolve_instance_class_or_null
 352 
 353 Klass* SystemDictionary::resolve_array_class_or_null(Symbol* class_name,
 354                                                      Handle class_loader,
 355                                                      Handle protection_domain,
 356                                                      TRAPS) {
 357   assert(Signature::is_array(class_name), "must be array");
 358   ResourceMark rm(THREAD);
 359   SignatureStream ss(class_name, false);
 360   int ndims = ss.skip_array_prefix();  // skip all '['s
 361   Klass* k = nullptr;

 385     probe->print_on(&ls);
 386     ls.cr();
 387   }
 388 }
 389 
 390 // Must be called for any superclass or superinterface resolution
 391 // during class definition to allow class circularity checking
 392 // superinterface callers:
 393 //    parse_interfaces - from defineClass
 394 // superclass callers:
 395 //   ClassFileParser - from defineClass
 396 //   load_shared_class - while loading a class from shared archive
 397 //   resolve_instance_class_or_null:
 398 //     via: handle_parallel_super_load
 399 //      when resolving a class that has an existing placeholder with
 400 //      a saved superclass [i.e. a defineClass is currently in progress]
 401 //      If another thread is trying to resolve the class, it must do
 402 //      superclass checks on its own thread to catch class circularity and
 403 //      to avoid deadlock.
 404 //
 405 // resolve_with_circularity_detection_or_fail adds a DETECT_CIRCULARITY placeholder to the placeholder table before calling
 406 // resolve_instance_class_or_null. ClassCircularityError is detected when a DETECT_CIRCULARITY or LOAD_INSTANCE
 407 // placeholder for the same thread, class, classloader is found.
 408 // This can be seen with logging option: -Xlog:class+load+placeholders=debug.
 409 //
 410 InstanceKlass* SystemDictionary::resolve_with_circularity_detection_or_fail(Symbol* class_name,
 411                                                        Symbol* next_name,
 412                                                        Handle class_loader,
 413                                                        Handle protection_domain,
 414                                                        bool is_superclass,
 415                                                        TRAPS) {
 416 
 417   assert(next_name != nullptr, "null superclass for resolving");
 418   assert(!Signature::is_array(next_name), "invalid superclass name");
 419 #if INCLUDE_CDS
 420   if (CDSConfig::is_dumping_static_archive()) {
 421     // Special processing for handling UNREGISTERED shared classes.
 422     InstanceKlass* k = SystemDictionaryShared::lookup_super_for_unregistered_class(class_name,
 423                            next_name, is_superclass);
 424     if (k) {
 425       return k;
 426     }
 427   }
 428 #endif // INCLUDE_CDS
 429 
 430   // If klass is already loaded, just return the superclass or superinterface.
 431   // Make sure there's a placeholder for the class_name before resolving.
 432   // This is used as a claim that this thread is currently loading superclass/classloader
 433   // and for ClassCircularity checks.
 434 
 435   ClassLoaderData* loader_data = class_loader_data(class_loader);
 436   Dictionary* dictionary = loader_data->dictionary();
 437 
 438   // can't throw error holding a lock
 439   bool throw_circularity_error = false;
 440   {
 441     MutexLocker mu(THREAD, SystemDictionary_lock);
 442     InstanceKlass* klassk = dictionary->find_class(THREAD, class_name);
 443     InstanceKlass* quicksuperk;
 444     // To support parallel loading: if class is done loading, just return the superclass
 445     // if the next_name matches class->super()->name() and if the class loaders match.
 446     // Otherwise, a LinkageError will be thrown later.
 447     if (klassk != nullptr && is_superclass &&
 448         ((quicksuperk = klassk->java_super()) != nullptr) &&
 449          ((quicksuperk->name() == next_name) &&
 450             (quicksuperk->class_loader() == class_loader()))) {
 451            return quicksuperk;
 452     } else {
 453       // Must check ClassCircularity before checking if superclass is already loaded.
 454       PlaceholderEntry* probe = PlaceholderTable::get_entry(class_name, loader_data);
 455       if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::DETECT_CIRCULARITY)) {
 456           log_circularity_error(class_name, probe);
 457           throw_circularity_error = true;
 458       }
 459     }
 460 
 461     if (!throw_circularity_error) {
 462       // Be careful not to exit resolve_super without removing this placeholder.
 463       PlaceholderEntry* newprobe = PlaceholderTable::find_and_add(class_name,
 464                                                                   loader_data,
 465                                                                   PlaceholderTable::DETECT_CIRCULARITY,
 466                                                                   next_name, THREAD);
 467     }
 468   }
 469 
 470   if (throw_circularity_error) {
 471       ResourceMark rm(THREAD);
 472       THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), class_name->as_C_string());
 473   }
 474 
 475   // Resolve the superclass or superinterface, check results on return
 476   InstanceKlass* superk =
 477     SystemDictionary::resolve_instance_class_or_null(next_name,
 478                                                      class_loader,
 479                                                      protection_domain,
 480                                                      THREAD);
 481 
 482   // Clean up placeholder entry.
 483   {
 484     MutexLocker mu(THREAD, SystemDictionary_lock);
 485     PlaceholderTable::find_and_remove(class_name, loader_data, PlaceholderTable::DETECT_CIRCULARITY, THREAD);
 486     SystemDictionary_lock->notify_all();
 487   }
 488 
 489   // Check for pending exception or null superk, and throw exception
 490   if (HAS_PENDING_EXCEPTION || superk == nullptr) {
 491     handle_resolution_exception(next_name, true, CHECK_NULL);
 492   }
 493 
 494   return superk;
 495 }
 496 
 497 // If the class in is in the placeholder table, class loading is in progress.
 498 // For cases where the application changes threads to load classes, it
 499 // is critical to ClassCircularity detection that we try loading
 500 // the superclass on the new thread internally, so we do parallel
 501 // superclass loading here.  This avoids deadlock for ClassCircularity
 502 // detection for parallelCapable class loaders that lock on a per-class lock.
 503 static void handle_parallel_super_load(Symbol* name,
 504                                        Symbol* superclassname,
 505                                        Handle class_loader,
 506                                        Handle protection_domain, TRAPS) {
 507 
 508   // superk is not used; resolve_super_or_fail is called for circularity check only.
 509   Klass* superk = SystemDictionary::resolve_with_circularity_detection_or_fail(name,
 510                                                           superclassname,
 511                                                           class_loader,
 512                                                           protection_domain,
 513                                                           false,
 514                                                           CHECK);
 515 }
 516 
 517 // Bootstrap and non-parallel capable class loaders use the LOAD_INSTANCE placeholder to
 518 // wait for parallel class loading and/or to check for circularity error for Xcomp when loading.
 519 static bool needs_load_placeholder(Handle class_loader) {
 520   return class_loader.is_null() || !is_parallelCapable(class_loader);
 521 }
 522 
 523 // Check for other threads loading this class either to throw CCE or wait in the case of the boot loader.
 524 static InstanceKlass* handle_parallel_loading(JavaThread* current,
 525                                               Symbol* name,
 526                                               ClassLoaderData* loader_data,
 527                                               bool must_wait_for_class_loading,
 528                                               bool* throw_circularity_error) {
 529   PlaceholderEntry* oldprobe = PlaceholderTable::get_entry(name, loader_data);
 530   if (oldprobe != nullptr) {
 531     // -Xcomp calls load_signature_classes which might result in loading
 532     // a class that's already in the process of loading, so we detect CCE here also.
 533     // Only need check_seen_thread once, not on each loop
 534     if (oldprobe->check_seen_thread(current, PlaceholderTable::LOAD_INSTANCE)) {
 535       log_circularity_error(name, oldprobe);
 536       *throw_circularity_error = true;
 537       return nullptr;
 538     } else if (must_wait_for_class_loading) {
 539       // Wait until the first thread has finished loading this class. Also wait until all the
 540       // threads trying to load its superclass have removed their placeholders.
 541       while (oldprobe != nullptr &&
 542              (oldprobe->instance_load_in_progress() || oldprobe->circularity_detection_in_progress())) {
 543 
 544         // LOAD_INSTANCE placeholders are used to implement parallel capable class loading
 545         // for the bootclass loader.
 546         SystemDictionary_lock->wait();
 547 
 548         // Check if classloading completed while we were waiting
 549         InstanceKlass* check = loader_data->dictionary()->find_class(current, name);
 550         if (check != nullptr) {
 551           // Klass is already loaded, so just return it
 552           return check;
 553         }
 554         // check if other thread failed to load and cleaned up
 555         oldprobe = PlaceholderTable::get_entry(name, loader_data);
 556       }
 557     }
 558   }
 559   return nullptr;
 560 }
 561 
 562 void SystemDictionary::post_class_load_event(EventClassLoad* event, const InstanceKlass* k, const ClassLoaderData* init_cld) {
 563   assert(event != nullptr, "invariant");
 564   assert(k != nullptr, "invariant");
 565   event->set_loadedClass(k);
 566   event->set_definingClassLoader(k->class_loader_data());
 567   event->set_initiatingClassLoader(init_cld);
 568   event->commit();
 569 }
 570 
 571 // SystemDictionary::resolve_instance_class_or_null is the main function for class name resolution.
 572 // After checking if the InstanceKlass already exists, it checks for ClassCircularityError and
 573 // whether the thread must wait for loading in parallel.  It eventually calls load_instance_class,
 574 // which will load the class via the bootstrap loader or call ClassLoader.loadClass().
 575 // This can return null, an exception or an InstanceKlass.
 576 InstanceKlass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
 577                                                                 Handle class_loader,
 578                                                                 Handle protection_domain,
 579                                                                 TRAPS) {
 580   // name must be in the form of "java/lang/Object" -- cannot be "Ljava/lang/Object;"
 581   assert(name != nullptr && !Signature::is_array(name) &&
 582          !Signature::has_envelope(name), "invalid class name: %s", name == nullptr ? "nullptr" : name->as_C_string());
 583 
 584   EventClassLoad class_load_start_event;
 585 
 586   HandleMark hm(THREAD);
 587 
 588   // Fix for 4474172; see evaluation for more details
 589   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
 590   ClassLoaderData* loader_data = register_loader(class_loader);
 591   Dictionary* dictionary = loader_data->dictionary();
 592 
 593   // Do lookup to see if class already exists and the protection domain
 594   // has the right access.
 595   // This call uses find which checks protection domain already matches
 596   // All subsequent calls use find_class, and set loaded_class so that
 597   // before we return a result, we call out to java to check for valid protection domain.
 598   InstanceKlass* probe = dictionary->find(THREAD, name, protection_domain);
 599   if (probe != nullptr) return probe;
 600 
 601   // Non-bootstrap class loaders will call out to class loader and
 602   // define via jvm/jni_DefineClass which will acquire the
 603   // class loader object lock to protect against multiple threads
 604   // defining the class in parallel by accident.
 605   // This lock must be acquired here so the waiter will find
 606   // any successful result in the SystemDictionary and not attempt
 607   // the define.
 608   // ParallelCapable class loaders and the bootstrap classloader
 609   // do not acquire lock here.
 610   Handle lockObject = get_loader_lock_or_null(class_loader);
 611   ObjectLocker ol(lockObject, THREAD);
 612 
 613   bool circularity_detection_in_progress  = false;
 614   InstanceKlass* loaded_class = nullptr;
 615   SymbolHandle superclassname; // Keep alive while loading in parallel thread.
 616 
 617   guarantee(THREAD->can_call_java(),
 618          "can not load classes with compiler thread: class=%s, classloader=%s",
 619          name->as_C_string(),
 620          class_loader.is_null() ? "null" : class_loader->klass()->name()->as_C_string());
 621 
 622   // Check again (after locking) if the class already exists in SystemDictionary
 623   {
 624     MutexLocker mu(THREAD, SystemDictionary_lock);
 625     InstanceKlass* check = dictionary->find_class(THREAD, name);
 626     if (check != nullptr) {
 627       // InstanceKlass is already loaded, but we still need to check protection domain below.
 628       loaded_class = check;
 629     } else {
 630       PlaceholderEntry* placeholder = PlaceholderTable::get_entry(name, loader_data);
 631       if (placeholder != nullptr && placeholder->circularity_detection_in_progress()) {
 632          circularity_detection_in_progress = true;
 633          superclassname = placeholder->next_klass_name();
 634          assert(superclassname != nullptr, "superclass has to have a name");
 635       }
 636     }
 637   }
 638 
 639   // If the class is in the placeholder table with super_class set,
 640   // handle superclass loading in progress.
 641   if (circularity_detection_in_progress) {
 642     handle_parallel_super_load(name, superclassname,
 643                                class_loader,
 644                                protection_domain,
 645                                CHECK_NULL);
 646   }
 647 
 648   bool throw_circularity_error = false;
 649   if (loaded_class == nullptr) {
 650     bool load_placeholder_added = false;
 651 
 652     // Add placeholder entry to record loading instance class
 653     // case 1. Bootstrap classloader
 654     //    This classloader supports parallelism at the classloader level
 655     //    but only allows a single thread to load a class/classloader pair.
 656     //    The LOAD_INSTANCE placeholder is the mechanism for mutual exclusion.
 657     // case 2. parallelCapable user level classloaders
 658     //    These class loaders lock a per-class object lock when ClassLoader.loadClass()
 659     //    is called. A LOAD_INSTANCE placeholder isn't used for mutual exclusion.
 660     // case 3. traditional classloaders that rely on the classloader object lock
 661     //    There should be no need for need for LOAD_INSTANCE for mutual exclusion,

1038 }
1039 
1040 bool SystemDictionary::check_shared_class_super_type(InstanceKlass* klass, InstanceKlass* super_type,
1041                                                      Handle class_loader,  Handle protection_domain,
1042                                                      bool is_superclass, TRAPS) {
1043   assert(super_type->is_shared(), "must be");
1044 
1045   // Quick check if the super type has been already loaded.
1046   // + Don't do it for unregistered classes -- they can be unloaded so
1047   //   super_type->class_loader_data() could be stale.
1048   // + Don't check if loader data is null, ie. the super_type isn't fully loaded.
1049   if (!super_type->is_shared_unregistered_class() && super_type->class_loader_data() != nullptr) {
1050     // Check if the superclass is loaded by the current class_loader
1051     Symbol* name = super_type->name();
1052     InstanceKlass* check = find_instance_klass(THREAD, name, class_loader, protection_domain);
1053     if (check == super_type) {
1054       return true;
1055     }
1056   }
1057 
1058   Klass *found = resolve_with_circularity_detection_or_fail(klass->name(), super_type->name(),
1059                                        class_loader, protection_domain, is_superclass, CHECK_0);
1060   if (found == super_type) {
1061     return true;
1062   } else {
1063     // The dynamically resolved super type is not the same as the one we used during dump time,
1064     // so we cannot use the class.
1065     return false;
1066   }
1067 }
1068 
1069 bool SystemDictionary::check_shared_class_super_types(InstanceKlass* ik, Handle class_loader,
1070                                                       Handle protection_domain, TRAPS) {
1071   // Check the superclass and interfaces. They must be the same
1072   // as in dump time, because the layout of <ik> depends on
1073   // the specific layout of ik->super() and ik->local_interfaces().
1074   //
1075   // If unexpected superclass or interfaces are found, we cannot
1076   // load <ik> from the shared archive.
1077 
1078   if (ik->super() != nullptr &&

1127 
1128 InstanceKlass* SystemDictionary::load_shared_class(InstanceKlass* ik,
1129                                                    Handle class_loader,
1130                                                    Handle protection_domain,
1131                                                    const ClassFileStream *cfs,
1132                                                    PackageEntry* pkg_entry,
1133                                                    TRAPS) {
1134   assert(ik != nullptr, "sanity");
1135   assert(!ik->is_unshareable_info_restored(), "shared class can be restored only once");
1136   assert(Atomic::add(&ik->_shared_class_load_count, 1) == 1, "shared class loaded more than once");
1137   Symbol* class_name = ik->name();
1138 
1139   if (!is_shared_class_visible(class_name, ik, pkg_entry, class_loader)) {
1140     return nullptr;
1141   }
1142 
1143   if (!check_shared_class_super_types(ik, class_loader, protection_domain, THREAD)) {
1144     return nullptr;
1145   }
1146 
1147   if (ik->has_inline_type_fields()) {
1148     for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
1149       if (fs.access_flags().is_static()) continue;
1150       Symbol* sig = fs.signature();
1151       if (fs.is_null_free_inline_type()) {
1152         // Pre-load inline class
1153         TempNewSymbol name = Signature::strip_envelope(sig);
1154         Klass* real_k = SystemDictionary::resolve_with_circularity_detection_or_fail(ik->name(), name,
1155           class_loader, protection_domain, false, CHECK_NULL);
1156         Klass* k = ik->get_inline_type_field_klass_or_null(fs.index());
1157         if (real_k != k) {
1158           // oops, the app has substituted a different version of k!
1159           return nullptr;
1160         }
1161       } else if (Signature::has_envelope(sig)) {
1162         TempNewSymbol name = Signature::strip_envelope(sig);
1163         if (name != ik->name() && ik->is_class_in_preload_attribute(name)) {
1164           Klass* real_k = SystemDictionary::resolve_with_circularity_detection_or_fail(ik->name(), name,
1165             class_loader, protection_domain, false, THREAD);
1166           if (HAS_PENDING_EXCEPTION) {
1167             CLEAR_PENDING_EXCEPTION;
1168           }
1169           Klass* k = ik->get_inline_type_field_klass_or_null(fs.index());
1170           if (real_k != k) {
1171             // oops, the app has substituted a different version of k!
1172             return nullptr;
1173           }
1174         }
1175       }
1176     }
1177   }
1178 
1179   InstanceKlass* new_ik = nullptr;
1180   // CFLH check is skipped for VM hidden classes (see KlassFactory::create_from_stream).
1181   // It will be skipped for shared VM hidden lambda proxy classes.
1182   if (!SystemDictionaryShared::is_hidden_lambda_proxy(ik)) {
1183     new_ik = KlassFactory::check_shared_class_file_load_hook(
1184       ik, class_name, class_loader, protection_domain, cfs, CHECK_NULL);
1185   }
1186   if (new_ik != nullptr) {
1187     // The class is changed by CFLH. Return the new class. The shared class is
1188     // not used.
1189     return new_ik;
1190   }
1191 
1192   // Adjust methods to recover missing data.  They need addresses for
1193   // interpreter entry points and their default native method address
1194   // must be reset.
1195 
1196   // Shared classes are all currently loaded by either the bootstrap or
1197   // internal parallel class loaders, so this will never cause a deadlock
1198   // on a custom class loader lock.
1199   // Since this class is already locked with parallel capable class
1200   // loaders, including the bootstrap loader via the placeholder table,
1201   // this lock is currently a nop.
1202 
1203   ClassLoaderData* loader_data = class_loader_data(class_loader);
1204   {
1205     HandleMark hm(THREAD);
1206     Handle lockObject = get_loader_lock_or_null(class_loader);
1207     ObjectLocker ol(lockObject, THREAD);
1208     // prohibited package check assumes all classes loaded from archive call
1209     // restore_unshareable_info which calls ik->set_package()
1210     ik->restore_unshareable_info(loader_data, protection_domain, pkg_entry, CHECK_NULL);
1211   }
1212 
1213   load_shared_class_misc(ik, loader_data);
1214 
1215   return ik;
1216 }
1217 
1218 void SystemDictionary::load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) {
1219   ik->print_class_load_logging(loader_data, nullptr, nullptr);
1220 
1221   // For boot loader, ensure that GetSystemPackage knows that a class in this
1222   // package was loaded.
1223   if (loader_data->is_the_null_class_loader_data()) {
1224     s2 path_index = ik->shared_classpath_index();
1225     ik->set_classpath_index(path_index);
1226   }
1227 
1228   // notify a class loaded from shared object
1229   ClassLoadingService::notify_class_loaded(ik, true /* shared class */);
1230 }
1231 
1232 #endif // INCLUDE_CDS
1233 
1234 InstanceKlass* SystemDictionary::load_instance_class_impl(Symbol* class_name, Handle class_loader, TRAPS) {
< prev index next >