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