1 /* 2 * Copyright (c) 1997, 2024, 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 "cds/archiveBuilder.hpp" 27 #include "cds/archiveHeapLoader.hpp" 28 #include "cds/cdsConfig.hpp" 29 #include "cds/heapShared.hpp" 30 #include "cds/metaspaceShared.hpp" 31 #include "classfile/altHashing.hpp" 32 #include "classfile/classLoaderData.inline.hpp" 33 #include "classfile/javaClasses.inline.hpp" 34 #include "classfile/javaClassesImpl.hpp" 35 #include "classfile/javaThreadStatus.hpp" 36 #include "classfile/moduleEntry.hpp" 37 #include "classfile/stringTable.hpp" 38 #include "classfile/symbolTable.hpp" 39 #include "classfile/systemDictionary.hpp" 40 #include "classfile/vmClasses.hpp" 41 #include "classfile/vmSymbols.hpp" 42 #include "code/debugInfo.hpp" 43 #include "code/dependencyContext.hpp" 44 #include "code/pcDesc.hpp" 45 #include "gc/shared/collectedHeap.inline.hpp" 46 #include "interpreter/interpreter.hpp" 47 #include "interpreter/linkResolver.hpp" 48 #include "jvm.h" 49 #include "logging/log.hpp" 50 #include "logging/logStream.hpp" 51 #include "memory/oopFactory.hpp" 52 #include "memory/resourceArea.hpp" 53 #include "memory/universe.hpp" 54 #include "oops/fieldInfo.hpp" 55 #include "oops/fieldStreams.inline.hpp" 56 #include "oops/instanceKlass.inline.hpp" 57 #include "oops/instanceMirrorKlass.hpp" 58 #include "oops/klass.hpp" 59 #include "oops/klass.inline.hpp" 60 #include "oops/method.inline.hpp" 61 #include "oops/objArrayKlass.hpp" 62 #include "oops/objArrayOop.inline.hpp" 63 #include "oops/oopCast.inline.hpp" 64 #include "oops/oop.inline.hpp" 65 #include "oops/symbol.hpp" 66 #include "oops/recordComponent.hpp" 67 #include "oops/typeArrayOop.inline.hpp" 68 #include "prims/jvmtiExport.hpp" 69 #include "prims/methodHandles.hpp" 70 #include "prims/resolvedMethodTable.hpp" 71 #include "runtime/continuationEntry.inline.hpp" 72 #include "runtime/continuationJavaClasses.inline.hpp" 73 #include "runtime/fieldDescriptor.inline.hpp" 74 #include "runtime/frame.inline.hpp" 75 #include "runtime/handles.inline.hpp" 76 #include "runtime/handshake.hpp" 77 #include "runtime/init.hpp" 78 #include "runtime/interfaceSupport.inline.hpp" 79 #include "runtime/java.hpp" 80 #include "runtime/javaCalls.hpp" 81 #include "runtime/javaThread.hpp" 82 #include "runtime/jniHandles.inline.hpp" 83 #include "runtime/reflectionUtils.hpp" 84 #include "runtime/safepoint.hpp" 85 #include "runtime/safepointVerifiers.hpp" 86 #include "runtime/threadSMR.hpp" 87 #include "runtime/vframe.inline.hpp" 88 #include "runtime/vm_version.hpp" 89 #include "utilities/align.hpp" 90 #include "utilities/globalDefinitions.hpp" 91 #include "utilities/growableArray.hpp" 92 #include "utilities/preserveException.hpp" 93 #include "utilities/utf8.hpp" 94 #if INCLUDE_JVMCI 95 #include "jvmci/jvmciJavaClasses.hpp" 96 #endif 97 98 #define DECLARE_INJECTED_FIELD(klass, name, signature, may_be_java) \ 99 { VM_CLASS_ID(klass), VM_SYMBOL_ENUM_NAME(name##_name), VM_SYMBOL_ENUM_NAME(signature), may_be_java }, 100 101 InjectedField JavaClasses::_injected_fields[] = { 102 ALL_INJECTED_FIELDS(DECLARE_INJECTED_FIELD) 103 }; 104 105 // Register native methods of Object 106 void java_lang_Object::register_natives(TRAPS) { 107 InstanceKlass* obj = vmClasses::Object_klass(); 108 Method::register_native(obj, vmSymbols::hashCode_name(), 109 vmSymbols::void_int_signature(), (address) &JVM_IHashCode, CHECK); 110 Method::register_native(obj, vmSymbols::wait_name(), 111 vmSymbols::long_void_signature(), (address) &JVM_MonitorWait, CHECK); 112 Method::register_native(obj, vmSymbols::notify_name(), 113 vmSymbols::void_method_signature(), (address) &JVM_MonitorNotify, CHECK); 114 Method::register_native(obj, vmSymbols::notifyAll_name(), 115 vmSymbols::void_method_signature(), (address) &JVM_MonitorNotifyAll, CHECK); 116 Method::register_native(obj, vmSymbols::clone_name(), 117 vmSymbols::void_object_signature(), (address) &JVM_Clone, THREAD); 118 } 119 120 int JavaClasses::compute_injected_offset(InjectedFieldID id) { 121 return _injected_fields[(int)id].compute_offset(); 122 } 123 124 InjectedField* JavaClasses::get_injected(Symbol* class_name, int* field_count) { 125 *field_count = 0; 126 127 vmSymbolID sid = vmSymbols::find_sid(class_name); 128 if (sid == vmSymbolID::NO_SID) { 129 // Only well known classes can inject fields 130 return nullptr; 131 } 132 133 int count = 0; 134 int start = -1; 135 136 #define LOOKUP_INJECTED_FIELD(klass, name, signature, may_be_java) \ 137 if (sid == VM_SYMBOL_ENUM_NAME(klass)) { \ 138 count++; \ 139 if (start == -1) { \ 140 start = (int)InjectedFieldID::klass##_##name##_enum; \ 141 } \ 142 } 143 ALL_INJECTED_FIELDS(LOOKUP_INJECTED_FIELD); 144 #undef LOOKUP_INJECTED_FIELD 145 146 if (start != -1) { 147 *field_count = count; 148 return _injected_fields + start; 149 } 150 return nullptr; 151 } 152 153 154 // Helpful routine for computing field offsets at run time rather than hardcoding them 155 // Finds local fields only, including static fields. Static field offsets are from the 156 // beginning of the mirror. 157 void JavaClasses::compute_offset(int &dest_offset, 158 InstanceKlass* ik, Symbol* name_symbol, Symbol* signature_symbol, 159 bool is_static) { 160 fieldDescriptor fd; 161 if (ik == nullptr) { 162 ResourceMark rm; 163 log_error(class)("Mismatch JDK version for field: %s type: %s", name_symbol->as_C_string(), signature_symbol->as_C_string()); 164 vm_exit_during_initialization("Invalid layout of well-known class"); 165 } 166 167 if (!ik->find_local_field(name_symbol, signature_symbol, &fd) || fd.is_static() != is_static) { 168 ResourceMark rm; 169 log_error(class)("Invalid layout of %s field: %s type: %s", ik->external_name(), 170 name_symbol->as_C_string(), signature_symbol->as_C_string()); 171 #ifndef PRODUCT 172 // Prints all fields and offsets 173 Log(class) lt; 174 LogStream ls(lt.error()); 175 ik->print_on(&ls); 176 #endif //PRODUCT 177 vm_exit_during_initialization("Invalid layout of well-known class: use -Xlog:class+load=info to see the origin of the problem class"); 178 } 179 dest_offset = fd.offset(); 180 } 181 182 // Overloading to pass name as a string. 183 void JavaClasses::compute_offset(int& dest_offset, InstanceKlass* ik, 184 const char* name_string, Symbol* signature_symbol, 185 bool is_static) { 186 TempNewSymbol name = SymbolTable::probe(name_string, (int)strlen(name_string)); 187 if (name == nullptr) { 188 ResourceMark rm; 189 log_error(class)("Name %s should be in the SymbolTable since its class is loaded", name_string); 190 vm_exit_during_initialization("Invalid layout of well-known class", ik->external_name()); 191 } 192 compute_offset(dest_offset, ik, name, signature_symbol, is_static); 193 } 194 195 // java_lang_String 196 197 int java_lang_String::_value_offset; 198 int java_lang_String::_hash_offset; 199 int java_lang_String::_hashIsZero_offset; 200 int java_lang_String::_coder_offset; 201 int java_lang_String::_flags_offset; 202 203 bool java_lang_String::_initialized; 204 205 206 bool java_lang_String::test_and_set_flag(oop java_string, uint8_t flag_mask) { 207 uint8_t* addr = flags_addr(java_string); 208 uint8_t value = Atomic::load(addr); 209 while ((value & flag_mask) == 0) { 210 uint8_t old_value = value; 211 value |= flag_mask; 212 value = Atomic::cmpxchg(addr, old_value, value); 213 if (value == old_value) return false; // Flag bit changed from 0 to 1. 214 } 215 return true; // Flag bit is already 1. 216 } 217 218 #define STRING_FIELDS_DO(macro) \ 219 macro(_value_offset, k, vmSymbols::value_name(), byte_array_signature, false); \ 220 macro(_hash_offset, k, "hash", int_signature, false); \ 221 macro(_hashIsZero_offset, k, "hashIsZero", bool_signature, false); \ 222 macro(_coder_offset, k, "coder", byte_signature, false); 223 224 void java_lang_String::compute_offsets() { 225 if (_initialized) { 226 return; 227 } 228 229 InstanceKlass* k = vmClasses::String_klass(); 230 STRING_FIELDS_DO(FIELD_COMPUTE_OFFSET); 231 STRING_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 232 233 _initialized = true; 234 } 235 236 #if INCLUDE_CDS 237 void java_lang_String::serialize_offsets(SerializeClosure* f) { 238 STRING_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 239 STRING_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 240 f->do_bool(&_initialized); 241 } 242 #endif 243 244 class CompactStringsFixup : public FieldClosure { 245 private: 246 bool _value; 247 248 public: 249 CompactStringsFixup(bool value) : _value(value) {} 250 251 void do_field(fieldDescriptor* fd) { 252 if (fd->name() == vmSymbols::compact_strings_name()) { 253 oop mirror = fd->field_holder()->java_mirror(); 254 assert(fd->field_holder() == vmClasses::String_klass(), "Should be String"); 255 assert(mirror != nullptr, "String must have mirror already"); 256 mirror->bool_field_put(fd->offset(), _value); 257 } 258 } 259 }; 260 261 void java_lang_String::set_compact_strings(bool value) { 262 CompactStringsFixup fix(value); 263 vmClasses::String_klass()->do_local_static_fields(&fix); 264 } 265 266 Handle java_lang_String::basic_create(int length, bool is_latin1, TRAPS) { 267 assert(_initialized, "Must be initialized"); 268 assert(CompactStrings || !is_latin1, "Must be UTF16 without CompactStrings"); 269 270 // Create the String object first, so there's a chance that the String 271 // and the char array it points to end up in the same cache line. 272 oop obj; 273 obj = vmClasses::String_klass()->allocate_instance(CHECK_NH); 274 275 // Create the char array. The String object must be handlized here 276 // because GC can happen as a result of the allocation attempt. 277 Handle h_obj(THREAD, obj); 278 int arr_length = is_latin1 ? length : length << 1; // 2 bytes per UTF16. 279 typeArrayOop buffer = oopFactory::new_byteArray(arr_length, CHECK_NH);; 280 281 // Point the String at the char array 282 obj = h_obj(); 283 set_value(obj, buffer); 284 // No need to zero the offset, allocation zero'ed the entire String object 285 set_coder(obj, is_latin1 ? CODER_LATIN1 : CODER_UTF16); 286 return h_obj; 287 } 288 289 Handle java_lang_String::create_from_unicode(const jchar* unicode, int length, TRAPS) { 290 bool is_latin1 = CompactStrings && UNICODE::is_latin1(unicode, length); 291 Handle h_obj = basic_create(length, is_latin1, CHECK_NH); 292 typeArrayOop buffer = value(h_obj()); 293 assert(TypeArrayKlass::cast(buffer->klass())->element_type() == T_BYTE, "only byte[]"); 294 if (is_latin1) { 295 for (int index = 0; index < length; index++) { 296 buffer->byte_at_put(index, (jbyte)unicode[index]); 297 } 298 } else { 299 for (int index = 0; index < length; index++) { 300 buffer->char_at_put(index, unicode[index]); 301 } 302 } 303 304 #ifdef ASSERT 305 { 306 ResourceMark rm; 307 size_t utf8_len = static_cast<size_t>(length); 308 char* expected = UNICODE::as_utf8(unicode, utf8_len); 309 char* actual = as_utf8_string(h_obj()); 310 if (strcmp(expected, actual) != 0) { 311 fatal("Unicode conversion failure: %s --> %s", expected, actual); 312 } 313 } 314 #endif 315 316 return h_obj; 317 } 318 319 oop java_lang_String::create_oop_from_unicode(const jchar* unicode, int length, TRAPS) { 320 Handle h_obj = create_from_unicode(unicode, length, CHECK_NULL); 321 return h_obj(); 322 } 323 324 Handle java_lang_String::create_from_str(const char* utf8_str, TRAPS) { 325 if (utf8_str == nullptr) { 326 return Handle(); 327 } 328 bool has_multibyte, is_latin1; 329 int length = UTF8::unicode_length(utf8_str, is_latin1, has_multibyte); 330 if (!CompactStrings) { 331 has_multibyte = true; 332 is_latin1 = false; 333 } 334 335 Handle h_obj = basic_create(length, is_latin1, CHECK_NH); 336 if (length > 0) { 337 if (!has_multibyte) { 338 const jbyte* src = reinterpret_cast<const jbyte*>(utf8_str); 339 ArrayAccess<>::arraycopy_from_native(src, value(h_obj()), typeArrayOopDesc::element_offset<jbyte>(0), length); 340 } else if (is_latin1) { 341 UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length); 342 } else { 343 UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length); 344 } 345 } 346 347 #ifdef ASSERT 348 // This check is too strict when the input string is not a valid UTF8. 349 // For example, it may be created with arbitrary content via jni_NewStringUTF. 350 if (UTF8::is_legal_utf8((const unsigned char*)utf8_str, strlen(utf8_str), /*version_leq_47*/false)) { 351 ResourceMark rm; 352 const char* expected = utf8_str; 353 char* actual = as_utf8_string(h_obj()); 354 if (strcmp(expected, actual) != 0) { 355 fatal("String conversion failure: %s --> %s", expected, actual); 356 } 357 } 358 #endif 359 360 return h_obj; 361 } 362 363 oop java_lang_String::create_oop_from_str(const char* utf8_str, TRAPS) { 364 Handle h_obj = create_from_str(utf8_str, CHECK_NULL); 365 return h_obj(); 366 } 367 368 Handle java_lang_String::create_from_symbol(const Symbol* symbol, TRAPS) { 369 const char* utf8_str = (char*)symbol->bytes(); 370 int utf8_len = symbol->utf8_length(); 371 372 bool has_multibyte, is_latin1; 373 int length = UTF8::unicode_length(utf8_str, utf8_len, is_latin1, has_multibyte); 374 if (!CompactStrings) { 375 has_multibyte = true; 376 is_latin1 = false; 377 } 378 379 Handle h_obj = basic_create(length, is_latin1, CHECK_NH); 380 if (length > 0) { 381 if (!has_multibyte) { 382 const jbyte* src = reinterpret_cast<const jbyte*>(utf8_str); 383 ArrayAccess<>::arraycopy_from_native(src, value(h_obj()), typeArrayOopDesc::element_offset<jbyte>(0), length); 384 } else if (is_latin1) { 385 UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length); 386 } else { 387 UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length); 388 } 389 } 390 391 #ifdef ASSERT 392 // This check is too strict on older classfile versions 393 if (UTF8::is_legal_utf8((const unsigned char*)utf8_str, utf8_len, /*version_leq_47*/false)) 394 { 395 ResourceMark rm; 396 const char* expected = symbol->as_utf8(); 397 char* actual = as_utf8_string(h_obj()); 398 if (strncmp(expected, actual, utf8_len) != 0) { 399 fatal("Symbol conversion failure: %s --> %s", expected, actual); 400 } 401 } 402 #endif 403 404 return h_obj; 405 } 406 407 // Converts a C string to a Java String based on current encoding 408 Handle java_lang_String::create_from_platform_dependent_str(const char* str, TRAPS) { 409 assert(str != nullptr, "bad arguments"); 410 411 typedef jstring (JNICALL *to_java_string_fn_t)(JNIEnv*, const char *); 412 static to_java_string_fn_t _to_java_string_fn = nullptr; 413 414 if (_to_java_string_fn == nullptr) { 415 void *lib_handle = os::native_java_library(); 416 _to_java_string_fn = CAST_TO_FN_PTR(to_java_string_fn_t, os::dll_lookup(lib_handle, "JNU_NewStringPlatform")); 417 if (_to_java_string_fn == nullptr) { 418 fatal("JNU_NewStringPlatform missing"); 419 } 420 } 421 422 jstring js = nullptr; 423 { 424 JavaThread* thread = THREAD; 425 HandleMark hm(thread); 426 ThreadToNativeFromVM ttn(thread); 427 js = (_to_java_string_fn)(thread->jni_environment(), str); 428 } 429 430 Handle native_platform_string(THREAD, JNIHandles::resolve(js)); 431 JNIHandles::destroy_local(js); // destroy local JNIHandle. 432 return native_platform_string; 433 } 434 435 // Converts a Java String to a native C string that can be used for 436 // native OS calls. 437 char* java_lang_String::as_platform_dependent_str(Handle java_string, TRAPS) { 438 typedef char* (*to_platform_string_fn_t)(JNIEnv*, jstring, bool*); 439 static to_platform_string_fn_t _to_platform_string_fn = nullptr; 440 441 if (_to_platform_string_fn == nullptr) { 442 void *lib_handle = os::native_java_library(); 443 _to_platform_string_fn = CAST_TO_FN_PTR(to_platform_string_fn_t, os::dll_lookup(lib_handle, "GetStringPlatformChars")); 444 if (_to_platform_string_fn == nullptr) { 445 fatal("GetStringPlatformChars missing"); 446 } 447 } 448 449 char *native_platform_string; 450 jstring js; 451 { JavaThread* thread = THREAD; 452 js = (jstring) JNIHandles::make_local(thread, java_string()); 453 HandleMark hm(thread); 454 ThreadToNativeFromVM ttn(thread); 455 JNIEnv *env = thread->jni_environment(); 456 bool is_copy; 457 native_platform_string = (_to_platform_string_fn)(env, js, &is_copy); 458 assert(is_copy == JNI_TRUE, "is_copy value changed"); 459 } 460 461 // Uses a store barrier and therefore needs to be in vm state 462 JNIHandles::destroy_local(js); 463 464 return native_platform_string; 465 } 466 467 Handle java_lang_String::externalize_classname(Symbol* java_name, TRAPS) { 468 ResourceMark rm(THREAD); 469 return create_from_str(java_name->as_klass_external_name(), THREAD); 470 } 471 472 jchar* java_lang_String::as_unicode_string(oop java_string, int& length, TRAPS) { 473 jchar* result = as_unicode_string_or_null(java_string, length); 474 if (result == nullptr) { 475 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "could not allocate Unicode string"); 476 } 477 return result; 478 } 479 480 jchar* java_lang_String::as_unicode_string_or_null(oop java_string, int& length) { 481 typeArrayOop value = java_lang_String::value(java_string); 482 length = java_lang_String::length(java_string, value); 483 bool is_latin1 = java_lang_String::is_latin1(java_string); 484 485 jchar* result = NEW_RESOURCE_ARRAY_RETURN_NULL(jchar, length); 486 if (result != nullptr) { 487 if (!is_latin1) { 488 for (int index = 0; index < length; index++) { 489 result[index] = value->char_at(index); 490 } 491 } else { 492 for (int index = 0; index < length; index++) { 493 result[index] = ((jchar) value->byte_at(index)) & 0xff; 494 } 495 } 496 } 497 return result; 498 } 499 500 inline unsigned int java_lang_String::hash_code_impl(oop java_string, bool update) { 501 // The hash and hashIsZero fields are subject to a benign data race, 502 // making it crucial to ensure that any observable result of the 503 // calculation in this method stays correct under any possible read of 504 // these fields. Necessary restrictions to allow this to be correct 505 // without explicit memory fences or similar concurrency primitives is 506 // that we can ever only write to one of these two fields for a given 507 // String instance, and that the computation is idempotent and derived 508 // from immutable state 509 assert(_initialized && (_hash_offset > 0) && (_hashIsZero_offset > 0), "Must be initialized"); 510 if (java_lang_String::hash_is_set(java_string)) { 511 return java_string->int_field(_hash_offset); 512 } 513 514 typeArrayOop value = java_lang_String::value(java_string); 515 int length = java_lang_String::length(java_string, value); 516 bool is_latin1 = java_lang_String::is_latin1(java_string); 517 518 unsigned int hash = 0; 519 if (length > 0) { 520 if (is_latin1) { 521 hash = java_lang_String::hash_code(value->byte_at_addr(0), length); 522 } else { 523 hash = java_lang_String::hash_code(value->char_at_addr(0), length); 524 } 525 } 526 527 if (update) { 528 if (hash != 0) { 529 java_string->int_field_put(_hash_offset, hash); 530 } else { 531 java_string->bool_field_put(_hashIsZero_offset, true); 532 } 533 } 534 return hash; 535 } 536 537 unsigned int java_lang_String::hash_code(oop java_string) { 538 return hash_code_impl(java_string, /*update=*/true); 539 } 540 541 unsigned int java_lang_String::hash_code_noupdate(oop java_string) { 542 return hash_code_impl(java_string, /*update=*/false); 543 } 544 545 546 char* java_lang_String::as_quoted_ascii(oop java_string) { 547 typeArrayOop value = java_lang_String::value(java_string); 548 int length = java_lang_String::length(java_string, value); 549 bool is_latin1 = java_lang_String::is_latin1(java_string); 550 551 if (length == 0) return nullptr; 552 553 char* result; 554 size_t result_length; 555 if (!is_latin1) { 556 jchar* base = value->char_at_addr(0); 557 result_length = UNICODE::quoted_ascii_length(base, length) + 1; 558 result = NEW_RESOURCE_ARRAY(char, result_length); 559 UNICODE::as_quoted_ascii(base, length, result, result_length); 560 } else { 561 jbyte* base = value->byte_at_addr(0); 562 result_length = UNICODE::quoted_ascii_length(base, length) + 1; 563 result = NEW_RESOURCE_ARRAY(char, result_length); 564 UNICODE::as_quoted_ascii(base, length, result, result_length); 565 } 566 assert(result_length >= (size_t)length + 1, "must not be shorter"); 567 assert(result_length == strlen(result) + 1, "must match"); 568 return result; 569 } 570 571 Symbol* java_lang_String::as_symbol(oop java_string) { 572 typeArrayOop value = java_lang_String::value(java_string); 573 int length = java_lang_String::length(java_string, value); 574 bool is_latin1 = java_lang_String::is_latin1(java_string); 575 if (!is_latin1) { 576 jchar* base = (length == 0) ? nullptr : value->char_at_addr(0); 577 Symbol* sym = SymbolTable::new_symbol(base, length); 578 return sym; 579 } else { 580 ResourceMark rm; 581 jbyte* position = (length == 0) ? nullptr : value->byte_at_addr(0); 582 size_t utf8_len = static_cast<size_t>(length); 583 const char* base = UNICODE::as_utf8(position, utf8_len); 584 Symbol* sym = SymbolTable::new_symbol(base, checked_cast<int>(utf8_len)); 585 return sym; 586 } 587 } 588 589 Symbol* java_lang_String::as_symbol_or_null(oop java_string) { 590 typeArrayOop value = java_lang_String::value(java_string); 591 int length = java_lang_String::length(java_string, value); 592 bool is_latin1 = java_lang_String::is_latin1(java_string); 593 if (!is_latin1) { 594 jchar* base = (length == 0) ? nullptr : value->char_at_addr(0); 595 return SymbolTable::probe_unicode(base, length); 596 } else { 597 ResourceMark rm; 598 jbyte* position = (length == 0) ? nullptr : value->byte_at_addr(0); 599 size_t utf8_len = static_cast<size_t>(length); 600 const char* base = UNICODE::as_utf8(position, utf8_len); 601 return SymbolTable::probe(base, checked_cast<int>(utf8_len)); 602 } 603 } 604 605 size_t java_lang_String::utf8_length(oop java_string, typeArrayOop value) { 606 assert(value_equals(value, java_lang_String::value(java_string)), 607 "value must be same as java_lang_String::value(java_string)"); 608 int length = java_lang_String::length(java_string, value); 609 if (length == 0) { 610 return 0; 611 } 612 if (!java_lang_String::is_latin1(java_string)) { 613 return UNICODE::utf8_length(value->char_at_addr(0), length); 614 } else { 615 return UNICODE::utf8_length(value->byte_at_addr(0), length); 616 } 617 } 618 619 size_t java_lang_String::utf8_length(oop java_string) { 620 typeArrayOop value = java_lang_String::value(java_string); 621 return utf8_length(java_string, value); 622 } 623 624 int java_lang_String::utf8_length_as_int(oop java_string) { 625 typeArrayOop value = java_lang_String::value(java_string); 626 return utf8_length_as_int(java_string, value); 627 } 628 629 int java_lang_String::utf8_length_as_int(oop java_string, typeArrayOop value) { 630 assert(value_equals(value, java_lang_String::value(java_string)), 631 "value must be same as java_lang_String::value(java_string)"); 632 int length = java_lang_String::length(java_string, value); 633 if (length == 0) { 634 return 0; 635 } 636 if (!java_lang_String::is_latin1(java_string)) { 637 return UNICODE::utf8_length_as_int(value->char_at_addr(0), length); 638 } else { 639 return UNICODE::utf8_length_as_int(value->byte_at_addr(0), length); 640 } 641 } 642 643 char* java_lang_String::as_utf8_string(oop java_string) { 644 size_t length; 645 return as_utf8_string(java_string, length); 646 } 647 648 char* java_lang_String::as_utf8_string(oop java_string, size_t& length) { 649 typeArrayOop value = java_lang_String::value(java_string); 650 // `length` is used as the incoming number of characters to 651 // convert, and then set as the number of bytes in the UTF8 sequence. 652 length = java_lang_String::length(java_string, value); 653 bool is_latin1 = java_lang_String::is_latin1(java_string); 654 if (!is_latin1) { 655 jchar* position = (length == 0) ? nullptr : value->char_at_addr(0); 656 return UNICODE::as_utf8(position, length); 657 } else { 658 jbyte* position = (length == 0) ? nullptr : value->byte_at_addr(0); 659 return UNICODE::as_utf8(position, length); 660 } 661 } 662 663 // Uses a provided buffer if it's sufficiently large, otherwise allocates 664 // a resource array to fit 665 char* java_lang_String::as_utf8_string_full(oop java_string, char* buf, size_t buflen, size_t& utf8_len) { 666 typeArrayOop value = java_lang_String::value(java_string); 667 int len = java_lang_String::length(java_string, value); 668 bool is_latin1 = java_lang_String::is_latin1(java_string); 669 if (!is_latin1) { 670 jchar *position = (len == 0) ? nullptr : value->char_at_addr(0); 671 utf8_len = UNICODE::utf8_length(position, len); 672 if (utf8_len >= buflen) { 673 buf = NEW_RESOURCE_ARRAY(char, utf8_len + 1); 674 } 675 return UNICODE::as_utf8(position, len, buf, utf8_len + 1); 676 } else { 677 jbyte *position = (len == 0) ? nullptr : value->byte_at_addr(0); 678 utf8_len = UNICODE::utf8_length(position, len); 679 if (utf8_len >= buflen) { 680 buf = NEW_RESOURCE_ARRAY(char, utf8_len + 1); 681 } 682 return UNICODE::as_utf8(position, len, buf, utf8_len + 1); 683 } 684 } 685 686 char* java_lang_String::as_utf8_string(oop java_string, typeArrayOop value, char* buf, size_t buflen) { 687 assert(value_equals(value, java_lang_String::value(java_string)), 688 "value must be same as java_lang_String::value(java_string)"); 689 int length = java_lang_String::length(java_string, value); 690 bool is_latin1 = java_lang_String::is_latin1(java_string); 691 if (!is_latin1) { 692 jchar* position = (length == 0) ? nullptr : value->char_at_addr(0); 693 return UNICODE::as_utf8(position, length, buf, buflen); 694 } else { 695 jbyte* position = (length == 0) ? nullptr : value->byte_at_addr(0); 696 return UNICODE::as_utf8(position, length, buf, buflen); 697 } 698 } 699 700 char* java_lang_String::as_utf8_string(oop java_string, char* buf, size_t buflen) { 701 typeArrayOop value = java_lang_String::value(java_string); 702 return as_utf8_string(java_string, value, buf, buflen); 703 } 704 705 char* java_lang_String::as_utf8_string(oop java_string, int start, int len) { 706 // `length` is used as the incoming number of characters to 707 // convert, and then set as the number of bytes in the UTF8 sequence. 708 size_t length = static_cast<size_t>(len); 709 typeArrayOop value = java_lang_String::value(java_string); 710 bool is_latin1 = java_lang_String::is_latin1(java_string); 711 assert(start + len <= java_lang_String::length(java_string), "just checking"); 712 if (!is_latin1) { 713 jchar* position = value->char_at_addr(start); 714 return UNICODE::as_utf8(position, length); 715 } else { 716 jbyte* position = value->byte_at_addr(start); 717 return UNICODE::as_utf8(position, length); 718 } 719 } 720 721 char* java_lang_String::as_utf8_string(oop java_string, typeArrayOop value, int start, int len, char* buf, size_t buflen) { 722 assert(value_equals(value, java_lang_String::value(java_string)), 723 "value must be same as java_lang_String::value(java_string)"); 724 assert(start + len <= java_lang_String::length(java_string), "just checking"); 725 bool is_latin1 = java_lang_String::is_latin1(java_string); 726 if (!is_latin1) { 727 jchar* position = value->char_at_addr(start); 728 return UNICODE::as_utf8(position, len, buf, buflen); 729 } else { 730 jbyte* position = value->byte_at_addr(start); 731 return UNICODE::as_utf8(position, len, buf, buflen); 732 } 733 } 734 735 bool java_lang_String::equals(oop java_string, const jchar* chars, int len) { 736 assert(java_string->klass() == vmClasses::String_klass(), 737 "must be java_string"); 738 typeArrayOop value = java_lang_String::value_no_keepalive(java_string); 739 int length = java_lang_String::length(java_string, value); 740 if (length != len) { 741 return false; 742 } 743 bool is_latin1 = java_lang_String::is_latin1(java_string); 744 if (!is_latin1) { 745 for (int i = 0; i < len; i++) { 746 if (value->char_at(i) != chars[i]) { 747 return false; 748 } 749 } 750 } else { 751 for (int i = 0; i < len; i++) { 752 if ((((jchar) value->byte_at(i)) & 0xff) != chars[i]) { 753 return false; 754 } 755 } 756 } 757 return true; 758 } 759 760 bool java_lang_String::equals(oop java_string, const char* utf8_string, size_t utf8_len) { 761 assert(java_string->klass() == vmClasses::String_klass(), 762 "must be java_string"); 763 typeArrayOop value = java_lang_String::value_no_keepalive(java_string); 764 int length = java_lang_String::length(java_string, value); 765 int unicode_length = UTF8::unicode_length(utf8_string, utf8_len); 766 if (length != unicode_length) { 767 return false; 768 } 769 bool is_latin1 = java_lang_String::is_latin1(java_string); 770 jchar c; 771 if (!is_latin1) { 772 for (int i = 0; i < unicode_length; i++) { 773 utf8_string = UTF8::next(utf8_string, &c); 774 if (value->char_at(i) != c) { 775 return false; 776 } 777 } 778 } else { 779 for (int i = 0; i < unicode_length; i++) { 780 utf8_string = UTF8::next(utf8_string, &c); 781 if ((((jchar) value->byte_at(i)) & 0xff) != c) { 782 return false; 783 } 784 } 785 } 786 return true; 787 } 788 789 bool java_lang_String::equals(oop str1, oop str2) { 790 assert(str1->klass() == vmClasses::String_klass(), 791 "must be java String"); 792 assert(str2->klass() == vmClasses::String_klass(), 793 "must be java String"); 794 typeArrayOop value1 = java_lang_String::value_no_keepalive(str1); 795 bool is_latin1 = java_lang_String::is_latin1(str1); 796 typeArrayOop value2 = java_lang_String::value_no_keepalive(str2); 797 bool is_latin2 = java_lang_String::is_latin1(str2); 798 799 if (is_latin1 != is_latin2) { 800 // Strings with different coders are never equal. 801 return false; 802 } 803 return value_equals(value1, value2); 804 } 805 806 // Print the given string to the given outputStream, limiting the output to 807 // at most max_length of the string's characters. If the length exceeds the 808 // limit we print an abridged version of the string with the "middle" elided 809 // and replaced by " ... (N characters ommitted) ... ". If max_length is odd 810 // it is treated as max_length-1. 811 void java_lang_String::print(oop java_string, outputStream* st, int max_length) { 812 assert(java_string->klass() == vmClasses::String_klass(), "must be java_string"); 813 // We need at least two characters to print A ... B 814 assert(max_length > 1, "invalid max_length: %d", max_length); 815 816 typeArrayOop value = java_lang_String::value_no_keepalive(java_string); 817 818 if (value == nullptr) { 819 // This can happen if, e.g., printing a String 820 // object before its initializer has been called 821 st->print("nullptr"); 822 return; 823 } 824 825 int length = java_lang_String::length(java_string, value); 826 bool is_latin1 = java_lang_String::is_latin1(java_string); 827 828 bool abridge = length > max_length; 829 830 st->print("\""); 831 for (int index = 0; index < length; index++) { 832 // If we need to abridge and we've printed half the allowed characters 833 // then jump to the tail of the string. 834 if (abridge && index >= max_length / 2) { 835 st->print(" ... (%d characters ommitted) ... ", length - 2 * (max_length / 2)); 836 index = length - (max_length / 2); 837 abridge = false; // only do this once 838 } 839 jchar c = (!is_latin1) ? value->char_at(index) : 840 ((jchar) value->byte_at(index)) & 0xff; 841 if (c < ' ') { 842 st->print("\\x%02X", c); // print control characters e.g. \x0A 843 } else { 844 st->print("%c", c); 845 } 846 } 847 st->print("\""); 848 849 if (length > max_length) { 850 st->print(" (abridged) "); 851 } 852 } 853 854 // java_lang_Class 855 856 int java_lang_Class::_klass_offset; 857 int java_lang_Class::_array_klass_offset; 858 int java_lang_Class::_oop_size_offset; 859 int java_lang_Class::_static_oop_field_count_offset; 860 int java_lang_Class::_class_loader_offset; 861 int java_lang_Class::_module_offset; 862 int java_lang_Class::_protection_domain_offset; 863 int java_lang_Class::_component_mirror_offset; 864 int java_lang_Class::_init_lock_offset; 865 int java_lang_Class::_signers_offset; 866 int java_lang_Class::_name_offset; 867 int java_lang_Class::_source_file_offset; 868 int java_lang_Class::_classData_offset; 869 int java_lang_Class::_classRedefinedCount_offset; 870 int java_lang_Class::_reflectionData_offset; 871 872 bool java_lang_Class::_offsets_computed = false; 873 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = nullptr; 874 GrowableArray<Klass*>* java_lang_Class::_fixup_module_field_list = nullptr; 875 876 #ifdef ASSERT 877 inline static void assert_valid_static_string_field(fieldDescriptor* fd) { 878 assert(fd->has_initial_value(), "caller should have checked this"); 879 assert(fd->field_type() == T_OBJECT, "caller should have checked this"); 880 assert(fd->signature() == vmSymbols::string_signature(), "just checking"); 881 } 882 #endif 883 884 static void initialize_static_string_field(fieldDescriptor* fd, Handle mirror, TRAPS) { 885 DEBUG_ONLY(assert_valid_static_string_field(fd);) 886 oop string = fd->string_initial_value(CHECK); 887 mirror()->obj_field_put(fd->offset(), string); 888 } 889 890 static void initialize_static_primitive_field(fieldDescriptor* fd, Handle mirror) { 891 assert(fd->has_initial_value(), "caller should have checked this"); 892 BasicType t = fd->field_type(); 893 switch (t) { 894 case T_BYTE: 895 mirror()->byte_field_put(fd->offset(), (jbyte)(fd->int_initial_value())); 896 break; 897 case T_BOOLEAN: 898 mirror()->bool_field_put(fd->offset(), (jboolean)(fd->int_initial_value())); 899 break; 900 case T_CHAR: 901 mirror()->char_field_put(fd->offset(), (jchar)(fd->int_initial_value())); 902 break; 903 case T_SHORT: 904 mirror()->short_field_put(fd->offset(), (jshort)(fd->int_initial_value())); 905 break; 906 case T_INT: 907 mirror()->int_field_put(fd->offset(), fd->int_initial_value()); 908 break; 909 case T_FLOAT: 910 mirror()->float_field_put(fd->offset(), fd->float_initial_value()); 911 break; 912 case T_DOUBLE: 913 mirror()->double_field_put(fd->offset(), fd->double_initial_value()); 914 break; 915 case T_LONG: 916 mirror()->long_field_put(fd->offset(), fd->long_initial_value()); 917 break; 918 default: 919 // Illegal ConstantValue attribute in class file should have been 920 // caught during classfile parsing. 921 ShouldNotReachHere(); 922 } 923 } 924 925 static void initialize_static_field(fieldDescriptor* fd, Handle mirror, TRAPS) { 926 assert(mirror.not_null() && fd->is_static(), "just checking"); 927 if (fd->has_initial_value()) { 928 if (fd->field_type() != T_OBJECT) { 929 initialize_static_primitive_field(fd, mirror); 930 } else { 931 initialize_static_string_field(fd, mirror, CHECK); 932 } 933 } 934 } 935 936 void java_lang_Class::fixup_mirror(Klass* k, TRAPS) { 937 assert(InstanceMirrorKlass::offset_of_static_fields() != 0, "must have been computed already"); 938 939 // If the offset was read from the shared archive, it was fixed up already 940 if (!k->is_shared()) { 941 if (k->is_instance_klass()) { 942 // During bootstrap, java.lang.Class wasn't loaded so static field 943 // offsets were computed without the size added it. Go back and 944 // update all the static field offsets to included the size. 945 946 // Unfortunately, the FieldInfo stream is encoded with UNSIGNED5 which doesn't allow 947 // content updates. So the FieldInfo stream has to be decompressed into a temporary array, 948 // static fields offsets are updated in this array before reencoding everything into 949 // a new UNSIGNED5 stream, and substitute it to the old FieldInfo stream. 950 951 int java_fields; 952 int injected_fields; 953 InstanceKlass* ik = InstanceKlass::cast(k); 954 GrowableArray<FieldInfo>* fields = 955 FieldInfoStream::create_FieldInfoArray(ik->fieldinfo_stream(), 956 &java_fields, &injected_fields); 957 for (int i = 0; i < fields->length(); i++) { 958 FieldInfo* fi = fields->adr_at(i); 959 if (fi->access_flags().is_static()) { 960 fi->set_offset(fi->offset() + InstanceMirrorKlass::offset_of_static_fields()); 961 } 962 } 963 Array<u1>* old_stream = ik->fieldinfo_stream(); 964 assert(fields->length() == (java_fields + injected_fields), "Must be"); 965 Array<u1>* new_fis = FieldInfoStream::create_FieldInfoStream(fields, java_fields, injected_fields, k->class_loader_data(), CHECK); 966 ik->set_fieldinfo_stream(new_fis); 967 MetadataFactory::free_array<u1>(k->class_loader_data(), old_stream); 968 } 969 } 970 971 if (k->is_shared() && k->has_archived_mirror_index()) { 972 if (ArchiveHeapLoader::is_in_use()) { 973 bool present = restore_archived_mirror(k, Handle(), Handle(), Handle(), CHECK); 974 assert(present, "Missing archived mirror for %s", k->external_name()); 975 return; 976 } else { 977 k->clear_java_mirror_handle(); 978 k->clear_archived_mirror_index(); 979 } 980 } 981 create_mirror(k, Handle(), Handle(), Handle(), Handle(), CHECK); 982 } 983 984 void java_lang_Class::initialize_mirror_fields(Klass* k, 985 Handle mirror, 986 Handle protection_domain, 987 Handle classData, 988 TRAPS) { 989 // Allocate a simple java object for a lock. 990 // This needs to be a java object because during class initialization 991 // it can be held across a java call. 992 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK); 993 set_init_lock(mirror(), r); 994 995 // Set protection domain also 996 set_protection_domain(mirror(), protection_domain()); 997 998 // Initialize static fields 999 InstanceKlass::cast(k)->do_local_static_fields(&initialize_static_field, mirror, CHECK); 1000 1001 // Set classData 1002 set_class_data(mirror(), classData()); 1003 } 1004 1005 // Set the java.lang.Module module field in the java_lang_Class mirror 1006 void java_lang_Class::set_mirror_module_field(JavaThread* current, Klass* k, Handle mirror, Handle module) { 1007 if (module.is_null()) { 1008 // During startup, the module may be null only if java.base has not been defined yet. 1009 // Put the class on the fixup_module_list to patch later when the java.lang.Module 1010 // for java.base is known. But note that since we captured the null module another 1011 // thread may have completed that initialization. 1012 1013 bool javabase_was_defined = false; 1014 { 1015 MutexLocker m1(current, Module_lock); 1016 // Keep list of classes needing java.base module fixup 1017 if (!ModuleEntryTable::javabase_defined()) { 1018 assert(k->java_mirror() != nullptr, "Class's mirror is null"); 1019 k->class_loader_data()->inc_keep_alive_ref_count(); 1020 assert(fixup_module_field_list() != nullptr, "fixup_module_field_list not initialized"); 1021 fixup_module_field_list()->push(k); 1022 } else { 1023 javabase_was_defined = true; 1024 } 1025 } 1026 1027 // If java.base was already defined then patch this particular class with java.base. 1028 if (javabase_was_defined) { 1029 ModuleEntry *javabase_entry = ModuleEntryTable::javabase_moduleEntry(); 1030 assert(javabase_entry != nullptr && javabase_entry->module() != nullptr, 1031 "Setting class module field, " JAVA_BASE_NAME " should be defined"); 1032 Handle javabase_handle(current, javabase_entry->module()); 1033 set_module(mirror(), javabase_handle()); 1034 } 1035 } else { 1036 assert(Universe::is_module_initialized() || 1037 (ModuleEntryTable::javabase_defined() && 1038 (module() == ModuleEntryTable::javabase_moduleEntry()->module())), 1039 "Incorrect java.lang.Module specification while creating mirror"); 1040 set_module(mirror(), module()); 1041 } 1042 } 1043 1044 // Statically allocate fixup lists because they always get created. 1045 void java_lang_Class::allocate_fixup_lists() { 1046 GrowableArray<Klass*>* mirror_list = 1047 new (mtClass) GrowableArray<Klass*>(40, mtClass); 1048 set_fixup_mirror_list(mirror_list); 1049 1050 GrowableArray<Klass*>* module_list = 1051 new (mtModule) GrowableArray<Klass*>(500, mtModule); 1052 set_fixup_module_field_list(module_list); 1053 } 1054 1055 void java_lang_Class::allocate_mirror(Klass* k, bool is_scratch, Handle protection_domain, Handle classData, 1056 Handle& mirror, Handle& comp_mirror, TRAPS) { 1057 // Allocate mirror (java.lang.Class instance) 1058 oop mirror_oop = InstanceMirrorKlass::cast(vmClasses::Class_klass())->allocate_instance(k, CHECK); 1059 mirror = Handle(THREAD, mirror_oop); 1060 1061 // Setup indirection from mirror->klass 1062 set_klass(mirror(), k); 1063 1064 InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass()); 1065 assert(oop_size(mirror()) == mk->instance_size(k), "should have been set"); 1066 1067 set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror())); 1068 1069 // It might also have a component mirror. This mirror must already exist. 1070 if (k->is_array_klass()) { 1071 if (k->is_typeArray_klass()) { 1072 BasicType type = TypeArrayKlass::cast(k)->element_type(); 1073 if (is_scratch) { 1074 comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(type)); 1075 } else { 1076 comp_mirror = Handle(THREAD, Universe::java_mirror(type)); 1077 } 1078 } else { 1079 assert(k->is_objArray_klass(), "Must be"); 1080 Klass* element_klass = ObjArrayKlass::cast(k)->element_klass(); 1081 assert(element_klass != nullptr, "Must have an element klass"); 1082 if (is_scratch) { 1083 comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(element_klass)); 1084 } else { 1085 comp_mirror = Handle(THREAD, element_klass->java_mirror()); 1086 } 1087 } 1088 assert(comp_mirror() != nullptr, "must have a mirror"); 1089 1090 // Two-way link between the array klass and its component mirror: 1091 // (array_klass) k -> mirror -> component_mirror -> array_klass -> k 1092 set_component_mirror(mirror(), comp_mirror()); 1093 // See below for ordering dependencies between field array_klass in component mirror 1094 // and java_mirror in this klass. 1095 } else { 1096 assert(k->is_instance_klass(), "Must be"); 1097 1098 initialize_mirror_fields(k, mirror, protection_domain, classData, THREAD); 1099 if (HAS_PENDING_EXCEPTION) { 1100 // If any of the fields throws an exception like OOM remove the klass field 1101 // from the mirror so GC doesn't follow it after the klass has been deallocated. 1102 // This mirror looks like a primitive type, which logically it is because it 1103 // it represents no class. 1104 set_klass(mirror(), nullptr); 1105 return; 1106 } 1107 } 1108 } 1109 1110 void java_lang_Class::create_mirror(Klass* k, Handle class_loader, 1111 Handle module, Handle protection_domain, 1112 Handle classData, TRAPS) { 1113 assert(k != nullptr, "Use create_basic_type_mirror for primitive types"); 1114 assert(k->java_mirror() == nullptr, "should only assign mirror once"); 1115 1116 // Use this moment of initialization to cache modifier_flags also, 1117 // to support Class.getModifiers(). Instance classes recalculate 1118 // the cached flags after the class file is parsed, but before the 1119 // class is put into the system dictionary. 1120 int computed_modifiers = k->compute_modifier_flags(); 1121 k->set_modifier_flags(computed_modifiers); 1122 // Class_klass has to be loaded because it is used to allocate 1123 // the mirror. 1124 if (vmClasses::Class_klass_loaded()) { 1125 Handle mirror; 1126 Handle comp_mirror; 1127 1128 allocate_mirror(k, /*is_scratch=*/false, protection_domain, classData, mirror, comp_mirror, CHECK); 1129 1130 // set the classLoader field in the java_lang_Class instance 1131 assert(class_loader() == k->class_loader(), "should be same"); 1132 set_class_loader(mirror(), class_loader()); 1133 1134 // Setup indirection from klass->mirror 1135 // after any exceptions can happen during allocations. 1136 k->set_java_mirror(mirror); 1137 1138 // Set the module field in the java_lang_Class instance. This must be done 1139 // after the mirror is set. 1140 set_mirror_module_field(THREAD, k, mirror, module); 1141 1142 if (comp_mirror() != nullptr) { 1143 // Set after k->java_mirror() is published, because compiled code running 1144 // concurrently doesn't expect a k to have a null java_mirror. 1145 release_set_array_klass(comp_mirror(), k); 1146 } 1147 if (CDSConfig::is_dumping_heap()) { 1148 create_scratch_mirror(k, CHECK); 1149 } 1150 } else { 1151 assert(fixup_mirror_list() != nullptr, "fixup_mirror_list not initialized"); 1152 fixup_mirror_list()->push(k); 1153 } 1154 } 1155 1156 #if INCLUDE_CDS_JAVA_HEAP 1157 // The "scratch mirror" stores the states of the mirror object that can be 1158 // decided at dump time (such as the initial values of the static fields, the 1159 // component mirror, etc). At runtime, more information is added to it by 1160 // java_lang_Class::restore_archived_mirror(). 1161 // 1162 // Essentially, /*dumptime*/create_scratch_mirror() + /*runtime*/restore_archived_mirror() 1163 // produces the same result as /*runtime*/create_mirror(). 1164 // 1165 // Note: we archive the "scratch mirror" instead of k->java_mirror(), because the 1166 // latter may contain dumptime-specific information that cannot be archived 1167 // (e.g., ClassLoaderData*, or static fields that are modified by Java code execution). 1168 void java_lang_Class::create_scratch_mirror(Klass* k, TRAPS) { 1169 if (k->class_loader() != nullptr && 1170 k->class_loader() != SystemDictionary::java_platform_loader() && 1171 k->class_loader() != SystemDictionary::java_system_loader()) { 1172 // We only archive the mirrors of classes loaded by the built-in loaders 1173 return; 1174 } 1175 1176 Handle protection_domain, classData; // set to null. Will be reinitialized at runtime 1177 Handle mirror; 1178 Handle comp_mirror; 1179 allocate_mirror(k, /*is_scratch=*/true, protection_domain, classData, mirror, comp_mirror, CHECK); 1180 1181 if (comp_mirror() != nullptr) { 1182 release_set_array_klass(comp_mirror(), k); 1183 } 1184 1185 HeapShared::set_scratch_java_mirror(k, mirror()); 1186 } 1187 1188 // Returns true if the mirror is updated, false if no archived mirror 1189 // data is present. After the archived mirror object is restored, the 1190 // shared klass' _has_raw_archived_mirror flag is cleared. 1191 bool java_lang_Class::restore_archived_mirror(Klass *k, 1192 Handle class_loader, Handle module, 1193 Handle protection_domain, TRAPS) { 1194 // Postpone restoring archived mirror until java.lang.Class is loaded. Please 1195 // see more details in vmClasses::resolve_all(). 1196 if (!vmClasses::Class_klass_loaded()) { 1197 assert(fixup_mirror_list() != nullptr, "fixup_mirror_list not initialized"); 1198 fixup_mirror_list()->push(k); 1199 return true; 1200 } 1201 1202 oop m = k->archived_java_mirror(); 1203 assert(m != nullptr, "must have stored non-null archived mirror"); 1204 1205 // Sanity: clear it now to prevent re-initialization if any of the following fails 1206 k->clear_archived_mirror_index(); 1207 1208 // mirror is archived, restore 1209 log_debug(cds, mirror)("Archived mirror is: " PTR_FORMAT, p2i(m)); 1210 assert(as_Klass(m) == k, "must be"); 1211 Handle mirror(THREAD, m); 1212 1213 if (!k->is_array_klass()) { 1214 // - local static final fields with initial values were initialized at dump time 1215 1216 // create the init_lock 1217 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK_(false)); 1218 set_init_lock(mirror(), r); 1219 1220 if (protection_domain.not_null()) { 1221 set_protection_domain(mirror(), protection_domain()); 1222 } 1223 } 1224 1225 assert(class_loader() == k->class_loader(), "should be same"); 1226 if (class_loader.not_null()) { 1227 set_class_loader(mirror(), class_loader()); 1228 } 1229 1230 k->set_java_mirror(mirror); 1231 1232 set_mirror_module_field(THREAD, k, mirror, module); 1233 1234 if (log_is_enabled(Trace, cds, heap, mirror)) { 1235 ResourceMark rm(THREAD); 1236 log_trace(cds, heap, mirror)( 1237 "Restored %s archived mirror " PTR_FORMAT, k->external_name(), p2i(mirror())); 1238 } 1239 1240 return true; 1241 } 1242 #endif // INCLUDE_CDS_JAVA_HEAP 1243 1244 void java_lang_Class::fixup_module_field(Klass* k, Handle module) { 1245 assert(_module_offset != 0, "must have been computed already"); 1246 set_module(k->java_mirror(), module()); 1247 } 1248 1249 void java_lang_Class::set_oop_size(HeapWord* java_class, size_t size) { 1250 assert(_oop_size_offset != 0, "must be set"); 1251 assert(size > 0, "Oop size must be greater than zero, not " SIZE_FORMAT, size); 1252 assert(size <= INT_MAX, "Lossy conversion: " SIZE_FORMAT, size); 1253 *(int*)(((char*)java_class) + _oop_size_offset) = (int)size; 1254 } 1255 1256 int java_lang_Class::static_oop_field_count(oop java_class) { 1257 assert(_static_oop_field_count_offset != 0, "must be set"); 1258 return java_class->int_field(_static_oop_field_count_offset); 1259 } 1260 1261 void java_lang_Class::set_static_oop_field_count(oop java_class, int size) { 1262 assert(_static_oop_field_count_offset != 0, "must be set"); 1263 java_class->int_field_put(_static_oop_field_count_offset, size); 1264 } 1265 1266 oop java_lang_Class::protection_domain(oop java_class) { 1267 assert(_protection_domain_offset != 0, "must be set"); 1268 return java_class->obj_field(_protection_domain_offset); 1269 } 1270 void java_lang_Class::set_protection_domain(oop java_class, oop pd) { 1271 assert(_protection_domain_offset != 0, "must be set"); 1272 java_class->obj_field_put(_protection_domain_offset, pd); 1273 } 1274 1275 void java_lang_Class::set_component_mirror(oop java_class, oop comp_mirror) { 1276 assert(_component_mirror_offset != 0, "must be set"); 1277 java_class->obj_field_put(_component_mirror_offset, comp_mirror); 1278 } 1279 oop java_lang_Class::component_mirror(oop java_class) { 1280 assert(_component_mirror_offset != 0, "must be set"); 1281 return java_class->obj_field(_component_mirror_offset); 1282 } 1283 1284 oop java_lang_Class::init_lock(oop java_class) { 1285 assert(_init_lock_offset != 0, "must be set"); 1286 return java_class->obj_field(_init_lock_offset); 1287 } 1288 void java_lang_Class::set_init_lock(oop java_class, oop init_lock) { 1289 assert(_init_lock_offset != 0, "must be set"); 1290 java_class->obj_field_put(_init_lock_offset, init_lock); 1291 } 1292 1293 objArrayOop java_lang_Class::signers(oop java_class) { 1294 assert(_signers_offset != 0, "must be set"); 1295 return (objArrayOop)java_class->obj_field(_signers_offset); 1296 } 1297 1298 oop java_lang_Class::class_data(oop java_class) { 1299 assert(_classData_offset != 0, "must be set"); 1300 return java_class->obj_field(_classData_offset); 1301 } 1302 void java_lang_Class::set_class_data(oop java_class, oop class_data) { 1303 assert(_classData_offset != 0, "must be set"); 1304 java_class->obj_field_put(_classData_offset, class_data); 1305 } 1306 1307 void java_lang_Class::set_reflection_data(oop java_class, oop reflection_data) { 1308 assert(_reflectionData_offset != 0, "must be set"); 1309 java_class->obj_field_put(_reflectionData_offset, reflection_data); 1310 } 1311 1312 void java_lang_Class::set_class_loader(oop java_class, oop loader) { 1313 assert(_class_loader_offset != 0, "offsets should have been initialized"); 1314 java_class->obj_field_put(_class_loader_offset, loader); 1315 } 1316 1317 oop java_lang_Class::class_loader(oop java_class) { 1318 assert(_class_loader_offset != 0, "must be set"); 1319 return java_class->obj_field(_class_loader_offset); 1320 } 1321 1322 oop java_lang_Class::module(oop java_class) { 1323 assert(_module_offset != 0, "must be set"); 1324 return java_class->obj_field(_module_offset); 1325 } 1326 1327 void java_lang_Class::set_module(oop java_class, oop module) { 1328 assert(_module_offset != 0, "must be set"); 1329 java_class->obj_field_put(_module_offset, module); 1330 } 1331 1332 oop java_lang_Class::name(Handle java_class, TRAPS) { 1333 assert(_name_offset != 0, "must be set"); 1334 oop o = java_class->obj_field(_name_offset); 1335 if (o == nullptr) { 1336 o = StringTable::intern(as_external_name(java_class()), THREAD); 1337 java_class->obj_field_put(_name_offset, o); 1338 } 1339 return o; 1340 } 1341 1342 oop java_lang_Class::source_file(oop java_class) { 1343 assert(_source_file_offset != 0, "must be set"); 1344 return java_class->obj_field(_source_file_offset); 1345 } 1346 1347 void java_lang_Class::set_source_file(oop java_class, oop source_file) { 1348 assert(_source_file_offset != 0, "must be set"); 1349 java_class->obj_field_put(_source_file_offset, source_file); 1350 } 1351 1352 oop java_lang_Class::create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS) { 1353 // This should be improved by adding a field at the Java level or by 1354 // introducing a new VM klass (see comment in ClassFileParser) 1355 oop java_class = InstanceMirrorKlass::cast(vmClasses::Class_klass())->allocate_instance(nullptr, CHECK_NULL); 1356 if (type != T_VOID) { 1357 Klass* aklass = Universe::typeArrayKlass(type); 1358 assert(aklass != nullptr, "correct bootstrap"); 1359 release_set_array_klass(java_class, aklass); 1360 } 1361 #ifdef ASSERT 1362 InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(vmClasses::Class_klass()); 1363 assert(static_oop_field_count(java_class) == 0, "should have been zeroed by allocation"); 1364 #endif 1365 return java_class; 1366 } 1367 1368 void java_lang_Class::set_klass(oop java_class, Klass* klass) { 1369 assert(is_instance(java_class), "must be a Class object"); 1370 java_class->metadata_field_put(_klass_offset, klass); 1371 } 1372 1373 1374 void java_lang_Class::print_signature(oop java_class, outputStream* st) { 1375 assert(is_instance(java_class), "must be a Class object"); 1376 Symbol* name = nullptr; 1377 bool is_instance = false; 1378 if (is_primitive(java_class)) { 1379 name = vmSymbols::type_signature(primitive_type(java_class)); 1380 } else { 1381 Klass* k = as_Klass(java_class); 1382 is_instance = k->is_instance_klass(); 1383 name = k->name(); 1384 } 1385 if (name == nullptr) { 1386 st->print("<null>"); 1387 return; 1388 } 1389 if (is_instance) st->print("L"); 1390 st->write((char*) name->base(), (int) name->utf8_length()); 1391 if (is_instance) st->print(";"); 1392 } 1393 1394 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found) { 1395 assert(is_instance(java_class), "must be a Class object"); 1396 Symbol* name; 1397 if (is_primitive(java_class)) { 1398 name = vmSymbols::type_signature(primitive_type(java_class)); 1399 // Because this can create a new symbol, the caller has to decrement 1400 // the refcount, so make adjustment here and below for symbols returned 1401 // that are not created or incremented due to a successful lookup. 1402 name->increment_refcount(); 1403 } else { 1404 Klass* k = as_Klass(java_class); 1405 if (!k->is_instance_klass()) { 1406 name = k->name(); 1407 name->increment_refcount(); 1408 } else { 1409 ResourceMark rm; 1410 const char* sigstr = k->signature_name(); 1411 int siglen = (int) strlen(sigstr); 1412 if (!intern_if_not_found) { 1413 name = SymbolTable::probe(sigstr, siglen); 1414 } else { 1415 name = SymbolTable::new_symbol(sigstr, siglen); 1416 } 1417 } 1418 } 1419 return name; 1420 } 1421 1422 // Returns the Java name for this Java mirror (Resource allocated) 1423 // See Klass::external_name(). 1424 // For primitive type Java mirrors, its type name is returned. 1425 const char* java_lang_Class::as_external_name(oop java_class) { 1426 assert(is_instance(java_class), "must be a Class object"); 1427 const char* name = nullptr; 1428 if (is_primitive(java_class)) { 1429 name = type2name(primitive_type(java_class)); 1430 } else { 1431 name = as_Klass(java_class)->external_name(); 1432 } 1433 if (name == nullptr) { 1434 name = "<null>"; 1435 } 1436 return name; 1437 } 1438 1439 Klass* java_lang_Class::array_klass_acquire(oop java_class) { 1440 Klass* k = ((Klass*)java_class->metadata_field_acquire(_array_klass_offset)); 1441 assert(k == nullptr || (k->is_klass() && k->is_array_klass()), "should be array klass"); 1442 return k; 1443 } 1444 1445 1446 void java_lang_Class::release_set_array_klass(oop java_class, Klass* klass) { 1447 assert(klass->is_klass() && klass->is_array_klass(), "should be array klass"); 1448 java_class->release_metadata_field_put(_array_klass_offset, klass); 1449 } 1450 1451 1452 BasicType java_lang_Class::primitive_type(oop java_class) { 1453 assert(is_primitive(java_class), "just checking"); 1454 Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset)); 1455 BasicType type = T_VOID; 1456 if (ak != nullptr) { 1457 // Note: create_basic_type_mirror above initializes ak to a non-null value. 1458 type = ArrayKlass::cast(ak)->element_type(); 1459 } else { 1460 assert(java_class == Universe::void_mirror(), "only valid non-array primitive"); 1461 } 1462 #ifdef ASSERT 1463 if (CDSConfig::is_dumping_heap()) { 1464 oop mirror = Universe::java_mirror(type); 1465 oop scratch_mirror = HeapShared::scratch_java_mirror(type); 1466 assert(java_class == mirror || java_class == scratch_mirror, "must be consistent"); 1467 } else { 1468 assert(Universe::java_mirror(type) == java_class, "must be consistent"); 1469 } 1470 #endif 1471 return type; 1472 } 1473 1474 BasicType java_lang_Class::as_BasicType(oop java_class, Klass** reference_klass) { 1475 assert(is_instance(java_class), "must be a Class object"); 1476 if (is_primitive(java_class)) { 1477 if (reference_klass != nullptr) 1478 (*reference_klass) = nullptr; 1479 return primitive_type(java_class); 1480 } else { 1481 if (reference_klass != nullptr) 1482 (*reference_klass) = as_Klass(java_class); 1483 return T_OBJECT; 1484 } 1485 } 1486 1487 1488 oop java_lang_Class::primitive_mirror(BasicType t) { 1489 oop mirror = Universe::java_mirror(t); 1490 assert(mirror != nullptr && mirror->is_a(vmClasses::Class_klass()), "must be a Class"); 1491 assert(is_primitive(mirror), "must be primitive"); 1492 return mirror; 1493 } 1494 1495 #define CLASS_FIELDS_DO(macro) \ 1496 macro(_classRedefinedCount_offset, k, "classRedefinedCount", int_signature, false); \ 1497 macro(_class_loader_offset, k, "classLoader", classloader_signature, false); \ 1498 macro(_component_mirror_offset, k, "componentType", class_signature, false); \ 1499 macro(_module_offset, k, "module", module_signature, false); \ 1500 macro(_name_offset, k, "name", string_signature, false); \ 1501 macro(_classData_offset, k, "classData", object_signature, false); \ 1502 macro(_reflectionData_offset, k, "reflectionData", java_lang_ref_SoftReference_signature, false); \ 1503 macro(_signers_offset, k, "signers", object_array_signature, false); 1504 1505 void java_lang_Class::compute_offsets() { 1506 if (_offsets_computed) { 1507 return; 1508 } 1509 1510 _offsets_computed = true; 1511 1512 InstanceKlass* k = vmClasses::Class_klass(); 1513 CLASS_FIELDS_DO(FIELD_COMPUTE_OFFSET); 1514 CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 1515 } 1516 1517 #if INCLUDE_CDS 1518 void java_lang_Class::serialize_offsets(SerializeClosure* f) { 1519 f->do_bool(&_offsets_computed); 1520 1521 CLASS_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 1522 1523 CLASS_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 1524 } 1525 #endif 1526 1527 int java_lang_Class::classRedefinedCount(oop the_class_mirror) { 1528 assert(_classRedefinedCount_offset != 0, "offsets should have been initialized"); 1529 return the_class_mirror->int_field(_classRedefinedCount_offset); 1530 } 1531 1532 void java_lang_Class::set_classRedefinedCount(oop the_class_mirror, int value) { 1533 assert(_classRedefinedCount_offset != 0, "offsets should have been initialized"); 1534 the_class_mirror->int_field_put(_classRedefinedCount_offset, value); 1535 } 1536 1537 1538 // Note: JDK1.1 and before had a privateInfo_offset field which was used for the 1539 // platform thread structure, and a eetop offset which was used for thread 1540 // local storage (and unused by the HotSpot VM). In JDK1.2 the two structures 1541 // merged, so in the HotSpot VM we just use the eetop field for the thread 1542 // instead of the privateInfo_offset. 1543 // 1544 // Note: The stackSize field is only present starting in 1.4. 1545 1546 int java_lang_Thread_FieldHolder::_group_offset; 1547 int java_lang_Thread_FieldHolder::_priority_offset; 1548 int java_lang_Thread_FieldHolder::_stackSize_offset; 1549 int java_lang_Thread_FieldHolder::_daemon_offset; 1550 int java_lang_Thread_FieldHolder::_thread_status_offset; 1551 1552 #define THREAD_FIELD_HOLDER_FIELDS_DO(macro) \ 1553 macro(_group_offset, k, vmSymbols::group_name(), threadgroup_signature, false); \ 1554 macro(_priority_offset, k, vmSymbols::priority_name(), int_signature, false); \ 1555 macro(_stackSize_offset, k, "stackSize", long_signature, false); \ 1556 macro(_daemon_offset, k, vmSymbols::daemon_name(), bool_signature, false); \ 1557 macro(_thread_status_offset, k, "threadStatus", int_signature, false) 1558 1559 void java_lang_Thread_FieldHolder::compute_offsets() { 1560 assert(_group_offset == 0, "offsets should be initialized only once"); 1561 1562 InstanceKlass* k = vmClasses::Thread_FieldHolder_klass(); 1563 THREAD_FIELD_HOLDER_FIELDS_DO(FIELD_COMPUTE_OFFSET); 1564 } 1565 1566 #if INCLUDE_CDS 1567 void java_lang_Thread_FieldHolder::serialize_offsets(SerializeClosure* f) { 1568 THREAD_FIELD_HOLDER_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 1569 } 1570 #endif 1571 1572 oop java_lang_Thread_FieldHolder::threadGroup(oop holder) { 1573 return holder->obj_field(_group_offset); 1574 } 1575 1576 ThreadPriority java_lang_Thread_FieldHolder::priority(oop holder) { 1577 return (ThreadPriority)holder->int_field(_priority_offset); 1578 } 1579 1580 void java_lang_Thread_FieldHolder::set_priority(oop holder, ThreadPriority priority) { 1581 holder->int_field_put(_priority_offset, priority); 1582 } 1583 1584 jlong java_lang_Thread_FieldHolder::stackSize(oop holder) { 1585 return holder->long_field(_stackSize_offset); 1586 } 1587 1588 bool java_lang_Thread_FieldHolder::is_daemon(oop holder) { 1589 return holder->bool_field(_daemon_offset) != 0; 1590 } 1591 1592 void java_lang_Thread_FieldHolder::set_daemon(oop holder, bool val) { 1593 assert(val, "daemon status is never turned off"); 1594 holder->bool_field_put(_daemon_offset, val); 1595 } 1596 1597 void java_lang_Thread_FieldHolder::set_thread_status(oop holder, JavaThreadStatus status) { 1598 holder->int_field_put(_thread_status_offset, static_cast<int>(status)); 1599 } 1600 1601 JavaThreadStatus java_lang_Thread_FieldHolder::get_thread_status(oop holder) { 1602 return static_cast<JavaThreadStatus>(holder->int_field(_thread_status_offset)); 1603 } 1604 1605 1606 int java_lang_Thread_Constants::_static_VTHREAD_GROUP_offset = 0; 1607 1608 #define THREAD_CONSTANTS_STATIC_FIELDS_DO(macro) \ 1609 macro(_static_VTHREAD_GROUP_offset, k, "VTHREAD_GROUP", threadgroup_signature, true); 1610 1611 void java_lang_Thread_Constants::compute_offsets() { 1612 assert(_static_VTHREAD_GROUP_offset == 0, "offsets should be initialized only once"); 1613 1614 InstanceKlass* k = vmClasses::Thread_Constants_klass(); 1615 THREAD_CONSTANTS_STATIC_FIELDS_DO(FIELD_COMPUTE_OFFSET); 1616 } 1617 1618 #if INCLUDE_CDS 1619 void java_lang_Thread_Constants::serialize_offsets(SerializeClosure* f) { 1620 THREAD_CONSTANTS_STATIC_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 1621 } 1622 #endif 1623 1624 oop java_lang_Thread_Constants::get_VTHREAD_GROUP() { 1625 InstanceKlass* k = vmClasses::Thread_Constants_klass(); 1626 oop base = k->static_field_base_raw(); 1627 return base->obj_field(_static_VTHREAD_GROUP_offset); 1628 } 1629 1630 1631 int java_lang_Thread::_holder_offset; 1632 int java_lang_Thread::_name_offset; 1633 int java_lang_Thread::_contextClassLoader_offset; 1634 int java_lang_Thread::_eetop_offset; 1635 int java_lang_Thread::_jvmti_thread_state_offset; 1636 int java_lang_Thread::_jvmti_VTMS_transition_disable_count_offset; 1637 int java_lang_Thread::_jvmti_is_in_VTMS_transition_offset; 1638 int java_lang_Thread::_interrupted_offset; 1639 int java_lang_Thread::_interruptLock_offset; 1640 int java_lang_Thread::_tid_offset; 1641 int java_lang_Thread::_continuation_offset; 1642 int java_lang_Thread::_park_blocker_offset; 1643 int java_lang_Thread::_scopedValueBindings_offset; 1644 JFR_ONLY(int java_lang_Thread::_jfr_epoch_offset;) 1645 1646 #define THREAD_FIELDS_DO(macro) \ 1647 macro(_holder_offset, k, "holder", thread_fieldholder_signature, false); \ 1648 macro(_name_offset, k, vmSymbols::name_name(), string_signature, false); \ 1649 macro(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), classloader_signature, false); \ 1650 macro(_eetop_offset, k, "eetop", long_signature, false); \ 1651 macro(_interrupted_offset, k, "interrupted", bool_signature, false); \ 1652 macro(_interruptLock_offset, k, "interruptLock", object_signature, false); \ 1653 macro(_tid_offset, k, "tid", long_signature, false); \ 1654 macro(_park_blocker_offset, k, "parkBlocker", object_signature, false); \ 1655 macro(_continuation_offset, k, "cont", continuation_signature, false); \ 1656 macro(_scopedValueBindings_offset, k, "scopedValueBindings", object_signature, false); 1657 1658 void java_lang_Thread::compute_offsets() { 1659 assert(_holder_offset == 0, "offsets should be initialized only once"); 1660 1661 InstanceKlass* k = vmClasses::Thread_klass(); 1662 THREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET); 1663 THREAD_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 1664 } 1665 1666 #if INCLUDE_CDS 1667 void java_lang_Thread::serialize_offsets(SerializeClosure* f) { 1668 THREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 1669 THREAD_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 1670 } 1671 #endif 1672 1673 JavaThread* java_lang_Thread::thread(oop java_thread) { 1674 return reinterpret_cast<JavaThread*>(java_thread->address_field(_eetop_offset)); 1675 } 1676 1677 JavaThread* java_lang_Thread::thread_acquire(oop java_thread) { 1678 return reinterpret_cast<JavaThread*>(java_thread->address_field_acquire(_eetop_offset)); 1679 } 1680 1681 void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) { 1682 java_thread->address_field_put(_eetop_offset, (address)thread); 1683 } 1684 1685 void java_lang_Thread::release_set_thread(oop java_thread, JavaThread* thread) { 1686 java_thread->release_address_field_put(_eetop_offset, (address)thread); 1687 } 1688 1689 JvmtiThreadState* java_lang_Thread::jvmti_thread_state(oop java_thread) { 1690 return (JvmtiThreadState*)java_thread->address_field(_jvmti_thread_state_offset); 1691 } 1692 1693 void java_lang_Thread::set_jvmti_thread_state(oop java_thread, JvmtiThreadState* state) { 1694 java_thread->address_field_put(_jvmti_thread_state_offset, (address)state); 1695 } 1696 1697 int java_lang_Thread::VTMS_transition_disable_count(oop java_thread) { 1698 return java_thread->int_field(_jvmti_VTMS_transition_disable_count_offset); 1699 } 1700 1701 void java_lang_Thread::inc_VTMS_transition_disable_count(oop java_thread) { 1702 assert(JvmtiVTMSTransition_lock->owned_by_self(), "Must be locked"); 1703 int val = VTMS_transition_disable_count(java_thread); 1704 java_thread->int_field_put(_jvmti_VTMS_transition_disable_count_offset, val + 1); 1705 } 1706 1707 void java_lang_Thread::dec_VTMS_transition_disable_count(oop java_thread) { 1708 assert(JvmtiVTMSTransition_lock->owned_by_self(), "Must be locked"); 1709 int val = VTMS_transition_disable_count(java_thread); 1710 assert(val > 0, "VTMS_transition_disable_count should never be negative"); 1711 java_thread->int_field_put(_jvmti_VTMS_transition_disable_count_offset, val - 1); 1712 } 1713 1714 bool java_lang_Thread::is_in_VTMS_transition(oop java_thread) { 1715 return java_thread->bool_field_volatile(_jvmti_is_in_VTMS_transition_offset); 1716 } 1717 1718 void java_lang_Thread::set_is_in_VTMS_transition(oop java_thread, bool val) { 1719 assert(is_in_VTMS_transition(java_thread) != val, "already %s transition", val ? "inside" : "outside"); 1720 java_thread->bool_field_put_volatile(_jvmti_is_in_VTMS_transition_offset, val); 1721 } 1722 1723 int java_lang_Thread::is_in_VTMS_transition_offset() { 1724 return _jvmti_is_in_VTMS_transition_offset; 1725 } 1726 1727 void java_lang_Thread::clear_scopedValueBindings(oop java_thread) { 1728 assert(java_thread != nullptr, "need a java_lang_Thread pointer here"); 1729 java_thread->obj_field_put(_scopedValueBindings_offset, nullptr); 1730 } 1731 1732 oop java_lang_Thread::holder(oop java_thread) { 1733 // Note: may return null if the thread is still attaching 1734 return java_thread->obj_field(_holder_offset); 1735 } 1736 oop java_lang_Thread::interrupt_lock(oop java_thread) { 1737 return java_thread->obj_field(_interruptLock_offset); 1738 } 1739 1740 bool java_lang_Thread::interrupted(oop java_thread) { 1741 // Make sure the caller can safely access oops. 1742 assert(Thread::current()->is_VM_thread() || 1743 (JavaThread::current()->thread_state() != _thread_blocked && 1744 JavaThread::current()->thread_state() != _thread_in_native), 1745 "Unsafe access to oop"); 1746 return java_thread->bool_field_volatile(_interrupted_offset); 1747 } 1748 1749 void java_lang_Thread::set_interrupted(oop java_thread, bool val) { 1750 // Make sure the caller can safely access oops. 1751 assert(Thread::current()->is_VM_thread() || 1752 (JavaThread::current()->thread_state() != _thread_blocked && 1753 JavaThread::current()->thread_state() != _thread_in_native), 1754 "Unsafe access to oop"); 1755 java_thread->bool_field_put_volatile(_interrupted_offset, val); 1756 } 1757 1758 1759 oop java_lang_Thread::name(oop java_thread) { 1760 return java_thread->obj_field(_name_offset); 1761 } 1762 1763 1764 void java_lang_Thread::set_name(oop java_thread, oop name) { 1765 java_thread->obj_field_put(_name_offset, name); 1766 } 1767 1768 // Convenience macros for setting and getting Thread fields that 1769 // are actually stored in the FieldHolder object of the thread. 1770 // The FieldHolder can be null whilst a thread is attaching via 1771 // JNI, and when the main thread is attaching. 1772 1773 // The default value should be the default/zero initialized value 1774 // of the field as it would be in java.lang.Thread.FieldHolder. 1775 #define GET_FIELDHOLDER_FIELD(java_thread, field, default_val) \ 1776 { \ 1777 oop holder = java_lang_Thread::holder(java_thread); \ 1778 if (holder != nullptr) \ 1779 return java_lang_Thread_FieldHolder::field(holder); \ 1780 else \ 1781 return default_val; \ 1782 } 1783 1784 // We should never be trying to set a field of an attaching thread. 1785 #define SET_FIELDHOLDER_FIELD(java_thread, field, value) \ 1786 { \ 1787 oop holder = java_lang_Thread::holder(java_thread); \ 1788 assert(holder != nullptr, "Thread not fully initialized"); \ 1789 java_lang_Thread_FieldHolder::set_##field(holder, value); \ 1790 } 1791 1792 1793 ThreadPriority java_lang_Thread::priority(oop java_thread) { 1794 GET_FIELDHOLDER_FIELD(java_thread, priority, (ThreadPriority)0); 1795 } 1796 1797 1798 void java_lang_Thread::set_priority(oop java_thread, ThreadPriority priority) { 1799 SET_FIELDHOLDER_FIELD(java_thread, priority, priority) 1800 } 1801 1802 1803 oop java_lang_Thread::threadGroup(oop java_thread) { 1804 GET_FIELDHOLDER_FIELD(java_thread, threadGroup, nullptr); 1805 } 1806 1807 1808 bool java_lang_Thread::is_alive(oop java_thread) { 1809 JavaThread* thr = java_lang_Thread::thread(java_thread); 1810 return (thr != nullptr); 1811 } 1812 1813 1814 bool java_lang_Thread::is_daemon(oop java_thread) { 1815 GET_FIELDHOLDER_FIELD(java_thread, is_daemon, false); 1816 } 1817 1818 1819 void java_lang_Thread::set_daemon(oop java_thread) { 1820 SET_FIELDHOLDER_FIELD(java_thread, daemon, true); 1821 } 1822 1823 oop java_lang_Thread::context_class_loader(oop java_thread) { 1824 return java_thread->obj_field(_contextClassLoader_offset); 1825 } 1826 1827 1828 jlong java_lang_Thread::stackSize(oop java_thread) { 1829 GET_FIELDHOLDER_FIELD(java_thread, stackSize, 0); 1830 } 1831 1832 // Write the thread status value to threadStatus field in java.lang.Thread java class. 1833 void java_lang_Thread::set_thread_status(oop java_thread, JavaThreadStatus status) { 1834 SET_FIELDHOLDER_FIELD(java_thread, thread_status, status); 1835 } 1836 1837 // Read thread status value from threadStatus field in java.lang.Thread java class. 1838 JavaThreadStatus java_lang_Thread::get_thread_status(oop java_thread) { 1839 // Make sure the caller is operating on behalf of the VM or is 1840 // running VM code (state == _thread_in_vm). 1841 assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() || 1842 JavaThread::current()->thread_state() == _thread_in_vm, 1843 "Java Thread is not running in vm"); 1844 GET_FIELDHOLDER_FIELD(java_thread, get_thread_status, JavaThreadStatus::NEW /* not initialized */); 1845 } 1846 1847 ByteSize java_lang_Thread::thread_id_offset() { 1848 return in_ByteSize(_tid_offset); 1849 } 1850 1851 oop java_lang_Thread::park_blocker(oop java_thread) { 1852 return java_thread->obj_field(_park_blocker_offset); 1853 } 1854 1855 oop java_lang_Thread::async_get_stack_trace(oop java_thread, TRAPS) { 1856 ThreadsListHandle tlh(JavaThread::current()); 1857 JavaThread* thread; 1858 bool is_virtual = java_lang_VirtualThread::is_instance(java_thread); 1859 if (is_virtual) { 1860 oop carrier_thread = java_lang_VirtualThread::carrier_thread(java_thread); 1861 if (carrier_thread == nullptr) { 1862 return nullptr; 1863 } 1864 thread = java_lang_Thread::thread(carrier_thread); 1865 } else { 1866 thread = java_lang_Thread::thread(java_thread); 1867 } 1868 if (thread == nullptr) { 1869 return nullptr; 1870 } 1871 1872 class GetStackTraceClosure : public HandshakeClosure { 1873 public: 1874 const Handle _java_thread; 1875 int _depth; 1876 bool _retry_handshake; 1877 GrowableArray<Method*>* _methods; 1878 GrowableArray<int>* _bcis; 1879 1880 GetStackTraceClosure(Handle java_thread) : 1881 HandshakeClosure("GetStackTraceClosure"), _java_thread(java_thread), _depth(0), _retry_handshake(false), 1882 _methods(nullptr), _bcis(nullptr) { 1883 } 1884 ~GetStackTraceClosure() { 1885 delete _methods; 1886 delete _bcis; 1887 } 1888 1889 bool read_reset_retry() { 1890 bool ret = _retry_handshake; 1891 // If we re-execute the handshake this method need to return false 1892 // when the handshake cannot be performed. (E.g. thread terminating) 1893 _retry_handshake = false; 1894 return ret; 1895 } 1896 1897 void do_thread(Thread* th) { 1898 if (!Thread::current()->is_Java_thread()) { 1899 _retry_handshake = true; 1900 return; 1901 } 1902 1903 JavaThread* thread = JavaThread::cast(th); 1904 1905 if (!thread->has_last_Java_frame()) { 1906 return; 1907 } 1908 1909 bool carrier = false; 1910 if (java_lang_VirtualThread::is_instance(_java_thread())) { 1911 // if (thread->vthread() != _java_thread()) // We might be inside a System.executeOnCarrierThread 1912 const ContinuationEntry* ce = thread->vthread_continuation(); 1913 if (ce == nullptr || ce->cont_oop(thread) != java_lang_VirtualThread::continuation(_java_thread())) { 1914 return; // not mounted 1915 } 1916 } else { 1917 carrier = (thread->vthread_continuation() != nullptr); 1918 } 1919 1920 const int max_depth = MaxJavaStackTraceDepth; 1921 const bool skip_hidden = !ShowHiddenFrames; 1922 1923 // Pick minimum length that will cover most cases 1924 int init_length = 64; 1925 _methods = new (mtInternal) GrowableArray<Method*>(init_length, mtInternal); 1926 _bcis = new (mtInternal) GrowableArray<int>(init_length, mtInternal); 1927 1928 int total_count = 0; 1929 for (vframeStream vfst(thread, false, false, carrier); // we don't process frames as we don't care about oops 1930 !vfst.at_end() && (max_depth == 0 || max_depth != total_count); 1931 vfst.next()) { 1932 1933 if (skip_hidden && (vfst.method()->is_hidden() || 1934 vfst.method()->is_continuation_enter_intrinsic())) { 1935 continue; 1936 } 1937 1938 _methods->push(vfst.method()); 1939 _bcis->push(vfst.bci()); 1940 total_count++; 1941 } 1942 1943 _depth = total_count; 1944 } 1945 }; 1946 1947 // Handshake with target 1948 ResourceMark rm(THREAD); 1949 HandleMark hm(THREAD); 1950 GetStackTraceClosure gstc(Handle(THREAD, java_thread)); 1951 do { 1952 Handshake::execute(&gstc, &tlh, thread); 1953 } while (gstc.read_reset_retry()); 1954 1955 // Stop if no stack trace is found. 1956 if (gstc._depth == 0) { 1957 return nullptr; 1958 } 1959 1960 // Convert to StackTraceElement array 1961 InstanceKlass* k = vmClasses::StackTraceElement_klass(); 1962 assert(k != nullptr, "must be loaded in 1.4+"); 1963 if (k->should_be_initialized()) { 1964 k->initialize(CHECK_NULL); 1965 } 1966 objArrayHandle trace = oopFactory::new_objArray_handle(k, gstc._depth, CHECK_NULL); 1967 1968 for (int i = 0; i < gstc._depth; i++) { 1969 methodHandle method(THREAD, gstc._methods->at(i)); 1970 oop element = java_lang_StackTraceElement::create(method, 1971 gstc._bcis->at(i), 1972 CHECK_NULL); 1973 trace->obj_at_put(i, element); 1974 } 1975 1976 return trace(); 1977 } 1978 1979 const char* java_lang_Thread::thread_status_name(oop java_thread) { 1980 JavaThreadStatus status = get_thread_status(java_thread); 1981 switch (status) { 1982 case JavaThreadStatus::NEW : return "NEW"; 1983 case JavaThreadStatus::RUNNABLE : return "RUNNABLE"; 1984 case JavaThreadStatus::SLEEPING : return "TIMED_WAITING (sleeping)"; 1985 case JavaThreadStatus::IN_OBJECT_WAIT : return "WAITING (on object monitor)"; 1986 case JavaThreadStatus::IN_OBJECT_WAIT_TIMED : return "TIMED_WAITING (on object monitor)"; 1987 case JavaThreadStatus::PARKED : return "WAITING (parking)"; 1988 case JavaThreadStatus::PARKED_TIMED : return "TIMED_WAITING (parking)"; 1989 case JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)"; 1990 case JavaThreadStatus::TERMINATED : return "TERMINATED"; 1991 default : return "UNKNOWN"; 1992 }; 1993 } 1994 int java_lang_ThreadGroup::_parent_offset; 1995 int java_lang_ThreadGroup::_name_offset; 1996 int java_lang_ThreadGroup::_maxPriority_offset; 1997 int java_lang_ThreadGroup::_daemon_offset; 1998 1999 oop java_lang_ThreadGroup::parent(oop java_thread_group) { 2000 assert(oopDesc::is_oop(java_thread_group), "thread group must be oop"); 2001 return java_thread_group->obj_field(_parent_offset); 2002 } 2003 2004 // ("name as oop" accessor is not necessary) 2005 2006 const char* java_lang_ThreadGroup::name(oop java_thread_group) { 2007 oop name = java_thread_group->obj_field(_name_offset); 2008 // ThreadGroup.name can be null 2009 if (name != nullptr) { 2010 return java_lang_String::as_utf8_string(name); 2011 } 2012 return nullptr; 2013 } 2014 2015 ThreadPriority java_lang_ThreadGroup::maxPriority(oop java_thread_group) { 2016 assert(oopDesc::is_oop(java_thread_group), "thread group must be oop"); 2017 return (ThreadPriority) java_thread_group->int_field(_maxPriority_offset); 2018 } 2019 2020 bool java_lang_ThreadGroup::is_daemon(oop java_thread_group) { 2021 assert(oopDesc::is_oop(java_thread_group), "thread group must be oop"); 2022 return java_thread_group->bool_field(_daemon_offset) != 0; 2023 } 2024 2025 #define THREADGROUP_FIELDS_DO(macro) \ 2026 macro(_parent_offset, k, vmSymbols::parent_name(), threadgroup_signature, false); \ 2027 macro(_name_offset, k, vmSymbols::name_name(), string_signature, false); \ 2028 macro(_maxPriority_offset, k, vmSymbols::maxPriority_name(), int_signature, false); \ 2029 macro(_daemon_offset, k, vmSymbols::daemon_name(), bool_signature, false); 2030 2031 void java_lang_ThreadGroup::compute_offsets() { 2032 assert(_parent_offset == 0, "offsets should be initialized only once"); 2033 2034 InstanceKlass* k = vmClasses::ThreadGroup_klass(); 2035 THREADGROUP_FIELDS_DO(FIELD_COMPUTE_OFFSET); 2036 } 2037 2038 #if INCLUDE_CDS 2039 void java_lang_ThreadGroup::serialize_offsets(SerializeClosure* f) { 2040 THREADGROUP_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 2041 } 2042 #endif 2043 2044 2045 // java_lang_VirtualThread 2046 2047 int java_lang_VirtualThread::static_vthread_scope_offset; 2048 int java_lang_VirtualThread::_carrierThread_offset; 2049 int java_lang_VirtualThread::_continuation_offset; 2050 int java_lang_VirtualThread::_state_offset; 2051 int java_lang_VirtualThread::_next_offset; 2052 int java_lang_VirtualThread::_onWaitingList_offset; 2053 int java_lang_VirtualThread::_notified_offset; 2054 int java_lang_VirtualThread::_timeout_offset; 2055 2056 #define VTHREAD_FIELDS_DO(macro) \ 2057 macro(static_vthread_scope_offset, k, "VTHREAD_SCOPE", continuationscope_signature, true); \ 2058 macro(_carrierThread_offset, k, "carrierThread", thread_signature, false); \ 2059 macro(_continuation_offset, k, "cont", continuation_signature, false); \ 2060 macro(_state_offset, k, "state", int_signature, false); \ 2061 macro(_next_offset, k, "next", vthread_signature, false); \ 2062 macro(_onWaitingList_offset, k, "onWaitingList", bool_signature, false); \ 2063 macro(_notified_offset, k, "notified", bool_signature, false); \ 2064 macro(_timeout_offset, k, "timeout", long_signature, false); 2065 2066 2067 void java_lang_VirtualThread::compute_offsets() { 2068 InstanceKlass* k = vmClasses::VirtualThread_klass(); 2069 VTHREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET); 2070 } 2071 2072 bool java_lang_VirtualThread::is_instance(oop obj) { 2073 return obj != nullptr && is_subclass(obj->klass()); 2074 } 2075 2076 oop java_lang_VirtualThread::carrier_thread(oop vthread) { 2077 oop thread = vthread->obj_field(_carrierThread_offset); 2078 return thread; 2079 } 2080 2081 oop java_lang_VirtualThread::continuation(oop vthread) { 2082 oop cont = vthread->obj_field(_continuation_offset); 2083 return cont; 2084 } 2085 2086 int java_lang_VirtualThread::state(oop vthread) { 2087 return vthread->int_field_acquire(_state_offset); 2088 } 2089 2090 void java_lang_VirtualThread::set_state(oop vthread, int state) { 2091 vthread->release_int_field_put(_state_offset, state); 2092 } 2093 2094 int java_lang_VirtualThread::cmpxchg_state(oop vthread, int old_state, int new_state) { 2095 jint* addr = vthread->field_addr<jint>(_state_offset); 2096 int res = Atomic::cmpxchg(addr, old_state, new_state); 2097 return res; 2098 } 2099 2100 oop java_lang_VirtualThread::next(oop vthread) { 2101 return vthread->obj_field(_next_offset); 2102 } 2103 2104 void java_lang_VirtualThread::set_next(oop vthread, oop next_vthread) { 2105 vthread->obj_field_put(_next_offset, next_vthread); 2106 } 2107 2108 // Add vthread to the waiting list if it's not already in it. Multiple threads 2109 // could be trying to add vthread to the list at the same time, so we control 2110 // access with a cmpxchg on onWaitingList. The winner adds vthread to the list. 2111 // Method returns true if we added vthread to the list, false otherwise. 2112 bool java_lang_VirtualThread::set_onWaitingList(oop vthread, OopHandle& list_head) { 2113 jboolean* addr = vthread->field_addr<jboolean>(_onWaitingList_offset); 2114 jboolean vthread_on_list = Atomic::load(addr); 2115 if (!vthread_on_list) { 2116 vthread_on_list = Atomic::cmpxchg(addr, (jboolean)JNI_FALSE, (jboolean)JNI_TRUE); 2117 if (!vthread_on_list) { 2118 for (;;) { 2119 oop head = list_head.resolve(); 2120 java_lang_VirtualThread::set_next(vthread, head); 2121 if (list_head.cmpxchg(head, vthread) == head) return true; 2122 } 2123 } 2124 } 2125 return false; // already on waiting list 2126 } 2127 2128 void java_lang_VirtualThread::set_notified(oop vthread, jboolean value) { 2129 vthread->bool_field_put_volatile(_notified_offset, value); 2130 } 2131 2132 jlong java_lang_VirtualThread::timeout(oop vthread) { 2133 return vthread->long_field(_timeout_offset); 2134 } 2135 2136 void java_lang_VirtualThread::set_timeout(oop vthread, jlong value) { 2137 vthread->long_field_put(_timeout_offset, value); 2138 } 2139 2140 JavaThreadStatus java_lang_VirtualThread::map_state_to_thread_status(int state) { 2141 JavaThreadStatus status = JavaThreadStatus::NEW; 2142 switch (state & ~SUSPENDED) { 2143 case NEW: 2144 status = JavaThreadStatus::NEW; 2145 break; 2146 case STARTED: 2147 case RUNNING: 2148 case PARKING: 2149 case TIMED_PARKING: 2150 case UNPARKED: 2151 case YIELDING: 2152 case YIELDED: 2153 case UNBLOCKED: 2154 case WAITING: 2155 case TIMED_WAITING: 2156 status = JavaThreadStatus::RUNNABLE; 2157 break; 2158 case PARKED: 2159 case PINNED: 2160 status = JavaThreadStatus::PARKED; 2161 break; 2162 case TIMED_PARKED: 2163 case TIMED_PINNED: 2164 status = JavaThreadStatus::PARKED_TIMED; 2165 break; 2166 case BLOCKING: 2167 case BLOCKED: 2168 status = JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER; 2169 break; 2170 case WAIT: 2171 status = JavaThreadStatus::IN_OBJECT_WAIT; 2172 break; 2173 case TIMED_WAIT: 2174 status = JavaThreadStatus::IN_OBJECT_WAIT_TIMED; 2175 break; 2176 case TERMINATED: 2177 status = JavaThreadStatus::TERMINATED; 2178 break; 2179 default: 2180 ShouldNotReachHere(); 2181 } 2182 return status; 2183 } 2184 2185 bool java_lang_VirtualThread::is_preempted(oop vthread) { 2186 oop continuation = java_lang_VirtualThread::continuation(vthread); 2187 assert(continuation != nullptr, "vthread with no continuation"); 2188 stackChunkOop chunk = jdk_internal_vm_Continuation::tail(continuation); 2189 return chunk != nullptr && chunk->preempted(); 2190 } 2191 2192 #if INCLUDE_CDS 2193 void java_lang_VirtualThread::serialize_offsets(SerializeClosure* f) { 2194 VTHREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 2195 } 2196 #endif 2197 2198 // java_lang_Throwable 2199 2200 int java_lang_Throwable::_backtrace_offset; 2201 int java_lang_Throwable::_detailMessage_offset; 2202 int java_lang_Throwable::_stackTrace_offset; 2203 int java_lang_Throwable::_depth_offset; 2204 int java_lang_Throwable::_cause_offset; 2205 int java_lang_Throwable::_static_unassigned_stacktrace_offset; 2206 2207 #define THROWABLE_FIELDS_DO(macro) \ 2208 macro(_backtrace_offset, k, "backtrace", object_signature, false); \ 2209 macro(_detailMessage_offset, k, "detailMessage", string_signature, false); \ 2210 macro(_stackTrace_offset, k, "stackTrace", java_lang_StackTraceElement_array, false); \ 2211 macro(_depth_offset, k, "depth", int_signature, false); \ 2212 macro(_cause_offset, k, "cause", throwable_signature, false); \ 2213 macro(_static_unassigned_stacktrace_offset, k, "UNASSIGNED_STACK", java_lang_StackTraceElement_array, true) 2214 2215 void java_lang_Throwable::compute_offsets() { 2216 InstanceKlass* k = vmClasses::Throwable_klass(); 2217 THROWABLE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 2218 } 2219 2220 #if INCLUDE_CDS 2221 void java_lang_Throwable::serialize_offsets(SerializeClosure* f) { 2222 THROWABLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 2223 } 2224 #endif 2225 2226 oop java_lang_Throwable::unassigned_stacktrace() { 2227 InstanceKlass* ik = vmClasses::Throwable_klass(); 2228 oop base = ik->static_field_base_raw(); 2229 return base->obj_field(_static_unassigned_stacktrace_offset); 2230 } 2231 2232 oop java_lang_Throwable::backtrace(oop throwable) { 2233 return throwable->obj_field_acquire(_backtrace_offset); 2234 } 2235 2236 2237 void java_lang_Throwable::set_backtrace(oop throwable, oop value) { 2238 throwable->release_obj_field_put(_backtrace_offset, value); 2239 } 2240 2241 int java_lang_Throwable::depth(oop throwable) { 2242 return throwable->int_field(_depth_offset); 2243 } 2244 2245 void java_lang_Throwable::set_depth(oop throwable, int value) { 2246 throwable->int_field_put(_depth_offset, value); 2247 } 2248 2249 oop java_lang_Throwable::message(oop throwable) { 2250 return throwable->obj_field(_detailMessage_offset); 2251 } 2252 2253 const char* java_lang_Throwable::message_as_utf8(oop throwable) { 2254 oop msg = java_lang_Throwable::message(throwable); 2255 const char* msg_utf8 = nullptr; 2256 if (msg != nullptr) { 2257 msg_utf8 = java_lang_String::as_utf8_string(msg); 2258 } 2259 return msg_utf8; 2260 } 2261 2262 oop java_lang_Throwable::cause(oop throwable) { 2263 return throwable->obj_field(_cause_offset); 2264 } 2265 2266 void java_lang_Throwable::set_message(oop throwable, oop value) { 2267 throwable->obj_field_put(_detailMessage_offset, value); 2268 } 2269 2270 2271 void java_lang_Throwable::set_stacktrace(oop throwable, oop st_element_array) { 2272 throwable->obj_field_put(_stackTrace_offset, st_element_array); 2273 } 2274 2275 void java_lang_Throwable::clear_stacktrace(oop throwable) { 2276 set_stacktrace(throwable, nullptr); 2277 } 2278 2279 2280 void java_lang_Throwable::print(oop throwable, outputStream* st) { 2281 ResourceMark rm; 2282 Klass* k = throwable->klass(); 2283 assert(k != nullptr, "just checking"); 2284 st->print("%s", k->external_name()); 2285 oop msg = message(throwable); 2286 if (msg != nullptr) { 2287 st->print(": %s", java_lang_String::as_utf8_string(msg)); 2288 } 2289 } 2290 2291 // After this many redefines, the stack trace is unreliable. 2292 const int MAX_VERSION = USHRT_MAX; 2293 2294 static inline bool version_matches(Method* method, int version) { 2295 assert(version < MAX_VERSION, "version is too big"); 2296 return method != nullptr && (method->constants()->version() == version); 2297 } 2298 2299 // This class provides a simple wrapper over the internal structure of 2300 // exception backtrace to insulate users of the backtrace from needing 2301 // to know what it looks like. 2302 // The code of this class is not GC safe. Allocations can only happen 2303 // in expand(). 2304 class BacktraceBuilder: public StackObj { 2305 friend class BacktraceIterator; 2306 private: 2307 Handle _backtrace; 2308 objArrayOop _head; 2309 typeArrayOop _methods; 2310 typeArrayOop _bcis; 2311 objArrayOop _mirrors; 2312 typeArrayOop _names; // Needed to insulate method name against redefinition. 2313 // True if the top frame of the backtrace is omitted because it shall be hidden. 2314 bool _has_hidden_top_frame; 2315 int _index; 2316 NoSafepointVerifier _nsv; 2317 2318 enum { 2319 trace_methods_offset = java_lang_Throwable::trace_methods_offset, 2320 trace_bcis_offset = java_lang_Throwable::trace_bcis_offset, 2321 trace_mirrors_offset = java_lang_Throwable::trace_mirrors_offset, 2322 trace_names_offset = java_lang_Throwable::trace_names_offset, 2323 trace_conts_offset = java_lang_Throwable::trace_conts_offset, 2324 trace_next_offset = java_lang_Throwable::trace_next_offset, 2325 trace_hidden_offset = java_lang_Throwable::trace_hidden_offset, 2326 trace_size = java_lang_Throwable::trace_size, 2327 trace_chunk_size = java_lang_Throwable::trace_chunk_size 2328 }; 2329 2330 // get info out of chunks 2331 static typeArrayOop get_methods(objArrayHandle chunk) { 2332 typeArrayOop methods = typeArrayOop(chunk->obj_at(trace_methods_offset)); 2333 assert(methods != nullptr, "method array should be initialized in backtrace"); 2334 return methods; 2335 } 2336 static typeArrayOop get_bcis(objArrayHandle chunk) { 2337 typeArrayOop bcis = typeArrayOop(chunk->obj_at(trace_bcis_offset)); 2338 assert(bcis != nullptr, "bci array should be initialized in backtrace"); 2339 return bcis; 2340 } 2341 static objArrayOop get_mirrors(objArrayHandle chunk) { 2342 objArrayOop mirrors = objArrayOop(chunk->obj_at(trace_mirrors_offset)); 2343 assert(mirrors != nullptr, "mirror array should be initialized in backtrace"); 2344 return mirrors; 2345 } 2346 static typeArrayOop get_names(objArrayHandle chunk) { 2347 typeArrayOop names = typeArrayOop(chunk->obj_at(trace_names_offset)); 2348 assert(names != nullptr, "names array should be initialized in backtrace"); 2349 return names; 2350 } 2351 static bool has_hidden_top_frame(objArrayHandle chunk) { 2352 oop hidden = chunk->obj_at(trace_hidden_offset); 2353 return hidden != nullptr; 2354 } 2355 2356 public: 2357 2358 // constructor for new backtrace 2359 BacktraceBuilder(TRAPS): _head(nullptr), _methods(nullptr), _bcis(nullptr), _mirrors(nullptr), _names(nullptr), _has_hidden_top_frame(false) { 2360 expand(CHECK); 2361 _backtrace = Handle(THREAD, _head); 2362 _index = 0; 2363 } 2364 2365 BacktraceBuilder(Thread* thread, objArrayHandle backtrace) { 2366 _methods = get_methods(backtrace); 2367 _bcis = get_bcis(backtrace); 2368 _mirrors = get_mirrors(backtrace); 2369 _names = get_names(backtrace); 2370 _has_hidden_top_frame = has_hidden_top_frame(backtrace); 2371 assert(_methods->length() == _bcis->length() && 2372 _methods->length() == _mirrors->length() && 2373 _mirrors->length() == _names->length(), 2374 "method and source information arrays should match"); 2375 2376 // head is the preallocated backtrace 2377 _head = backtrace(); 2378 _backtrace = Handle(thread, _head); 2379 _index = 0; 2380 } 2381 2382 void expand(TRAPS) { 2383 objArrayHandle old_head(THREAD, _head); 2384 PauseNoSafepointVerifier pnsv(&_nsv); 2385 2386 objArrayOop head = oopFactory::new_objectArray(trace_size, CHECK); 2387 objArrayHandle new_head(THREAD, head); 2388 2389 typeArrayOop methods = oopFactory::new_shortArray(trace_chunk_size, CHECK); 2390 typeArrayHandle new_methods(THREAD, methods); 2391 2392 typeArrayOop bcis = oopFactory::new_intArray(trace_chunk_size, CHECK); 2393 typeArrayHandle new_bcis(THREAD, bcis); 2394 2395 objArrayOop mirrors = oopFactory::new_objectArray(trace_chunk_size, CHECK); 2396 objArrayHandle new_mirrors(THREAD, mirrors); 2397 2398 typeArrayOop names = oopFactory::new_symbolArray(trace_chunk_size, CHECK); 2399 typeArrayHandle new_names(THREAD, names); 2400 2401 if (!old_head.is_null()) { 2402 old_head->obj_at_put(trace_next_offset, new_head()); 2403 } 2404 new_head->obj_at_put(trace_methods_offset, new_methods()); 2405 new_head->obj_at_put(trace_bcis_offset, new_bcis()); 2406 new_head->obj_at_put(trace_mirrors_offset, new_mirrors()); 2407 new_head->obj_at_put(trace_names_offset, new_names()); 2408 new_head->obj_at_put(trace_hidden_offset, nullptr); 2409 2410 _head = new_head(); 2411 _methods = new_methods(); 2412 _bcis = new_bcis(); 2413 _mirrors = new_mirrors(); 2414 _names = new_names(); 2415 _index = 0; 2416 } 2417 2418 oop backtrace() { 2419 return _backtrace(); 2420 } 2421 2422 inline void push(Method* method, int bci, TRAPS) { 2423 // Smear the -1 bci to 0 since the array only holds unsigned 2424 // shorts. The later line number lookup would just smear the -1 2425 // to a 0 even if it could be recorded. 2426 if (bci == SynchronizationEntryBCI) bci = 0; 2427 2428 if (_index >= trace_chunk_size) { 2429 methodHandle mhandle(THREAD, method); 2430 expand(CHECK); 2431 method = mhandle(); 2432 } 2433 2434 _methods->ushort_at_put(_index, method->orig_method_idnum()); 2435 _bcis->int_at_put(_index, Backtrace::merge_bci_and_version(bci, method->constants()->version())); 2436 2437 // Note:this doesn't leak symbols because the mirror in the backtrace keeps the 2438 // klass owning the symbols alive so their refcounts aren't decremented. 2439 Symbol* name = method->name(); 2440 _names->symbol_at_put(_index, name); 2441 2442 // We need to save the mirrors in the backtrace to keep the class 2443 // from being unloaded while we still have this stack trace. 2444 assert(method->method_holder()->java_mirror() != nullptr, "never push null for mirror"); 2445 _mirrors->obj_at_put(_index, method->method_holder()->java_mirror()); 2446 2447 _index++; 2448 } 2449 2450 void set_has_hidden_top_frame() { 2451 if (!_has_hidden_top_frame) { 2452 // It would be nice to add java/lang/Boolean::TRUE here 2453 // to indicate that this backtrace has a hidden top frame. 2454 // But this code is used before TRUE is allocated. 2455 // Therefore let's just use an arbitrary legal oop 2456 // available right here. _methods is a short[]. 2457 assert(_methods != nullptr, "we need a legal oop"); 2458 _has_hidden_top_frame = true; 2459 _head->obj_at_put(trace_hidden_offset, _methods); 2460 } 2461 } 2462 }; 2463 2464 struct BacktraceElement : public StackObj { 2465 int _method_id; 2466 int _bci; 2467 int _version; 2468 Symbol* _name; 2469 Handle _mirror; 2470 BacktraceElement(Handle mirror, int mid, int version, int bci, Symbol* name) : 2471 _method_id(mid), _bci(bci), _version(version), _name(name), _mirror(mirror) {} 2472 }; 2473 2474 class BacktraceIterator : public StackObj { 2475 int _index; 2476 objArrayHandle _result; 2477 objArrayHandle _mirrors; 2478 typeArrayHandle _methods; 2479 typeArrayHandle _bcis; 2480 typeArrayHandle _names; 2481 2482 void init(objArrayHandle result, Thread* thread) { 2483 // Get method id, bci, version and mirror from chunk 2484 _result = result; 2485 if (_result.not_null()) { 2486 _methods = typeArrayHandle(thread, BacktraceBuilder::get_methods(_result)); 2487 _bcis = typeArrayHandle(thread, BacktraceBuilder::get_bcis(_result)); 2488 _mirrors = objArrayHandle(thread, BacktraceBuilder::get_mirrors(_result)); 2489 _names = typeArrayHandle(thread, BacktraceBuilder::get_names(_result)); 2490 _index = 0; 2491 } 2492 } 2493 public: 2494 BacktraceIterator(objArrayHandle result, Thread* thread) { 2495 init(result, thread); 2496 assert(_methods.is_null() || _methods->length() == java_lang_Throwable::trace_chunk_size, "lengths don't match"); 2497 } 2498 2499 BacktraceElement next(Thread* thread) { 2500 BacktraceElement e (Handle(thread, _mirrors->obj_at(_index)), 2501 _methods->ushort_at(_index), 2502 Backtrace::version_at(_bcis->int_at(_index)), 2503 Backtrace::bci_at(_bcis->int_at(_index)), 2504 _names->symbol_at(_index)); 2505 _index++; 2506 2507 if (_index >= java_lang_Throwable::trace_chunk_size) { 2508 int next_offset = java_lang_Throwable::trace_next_offset; 2509 // Get next chunk 2510 objArrayHandle result (thread, objArrayOop(_result->obj_at(next_offset))); 2511 init(result, thread); 2512 } 2513 return e; 2514 } 2515 2516 bool repeat() { 2517 return _result.not_null() && _mirrors->obj_at(_index) != nullptr; 2518 } 2519 }; 2520 2521 2522 // Print stack trace element to resource allocated buffer 2523 static void print_stack_element_to_stream(outputStream* st, Handle mirror, int method_id, 2524 int version, int bci, Symbol* name) { 2525 ResourceMark rm; 2526 2527 // Get strings and string lengths 2528 InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror())); 2529 const char* klass_name = holder->external_name(); 2530 int buf_len = (int)strlen(klass_name); 2531 2532 char* method_name = name->as_C_string(); 2533 buf_len += (int)strlen(method_name); 2534 2535 char* source_file_name = nullptr; 2536 Symbol* source = Backtrace::get_source_file_name(holder, version); 2537 if (source != nullptr) { 2538 source_file_name = source->as_C_string(); 2539 buf_len += (int)strlen(source_file_name); 2540 } 2541 2542 char *module_name = nullptr, *module_version = nullptr; 2543 ModuleEntry* module = holder->module(); 2544 if (module->is_named()) { 2545 module_name = module->name()->as_C_string(); 2546 buf_len += (int)strlen(module_name); 2547 if (module->version() != nullptr) { 2548 module_version = module->version()->as_C_string(); 2549 buf_len += (int)strlen(module_version); 2550 } 2551 } 2552 2553 // Allocate temporary buffer with extra space for formatting and line number 2554 const size_t buf_size = buf_len + 64; 2555 char* buf = NEW_RESOURCE_ARRAY(char, buf_size); 2556 2557 // Print stack trace line in buffer 2558 size_t buf_off = os::snprintf_checked(buf, buf_size, "\tat %s.%s(", klass_name, method_name); 2559 2560 // Print module information 2561 if (module_name != nullptr) { 2562 if (module_version != nullptr) { 2563 buf_off += os::snprintf_checked(buf + buf_off, buf_size - buf_off, "%s@%s/", module_name, module_version); 2564 } else { 2565 buf_off += os::snprintf_checked(buf + buf_off, buf_size - buf_off, "%s/", module_name); 2566 } 2567 } 2568 2569 // The method can be null if the requested class version is gone 2570 Method* method = holder->method_with_orig_idnum(method_id, version); 2571 if (!version_matches(method, version)) { 2572 strcat(buf, "Redefined)"); 2573 } else { 2574 int line_number = Backtrace::get_line_number(method, bci); 2575 if (line_number == -2) { 2576 strcat(buf, "Native Method)"); 2577 } else { 2578 if (source_file_name != nullptr && (line_number != -1)) { 2579 // Sourcename and linenumber 2580 buf_off += os::snprintf_checked(buf + buf_off, buf_size - buf_off, "%s:%d)", source_file_name, line_number); 2581 } else if (source_file_name != nullptr) { 2582 // Just sourcename 2583 buf_off += os::snprintf_checked(buf + buf_off, buf_size - buf_off, "%s)", source_file_name); 2584 } else { 2585 // Neither sourcename nor linenumber 2586 buf_off += os::snprintf_checked(buf + buf_off, buf_size - buf_off, "Unknown Source)"); 2587 } 2588 nmethod* nm = method->code(); 2589 if (WizardMode && nm != nullptr) { 2590 os::snprintf_checked(buf + buf_off, buf_size - buf_off, "(nmethod " INTPTR_FORMAT ")", (intptr_t)nm); 2591 } 2592 } 2593 } 2594 2595 st->print_cr("%s", buf); 2596 } 2597 2598 void java_lang_Throwable::print_stack_element(outputStream *st, Method* method, int bci) { 2599 Handle mirror (Thread::current(), method->method_holder()->java_mirror()); 2600 int method_id = method->orig_method_idnum(); 2601 int version = method->constants()->version(); 2602 print_stack_element_to_stream(st, mirror, method_id, version, bci, method->name()); 2603 } 2604 2605 /** 2606 * Print the throwable message and its stack trace plus all causes by walking the 2607 * cause chain. The output looks the same as of Throwable.printStackTrace(). 2608 */ 2609 void java_lang_Throwable::print_stack_trace(Handle throwable, outputStream* st) { 2610 // First, print the message. 2611 print(throwable(), st); 2612 st->cr(); 2613 2614 // Now print the stack trace. 2615 JavaThread* THREAD = JavaThread::current(); // For exception macros. 2616 while (throwable.not_null()) { 2617 objArrayHandle result (THREAD, objArrayOop(backtrace(throwable()))); 2618 if (result.is_null()) { 2619 st->print_raw_cr("\t<<no stack trace available>>"); 2620 return; 2621 } 2622 BacktraceIterator iter(result, THREAD); 2623 2624 while (iter.repeat()) { 2625 BacktraceElement bte = iter.next(THREAD); 2626 print_stack_element_to_stream(st, bte._mirror, bte._method_id, bte._version, bte._bci, bte._name); 2627 } 2628 if (THREAD->can_call_java()) { 2629 // Call getCause() which doesn't necessarily return the _cause field. 2630 ExceptionMark em(THREAD); 2631 JavaValue cause(T_OBJECT); 2632 JavaCalls::call_virtual(&cause, 2633 throwable, 2634 throwable->klass(), 2635 vmSymbols::getCause_name(), 2636 vmSymbols::void_throwable_signature(), 2637 THREAD); 2638 // Ignore any exceptions. we are in the middle of exception handling. Same as classic VM. 2639 if (HAS_PENDING_EXCEPTION) { 2640 CLEAR_PENDING_EXCEPTION; 2641 throwable = Handle(); 2642 } else { 2643 throwable = Handle(THREAD, cause.get_oop()); 2644 if (throwable.not_null()) { 2645 st->print("Caused by: "); 2646 print(throwable(), st); 2647 st->cr(); 2648 } 2649 } 2650 } else { 2651 st->print_raw_cr("<<cannot call Java to get cause>>"); 2652 return; 2653 } 2654 } 2655 } 2656 2657 /** 2658 * Print the throwable stack trace by calling the Java method java.lang.Throwable.printStackTrace(). 2659 */ 2660 void java_lang_Throwable::java_printStackTrace(Handle throwable, TRAPS) { 2661 assert(throwable->is_a(vmClasses::Throwable_klass()), "Throwable instance expected"); 2662 JavaValue result(T_VOID); 2663 JavaCalls::call_virtual(&result, 2664 throwable, 2665 vmClasses::Throwable_klass(), 2666 vmSymbols::printStackTrace_name(), 2667 vmSymbols::void_method_signature(), 2668 THREAD); 2669 } 2670 2671 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method, TRAPS) { 2672 if (!StackTraceInThrowable) return; 2673 ResourceMark rm(THREAD); 2674 2675 // Start out by clearing the backtrace for this object, in case the VM 2676 // runs out of memory while allocating the stack trace 2677 set_backtrace(throwable(), nullptr); 2678 // Clear lazily constructed Java level stacktrace if refilling occurs 2679 // This is unnecessary in 1.7+ but harmless 2680 clear_stacktrace(throwable()); 2681 2682 int max_depth = MaxJavaStackTraceDepth; 2683 JavaThread* thread = THREAD; 2684 2685 BacktraceBuilder bt(CHECK); 2686 2687 // If there is no Java frame just return the method that was being called 2688 // with bci 0 2689 if (!thread->has_last_Java_frame()) { 2690 if (max_depth >= 1 && method() != nullptr) { 2691 bt.push(method(), 0, CHECK); 2692 log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), 1); 2693 set_depth(throwable(), 1); 2694 set_backtrace(throwable(), bt.backtrace()); 2695 } 2696 return; 2697 } 2698 2699 // Instead of using vframe directly, this version of fill_in_stack_trace 2700 // basically handles everything by hand. This significantly improved the 2701 // speed of this method call up to 28.5% on Solaris sparc. 27.1% on Windows. 2702 // See bug 6333838 for more details. 2703 // The "ASSERT" here is to verify this method generates the exactly same stack 2704 // trace as utilizing vframe. 2705 #ifdef ASSERT 2706 vframeStream st(thread, false /* stop_at_java_call_stub */, false /* process_frames */); 2707 #endif 2708 int total_count = 0; 2709 RegisterMap map(thread, 2710 RegisterMap::UpdateMap::skip, 2711 RegisterMap::ProcessFrames::skip, 2712 RegisterMap::WalkContinuation::include); 2713 int decode_offset = 0; 2714 nmethod* nm = nullptr; 2715 bool skip_fillInStackTrace_check = false; 2716 bool skip_throwableInit_check = false; 2717 bool skip_hidden = !ShowHiddenFrames; 2718 bool show_carrier = ShowCarrierFrames; 2719 ContinuationEntry* cont_entry = thread->last_continuation(); 2720 for (frame fr = thread->last_frame(); max_depth == 0 || max_depth != total_count;) { 2721 Method* method = nullptr; 2722 int bci = 0; 2723 2724 // Compiled java method case. 2725 if (decode_offset != 0) { 2726 DebugInfoReadStream stream(nm, decode_offset); 2727 decode_offset = stream.read_int(); 2728 method = (Method*)nm->metadata_at(stream.read_int()); 2729 bci = stream.read_bci(); 2730 } else { 2731 if (fr.is_first_frame()) break; 2732 2733 if (Continuation::is_continuation_enterSpecial(fr)) { 2734 assert(cont_entry == Continuation::get_continuation_entry_for_entry_frame(thread, fr), ""); 2735 if (!show_carrier && cont_entry->is_virtual_thread()) { 2736 break; 2737 } 2738 cont_entry = cont_entry->parent(); 2739 } 2740 2741 address pc = fr.pc(); 2742 if (fr.is_interpreted_frame()) { 2743 address bcp; 2744 if (!map.in_cont()) { 2745 bcp = fr.interpreter_frame_bcp(); 2746 method = fr.interpreter_frame_method(); 2747 } else { 2748 bcp = map.stack_chunk()->interpreter_frame_bcp(fr); 2749 method = map.stack_chunk()->interpreter_frame_method(fr); 2750 } 2751 bci = method->bci_from(bcp); 2752 fr = fr.sender(&map); 2753 } else { 2754 CodeBlob* cb = fr.cb(); 2755 // HMMM QQQ might be nice to have frame return nm as null if cb is non-null 2756 // but non nmethod 2757 fr = fr.sender(&map); 2758 if (cb == nullptr || !cb->is_nmethod()) { 2759 continue; 2760 } 2761 nm = cb->as_nmethod(); 2762 assert(nm->method() != nullptr, "must be"); 2763 if (nm->method()->is_native()) { 2764 method = nm->method(); 2765 bci = 0; 2766 } else { 2767 PcDesc* pd = nm->pc_desc_at(pc); 2768 decode_offset = pd->scope_decode_offset(); 2769 // if decode_offset is not equal to 0, it will execute the 2770 // "compiled java method case" at the beginning of the loop. 2771 continue; 2772 } 2773 } 2774 } 2775 #ifdef ASSERT 2776 if (!st.at_end()) { // TODO LOOM remove once we show only vthread trace 2777 assert(st.method() == method && st.bci() == bci, "Wrong stack trace"); 2778 st.next(); 2779 } 2780 #endif 2781 2782 // the format of the stacktrace will be: 2783 // - 1 or more fillInStackTrace frames for the exception class (skipped) 2784 // - 0 or more <init> methods for the exception class (skipped) 2785 // - rest of the stack 2786 2787 if (!skip_fillInStackTrace_check) { 2788 if (method->name() == vmSymbols::fillInStackTrace_name() && 2789 throwable->is_a(method->method_holder())) { 2790 continue; 2791 } 2792 else { 2793 skip_fillInStackTrace_check = true; // gone past them all 2794 } 2795 } 2796 if (!skip_throwableInit_check) { 2797 assert(skip_fillInStackTrace_check, "logic error in backtrace filtering"); 2798 2799 // skip <init> methods of the exception class and superclasses 2800 // This is similar to classic VM. 2801 if (method->name() == vmSymbols::object_initializer_name() && 2802 throwable->is_a(method->method_holder())) { 2803 continue; 2804 } else { 2805 // there are none or we've seen them all - either way stop checking 2806 skip_throwableInit_check = true; 2807 } 2808 } 2809 if (method->is_hidden() || method->is_continuation_enter_intrinsic()) { 2810 if (skip_hidden) { 2811 if (total_count == 0) { 2812 // The top frame will be hidden from the stack trace. 2813 bt.set_has_hidden_top_frame(); 2814 } 2815 continue; 2816 } 2817 } 2818 2819 bt.push(method, bci, CHECK); 2820 total_count++; 2821 } 2822 2823 log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), total_count); 2824 2825 // Put completed stack trace into throwable object 2826 set_backtrace(throwable(), bt.backtrace()); 2827 set_depth(throwable(), total_count); 2828 } 2829 2830 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method) { 2831 // No-op if stack trace is disabled 2832 if (!StackTraceInThrowable) { 2833 return; 2834 } 2835 2836 // Disable stack traces for some preallocated out of memory errors 2837 if (!Universe::should_fill_in_stack_trace(throwable)) { 2838 return; 2839 } 2840 2841 JavaThread* THREAD = JavaThread::current(); // For exception macros. 2842 PreserveExceptionMark pm(THREAD); 2843 2844 fill_in_stack_trace(throwable, method, THREAD); 2845 // Ignore exceptions thrown during stack trace filling (OOM) and reinstall the 2846 // original exception via the PreserveExceptionMark destructor. 2847 CLEAR_PENDING_EXCEPTION; 2848 } 2849 2850 void java_lang_Throwable::allocate_backtrace(Handle throwable, TRAPS) { 2851 // Allocate stack trace - backtrace is created but not filled in 2852 2853 // No-op if stack trace is disabled 2854 if (!StackTraceInThrowable) return; 2855 BacktraceBuilder bt(CHECK); // creates a backtrace 2856 set_backtrace(throwable(), bt.backtrace()); 2857 } 2858 2859 2860 void java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(Handle throwable) { 2861 // Fill in stack trace into preallocated backtrace (no GC) 2862 2863 // No-op if stack trace is disabled 2864 if (!StackTraceInThrowable) return; 2865 2866 assert(throwable->is_a(vmClasses::Throwable_klass()), "sanity check"); 2867 2868 JavaThread* THREAD = JavaThread::current(); // For exception macros. 2869 2870 objArrayHandle backtrace (THREAD, (objArrayOop)java_lang_Throwable::backtrace(throwable())); 2871 assert(backtrace.not_null(), "backtrace should have been preallocated"); 2872 2873 ResourceMark rm(THREAD); 2874 vframeStream st(THREAD, false /* stop_at_java_call_stub */, false /* process_frames */); 2875 2876 BacktraceBuilder bt(THREAD, backtrace); 2877 2878 // Unlike fill_in_stack_trace we do not skip fillInStackTrace or throwable init 2879 // methods as preallocated errors aren't created by "java" code. 2880 2881 // fill in as much stack trace as possible 2882 int chunk_count = 0; 2883 for (;!st.at_end(); st.next()) { 2884 bt.push(st.method(), st.bci(), CHECK); 2885 chunk_count++; 2886 2887 // Bail-out for deep stacks 2888 if (chunk_count >= trace_chunk_size) break; 2889 } 2890 set_depth(throwable(), chunk_count); 2891 log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), chunk_count); 2892 2893 // We support the Throwable immutability protocol defined for Java 7. 2894 java_lang_Throwable::set_stacktrace(throwable(), java_lang_Throwable::unassigned_stacktrace()); 2895 assert(java_lang_Throwable::unassigned_stacktrace() != nullptr, "not initialized"); 2896 } 2897 2898 void java_lang_Throwable::get_stack_trace_elements(int depth, Handle backtrace, 2899 objArrayHandle stack_trace_array_h, TRAPS) { 2900 2901 if (backtrace.is_null() || stack_trace_array_h.is_null()) { 2902 THROW(vmSymbols::java_lang_NullPointerException()); 2903 } 2904 2905 assert(stack_trace_array_h->is_objArray(), "Stack trace array should be an array of StackTraceElenent"); 2906 2907 if (stack_trace_array_h->length() != depth) { 2908 THROW(vmSymbols::java_lang_IndexOutOfBoundsException()); 2909 } 2910 2911 objArrayHandle result(THREAD, objArrayOop(backtrace())); 2912 BacktraceIterator iter(result, THREAD); 2913 2914 int index = 0; 2915 while (iter.repeat()) { 2916 BacktraceElement bte = iter.next(THREAD); 2917 2918 Handle stack_trace_element(THREAD, stack_trace_array_h->obj_at(index++)); 2919 2920 if (stack_trace_element.is_null()) { 2921 THROW(vmSymbols::java_lang_NullPointerException()); 2922 } 2923 2924 InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror())); 2925 methodHandle method (THREAD, holder->method_with_orig_idnum(bte._method_id, bte._version)); 2926 2927 java_lang_StackTraceElement::fill_in(stack_trace_element, holder, 2928 method, 2929 bte._version, 2930 bte._bci, 2931 bte._name, 2932 CHECK); 2933 } 2934 } 2935 2936 Handle java_lang_Throwable::create_initialization_error(JavaThread* current, Handle throwable) { 2937 // Creates an ExceptionInInitializerError to be recorded as the initialization error when class initialization 2938 // failed due to the passed in 'throwable'. We cannot save 'throwable' directly due to issues with keeping alive 2939 // all objects referenced via its stacktrace. So instead we save a new EIIE instance, with the same message and 2940 // symbolic stacktrace of 'throwable'. 2941 assert(throwable.not_null(), "shouldn't be"); 2942 2943 // Now create the message from the original exception and thread name. 2944 ResourceMark rm(current); 2945 const char *message = nullptr; 2946 oop detailed_message = java_lang_Throwable::message(throwable()); 2947 if (detailed_message != nullptr) { 2948 message = java_lang_String::as_utf8_string(detailed_message); 2949 } 2950 stringStream st; 2951 st.print("Exception %s%s ", throwable()->klass()->name()->as_klass_external_name(), 2952 message == nullptr ? "" : ":"); 2953 if (message == nullptr) { 2954 st.print("[in thread \"%s\"]", current->name()); 2955 } else { 2956 st.print("%s [in thread \"%s\"]", message, current->name()); 2957 } 2958 2959 Symbol* exception_name = vmSymbols::java_lang_ExceptionInInitializerError(); 2960 Handle init_error = Exceptions::new_exception(current, exception_name, st.as_string()); 2961 // If new_exception returns a different exception while creating the exception, 2962 // abandon the attempt to save the initialization error and return null. 2963 if (init_error->klass()->name() != exception_name) { 2964 log_info(class, init)("Exception %s thrown while saving initialization exception", 2965 init_error->klass()->external_name()); 2966 return Handle(); 2967 } 2968 2969 // Call to java to fill in the stack trace and clear declaringClassObject to 2970 // not keep classes alive in the stack trace. 2971 // call this: public StackTraceElement[] getStackTrace() 2972 JavaValue result(T_ARRAY); 2973 JavaCalls::call_virtual(&result, throwable, 2974 vmClasses::Throwable_klass(), 2975 vmSymbols::getStackTrace_name(), 2976 vmSymbols::getStackTrace_signature(), 2977 current); 2978 if (!current->has_pending_exception()){ 2979 Handle stack_trace(current, result.get_oop()); 2980 assert(stack_trace->is_objArray(), "Should be an array"); 2981 java_lang_Throwable::set_stacktrace(init_error(), stack_trace()); 2982 // Clear backtrace because the stacktrace should be used instead. 2983 set_backtrace(init_error(), nullptr); 2984 } else { 2985 log_info(class, init)("Exception thrown while getting stack trace for initialization exception %s", 2986 init_error->klass()->external_name()); 2987 current->clear_pending_exception(); 2988 } 2989 2990 return init_error; 2991 } 2992 2993 bool java_lang_Throwable::get_top_method_and_bci(oop throwable, Method** method, int* bci) { 2994 JavaThread* current = JavaThread::current(); 2995 objArrayHandle result(current, objArrayOop(backtrace(throwable))); 2996 BacktraceIterator iter(result, current); 2997 // No backtrace available. 2998 if (!iter.repeat()) return false; 2999 3000 // If the exception happened in a frame that has been hidden, i.e., 3001 // omitted from the back trace, we can not compute the message. 3002 oop hidden = ((objArrayOop)backtrace(throwable))->obj_at(trace_hidden_offset); 3003 if (hidden != nullptr) { 3004 return false; 3005 } 3006 3007 // Get first backtrace element. 3008 BacktraceElement bte = iter.next(current); 3009 3010 InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror())); 3011 assert(holder != nullptr, "first element should be non-null"); 3012 Method* m = holder->method_with_orig_idnum(bte._method_id, bte._version); 3013 3014 // Original version is no longer available. 3015 if (m == nullptr || !version_matches(m, bte._version)) { 3016 return false; 3017 } 3018 3019 *method = m; 3020 *bci = bte._bci; 3021 return true; 3022 } 3023 3024 oop java_lang_StackTraceElement::create(const methodHandle& method, int bci, TRAPS) { 3025 // Allocate java.lang.StackTraceElement instance 3026 InstanceKlass* k = vmClasses::StackTraceElement_klass(); 3027 assert(k != nullptr, "must be loaded in 1.4+"); 3028 if (k->should_be_initialized()) { 3029 k->initialize(CHECK_NULL); 3030 } 3031 3032 Handle element = k->allocate_instance_handle(CHECK_NULL); 3033 3034 int version = method->constants()->version(); 3035 fill_in(element, method->method_holder(), method, version, bci, method->name(), CHECK_NULL); 3036 return element(); 3037 } 3038 3039 void java_lang_StackTraceElement::fill_in(Handle element, 3040 InstanceKlass* holder, const methodHandle& method, 3041 int version, int bci, Symbol* name, TRAPS) { 3042 assert(element->is_a(vmClasses::StackTraceElement_klass()), "sanity check"); 3043 3044 ResourceMark rm(THREAD); 3045 HandleMark hm(THREAD); 3046 3047 // Fill in class name 3048 Handle java_class(THREAD, holder->java_mirror()); 3049 oop classname = java_lang_Class::name(java_class, CHECK); 3050 java_lang_StackTraceElement::set_declaringClass(element(), classname); 3051 java_lang_StackTraceElement::set_declaringClassObject(element(), java_class()); 3052 3053 oop loader = holder->class_loader(); 3054 if (loader != nullptr) { 3055 oop loader_name = java_lang_ClassLoader::name(loader); 3056 if (loader_name != nullptr) 3057 java_lang_StackTraceElement::set_classLoaderName(element(), loader_name); 3058 } 3059 3060 // Fill in method name 3061 oop methodname = StringTable::intern(name, CHECK); 3062 java_lang_StackTraceElement::set_methodName(element(), methodname); 3063 3064 // Fill in module name and version 3065 ModuleEntry* module = holder->module(); 3066 if (module->is_named()) { 3067 oop module_name = StringTable::intern(module->name(), CHECK); 3068 java_lang_StackTraceElement::set_moduleName(element(), module_name); 3069 oop module_version; 3070 if (module->version() != nullptr) { 3071 module_version = StringTable::intern(module->version(), CHECK); 3072 } else { 3073 module_version = nullptr; 3074 } 3075 java_lang_StackTraceElement::set_moduleVersion(element(), module_version); 3076 } 3077 3078 if (method() == nullptr || !version_matches(method(), version)) { 3079 // The method was redefined, accurate line number information isn't available 3080 java_lang_StackTraceElement::set_fileName(element(), nullptr); 3081 java_lang_StackTraceElement::set_lineNumber(element(), -1); 3082 } else { 3083 Symbol* source; 3084 oop source_file; 3085 int line_number; 3086 decode_file_and_line(java_class, holder, version, method, bci, source, source_file, line_number, CHECK); 3087 3088 java_lang_StackTraceElement::set_fileName(element(), source_file); 3089 java_lang_StackTraceElement::set_lineNumber(element(), line_number); 3090 } 3091 } 3092 3093 void java_lang_StackTraceElement::decode_file_and_line(Handle java_class, 3094 InstanceKlass* holder, 3095 int version, 3096 const methodHandle& method, 3097 int bci, 3098 Symbol*& source, 3099 oop& source_file, 3100 int& line_number, TRAPS) { 3101 // Fill in source file name and line number. 3102 source = Backtrace::get_source_file_name(holder, version); 3103 source_file = java_lang_Class::source_file(java_class()); 3104 if (source != nullptr) { 3105 // Class was not redefined. We can trust its cache if set, 3106 // else we have to initialize it. 3107 if (source_file == nullptr) { 3108 source_file = StringTable::intern(source, CHECK); 3109 java_lang_Class::set_source_file(java_class(), source_file); 3110 } 3111 } else { 3112 // Class was redefined. Dump the cache if it was set. 3113 if (source_file != nullptr) { 3114 source_file = nullptr; 3115 java_lang_Class::set_source_file(java_class(), source_file); 3116 } 3117 } 3118 line_number = Backtrace::get_line_number(method(), bci); 3119 } 3120 3121 #if INCLUDE_JVMCI 3122 void java_lang_StackTraceElement::decode(const methodHandle& method, int bci, 3123 Symbol*& filename, int& line_number, TRAPS) { 3124 ResourceMark rm(THREAD); 3125 HandleMark hm(THREAD); 3126 3127 filename = nullptr; 3128 line_number = -1; 3129 3130 oop source_file; 3131 int version = method->constants()->version(); 3132 InstanceKlass* holder = method->method_holder(); 3133 Handle java_class(THREAD, holder->java_mirror()); 3134 decode_file_and_line(java_class, holder, version, method, bci, filename, source_file, line_number, CHECK); 3135 } 3136 #endif // INCLUDE_JVMCI 3137 3138 // java_lang_ClassFrameInfo 3139 3140 int java_lang_ClassFrameInfo::_classOrMemberName_offset; 3141 int java_lang_ClassFrameInfo::_flags_offset; 3142 3143 #define CLASSFRAMEINFO_FIELDS_DO(macro) \ 3144 macro(_classOrMemberName_offset, k, "classOrMemberName", object_signature, false); \ 3145 macro(_flags_offset, k, vmSymbols::flags_name(), int_signature, false) 3146 3147 void java_lang_ClassFrameInfo::compute_offsets() { 3148 InstanceKlass* k = vmClasses::ClassFrameInfo_klass(); 3149 CLASSFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3150 } 3151 3152 #if INCLUDE_CDS 3153 void java_lang_ClassFrameInfo::serialize_offsets(SerializeClosure* f) { 3154 CLASSFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3155 } 3156 #endif 3157 3158 static int get_flags(const methodHandle& m) { 3159 int flags = (jushort)( m->access_flags().as_short() & JVM_RECOGNIZED_METHOD_MODIFIERS ); 3160 if (m->is_object_initializer()) { 3161 flags |= java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR; 3162 } else { 3163 // Note: Static initializers can be here. Record them as plain methods. 3164 flags |= java_lang_invoke_MemberName::MN_IS_METHOD; 3165 } 3166 if (m->caller_sensitive()) { 3167 flags |= java_lang_invoke_MemberName::MN_CALLER_SENSITIVE; 3168 } 3169 if (m->is_hidden()) { 3170 flags |= java_lang_invoke_MemberName::MN_HIDDEN_MEMBER; 3171 } 3172 assert((flags & 0xFF000000) == 0, "unexpected flags"); 3173 return flags; 3174 } 3175 3176 oop java_lang_ClassFrameInfo::classOrMemberName(oop obj) { 3177 return obj->obj_field(_classOrMemberName_offset); 3178 } 3179 3180 int java_lang_ClassFrameInfo::flags(oop obj) { 3181 return obj->int_field(_flags_offset); 3182 } 3183 3184 void java_lang_ClassFrameInfo::init_class(Handle stackFrame, const methodHandle& m) { 3185 stackFrame->obj_field_put(_classOrMemberName_offset, m->method_holder()->java_mirror()); 3186 // flags is initialized when ClassFrameInfo object is constructed and retain the value 3187 int flags = java_lang_ClassFrameInfo::flags(stackFrame()) | get_flags(m); 3188 stackFrame->int_field_put(_flags_offset, flags); 3189 } 3190 3191 void java_lang_ClassFrameInfo::init_method(Handle stackFrame, const methodHandle& m, TRAPS) { 3192 oop rmethod_name = java_lang_invoke_ResolvedMethodName::find_resolved_method(m, CHECK); 3193 stackFrame->obj_field_put(_classOrMemberName_offset, rmethod_name); 3194 // flags is initialized when ClassFrameInfo object is constructed and retain the value 3195 int flags = java_lang_ClassFrameInfo::flags(stackFrame()) | get_flags(m); 3196 stackFrame->int_field_put(_flags_offset, flags); 3197 } 3198 3199 3200 // java_lang_StackFrameInfo 3201 3202 int java_lang_StackFrameInfo::_type_offset; 3203 int java_lang_StackFrameInfo::_name_offset; 3204 int java_lang_StackFrameInfo::_bci_offset; 3205 int java_lang_StackFrameInfo::_version_offset; 3206 int java_lang_StackFrameInfo::_contScope_offset; 3207 3208 #define STACKFRAMEINFO_FIELDS_DO(macro) \ 3209 macro(_type_offset, k, "type", object_signature, false); \ 3210 macro(_name_offset, k, "name", string_signature, false); \ 3211 macro(_bci_offset, k, "bci", int_signature, false); \ 3212 macro(_contScope_offset, k, "contScope", continuationscope_signature, false) 3213 3214 void java_lang_StackFrameInfo::compute_offsets() { 3215 InstanceKlass* k = vmClasses::StackFrameInfo_klass(); 3216 STACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3217 STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 3218 } 3219 3220 #if INCLUDE_CDS 3221 void java_lang_StackFrameInfo::serialize_offsets(SerializeClosure* f) { 3222 STACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3223 STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 3224 } 3225 #endif 3226 3227 Method* java_lang_StackFrameInfo::get_method(oop obj) { 3228 oop m = java_lang_ClassFrameInfo::classOrMemberName(obj); 3229 Method* method = java_lang_invoke_ResolvedMethodName::vmtarget(m); 3230 return method; 3231 } 3232 3233 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci, oop cont, TRAPS) { 3234 // set Method* or mid/cpref 3235 HandleMark hm(THREAD); 3236 Handle cont_h(THREAD, cont); 3237 java_lang_ClassFrameInfo::init_method(stackFrame, method, CHECK); 3238 3239 // set bci 3240 java_lang_StackFrameInfo::set_bci(stackFrame(), bci); 3241 // method may be redefined; store the version 3242 int version = method->constants()->version(); 3243 assert((jushort)version == version, "version should be short"); 3244 java_lang_StackFrameInfo::set_version(stackFrame(), (short)version); 3245 3246 oop contScope = cont_h() != nullptr ? jdk_internal_vm_Continuation::scope(cont_h()) : (oop)nullptr; 3247 java_lang_StackFrameInfo::set_contScope(stackFrame(), contScope); 3248 } 3249 3250 void java_lang_StackFrameInfo::to_stack_trace_element(Handle stackFrame, Handle stack_trace_element, TRAPS) { 3251 ResourceMark rm(THREAD); 3252 HandleMark hm(THREAD); 3253 3254 Method* method = java_lang_StackFrameInfo::get_method(stackFrame()); 3255 InstanceKlass* holder = method->method_holder(); 3256 short version = stackFrame->short_field(_version_offset); 3257 int bci = stackFrame->int_field(_bci_offset); 3258 Symbol* name = method->name(); 3259 java_lang_StackTraceElement::fill_in(stack_trace_element, holder, methodHandle(THREAD, method), version, bci, name, CHECK); 3260 } 3261 3262 oop java_lang_StackFrameInfo::type(oop obj) { 3263 return obj->obj_field(_type_offset); 3264 } 3265 3266 void java_lang_StackFrameInfo::set_type(oop obj, oop value) { 3267 obj->obj_field_put(_type_offset, value); 3268 } 3269 3270 oop java_lang_StackFrameInfo::name(oop obj) { 3271 return obj->obj_field(_name_offset); 3272 } 3273 3274 void java_lang_StackFrameInfo::set_name(oop obj, oop value) { 3275 obj->obj_field_put(_name_offset, value); 3276 } 3277 3278 void java_lang_StackFrameInfo::set_version(oop obj, short value) { 3279 obj->short_field_put(_version_offset, value); 3280 } 3281 3282 void java_lang_StackFrameInfo::set_bci(oop obj, int value) { 3283 assert(value >= 0 && value < max_jushort, "must be a valid bci value"); 3284 obj->int_field_put(_bci_offset, value); 3285 } 3286 3287 void java_lang_StackFrameInfo::set_contScope(oop obj, oop value) { 3288 obj->obj_field_put(_contScope_offset, value); 3289 } 3290 3291 int java_lang_LiveStackFrameInfo::_monitors_offset; 3292 int java_lang_LiveStackFrameInfo::_locals_offset; 3293 int java_lang_LiveStackFrameInfo::_operands_offset; 3294 int java_lang_LiveStackFrameInfo::_mode_offset; 3295 3296 #define LIVESTACKFRAMEINFO_FIELDS_DO(macro) \ 3297 macro(_monitors_offset, k, "monitors", object_array_signature, false); \ 3298 macro(_locals_offset, k, "locals", object_array_signature, false); \ 3299 macro(_operands_offset, k, "operands", object_array_signature, false); \ 3300 macro(_mode_offset, k, "mode", int_signature, false) 3301 3302 void java_lang_LiveStackFrameInfo::compute_offsets() { 3303 InstanceKlass* k = vmClasses::LiveStackFrameInfo_klass(); 3304 LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3305 } 3306 3307 #if INCLUDE_CDS 3308 void java_lang_LiveStackFrameInfo::serialize_offsets(SerializeClosure* f) { 3309 LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3310 } 3311 #endif 3312 3313 void java_lang_LiveStackFrameInfo::set_monitors(oop obj, oop value) { 3314 obj->obj_field_put(_monitors_offset, value); 3315 } 3316 3317 void java_lang_LiveStackFrameInfo::set_locals(oop obj, oop value) { 3318 obj->obj_field_put(_locals_offset, value); 3319 } 3320 3321 void java_lang_LiveStackFrameInfo::set_operands(oop obj, oop value) { 3322 obj->obj_field_put(_operands_offset, value); 3323 } 3324 3325 void java_lang_LiveStackFrameInfo::set_mode(oop obj, int value) { 3326 obj->int_field_put(_mode_offset, value); 3327 } 3328 3329 3330 // java_lang_AccessibleObject 3331 3332 int java_lang_reflect_AccessibleObject::_override_offset; 3333 3334 #define ACCESSIBLEOBJECT_FIELDS_DO(macro) \ 3335 macro(_override_offset, k, "override", bool_signature, false) 3336 3337 void java_lang_reflect_AccessibleObject::compute_offsets() { 3338 InstanceKlass* k = vmClasses::reflect_AccessibleObject_klass(); 3339 ACCESSIBLEOBJECT_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3340 } 3341 3342 #if INCLUDE_CDS 3343 void java_lang_reflect_AccessibleObject::serialize_offsets(SerializeClosure* f) { 3344 ACCESSIBLEOBJECT_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3345 } 3346 #endif 3347 3348 jboolean java_lang_reflect_AccessibleObject::override(oop reflect) { 3349 return (jboolean) reflect->bool_field(_override_offset); 3350 } 3351 3352 void java_lang_reflect_AccessibleObject::set_override(oop reflect, jboolean value) { 3353 reflect->bool_field_put(_override_offset, (int) value); 3354 } 3355 3356 // java_lang_reflect_Method 3357 3358 int java_lang_reflect_Method::_clazz_offset; 3359 int java_lang_reflect_Method::_name_offset; 3360 int java_lang_reflect_Method::_returnType_offset; 3361 int java_lang_reflect_Method::_parameterTypes_offset; 3362 int java_lang_reflect_Method::_exceptionTypes_offset; 3363 int java_lang_reflect_Method::_slot_offset; 3364 int java_lang_reflect_Method::_modifiers_offset; 3365 int java_lang_reflect_Method::_signature_offset; 3366 int java_lang_reflect_Method::_annotations_offset; 3367 int java_lang_reflect_Method::_parameter_annotations_offset; 3368 int java_lang_reflect_Method::_annotation_default_offset; 3369 3370 #define METHOD_FIELDS_DO(macro) \ 3371 macro(_clazz_offset, k, vmSymbols::clazz_name(), class_signature, false); \ 3372 macro(_name_offset, k, vmSymbols::name_name(), string_signature, false); \ 3373 macro(_returnType_offset, k, vmSymbols::returnType_name(), class_signature, false); \ 3374 macro(_parameterTypes_offset, k, vmSymbols::parameterTypes_name(), class_array_signature, false); \ 3375 macro(_exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), class_array_signature, false); \ 3376 macro(_slot_offset, k, vmSymbols::slot_name(), int_signature, false); \ 3377 macro(_modifiers_offset, k, vmSymbols::modifiers_name(), int_signature, false); \ 3378 macro(_signature_offset, k, vmSymbols::signature_name(), string_signature, false); \ 3379 macro(_annotations_offset, k, vmSymbols::annotations_name(), byte_array_signature, false); \ 3380 macro(_parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), byte_array_signature, false); \ 3381 macro(_annotation_default_offset, k, vmSymbols::annotation_default_name(), byte_array_signature, false); 3382 3383 void java_lang_reflect_Method::compute_offsets() { 3384 InstanceKlass* k = vmClasses::reflect_Method_klass(); 3385 METHOD_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3386 } 3387 3388 #if INCLUDE_CDS 3389 void java_lang_reflect_Method::serialize_offsets(SerializeClosure* f) { 3390 METHOD_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3391 } 3392 #endif 3393 3394 Handle java_lang_reflect_Method::create(TRAPS) { 3395 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3396 Klass* klass = vmClasses::reflect_Method_klass(); 3397 // This class is eagerly initialized during VM initialization, since we keep a reference 3398 // to one of the methods 3399 assert(InstanceKlass::cast(klass)->is_initialized(), "must be initialized"); 3400 return InstanceKlass::cast(klass)->allocate_instance_handle(THREAD); 3401 } 3402 3403 oop java_lang_reflect_Method::clazz(oop reflect) { 3404 return reflect->obj_field(_clazz_offset); 3405 } 3406 3407 void java_lang_reflect_Method::set_clazz(oop reflect, oop value) { 3408 reflect->obj_field_put(_clazz_offset, value); 3409 } 3410 3411 int java_lang_reflect_Method::slot(oop reflect) { 3412 return reflect->int_field(_slot_offset); 3413 } 3414 3415 void java_lang_reflect_Method::set_slot(oop reflect, int value) { 3416 reflect->int_field_put(_slot_offset, value); 3417 } 3418 3419 void java_lang_reflect_Method::set_name(oop method, oop value) { 3420 method->obj_field_put(_name_offset, value); 3421 } 3422 3423 oop java_lang_reflect_Method::return_type(oop method) { 3424 return method->obj_field(_returnType_offset); 3425 } 3426 3427 void java_lang_reflect_Method::set_return_type(oop method, oop value) { 3428 method->obj_field_put(_returnType_offset, value); 3429 } 3430 3431 oop java_lang_reflect_Method::parameter_types(oop method) { 3432 return method->obj_field(_parameterTypes_offset); 3433 } 3434 3435 void java_lang_reflect_Method::set_parameter_types(oop method, oop value) { 3436 method->obj_field_put(_parameterTypes_offset, value); 3437 } 3438 3439 void java_lang_reflect_Method::set_exception_types(oop method, oop value) { 3440 method->obj_field_put(_exceptionTypes_offset, value); 3441 } 3442 3443 void java_lang_reflect_Method::set_modifiers(oop method, int value) { 3444 method->int_field_put(_modifiers_offset, value); 3445 } 3446 3447 void java_lang_reflect_Method::set_signature(oop method, oop value) { 3448 method->obj_field_put(_signature_offset, value); 3449 } 3450 3451 void java_lang_reflect_Method::set_annotations(oop method, oop value) { 3452 method->obj_field_put(_annotations_offset, value); 3453 } 3454 3455 void java_lang_reflect_Method::set_parameter_annotations(oop method, oop value) { 3456 method->obj_field_put(_parameter_annotations_offset, value); 3457 } 3458 3459 void java_lang_reflect_Method::set_annotation_default(oop method, oop value) { 3460 method->obj_field_put(_annotation_default_offset, value); 3461 } 3462 3463 int java_lang_reflect_Constructor::_clazz_offset; 3464 int java_lang_reflect_Constructor::_parameterTypes_offset; 3465 int java_lang_reflect_Constructor::_exceptionTypes_offset; 3466 int java_lang_reflect_Constructor::_slot_offset; 3467 int java_lang_reflect_Constructor::_modifiers_offset; 3468 int java_lang_reflect_Constructor::_signature_offset; 3469 int java_lang_reflect_Constructor::_annotations_offset; 3470 int java_lang_reflect_Constructor::_parameter_annotations_offset; 3471 3472 #define CONSTRUCTOR_FIELDS_DO(macro) \ 3473 macro(_clazz_offset, k, vmSymbols::clazz_name(), class_signature, false); \ 3474 macro(_parameterTypes_offset, k, vmSymbols::parameterTypes_name(), class_array_signature, false); \ 3475 macro(_exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), class_array_signature, false); \ 3476 macro(_slot_offset, k, vmSymbols::slot_name(), int_signature, false); \ 3477 macro(_modifiers_offset, k, vmSymbols::modifiers_name(), int_signature, false); \ 3478 macro(_signature_offset, k, vmSymbols::signature_name(), string_signature, false); \ 3479 macro(_annotations_offset, k, vmSymbols::annotations_name(), byte_array_signature, false); \ 3480 macro(_parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), byte_array_signature, false); 3481 3482 void java_lang_reflect_Constructor::compute_offsets() { 3483 InstanceKlass* k = vmClasses::reflect_Constructor_klass(); 3484 CONSTRUCTOR_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3485 } 3486 3487 #if INCLUDE_CDS 3488 void java_lang_reflect_Constructor::serialize_offsets(SerializeClosure* f) { 3489 CONSTRUCTOR_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3490 } 3491 #endif 3492 3493 Handle java_lang_reflect_Constructor::create(TRAPS) { 3494 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3495 Symbol* name = vmSymbols::java_lang_reflect_Constructor(); 3496 Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH); 3497 InstanceKlass* ik = InstanceKlass::cast(k); 3498 // Ensure it is initialized 3499 ik->initialize(CHECK_NH); 3500 return ik->allocate_instance_handle(THREAD); 3501 } 3502 3503 oop java_lang_reflect_Constructor::clazz(oop reflect) { 3504 return reflect->obj_field(_clazz_offset); 3505 } 3506 3507 void java_lang_reflect_Constructor::set_clazz(oop reflect, oop value) { 3508 reflect->obj_field_put(_clazz_offset, value); 3509 } 3510 3511 oop java_lang_reflect_Constructor::parameter_types(oop constructor) { 3512 return constructor->obj_field(_parameterTypes_offset); 3513 } 3514 3515 void java_lang_reflect_Constructor::set_parameter_types(oop constructor, oop value) { 3516 constructor->obj_field_put(_parameterTypes_offset, value); 3517 } 3518 3519 void java_lang_reflect_Constructor::set_exception_types(oop constructor, oop value) { 3520 constructor->obj_field_put(_exceptionTypes_offset, value); 3521 } 3522 3523 int java_lang_reflect_Constructor::slot(oop reflect) { 3524 return reflect->int_field(_slot_offset); 3525 } 3526 3527 void java_lang_reflect_Constructor::set_slot(oop reflect, int value) { 3528 reflect->int_field_put(_slot_offset, value); 3529 } 3530 3531 void java_lang_reflect_Constructor::set_modifiers(oop constructor, int value) { 3532 constructor->int_field_put(_modifiers_offset, value); 3533 } 3534 3535 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) { 3536 constructor->obj_field_put(_signature_offset, value); 3537 } 3538 3539 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) { 3540 constructor->obj_field_put(_annotations_offset, value); 3541 } 3542 3543 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) { 3544 method->obj_field_put(_parameter_annotations_offset, value); 3545 } 3546 3547 int java_lang_reflect_Field::_clazz_offset; 3548 int java_lang_reflect_Field::_name_offset; 3549 int java_lang_reflect_Field::_type_offset; 3550 int java_lang_reflect_Field::_slot_offset; 3551 int java_lang_reflect_Field::_modifiers_offset; 3552 int java_lang_reflect_Field::_trusted_final_offset; 3553 int java_lang_reflect_Field::_signature_offset; 3554 int java_lang_reflect_Field::_annotations_offset; 3555 3556 #define FIELD_FIELDS_DO(macro) \ 3557 macro(_clazz_offset, k, vmSymbols::clazz_name(), class_signature, false); \ 3558 macro(_name_offset, k, vmSymbols::name_name(), string_signature, false); \ 3559 macro(_type_offset, k, vmSymbols::type_name(), class_signature, false); \ 3560 macro(_slot_offset, k, vmSymbols::slot_name(), int_signature, false); \ 3561 macro(_modifiers_offset, k, vmSymbols::modifiers_name(), int_signature, false); \ 3562 macro(_trusted_final_offset, k, vmSymbols::trusted_final_name(), bool_signature, false); \ 3563 macro(_signature_offset, k, vmSymbols::signature_name(), string_signature, false); \ 3564 macro(_annotations_offset, k, vmSymbols::annotations_name(), byte_array_signature, false); 3565 3566 void java_lang_reflect_Field::compute_offsets() { 3567 InstanceKlass* k = vmClasses::reflect_Field_klass(); 3568 FIELD_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3569 } 3570 3571 #if INCLUDE_CDS 3572 void java_lang_reflect_Field::serialize_offsets(SerializeClosure* f) { 3573 FIELD_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3574 } 3575 #endif 3576 3577 Handle java_lang_reflect_Field::create(TRAPS) { 3578 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3579 Symbol* name = vmSymbols::java_lang_reflect_Field(); 3580 Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH); 3581 InstanceKlass* ik = InstanceKlass::cast(k); 3582 // Ensure it is initialized 3583 ik->initialize(CHECK_NH); 3584 return ik->allocate_instance_handle(THREAD); 3585 } 3586 3587 oop java_lang_reflect_Field::clazz(oop reflect) { 3588 return reflect->obj_field(_clazz_offset); 3589 } 3590 3591 void java_lang_reflect_Field::set_clazz(oop reflect, oop value) { 3592 reflect->obj_field_put(_clazz_offset, value); 3593 } 3594 3595 oop java_lang_reflect_Field::name(oop field) { 3596 return field->obj_field(_name_offset); 3597 } 3598 3599 void java_lang_reflect_Field::set_name(oop field, oop value) { 3600 field->obj_field_put(_name_offset, value); 3601 } 3602 3603 oop java_lang_reflect_Field::type(oop field) { 3604 return field->obj_field(_type_offset); 3605 } 3606 3607 void java_lang_reflect_Field::set_type(oop field, oop value) { 3608 field->obj_field_put(_type_offset, value); 3609 } 3610 3611 int java_lang_reflect_Field::slot(oop reflect) { 3612 return reflect->int_field(_slot_offset); 3613 } 3614 3615 void java_lang_reflect_Field::set_slot(oop reflect, int value) { 3616 reflect->int_field_put(_slot_offset, value); 3617 } 3618 3619 int java_lang_reflect_Field::modifiers(oop field) { 3620 return field->int_field(_modifiers_offset); 3621 } 3622 3623 void java_lang_reflect_Field::set_modifiers(oop field, int value) { 3624 field->int_field_put(_modifiers_offset, value); 3625 } 3626 3627 void java_lang_reflect_Field::set_trusted_final(oop field) { 3628 field->bool_field_put(_trusted_final_offset, true); 3629 } 3630 3631 void java_lang_reflect_Field::set_signature(oop field, oop value) { 3632 field->obj_field_put(_signature_offset, value); 3633 } 3634 3635 void java_lang_reflect_Field::set_annotations(oop field, oop value) { 3636 field->obj_field_put(_annotations_offset, value); 3637 } 3638 3639 oop java_lang_reflect_RecordComponent::create(InstanceKlass* holder, RecordComponent* component, TRAPS) { 3640 // Allocate java.lang.reflect.RecordComponent instance 3641 HandleMark hm(THREAD); 3642 InstanceKlass* ik = vmClasses::RecordComponent_klass(); 3643 assert(ik != nullptr, "must be loaded"); 3644 ik->initialize(CHECK_NULL); 3645 3646 Handle element = ik->allocate_instance_handle(CHECK_NULL); 3647 3648 Handle decl_class(THREAD, holder->java_mirror()); 3649 java_lang_reflect_RecordComponent::set_clazz(element(), decl_class()); 3650 3651 Symbol* name = holder->constants()->symbol_at(component->name_index()); // name_index is a utf8 3652 oop component_name = StringTable::intern(name, CHECK_NULL); 3653 java_lang_reflect_RecordComponent::set_name(element(), component_name); 3654 3655 Symbol* type = holder->constants()->symbol_at(component->descriptor_index()); 3656 Handle component_type_h = 3657 SystemDictionary::find_java_mirror_for_type(type, holder, SignatureStream::NCDFError, CHECK_NULL); 3658 java_lang_reflect_RecordComponent::set_type(element(), component_type_h()); 3659 3660 Method* accessor_method = nullptr; 3661 { 3662 // Prepend "()" to type to create the full method signature. 3663 ResourceMark rm(THREAD); 3664 int sig_len = type->utf8_length() + 3; // "()" and null char 3665 char* sig = NEW_RESOURCE_ARRAY(char, sig_len); 3666 jio_snprintf(sig, sig_len, "%c%c%s", JVM_SIGNATURE_FUNC, JVM_SIGNATURE_ENDFUNC, type->as_C_string()); 3667 TempNewSymbol full_sig = SymbolTable::new_symbol(sig); 3668 accessor_method = holder->find_instance_method(name, full_sig, Klass::PrivateLookupMode::find); 3669 } 3670 3671 if (accessor_method != nullptr) { 3672 methodHandle method(THREAD, accessor_method); 3673 oop m = Reflection::new_method(method, false, CHECK_NULL); 3674 java_lang_reflect_RecordComponent::set_accessor(element(), m); 3675 } else { 3676 java_lang_reflect_RecordComponent::set_accessor(element(), nullptr); 3677 } 3678 3679 int sig_index = component->generic_signature_index(); 3680 if (sig_index > 0) { 3681 Symbol* sig = holder->constants()->symbol_at(sig_index); // sig_index is a utf8 3682 oop component_sig = StringTable::intern(sig, CHECK_NULL); 3683 java_lang_reflect_RecordComponent::set_signature(element(), component_sig); 3684 } else { 3685 java_lang_reflect_RecordComponent::set_signature(element(), nullptr); 3686 } 3687 3688 typeArrayOop annotation_oop = Annotations::make_java_array(component->annotations(), CHECK_NULL); 3689 java_lang_reflect_RecordComponent::set_annotations(element(), annotation_oop); 3690 3691 typeArrayOop type_annotation_oop = Annotations::make_java_array(component->type_annotations(), CHECK_NULL); 3692 java_lang_reflect_RecordComponent::set_typeAnnotations(element(), type_annotation_oop); 3693 3694 return element(); 3695 } 3696 3697 int reflect_ConstantPool::_oop_offset; 3698 3699 #define CONSTANTPOOL_FIELDS_DO(macro) \ 3700 macro(_oop_offset, k, "constantPoolOop", object_signature, false) 3701 3702 void reflect_ConstantPool::compute_offsets() { 3703 InstanceKlass* k = vmClasses::reflect_ConstantPool_klass(); 3704 // The field is called ConstantPool* in the sun.reflect.ConstantPool class. 3705 CONSTANTPOOL_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3706 } 3707 3708 #if INCLUDE_CDS 3709 void reflect_ConstantPool::serialize_offsets(SerializeClosure* f) { 3710 CONSTANTPOOL_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3711 } 3712 #endif 3713 3714 int java_lang_reflect_Parameter::_name_offset; 3715 int java_lang_reflect_Parameter::_modifiers_offset; 3716 int java_lang_reflect_Parameter::_index_offset; 3717 int java_lang_reflect_Parameter::_executable_offset; 3718 3719 #define PARAMETER_FIELDS_DO(macro) \ 3720 macro(_name_offset, k, vmSymbols::name_name(), string_signature, false); \ 3721 macro(_modifiers_offset, k, vmSymbols::modifiers_name(), int_signature, false); \ 3722 macro(_index_offset, k, vmSymbols::index_name(), int_signature, false); \ 3723 macro(_executable_offset, k, vmSymbols::executable_name(), executable_signature, false) 3724 3725 void java_lang_reflect_Parameter::compute_offsets() { 3726 InstanceKlass* k = vmClasses::reflect_Parameter_klass(); 3727 PARAMETER_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3728 } 3729 3730 #if INCLUDE_CDS 3731 void java_lang_reflect_Parameter::serialize_offsets(SerializeClosure* f) { 3732 PARAMETER_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3733 } 3734 #endif 3735 3736 Handle java_lang_reflect_Parameter::create(TRAPS) { 3737 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3738 Symbol* name = vmSymbols::java_lang_reflect_Parameter(); 3739 Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH); 3740 InstanceKlass* ik = InstanceKlass::cast(k); 3741 // Ensure it is initialized 3742 ik->initialize(CHECK_NH); 3743 return ik->allocate_instance_handle(THREAD); 3744 } 3745 3746 oop java_lang_reflect_Parameter::name(oop param) { 3747 return param->obj_field(_name_offset); 3748 } 3749 3750 void java_lang_reflect_Parameter::set_name(oop param, oop value) { 3751 param->obj_field_put(_name_offset, value); 3752 } 3753 3754 int java_lang_reflect_Parameter::modifiers(oop param) { 3755 return param->int_field(_modifiers_offset); 3756 } 3757 3758 void java_lang_reflect_Parameter::set_modifiers(oop param, int value) { 3759 param->int_field_put(_modifiers_offset, value); 3760 } 3761 3762 int java_lang_reflect_Parameter::index(oop param) { 3763 return param->int_field(_index_offset); 3764 } 3765 3766 void java_lang_reflect_Parameter::set_index(oop param, int value) { 3767 param->int_field_put(_index_offset, value); 3768 } 3769 3770 oop java_lang_reflect_Parameter::executable(oop param) { 3771 return param->obj_field(_executable_offset); 3772 } 3773 3774 void java_lang_reflect_Parameter::set_executable(oop param, oop value) { 3775 param->obj_field_put(_executable_offset, value); 3776 } 3777 3778 // java_lang_Module 3779 3780 int java_lang_Module::_loader_offset; 3781 int java_lang_Module::_name_offset; 3782 int java_lang_Module::_module_entry_offset; 3783 3784 Handle java_lang_Module::create(Handle loader, Handle module_name, TRAPS) { 3785 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3786 return JavaCalls::construct_new_instance(vmClasses::Module_klass(), 3787 vmSymbols::java_lang_module_init_signature(), 3788 loader, module_name, CHECK_NH); 3789 } 3790 3791 #define MODULE_FIELDS_DO(macro) \ 3792 macro(_loader_offset, k, vmSymbols::loader_name(), classloader_signature, false); \ 3793 macro(_name_offset, k, vmSymbols::name_name(), string_signature, false) 3794 3795 void java_lang_Module::compute_offsets() { 3796 InstanceKlass* k = vmClasses::Module_klass(); 3797 MODULE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3798 MODULE_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 3799 } 3800 3801 #if INCLUDE_CDS 3802 void java_lang_Module::serialize_offsets(SerializeClosure* f) { 3803 MODULE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3804 MODULE_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 3805 } 3806 #endif 3807 3808 oop java_lang_Module::loader(oop module) { 3809 return module->obj_field(_loader_offset); 3810 } 3811 3812 void java_lang_Module::set_loader(oop module, oop value) { 3813 module->obj_field_put(_loader_offset, value); 3814 } 3815 3816 oop java_lang_Module::name(oop module) { 3817 return module->obj_field(_name_offset); 3818 } 3819 3820 void java_lang_Module::set_name(oop module, oop value) { 3821 module->obj_field_put(_name_offset, value); 3822 } 3823 3824 ModuleEntry* java_lang_Module::module_entry_raw(oop module) { 3825 assert(_module_entry_offset != 0, "Uninitialized module_entry_offset"); 3826 assert(module != nullptr, "module can't be null"); 3827 assert(oopDesc::is_oop(module), "module must be oop"); 3828 3829 ModuleEntry* module_entry = (ModuleEntry*)module->address_field(_module_entry_offset); 3830 return module_entry; 3831 } 3832 3833 ModuleEntry* java_lang_Module::module_entry(oop module) { 3834 ModuleEntry* module_entry = module_entry_raw(module); 3835 if (module_entry == nullptr) { 3836 // If the inject field containing the ModuleEntry* is null then return the 3837 // class loader's unnamed module. 3838 oop loader = java_lang_Module::loader(module); 3839 Handle h_loader = Handle(Thread::current(), loader); 3840 ClassLoaderData* loader_cld = SystemDictionary::register_loader(h_loader); 3841 return loader_cld->unnamed_module(); 3842 } 3843 return module_entry; 3844 } 3845 3846 void java_lang_Module::set_module_entry(oop module, ModuleEntry* module_entry) { 3847 assert(_module_entry_offset != 0, "Uninitialized module_entry_offset"); 3848 assert(module != nullptr, "module can't be null"); 3849 assert(oopDesc::is_oop(module), "module must be oop"); 3850 module->address_field_put(_module_entry_offset, (address)module_entry); 3851 } 3852 3853 Handle reflect_ConstantPool::create(TRAPS) { 3854 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem"); 3855 InstanceKlass* k = vmClasses::reflect_ConstantPool_klass(); 3856 // Ensure it is initialized 3857 k->initialize(CHECK_NH); 3858 return k->allocate_instance_handle(THREAD); 3859 } 3860 3861 3862 void reflect_ConstantPool::set_cp(oop reflect, ConstantPool* value) { 3863 oop mirror = value->pool_holder()->java_mirror(); 3864 // Save the mirror to get back the constant pool. 3865 reflect->obj_field_put(_oop_offset, mirror); 3866 } 3867 3868 ConstantPool* reflect_ConstantPool::get_cp(oop reflect) { 3869 3870 oop mirror = reflect->obj_field(_oop_offset); 3871 Klass* k = java_lang_Class::as_Klass(mirror); 3872 assert(k->is_instance_klass(), "Must be"); 3873 3874 // Get the constant pool back from the klass. Since class redefinition 3875 // merges the new constant pool into the old, this is essentially the 3876 // same constant pool as the original. If constant pool merging is 3877 // no longer done in the future, this will have to change to save 3878 // the original. 3879 return InstanceKlass::cast(k)->constants(); 3880 } 3881 3882 3883 // Support for java_lang_ref_Reference 3884 3885 bool java_lang_ref_Reference::_offsets_initialized; 3886 3887 int java_lang_ref_Reference::_referent_offset; 3888 int java_lang_ref_Reference::_queue_offset; 3889 int java_lang_ref_Reference::_next_offset; 3890 int java_lang_ref_Reference::_discovered_offset; 3891 3892 #define REFERENCE_FIELDS_DO(macro) \ 3893 macro(_referent_offset, k, "referent", object_signature, false); \ 3894 macro(_queue_offset, k, "queue", referencequeue_signature, false); \ 3895 macro(_next_offset, k, "next", reference_signature, false); \ 3896 macro(_discovered_offset, k, "discovered", reference_signature, false); 3897 3898 void java_lang_ref_Reference::compute_offsets() { 3899 if (_offsets_initialized) { 3900 return; 3901 } 3902 _offsets_initialized = true; 3903 InstanceKlass* k = vmClasses::Reference_klass(); 3904 REFERENCE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3905 } 3906 3907 #if INCLUDE_CDS 3908 void java_lang_ref_Reference::serialize_offsets(SerializeClosure* f) { 3909 f->do_bool(&_offsets_initialized); 3910 REFERENCE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3911 } 3912 #endif 3913 3914 bool java_lang_ref_Reference::is_referent_field(oop obj, ptrdiff_t offset) { 3915 assert(obj != nullptr, "sanity"); 3916 if (offset != _referent_offset) { 3917 return false; 3918 } 3919 3920 Klass* k = obj->klass(); 3921 if (!k->is_instance_klass()) { 3922 return false; 3923 } 3924 3925 InstanceKlass* ik = InstanceKlass::cast(obj->klass()); 3926 bool is_reference = ik->reference_type() != REF_NONE; 3927 assert(!is_reference || ik->is_subclass_of(vmClasses::Reference_klass()), "sanity"); 3928 return is_reference; 3929 } 3930 3931 int java_lang_boxing_object::_value_offset; 3932 int java_lang_boxing_object::_long_value_offset; 3933 3934 #define BOXING_FIELDS_DO(macro) \ 3935 macro(_value_offset, integerKlass, "value", int_signature, false); \ 3936 macro(_long_value_offset, longKlass, "value", long_signature, false); 3937 3938 void java_lang_boxing_object::compute_offsets() { 3939 InstanceKlass* integerKlass = vmClasses::Integer_klass(); 3940 InstanceKlass* longKlass = vmClasses::Long_klass(); 3941 BOXING_FIELDS_DO(FIELD_COMPUTE_OFFSET); 3942 } 3943 3944 #if INCLUDE_CDS 3945 void java_lang_boxing_object::serialize_offsets(SerializeClosure* f) { 3946 BOXING_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 3947 } 3948 #endif 3949 3950 oop java_lang_boxing_object::initialize_and_allocate(BasicType type, TRAPS) { 3951 Klass* k = vmClasses::box_klass(type); 3952 if (k == nullptr) return nullptr; 3953 InstanceKlass* ik = InstanceKlass::cast(k); 3954 if (!ik->is_initialized()) { 3955 ik->initialize(CHECK_NULL); 3956 } 3957 return ik->allocate_instance(THREAD); 3958 } 3959 3960 3961 oop java_lang_boxing_object::create(BasicType type, jvalue* value, TRAPS) { 3962 oop box = initialize_and_allocate(type, CHECK_NULL); 3963 if (box == nullptr) return nullptr; 3964 switch (type) { 3965 case T_BOOLEAN: 3966 box->bool_field_put(_value_offset, value->z); 3967 break; 3968 case T_CHAR: 3969 box->char_field_put(_value_offset, value->c); 3970 break; 3971 case T_FLOAT: 3972 box->float_field_put(_value_offset, value->f); 3973 break; 3974 case T_DOUBLE: 3975 box->double_field_put(_long_value_offset, value->d); 3976 break; 3977 case T_BYTE: 3978 box->byte_field_put(_value_offset, value->b); 3979 break; 3980 case T_SHORT: 3981 box->short_field_put(_value_offset, value->s); 3982 break; 3983 case T_INT: 3984 box->int_field_put(_value_offset, value->i); 3985 break; 3986 case T_LONG: 3987 box->long_field_put(_long_value_offset, value->j); 3988 break; 3989 default: 3990 return nullptr; 3991 } 3992 return box; 3993 } 3994 3995 3996 BasicType java_lang_boxing_object::basic_type(oop box) { 3997 if (box == nullptr) return T_ILLEGAL; 3998 BasicType type = vmClasses::box_klass_type(box->klass()); 3999 if (type == T_OBJECT) // 'unknown' value returned by SD::bkt 4000 return T_ILLEGAL; 4001 return type; 4002 } 4003 4004 4005 BasicType java_lang_boxing_object::get_value(oop box, jvalue* value) { 4006 BasicType type = vmClasses::box_klass_type(box->klass()); 4007 switch (type) { 4008 case T_BOOLEAN: 4009 value->z = box->bool_field(_value_offset); 4010 break; 4011 case T_CHAR: 4012 value->c = box->char_field(_value_offset); 4013 break; 4014 case T_FLOAT: 4015 value->f = box->float_field(_value_offset); 4016 break; 4017 case T_DOUBLE: 4018 value->d = box->double_field(_long_value_offset); 4019 break; 4020 case T_BYTE: 4021 value->b = box->byte_field(_value_offset); 4022 break; 4023 case T_SHORT: 4024 value->s = box->short_field(_value_offset); 4025 break; 4026 case T_INT: 4027 value->i = box->int_field(_value_offset); 4028 break; 4029 case T_LONG: 4030 value->j = box->long_field(_long_value_offset); 4031 break; 4032 default: 4033 return T_ILLEGAL; 4034 } // end switch 4035 return type; 4036 } 4037 4038 4039 BasicType java_lang_boxing_object::set_value(oop box, jvalue* value) { 4040 BasicType type = vmClasses::box_klass_type(box->klass()); 4041 switch (type) { 4042 case T_BOOLEAN: 4043 box->bool_field_put(_value_offset, value->z); 4044 break; 4045 case T_CHAR: 4046 box->char_field_put(_value_offset, value->c); 4047 break; 4048 case T_FLOAT: 4049 box->float_field_put(_value_offset, value->f); 4050 break; 4051 case T_DOUBLE: 4052 box->double_field_put(_long_value_offset, value->d); 4053 break; 4054 case T_BYTE: 4055 box->byte_field_put(_value_offset, value->b); 4056 break; 4057 case T_SHORT: 4058 box->short_field_put(_value_offset, value->s); 4059 break; 4060 case T_INT: 4061 box->int_field_put(_value_offset, value->i); 4062 break; 4063 case T_LONG: 4064 box->long_field_put(_long_value_offset, value->j); 4065 break; 4066 default: 4067 return T_ILLEGAL; 4068 } // end switch 4069 return type; 4070 } 4071 4072 4073 void java_lang_boxing_object::print(BasicType type, jvalue* value, outputStream* st) { 4074 switch (type) { 4075 case T_BOOLEAN: st->print("%s", value->z ? "true" : "false"); break; 4076 case T_CHAR: st->print("%d", value->c); break; 4077 case T_BYTE: st->print("%d", value->b); break; 4078 case T_SHORT: st->print("%d", value->s); break; 4079 case T_INT: st->print("%d", value->i); break; 4080 case T_LONG: st->print(JLONG_FORMAT, value->j); break; 4081 case T_FLOAT: st->print("%f", value->f); break; 4082 case T_DOUBLE: st->print("%lf", value->d); break; 4083 default: st->print("type %d?", type); break; 4084 } 4085 } 4086 4087 4088 // Support for java_lang_ref_SoftReference 4089 // 4090 4091 int java_lang_ref_SoftReference::_timestamp_offset; 4092 int java_lang_ref_SoftReference::_static_clock_offset; 4093 4094 #define SOFTREFERENCE_FIELDS_DO(macro) \ 4095 macro(_timestamp_offset, k, "timestamp", long_signature, false); \ 4096 macro(_static_clock_offset, k, "clock", long_signature, true) 4097 4098 void java_lang_ref_SoftReference::compute_offsets() { 4099 InstanceKlass* k = vmClasses::SoftReference_klass(); 4100 SOFTREFERENCE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4101 } 4102 4103 #if INCLUDE_CDS 4104 void java_lang_ref_SoftReference::serialize_offsets(SerializeClosure* f) { 4105 SOFTREFERENCE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4106 } 4107 #endif 4108 4109 jlong java_lang_ref_SoftReference::timestamp(oop ref) { 4110 return ref->long_field(_timestamp_offset); 4111 } 4112 4113 jlong java_lang_ref_SoftReference::clock() { 4114 InstanceKlass* ik = vmClasses::SoftReference_klass(); 4115 oop base = ik->static_field_base_raw(); 4116 return base->long_field(_static_clock_offset); 4117 } 4118 4119 void java_lang_ref_SoftReference::set_clock(jlong value) { 4120 InstanceKlass* ik = vmClasses::SoftReference_klass(); 4121 oop base = ik->static_field_base_raw(); 4122 base->long_field_put(_static_clock_offset, value); 4123 } 4124 4125 // Support for java_lang_invoke_DirectMethodHandle 4126 4127 int java_lang_invoke_DirectMethodHandle::_member_offset; 4128 4129 oop java_lang_invoke_DirectMethodHandle::member(oop dmh) { 4130 oop member_name = nullptr; 4131 assert(oopDesc::is_oop(dmh) && java_lang_invoke_DirectMethodHandle::is_instance(dmh), 4132 "a DirectMethodHandle oop is expected"); 4133 return dmh->obj_field(_member_offset); 4134 } 4135 4136 #define DIRECTMETHODHANDLE_FIELDS_DO(macro) \ 4137 macro(_member_offset, k, "member", java_lang_invoke_MemberName_signature, false) 4138 4139 void java_lang_invoke_DirectMethodHandle::compute_offsets() { 4140 InstanceKlass* k = vmClasses::DirectMethodHandle_klass(); 4141 DIRECTMETHODHANDLE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4142 } 4143 4144 #if INCLUDE_CDS 4145 void java_lang_invoke_DirectMethodHandle::serialize_offsets(SerializeClosure* f) { 4146 DIRECTMETHODHANDLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4147 } 4148 #endif 4149 4150 // Support for java_lang_invoke_MethodHandle 4151 4152 int java_lang_invoke_MethodHandle::_type_offset; 4153 int java_lang_invoke_MethodHandle::_form_offset; 4154 4155 int java_lang_invoke_MemberName::_clazz_offset; 4156 int java_lang_invoke_MemberName::_name_offset; 4157 int java_lang_invoke_MemberName::_type_offset; 4158 int java_lang_invoke_MemberName::_flags_offset; 4159 int java_lang_invoke_MemberName::_method_offset; 4160 int java_lang_invoke_MemberName::_vmindex_offset; 4161 4162 int java_lang_invoke_ResolvedMethodName::_vmtarget_offset; 4163 int java_lang_invoke_ResolvedMethodName::_vmholder_offset; 4164 4165 int java_lang_invoke_LambdaForm::_vmentry_offset; 4166 4167 #define METHODHANDLE_FIELDS_DO(macro) \ 4168 macro(_type_offset, k, vmSymbols::type_name(), java_lang_invoke_MethodType_signature, false); \ 4169 macro(_form_offset, k, "form", java_lang_invoke_LambdaForm_signature, false) 4170 4171 void java_lang_invoke_MethodHandle::compute_offsets() { 4172 InstanceKlass* k = vmClasses::MethodHandle_klass(); 4173 METHODHANDLE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4174 } 4175 4176 #if INCLUDE_CDS 4177 void java_lang_invoke_MethodHandle::serialize_offsets(SerializeClosure* f) { 4178 METHODHANDLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4179 } 4180 #endif 4181 4182 #define MEMBERNAME_FIELDS_DO(macro) \ 4183 macro(_clazz_offset, k, vmSymbols::clazz_name(), class_signature, false); \ 4184 macro(_name_offset, k, vmSymbols::name_name(), string_signature, false); \ 4185 macro(_type_offset, k, vmSymbols::type_name(), object_signature, false); \ 4186 macro(_flags_offset, k, vmSymbols::flags_name(), int_signature, false); \ 4187 macro(_method_offset, k, vmSymbols::method_name(), java_lang_invoke_ResolvedMethodName_signature, false) 4188 4189 void java_lang_invoke_MemberName::compute_offsets() { 4190 InstanceKlass* k = vmClasses::MemberName_klass(); 4191 MEMBERNAME_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4192 MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 4193 } 4194 4195 #if INCLUDE_CDS 4196 void java_lang_invoke_MemberName::serialize_offsets(SerializeClosure* f) { 4197 MEMBERNAME_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4198 MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 4199 } 4200 #endif 4201 4202 #define RESOLVEDMETHOD_FIELDS_DO(macro) \ 4203 macro(_vmholder_offset, k, "vmholder", class_signature, false) 4204 4205 void java_lang_invoke_ResolvedMethodName::compute_offsets() { 4206 InstanceKlass* k = vmClasses::ResolvedMethodName_klass(); 4207 assert(k != nullptr, "jdk mismatch"); 4208 RESOLVEDMETHOD_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4209 RESOLVEDMETHOD_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 4210 } 4211 4212 #if INCLUDE_CDS 4213 void java_lang_invoke_ResolvedMethodName::serialize_offsets(SerializeClosure* f) { 4214 RESOLVEDMETHOD_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4215 RESOLVEDMETHOD_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 4216 } 4217 #endif 4218 4219 #define LAMBDAFORM_FIELDS_DO(macro) \ 4220 macro(_vmentry_offset, k, "vmentry", java_lang_invoke_MemberName_signature, false) 4221 4222 void java_lang_invoke_LambdaForm::compute_offsets() { 4223 InstanceKlass* k = vmClasses::LambdaForm_klass(); 4224 assert (k != nullptr, "jdk mismatch"); 4225 LAMBDAFORM_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4226 } 4227 4228 #if INCLUDE_CDS 4229 void java_lang_invoke_LambdaForm::serialize_offsets(SerializeClosure* f) { 4230 LAMBDAFORM_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4231 } 4232 #endif 4233 4234 bool java_lang_invoke_LambdaForm::is_instance(oop obj) { 4235 return obj != nullptr && is_subclass(obj->klass()); 4236 } 4237 4238 int jdk_internal_foreign_abi_NativeEntryPoint::_method_type_offset; 4239 int jdk_internal_foreign_abi_NativeEntryPoint::_downcall_stub_address_offset; 4240 4241 #define NEP_FIELDS_DO(macro) \ 4242 macro(_method_type_offset, k, "methodType", java_lang_invoke_MethodType_signature, false); \ 4243 macro(_downcall_stub_address_offset, k, "downcallStubAddress", long_signature, false); 4244 4245 bool jdk_internal_foreign_abi_NativeEntryPoint::is_instance(oop obj) { 4246 return obj != nullptr && is_subclass(obj->klass()); 4247 } 4248 4249 void jdk_internal_foreign_abi_NativeEntryPoint::compute_offsets() { 4250 InstanceKlass* k = vmClasses::NativeEntryPoint_klass(); 4251 NEP_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4252 } 4253 4254 #if INCLUDE_CDS 4255 void jdk_internal_foreign_abi_NativeEntryPoint::serialize_offsets(SerializeClosure* f) { 4256 NEP_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4257 } 4258 #endif 4259 4260 oop jdk_internal_foreign_abi_NativeEntryPoint::method_type(oop entry) { 4261 return entry->obj_field(_method_type_offset); 4262 } 4263 4264 jlong jdk_internal_foreign_abi_NativeEntryPoint::downcall_stub_address(oop entry) { 4265 return entry->long_field(_downcall_stub_address_offset); 4266 } 4267 4268 int jdk_internal_foreign_abi_ABIDescriptor::_inputStorage_offset; 4269 int jdk_internal_foreign_abi_ABIDescriptor::_outputStorage_offset; 4270 int jdk_internal_foreign_abi_ABIDescriptor::_volatileStorage_offset; 4271 int jdk_internal_foreign_abi_ABIDescriptor::_stackAlignment_offset; 4272 int jdk_internal_foreign_abi_ABIDescriptor::_shadowSpace_offset; 4273 int jdk_internal_foreign_abi_ABIDescriptor::_scratch1_offset; 4274 int jdk_internal_foreign_abi_ABIDescriptor::_scratch2_offset; 4275 4276 #define ABIDescriptor_FIELDS_DO(macro) \ 4277 macro(_inputStorage_offset, k, "inputStorage", jdk_internal_foreign_abi_VMStorage_array_array_signature, false); \ 4278 macro(_outputStorage_offset, k, "outputStorage", jdk_internal_foreign_abi_VMStorage_array_array_signature, false); \ 4279 macro(_volatileStorage_offset, k, "volatileStorage", jdk_internal_foreign_abi_VMStorage_array_array_signature, false); \ 4280 macro(_stackAlignment_offset, k, "stackAlignment", int_signature, false); \ 4281 macro(_shadowSpace_offset, k, "shadowSpace", int_signature, false); \ 4282 macro(_scratch1_offset, k, "scratch1", jdk_internal_foreign_abi_VMStorage_signature, false); \ 4283 macro(_scratch2_offset, k, "scratch2", jdk_internal_foreign_abi_VMStorage_signature, false); 4284 4285 bool jdk_internal_foreign_abi_ABIDescriptor::is_instance(oop obj) { 4286 return obj != nullptr && is_subclass(obj->klass()); 4287 } 4288 4289 void jdk_internal_foreign_abi_ABIDescriptor::compute_offsets() { 4290 InstanceKlass* k = vmClasses::ABIDescriptor_klass(); 4291 ABIDescriptor_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4292 } 4293 4294 #if INCLUDE_CDS 4295 void jdk_internal_foreign_abi_ABIDescriptor::serialize_offsets(SerializeClosure* f) { 4296 ABIDescriptor_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4297 } 4298 #endif 4299 4300 objArrayOop jdk_internal_foreign_abi_ABIDescriptor::inputStorage(oop entry) { 4301 return oop_cast<objArrayOop>(entry->obj_field(_inputStorage_offset)); 4302 } 4303 4304 objArrayOop jdk_internal_foreign_abi_ABIDescriptor::outputStorage(oop entry) { 4305 return oop_cast<objArrayOop>(entry->obj_field(_outputStorage_offset)); 4306 } 4307 4308 objArrayOop jdk_internal_foreign_abi_ABIDescriptor::volatileStorage(oop entry) { 4309 return oop_cast<objArrayOop>(entry->obj_field(_volatileStorage_offset)); 4310 } 4311 4312 jint jdk_internal_foreign_abi_ABIDescriptor::stackAlignment(oop entry) { 4313 return entry->int_field(_stackAlignment_offset); 4314 } 4315 4316 jint jdk_internal_foreign_abi_ABIDescriptor::shadowSpace(oop entry) { 4317 return entry->int_field(_shadowSpace_offset); 4318 } 4319 4320 oop jdk_internal_foreign_abi_ABIDescriptor::scratch1(oop entry) { 4321 return entry->obj_field(_scratch1_offset); 4322 } 4323 4324 oop jdk_internal_foreign_abi_ABIDescriptor::scratch2(oop entry) { 4325 return entry->obj_field(_scratch2_offset); 4326 } 4327 4328 int jdk_internal_foreign_abi_VMStorage::_type_offset; 4329 int jdk_internal_foreign_abi_VMStorage::_indexOrOffset_offset; 4330 int jdk_internal_foreign_abi_VMStorage::_segmentMaskOrSize_offset; 4331 int jdk_internal_foreign_abi_VMStorage::_debugName_offset; 4332 4333 #define VMStorage_FIELDS_DO(macro) \ 4334 macro(_type_offset, k, "type", byte_signature, false); \ 4335 macro(_indexOrOffset_offset, k, "indexOrOffset", int_signature, false); \ 4336 macro(_segmentMaskOrSize_offset, k, "segmentMaskOrSize", short_signature, false); \ 4337 macro(_debugName_offset, k, "debugName", string_signature, false); \ 4338 4339 bool jdk_internal_foreign_abi_VMStorage::is_instance(oop obj) { 4340 return obj != nullptr && is_subclass(obj->klass()); 4341 } 4342 4343 void jdk_internal_foreign_abi_VMStorage::compute_offsets() { 4344 InstanceKlass* k = vmClasses::VMStorage_klass(); 4345 VMStorage_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4346 } 4347 4348 #if INCLUDE_CDS 4349 void jdk_internal_foreign_abi_VMStorage::serialize_offsets(SerializeClosure* f) { 4350 VMStorage_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4351 } 4352 #endif 4353 4354 jbyte jdk_internal_foreign_abi_VMStorage::type(oop entry) { 4355 return entry->byte_field(_type_offset); 4356 } 4357 4358 jint jdk_internal_foreign_abi_VMStorage::index_or_offset(oop entry) { 4359 return entry->int_field(_indexOrOffset_offset); 4360 } 4361 4362 jshort jdk_internal_foreign_abi_VMStorage::segment_mask_or_size(oop entry) { 4363 return entry->short_field(_segmentMaskOrSize_offset); 4364 } 4365 4366 oop jdk_internal_foreign_abi_VMStorage::debugName(oop entry) { 4367 return entry->obj_field(_debugName_offset); 4368 } 4369 4370 int jdk_internal_foreign_abi_CallConv::_argRegs_offset; 4371 int jdk_internal_foreign_abi_CallConv::_retRegs_offset; 4372 4373 #define CallConv_FIELDS_DO(macro) \ 4374 macro(_argRegs_offset, k, "argRegs", jdk_internal_foreign_abi_VMStorage_array_signature, false); \ 4375 macro(_retRegs_offset, k, "retRegs", jdk_internal_foreign_abi_VMStorage_array_signature, false); \ 4376 4377 bool jdk_internal_foreign_abi_CallConv::is_instance(oop obj) { 4378 return obj != nullptr && is_subclass(obj->klass()); 4379 } 4380 4381 void jdk_internal_foreign_abi_CallConv::compute_offsets() { 4382 InstanceKlass* k = vmClasses::CallConv_klass(); 4383 CallConv_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4384 } 4385 4386 #if INCLUDE_CDS 4387 void jdk_internal_foreign_abi_CallConv::serialize_offsets(SerializeClosure* f) { 4388 CallConv_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4389 } 4390 #endif 4391 4392 objArrayOop jdk_internal_foreign_abi_CallConv::argRegs(oop entry) { 4393 return oop_cast<objArrayOop>(entry->obj_field(_argRegs_offset)); 4394 } 4395 4396 objArrayOop jdk_internal_foreign_abi_CallConv::retRegs(oop entry) { 4397 return oop_cast<objArrayOop>(entry->obj_field(_retRegs_offset)); 4398 } 4399 4400 oop java_lang_invoke_MethodHandle::type(oop mh) { 4401 return mh->obj_field(_type_offset); 4402 } 4403 4404 void java_lang_invoke_MethodHandle::set_type(oop mh, oop mtype) { 4405 mh->obj_field_put(_type_offset, mtype); 4406 } 4407 4408 oop java_lang_invoke_MethodHandle::form(oop mh) { 4409 assert(_form_offset != 0, ""); 4410 return mh->obj_field(_form_offset); 4411 } 4412 4413 void java_lang_invoke_MethodHandle::set_form(oop mh, oop lform) { 4414 assert(_form_offset != 0, ""); 4415 mh->obj_field_put(_form_offset, lform); 4416 } 4417 4418 /// MemberName accessors 4419 4420 oop java_lang_invoke_MemberName::clazz(oop mname) { 4421 assert(is_instance(mname), "wrong type"); 4422 return mname->obj_field(_clazz_offset); 4423 } 4424 4425 void java_lang_invoke_MemberName::set_clazz(oop mname, oop clazz) { 4426 assert(is_instance(mname), "wrong type"); 4427 mname->obj_field_put(_clazz_offset, clazz); 4428 } 4429 4430 oop java_lang_invoke_MemberName::name(oop mname) { 4431 assert(is_instance(mname), "wrong type"); 4432 return mname->obj_field(_name_offset); 4433 } 4434 4435 void java_lang_invoke_MemberName::set_name(oop mname, oop name) { 4436 assert(is_instance(mname), "wrong type"); 4437 mname->obj_field_put(_name_offset, name); 4438 } 4439 4440 oop java_lang_invoke_MemberName::type(oop mname) { 4441 assert(is_instance(mname), "wrong type"); 4442 return mname->obj_field(_type_offset); 4443 } 4444 4445 void java_lang_invoke_MemberName::set_type(oop mname, oop type) { 4446 assert(is_instance(mname), "wrong type"); 4447 mname->obj_field_put(_type_offset, type); 4448 } 4449 4450 int java_lang_invoke_MemberName::flags(oop mname) { 4451 assert(is_instance(mname), "wrong type"); 4452 return mname->int_field(_flags_offset); 4453 } 4454 4455 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) { 4456 assert(is_instance(mname), "wrong type"); 4457 mname->int_field_put(_flags_offset, flags); 4458 } 4459 4460 4461 // Return vmtarget from ResolvedMethodName method field through indirection 4462 Method* java_lang_invoke_MemberName::vmtarget(oop mname) { 4463 assert(is_instance(mname), "wrong type"); 4464 oop method = mname->obj_field(_method_offset); 4465 return method == nullptr ? nullptr : java_lang_invoke_ResolvedMethodName::vmtarget(method); 4466 } 4467 4468 bool java_lang_invoke_MemberName::is_method(oop mname) { 4469 assert(is_instance(mname), "must be MemberName"); 4470 return (flags(mname) & (MN_IS_METHOD | MN_IS_CONSTRUCTOR)) > 0; 4471 } 4472 4473 void java_lang_invoke_MemberName::set_method(oop mname, oop resolved_method) { 4474 assert(is_instance(mname), "wrong type"); 4475 mname->obj_field_put(_method_offset, resolved_method); 4476 } 4477 4478 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) { 4479 assert(is_instance(mname), "wrong type"); 4480 return (intptr_t) mname->address_field(_vmindex_offset); 4481 } 4482 4483 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) { 4484 assert(is_instance(mname), "wrong type"); 4485 mname->address_field_put(_vmindex_offset, (address) index); 4486 } 4487 4488 4489 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) { 4490 assert(is_instance(resolved_method), "wrong type"); 4491 Method* m = (Method*)resolved_method->address_field(_vmtarget_offset); 4492 assert(m->is_method(), "must be"); 4493 return m; 4494 } 4495 4496 // Used by redefinition to change Method* to new Method* with same hash (name, signature) 4497 void java_lang_invoke_ResolvedMethodName::set_vmtarget(oop resolved_method, Method* m) { 4498 assert(is_instance(resolved_method), "wrong type"); 4499 resolved_method->address_field_put(_vmtarget_offset, (address)m); 4500 } 4501 4502 void java_lang_invoke_ResolvedMethodName::set_vmholder(oop resolved_method, oop holder) { 4503 assert(is_instance(resolved_method), "wrong type"); 4504 resolved_method->obj_field_put(_vmholder_offset, holder); 4505 } 4506 4507 oop java_lang_invoke_ResolvedMethodName::find_resolved_method(const methodHandle& m, TRAPS) { 4508 const Method* method = m(); 4509 4510 // lookup ResolvedMethod oop in the table, or create a new one and intern it 4511 oop resolved_method = ResolvedMethodTable::find_method(method); 4512 if (resolved_method != nullptr) { 4513 return resolved_method; 4514 } 4515 4516 InstanceKlass* k = vmClasses::ResolvedMethodName_klass(); 4517 if (!k->is_initialized()) { 4518 k->initialize(CHECK_NULL); 4519 } 4520 4521 oop new_resolved_method = k->allocate_instance(CHECK_NULL); 4522 4523 NoSafepointVerifier nsv; 4524 4525 if (method->is_old()) { 4526 method = (method->is_deleted()) ? Universe::throw_no_such_method_error() : 4527 method->get_new_method(); 4528 } 4529 4530 InstanceKlass* holder = method->method_holder(); 4531 4532 set_vmtarget(new_resolved_method, const_cast<Method*>(method)); 4533 // Add a reference to the loader (actually mirror because hidden classes may not have 4534 // distinct loaders) to ensure the metadata is kept alive. 4535 // This mirror may be different than the one in clazz field. 4536 set_vmholder(new_resolved_method, holder->java_mirror()); 4537 4538 // Set flag in class to indicate this InstanceKlass has entries in the table 4539 // to avoid walking table during redefinition if none of the redefined classes 4540 // have any membernames in the table. 4541 holder->set_has_resolved_methods(); 4542 4543 return ResolvedMethodTable::add_method(method, Handle(THREAD, new_resolved_method)); 4544 } 4545 4546 oop java_lang_invoke_LambdaForm::vmentry(oop lform) { 4547 assert(is_instance(lform), "wrong type"); 4548 return lform->obj_field(_vmentry_offset); 4549 } 4550 4551 4552 // Support for java_lang_invoke_MethodType 4553 4554 int java_lang_invoke_MethodType::_rtype_offset; 4555 int java_lang_invoke_MethodType::_ptypes_offset; 4556 4557 #define METHODTYPE_FIELDS_DO(macro) \ 4558 macro(_rtype_offset, k, "rtype", class_signature, false); \ 4559 macro(_ptypes_offset, k, "ptypes", class_array_signature, false) 4560 4561 void java_lang_invoke_MethodType::compute_offsets() { 4562 InstanceKlass* k = vmClasses::MethodType_klass(); 4563 METHODTYPE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4564 } 4565 4566 #if INCLUDE_CDS 4567 void java_lang_invoke_MethodType::serialize_offsets(SerializeClosure* f) { 4568 METHODTYPE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4569 } 4570 #endif 4571 4572 void java_lang_invoke_MethodType::print_signature(oop mt, outputStream* st) { 4573 st->print("("); 4574 objArrayOop pts = ptypes(mt); 4575 if (pts != nullptr) { 4576 for (int i = 0, limit = pts->length(); i < limit; i++) { 4577 java_lang_Class::print_signature(pts->obj_at(i), st); 4578 } 4579 } else { 4580 st->print("null"); 4581 } 4582 st->print(")"); 4583 oop rt = rtype(mt); 4584 if (rt != nullptr) { 4585 java_lang_Class::print_signature(rt, st); 4586 } else { 4587 st->print("null"); 4588 } 4589 } 4590 4591 Symbol* java_lang_invoke_MethodType::as_signature(oop mt, bool intern_if_not_found) { 4592 ResourceMark rm; 4593 stringStream buffer(128); 4594 print_signature(mt, &buffer); 4595 const char* sigstr = buffer.base(); 4596 int siglen = (int) buffer.size(); 4597 Symbol *name; 4598 if (!intern_if_not_found) { 4599 name = SymbolTable::probe(sigstr, siglen); 4600 } else { 4601 name = SymbolTable::new_symbol(sigstr, siglen); 4602 } 4603 return name; 4604 } 4605 4606 bool java_lang_invoke_MethodType::equals(oop mt1, oop mt2) { 4607 if (mt1 == mt2) 4608 return true; 4609 if (rtype(mt1) != rtype(mt2)) 4610 return false; 4611 if (ptype_count(mt1) != ptype_count(mt2)) 4612 return false; 4613 for (int i = ptype_count(mt1) - 1; i >= 0; i--) { 4614 if (ptype(mt1, i) != ptype(mt2, i)) 4615 return false; 4616 } 4617 return true; 4618 } 4619 4620 oop java_lang_invoke_MethodType::rtype(oop mt) { 4621 assert(is_instance(mt), "must be a MethodType"); 4622 return mt->obj_field(_rtype_offset); 4623 } 4624 4625 objArrayOop java_lang_invoke_MethodType::ptypes(oop mt) { 4626 assert(is_instance(mt), "must be a MethodType"); 4627 return (objArrayOop) mt->obj_field(_ptypes_offset); 4628 } 4629 4630 oop java_lang_invoke_MethodType::ptype(oop mt, int idx) { 4631 return ptypes(mt)->obj_at(idx); 4632 } 4633 4634 int java_lang_invoke_MethodType::ptype_count(oop mt) { 4635 return ptypes(mt)->length(); 4636 } 4637 4638 int java_lang_invoke_MethodType::ptype_slot_count(oop mt) { 4639 objArrayOop pts = ptypes(mt); 4640 int count = pts->length(); 4641 int slots = 0; 4642 for (int i = 0; i < count; i++) { 4643 BasicType bt = java_lang_Class::as_BasicType(pts->obj_at(i)); 4644 slots += type2size[bt]; 4645 } 4646 return slots; 4647 } 4648 4649 int java_lang_invoke_MethodType::rtype_slot_count(oop mt) { 4650 BasicType bt = java_lang_Class::as_BasicType(rtype(mt)); 4651 return type2size[bt]; 4652 } 4653 4654 4655 // Support for java_lang_invoke_CallSite 4656 4657 int java_lang_invoke_CallSite::_target_offset; 4658 int java_lang_invoke_CallSite::_context_offset; 4659 4660 #define CALLSITE_FIELDS_DO(macro) \ 4661 macro(_target_offset, k, "target", java_lang_invoke_MethodHandle_signature, false); \ 4662 macro(_context_offset, k, "context", java_lang_invoke_MethodHandleNatives_CallSiteContext_signature, false) 4663 4664 void java_lang_invoke_CallSite::compute_offsets() { 4665 InstanceKlass* k = vmClasses::CallSite_klass(); 4666 CALLSITE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4667 } 4668 4669 #if INCLUDE_CDS 4670 void java_lang_invoke_CallSite::serialize_offsets(SerializeClosure* f) { 4671 CALLSITE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4672 } 4673 #endif 4674 4675 oop java_lang_invoke_CallSite::context_no_keepalive(oop call_site) { 4676 assert(java_lang_invoke_CallSite::is_instance(call_site), ""); 4677 4678 oop dep_oop = call_site->obj_field_access<AS_NO_KEEPALIVE>(_context_offset); 4679 return dep_oop; 4680 } 4681 4682 // Support for java_lang_invoke_ConstantCallSite 4683 4684 int java_lang_invoke_ConstantCallSite::_is_frozen_offset; 4685 4686 #define CONSTANTCALLSITE_FIELDS_DO(macro) \ 4687 macro(_is_frozen_offset, k, "isFrozen", bool_signature, false) 4688 4689 void java_lang_invoke_ConstantCallSite::compute_offsets() { 4690 InstanceKlass* k = vmClasses::ConstantCallSite_klass(); 4691 CONSTANTCALLSITE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4692 } 4693 4694 #if INCLUDE_CDS 4695 void java_lang_invoke_ConstantCallSite::serialize_offsets(SerializeClosure* f) { 4696 CONSTANTCALLSITE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4697 } 4698 #endif 4699 4700 // Support for java_lang_invoke_MethodHandleNatives_CallSiteContext 4701 4702 int java_lang_invoke_MethodHandleNatives_CallSiteContext::_vmdependencies_offset; 4703 int java_lang_invoke_MethodHandleNatives_CallSiteContext::_last_cleanup_offset; 4704 4705 void java_lang_invoke_MethodHandleNatives_CallSiteContext::compute_offsets() { 4706 InstanceKlass* k = vmClasses::Context_klass(); 4707 CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 4708 } 4709 4710 #if INCLUDE_CDS 4711 void java_lang_invoke_MethodHandleNatives_CallSiteContext::serialize_offsets(SerializeClosure* f) { 4712 CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 4713 } 4714 #endif 4715 4716 DependencyContext java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(oop call_site) { 4717 assert(java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(call_site), ""); 4718 nmethodBucket* volatile* vmdeps_addr = call_site->field_addr<nmethodBucket* volatile>(_vmdependencies_offset); 4719 volatile uint64_t* last_cleanup_addr = call_site->field_addr<volatile uint64_t>(_last_cleanup_offset); 4720 DependencyContext dep_ctx(vmdeps_addr, last_cleanup_addr); 4721 return dep_ctx; 4722 } 4723 4724 // Support for java_security_AccessControlContext 4725 4726 int java_security_AccessControlContext::_context_offset; 4727 int java_security_AccessControlContext::_privilegedContext_offset; 4728 int java_security_AccessControlContext::_isPrivileged_offset; 4729 int java_security_AccessControlContext::_isAuthorized_offset; 4730 4731 #define ACCESSCONTROLCONTEXT_FIELDS_DO(macro) \ 4732 macro(_context_offset, k, "context", protectiondomain_signature, false); \ 4733 macro(_privilegedContext_offset, k, "privilegedContext", accesscontrolcontext_signature, false); \ 4734 macro(_isPrivileged_offset, k, "isPrivileged", bool_signature, false); \ 4735 macro(_isAuthorized_offset, k, "isAuthorized", bool_signature, false) 4736 4737 void java_security_AccessControlContext::compute_offsets() { 4738 assert(_isPrivileged_offset == 0, "offsets should be initialized only once"); 4739 InstanceKlass* k = vmClasses::AccessControlContext_klass(); 4740 ACCESSCONTROLCONTEXT_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4741 } 4742 4743 #if INCLUDE_CDS 4744 void java_security_AccessControlContext::serialize_offsets(SerializeClosure* f) { 4745 ACCESSCONTROLCONTEXT_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4746 } 4747 #endif 4748 4749 oop java_security_AccessControlContext::create(objArrayHandle context, bool isPrivileged, Handle privileged_context, TRAPS) { 4750 assert(_isPrivileged_offset != 0, "offsets should have been initialized"); 4751 assert(_isAuthorized_offset != 0, "offsets should have been initialized"); 4752 // Ensure klass is initialized 4753 vmClasses::AccessControlContext_klass()->initialize(CHECK_NULL); 4754 // Allocate result 4755 oop result = vmClasses::AccessControlContext_klass()->allocate_instance(CHECK_NULL); 4756 // Fill in values 4757 result->obj_field_put(_context_offset, context()); 4758 result->obj_field_put(_privilegedContext_offset, privileged_context()); 4759 result->bool_field_put(_isPrivileged_offset, isPrivileged); 4760 result->bool_field_put(_isAuthorized_offset, true); 4761 return result; 4762 } 4763 4764 4765 // Support for java_lang_ClassLoader 4766 4767 int java_lang_ClassLoader::_loader_data_offset; 4768 int java_lang_ClassLoader::_parallelCapable_offset; 4769 int java_lang_ClassLoader::_name_offset; 4770 int java_lang_ClassLoader::_nameAndId_offset; 4771 int java_lang_ClassLoader::_unnamedModule_offset; 4772 int java_lang_ClassLoader::_parent_offset; 4773 4774 ClassLoaderData* java_lang_ClassLoader::loader_data_acquire(oop loader) { 4775 assert(loader != nullptr, "loader must not be null"); 4776 assert(oopDesc::is_oop(loader), "loader must be oop"); 4777 return Atomic::load_acquire(loader->field_addr<ClassLoaderData*>(_loader_data_offset)); 4778 } 4779 4780 ClassLoaderData* java_lang_ClassLoader::loader_data(oop loader) { 4781 assert(loader != nullptr, "loader must not be null"); 4782 assert(oopDesc::is_oop(loader), "loader must be oop"); 4783 return *loader->field_addr<ClassLoaderData*>(_loader_data_offset); 4784 } 4785 4786 void java_lang_ClassLoader::release_set_loader_data(oop loader, ClassLoaderData* new_data) { 4787 assert(loader != nullptr, "loader must not be null"); 4788 assert(oopDesc::is_oop(loader), "loader must be oop"); 4789 Atomic::release_store(loader->field_addr<ClassLoaderData*>(_loader_data_offset), new_data); 4790 } 4791 4792 #define CLASSLOADER_FIELDS_DO(macro) \ 4793 macro(_parallelCapable_offset, k1, "parallelLockMap", concurrenthashmap_signature, false); \ 4794 macro(_name_offset, k1, vmSymbols::name_name(), string_signature, false); \ 4795 macro(_nameAndId_offset, k1, "nameAndId", string_signature, false); \ 4796 macro(_unnamedModule_offset, k1, "unnamedModule", module_signature, false); \ 4797 macro(_parent_offset, k1, "parent", classloader_signature, false) 4798 4799 void java_lang_ClassLoader::compute_offsets() { 4800 InstanceKlass* k1 = vmClasses::ClassLoader_klass(); 4801 CLASSLOADER_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4802 4803 CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 4804 } 4805 4806 #if INCLUDE_CDS 4807 void java_lang_ClassLoader::serialize_offsets(SerializeClosure* f) { 4808 CLASSLOADER_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4809 CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 4810 } 4811 #endif 4812 4813 oop java_lang_ClassLoader::parent(oop loader) { 4814 assert(is_instance(loader), "loader must be oop"); 4815 return loader->obj_field(_parent_offset); 4816 } 4817 4818 oop java_lang_ClassLoader::parent_no_keepalive(oop loader) { 4819 assert(is_instance(loader), "loader must be oop"); 4820 return loader->obj_field_access<AS_NO_KEEPALIVE>(_parent_offset); 4821 } 4822 4823 // Returns the name field of this class loader. If the name field has not 4824 // been set, null will be returned. 4825 oop java_lang_ClassLoader::name(oop loader) { 4826 assert(is_instance(loader), "loader must be oop"); 4827 return loader->obj_field(_name_offset); 4828 } 4829 4830 // Returns the nameAndId field of this class loader. The format is 4831 // as follows: 4832 // If the defining loader has a name explicitly set then '<loader-name>' @<id> 4833 // If the defining loader has no name then <qualified-class-name> @<id> 4834 // If built-in loader, then omit '@<id>' as there is only one instance. 4835 // Use ClassLoader::loader_name_id() to obtain this String as a char*. 4836 oop java_lang_ClassLoader::nameAndId(oop loader) { 4837 assert(is_instance(loader), "loader must be oop"); 4838 return loader->obj_field(_nameAndId_offset); 4839 } 4840 4841 bool java_lang_ClassLoader::isAncestor(oop loader, oop cl) { 4842 assert(is_instance(loader), "loader must be oop"); 4843 assert(cl == nullptr || is_instance(cl), "cl argument must be oop"); 4844 oop acl = loader; 4845 debug_only(jint loop_count = 0); 4846 // This loop taken verbatim from ClassLoader.java: 4847 do { 4848 acl = parent(acl); 4849 if (cl == acl) { 4850 return true; 4851 } 4852 assert(++loop_count > 0, "loop_count overflow"); 4853 } while (acl != nullptr); 4854 return false; 4855 } 4856 4857 bool java_lang_ClassLoader::is_instance(oop obj) { 4858 return obj != nullptr && is_subclass(obj->klass()); 4859 } 4860 4861 4862 // For class loader classes, parallelCapable defined 4863 // based on non-null field 4864 // Written to by java.lang.ClassLoader, vm only reads this field, doesn't set it 4865 bool java_lang_ClassLoader::parallelCapable(oop class_loader) { 4866 assert(_parallelCapable_offset != 0, "offsets should have been initialized"); 4867 return (class_loader->obj_field(_parallelCapable_offset) != nullptr); 4868 } 4869 4870 bool java_lang_ClassLoader::is_trusted_loader(oop loader) { 4871 oop cl = SystemDictionary::java_system_loader(); 4872 while(cl != nullptr) { 4873 if (cl == loader) return true; 4874 cl = parent(cl); 4875 } 4876 return false; 4877 } 4878 4879 oop java_lang_ClassLoader::unnamedModule(oop loader) { 4880 assert(is_instance(loader), "loader must be oop"); 4881 return loader->obj_field(_unnamedModule_offset); 4882 } 4883 4884 // Support for java_lang_System 4885 // 4886 4887 int java_lang_System::_static_in_offset; 4888 int java_lang_System::_static_out_offset; 4889 int java_lang_System::_static_err_offset; 4890 4891 #define SYSTEM_FIELDS_DO(macro) \ 4892 macro(_static_in_offset, k, "in", input_stream_signature, true); \ 4893 macro(_static_out_offset, k, "out", print_stream_signature, true); \ 4894 macro(_static_err_offset, k, "err", print_stream_signature, true); 4895 4896 void java_lang_System::compute_offsets() { 4897 InstanceKlass* k = vmClasses::System_klass(); 4898 SYSTEM_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4899 } 4900 4901 // This field tells us that a security manager can never be installed so we 4902 // can completely skip populating the ProtectionDomainCacheTable. 4903 bool java_lang_System::allow_security_manager() { 4904 return false; 4905 } 4906 4907 // This field tells us that a security manager is installed. 4908 bool java_lang_System::has_security_manager() { 4909 return false; 4910 } 4911 4912 #if INCLUDE_CDS 4913 void java_lang_System::serialize_offsets(SerializeClosure* f) { 4914 SYSTEM_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4915 } 4916 #endif 4917 4918 // Support for jdk_internal_misc_UnsafeConstants 4919 // 4920 class UnsafeConstantsFixup : public FieldClosure { 4921 private: 4922 int _address_size; 4923 int _page_size; 4924 bool _big_endian; 4925 bool _use_unaligned_access; 4926 int _data_cache_line_flush_size; 4927 public: 4928 UnsafeConstantsFixup() { 4929 // round up values for all static final fields 4930 _address_size = sizeof(void*); 4931 _page_size = AIX_ONLY(sysconf(_SC_PAGESIZE)) NOT_AIX((int)os::vm_page_size()); 4932 _big_endian = LITTLE_ENDIAN_ONLY(false) BIG_ENDIAN_ONLY(true); 4933 _use_unaligned_access = UseUnalignedAccesses; 4934 _data_cache_line_flush_size = (int)VM_Version::data_cache_line_flush_size(); 4935 } 4936 4937 void do_field(fieldDescriptor* fd) { 4938 oop mirror = fd->field_holder()->java_mirror(); 4939 assert(mirror != nullptr, "UnsafeConstants must have mirror already"); 4940 assert(fd->field_holder() == vmClasses::UnsafeConstants_klass(), "Should be UnsafeConstants"); 4941 assert(fd->is_final(), "fields of UnsafeConstants must be final"); 4942 assert(fd->is_static(), "fields of UnsafeConstants must be static"); 4943 if (fd->name() == vmSymbols::address_size_name()) { 4944 mirror->int_field_put(fd->offset(), _address_size); 4945 } else if (fd->name() == vmSymbols::page_size_name()) { 4946 mirror->int_field_put(fd->offset(), _page_size); 4947 } else if (fd->name() == vmSymbols::big_endian_name()) { 4948 mirror->bool_field_put(fd->offset(), _big_endian); 4949 } else if (fd->name() == vmSymbols::use_unaligned_access_name()) { 4950 mirror->bool_field_put(fd->offset(), _use_unaligned_access); 4951 } else if (fd->name() == vmSymbols::data_cache_line_flush_size_name()) { 4952 mirror->int_field_put(fd->offset(), _data_cache_line_flush_size); 4953 } else { 4954 assert(false, "unexpected UnsafeConstants field"); 4955 } 4956 } 4957 }; 4958 4959 void jdk_internal_misc_UnsafeConstants::set_unsafe_constants() { 4960 UnsafeConstantsFixup fixup; 4961 vmClasses::UnsafeConstants_klass()->do_local_static_fields(&fixup); 4962 } 4963 4964 4965 // java_lang_StackTraceElement 4966 4967 int java_lang_StackTraceElement::_methodName_offset; 4968 int java_lang_StackTraceElement::_fileName_offset; 4969 int java_lang_StackTraceElement::_lineNumber_offset; 4970 int java_lang_StackTraceElement::_moduleName_offset; 4971 int java_lang_StackTraceElement::_moduleVersion_offset; 4972 int java_lang_StackTraceElement::_classLoaderName_offset; 4973 int java_lang_StackTraceElement::_declaringClass_offset; 4974 int java_lang_StackTraceElement::_declaringClassObject_offset; 4975 4976 #define STACKTRACEELEMENT_FIELDS_DO(macro) \ 4977 macro(_declaringClassObject_offset, k, "declaringClassObject", class_signature, false); \ 4978 macro(_classLoaderName_offset, k, "classLoaderName", string_signature, false); \ 4979 macro(_moduleName_offset, k, "moduleName", string_signature, false); \ 4980 macro(_moduleVersion_offset, k, "moduleVersion", string_signature, false); \ 4981 macro(_declaringClass_offset, k, "declaringClass", string_signature, false); \ 4982 macro(_methodName_offset, k, "methodName", string_signature, false); \ 4983 macro(_fileName_offset, k, "fileName", string_signature, false); \ 4984 macro(_lineNumber_offset, k, "lineNumber", int_signature, false) 4985 4986 // Support for java_lang_StackTraceElement 4987 void java_lang_StackTraceElement::compute_offsets() { 4988 InstanceKlass* k = vmClasses::StackTraceElement_klass(); 4989 STACKTRACEELEMENT_FIELDS_DO(FIELD_COMPUTE_OFFSET); 4990 } 4991 4992 #if INCLUDE_CDS 4993 void java_lang_StackTraceElement::serialize_offsets(SerializeClosure* f) { 4994 STACKTRACEELEMENT_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 4995 } 4996 #endif 4997 4998 void java_lang_StackTraceElement::set_fileName(oop element, oop value) { 4999 element->obj_field_put(_fileName_offset, value); 5000 } 5001 5002 void java_lang_StackTraceElement::set_declaringClass(oop element, oop value) { 5003 element->obj_field_put(_declaringClass_offset, value); 5004 } 5005 5006 void java_lang_StackTraceElement::set_methodName(oop element, oop value) { 5007 element->obj_field_put(_methodName_offset, value); 5008 } 5009 5010 void java_lang_StackTraceElement::set_lineNumber(oop element, int value) { 5011 element->int_field_put(_lineNumber_offset, value); 5012 } 5013 5014 void java_lang_StackTraceElement::set_moduleName(oop element, oop value) { 5015 element->obj_field_put(_moduleName_offset, value); 5016 } 5017 5018 void java_lang_StackTraceElement::set_moduleVersion(oop element, oop value) { 5019 element->obj_field_put(_moduleVersion_offset, value); 5020 } 5021 5022 void java_lang_StackTraceElement::set_classLoaderName(oop element, oop value) { 5023 element->obj_field_put(_classLoaderName_offset, value); 5024 } 5025 5026 void java_lang_StackTraceElement::set_declaringClassObject(oop element, oop value) { 5027 element->obj_field_put(_declaringClassObject_offset, value); 5028 } 5029 5030 5031 // java_lang_AssertionStatusDirectives 5032 5033 int java_lang_AssertionStatusDirectives::_classes_offset; 5034 int java_lang_AssertionStatusDirectives::_classEnabled_offset; 5035 int java_lang_AssertionStatusDirectives::_packages_offset; 5036 int java_lang_AssertionStatusDirectives::_packageEnabled_offset; 5037 int java_lang_AssertionStatusDirectives::_deflt_offset; 5038 5039 // Support for java Assertions - java_lang_AssertionStatusDirectives. 5040 #define ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(macro) \ 5041 macro(_classes_offset, k, "classes", string_array_signature, false); \ 5042 macro(_classEnabled_offset, k, "classEnabled", bool_array_signature, false); \ 5043 macro(_packages_offset, k, "packages", string_array_signature, false); \ 5044 macro(_packageEnabled_offset, k, "packageEnabled", bool_array_signature, false); \ 5045 macro(_deflt_offset, k, "deflt", bool_signature, false) 5046 5047 void java_lang_AssertionStatusDirectives::compute_offsets() { 5048 InstanceKlass* k = vmClasses::AssertionStatusDirectives_klass(); 5049 ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(FIELD_COMPUTE_OFFSET); 5050 } 5051 5052 #if INCLUDE_CDS 5053 void java_lang_AssertionStatusDirectives::serialize_offsets(SerializeClosure* f) { 5054 ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 5055 } 5056 #endif 5057 5058 void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) { 5059 o->obj_field_put(_classes_offset, val); 5060 } 5061 5062 void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) { 5063 o->obj_field_put(_classEnabled_offset, val); 5064 } 5065 5066 void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) { 5067 o->obj_field_put(_packages_offset, val); 5068 } 5069 5070 void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) { 5071 o->obj_field_put(_packageEnabled_offset, val); 5072 } 5073 5074 void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) { 5075 o->bool_field_put(_deflt_offset, val); 5076 } 5077 5078 int java_util_concurrent_locks_AbstractOwnableSynchronizer::_owner_offset; 5079 5080 #define AOS_FIELDS_DO(macro) \ 5081 macro(_owner_offset, k, "exclusiveOwnerThread", thread_signature, false) 5082 5083 void java_util_concurrent_locks_AbstractOwnableSynchronizer::compute_offsets() { 5084 InstanceKlass* k = vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass(); 5085 AOS_FIELDS_DO(FIELD_COMPUTE_OFFSET); 5086 } 5087 5088 oop java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(oop obj) { 5089 assert(_owner_offset != 0, "Must be initialized"); 5090 return obj->obj_field(_owner_offset); 5091 } 5092 5093 #if INCLUDE_CDS 5094 void java_util_concurrent_locks_AbstractOwnableSynchronizer::serialize_offsets(SerializeClosure* f) { 5095 AOS_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 5096 } 5097 #endif 5098 5099 int vector_VectorPayload::_payload_offset; 5100 5101 #define VECTORPAYLOAD_FIELDS_DO(macro) \ 5102 macro(_payload_offset, k, "payload", object_signature, false) 5103 5104 void vector_VectorPayload::compute_offsets() { 5105 InstanceKlass* k = vmClasses::vector_VectorPayload_klass(); 5106 VECTORPAYLOAD_FIELDS_DO(FIELD_COMPUTE_OFFSET); 5107 } 5108 5109 #if INCLUDE_CDS 5110 void vector_VectorPayload::serialize_offsets(SerializeClosure* f) { 5111 VECTORPAYLOAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 5112 } 5113 #endif 5114 5115 void vector_VectorPayload::set_payload(oop o, oop val) { 5116 o->obj_field_put(_payload_offset, val); 5117 } 5118 5119 bool vector_VectorPayload::is_instance(oop obj) { 5120 return obj != nullptr && is_subclass(obj->klass()); 5121 } 5122 5123 int java_lang_Integer_IntegerCache::_static_cache_offset; 5124 int java_lang_Long_LongCache::_static_cache_offset; 5125 int java_lang_Character_CharacterCache::_static_cache_offset; 5126 int java_lang_Short_ShortCache::_static_cache_offset; 5127 int java_lang_Byte_ByteCache::_static_cache_offset; 5128 5129 #define INTEGER_CACHE_FIELDS_DO(macro) \ 5130 macro(_static_cache_offset, k, "cache", java_lang_Integer_array_signature, true) 5131 5132 void java_lang_Integer_IntegerCache::compute_offsets(InstanceKlass *k) { 5133 guarantee(k != nullptr && k->is_initialized(), "must be loaded and initialized"); 5134 INTEGER_CACHE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 5135 } 5136 5137 objArrayOop java_lang_Integer_IntegerCache::cache(InstanceKlass *ik) { 5138 oop base = ik->static_field_base_raw(); 5139 return objArrayOop(base->obj_field(_static_cache_offset)); 5140 } 5141 5142 Symbol* java_lang_Integer_IntegerCache::symbol() { 5143 return vmSymbols::java_lang_Integer_IntegerCache(); 5144 } 5145 5146 #if INCLUDE_CDS 5147 void java_lang_Integer_IntegerCache::serialize_offsets(SerializeClosure* f) { 5148 INTEGER_CACHE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 5149 } 5150 #endif 5151 #undef INTEGER_CACHE_FIELDS_DO 5152 5153 jint java_lang_Integer::value(oop obj) { 5154 jvalue v; 5155 java_lang_boxing_object::get_value(obj, &v); 5156 return v.i; 5157 } 5158 5159 #define LONG_CACHE_FIELDS_DO(macro) \ 5160 macro(_static_cache_offset, k, "cache", java_lang_Long_array_signature, true) 5161 5162 void java_lang_Long_LongCache::compute_offsets(InstanceKlass *k) { 5163 guarantee(k != nullptr && k->is_initialized(), "must be loaded and initialized"); 5164 LONG_CACHE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 5165 } 5166 5167 objArrayOop java_lang_Long_LongCache::cache(InstanceKlass *ik) { 5168 oop base = ik->static_field_base_raw(); 5169 return objArrayOop(base->obj_field(_static_cache_offset)); 5170 } 5171 5172 Symbol* java_lang_Long_LongCache::symbol() { 5173 return vmSymbols::java_lang_Long_LongCache(); 5174 } 5175 5176 #if INCLUDE_CDS 5177 void java_lang_Long_LongCache::serialize_offsets(SerializeClosure* f) { 5178 LONG_CACHE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 5179 } 5180 #endif 5181 #undef LONG_CACHE_FIELDS_DO 5182 5183 jlong java_lang_Long::value(oop obj) { 5184 jvalue v; 5185 java_lang_boxing_object::get_value(obj, &v); 5186 return v.j; 5187 } 5188 5189 #define CHARACTER_CACHE_FIELDS_DO(macro) \ 5190 macro(_static_cache_offset, k, "cache", java_lang_Character_array_signature, true) 5191 5192 void java_lang_Character_CharacterCache::compute_offsets(InstanceKlass *k) { 5193 guarantee(k != nullptr && k->is_initialized(), "must be loaded and initialized"); 5194 CHARACTER_CACHE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 5195 } 5196 5197 objArrayOop java_lang_Character_CharacterCache::cache(InstanceKlass *ik) { 5198 oop base = ik->static_field_base_raw(); 5199 return objArrayOop(base->obj_field(_static_cache_offset)); 5200 } 5201 5202 Symbol* java_lang_Character_CharacterCache::symbol() { 5203 return vmSymbols::java_lang_Character_CharacterCache(); 5204 } 5205 5206 #if INCLUDE_CDS 5207 void java_lang_Character_CharacterCache::serialize_offsets(SerializeClosure* f) { 5208 CHARACTER_CACHE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 5209 } 5210 #endif 5211 #undef CHARACTER_CACHE_FIELDS_DO 5212 5213 jchar java_lang_Character::value(oop obj) { 5214 jvalue v; 5215 java_lang_boxing_object::get_value(obj, &v); 5216 return v.c; 5217 } 5218 5219 #define SHORT_CACHE_FIELDS_DO(macro) \ 5220 macro(_static_cache_offset, k, "cache", java_lang_Short_array_signature, true) 5221 5222 void java_lang_Short_ShortCache::compute_offsets(InstanceKlass *k) { 5223 guarantee(k != nullptr && k->is_initialized(), "must be loaded and initialized"); 5224 SHORT_CACHE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 5225 } 5226 5227 objArrayOop java_lang_Short_ShortCache::cache(InstanceKlass *ik) { 5228 oop base = ik->static_field_base_raw(); 5229 return objArrayOop(base->obj_field(_static_cache_offset)); 5230 } 5231 5232 Symbol* java_lang_Short_ShortCache::symbol() { 5233 return vmSymbols::java_lang_Short_ShortCache(); 5234 } 5235 5236 #if INCLUDE_CDS 5237 void java_lang_Short_ShortCache::serialize_offsets(SerializeClosure* f) { 5238 SHORT_CACHE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 5239 } 5240 #endif 5241 #undef SHORT_CACHE_FIELDS_DO 5242 5243 jshort java_lang_Short::value(oop obj) { 5244 jvalue v; 5245 java_lang_boxing_object::get_value(obj, &v); 5246 return v.s; 5247 } 5248 5249 #define BYTE_CACHE_FIELDS_DO(macro) \ 5250 macro(_static_cache_offset, k, "cache", java_lang_Byte_array_signature, true) 5251 5252 void java_lang_Byte_ByteCache::compute_offsets(InstanceKlass *k) { 5253 guarantee(k != nullptr && k->is_initialized(), "must be loaded and initialized"); 5254 BYTE_CACHE_FIELDS_DO(FIELD_COMPUTE_OFFSET); 5255 } 5256 5257 objArrayOop java_lang_Byte_ByteCache::cache(InstanceKlass *ik) { 5258 oop base = ik->static_field_base_raw(); 5259 return objArrayOop(base->obj_field(_static_cache_offset)); 5260 } 5261 5262 Symbol* java_lang_Byte_ByteCache::symbol() { 5263 return vmSymbols::java_lang_Byte_ByteCache(); 5264 } 5265 5266 #if INCLUDE_CDS 5267 void java_lang_Byte_ByteCache::serialize_offsets(SerializeClosure* f) { 5268 BYTE_CACHE_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 5269 } 5270 #endif 5271 #undef BYTE_CACHE_FIELDS_DO 5272 5273 jbyte java_lang_Byte::value(oop obj) { 5274 jvalue v; 5275 java_lang_boxing_object::get_value(obj, &v); 5276 return v.b; 5277 } 5278 5279 int java_lang_Boolean::_static_TRUE_offset; 5280 int java_lang_Boolean::_static_FALSE_offset; 5281 5282 #define BOOLEAN_FIELDS_DO(macro) \ 5283 macro(_static_TRUE_offset, k, "TRUE", java_lang_Boolean_signature, true); \ 5284 macro(_static_FALSE_offset, k, "FALSE", java_lang_Boolean_signature, true) 5285 5286 5287 void java_lang_Boolean::compute_offsets(InstanceKlass *k) { 5288 guarantee(k != nullptr && k->is_initialized(), "must be loaded and initialized"); 5289 BOOLEAN_FIELDS_DO(FIELD_COMPUTE_OFFSET); 5290 } 5291 5292 oop java_lang_Boolean::get_TRUE(InstanceKlass *ik) { 5293 oop base = ik->static_field_base_raw(); 5294 return base->obj_field(_static_TRUE_offset); 5295 } 5296 5297 oop java_lang_Boolean::get_FALSE(InstanceKlass *ik) { 5298 oop base = ik->static_field_base_raw(); 5299 return base->obj_field(_static_FALSE_offset); 5300 } 5301 5302 Symbol* java_lang_Boolean::symbol() { 5303 return vmSymbols::java_lang_Boolean(); 5304 } 5305 5306 #if INCLUDE_CDS 5307 void java_lang_Boolean::serialize_offsets(SerializeClosure* f) { 5308 BOOLEAN_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 5309 } 5310 #endif 5311 #undef BOOLEAN_CACHE_FIELDS_DO 5312 5313 jboolean java_lang_Boolean::value(oop obj) { 5314 jvalue v; 5315 java_lang_boxing_object::get_value(obj, &v); 5316 return v.z; 5317 } 5318 5319 // java_lang_reflect_RecordComponent 5320 5321 int java_lang_reflect_RecordComponent::_clazz_offset; 5322 int java_lang_reflect_RecordComponent::_name_offset; 5323 int java_lang_reflect_RecordComponent::_type_offset; 5324 int java_lang_reflect_RecordComponent::_accessor_offset; 5325 int java_lang_reflect_RecordComponent::_signature_offset; 5326 int java_lang_reflect_RecordComponent::_annotations_offset; 5327 int java_lang_reflect_RecordComponent::_typeAnnotations_offset; 5328 5329 #define RECORDCOMPONENT_FIELDS_DO(macro) \ 5330 macro(_clazz_offset, k, "clazz", class_signature, false); \ 5331 macro(_name_offset, k, "name", string_signature, false); \ 5332 macro(_type_offset, k, "type", class_signature, false); \ 5333 macro(_accessor_offset, k, "accessor", reflect_method_signature, false); \ 5334 macro(_signature_offset, k, "signature", string_signature, false); \ 5335 macro(_annotations_offset, k, "annotations", byte_array_signature, false); \ 5336 macro(_typeAnnotations_offset, k, "typeAnnotations", byte_array_signature, false); 5337 5338 // Support for java_lang_reflect_RecordComponent 5339 void java_lang_reflect_RecordComponent::compute_offsets() { 5340 InstanceKlass* k = vmClasses::RecordComponent_klass(); 5341 RECORDCOMPONENT_FIELDS_DO(FIELD_COMPUTE_OFFSET); 5342 } 5343 5344 #if INCLUDE_CDS 5345 void java_lang_reflect_RecordComponent::serialize_offsets(SerializeClosure* f) { 5346 RECORDCOMPONENT_FIELDS_DO(FIELD_SERIALIZE_OFFSET); 5347 } 5348 #endif 5349 5350 void java_lang_reflect_RecordComponent::set_clazz(oop element, oop value) { 5351 element->obj_field_put(_clazz_offset, value); 5352 } 5353 5354 void java_lang_reflect_RecordComponent::set_name(oop element, oop value) { 5355 element->obj_field_put(_name_offset, value); 5356 } 5357 5358 void java_lang_reflect_RecordComponent::set_type(oop element, oop value) { 5359 element->obj_field_put(_type_offset, value); 5360 } 5361 5362 void java_lang_reflect_RecordComponent::set_accessor(oop element, oop value) { 5363 element->obj_field_put(_accessor_offset, value); 5364 } 5365 5366 void java_lang_reflect_RecordComponent::set_signature(oop element, oop value) { 5367 element->obj_field_put(_signature_offset, value); 5368 } 5369 5370 void java_lang_reflect_RecordComponent::set_annotations(oop element, oop value) { 5371 element->obj_field_put(_annotations_offset, value); 5372 } 5373 5374 void java_lang_reflect_RecordComponent::set_typeAnnotations(oop element, oop value) { 5375 element->obj_field_put(_typeAnnotations_offset, value); 5376 } 5377 5378 // java_lang_InternalError 5379 int java_lang_InternalError::_during_unsafe_access_offset; 5380 5381 void java_lang_InternalError::set_during_unsafe_access(oop internal_error) { 5382 internal_error->bool_field_put(_during_unsafe_access_offset, true); 5383 } 5384 5385 jboolean java_lang_InternalError::during_unsafe_access(oop internal_error) { 5386 return internal_error->bool_field(_during_unsafe_access_offset); 5387 } 5388 5389 void java_lang_InternalError::compute_offsets() { 5390 INTERNALERROR_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET); 5391 } 5392 5393 #if INCLUDE_CDS 5394 void java_lang_InternalError::serialize_offsets(SerializeClosure* f) { 5395 INTERNALERROR_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET); 5396 } 5397 #endif 5398 5399 #define BASIC_JAVA_CLASSES_DO_PART1(f) \ 5400 f(java_lang_Class) \ 5401 f(java_lang_String) \ 5402 f(java_lang_ref_Reference) \ 5403 //end 5404 5405 #define BASIC_JAVA_CLASSES_DO_PART2(f) \ 5406 f(java_lang_System) \ 5407 f(java_lang_ClassLoader) \ 5408 f(java_lang_Throwable) \ 5409 f(java_lang_Thread) \ 5410 f(java_lang_Thread_FieldHolder) \ 5411 f(java_lang_Thread_Constants) \ 5412 f(java_lang_ThreadGroup) \ 5413 f(java_lang_VirtualThread) \ 5414 f(java_lang_InternalError) \ 5415 f(java_lang_AssertionStatusDirectives) \ 5416 f(java_lang_ref_SoftReference) \ 5417 f(java_lang_invoke_MethodHandle) \ 5418 f(java_lang_invoke_DirectMethodHandle) \ 5419 f(java_lang_invoke_MemberName) \ 5420 f(java_lang_invoke_ResolvedMethodName) \ 5421 f(java_lang_invoke_LambdaForm) \ 5422 f(java_lang_invoke_MethodType) \ 5423 f(java_lang_invoke_CallSite) \ 5424 f(java_lang_invoke_ConstantCallSite) \ 5425 f(java_lang_invoke_MethodHandleNatives_CallSiteContext) \ 5426 f(java_security_AccessControlContext) \ 5427 f(java_lang_reflect_AccessibleObject) \ 5428 f(java_lang_reflect_Method) \ 5429 f(java_lang_reflect_Constructor) \ 5430 f(java_lang_reflect_Field) \ 5431 f(java_lang_reflect_RecordComponent) \ 5432 f(reflect_ConstantPool) \ 5433 f(java_lang_reflect_Parameter) \ 5434 f(java_lang_Module) \ 5435 f(java_lang_StackTraceElement) \ 5436 f(java_lang_ClassFrameInfo) \ 5437 f(java_lang_StackFrameInfo) \ 5438 f(java_lang_LiveStackFrameInfo) \ 5439 f(jdk_internal_vm_ContinuationScope) \ 5440 f(jdk_internal_vm_Continuation) \ 5441 f(jdk_internal_vm_StackChunk) \ 5442 f(java_util_concurrent_locks_AbstractOwnableSynchronizer) \ 5443 f(jdk_internal_foreign_abi_NativeEntryPoint) \ 5444 f(jdk_internal_foreign_abi_ABIDescriptor) \ 5445 f(jdk_internal_foreign_abi_VMStorage) \ 5446 f(jdk_internal_foreign_abi_CallConv) \ 5447 f(jdk_internal_misc_UnsafeConstants) \ 5448 f(java_lang_boxing_object) \ 5449 f(vector_VectorPayload) \ 5450 //end 5451 5452 #define BASIC_JAVA_CLASSES_DO(f) \ 5453 BASIC_JAVA_CLASSES_DO_PART1(f) \ 5454 BASIC_JAVA_CLASSES_DO_PART2(f) 5455 5456 #define DO_COMPUTE_OFFSETS(k) k::compute_offsets(); 5457 5458 // Compute field offsets of all the classes in this file 5459 void JavaClasses::compute_offsets() { 5460 if (CDSConfig::is_using_archive()) { 5461 JVMTI_ONLY(assert(JvmtiExport::is_early_phase() && !(JvmtiExport::should_post_class_file_load_hook() && 5462 JvmtiExport::has_early_class_hook_env()), 5463 "JavaClasses::compute_offsets() must be called in early JVMTI phase.")); 5464 // None of the classes used by the rest of this function can be replaced by 5465 // JVMTI ClassFileLoadHook. 5466 // We are safe to use the archived offsets, which have already been restored 5467 // by JavaClasses::serialize_offsets, without computing the offsets again. 5468 return; 5469 } 5470 5471 // We have already called the compute_offsets() of the 5472 // BASIC_JAVA_CLASSES_DO_PART1 classes (java_lang_String, java_lang_Class and 5473 // java_lang_ref_Reference) earlier inside vmClasses::resolve_all() 5474 BASIC_JAVA_CLASSES_DO_PART2(DO_COMPUTE_OFFSETS); 5475 } 5476 5477 #if INCLUDE_CDS 5478 #define DO_SERIALIZE_OFFSETS(k) k::serialize_offsets(soc); 5479 5480 void JavaClasses::serialize_offsets(SerializeClosure* soc) { 5481 BASIC_JAVA_CLASSES_DO(DO_SERIALIZE_OFFSETS); 5482 } 5483 #endif 5484 5485 #if INCLUDE_CDS_JAVA_HEAP 5486 bool JavaClasses::is_supported_for_archiving(oop obj) { 5487 Klass* klass = obj->klass(); 5488 5489 if (!CDSConfig::is_dumping_invokedynamic()) { 5490 // These are supported by CDS only when CDSConfig::is_dumping_invokedynamic() is enabled. 5491 if (klass == vmClasses::ResolvedMethodName_klass() || 5492 klass == vmClasses::MemberName_klass() || 5493 klass == vmClasses::Context_klass()) { 5494 return false; 5495 } 5496 } 5497 5498 if (klass->is_subclass_of(vmClasses::Reference_klass())) { 5499 // It's problematic to archive Reference objects. One of the reasons is that 5500 // Reference::discovered may pull in unwanted objects (see JDK-8284336) 5501 return false; 5502 } 5503 5504 return true; 5505 } 5506 #endif 5507 5508 #ifndef PRODUCT 5509 5510 // These functions exist to assert the validity of de-serialized offsets in boxing object as a sanity check. 5511 5512 bool JavaClasses::check_offset(const char *klass_name, int deserialized_offset, const char *field_name, 5513 const char* field_sig) { 5514 EXCEPTION_MARK; 5515 fieldDescriptor fd; 5516 TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name); 5517 Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH); 5518 InstanceKlass* ik = InstanceKlass::cast(k); 5519 TempNewSymbol f_name = SymbolTable::new_symbol(field_name); 5520 TempNewSymbol f_sig = SymbolTable::new_symbol(field_sig); 5521 if (!ik->find_local_field(f_name, f_sig, &fd)) { 5522 tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name); 5523 return false; 5524 } 5525 if (fd.is_static()) { 5526 tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name); 5527 return false; 5528 } 5529 if (fd.offset() == deserialized_offset ) { 5530 return true; 5531 } else { 5532 tty->print_cr("Offset of nonstatic field %s.%s is deserialized as %d but should really be %d.", 5533 klass_name, field_name, deserialized_offset, fd.offset()); 5534 return false; 5535 } 5536 } 5537 5538 void JavaClasses::check_offsets() { 5539 bool valid = true; 5540 5541 #define CHECK_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \ 5542 valid &= check_offset(klass_name, cpp_klass_name :: _##field_name ## _offset, #field_name, field_sig) 5543 5544 #define CHECK_LONG_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \ 5545 valid &= check_offset(klass_name, cpp_klass_name :: _##long_ ## field_name ## _offset, #field_name, field_sig) 5546 5547 // Boxed primitive objects (java_lang_boxing_object) 5548 5549 CHECK_OFFSET("java/lang/Boolean", java_lang_boxing_object, value, "Z"); 5550 CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C"); 5551 CHECK_OFFSET("java/lang/Float", java_lang_boxing_object, value, "F"); 5552 CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D"); 5553 CHECK_OFFSET("java/lang/Byte", java_lang_boxing_object, value, "B"); 5554 CHECK_OFFSET("java/lang/Short", java_lang_boxing_object, value, "S"); 5555 CHECK_OFFSET("java/lang/Integer", java_lang_boxing_object, value, "I"); 5556 CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J"); 5557 5558 if (!valid) vm_exit_during_initialization("Field offset verification failed"); 5559 } 5560 5561 #endif // PRODUCT 5562 5563 int InjectedField::compute_offset() { 5564 InstanceKlass* ik = InstanceKlass::cast(klass()); 5565 for (AllFieldStream fs(ik); !fs.done(); fs.next()) { 5566 if (!may_be_java && !fs.field_flags().is_injected()) { 5567 // Only look at injected fields 5568 continue; 5569 } 5570 if (fs.name() == name() && fs.signature() == signature()) { 5571 return fs.offset(); 5572 } 5573 } 5574 ResourceMark rm; 5575 tty->print_cr("Invalid layout of %s at %s/%s%s", ik->external_name(), name()->as_C_string(), signature()->as_C_string(), may_be_java ? " (may_be_java)" : ""); 5576 #ifndef PRODUCT 5577 ik->print(); 5578 tty->print_cr("all fields:"); 5579 for (AllFieldStream fs(ik); !fs.done(); fs.next()) { 5580 tty->print_cr(" name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int()); 5581 } 5582 #endif //PRODUCT 5583 vm_exit_during_initialization("Invalid layout of well-known class: use -Xlog:class+load=info to see the origin of the problem class"); 5584 return -1; 5585 } 5586 5587 void javaClasses_init() { 5588 JavaClasses::compute_offsets(); 5589 JavaClasses::check_offsets(); 5590 FilteredFieldsMap::initialize(); // must be done after computing offsets. 5591 }