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