1 /*
  2  * Copyright (c) 1997, 2024, 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/cdsConfig.hpp"
 27 #include "cds/metaspaceShared.hpp"
 28 #include "classfile/javaClasses.hpp"
 29 #include "classfile/moduleEntry.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.inline.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 ArrayKlass::ArrayKlass() {
 46   assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for CDS");
 47 }
 48 
 49 int ArrayKlass::static_size(int header_size) {
 50   // size of an array klass object
 51   assert(header_size <= InstanceKlass::header_size(), "bad header size");
 52   // If this assert fails, see comments in base_create_array_klass.
 53   header_size = InstanceKlass::header_size();
 54   int vtable_len = Universe::base_vtable_size();
 55   int size = header_size + vtable_len;
 56   return align_metadata_size(size);
 57 }
 58 
 59 
 60 InstanceKlass* ArrayKlass::java_super() const {
 61   if (super() == nullptr)  return nullptr;  // bootstrap case
 62   // Array klasses have primary supertypes which are not reported to Java.
 63   // Example super chain:  String[][] -> Object[][] -> Object[] -> Object
 64   return vmClasses::Object_klass();
 65 }
 66 
 67 
 68 oop ArrayKlass::multi_allocate(int rank, jint* sizes, TRAPS) {
 69   ShouldNotReachHere();
 70   return nullptr;
 71 }
 72 
 73 // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
 74 Klass* ArrayKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
 75   // There are no fields in an array klass but look to the super class (Object)
 76   assert(super(), "super klass must be present");
 77   return super()->find_field(name, sig, fd);
 78 }
 79 
 80 Method* ArrayKlass::uncached_lookup_method(const Symbol* name,
 81                                            const Symbol* signature,
 82                                            OverpassLookupMode overpass_mode,
 83                                            PrivateLookupMode private_mode) const {
 84   // There are no methods in an array klass but the super class (Object) has some
 85   assert(super(), "super klass must be present");
 86   // Always ignore overpass methods in superclasses, although technically the
 87   // super klass of an array, (j.l.Object) should not have
 88   // any overpass methods present.
 89   return super()->uncached_lookup_method(name, signature, OverpassLookupMode::skip, private_mode);
 90 }
 91 
 92 ArrayKlass::ArrayKlass(Symbol* name, KlassKind kind) :
 93   Klass(kind),
 94   _dimension(1),
 95   _higher_dimension(nullptr),
 96   _lower_dimension(nullptr) {
 97   // Arrays don't add any new methods, so their vtable is the same size as
 98   // the vtable of klass Object.
 99   set_vtable_length(Universe::base_vtable_size());
100   set_name(name);
101   set_super(Universe::is_bootstrapping() ? nullptr : vmClasses::Object_klass());
102   set_layout_helper(Klass::_lh_neutral_value);
103   set_is_cloneable(); // All arrays are considered to be cloneable (See JLS 20.1.5)
104   JFR_ONLY(INIT_ID(this);)
105   log_array_class_load(this);
106 }
107 
108 
109 // Initialization of vtables and mirror object is done separately from base_create_array_klass,
110 // since a GC can happen. At this point all instance variables of the ArrayKlass must be setup.
111 void ArrayKlass::complete_create_array_klass(ArrayKlass* k, Klass* super_klass, ModuleEntry* module_entry, TRAPS) {
112   k->initialize_supers(super_klass, nullptr, CHECK);
113   k->vtable().initialize_vtable();
114 
115   // During bootstrapping, before java.base is defined, the module_entry may not be present yet.
116   // These classes will be put on a fixup list and their module fields will be patched once
117   // java.base is defined.
118   assert((module_entry != nullptr) || ((module_entry == nullptr) && !ModuleEntryTable::javabase_defined()),
119          "module entry not available post " JAVA_BASE_NAME " definition");
120   oop module = (module_entry != nullptr) ? module_entry->module() : (oop)nullptr;
121   java_lang_Class::create_mirror(k, Handle(THREAD, k->class_loader()), Handle(THREAD, module), Handle(), Handle(), CHECK);
122 }
123 
124 ArrayKlass* ArrayKlass::array_klass(int n, TRAPS) {
125 
126   assert(dimension() <= n, "check order of chain");
127   int dim = dimension();
128   if (dim == n) return this;
129 
130   // lock-free read needs acquire semantics
131   if (higher_dimension_acquire() == nullptr) {
132 
133     // Ensure atomic creation of higher dimensions
134     RecursiveLocker rl(MultiArray_lock, THREAD);
135 
136     if (higher_dimension() == nullptr) {
137       // Create multi-dim klass object and link them together
138       ObjArrayKlass* ak =
139           ObjArrayKlass::allocate_objArray_klass(class_loader_data(), dim + 1, this, CHECK_NULL);
140       // use 'release' to pair with lock-free load
141       release_set_higher_dimension(ak);
142       assert(ak->lower_dimension() == this, "lower dimension mismatch");
143     }
144   }
145 
146   ObjArrayKlass* ak = higher_dimension();
147   assert(ak != nullptr, "should be set");
148   THREAD->check_possible_safepoint();
149   return ak->array_klass(n, THREAD);
150 }
151 
152 ArrayKlass* ArrayKlass::array_klass_or_null(int n) {
153 
154   assert(dimension() <= n, "check order of chain");
155   int dim = dimension();
156   if (dim == n) return this;
157 
158   // lock-free read needs acquire semantics
159   if (higher_dimension_acquire() == nullptr) {
160     return nullptr;
161   }
162 
163   ObjArrayKlass *ak = higher_dimension();
164   return ak->array_klass_or_null(n);
165 }
166 
167 ArrayKlass* ArrayKlass::array_klass(TRAPS) {
168   return array_klass(dimension() +  1, THREAD);
169 }
170 
171 ArrayKlass* ArrayKlass::array_klass_or_null() {
172   return array_klass_or_null(dimension() +  1);
173 }
174 
175 
176 GrowableArray<Klass*>* ArrayKlass::compute_secondary_supers(int num_extra_slots,
177                                                             Array<InstanceKlass*>* transitive_interfaces) {
178   // interfaces = { cloneable_klass, serializable_klass };
179   assert(num_extra_slots == 0, "sanity of primitive array type");
180   assert(transitive_interfaces == nullptr, "sanity");
181   // Must share this for correct bootstrapping!
182   set_secondary_supers(Universe::the_array_interfaces_array(),
183                        Universe::the_array_interfaces_bitmap());
184   return nullptr;
185 }
186 
187 objArrayOop ArrayKlass::allocate_arrayArray(int n, int length, TRAPS) {
188   check_array_allocation_length(length, arrayOopDesc::max_array_length(T_ARRAY), CHECK_NULL);
189   size_t size = objArrayOopDesc::object_size(length);
190   ArrayKlass* ak = array_klass(n + dimension(), CHECK_NULL);
191   objArrayOop o = (objArrayOop)Universe::heap()->array_allocate(ak, size, length,
192                                                                 /* do_zero */ true, CHECK_NULL);
193   // initialization to null not necessary, area already cleared
194   return o;
195 }
196 
197 jint ArrayKlass::compute_modifier_flags() const {
198   return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
199 }
200 
201 // JVMTI support
202 
203 jint ArrayKlass::jvmti_class_status() const {
204   return JVMTI_CLASS_STATUS_ARRAY;
205 }
206 
207 void ArrayKlass::metaspace_pointers_do(MetaspaceClosure* it) {
208   Klass::metaspace_pointers_do(it);
209 
210   ResourceMark rm;
211   log_trace(cds)("Iter(ArrayKlass): %p (%s)", this, external_name());
212 
213   // need to cast away volatile
214   it->push((Klass**)&_higher_dimension);
215   it->push((Klass**)&_lower_dimension);
216 }
217 
218 #if INCLUDE_CDS
219 void ArrayKlass::remove_unshareable_info() {
220   Klass::remove_unshareable_info();
221   if (_higher_dimension != nullptr) {
222     ArrayKlass *ak = higher_dimension();
223     ak->remove_unshareable_info();
224   }
225 }
226 
227 void ArrayKlass::remove_java_mirror() {
228   Klass::remove_java_mirror();
229   if (_higher_dimension != nullptr) {
230     ArrayKlass *ak = higher_dimension();
231     ak->remove_java_mirror();
232   }
233 }
234 
235 void ArrayKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
236   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
237   // Klass recreates the component mirror also
238 
239   if (_higher_dimension != nullptr) {
240     ArrayKlass *ak = higher_dimension();
241     log_array_class_load(ak);
242     ak->restore_unshareable_info(loader_data, protection_domain, CHECK);
243   }
244 }
245 
246 void ArrayKlass::cds_print_value_on(outputStream* st) const {
247   assert(is_klass(), "must be klass");
248   st->print("      - array: %s", internal_name());
249   if (_higher_dimension != nullptr) {
250     ArrayKlass* ak = higher_dimension();
251     st->cr();
252     ak->cds_print_value_on(st);
253   }
254 }
255 #endif // INCLUDE_CDS
256 
257 void ArrayKlass::log_array_class_load(Klass* k) {
258   LogTarget(Debug, class, load, array) lt;
259   if (lt.is_enabled()) {
260     LogStream ls(lt);
261     ResourceMark rm;
262     ls.print("%s", k->name()->as_klass_external_name());
263     if (MetaspaceShared::is_shared_dynamic((void*)k)) {
264       ls.print(" source: shared objects file (top)");
265     } else if (MetaspaceShared::is_shared_static((void*)k)) {
266       ls.print(" source: shared objects file");
267     }
268     ls.cr();
269   }
270 }
271 
272 // Printing
273 
274 void ArrayKlass::print_on(outputStream* st) const {
275   assert(is_klass(), "must be klass");
276   Klass::print_on(st);
277 }
278 
279 void ArrayKlass::print_value_on(outputStream* st) const {
280   assert(is_klass(), "must be klass");
281   for(int index = 0; index < dimension(); index++) {
282     st->print("[]");
283   }
284 }
285 
286 void ArrayKlass::oop_print_on(oop obj, outputStream* st) {
287   assert(obj->is_array(), "must be array");
288   Klass::oop_print_on(obj, st);
289   st->print_cr(" - length: %d", arrayOop(obj)->length());
290 }
291 
292 
293 // Verification
294 
295 void ArrayKlass::verify_on(outputStream* st) {
296   Klass::verify_on(st);
297 }
298 
299 void ArrayKlass::oop_verify_on(oop obj, outputStream* st) {
300   guarantee(obj->is_array(), "must be array");
301   arrayOop a = arrayOop(obj);
302   guarantee(a->length() >= 0, "array with negative length?");
303 }