< prev index next >

src/hotspot/share/oops/instanceKlass.cpp

Print this page




  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "aot/aotLoader.hpp"
  28 #include "classfile/classFileParser.hpp"
  29 #include "classfile/classFileStream.hpp"
  30 #include "classfile/classLoader.hpp"
  31 #include "classfile/classLoaderData.inline.hpp"
  32 #include "classfile/javaClasses.hpp"
  33 #include "classfile/moduleEntry.hpp"
  34 #include "classfile/symbolTable.hpp"
  35 #include "classfile/systemDictionary.hpp"
  36 #include "classfile/systemDictionaryShared.hpp"
  37 #include "classfile/verifier.hpp"
  38 #include "classfile/vmSymbols.hpp"
  39 #include "code/dependencyContext.hpp"
  40 #include "compiler/compileBroker.hpp"
  41 #include "gc/shared/collectedHeap.inline.hpp"
  42 #include "interpreter/oopMapCache.hpp"
  43 #include "interpreter/rewriter.hpp"
  44 #include "jvmtifiles/jvmti.h"
  45 #include "logging/log.hpp"
  46 #include "logging/logMessage.hpp"
  47 #include "logging/logStream.hpp"
  48 #include "memory/allocation.inline.hpp"
  49 #include "memory/heapInspection.hpp"
  50 #include "memory/iterator.inline.hpp"
  51 #include "memory/metadataFactory.hpp"
  52 #include "memory/metaspaceClosure.hpp"
  53 #include "memory/metaspaceShared.hpp"
  54 #include "memory/oopFactory.hpp"
  55 #include "memory/resourceArea.hpp"
  56 #include "memory/universe.hpp"
  57 #include "oops/fieldStreams.hpp"
  58 #include "oops/constantPool.hpp"
  59 #include "oops/instanceClassLoaderKlass.hpp"
  60 #include "oops/instanceKlass.inline.hpp"
  61 #include "oops/instanceMirrorKlass.hpp"
  62 #include "oops/instanceOop.hpp"
  63 #include "oops/klass.inline.hpp"
  64 #include "oops/method.hpp"
  65 #include "oops/oop.inline.hpp"
  66 #include "oops/symbol.hpp"
  67 #include "prims/jvmtiExport.hpp"
  68 #include "prims/jvmtiRedefineClasses.hpp"
  69 #include "prims/jvmtiThreadState.hpp"
  70 #include "prims/methodComparator.hpp"
  71 #include "runtime/atomic.hpp"
  72 #include "runtime/fieldDescriptor.inline.hpp"
  73 #include "runtime/handles.inline.hpp"
  74 #include "runtime/javaCalls.hpp"
  75 #include "runtime/mutexLocker.hpp"
  76 #include "runtime/orderAccess.hpp"


 434   Klass(id),
 435   _nest_members(NULL),
 436   _nest_host_index(0),
 437   _nest_host(NULL),
 438   _static_field_size(parser.static_field_size()),
 439   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 440   _itable_len(parser.itable_size()),
 441   _reference_type(parser.reference_type())
 442 {
 443   set_vtable_length(parser.vtable_size());
 444   set_kind(kind);
 445   set_access_flags(parser.access_flags());
 446   set_is_unsafe_anonymous(parser.is_unsafe_anonymous());
 447   set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 448                                                     false));
 449 
 450   assert(NULL == _methods, "underlying memory not zeroed?");
 451   assert(is_instance_klass(), "is layout incorrect?");
 452   assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
 453 
 454   if (DumpSharedSpaces || DynamicDumpSharedSpaces) {
 455     SystemDictionaryShared::init_dumptime_info(this);
 456   }
 457 }
 458 
 459 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
 460                                        Array<Method*>* methods) {
 461   if (methods != NULL && methods != Universe::the_empty_method_array() &&
 462       !methods->is_shared()) {
 463     for (int i = 0; i < methods->length(); i++) {
 464       Method* method = methods->at(i);
 465       if (method == NULL) continue;  // maybe null if error processing
 466       // Only want to delete methods that are not executing for RedefineClasses.
 467       // The previous version will point to them so they're not totally dangling
 468       assert (!method->on_stack(), "shouldn't be called with methods on stack");
 469       MetadataFactory::free_metadata(loader_data, method);
 470     }
 471     MetadataFactory::free_array<Method*>(loader_data, methods);
 472   }
 473 }
 474 


 584   if (inner_classes() != NULL &&
 585       inner_classes() != Universe::the_empty_short_array() &&
 586       !inner_classes()->is_shared()) {
 587     MetadataFactory::free_array<jushort>(loader_data, inner_classes());
 588   }
 589   set_inner_classes(NULL);
 590 
 591   if (nest_members() != NULL &&
 592       nest_members() != Universe::the_empty_short_array() &&
 593       !nest_members()->is_shared()) {
 594     MetadataFactory::free_array<jushort>(loader_data, nest_members());
 595   }
 596   set_nest_members(NULL);
 597 
 598   // We should deallocate the Annotations instance if it's not in shared spaces.
 599   if (annotations() != NULL && !annotations()->is_shared()) {
 600     MetadataFactory::free_metadata(loader_data, annotations());
 601   }
 602   set_annotations(NULL);
 603 
 604   if (DumpSharedSpaces || DynamicDumpSharedSpaces) {
 605     SystemDictionaryShared::remove_dumptime_info(this);
 606   }
 607 }
 608 
 609 bool InstanceKlass::should_be_initialized() const {
 610   return !is_initialized();
 611 }
 612 
 613 klassItable InstanceKlass::itable() const {
 614   return klassItable(const_cast<InstanceKlass*>(this));
 615 }
 616 
 617 void InstanceKlass::eager_initialize(Thread *thread) {
 618   if (!EagerInitialization) return;
 619 
 620   if (this->is_not_initialized()) {
 621     // abort if the the class has a class initializer
 622     if (this->class_initializer() != NULL) return;
 623 
 624     // abort if it is java.lang.Object (initialization is handled in genesis)


2172         Klass* volatile* klass = adr_implementor();
2173         if (Atomic::cmpxchg((Klass*)NULL, klass, impl) == impl) {
2174           // Successfully unlinking implementor.
2175           if (log_is_enabled(Trace, class, unload)) {
2176             ResourceMark rm;
2177             log_trace(class, unload)("unlinking class (implementor): %s", impl->external_name());
2178           }
2179           return;
2180         }
2181       } else {
2182         return;
2183       }
2184     }
2185   }
2186 }
2187 
2188 void InstanceKlass::clean_method_data() {
2189   for (int m = 0; m < methods()->length(); m++) {
2190     MethodData* mdo = methods()->at(m)->method_data();
2191     if (mdo != NULL) {
2192       MutexLocker ml(SafepointSynchronize::is_at_safepoint() ? NULL : mdo->extra_data_lock());
2193       mdo->clean_method_data(/*always_clean*/false);
2194     }
2195   }
2196 }
2197 
2198 bool InstanceKlass::supers_have_passed_fingerprint_checks() {
2199   if (java_super() != NULL && !java_super()->has_passed_fingerprint_check()) {
2200     ResourceMark rm;
2201     log_trace(class, fingerprint)("%s : super %s not fingerprinted", external_name(), java_super()->external_name());
2202     return false;
2203   }
2204 
2205   Array<InstanceKlass*>* local_interfaces = this->local_interfaces();
2206   if (local_interfaces != NULL) {
2207     int length = local_interfaces->length();
2208     for (int i = 0; i < length; i++) {
2209       InstanceKlass* intf = local_interfaces->at(i);
2210       if (!intf->has_passed_fingerprint_check()) {
2211         ResourceMark rm;
2212         log_trace(class, fingerprint)("%s : interface %s not fingerprinted", external_name(), intf->external_name());
2213         return false;
2214       }
2215     }
2216   }
2217 
2218   return true;
2219 }
2220 
2221 bool InstanceKlass::should_store_fingerprint(bool is_unsafe_anonymous) {
2222 #if INCLUDE_AOT
2223   // We store the fingerprint into the InstanceKlass only in the following 2 cases:
2224   if (CalculateClassFingerprint) {
2225     // (1) We are running AOT to generate a shared library.
2226     return true;
2227   }
2228   if (DumpSharedSpaces || DynamicDumpSharedSpaces) {
2229     // (2) We are running -Xshare:dump or -XX:ArchiveClassesAtExit to create a shared archive
2230     return true;
2231   }
2232   if (UseAOT && is_unsafe_anonymous) {
2233     // (3) We are using AOT code from a shared library and see an unsafe anonymous class
2234     return true;
2235   }
2236 #endif
2237 
2238   // In all other cases we might set the _misc_has_passed_fingerprint_check bit,
2239   // but do not store the 64-bit fingerprint to save space.
2240   return false;
2241 }
2242 
2243 bool InstanceKlass::has_stored_fingerprint() const {
2244 #if INCLUDE_AOT
2245   return should_store_fingerprint() || is_shared();
2246 #else
2247   return false;
2248 #endif
2249 }


