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