< prev index next >

src/hotspot/share/oops/instanceKlass.cpp

Print this page




 136 
 137 static inline bool is_class_loader(const Symbol* class_name,
 138                                    const ClassFileParser& parser) {
 139   assert(class_name != NULL, "invariant");
 140 
 141   if (class_name == vmSymbols::java_lang_ClassLoader()) {
 142     return true;
 143   }
 144 
 145   if (SystemDictionary::ClassLoader_klass_loaded()) {
 146     const Klass* const super_klass = parser.super_klass();
 147     if (super_klass != NULL) {
 148       if (super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass())) {
 149         return true;
 150       }
 151     }
 152   }
 153   return false;
 154 }
 155 
 156 // called to verify that k is a member of this nest


 157 bool InstanceKlass::has_nest_member(InstanceKlass* k, TRAPS) const {
 158   assert(!is_hidden(), "unexpected hidden class");
 159   if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
 160     if (log_is_enabled(Trace, class, nestmates)) {
 161       ResourceMark rm(THREAD);
 162       log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
 163                                   k->external_name(), this->external_name());
 164     }
 165     return false;
 166   }
 167 
 168   if (log_is_enabled(Trace, class, nestmates)) {
 169     ResourceMark rm(THREAD);
 170     log_trace(class, nestmates)("Checking nest membership of %s in %s",
 171                                 k->external_name(), this->external_name());
 172   }
 173 
 174   // Check for a resolved cp entry , else fall back to a name check.
 175   // We don't want to resolve any class other than the one being checked.
 176   for (int i = 0; i < _nest_members->length(); i++) {
 177     int cp_index = _nest_members->at(i);
 178     if (_constants->tag_at(cp_index).is_klass()) {
 179       Klass* k2 = _constants->klass_at(cp_index, CHECK_false);

 180       if (k2 == k) {
 181         log_trace(class, nestmates)("- class is listed at nest_members[%d] => cp[%d]", i, cp_index);
 182         return true;
 183       }
 184     }
 185     else {
 186       Symbol* name = _constants->klass_name_at(cp_index);
 187       if (name == k->name()) {
 188         log_trace(class, nestmates)("- Found it at nest_members[%d] => cp[%d]", i, cp_index);
 189 
 190         // Names match so check actual klass - this may trigger class loading if
 191         // it doesn't match (though that should be impossible). But to be safe we
 192         // have to check for a compiler thread executing here.
 193         if (!THREAD->can_call_java() && !_constants->tag_at(cp_index).is_klass()) {
 194           log_trace(class, nestmates)("- validation required resolution in an unsuitable thread");
 195           return false;
 196         }
 197 
 198         Klass* k2 = _constants->klass_at(cp_index, CHECK_false);
 199         if (k2 == k) {
 200           log_trace(class, nestmates)("- class is listed as a nest member");
 201           return true;
 202         }
 203         else {
 204           // same name but different klass!
 205           log_trace(class, nestmates)(" - klass comparison failed!");
 206           // can't have two names the same, so we're done
 207           return false;
 208         }
 209       }
 210     }
 211   }
 212   log_trace(class, nestmates)("- class is NOT a nest member!");
 213   return false;
 214 }
 215 
 216 InstanceKlass* InstanceKlass::runtime_nest_host(TRAPS) {
 217   // TODO: nest_host returns NULL if validation fails.  Need to follow up
 218   // the specification when to evaluate the runtime nest host.  Right now
 219   // it's only determined when a dynamic nestmate is added.
 220   InstanceKlass* nest_host_k = nest_host(NULL, CHECK_NULL);
 221   if (nest_host_k == NULL) {
 222     assert(_nest_host == NULL, "should fail to validate NestNost");
 223     // drop the static nest information; set dynamic nest to this class
 224     if (log_is_enabled(Trace, class, nestmates)) {
 225       ResourceMark rm(THREAD);
 226       log_trace(class, nestmates)("Fail to validate static nest host of %s: setting nest-host to self",
 227                                   this->external_name());
 228     }
 229     _nest_host = nest_host_k = this;
 230   }
 231   return nest_host_k;
 232 }
 233 
 234 // Return nest-host class, resolving, validating and saving it if needed.
 235 // In cases where this is called from a thread that can not do classloading
 236 // (such as a native JIT thread) then we simply return NULL, which in turn
 237 // causes the access check to return false. Such code will retry the access
 238 // from a more suitable environment later.
 239 InstanceKlass* InstanceKlass::nest_host(Symbol* validationException, TRAPS) {




 240   InstanceKlass* nest_host_k = _nest_host;
 241   if (nest_host_k == NULL) {





 242     // need to resolve and save our nest-host class. This could be attempted
 243     // concurrently but as the result is idempotent and we don't use the class
 244     // then we do not need any synchronization beyond what is implicitly used
 245     // during class loading.
 246     if (_nest_host_index != 0) { // we have a real nest_host
 247       // Before trying to resolve check if we're in a suitable context
 248       if (!THREAD->can_call_java() && !_constants->tag_at(_nest_host_index).is_klass()) {
 249         if (log_is_enabled(Trace, class, nestmates)) {
 250           ResourceMark rm(THREAD);
 251           log_trace(class, nestmates)("Rejected resolution of nest-host of %s in unsuitable thread",
 252                                       this->external_name());
 253         }
 254         return NULL;
 255       }
 256 
 257       if (log_is_enabled(Trace, class, nestmates)) {
 258         ResourceMark rm(THREAD);
 259         log_trace(class, nestmates)("Resolving nest-host of %s using cp entry for %s",
 260                                     this->external_name(),
 261                                     _constants->klass_name_at(_nest_host_index)->as_C_string());
 262       }
 263 
 264       Klass* k = _constants->klass_at(_nest_host_index, THREAD);
 265       if (HAS_PENDING_EXCEPTION) {
 266         Handle exc_h = Handle(THREAD, PENDING_EXCEPTION);
 267         if (validationException == NULL && exc_h->is_a(SystemDictionary::LinkageError_klass())) {
 268           // clear exception if fails to resolve the nest host class
 269           CLEAR_PENDING_EXCEPTION;
 270         }
 271         // throw a new NCDFE with the original as its cause, and a clear msg
 272         if (exc_h->is_a(SystemDictionary::NoClassDefFoundError_klass()) && validationException != NULL) {
 273           // throw a new NCDFE with the original as its cause, and a clear msg
 274           ResourceMark rm(THREAD);
 275           char buf[200];





 276           CLEAR_PENDING_EXCEPTION;
 277           jio_snprintf(buf, sizeof(buf),
 278                        "Unable to load nest-host class (%s) of %s",
 279                        _constants->klass_name_at(_nest_host_index)->as_C_string(),
 280                        this->external_name());
 281           log_trace(class, nestmates)("%s - NoClassDefFoundError", buf);
 282           THROW_MSG_CAUSE_NULL(vmSymbols::java_lang_NoClassDefFoundError(), buf, exc_h);
 283         }
 284         // All other exceptions pass through (OOME, StackOverflowError, LinkageErrors etc).
 285         return NULL;
 286       }
 287 
 288       // A valid nest-host is an instance class in the current package that lists this
 289       // class as a nest member. If any of these conditions are not met we post the
 290       // requested exception type (if any) and return NULL
 291 
 292       const char* error = NULL;
 293 
 294       // JVMS 5.4.4 indicates package check comes first
 295       if (is_same_class_package(k)) {
 296 
 297         // Now check actual membership. We can't be a member if our "host" is
 298         // not an instance class.
 299         if (k->is_instance_klass()) {
 300           nest_host_k = InstanceKlass::cast(k);
 301 
 302           bool is_member = nest_host_k->has_nest_member(this, CHECK_NULL);

 303           if (is_member) {
 304             // save resolved nest-host value
 305             _nest_host = nest_host_k;
 306 
 307             if (log_is_enabled(Trace, class, nestmates)) {
 308               ResourceMark rm(THREAD);
 309               log_trace(class, nestmates)("Resolved nest-host of %s to %s",
 310                                           this->external_name(), k->external_name());
 311             }
 312             return nest_host_k;


 313           }








 314         }
 315         error = "current type is not listed as a nest member";
 316       } else {
 317         error = "types are in different packages";
 318       }
 319 
 320       if (log_is_enabled(Trace, class, nestmates)) {

 321         ResourceMark rm(THREAD);
 322         log_trace(class, nestmates)
 323           ("Type %s (loader: %s) is not a nest member of "
 324            "resolved type %s (loader: %s): %s",
 325            this->external_name(),
 326            this->class_loader_data()->loader_name_and_id(),
 327            k->external_name(),
 328            k->class_loader_data()->loader_name_and_id(),
 329            error);
 330       }
 331 
 332       if (validationException != NULL && THREAD->can_call_java()) {
 333         ResourceMark rm(THREAD);
 334         Exceptions::fthrow(THREAD_AND_LOCATION,
 335                            validationException,
 336                            "Type %s (loader: %s) is not a nest member of %s (loader: %s): %s",
 337                            this->external_name(),
 338                            this->class_loader_data()->loader_name_and_id(),
 339                            k->external_name(),
 340                            k->class_loader_data()->loader_name_and_id(),
 341                            error
 342                            );
 343       }
 344       return NULL;
 345     } else {
 346       if (log_is_enabled(Trace, class, nestmates)) {
 347         ResourceMark rm(THREAD);
 348         log_trace(class, nestmates)("Type %s is not part of a nest: setting nest-host to self",
 349                                     this->external_name());
 350       }
 351       // save resolved nest-host value
 352       return (_nest_host = this);
 353     }
 354   }
 355   return nest_host_k;
 356 }
 357 




 358 
 359 // Dynamic nest member support: set this class's nest host to the given class.
 360 // This occurs as part of the class definition, as soon as the instanceKlass
 361 // has been created and doesn't require further resolution. The code:
 362 //    lookup().defineHiddenClass(bytes_for_X, NESTMATE);
 363 // results in:
 364 //    class_of_X.set_nest_host(lookup().lookupClass().getNestHost())
 365 // If it has an explicit _nest_host_index or _nest_members, these will be ignored.
 366 // We also know the "host" is a valid nest-host in the same package so we can
 367 // assert some of those facts.
 368 void InstanceKlass::set_nest_host(InstanceKlass* host, TRAPS) {
 369   assert(is_hidden(), "must be a hidden class");
 370   assert(host != NULL, "NULL nest host specified");
 371   assert(_nest_host == NULL, "current class has resolved nest-host");


 372   assert((host->_nest_host == NULL && host->_nest_host_index == 0) ||
 373          (host->_nest_host == host), "proposed host is not a valid nest-host");
 374   // Can't assert this as package is not set yet:
 375   // assert(is_same_class_package(host), "proposed host is in wrong package");
 376 
 377   if (log_is_enabled(Trace, class, nestmates)) {
 378     ResourceMark rm(THREAD);

 379     // a hidden class does not expect a statically defined nest-host
 380     if (_nest_host_index > 0) {
 381       log_trace(class, nestmates)("Type %s is a dynamic nest member of %s: the NestHost attribute in the current class is ignored",
 382                                   this->external_name(),
 383                                   host->external_name());
 384     } else if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) {
 385       log_trace(class, nestmates)("Type %s is a dynamic nest member of %s: the NestMembers attribute in the current class is ignored",
 386                                   this->external_name(),
 387                                   host->external_name());
 388     }




 389   }
 390   // set dynamic nest host
 391   _nest_host = host;
 392 }
 393 
 394 // check if 'this' and k are nestmates (same nest_host), or k is our nest_host,
 395 // or we are k's nest_host - all of which is covered by comparing the two
 396 // resolved_nest_hosts
 397 bool InstanceKlass::has_nestmate_access_to(InstanceKlass* k, TRAPS) {
 398 
 399   assert(this != k, "this should be handled by higher-level code");
 400 
 401   // Per JVMS 5.4.4 we first resolve and validate the current class, then
 402   // the target class k. Resolution exceptions will be passed on by upper
 403   // layers. IncompatibleClassChangeErrors from membership validation failures
 404   // will also be passed through.
 405 
 406   Symbol* icce = vmSymbols::java_lang_IncompatibleClassChangeError();
 407   InstanceKlass* cur_host = nest_host(icce, CHECK_false);
 408   if (cur_host == NULL) {
 409     return false;
 410   }
 411 
 412   Klass* k_nest_host = k->nest_host(icce, CHECK_false);
 413   if (k_nest_host == NULL) {
 414     return false;
 415   }
 416 
 417   bool access = (cur_host == k_nest_host);
 418 
 419   if (log_is_enabled(Trace, class, nestmates)) {
 420     ResourceMark rm(THREAD);
 421     log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
 422                                 this->external_name(),
 423                                 access ? "" : "NOT ",
 424                                 k->external_name());
 425   }
 426 
 427   return access;
 428 }
 429 
 430 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
 431   bool is_hidden_or_anonymous = parser.is_hidden() || parser.is_unsafe_anonymous();
 432   const int size = InstanceKlass::size(parser.vtable_size(),


 480       _method_ordering->at_put(i, m->at(i));
 481     }
 482   } else {
 483     _method_ordering = Universe::the_empty_int_array();
 484   }
 485 }
 486 
 487 // create a new array of vtable_indices for default methods
 488 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
 489   Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
 490   assert(default_vtable_indices() == NULL, "only create once");
 491   set_default_vtable_indices(vtable_indices);
 492   return vtable_indices;
 493 }
 494 
 495 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id) :
 496   Klass(id),
 497   _nest_members(NULL),
 498   _nest_host_index(0),
 499   _nest_host(NULL),

 500   _record_components(NULL),
 501   _static_field_size(parser.static_field_size()),
 502   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 503   _itable_len(parser.itable_size()),
 504   _init_thread(NULL),
 505   _init_state(allocated),
 506   _reference_type(parser.reference_type())
 507 {
 508   set_vtable_length(parser.vtable_size());
 509   set_kind(kind);
 510   set_access_flags(parser.access_flags());
 511   if (parser.is_hidden()) set_is_hidden();
 512   set_is_unsafe_anonymous(parser.is_unsafe_anonymous());
 513   set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 514                                                     false));
 515 
 516   assert(NULL == _methods, "underlying memory not zeroed?");
 517   assert(is_instance_klass(), "is layout incorrect?");
 518   assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
 519 