2329   // being added to class hierarchy (see SystemDictionary:::add_to_hierarchy()).
2330   _init_state = allocated;
2331 
2332   {
2333     MutexLocker ml(Compile_lock);
2334     init_implementor();
2335   }
2336 
2337   constants()->remove_unshareable_info();
2338 
2339   for (int i = 0; i < methods()->length(); i++) {
2340     Method* m = methods()->at(i);
2341     m->remove_unshareable_info();
2342   }
2343 
2344   // do array classes also.
2345   if (array_klasses() != NULL) {
2346     array_klasses()->remove_unshareable_info();
2347   }
2348 
2349   // These are not allocated from metaspace. They are safe to set to NULL.
2350   _source_debug_extension = NULL;
2351   _dep_context = NULL;
2352   _osr_nmethods_head = NULL;


2353 #if INCLUDE_JVMTI
2354   _breakpoints = NULL;
2355   _previous_versions = NULL;
2356   _cached_class_file = NULL;
2357 #endif
2358 
2359   _init_thread = NULL;
2360   _methods_jmethod_ids = NULL;
2361   _jni_ids = NULL;
2362   _oop_map_cache = NULL;
2363   // clear _nest_host to ensure re-load at runtime
2364   _nest_host = NULL;
2365 }
2366 
2367 void InstanceKlass::remove_java_mirror() {
2368   Klass::remove_java_mirror();
2369 
2370   // do array classes also.
2371   if (array_klasses() != NULL) {
2372     array_klasses()->remove_java_mirror();
2373   }
2374 }
2375 


