< prev index next >

src/hotspot/share/oops/instanceKlass.cpp

Print this page




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

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


 117 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)     \
 118   {                                                              \
 119     char* data = NULL;                                           \
 120     int len = 0;                                                 \
 121     Symbol* clss_name = name();                                  \
 122     if (clss_name != NULL) {                                     \
 123       data = (char*)clss_name->bytes();                          \
 124       len = clss_name->utf8_length();                            \
 125     }                                                            \
 126     HOTSPOT_CLASS_INITIALIZATION_##type(                         \
 127       data, len, (void*)class_loader(), thread_type, wait);      \
 128   }
 129 
 130 #else //  ndef DTRACE_ENABLED
 131 
 132 #define DTRACE_CLASSINIT_PROBE(type, thread_type)
 133 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)
 134 
 135 #endif //  ndef DTRACE_ENABLED
 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.


 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 // Under any conditions where the _nest_host can be set to non-NULL the resulting value
 230 // of it and, if applicable, _nest_host_res_error, are idempotent. But as we can be
 231 // executing this code concurrently we need to ensure ordering is maintained so that
 232 // errors messages can safely be read.
 233 InstanceKlass* InstanceKlass::nest_host(TRAPS) {
 234   InstanceKlass* nest_host_k = _nest_host;
 235   if (nest_host_k != NULL) {
 236     return nest_host_k;
 237   }
 238 
 239   ResourceMark rm(THREAD);
 240 
 241   // need to resolve and save our nest-host class.
 242   if (_nest_host_index != 0) { // we have a real nest_host
 243     // Before trying to resolve check if we're in a suitable context
 244     if (!THREAD->can_call_java() && !_constants->tag_at(_nest_host_index).is_klass()) {
 245       log_trace(class, nestmates)("Rejected resolution of nest-host of %s in unsuitable thread",
 246                                   this->external_name());
 247       return NULL; // sentinel to say "try again from a different context"
 248     }
 249 
 250     log_trace(class, nestmates)("Resolving nest-host of %s using cp entry for %s",
 251                                 this->external_name(),
 252                                 _constants->klass_name_at(_nest_host_index)->as_C_string());
 253 
 254     Klass* k = _constants->klass_at(_nest_host_index, THREAD);
 255     if (HAS_PENDING_EXCEPTION) {
 256       if (PENDING_EXCEPTION->is_a(SystemDictionary::VirtualMachineError_klass())) {
 257         return NULL; // propagate VMEs
 258       }
 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       // ensure we see _nest_host_res_error is set if _nest_host is non-NULL
 266       OrderAccess::storestore();
 267       CLEAR_PENDING_EXCEPTION;
 268 
 269       log_trace(class, nestmates)("%s", _nest_host_res_error);
 270     } else {
 271       // A valid nest-host is an instance class in the current package that lists this
 272       // class as a nest member. If any of these conditions are not met the class is
 273       // its own nest-host.
 274       const char* error = NULL;
 275 
 276       // JVMS 5.4.4 indicates package check comes first
 277       if (is_same_class_package(k)) {
 278         // Now check actual membership. We can't be a member if our "host" is
 279         // not an instance class.
 280         if (k->is_instance_klass()) {
 281           nest_host_k = InstanceKlass::cast(k);
 282           bool is_member = nest_host_k->has_nest_member(this, THREAD);
 283           // exception is rare, perhaps impossible
 284           if (!HAS_PENDING_EXCEPTION) {
 285             if (is_member) {
 286               _nest_host = nest_host_k; // save resolved nest-host value
 287 
 288               log_trace(class, nestmates)("Resolved nest-host of %s to %s",
 289                                           this->external_name(), k->external_name());


 299             ss.print("exception on member check: ");
 300             java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
 301             error = ss.as_string();
 302           }
 303         } else {
 304           error = "host is not an instance class";
 305         }
 306       } else {
 307         error = "types are in different packages";
 308       }
 309 
 310       // something went wrong, so record what and log it
 311       {
 312         stringStream ss;
 313         ss.print("Type %s (loader: %s) is not a nest member of type %s (loader: %s): %s",
 314                  this->external_name(),
 315                  this->class_loader_data()->loader_name_and_id(),
 316                  k->external_name(),
 317                  k->class_loader_data()->loader_name_and_id(),
 318                  error);
 319         _nest_host_res_error = ss.as_string(true /* on C-heap */);
 320         // ensure we see _nest_host_res_error is set if _nest_host is non-NULL
 321         OrderAccess::storestore();
 322 
 323         log_trace(class, nestmates)("%s", _nest_host_res_error);
 324       }
 325     }
 326   } else {
 327     log_trace(class, nestmates)("Type %s is not part of a nest: setting nest-host to self",
 328                                 this->external_name());
 329   }
 330 
 331   // Either not in an explicit nest, or else an error occurred, so
 332   // the nest-host is set to `this`. Any thread that sees this assignment
 333   // will also see any setting of _nest_host_res_error, if applicable.
 334   return (_nest_host = this);
 335 }
 336 
 337 // Dynamic nest member support: set this class's nest host to the given class.
 338 // This occurs as part of the class definition, as soon as the instanceKlass
 339 // has been created and doesn't require further resolution. The code:
 340 //    lookup().defineHiddenClass(bytes_for_X, NESTMATE);
 341 // results in:
 342 //    class_of_X.set_nest_host(lookup().lookupClass().getNestHost())
 343 // If it has an explicit _nest_host_index or _nest_members, these will be ignored.
 344 // We also know the "host" is a valid nest-host in the same package so we can
 345 // assert some of those facts.
 346 void InstanceKlass::set_nest_host(InstanceKlass* host, TRAPS) {
 347   assert(is_hidden(), "must be a hidden class");
 348   assert(host != NULL, "NULL nest host specified");
 349   assert(_nest_host == NULL, "current class has resolved nest-host");
 350   assert(_nest_host_res_error == NULL, "unexpected nest host resolution error exists: %s",
 351          _nest_host_res_error);
 352   assert((host->_nest_host == NULL && host->_nest_host_index == 0) ||
 353          (host->_nest_host == host), "proposed host is not a valid nest-host");
 354   // Can't assert this as package is not set yet:
 355   // assert(is_same_class_package(host), "proposed host is in wrong package");
 356 
 357   if (log_is_enabled(Trace, class, nestmates)) {
 358     ResourceMark rm(THREAD);
 359     const char* msg = "";
 360     // a hidden class does not expect a statically defined nest-host
 361     if (_nest_host_index > 0) {
 362       msg = "(the NestHost attribute in the current class is ignored)";
 363     } else if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) {
 364       msg = "(the NestMembers attribute in the current class is ignored)";
 365     }
 366     log_trace(class, nestmates)("Injected type %s into the nest of %s %s",
 367                                 this->external_name(),
 368                                 host->external_name(),
 369                                 msg);
 370   }
 371   // set dynamic nest host


 389   InstanceKlass* cur_host = nest_host(CHECK_false);
 390   if (cur_host == NULL) {
 391     return false;
 392   }
 393 
 394   Klass* k_nest_host = k->nest_host(CHECK_false);
 395   if (k_nest_host == NULL) {
 396     return false;
 397   }
 398 
 399   bool access = (cur_host == k_nest_host);
 400 
 401   ResourceMark rm(THREAD);
 402   log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
 403                               this->external_name(),
 404                               access ? "" : "NOT ",
 405                               k->external_name());
 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(),
 412                                        parser.itable_size(),
 413                                        nonstatic_oop_map_size(parser.total_oop_map_count()),
 414                                        parser.is_interface(),
 415                                        parser.is_unsafe_anonymous(),
 416                                        should_store_fingerprint(is_hidden_or_anonymous));
 417 
 418   const Symbol* const class_name = parser.class_name();
 419   assert(class_name != NULL, "invariant");
 420   ClassLoaderData* loader_data = parser.loader_data();
 421   assert(loader_data != NULL, "invariant");
 422 
 423   InstanceKlass* ik;
 424 
 425   // Allocation
 426   if (REF_NONE == parser.reference_type()) {
 427     if (class_name == vmSymbols::java_lang_Class()) {
 428       // mirror


 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 




  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "aot/aotLoader.hpp"
  28 #include "classfile/classFileParser.hpp"
  29 #include "classfile/classFileStream.hpp"
  30 #include "classfile/classLoader.hpp"
  31 #include "classfile/classLoaderData.inline.hpp"
  32 #include "classfile/javaClasses.hpp"
  33 #include "classfile/moduleEntry.hpp"
  34 #include "classfile/resolutionErrors.hpp"
  35 #include "classfile/symbolTable.hpp"
  36 #include "classfile/systemDictionary.hpp"
  37 #include "classfile/systemDictionaryShared.hpp"
  38 #include "classfile/verifier.hpp"
  39 #include "classfile/vmSymbols.hpp"
  40 #include "code/dependencyContext.hpp"
  41 #include "compiler/compileBroker.hpp"
  42 #include "gc/shared/collectedHeap.inline.hpp"
  43 #include "interpreter/oopMapCache.hpp"
  44 #include "interpreter/rewriter.hpp"
  45 #include "jvmtifiles/jvmti.h"
  46 #include "logging/log.hpp"
  47 #include "logging/logMessage.hpp"
  48 #include "logging/logStream.hpp"
  49 #include "memory/allocation.inline.hpp"
  50 #include "memory/iterator.inline.hpp"
  51 #include "memory/metadataFactory.hpp"
  52 #include "memory/metaspaceClosure.hpp"
  53 #include "memory/metaspaceShared.hpp"
  54 #include "memory/oopFactory.hpp"


 118 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)     \
 119   {                                                              \
 120     char* data = NULL;                                           \
 121     int len = 0;                                                 \
 122     Symbol* clss_name = name();                                  \
 123     if (clss_name != NULL) {                                     \
 124       data = (char*)clss_name->bytes();                          \
 125       len = clss_name->utf8_length();                            \
 126     }                                                            \
 127     HOTSPOT_CLASS_INITIALIZATION_##type(                         \
 128       data, len, (void*)class_loader(), thread_type, wait);      \
 129   }
 130 
 131 #else //  ndef DTRACE_ENABLED
 132 
 133 #define DTRACE_CLASSINIT_PROBE(type, thread_type)
 134 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)
 135 
 136 #endif //  ndef DTRACE_ENABLED
 137 
 138 
 139 static inline bool is_class_loader(const Symbol* class_name,
 140                                    const ClassFileParser& parser) {
 141   assert(class_name != NULL, "invariant");
 142 
 143   if (class_name == vmSymbols::java_lang_ClassLoader()) {
 144     return true;
 145   }
 146 
 147   if (SystemDictionary::ClassLoader_klass_loaded()) {
 148     const Klass* const super_klass = parser.super_klass();
 149     if (super_klass != NULL) {
 150       if (super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass())) {
 151         return true;
 152       }
 153     }
 154   }
 155   return false;
 156 }
 157 
 158 // private: called to verify that k is a static member of this nest.


 211           // can't have two names the same, so we're done
 212           return false;
 213         }
 214       }
 215     }
 216   }
 217   log_trace(class, nestmates)("- class is NOT a nest member!");
 218   return false;
 219 }
 220 
 221 // Return nest-host class, resolving, validating and saving it if needed.
 222 // In cases where this is called from a thread that cannot do classloading
 223 // (such as a native JIT thread) then we simply return NULL, which in turn
 224 // causes the access check to return false. Such code will retry the access
 225 // from a more suitable environment later. Otherwise the _nest_host is always
 226 // set once this method returns.
 227 // Any errors from nest-host resolution must be preserved so they can be queried
 228 // from higher-level access checking code, and reported as part of access checking
 229 // exceptions.
 230 // VirtualMachineErrors are propagated with a NULL return.
 231 // Under any conditions where the _nest_host can be set to non-NULL the resulting
 232 // value of it and, if applicable, the nest host resolution/validation error,
 233 // are idempotent.

 234 InstanceKlass* InstanceKlass::nest_host(TRAPS) {
 235   InstanceKlass* nest_host_k = _nest_host;
 236   if (nest_host_k != NULL) {
 237     return nest_host_k;
 238   }
 239 
 240   ResourceMark rm(THREAD);
 241 
 242   // need to resolve and save our nest-host class.
 243   if (_nest_host_index != 0) { // we have a real nest_host
 244     // Before trying to resolve check if we're in a suitable context
 245     if (!THREAD->can_call_java() && !_constants->tag_at(_nest_host_index).is_klass()) {
 246       log_trace(class, nestmates)("Rejected resolution of nest-host of %s in unsuitable thread",
 247                                   this->external_name());
 248       return NULL; // sentinel to say "try again from a different context"
 249     }
 250 
 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     Klass* k = _constants->klass_at(_nest_host_index, THREAD);
 256     if (HAS_PENDING_EXCEPTION) {
 257       if (PENDING_EXCEPTION->is_a(SystemDictionary::VirtualMachineError_klass())) {
 258         return NULL; // propagate VMEs
 259       }
 260       stringStream ss;
 261       char* target_host_class = _constants->klass_name_at(_nest_host_index)->as_C_string();
 262       ss.print("Nest host resolution of %s with host %s failed: ",
 263                this->external_name(), target_host_class);
 264       java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
 265       const char* msg = ss.as_string(true /* on C-heap */);
 266       constantPoolHandle cph(THREAD, constants());
 267       SystemDictionary::add_nest_host_error(cph, _nest_host_index, msg);
 268       CLEAR_PENDING_EXCEPTION;
 269 
 270       log_trace(class, nestmates)("%s", msg);
 271     } else {
 272       // A valid nest-host is an instance class in the current package that lists this
 273       // class as a nest member. If any of these conditions are not met the class is
 274       // its own nest-host.
 275       const char* error = NULL;
 276 
 277       // JVMS 5.4.4 indicates package check comes first
 278       if (is_same_class_package(k)) {
 279         // Now check actual membership. We can't be a member if our "host" is
 280         // not an instance class.
 281         if (k->is_instance_klass()) {
 282           nest_host_k = InstanceKlass::cast(k);
 283           bool is_member = nest_host_k->has_nest_member(this, THREAD);
 284           // exception is rare, perhaps impossible
 285           if (!HAS_PENDING_EXCEPTION) {
 286             if (is_member) {
 287               _nest_host = nest_host_k; // save resolved nest-host value
 288 
 289               log_trace(class, nestmates)("Resolved nest-host of %s to %s",
 290                                           this->external_name(), k->external_name());


 300             ss.print("exception on member check: ");
 301             java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
 302             error = ss.as_string();
 303           }
 304         } else {
 305           error = "host is not an instance class";
 306         }
 307       } else {
 308         error = "types are in different packages";
 309       }
 310 
 311       // something went wrong, so record what and log it
 312       {
 313         stringStream ss;
 314         ss.print("Type %s (loader: %s) is not a nest member of type %s (loader: %s): %s",
 315                  this->external_name(),
 316                  this->class_loader_data()->loader_name_and_id(),
 317                  k->external_name(),
 318                  k->class_loader_data()->loader_name_and_id(),
 319                  error);
 320         const char* msg = ss.as_string(true /* on C-heap */);
 321         constantPoolHandle cph(THREAD, constants());
 322         SystemDictionary::add_nest_host_error(cph, _nest_host_index, msg);
 323         log_trace(class, nestmates)("%s", msg);

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


 389   InstanceKlass* cur_host = nest_host(CHECK_false);
 390   if (cur_host == NULL) {
 391     return false;
 392   }
 393 
 394   Klass* k_nest_host = k->nest_host(CHECK_false);
 395   if (k_nest_host == NULL) {
 396     return false;
 397   }
 398 
 399   bool access = (cur_host == k_nest_host);
 400 
 401   ResourceMark rm(THREAD);
 402   log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
 403                               this->external_name(),
 404                               access ? "" : "NOT ",
 405                               k->external_name());
 406   return access;
 407 }
 408 
 409 const char* InstanceKlass::nest_host_error(TRAPS) {
 410   if (_nest_host_index == 0) {
 411     return NULL;
 412   } else {
 413     constantPoolHandle cph(THREAD, constants());
 414     return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
 415   }
 416 }
 417 
 418 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
 419   bool is_hidden_or_anonymous = parser.is_hidden() || parser.is_unsafe_anonymous();
 420   const int size = InstanceKlass::size(parser.vtable_size(),
 421                                        parser.itable_size(),
 422                                        nonstatic_oop_map_size(parser.total_oop_map_count()),
 423                                        parser.is_interface(),
 424                                        parser.is_unsafe_anonymous(),
 425                                        should_store_fingerprint(is_hidden_or_anonymous));
 426 
 427   const Symbol* const class_name = parser.class_name();
 428   assert(class_name != NULL, "invariant");
 429   ClassLoaderData* loader_data = parser.loader_data();
 430   assert(loader_data != NULL, "invariant");
 431 
 432   InstanceKlass* ik;
 433 
 434   // Allocation
 435   if (REF_NONE == parser.reference_type()) {
 436     if (class_name == vmSymbols::java_lang_Class()) {
 437       // mirror


 468       _method_ordering->at_put(i, m->at(i));
 469     }
 470   } else {
 471     _method_ordering = Universe::the_empty_int_array();
 472   }
 473 }
 474 
 475 // create a new array of vtable_indices for default methods
 476 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
 477   Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
 478   assert(default_vtable_indices() == NULL, "only create once");
 479   set_default_vtable_indices(vtable_indices);
 480   return vtable_indices;
 481 }
 482 
 483 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id) :
 484   Klass(id),
 485   _nest_members(NULL),
 486   _nest_host_index(0),
 487   _nest_host(NULL),

 488   _record_components(NULL),
 489   _static_field_size(parser.static_field_size()),
 490   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 491   _itable_len(parser.itable_size()),
 492   _init_thread(NULL),
 493   _init_state(allocated),
 494   _reference_type(parser.reference_type())
 495 {
 496   set_vtable_length(parser.vtable_size());
 497   set_kind(kind);
 498   set_access_flags(parser.access_flags());
 499   if (parser.is_hidden()) set_is_hidden();
 500   set_is_unsafe_anonymous(parser.is_unsafe_anonymous());
 501   set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 502                                                     false));
 503 
 504   assert(NULL == _methods, "underlying memory not zeroed?");
 505   assert(is_instance_klass(), "is layout incorrect?");
 506   assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
 507 


