< 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 || PENDING_EXCEPTION->is_a(SystemDictionary::VirtualMachineError_klass()),
 183              "Exceptions should not be possible here");
 184       if (k2 == k) {
 185         log_trace(class, nestmates)("- class is listed at nest_members[%d] => cp[%d]", i, cp_index);
 186         return true;
 187       }
 188     }
 189     else {
 190       Symbol* name = _constants->klass_name_at(cp_index);
 191       if (name == k->name()) {
 192         log_trace(class, nestmates)("- Found it at nest_members[%d] => cp[%d]", i, cp_index);
 193 
 194         // Names match so check actual klass. This may trigger class loading if
 195         // it doesn't match though that should be impossible as it means one classloader
 196         // has defined two different classes with the same name! A compiler thread won't be
 197         // able to perform that loading but we can't exclude the compiler threads from
 198         // executing this logic. But it should actually be impossible to trigger loading here.
 199         Klass* k2 = _constants->klass_at(cp_index, THREAD);
 200         assert(!HAS_PENDING_EXCEPTION || PENDING_EXCEPTION->is_a(SystemDictionary::VirtualMachineError_klass()),
 201                "Exceptions should not be possible here");

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


















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


 263       }



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




 274       }
 275     } else {



 276       // A valid nest-host is an instance class in the current package that lists this
 277       // class as a nest member. If any of these conditions are not met the class is
 278       // its own nest-host.

 279       const char* error = NULL;
 280 
 281       // JVMS 5.4.4 indicates package check comes first
 282       if (is_same_class_package(k)) {

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


 293                 ResourceMark rm(THREAD);
 294                 log_trace(class, nestmates)("Resolved nest-host of %s to %s",
 295                                             this->external_name(), k->external_name());
 296               }
 297               return nest_host_k;
 298             } else {
 299               error = "current type is not listed as a nest member";
 300             }
 301           } else {
 302             if (PENDING_EXCEPTION->is_a(SystemDictionary::VirtualMachineError_klass())) {
 303               return NULL; // propagate VMEs
 304             }
 305             stringStream ss;
 306             ss.print("exception on member check: ");
 307             java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
 308             error = ss.as_string();
 309           }
 310         } else {
 311           error = "host is not an instance class";
 312         }

 313       } else {
 314         error = "types are in different packages";
 315       }
 316 
 317       // something went wrong, so record what and log it
 318       {
 319         ResourceMark rm(THREAD);
 320         stringStream ss;
 321         ss.print("Type %s (loader: %s) is not a nest member of type %s (loader: %s): %s",

 322                  this->external_name(),
 323                  this->class_loader_data()->loader_name_and_id(),
 324                  k->external_name(),
 325                  k->class_loader_data()->loader_name_and_id(),
 326                  error);
 327         _nest_host_res_error = ss.as_string(true /* on C-heap */);
 328 
 329         if (doLog) {
 330           log_trace(class, nestmates)("%s", _nest_host_res_error);
 331         }
 332       }







 333     }

 334   } else {
 335     if (doLog) {
 336       ResourceMark rm(THREAD);
 337       log_trace(class, nestmates)("Type %s is not part of a nest: setting nest-host to self",
 338                                   this->external_name());
 339     }



 340   }


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


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


 375     }
 376     log_trace(class, nestmates)("Injected type %s into the nest of %s %s",
 377                                 this->external_name(),
 378                                 host->external_name(),
 379                                 msg);
 380   }
 381   // set dynamic nest host
 382   _nest_host = host;
 383 }
 384 
 385 // check if 'this' and k are nestmates (same nest_host), or k is our nest_host,
 386 // or we are k's nest_host - all of which is covered by comparing the two
 387 // resolved_nest_hosts.
 388 // Any exceptions (i.e. VMEs) are propagated.
 389 bool InstanceKlass::has_nestmate_access_to(InstanceKlass* k, TRAPS) {
 390 
 391   assert(this != k, "this should be handled by higher-level code");
 392 
 393   // Per JVMS 5.4.4 we first resolve and validate the current class, then
 394   // the target class k.


 395 
 396   InstanceKlass* cur_host = nest_host(CHECK_false);

 397   if (cur_host == NULL) {
 398     return false;
 399   }
 400 
 401   Klass* k_nest_host = k->nest_host(CHECK_false);
 402   if (k_nest_host == NULL) {
 403     return false;
 404   }
 405 
 406   bool access = (cur_host == k_nest_host);
 407 
 408   if (log_is_enabled(Trace, class, nestmates)) {
 409     ResourceMark rm(THREAD);
 410     log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
 411                                 this->external_name(),
 412                                 access ? "" : "NOT ",
 413                                 k->external_name());
 414   }
 415 
 416   return access;
 417 }
 418 
 419 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
 420   bool is_hidden_or_anonymous = parser.is_hidden() || parser.is_unsafe_anonymous();
 421   const int size = InstanceKlass::size(parser.vtable_size(),


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


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


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


< prev index next >