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