1 /*
   2  * Copyright (c) 1997, 2016, 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  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/javaClasses.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "gc/shared/collectedHeap.inline.hpp"
  30 #include "gc/shared/gcLocker.hpp"
  31 #include "jvmtifiles/jvmti.h"
  32 #include "memory/universe.inline.hpp"
  33 #include "oops/arrayKlass.hpp"
  34 #include "oops/arrayOop.hpp"
  35 #include "oops/instanceKlass.hpp"
  36 #include "oops/objArrayOop.hpp"
  37 #include "oops/oop.inline.hpp"
  38 
  39 int ArrayKlass::static_size(int header_size) {
  40   // size of an array klass object
  41   assert(header_size <= InstanceKlass::header_size(), "bad header size");
  42   // If this assert fails, see comments in base_create_array_klass.
  43   header_size = InstanceKlass::header_size();
  44   int vtable_len = Universe::base_vtable_size();
  45   int size = header_size + vtable_len;
  46   return align_metadata_size(size);
  47 }
  48 
  49 
  50 Klass* ArrayKlass::java_super() const {
  51   if (super() == NULL)  return NULL;  // bootstrap case
  52   // Array klasses have primary supertypes which are not reported to Java.
  53   // Example super chain:  String[][] -> Object[][] -> Object[] -> Object
  54   return SystemDictionary::Object_klass();
  55 }
  56 
  57 
  58 oop ArrayKlass::multi_allocate(int rank, jint* sizes, TRAPS) {
  59   ShouldNotReachHere();
  60   return NULL;
  61 }
  62 
  63 // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
  64 Klass* ArrayKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
  65   // There are no fields in an array klass but look to the super class (Object)
  66   assert(super(), "super klass must be present");
  67   return super()->find_field(name, sig, fd);
  68 }
  69 
  70 Method* ArrayKlass::uncached_lookup_method(const Symbol* name,
  71                                            const Symbol* signature,
  72                                            OverpassLookupMode overpass_mode) const {
  73   // There are no methods in an array klass but the super class (Object) has some
  74   assert(super(), "super klass must be present");
  75   // Always ignore overpass methods in superclasses, although technically the
  76   // super klass of an array, (j.l.Object) should not have
  77   // any overpass methods present.
  78   return super()->uncached_lookup_method(name, signature, Klass::skip_overpass);
  79 }
  80 
  81 ArrayKlass::ArrayKlass(Symbol* name) :
  82   _dimension(1),
  83   _higher_dimension(NULL),
  84   _lower_dimension(NULL) {
  85     // Arrays don't add any new methods, so their vtable is the same size as
  86     // the vtable of klass Object.
  87     set_vtable_length(Universe::base_vtable_size());
  88     set_name(name);
  89     set_super(Universe::is_bootstrapping() ? (Klass*)NULL : SystemDictionary::Object_klass());
  90     set_layout_helper(Klass::_lh_neutral_value);
  91     set_is_cloneable(); // All arrays are considered to be cloneable (See JLS 20.1.5)
  92     TRACE_INIT_KLASS_ID(this);
  93 }
  94 
  95 
  96 // Initialization of vtables and mirror object is done separatly from base_create_array_klass,
  97 // since a GC can happen. At this point all instance variables of the ArrayKlass must be setup.
  98 void ArrayKlass::complete_create_array_klass(ArrayKlass* k, KlassHandle super_klass, TRAPS) {
  99   ResourceMark rm(THREAD);
 100   k->initialize_supers(super_klass(), CHECK);
 101   k->vtable()->initialize_vtable(false, CHECK);
 102   java_lang_Class::create_mirror(k, Handle(THREAD, k->class_loader()), Handle(NULL), CHECK);
 103 }
 104 
 105 GrowableArray<Klass*>* ArrayKlass::compute_secondary_supers(int num_extra_slots) {
 106   // interfaces = { cloneable_klass, serializable_klass };
 107   assert(num_extra_slots == 0, "sanity of primitive array type");
 108   // Must share this for correct bootstrapping!
 109   set_secondary_supers(Universe::the_array_interfaces_array());
 110   return NULL;
 111 }
 112 
 113 bool ArrayKlass::compute_is_subtype_of(Klass* k) {
 114   // An array is a subtype of Serializable, Clonable, and Object
 115   return    k == SystemDictionary::Object_klass()
 116          || k == SystemDictionary::Cloneable_klass()
 117          || k == SystemDictionary::Serializable_klass();
 118 }
 119 
 120 objArrayOop ArrayKlass::allocate_arrayArray(int n, int length, TRAPS) {
 121   if (length < 0) {
 122     THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
 123   }
 124   if (length > arrayOopDesc::max_array_length(T_ARRAY)) {
 125     report_java_out_of_memory("Requested array size exceeds VM limit");
 126     JvmtiExport::post_array_size_exhausted();
 127     THROW_OOP_0(Universe::out_of_memory_error_array_size());
 128   }
 129   int size = objArrayOopDesc::object_size(length);
 130   Klass* k = array_klass(n+dimension(), CHECK_0);
 131   ArrayKlass* ak = ArrayKlass::cast(k);
 132   objArrayOop o =
 133     (objArrayOop)CollectedHeap::array_allocate(ak, size, length, CHECK_0);
 134   // initialization to NULL not necessary, area already cleared
 135   return o;
 136 }
 137 
 138 void ArrayKlass::array_klasses_do(void f(Klass* k, TRAPS), TRAPS) {
 139   Klass* k = this;
 140   // Iterate over this array klass and all higher dimensions
 141   while (k != NULL) {
 142     f(k, CHECK);
 143     k = ArrayKlass::cast(k)->higher_dimension();
 144   }
 145 }
 146 
 147 void ArrayKlass::array_klasses_do(void f(Klass* k)) {
 148   Klass* k = this;
 149   // Iterate over this array klass and all higher dimensions
 150   while (k != NULL) {
 151     f(k);
 152     k = ArrayKlass::cast(k)->higher_dimension();
 153   }
 154 }
 155 
 156 // JVM support
 157 
 158 jint ArrayKlass::compute_modifier_flags(TRAPS) const {
 159   return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
 160 }
 161 
 162 // JVMTI support
 163 
 164 jint ArrayKlass::jvmti_class_status() const {
 165   return JVMTI_CLASS_STATUS_ARRAY;
 166 }
 167 
 168 void ArrayKlass::remove_unshareable_info() {
 169   Klass::remove_unshareable_info();
 170 }
 171 
 172 void ArrayKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
 173   assert(loader_data == ClassLoaderData::the_null_class_loader_data(), "array classes belong to null loader");
 174   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
 175   // Klass recreates the component mirror also
 176 }
 177 
 178 // Printing
 179 
 180 void ArrayKlass::print_on(outputStream* st) const {
 181   assert(is_klass(), "must be klass");
 182   Klass::print_on(st);
 183 }
 184 
 185 void ArrayKlass::print_value_on(outputStream* st) const {
 186   assert(is_klass(), "must be klass");
 187   for(int index = 0; index < dimension(); index++) {
 188     st->print("[]");
 189   }
 190 }
 191 
 192 void ArrayKlass::oop_print_on(oop obj, outputStream* st) {
 193   assert(obj->is_array(), "must be array");
 194   Klass::oop_print_on(obj, st);
 195   st->print_cr(" - length: %d", arrayOop(obj)->length());
 196 }
 197 
 198 
 199 // Verification
 200 
 201 void ArrayKlass::verify_on(outputStream* st) {
 202   Klass::verify_on(st);
 203 }
 204 
 205 void ArrayKlass::oop_verify_on(oop obj, outputStream* st) {
 206   guarantee(obj->is_array(), "must be array");
 207   arrayOop a = arrayOop(obj);
 208   guarantee(a->length() >= 0, "array with negative length?");
 209 }