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