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