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