2456 }
2457 
2458 #if INCLUDE_JVMTI
2459 static void clear_all_breakpoints(Method* m) {
2460   m->clear_all_breakpoints();
2461 }
2462 #endif
2463 
2464 void InstanceKlass::unload_class(InstanceKlass* ik) {
2465   // Release dependencies.
2466   ik->dependencies().remove_all_dependents();
2467 
2468   // notify the debugger
2469   if (JvmtiExport::should_post_class_unload()) {
2470     JvmtiExport::post_class_unload(ik);
2471   }
2472 
2473   // notify ClassLoadingService of class unload
2474   ClassLoadingService::notify_class_unloaded(ik);
2475 
2476   if (DumpSharedSpaces || DynamicDumpSharedSpaces) {
2477     SystemDictionaryShared::remove_dumptime_info(ik);
2478   }
2479 
2480   if (log_is_enabled(Info, class, unload)) {
2481     ResourceMark rm;
2482     log_info(class, unload)("unloading class %s " INTPTR_FORMAT, ik->external_name(), p2i(ik));
2483   }
2484 
2485   Events::log_class_unloading(Thread::current(), ik);
2486 
2487 #if INCLUDE_JFR
2488   assert(ik != NULL, "invariant");
2489   EventClassUnload event;
2490   event.set_unloadedClass(ik);
2491   event.set_definingClassLoader(ik->class_loader_data());
2492   event.commit();
2493 #endif
2494 }
2495 
2496 void InstanceKlass::release_C_heap_structures(InstanceKlass* ik) {
2497   // Clean up C heap
2498   ik->release_C_heap_structures();
2499   ik->constants()->release_C_heap_structures();


2596 
2597   // Add the semicolon and the NULL
2598   dest[dest_index++] = ';';
2599   dest[dest_index] = '\0';
2600   return dest;
2601 }
2602 
2603 // Used to obtain the package name from a fully qualified class name.
2604 Symbol* InstanceKlass::package_from_name(const Symbol* name, TRAPS) {
2605   if (name == NULL) {
2606     return NULL;
2607   } else {
2608     if (name->utf8_length() <= 0) {
2609       return NULL;
2610     }
2611     ResourceMark rm;
2612     const char* package_name = ClassLoader::package_from_name((const char*) name->as_C_string());
2613     if (package_name == NULL) {
2614       return NULL;
2615     }
2616     Symbol* pkg_name = SymbolTable::new_symbol(package_name);
2617     return pkg_name;
2618   }
2619 }
2620 
2621 ModuleEntry* InstanceKlass::module() const {
2622   // For an unsafe anonymous class return the host class' module
2623   if (is_unsafe_anonymous()) {
2624     assert(unsafe_anonymous_host() != NULL, "unsafe anonymous class must have a host class");
2625     return unsafe_anonymous_host()->module();
2626   }
2627 
2628   // Class is in a named package
2629   if (!in_unnamed_package()) {
2630     return _package_entry->module();
2631   }
2632 
2633   // Class is in an unnamed package, return its loader's unnamed module
2634   return class_loader_data()->unnamed_module();
2635 }
2636 


