1 /*
  2  * Copyright (c) 1997, 2021, 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() == NULL)  return NULL;  // 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 NULL;
 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, KlassID id) :
 89   Klass(id),
 90   _dimension(1),
 91   _higher_dimension(NULL),
 92   _lower_dimension(NULL) {
 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() ? NULL : 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 = NULL;
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_INLINE_TYPE;
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 separatly 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, NULL, 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 != NULL) || ((module_entry == NULL) && !ModuleEntryTable::javabase_defined()),
137          "module entry not available post " JAVA_BASE_NAME " definition");
138   oop module = (module_entry != NULL) ? module_entry->module() : (oop)NULL;
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 == NULL, "sanity");
147   // Must share this for correct bootstrapping!
148   set_secondary_supers(Universe::the_array_interfaces_array());
149   return NULL;
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   int 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 void ArrayKlass::array_klasses_do(void f(Klass* k, TRAPS), TRAPS) {
164   Klass* k = this;
165   // Iterate over this array klass and all higher dimensions
166   while (k != NULL) {
167     f(k, CHECK);
168     k = ArrayKlass::cast(k)->higher_dimension();
169   }
170 }
171 
172 void ArrayKlass::array_klasses_do(void f(Klass* k)) {
173   Klass* k = this;
174   // Iterate over this array klass and all higher dimensions
175   while (k != NULL) {
176     f(k);
177     k = ArrayKlass::cast(k)->higher_dimension();
178   }
179 }
180 
181 oop ArrayKlass::component_mirror() const {
182   return java_lang_Class::component_mirror(java_mirror());
183 }
184 
185 jint ArrayKlass::compute_modifier_flags() const {
186   return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
187 }
188 
189 // JVMTI support
190 
191 jint ArrayKlass::jvmti_class_status() const {
192   return JVMTI_CLASS_STATUS_ARRAY;
193 }
194 
195 void ArrayKlass::metaspace_pointers_do(MetaspaceClosure* it) {
196   Klass::metaspace_pointers_do(it);
197 
198   ResourceMark rm;
199   log_trace(cds)("Iter(ArrayKlass): %p (%s)", this, external_name());
200 
201   // need to cast away volatile
202   it->push((Klass**)&_higher_dimension);
203   it->push((Klass**)&_lower_dimension);
204 }
205 
206 void ArrayKlass::remove_unshareable_info() {
207   Klass::remove_unshareable_info();
208   if (_higher_dimension != NULL) {
209     ArrayKlass *ak = ArrayKlass::cast(higher_dimension());
210     ak->remove_unshareable_info();
211   }
212 }
213 
214 void ArrayKlass::remove_java_mirror() {
215   Klass::remove_java_mirror();
216   if (_higher_dimension != NULL) {
217     ArrayKlass *ak = ArrayKlass::cast(higher_dimension());
218     ak->remove_java_mirror();
219   }
220 }
221 
222 void ArrayKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
223   assert(loader_data == ClassLoaderData::the_null_class_loader_data(), "array classes belong to null loader");
224   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
225   // Klass recreates the component mirror also
226 
227   if (_higher_dimension != NULL) {
228     ArrayKlass *ak = ArrayKlass::cast(higher_dimension());
229     ak->restore_unshareable_info(loader_data, protection_domain, CHECK);
230   }
231 }
232 
233 // Printing
234 
235 void ArrayKlass::print_on(outputStream* st) const {
236   assert(is_klass(), "must be klass");
237   Klass::print_on(st);
238 }
239 
240 void ArrayKlass::print_value_on(outputStream* st) const {
241   assert(is_klass(), "must be klass");
242   for(int index = 0; index < dimension(); index++) {
243     st->print("[]");
244   }
245 }
246 
247 void ArrayKlass::oop_print_on(oop obj, outputStream* st) {
248   assert(obj->is_array(), "must be array");
249   Klass::oop_print_on(obj, st);
250   st->print_cr(" - length: %d", arrayOop(obj)->length());
251 }
252 
253 
254 // Verification
255 
256 void ArrayKlass::verify_on(outputStream* st) {
257   Klass::verify_on(st);
258 }
259 
260 void ArrayKlass::oop_verify_on(oop obj, outputStream* st) {
261   guarantee(obj->is_array(), "must be array");
262   arrayOop a = arrayOop(obj);
263   guarantee(a->length() >= 0, "array with negative length?");
264 }