2465     array_klasses()->remove_unshareable_info();
2466   }
2467 
2468   // These are not allocated from metaspace. They are safe to set to NULL.
2469   _source_debug_extension = NULL;
2470   _dep_context = NULL;
2471   _osr_nmethods_head = NULL;
2472 #if INCLUDE_JVMTI
2473   _breakpoints = NULL;
2474   _previous_versions = NULL;
2475   _cached_class_file = NULL;
2476   _jvmti_cached_class_field_map = NULL;
2477 #endif
2478 
2479   _init_thread = NULL;
2480   _methods_jmethod_ids = NULL;
2481   _jni_ids = NULL;
2482   _oop_map_cache = NULL;
2483   // clear _nest_host to ensure re-load at runtime
2484   _nest_host = NULL;

2485   _package_entry = NULL;
2486   _dep_context_last_cleaned = 0;
2487 }
2488 
2489 void InstanceKlass::remove_java_mirror() {
2490   Klass::remove_java_mirror();
2491 
2492   // do array classes also.
2493   if (array_klasses() != NULL) {
2494     array_klasses()->remove_java_mirror();
2495   }
2496 }
2497 
2498 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
2499   // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
2500   // before the InstanceKlass is added to the SystemDictionary. Make
2501   // sure the current state is <loaded.
2502   assert(!is_loaded(), "invalid init state");
2503   set_package(loader_data, CHECK);
2504   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);


