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