1 /* 2 * Copyright (c) 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 "gc/shared/gcLocker.inline.hpp" 27 #include "interpreter/interpreter.hpp" 28 #include "oops/oop.inline.hpp" 29 #include "oops/fieldStreams.hpp" 30 #include "oops/method.hpp" 31 #include "oops/objArrayKlass.hpp" 32 #include "oops/valueKlass.hpp" 33 #include "oops/valueArrayKlass.hpp" 34 #include "runtime/signature.hpp" 35 #include "utilities/copy.hpp" 36 37 int ValueKlass::first_field_offset() const { 38 #ifdef ASSERT 39 int first_offset = INT_MAX; 40 for (JavaFieldStream fs(this); !fs.done(); fs.next()) { 41 if (fs.offset() < first_offset) first_offset= fs.offset(); 42 } 43 #endif 44 int base_offset = instanceOopDesc::base_offset_in_bytes(); 45 // The first field of value types is aligned on a long boundary 46 base_offset = align_size_up(base_offset, BytesPerLong); 47 assert(base_offset = first_offset, "inconsistent offsets"); 48 return base_offset; 49 } 50 51 int ValueKlass::raw_value_byte_size() const { 52 int heapOopAlignedSize = nonstatic_field_size() << LogBytesPerHeapOop; 53 // If bigger than 64 bits or needs oop alignment, then use jlong aligned 54 // which for values should be jlong aligned, asserts in raw_field_copy otherwise 55 if (heapOopAlignedSize >= longSize || contains_oops()) { 56 return heapOopAlignedSize; 57 } 58 // Small primitives... 59 // If a few small basic type fields, return the actual size, i.e. 60 // 1 byte = 1 61 // 2 byte = 2 62 // 3 byte = 4, because pow2 needed for element stores 63 int first_offset = first_field_offset(); 64 int last_offset = 0; // find the last offset, add basic type size 65 for (JavaFieldStream fs(this); !fs.done(); fs.next()) { 66 if (fs.offset() > last_offset) { 67 int tsz = 0; 68 BasicType type = fs.field_descriptor().field_type(); 69 if (is_java_primitive(type)) { 70 tsz = type2aelembytes(type); 71 } else if (type == T_VALUETYPE) { 72 // Not just primitives. Layout aligns embedded value, so use jlong aligned it is 73 return heapOopAlignedSize; 74 } else { 75 guarantee(0, "Unknown type %d", type); 76 } 77 assert(tsz > 0, "Invariant"); 78 last_offset = fs.offset() + tsz; 79 } 80 } 81 assert(last_offset > first_offset, "Invariant"); 82 return 1 << upper_log2(last_offset - first_offset); 83 } 84 85 bool ValueKlass::is_atomic() { 86 return (nonstatic_field_size() * heapOopSize) <= longSize; 87 } 88 89 int ValueKlass::nonstatic_oop_count() { 90 int oops = 0; 91 int map_count = nonstatic_oop_map_count(); 92 OopMapBlock* block = start_of_nonstatic_oop_maps(); 93 OopMapBlock* end = block + map_count; 94 while (block != end) { 95 oops += block->count(); 96 block++; 97 } 98 return oops; 99 } 100 101 // Arrays of... 102 103 bool ValueKlass::flatten_array() { 104 if (!ValueArrayFlatten) { 105 return false; 106 } 107 108 int elem_bytes = raw_value_byte_size(); 109 // Too big 110 if ((ValueArrayElemMaxFlatSize >= 0) && (elem_bytes > ValueArrayElemMaxFlatSize)) { 111 return false; 112 } 113 // Too many embedded oops 114 if ((ValueArrayElemMaxFlatOops >= 0) && (nonstatic_oop_count() > ValueArrayElemMaxFlatOops)) { 115 return false; 116 } 117 118 return true; 119 } 120 121 122 Klass* ValueKlass::array_klass_impl(bool or_null, int n, TRAPS) { 123 if (!flatten_array()) { 124 return InstanceKlass::array_klass_impl(or_null, n, THREAD); 125 } 126 127 // Basically the same as instanceKlass, but using "ValueArrayKlass::allocate_klass" 128 if (array_klasses() == NULL) { 129 if (or_null) return NULL; 130 131 ResourceMark rm; 132 JavaThread *jt = (JavaThread *)THREAD; 133 { 134 // Atomic creation of array_klasses 135 MutexLocker mc(Compile_lock, THREAD); // for vtables 136 MutexLocker ma(MultiArray_lock, THREAD); 137 138 // Check if update has already taken place 139 if (array_klasses() == NULL) { 140 Klass* ak; 141 if (is_atomic() || (!ValueArrayAtomicAccess)) { 142 ak = ValueArrayKlass::allocate_klass(this, CHECK_NULL); 143 } else { 144 ak = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, CHECK_NULL); 145 } 146 set_array_klasses(ak); 147 } 148 } 149 } 150 // _this will always be set at this point 151 ArrayKlass* ak = ArrayKlass::cast(array_klasses()); 152 if (or_null) { 153 return ak->array_klass_or_null(n); 154 } 155 return ak->array_klass(n, THREAD); 156 } 157 158 Klass* ValueKlass::array_klass_impl(bool or_null, TRAPS) { 159 return array_klass_impl(or_null, 1, THREAD); 160 } 161 162 void ValueKlass::raw_field_copy(void* src, void* dst, size_t raw_byte_size) { 163 /* 164 * Try not to shear fields even if not an atomic store... 165 * 166 * First 3 cases handle value array store, otherwise works on the same basis 167 * as JVM_Clone, at this size data is aligned. The order of primitive types 168 * is largest to smallest, and it not possible for fields to stradle long 169 * copy boundaries. 170 * 171 * If MT without exclusive access, possible to observe partial value store, 172 * but not partial primitive and reference field values 173 */ 174 switch (raw_byte_size) { 175 case 1: 176 *((jbyte*) dst) = *(jbyte*)src; 177 break; 178 case 2: 179 *((jshort*) dst) = *(jshort*)src; 180 break; 181 case 4: 182 *((jint*) dst) = *(jint*) src; 183 break; 184 default: 185 assert(raw_byte_size % sizeof(jlong) == 0, "Unaligned raw_byte_size"); 186 Copy::conjoint_jlongs_atomic((jlong*)src, (jlong*)dst, raw_byte_size >> LogBytesPerLong); 187 } 188 } 189 190 /* 191 * Store the value of this klass contained with src into dst. 192 * 193 * This operation is appropriate for use from vastore, vaload and putfield (for values) 194 * 195 * GC barriers currently can lock with no safepoint check and allocate c-heap, 196 * so raw point is "safe" for now. 197 * 198 * Going forward, look to use machine generated (stub gen or bc) version for most used klass layouts 199 * 200 */ 201 void ValueKlass::value_store(void* src, void* dst, size_t raw_byte_size, bool dst_heap, bool dst_uninitialized) { 202 if (contains_oops() && dst_heap) { 203 // src/dst aren't oops, need offset to adjust oop map offset 204 const address dst_oop_addr = ((address) dst) - first_field_offset(); 205 206 // Pre-barriers... 207 OopMapBlock* map = start_of_nonstatic_oop_maps(); 208 OopMapBlock* const end = map + nonstatic_oop_map_count(); 209 while (map != end) { 210 // Shame we can't just use the existing oop iterator...src/dst aren't oop 211 address doop_address = dst_oop_addr + map->offset(); 212 if (UseCompressedOops) { 213 oopDesc::bs()->write_ref_array_pre((narrowOop*) doop_address, map->count(), dst_uninitialized); 214 } else { 215 oopDesc::bs()->write_ref_array_pre((oop*) doop_address, map->count(), dst_uninitialized); 216 } 217 map++; 218 } 219 220 raw_field_copy(src, dst, raw_byte_size); 221 222 // Post-barriers... 223 map = start_of_nonstatic_oop_maps(); 224 while (map != end) { 225 address doop_address = dst_oop_addr + map->offset(); 226 oopDesc::bs()->write_ref_array((HeapWord*) doop_address, map->count()); 227 map++; 228 } 229 } else { // Primitive-only case... 230 raw_field_copy(src, dst, raw_byte_size); 231 } 232 } 233 234 oop ValueKlass::derive_value_type_copy(Handle src, InstanceKlass* target_klass, TRAPS) { 235 assert(InstanceKlass::cast(src->klass())->derive_value_type_klass() == target_klass, "Not this DVT"); 236 237 // Allocate new for safety, simply reinstalling the klass pointer is a little too risky 238 target_klass->initialize(CHECK_0); 239 instanceOop value = target_klass->allocate_instance(CHECK_0); 240 value_store(data_for_oop(src()), data_for_oop(value), true, true); 241 return value; 242 } 243 244 // Value type arguments are not passed by reference, instead each 245 // field of the value type is passed as an argument. This helper 246 // function collects the fields of the value types (including embedded 247 // value type's fields) in a list. Included with the field's type is 248 // the offset of each field in the value type: i2c and c2i adapters 249 // need that to load or store fields. Finally, the list of fields is 250 // sorted in order of increasing offsets: the adapters and the 251 // compiled code need and agreed upon order of fields. 252 // 253 // The list of basic types that is returned starts with a T_VALUETYPE 254 // and ends with an extra T_VOID. T_VALUETYPE/T_VOID are used as 255 // delimiters. Every entry between the two is a field of the value 256 // type. If there's an embedded value type in the list, it also starts 257 // with a T_VALUETYPE and ends with a T_VOID. This is so we can 258 // generate a unique fingerprint for the method's adapters and we can 259 // generate the list of basic types from the interpreter point of view 260 // (value types passed as reference: iterate on the list until a 261 // T_VALUETYPE, drop everything until and including the closing 262 // T_VOID) or the compiler point of view (each field of the value 263 // types is an argument: drop all T_VALUETYPE/T_VOID from the list). 264 GrowableArray<SigEntry> ValueKlass::collect_fields(int base_off) const { 265 GrowableArray<SigEntry> sig_extended; 266 sig_extended.push(SigEntry(T_VALUETYPE, base_off)); 267 for (JavaFieldStream fs(this); !fs.done(); fs.next()) { 268 if (fs.access_flags().is_static()) continue; 269 fieldDescriptor& fd = fs.field_descriptor(); 270 BasicType bt = fd.field_type(); 271 int offset = base_off + fd.offset() - (base_off > 0 ? first_field_offset() : 0); 272 if (bt == T_VALUETYPE) { 273 Symbol* signature = fd.signature(); 274 JavaThread* THREAD = JavaThread::current(); 275 oop loader = class_loader(); 276 oop domain = protection_domain(); 277 ResetNoHandleMark rnhm; 278 HandleMark hm; 279 NoSafepointVerifier nsv; 280 Klass* klass = SystemDictionary::resolve_or_null(signature, 281 Handle(THREAD, loader), Handle(THREAD, domain), 282 THREAD); 283 assert(klass != NULL && !HAS_PENDING_EXCEPTION, "lookup shouldn't fail"); 284 const GrowableArray<SigEntry>& embedded = ValueKlass::cast(klass)->collect_fields(offset); 285 sig_extended.appendAll(&embedded); 286 } else { 287 sig_extended.push(SigEntry(bt, offset)); 288 if (bt == T_LONG || bt == T_DOUBLE) { 289 sig_extended.push(SigEntry(T_VOID, offset)); 290 } 291 } 292 } 293 int offset = base_off + size_helper()*HeapWordSize - (base_off > 0 ? first_field_offset() : 0); 294 sig_extended.push(SigEntry(T_VOID, offset)); // hack: use T_VOID to mark end of value type fields 295 if (base_off == 0) { 296 sig_extended.sort(SigEntry::compare); 297 } 298 assert(sig_extended.at(0)._bt == T_VALUETYPE && sig_extended.at(sig_extended.length()-1)._bt == T_VOID, "broken structure"); 299 return sig_extended; 300 } 301 302 // Returns the basic types and registers for fields to return an 303 // instance of this value type in registers if possible. 304 GrowableArray<SigEntry> ValueKlass::return_convention(VMRegPair*& regs, int& nb_fields) const { 305 assert(ValueTypeReturnedAsFields, "inconsistent"); 306 const GrowableArray<SigEntry>& sig_vk = collect_fields(); 307 nb_fields = SigEntry::count_fields(sig_vk)+1; 308 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, nb_fields); 309 sig_bt[0] = T_METADATA; 310 SigEntry::fill_sig_bt(sig_vk, sig_bt+1, nb_fields-1, true); 311 regs = NEW_RESOURCE_ARRAY(VMRegPair, nb_fields); 312 int total = SharedRuntime::java_return_convention(sig_bt, regs, nb_fields); 313 314 if (total <= 0) { 315 regs = NULL; 316 } 317 318 return sig_vk; 319 } 320 321 // Create handles for all oop fields returned in registers that are 322 // going to be live across a safepoint. 323 bool ValueKlass::save_oop_results(RegisterMap& reg_map, GrowableArray<Handle>& handles) const { 324 if (ValueTypeReturnedAsFields) { 325 int nb_fields; 326 VMRegPair* regs; 327 const GrowableArray<SigEntry>& sig_vk = return_convention(regs, nb_fields); 328 329 if (regs != NULL) { 330 regs++; 331 nb_fields--; 332 save_oop_fields(sig_vk, reg_map, regs, handles, nb_fields); 333 return true; 334 } 335 } 336 return false; 337 } 338 339 // Same as above but with pre-computed return convention 340 void ValueKlass::save_oop_fields(const GrowableArray<SigEntry>& sig_vk, RegisterMap& reg_map, const VMRegPair* regs, GrowableArray<Handle>& handles, int nb_fields) const { 341 int j = 0; 342 Thread* thread = Thread::current(); 343 for (int i = 0; i < sig_vk.length(); i++) { 344 BasicType bt = sig_vk.at(i)._bt; 345 if (bt == T_OBJECT || bt == T_ARRAY) { 346 int off = sig_vk.at(i)._offset; 347 VMRegPair pair = regs[j]; 348 address loc = reg_map.location(pair.first()); 349 oop v = *(oop*)loc; 350 assert(v == NULL || v->is_oop(), "not an oop?"); 351 assert(Universe::heap()->is_in_or_null(v), "must be heap pointer"); 352 handles.push(Handle(thread, v)); 353 } 354 if (bt == T_VALUETYPE) { 355 continue; 356 } 357 if (bt == T_VOID && 358 sig_vk.at(i-1)._bt != T_LONG && 359 sig_vk.at(i-1)._bt != T_DOUBLE) { 360 continue; 361 } 362 j++; 363 } 364 assert(j == nb_fields, "missed a field?"); 365 } 366 367 // Update oop fields in registers from handles after a safepoint 368 void ValueKlass::restore_oop_results(RegisterMap& reg_map, GrowableArray<Handle>& handles) const { 369 assert(ValueTypeReturnedAsFields, "inconsistent"); 370 int nb_fields; 371 VMRegPair* regs; 372 const GrowableArray<SigEntry>& sig_vk = return_convention(regs, nb_fields); 373 assert(regs != NULL, "inconsistent"); 374 375 regs++; 376 nb_fields--; 377 378 int j = 0; 379 for (int i = 0, k = 0; i < sig_vk.length(); i++) { 380 BasicType bt = sig_vk.at(i)._bt; 381 if (bt == T_OBJECT || bt == T_ARRAY) { 382 int off = sig_vk.at(i)._offset; 383 VMRegPair pair = regs[j]; 384 address loc = reg_map.location(pair.first()); 385 *(oop*)loc = handles.at(k++)(); 386 } 387 if (bt == T_VALUETYPE) { 388 continue; 389 } 390 if (bt == T_VOID && 391 sig_vk.at(i-1)._bt != T_LONG && 392 sig_vk.at(i-1)._bt != T_DOUBLE) { 393 continue; 394 } 395 j++; 396 } 397 assert(j == nb_fields, "missed a field?"); 398 } 399 400 // Fields are in registers. Create an instance of the value type and 401 // initialize it with the values of the fields. 402 oop ValueKlass::realloc_result(const GrowableArray<SigEntry>& sig_vk, const RegisterMap& reg_map, const VMRegPair* regs, 403 const GrowableArray<Handle>& handles, int nb_fields, TRAPS) { 404 oop new_vt = allocate_instance(CHECK_NULL); 405 406 int j = 0; 407 int k = 0; 408 for (int i = 0; i < sig_vk.length(); i++) { 409 BasicType bt = sig_vk.at(i)._bt; 410 if (bt == T_VALUETYPE) { 411 continue; 412 } 413 if (bt == T_VOID) { 414 if (sig_vk.at(i-1)._bt == T_LONG || 415 sig_vk.at(i-1)._bt == T_DOUBLE) { 416 j++; 417 } 418 continue; 419 } 420 int off = sig_vk.at(i)._offset; 421 VMRegPair pair = regs[j]; 422 address loc = reg_map.location(pair.first()); 423 switch(bt) { 424 case T_BOOLEAN: { 425 jboolean v = *(intptr_t*)loc; 426 *(jboolean*)((address)new_vt + off) = v; 427 break; 428 } 429 case T_CHAR: { 430 jchar v = *(intptr_t*)loc; 431 *(jchar*)((address)new_vt + off) = v; 432 break; 433 } 434 case T_BYTE: { 435 jbyte v = *(intptr_t*)loc; 436 *(jbyte*)((address)new_vt + off) = v; 437 break; 438 } 439 case T_SHORT: { 440 jshort v = *(intptr_t*)loc; 441 *(jshort*)((address)new_vt + off) = v; 442 break; 443 } 444 case T_INT: { 445 jint v = *(intptr_t*)loc; 446 *(jint*)((address)new_vt + off) = v; 447 break; 448 } 449 case T_LONG: { 450 #ifdef _LP64 451 jlong v = *(intptr_t*)loc; 452 *(jlong*)((address)new_vt + off) = v; 453 #else 454 Unimplemented(); 455 #endif 456 break; 457 } 458 case T_OBJECT: 459 case T_ARRAY: { 460 Handle handle = handles.at(k++); 461 oop v = handle(); 462 if (!UseCompressedOops) { 463 oop* p = (oop*)((address)new_vt + off); 464 oopDesc::store_heap_oop(p, v); 465 } else { 466 narrowOop* p = (narrowOop*)((address)new_vt + off); 467 oopDesc::encode_store_heap_oop(p, v); 468 } 469 break; 470 } 471 case T_FLOAT: { 472 jfloat v = *(jfloat*)loc; 473 *(jfloat*)((address)new_vt + off) = v; 474 break; 475 } 476 case T_DOUBLE: { 477 jdouble v = *(jdouble*)loc; 478 *(jdouble*)((address)new_vt + off) = v; 479 break; 480 } 481 default: 482 ShouldNotReachHere(); 483 } 484 j++; 485 } 486 assert(j == nb_fields, "missed a field?"); 487 assert(k == handles.length(), "missed an oop?"); 488 return new_vt; 489 } 490 491 ValueKlass* ValueKlass::returned_value_type(const RegisterMap& map) { 492 BasicType bt = T_METADATA; 493 VMRegPair pair; 494 int nb = SharedRuntime::java_return_convention(&bt, &pair, 1); 495 assert(nb == 1, "broken"); 496 497 address loc = map.location(pair.first()); 498 intptr_t ptr = *(intptr_t*)loc; 499 if (Universe::heap()->is_in_reserved((void*)ptr)) { 500 return NULL; 501 } 502 return (ValueKlass*)ptr; 503 }