1 /*
2  * Copyright (c) 1997, 2018, 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/systemDictionary.hpp"
28 #include "classfile/vmSymbols.hpp"
29 #include "gc/shared/collectedHeap.inline.hpp"
30 #include "jvmtifiles/jvmti.h"
31 #include "memory/metaspaceClosure.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "memory/universe.hpp"
34 #include "oops/arrayKlass.hpp"
35 #include "oops/arrayOop.hpp"
36 #include "oops/instanceKlass.hpp"
37 #include "oops/objArrayOop.hpp"
38 #include "oops/oop.inline.hpp"
39 #include "runtime/handles.inline.hpp"
40 
static_size(int header_size)41 int ArrayKlass::static_size(int header_size) {
42   // size of an array klass object
43   assert(header_size <= InstanceKlass::header_size(), "bad header size");
44   // If this assert fails, see comments in base_create_array_klass.
45   header_size = InstanceKlass::header_size();
46   int vtable_len = Universe::base_vtable_size();
47   int size = header_size + vtable_len;
48   return align_metadata_size(size);
49 }
50 
51 
java_super() const52 Klass* ArrayKlass::java_super() const {
53   if (super() == NULL)  return NULL;  // bootstrap case
54   // Array klasses have primary supertypes which are not reported to Java.
55   // Example super chain:  String[][] -> Object[][] -> Object[] -> Object
56   return SystemDictionary::Object_klass();
57 }
58 
59 
multi_allocate(int rank,jint * sizes,TRAPS)60 oop ArrayKlass::multi_allocate(int rank, jint* sizes, TRAPS) {
61   ShouldNotReachHere();
62   return NULL;
63 }
64 
65 // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
find_field(Symbol * name,Symbol * sig,fieldDescriptor * fd) const66 Klass* ArrayKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
67   // There are no fields in an array klass but look to the super class (Object)
68   assert(super(), "super klass must be present");
69   return super()->find_field(name, sig, fd);
70 }
71 
uncached_lookup_method(const Symbol * name,const Symbol * signature,OverpassLookupMode overpass_mode,PrivateLookupMode private_mode) const72 Method* ArrayKlass::uncached_lookup_method(const Symbol* name,
73                                            const Symbol* signature,
74                                            OverpassLookupMode overpass_mode,
75                                            PrivateLookupMode private_mode) const {
76   // There are no methods in an array klass but the super class (Object) has some
77   assert(super(), "super klass must be present");
78   // Always ignore overpass methods in superclasses, although technically the
79   // super klass of an array, (j.l.Object) should not have
80   // any overpass methods present.
81   return super()->uncached_lookup_method(name, signature, Klass::skip_overpass, private_mode);
82 }
83 
ArrayKlass(Symbol * name,KlassID id)84 ArrayKlass::ArrayKlass(Symbol* name, KlassID id) :
85   Klass(id),
86   _dimension(1),
87   _higher_dimension(NULL),
88   _lower_dimension(NULL) {
89     // Arrays don't add any new methods, so their vtable is the same size as
90     // the vtable of klass Object.
91     set_vtable_length(Universe::base_vtable_size());
92     set_name(name);
93     set_super(Universe::is_bootstrapping() ? (Klass*)NULL : SystemDictionary::Object_klass());
94     set_layout_helper(Klass::_lh_neutral_value);
95     set_is_cloneable(); // All arrays are considered to be cloneable (See JLS 20.1.5)
96     JFR_ONLY(INIT_ID(this);)
97 }
98 
99 
100 // Initialization of vtables and mirror object is done separatly from base_create_array_klass,
101 // since a GC can happen. At this point all instance variables of the ArrayKlass must be setup.
complete_create_array_klass(ArrayKlass * k,Klass * super_klass,ModuleEntry * module_entry,TRAPS)102 void ArrayKlass::complete_create_array_klass(ArrayKlass* k, Klass* super_klass, ModuleEntry* module_entry, TRAPS) {
103   ResourceMark rm(THREAD);
104   k->initialize_supers(super_klass, NULL, CHECK);
105   k->vtable().initialize_vtable(false, CHECK);
106 
107   // During bootstrapping, before java.base is defined, the module_entry may not be present yet.
108   // These classes will be put on a fixup list and their module fields will be patched once
109   // java.base is defined.
110   assert((module_entry != NULL) || ((module_entry == NULL) && !ModuleEntryTable::javabase_defined()),
111          "module entry not available post " JAVA_BASE_NAME " definition");
112   oop module = (module_entry != NULL) ? module_entry->module() : (oop)NULL;
113   java_lang_Class::create_mirror(k, Handle(THREAD, k->class_loader()), Handle(THREAD, module), Handle(), CHECK);
114 }
115 
compute_secondary_supers(int num_extra_slots,Array<Klass * > * transitive_interfaces)116 GrowableArray<Klass*>* ArrayKlass::compute_secondary_supers(int num_extra_slots,
117                                                             Array<Klass*>* transitive_interfaces) {
118   // interfaces = { cloneable_klass, serializable_klass };
119   assert(num_extra_slots == 0, "sanity of primitive array type");
120   assert(transitive_interfaces == NULL, "sanity");
121   // Must share this for correct bootstrapping!
122   set_secondary_supers(Universe::the_array_interfaces_array());
123   return NULL;
124 }
125 
compute_is_subtype_of(Klass * k)126 bool ArrayKlass::compute_is_subtype_of(Klass* k) {
127   // An array is a subtype of Serializable, Clonable, and Object
128   return    k == SystemDictionary::Object_klass()
129          || k == SystemDictionary::Cloneable_klass()
130          || k == SystemDictionary::Serializable_klass();
131 }
132 
allocate_arrayArray(int n,int length,TRAPS)133 objArrayOop ArrayKlass::allocate_arrayArray(int n, int length, TRAPS) {
134   if (length < 0) {
135     THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length));
136   }
137   if (length > arrayOopDesc::max_array_length(T_ARRAY)) {
138     report_java_out_of_memory("Requested array size exceeds VM limit");
139     JvmtiExport::post_array_size_exhausted();
140     THROW_OOP_0(Universe::out_of_memory_error_array_size());
141   }
142   int size = objArrayOopDesc::object_size(length);
143   Klass* k = array_klass(n+dimension(), CHECK_0);
144   ArrayKlass* ak = ArrayKlass::cast(k);
145   objArrayOop o = (objArrayOop)Universe::heap()->array_allocate(ak, size, length,
146                                                                 /* do_zero */ true, CHECK_0);
147   // initialization to NULL not necessary, area already cleared
148   return o;
149 }
150 
array_klasses_do(void f (Klass * k,TRAPS),TRAPS)151 void ArrayKlass::array_klasses_do(void f(Klass* k, TRAPS), TRAPS) {
152   Klass* k = this;
153   // Iterate over this array klass and all higher dimensions
154   while (k != NULL) {
155     f(k, CHECK);
156     k = ArrayKlass::cast(k)->higher_dimension();
157   }
158 }
159 
array_klasses_do(void f (Klass * k))160 void ArrayKlass::array_klasses_do(void f(Klass* k)) {
161   Klass* k = this;
162   // Iterate over this array klass and all higher dimensions
163   while (k != NULL) {
164     f(k);
165     k = ArrayKlass::cast(k)->higher_dimension();
166   }
167 }
168 
169 // JVM support
170 
compute_modifier_flags(TRAPS) const171 jint ArrayKlass::compute_modifier_flags(TRAPS) const {
172   return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
173 }
174 
175 // JVMTI support
176 
jvmti_class_status() const177 jint ArrayKlass::jvmti_class_status() const {
178   return JVMTI_CLASS_STATUS_ARRAY;
179 }
180 
metaspace_pointers_do(MetaspaceClosure * it)181 void ArrayKlass::metaspace_pointers_do(MetaspaceClosure* it) {
182   Klass::metaspace_pointers_do(it);
183 
184   ResourceMark rm;
185   log_trace(cds)("Iter(ArrayKlass): %p (%s)", this, external_name());
186 
187   // need to cast away volatile
188   it->push((Klass**)&_higher_dimension);
189   it->push((Klass**)&_lower_dimension);
190 }
191 
remove_unshareable_info()192 void ArrayKlass::remove_unshareable_info() {
193   Klass::remove_unshareable_info();
194   if (_higher_dimension != NULL) {
195     ArrayKlass *ak = ArrayKlass::cast(higher_dimension());
196     ak->remove_unshareable_info();
197   }
198 }
199 
remove_java_mirror()200 void ArrayKlass::remove_java_mirror() {
201   Klass::remove_java_mirror();
202   if (_higher_dimension != NULL) {
203     ArrayKlass *ak = ArrayKlass::cast(higher_dimension());
204     ak->remove_java_mirror();
205   }
206 }
207 
restore_unshareable_info(ClassLoaderData * loader_data,Handle protection_domain,TRAPS)208 void ArrayKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
209   assert(loader_data == ClassLoaderData::the_null_class_loader_data(), "array classes belong to null loader");
210   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
211   // Klass recreates the component mirror also
212 
213   if (_higher_dimension != NULL) {
214     ArrayKlass *ak = ArrayKlass::cast(higher_dimension());
215     ak->restore_unshareable_info(loader_data, protection_domain, CHECK);
216   }
217 }
218 
219 // Printing
220 
print_on(outputStream * st) const221 void ArrayKlass::print_on(outputStream* st) const {
222   assert(is_klass(), "must be klass");
223   Klass::print_on(st);
224 }
225 
print_value_on(outputStream * st) const226 void ArrayKlass::print_value_on(outputStream* st) const {
227   assert(is_klass(), "must be klass");
228   for(int index = 0; index < dimension(); index++) {
229     st->print("[]");
230   }
231 }
232 
oop_print_on(oop obj,outputStream * st)233 void ArrayKlass::oop_print_on(oop obj, outputStream* st) {
234   assert(obj->is_array(), "must be array");
235   Klass::oop_print_on(obj, st);
236   st->print_cr(" - length: %d", arrayOop(obj)->length());
237 }
238 
239 
240 // Verification
241 
verify_on(outputStream * st)242 void ArrayKlass::verify_on(outputStream* st) {
243   Klass::verify_on(st);
244 }
245 
oop_verify_on(oop obj,outputStream * st)246 void ArrayKlass::oop_verify_on(oop obj, outputStream* st) {
247   guarantee(obj->is_array(), "must be array");
248   arrayOop a = arrayOop(obj);
249   guarantee(a->length() >= 0, "array with negative length?");
250 }
251