2630   // Deallocate breakpoint records
2631   if (breakpoints() != 0x0) {
2632     methods_do(clear_all_breakpoints);
2633     assert(breakpoints() == 0x0, "should have cleared breakpoints");
2634   }
2635 
2636   // deallocate the cached class file
2637   if (_cached_class_file != NULL) {
2638     os::free(_cached_class_file);
2639     _cached_class_file = NULL;
2640   }
2641 #endif
2642 
2643   // Decrement symbol reference counts associated with the unloaded class.
2644   if (_name != NULL) _name->decrement_refcount();
2645 
2646   // unreference array name derived from this class name (arrays of an unloaded
2647   // class can't be referenced anymore).
2648   if (_array_name != NULL)  _array_name->decrement_refcount();
2649   FREE_C_HEAP_ARRAY(char, _source_debug_extension);






2650 }
2651 
2652 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2653   if (array == NULL) {
2654     _source_debug_extension = NULL;
2655   } else {
2656     // Adding one to the attribute length in order to store a null terminator
2657     // character could cause an overflow because the attribute length is
2658     // already coded with an u4 in the classfile, but in practice, it's
2659     // unlikely to happen.
2660     assert((length+1) > length, "Overflow checking");
2661     char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2662     for (int i = 0; i < length; i++) {
2663       sde[i] = array[i];
2664     }
2665     sde[length] = '\0';
2666     _source_debug_extension = sde;
2667   }
2668 }
2669 




 136 
 137 static inline bool is_class_loader(const Symbol* class_name,
 138                                    const ClassFileParser& parser) {
 139   assert(class_name != NULL, "invariant");
 140 
 141   if (class_name == vmSymbols::java_lang_ClassLoader()) {
 142     return true;
 143   }
 144 
 145   if (SystemDictionary::ClassLoader_klass_loaded()) {
 146     const Klass* const super_klass = parser.super_klass();
 147     if (super_klass != NULL) {
 148       if (super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass())) {
 149         return true;
 150       }
 151     }
 152   }
 153   return false;
 154 }
 155 
 156 // private: called to verify that k is a static member of this nest.
 157 // We know that k is an instance class in the same package and hence the
 158 // same classloader.
 159 bool InstanceKlass::has_nest_member(InstanceKlass* k, TRAPS) const {
 160   assert(!is_hidden(), "unexpected hidden class");
 161   if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
 162     if (log_is_enabled(Trace, class, nestmates)) {
 163       ResourceMark rm(THREAD);
 164       log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
 165                                   k->external_name(), this->external_name());
 166     }
 167     return false;
 168   }
 169 
 170   if (log_is_enabled(Trace, class, nestmates)) {
 171     ResourceMark rm(THREAD);
 172     log_trace(class, nestmates)("Checking nest membership of %s in %s",
 173                                 k->external_name(), this->external_name());
 174   }
 175 
 176   // Check for a resolved cp entry , else fall back to a name check.
 177   // We don't want to resolve any class other than the one being checked.
 178   for (int i = 0; i < _nest_members->length(); i++) {
 179     int cp_index = _nest_members->at(i);
 180     if (_constants->tag_at(cp_index).is_klass()) {
 181       Klass* k2 = _constants->klass_at(cp_index, THREAD);
 182       assert(!HAS_PENDING_EXCEPTION, "Exceptions should not be possible here");
 183       if (k2 == k) {
 184         log_trace(class, nestmates)("- class is listed at nest_members[%d] => cp[%d]", i, cp_index);
 185         return true;
 186       }
 187     }
 188     else {
 189       Symbol* name = _constants->klass_name_at(cp_index);
 190       if (name == k->name()) {
 191         log_trace(class, nestmates)("- Found it at nest_members[%d] => cp[%d]", i, cp_index);
 192 
 193         // Names match so check actual klass. This may trigger class loading if
 194         // it doesn't match though that should be impossible as it means one classloader
 195         // has defined two different classes with the same name! A compiler thread won't be
 196         // able to perform that loading but we can't exclude the compiler threads from
 197         // executing this logic. But it should actually be impossible to trigger loading here.
 198         Klass* k2 = _constants->klass_at(cp_index, THREAD);
 199         assert(!HAS_PENDING_EXCEPTION, "Exceptions should not be possible here");


 200         if (k2 == k) {
 201           log_trace(class, nestmates)("- class is listed as a nest member");
 202           return true;
 203         }
 204         else {
 205           // same name but different klass!
 206           log_trace(class, nestmates)(" - klass comparison failed!");
 207           // can't have two names the same, so we're done
 208           return false;
 209         }
 210       }
 211     }
 212   }
 213   log_trace(class, nestmates)("- class is NOT a nest member!");
 214   return false;
 215 }
 216 


















 217 // Return nest-host class, resolving, validating and saving it if needed.
 218 // In cases where this is called from a thread that cannot do classloading
 219 // (such as a native JIT thread) then we simply return NULL, which in turn
 220 // causes the access check to return false. Such code will retry the access
 221 // from a more suitable environment later. Otherwise the _nest_host is always
 222 // set once this method returns.
 223 // Any errors from nest-host resolution must be preserved so they can be queried
 224 // from higher-level access checking code, and reported as part of access checking
 225 // exceptions.
 226 InstanceKlass* InstanceKlass::nest_host(TRAPS) {
 227   InstanceKlass* nest_host_k = _nest_host;
 228   if (nest_host_k != NULL) {
 229     return nest_host_k;
 230   }
 231 
 232   const bool doLog = log_is_enabled(Trace, class, nestmates);
 233 
 234   // need to resolve and save our nest-host class. This could be attempted
 235   // concurrently but as the result is idempotent and we don't use the class
 236   // then we do not need any synchronization beyond what is implicitly used
 237   // during class loading.
 238   if (_nest_host_index != 0) { // we have a real nest_host
 239     // Before trying to resolve check if we're in a suitable context
 240     if (!THREAD->can_call_java() && !_constants->tag_at(_nest_host_index).is_klass()) {
 241       if (doLog) {
 242         ResourceMark rm(THREAD);
 243         log_trace(class, nestmates)("Rejected resolution of nest-host of %s in unsuitable thread",
 244                                     this->external_name());
 245       }
 246       return NULL; // sentinel to say "try again from a different context"
 247     }
 248 
 249     if (doLog) {
 250       ResourceMark rm(THREAD);
 251       log_trace(class, nestmates)("Resolving nest-host of %s using cp entry for %s",
 252                                   this->external_name(),
 253                                   _constants->klass_name_at(_nest_host_index)->as_C_string());
 254     }
 255 
 256     Klass* k = _constants->klass_at(_nest_host_index, THREAD);
 257     if (HAS_PENDING_EXCEPTION) {








 258       ResourceMark rm(THREAD);
 259       stringStream ss;
 260       char* target_host_class = _constants->klass_name_at(_nest_host_index)->as_C_string();
 261       ss.print("Nest host resolution of %s with host %s failed: ",
 262                this->external_name(), target_host_class);
 263       java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
 264       _nest_host_res_error = ss.as_string(true /* on C-heap */);
 265       CLEAR_PENDING_EXCEPTION;
 266       if (doLog) {
 267         log_trace(class, nestmates)("%s", _nest_host_res_error);







 268       }
 269     } else {
 270       // A valid nest-host is an instance class in the current package that lists this
 271       // class as a nest member. If any of these conditions are not met the class is
 272       // its own nest-host.

 273       const char* error = NULL;
 274 
 275       // JVMS 5.4.4 indicates package check comes first
 276       if (is_same_class_package(k)) {

 277         // Now check actual membership. We can't be a member if our "host" is
 278         // not an instance class.
 279         if (k->is_instance_klass()) {
 280           nest_host_k = InstanceKlass::cast(k);
 281           bool is_member = nest_host_k->has_nest_member(this, THREAD);
 282           // exception is rare, perhaps impossible
 283           if (!HAS_PENDING_EXCEPTION) {
 284             if (is_member) {
 285               _nest_host = nest_host_k; // save resolved nest-host value
 286               if (doLog) {


 287                 ResourceMark rm(THREAD);
 288                 log_trace(class, nestmates)("Resolved nest-host of %s to %s",
 289                                             this->external_name(), k->external_name());
 290               }
 291               return nest_host_k;
 292             } else {
 293               error = "current type is not listed as a nest member";
 294             }
 295           } else {
 296             stringStream ss;
 297             ss.print("exception on member check: ");
 298             java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
 299             error = ss.as_string();
 300           }
 301         } else {
 302           error = "host is not an instance class";
 303         }

 304       } else {
 305         error = "types are in different packages";
 306       }
 307 
 308       // something went wrong, so record what and log it
 309       {
 310         ResourceMark rm(THREAD);
 311         stringStream ss;
 312         ss.print("Type %s (loader: %s) is not a nest member of type %s (loader: %s): %s",

 313                  this->external_name(),
 314                  this->class_loader_data()->loader_name_and_id(),
 315                  k->external_name(),
 316                  k->class_loader_data()->loader_name_and_id(),
 317                  error);
 318         _nest_host_res_error = ss.as_string(true /* on C-heap */);
 319 
 320         if (doLog) {
 321           log_trace(class, nestmates)("%s", _nest_host_res_error);
 322         }
 323       }







 324     }

 325   } else {
 326     if (doLog) {
 327       ResourceMark rm(THREAD);
 328       log_trace(class, nestmates)("Type %s is not part of a nest: setting nest-host to self",
 329                                   this->external_name());
 330     }



 331   }


 332 
 333   // Either not in an explicit nest, or else an error occurred, so
 334   // the nest-host is set to `this`.
 335   return (_nest_host = this);
 336 }
 337 
 338 // Dynamic nest member support: set this class's nest host to the given class.
 339 // This occurs as part of the class definition, as soon as the instanceKlass
 340 // has been created and doesn't require further resolution. The code:
 341 //    lookup().defineHiddenClass(bytes_for_X, NESTMATE);
 342 // results in:
 343 //    class_of_X.set_nest_host(lookup().lookupClass().getNestHost())
 344 // If it has an explicit _nest_host_index or _nest_members, these will be ignored.
 345 // We also know the "host" is a valid nest-host in the same package so we can
 346 // assert some of those facts.
 347 void InstanceKlass::set_nest_host(InstanceKlass* host, TRAPS) {
 348   assert(is_hidden(), "must be a hidden class");
 349   assert(host != NULL, "NULL nest host specified");
 350   assert(_nest_host == NULL, "current class has resolved nest-host");
 351   assert(_nest_host_res_error == NULL, "unexpected nest host resolution error exists: %s",
 352          _nest_host_res_error);
 353   assert((host->_nest_host == NULL && host->_nest_host_index == 0) ||
 354          (host->_nest_host == host), "proposed host is not a valid nest-host");
 355   // Can't assert this as package is not set yet:
 356   // assert(is_same_class_package(host), "proposed host is in wrong package");
 357 
 358   if (log_is_enabled(Trace, class, nestmates)) {
 359     ResourceMark rm(THREAD);
 360     const char* msg = "";
 361     // a hidden class does not expect a statically defined nest-host
 362     if (_nest_host_index > 0) {
 363       msg = "(the NestHost attribute in the current class is ignored)";


 364     } else if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) {
 365       msg = "(the NestMembers attribute in the current class is ignored)";


 366     }
 367     log_trace(class, nestmates)("Injected type %s into the nest of %s %s",
 368                                 this->external_name(),
 369                                 host->external_name(),
 370                                 msg);
 371   }
 372   // set dynamic nest host
 373   _nest_host = host;
 374 }
 375 
 376 // check if 'this' and k are nestmates (same nest_host), or k is our nest_host,
 377 // or we are k's nest_host - all of which is covered by comparing the two
 378 // resolved_nest_hosts
 379 bool InstanceKlass::has_nestmate_access_to(InstanceKlass* k, TRAPS) {
 380 
 381   assert(this != k, "this should be handled by higher-level code");
 382 
 383   // Per JVMS 5.4.4 we first resolve and validate the current class, then
 384   // the target class k.


 385 
 386   InstanceKlass* cur_host = nest_host(THREAD);

 387   if (cur_host == NULL) {
 388     return false;
 389   }
 390 
 391   Klass* k_nest_host = k->nest_host(THREAD);
 392   if (k_nest_host == NULL) {
 393     return false;
 394   }
 395 
 396   bool access = (cur_host == k_nest_host);
 397 
 398   if (log_is_enabled(Trace, class, nestmates)) {
 399     ResourceMark rm(THREAD);
 400     log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
 401                                 this->external_name(),
 402                                 access ? "" : "NOT ",
 403                                 k->external_name());
 404   }
 405 
 406   return access;
 407 }
 408 
 409 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
 410   bool is_hidden_or_anonymous = parser.is_hidden() || parser.is_unsafe_anonymous();
 411   const int size = InstanceKlass::size(parser.vtable_size(),


 459       _method_ordering->at_put(i, m->at(i));
 460     }
 461   } else {
 462     _method_ordering = Universe::the_empty_int_array();
 463   }
 464 }
 465 
 466 // create a new array of vtable_indices for default methods
 467 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
 468   Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
 469   assert(default_vtable_indices() == NULL, "only create once");
 470   set_default_vtable_indices(vtable_indices);
 471   return vtable_indices;
 472 }
 473 
 474 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id) :
 475   Klass(id),
 476   _nest_members(NULL),
 477   _nest_host_index(0),
 478   _nest_host(NULL),
 479   _nest_host_res_error(NULL),
 480   _record_components(NULL),
 481   _static_field_size(parser.static_field_size()),
 482   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 483   _itable_len(parser.itable_size()),
 484   _init_thread(NULL),
 485   _init_state(allocated),
 486   _reference_type(parser.reference_type())
 487 {
 488   set_vtable_length(parser.vtable_size());
 489   set_kind(kind);
 490   set_access_flags(parser.access_flags());
 491   if (parser.is_hidden()) set_is_hidden();
 492   set_is_unsafe_anonymous(parser.is_unsafe_anonymous());
 493   set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 494                                                     false));
 495 
 496   assert(NULL == _methods, "underlying memory not zeroed?");
 497   assert(is_instance_klass(), "is layout incorrect?");
 498   assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
 499 


