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