2453     array_klasses()->remove_unshareable_info();
2454   }
2455 
2456   // These are not allocated from metaspace. They are safe to set to NULL.
2457   _source_debug_extension = NULL;
2458   _dep_context = NULL;
2459   _osr_nmethods_head = NULL;
2460 #if INCLUDE_JVMTI
2461   _breakpoints = NULL;
2462   _previous_versions = NULL;
2463   _cached_class_file = NULL;
2464   _jvmti_cached_class_field_map = NULL;
2465 #endif
2466 
2467   _init_thread = NULL;
2468   _methods_jmethod_ids = NULL;
2469   _jni_ids = NULL;
2470   _oop_map_cache = NULL;
2471   // clear _nest_host to ensure re-load at runtime
2472   _nest_host = NULL;

2473   _package_entry = NULL;
2474   _dep_context_last_cleaned = 0;
2475 }
2476 
2477 void InstanceKlass::remove_java_mirror() {
2478   Klass::remove_java_mirror();
2479 
2480   // do array classes also.
2481   if (array_klasses() != NULL) {
2482     array_klasses()->remove_java_mirror();
2483   }
2484 }
2485 
2486 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
2487   // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
2488   // before the InstanceKlass is added to the SystemDictionary. Make
2489   // sure the current state is <loaded.
2490   assert(!is_loaded(), "invalid init state");
2491   set_package(loader_data, CHECK);
2492   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);


