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