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 "cds/metaspaceShared.hpp"
 27 #include "classfile/javaClasses.hpp"
 28 #include "classfile/moduleEntry.hpp"
 29 #include "classfile/symbolTable.hpp"
 30 #include "classfile/vmClasses.hpp"
 31 #include "classfile/vmSymbols.hpp"
 32 #include "gc/shared/collectedHeap.inline.hpp"
 33 #include "jvmtifiles/jvmti.h"
 34 #include "memory/metaspaceClosure.hpp"
 35 #include "memory/resourceArea.hpp"
 36 #include "memory/universe.hpp"
 37 #include "oops/arrayKlass.hpp"
 38 #include "oops/objArrayKlass.hpp"
 39 #include "oops/arrayOop.hpp"
 40 #include "oops/instanceKlass.hpp"
 41 #include "oops/klass.inline.hpp"
 42 #include "oops/objArrayOop.hpp"
 43 #include "oops/oop.inline.hpp"
 44 #include "runtime/handles.inline.hpp"
 45 
 46 int ArrayKlass::static_size(int header_size) {
 47   // size of an array klass object
 48   assert(header_size <= InstanceKlass::header_size(), "bad header size");
 49   // If this assert fails, see comments in base_create_array_klass.
 50   header_size = InstanceKlass::header_size();
 51   int vtable_len = Universe::base_vtable_size();
 52   int size = header_size + vtable_len;
 53   return align_metadata_size(size);
 54 }
 55 
 56 
 57 InstanceKlass* ArrayKlass::java_super() const {
 58   if (super() == nullptr)  return nullptr;  // bootstrap case
 59   // Array klasses have primary supertypes which are not reported to Java.
 60   // Example super chain:  String[][] -> Object[][] -> Object[] -> Object
 61   return vmClasses::Object_klass();
 62 }
 63 
 64 
 65 oop ArrayKlass::multi_allocate(int rank, jint* sizes, TRAPS) {
 66   ShouldNotReachHere();
 67   return nullptr;
 68 }
 69 
 70 // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
 71 Klass* ArrayKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
 72   // There are no fields in an array klass but look to the super class (Object)
 73   assert(super(), "super klass must be present");
 74   return super()->find_field(name, sig, fd);
 75 }
 76 
 77 Method* ArrayKlass::uncached_lookup_method(const Symbol* name,
 78                                            const Symbol* signature,
 79                                            OverpassLookupMode overpass_mode,
 80                                            PrivateLookupMode private_mode) const {
 81   // There are no methods in an array klass but the super class (Object) has some
 82   assert(super(), "super klass must be present");
 83   // Always ignore overpass methods in superclasses, although technically the
 84   // super klass of an array, (j.l.Object) should not have
 85   // any overpass methods present.
 86   return super()->uncached_lookup_method(name, signature, OverpassLookupMode::skip, private_mode);
 87 }
 88 
 89 ArrayKlass::ArrayKlass(Symbol* name, KlassKind kind) :
 90   Klass(kind),
 91   _dimension(1),
 92   _higher_dimension(nullptr),
 93   _lower_dimension(nullptr) {
 94   // Arrays don't add any new methods, so their vtable is the same size as
 95   // the vtable of klass Object.
 96   set_vtable_length(Universe::base_vtable_size());
 97   set_name(name);
 98   set_super(Universe::is_bootstrapping() ? nullptr : vmClasses::Object_klass());
 99   set_layout_helper(Klass::_lh_neutral_value);
100   set_is_cloneable(); // All arrays are considered to be cloneable (See JLS 20.1.5)
101   JFR_ONLY(INIT_ID(this);)
102   log_array_class_load(this);
103 }
104 
105 Symbol* ArrayKlass::create_element_klass_array_name(Klass* element_klass, bool qdesc, TRAPS) {
106   ResourceMark rm(THREAD);
107   Symbol* name = nullptr;
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++] = JVM_SIGNATURE_ARRAY;
113   if (element_klass->is_instance_klass()) { // it could be an array or simple type
114     if (qdesc) {
115       new_str[idx++] = JVM_SIGNATURE_PRIMITIVE_OBJECT;
116     } else {
117       new_str[idx++] = JVM_SIGNATURE_CLASS;
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++] = JVM_SIGNATURE_ENDCLASS;
124   }
125   new_str[idx++] = '\0';
126   return SymbolTable::new_symbol(new_str);
127 }
128 
129 // Initialization of vtables and mirror object is done separately from base_create_array_klass,
130 // since a GC can happen. At this point all instance variables of the ArrayKlass must be setup.
131 void ArrayKlass::complete_create_array_klass(ArrayKlass* k, Klass* super_klass, ModuleEntry* module_entry, TRAPS) {
132   k->initialize_supers(super_klass, nullptr, CHECK);
133   k->vtable().initialize_vtable();
134 
135   // During bootstrapping, before java.base is defined, the module_entry may not be present yet.
136   // These classes will be put on a fixup list and their module fields will be patched once
137   // java.base is defined.
138   assert((module_entry != nullptr) || ((module_entry == nullptr) && !ModuleEntryTable::javabase_defined()),
139          "module entry not available post " JAVA_BASE_NAME " definition");
140   oop module = (module_entry != nullptr) ? module_entry->module() : (oop)nullptr;
141   java_lang_Class::create_mirror(k, Handle(THREAD, k->class_loader()), Handle(THREAD, module), Handle(), Handle(), CHECK);
142 }
143 
144 GrowableArray<Klass*>* ArrayKlass::compute_secondary_supers(int num_extra_slots,
145                                                             Array<InstanceKlass*>* transitive_interfaces) {
146   // interfaces = { cloneable_klass, serializable_klass };
147   assert(num_extra_slots == 0, "sanity of primitive array type");
148   assert(transitive_interfaces == nullptr, "sanity");
149   // Must share this for correct bootstrapping!
150   set_secondary_supers(Universe::the_array_interfaces_array());
151   return nullptr;
152 }
153 
154 objArrayOop ArrayKlass::allocate_arrayArray(int n, int length, TRAPS) {
155   check_array_allocation_length(length, arrayOopDesc::max_array_length(T_ARRAY), CHECK_NULL);
156   size_t size = objArrayOopDesc::object_size(length);
157   ArrayKlass* ak = array_klass(n + dimension(), CHECK_NULL);
158   objArrayOop o = (objArrayOop)Universe::heap()->array_allocate(ak, size, length,
159                                                                 /* do_zero */ true, CHECK_NULL);
160   // initialization to null not necessary, area already cleared
161   return o;
162 }
163 
164 oop ArrayKlass::component_mirror() const {
165   return java_lang_Class::component_mirror(java_mirror());
166 }
167 
168 jint ArrayKlass::compute_modifier_flags() 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 #if INCLUDE_CDS
190 void ArrayKlass::remove_unshareable_info() {
191   Klass::remove_unshareable_info();
192   if (_higher_dimension != nullptr) {
193     ArrayKlass *ak = higher_dimension();
194     ak->remove_unshareable_info();
195   }
196 }
197 
198 void ArrayKlass::remove_java_mirror() {
199   Klass::remove_java_mirror();
200   if (_higher_dimension != nullptr) {
201     ArrayKlass *ak = higher_dimension();
202     ak->remove_java_mirror();
203   }
204 }
205 
206 void ArrayKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
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 = higher_dimension();
212     log_array_class_load(ak);
213     ak->restore_unshareable_info(loader_data, protection_domain, CHECK);
214   }
215 }
216 
217 void ArrayKlass::cds_print_value_on(outputStream* st) const {
218   assert(is_klass(), "must be klass");
219   st->print("      - array: %s", internal_name());
220   if (_higher_dimension != nullptr) {
221     ArrayKlass* ak = higher_dimension();
222     st->cr();
223     ak->cds_print_value_on(st);
224   }
225 }
226 #endif // INCLUDE_CDS
227 
228 void ArrayKlass::log_array_class_load(Klass* k) {
229   LogTarget(Debug, class, load, array) lt;
230   if (lt.is_enabled()) {
231     LogStream ls(lt);
232     ResourceMark rm;
233     ls.print("%s", k->name()->as_klass_external_name());
234     if (MetaspaceShared::is_shared_dynamic((void*)k)) {
235       ls.print(" source: shared objects file (top)");
236     } else if (MetaspaceShared::is_shared_static((void*)k)) {
237       ls.print(" source: shared objects file");
238     }
239     ls.cr();
240   }
241 }
242 
243 // Printing
244 
245 void ArrayKlass::print_on(outputStream* st) const {
246   assert(is_klass(), "must be klass");
247   Klass::print_on(st);
248 }
249 
250 void ArrayKlass::print_value_on(outputStream* st) const {
251   assert(is_klass(), "must be klass");
252   for(int index = 0; index < dimension(); index++) {
253     st->print("[]");
254   }
255 }
256 
257 void ArrayKlass::oop_print_on(oop obj, outputStream* st) {
258   assert(obj->is_array(), "must be array");
259   Klass::oop_print_on(obj, st);
260   st->print_cr(" - length: %d", arrayOop(obj)->length());
261 }
262 
263 
264 // Verification
265 
266 void ArrayKlass::verify_on(outputStream* st) {
267   Klass::verify_on(st);
268 }
269 
270 void ArrayKlass::oop_verify_on(oop obj, outputStream* st) {
271   guarantee(obj->is_array(), "must be array");
272   arrayOop a = arrayOop(obj);
273   guarantee(a->length() >= 0, "array with negative length?");
274 }