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