src/hotspot/share/classfile/systemDictionary.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File webrev Sdiff src/hotspot/share/classfile

src/hotspot/share/classfile/systemDictionary.cpp

Print this page




 354 #endif // INCLUDE_CDS
 355 
 356   // Double-check, if child class is already loaded, just return super-class,interface
 357   // Don't add a placedholder if already loaded, i.e. already in appropriate class loader
 358   // dictionary.
 359   // Make sure there's a placeholder for the *child* before resolving.
 360   // Used as a claim that this thread is currently loading superclass/classloader
 361   // Used here for ClassCircularity checks and also for heap verification
 362   // (every InstanceKlass needs to be in its class loader dictionary or have a placeholder).
 363   // Must check ClassCircularity before checking if super class is already loaded.
 364   //
 365   // We might not already have a placeholder if this child_name was
 366   // first seen via resolve_from_stream (jni_DefineClass or JVM_DefineClass);
 367   // the name of the class might not be known until the stream is actually
 368   // parsed.
 369   // Bugs 4643874, 4715493
 370 
 371   ClassLoaderData* loader_data = class_loader_data(class_loader);
 372   Dictionary* dictionary = loader_data->dictionary();
 373   unsigned int d_hash = dictionary->compute_hash(child_name);
 374   int d_index = dictionary->hash_to_index(d_hash);
 375   unsigned int p_hash = placeholders()->compute_hash(child_name);
 376   int p_index = placeholders()->hash_to_index(p_hash);
 377   // can't throw error holding a lock
 378   bool child_already_loaded = false;
 379   bool throw_circularity_error = false;
 380   {
 381     MutexLocker mu(SystemDictionary_lock, THREAD);
 382     Klass* childk = find_class(d_index, d_hash, child_name, dictionary);
 383     Klass* quicksuperk;
 384     // to support // loading: if child done loading, just return superclass
 385     // if class_name, & class_loader don't match:
 386     // if initial define, SD update will give LinkageError
 387     // if redefine: compare_class_versions will give HIERARCHY_CHANGED
 388     // so we don't throw an exception here.
 389     // see: nsk redefclass014 & java.lang.instrument Instrument032
 390     if ((childk != NULL ) && (is_superclass) &&
 391        ((quicksuperk = childk->super()) != NULL) &&
 392 
 393          ((quicksuperk->name() == class_name) &&
 394             (quicksuperk->class_loader()  == class_loader()))) {
 395            return quicksuperk;
 396     } else {
 397       PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, loader_data);
 398       if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER)) {
 399           throw_circularity_error = true;
 400       }
 401     }
 402     if (!throw_circularity_error) {


 470                          mirror,
 471                          protection_domain,
 472                          THREAD);
 473 
 474   if (HAS_PENDING_EXCEPTION) {
 475     log_debug(protectiondomain)("DENIED !!!!!!!!!!!!!!!!!!!!!");
 476   } else {
 477    log_debug(protectiondomain)("granted");
 478   }
 479 
 480   if (HAS_PENDING_EXCEPTION) return;
 481 
 482   // If no exception has been thrown, we have validated the protection domain
 483   // Insert the protection domain of the initiating class into the set.
 484   {
 485     ClassLoaderData* loader_data = class_loader_data(class_loader);
 486     Dictionary* dictionary = loader_data->dictionary();
 487 
 488     Symbol*  kn = klass->name();
 489     unsigned int d_hash = dictionary->compute_hash(kn);
 490     int d_index = dictionary->hash_to_index(d_hash);
 491 
 492     MutexLocker mu(SystemDictionary_lock, THREAD);

 493     dictionary->add_protection_domain(d_index, d_hash, klass,
 494                                       protection_domain, THREAD);
 495   }
 496 }
 497 
 498 // We only get here if this thread finds that another thread
 499 // has already claimed the placeholder token for the current operation,
 500 // but that other thread either never owned or gave up the
 501 // object lock
 502 // Waits on SystemDictionary_lock to indicate placeholder table updated
 503 // On return, caller must recheck placeholder table state
 504 //
 505 // We only get here if
 506 //  1) custom classLoader, i.e. not bootstrap classloader
 507 //  2) UnsyncloadClass not set
 508 //  3) custom classLoader has broken the class loader objectLock
 509 //     so another thread got here in parallel
 510 //
 511 // lockObject must be held.
 512 // Complicated dance due to lock ordering:


 538 // If the class in is in the placeholder table, class loading is in progress
 539 // For cases where the application changes threads to load classes, it
 540 // is critical to ClassCircularity detection that we try loading
 541 // the superclass on the same thread internally, so we do parallel
 542 // super class loading here.
 543 // This also is critical in cases where the original thread gets stalled
 544 // even in non-circularity situations.
 545 // Note: must call resolve_super_or_fail even if null super -
 546 // to force placeholder entry creation for this class for circularity detection
 547 // Caller must check for pending exception
 548 // Returns non-null Klass* if other thread has completed load
 549 // and we are done,
 550 // If return null Klass* and no pending exception, the caller must load the class
 551 InstanceKlass* SystemDictionary::handle_parallel_super_load(
 552     Symbol* name, Symbol* superclassname, Handle class_loader,
 553     Handle protection_domain, Handle lockObject, TRAPS) {
 554 
 555   ClassLoaderData* loader_data = class_loader_data(class_loader);
 556   Dictionary* dictionary = loader_data->dictionary();
 557   unsigned int d_hash = dictionary->compute_hash(name);
 558   int d_index = dictionary->hash_to_index(d_hash);
 559   unsigned int p_hash = placeholders()->compute_hash(name);
 560   int p_index = placeholders()->hash_to_index(p_hash);
 561 
 562   // superk is not used, resolve_super called for circularity check only
 563   // This code is reached in two situations. One if this thread
 564   // is loading the same class twice (e.g. ClassCircularity, or
 565   // java.lang.instrument).
 566   // The second is if another thread started the resolve_super first
 567   // and has not yet finished.
 568   // In both cases the original caller will clean up the placeholder
 569   // entry on error.
 570   Klass* superk = SystemDictionary::resolve_super_or_fail(name,
 571                                                           superclassname,
 572                                                           class_loader,
 573                                                           protection_domain,
 574                                                           true,
 575                                                           CHECK_NULL);
 576 
 577   // parallelCapable class loaders do NOT wait for parallel superclass loads to complete
 578   // Serial class loaders and bootstrap classloader do wait for superclass loads
 579  if (!class_loader.is_null() && is_parallelCapable(class_loader)) {
 580     MutexLocker mu(SystemDictionary_lock, THREAD);
 581     // Check if classloading completed while we were loading superclass or waiting
 582     return find_class(d_index, d_hash, name, dictionary);
 583   }
 584 
 585   // must loop to both handle other placeholder updates
 586   // and spurious notifications
 587   bool super_load_in_progress = true;
 588   PlaceholderEntry* placeholder;
 589   while (super_load_in_progress) {
 590     MutexLocker mu(SystemDictionary_lock, THREAD);
 591     // Check if classloading completed while we were loading superclass or waiting
 592     InstanceKlass* check = find_class(d_index, d_hash, name, dictionary);
 593     if (check != NULL) {
 594       // Klass is already loaded, so just return it
 595       return check;
 596     } else {
 597       placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 598       if (placeholder && placeholder->super_load_in_progress() ){
 599         // Before UnsyncloadClass:
 600         // We only get here if the application has released the
 601         // classloader lock when another thread was in the middle of loading a
 602         // superclass/superinterface for this class, and now
 603         // this thread is also trying to load this class.
 604         // To minimize surprises, the first thread that started to
 605         // load a class should be the one to complete the loading
 606         // with the classfile it initially expected.
 607         // This logic has the current thread wait once it has done
 608         // all the superclass/superinterface loading it can, until
 609         // the original thread completes the class loading or fails
 610         // If it completes we will use the resulting InstanceKlass
 611         // which we will find below in the systemDictionary.
 612         // We also get here for parallel bootstrap classloader


 653 
 654 // Be careful when modifying this code: once you have run
 655 // placeholders()->find_and_add(PlaceholderTable::LOAD_INSTANCE),
 656 // you need to find_and_remove it before returning.
 657 // So be careful to not exit with a CHECK_ macro betweeen these calls.
 658 Klass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
 659                                                         Handle class_loader,
 660                                                         Handle protection_domain,
 661                                                         TRAPS) {
 662   assert(name != NULL && !FieldType::is_array(name) &&
 663          !FieldType::is_obj(name), "invalid class name");
 664 
 665   EventClassLoad class_load_start_event;
 666 
 667   HandleMark hm(THREAD);
 668 
 669   // Fix for 4474172; see evaluation for more details
 670   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
 671   ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
 672   Dictionary* dictionary = loader_data->dictionary();

 673 
 674   // Do lookup to see if class already exist and the protection domain
 675   // has the right access
 676   // This call uses find which checks protection domain already matches
 677   // All subsequent calls use find_class, and set has_loaded_class so that
 678   // before we return a result we call out to java to check for valid protection domain
 679   // to allow returning the Klass* and add it to the pd_set if it is valid
 680   unsigned int d_hash = dictionary->compute_hash(name);
 681   int d_index = dictionary->hash_to_index(d_hash);
 682   Klass* probe = dictionary->find(d_index, d_hash, name, protection_domain);
 683   if (probe != NULL) return probe;
 684 
 685 
 686   // Non-bootstrap class loaders will call out to class loader and
 687   // define via jvm/jni_DefineClass which will acquire the
 688   // class loader object lock to protect against multiple threads
 689   // defining the class in parallel by accident.
 690   // This lock must be acquired here so the waiter will find
 691   // any successful result in the SystemDictionary and not attempt
 692   // the define
 693   // ParallelCapable Classloaders and the bootstrap classloader,
 694   // or all classloaders with UnsyncloadClass do not acquire lock here
 695   bool DoObjectLock = true;
 696   if (is_parallelCapable(class_loader)) {
 697     DoObjectLock = false;
 698   }
 699 
 700   unsigned int p_hash = placeholders()->compute_hash(name);
 701   int p_index = placeholders()->hash_to_index(p_hash);
 702 
 703   // Class is not in SystemDictionary so we have to do loading.
 704   // Make sure we are synchronized on the class loader before we proceed
 705   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
 706   check_loader_lock_contention(lockObject, THREAD);
 707   ObjectLocker ol(lockObject, THREAD, DoObjectLock);
 708 
 709   // Check again (after locking) if class already exist in SystemDictionary
 710   bool class_has_been_loaded   = false;
 711   bool super_load_in_progress  = false;
 712   bool havesupername = false;
 713   InstanceKlass* k = NULL;
 714   PlaceholderEntry* placeholder;
 715   Symbol* superclassname = NULL;
 716 
 717   {
 718     MutexLocker mu(SystemDictionary_lock, THREAD);
 719     InstanceKlass* check = find_class(d_index, d_hash, name, dictionary);
 720     if (check != NULL) {
 721       // Klass is already loaded, so just return it
 722       class_has_been_loaded = true;
 723       k = check;
 724     } else {
 725       placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 726       if (placeholder && placeholder->super_load_in_progress()) {
 727          super_load_in_progress = true;
 728          if (placeholder->havesupername() == true) {
 729            superclassname = placeholder->supername();
 730            havesupername = true;
 731          }
 732       }
 733     }
 734   }
 735 
 736   // If the class is in the placeholder table, class loading is in progress
 737   if (super_load_in_progress && havesupername==true) {
 738     k = handle_parallel_super_load(name,
 739                                    superclassname,


 783         PlaceholderEntry* oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 784         if (oldprobe) {
 785           // only need check_seen_thread once, not on each loop
 786           // 6341374 java/lang/Instrument with -Xcomp
 787           if (oldprobe->check_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE)) {
 788             throw_circularity_error = true;
 789           } else {
 790             // case 1: traditional: should never see load_in_progress.
 791             while (!class_has_been_loaded && oldprobe && oldprobe->instance_load_in_progress()) {
 792 
 793               // case 4: bootstrap classloader: prevent futile classloading,
 794               // wait on first requestor
 795               if (class_loader.is_null()) {
 796                 SystemDictionary_lock->wait();
 797               } else {
 798               // case 2: traditional with broken classloader lock. wait on first
 799               // requestor.
 800                 double_lock_wait(lockObject, THREAD);
 801               }
 802               // Check if classloading completed while we were waiting
 803               InstanceKlass* check = find_class(d_index, d_hash, name, dictionary);
 804               if (check != NULL) {
 805                 // Klass is already loaded, so just return it
 806                 k = check;
 807                 class_has_been_loaded = true;
 808               }
 809               // check if other thread failed to load and cleaned up
 810               oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 811             }
 812           }
 813         }
 814       }
 815       // All cases: add LOAD_INSTANCE holding SystemDictionary_lock
 816       // case 3: UnsyncloadClass || case 5: parallelCapable: allow competing threads to try
 817       // LOAD_INSTANCE in parallel
 818 
 819       if (!throw_circularity_error && !class_has_been_loaded) {
 820         PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, NULL, THREAD);
 821         load_instance_added = true;
 822         // For class loaders that do not acquire the classloader object lock,
 823         // if they did not catch another thread holding LOAD_INSTANCE,
 824         // need a check analogous to the acquire ObjectLocker/find_class
 825         // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL
 826         // one final check if the load has already completed
 827         // class loaders holding the ObjectLock shouldn't find the class here
 828         InstanceKlass* check = find_class(d_index, d_hash, name, dictionary);
 829         if (check != NULL) {
 830         // Klass is already loaded, so return it after checking/adding protection domain
 831           k = check;
 832           class_has_been_loaded = true;
 833         }
 834       }
 835     }
 836 
 837     // must throw error outside of owning lock
 838     if (throw_circularity_error) {
 839       assert(!HAS_PENDING_EXCEPTION && load_instance_added == false,"circularity error cleanup");
 840       ResourceMark rm(THREAD);
 841       THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), name->as_C_string());
 842     }
 843 
 844     if (!class_has_been_loaded) {
 845 
 846       // Do actual loading
 847       k = load_instance_class(name, class_loader, THREAD);
 848 
 849       // For UnsyncloadClass only
 850       // If they got a linkageError, check if a parallel class load succeeded.
 851       // If it did, then for bytecode resolution the specification requires
 852       // that we return the same result we did for the other thread, i.e. the
 853       // successfully loaded InstanceKlass
 854       // Should not get here for classloaders that support parallelism
 855       // with the new cleaner mechanism, even with AllowParallelDefineClass
 856       // Bootstrap goes through here to allow for an extra guarantee check
 857       if (UnsyncloadClass || (class_loader.is_null())) {
 858         if (k == NULL && HAS_PENDING_EXCEPTION
 859           && PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
 860           MutexLocker mu(SystemDictionary_lock, THREAD);
 861           InstanceKlass* check = find_class(d_index, d_hash, name, dictionary);
 862           if (check != NULL) {
 863             // Klass is already loaded, so just use it
 864             k = check;
 865             CLEAR_PENDING_EXCEPTION;
 866             guarantee((!class_loader.is_null()), "dup definition for bootstrap loader?");
 867           }
 868         }
 869       }
 870 
 871       // If everything was OK (no exceptions, no null return value), and
 872       // class_loader is NOT the defining loader, do a little more bookkeeping.
 873       if (!HAS_PENDING_EXCEPTION && k != NULL &&
 874         k->class_loader() != class_loader()) {
 875 
 876         check_constraints(d_index, d_hash, k, class_loader, false, THREAD);
 877 
 878         // Need to check for a PENDING_EXCEPTION again; check_constraints
 879         // can throw and doesn't use the CHECK macro.
 880         if (!HAS_PENDING_EXCEPTION) {
 881           { // Grabbing the Compile_lock prevents systemDictionary updates
 882             // during compilations.
 883             MutexLocker mu(Compile_lock, THREAD);
 884             update_dictionary(d_index, d_hash, p_index, p_hash,
 885               k, class_loader, THREAD);
 886           }
 887 
 888           if (JvmtiExport::should_post_class_load()) {
 889             Thread *thread = THREAD;
 890             assert(thread->is_Java_thread(), "thread->is_Java_thread()");
 891             JvmtiExport::post_class_load((JavaThread *) thread, k);
 892           }
 893         }
 894       }
 895     } // load_instance_class
 896 
 897     if (load_instance_added == true) {
 898       // clean up placeholder entries for LOAD_INSTANCE success or error
 899       // This brackets the SystemDictionary updates for both defining
 900       // and initiating loaders
 901       MutexLocker mu(SystemDictionary_lock, THREAD);
 902       placeholders()->find_and_remove(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, THREAD);
 903       SystemDictionary_lock->notify_all();
 904     }


 906 
 907   if (HAS_PENDING_EXCEPTION || k == NULL) {
 908     return NULL;
 909   }
 910 
 911   post_class_load_event(&class_load_start_event, k, loader_data);
 912 
 913 #ifdef ASSERT
 914   {
 915     ClassLoaderData* loader_data = k->class_loader_data();
 916     MutexLocker mu(SystemDictionary_lock, THREAD);
 917     Klass* kk = find_class(name, loader_data);
 918     assert(kk == k, "should be present in dictionary");
 919   }
 920 #endif
 921 
 922   // return if the protection domain in NULL
 923   if (protection_domain() == NULL) return k;
 924 
 925   // Check the protection domain has the right access
 926   if (dictionary->is_valid_protection_domain(d_index, d_hash, name,
 927                                              protection_domain)) {
 928     return k;
 929   }
 930 
 931   // Verify protection domain. If it fails an exception is thrown
 932   validate_protection_domain(k, class_loader, protection_domain, CHECK_NULL);
 933 
 934   return k;
 935 }
 936 
 937 
 938 // This routine does not lock the system dictionary.
 939 //
 940 // Since readers don't hold a lock, we must make sure that system
 941 // dictionary entries are only removed at a safepoint (when only one
 942 // thread is running), and are added to in a safe way (all links must
 943 // be updated in an MT-safe manner).
 944 //
 945 // Callers should be aware that an entry could be added just after
 946 // _dictionary->bucket(index) is read here, so the caller will not see


 948 
 949 Klass* SystemDictionary::find(Symbol* class_name,
 950                               Handle class_loader,
 951                               Handle protection_domain,
 952                               TRAPS) {
 953 
 954   // The result of this call should be consistent with the result
 955   // of the call to resolve_instance_class_or_null().
 956   // See evaluation 6790209 and 4474172 for more details.
 957   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
 958   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data_or_null(class_loader());
 959 
 960   if (loader_data == NULL) {
 961     // If the ClassLoaderData has not been setup,
 962     // then the class loader has no entries in the dictionary.
 963     return NULL;
 964   }
 965 
 966   Dictionary* dictionary = loader_data->dictionary();
 967   unsigned int d_hash = dictionary->compute_hash(class_name);
 968   int d_index = dictionary->hash_to_index(d_hash);
 969   return dictionary->find(d_index, d_hash, class_name,
 970                           protection_domain);
 971 }
 972 
 973 
 974 // Look for a loaded instance or array klass by name.  Do not do any loading.
 975 // return NULL in case of error.
 976 Klass* SystemDictionary::find_instance_or_array_klass(Symbol* class_name,
 977                                                       Handle class_loader,
 978                                                       Handle protection_domain,
 979                                                       TRAPS) {
 980   Klass* k = NULL;
 981   assert(class_name != NULL, "class name must be non NULL");
 982 
 983   if (FieldType::is_array(class_name)) {
 984     // The name refers to an array.  Parse the name.
 985     // dimension and object_key in FieldArrayInfo are assigned as a
 986     // side-effect of this call
 987     FieldArrayInfo fd;
 988     BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(NULL));
 989     if (t != T_OBJECT) {


1627  // find_or_define_instance_class to get here, we have a timing
1628  // hole with systemDictionary updates and check_constraints
1629  if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) {
1630     assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,
1631          compute_loader_lock_object(class_loader_h, THREAD)),
1632          "define called without lock");
1633   }
1634 
1635   // Check class-loading constraints. Throw exception if violation is detected.
1636   // Grabs and releases SystemDictionary_lock
1637   // The check_constraints/find_class call and update_dictionary sequence
1638   // must be "atomic" for a specific class/classloader pair so we never
1639   // define two different instanceKlasses for that class/classloader pair.
1640   // Existing classloaders will call define_instance_class with the
1641   // classloader lock held
1642   // Parallel classloaders will call find_or_define_instance_class
1643   // which will require a token to perform the define class
1644   Symbol*  name_h = k->name();
1645   Dictionary* dictionary = loader_data->dictionary();
1646   unsigned int d_hash = dictionary->compute_hash(name_h);
1647   int d_index = dictionary->hash_to_index(d_hash);
1648   check_constraints(d_index, d_hash, k, class_loader_h, true, CHECK);
1649 
1650   // Register class just loaded with class loader (placed in Vector)
1651   // Note we do this before updating the dictionary, as this can
1652   // fail with an OutOfMemoryError (if it does, we will *not* put this
1653   // class in the dictionary and will not update the class hierarchy).
1654   // JVMTI FollowReferences needs to find the classes this way.
1655   if (k->class_loader() != NULL) {
1656     methodHandle m(THREAD, Universe::loader_addClass_method());
1657     JavaValue result(T_VOID);
1658     JavaCallArguments args(class_loader_h);
1659     args.push_oop(Handle(THREAD, k->java_mirror()));
1660     JavaCalls::call(&result, m, &args, CHECK);
1661   }
1662 
1663   // Add the new class. We need recompile lock during update of CHA.
1664   {
1665     unsigned int p_hash = placeholders()->compute_hash(name_h);
1666     int p_index = placeholders()->hash_to_index(p_hash);
1667 
1668     MutexLocker mu_r(Compile_lock, THREAD);
1669 
1670     // Add to class hierarchy, initialize vtables, and do possible
1671     // deoptimizations.
1672     add_to_hierarchy(k, CHECK); // No exception, but can block
1673 
1674     // Add to systemDictionary - so other classes can see it.
1675     // Grabs and releases SystemDictionary_lock
1676     update_dictionary(d_index, d_hash, p_index, p_hash,
1677                       k, class_loader_h, THREAD);
1678   }
1679   k->eager_initialize(THREAD);
1680 
1681   // notify jvmti
1682   if (JvmtiExport::should_post_class_load()) {
1683       assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1684       JvmtiExport::post_class_load((JavaThread *) THREAD, k);
1685 
1686   }
1687   class_define_event(k, loader_data);
1688 }
1689 
1690 // Support parallel classloading
1691 // All parallel class loaders, including bootstrap classloader
1692 // lock a placeholder entry for this class/class_loader pair
1693 // to allow parallel defines of different classes for this class loader
1694 // With AllowParallelDefine flag==true, in case they do not synchronize around
1695 // FindLoadedClass/DefineClass, calls, we check for parallel
1696 // loading for them, wait if a defineClass is in progress