2445     array_klasses()->remove_unshareable_info();
2446   }
2447 
2448   // These are not allocated from metaspace. They are safe to set to NULL.
2449   _source_debug_extension = NULL;
2450   _dep_context = NULL;
2451   _osr_nmethods_head = NULL;
2452 #if INCLUDE_JVMTI
2453   _breakpoints = NULL;
2454   _previous_versions = NULL;
2455   _cached_class_file = NULL;
2456   _jvmti_cached_class_field_map = NULL;
2457 #endif
2458 
2459   _init_thread = NULL;
2460   _methods_jmethod_ids = NULL;
2461   _jni_ids = NULL;
2462   _oop_map_cache = NULL;
2463   // clear _nest_host to ensure re-load at runtime
2464   _nest_host = NULL;
2465   _nest_host_res_error = NULL;
2466   _package_entry = NULL;
2467   _dep_context_last_cleaned = 0;
2468 }
2469 
2470 void InstanceKlass::remove_java_mirror() {
2471   Klass::remove_java_mirror();
2472 
2473   // do array classes also.
2474   if (array_klasses() != NULL) {
2475     array_klasses()->remove_java_mirror();
2476   }
2477 }
2478 
2479 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
2480   // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
2481   // before the InstanceKlass is added to the SystemDictionary. Make
2482   // sure the current state is <loaded.
2483   assert(!is_loaded(), "invalid init state");
2484   set_package(loader_data, CHECK);
2485   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);


