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