src/share/vm/ci/ciInstanceKlass.cpp

Sat, 12 Jun 2010 22:53:43 -0700

author
jrose
date
Sat, 12 Jun 2010 22:53:43 -0700
changeset 1959
b918d354830a
parent 1957
136b78722a08
child 2000
3941674cc7fa
permissions
-rw-r--r--

6960865: ldc of unloaded class throws an assert in ciTypeFlow
Summary: Support java_mirror for unloaded klasses, arrays as well as instances. Simplify ciTypeFlow by removing unused path.
Reviewed-by: kvn

     1 /*
     2  * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    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  */
    25 #include "incls/_precompiled.incl"
    26 #include "incls/_ciInstanceKlass.cpp.incl"
    28 // ciInstanceKlass
    29 //
    30 // This class represents a klassOop in the HotSpot virtual machine
    31 // whose Klass part in an instanceKlass.
    33 // ------------------------------------------------------------------
    34 // ciInstanceKlass::ciInstanceKlass
    35 //
    36 // Loaded instance klass.
    37 ciInstanceKlass::ciInstanceKlass(KlassHandle h_k) :
    38   ciKlass(h_k), _non_static_fields(NULL)
    39 {
    40   assert(get_Klass()->oop_is_instance(), "wrong type");
    41   instanceKlass* ik = get_instanceKlass();
    43   AccessFlags access_flags = ik->access_flags();
    44   _flags = ciFlags(access_flags);
    45   _has_finalizer = access_flags.has_finalizer();
    46   _has_subklass = ik->subklass() != NULL;
    47   _is_initialized = ik->is_initialized();
    48   // Next line must follow and use the result of the previous line:
    49   _is_linked = _is_initialized || ik->is_linked();
    50   _nonstatic_field_size = ik->nonstatic_field_size();
    51   _has_nonstatic_fields = ik->has_nonstatic_fields();
    52   _nonstatic_fields = NULL; // initialized lazily by compute_nonstatic_fields:
    54   _nof_implementors = ik->nof_implementors();
    55   for (int i = 0; i < implementors_limit; i++) {
    56     _implementors[i] = NULL;  // we will fill these lazily
    57   }
    59   Thread *thread = Thread::current();
    60   if (ciObjectFactory::is_initialized()) {
    61     _loader = JNIHandles::make_local(thread, ik->class_loader());
    62     _protection_domain = JNIHandles::make_local(thread,
    63                                                 ik->protection_domain());
    64     _is_shared = false;
    65   } else {
    66     Handle h_loader(thread, ik->class_loader());
    67     Handle h_protection_domain(thread, ik->protection_domain());
    68     _loader = JNIHandles::make_global(h_loader);
    69     _protection_domain = JNIHandles::make_global(h_protection_domain);
    70     _is_shared = true;
    71   }
    73   // Lazy fields get filled in only upon request.
    74   _super  = NULL;
    75   _java_mirror = NULL;
    77   if (is_shared()) {
    78     if (h_k() != SystemDictionary::Object_klass()) {
    79       super();
    80     }
    81     java_mirror();
    82     //compute_nonstatic_fields();  // done outside of constructor
    83   }
    85   _field_cache = NULL;
    86 }
    88 // Version for unloaded classes:
    89 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
    90                                  jobject loader, jobject protection_domain)
    91   : ciKlass(name, ciInstanceKlassKlass::make())
    92 {
    93   assert(name->byte_at(0) != '[', "not an instance klass");
    94   _is_initialized = false;
    95   _is_linked = false;
    96   _nonstatic_field_size = -1;
    97   _has_nonstatic_fields = false;
    98   _nonstatic_fields = NULL;
    99   _nof_implementors = -1;
   100   _loader = loader;
   101   _protection_domain = protection_domain;
   102   _is_shared = false;
   103   _super = NULL;
   104   _java_mirror = NULL;
   105   _field_cache = NULL;
   106 }
   110 // ------------------------------------------------------------------
   111 // ciInstanceKlass::compute_shared_is_initialized
   112 bool ciInstanceKlass::compute_shared_is_initialized() {
   113   GUARDED_VM_ENTRY(
   114     instanceKlass* ik = get_instanceKlass();
   115     _is_initialized = ik->is_initialized();
   116     return _is_initialized;
   117   )
   118 }
   120 // ------------------------------------------------------------------
   121 // ciInstanceKlass::compute_shared_is_linked
   122 bool ciInstanceKlass::compute_shared_is_linked() {
   123   GUARDED_VM_ENTRY(
   124     instanceKlass* ik = get_instanceKlass();
   125     _is_linked = ik->is_linked();
   126     return _is_linked;
   127   )
   128 }
   130 // ------------------------------------------------------------------
   131 // ciInstanceKlass::compute_shared_has_subklass
   132 bool ciInstanceKlass::compute_shared_has_subklass() {
   133   GUARDED_VM_ENTRY(
   134     instanceKlass* ik = get_instanceKlass();
   135     _has_subklass = ik->subklass() != NULL;
   136     return _has_subklass;
   137   )
   138 }
   140 // ------------------------------------------------------------------
   141 // ciInstanceKlass::compute_shared_nof_implementors
   142 int ciInstanceKlass::compute_shared_nof_implementors() {
   143   // We requery this property, since it is a very old ciObject.
   144   GUARDED_VM_ENTRY(
   145     instanceKlass* ik = get_instanceKlass();
   146     _nof_implementors = ik->nof_implementors();
   147     return _nof_implementors;
   148   )
   149 }
   151 // ------------------------------------------------------------------
   152 // ciInstanceKlass::loader
   153 oop ciInstanceKlass::loader() {
   154   ASSERT_IN_VM;
   155   return JNIHandles::resolve(_loader);
   156 }
   158 // ------------------------------------------------------------------
   159 // ciInstanceKlass::loader_handle
   160 jobject ciInstanceKlass::loader_handle() {
   161   return _loader;
   162 }
   164 // ------------------------------------------------------------------
   165 // ciInstanceKlass::protection_domain
   166 oop ciInstanceKlass::protection_domain() {
   167   ASSERT_IN_VM;
   168   return JNIHandles::resolve(_protection_domain);
   169 }
   171 // ------------------------------------------------------------------
   172 // ciInstanceKlass::protection_domain_handle
   173 jobject ciInstanceKlass::protection_domain_handle() {
   174   return _protection_domain;
   175 }
   177 // ------------------------------------------------------------------
   178 // ciInstanceKlass::field_cache
   179 //
   180 // Get the field cache associated with this klass.
   181 ciConstantPoolCache* ciInstanceKlass::field_cache() {
   182   if (is_shared()) {
   183     return NULL;
   184   }
   185   if (_field_cache == NULL) {
   186     assert(!is_java_lang_Object(), "Object has no fields");
   187     Arena* arena = CURRENT_ENV->arena();
   188     _field_cache = new (arena) ciConstantPoolCache(arena, 5);
   189   }
   190   return _field_cache;
   191 }
   193 // ------------------------------------------------------------------
   194 // ciInstanceKlass::get_canonical_holder
   195 //
   196 ciInstanceKlass* ciInstanceKlass::get_canonical_holder(int offset) {
   197   #ifdef ASSERT
   198   if (!(offset >= 0 && offset < layout_helper())) {
   199     tty->print("*** get_canonical_holder(%d) on ", offset);
   200     this->print();
   201     tty->print_cr(" ***");
   202   };
   203   assert(offset >= 0 && offset < layout_helper(), "offset must be tame");
   204   #endif
   206   if (offset < instanceOopDesc::base_offset_in_bytes()) {
   207     // All header offsets belong properly to java/lang/Object.
   208     return CURRENT_ENV->Object_klass();
   209   }
   211   ciInstanceKlass* self = this;
   212   for (;;) {
   213     assert(self->is_loaded(), "must be loaded to have size");
   214     ciInstanceKlass* super = self->super();
   215     if (super == NULL || super->nof_nonstatic_fields() == 0 ||
   216         !super->contains_field_offset(offset)) {
   217       return self;
   218     } else {
   219       self = super;  // return super->get_canonical_holder(offset)
   220     }
   221   }
   222 }
   224 // ------------------------------------------------------------------
   225 // ciInstanceKlass::is_java_lang_Object
   226 //
   227 // Is this klass java.lang.Object?
   228 bool ciInstanceKlass::is_java_lang_Object() {
   229   return equals(CURRENT_ENV->Object_klass());
   230 }
   232 // ------------------------------------------------------------------
   233 // ciInstanceKlass::uses_default_loader
   234 bool ciInstanceKlass::uses_default_loader() {
   235   // Note:  We do not need to resolve the handle or enter the VM
   236   // in order to test null-ness.
   237   return _loader == NULL;
   238 }
   240 // ------------------------------------------------------------------
   241 // ciInstanceKlass::is_in_package
   242 //
   243 // Is this klass in the given package?
   244 bool ciInstanceKlass::is_in_package(const char* packagename, int len) {
   245   // To avoid class loader mischief, this test always rejects application classes.
   246   if (!uses_default_loader())
   247     return false;
   248   GUARDED_VM_ENTRY(
   249     return is_in_package_impl(packagename, len);
   250   )
   251 }
   253 bool ciInstanceKlass::is_in_package_impl(const char* packagename, int len) {
   254   ASSERT_IN_VM;
   256   // If packagename contains trailing '/' exclude it from the
   257   // prefix-test since we test for it explicitly.
   258   if (packagename[len - 1] == '/')
   259     len--;
   261   if (!name()->starts_with(packagename, len))
   262     return false;
   264   // Test if the class name is something like "java/lang".
   265   if ((len + 1) > name()->utf8_length())
   266     return false;
   268   // Test for trailing '/'
   269   if ((char) name()->byte_at(len) != '/')
   270     return false;
   272   // Make sure it's not actually in a subpackage:
   273   if (name()->index_of_at(len+1, "/", 1) >= 0)
   274     return false;
   276   return true;
   277 }
   279 // ------------------------------------------------------------------
   280 // ciInstanceKlass::print_impl
   281 //
   282 // Implementation of the print method.
   283 void ciInstanceKlass::print_impl(outputStream* st) {
   284   ciKlass::print_impl(st);
   285   GUARDED_VM_ENTRY(st->print(" loader=0x%x", (address)loader());)
   286   if (is_loaded()) {
   287     st->print(" loaded=true initialized=%s finalized=%s subklass=%s size=%d flags=",
   288               bool_to_str(is_initialized()),
   289               bool_to_str(has_finalizer()),
   290               bool_to_str(has_subklass()),
   291               layout_helper());
   293     _flags.print_klass_flags();
   295     if (_super) {
   296       st->print(" super=");
   297       _super->print_name();
   298     }
   299     if (_java_mirror) {
   300       st->print(" mirror=PRESENT");
   301     }
   302   } else {
   303     st->print(" loaded=false");
   304   }
   305 }
   307 // ------------------------------------------------------------------
   308 // ciInstanceKlass::super
   309 //
   310 // Get the superklass of this klass.
   311 ciInstanceKlass* ciInstanceKlass::super() {
   312   assert(is_loaded(), "must be loaded");
   313   if (_super == NULL && !is_java_lang_Object()) {
   314     GUARDED_VM_ENTRY(
   315       klassOop super_klass = get_instanceKlass()->super();
   316       _super = CURRENT_ENV->get_object(super_klass)->as_instance_klass();
   317     )
   318   }
   319   return _super;
   320 }
   322 // ------------------------------------------------------------------
   323 // ciInstanceKlass::java_mirror
   324 //
   325 // Get the instance of java.lang.Class corresponding to this klass.
   326 // Cache it on this->_java_mirror.
   327 ciInstance* ciInstanceKlass::java_mirror() {
   328   if (_java_mirror == NULL) {
   329     _java_mirror = ciKlass::java_mirror();
   330   }
   331   return _java_mirror;
   332 }
   334 // ------------------------------------------------------------------
   335 // ciInstanceKlass::unique_concrete_subklass
   336 ciInstanceKlass* ciInstanceKlass::unique_concrete_subklass() {
   337   if (!is_loaded())     return NULL; // No change if class is not loaded
   338   if (!is_abstract())   return NULL; // Only applies to abstract classes.
   339   if (!has_subklass())  return NULL; // Must have at least one subklass.
   340   VM_ENTRY_MARK;
   341   instanceKlass* ik = get_instanceKlass();
   342   Klass* up = ik->up_cast_abstract();
   343   assert(up->oop_is_instance(), "must be instanceKlass");
   344   if (ik == up) {
   345     return NULL;
   346   }
   347   return CURRENT_THREAD_ENV->get_object(up->as_klassOop())->as_instance_klass();
   348 }
   350 // ------------------------------------------------------------------
   351 // ciInstanceKlass::has_finalizable_subclass
   352 bool ciInstanceKlass::has_finalizable_subclass() {
   353   if (!is_loaded())     return true;
   354   VM_ENTRY_MARK;
   355   return Dependencies::find_finalizable_subclass(get_instanceKlass()) != NULL;
   356 }
   358 // ------------------------------------------------------------------
   359 // ciInstanceKlass::get_field_by_offset
   360 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
   361   if (!is_static) {
   362     for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) {
   363       ciField* field = _nonstatic_fields->at(i);
   364       int  field_off = field->offset_in_bytes();
   365       if (field_off == field_offset)
   366         return field;
   367       if (field_off > field_offset)
   368         break;
   369       // could do binary search or check bins, but probably not worth it
   370     }
   371     return NULL;
   372   }
   373   VM_ENTRY_MARK;
   374   instanceKlass* k = get_instanceKlass();
   375   fieldDescriptor fd;
   376   if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
   377     return NULL;
   378   }
   379   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
   380   return field;
   381 }
   383 // ------------------------------------------------------------------
   384 // ciInstanceKlass::get_field_by_name
   385 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
   386   VM_ENTRY_MARK;
   387   instanceKlass* k = get_instanceKlass();
   388   fieldDescriptor fd;
   389   klassOop def = k->find_field(name->get_symbolOop(), signature->get_symbolOop(), is_static, &fd);
   390   if (def == NULL) {
   391     return NULL;
   392   }
   393   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
   394   return field;
   395 }
   397 // ------------------------------------------------------------------
   398 // ciInstanceKlass::non_static_fields.
   400 class NonStaticFieldFiller: public FieldClosure {
   401   GrowableArray<ciField*>* _arr;
   402   ciEnv* _curEnv;
   403 public:
   404   NonStaticFieldFiller(ciEnv* curEnv, GrowableArray<ciField*>* arr) :
   405     _curEnv(curEnv), _arr(arr)
   406   {}
   407   void do_field(fieldDescriptor* fd) {
   408     ciField* field = new (_curEnv->arena()) ciField(fd);
   409     _arr->append(field);
   410   }
   411 };
   413 GrowableArray<ciField*>* ciInstanceKlass::non_static_fields() {
   414   if (_non_static_fields == NULL) {
   415     VM_ENTRY_MARK;
   416     ciEnv* curEnv = ciEnv::current();
   417     instanceKlass* ik = get_instanceKlass();
   418     int max_n_fields = ik->fields()->length()/instanceKlass::next_offset;
   420     _non_static_fields =
   421       new (curEnv->arena()) GrowableArray<ciField*>(max_n_fields);
   422     NonStaticFieldFiller filler(curEnv, _non_static_fields);
   423     ik->do_nonstatic_fields(&filler);
   424   }
   425   return _non_static_fields;
   426 }
   428 static int sort_field_by_offset(ciField** a, ciField** b) {
   429   return (*a)->offset_in_bytes() - (*b)->offset_in_bytes();
   430   // (no worries about 32-bit overflow...)
   431 }
   433 // ------------------------------------------------------------------
   434 // ciInstanceKlass::compute_nonstatic_fields
   435 int ciInstanceKlass::compute_nonstatic_fields() {
   436   assert(is_loaded(), "must be loaded");
   438   if (_nonstatic_fields != NULL)
   439     return _nonstatic_fields->length();
   441   if (!has_nonstatic_fields()) {
   442     Arena* arena = CURRENT_ENV->arena();
   443     _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL);
   444     return 0;
   445   }
   446   assert(!is_java_lang_Object(), "bootstrap OK");
   448   // Size in bytes of my fields, including inherited fields.
   449   int fsize = nonstatic_field_size() * heapOopSize;
   451   ciInstanceKlass* super = this->super();
   452   GrowableArray<ciField*>* super_fields = NULL;
   453   if (super != NULL && super->has_nonstatic_fields()) {
   454     int super_fsize  = super->nonstatic_field_size() * heapOopSize;
   455     int super_flen   = super->nof_nonstatic_fields();
   456     super_fields = super->_nonstatic_fields;
   457     assert(super_flen == 0 || super_fields != NULL, "first get nof_fields");
   458     // See if I am no larger than my super; if so, I can use his fields.
   459     if (fsize == super_fsize) {
   460       _nonstatic_fields = super_fields;
   461       return super_fields->length();
   462     }
   463   }
   465   GrowableArray<ciField*>* fields = NULL;
   466   GUARDED_VM_ENTRY({
   467       fields = compute_nonstatic_fields_impl(super_fields);
   468     });
   470   if (fields == NULL) {
   471     // This can happen if this class (java.lang.Class) has invisible fields.
   472     _nonstatic_fields = super_fields;
   473     return super_fields->length();
   474   }
   476   int flen = fields->length();
   478   // Now sort them by offset, ascending.
   479   // (In principle, they could mix with superclass fields.)
   480   fields->sort(sort_field_by_offset);
   481 #ifdef ASSERT
   482   int last_offset = instanceOopDesc::base_offset_in_bytes();
   483   for (int i = 0; i < fields->length(); i++) {
   484     ciField* field = fields->at(i);
   485     int offset = field->offset_in_bytes();
   486     int size   = (field->_type == NULL) ? heapOopSize : field->size_in_bytes();
   487     assert(last_offset <= offset, "no field overlap");
   488     if (last_offset > (int)sizeof(oopDesc))
   489       assert((offset - last_offset) < BytesPerLong, "no big holes");
   490     // Note:  Two consecutive T_BYTE fields will be separated by wordSize-1
   491     // padding bytes if one of them is declared by a superclass.
   492     // This is a minor inefficiency classFileParser.cpp.
   493     last_offset = offset + size;
   494   }
   495   assert(last_offset <= (int)instanceOopDesc::base_offset_in_bytes() + fsize, "no overflow");
   496 #endif
   498   _nonstatic_fields = fields;
   499   return flen;
   500 }
   502 GrowableArray<ciField*>*
   503 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
   504                                                super_fields) {
   505   ASSERT_IN_VM;
   506   Arena* arena = CURRENT_ENV->arena();
   507   int flen = 0;
   508   GrowableArray<ciField*>* fields = NULL;
   509   instanceKlass* k = get_instanceKlass();
   510   typeArrayOop fields_array = k->fields();
   511   for (int pass = 0; pass <= 1; pass++) {
   512     for (int i = 0, alen = fields_array->length(); i < alen; i += instanceKlass::next_offset) {
   513       fieldDescriptor fd;
   514       fd.initialize(k->as_klassOop(), i);
   515       if (fd.is_static())  continue;
   516       if (pass == 0) {
   517         flen += 1;
   518       } else {
   519         ciField* field = new (arena) ciField(&fd);
   520         fields->append(field);
   521       }
   522     }
   524     // Between passes, allocate the array:
   525     if (pass == 0) {
   526       if (flen == 0) {
   527         return NULL;  // return nothing if none are locally declared
   528       }
   529       if (super_fields != NULL) {
   530         flen += super_fields->length();
   531       }
   532       fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, NULL);
   533       if (super_fields != NULL) {
   534         fields->appendAll(super_fields);
   535       }
   536     }
   537   }
   538   assert(fields->length() == flen, "sanity");
   539   return fields;
   540 }
   542 // ------------------------------------------------------------------
   543 // ciInstanceKlass::find_method
   544 //
   545 // Find a method in this klass.
   546 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) {
   547   VM_ENTRY_MARK;
   548   instanceKlass* k = get_instanceKlass();
   549   symbolOop name_sym = name->get_symbolOop();
   550   symbolOop sig_sym= signature->get_symbolOop();
   552   methodOop m = k->find_method(name_sym, sig_sym);
   553   if (m == NULL)  return NULL;
   555   return CURRENT_THREAD_ENV->get_object(m)->as_method();
   556 }
   558 // ------------------------------------------------------------------
   559 // ciInstanceKlass::is_leaf_type
   560 bool ciInstanceKlass::is_leaf_type() {
   561   assert(is_loaded(), "must be loaded");
   562   if (is_shared()) {
   563     return is_final();  // approximately correct
   564   } else {
   565     return !_has_subklass && (_nof_implementors == 0);
   566   }
   567 }
   569 // ------------------------------------------------------------------
   570 // ciInstanceKlass::implementor
   571 //
   572 // Report an implementor of this interface.
   573 // Returns NULL if exact information is not available.
   574 // Note that there are various races here, since my copy
   575 // of _nof_implementors might be out of date with respect
   576 // to results returned by instanceKlass::implementor.
   577 // This is OK, since any dependencies we decide to assert
   578 // will be checked later under the Compile_lock.
   579 ciInstanceKlass* ciInstanceKlass::implementor(int n) {
   580   if (n > implementors_limit) {
   581     return NULL;
   582   }
   583   ciInstanceKlass* impl = _implementors[n];
   584   if (impl == NULL) {
   585     if (_nof_implementors > implementors_limit) {
   586       return NULL;
   587     }
   588     // Go into the VM to fetch the implementor.
   589     {
   590       VM_ENTRY_MARK;
   591       klassOop k = get_instanceKlass()->implementor(n);
   592       if (k != NULL) {
   593         impl = CURRENT_THREAD_ENV->get_object(k)->as_instance_klass();
   594       }
   595     }
   596     // Memoize this result.
   597     if (!is_shared()) {
   598       _implementors[n] = (impl == NULL)? this: impl;
   599     }
   600   } else if (impl == this) {
   601     impl = NULL;  // memoized null result from a VM query
   602   }
   603   return impl;
   604 }

mercurial