2611   // Deallocate breakpoint records
2612   if (breakpoints() != 0x0) {
2613     methods_do(clear_all_breakpoints);
2614     assert(breakpoints() == 0x0, "should have cleared breakpoints");
2615   }
2616 
2617   // deallocate the cached class file
2618   if (_cached_class_file != NULL) {
2619     os::free(_cached_class_file);
2620     _cached_class_file = NULL;
2621   }
2622 #endif
2623 
2624   // Decrement symbol reference counts associated with the unloaded class.
2625   if (_name != NULL) _name->decrement_refcount();
2626 
2627   // unreference array name derived from this class name (arrays of an unloaded
2628   // class can't be referenced anymore).
2629   if (_array_name != NULL)  _array_name->decrement_refcount();
2630   FREE_C_HEAP_ARRAY(char, _source_debug_extension);
2631 
2632   // deallocate memoized nest-host resolution error
2633   if (_nest_host_res_error != NULL) {
2634     FREE_C_HEAP_ARRAY(char, _nest_host_res_error);
2635     _nest_host_res_error = NULL;
2636   }
2637 }
2638 
2639 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2640   if (array == NULL) {
2641     _source_debug_extension = NULL;
2642   } else {
2643     // Adding one to the attribute length in order to store a null terminator
2644     // character could cause an overflow because the attribute length is
2645     // already coded with an u4 in the classfile, but in practice, it's
2646     // unlikely to happen.
2647     assert((length+1) > length, "Overflow checking");
2648     char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2649     for (int i = 0; i < length; i++) {
2650       sde[i] = array[i];
2651     }
2652     sde[length] = '\0';
2653     _source_debug_extension = sde;
2654   }
2655 }
2656 


< prev index next >