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(), Handle(), CHECK);
 827 }
 828 
 829 void java_lang_Class::initialize_mirror_fields(Klass* k,
 830                                                Handle mirror,
 831                                                Handle protection_domain,
 832                                                Handle classData,
 833                                                TRAPS) {
 834   // Allocate a simple java object for a lock.
 835   // This needs to be a java object because during class initialization
 836   // it can be held across a java call.
 837   typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
 838   set_init_lock(mirror(), r);
 839 
 840   // Set protection domain also
 841   set_protection_domain(mirror(), protection_domain());
 842 
 843   // Initialize static fields
 844   InstanceKlass::cast(k)->do_local_static_fields(&initialize_static_field, mirror, CHECK);
 845 
 846  // Set classData
 847   set_class_data(mirror(), classData());
 848 }
 849 
 850 // Set the java.lang.Module module field in the java_lang_Class mirror
 851 void java_lang_Class::set_mirror_module_field(Klass* k, Handle mirror, Handle module, TRAPS) {
 852   if (module.is_null()) {
 853     // During startup, the module may be NULL only if java.base has not been defined yet.
 854     // Put the class on the fixup_module_list to patch later when the java.lang.Module
 855     // for java.base is known. But note that since we captured the NULL module another
 856     // thread may have completed that initialization.
 857 
 858     bool javabase_was_defined = false;
 859     {
 860       MutexLocker m1(Module_lock, THREAD);
 861       // Keep list of classes needing java.base module fixup
 862       if (!ModuleEntryTable::javabase_defined()) {
 863         assert(k->java_mirror() != NULL, "Class's mirror is null");
 864         k->class_loader_data()->inc_keep_alive();
 865         assert(fixup_module_field_list() != NULL, "fixup_module_field_list not initialized");
 866         fixup_module_field_list()->push(k);
 867       } else {
 868         javabase_was_defined = true;
 869       }
 870     }
 871 
 872     // If java.base was already defined then patch this particular class with java.base.
 873     if (javabase_was_defined) {
 874       ModuleEntry *javabase_entry = ModuleEntryTable::javabase_moduleEntry();
 875       assert(javabase_entry != NULL && javabase_entry->module() != NULL,
 876              "Setting class module field, " JAVA_BASE_NAME " should be defined");
 877       Handle javabase_handle(THREAD, javabase_entry->module());
 878       set_module(mirror(), javabase_handle());
 879     }
 880   } else {
 881     assert(Universe::is_module_initialized() ||
 882            (ModuleEntryTable::javabase_defined() &&
 883             (oopDesc::equals(module(), ModuleEntryTable::javabase_moduleEntry()->module()))),
 884            "Incorrect java.lang.Module specification while creating mirror");
 885     set_module(mirror(), module());
 886   }
 887 }
 888 
 889 // Statically allocate fixup lists because they always get created.
 890 void java_lang_Class::allocate_fixup_lists() {
 891   GrowableArray<Klass*>* mirror_list =
 892     new (ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(40, true);
 893   set_fixup_mirror_list(mirror_list);
 894 
 895   GrowableArray<Klass*>* module_list =
 896     new (ResourceObj::C_HEAP, mtModule) GrowableArray<Klass*>(500, true);
 897   set_fixup_module_field_list(module_list);
 898 }
 899 
 900 void java_lang_Class::create_mirror(Klass* k, Handle class_loader,
 901                                     Handle module, Handle protection_domain,
 902                                     Handle classData, TRAPS) {
 903   assert(k != NULL, "Use create_basic_type_mirror for primitive types");
 904   assert(k->java_mirror() == NULL, "should only assign mirror once");
 905 
 906   // Use this moment of initialization to cache modifier_flags also,
 907   // to support Class.getModifiers().  Instance classes recalculate
 908   // the cached flags after the class file is parsed, but before the
 909   // class is put into the system dictionary.
 910   int computed_modifiers = k->compute_modifier_flags(CHECK);
 911   k->set_modifier_flags(computed_modifiers);
 912   // Class_klass has to be loaded because it is used to allocate
 913   // the mirror.
 914   if (SystemDictionary::Class_klass_loaded()) {
 915     // Allocate mirror (java.lang.Class instance)
 916     oop mirror_oop = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK);
 917     Handle mirror(THREAD, mirror_oop);
 918     Handle comp_mirror;
 919 
 920     // Setup indirection from mirror->klass
 921     java_lang_Class::set_klass(mirror(), k);
 922 
 923     InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
 924     assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");
 925 
 926     java_lang_Class::set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));
 927 
 928     // It might also have a component mirror.  This mirror must already exist.
 929     if (k->is_array_klass()) {
 930       if (k->is_typeArray_klass()) {
 931         BasicType type = TypeArrayKlass::cast(k)->element_type();
 932         comp_mirror = Handle(THREAD, Universe::java_mirror(type));
 933       } else {
 934         assert(k->is_objArray_klass(), "Must be");
 935         Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
 936         assert(element_klass != NULL, "Must have an element klass");
 937         comp_mirror = Handle(THREAD, element_klass->java_mirror());
 938       }
 939       assert(comp_mirror() != NULL, "must have a mirror");
 940 
 941       // Two-way link between the array klass and its component mirror:
 942       // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
 943       set_component_mirror(mirror(), comp_mirror());
 944       // See below for ordering dependencies between field array_klass in component mirror
 945       // and java_mirror in this klass.
 946     } else {
 947       assert(k->is_instance_klass(), "Must be");
 948 
 949       initialize_mirror_fields(k, mirror, protection_domain, classData, THREAD);
 950       if (HAS_PENDING_EXCEPTION) {
 951         // If any of the fields throws an exception like OOM remove the klass field
 952         // from the mirror so GC doesn't follow it after the klass has been deallocated.
 953         // This mirror looks like a primitive type, which logically it is because it
 954         // it represents no class.
 955         java_lang_Class::set_klass(mirror(), NULL);
 956         return;
 957       }
 958     }
 959 
 960     // set the classLoader field in the java_lang_Class instance
 961     assert(oopDesc::equals(class_loader(), k->class_loader()), "should be same");
 962     set_class_loader(mirror(), class_loader());
 963 
 964     // Setup indirection from klass->mirror
 965     // after any exceptions can happen during allocations.
 966     k->set_java_mirror(mirror);
 967 
 968     // Set the module field in the java_lang_Class instance.  This must be done
 969     // after the mirror is set.
 970     set_mirror_module_field(k, mirror, module, THREAD);
 971 
 972     if (comp_mirror() != NULL) {
 973       // Set after k->java_mirror() is published, because compiled code running
 974       // concurrently doesn't expect a k to have a null java_mirror.
 975       release_set_array_klass(comp_mirror(), k);
 976     }
 977   } else {
 978     assert(fixup_mirror_list() != NULL, "fixup_mirror_list not initialized");
 979     fixup_mirror_list()->push(k);
 980   }
 981 }
 982 
 983 #if INCLUDE_CDS_JAVA_HEAP
 984 // Clears mirror fields. Static final fields with initial values are reloaded
 985 // from constant pool. The object identity hash is in the object header and is
 986 // not affected.
 987 class ResetMirrorField: public FieldClosure {
 988  private:
 989   Handle _m;
 990 
 991  public:
 992   ResetMirrorField(Handle mirror) : _m(mirror) {}
 993 
 994   void do_field(fieldDescriptor* fd) {
 995     assert(DumpSharedSpaces, "dump time only");
 996     assert(_m.not_null(), "Mirror cannot be NULL");
 997 
 998     if (fd->is_static() && fd->has_initial_value()) {
 999       initialize_static_field(fd, _m, Thread::current());
1000       return;
1001     }
1002 
1003     BasicType ft = fd->field_type();
1004     switch (ft) {
1005       case T_BYTE:
1006         _m()->byte_field_put(fd->offset(), 0);
1007         break;
1008       case T_CHAR:
1009         _m()->char_field_put(fd->offset(), 0);
1010         break;
1011       case T_DOUBLE:
1012         _m()->double_field_put(fd->offset(), 0);
1013         break;
1014       case T_FLOAT:
1015         _m()->float_field_put(fd->offset(), 0);
1016         break;
1017       case T_INT:
1018         _m()->int_field_put(fd->offset(), 0);
1019         break;
1020       case T_LONG:
1021         _m()->long_field_put(fd->offset(), 0);
1022         break;
1023       case T_SHORT:
1024         _m()->short_field_put(fd->offset(), 0);
1025         break;
1026       case T_BOOLEAN:
1027         _m()->bool_field_put(fd->offset(), false);
1028         break;
1029       case T_ARRAY:
1030       case T_OBJECT: {
1031         // It might be useful to cache the String field, but
1032         // for now just clear out any reference field
1033         oop o = _m()->obj_field(fd->offset());
1034         _m()->obj_field_put(fd->offset(), NULL);
1035         break;
1036       }
1037       default:
1038         ShouldNotReachHere();
1039         break;
1040      }
1041   }
1042 };
1043 
1044 void java_lang_Class::archive_basic_type_mirrors(TRAPS) {
1045   assert(HeapShared::is_heap_object_archiving_allowed(),
1046          "HeapShared::is_heap_object_archiving_allowed() must be true");
1047 
1048   for (int t = 0; t <= T_VOID; t++) {
1049     oop m = Universe::_mirrors[t];
1050     if (m != NULL) {
1051       // Update the field at _array_klass_offset to point to the relocated array klass.
1052       oop archived_m = HeapShared::archive_heap_object(m, THREAD);
1053       assert(archived_m != NULL, "sanity");
1054       Klass *ak = (Klass*)(archived_m->metadata_field(_array_klass_offset));
1055       assert(ak != NULL || t == T_VOID, "should not be NULL");
1056       if (ak != NULL) {
1057         Klass *reloc_ak = MetaspaceShared::get_relocated_klass(ak);
1058         archived_m->metadata_field_put(_array_klass_offset, reloc_ak);
1059       }
1060 
1061       // Clear the fields. Just to be safe
1062       Klass *k = m->klass();
1063       Handle archived_mirror_h(THREAD, archived_m);
1064       ResetMirrorField reset(archived_mirror_h);
1065       InstanceKlass::cast(k)->do_nonstatic_fields(&reset);
1066 
1067       log_trace(cds, heap, mirror)(
1068         "Archived %s mirror object from " PTR_FORMAT " ==> " PTR_FORMAT,
1069         type2name((BasicType)t), p2i(Universe::_mirrors[t]), p2i(archived_m));
1070 
1071       Universe::_mirrors[t] = archived_m;
1072     }
1073   }
1074 
1075   assert(Universe::_mirrors[T_INT] != NULL &&
1076          Universe::_mirrors[T_FLOAT] != NULL &&
1077          Universe::_mirrors[T_DOUBLE] != NULL &&
1078          Universe::_mirrors[T_BYTE] != NULL &&
1079          Universe::_mirrors[T_BOOLEAN] != NULL &&
1080          Universe::_mirrors[T_CHAR] != NULL &&
1081          Universe::_mirrors[T_LONG] != NULL &&
1082          Universe::_mirrors[T_SHORT] != NULL &&
1083          Universe::_mirrors[T_VOID] != NULL, "sanity");
1084 
1085   Universe::set_int_mirror(Universe::_mirrors[T_INT]);
1086   Universe::set_float_mirror(Universe::_mirrors[T_FLOAT]);
1087   Universe::set_double_mirror(Universe::_mirrors[T_DOUBLE]);
1088   Universe::set_byte_mirror(Universe::_mirrors[T_BYTE]);
1089   Universe::set_bool_mirror(Universe::_mirrors[T_BOOLEAN]);
1090   Universe::set_char_mirror(Universe::_mirrors[T_CHAR]);
1091   Universe::set_long_mirror(Universe::_mirrors[T_LONG]);
1092   Universe::set_short_mirror(Universe::_mirrors[T_SHORT]);
1093   Universe::set_void_mirror(Universe::_mirrors[T_VOID]);
1094 }
1095 
1096 //
1097 // After the mirror object is successfully archived, the archived
1098 // klass is set with _has_archived_raw_mirror flag.
1099 //
1100 // The _has_archived_raw_mirror flag is cleared at runtime when the
1101 // archived mirror is restored. If archived java heap data cannot
1102 // be used at runtime, new mirror object is created for the shared
1103 // class. The _has_archived_raw_mirror is cleared also during the process.
1104 oop java_lang_Class::archive_mirror(Klass* k, TRAPS) {
1105   assert(HeapShared::is_heap_object_archiving_allowed(),
1106          "HeapShared::is_heap_object_archiving_allowed() must be true");
1107 
1108   // Mirror is already archived
1109   if (k->has_raw_archived_mirror()) {
1110     assert(k->archived_java_mirror_raw() != NULL, "no archived mirror");
1111     return k->archived_java_mirror_raw();
1112   }
1113 
1114   // No mirror
1115   oop mirror = k->java_mirror();
1116   if (mirror == NULL) {
1117     return NULL;
1118   }
1119 
1120   if (k->is_instance_klass()) {
1121     InstanceKlass *ik = InstanceKlass::cast(k);
1122     assert(ik->signers() == NULL, "class with signer should have been excluded");
1123 
1124     if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
1125           ik->is_shared_app_class())) {
1126       // Archiving mirror for classes from non-builtin loaders is not
1127       // supported. Clear the _java_mirror within the archived class.
1128       k->set_java_mirror_handle(NULL);
1129       return NULL;
1130     }
1131   }
1132 
1133   // Now start archiving the mirror object
1134   oop archived_mirror = HeapShared::archive_heap_object(mirror, THREAD);
1135   if (archived_mirror == NULL) {
1136     return NULL;
1137   }
1138 
1139   archived_mirror = process_archived_mirror(k, mirror, archived_mirror, THREAD);
1140   if (archived_mirror == NULL) {
1141     return NULL;
1142   }
1143 
1144   k->set_archived_java_mirror_raw(archived_mirror);
1145 
1146   k->set_has_raw_archived_mirror();
1147 
1148   ResourceMark rm;
1149   log_trace(cds, heap, mirror)(
1150     "Archived %s mirror object from " PTR_FORMAT " ==> " PTR_FORMAT,
1151     k->external_name(), p2i(mirror), p2i(archived_mirror));
1152 
1153   return archived_mirror;
1154 }
1155 
1156 // The process is based on create_mirror().
1157 oop java_lang_Class::process_archived_mirror(Klass* k, oop mirror,
1158                                              oop archived_mirror,
1159                                              Thread *THREAD) {
1160   // Clear nonstatic fields in archived mirror. Some of the fields will be set
1161   // to archived metadata and objects below.
1162   Klass *c = archived_mirror->klass();
1163   Handle archived_mirror_h(THREAD, archived_mirror);
1164   ResetMirrorField reset(archived_mirror_h);
1165   InstanceKlass::cast(c)->do_nonstatic_fields(&reset);
1166 
1167   if (k->is_array_klass()) {
1168     oop archived_comp_mirror;
1169     if (k->is_typeArray_klass()) {
1170       // The primitive type mirrors are already archived. Get the archived mirror.
1171       oop comp_mirror = java_lang_Class::component_mirror(mirror);
1172       archived_comp_mirror = HeapShared::find_archived_heap_object(comp_mirror);
1173       assert(archived_comp_mirror != NULL, "Must be");
1174     } else {
1175       assert(k->is_objArray_klass(), "Must be");
1176       Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
1177       assert(element_klass != NULL, "Must have an element klass");
1178       archived_comp_mirror = archive_mirror(element_klass, THREAD);
1179       if (archived_comp_mirror == NULL) {
1180         return NULL;
1181       }
1182     }
1183     java_lang_Class::set_component_mirror(archived_mirror, archived_comp_mirror);
1184   } else {
1185     assert(k->is_instance_klass(), "Must be");
1186 
1187     // Reset local static fields in the mirror
1188     InstanceKlass::cast(k)->do_local_static_fields(&reset);
1189 
1190     java_lang_Class:set_init_lock(archived_mirror, NULL);
1191 
1192     set_protection_domain(archived_mirror, NULL);
1193   }
1194 
1195   // clear class loader and mirror_module_field
1196   set_class_loader(archived_mirror, NULL);
1197   set_module(archived_mirror, NULL);
1198 
1199   // The archived mirror's field at _klass_offset is still pointing to the original
1200   // klass. Updated the field in the archived mirror to point to the relocated
1201   // klass in the archive.
1202   Klass *reloc_k = MetaspaceShared::get_relocated_klass(as_Klass(mirror));
1203   log_debug(cds, heap, mirror)(
1204     "Relocate mirror metadata field at _klass_offset from " PTR_FORMAT " ==> " PTR_FORMAT,
1205     p2i(as_Klass(mirror)), p2i(reloc_k));
1206   archived_mirror->metadata_field_put(_klass_offset, reloc_k);
1207 
1208   // The field at _array_klass_offset is pointing to the original one dimension
1209   // higher array klass if exists. Relocate the pointer.
1210   Klass *arr = array_klass_acquire(mirror);
1211   if (arr != NULL) {
1212     Klass *reloc_arr = MetaspaceShared::get_relocated_klass(arr);
1213     log_debug(cds, heap, mirror)(
1214       "Relocate mirror metadata field at _array_klass_offset from " PTR_FORMAT " ==> " PTR_FORMAT,
1215       p2i(arr), p2i(reloc_arr));
1216     archived_mirror->metadata_field_put(_array_klass_offset, reloc_arr);
1217   }
1218   return archived_mirror;
1219 }
1220 
1221 // Returns true if the mirror is updated, false if no archived mirror
1222 // data is present. After the archived mirror object is restored, the
1223 // shared klass' _has_raw_archived_mirror flag is cleared.
1224 bool java_lang_Class::restore_archived_mirror(Klass *k,
1225                                               Handle class_loader, Handle module,
1226                                               Handle protection_domain, TRAPS) {
1227   // Postpone restoring archived mirror until java.lang.Class is loaded. Please
1228   // see more details in SystemDictionary::resolve_well_known_classes().
1229   if (!SystemDictionary::Class_klass_loaded()) {
1230     assert(fixup_mirror_list() != NULL, "fixup_mirror_list not initialized");
1231     fixup_mirror_list()->push(k);
1232     return true;
1233   }
1234 
1235   oop m = HeapShared::materialize_archived_object(k->archived_java_mirror_raw_narrow());
1236 
1237   if (m == NULL) {
1238     return false;
1239   }
1240 
1241   log_debug(cds, mirror)("Archived mirror is: " PTR_FORMAT, p2i(m));
1242 
1243   // mirror is archived, restore
1244   assert(HeapShared::is_archived_object(m), "must be archived mirror object");
1245   Handle mirror(THREAD, m);
1246 
1247   if (!k->is_array_klass()) {
1248     // - local static final fields with initial values were initialized at dump time
1249 
1250     // create the init_lock
1251     typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK_(false));
1252     set_init_lock(mirror(), r);
1253 
1254     if (protection_domain.not_null()) {
1255       set_protection_domain(mirror(), protection_domain());
1256     }
1257   }
1258 
1259   assert(class_loader() == k->class_loader(), "should be same");
1260   if (class_loader.not_null()) {
1261     set_class_loader(mirror(), class_loader());
1262   }
1263 
1264   k->set_java_mirror(mirror);
1265   k->clear_has_raw_archived_mirror();
1266 
1267   set_mirror_module_field(k, mirror, module, THREAD);
1268 
1269   ResourceMark rm;
1270   log_trace(cds, heap, mirror)(
1271     "Restored %s archived mirror " PTR_FORMAT, k->external_name(), p2i(mirror()));
1272 
1273   return true;
1274 }
1275 #endif // INCLUDE_CDS_JAVA_HEAP
1276 
1277 void java_lang_Class::fixup_module_field(Klass* k, Handle module) {
1278   assert(_module_offset != 0, "must have been computed already");
1279   java_lang_Class::set_module(k->java_mirror(), module());
1280 }
1281 
1282 int  java_lang_Class::oop_size(oop java_class) {
1283   assert(_oop_size_offset != 0, "must be set");
1284   int size = java_class->int_field(_oop_size_offset);
1285   assert(size > 0, "Oop size must be greater than zero, not %d", size);
1286   return size;
1287 }
1288 
1289 
1290 void java_lang_Class::set_oop_size(HeapWord* java_class, int size) {
1291   assert(_oop_size_offset != 0, "must be set");
1292   assert(size > 0, "Oop size must be greater than zero, not %d", size);
1293   *(int*)(((char*)java_class) + _oop_size_offset) = size;
1294 }
1295 
1296 int  java_lang_Class::static_oop_field_count(oop java_class) {
1297   assert(_static_oop_field_count_offset != 0, "must be set");
1298   return java_class->int_field(_static_oop_field_count_offset);
1299 }
1300 
1301 int  java_lang_Class::static_oop_field_count_raw(oop java_class) {
1302   assert(_static_oop_field_count_offset != 0, "must be set");
1303   return java_class->int_field_raw(_static_oop_field_count_offset);
1304 }
1305 
1306 void java_lang_Class::set_static_oop_field_count(oop java_class, int size) {
1307   assert(_static_oop_field_count_offset != 0, "must be set");
1308   java_class->int_field_put(_static_oop_field_count_offset, size);
1309 }
1310 
1311 oop java_lang_Class::protection_domain(oop java_class) {
1312   assert(_protection_domain_offset != 0, "must be set");
1313   return java_class->obj_field(_protection_domain_offset);
1314 }
1315 void java_lang_Class::set_protection_domain(oop java_class, oop pd) {
1316   assert(_protection_domain_offset != 0, "must be set");
1317   java_class->obj_field_put(_protection_domain_offset, pd);
1318 }
1319 
1320 void java_lang_Class::set_component_mirror(oop java_class, oop comp_mirror) {
1321   assert(_component_mirror_offset != 0, "must be set");
1322     java_class->obj_field_put(_component_mirror_offset, comp_mirror);
1323   }
1324 oop java_lang_Class::component_mirror(oop java_class) {
1325   assert(_component_mirror_offset != 0, "must be set");
1326   return java_class->obj_field(_component_mirror_offset);
1327 }
1328 
1329 oop java_lang_Class::init_lock(oop java_class) {
1330   assert(_init_lock_offset != 0, "must be set");
1331   return java_class->obj_field(_init_lock_offset);
1332 }
1333 void java_lang_Class::set_init_lock(oop java_class, oop init_lock) {
1334   assert(_init_lock_offset != 0, "must be set");
1335   java_class->obj_field_put(_init_lock_offset, init_lock);
1336 }
1337 
1338 objArrayOop java_lang_Class::signers(oop java_class) {
1339   assert(_signers_offset != 0, "must be set");
1340   return (objArrayOop)java_class->obj_field(_signers_offset);
1341 }
1342 void java_lang_Class::set_signers(oop java_class, objArrayOop signers) {
1343   assert(_signers_offset != 0, "must be set");
1344   java_class->obj_field_put(_signers_offset, (oop)signers);
1345 }
1346 
1347 oop java_lang_Class::class_data(oop java_class) {
1348   assert(_classData_offset != 0, "must be set");
1349   return java_class->obj_field(_classData_offset);
1350 }
1351 void java_lang_Class::set_class_data(oop java_class, oop class_data) {
1352   assert(_classData_offset != 0, "must be set");
1353   java_class->obj_field_put(_classData_offset, class_data);
1354 }
1355 
1356 
1357 void java_lang_Class::set_class_loader(oop java_class, oop loader) {
1358   // jdk7 runs Queens in bootstrapping and jdk8-9 has no coordinated pushes yet.
1359   if (_class_loader_offset != 0) {
1360     java_class->obj_field_put(_class_loader_offset, loader);
1361   }
1362 }
1363 
1364 oop java_lang_Class::class_loader(oop java_class) {
1365   assert(_class_loader_offset != 0, "must be set");
1366   return java_class->obj_field(_class_loader_offset);
1367 }
1368 
1369 oop java_lang_Class::module(oop java_class) {
1370   assert(_module_offset != 0, "must be set");
1371   return java_class->obj_field(_module_offset);
1372 }
1373 
1374 void java_lang_Class::set_module(oop java_class, oop module) {
1375   assert(_module_offset != 0, "must be set");
1376   java_class->obj_field_put(_module_offset, module);
1377 }
1378 
1379 oop java_lang_Class::name(Handle java_class, TRAPS) {
1380   assert(_name_offset != 0, "must be set");
1381   oop o = java_class->obj_field(_name_offset);
1382   if (o == NULL) {
1383     o = StringTable::intern(java_lang_Class::as_external_name(java_class()), THREAD);
1384     java_class->obj_field_put(_name_offset, o);
1385   }
1386   return o;
1387 }
1388 
1389 oop java_lang_Class::source_file(oop java_class) {
1390   assert(_source_file_offset != 0, "must be set");
1391   return java_class->obj_field(_source_file_offset);
1392 }
1393 
1394 void java_lang_Class::set_source_file(oop java_class, oop source_file) {
1395   assert(_source_file_offset != 0, "must be set");
1396   java_class->obj_field_put(_source_file_offset, source_file);
1397 }
1398 
1399 oop java_lang_Class::create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS) {
1400   // This should be improved by adding a field at the Java level or by
1401   // introducing a new VM klass (see comment in ClassFileParser)
1402   oop java_class = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(NULL, CHECK_0);
1403   if (type != T_VOID) {
1404     Klass* aklass = Universe::typeArrayKlassObj(type);
1405     assert(aklass != NULL, "correct bootstrap");
1406     release_set_array_klass(java_class, aklass);
1407   }
1408 #ifdef ASSERT
1409   InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(SystemDictionary::Class_klass());
1410   assert(java_lang_Class::static_oop_field_count(java_class) == 0, "should have been zeroed by allocation");
1411 #endif
1412   return java_class;
1413 }
1414 
1415 
1416 Klass* java_lang_Class::as_Klass(oop java_class) {
1417   //%note memory_2
1418   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1419   Klass* k = ((Klass*)java_class->metadata_field(_klass_offset));
1420   assert(k == NULL || k->is_klass(), "type check");
1421   return k;
1422 }
1423 
1424 Klass* java_lang_Class::as_Klass_raw(oop java_class) {
1425   //%note memory_2
1426   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1427   Klass* k = ((Klass*)java_class->metadata_field_raw(_klass_offset));
1428   assert(k == NULL || k->is_klass(), "type check");
1429   return k;
1430 }
1431 
1432 
1433 void java_lang_Class::set_klass(oop java_class, Klass* klass) {
1434   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1435   java_class->metadata_field_put(_klass_offset, klass);
1436 }
1437 
1438 
1439 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1440   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1441   Symbol* name = NULL;
1442   bool is_instance = false;
1443   if (is_primitive(java_class)) {
1444     name = vmSymbols::type_signature(primitive_type(java_class));
1445   } else {
1446     Klass* k = as_Klass(java_class);
1447     is_instance = k->is_instance_klass();
1448     name = k->name();
1449   }
1450   if (name == NULL) {
1451     st->print("<null>");
1452     return;
1453   }
1454   if (is_instance)  st->print("L");
1455   st->write((char*) name->base(), (int) name->utf8_length());
1456   if (is_instance)  st->print(";");
1457 }
1458 
1459 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found, TRAPS) {
1460   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1461   Symbol* name;
1462   if (is_primitive(java_class)) {
1463     name = vmSymbols::type_signature(primitive_type(java_class));
1464     // Because this can create a new symbol, the caller has to decrement
1465     // the refcount, so make adjustment here and below for symbols returned
1466     // that are not created or incremented due to a successful lookup.
1467     name->increment_refcount();
1468   } else {
1469     Klass* k = as_Klass(java_class);
1470     if (!k->is_instance_klass()) {
1471       name = k->name();
1472       name->increment_refcount();
1473     } else {
1474       ResourceMark rm;
1475       const char* sigstr = k->signature_name();
1476       int         siglen = (int) strlen(sigstr);
1477       if (!intern_if_not_found) {
1478         name = SymbolTable::probe(sigstr, siglen);
1479       } else {
1480         name = SymbolTable::new_symbol(sigstr, siglen, THREAD);
1481       }
1482     }
1483   }
1484   return name;
1485 }
1486 
1487 // Returns the Java name for this Java mirror (Resource allocated)
1488 // See Klass::external_name().
1489 // For primitive type Java mirrors, its type name is returned.
1490 const char* java_lang_Class::as_external_name(oop java_class) {
1491   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1492   const char* name = NULL;
1493   if (is_primitive(java_class)) {
1494     name = type2name(primitive_type(java_class));
1495   } else {
1496     name = as_Klass(java_class)->external_name();
1497   }
1498   if (name == NULL) {
1499     name = "<null>";
1500   }
1501   return name;
1502 }
1503 
1504 Klass* java_lang_Class::array_klass_acquire(oop java_class) {
1505   Klass* k = ((Klass*)java_class->metadata_field_acquire(_array_klass_offset));
1506   assert(k == NULL || k->is_klass() && k->is_array_klass(), "should be array klass");
1507   return k;
1508 }
1509 
1510 
1511 void java_lang_Class::release_set_array_klass(oop java_class, Klass* klass) {
1512   assert(klass->is_klass() && klass->is_array_klass(), "should be array klass");
1513   java_class->release_metadata_field_put(_array_klass_offset, klass);
1514 }
1515 
1516 
1517 BasicType java_lang_Class::primitive_type(oop java_class) {
1518   assert(java_lang_Class::is_primitive(java_class), "just checking");
1519   Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset));
1520   BasicType type = T_VOID;
1521   if (ak != NULL) {
1522     // Note: create_basic_type_mirror above initializes ak to a non-null value.
1523     type = ArrayKlass::cast(ak)->element_type();
1524   } else {
1525     assert(oopDesc::equals(java_class, Universe::void_mirror()), "only valid non-array primitive");
1526   }
1527   assert(oopDesc::equals(Universe::java_mirror(type), java_class), "must be consistent");
1528   return type;
1529 }
1530 
1531 BasicType java_lang_Class::as_BasicType(oop java_class, Klass** reference_klass) {
1532   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1533   if (is_primitive(java_class)) {
1534     if (reference_klass != NULL)
1535       (*reference_klass) = NULL;
1536     return primitive_type(java_class);
1537   } else {
1538     if (reference_klass != NULL)
1539       (*reference_klass) = as_Klass(java_class);
1540     return T_OBJECT;
1541   }
1542 }
1543 
1544 
1545 oop java_lang_Class::primitive_mirror(BasicType t) {
1546   oop mirror = Universe::java_mirror(t);
1547   assert(mirror != NULL && mirror->is_a(SystemDictionary::Class_klass()), "must be a Class");
1548   assert(java_lang_Class::is_primitive(mirror), "must be primitive");
1549   return mirror;
1550 }
1551 
1552 bool java_lang_Class::offsets_computed = false;
1553 int  java_lang_Class::classRedefinedCount_offset = -1;
1554 
1555 #define CLASS_FIELDS_DO(macro) \
1556   macro(classRedefinedCount_offset, k, "classRedefinedCount", int_signature,         false) ; \
1557   macro(_class_loader_offset,       k, "classLoader",         classloader_signature, false); \
1558   macro(_component_mirror_offset,   k, "componentType",       class_signature,       false); \
1559   macro(_module_offset,             k, "module",              module_signature,      false); \
1560   macro(_name_offset,               k, "name",                string_signature,      false); \
1561 
1562 void java_lang_Class::compute_offsets() {
1563   if (offsets_computed) {
1564     return;
1565   }
1566 
1567   offsets_computed = true;
1568 
1569   InstanceKlass* k = SystemDictionary::Class_klass();
1570   CLASS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1571 
1572   // Init lock is a C union with component_mirror.  Only instanceKlass mirrors have
1573   // init_lock and only ArrayKlass mirrors have component_mirror.  Since both are oops
1574   // GC treats them the same.
1575   _init_lock_offset = _component_mirror_offset;
1576 
1577   CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
1578 }
1579 
1580 #if INCLUDE_CDS
1581 void java_lang_Class::serialize_offsets(SerializeClosure* f) {
1582   f->do_u4((u4*)&offsets_computed);
1583   f->do_u4((u4*)&_init_lock_offset);
1584 
1585   CLASS_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1586 
1587   CLASS_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
1588 }
1589 #endif
1590 
1591 int java_lang_Class::classRedefinedCount(oop the_class_mirror) {
1592   if (classRedefinedCount_offset == -1) {
1593     // If we don't have an offset for it then just return -1 as a marker.
1594     return -1;
1595   }
1596 
1597   return the_class_mirror->int_field(classRedefinedCount_offset);
1598 }
1599 
1600 void java_lang_Class::set_classRedefinedCount(oop the_class_mirror, int value) {
1601   if (classRedefinedCount_offset == -1) {
1602     // If we don't have an offset for it then nothing to set.
1603     return;
1604   }
1605 
1606   the_class_mirror->int_field_put(classRedefinedCount_offset, value);
1607 }
1608 
1609 
1610 // Note: JDK1.1 and before had a privateInfo_offset field which was used for the
1611 //       platform thread structure, and a eetop offset which was used for thread
1612 //       local storage (and unused by the HotSpot VM). In JDK1.2 the two structures
1613 //       merged, so in the HotSpot VM we just use the eetop field for the thread
1614 //       instead of the privateInfo_offset.
1615 //
1616 // Note: The stackSize field is only present starting in 1.4.
1617 
1618 int java_lang_Thread::_name_offset = 0;
1619 int java_lang_Thread::_group_offset = 0;
1620 int java_lang_Thread::_contextClassLoader_offset = 0;
1621 int java_lang_Thread::_inheritedAccessControlContext_offset = 0;
1622 int java_lang_Thread::_priority_offset = 0;
1623 int java_lang_Thread::_eetop_offset = 0;
1624 int java_lang_Thread::_daemon_offset = 0;
1625 int java_lang_Thread::_stillborn_offset = 0;
1626 int java_lang_Thread::_stackSize_offset = 0;
1627 int java_lang_Thread::_tid_offset = 0;
1628 int java_lang_Thread::_thread_status_offset = 0;
1629 int java_lang_Thread::_park_blocker_offset = 0;
1630 int java_lang_Thread::_park_event_offset = 0 ;
1631 
1632 #define THREAD_FIELDS_DO(macro) \
1633   macro(_name_offset,          k, vmSymbols::name_name(), string_signature, false); \
1634   macro(_group_offset,         k, vmSymbols::group_name(), threadgroup_signature, false); \
1635   macro(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), classloader_signature, false); \
1636   macro(_inheritedAccessControlContext_offset, k, vmSymbols::inheritedAccessControlContext_name(), accesscontrolcontext_signature, false); \
1637   macro(_priority_offset,      k, vmSymbols::priority_name(), int_signature, false); \
1638   macro(_daemon_offset,        k, vmSymbols::daemon_name(), bool_signature, false); \
1639   macro(_eetop_offset,         k, "eetop", long_signature, false); \
1640   macro(_stillborn_offset,     k, "stillborn", bool_signature, false); \
1641   macro(_stackSize_offset,     k, "stackSize", long_signature, false); \
1642   macro(_tid_offset,           k, "tid", long_signature, false); \
1643   macro(_thread_status_offset, k, "threadStatus", int_signature, false); \
1644   macro(_park_blocker_offset,  k, "parkBlocker", object_signature, false); \
1645   macro(_park_event_offset,    k, "nativeParkEventPointer", long_signature, false)
1646 
1647 void java_lang_Thread::compute_offsets() {
1648   assert(_group_offset == 0, "offsets should be initialized only once");
1649 
1650   InstanceKlass* k = SystemDictionary::Thread_klass();
1651   THREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1652 }
1653 
1654 #if INCLUDE_CDS
1655 void java_lang_Thread::serialize_offsets(SerializeClosure* f) {
1656   THREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1657 }
1658 #endif
1659 
1660 JavaThread* java_lang_Thread::thread(oop java_thread) {
1661   return (JavaThread*)java_thread->address_field(_eetop_offset);
1662 }
1663 
1664 
1665 void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) {
1666   java_thread->address_field_put(_eetop_offset, (address)thread);
1667 }
1668 
1669 
1670 oop java_lang_Thread::name(oop java_thread) {
1671   return java_thread->obj_field(_name_offset);
1672 }
1673 
1674 
1675 void java_lang_Thread::set_name(oop java_thread, oop name) {
1676   java_thread->obj_field_put(_name_offset, name);
1677 }
1678 
1679 
1680 ThreadPriority java_lang_Thread::priority(oop java_thread) {
1681   return (ThreadPriority)java_thread->int_field(_priority_offset);
1682 }
1683 
1684 
1685 void java_lang_Thread::set_priority(oop java_thread, ThreadPriority priority) {
1686   java_thread->int_field_put(_priority_offset, priority);
1687 }
1688 
1689 
1690 oop java_lang_Thread::threadGroup(oop java_thread) {
1691   return java_thread->obj_field(_group_offset);
1692 }
1693 
1694 
1695 bool java_lang_Thread::is_stillborn(oop java_thread) {
1696   return java_thread->bool_field(_stillborn_offset) != 0;
1697 }
1698 
1699 
1700 // We never have reason to turn the stillborn bit off
1701 void java_lang_Thread::set_stillborn(oop java_thread) {
1702   java_thread->bool_field_put(_stillborn_offset, true);
1703 }
1704 
1705 
1706 bool java_lang_Thread::is_alive(oop java_thread) {
1707   JavaThread* thr = java_lang_Thread::thread(java_thread);
1708   return (thr != NULL);
1709 }
1710 
1711 
1712 bool java_lang_Thread::is_daemon(oop java_thread) {
1713   return java_thread->bool_field(_daemon_offset) != 0;
1714 }
1715 
1716 
1717 void java_lang_Thread::set_daemon(oop java_thread) {
1718   java_thread->bool_field_put(_daemon_offset, true);
1719 }
1720 
1721 oop java_lang_Thread::context_class_loader(oop java_thread) {
1722   return java_thread->obj_field(_contextClassLoader_offset);
1723 }
1724 
1725 oop java_lang_Thread::inherited_access_control_context(oop java_thread) {
1726   return java_thread->obj_field(_inheritedAccessControlContext_offset);
1727 }
1728 
1729 
1730 jlong java_lang_Thread::stackSize(oop java_thread) {
1731   if (_stackSize_offset > 0) {
1732     return java_thread->long_field(_stackSize_offset);
1733   } else {
1734     return 0;
1735   }
1736 }
1737 
1738 // Write the thread status value to threadStatus field in java.lang.Thread java class.
1739 void java_lang_Thread::set_thread_status(oop java_thread,
1740                                          java_lang_Thread::ThreadStatus status) {
1741   // The threadStatus is only present starting in 1.5
1742   if (_thread_status_offset > 0) {
1743     java_thread->int_field_put(_thread_status_offset, status);
1744   }
1745 }
1746 
1747 // Read thread status value from threadStatus field in java.lang.Thread java class.
1748 java_lang_Thread::ThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
1749   // Make sure the caller is operating on behalf of the VM or is
1750   // running VM code (state == _thread_in_vm).
1751   assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
1752          JavaThread::current()->thread_state() == _thread_in_vm,
1753          "Java Thread is not running in vm");
1754   // The threadStatus is only present starting in 1.5
1755   if (_thread_status_offset > 0) {
1756     return (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
1757   } else {
1758     // All we can easily figure out is if it is alive, but that is
1759     // enough info for a valid unknown status.
1760     // These aren't restricted to valid set ThreadStatus values, so
1761     // use JVMTI values and cast.
1762     JavaThread* thr = java_lang_Thread::thread(java_thread);
1763     if (thr == NULL) {
1764       // the thread hasn't run yet or is in the process of exiting
1765       return NEW;
1766     }
1767     return (java_lang_Thread::ThreadStatus)JVMTI_THREAD_STATE_ALIVE;
1768   }
1769 }
1770 
1771 
1772 jlong java_lang_Thread::thread_id(oop java_thread) {
1773   // The thread ID field is only present starting in 1.5
1774   if (_tid_offset > 0) {
1775     return java_thread->long_field(_tid_offset);
1776   } else {
1777     return 0;
1778   }
1779 }
1780 
1781 oop java_lang_Thread::park_blocker(oop java_thread) {
1782   assert(JDK_Version::current().supports_thread_park_blocker() &&
1783          _park_blocker_offset != 0, "Must support parkBlocker field");
1784 
1785   if (_park_blocker_offset > 0) {
1786     return java_thread->obj_field(_park_blocker_offset);
1787   }
1788 
1789   return NULL;
1790 }
1791 
1792 jlong java_lang_Thread::park_event(oop java_thread) {
1793   if (_park_event_offset > 0) {
1794     return java_thread->long_field(_park_event_offset);
1795   }
1796   return 0;
1797 }
1798 
1799 bool java_lang_Thread::set_park_event(oop java_thread, jlong ptr) {
1800   if (_park_event_offset > 0) {
1801     java_thread->long_field_put(_park_event_offset, ptr);
1802     return true;
1803   }
1804   return false;
1805 }
1806 
1807 
1808 const char* java_lang_Thread::thread_status_name(oop java_thread) {
1809   assert(_thread_status_offset != 0, "Must have thread status");
1810   ThreadStatus status = (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
1811   switch (status) {
1812     case NEW                      : return "NEW";
1813     case RUNNABLE                 : return "RUNNABLE";
1814     case SLEEPING                 : return "TIMED_WAITING (sleeping)";
1815     case IN_OBJECT_WAIT           : return "WAITING (on object monitor)";
1816     case IN_OBJECT_WAIT_TIMED     : return "TIMED_WAITING (on object monitor)";
1817     case PARKED                   : return "WAITING (parking)";
1818     case PARKED_TIMED             : return "TIMED_WAITING (parking)";
1819     case BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
1820     case TERMINATED               : return "TERMINATED";
1821     default                       : return "UNKNOWN";
1822   };
1823 }
1824 int java_lang_ThreadGroup::_parent_offset = 0;
1825 int java_lang_ThreadGroup::_name_offset = 0;
1826 int java_lang_ThreadGroup::_threads_offset = 0;
1827 int java_lang_ThreadGroup::_groups_offset = 0;
1828 int java_lang_ThreadGroup::_maxPriority_offset = 0;
1829 int java_lang_ThreadGroup::_destroyed_offset = 0;
1830 int java_lang_ThreadGroup::_daemon_offset = 0;
1831 int java_lang_ThreadGroup::_nthreads_offset = 0;
1832 int java_lang_ThreadGroup::_ngroups_offset = 0;
1833 
1834 oop  java_lang_ThreadGroup::parent(oop java_thread_group) {
1835   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1836   return java_thread_group->obj_field(_parent_offset);
1837 }
1838 
1839 // ("name as oop" accessor is not necessary)
1840 
1841 const char* java_lang_ThreadGroup::name(oop java_thread_group) {
1842   oop name = java_thread_group->obj_field(_name_offset);
1843   // ThreadGroup.name can be null
1844   if (name != NULL) {
1845     return java_lang_String::as_utf8_string(name);
1846   }
1847   return NULL;
1848 }
1849 
1850 int java_lang_ThreadGroup::nthreads(oop java_thread_group) {
1851   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1852   return java_thread_group->int_field(_nthreads_offset);
1853 }
1854 
1855 objArrayOop java_lang_ThreadGroup::threads(oop java_thread_group) {
1856   oop threads = java_thread_group->obj_field(_threads_offset);
1857   assert(threads != NULL, "threadgroups should have threads");
1858   assert(threads->is_objArray(), "just checking"); // Todo: Add better type checking code
1859   return objArrayOop(threads);
1860 }
1861 
1862 int java_lang_ThreadGroup::ngroups(oop java_thread_group) {
1863   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1864   return java_thread_group->int_field(_ngroups_offset);
1865 }
1866 
1867 objArrayOop java_lang_ThreadGroup::groups(oop java_thread_group) {
1868   oop groups = java_thread_group->obj_field(_groups_offset);
1869   assert(groups == NULL || groups->is_objArray(), "just checking"); // Todo: Add better type checking code
1870   return objArrayOop(groups);
1871 }
1872 
1873 ThreadPriority java_lang_ThreadGroup::maxPriority(oop java_thread_group) {
1874   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1875   return (ThreadPriority) java_thread_group->int_field(_maxPriority_offset);
1876 }
1877 
1878 bool java_lang_ThreadGroup::is_destroyed(oop java_thread_group) {
1879   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1880   return java_thread_group->bool_field(_destroyed_offset) != 0;
1881 }
1882 
1883 bool java_lang_ThreadGroup::is_daemon(oop java_thread_group) {
1884   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1885   return java_thread_group->bool_field(_daemon_offset) != 0;
1886 }
1887 
1888 #define THREADGROUP_FIELDS_DO(macro) \
1889   macro(_parent_offset,      k, vmSymbols::parent_name(),      threadgroup_signature,       false); \
1890   macro(_name_offset,        k, vmSymbols::name_name(),        string_signature,            false); \
1891   macro(_threads_offset,     k, vmSymbols::threads_name(),     thread_array_signature,      false); \
1892   macro(_groups_offset,      k, vmSymbols::groups_name(),      threadgroup_array_signature, false); \
1893   macro(_maxPriority_offset, k, vmSymbols::maxPriority_name(), int_signature,               false); \
1894   macro(_destroyed_offset,   k, vmSymbols::destroyed_name(),   bool_signature,              false); \
1895   macro(_daemon_offset,      k, vmSymbols::daemon_name(),      bool_signature,              false); \
1896   macro(_nthreads_offset,    k, vmSymbols::nthreads_name(),    int_signature,               false); \
1897   macro(_ngroups_offset,     k, vmSymbols::ngroups_name(),     int_signature,               false)
1898 
1899 void java_lang_ThreadGroup::compute_offsets() {
1900   assert(_parent_offset == 0, "offsets should be initialized only once");
1901 
1902   InstanceKlass* k = SystemDictionary::ThreadGroup_klass();
1903   THREADGROUP_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1904 }
1905 
1906 #if INCLUDE_CDS
1907 void java_lang_ThreadGroup::serialize_offsets(SerializeClosure* f) {
1908   THREADGROUP_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1909 }
1910 #endif
1911 
1912 #define THROWABLE_FIELDS_DO(macro) \
1913   macro(backtrace_offset,     k, "backtrace",     object_signature,                  false); \
1914   macro(detailMessage_offset, k, "detailMessage", string_signature,                  false); \
1915   macro(stackTrace_offset,    k, "stackTrace",    java_lang_StackTraceElement_array, false); \
1916   macro(depth_offset,         k, "depth",         int_signature,                     false); \
1917   macro(static_unassigned_stacktrace_offset, k, "UNASSIGNED_STACK", java_lang_StackTraceElement_array, true)
1918 
1919 void java_lang_Throwable::compute_offsets() {
1920   InstanceKlass* k = SystemDictionary::Throwable_klass();
1921   THROWABLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1922 }
1923 
1924 #if INCLUDE_CDS
1925 void java_lang_Throwable::serialize_offsets(SerializeClosure* f) {
1926   THROWABLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1927 }
1928 #endif
1929 
1930 oop java_lang_Throwable::unassigned_stacktrace() {
1931   InstanceKlass* ik = SystemDictionary::Throwable_klass();
1932   oop base = ik->static_field_base_raw();
1933   return base->obj_field(static_unassigned_stacktrace_offset);
1934 }
1935 
1936 oop java_lang_Throwable::backtrace(oop throwable) {
1937   return throwable->obj_field_acquire(backtrace_offset);
1938 }
1939 
1940 
1941 void java_lang_Throwable::set_backtrace(oop throwable, oop value) {
1942   throwable->release_obj_field_put(backtrace_offset, value);
1943 }
1944 
1945 int java_lang_Throwable::depth(oop throwable) {
1946   return throwable->int_field(depth_offset);
1947 }
1948 
1949 void java_lang_Throwable::set_depth(oop throwable, int value) {
1950   throwable->int_field_put(depth_offset, value);
1951 }
1952 
1953 oop java_lang_Throwable::message(oop throwable) {
1954   return throwable->obj_field(detailMessage_offset);
1955 }
1956 
1957 
1958 // Return Symbol for detailed_message or NULL
1959 Symbol* java_lang_Throwable::detail_message(oop throwable) {
1960   PRESERVE_EXCEPTION_MARK;  // Keep original exception
1961   oop detailed_message = java_lang_Throwable::message(throwable);
1962   if (detailed_message != NULL) {
1963     return java_lang_String::as_symbol(detailed_message, THREAD);
1964   }
1965   return NULL;
1966 }
1967 
1968 void java_lang_Throwable::set_message(oop throwable, oop value) {
1969   throwable->obj_field_put(detailMessage_offset, value);
1970 }
1971 
1972 
1973 void java_lang_Throwable::set_stacktrace(oop throwable, oop st_element_array) {
1974   throwable->obj_field_put(stackTrace_offset, st_element_array);
1975 }
1976 
1977 void java_lang_Throwable::clear_stacktrace(oop throwable) {
1978   set_stacktrace(throwable, NULL);
1979 }
1980 
1981 
1982 void java_lang_Throwable::print(oop throwable, outputStream* st) {
1983   ResourceMark rm;
1984   Klass* k = throwable->klass();
1985   assert(k != NULL, "just checking");
1986   st->print("%s", k->external_name());
1987   oop msg = message(throwable);
1988   if (msg != NULL) {
1989     st->print(": %s", java_lang_String::as_utf8_string(msg));
1990   }
1991 }
1992 
1993 // After this many redefines, the stack trace is unreliable.
1994 const int MAX_VERSION = USHRT_MAX;
1995 
1996 static inline bool version_matches(Method* method, int version) {
1997   assert(version < MAX_VERSION, "version is too big");
1998   return method != NULL && (method->constants()->version() == version);
1999 }
2000 
2001 
2002 // This class provides a simple wrapper over the internal structure of
2003 // exception backtrace to insulate users of the backtrace from needing
2004 // to know what it looks like.
2005 class BacktraceBuilder: public StackObj {
2006  friend class BacktraceIterator;
2007  private:
2008   Handle          _backtrace;
2009   objArrayOop     _head;
2010   typeArrayOop    _methods;
2011   typeArrayOop    _bcis;
2012   objArrayOop     _mirrors;
2013   typeArrayOop    _names; // needed to insulate method name against redefinition
2014   int             _index;
2015   NoSafepointVerifier _nsv;
2016 
2017   enum {
2018     trace_methods_offset = java_lang_Throwable::trace_methods_offset,
2019     trace_bcis_offset    = java_lang_Throwable::trace_bcis_offset,
2020     trace_mirrors_offset = java_lang_Throwable::trace_mirrors_offset,
2021     trace_names_offset   = java_lang_Throwable::trace_names_offset,
2022     trace_next_offset    = java_lang_Throwable::trace_next_offset,
2023     trace_size           = java_lang_Throwable::trace_size,
2024     trace_chunk_size     = java_lang_Throwable::trace_chunk_size
2025   };
2026 
2027   // get info out of chunks
2028   static typeArrayOop get_methods(objArrayHandle chunk) {
2029     typeArrayOop methods = typeArrayOop(chunk->obj_at(trace_methods_offset));
2030     assert(methods != NULL, "method array should be initialized in backtrace");
2031     return methods;
2032   }
2033   static typeArrayOop get_bcis(objArrayHandle chunk) {
2034     typeArrayOop bcis = typeArrayOop(chunk->obj_at(trace_bcis_offset));
2035     assert(bcis != NULL, "bci array should be initialized in backtrace");
2036     return bcis;
2037   }
2038   static objArrayOop get_mirrors(objArrayHandle chunk) {
2039     objArrayOop mirrors = objArrayOop(chunk->obj_at(trace_mirrors_offset));
2040     assert(mirrors != NULL, "mirror array should be initialized in backtrace");
2041     return mirrors;
2042   }
2043   static typeArrayOop get_names(objArrayHandle chunk) {
2044     typeArrayOop names = typeArrayOop(chunk->obj_at(trace_names_offset));
2045     assert(names != NULL, "names array should be initialized in backtrace");
2046     return names;
2047   }
2048 
2049  public:
2050 
2051   // constructor for new backtrace
2052   BacktraceBuilder(TRAPS): _head(NULL), _methods(NULL), _bcis(NULL), _mirrors(NULL), _names(NULL) {
2053     expand(CHECK);
2054     _backtrace = Handle(THREAD, _head);
2055     _index = 0;
2056   }
2057 
2058   BacktraceBuilder(Thread* thread, objArrayHandle backtrace) {
2059     _methods = get_methods(backtrace);
2060     _bcis = get_bcis(backtrace);
2061     _mirrors = get_mirrors(backtrace);
2062     _names = get_names(backtrace);
2063     assert(_methods->length() == _bcis->length() &&
2064            _methods->length() == _mirrors->length() &&
2065            _mirrors->length() == _names->length(),
2066            "method and source information arrays should match");
2067 
2068     // head is the preallocated backtrace
2069     _head = backtrace();
2070     _backtrace = Handle(thread, _head);
2071     _index = 0;
2072   }
2073 
2074   void expand(TRAPS) {
2075     objArrayHandle old_head(THREAD, _head);
2076     PauseNoSafepointVerifier pnsv(&_nsv);
2077 
2078     objArrayOop head = oopFactory::new_objectArray(trace_size, CHECK);
2079     objArrayHandle new_head(THREAD, head);
2080 
2081     typeArrayOop methods = oopFactory::new_shortArray(trace_chunk_size, CHECK);
2082     typeArrayHandle new_methods(THREAD, methods);
2083 
2084     typeArrayOop bcis = oopFactory::new_intArray(trace_chunk_size, CHECK);
2085     typeArrayHandle new_bcis(THREAD, bcis);
2086 
2087     objArrayOop mirrors = oopFactory::new_objectArray(trace_chunk_size, CHECK);
2088     objArrayHandle new_mirrors(THREAD, mirrors);
2089 
2090     typeArrayOop names = oopFactory::new_symbolArray(trace_chunk_size, CHECK);
2091     typeArrayHandle new_names(THREAD, names);
2092 
2093     if (!old_head.is_null()) {
2094       old_head->obj_at_put(trace_next_offset, new_head());
2095     }
2096     new_head->obj_at_put(trace_methods_offset, new_methods());
2097     new_head->obj_at_put(trace_bcis_offset, new_bcis());
2098     new_head->obj_at_put(trace_mirrors_offset, new_mirrors());
2099     new_head->obj_at_put(trace_names_offset, new_names());
2100 
2101     _head    = new_head();
2102     _methods = new_methods();
2103     _bcis = new_bcis();
2104     _mirrors = new_mirrors();
2105     _names  = new_names();
2106     _index = 0;
2107   }
2108 
2109   oop backtrace() {
2110     return _backtrace();
2111   }
2112 
2113   inline void push(Method* method, int bci, TRAPS) {
2114     // Smear the -1 bci to 0 since the array only holds unsigned
2115     // shorts.  The later line number lookup would just smear the -1
2116     // to a 0 even if it could be recorded.
2117     if (bci == SynchronizationEntryBCI) bci = 0;
2118 
2119     if (_index >= trace_chunk_size) {
2120       methodHandle mhandle(THREAD, method);
2121       expand(CHECK);
2122       method = mhandle();
2123     }
2124 
2125     _methods->ushort_at_put(_index, method->orig_method_idnum());
2126     _bcis->int_at_put(_index, Backtrace::merge_bci_and_version(bci, method->constants()->version()));
2127 
2128     // Note:this doesn't leak symbols because the mirror in the backtrace keeps the
2129     // klass owning the symbols alive so their refcounts aren't decremented.
2130     Symbol* name = method->name();
2131     _names->symbol_at_put(_index, name);
2132 
2133     // We need to save the mirrors in the backtrace to keep the class
2134     // from being unloaded while we still have this stack trace.
2135     assert(method->method_holder()->java_mirror() != NULL, "never push null for mirror");
2136     _mirrors->obj_at_put(_index, method->method_holder()->java_mirror());
2137     _index++;
2138   }
2139 
2140 };
2141 
2142 struct BacktraceElement : public StackObj {
2143   int _method_id;
2144   int _bci;
2145   int _version;
2146   Symbol* _name;
2147   Handle _mirror;
2148   BacktraceElement(Handle mirror, int mid, int version, int bci, Symbol* name) :
2149                    _method_id(mid), _bci(bci), _version(version), _name(name), _mirror(mirror) {}
2150 };
2151 
2152 class BacktraceIterator : public StackObj {
2153   int _index;
2154   objArrayHandle  _result;
2155   objArrayHandle  _mirrors;
2156   typeArrayHandle _methods;
2157   typeArrayHandle _bcis;
2158   typeArrayHandle _names;
2159 
2160   void init(objArrayHandle result, Thread* thread) {
2161     // Get method id, bci, version and mirror from chunk
2162     _result = result;
2163     if (_result.not_null()) {
2164       _methods = typeArrayHandle(thread, BacktraceBuilder::get_methods(_result));
2165       _bcis = typeArrayHandle(thread, BacktraceBuilder::get_bcis(_result));
2166       _mirrors = objArrayHandle(thread, BacktraceBuilder::get_mirrors(_result));
2167       _names = typeArrayHandle(thread, BacktraceBuilder::get_names(_result));
2168       _index = 0;
2169     }
2170   }
2171  public:
2172   BacktraceIterator(objArrayHandle result, Thread* thread) {
2173     init(result, thread);
2174     assert(_methods.is_null() || _methods->length() == java_lang_Throwable::trace_chunk_size, "lengths don't match");
2175   }
2176 
2177   BacktraceElement next(Thread* thread) {
2178     BacktraceElement e (Handle(thread, _mirrors->obj_at(_index)),
2179                         _methods->ushort_at(_index),
2180                         Backtrace::version_at(_bcis->int_at(_index)),
2181                         Backtrace::bci_at(_bcis->int_at(_index)),
2182                         _names->symbol_at(_index));
2183     _index++;
2184 
2185     if (_index >= java_lang_Throwable::trace_chunk_size) {
2186       int next_offset = java_lang_Throwable::trace_next_offset;
2187       // Get next chunk
2188       objArrayHandle result (thread, objArrayOop(_result->obj_at(next_offset)));
2189       init(result, thread);
2190     }
2191     return e;
2192   }
2193 
2194   bool repeat() {
2195     return _result.not_null() && _mirrors->obj_at(_index) != NULL;
2196   }
2197 };
2198 
2199 
2200 // Print stack trace element to resource allocated buffer
2201 static void print_stack_element_to_stream(outputStream* st, Handle mirror, int method_id,
2202                                           int version, int bci, Symbol* name) {
2203   ResourceMark rm;
2204 
2205   // Get strings and string lengths
2206   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror()));
2207   const char* klass_name  = holder->external_name();
2208   int buf_len = (int)strlen(klass_name);
2209 
2210   char* method_name = name->as_C_string();
2211   buf_len += (int)strlen(method_name);
2212 
2213   char* source_file_name = NULL;
2214   Symbol* source = Backtrace::get_source_file_name(holder, version);
2215   if (source != NULL) {
2216     source_file_name = source->as_C_string();
2217     buf_len += (int)strlen(source_file_name);
2218   }
2219 
2220   char *module_name = NULL, *module_version = NULL;
2221   ModuleEntry* module = holder->module();
2222   if (module->is_named()) {
2223     module_name = module->name()->as_C_string();
2224     buf_len += (int)strlen(module_name);
2225     if (module->version() != NULL) {
2226       module_version = module->version()->as_C_string();
2227       buf_len += (int)strlen(module_version);
2228     }
2229   }
2230 
2231   // Allocate temporary buffer with extra space for formatting and line number
2232   char* buf = NEW_RESOURCE_ARRAY(char, buf_len + 64);
2233 
2234   // Print stack trace line in buffer
2235   sprintf(buf, "\tat %s.%s(", klass_name, method_name);
2236 
2237   // Print module information
2238   if (module_name != NULL) {
2239     if (module_version != NULL) {
2240       sprintf(buf + (int)strlen(buf), "%s@%s/", module_name, module_version);
2241     } else {
2242       sprintf(buf + (int)strlen(buf), "%s/", module_name);
2243     }
2244   }
2245 
2246   // The method can be NULL if the requested class version is gone
2247   Method* method = holder->method_with_orig_idnum(method_id, version);
2248   if (!version_matches(method, version)) {
2249     strcat(buf, "Redefined)");
2250   } else {
2251     int line_number = Backtrace::get_line_number(method, bci);
2252     if (line_number == -2) {
2253       strcat(buf, "Native Method)");
2254     } else {
2255       if (source_file_name != NULL && (line_number != -1)) {
2256         // Sourcename and linenumber
2257         sprintf(buf + (int)strlen(buf), "%s:%d)", source_file_name, line_number);
2258       } else if (source_file_name != NULL) {
2259         // Just sourcename
2260         sprintf(buf + (int)strlen(buf), "%s)", source_file_name);
2261       } else {
2262         // Neither sourcename nor linenumber
2263         sprintf(buf + (int)strlen(buf), "Unknown Source)");
2264       }
2265       CompiledMethod* nm = method->code();
2266       if (WizardMode && nm != NULL) {
2267         sprintf(buf + (int)strlen(buf), "(nmethod " INTPTR_FORMAT ")", (intptr_t)nm);
2268       }
2269     }
2270   }
2271 
2272   st->print_cr("%s", buf);
2273 }
2274 
2275 void java_lang_Throwable::print_stack_element(outputStream *st, const methodHandle& method, int bci) {
2276   Handle mirror (Thread::current(),  method->method_holder()->java_mirror());
2277   int method_id = method->orig_method_idnum();
2278   int version = method->constants()->version();
2279   print_stack_element_to_stream(st, mirror, method_id, version, bci, method->name());
2280 }
2281 
2282 /**
2283  * Print the throwable message and its stack trace plus all causes by walking the
2284  * cause chain.  The output looks the same as of Throwable.printStackTrace().
2285  */
2286 void java_lang_Throwable::print_stack_trace(Handle throwable, outputStream* st) {
2287   // First, print the message.
2288   print(throwable(), st);
2289   st->cr();
2290 
2291   // Now print the stack trace.
2292   Thread* THREAD = Thread::current();
2293   while (throwable.not_null()) {
2294     objArrayHandle result (THREAD, objArrayOop(backtrace(throwable())));
2295     if (result.is_null()) {
2296       st->print_raw_cr("\t<<no stack trace available>>");
2297       return;
2298     }
2299     BacktraceIterator iter(result, THREAD);
2300 
2301     while (iter.repeat()) {
2302       BacktraceElement bte = iter.next(THREAD);
2303       print_stack_element_to_stream(st, bte._mirror, bte._method_id, bte._version, bte._bci, bte._name);
2304     }
2305     {
2306       // Call getCause() which doesn't necessarily return the _cause field.
2307       EXCEPTION_MARK;
2308       JavaValue cause(T_OBJECT);
2309       JavaCalls::call_virtual(&cause,
2310                               throwable,
2311                               throwable->klass(),
2312                               vmSymbols::getCause_name(),
2313                               vmSymbols::void_throwable_signature(),
2314                               THREAD);
2315       // Ignore any exceptions. we are in the middle of exception handling. Same as classic VM.
2316       if (HAS_PENDING_EXCEPTION) {
2317         CLEAR_PENDING_EXCEPTION;
2318         throwable = Handle();
2319       } else {
2320         throwable = Handle(THREAD, (oop) cause.get_jobject());
2321         if (throwable.not_null()) {
2322           st->print("Caused by: ");
2323           print(throwable(), st);
2324           st->cr();
2325         }
2326       }
2327     }
2328   }
2329 }
2330 
2331 /**
2332  * Print the throwable stack trace by calling the Java method java.lang.Throwable.printStackTrace().
2333  */
2334 void java_lang_Throwable::java_printStackTrace(Handle throwable, TRAPS) {
2335   assert(throwable->is_a(SystemDictionary::Throwable_klass()), "Throwable instance expected");
2336   JavaValue result(T_VOID);
2337   JavaCalls::call_virtual(&result,
2338                           throwable,
2339                           SystemDictionary::Throwable_klass(),
2340                           vmSymbols::printStackTrace_name(),
2341                           vmSymbols::void_method_signature(),
2342                           THREAD);
2343 }
2344 
2345 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method, TRAPS) {
2346   if (!StackTraceInThrowable) return;
2347   ResourceMark rm(THREAD);
2348 
2349   // Start out by clearing the backtrace for this object, in case the VM
2350   // runs out of memory while allocating the stack trace
2351   set_backtrace(throwable(), NULL);
2352   // Clear lazily constructed Java level stacktrace if refilling occurs
2353   // This is unnecessary in 1.7+ but harmless
2354   clear_stacktrace(throwable());
2355 
2356   int max_depth = MaxJavaStackTraceDepth;
2357   JavaThread* thread = (JavaThread*)THREAD;
2358 
2359   BacktraceBuilder bt(CHECK);
2360 
2361   // If there is no Java frame just return the method that was being called
2362   // with bci 0
2363   if (!thread->has_last_Java_frame()) {
2364     if (max_depth >= 1 && method() != NULL) {
2365       bt.push(method(), 0, CHECK);
2366       log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), 1);
2367       set_depth(throwable(), 1);
2368       set_backtrace(throwable(), bt.backtrace());
2369     }
2370     return;
2371   }
2372 
2373   // Instead of using vframe directly, this version of fill_in_stack_trace
2374   // basically handles everything by hand. This significantly improved the
2375   // speed of this method call up to 28.5% on Solaris sparc. 27.1% on Windows.
2376   // See bug 6333838 for  more details.
2377   // The "ASSERT" here is to verify this method generates the exactly same stack
2378   // trace as utilizing vframe.
2379 #ifdef ASSERT
2380   vframeStream st(thread);
2381   methodHandle st_method(THREAD, st.method());
2382 #endif
2383   int total_count = 0;
2384   RegisterMap map(thread, false);
2385   int decode_offset = 0;
2386   CompiledMethod* nm = NULL;
2387   bool skip_fillInStackTrace_check = false;
2388   bool skip_throwableInit_check = false;
2389   bool skip_hidden = !ShowHiddenFrames;
2390 
2391   for (frame fr = thread->last_frame(); max_depth == 0 || max_depth != total_count;) {
2392     Method* method = NULL;
2393     int bci = 0;
2394 
2395     // Compiled java method case.
2396     if (decode_offset != 0) {
2397       DebugInfoReadStream stream(nm, decode_offset);
2398       decode_offset = stream.read_int();
2399       method = (Method*)nm->metadata_at(stream.read_int());
2400       bci = stream.read_bci();
2401     } else {
2402       if (fr.is_first_frame()) break;
2403       address pc = fr.pc();
2404       if (fr.is_interpreted_frame()) {
2405         address bcp = fr.interpreter_frame_bcp();
2406         method = fr.interpreter_frame_method();
2407         bci =  method->bci_from(bcp);
2408         fr = fr.sender(&map);
2409       } else {
2410         CodeBlob* cb = fr.cb();
2411         // HMMM QQQ might be nice to have frame return nm as NULL if cb is non-NULL
2412         // but non nmethod
2413         fr = fr.sender(&map);
2414         if (cb == NULL || !cb->is_compiled()) {
2415           continue;
2416         }
2417         nm = cb->as_compiled_method();
2418         if (nm->method()->is_native()) {
2419           method = nm->method();
2420           bci = 0;
2421         } else {
2422           PcDesc* pd = nm->pc_desc_at(pc);
2423           decode_offset = pd->scope_decode_offset();
2424           // if decode_offset is not equal to 0, it will execute the
2425           // "compiled java method case" at the beginning of the loop.
2426           continue;
2427         }
2428       }
2429     }
2430 #ifdef ASSERT
2431     assert(st_method() == method && st.bci() == bci,
2432            "Wrong stack trace");
2433     st.next();
2434     // vframeStream::method isn't GC-safe so store off a copy
2435     // of the Method* in case we GC.
2436     if (!st.at_end()) {
2437       st_method = st.method();
2438     }
2439 #endif
2440 
2441     // the format of the stacktrace will be:
2442     // - 1 or more fillInStackTrace frames for the exception class (skipped)
2443     // - 0 or more <init> methods for the exception class (skipped)
2444     // - rest of the stack
2445 
2446     if (!skip_fillInStackTrace_check) {
2447       if (method->name() == vmSymbols::fillInStackTrace_name() &&
2448           throwable->is_a(method->method_holder())) {
2449         continue;
2450       }
2451       else {
2452         skip_fillInStackTrace_check = true; // gone past them all
2453       }
2454     }
2455     if (!skip_throwableInit_check) {
2456       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2457 
2458       // skip <init> methods of the exception class and superclasses
2459       // This is simlar to classic VM.
2460       if (method->name() == vmSymbols::object_initializer_name() &&
2461           throwable->is_a(method->method_holder())) {
2462         continue;
2463       } else {
2464         // there are none or we've seen them all - either way stop checking
2465         skip_throwableInit_check = true;
2466       }
2467     }
2468     if (method->is_hidden()) {
2469       if (skip_hidden)  continue;
2470     }
2471     bt.push(method, bci, CHECK);
2472     total_count++;
2473   }
2474 
2475   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), total_count);
2476 
2477   // Put completed stack trace into throwable object
2478   set_backtrace(throwable(), bt.backtrace());
2479   set_depth(throwable(), total_count);
2480 }
2481 
2482 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method) {
2483   // No-op if stack trace is disabled
2484   if (!StackTraceInThrowable) {
2485     return;
2486   }
2487 
2488   // Disable stack traces for some preallocated out of memory errors
2489   if (!Universe::should_fill_in_stack_trace(throwable)) {
2490     return;
2491   }
2492 
2493   PRESERVE_EXCEPTION_MARK;
2494 
2495   JavaThread* thread = JavaThread::active();
2496   fill_in_stack_trace(throwable, method, thread);
2497   // ignore exceptions thrown during stack trace filling
2498   CLEAR_PENDING_EXCEPTION;
2499 }
2500 
2501 void java_lang_Throwable::allocate_backtrace(Handle throwable, TRAPS) {
2502   // Allocate stack trace - backtrace is created but not filled in
2503 
2504   // No-op if stack trace is disabled
2505   if (!StackTraceInThrowable) return;
2506   BacktraceBuilder bt(CHECK);   // creates a backtrace
2507   set_backtrace(throwable(), bt.backtrace());
2508 }
2509 
2510 
2511 void java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(Handle throwable) {
2512   // Fill in stack trace into preallocated backtrace (no GC)
2513 
2514   // No-op if stack trace is disabled
2515   if (!StackTraceInThrowable) return;
2516 
2517   assert(throwable->is_a(SystemDictionary::Throwable_klass()), "sanity check");
2518 
2519   JavaThread* THREAD = JavaThread::current();
2520 
2521   objArrayHandle backtrace (THREAD, (objArrayOop)java_lang_Throwable::backtrace(throwable()));
2522   assert(backtrace.not_null(), "backtrace should have been preallocated");
2523 
2524   ResourceMark rm(THREAD);
2525   vframeStream st(THREAD);
2526 
2527   BacktraceBuilder bt(THREAD, backtrace);
2528 
2529   // Unlike fill_in_stack_trace we do not skip fillInStackTrace or throwable init
2530   // methods as preallocated errors aren't created by "java" code.
2531 
2532   // fill in as much stack trace as possible
2533   int chunk_count = 0;
2534   for (;!st.at_end(); st.next()) {
2535     bt.push(st.method(), st.bci(), CHECK);
2536     chunk_count++;
2537 
2538     // Bail-out for deep stacks
2539     if (chunk_count >= trace_chunk_size) break;
2540   }
2541   set_depth(throwable(), chunk_count);
2542   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), chunk_count);
2543 
2544   // We support the Throwable immutability protocol defined for Java 7.
2545   java_lang_Throwable::set_stacktrace(throwable(), java_lang_Throwable::unassigned_stacktrace());
2546   assert(java_lang_Throwable::unassigned_stacktrace() != NULL, "not initialized");
2547 }
2548 
2549 void java_lang_Throwable::get_stack_trace_elements(Handle throwable,
2550                                                    objArrayHandle stack_trace_array_h, TRAPS) {
2551 
2552   if (throwable.is_null() || stack_trace_array_h.is_null()) {
2553     THROW(vmSymbols::java_lang_NullPointerException());
2554   }
2555 
2556   assert(stack_trace_array_h->is_objArray(), "Stack trace array should be an array of StackTraceElenent");
2557 
2558   if (stack_trace_array_h->length() != depth(throwable())) {
2559     THROW(vmSymbols::java_lang_IndexOutOfBoundsException());
2560   }
2561 
2562   objArrayHandle result(THREAD, objArrayOop(backtrace(throwable())));
2563   BacktraceIterator iter(result, THREAD);
2564 
2565   int index = 0;
2566   while (iter.repeat()) {
2567     BacktraceElement bte = iter.next(THREAD);
2568 
2569     Handle stack_trace_element(THREAD, stack_trace_array_h->obj_at(index++));
2570 
2571     if (stack_trace_element.is_null()) {
2572       THROW(vmSymbols::java_lang_NullPointerException());
2573     }
2574 
2575     InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror()));
2576     methodHandle method (THREAD, holder->method_with_orig_idnum(bte._method_id, bte._version));
2577 
2578     java_lang_StackTraceElement::fill_in(stack_trace_element, holder,
2579                                          method,
2580                                          bte._version,
2581                                          bte._bci,
2582                                          bte._name, CHECK);
2583   }
2584 }
2585 
2586 oop java_lang_StackTraceElement::create(const methodHandle& method, int bci, TRAPS) {
2587   // Allocate java.lang.StackTraceElement instance
2588   InstanceKlass* k = SystemDictionary::StackTraceElement_klass();
2589   assert(k != NULL, "must be loaded in 1.4+");
2590   if (k->should_be_initialized()) {
2591     k->initialize(CHECK_0);
2592   }
2593 
2594   Handle element = k->allocate_instance_handle(CHECK_0);
2595 
2596   int version = method->constants()->version();
2597   fill_in(element, method->method_holder(), method, version, bci, method->name(), CHECK_0);
2598   return element();
2599 }
2600 
2601 void java_lang_StackTraceElement::fill_in(Handle element,
2602                                           InstanceKlass* holder, const methodHandle& method,
2603                                           int version, int bci, Symbol* name, TRAPS) {
2604   assert(element->is_a(SystemDictionary::StackTraceElement_klass()), "sanity check");
2605 
2606   ResourceMark rm(THREAD);
2607   HandleMark hm(THREAD);
2608 
2609   // Fill in class name
2610   Handle java_class(THREAD, holder->java_mirror());
2611   oop classname = java_lang_Class::name(java_class, CHECK);
2612   java_lang_StackTraceElement::set_declaringClass(element(), classname);
2613   java_lang_StackTraceElement::set_declaringClassObject(element(), java_class());
2614 
2615   oop loader = holder->class_loader();
2616   if (loader != NULL) {
2617     oop loader_name = java_lang_ClassLoader::name(loader);
2618     if (loader_name != NULL)
2619       java_lang_StackTraceElement::set_classLoaderName(element(), loader_name);
2620   }
2621 
2622   // Fill in method name
2623   oop methodname = StringTable::intern(name, CHECK);
2624   java_lang_StackTraceElement::set_methodName(element(), methodname);
2625 
2626   // Fill in module name and version
2627   ModuleEntry* module = holder->module();
2628   if (module->is_named()) {
2629     oop module_name = StringTable::intern(module->name(), CHECK);
2630     java_lang_StackTraceElement::set_moduleName(element(), module_name);
2631     oop module_version;
2632     if (module->version() != NULL) {
2633       module_version = StringTable::intern(module->version(), CHECK);
2634     } else {
2635       module_version = NULL;
2636     }
2637     java_lang_StackTraceElement::set_moduleVersion(element(), module_version);
2638   }
2639 
2640   if (method() == NULL || !version_matches(method(), version)) {
2641     // The method was redefined, accurate line number information isn't available
2642     java_lang_StackTraceElement::set_fileName(element(), NULL);
2643     java_lang_StackTraceElement::set_lineNumber(element(), -1);
2644   } else {
2645     // Fill in source file name and line number.
2646     Symbol* source = Backtrace::get_source_file_name(holder, version);
2647     oop source_file = java_lang_Class::source_file(java_class());
2648     if (source != NULL) {
2649       // Class was not redefined. We can trust its cache if set,
2650       // else we have to initialize it.
2651       if (source_file == NULL) {
2652         source_file = StringTable::intern(source, CHECK);
2653         java_lang_Class::set_source_file(java_class(), source_file);
2654       }
2655     } else {
2656       // Class was redefined. Dump the cache if it was set.
2657       if (source_file != NULL) {
2658         source_file = NULL;
2659         java_lang_Class::set_source_file(java_class(), source_file);
2660       }
2661       if (ShowHiddenFrames) {
2662         source = vmSymbols::unknown_class_name();
2663         source_file = StringTable::intern(source, CHECK);
2664       }
2665     }
2666     java_lang_StackTraceElement::set_fileName(element(), source_file);
2667 
2668     int line_number = Backtrace::get_line_number(method, bci);
2669     java_lang_StackTraceElement::set_lineNumber(element(), line_number);
2670   }
2671 }
2672 
2673 Method* java_lang_StackFrameInfo::get_method(Handle stackFrame, InstanceKlass* holder, TRAPS) {
2674   HandleMark hm(THREAD);
2675   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
2676   Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mname());
2677   // we should expand MemberName::name when Throwable uses StackTrace
2678   // MethodHandles::expand_MemberName(mname, MethodHandles::_suppress_defc|MethodHandles::_suppress_type, CHECK_NULL);
2679   return method;
2680 }
2681 
2682 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci, TRAPS) {
2683   // set Method* or mid/cpref
2684   HandleMark hm(THREAD);
2685   Handle mname(Thread::current(), stackFrame->obj_field(_memberName_offset));
2686   InstanceKlass* ik = method->method_holder();
2687   CallInfo info(method(), ik, CHECK);
2688   MethodHandles::init_method_MemberName(mname, info);
2689   // set bci
2690   java_lang_StackFrameInfo::set_bci(stackFrame(), bci);
2691   // method may be redefined; store the version
2692   int version = method->constants()->version();
2693   assert((jushort)version == version, "version should be short");
2694   java_lang_StackFrameInfo::set_version(stackFrame(), (short)version);
2695 }
2696 
2697 void java_lang_StackFrameInfo::to_stack_trace_element(Handle stackFrame, Handle stack_trace_element, TRAPS) {
2698   ResourceMark rm(THREAD);
2699   HandleMark hm(THREAD);
2700   Handle mname(THREAD, stackFrame->obj_field(java_lang_StackFrameInfo::_memberName_offset));
2701   Klass* clazz = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname()));
2702   InstanceKlass* holder = InstanceKlass::cast(clazz);
2703   Method* method = java_lang_StackFrameInfo::get_method(stackFrame, holder, CHECK);
2704 
2705   short version = stackFrame->short_field(_version_offset);
2706   short bci = stackFrame->short_field(_bci_offset);
2707   Symbol* name = method->name();
2708   java_lang_StackTraceElement::fill_in(stack_trace_element, holder, method, version, bci, name, CHECK);
2709 }
2710 
2711 #define STACKFRAMEINFO_FIELDS_DO(macro) \
2712   macro(_memberName_offset,     k, "memberName",  object_signature, false); \
2713   macro(_bci_offset,            k, "bci",         short_signature,  false)
2714 
2715 void java_lang_StackFrameInfo::compute_offsets() {
2716   InstanceKlass* k = SystemDictionary::StackFrameInfo_klass();
2717   STACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2718   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2719 }
2720 
2721 #if INCLUDE_CDS
2722 void java_lang_StackFrameInfo::serialize_offsets(SerializeClosure* f) {
2723   STACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2724   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
2725 }
2726 #endif
2727 
2728 #define LIVESTACKFRAMEINFO_FIELDS_DO(macro) \
2729   macro(_monitors_offset,   k, "monitors",    object_array_signature, false); \
2730   macro(_locals_offset,     k, "locals",      object_array_signature, false); \
2731   macro(_operands_offset,   k, "operands",    object_array_signature, false); \
2732   macro(_mode_offset,       k, "mode",        int_signature,          false)
2733 
2734 void java_lang_LiveStackFrameInfo::compute_offsets() {
2735   InstanceKlass* k = SystemDictionary::LiveStackFrameInfo_klass();
2736   LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2737 }
2738 
2739 #if INCLUDE_CDS
2740 void java_lang_LiveStackFrameInfo::serialize_offsets(SerializeClosure* f) {
2741   LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2742 }
2743 #endif
2744 
2745 #define ACCESSIBLEOBJECT_FIELDS_DO(macro) \
2746   macro(override_offset, k, "override", bool_signature, false)
2747 
2748 void java_lang_reflect_AccessibleObject::compute_offsets() {
2749   InstanceKlass* k = SystemDictionary::reflect_AccessibleObject_klass();
2750   ACCESSIBLEOBJECT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2751 }
2752 
2753 #if INCLUDE_CDS
2754 void java_lang_reflect_AccessibleObject::serialize_offsets(SerializeClosure* f) {
2755   ACCESSIBLEOBJECT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2756 }
2757 #endif
2758 
2759 jboolean java_lang_reflect_AccessibleObject::override(oop reflect) {
2760   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2761   return (jboolean) reflect->bool_field(override_offset);
2762 }
2763 
2764 void java_lang_reflect_AccessibleObject::set_override(oop reflect, jboolean value) {
2765   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2766   reflect->bool_field_put(override_offset, (int) value);
2767 }
2768 
2769 #define METHOD_FIELDS_DO(macro) \
2770   macro(clazz_offset,          k, vmSymbols::clazz_name(),          class_signature,       false); \
2771   macro(name_offset,           k, vmSymbols::name_name(),           string_signature,      false); \
2772   macro(returnType_offset,     k, vmSymbols::returnType_name(),     class_signature,       false); \
2773   macro(parameterTypes_offset, k, vmSymbols::parameterTypes_name(), class_array_signature, false); \
2774   macro(exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), class_array_signature, false); \
2775   macro(slot_offset,           k, vmSymbols::slot_name(),           int_signature,         false); \
2776   macro(modifiers_offset,      k, vmSymbols::modifiers_name(),      int_signature,         false); \
2777   macro(signature_offset,             k, vmSymbols::signature_name(),             string_signature,     false); \
2778   macro(annotations_offset,           k, vmSymbols::annotations_name(),           byte_array_signature, false); \
2779   macro(parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), byte_array_signature, false); \
2780   macro(annotation_default_offset,    k, vmSymbols::annotation_default_name(),    byte_array_signature, false);
2781 
2782 void java_lang_reflect_Method::compute_offsets() {
2783   InstanceKlass* k = SystemDictionary::reflect_Method_klass();
2784   METHOD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2785 }
2786 
2787 #if INCLUDE_CDS
2788 void java_lang_reflect_Method::serialize_offsets(SerializeClosure* f) {
2789   METHOD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2790 }
2791 #endif
2792 
2793 Handle java_lang_reflect_Method::create(TRAPS) {
2794   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2795   Klass* klass = SystemDictionary::reflect_Method_klass();
2796   // This class is eagerly initialized during VM initialization, since we keep a refence
2797   // to one of the methods
2798   assert(InstanceKlass::cast(klass)->is_initialized(), "must be initialized");
2799   return InstanceKlass::cast(klass)->allocate_instance_handle(THREAD);
2800 }
2801 
2802 oop java_lang_reflect_Method::clazz(oop reflect) {
2803   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2804   return reflect->obj_field(clazz_offset);
2805 }
2806 
2807 void java_lang_reflect_Method::set_clazz(oop reflect, oop value) {
2808   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2809    reflect->obj_field_put(clazz_offset, value);
2810 }
2811 
2812 int java_lang_reflect_Method::slot(oop reflect) {
2813   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2814   return reflect->int_field(slot_offset);
2815 }
2816 
2817 void java_lang_reflect_Method::set_slot(oop reflect, int value) {
2818   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2819   reflect->int_field_put(slot_offset, value);
2820 }
2821 
2822 void java_lang_reflect_Method::set_name(oop method, oop value) {
2823   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2824   method->obj_field_put(name_offset, value);
2825 }
2826 
2827 oop java_lang_reflect_Method::return_type(oop method) {
2828   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2829   return method->obj_field(returnType_offset);
2830 }
2831 
2832 void java_lang_reflect_Method::set_return_type(oop method, oop value) {
2833   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2834   method->obj_field_put(returnType_offset, value);
2835 }
2836 
2837 oop java_lang_reflect_Method::parameter_types(oop method) {
2838   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2839   return method->obj_field(parameterTypes_offset);
2840 }
2841 
2842 void java_lang_reflect_Method::set_parameter_types(oop method, oop value) {
2843   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2844   method->obj_field_put(parameterTypes_offset, value);
2845 }
2846 
2847 void java_lang_reflect_Method::set_exception_types(oop method, oop value) {
2848   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2849   method->obj_field_put(exceptionTypes_offset, value);
2850 }
2851 
2852 void java_lang_reflect_Method::set_modifiers(oop method, int value) {
2853   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2854   method->int_field_put(modifiers_offset, value);
2855 }
2856 
2857 void java_lang_reflect_Method::set_signature(oop method, oop value) {
2858   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2859   method->obj_field_put(signature_offset, value);
2860 }
2861 
2862 void java_lang_reflect_Method::set_annotations(oop method, oop value) {
2863   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2864   method->obj_field_put(annotations_offset, value);
2865 }
2866 
2867 void java_lang_reflect_Method::set_parameter_annotations(oop method, oop value) {
2868   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2869   method->obj_field_put(parameter_annotations_offset, value);
2870 }
2871 
2872 void java_lang_reflect_Method::set_annotation_default(oop method, oop value) {
2873   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2874   method->obj_field_put(annotation_default_offset, value);
2875 }
2876 
2877 #define CONSTRUCTOR_FIELDS_DO(macro) \
2878   macro(clazz_offset,          k, vmSymbols::clazz_name(),          class_signature,       false); \
2879   macro(parameterTypes_offset, k, vmSymbols::parameterTypes_name(), class_array_signature, false); \
2880   macro(exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), class_array_signature, false); \
2881   macro(slot_offset,           k, vmSymbols::slot_name(),           int_signature,         false); \
2882   macro(modifiers_offset,      k, vmSymbols::modifiers_name(),      int_signature,         false); \
2883   macro(signature_offset,             k, vmSymbols::signature_name(),             string_signature,     false); \
2884   macro(annotations_offset,           k, vmSymbols::annotations_name(),           byte_array_signature, false); \
2885   macro(parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), byte_array_signature, false);
2886 
2887 void java_lang_reflect_Constructor::compute_offsets() {
2888   InstanceKlass* k = SystemDictionary::reflect_Constructor_klass();
2889   CONSTRUCTOR_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2890 }
2891 
2892 #if INCLUDE_CDS
2893 void java_lang_reflect_Constructor::serialize_offsets(SerializeClosure* f) {
2894   CONSTRUCTOR_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2895 }
2896 #endif
2897 
2898 Handle java_lang_reflect_Constructor::create(TRAPS) {
2899   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2900   Symbol* name = vmSymbols::java_lang_reflect_Constructor();
2901   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
2902   InstanceKlass* ik = InstanceKlass::cast(k);
2903   // Ensure it is initialized
2904   ik->initialize(CHECK_NH);
2905   return ik->allocate_instance_handle(THREAD);
2906 }
2907 
2908 oop java_lang_reflect_Constructor::clazz(oop reflect) {
2909   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2910   return reflect->obj_field(clazz_offset);
2911 }
2912 
2913 void java_lang_reflect_Constructor::set_clazz(oop reflect, oop value) {
2914   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2915    reflect->obj_field_put(clazz_offset, value);
2916 }
2917 
2918 oop java_lang_reflect_Constructor::parameter_types(oop constructor) {
2919   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2920   return constructor->obj_field(parameterTypes_offset);
2921 }
2922 
2923 void java_lang_reflect_Constructor::set_parameter_types(oop constructor, oop value) {
2924   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2925   constructor->obj_field_put(parameterTypes_offset, value);
2926 }
2927 
2928 void java_lang_reflect_Constructor::set_exception_types(oop constructor, oop value) {
2929   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2930   constructor->obj_field_put(exceptionTypes_offset, value);
2931 }
2932 
2933 int java_lang_reflect_Constructor::slot(oop reflect) {
2934   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2935   return reflect->int_field(slot_offset);
2936 }
2937 
2938 void java_lang_reflect_Constructor::set_slot(oop reflect, int value) {
2939   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2940   reflect->int_field_put(slot_offset, value);
2941 }
2942 
2943 void java_lang_reflect_Constructor::set_modifiers(oop constructor, int value) {
2944   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2945   constructor->int_field_put(modifiers_offset, value);
2946 }
2947 
2948 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) {
2949   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2950   constructor->obj_field_put(signature_offset, value);
2951 }
2952 
2953 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) {
2954   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2955   constructor->obj_field_put(annotations_offset, value);
2956 }
2957 
2958 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) {
2959   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2960   method->obj_field_put(parameter_annotations_offset, value);
2961 }
2962 
2963 #define FIELD_FIELDS_DO(macro) \
2964   macro(clazz_offset,     k, vmSymbols::clazz_name(),     class_signature,  false); \
2965   macro(name_offset,      k, vmSymbols::name_name(),      string_signature, false); \
2966   macro(type_offset,      k, vmSymbols::type_name(),      class_signature,  false); \
2967   macro(slot_offset,      k, vmSymbols::slot_name(),      int_signature,    false); \
2968   macro(modifiers_offset, k, vmSymbols::modifiers_name(), int_signature,    false); \
2969   macro(signature_offset,        k, vmSymbols::signature_name(),        string_signature,     false); \
2970   macro(annotations_offset,      k, vmSymbols::annotations_name(),      byte_array_signature, false);
2971 
2972 void java_lang_reflect_Field::compute_offsets() {
2973   InstanceKlass* k = SystemDictionary::reflect_Field_klass();
2974   FIELD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2975 }
2976 
2977 #if INCLUDE_CDS
2978 void java_lang_reflect_Field::serialize_offsets(SerializeClosure* f) {
2979   FIELD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2980 }
2981 #endif
2982 
2983 Handle java_lang_reflect_Field::create(TRAPS) {
2984   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2985   Symbol* name = vmSymbols::java_lang_reflect_Field();
2986   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
2987   InstanceKlass* ik = InstanceKlass::cast(k);
2988   // Ensure it is initialized
2989   ik->initialize(CHECK_NH);
2990   return ik->allocate_instance_handle(THREAD);
2991 }
2992 
2993 oop java_lang_reflect_Field::clazz(oop reflect) {
2994   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2995   return reflect->obj_field(clazz_offset);
2996 }
2997 
2998 void java_lang_reflect_Field::set_clazz(oop reflect, oop value) {
2999   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3000    reflect->obj_field_put(clazz_offset, value);
3001 }
3002 
3003 oop java_lang_reflect_Field::name(oop field) {
3004   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3005   return field->obj_field(name_offset);
3006 }
3007 
3008 void java_lang_reflect_Field::set_name(oop field, oop value) {
3009   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3010   field->obj_field_put(name_offset, value);
3011 }
3012 
3013 oop java_lang_reflect_Field::type(oop field) {
3014   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3015   return field->obj_field(type_offset);
3016 }
3017 
3018 void java_lang_reflect_Field::set_type(oop field, oop value) {
3019   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3020   field->obj_field_put(type_offset, value);
3021 }
3022 
3023 int java_lang_reflect_Field::slot(oop reflect) {
3024   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3025   return reflect->int_field(slot_offset);
3026 }
3027 
3028 void java_lang_reflect_Field::set_slot(oop reflect, int value) {
3029   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3030   reflect->int_field_put(slot_offset, value);
3031 }
3032 
3033 int java_lang_reflect_Field::modifiers(oop field) {
3034   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3035   return field->int_field(modifiers_offset);
3036 }
3037 
3038 void java_lang_reflect_Field::set_modifiers(oop field, int value) {
3039   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3040   field->int_field_put(modifiers_offset, value);
3041 }
3042 
3043 void java_lang_reflect_Field::set_signature(oop field, oop value) {
3044   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3045   field->obj_field_put(signature_offset, value);
3046 }
3047 
3048 void java_lang_reflect_Field::set_annotations(oop field, oop value) {
3049   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3050   field->obj_field_put(annotations_offset, value);
3051 }
3052 
3053 #define CONSTANTPOOL_FIELDS_DO(macro) \
3054   macro(_oop_offset, k, "constantPoolOop", object_signature, false)
3055 
3056 void reflect_ConstantPool::compute_offsets() {
3057   InstanceKlass* k = SystemDictionary::reflect_ConstantPool_klass();
3058   // The field is called ConstantPool* in the sun.reflect.ConstantPool class.
3059   CONSTANTPOOL_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3060 }
3061 
3062 #if INCLUDE_CDS
3063 void reflect_ConstantPool::serialize_offsets(SerializeClosure* f) {
3064   CONSTANTPOOL_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3065 }
3066 #endif
3067 
3068 #define PARAMETER_FIELDS_DO(macro) \
3069   macro(name_offset,        k, vmSymbols::name_name(),        string_signature, false); \
3070   macro(modifiers_offset,   k, vmSymbols::modifiers_name(),   int_signature,    false); \
3071   macro(index_offset,       k, vmSymbols::index_name(),       int_signature,    false); \
3072   macro(executable_offset,  k, vmSymbols::executable_name(),  executable_signature, false)
3073 
3074 void java_lang_reflect_Parameter::compute_offsets() {
3075   InstanceKlass* k = SystemDictionary::reflect_Parameter_klass();
3076   PARAMETER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3077 }
3078 
3079 #if INCLUDE_CDS
3080 void java_lang_reflect_Parameter::serialize_offsets(SerializeClosure* f) {
3081   PARAMETER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3082 }
3083 #endif
3084 
3085 Handle java_lang_reflect_Parameter::create(TRAPS) {
3086   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3087   Symbol* name = vmSymbols::java_lang_reflect_Parameter();
3088   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3089   InstanceKlass* ik = InstanceKlass::cast(k);
3090   // Ensure it is initialized
3091   ik->initialize(CHECK_NH);
3092   return ik->allocate_instance_handle(THREAD);
3093 }
3094 
3095 oop java_lang_reflect_Parameter::name(oop param) {
3096   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3097   return param->obj_field(name_offset);
3098 }
3099 
3100 void java_lang_reflect_Parameter::set_name(oop param, oop value) {
3101   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3102   param->obj_field_put(name_offset, value);
3103 }
3104 
3105 int java_lang_reflect_Parameter::modifiers(oop param) {
3106   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3107   return param->int_field(modifiers_offset);
3108 }
3109 
3110 void java_lang_reflect_Parameter::set_modifiers(oop param, int value) {
3111   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3112   param->int_field_put(modifiers_offset, value);
3113 }
3114 
3115 int java_lang_reflect_Parameter::index(oop param) {
3116   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3117   return param->int_field(index_offset);
3118 }
3119 
3120 void java_lang_reflect_Parameter::set_index(oop param, int value) {
3121   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3122   param->int_field_put(index_offset, value);
3123 }
3124 
3125 oop java_lang_reflect_Parameter::executable(oop param) {
3126   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3127   return param->obj_field(executable_offset);
3128 }
3129 
3130 void java_lang_reflect_Parameter::set_executable(oop param, oop value) {
3131   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3132   param->obj_field_put(executable_offset, value);
3133 }
3134 
3135 
3136 int java_lang_Module::loader_offset;
3137 int java_lang_Module::name_offset;
3138 int java_lang_Module::_module_entry_offset = -1;
3139 
3140 Handle java_lang_Module::create(Handle loader, Handle module_name, TRAPS) {
3141   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3142   return JavaCalls::construct_new_instance(SystemDictionary::Module_klass(),
3143                           vmSymbols::java_lang_module_init_signature(),
3144                           loader, module_name, CHECK_NH);
3145 }
3146 
3147 #define MODULE_FIELDS_DO(macro) \
3148   macro(loader_offset,  k, vmSymbols::loader_name(),  classloader_signature, false); \
3149   macro(name_offset,    k, vmSymbols::name_name(),    string_signature,      false)
3150 
3151 void java_lang_Module::compute_offsets() {
3152   InstanceKlass* k = SystemDictionary::Module_klass();
3153   MODULE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3154   MODULE_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3155 }
3156 
3157 #if INCLUDE_CDS
3158 void java_lang_Module::serialize_offsets(SerializeClosure* f) {
3159   MODULE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3160   MODULE_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3161 }
3162 #endif
3163 
3164 oop java_lang_Module::loader(oop module) {
3165   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3166   return module->obj_field(loader_offset);
3167 }
3168 
3169 void java_lang_Module::set_loader(oop module, oop value) {
3170   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3171   module->obj_field_put(loader_offset, value);
3172 }
3173 
3174 oop java_lang_Module::name(oop module) {
3175   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3176   return module->obj_field(name_offset);
3177 }
3178 
3179 void java_lang_Module::set_name(oop module, oop value) {
3180   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3181   module->obj_field_put(name_offset, value);
3182 }
3183 
3184 ModuleEntry* java_lang_Module::module_entry(oop module) {
3185   assert(_module_entry_offset != -1, "Uninitialized module_entry_offset");
3186   assert(module != NULL, "module can't be null");
3187   assert(oopDesc::is_oop(module), "module must be oop");
3188 
3189   ModuleEntry* module_entry = (ModuleEntry*)module->address_field(_module_entry_offset);
3190   if (module_entry == NULL) {
3191     // If the inject field containing the ModuleEntry* is null then return the
3192     // class loader's unnamed module.
3193     oop loader = java_lang_Module::loader(module);
3194     Handle h_loader = Handle(Thread::current(), loader);
3195     ClassLoaderData* loader_cld = SystemDictionary::register_loader(h_loader);
3196     return loader_cld->unnamed_module();
3197   }
3198   return module_entry;
3199 }
3200 
3201 void java_lang_Module::set_module_entry(oop module, ModuleEntry* module_entry) {
3202   assert(_module_entry_offset != -1, "Uninitialized module_entry_offset");
3203   assert(module != NULL, "module can't be null");
3204   assert(oopDesc::is_oop(module), "module must be oop");
3205   module->address_field_put(_module_entry_offset, (address)module_entry);
3206 }
3207 
3208 Handle reflect_ConstantPool::create(TRAPS) {
3209   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3210   InstanceKlass* k = SystemDictionary::reflect_ConstantPool_klass();
3211   // Ensure it is initialized
3212   k->initialize(CHECK_NH);
3213   return k->allocate_instance_handle(THREAD);
3214 }
3215 
3216 
3217 void reflect_ConstantPool::set_cp(oop reflect, ConstantPool* value) {
3218   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3219   oop mirror = value->pool_holder()->java_mirror();
3220   // Save the mirror to get back the constant pool.
3221   reflect->obj_field_put(_oop_offset, mirror);
3222 }
3223 
3224 ConstantPool* reflect_ConstantPool::get_cp(oop reflect) {
3225   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3226 
3227   oop mirror = reflect->obj_field(_oop_offset);
3228   Klass* k = java_lang_Class::as_Klass(mirror);
3229   assert(k->is_instance_klass(), "Must be");
3230 
3231   // Get the constant pool back from the klass.  Since class redefinition
3232   // merges the new constant pool into the old, this is essentially the
3233   // same constant pool as the original.  If constant pool merging is
3234   // no longer done in the future, this will have to change to save
3235   // the original.
3236   return InstanceKlass::cast(k)->constants();
3237 }
3238 
3239 #define UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(macro) \
3240   macro(_base_offset, k, "base", object_signature, false)
3241 
3242 void reflect_UnsafeStaticFieldAccessorImpl::compute_offsets() {
3243   InstanceKlass* k = SystemDictionary::reflect_UnsafeStaticFieldAccessorImpl_klass();
3244   UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3245 }
3246 
3247 #if INCLUDE_CDS
3248 void reflect_UnsafeStaticFieldAccessorImpl::serialize_offsets(SerializeClosure* f) {
3249   UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3250 }
3251 #endif
3252 
3253 oop java_lang_boxing_object::initialize_and_allocate(BasicType type, TRAPS) {
3254   Klass* k = SystemDictionary::box_klass(type);
3255   if (k == NULL)  return NULL;
3256   InstanceKlass* ik = InstanceKlass::cast(k);
3257   if (!ik->is_initialized())  ik->initialize(CHECK_0);
3258   return ik->allocate_instance(THREAD);
3259 }
3260 
3261 
3262 oop java_lang_boxing_object::create(BasicType type, jvalue* value, TRAPS) {
3263   oop box = initialize_and_allocate(type, CHECK_0);
3264   if (box == NULL)  return NULL;
3265   switch (type) {
3266     case T_BOOLEAN:
3267       box->bool_field_put(value_offset, value->z);
3268       break;
3269     case T_CHAR:
3270       box->char_field_put(value_offset, value->c);
3271       break;
3272     case T_FLOAT:
3273       box->float_field_put(value_offset, value->f);
3274       break;
3275     case T_DOUBLE:
3276       box->double_field_put(long_value_offset, value->d);
3277       break;
3278     case T_BYTE:
3279       box->byte_field_put(value_offset, value->b);
3280       break;
3281     case T_SHORT:
3282       box->short_field_put(value_offset, value->s);
3283       break;
3284     case T_INT:
3285       box->int_field_put(value_offset, value->i);
3286       break;
3287     case T_LONG:
3288       box->long_field_put(long_value_offset, value->j);
3289       break;
3290     default:
3291       return NULL;
3292   }
3293   return box;
3294 }
3295 
3296 
3297 BasicType java_lang_boxing_object::basic_type(oop box) {
3298   if (box == NULL)  return T_ILLEGAL;
3299   BasicType type = SystemDictionary::box_klass_type(box->klass());
3300   if (type == T_OBJECT)         // 'unknown' value returned by SD::bkt
3301     return T_ILLEGAL;
3302   return type;
3303 }
3304 
3305 
3306 BasicType java_lang_boxing_object::get_value(oop box, jvalue* value) {
3307   BasicType type = SystemDictionary::box_klass_type(box->klass());
3308   switch (type) {
3309   case T_BOOLEAN:
3310     value->z = box->bool_field(value_offset);
3311     break;
3312   case T_CHAR:
3313     value->c = box->char_field(value_offset);
3314     break;
3315   case T_FLOAT:
3316     value->f = box->float_field(value_offset);
3317     break;
3318   case T_DOUBLE:
3319     value->d = box->double_field(long_value_offset);
3320     break;
3321   case T_BYTE:
3322     value->b = box->byte_field(value_offset);
3323     break;
3324   case T_SHORT:
3325     value->s = box->short_field(value_offset);
3326     break;
3327   case T_INT:
3328     value->i = box->int_field(value_offset);
3329     break;
3330   case T_LONG:
3331     value->j = box->long_field(long_value_offset);
3332     break;
3333   default:
3334     return T_ILLEGAL;
3335   } // end switch
3336   return type;
3337 }
3338 
3339 
3340 BasicType java_lang_boxing_object::set_value(oop box, jvalue* value) {
3341   BasicType type = SystemDictionary::box_klass_type(box->klass());
3342   switch (type) {
3343   case T_BOOLEAN:
3344     box->bool_field_put(value_offset, value->z);
3345     break;
3346   case T_CHAR:
3347     box->char_field_put(value_offset, value->c);
3348     break;
3349   case T_FLOAT:
3350     box->float_field_put(value_offset, value->f);
3351     break;
3352   case T_DOUBLE:
3353     box->double_field_put(long_value_offset, value->d);
3354     break;
3355   case T_BYTE:
3356     box->byte_field_put(value_offset, value->b);
3357     break;
3358   case T_SHORT:
3359     box->short_field_put(value_offset, value->s);
3360     break;
3361   case T_INT:
3362     box->int_field_put(value_offset, value->i);
3363     break;
3364   case T_LONG:
3365     box->long_field_put(long_value_offset, value->j);
3366     break;
3367   default:
3368     return T_ILLEGAL;
3369   } // end switch
3370   return type;
3371 }
3372 
3373 
3374 void java_lang_boxing_object::print(BasicType type, jvalue* value, outputStream* st) {
3375   switch (type) {
3376   case T_BOOLEAN:   st->print("%s", value->z ? "true" : "false");   break;
3377   case T_CHAR:      st->print("%d", value->c);                      break;
3378   case T_BYTE:      st->print("%d", value->b);                      break;
3379   case T_SHORT:     st->print("%d", value->s);                      break;
3380   case T_INT:       st->print("%d", value->i);                      break;
3381   case T_LONG:      st->print(JLONG_FORMAT, value->j);              break;
3382   case T_FLOAT:     st->print("%f", value->f);                      break;
3383   case T_DOUBLE:    st->print("%lf", value->d);                     break;
3384   default:          st->print("type %d?", type);                    break;
3385   }
3386 }
3387 
3388 // Support for java_lang_ref_Reference
3389 
3390 bool java_lang_ref_Reference::is_referent_field(oop obj, ptrdiff_t offset) {
3391   assert(obj != NULL, "sanity");
3392   if (offset != java_lang_ref_Reference::referent_offset) {
3393     return false;
3394   }
3395 
3396   Klass* k = obj->klass();
3397   if (!k->is_instance_klass()) {
3398     return false;
3399   }
3400 
3401   InstanceKlass* ik = InstanceKlass::cast(obj->klass());
3402   bool is_reference = ik->reference_type() != REF_NONE;
3403   assert(!is_reference || ik->is_subclass_of(SystemDictionary::Reference_klass()), "sanity");
3404   return is_reference;
3405 }
3406 
3407 // Support for java_lang_ref_SoftReference
3408 //
3409 
3410 #define SOFTREFERENCE_FIELDS_DO(macro) \
3411   macro(timestamp_offset,    k, "timestamp", long_signature, false); \
3412   macro(static_clock_offset, k, "clock",     long_signature, true)
3413 
3414 void java_lang_ref_SoftReference::compute_offsets() {
3415   InstanceKlass* k = SystemDictionary::SoftReference_klass();
3416   SOFTREFERENCE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3417 }
3418 
3419 #if INCLUDE_CDS
3420 void java_lang_ref_SoftReference::serialize_offsets(SerializeClosure* f) {
3421   SOFTREFERENCE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3422 }
3423 #endif
3424 
3425 jlong java_lang_ref_SoftReference::timestamp(oop ref) {
3426   return ref->long_field(timestamp_offset);
3427 }
3428 
3429 jlong java_lang_ref_SoftReference::clock() {
3430   InstanceKlass* ik = SystemDictionary::SoftReference_klass();
3431   oop base = ik->static_field_base_raw();
3432   return base->long_field(static_clock_offset);
3433 }
3434 
3435 void java_lang_ref_SoftReference::set_clock(jlong value) {
3436   InstanceKlass* ik = SystemDictionary::SoftReference_klass();
3437   oop base = ik->static_field_base_raw();
3438   base->long_field_put(static_clock_offset, value);
3439 }
3440 
3441 // Support for java_lang_invoke_DirectMethodHandle
3442 
3443 int java_lang_invoke_DirectMethodHandle::_member_offset;
3444 
3445 oop java_lang_invoke_DirectMethodHandle::member(oop dmh) {
3446   oop member_name = NULL;
3447   assert(oopDesc::is_oop(dmh) && java_lang_invoke_DirectMethodHandle::is_instance(dmh),
3448          "a DirectMethodHandle oop is expected");
3449   return dmh->obj_field(member_offset_in_bytes());
3450 }
3451 
3452 #define DIRECTMETHODHANDLE_FIELDS_DO(macro) \
3453   macro(_member_offset, k, "member", java_lang_invoke_MemberName_signature, false)
3454 
3455 void java_lang_invoke_DirectMethodHandle::compute_offsets() {
3456   InstanceKlass* k = SystemDictionary::DirectMethodHandle_klass();
3457   DIRECTMETHODHANDLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3458 }
3459 
3460 #if INCLUDE_CDS
3461 void java_lang_invoke_DirectMethodHandle::serialize_offsets(SerializeClosure* f) {
3462   DIRECTMETHODHANDLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3463 }
3464 #endif
3465 
3466 // Support for java_lang_invoke_MethodHandle
3467 
3468 int java_lang_invoke_MethodHandle::_type_offset;
3469 int java_lang_invoke_MethodHandle::_form_offset;
3470 
3471 int java_lang_invoke_MemberName::_clazz_offset;
3472 int java_lang_invoke_MemberName::_name_offset;
3473 int java_lang_invoke_MemberName::_type_offset;
3474 int java_lang_invoke_MemberName::_flags_offset;
3475 int java_lang_invoke_MemberName::_method_offset;
3476 int java_lang_invoke_MemberName::_vmindex_offset;
3477 
3478 int java_lang_invoke_ResolvedMethodName::_vmtarget_offset;
3479 int java_lang_invoke_ResolvedMethodName::_vmholder_offset;
3480 
3481 int java_lang_invoke_LambdaForm::_vmentry_offset;
3482 
3483 #define METHODHANDLE_FIELDS_DO(macro) \
3484   macro(_type_offset, k, vmSymbols::type_name(), java_lang_invoke_MethodType_signature, false); \
3485   macro(_form_offset, k, "form",                 java_lang_invoke_LambdaForm_signature, false)
3486 
3487 void java_lang_invoke_MethodHandle::compute_offsets() {
3488   InstanceKlass* k = SystemDictionary::MethodHandle_klass();
3489   METHODHANDLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3490 }
3491 
3492 #if INCLUDE_CDS
3493 void java_lang_invoke_MethodHandle::serialize_offsets(SerializeClosure* f) {
3494   METHODHANDLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3495 }
3496 #endif
3497 
3498 #define MEMBERNAME_FIELDS_DO(macro) \
3499   macro(_clazz_offset,   k, vmSymbols::clazz_name(),   class_signature,  false); \
3500   macro(_name_offset,    k, vmSymbols::name_name(),    string_signature, false); \
3501   macro(_type_offset,    k, vmSymbols::type_name(),    object_signature, false); \
3502   macro(_flags_offset,   k, vmSymbols::flags_name(),   int_signature,    false); \
3503   macro(_method_offset,  k, vmSymbols::method_name(),  java_lang_invoke_ResolvedMethodName_signature, false)
3504 
3505 void java_lang_invoke_MemberName::compute_offsets() {
3506   InstanceKlass* k = SystemDictionary::MemberName_klass();
3507   MEMBERNAME_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3508   MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3509 }
3510 
3511 #if INCLUDE_CDS
3512 void java_lang_invoke_MemberName::serialize_offsets(SerializeClosure* f) {
3513   MEMBERNAME_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3514   MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3515 }
3516 #endif
3517 
3518 void java_lang_invoke_ResolvedMethodName::compute_offsets() {
3519   InstanceKlass* k = SystemDictionary::ResolvedMethodName_klass();
3520   assert(k != NULL, "jdk mismatch");
3521   RESOLVEDMETHOD_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3522 }
3523 
3524 #if INCLUDE_CDS
3525 void java_lang_invoke_ResolvedMethodName::serialize_offsets(SerializeClosure* f) {
3526   RESOLVEDMETHOD_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3527 }
3528 #endif
3529 
3530 #define LAMBDAFORM_FIELDS_DO(macro) \
3531   macro(_vmentry_offset, k, "vmentry", java_lang_invoke_MemberName_signature, false)
3532 
3533 void java_lang_invoke_LambdaForm::compute_offsets() {
3534   InstanceKlass* k = SystemDictionary::LambdaForm_klass();
3535   assert (k != NULL, "jdk mismatch");
3536   LAMBDAFORM_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3537 }
3538 
3539 #if INCLUDE_CDS
3540 void java_lang_invoke_LambdaForm::serialize_offsets(SerializeClosure* f) {
3541   LAMBDAFORM_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3542 }
3543 #endif
3544 
3545 bool java_lang_invoke_LambdaForm::is_instance(oop obj) {
3546   return obj != NULL && is_subclass(obj->klass());
3547 }
3548 
3549 
3550 oop java_lang_invoke_MethodHandle::type(oop mh) {
3551   return mh->obj_field(_type_offset);
3552 }
3553 
3554 void java_lang_invoke_MethodHandle::set_type(oop mh, oop mtype) {
3555   mh->obj_field_put(_type_offset, mtype);
3556 }
3557 
3558 oop java_lang_invoke_MethodHandle::form(oop mh) {
3559   assert(_form_offset != 0, "");
3560   return mh->obj_field(_form_offset);
3561 }
3562 
3563 void java_lang_invoke_MethodHandle::set_form(oop mh, oop lform) {
3564   assert(_form_offset != 0, "");
3565   mh->obj_field_put(_form_offset, lform);
3566 }
3567 
3568 /// MemberName accessors
3569 
3570 oop java_lang_invoke_MemberName::clazz(oop mname) {
3571   assert(is_instance(mname), "wrong type");
3572   return mname->obj_field(_clazz_offset);
3573 }
3574 
3575 void java_lang_invoke_MemberName::set_clazz(oop mname, oop clazz) {
3576   assert(is_instance(mname), "wrong type");
3577   mname->obj_field_put(_clazz_offset, clazz);
3578 }
3579 
3580 oop java_lang_invoke_MemberName::name(oop mname) {
3581   assert(is_instance(mname), "wrong type");
3582   return mname->obj_field(_name_offset);
3583 }
3584 
3585 void java_lang_invoke_MemberName::set_name(oop mname, oop name) {
3586   assert(is_instance(mname), "wrong type");
3587   mname->obj_field_put(_name_offset, name);
3588 }
3589 
3590 oop java_lang_invoke_MemberName::type(oop mname) {
3591   assert(is_instance(mname), "wrong type");
3592   return mname->obj_field(_type_offset);
3593 }
3594 
3595 void java_lang_invoke_MemberName::set_type(oop mname, oop type) {
3596   assert(is_instance(mname), "wrong type");
3597   mname->obj_field_put(_type_offset, type);
3598 }
3599 
3600 int java_lang_invoke_MemberName::flags(oop mname) {
3601   assert(is_instance(mname), "wrong type");
3602   return mname->int_field(_flags_offset);
3603 }
3604 
3605 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) {
3606   assert(is_instance(mname), "wrong type");
3607   mname->int_field_put(_flags_offset, flags);
3608 }
3609 
3610 
3611 // Return vmtarget from ResolvedMethodName method field through indirection
3612 Method* java_lang_invoke_MemberName::vmtarget(oop mname) {
3613   assert(is_instance(mname), "wrong type");
3614   oop method = mname->obj_field(_method_offset);
3615   return method == NULL ? NULL : java_lang_invoke_ResolvedMethodName::vmtarget(method);
3616 }
3617 
3618 bool java_lang_invoke_MemberName::is_method(oop mname) {
3619   assert(is_instance(mname), "must be MemberName");
3620   return (flags(mname) & (MN_IS_METHOD | MN_IS_CONSTRUCTOR)) > 0;
3621 }
3622 
3623 void java_lang_invoke_MemberName::set_method(oop mname, oop resolved_method) {
3624   assert(is_instance(mname), "wrong type");
3625   mname->obj_field_put(_method_offset, resolved_method);
3626 }
3627 
3628 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) {
3629   assert(is_instance(mname), "wrong type");
3630   return (intptr_t) mname->address_field(_vmindex_offset);
3631 }
3632 
3633 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
3634   assert(is_instance(mname), "wrong type");
3635   mname->address_field_put(_vmindex_offset, (address) index);
3636 }
3637 
3638 
3639 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) {
3640   assert(is_instance(resolved_method), "wrong type");
3641   Method* m = (Method*)resolved_method->address_field(_vmtarget_offset);
3642   assert(m->is_method(), "must be");
3643   return m;
3644 }
3645 
3646 // Used by redefinition to change Method* to new Method* with same hash (name, signature)
3647 void java_lang_invoke_ResolvedMethodName::set_vmtarget(oop resolved_method, Method* m) {
3648   assert(is_instance(resolved_method), "wrong type");
3649   resolved_method->address_field_put(_vmtarget_offset, (address)m);
3650 }
3651 
3652 oop java_lang_invoke_ResolvedMethodName::find_resolved_method(const methodHandle& m, TRAPS) {
3653   // lookup ResolvedMethod oop in the table, or create a new one and intern it
3654   oop resolved_method = ResolvedMethodTable::find_method(m());
3655   if (resolved_method == NULL) {
3656     InstanceKlass* k = SystemDictionary::ResolvedMethodName_klass();
3657     if (!k->is_initialized()) {
3658       k->initialize(CHECK_NULL);
3659     }
3660     oop new_resolved_method = k->allocate_instance(CHECK_NULL);
3661     new_resolved_method->address_field_put(_vmtarget_offset, (address)m());
3662     // Add a reference to the loader (actually mirror because unsafe anonymous classes will not have
3663     // distinct loaders) to ensure the metadata is kept alive.
3664     // This mirror may be different than the one in clazz field.
3665     new_resolved_method->obj_field_put(_vmholder_offset, m->method_holder()->java_mirror());
3666     resolved_method = ResolvedMethodTable::add_method(m, Handle(THREAD, new_resolved_method));
3667   }
3668   return resolved_method;
3669 }
3670 
3671 oop java_lang_invoke_LambdaForm::vmentry(oop lform) {
3672   assert(is_instance(lform), "wrong type");
3673   return lform->obj_field(_vmentry_offset);
3674 }
3675 
3676 
3677 // Support for java_lang_invoke_MethodType
3678 
3679 int java_lang_invoke_MethodType::_rtype_offset;
3680 int java_lang_invoke_MethodType::_ptypes_offset;
3681 
3682 #define METHODTYPE_FIELDS_DO(macro) \
3683   macro(_rtype_offset,  k, "rtype",  class_signature,       false); \
3684   macro(_ptypes_offset, k, "ptypes", class_array_signature, false)
3685 
3686 void java_lang_invoke_MethodType::compute_offsets() {
3687   InstanceKlass* k = SystemDictionary::MethodType_klass();
3688   METHODTYPE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3689 }
3690 
3691 #if INCLUDE_CDS
3692 void java_lang_invoke_MethodType::serialize_offsets(SerializeClosure* f) {
3693   METHODTYPE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3694 }
3695 #endif
3696 
3697 void java_lang_invoke_MethodType::print_signature(oop mt, outputStream* st) {
3698   st->print("(");
3699   objArrayOop pts = ptypes(mt);
3700   for (int i = 0, limit = pts->length(); i < limit; i++) {
3701     java_lang_Class::print_signature(pts->obj_at(i), st);
3702   }
3703   st->print(")");
3704   java_lang_Class::print_signature(rtype(mt), st);
3705 }
3706 
3707 Symbol* java_lang_invoke_MethodType::as_signature(oop mt, bool intern_if_not_found, TRAPS) {
3708   ResourceMark rm;
3709   stringStream buffer(128);
3710   print_signature(mt, &buffer);
3711   const char* sigstr =       buffer.base();
3712   int         siglen = (int) buffer.size();
3713   Symbol *name;
3714   if (!intern_if_not_found) {
3715     name = SymbolTable::probe(sigstr, siglen);
3716   } else {
3717     name = SymbolTable::new_symbol(sigstr, siglen, THREAD);
3718   }
3719   return name;
3720 }
3721 
3722 bool java_lang_invoke_MethodType::equals(oop mt1, oop mt2) {
3723   if (oopDesc::equals(mt1, mt2))
3724     return true;
3725   if (!oopDesc::equals(rtype(mt1), rtype(mt2)))
3726     return false;
3727   if (ptype_count(mt1) != ptype_count(mt2))
3728     return false;
3729   for (int i = ptype_count(mt1) - 1; i >= 0; i--) {
3730     if (!oopDesc::equals(ptype(mt1, i), ptype(mt2, i)))
3731       return false;
3732   }
3733   return true;
3734 }
3735 
3736 oop java_lang_invoke_MethodType::rtype(oop mt) {
3737   assert(is_instance(mt), "must be a MethodType");
3738   return mt->obj_field(_rtype_offset);
3739 }
3740 
3741 objArrayOop java_lang_invoke_MethodType::ptypes(oop mt) {
3742   assert(is_instance(mt), "must be a MethodType");
3743   return (objArrayOop) mt->obj_field(_ptypes_offset);
3744 }
3745 
3746 oop java_lang_invoke_MethodType::ptype(oop mt, int idx) {
3747   return ptypes(mt)->obj_at(idx);
3748 }
3749 
3750 int java_lang_invoke_MethodType::ptype_count(oop mt) {
3751   return ptypes(mt)->length();
3752 }
3753 
3754 int java_lang_invoke_MethodType::ptype_slot_count(oop mt) {
3755   objArrayOop pts = ptypes(mt);
3756   int count = pts->length();
3757   int slots = 0;
3758   for (int i = 0; i < count; i++) {
3759     BasicType bt = java_lang_Class::as_BasicType(pts->obj_at(i));
3760     slots += type2size[bt];
3761   }
3762   return slots;
3763 }
3764 
3765 int java_lang_invoke_MethodType::rtype_slot_count(oop mt) {
3766   BasicType bt = java_lang_Class::as_BasicType(rtype(mt));
3767   return type2size[bt];
3768 }
3769 
3770 
3771 // Support for java_lang_invoke_CallSite
3772 
3773 int java_lang_invoke_CallSite::_target_offset;
3774 int java_lang_invoke_CallSite::_context_offset;
3775 
3776 #define CALLSITE_FIELDS_DO(macro) \
3777   macro(_target_offset,  k, "target", java_lang_invoke_MethodHandle_signature, false); \
3778   macro(_context_offset, k, "context", java_lang_invoke_MethodHandleNatives_CallSiteContext_signature, false)
3779 
3780 void java_lang_invoke_CallSite::compute_offsets() {
3781   InstanceKlass* k = SystemDictionary::CallSite_klass();
3782   CALLSITE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3783 }
3784 
3785 #if INCLUDE_CDS
3786 void java_lang_invoke_CallSite::serialize_offsets(SerializeClosure* f) {
3787   CALLSITE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3788 }
3789 #endif
3790 
3791 oop java_lang_invoke_CallSite::context_no_keepalive(oop call_site) {
3792   assert(java_lang_invoke_CallSite::is_instance(call_site), "");
3793 
3794   oop dep_oop = call_site->obj_field_access<AS_NO_KEEPALIVE>(_context_offset);
3795   return dep_oop;
3796 }
3797 
3798 // Support for java_lang_invoke_MethodHandleNatives_CallSiteContext
3799 
3800 int java_lang_invoke_MethodHandleNatives_CallSiteContext::_vmdependencies_offset;
3801 int java_lang_invoke_MethodHandleNatives_CallSiteContext::_last_cleanup_offset;
3802 
3803 void java_lang_invoke_MethodHandleNatives_CallSiteContext::compute_offsets() {
3804   InstanceKlass* k = SystemDictionary::Context_klass();
3805   CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3806 }
3807 
3808 #if INCLUDE_CDS
3809 void java_lang_invoke_MethodHandleNatives_CallSiteContext::serialize_offsets(SerializeClosure* f) {
3810   CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3811 }
3812 #endif
3813 
3814 DependencyContext java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(oop call_site) {
3815   assert(java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(call_site), "");
3816   nmethodBucket* volatile* vmdeps_addr = (nmethodBucket* volatile*)call_site->field_addr(_vmdependencies_offset);
3817   volatile uint64_t* last_cleanup_addr = (volatile uint64_t*)call_site->field_addr(_last_cleanup_offset);
3818   DependencyContext dep_ctx(vmdeps_addr, last_cleanup_addr);
3819   return dep_ctx;
3820 }
3821 
3822 // Support for java_security_AccessControlContext
3823 
3824 int java_security_AccessControlContext::_context_offset = 0;
3825 int java_security_AccessControlContext::_privilegedContext_offset = 0;
3826 int java_security_AccessControlContext::_isPrivileged_offset = 0;
3827 int java_security_AccessControlContext::_isAuthorized_offset = -1;
3828 
3829 #define ACCESSCONTROLCONTEXT_FIELDS_DO(macro) \
3830   macro(_context_offset,           k, "context",      protectiondomain_signature, false); \
3831   macro(_privilegedContext_offset, k, "privilegedContext", accesscontrolcontext_signature, false); \
3832   macro(_isPrivileged_offset,      k, "isPrivileged", bool_signature, false); \
3833   macro(_isAuthorized_offset,      k, "isAuthorized", bool_signature, false)
3834 
3835 void java_security_AccessControlContext::compute_offsets() {
3836   assert(_isPrivileged_offset == 0, "offsets should be initialized only once");
3837   InstanceKlass* k = SystemDictionary::AccessControlContext_klass();
3838   ACCESSCONTROLCONTEXT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3839 }
3840 
3841 #if INCLUDE_CDS
3842 void java_security_AccessControlContext::serialize_offsets(SerializeClosure* f) {
3843   ACCESSCONTROLCONTEXT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3844 }
3845 #endif
3846 
3847 oop java_security_AccessControlContext::create(objArrayHandle context, bool isPrivileged, Handle privileged_context, TRAPS) {
3848   assert(_isPrivileged_offset != 0, "offsets should have been initialized");
3849   // Ensure klass is initialized
3850   SystemDictionary::AccessControlContext_klass()->initialize(CHECK_0);
3851   // Allocate result
3852   oop result = SystemDictionary::AccessControlContext_klass()->allocate_instance(CHECK_0);
3853   // Fill in values
3854   result->obj_field_put(_context_offset, context());
3855   result->obj_field_put(_privilegedContext_offset, privileged_context());
3856   result->bool_field_put(_isPrivileged_offset, isPrivileged);
3857   // whitelist AccessControlContexts created by the JVM if present
3858   if (_isAuthorized_offset != -1) {
3859     result->bool_field_put(_isAuthorized_offset, true);
3860   }
3861   return result;
3862 }
3863 
3864 
3865 // Support for java_lang_ClassLoader
3866 
3867 bool java_lang_ClassLoader::offsets_computed = false;
3868 int  java_lang_ClassLoader::_loader_data_offset = -1;
3869 int  java_lang_ClassLoader::parallelCapable_offset = -1;
3870 int  java_lang_ClassLoader::name_offset = -1;
3871 int  java_lang_ClassLoader::nameAndId_offset = -1;
3872 int  java_lang_ClassLoader::unnamedModule_offset = -1;
3873 
3874 ClassLoaderData* java_lang_ClassLoader::loader_data_acquire(oop loader) {
3875   assert(loader != NULL && oopDesc::is_oop(loader), "loader must be oop");
3876   return HeapAccess<MO_ACQUIRE>::load_at(loader, _loader_data_offset);
3877 }
3878 
3879 ClassLoaderData* java_lang_ClassLoader::loader_data_raw(oop loader) {
3880   assert(loader != NULL && oopDesc::is_oop(loader), "loader must be oop");
3881   return RawAccess<>::load_at(loader, _loader_data_offset);
3882 }
3883 
3884 void java_lang_ClassLoader::release_set_loader_data(oop loader, ClassLoaderData* new_data) {
3885   assert(loader != NULL && oopDesc::is_oop(loader), "loader must be oop");
3886   HeapAccess<MO_RELEASE>::store_at(loader, _loader_data_offset, new_data);
3887 }
3888 
3889 #define CLASSLOADER_FIELDS_DO(macro) \
3890   macro(parallelCapable_offset, k1, "parallelLockMap",      concurrenthashmap_signature, false); \
3891   macro(name_offset,            k1, vmSymbols::name_name(), string_signature, false); \
3892   macro(nameAndId_offset,       k1, "nameAndId",            string_signature, false); \
3893   macro(unnamedModule_offset,   k1, "unnamedModule",        module_signature, false); \
3894   macro(parent_offset,          k1, "parent",               classloader_signature, false)
3895 
3896 void java_lang_ClassLoader::compute_offsets() {
3897   assert(!offsets_computed, "offsets should be initialized only once");
3898   offsets_computed = true;
3899 
3900   InstanceKlass* k1 = SystemDictionary::ClassLoader_klass();
3901   CLASSLOADER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3902 
3903   CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3904 }
3905 
3906 #if INCLUDE_CDS
3907 void java_lang_ClassLoader::serialize_offsets(SerializeClosure* f) {
3908   CLASSLOADER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3909   CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3910 }
3911 #endif
3912 
3913 oop java_lang_ClassLoader::parent(oop loader) {
3914   assert(is_instance(loader), "loader must be oop");
3915   return loader->obj_field(parent_offset);
3916 }
3917 
3918 // Returns the name field of this class loader.  If the name field has not
3919 // been set, null will be returned.
3920 oop java_lang_ClassLoader::name(oop loader) {
3921   assert(is_instance(loader), "loader must be oop");
3922   return loader->obj_field(name_offset);
3923 }
3924 
3925 // Returns the nameAndId field of this class loader. The format is
3926 // as follows:
3927 //   If the defining loader has a name explicitly set then '<loader-name>' @<id>
3928 //   If the defining loader has no name then <qualified-class-name> @<id>
3929 //   If built-in loader, then omit '@<id>' as there is only one instance.
3930 // Use ClassLoader::loader_name_id() to obtain this String as a char*.
3931 oop java_lang_ClassLoader::nameAndId(oop loader) {
3932   assert(is_instance(loader), "loader must be oop");
3933   return loader->obj_field(nameAndId_offset);
3934 }
3935 
3936 bool java_lang_ClassLoader::isAncestor(oop loader, oop cl) {
3937   assert(is_instance(loader), "loader must be oop");
3938   assert(cl == NULL || is_instance(cl), "cl argument must be oop");
3939   oop acl = loader;
3940   debug_only(jint loop_count = 0);
3941   // This loop taken verbatim from ClassLoader.java:
3942   do {
3943     acl = parent(acl);
3944     if (oopDesc::equals(cl, acl)) {
3945       return true;
3946     }
3947     assert(++loop_count > 0, "loop_count overflow");
3948   } while (acl != NULL);
3949   return false;
3950 }
3951 
3952 bool java_lang_ClassLoader::is_instance(oop obj) {
3953   return obj != NULL && is_subclass(obj->klass());
3954 }
3955 
3956 
3957 // For class loader classes, parallelCapable defined
3958 // based on non-null field
3959 // Written to by java.lang.ClassLoader, vm only reads this field, doesn't set it
3960 bool java_lang_ClassLoader::parallelCapable(oop class_loader) {
3961   if (parallelCapable_offset == -1) {
3962      // Default for backward compatibility is false
3963      return false;
3964   }
3965   return (class_loader->obj_field(parallelCapable_offset) != NULL);
3966 }
3967 
3968 bool java_lang_ClassLoader::is_trusted_loader(oop loader) {
3969   // Fix for 4474172; see evaluation for more details
3970   loader = non_reflection_class_loader(loader);
3971 
3972   oop cl = SystemDictionary::java_system_loader();
3973   while(cl != NULL) {
3974     if (oopDesc::equals(cl, loader)) return true;
3975     cl = parent(cl);
3976   }
3977   return false;
3978 }
3979 
3980 // Return true if this is one of the class loaders associated with
3981 // the generated bytecodes for reflection.
3982 bool java_lang_ClassLoader::is_reflection_class_loader(oop loader) {
3983   if (loader != NULL) {
3984     Klass* delegating_cl_class = SystemDictionary::reflect_DelegatingClassLoader_klass();
3985     // This might be null in non-1.4 JDKs
3986     return (delegating_cl_class != NULL && loader->is_a(delegating_cl_class));
3987   }
3988   return false;
3989 }
3990 
3991 oop java_lang_ClassLoader::non_reflection_class_loader(oop loader) {
3992   // See whether this is one of the class loaders associated with
3993   // the generated bytecodes for reflection, and if so, "magically"
3994   // delegate to its parent to prevent class loading from occurring
3995   // in places where applications using reflection didn't expect it.
3996   if (is_reflection_class_loader(loader)) {
3997     return parent(loader);
3998   }
3999   return loader;
4000 }
4001 
4002 oop java_lang_ClassLoader::unnamedModule(oop loader) {
4003   assert(is_instance(loader), "loader must be oop");
4004   return loader->obj_field(unnamedModule_offset);
4005 }
4006 
4007 // Support for java_lang_System
4008 //
4009 #define SYSTEM_FIELDS_DO(macro) \
4010   macro(static_in_offset,  k, "in",  input_stream_signature, true); \
4011   macro(static_out_offset, k, "out", print_stream_signature, true); \
4012   macro(static_err_offset, k, "err", print_stream_signature, true); \
4013   macro(static_security_offset, k, "security", security_manager_signature, true)
4014 
4015 void java_lang_System::compute_offsets() {
4016   InstanceKlass* k = SystemDictionary::System_klass();
4017   SYSTEM_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4018 }
4019 
4020 #if INCLUDE_CDS
4021 void java_lang_System::serialize_offsets(SerializeClosure* f) {
4022    SYSTEM_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4023 }
4024 #endif
4025 
4026 int java_lang_System::in_offset_in_bytes() { return static_in_offset; }
4027 int java_lang_System::out_offset_in_bytes() { return static_out_offset; }
4028 int java_lang_System::err_offset_in_bytes() { return static_err_offset; }
4029 
4030 // Support for jdk_internal_misc_UnsafeConstants
4031 //
4032 class UnsafeConstantsFixup : public FieldClosure {
4033 private:
4034   int _address_size;
4035   int _page_size;
4036   bool _big_endian;
4037   bool _use_unaligned_access;
4038 public:
4039   UnsafeConstantsFixup() {
4040     // round up values for all static final fields
4041     _address_size = sizeof(void*);
4042     _page_size = os::vm_page_size();
4043     _big_endian = LITTLE_ENDIAN_ONLY(false) BIG_ENDIAN_ONLY(true);
4044     _use_unaligned_access = UseUnalignedAccesses;
4045   }
4046 
4047   void do_field(fieldDescriptor* fd) {
4048     oop mirror = fd->field_holder()->java_mirror();
4049     assert(mirror != NULL, "UnsafeConstants must have mirror already");
4050     assert(fd->field_holder() == SystemDictionary::UnsafeConstants_klass(), "Should be UnsafeConstants");
4051     assert(fd->is_final(), "fields of UnsafeConstants must be final");
4052     assert(fd->is_static(), "fields of UnsafeConstants must be static");
4053     if (fd->name() == vmSymbols::address_size_name()) {
4054       mirror->int_field_put(fd->offset(), _address_size);
4055     } else if (fd->name() == vmSymbols::page_size_name()) {
4056       mirror->int_field_put(fd->offset(), _page_size);
4057     } else if (fd->name() == vmSymbols::big_endian_name()) {
4058       mirror->bool_field_put(fd->offset(), _big_endian);
4059     } else if (fd->name() == vmSymbols::use_unaligned_access_name()) {
4060       mirror->bool_field_put(fd->offset(), _use_unaligned_access);
4061     } else {
4062       assert(false, "unexpected UnsafeConstants field");
4063     }
4064   }
4065 };
4066 
4067 void jdk_internal_misc_UnsafeConstants::set_unsafe_constants() {
4068   UnsafeConstantsFixup fixup;
4069   SystemDictionary::UnsafeConstants_klass()->do_local_static_fields(&fixup);
4070 }
4071 
4072 int java_lang_Class::_klass_offset;
4073 int java_lang_Class::_array_klass_offset;
4074 int java_lang_Class::_oop_size_offset;
4075 int java_lang_Class::_static_oop_field_count_offset;
4076 int java_lang_Class::_class_loader_offset;
4077 int java_lang_Class::_module_offset;
4078 int java_lang_Class::_protection_domain_offset;
4079 int java_lang_Class::_component_mirror_offset;
4080 int java_lang_Class::_init_lock_offset;
4081 int java_lang_Class::_signers_offset;
4082 int java_lang_Class::_name_offset;
4083 int java_lang_Class::_source_file_offset;
4084 int java_lang_Class::_classData_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 }