src/share/vm/ci/ciInstanceKlass.cpp

Tue, 09 Nov 2010 17:31:18 -0800

author
kvn
date
Tue, 09 Nov 2010 17:31:18 -0800
changeset 2279
b0e6879e48fa
parent 2201
d55217dc206f
child 2314
f95d63e2154a
permissions
-rw-r--r--

6839891: Array overrun in vm ci
Summary: fix index check
Reviewed-by: never

     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     Arena* arena = curEnv->arena();
   407     _non_static_fields =
   408       new (arena) GrowableArray<ciField*>(arena, max_n_fields, 0, NULL);
   409     NonStaticFieldFiller filler(curEnv, _non_static_fields);
   410     ik->do_nonstatic_fields(&filler);
   411   }
   412   return _non_static_fields;
   413 }
   415 static int sort_field_by_offset(ciField** a, ciField** b) {
   416   return (*a)->offset_in_bytes() - (*b)->offset_in_bytes();
   417   // (no worries about 32-bit overflow...)
   418 }
   420 // ------------------------------------------------------------------
   421 // ciInstanceKlass::compute_nonstatic_fields
   422 int ciInstanceKlass::compute_nonstatic_fields() {
   423   assert(is_loaded(), "must be loaded");
   425   if (_nonstatic_fields != NULL)
   426     return _nonstatic_fields->length();
   428   if (!has_nonstatic_fields()) {
   429     Arena* arena = CURRENT_ENV->arena();
   430     _nonstatic_fields = new (arena) GrowableArray<ciField*>(arena, 0, 0, NULL);
   431     return 0;
   432   }
   433   assert(!is_java_lang_Object(), "bootstrap OK");
   435   // Size in bytes of my fields, including inherited fields.
   436   int fsize = nonstatic_field_size() * heapOopSize;
   438   ciInstanceKlass* super = this->super();
   439   GrowableArray<ciField*>* super_fields = NULL;
   440   if (super != NULL && super->has_nonstatic_fields()) {
   441     int super_fsize  = super->nonstatic_field_size() * heapOopSize;
   442     int super_flen   = super->nof_nonstatic_fields();
   443     super_fields = super->_nonstatic_fields;
   444     assert(super_flen == 0 || super_fields != NULL, "first get nof_fields");
   445     // See if I am no larger than my super; if so, I can use his fields.
   446     if (fsize == super_fsize) {
   447       _nonstatic_fields = super_fields;
   448       return super_fields->length();
   449     }
   450   }
   452   GrowableArray<ciField*>* fields = NULL;
   453   GUARDED_VM_ENTRY({
   454       fields = compute_nonstatic_fields_impl(super_fields);
   455     });
   457   if (fields == NULL) {
   458     // This can happen if this class (java.lang.Class) has invisible fields.
   459     _nonstatic_fields = super_fields;
   460     return super_fields->length();
   461   }
   463   int flen = fields->length();
   465   // Now sort them by offset, ascending.
   466   // (In principle, they could mix with superclass fields.)
   467   fields->sort(sort_field_by_offset);
   468 #ifdef ASSERT
   469   int last_offset = instanceOopDesc::base_offset_in_bytes();
   470   for (int i = 0; i < fields->length(); i++) {
   471     ciField* field = fields->at(i);
   472     int offset = field->offset_in_bytes();
   473     int size   = (field->_type == NULL) ? heapOopSize : field->size_in_bytes();
   474     assert(last_offset <= offset, err_msg("no field overlap: %d <= %d", last_offset, offset));
   475     if (last_offset > (int)sizeof(oopDesc))
   476       assert((offset - last_offset) < BytesPerLong, "no big holes");
   477     // Note:  Two consecutive T_BYTE fields will be separated by wordSize-1
   478     // padding bytes if one of them is declared by a superclass.
   479     // This is a minor inefficiency classFileParser.cpp.
   480     last_offset = offset + size;
   481   }
   482   assert(last_offset <= (int)instanceOopDesc::base_offset_in_bytes() + fsize, "no overflow");
   483 #endif
   485   _nonstatic_fields = fields;
   486   return flen;
   487 }
   489 GrowableArray<ciField*>*
   490 ciInstanceKlass::compute_nonstatic_fields_impl(GrowableArray<ciField*>*
   491                                                super_fields) {
   492   ASSERT_IN_VM;
   493   Arena* arena = CURRENT_ENV->arena();
   494   int flen = 0;
   495   GrowableArray<ciField*>* fields = NULL;
   496   instanceKlass* k = get_instanceKlass();
   497   typeArrayOop fields_array = k->fields();
   498   for (int pass = 0; pass <= 1; pass++) {
   499     for (int i = 0, alen = fields_array->length(); i < alen; i += instanceKlass::next_offset) {
   500       fieldDescriptor fd;
   501       fd.initialize(k->as_klassOop(), i);
   502       if (fd.is_static())  continue;
   503       if (pass == 0) {
   504         flen += 1;
   505       } else {
   506         ciField* field = new (arena) ciField(&fd);
   507         fields->append(field);
   508       }
   509     }
   511     // Between passes, allocate the array:
   512     if (pass == 0) {
   513       if (flen == 0) {
   514         return NULL;  // return nothing if none are locally declared
   515       }
   516       if (super_fields != NULL) {
   517         flen += super_fields->length();
   518       }
   519       fields = new (arena) GrowableArray<ciField*>(arena, flen, 0, NULL);
   520       if (super_fields != NULL) {
   521         fields->appendAll(super_fields);
   522       }
   523     }
   524   }
   525   assert(fields->length() == flen, "sanity");
   526   return fields;
   527 }
   529 // ------------------------------------------------------------------
   530 // ciInstanceKlass::find_method
   531 //
   532 // Find a method in this klass.
   533 ciMethod* ciInstanceKlass::find_method(ciSymbol* name, ciSymbol* signature) {
   534   VM_ENTRY_MARK;
   535   instanceKlass* k = get_instanceKlass();
   536   symbolOop name_sym = name->get_symbolOop();
   537   symbolOop sig_sym= signature->get_symbolOop();
   539   methodOop m = k->find_method(name_sym, sig_sym);
   540   if (m == NULL)  return NULL;
   542   return CURRENT_THREAD_ENV->get_object(m)->as_method();
   543 }
   545 // ------------------------------------------------------------------
   546 // ciInstanceKlass::is_leaf_type
   547 bool ciInstanceKlass::is_leaf_type() {
   548   assert(is_loaded(), "must be loaded");
   549   if (is_shared()) {
   550     return is_final();  // approximately correct
   551   } else {
   552     return !_has_subklass && (_nof_implementors == 0);
   553   }
   554 }
   556 // ------------------------------------------------------------------
   557 // ciInstanceKlass::implementor
   558 //
   559 // Report an implementor of this interface.
   560 // Returns NULL if exact information is not available.
   561 // Note that there are various races here, since my copy
   562 // of _nof_implementors might be out of date with respect
   563 // to results returned by instanceKlass::implementor.
   564 // This is OK, since any dependencies we decide to assert
   565 // will be checked later under the Compile_lock.
   566 ciInstanceKlass* ciInstanceKlass::implementor(int n) {
   567   if (n >= implementors_limit) {
   568     return NULL;
   569   }
   570   ciInstanceKlass* impl = _implementors[n];
   571   if (impl == NULL) {
   572     if (_nof_implementors > implementors_limit) {
   573       return NULL;
   574     }
   575     // Go into the VM to fetch the implementor.
   576     {
   577       VM_ENTRY_MARK;
   578       klassOop k = get_instanceKlass()->implementor(n);
   579       if (k != NULL) {
   580         impl = CURRENT_THREAD_ENV->get_object(k)->as_instance_klass();
   581       }
   582     }
   583     // Memoize this result.
   584     if (!is_shared()) {
   585       _implementors[n] = (impl == NULL)? this: impl;
   586     }
   587   } else if (impl == this) {
   588     impl = NULL;  // memoized null result from a VM query
   589   }
   590   return impl;
   591 }

mercurial