src/share/vm/ci/ciInstanceKlass.cpp

Mon, 12 Jul 2010 10:58:25 -0700

author
never
date
Mon, 12 Jul 2010 10:58:25 -0700
changeset 2000
3941674cc7fa
parent 1959
b918d354830a
child 2040
0e35fa8ebccd
permissions
-rw-r--r--

6958668: repeated uncommon trapping for new of klass which is being initialized
Reviewed-by: kvn, jrose

     1 /*
     2  * Copyright (c) 1999, 2010, 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   _init_state = (instanceKlass::ClassState)ik->get_init_state();
    48   _nonstatic_field_size = ik->nonstatic_field_size();
    49   _has_nonstatic_fields = ik->has_nonstatic_fields();
    50   _nonstatic_fields = NULL; // initialized lazily by compute_nonstatic_fields:
    52   _nof_implementors = ik->nof_implementors();
    53   for (int i = 0; i < implementors_limit; i++) {
    54     _implementors[i] = NULL;  // we will fill these lazily
    55   }
    57   Thread *thread = Thread::current();
    58   if (ciObjectFactory::is_initialized()) {
    59     _loader = JNIHandles::make_local(thread, ik->class_loader());
    60     _protection_domain = JNIHandles::make_local(thread,
    61                                                 ik->protection_domain());
    62     _is_shared = false;
    63   } else {
    64     Handle h_loader(thread, ik->class_loader());
    65     Handle h_protection_domain(thread, ik->protection_domain());
    66     _loader = JNIHandles::make_global(h_loader);
    67     _protection_domain = JNIHandles::make_global(h_protection_domain);
    68     _is_shared = true;
    69   }
    71   // Lazy fields get filled in only upon request.
    72   _super  = NULL;
    73   _java_mirror = NULL;
    75   if (is_shared()) {
    76     if (h_k() != SystemDictionary::Object_klass()) {
    77       super();
    78     }
    79     java_mirror();
    80     //compute_nonstatic_fields();  // done outside of constructor
    81   }
    83   _field_cache = NULL;
    84 }
    86 // Version for unloaded classes:
    87 ciInstanceKlass::ciInstanceKlass(ciSymbol* name,
    88                                  jobject loader, jobject protection_domain)
    89   : ciKlass(name, ciInstanceKlassKlass::make())
    90 {
    91   assert(name->byte_at(0) != '[', "not an instance klass");
    92   _init_state = (instanceKlass::ClassState)0;
    93   _nonstatic_field_size = -1;
    94   _has_nonstatic_fields = false;
    95   _nonstatic_fields = NULL;
    96   _nof_implementors = -1;
    97   _loader = loader;
    98   _protection_domain = protection_domain;
    99   _is_shared = false;
   100   _super = NULL;
   101   _java_mirror = NULL;
   102   _field_cache = NULL;
   103 }
   107 // ------------------------------------------------------------------
   108 // ciInstanceKlass::compute_shared_is_initialized
   109 void ciInstanceKlass::compute_shared_init_state() {
   110   GUARDED_VM_ENTRY(
   111     instanceKlass* ik = get_instanceKlass();
   112     _init_state = (instanceKlass::ClassState)ik->get_init_state();
   113   )
   114 }
   116 // ------------------------------------------------------------------
   117 // ciInstanceKlass::compute_shared_has_subklass
   118 bool ciInstanceKlass::compute_shared_has_subklass() {
   119   GUARDED_VM_ENTRY(
   120     instanceKlass* ik = get_instanceKlass();
   121     _has_subklass = ik->subklass() != NULL;
   122     return _has_subklass;
   123   )
   124 }
   126 // ------------------------------------------------------------------
   127 // ciInstanceKlass::compute_shared_nof_implementors
   128 int ciInstanceKlass::compute_shared_nof_implementors() {
   129   // We requery this property, since it is a very old ciObject.
   130   GUARDED_VM_ENTRY(
   131     instanceKlass* ik = get_instanceKlass();
   132     _nof_implementors = ik->nof_implementors();
   133     return _nof_implementors;
   134   )
   135 }
   137 // ------------------------------------------------------------------
   138 // ciInstanceKlass::loader
   139 oop ciInstanceKlass::loader() {
   140   ASSERT_IN_VM;
   141   return JNIHandles::resolve(_loader);
   142 }
   144 // ------------------------------------------------------------------
   145 // ciInstanceKlass::loader_handle
   146 jobject ciInstanceKlass::loader_handle() {
   147   return _loader;
   148 }
   150 // ------------------------------------------------------------------
   151 // ciInstanceKlass::protection_domain
   152 oop ciInstanceKlass::protection_domain() {
   153   ASSERT_IN_VM;
   154   return JNIHandles::resolve(_protection_domain);
   155 }
   157 // ------------------------------------------------------------------
   158 // ciInstanceKlass::protection_domain_handle
   159 jobject ciInstanceKlass::protection_domain_handle() {
   160   return _protection_domain;
   161 }
   163 // ------------------------------------------------------------------
   164 // ciInstanceKlass::field_cache
   165 //
   166 // Get the field cache associated with this klass.
   167 ciConstantPoolCache* ciInstanceKlass::field_cache() {
   168   if (is_shared()) {
   169     return NULL;
   170   }
   171   if (_field_cache == NULL) {
   172     assert(!is_java_lang_Object(), "Object has no fields");
   173     Arena* arena = CURRENT_ENV->arena();
   174     _field_cache = new (arena) ciConstantPoolCache(arena, 5);
   175   }
   176   return _field_cache;
   177 }
   179 // ------------------------------------------------------------------
   180 // ciInstanceKlass::get_canonical_holder
   181 //
   182 ciInstanceKlass* ciInstanceKlass::get_canonical_holder(int offset) {
   183   #ifdef ASSERT
   184   if (!(offset >= 0 && offset < layout_helper())) {
   185     tty->print("*** get_canonical_holder(%d) on ", offset);
   186     this->print();
   187     tty->print_cr(" ***");
   188   };
   189   assert(offset >= 0 && offset < layout_helper(), "offset must be tame");
   190   #endif
   192   if (offset < instanceOopDesc::base_offset_in_bytes()) {
   193     // All header offsets belong properly to java/lang/Object.
   194     return CURRENT_ENV->Object_klass();
   195   }
   197   ciInstanceKlass* self = this;
   198   for (;;) {
   199     assert(self->is_loaded(), "must be loaded to have size");
   200     ciInstanceKlass* super = self->super();
   201     if (super == NULL || super->nof_nonstatic_fields() == 0 ||
   202         !super->contains_field_offset(offset)) {
   203       return self;
   204     } else {
   205       self = super;  // return super->get_canonical_holder(offset)
   206     }
   207   }
   208 }
   210 // ------------------------------------------------------------------
   211 // ciInstanceKlass::is_java_lang_Object
   212 //
   213 // Is this klass java.lang.Object?
   214 bool ciInstanceKlass::is_java_lang_Object() {
   215   return equals(CURRENT_ENV->Object_klass());
   216 }
   218 // ------------------------------------------------------------------
   219 // ciInstanceKlass::uses_default_loader
   220 bool ciInstanceKlass::uses_default_loader() {
   221   // Note:  We do not need to resolve the handle or enter the VM
   222   // in order to test null-ness.
   223   return _loader == NULL;
   224 }
   226 // ------------------------------------------------------------------
   227 // ciInstanceKlass::is_in_package
   228 //
   229 // Is this klass in the given package?
   230 bool ciInstanceKlass::is_in_package(const char* packagename, int len) {
   231   // To avoid class loader mischief, this test always rejects application classes.
   232   if (!uses_default_loader())
   233     return false;
   234   GUARDED_VM_ENTRY(
   235     return is_in_package_impl(packagename, len);
   236   )
   237 }
   239 bool ciInstanceKlass::is_in_package_impl(const char* packagename, int len) {
   240   ASSERT_IN_VM;
   242   // If packagename contains trailing '/' exclude it from the
   243   // prefix-test since we test for it explicitly.
   244   if (packagename[len - 1] == '/')
   245     len--;
   247   if (!name()->starts_with(packagename, len))
   248     return false;
   250   // Test if the class name is something like "java/lang".
   251   if ((len + 1) > name()->utf8_length())
   252     return false;
   254   // Test for trailing '/'
   255   if ((char) name()->byte_at(len) != '/')
   256     return false;
   258   // Make sure it's not actually in a subpackage:
   259   if (name()->index_of_at(len+1, "/", 1) >= 0)
   260     return false;
   262   return true;
   263 }
   265 // ------------------------------------------------------------------
   266 // ciInstanceKlass::print_impl
   267 //
   268 // Implementation of the print method.
   269 void ciInstanceKlass::print_impl(outputStream* st) {
   270   ciKlass::print_impl(st);
   271   GUARDED_VM_ENTRY(st->print(" loader=0x%x", (address)loader());)
   272   if (is_loaded()) {
   273     st->print(" loaded=true initialized=%s finalized=%s subklass=%s size=%d flags=",
   274               bool_to_str(is_initialized()),
   275               bool_to_str(has_finalizer()),
   276               bool_to_str(has_subklass()),
   277               layout_helper());
   279     _flags.print_klass_flags();
   281     if (_super) {
   282       st->print(" super=");
   283       _super->print_name();
   284     }
   285     if (_java_mirror) {
   286       st->print(" mirror=PRESENT");
   287     }
   288   } else {
   289     st->print(" loaded=false");
   290   }
   291 }
   293 // ------------------------------------------------------------------
   294 // ciInstanceKlass::super
   295 //
   296 // Get the superklass of this klass.
   297 ciInstanceKlass* ciInstanceKlass::super() {
   298   assert(is_loaded(), "must be loaded");
   299   if (_super == NULL && !is_java_lang_Object()) {
   300     GUARDED_VM_ENTRY(
   301       klassOop super_klass = get_instanceKlass()->super();
   302       _super = CURRENT_ENV->get_object(super_klass)->as_instance_klass();
   303     )
   304   }
   305   return _super;
   306 }
   308 // ------------------------------------------------------------------
   309 // ciInstanceKlass::java_mirror
   310 //
   311 // Get the instance of java.lang.Class corresponding to this klass.
   312 // Cache it on this->_java_mirror.
   313 ciInstance* ciInstanceKlass::java_mirror() {
   314   if (_java_mirror == NULL) {
   315     _java_mirror = ciKlass::java_mirror();
   316   }
   317   return _java_mirror;
   318 }
   320 // ------------------------------------------------------------------
   321 // ciInstanceKlass::unique_concrete_subklass
   322 ciInstanceKlass* ciInstanceKlass::unique_concrete_subklass() {
   323   if (!is_loaded())     return NULL; // No change if class is not loaded
   324   if (!is_abstract())   return NULL; // Only applies to abstract classes.
   325   if (!has_subklass())  return NULL; // Must have at least one subklass.
   326   VM_ENTRY_MARK;
   327   instanceKlass* ik = get_instanceKlass();
   328   Klass* up = ik->up_cast_abstract();
   329   assert(up->oop_is_instance(), "must be instanceKlass");
   330   if (ik == up) {
   331     return NULL;
   332   }
   333   return CURRENT_THREAD_ENV->get_object(up->as_klassOop())->as_instance_klass();
   334 }
   336 // ------------------------------------------------------------------
   337 // ciInstanceKlass::has_finalizable_subclass
   338 bool ciInstanceKlass::has_finalizable_subclass() {
   339   if (!is_loaded())     return true;
   340   VM_ENTRY_MARK;
   341   return Dependencies::find_finalizable_subclass(get_instanceKlass()) != NULL;
   342 }
   344 // ------------------------------------------------------------------
   345 // ciInstanceKlass::get_field_by_offset
   346 ciField* ciInstanceKlass::get_field_by_offset(int field_offset, bool is_static) {
   347   if (!is_static) {
   348     for (int i = 0, len = nof_nonstatic_fields(); i < len; i++) {
   349       ciField* field = _nonstatic_fields->at(i);
   350       int  field_off = field->offset_in_bytes();
   351       if (field_off == field_offset)
   352         return field;
   353       if (field_off > field_offset)
   354         break;
   355       // could do binary search or check bins, but probably not worth it
   356     }
   357     return NULL;
   358   }
   359   VM_ENTRY_MARK;
   360   instanceKlass* k = get_instanceKlass();
   361   fieldDescriptor fd;
   362   if (!k->find_field_from_offset(field_offset, is_static, &fd)) {
   363     return NULL;
   364   }
   365   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
   366   return field;
   367 }
   369 // ------------------------------------------------------------------
   370 // ciInstanceKlass::get_field_by_name
   371 ciField* ciInstanceKlass::get_field_by_name(ciSymbol* name, ciSymbol* signature, bool is_static) {
   372   VM_ENTRY_MARK;
   373   instanceKlass* k = get_instanceKlass();
   374   fieldDescriptor fd;
   375   klassOop def = k->find_field(name->get_symbolOop(), signature->get_symbolOop(), is_static, &fd);
   376   if (def == NULL) {
   377     return NULL;
   378   }
   379   ciField* field = new (CURRENT_THREAD_ENV->arena()) ciField(&fd);
   380   return field;
   381 }
   383 // ------------------------------------------------------------------
   384 // ciInstanceKlass::non_static_fields.
   386 class NonStaticFieldFiller: public FieldClosure {
   387   GrowableArray<ciField*>* _arr;
   388   ciEnv* _curEnv;
   389 public:
   390   NonStaticFieldFiller(ciEnv* curEnv, GrowableArray<ciField*>* arr) :
   391     _curEnv(curEnv), _arr(arr)
   392   {}
   393   void do_field(fieldDescriptor* fd) {
   394     ciField* field = new (_curEnv->arena()) ciField(fd);
   395     _arr->append(field);
   396   }
   397 };
   399 GrowableArray<ciField*>* ciInstanceKlass::non_static_fields() {
   400   if (_non_static_fields == NULL) {
   401     VM_ENTRY_MARK;
   402     ciEnv* curEnv = ciEnv::current();
   403     instanceKlass* ik = get_instanceKlass();
   404     int max_n_fields = ik->fields()->length()/instanceKlass::next_offset;
   406     _non_static_fields =
   407       new (curEnv->arena()) GrowableArray<ciField*>(max_n_fields);
   408     NonStaticFieldFiller filler(curEnv, _non_static_fields);
   409     ik->do_nonstatic_fields(&filler);
   410   }
   411   return _non_static_fields;
   412 }
   414 static int sort_field_by_offset(ciField** a, ciField** b) {
   415   return (*a)->offset_in_bytes() - (*b)->offset_in_bytes();
   416   // (no worries about 32-bit overflow...)
   417 }
   419 // ------------------------------------------------------------------
   420 // ciInstanceKlass::compute_nonstatic_fields
   421 int ciInstanceKlass::compute_nonstatic_fields() {
   422   assert(is_loaded(), "must be loaded");
   424   if (_nonstatic_fields != NULL)
   425     return _nonstatic_fields->length();
   427   if (!has_nonstatic_fields()) {
   428     Arena* arena = CURRENT_ENV->arena();
   429     _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL);
   430     return 0;
   431   }
   432   assert(!is_java_lang_Object(), "bootstrap OK");
   434   // Size in bytes of my fields, including inherited fields.
   435   int fsize = nonstatic_field_size() * heapOopSize;
   437   ciInstanceKlass* super = this->super();
   438   GrowableArray<ciField*>* super_fields = NULL;
   439   if (super != NULL && super->has_nonstatic_fields()) {
   440     int super_fsize  = super->nonstatic_field_size() * heapOopSize;
   441     int super_flen   = super->nof_nonstatic_fields();
   442     super_fields = super->_nonstatic_fields;
   443     assert(super_flen == 0 || super_fields != NULL, "first get nof_fields");
   444     // See if I am no larger than my super; if so, I can use his fields.
   445     if (fsize == super_fsize) {
   446       _nonstatic_fields = super_fields;
   447       return super_fields->length();
   448     }
   449   }
   451   GrowableArray<ciField*>* fields = NULL;
   452   GUARDED_VM_ENTRY({
   453       fields = compute_nonstatic_fields_impl(super_fields);
   454     });
   456   if (fields == NULL) {
   457     // This can happen if this class (java.lang.Class) has invisible fields.
   458     _nonstatic_fields = super_fields;
   459     return super_fields->length();
   460   }
   462   int flen = fields->length();
   464   // Now sort them by offset, ascending.
   465   // (In principle, they could mix with superclass fields.)
   466   fields->sort(sort_field_by_offset);
   467 #ifdef ASSERT
   468   int last_offset = instanceOopDesc::base_offset_in_bytes();
   469   for (int i = 0; i < fields->length(); i++) {
   470     ciField* field = fields->at(i);
   471     int offset = field->offset_in_bytes();
   472     int size   = (field->_type == NULL) ? heapOopSize : field->size_in_bytes();
   473     assert(last_offset <= offset, "no field overlap");
   474     if (last_offset > (int)sizeof(oopDesc))
   475       assert((offset - last_offset) < BytesPerLong, "no big holes");
   476     // Note:  Two consecutive T_BYTE fields will be separated by wordSize-1
   477     // padding bytes if one of them is declared by a superclass.
   478     // This is a minor inefficiency classFileParser.cpp.
   479     last_offset = offset + size;
   480   }
   481   assert(last_offset <= (int)instanceOopDesc::base_offset_in_bytes() + fsize, "no overflow");
   482 #endif
   484   _nonstatic_fields = fields;
   485   return flen;
   486 }
   488 GrowableArray<ciField*>*
   489 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
   490                                                super_fields) {
   491   ASSERT_IN_VM;
   492   Arena* arena = CURRENT_ENV->arena();
   493   int flen = 0;
   494   GrowableArray<ciField*>* fields = NULL;
   495   instanceKlass* k = get_instanceKlass();
   496   typeArrayOop fields_array = k->fields();
   497   for (int pass = 0; pass <= 1; pass++) {
   498     for (int i = 0, alen = fields_array->length(); i < alen; i += instanceKlass::next_offset) {
   499       fieldDescriptor fd;
   500       fd.initialize(k->as_klassOop(), i);
   501       if (fd.is_static())  continue;
   502       if (pass == 0) {
   503         flen += 1;
   504       } else {
   505         ciField* field = new (arena) ciField(&fd);
   506         fields->append(field);
   507       }
   508     }
   510     // Between passes, allocate the array:
   511     if (pass == 0) {
   512       if (flen == 0) {
   513         return NULL;  // return nothing if none are locally declared
   514       }
   515       if (super_fields != NULL) {
   516         flen += super_fields->length();
   517       }
   518       fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, NULL);
   519       if (super_fields != NULL) {
   520         fields->appendAll(super_fields);
   521       }
   522     }
   523   }
   524   assert(fields->length() == flen, "sanity");
   525   return fields;
   526 }
   528 // ------------------------------------------------------------------
   529 // ciInstanceKlass::find_method
   530 //
   531 // Find a method in this klass.
   532 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) {
   533   VM_ENTRY_MARK;
   534   instanceKlass* k = get_instanceKlass();
   535   symbolOop name_sym = name->get_symbolOop();
   536   symbolOop sig_sym= signature->get_symbolOop();
   538   methodOop m = k->find_method(name_sym, sig_sym);
   539   if (m == NULL)  return NULL;
   541   return CURRENT_THREAD_ENV->get_object(m)->as_method();
   542 }
   544 // ------------------------------------------------------------------
   545 // ciInstanceKlass::is_leaf_type
   546 bool ciInstanceKlass::is_leaf_type() {
   547   assert(is_loaded(), "must be loaded");
   548   if (is_shared()) {
   549     return is_final();  // approximately correct
   550   } else {
   551     return !_has_subklass && (_nof_implementors == 0);
   552   }
   553 }
   555 // ------------------------------------------------------------------
   556 // ciInstanceKlass::implementor
   557 //
   558 // Report an implementor of this interface.
   559 // Returns NULL if exact information is not available.
   560 // Note that there are various races here, since my copy
   561 // of _nof_implementors might be out of date with respect
   562 // to results returned by instanceKlass::implementor.
   563 // This is OK, since any dependencies we decide to assert
   564 // will be checked later under the Compile_lock.
   565 ciInstanceKlass* ciInstanceKlass::implementor(int n) {
   566   if (n > implementors_limit) {
   567     return NULL;
   568   }
   569   ciInstanceKlass* impl = _implementors[n];
   570   if (impl == NULL) {
   571     if (_nof_implementors > implementors_limit) {
   572       return NULL;
   573     }
   574     // Go into the VM to fetch the implementor.
   575     {
   576       VM_ENTRY_MARK;
   577       klassOop k = get_instanceKlass()->implementor(n);
   578       if (k != NULL) {
   579         impl = CURRENT_THREAD_ENV->get_object(k)->as_instance_klass();
   580       }
   581     }
   582     // Memoize this result.
   583     if (!is_shared()) {
   584       _implementors[n] = (impl == NULL)? this: impl;
   585     }
   586   } else if (impl == this) {
   587     impl = NULL;  // memoized null result from a VM query
   588   }
   589   return impl;
   590 }

mercurial