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