1698 // This flag does not apply to the bootstrap classloader.
1699 // With AllowParallelDefine flag==false, call through to define_instance_class
1700 // which will throw LinkageError: duplicate class definition.
1701 // False is the requested default.
1702 // For better performance, the class loaders should synchronize
1703 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
1704 // potentially waste time reading and parsing the bytestream.
1705 // Note: VM callers should ensure consistency of k/class_name,class_loader
1706 // Be careful when modifying this code: once you have run
1707 // placeholders()->find_and_add(PlaceholderTable::DEFINE_CLASS),
1708 // you need to find_and_remove it before returning.
1709 // So be careful to not exit with a CHECK_ macro betweeen these calls.
1710 InstanceKlass* SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader,
1711                                                                InstanceKlass* k, TRAPS) {
1712 
1713   Symbol*  name_h = k->name(); // passed in class_name may be null
1714   ClassLoaderData* loader_data = class_loader_data(class_loader);
1715   Dictionary* dictionary = loader_data->dictionary();
1716 
1717   unsigned int d_hash = dictionary->compute_hash(name_h);
1718   int d_index = dictionary->hash_to_index(d_hash);
1719 
1720   // Hold SD lock around find_class and placeholder creation for DEFINE_CLASS
1721   unsigned int p_hash = placeholders()->compute_hash(name_h);
1722   int p_index = placeholders()->hash_to_index(p_hash);
1723   PlaceholderEntry* probe;
1724 
1725   {
1726     MutexLocker mu(SystemDictionary_lock, THREAD);
1727     // First check if class already defined
1728     if (UnsyncloadClass || (is_parallelDefine(class_loader))) {
1729       InstanceKlass* check = find_class(d_index, d_hash, name_h, dictionary);
1730       if (check != NULL) {
1731         return check;
1732       }
1733     }
1734 
1735     // Acquire define token for this class/classloader
1736     probe = placeholders()->find_and_add(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, NULL, THREAD);
1737     // Wait if another thread defining in parallel
1738     // All threads wait - even those that will throw duplicate class: otherwise
1739     // caller is surprised by LinkageError: duplicate, but findLoadedClass fails
1740     // if other thread has not finished updating dictionary
1741     while (probe->definer() != NULL) {
1742       SystemDictionary_lock->wait();
1743     }
1744     // Only special cases allow parallel defines and can use other thread's results
1745     // Other cases fall through, and may run into duplicate defines
1746     // caught by finding an entry in the SystemDictionary
1747     if ((UnsyncloadClass || is_parallelDefine(class_loader)) && (probe->instance_klass() != NULL)) {
1748         placeholders()->find_and_remove(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, THREAD);
1749         SystemDictionary_lock->notify_all();
1750 #ifdef ASSERT
1751         InstanceKlass* check = find_class(d_index, d_hash, name_h, dictionary);
1752         assert(check != NULL, "definer missed recording success");
1753 #endif
1754         return probe->instance_klass();
1755     } else {
1756       // This thread will define the class (even if earlier thread tried and had an error)
1757       probe->set_definer(THREAD);
1758     }
1759   }
1760 
1761   define_instance_class(k, THREAD);
1762 
1763   Handle linkage_exception = Handle(); // null handle
1764 
1765   // definer must notify any waiting threads
1766   {
1767     MutexLocker mu(SystemDictionary_lock, THREAD);
1768     PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, name_h, loader_data);
1769     assert(probe != NULL, "DEFINE_CLASS placeholder lost?");
1770     if (probe != NULL) {
1771       if (HAS_PENDING_EXCEPTION) {


1806     return;
1807   }
1808 
1809   assert(!loader_lock.is_null(), "NULL lock object");
1810 
1811   if (ObjectSynchronizer::query_lock_ownership((JavaThread*)THREAD, loader_lock)
1812       == ObjectSynchronizer::owner_other) {
1813     // contention will likely happen, so increment the corresponding
1814     // contention counter.
1815     if (loader_lock() == _system_loader_lock_obj) {
1816       ClassLoader::sync_systemLoaderLockContentionRate()->inc();
1817     } else {
1818       ClassLoader::sync_nonSystemLoaderLockContentionRate()->inc();
1819     }
1820   }
1821 }
1822 
1823 // ----------------------------------------------------------------------------
1824 // Lookup
1825 
1826 InstanceKlass* SystemDictionary::find_class(int index, unsigned int hash,
1827                                             Symbol* class_name,
1828                                             Dictionary* dictionary) {
1829   assert_locked_or_safepoint(SystemDictionary_lock);

1830   return dictionary->find_class(index, hash, class_name);
1831 }
1832 
1833 
1834 // Basic find on classes in the midst of being loaded
1835 Symbol* SystemDictionary::find_placeholder(Symbol* class_name,
1836                                            ClassLoaderData* loader_data) {
1837   assert_locked_or_safepoint(SystemDictionary_lock);
1838   unsigned int p_hash = placeholders()->compute_hash(class_name);
1839   int p_index = placeholders()->hash_to_index(p_hash);
1840   return placeholders()->find_entry(p_index, p_hash, class_name, loader_data);
1841 }
1842 
1843 
1844 // Used for assertions and verification only
1845 // Precalculating the hash and index is an optimization because there are many lookups
1846 // before adding the class.
1847 InstanceKlass* SystemDictionary::find_class(Symbol* class_name, ClassLoaderData* loader_data) {
1848   assert_locked_or_safepoint(SystemDictionary_lock);
1849   #ifndef ASSERT
1850   guarantee(VerifyBeforeGC      ||
1851             VerifyDuringGC      ||
1852             VerifyBeforeExit    ||
1853             VerifyDuringStartup ||
1854             VerifyAfterGC, "too expensive");
1855   #endif
1856 
1857   Dictionary* dictionary = loader_data->dictionary();
1858   unsigned int d_hash = dictionary->compute_hash(class_name);
1859   int d_index = dictionary->hash_to_index(d_hash);
1860   return find_class(d_index, d_hash, class_name, dictionary);
1861 }
1862 
1863 
1864 // ----------------------------------------------------------------------------
1865 // Update hierachy. This is done before the new klass has been added to the SystemDictionary. The Recompile_lock
1866 // is held, to ensure that the compiler is not using the class hierachy, and that deoptimization will kick in
1867 // before a new class is used.
1868 
1869 void SystemDictionary::add_to_hierarchy(InstanceKlass* k, TRAPS) {
1870   assert(k != NULL, "just checking");
1871   assert_locked_or_safepoint(Compile_lock);
1872 
1873   // Link into hierachy. Make sure the vtables are initialized before linking into
1874   k->append_to_sibling_list();                    // add to superklass/sibling list
1875   k->process_interfaces(THREAD);                  // handle all "implements" declarations
1876   k->set_init_state(InstanceKlass::loaded);
1877   // Now flush all code that depended on old class hierarchy.
1878   // Note: must be done *after* linking k into the hierarchy (was bug 12/9/97)
1879   // Also, first reinitialize vtable because it may have gotten out of synch
1880   // while the new class wasn't connected to the class hierarchy.


2193 
2194 // Tells if a given klass is a box (wrapper class, such as java.lang.Integer).
2195 // If so, returns the basic type it holds.  If not, returns T_OBJECT.
2196 BasicType SystemDictionary::box_klass_type(Klass* k) {
2197   assert(k != NULL, "");
2198   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
2199     if (_box_klasses[i] == k)
2200       return (BasicType)i;
2201   }
2202   return T_OBJECT;
2203 }
2204 
2205 // Constraints on class loaders. The details of the algorithm can be
2206 // found in the OOPSLA'98 paper "Dynamic Class Loading in the Java
2207 // Virtual Machine" by Sheng Liang and Gilad Bracha.  The basic idea is
2208 // that the dictionary needs to maintain a set of contraints that
2209 // must be satisfied by all classes in the dictionary.
2210 // if defining is true, then LinkageError if already in dictionary
2211 // if initiating loader, then ok if InstanceKlass matches existing entry
2212 
2213 void SystemDictionary::check_constraints(int d_index, unsigned int d_hash,
2214                                          InstanceKlass* k,
2215                                          Handle class_loader, bool defining,
2216                                          TRAPS) {
2217   const char *linkage_error1 = NULL;
2218   const char *linkage_error2 = NULL;
2219   {
2220     Symbol*  name  = k->name();
2221     ClassLoaderData *loader_data = class_loader_data(class_loader);
2222 
2223     MutexLocker mu(SystemDictionary_lock, THREAD);
2224 
2225     InstanceKlass* check = find_class(d_index, d_hash, name, loader_data->dictionary());
2226     if (check != NULL) {
2227       // if different InstanceKlass - duplicate class definition,
2228       // else - ok, class loaded by a different thread in parallel,
2229       // we should only have found it if it was done loading and ok to use
2230       // dictionary only holds instance classes, placeholders
2231       // also holds array classes
2232 
2233       assert(check->is_instance_klass(), "noninstance in systemdictionary");
2234       if ((defining == true) || (k != check)) {
2235         linkage_error1 = "loader (instance of ";
2236         linkage_error2 = "): attempted duplicate class definition for name: \"";
2237       } else {
2238         return;
2239       }
2240     }
2241 
2242 #ifdef ASSERT
2243     Symbol* ph_check = find_placeholder(name, loader_data);
2244     assert(ph_check == NULL || ph_check == name, "invalid symbol");
2245 #endif


2253   }
2254 
2255   // Throw error now if needed (cannot throw while holding
2256   // SystemDictionary_lock because of rank ordering)
2257 
2258   if (linkage_error1) {
2259     ResourceMark rm(THREAD);
2260     const char* class_loader_name = loader_name(class_loader());
2261     char* type_name = k->name()->as_C_string();
2262     size_t buflen = strlen(linkage_error1) + strlen(class_loader_name) +
2263       strlen(linkage_error2) + strlen(type_name) + 2; // +2 for '"' and null byte.
2264     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
2265     jio_snprintf(buf, buflen, "%s%s%s%s\"", linkage_error1, class_loader_name, linkage_error2, type_name);
2266     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
2267   }
2268 }
2269 
2270 
2271 // Update class loader data dictionary - done after check_constraint and add_to_hierachy
2272 // have been called.
2273 void SystemDictionary::update_dictionary(int d_index, unsigned int d_hash,
2274                                          int p_index, unsigned int p_hash,
2275                                          InstanceKlass* k,
2276                                          Handle class_loader,
2277                                          TRAPS) {
2278   // Compile_lock prevents systemDictionary updates during compilations
2279   assert_locked_or_safepoint(Compile_lock);
2280   Symbol*  name  = k->name();
2281   ClassLoaderData *loader_data = class_loader_data(class_loader);
2282 
2283   {
2284     MutexLocker mu1(SystemDictionary_lock, THREAD);
2285 
2286     // See whether biased locking is enabled and if so set it for this
2287     // klass.
2288     // Note that this must be done past the last potential blocking
2289     // point / safepoint. We enable biased locking lazily using a
2290     // VM_Operation to iterate the SystemDictionary and installing the
2291     // biasable mark word into each InstanceKlass's prototype header.
2292     // To avoid race conditions where we accidentally miss enabling the
2293     // optimization for one class in the process of being added to the
2294     // dictionary, we must not safepoint after the test of
2295     // BiasedLocking::enabled().
2296     if (UseBiasedLocking && BiasedLocking::enabled()) {
2297       // Set biased locking bit for all loaded classes; it will be
2298       // cleared if revocation occurs too often for this type
2299       // NOTE that we must only do this when the class is initally
2300       // defined, not each time it is referenced from a new class loader
2301       if (k->class_loader() == class_loader()) {
2302         k->set_prototype_header(markOopDesc::biased_locking_prototype());
2303       }
2304     }
2305 
2306     // Make a new dictionary entry.
2307     Dictionary* dictionary = loader_data->dictionary();
2308     InstanceKlass* sd_check = find_class(d_index, d_hash, name, dictionary);
2309     if (sd_check == NULL) {
2310       dictionary->add_klass(d_index, d_hash, name, k);
2311       notice_modification();
2312     }
2313   #ifdef ASSERT
2314     sd_check = find_class(d_index, d_hash, name, dictionary);
2315     assert (sd_check != NULL, "should have entry in dictionary");
2316     // Note: there may be a placeholder entry: for circularity testing
2317     // or for parallel defines
2318   #endif
2319     SystemDictionary_lock->notify_all();
2320   }
2321 }
2322 
2323 
2324 // Try to find a class name using the loader constraints.  The
2325 // loader constraints might know about a class that isn't fully loaded
2326 // yet and these will be ignored.
2327 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
2328                     Symbol* class_name, Handle class_loader, TRAPS) {
2329 
2330   // First see if it has been loaded directly.
2331   // Force the protection domain to be null.  (This removes protection checks.)
2332   Handle no_protection_domain;
2333   Klass* klass = find_instance_or_array_klass(class_name, class_loader,
2334                                               no_protection_domain, CHECK_NULL);


2371   ClassLoaderData* loader_data2 = class_loader_data(class_loader2);
2372 
2373   Symbol* constraint_name = NULL;
2374   if (!FieldType::is_array(class_name)) {
2375     constraint_name = class_name;
2376   } else {
2377     // For array classes, their Klass*s are not kept in the
2378     // constraint table. The element classes are.
2379     FieldArrayInfo fd;
2380     BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(false));
2381     // primitive types always pass
2382     if (t != T_OBJECT) {
2383       return true;
2384     } else {
2385       constraint_name = fd.object_key();
2386     }
2387   }
2388 
2389   Dictionary* dictionary1 = loader_data1->dictionary();
2390   unsigned int d_hash1 = dictionary1->compute_hash(constraint_name);
2391   int d_index1 = dictionary1->hash_to_index(d_hash1);
2392 
2393   Dictionary* dictionary2 = loader_data2->dictionary();
2394   unsigned int d_hash2 = dictionary2->compute_hash(constraint_name);
2395   int d_index2 = dictionary2->hash_to_index(d_hash2);
2396 
2397   {
2398     MutexLocker mu_s(SystemDictionary_lock, THREAD);
2399     InstanceKlass* klass1 = find_class(d_index1, d_hash1, constraint_name, dictionary1);
2400     InstanceKlass* klass2 = find_class(d_index2, d_hash2, constraint_name, dictionary2);
2401     return constraints()->add_entry(constraint_name, klass1, class_loader1,
2402                                     klass2, class_loader2);
2403   }
2404 }
2405 
2406 // Add entry to resolution error table to record the error when the first
2407 // attempt to resolve a reference to a class has failed.
2408 void SystemDictionary::add_resolution_error(const constantPoolHandle& pool, int which,
2409                                             Symbol* error, Symbol* message) {
2410   unsigned int hash = resolution_errors()->compute_hash(pool, which);
2411   int index = resolution_errors()->hash_to_index(hash);
2412   {
2413     MutexLocker ml(SystemDictionary_lock, Thread::current());
2414     resolution_errors()->add_entry(index, hash, pool, which, error, message);
2415   }
2416 }
2417 
2418 // Delete a resolution error for RedefineClasses for a constant pool is going away
2419 void SystemDictionary::delete_resolution_error(ConstantPool* pool) {
2420   resolution_errors()->delete_entry(pool);




 354 #endif // INCLUDE_CDS
 355 
 356   // Double-check, if child class is already loaded, just return super-class,interface
 357   // Don't add a placedholder if already loaded, i.e. already in appropriate class loader
 358   // dictionary.
 359   // Make sure there's a placeholder for the *child* before resolving.
 360   // Used as a claim that this thread is currently loading superclass/classloader
 361   // Used here for ClassCircularity checks and also for heap verification
 362   // (every InstanceKlass needs to be in its class loader dictionary or have a placeholder).
 363   // Must check ClassCircularity before checking if super class is already loaded.
 364   //
 365   // We might not already have a placeholder if this child_name was
 366   // first seen via resolve_from_stream (jni_DefineClass or JVM_DefineClass);
 367   // the name of the class might not be known until the stream is actually
 368   // parsed.
 369   // Bugs 4643874, 4715493
 370 
 371   ClassLoaderData* loader_data = class_loader_data(class_loader);
 372   Dictionary* dictionary = loader_data->dictionary();
 373   unsigned int d_hash = dictionary->compute_hash(child_name);

 374   unsigned int p_hash = placeholders()->compute_hash(child_name);
 375   int p_index = placeholders()->hash_to_index(p_hash);
 376   // can't throw error holding a lock
 377   bool child_already_loaded = false;
 378   bool throw_circularity_error = false;
 379   {
 380     MutexLocker mu(SystemDictionary_lock, THREAD);
 381     Klass* childk = find_class(d_hash, child_name, dictionary);
 382     Klass* quicksuperk;
 383     // to support // loading: if child done loading, just return superclass
 384     // if class_name, & class_loader don't match:
 385     // if initial define, SD update will give LinkageError
 386     // if redefine: compare_class_versions will give HIERARCHY_CHANGED
 387     // so we don't throw an exception here.
 388     // see: nsk redefclass014 & java.lang.instrument Instrument032
 389     if ((childk != NULL ) && (is_superclass) &&
 390        ((quicksuperk = childk->super()) != NULL) &&
 391 
 392          ((quicksuperk->name() == class_name) &&
 393             (quicksuperk->class_loader()  == class_loader()))) {
 394            return quicksuperk;
 395     } else {
 396       PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, child_name, loader_data);
 397       if (probe && probe->check_seen_thread(THREAD, PlaceholderTable::LOAD_SUPER)) {
 398           throw_circularity_error = true;
 399       }
 400     }
 401     if (!throw_circularity_error) {


 469                          mirror,
 470                          protection_domain,
 471                          THREAD);
 472 
 473   if (HAS_PENDING_EXCEPTION) {
 474     log_debug(protectiondomain)("DENIED !!!!!!!!!!!!!!!!!!!!!");
 475   } else {
 476    log_debug(protectiondomain)("granted");
 477   }
 478 
 479   if (HAS_PENDING_EXCEPTION) return;
 480 
 481   // If no exception has been thrown, we have validated the protection domain
 482   // Insert the protection domain of the initiating class into the set.
 483   {
 484     ClassLoaderData* loader_data = class_loader_data(class_loader);
 485     Dictionary* dictionary = loader_data->dictionary();
 486 
 487     Symbol*  kn = klass->name();
 488     unsigned int d_hash = dictionary->compute_hash(kn);

 489 
 490     MutexLocker mu(SystemDictionary_lock, THREAD);
 491     int d_index = dictionary->hash_to_index(d_hash);
 492     dictionary->add_protection_domain(d_index, d_hash, klass,
 493                                       protection_domain, THREAD);
 494   }
 495 }
 496 
 497 // We only get here if this thread finds that another thread
 498 // has already claimed the placeholder token for the current operation,
 499 // but that other thread either never owned or gave up the
 500 // object lock
 501 // Waits on SystemDictionary_lock to indicate placeholder table updated
 502 // On return, caller must recheck placeholder table state
 503 //
 504 // We only get here if
 505 //  1) custom classLoader, i.e. not bootstrap classloader
 506 //  2) UnsyncloadClass not set
 507 //  3) custom classLoader has broken the class loader objectLock
 508 //     so another thread got here in parallel
 509 //
 510 // lockObject must be held.
 511 // Complicated dance due to lock ordering:


 537 // If the class in is in the placeholder table, class loading is in progress
 538 // For cases where the application changes threads to load classes, it
 539 // is critical to ClassCircularity detection that we try loading
 540 // the superclass on the same thread internally, so we do parallel
 541 // super class loading here.
 542 // This also is critical in cases where the original thread gets stalled
 543 // even in non-circularity situations.
 544 // Note: must call resolve_super_or_fail even if null super -
 545 // to force placeholder entry creation for this class for circularity detection
 546 // Caller must check for pending exception
 547 // Returns non-null Klass* if other thread has completed load
 548 // and we are done,
 549 // If return null Klass* and no pending exception, the caller must load the class
 550 InstanceKlass* SystemDictionary::handle_parallel_super_load(
 551     Symbol* name, Symbol* superclassname, Handle class_loader,
 552     Handle protection_domain, Handle lockObject, TRAPS) {
 553 
 554   ClassLoaderData* loader_data = class_loader_data(class_loader);
 555   Dictionary* dictionary = loader_data->dictionary();
 556   unsigned int d_hash = dictionary->compute_hash(name);

 557   unsigned int p_hash = placeholders()->compute_hash(name);
 558   int p_index = placeholders()->hash_to_index(p_hash);
 559 
 560   // superk is not used, resolve_super called for circularity check only
 561   // This code is reached in two situations. One if this thread
 562   // is loading the same class twice (e.g. ClassCircularity, or
 563   // java.lang.instrument).
 564   // The second is if another thread started the resolve_super first
 565   // and has not yet finished.
 566   // In both cases the original caller will clean up the placeholder
 567   // entry on error.
 568   Klass* superk = SystemDictionary::resolve_super_or_fail(name,
 569                                                           superclassname,
 570                                                           class_loader,
 571                                                           protection_domain,
 572                                                           true,
 573                                                           CHECK_NULL);
 574 
 575   // parallelCapable class loaders do NOT wait for parallel superclass loads to complete
 576   // Serial class loaders and bootstrap classloader do wait for superclass loads
 577  if (!class_loader.is_null() && is_parallelCapable(class_loader)) {
 578     MutexLocker mu(SystemDictionary_lock, THREAD);
 579     // Check if classloading completed while we were loading superclass or waiting
 580     return find_class(d_hash, name, dictionary);
 581   }
 582 
 583   // must loop to both handle other placeholder updates
 584   // and spurious notifications
 585   bool super_load_in_progress = true;
 586   PlaceholderEntry* placeholder;
 587   while (super_load_in_progress) {
 588     MutexLocker mu(SystemDictionary_lock, THREAD);
 589     // Check if classloading completed while we were loading superclass or waiting
 590     InstanceKlass* check = find_class(d_hash, name, dictionary);
 591     if (check != NULL) {
 592       // Klass is already loaded, so just return it
 593       return check;
 594     } else {
 595       placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 596       if (placeholder && placeholder->super_load_in_progress() ){
 597         // Before UnsyncloadClass:
 598         // We only get here if the application has released the
 599         // classloader lock when another thread was in the middle of loading a
 600         // superclass/superinterface for this class, and now
 601         // this thread is also trying to load this class.
 602         // To minimize surprises, the first thread that started to
 603         // load a class should be the one to complete the loading
 604         // with the classfile it initially expected.
 605         // This logic has the current thread wait once it has done
 606         // all the superclass/superinterface loading it can, until
 607         // the original thread completes the class loading or fails
 608         // If it completes we will use the resulting InstanceKlass
 609         // which we will find below in the systemDictionary.
 610         // We also get here for parallel bootstrap classloader


 651 
 652 // Be careful when modifying this code: once you have run
 653 // placeholders()->find_and_add(PlaceholderTable::LOAD_INSTANCE),
 654 // you need to find_and_remove it before returning.
 655 // So be careful to not exit with a CHECK_ macro betweeen these calls.
 656 Klass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
 657                                                         Handle class_loader,
 658                                                         Handle protection_domain,
 659                                                         TRAPS) {
 660   assert(name != NULL && !FieldType::is_array(name) &&
 661          !FieldType::is_obj(name), "invalid class name");
 662 
 663   EventClassLoad class_load_start_event;
 664 
 665   HandleMark hm(THREAD);
 666 
 667   // Fix for 4474172; see evaluation for more details
 668   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
 669   ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
 670   Dictionary* dictionary = loader_data->dictionary();
 671   unsigned int d_hash = dictionary->compute_hash(name);
 672 
 673   // Do lookup to see if class already exist and the protection domain
 674   // has the right access
 675   // This call uses find which checks protection domain already matches
 676   // All subsequent calls use find_class, and set has_loaded_class so that
 677   // before we return a result we call out to java to check for valid protection domain
 678   // to allow returning the Klass* and add it to the pd_set if it is valid
 679   {
 680     Klass* probe = dictionary->find(d_hash, name, protection_domain);

 681     if (probe != NULL) return probe;
 682   }
 683 
 684   // Non-bootstrap class loaders will call out to class loader and
 685   // define via jvm/jni_DefineClass which will acquire the
 686   // class loader object lock to protect against multiple threads
 687   // defining the class in parallel by accident.
 688   // This lock must be acquired here so the waiter will find
 689   // any successful result in the SystemDictionary and not attempt
 690   // the define
 691   // ParallelCapable Classloaders and the bootstrap classloader,
 692   // or all classloaders with UnsyncloadClass do not acquire lock here
 693   bool DoObjectLock = true;
 694   if (is_parallelCapable(class_loader)) {
 695     DoObjectLock = false;
 696   }
 697 
 698   unsigned int p_hash = placeholders()->compute_hash(name);
 699   int p_index = placeholders()->hash_to_index(p_hash);
 700 
 701   // Class is not in SystemDictionary so we have to do loading.
 702   // Make sure we are synchronized on the class loader before we proceed
 703   Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
 704   check_loader_lock_contention(lockObject, THREAD);
 705   ObjectLocker ol(lockObject, THREAD, DoObjectLock);
 706 
 707   // Check again (after locking) if class already exist in SystemDictionary
 708   bool class_has_been_loaded   = false;
 709   bool super_load_in_progress  = false;
 710   bool havesupername = false;
 711   InstanceKlass* k = NULL;
 712   PlaceholderEntry* placeholder;
 713   Symbol* superclassname = NULL;
 714 
 715   {
 716     MutexLocker mu(SystemDictionary_lock, THREAD);
 717     InstanceKlass* check = find_class(d_hash, name, dictionary);
 718     if (check != NULL) {
 719       // Klass is already loaded, so just return it
 720       class_has_been_loaded = true;
 721       k = check;
 722     } else {
 723       placeholder = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 724       if (placeholder && placeholder->super_load_in_progress()) {
 725          super_load_in_progress = true;
 726          if (placeholder->havesupername() == true) {
 727            superclassname = placeholder->supername();
 728            havesupername = true;
 729          }
 730       }
 731     }
 732   }
 733 
 734   // If the class is in the placeholder table, class loading is in progress
 735   if (super_load_in_progress && havesupername==true) {
 736     k = handle_parallel_super_load(name,
 737                                    superclassname,


 781         PlaceholderEntry* oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 782         if (oldprobe) {
 783           // only need check_seen_thread once, not on each loop
 784           // 6341374 java/lang/Instrument with -Xcomp
 785           if (oldprobe->check_seen_thread(THREAD, PlaceholderTable::LOAD_INSTANCE)) {
 786             throw_circularity_error = true;
 787           } else {
 788             // case 1: traditional: should never see load_in_progress.
 789             while (!class_has_been_loaded && oldprobe && oldprobe->instance_load_in_progress()) {
 790 
 791               // case 4: bootstrap classloader: prevent futile classloading,
 792               // wait on first requestor
 793               if (class_loader.is_null()) {
 794                 SystemDictionary_lock->wait();
 795               } else {
 796               // case 2: traditional with broken classloader lock. wait on first
 797               // requestor.
 798                 double_lock_wait(lockObject, THREAD);
 799               }
 800               // Check if classloading completed while we were waiting
 801               InstanceKlass* check = find_class(d_hash, name, dictionary);
 802               if (check != NULL) {
 803                 // Klass is already loaded, so just return it
 804                 k = check;
 805                 class_has_been_loaded = true;
 806               }
 807               // check if other thread failed to load and cleaned up
 808               oldprobe = placeholders()->get_entry(p_index, p_hash, name, loader_data);
 809             }
 810           }
 811         }
 812       }
 813       // All cases: add LOAD_INSTANCE holding SystemDictionary_lock
 814       // case 3: UnsyncloadClass || case 5: parallelCapable: allow competing threads to try
 815       // LOAD_INSTANCE in parallel
 816 
 817       if (!throw_circularity_error && !class_has_been_loaded) {
 818         PlaceholderEntry* newprobe = placeholders()->find_and_add(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, NULL, THREAD);
 819         load_instance_added = true;
 820         // For class loaders that do not acquire the classloader object lock,
 821         // if they did not catch another thread holding LOAD_INSTANCE,
 822         // need a check analogous to the acquire ObjectLocker/find_class
 823         // i.e. now that we hold the LOAD_INSTANCE token on loading this class/CL
 824         // one final check if the load has already completed
 825         // class loaders holding the ObjectLock shouldn't find the class here
 826         InstanceKlass* check = find_class(d_hash, name, dictionary);
 827         if (check != NULL) {
 828         // Klass is already loaded, so return it after checking/adding protection domain
 829           k = check;
 830           class_has_been_loaded = true;
 831         }
 832       }
 833     }
 834 
 835     // must throw error outside of owning lock
 836     if (throw_circularity_error) {
 837       assert(!HAS_PENDING_EXCEPTION && load_instance_added == false,"circularity error cleanup");
 838       ResourceMark rm(THREAD);
 839       THROW_MSG_NULL(vmSymbols::java_lang_ClassCircularityError(), name->as_C_string());
 840     }
 841 
 842     if (!class_has_been_loaded) {
 843 
 844       // Do actual loading
 845       k = load_instance_class(name, class_loader, THREAD);
 846 
 847       // For UnsyncloadClass only
 848       // If they got a linkageError, check if a parallel class load succeeded.
 849       // If it did, then for bytecode resolution the specification requires
 850       // that we return the same result we did for the other thread, i.e. the
 851       // successfully loaded InstanceKlass
 852       // Should not get here for classloaders that support parallelism
 853       // with the new cleaner mechanism, even with AllowParallelDefineClass
 854       // Bootstrap goes through here to allow for an extra guarantee check
 855       if (UnsyncloadClass || (class_loader.is_null())) {
 856         if (k == NULL && HAS_PENDING_EXCEPTION
 857           && PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
 858           MutexLocker mu(SystemDictionary_lock, THREAD);
 859           InstanceKlass* check = find_class(d_hash, name, dictionary);
 860           if (check != NULL) {
 861             // Klass is already loaded, so just use it
 862             k = check;
 863             CLEAR_PENDING_EXCEPTION;
 864             guarantee((!class_loader.is_null()), "dup definition for bootstrap loader?");
 865           }
 866         }
 867       }
 868 
 869       // If everything was OK (no exceptions, no null return value), and
 870       // class_loader is NOT the defining loader, do a little more bookkeeping.
 871       if (!HAS_PENDING_EXCEPTION && k != NULL &&
 872         k->class_loader() != class_loader()) {
 873 
 874         check_constraints(d_hash, k, class_loader, false, THREAD);
 875 
 876         // Need to check for a PENDING_EXCEPTION again; check_constraints
 877         // can throw and doesn't use the CHECK macro.
 878         if (!HAS_PENDING_EXCEPTION) {
 879           { // Grabbing the Compile_lock prevents systemDictionary updates
 880             // during compilations.
 881             MutexLocker mu(Compile_lock, THREAD);
 882             update_dictionary(d_hash, p_index, p_hash,
 883               k, class_loader, THREAD);
 884           }
 885 
 886           if (JvmtiExport::should_post_class_load()) {
 887             Thread *thread = THREAD;
 888             assert(thread->is_Java_thread(), "thread->is_Java_thread()");
 889             JvmtiExport::post_class_load((JavaThread *) thread, k);
 890           }
 891         }
 892       }
 893     } // load_instance_class
 894 
 895     if (load_instance_added == true) {
 896       // clean up placeholder entries for LOAD_INSTANCE success or error
 897       // This brackets the SystemDictionary updates for both defining
 898       // and initiating loaders
 899       MutexLocker mu(SystemDictionary_lock, THREAD);
 900       placeholders()->find_and_remove(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, THREAD);
 901       SystemDictionary_lock->notify_all();
 902     }


 904 
 905   if (HAS_PENDING_EXCEPTION || k == NULL) {
 906     return NULL;
 907   }
 908 
 909   post_class_load_event(&class_load_start_event, k, loader_data);
 910 
 911 #ifdef ASSERT
 912   {
 913     ClassLoaderData* loader_data = k->class_loader_data();
 914     MutexLocker mu(SystemDictionary_lock, THREAD);
 915     Klass* kk = find_class(name, loader_data);
 916     assert(kk == k, "should be present in dictionary");
 917   }
 918 #endif
 919 
 920   // return if the protection domain in NULL
 921   if (protection_domain() == NULL) return k;
 922 
 923   // Check the protection domain has the right access
 924   if (dictionary->is_valid_protection_domain(d_hash, name,
 925                                              protection_domain)) {
 926     return k;
 927   }
 928 
 929   // Verify protection domain. If it fails an exception is thrown
 930   validate_protection_domain(k, class_loader, protection_domain, CHECK_NULL);
 931 
 932   return k;
 933 }
 934 
 935 
 936 // This routine does not lock the system dictionary.
 937 //
 938 // Since readers don't hold a lock, we must make sure that system
 939 // dictionary entries are only removed at a safepoint (when only one
 940 // thread is running), and are added to in a safe way (all links must
 941 // be updated in an MT-safe manner).
 942 //
 943 // Callers should be aware that an entry could be added just after
 944 // _dictionary->bucket(index) is read here, so the caller will not see


 946 
 947 Klass* SystemDictionary::find(Symbol* class_name,
 948                               Handle class_loader,
 949                               Handle protection_domain,
 950                               TRAPS) {
 951 
 952   // The result of this call should be consistent with the result
 953   // of the call to resolve_instance_class_or_null().
 954   // See evaluation 6790209 and 4474172 for more details.
 955   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
 956   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data_or_null(class_loader());
 957 
 958   if (loader_data == NULL) {
 959     // If the ClassLoaderData has not been setup,
 960     // then the class loader has no entries in the dictionary.
 961     return NULL;
 962   }
 963 
 964   Dictionary* dictionary = loader_data->dictionary();
 965   unsigned int d_hash = dictionary->compute_hash(class_name);
 966   return dictionary->find(d_hash, class_name,

 967                           protection_domain);
 968 }
 969 
 970 
 971 // Look for a loaded instance or array klass by name.  Do not do any loading.
 972 // return NULL in case of error.
 973 Klass* SystemDictionary::find_instance_or_array_klass(Symbol* class_name,
 974                                                       Handle class_loader,
 975                                                       Handle protection_domain,
 976                                                       TRAPS) {
 977   Klass* k = NULL;
 978   assert(class_name != NULL, "class name must be non NULL");
 979 
 980   if (FieldType::is_array(class_name)) {
 981     // The name refers to an array.  Parse the name.
 982     // dimension and object_key in FieldArrayInfo are assigned as a
 983     // side-effect of this call
 984     FieldArrayInfo fd;
 985     BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(NULL));
 986     if (t != T_OBJECT) {


1624  // find_or_define_instance_class to get here, we have a timing
1625  // hole with systemDictionary updates and check_constraints
1626  if (!class_loader_h.is_null() && !is_parallelCapable(class_loader_h)) {
1627     assert(ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD,
1628          compute_loader_lock_object(class_loader_h, THREAD)),
1629          "define called without lock");
1630   }
1631 
1632   // Check class-loading constraints. Throw exception if violation is detected.
1633   // Grabs and releases SystemDictionary_lock
1634   // The check_constraints/find_class call and update_dictionary sequence
1635   // must be "atomic" for a specific class/classloader pair so we never
1636   // define two different instanceKlasses for that class/classloader pair.
1637   // Existing classloaders will call define_instance_class with the
1638   // classloader lock held
1639   // Parallel classloaders will call find_or_define_instance_class
1640   // which will require a token to perform the define class
1641   Symbol*  name_h = k->name();
1642   Dictionary* dictionary = loader_data->dictionary();
1643   unsigned int d_hash = dictionary->compute_hash(name_h);
1644   check_constraints(d_hash, k, class_loader_h, true, CHECK);

1645 
1646   // Register class just loaded with class loader (placed in Vector)
1647   // Note we do this before updating the dictionary, as this can
1648   // fail with an OutOfMemoryError (if it does, we will *not* put this
1649   // class in the dictionary and will not update the class hierarchy).
1650   // JVMTI FollowReferences needs to find the classes this way.
1651   if (k->class_loader() != NULL) {
1652     methodHandle m(THREAD, Universe::loader_addClass_method());
1653     JavaValue result(T_VOID);
1654     JavaCallArguments args(class_loader_h);
1655     args.push_oop(Handle(THREAD, k->java_mirror()));
1656     JavaCalls::call(&result, m, &args, CHECK);
1657   }
1658 
1659   // Add the new class. We need recompile lock during update of CHA.
1660   {
1661     unsigned int p_hash = placeholders()->compute_hash(name_h);
1662     int p_index = placeholders()->hash_to_index(p_hash);
1663 
1664     MutexLocker mu_r(Compile_lock, THREAD);
1665 
1666     // Add to class hierarchy, initialize vtables, and do possible
1667     // deoptimizations.
1668     add_to_hierarchy(k, CHECK); // No exception, but can block
1669 
1670     // Add to systemDictionary - so other classes can see it.
1671     // Grabs and releases SystemDictionary_lock
1672     update_dictionary(d_hash, p_index, p_hash,
1673                       k, class_loader_h, THREAD);
1674   }
1675   k->eager_initialize(THREAD);
1676 
1677   // notify jvmti
1678   if (JvmtiExport::should_post_class_load()) {
1679       assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1680       JvmtiExport::post_class_load((JavaThread *) THREAD, k);
1681 
1682   }
1683   class_define_event(k, loader_data);
1684 }
1685 
1686 // Support parallel classloading
1687 // All parallel class loaders, including bootstrap classloader
1688 // lock a placeholder entry for this class/class_loader pair
1689 // to allow parallel defines of different classes for this class loader
1690 // With AllowParallelDefine flag==true, in case they do not synchronize around
1691 // FindLoadedClass/DefineClass, calls, we check for parallel
1692 // loading for them, wait if a defineClass is in progress


1694 // This flag does not apply to the bootstrap classloader.
1695 // With AllowParallelDefine flag==false, call through to define_instance_class
1696 // which will throw LinkageError: duplicate class definition.
1697 // False is the requested default.
1698 // For better performance, the class loaders should synchronize
1699 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
1700 // potentially waste time reading and parsing the bytestream.
1701 // Note: VM callers should ensure consistency of k/class_name,class_loader
1702 // Be careful when modifying this code: once you have run
1703 // placeholders()->find_and_add(PlaceholderTable::DEFINE_CLASS),
1704 // you need to find_and_remove it before returning.
1705 // So be careful to not exit with a CHECK_ macro betweeen these calls.
1706 InstanceKlass* SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader,
1707                                                                InstanceKlass* k, TRAPS) {
1708 
1709   Symbol*  name_h = k->name(); // passed in class_name may be null
1710   ClassLoaderData* loader_data = class_loader_data(class_loader);
1711   Dictionary* dictionary = loader_data->dictionary();
1712 
1713   unsigned int d_hash = dictionary->compute_hash(name_h);

1714 
1715   // Hold SD lock around find_class and placeholder creation for DEFINE_CLASS
1716   unsigned int p_hash = placeholders()->compute_hash(name_h);
1717   int p_index = placeholders()->hash_to_index(p_hash);
1718   PlaceholderEntry* probe;
1719 
1720   {
1721     MutexLocker mu(SystemDictionary_lock, THREAD);
1722     // First check if class already defined
1723     if (UnsyncloadClass || (is_parallelDefine(class_loader))) {
1724       InstanceKlass* check = find_class(d_hash, name_h, dictionary);
1725       if (check != NULL) {
1726         return check;
1727       }
1728     }
1729 
1730     // Acquire define token for this class/classloader
1731     probe = placeholders()->find_and_add(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, NULL, THREAD);
1732     // Wait if another thread defining in parallel
1733     // All threads wait - even those that will throw duplicate class: otherwise
1734     // caller is surprised by LinkageError: duplicate, but findLoadedClass fails
1735     // if other thread has not finished updating dictionary
1736     while (probe->definer() != NULL) {
1737       SystemDictionary_lock->wait();
1738     }
1739     // Only special cases allow parallel defines and can use other thread's results
1740     // Other cases fall through, and may run into duplicate defines
1741     // caught by finding an entry in the SystemDictionary
1742     if ((UnsyncloadClass || is_parallelDefine(class_loader)) && (probe->instance_klass() != NULL)) {
1743         placeholders()->find_and_remove(p_index, p_hash, name_h, loader_data, PlaceholderTable::DEFINE_CLASS, THREAD);
1744         SystemDictionary_lock->notify_all();
1745 #ifdef ASSERT
1746         InstanceKlass* check = find_class(d_hash, name_h, dictionary);
1747         assert(check != NULL, "definer missed recording success");
1748 #endif
1749         return probe->instance_klass();
1750     } else {
1751       // This thread will define the class (even if earlier thread tried and had an error)
1752       probe->set_definer(THREAD);
1753     }
1754   }
1755 
1756   define_instance_class(k, THREAD);
1757 
1758   Handle linkage_exception = Handle(); // null handle
1759 
1760   // definer must notify any waiting threads
1761   {
1762     MutexLocker mu(SystemDictionary_lock, THREAD);
1763     PlaceholderEntry* probe = placeholders()->get_entry(p_index, p_hash, name_h, loader_data);
1764     assert(probe != NULL, "DEFINE_CLASS placeholder lost?");
1765     if (probe != NULL) {
1766       if (HAS_PENDING_EXCEPTION) {


1801     return;
1802   }
1803 
1804   assert(!loader_lock.is_null(), "NULL lock object");
1805 
1806   if (ObjectSynchronizer::query_lock_ownership((JavaThread*)THREAD, loader_lock)
1807       == ObjectSynchronizer::owner_other) {
1808     // contention will likely happen, so increment the corresponding
1809     // contention counter.
1810     if (loader_lock() == _system_loader_lock_obj) {
1811       ClassLoader::sync_systemLoaderLockContentionRate()->inc();
1812     } else {
1813       ClassLoader::sync_nonSystemLoaderLockContentionRate()->inc();
1814     }
1815   }
1816 }
1817 
1818 // ----------------------------------------------------------------------------
1819 // Lookup
1820 
1821 InstanceKlass* SystemDictionary::find_class(unsigned int hash,
1822                                             Symbol* class_name,
1823                                             Dictionary* dictionary) {
1824   assert_locked_or_safepoint(SystemDictionary_lock);
1825   int index = dictionary->hash_to_index(hash);
1826   return dictionary->find_class(index, hash, class_name);
1827 }
1828 
1829 
1830 // Basic find on classes in the midst of being loaded
1831 Symbol* SystemDictionary::find_placeholder(Symbol* class_name,
1832                                            ClassLoaderData* loader_data) {
1833   assert_locked_or_safepoint(SystemDictionary_lock);
1834   unsigned int p_hash = placeholders()->compute_hash(class_name);
1835   int p_index = placeholders()->hash_to_index(p_hash);
1836   return placeholders()->find_entry(p_index, p_hash, class_name, loader_data);
1837 }
1838 
1839 
1840 // Used for assertions and verification only
1841 // Precalculating the hash and index is an optimization because there are many lookups
1842 // before adding the class.
1843 InstanceKlass* SystemDictionary::find_class(Symbol* class_name, ClassLoaderData* loader_data) {
1844   assert_locked_or_safepoint(SystemDictionary_lock);
1845   #ifndef ASSERT
1846   guarantee(VerifyBeforeGC      ||
1847             VerifyDuringGC      ||
1848             VerifyBeforeExit    ||
1849             VerifyDuringStartup ||
1850             VerifyAfterGC, "too expensive");
1851   #endif
1852 
1853   Dictionary* dictionary = loader_data->dictionary();
1854   unsigned int d_hash = dictionary->compute_hash(class_name);
1855   return find_class(d_hash, class_name, dictionary);

1856 }
1857 
1858 
1859 // ----------------------------------------------------------------------------
1860 // Update hierachy. This is done before the new klass has been added to the SystemDictionary. The Recompile_lock
1861 // is held, to ensure that the compiler is not using the class hierachy, and that deoptimization will kick in
1862 // before a new class is used.
1863 
1864 void SystemDictionary::add_to_hierarchy(InstanceKlass* k, TRAPS) {
1865   assert(k != NULL, "just checking");
1866   assert_locked_or_safepoint(Compile_lock);
1867 
1868   // Link into hierachy. Make sure the vtables are initialized before linking into
1869   k->append_to_sibling_list();                    // add to superklass/sibling list
1870   k->process_interfaces(THREAD);                  // handle all "implements" declarations
1871   k->set_init_state(InstanceKlass::loaded);
1872   // Now flush all code that depended on old class hierarchy.
1873   // Note: must be done *after* linking k into the hierarchy (was bug 12/9/97)
1874   // Also, first reinitialize vtable because it may have gotten out of synch
1875   // while the new class wasn't connected to the class hierarchy.


2188 
2189 // Tells if a given klass is a box (wrapper class, such as java.lang.Integer).
2190 // If so, returns the basic type it holds.  If not, returns T_OBJECT.
2191 BasicType SystemDictionary::box_klass_type(Klass* k) {
2192   assert(k != NULL, "");
2193   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
2194     if (_box_klasses[i] == k)
2195       return (BasicType)i;
2196   }
2197   return T_OBJECT;
2198 }
2199 
2200 // Constraints on class loaders. The details of the algorithm can be
2201 // found in the OOPSLA'98 paper "Dynamic Class Loading in the Java
2202 // Virtual Machine" by Sheng Liang and Gilad Bracha.  The basic idea is
2203 // that the dictionary needs to maintain a set of contraints that
2204 // must be satisfied by all classes in the dictionary.
2205 // if defining is true, then LinkageError if already in dictionary
2206 // if initiating loader, then ok if InstanceKlass matches existing entry
2207 
2208 void SystemDictionary::check_constraints(unsigned int d_hash,
2209                                          InstanceKlass* k,
2210                                          Handle class_loader, bool defining,
2211                                          TRAPS) {
2212   const char *linkage_error1 = NULL;
2213   const char *linkage_error2 = NULL;
2214   {
2215     Symbol*  name  = k->name();
2216     ClassLoaderData *loader_data = class_loader_data(class_loader);
2217 
2218     MutexLocker mu(SystemDictionary_lock, THREAD);
2219 
2220     InstanceKlass* check = find_class(d_hash, name, loader_data->dictionary());
2221     if (check != NULL) {
2222       // if different InstanceKlass - duplicate class definition,
2223       // else - ok, class loaded by a different thread in parallel,
2224       // we should only have found it if it was done loading and ok to use
2225       // dictionary only holds instance classes, placeholders
2226       // also holds array classes
2227 
2228       assert(check->is_instance_klass(), "noninstance in systemdictionary");
2229       if ((defining == true) || (k != check)) {
2230         linkage_error1 = "loader (instance of ";
2231         linkage_error2 = "): attempted duplicate class definition for name: \"";
2232       } else {
2233         return;
2234       }
2235     }
2236 
2237 #ifdef ASSERT
2238     Symbol* ph_check = find_placeholder(name, loader_data);
2239     assert(ph_check == NULL || ph_check == name, "invalid symbol");
2240 #endif


2248   }
2249 
2250   // Throw error now if needed (cannot throw while holding
2251   // SystemDictionary_lock because of rank ordering)
2252 
2253   if (linkage_error1) {
2254     ResourceMark rm(THREAD);
2255     const char* class_loader_name = loader_name(class_loader());
2256     char* type_name = k->name()->as_C_string();
2257     size_t buflen = strlen(linkage_error1) + strlen(class_loader_name) +
2258       strlen(linkage_error2) + strlen(type_name) + 2; // +2 for '"' and null byte.
2259     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
2260     jio_snprintf(buf, buflen, "%s%s%s%s\"", linkage_error1, class_loader_name, linkage_error2, type_name);
2261     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
2262   }
2263 }
2264 
2265 
2266 // Update class loader data dictionary - done after check_constraint and add_to_hierachy
2267 // have been called.
2268 void SystemDictionary::update_dictionary(unsigned int d_hash,
2269                                          int p_index, unsigned int p_hash,
2270                                          InstanceKlass* k,
2271                                          Handle class_loader,
2272                                          TRAPS) {
2273   // Compile_lock prevents systemDictionary updates during compilations
2274   assert_locked_or_safepoint(Compile_lock);
2275   Symbol*  name  = k->name();
2276   ClassLoaderData *loader_data = class_loader_data(class_loader);
2277 
2278   {
2279     MutexLocker mu1(SystemDictionary_lock, THREAD);
2280 
2281     // See whether biased locking is enabled and if so set it for this
2282     // klass.
2283     // Note that this must be done past the last potential blocking
2284     // point / safepoint. We enable biased locking lazily using a
2285     // VM_Operation to iterate the SystemDictionary and installing the
2286     // biasable mark word into each InstanceKlass's prototype header.
2287     // To avoid race conditions where we accidentally miss enabling the
2288     // optimization for one class in the process of being added to the
2289     // dictionary, we must not safepoint after the test of
2290     // BiasedLocking::enabled().
2291     if (UseBiasedLocking && BiasedLocking::enabled()) {
2292       // Set biased locking bit for all loaded classes; it will be
2293       // cleared if revocation occurs too often for this type
2294       // NOTE that we must only do this when the class is initally
2295       // defined, not each time it is referenced from a new class loader
2296       if (k->class_loader() == class_loader()) {
2297         k->set_prototype_header(markOopDesc::biased_locking_prototype());
2298       }
2299     }
2300 
2301     // Make a new dictionary entry.
2302     Dictionary* dictionary = loader_data->dictionary();
2303     InstanceKlass* sd_check = find_class(d_hash, name, dictionary);
2304     if (sd_check == NULL) {
2305       dictionary->add_klass(d_hash, name, k);
2306       notice_modification();
2307     }
2308   #ifdef ASSERT
2309     sd_check = find_class(d_hash, name, dictionary);
2310     assert (sd_check != NULL, "should have entry in dictionary");
2311     // Note: there may be a placeholder entry: for circularity testing
2312     // or for parallel defines
2313   #endif
2314     SystemDictionary_lock->notify_all();
2315   }
2316 }
2317 
2318 
2319 // Try to find a class name using the loader constraints.  The
2320 // loader constraints might know about a class that isn't fully loaded
2321 // yet and these will be ignored.
2322 Klass* SystemDictionary::find_constrained_instance_or_array_klass(
2323                     Symbol* class_name, Handle class_loader, TRAPS) {
2324 
2325   // First see if it has been loaded directly.
2326   // Force the protection domain to be null.  (This removes protection checks.)
2327   Handle no_protection_domain;
2328   Klass* klass = find_instance_or_array_klass(class_name, class_loader,
2329                                               no_protection_domain, CHECK_NULL);


2366   ClassLoaderData* loader_data2 = class_loader_data(class_loader2);
2367 
2368   Symbol* constraint_name = NULL;
2369   if (!FieldType::is_array(class_name)) {
2370     constraint_name = class_name;
2371   } else {
2372     // For array classes, their Klass*s are not kept in the
2373     // constraint table. The element classes are.
2374     FieldArrayInfo fd;
2375     BasicType t = FieldType::get_array_info(class_name, fd, CHECK_(false));
2376     // primitive types always pass
2377     if (t != T_OBJECT) {
2378       return true;
2379     } else {
2380       constraint_name = fd.object_key();
2381     }
2382   }
2383 
2384   Dictionary* dictionary1 = loader_data1->dictionary();
2385   unsigned int d_hash1 = dictionary1->compute_hash(constraint_name);

2386 
2387   Dictionary* dictionary2 = loader_data2->dictionary();
2388   unsigned int d_hash2 = dictionary2->compute_hash(constraint_name);

2389 
2390   {
2391     MutexLocker mu_s(SystemDictionary_lock, THREAD);
2392     InstanceKlass* klass1 = find_class(d_hash1, constraint_name, dictionary1);
2393     InstanceKlass* klass2 = find_class(d_hash2, constraint_name, dictionary2);
2394     return constraints()->add_entry(constraint_name, klass1, class_loader1,
2395                                     klass2, class_loader2);
2396   }
2397 }
2398 
2399 // Add entry to resolution error table to record the error when the first
2400 // attempt to resolve a reference to a class has failed.
2401 void SystemDictionary::add_resolution_error(const constantPoolHandle& pool, int which,
2402                                             Symbol* error, Symbol* message) {
2403   unsigned int hash = resolution_errors()->compute_hash(pool, which);
2404   int index = resolution_errors()->hash_to_index(hash);
2405   {
2406     MutexLocker ml(SystemDictionary_lock, Thread::current());
2407     resolution_errors()->add_entry(index, hash, pool, which, error, message);
2408   }
2409 }
2410 
2411 // Delete a resolution error for RedefineClasses for a constant pool is going away
2412 void SystemDictionary::delete_resolution_error(ConstantPool* pool) {
2413   resolution_errors()->delete_entry(pool);


src/hotspot/share/classfile/systemDictionary.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File