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