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