< prev index next >

src/hotspot/share/oops/instanceKlass.cpp

Print this page




 311                            this->class_loader_data()->loader_name_and_id(),
 312                            k->external_name(),
 313                            k->class_loader_data()->loader_name_and_id(),
 314                            error
 315                            );
 316       }
 317       return NULL;
 318     } else {
 319       if (log_is_enabled(Trace, class, nestmates)) {
 320         ResourceMark rm(THREAD);
 321         log_trace(class, nestmates)("Type %s is not part of a nest: setting nest-host to self",
 322                                     this->external_name());
 323       }
 324       // save resolved nest-host value
 325       return (_nest_host = this);
 326     }
 327   }
 328   return nest_host_k;
 329 }
 330 














































 331 // check if 'this' and k are nestmates (same nest_host), or k is our nest_host,
 332 // or we are k's nest_host - all of which is covered by comparing the two
 333 // resolved_nest_hosts
 334 bool InstanceKlass::has_nestmate_access_to(InstanceKlass* k, TRAPS) {
 335 
 336   assert(this != k, "this should be handled by higher-level code");
 337 
 338   // Per JVMS 5.4.4 we first resolve and validate the current class, then
 339   // the target class k. Resolution exceptions will be passed on by upper
 340   // layers. IncompatibleClassChangeErrors from membership validation failures
 341   // will also be passed through.
 342 
 343   Symbol* icce = vmSymbols::java_lang_IncompatibleClassChangeError();
 344   InstanceKlass* cur_host = nest_host(icce, CHECK_false);
 345   if (cur_host == NULL) {
 346     return false;
 347   }
 348 
 349   Klass* k_nest_host = k->nest_host(icce, CHECK_false);
 350   if (k_nest_host == NULL) {


 352   }
 353 
 354   bool access = (cur_host == k_nest_host);
 355 
 356   if (log_is_enabled(Trace, class, nestmates)) {
 357     ResourceMark rm(THREAD);
 358     log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
 359                                 this->external_name(),
 360                                 access ? "" : "NOT ",
 361                                 k->external_name());
 362   }
 363 
 364   return access;
 365 }
 366 
 367 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
 368   const int size = InstanceKlass::size(parser.vtable_size(),
 369                                        parser.itable_size(),
 370                                        nonstatic_oop_map_size(parser.total_oop_map_count()),
 371                                        parser.is_interface(),
 372                                        parser.is_unsafe_anonymous(),
 373                                        should_store_fingerprint(parser.is_unsafe_anonymous()));
 374 
 375   const Symbol* const class_name = parser.class_name();
 376   assert(class_name != NULL, "invariant");
 377   ClassLoaderData* loader_data = parser.loader_data();
 378   assert(loader_data != NULL, "invariant");
 379 
 380   InstanceKlass* ik;
 381 
 382   // Allocation
 383   if (REF_NONE == parser.reference_type()) {
 384     if (class_name == vmSymbols::java_lang_Class()) {
 385       // mirror
 386       ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
 387     }
 388     else if (is_class_loader(class_name, parser)) {
 389       // class loader
 390       ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
 391     } else {
 392       // normal


 424 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
 425   Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
 426   assert(default_vtable_indices() == NULL, "only create once");
 427   set_default_vtable_indices(vtable_indices);
 428   return vtable_indices;
 429 }
 430 
 431 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id) :
 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


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 }
2248 
2249 uint64_t InstanceKlass::get_stored_fingerprint() const {
2250   address adr = adr_fingerprint();
2251   if (adr != NULL) {


2518 
2519   assert(_dep_context == NULL,
2520          "dependencies should already be cleaned");
2521 
2522 #if INCLUDE_JVMTI
2523   // Deallocate breakpoint records
2524   if (breakpoints() != 0x0) {
2525     methods_do(clear_all_breakpoints);
2526     assert(breakpoints() == 0x0, "should have cleared breakpoints");
2527   }
2528 
2529   // deallocate the cached class file
2530   if (_cached_class_file != NULL) {
2531     os::free(_cached_class_file);
2532     _cached_class_file = NULL;
2533   }
2534 #endif
2535 
2536   // Decrement symbol reference counts associated with the unloaded class.
2537   if (_name != NULL) _name->decrement_refcount();

2538   // unreference array name derived from this class name (arrays of an unloaded
2539   // class can't be referenced anymore).
2540   if (_array_name != NULL)  _array_name->decrement_refcount();
2541   if (_source_debug_extension != NULL) FREE_C_HEAP_ARRAY(char, _source_debug_extension);
2542 }
2543 
2544 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2545   if (array == NULL) {
2546     _source_debug_extension = NULL;
2547   } else {
2548     // Adding one to the attribute length in order to store a null terminator
2549     // character could cause an overflow because the attribute length is
2550     // already coded with an u4 in the classfile, but in practice, it's
2551     // unlikely to happen.
2552     assert((length+1) > length, "Overflow checking");
2553     char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2554     for (int i = 0; i < length; i++) {
2555       sde[i] = array[i];
2556     }
2557     sde[length] = '\0';


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 
2633 void InstanceKlass::set_package(ClassLoaderData* loader_data, TRAPS) {
2634 
2635   // ensure java/ packages only loaded by boot or platform builtin loaders
2636   check_prohibited_package(name(), loader_data, CHECK);
2637 
2638   TempNewSymbol pkg_name = package_from_name(name(), CHECK);
2639 
2640   if (pkg_name != NULL && loader_data != NULL) {
2641 




 311                            this->class_loader_data()->loader_name_and_id(),
 312                            k->external_name(),
 313                            k->class_loader_data()->loader_name_and_id(),
 314                            error
 315                            );
 316       }
 317       return NULL;
 318     } else {
 319       if (log_is_enabled(Trace, class, nestmates)) {
 320         ResourceMark rm(THREAD);
 321         log_trace(class, nestmates)("Type %s is not part of a nest: setting nest-host to self",
 322                                     this->external_name());
 323       }
 324       // save resolved nest-host value
 325       return (_nest_host = this);
 326     }
 327   }
 328   return nest_host_k;
 329 }
 330 
 331 
 332 // Dynamic nest member support: set this class's nest host to the given class.
 333 // This occurs as part of the class definition, as soon as the instanceKlass
 334 // has been created and doesn't require further resolution. The code:
 335 //    lookup().defineClass(bytes_for_X, NESTMATE);
 336 // results in:
 337 //    class_of_X.set_nest_host(lookup().lookupClass().getNestHost())
 338 // So we know that current class is "pristine" and its _nest_host must be NULL.
 339 // We also know the "host" is a valid nest-host in the same package so we can
 340 // assert some of those facts.
 341 void InstanceKlass::set_nest_host(InstanceKlass* host, TRAPS) {
 342   assert(host != NULL, "NULL nest host specified");
 343   assert(_nest_host == NULL, "current class has resolved nest-host");
 344   assert((host->_nest_host == NULL && host->_nest_host_index == 0) ||
 345          (host->_nest_host == host), "proposed host is not a valid nest-host");
 346   // Can't assert this as package is not set yet:
 347   // assert(is_same_class_package(host), "proposed host is in wrong package");
 348 
 349   const char * error_msg = NULL;
 350   // There are two validity checks that have to be made:
 351   // 1. The current class must not expect a statically defined nest-host
 352   if (_nest_host_index == 0) {
 353     // 2. The current class can't itself be a nest-host
 354     if (_nest_members == NULL ||
 355         _nest_members == Universe::the_empty_short_array()) {
 356       _nest_host = host;
 357       return;
 358     }
 359     else {
 360       error_msg = "the current class is already a nest-host";
 361     }
 362   }
 363   else {
 364     error_msg = "the current class is already a member of a nest";
 365   }
 366 
 367   ResourceMark rm(THREAD);
 368   Exceptions::fthrow(THREAD_AND_LOCATION,
 369                      vmSymbols::java_lang_ClassFormatError(),
 370                      "Type %s can not be a dynamic nest member of %s: %s",
 371                      this->external_name(),
 372                      host->external_name(),
 373                      error_msg
 374                      );
 375 }
 376 
 377 // check if 'this' and k are nestmates (same nest_host), or k is our nest_host,
 378 // or we are k's nest_host - all of which is covered by comparing the two
 379 // resolved_nest_hosts
 380 bool InstanceKlass::has_nestmate_access_to(InstanceKlass* k, TRAPS) {
 381 
 382   assert(this != k, "this should be handled by higher-level code");
 383 
 384   // Per JVMS 5.4.4 we first resolve and validate the current class, then
 385   // the target class k. Resolution exceptions will be passed on by upper
 386   // layers. IncompatibleClassChangeErrors from membership validation failures
 387   // will also be passed through.
 388 
 389   Symbol* icce = vmSymbols::java_lang_IncompatibleClassChangeError();
 390   InstanceKlass* cur_host = nest_host(icce, CHECK_false);
 391   if (cur_host == NULL) {
 392     return false;
 393   }
 394 
 395   Klass* k_nest_host = k->nest_host(icce, CHECK_false);
 396   if (k_nest_host == NULL) {


 398   }
 399 
 400   bool access = (cur_host == k_nest_host);
 401 
 402   if (log_is_enabled(Trace, class, nestmates)) {
 403     ResourceMark rm(THREAD);
 404     log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
 405                                 this->external_name(),
 406                                 access ? "" : "NOT ",
 407                                 k->external_name());
 408   }
 409 
 410   return access;
 411 }
 412 
 413 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
 414   const int size = InstanceKlass::size(parser.vtable_size(),
 415                                        parser.itable_size(),
 416                                        nonstatic_oop_map_size(parser.total_oop_map_count()),
 417                                        parser.is_interface(),
 418                                        (parser.is_nonfindable() || parser.is_unsafe_anonymous()),
 419                                        should_store_fingerprint(parser.is_unsafe_anonymous()));
 420 
 421   const Symbol* const class_name = parser.class_name();
 422   assert(class_name != NULL, "invariant");
 423   ClassLoaderData* loader_data = parser.loader_data();
 424   assert(loader_data != NULL, "invariant");
 425 
 426   InstanceKlass* ik;
 427 
 428   // Allocation
 429   if (REF_NONE == parser.reference_type()) {
 430     if (class_name == vmSymbols::java_lang_Class()) {
 431       // mirror
 432       ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
 433     }
 434     else if (is_class_loader(class_name, parser)) {
 435       // class loader
 436       ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
 437     } else {
 438       // normal


 470 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
 471   Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
 472   assert(default_vtable_indices() == NULL, "only create once");
 473   set_default_vtable_indices(vtable_indices);
 474   return vtable_indices;
 475 }
 476 
 477 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id) :
 478   Klass(id),
 479   _nest_members(NULL),
 480   _nest_host_index(0),
 481   _nest_host(NULL),
 482   _static_field_size(parser.static_field_size()),
 483   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 484   _itable_len(parser.itable_size()),
 485   _reference_type(parser.reference_type())
 486 {
 487   set_vtable_length(parser.vtable_size());
 488   set_kind(kind);
 489   set_access_flags(parser.access_flags());
 490   set_is_nonfindable(parser.is_nonfindable());
 491   set_is_unsafe_anonymous(parser.is_unsafe_anonymous());
 492   set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 493                                                     false));
 494 
 495   assert(NULL == _methods, "underlying memory not zeroed?");
 496   assert(is_instance_klass(), "is layout incorrect?");
 497   assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
 498 
 499   if (DumpSharedSpaces) {
 500     SystemDictionaryShared::init_dumptime_info(this);
 501   }
 502 }
 503 
 504 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
 505                                        Array<Method*>* methods) {
 506   if (methods != NULL && methods != Universe::the_empty_method_array() &&
 507       !methods->is_shared()) {
 508     for (int i = 0; i < methods->length(); i++) {
 509       Method* method = methods->at(i);
 510       if (method == NULL) continue;  // maybe null if error processing


2246     log_trace(class, fingerprint)("%s : super %s not fingerprinted", external_name(), java_super()->external_name());
2247     return false;
2248   }
2249 
2250   Array<InstanceKlass*>* local_interfaces = this->local_interfaces();
2251   if (local_interfaces != NULL) {
2252     int length = local_interfaces->length();
2253     for (int i = 0; i < length; i++) {
2254       InstanceKlass* intf = local_interfaces->at(i);
2255       if (!intf->has_passed_fingerprint_check()) {
2256         ResourceMark rm;
2257         log_trace(class, fingerprint)("%s : interface %s not fingerprinted", external_name(), intf->external_name());
2258         return false;
2259       }
2260     }
2261   }
2262 
2263   return true;
2264 }
2265 
2266 bool InstanceKlass::should_store_fingerprint(bool is_nonfindable) {
2267 #if INCLUDE_AOT
2268   // We store the fingerprint into the InstanceKlass only in the following 2 cases:
2269   if (CalculateClassFingerprint) {
2270     // (1) We are running AOT to generate a shared library.
2271     return true;
2272   }
2273   if (DumpSharedSpaces) {
2274     // (2) We are running -Xshare:dump to create a shared archive
2275     return true;
2276   }
2277   if (UseAOT && is_nonfindable) {
2278     // (3) We are using AOT code from a shared library and see a nonfindable or unsafe anonymous class
2279     return true;
2280   }
2281 #endif
2282 
2283   // In all other cases we might set the _misc_has_passed_fingerprint_check bit,
2284   // but do not store the 64-bit fingerprint to save space.
2285   return false;
2286 }
2287 
2288 bool InstanceKlass::has_stored_fingerprint() const {
2289 #if INCLUDE_AOT
2290   return should_store_fingerprint() || is_shared();
2291 #else
2292   return false;
2293 #endif
2294 }
2295 
2296 uint64_t InstanceKlass::get_stored_fingerprint() const {
2297   address adr = adr_fingerprint();
2298   if (adr != NULL) {


2565 
2566   assert(_dep_context == NULL,
2567          "dependencies should already be cleaned");
2568 
2569 #if INCLUDE_JVMTI
2570   // Deallocate breakpoint records
2571   if (breakpoints() != 0x0) {
2572     methods_do(clear_all_breakpoints);
2573     assert(breakpoints() == 0x0, "should have cleared breakpoints");
2574   }
2575 
2576   // deallocate the cached class file
2577   if (_cached_class_file != NULL) {
2578     os::free(_cached_class_file);
2579     _cached_class_file = NULL;
2580   }
2581 #endif
2582 
2583   // Decrement symbol reference counts associated with the unloaded class.
2584   if (_name != NULL) _name->decrement_refcount();
2585 
2586   // unreference array name derived from this class name (arrays of an unloaded
2587   // class can't be referenced anymore).
2588   if (_array_name != NULL)  _array_name->decrement_refcount();
2589   if (_source_debug_extension != NULL) FREE_C_HEAP_ARRAY(char, _source_debug_extension);
2590 }
2591 
2592 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2593   if (array == NULL) {
2594     _source_debug_extension = NULL;
2595   } else {
2596     // Adding one to the attribute length in order to store a null terminator
2597     // character could cause an overflow because the attribute length is
2598     // already coded with an u4 in the classfile, but in practice, it's
2599     // unlikely to happen.
2600     assert((length+1) > length, "Overflow checking");
2601     char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2602     for (int i = 0; i < length; i++) {
2603       sde[i] = array[i];
2604     }
2605     sde[length] = '\0';


2650     return NULL;
2651   } else {
2652     if (name->utf8_length() <= 0) {
2653       return NULL;
2654     }
2655     ResourceMark rm;
2656     const char* package_name = ClassLoader::package_from_name((const char*) name->as_C_string());
2657     if (package_name == NULL) {
2658       return NULL;
2659     }
2660     Symbol* pkg_name = SymbolTable::new_symbol(package_name, THREAD);
2661     return pkg_name;
2662   }
2663 }
2664 
2665 ModuleEntry* InstanceKlass::module() const {
2666   // For an unsafe anonymous class return the host class' module
2667   if (is_unsafe_anonymous()) {
2668     assert(unsafe_anonymous_host() != NULL, "unsafe anonymous class must have a host class");
2669     return unsafe_anonymous_host()->module();
2670   }
2671 
2672   if (is_nonfindable() &&
2673       in_unnamed_package() &&
2674       class_loader_data()->is_shortlived()) {
2675     // For a weak nonfindable class defined to an unnamed package,
2676     // the short-lived CLD will not have an unnamed module created for it.
2677     // Two choices to find the correct ModuleEntry:
2678     // 1. If nonfindable class is within a nest, use nest host's module
2679     // 2. Find the unnamed module off from the class loader
2680     // For now option #2 is used since a nest host is not set until
2681     // after the instance class is created in jvm_lookup_define_class().
2682     if (class_loader_data()->is_boot_class_loader_data()) {
2683       return ClassLoaderData::the_null_class_loader_data()->unnamed_module();
2684     } else {
2685       oop module = java_lang_ClassLoader::unnamedModule(class_loader_data()->class_loader());
2686       assert(java_lang_Module::is_instance(module), "Not an instance of java.lang.Module");
2687       return java_lang_Module::module_entry(module);
2688     }
2689   }
2690 
2691   // Class is in a named package
2692   if (!in_unnamed_package()) {
2693     return _package_entry->module();
2694   }
2695 
2696   // Class is in an unnamed package, return its loader's unnamed module
2697   return class_loader_data()->unnamed_module();
2698 }
2699 
2700 void InstanceKlass::set_package(ClassLoaderData* loader_data, TRAPS) {
2701 
2702   // ensure java/ packages only loaded by boot or platform builtin loaders
2703   check_prohibited_package(name(), loader_data, CHECK);
2704 
2705   TempNewSymbol pkg_name = package_from_name(name(), CHECK);
2706 
2707   if (pkg_name != NULL && loader_data != NULL) {
2708 


< prev index next >