2952 
2953       if (log_is_enabled(Info, redefine, class, update)) {
2954         ResourceMark rm;
2955         if (!(*trace_name_printed)) {
2956           log_info(redefine, class, update)
2957             ("adjust: klassname=%s default methods from name=%s",
2958              external_name(), old_method->method_holder()->external_name());
2959           *trace_name_printed = true;
2960         }
2961         log_debug(redefine, class, update, vtables)
2962           ("default method update: %s(%s) ",
2963            new_method->name()->as_C_string(), new_method->signature()->as_C_string());
2964       }
2965     }
2966   }
2967 }
2968 #endif // INCLUDE_JVMTI
2969 
2970 // On-stack replacement stuff
2971 void InstanceKlass::add_osr_nmethod(nmethod* n) {
2972 #ifndef PRODUCT
2973   if (TieredCompilation) {
2974       nmethod * prev = lookup_osr_nmethod(n->method(), n->osr_entry_bci(), n->comp_level(), true);
2975       assert(prev == NULL || !prev->is_in_use(),
2976       "redundunt OSR recompilation detected. memory leak in CodeCache!");
2977   }
2978 #endif
2979   // only one compilation can be active
2980   {
2981     // This is a short non-blocking critical region, so the no safepoint check is ok.
2982     MutexLocker ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
2983     assert(n->is_osr_method(), "wrong kind of nmethod");
2984     n->set_osr_link(osr_nmethods_head());
2985     set_osr_nmethods_head(n);
2986     // Raise the highest osr level if necessary
2987     if (TieredCompilation) {
2988       Method* m = n->method();
2989       m->set_highest_osr_comp_level(MAX2(m->highest_osr_comp_level(), n->comp_level()));
2990     }
2991   }
2992 
2993   // Get rid of the osr methods for the same bci that have lower levels.
2994   if (TieredCompilation) {
2995     for (int l = CompLevel_limited_profile; l < n->comp_level(); l++) {
2996       nmethod *inv = lookup_osr_nmethod(n->method(), n->osr_entry_bci(), l, true);
2997       if (inv != NULL && inv->is_in_use()) {
2998         inv->make_not_entrant();
2999       }
3000     }
3001   }
3002 }
3003 
3004 // Remove osr nmethod from the list. Return true if found and removed.
3005 bool InstanceKlass::remove_osr_nmethod(nmethod* n) {
3006   // This is a short non-blocking critical region, so the no safepoint check is ok.
3007   MutexLocker ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
3008   assert(n->is_osr_method(), "wrong kind of nmethod");
3009   nmethod* last = NULL;
3010   nmethod* cur  = osr_nmethods_head();
3011   int max_level = CompLevel_none;  // Find the max comp level excluding n
3012   Method* m = n->method();
3013   // Search for match
3014   bool found = false;
3015   while(cur != NULL && cur != n) {
3016     if (TieredCompilation && m == cur->method()) {
3017       // Find max level before n
3018       max_level = MAX2(max_level, cur->comp_level());
3019     }
3020     last = cur;
3021     cur = cur->osr_link();
3022   }
3023   nmethod* next = NULL;
3024   if (cur == n) {
3025     found = true;
3026     next = cur->osr_link();
3027     if (last == NULL) {


3031       last->set_osr_link(next);
3032     }
3033   }
3034   n->set_osr_link(NULL);
3035   if (TieredCompilation) {
3036     cur = next;
3037     while (cur != NULL) {
3038       // Find max level after n
3039       if (m == cur->method()) {
3040         max_level = MAX2(max_level, cur->comp_level());
3041       }
3042       cur = cur->osr_link();
3043     }
3044     m->set_highest_osr_comp_level(max_level);
3045   }
3046   return found;
3047 }
3048 
3049 int InstanceKlass::mark_osr_nmethods(const Method* m) {
3050   // This is a short non-blocking critical region, so the no safepoint check is ok.
3051   MutexLocker ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
3052   nmethod* osr = osr_nmethods_head();
3053   int found = 0;
3054   while (osr != NULL) {
3055     assert(osr->is_osr_method(), "wrong kind of nmethod found in chain");
3056     if (osr->method() == m) {
3057       osr->mark_for_deoptimization();
3058       found++;
3059     }
3060     osr = osr->osr_link();
3061   }
3062   return found;
3063 }
3064 
3065 nmethod* InstanceKlass::lookup_osr_nmethod(const Method* m, int bci, int comp_level, bool match_level) const {
3066   // This is a short non-blocking critical region, so the no safepoint check is ok.
3067   MutexLocker ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
3068   nmethod* osr = osr_nmethods_head();
3069   nmethod* best = NULL;
3070   while (osr != NULL) {
3071     assert(osr->is_osr_method(), "wrong kind of nmethod found in chain");
3072     // There can be a time when a c1 osr method exists but we are waiting
3073     // for a c2 version. When c2 completes its osr nmethod we will trash
3074     // the c1 version and only be able to find the c2 version. However
3075     // while we overflow in the c1 code at back branches we don't want to
3076     // try and switch to the same code as we are already running
3077 
3078     if (osr->method() == m &&
3079         (bci == InvocationEntryBci || osr->osr_entry_bci() == bci)) {
3080       if (match_level) {
3081         if (osr->comp_level() == comp_level) {
3082           // Found a match - return it.
3083           return osr;
3084         }
3085       } else {
3086         if (best == NULL || (osr->comp_level() > best->comp_level())) {
3087           if (osr->comp_level() == CompLevel_highest_tier) {
3088             // Found the best possible - return it.
3089             return osr;
3090           }
3091           best = osr;
3092         }
3093       }
3094     }
3095     osr = osr->osr_link();
3096   }
3097 
3098   assert(match_level == false || best == NULL, "shouldn't pick up anything if match_level is set");
3099   if (best != NULL && best->comp_level() >= comp_level) {
3100     return best;
3101   }
3102   return NULL;
3103 }
3104 
3105 // -----------------------------------------------------------------------------------------------------
3106 // Printing
3107 
3108 #ifndef PRODUCT
3109 
3110 #define BULLET  " - "
3111 
3112 static const char* state_names[] = {
3113   "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
3114 };
3115 
3116 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3117   for (int i = 0; i < len; i++) {
3118     intptr_t e = start[i];
3119     st->print("%d : " INTPTR_FORMAT, i, e);
3120     if (MetaspaceObj::is_valid((Metadata*)e)) {
3121       st->print(" ");
3122       ((Metadata*)e)->print_value_on(st);
3123     }
3124     st->cr();
3125   }
3126 }
3127 
3128 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3129   return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3130 }
3131 
3132 void InstanceKlass::print_on(outputStream* st) const {
3133   assert(is_klass(), "must be klass");
3134   Klass::print_on(st);
3135 
3136   st->print(BULLET"instance size:     %d", size_helper());                        st->cr();
3137   st->print(BULLET"klass size:        %d", size());                               st->cr();
3138   st->print(BULLET"access:            "); access_flags().print_on(st);            st->cr();
3139   st->print(BULLET"state:             "); st->print_cr("%s", state_names[_init_state]);
3140   st->print(BULLET"name:              "); name()->print_value_on(st);             st->cr();


3407       } else {
3408         info_stream.print(" source: %s", cfs->source());
3409       }
3410     } else if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
3411       Thread* THREAD = Thread::current();
3412       Klass* caller =
3413             THREAD->is_Java_thread()
3414                 ? ((JavaThread*)THREAD)->security_get_caller_class(1)
3415                 : NULL;
3416       // caller can be NULL, for example, during a JVMTI VM_Init hook
3417       if (caller != NULL) {
3418         info_stream.print(" source: instance of %s", caller->external_name());
3419       } else {
3420         // source is unknown
3421       }
3422     } else {
3423       oop class_loader = loader_data->class_loader();
3424       info_stream.print(" source: %s", class_loader->klass()->external_name());
3425     }
3426   } else {
3427     assert(this->is_shared(), "must be");
3428     if (MetaspaceShared::is_shared_dynamic((void*)this)) {
3429       info_stream.print(" source: shared objects file (top)");
3430     } else {
3431       info_stream.print(" source: shared objects file");
3432     }
3433   }
3434 
3435   msg.info("%s", info_stream.as_string());
3436 
3437   if (log_is_enabled(Debug, class, load)) {
3438     stringStream debug_stream;
3439 
3440     // Class hierarchy info
3441     debug_stream.print(" klass: " INTPTR_FORMAT " super: " INTPTR_FORMAT,
3442                        p2i(this),  p2i(superklass()));
3443 
3444     // Interfaces
3445     if (local_interfaces() != NULL && local_interfaces()->length() > 0) {
3446       debug_stream.print(" interfaces:");
3447       int length = local_interfaces()->length();
3448       for (int i = 0; i < length; i++) {
3449         debug_stream.print(" " INTPTR_FORMAT,
3450                            p2i(InstanceKlass::cast(local_interfaces()->at(i))));
3451       }
3452     }




  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "aot/aotLoader.hpp"
  28 #include "classfile/classFileParser.hpp"
  29 #include "classfile/classFileStream.hpp"
  30 #include "classfile/classLoader.hpp"
  31 #include "classfile/classLoaderData.inline.hpp"
  32 #include "classfile/javaClasses.hpp"
  33 #include "classfile/moduleEntry.hpp"

  34 #include "classfile/systemDictionary.hpp"
  35 #include "classfile/systemDictionaryShared.hpp"
  36 #include "classfile/verifier.hpp"
  37 #include "classfile/vmSymbols.hpp"
  38 #include "code/dependencyContext.hpp"
  39 #include "compiler/compileBroker.hpp"
  40 #include "gc/shared/collectedHeap.inline.hpp"
  41 #include "interpreter/oopMapCache.hpp"
  42 #include "interpreter/rewriter.hpp"
  43 #include "jvmtifiles/jvmti.h"
  44 #include "logging/log.hpp"
  45 #include "logging/logMessage.hpp"
  46 #include "logging/logStream.hpp"
  47 #include "memory/allocation.inline.hpp"
  48 #include "memory/heapInspection.hpp"
  49 #include "memory/iterator.inline.hpp"
  50 #include "memory/metadataFactory.hpp"
  51 #include "memory/metaspaceClosure.hpp"
  52 #include "memory/metaspaceShared.hpp"
  53 #include "memory/oopFactory.hpp"
  54 #include "memory/resourceArea.hpp"

  55 #include "oops/fieldStreams.hpp"
  56 #include "oops/constantPool.hpp"
  57 #include "oops/instanceClassLoaderKlass.hpp"
  58 #include "oops/instanceKlass.inline.hpp"
  59 #include "oops/instanceMirrorKlass.hpp"
  60 #include "oops/instanceOop.hpp"
  61 #include "oops/klass.inline.hpp"
  62 #include "oops/method.hpp"
  63 #include "oops/oop.inline.hpp"
  64 #include "oops/symbol.hpp"
  65 #include "prims/jvmtiExport.hpp"
  66 #include "prims/jvmtiRedefineClasses.hpp"
  67 #include "prims/jvmtiThreadState.hpp"
  68 #include "prims/methodComparator.hpp"
  69 #include "runtime/atomic.hpp"
  70 #include "runtime/fieldDescriptor.inline.hpp"
  71 #include "runtime/handles.inline.hpp"
  72 #include "runtime/javaCalls.hpp"
  73 #include "runtime/mutexLocker.hpp"
  74 #include "runtime/orderAccess.hpp"


 432   Klass(id),
 433   _nest_members(NULL),
 434   _nest_host_index(0),
 435   _nest_host(NULL),
 436   _static_field_size(parser.static_field_size()),
 437   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 438   _itable_len(parser.itable_size()),
 439   _reference_type(parser.reference_type())
 440 {
 441   set_vtable_length(parser.vtable_size());
 442   set_kind(kind);
 443   set_access_flags(parser.access_flags());
 444   set_is_unsafe_anonymous(parser.is_unsafe_anonymous());
 445   set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 446                                                     false));
 447 
 448   assert(NULL == _methods, "underlying memory not zeroed?");
 449   assert(is_instance_klass(), "is layout incorrect?");
 450   assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
 451 
 452   if (DumpSharedSpaces) {
 453     SystemDictionaryShared::init_dumptime_info(this);
 454   }
 455 }
 456 
 457 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
 458                                        Array<Method*>* methods) {
 459   if (methods != NULL && methods != Universe::the_empty_method_array() &&
 460       !methods->is_shared()) {
 461     for (int i = 0; i < methods->length(); i++) {
 462       Method* method = methods->at(i);
 463       if (method == NULL) continue;  // maybe null if error processing
 464       // Only want to delete methods that are not executing for RedefineClasses.
 465       // The previous version will point to them so they're not totally dangling
 466       assert (!method->on_stack(), "shouldn't be called with methods on stack");
 467       MetadataFactory::free_metadata(loader_data, method);
 468     }
 469     MetadataFactory::free_array<Method*>(loader_data, methods);
 470   }
 471 }
 472 


 582   if (inner_classes() != NULL &&
 583       inner_classes() != Universe::the_empty_short_array() &&
 584       !inner_classes()->is_shared()) {
 585     MetadataFactory::free_array<jushort>(loader_data, inner_classes());
 586   }
 587   set_inner_classes(NULL);
 588 
 589   if (nest_members() != NULL &&
 590       nest_members() != Universe::the_empty_short_array() &&
 591       !nest_members()->is_shared()) {
 592     MetadataFactory::free_array<jushort>(loader_data, nest_members());
 593   }
 594   set_nest_members(NULL);
 595 
 596   // We should deallocate the Annotations instance if it's not in shared spaces.
 597   if (annotations() != NULL && !annotations()->is_shared()) {
 598     MetadataFactory::free_metadata(loader_data, annotations());
 599   }
 600   set_annotations(NULL);
 601 
 602   if (DumpSharedSpaces) {
 603     SystemDictionaryShared::remove_dumptime_info(this);
 604   }
 605 }
 606 
 607 bool InstanceKlass::should_be_initialized() const {
 608   return !is_initialized();
 609 }
 610 
 611 klassItable InstanceKlass::itable() const {
 612   return klassItable(const_cast<InstanceKlass*>(this));
 613 }
 614 
 615 void InstanceKlass::eager_initialize(Thread *thread) {
 616   if (!EagerInitialization) return;
 617 
 618   if (this->is_not_initialized()) {
 619     // abort if the the class has a class initializer
 620     if (this->class_initializer() != NULL) return;
 621 
 622     // abort if it is java.lang.Object (initialization is handled in genesis)


2170         Klass* volatile* klass = adr_implementor();
2171         if (Atomic::cmpxchg((Klass*)NULL, klass, impl) == impl) {
2172           // Successfully unlinking implementor.
2173           if (log_is_enabled(Trace, class, unload)) {
2174             ResourceMark rm;
2175             log_trace(class, unload)("unlinking class (implementor): %s", impl->external_name());
2176           }
2177           return;
2178         }
2179       } else {
2180         return;
2181       }
2182     }
2183   }
2184 }
2185 
2186 void InstanceKlass::clean_method_data() {
2187   for (int m = 0; m < methods()->length(); m++) {
2188     MethodData* mdo = methods()->at(m)->method_data();
2189     if (mdo != NULL) {
2190       MutexLockerEx ml(SafepointSynchronize::is_at_safepoint() ? NULL : mdo->extra_data_lock());
2191       mdo->clean_method_data(/*always_clean*/false);
2192     }
2193   }
2194 }
2195 
2196 bool InstanceKlass::supers_have_passed_fingerprint_checks() {
2197   if (java_super() != NULL && !java_super()->has_passed_fingerprint_check()) {
2198     ResourceMark rm;
2199     log_trace(class, fingerprint)("%s : super %s not fingerprinted", external_name(), java_super()->external_name());
2200     return false;
2201   }
2202 
2203   Array<InstanceKlass*>* local_interfaces = this->local_interfaces();
2204   if (local_interfaces != NULL) {
2205     int length = local_interfaces->length();
2206     for (int i = 0; i < length; i++) {
2207       InstanceKlass* intf = local_interfaces->at(i);
2208       if (!intf->has_passed_fingerprint_check()) {
2209         ResourceMark rm;
2210         log_trace(class, fingerprint)("%s : interface %s not fingerprinted", external_name(), intf->external_name());
2211         return false;
2212       }
2213     }
2214   }
2215 
2216   return true;
2217 }
2218 
2219 bool InstanceKlass::should_store_fingerprint(bool is_unsafe_anonymous) {
2220 #if INCLUDE_AOT
2221   // We store the fingerprint into the InstanceKlass only in the following 2 cases:
2222   if (CalculateClassFingerprint) {
2223     // (1) We are running AOT to generate a shared library.
2224     return true;
2225   }
2226   if (DumpSharedSpaces) {
2227     // (2) We are running -Xshare:dump to create a shared archive
2228     return true;
2229   }
2230   if (UseAOT && is_unsafe_anonymous) {
2231     // (3) We are using AOT code from a shared library and see an unsafe anonymous class
2232     return true;
2233   }
2234 #endif
2235 
2236   // In all other cases we might set the _misc_has_passed_fingerprint_check bit,
2237   // but do not store the 64-bit fingerprint to save space.
2238   return false;
2239 }
2240 
2241 bool InstanceKlass::has_stored_fingerprint() const {
2242 #if INCLUDE_AOT
2243   return should_store_fingerprint() || is_shared();
2244 #else
2245   return false;
2246 #endif
2247 }


