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