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