2327   // being added to class hierarchy (see SystemDictionary:::add_to_hierarchy()).
2328   _init_state = allocated;
2329 
2330   {
2331     MutexLocker ml(Compile_lock);
2332     init_implementor();
2333   }
2334 
2335   constants()->remove_unshareable_info();
2336 
2337   for (int i = 0; i < methods()->length(); i++) {
2338     Method* m = methods()->at(i);
2339     m->remove_unshareable_info();
2340   }
2341 
2342   // do array classes also.
2343   if (array_klasses() != NULL) {
2344     array_klasses()->remove_unshareable_info();
2345   }
2346 
2347   // These are not allocated from metaspace, but they should should all be empty
2348   // during dump time, so we don't need to worry about them in InstanceKlass::iterate().
2349   guarantee(_source_debug_extension == NULL, "must be");
2350   guarantee(_dep_context == NULL, "must be");
2351   guarantee(_osr_nmethods_head == NULL, "must be");
2352 
2353 #if INCLUDE_JVMTI
2354   guarantee(_breakpoints == NULL, "must be");
2355   guarantee(_previous_versions == NULL, "must be");
2356   _cached_class_file = NULL;
2357 #endif
2358 
2359   _init_thread = NULL;
2360   _methods_jmethod_ids = NULL;
2361   _jni_ids = NULL;
2362   _oop_map_cache = NULL;
2363   // clear _nest_host to ensure re-load at runtime
2364   _nest_host = NULL;
2365 }
2366 
2367 void InstanceKlass::remove_java_mirror() {
2368   Klass::remove_java_mirror();
2369 
2370   // do array classes also.
2371   if (array_klasses() != NULL) {
2372     array_klasses()->remove_java_mirror();
2373   }
2374 }
2375 


