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