1 /*
2  * Copyright (c) 1997, 2017, 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_interface/collectedHeap.inline.hpp"
30 #include "jvmtifiles/jvmti.h"
31 #include "memory/gcLocker.hpp"
32 #include "memory/universe.inline.hpp"
33 #include "oops/arrayKlass.hpp"
34 #include "oops/arrayOop.hpp"
35 #include "oops/instanceKlass.hpp"
36 #include "oops/objArrayOop.hpp"
37 #include "oops/oop.inline.hpp"
38 
static_size(int header_size)39 int ArrayKlass::static_size(int header_size) {
40   // size of an array klass object
41   assert(header_size <= InstanceKlass::header_size(), "bad header size");
42   // If this assert fails, see comments in base_create_array_klass.
43   header_size = InstanceKlass::header_size();
44   int vtable_len = Universe::base_vtable_size();
45 #ifdef _LP64
46   int size = header_size + align_object_offset(vtable_len);
47 #else
48   int size = header_size + vtable_len;
49 #endif
50   return align_object_size(size);
51 }
52 
53 
java_super() const54 Klass* ArrayKlass::java_super() const {
55   if (super() == NULL)  return NULL;  // bootstrap case
56   // Array klasses have primary supertypes which are not reported to Java.
57   // Example super chain:  String[][] -> Object[][] -> Object[] -> Object
58   return SystemDictionary::Object_klass();
59 }
60 
61 
multi_allocate(int rank,jint * sizes,TRAPS)62 oop ArrayKlass::multi_allocate(int rank, jint* sizes, TRAPS) {
63   ShouldNotReachHere();
64   return NULL;
65 }
66 
67 // 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) const68 Klass* ArrayKlass::find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
69   // There are no fields in an array klass but look to the super class (Object)
70   assert(super(), "super klass must be present");
71   return super()->find_field(name, sig, fd);
72 }
73 
uncached_lookup_method(Symbol * name,Symbol * signature,OverpassLookupMode overpass_mode) const74 Method* ArrayKlass::uncached_lookup_method(Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode) const {
75   // There are no methods in an array klass but the super class (Object) has some
76   assert(super(), "super klass must be present");
77   // Always ignore overpass methods in superclasses, although technically the
78   // super klass of an array, (j.l.Object) should not have
79   // any overpass methods present.
80   return super()->uncached_lookup_method(name, signature, Klass::skip_overpass);
81 }
82 
ArrayKlass(Symbol * name)83 ArrayKlass::ArrayKlass(Symbol* name) {
84   set_name(name);
85 
86   set_super(Universe::is_bootstrapping() ? (Klass*)NULL : SystemDictionary::Object_klass());
87   set_layout_helper(Klass::_lh_neutral_value);
88   set_dimension(1);
89   set_higher_dimension(NULL);
90   set_lower_dimension(NULL);
91   set_component_mirror(NULL);
92   // Arrays don't add any new methods, so their vtable is the same size as
93   // the vtable of klass Object.
94   int vtable_size = Universe::base_vtable_size();
95   set_vtable_length(vtable_size);
96   set_is_cloneable(); // All arrays are considered to be cloneable (See JLS 20.1.5)
97   JFR_ONLY(INIT_ID(this);)
98 }
99 
100 
101 // Initialization of vtables and mirror object is done separatly from base_create_array_klass,
102 // since a GC can happen. At this point all instance variables of the ArrayKlass must be setup.
complete_create_array_klass(ArrayKlass * k,KlassHandle super_klass,TRAPS)103 void ArrayKlass::complete_create_array_klass(ArrayKlass* k, KlassHandle super_klass, TRAPS) {
104   ResourceMark rm(THREAD);
105   k->initialize_supers(super_klass(), CHECK);
106   k->vtable()->initialize_vtable(false, CHECK);
107   java_lang_Class::create_mirror(k, Handle(THREAD, k->class_loader()), Handle(NULL), CHECK);
108 }
109 
compute_secondary_supers(int num_extra_slots)110 GrowableArray<Klass*>* ArrayKlass::compute_secondary_supers(int num_extra_slots) {
111   // interfaces = { cloneable_klass, serializable_klass };
112   assert(num_extra_slots == 0, "sanity of primitive array type");
113   // Must share this for correct bootstrapping!
114   set_secondary_supers(Universe::the_array_interfaces_array());
115   return NULL;
116 }
117 
compute_is_subtype_of(Klass * k)118 bool ArrayKlass::compute_is_subtype_of(Klass* k) {
119   // An array is a subtype of Serializable, Clonable, and Object
120   return    k == SystemDictionary::Object_klass()
121          || k == SystemDictionary::Cloneable_klass()
122          || k == SystemDictionary::Serializable_klass();
123 }
124 
125 
start_of_vtable() const126 inline intptr_t* ArrayKlass::start_of_vtable() const {
127   // all vtables start at the same place, that's why we use InstanceKlass::header_size here
128   return ((intptr_t*)this) + InstanceKlass::header_size();
129 }
130 
131 
vtable() const132 klassVtable* ArrayKlass::vtable() const {
133   KlassHandle kh(Thread::current(), this);
134   return new klassVtable(kh, start_of_vtable(), vtable_length() / vtableEntry::size());
135 }
136 
137 
allocate_arrayArray(int n,int length,TRAPS)138 objArrayOop ArrayKlass::allocate_arrayArray(int n, int length, TRAPS) {
139   if (length < 0) {
140     THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
141   }
142   if (length > arrayOopDesc::max_array_length(T_ARRAY)) {
143     report_java_out_of_memory("Requested array size exceeds VM limit");
144     JvmtiExport::post_array_size_exhausted();
145     THROW_OOP_0(Universe::out_of_memory_error_array_size());
146   }
147   int size = objArrayOopDesc::object_size(length);
148   Klass* k = array_klass(n+dimension(), CHECK_0);
149   ArrayKlass* ak = ArrayKlass::cast(k);
150   objArrayOop o =
151     (objArrayOop)CollectedHeap::array_allocate(ak, size, length, CHECK_0);
152   // initialization to NULL not necessary, area already cleared
153   return o;
154 }
155 
array_klasses_do(void f (Klass * k,TRAPS),TRAPS)156 void ArrayKlass::array_klasses_do(void f(Klass* k, TRAPS), TRAPS) {
157   Klass* k = this;
158   // Iterate over this array klass and all higher dimensions
159   while (k != NULL) {
160     f(k, CHECK);
161     k = ArrayKlass::cast(k)->higher_dimension();
162   }
163 }
164 
array_klasses_do(void f (Klass * k))165 void ArrayKlass::array_klasses_do(void f(Klass* k)) {
166   Klass* k = this;
167   // Iterate over this array klass and all higher dimensions
168   while (k != NULL) {
169     f(k);
170     k = ArrayKlass::cast(k)->higher_dimension();
171   }
172 }
173 
174 // GC support
175 
oops_do(OopClosure * cl)176 void ArrayKlass::oops_do(OopClosure* cl) {
177   Klass::oops_do(cl);
178 
179   cl->do_oop(adr_component_mirror());
180 }
181 
182 // JVM support
183 
compute_modifier_flags(TRAPS) const184 jint ArrayKlass::compute_modifier_flags(TRAPS) const {
185   return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
186 }
187 
188 // JVMTI support
189 
jvmti_class_status() const190 jint ArrayKlass::jvmti_class_status() const {
191   return JVMTI_CLASS_STATUS_ARRAY;
192 }
193 
remove_unshareable_info()194 void ArrayKlass::remove_unshareable_info() {
195   Klass::remove_unshareable_info();
196   // Clear the java mirror
197   set_component_mirror(NULL);
198 }
199 
restore_unshareable_info(ClassLoaderData * loader_data,Handle protection_domain,TRAPS)200 void ArrayKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
201   assert(loader_data == ClassLoaderData::the_null_class_loader_data(), "array classes belong to null loader");
202   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
203   // Klass recreates the component mirror also
204 }
205 
206 // Printing
207 
print_on(outputStream * st) const208 void ArrayKlass::print_on(outputStream* st) const {
209   assert(is_klass(), "must be klass");
210   Klass::print_on(st);
211 }
212 
print_value_on(outputStream * st) const213 void ArrayKlass::print_value_on(outputStream* st) const {
214   assert(is_klass(), "must be klass");
215   for(int index = 0; index < dimension(); index++) {
216     st->print("[]");
217   }
218 }
219 
oop_print_on(oop obj,outputStream * st)220 void ArrayKlass::oop_print_on(oop obj, outputStream* st) {
221   assert(obj->is_array(), "must be array");
222   Klass::oop_print_on(obj, st);
223   st->print_cr(" - length: %d", arrayOop(obj)->length());
224 }
225 
226 
227 // Verification
228 
verify_on(outputStream * st)229 void ArrayKlass::verify_on(outputStream* st) {
230   Klass::verify_on(st);
231 
232   if (component_mirror() != NULL) {
233     guarantee(component_mirror()->klass() != NULL, "should have a class");
234   }
235 }
236 
oop_verify_on(oop obj,outputStream * st)237 void ArrayKlass::oop_verify_on(oop obj, outputStream* st) {
238   guarantee(obj->is_array(), "must be array");
239   arrayOop a = arrayOop(obj);
240   guarantee(a->length() >= 0, "array with negative length?");
241 }
242