2456 }
2457 
2458 #if INCLUDE_JVMTI
2459 static void clear_all_breakpoints(Method* m) {
2460   m->clear_all_breakpoints();
2461 }
2462 #endif
2463 
2464 void InstanceKlass::unload_class(InstanceKlass* ik) {
2465   // Release dependencies.
2466   ik->dependencies().remove_all_dependents();
2467 
2468   // notify the debugger
2469   if (JvmtiExport::should_post_class_unload()) {
2470     JvmtiExport::post_class_unload(ik);
2471   }
2472 
2473   // notify ClassLoadingService of class unload
2474   ClassLoadingService::notify_class_unloaded(ik);
2475 




2476   if (log_is_enabled(Info, class, unload)) {
2477     ResourceMark rm;
2478     log_info(class, unload)("unloading class %s " INTPTR_FORMAT, ik->external_name(), p2i(ik));
2479   }
2480 
2481   Events::log_class_unloading(Thread::current(), ik);
2482 
2483 #if INCLUDE_JFR
2484   assert(ik != NULL, "invariant");
2485   EventClassUnload event;
2486   event.set_unloadedClass(ik);
2487   event.set_definingClassLoader(ik->class_loader_data());
2488   event.commit();
2489 #endif
2490 }
2491 
2492 void InstanceKlass::release_C_heap_structures(InstanceKlass* ik) {
2493   // Clean up C heap
2494   ik->release_C_heap_structures();
2495   ik->constants()->release_C_heap_structures();


2592 
2593   // Add the semicolon and the NULL
2594   dest[dest_index++] = ';';
2595   dest[dest_index] = '\0';
2596   return dest;
2597 }
2598 
2599 // Used to obtain the package name from a fully qualified class name.
2600 Symbol* InstanceKlass::package_from_name(const Symbol* name, TRAPS) {
2601   if (name == NULL) {
2602     return NULL;
2603   } else {
2604     if (name->utf8_length() <= 0) {
2605       return NULL;
2606     }
2607     ResourceMark rm;
2608     const char* package_name = ClassLoader::package_from_name((const char*) name->as_C_string());
2609     if (package_name == NULL) {
2610       return NULL;
2611     }
2612     Symbol* pkg_name = SymbolTable::new_symbol(package_name, THREAD);
2613     return pkg_name;
2614   }
2615 }
2616 
2617 ModuleEntry* InstanceKlass::module() const {
2618   // For an unsafe anonymous class return the host class' module
2619   if (is_unsafe_anonymous()) {
2620     assert(unsafe_anonymous_host() != NULL, "unsafe anonymous class must have a host class");
2621     return unsafe_anonymous_host()->module();
2622   }
2623 
2624   // Class is in a named package
2625   if (!in_unnamed_package()) {
2626     return _package_entry->module();
2627   }
2628 
2629   // Class is in an unnamed package, return its loader's unnamed module
2630   return class_loader_data()->unnamed_module();
2631 }
2632 