2618   // Deallocate breakpoint records
2619   if (breakpoints() != 0x0) {
2620     methods_do(clear_all_breakpoints);
2621     assert(breakpoints() == 0x0, "should have cleared breakpoints");
2622   }
2623 
2624   // deallocate the cached class file
2625   if (_cached_class_file != NULL) {
2626     os::free(_cached_class_file);
2627     _cached_class_file = NULL;
2628   }
2629 #endif
2630 
2631   // Decrement symbol reference counts associated with the unloaded class.
2632   if (_name != NULL) _name->decrement_refcount();
2633 
2634   // unreference array name derived from this class name (arrays of an unloaded
2635   // class can't be referenced anymore).
2636   if (_array_name != NULL)  _array_name->decrement_refcount();
2637   FREE_C_HEAP_ARRAY(char, _source_debug_extension);






2638 }
2639 
2640 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2641   if (array == NULL) {
2642     _source_debug_extension = NULL;
2643   } else {
2644     // Adding one to the attribute length in order to store a null terminator
2645     // character could cause an overflow because the attribute length is
2646     // already coded with an u4 in the classfile, but in practice, it's
2647     // unlikely to happen.
2648     assert((length+1) > length, "Overflow checking");
2649     char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2650     for (int i = 0; i < length; i++) {
2651       sde[i] = array[i];
2652     }
2653     sde[length] = '\0';
2654     _source_debug_extension = sde;
2655   }
2656 }
2657 


< prev index next >