2948 
2949       if (log_is_enabled(Info, redefine, class, update)) {
2950         ResourceMark rm;
2951         if (!(*trace_name_printed)) {
2952           log_info(redefine, class, update)
2953             ("adjust: klassname=%s default methods from name=%s",
2954              external_name(), old_method->method_holder()->external_name());
2955           *trace_name_printed = true;
2956         }
2957         log_debug(redefine, class, update, vtables)
2958           ("default method update: %s(%s) ",
2959            new_method->name()->as_C_string(), new_method->signature()->as_C_string());
2960       }
2961     }
2962   }
2963 }
2964 #endif // INCLUDE_JVMTI
2965 
2966 // On-stack replacement stuff
2967 void InstanceKlass::add_osr_nmethod(nmethod* n) {







2968   // only one compilation can be active
2969   {
2970     // This is a short non-blocking critical region, so the no safepoint check is ok.
2971     MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
2972     assert(n->is_osr_method(), "wrong kind of nmethod");
2973     n->set_osr_link(osr_nmethods_head());
2974     set_osr_nmethods_head(n);
2975     // Raise the highest osr level if necessary
2976     if (TieredCompilation) {
2977       Method* m = n->method();
2978       m->set_highest_osr_comp_level(MAX2(m->highest_osr_comp_level(), n->comp_level()));
2979     }
2980   }
2981 
2982   // Get rid of the osr methods for the same bci that have lower levels.
2983   if (TieredCompilation) {
2984     for (int l = CompLevel_limited_profile; l < n->comp_level(); l++) {
2985       nmethod *inv = lookup_osr_nmethod(n->method(), n->osr_entry_bci(), l, true);
2986       if (inv != NULL && inv->is_in_use()) {
2987         inv->make_not_entrant();
2988       }
2989     }
2990   }
2991 }
2992 
2993 // Remove osr nmethod from the list. Return true if found and removed.
2994 bool InstanceKlass::remove_osr_nmethod(nmethod* n) {
2995   // This is a short non-blocking critical region, so the no safepoint check is ok.
2996   MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
2997   assert(n->is_osr_method(), "wrong kind of nmethod");
2998   nmethod* last = NULL;
2999   nmethod* cur  = osr_nmethods_head();
3000   int max_level = CompLevel_none;  // Find the max comp level excluding n
3001   Method* m = n->method();
3002   // Search for match
3003   bool found = false;
3004   while(cur != NULL && cur != n) {
3005     if (TieredCompilation && m == cur->method()) {
3006       // Find max level before n
3007       max_level = MAX2(max_level, cur->comp_level());
3008     }
3009     last = cur;
3010     cur = cur->osr_link();
3011   }
3012   nmethod* next = NULL;
3013   if (cur == n) {
3014     found = true;
3015     next = cur->osr_link();
3016     if (last == NULL) {


3020       last->set_osr_link(next);
3021     }
3022   }
3023   n->set_osr_link(NULL);
3024   if (TieredCompilation) {
3025     cur = next;
3026     while (cur != NULL) {
3027       // Find max level after n
3028       if (m == cur->method()) {
3029         max_level = MAX2(max_level, cur->comp_level());
3030       }
3031       cur = cur->osr_link();
3032     }
3033     m->set_highest_osr_comp_level(max_level);
3034   }
3035   return found;
3036 }
3037 
3038 int InstanceKlass::mark_osr_nmethods(const Method* m) {
3039   // This is a short non-blocking critical region, so the no safepoint check is ok.
3040   MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
3041   nmethod* osr = osr_nmethods_head();
3042   int found = 0;
3043   while (osr != NULL) {
3044     assert(osr->is_osr_method(), "wrong kind of nmethod found in chain");
3045     if (osr->method() == m) {
3046       osr->mark_for_deoptimization();
3047       found++;
3048     }
3049     osr = osr->osr_link();
3050   }
3051   return found;
3052 }
3053 
3054 nmethod* InstanceKlass::lookup_osr_nmethod(const Method* m, int bci, int comp_level, bool match_level) const {
3055   // This is a short non-blocking critical region, so the no safepoint check is ok.
3056   MutexLockerEx ml(OsrList_lock, Mutex::_no_safepoint_check_flag);
3057   nmethod* osr = osr_nmethods_head();
3058   nmethod* best = NULL;
3059   while (osr != NULL) {
3060     assert(osr->is_osr_method(), "wrong kind of nmethod found in chain");
3061     // There can be a time when a c1 osr method exists but we are waiting
3062     // for a c2 version. When c2 completes its osr nmethod we will trash
3063     // the c1 version and only be able to find the c2 version. However
3064     // while we overflow in the c1 code at back branches we don't want to
3065     // try and switch to the same code as we are already running
3066 
3067     if (osr->method() == m &&
3068         (bci == InvocationEntryBci || osr->osr_entry_bci() == bci)) {
3069       if (match_level) {
3070         if (osr->comp_level() == comp_level) {
3071           // Found a match - return it.
3072           return osr;
3073         }
3074       } else {
3075         if (best == NULL || (osr->comp_level() > best->comp_level())) {
3076           if (osr->comp_level() == CompLevel_highest_tier) {
3077             // Found the best possible - return it.
3078             return osr;
3079           }
3080           best = osr;
3081         }
3082       }
3083     }
3084     osr = osr->osr_link();
3085   }
3086   if (best != NULL && best->comp_level() >= comp_level && match_level == false) {


3087     return best;
3088   }
3089   return NULL;
3090 }
3091 
3092 // -----------------------------------------------------------------------------------------------------
3093 // Printing
3094 
3095 #ifndef PRODUCT
3096 
3097 #define BULLET  " - "
3098 
3099 static const char* state_names[] = {
3100   "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
3101 };
3102 
3103 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3104   for (int i = 0; i < len; i++) {
3105     intptr_t e = start[i];
3106     st->print("%d : " INTPTR_FORMAT, i, e);
3107     if (e != 0 && ((Metadata*)e)->is_metaspace_object()) {
3108       st->print(" ");
3109       ((Metadata*)e)->print_value_on(st);
3110     }
3111     st->cr();
3112   }
3113 }
3114 
3115 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3116   return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3117 }
3118 
3119 void InstanceKlass::print_on(outputStream* st) const {
3120   assert(is_klass(), "must be klass");
3121   Klass::print_on(st);
3122 
3123   st->print(BULLET"instance size:     %d", size_helper());                        st->cr();
3124   st->print(BULLET"klass size:        %d", size());                               st->cr();
3125   st->print(BULLET"access:            "); access_flags().print_on(st);            st->cr();
3126   st->print(BULLET"state:             "); st->print_cr("%s", state_names[_init_state]);
3127   st->print(BULLET"name:              "); name()->print_value_on(st);             st->cr();


3394       } else {
3395         info_stream.print(" source: %s", cfs->source());
3396       }
3397     } else if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
3398       Thread* THREAD = Thread::current();
3399       Klass* caller =
3400             THREAD->is_Java_thread()
3401                 ? ((JavaThread*)THREAD)->security_get_caller_class(1)
3402                 : NULL;
3403       // caller can be NULL, for example, during a JVMTI VM_Init hook
3404       if (caller != NULL) {
3405         info_stream.print(" source: instance of %s", caller->external_name());
3406       } else {
3407         // source is unknown
3408       }
3409     } else {
3410       oop class_loader = loader_data->class_loader();
3411       info_stream.print(" source: %s", class_loader->klass()->external_name());
3412     }
3413   } else {
3414     info_stream.print(" source: shared objects file");





3415   }
3416 
3417   msg.info("%s", info_stream.as_string());
3418 
3419   if (log_is_enabled(Debug, class, load)) {
3420     stringStream debug_stream;
3421 
3422     // Class hierarchy info
3423     debug_stream.print(" klass: " INTPTR_FORMAT " super: " INTPTR_FORMAT,
3424                        p2i(this),  p2i(superklass()));
3425 
3426     // Interfaces
3427     if (local_interfaces() != NULL && local_interfaces()->length() > 0) {
3428       debug_stream.print(" interfaces:");
3429       int length = local_interfaces()->length();
3430       for (int i = 0; i < length; i++) {
3431         debug_stream.print(" " INTPTR_FORMAT,
3432                            p2i(InstanceKlass::cast(local_interfaces()->at(i))));
3433       }
3434     }


< prev index next >