rev 50073 : [mq]: cont

   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 "interpreter/linkResolver.hpp"
  37 #include "logging/log.hpp"
  38 #include "logging/logStream.hpp"
  39 #include "memory/oopFactory.hpp"
  40 #include "memory/metaspaceShared.hpp"
  41 #include "memory/resourceArea.hpp"
  42 #include "memory/universe.hpp"
  43 #include "oops/fieldStreams.hpp"
  44 #include "oops/instanceKlass.hpp"
  45 #include "oops/instanceMirrorKlass.hpp"
  46 #include "oops/klass.hpp"
  47 #include "oops/method.inline.hpp"
  48 #include "oops/objArrayOop.inline.hpp"
  49 #include "oops/oop.inline.hpp"
  50 #include "oops/symbol.hpp"
  51 #include "oops/typeArrayOop.inline.hpp"
  52 #include "prims/resolvedMethodTable.hpp"
  53 #include "runtime/fieldDescriptor.hpp"
  54 #include "runtime/frame.inline.hpp"
  55 #include "runtime/handles.inline.hpp"
  56 #include "runtime/interfaceSupport.inline.hpp"
  57 #include "runtime/java.hpp"
  58 #include "runtime/javaCalls.hpp"
  59 #include "runtime/jniHandles.inline.hpp"
  60 #include "runtime/safepoint.hpp"
  61 #include "runtime/safepointVerifiers.hpp"
  62 #include "runtime/thread.inline.hpp"
  63 #include "runtime/vframe.inline.hpp"
  64 #include "utilities/align.hpp"
  65 #include "utilities/preserveException.hpp"
  66 
  67 #if INCLUDE_JVMCI
  68 #include "jvmci/jvmciJavaClasses.hpp"
  69 #endif
  70 
  71 #define INJECTED_FIELD_COMPUTE_OFFSET(klass, name, signature, may_be_java)    \
  72   klass::_##name##_offset = JavaClasses::compute_injected_offset(JavaClasses::klass##_##name##_enum);
  73 
  74 #if INCLUDE_CDS
  75 #define INJECTED_FIELD_SERIALIZE_OFFSET(klass, name, signature, may_be_java) \
  76   f->do_u4((u4*)&_##name##_offset);
  77 #endif
  78 
  79 #define DECLARE_INJECTED_FIELD(klass, name, signature, may_be_java)           \
  80   { SystemDictionary::WK_KLASS_ENUM_NAME(klass), vmSymbols::VM_SYMBOL_ENUM_NAME(name##_name), vmSymbols::VM_SYMBOL_ENUM_NAME(signature), may_be_java },
  81 
  82 InjectedField JavaClasses::_injected_fields[] = {
  83   ALL_INJECTED_FIELDS(DECLARE_INJECTED_FIELD)
  84 };
  85 
  86 int JavaClasses::compute_injected_offset(InjectedFieldID id) {
  87   return _injected_fields[id].compute_offset();
  88 }
  89 
  90 
  91 InjectedField* JavaClasses::get_injected(Symbol* class_name, int* field_count) {
  92   *field_count = 0;
  93 
  94   vmSymbols::SID sid = vmSymbols::find_sid(class_name);
  95   if (sid == vmSymbols::NO_SID) {
  96     // Only well known classes can inject fields
  97     return NULL;
  98   }
  99 
 100   int count = 0;
 101   int start = -1;
 102 
 103 #define LOOKUP_INJECTED_FIELD(klass, name, signature, may_be_java) \
 104   if (sid == vmSymbols::VM_SYMBOL_ENUM_NAME(klass)) {              \
 105     count++;                                                       \
 106     if (start == -1) start = klass##_##name##_enum;                \
 107   }
 108   ALL_INJECTED_FIELDS(LOOKUP_INJECTED_FIELD);
 109 #undef LOOKUP_INJECTED_FIELD
 110 
 111   if (start != -1) {
 112     *field_count = count;
 113     return _injected_fields + start;
 114   }
 115   return NULL;
 116 }
 117 
 118 
 119 // Helpful routine for computing field offsets at run time rather than hardcoding them
 120 // Finds local fields only, including static fields.  Static field offsets are from the
 121 // beginning of the mirror.
 122 static void compute_offset(int &dest_offset,
 123                            InstanceKlass* ik, Symbol* name_symbol, Symbol* signature_symbol,
 124                            bool is_static = false) {
 125   fieldDescriptor fd;
 126   if (ik == NULL) {
 127     ResourceMark rm;
 128     log_error(class)("Mismatch JDK version for field: %s type: %s", name_symbol->as_C_string(), signature_symbol->as_C_string());
 129     vm_exit_during_initialization("Invalid layout of preloaded class");
 130   }
 131 
 132   if (!ik->find_local_field(name_symbol, signature_symbol, &fd) || fd.is_static() != is_static) {
 133     ResourceMark rm;
 134     log_error(class)("Invalid layout of %s field: %s type: %s", ik->external_name(),
 135                      name_symbol->as_C_string(), signature_symbol->as_C_string());
 136 #ifndef PRODUCT
 137     // Prints all fields and offsets
 138     Log(class) lt;
 139     LogStream ls(lt.error());
 140     ik->print_on(&ls);
 141 #endif //PRODUCT
 142     vm_exit_during_initialization("Invalid layout of preloaded class: use -Xlog:class+load=info to see the origin of the problem class");
 143   }
 144   dest_offset = fd.offset();
 145 }
 146 
 147 // Overloading to pass name as a string.
 148 static void compute_offset(int& dest_offset, InstanceKlass* ik,
 149                            const char* name_string, Symbol* signature_symbol,
 150                            bool is_static = false) {
 151   TempNewSymbol name = SymbolTable::probe(name_string, (int)strlen(name_string));
 152   if (name == NULL) {
 153     ResourceMark rm;
 154     log_error(class)("Name %s should be in the SymbolTable since its class is loaded", name_string);
 155     vm_exit_during_initialization("Invalid layout of preloaded class", ik->external_name());
 156   }
 157   compute_offset(dest_offset, ik, name, signature_symbol, is_static);
 158 }
 159 
 160 // Same as above but for "optional" offsets that might not be present in certain JDK versions
 161 // Old versions should be cleaned out since Hotspot only supports the current JDK, and this
 162 // function should be removed.
 163 static void
 164 compute_optional_offset(int& dest_offset,
 165                         InstanceKlass* ik, Symbol* name_symbol, Symbol* signature_symbol) {
 166   fieldDescriptor fd;
 167   if (ik->find_local_field(name_symbol, signature_symbol, &fd)) {
 168     dest_offset = fd.offset();
 169   }
 170 }
 171 
 172 int java_lang_String::value_offset  = 0;
 173 int java_lang_String::hash_offset   = 0;
 174 int java_lang_String::coder_offset  = 0;
 175 
 176 bool java_lang_String::initialized  = false;
 177 
 178 bool java_lang_String::is_instance(oop obj) {
 179   return is_instance_inlined(obj);
 180 }
 181 
 182 #if INCLUDE_CDS
 183 #define FIELD_SERIALIZE_OFFSET(offset, klass, name, signature, is_static) \
 184   f->do_u4((u4*)&offset)
 185 
 186 #define FIELD_SERIALIZE_OFFSET_OPTIONAL(offset, klass, name, signature) \
 187   f->do_u4((u4*)&offset)
 188 #endif
 189 
 190 #define FIELD_COMPUTE_OFFSET(offset, klass, name, signature, is_static) \
 191   compute_offset(offset, klass, name, vmSymbols::signature(), is_static)
 192 
 193 #define FIELD_COMPUTE_OFFSET_OPTIONAL(offset, klass, name, signature) \
 194   compute_optional_offset(offset, klass, name, vmSymbols::signature())
 195 
 196 #define STRING_FIELDS_DO(macro) \
 197   macro(value_offset, k, vmSymbols::value_name(), byte_array_signature, false); \
 198   macro(hash_offset,  k, "hash",                  int_signature,        false); \
 199   macro(coder_offset, k, "coder",                 byte_signature,       false)
 200 
 201 void java_lang_String::compute_offsets() {
 202   if (initialized) {
 203     return;
 204   }
 205 
 206   InstanceKlass* k = SystemDictionary::String_klass();
 207   STRING_FIELDS_DO(FIELD_COMPUTE_OFFSET);
 208 
 209   initialized = true;
 210 }
 211 
 212 #if INCLUDE_CDS
 213 void java_lang_String::serialize(SerializeClosure* f) {
 214   STRING_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
 215   f->do_u4((u4*)&initialized);
 216 }
 217 #endif
 218 
 219 class CompactStringsFixup : public FieldClosure {
 220 private:
 221   bool _value;
 222 
 223 public:
 224   CompactStringsFixup(bool value) : _value(value) {}
 225 
 226   void do_field(fieldDescriptor* fd) {
 227     if (fd->name() == vmSymbols::compact_strings_name()) {
 228       oop mirror = fd->field_holder()->java_mirror();
 229       assert(fd->field_holder() == SystemDictionary::String_klass(), "Should be String");
 230       assert(mirror != NULL, "String must have mirror already");
 231       mirror->bool_field_put(fd->offset(), _value);
 232     }
 233   }
 234 };
 235 
 236 void java_lang_String::set_compact_strings(bool value) {
 237   CompactStringsFixup fix(value);
 238   InstanceKlass::cast(SystemDictionary::String_klass())->do_local_static_fields(&fix);
 239 }
 240 
 241 Handle java_lang_String::basic_create(int length, bool is_latin1, TRAPS) {
 242   assert(initialized, "Must be initialized");
 243   assert(CompactStrings || !is_latin1, "Must be UTF16 without CompactStrings");
 244 
 245   // Create the String object first, so there's a chance that the String
 246   // and the char array it points to end up in the same cache line.
 247   oop obj;
 248   obj = SystemDictionary::String_klass()->allocate_instance(CHECK_NH);
 249 
 250   // Create the char array.  The String object must be handlized here
 251   // because GC can happen as a result of the allocation attempt.
 252   Handle h_obj(THREAD, obj);
 253   int arr_length = is_latin1 ? length : length << 1; // 2 bytes per UTF16.
 254   typeArrayOop buffer = oopFactory::new_byteArray(arr_length, CHECK_NH);;
 255 
 256   // Point the String at the char array
 257   obj = h_obj();
 258   set_value(obj, buffer);
 259   // No need to zero the offset, allocation zero'ed the entire String object
 260   set_coder(obj, is_latin1 ? CODER_LATIN1 : CODER_UTF16);
 261   return h_obj;
 262 }
 263 
 264 Handle java_lang_String::create_from_unicode(jchar* unicode, int length, TRAPS) {
 265   bool is_latin1 = CompactStrings && UNICODE::is_latin1(unicode, length);
 266   Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
 267   typeArrayOop buffer = value(h_obj());
 268   assert(TypeArrayKlass::cast(buffer->klass())->element_type() == T_BYTE, "only byte[]");
 269   if (is_latin1) {
 270     for (int index = 0; index < length; index++) {
 271       buffer->byte_at_put(index, (jbyte)unicode[index]);
 272     }
 273   } else {
 274     for (int index = 0; index < length; index++) {
 275       buffer->char_at_put(index, unicode[index]);
 276     }
 277   }
 278 
 279 #ifdef ASSERT
 280   {
 281     ResourceMark rm;
 282     char* expected = UNICODE::as_utf8(unicode, length);
 283     char* actual = as_utf8_string(h_obj());
 284     if (strcmp(expected, actual) != 0) {
 285       tty->print_cr("Unicode conversion failure: %s --> %s", expected, actual);
 286       ShouldNotReachHere();
 287     }
 288   }
 289 #endif
 290 
 291   return h_obj;
 292 }
 293 
 294 oop java_lang_String::create_oop_from_unicode(jchar* unicode, int length, TRAPS) {
 295   Handle h_obj = create_from_unicode(unicode, length, CHECK_0);
 296   return h_obj();
 297 }
 298 
 299 Handle java_lang_String::create_from_str(const char* utf8_str, TRAPS) {
 300   if (utf8_str == NULL) {
 301     return Handle();
 302   }
 303   bool has_multibyte, is_latin1;
 304   int length = UTF8::unicode_length(utf8_str, is_latin1, has_multibyte);
 305   if (!CompactStrings) {
 306     has_multibyte = true;
 307     is_latin1 = false;
 308   }
 309 
 310   Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
 311   if (length > 0) {
 312     if (!has_multibyte) {
 313       strncpy((char*)value(h_obj())->byte_at_addr(0), utf8_str, length);
 314     } else if (is_latin1) {
 315       UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length);
 316     } else {
 317       UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length);
 318     }
 319   }
 320 
 321 #ifdef ASSERT
 322   // This check is too strict because the input string is not necessarily valid UTF8.
 323   // For example, it may be created with arbitrary content via jni_NewStringUTF.
 324   /*
 325   {
 326     ResourceMark rm;
 327     const char* expected = utf8_str;
 328     char* actual = as_utf8_string(h_obj());
 329     if (strcmp(expected, actual) != 0) {
 330       tty->print_cr("String conversion failure: %s --> %s", expected, actual);
 331       ShouldNotReachHere();
 332     }
 333   }
 334   */
 335 #endif
 336 
 337   return h_obj;
 338 }
 339 
 340 oop java_lang_String::create_oop_from_str(const char* utf8_str, TRAPS) {
 341   Handle h_obj = create_from_str(utf8_str, CHECK_0);
 342   return h_obj();
 343 }
 344 
 345 Handle java_lang_String::create_from_symbol(Symbol* symbol, TRAPS) {
 346   const char* utf8_str = (char*)symbol->bytes();
 347   int utf8_len = symbol->utf8_length();
 348 
 349   bool has_multibyte, is_latin1;
 350   int length = UTF8::unicode_length(utf8_str, utf8_len, is_latin1, has_multibyte);
 351   if (!CompactStrings) {
 352     has_multibyte = true;
 353     is_latin1 = false;
 354   }
 355 
 356   Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
 357   if (length > 0) {
 358     if (!has_multibyte) {
 359       strncpy((char*)value(h_obj())->byte_at_addr(0), utf8_str, length);
 360     } else if (is_latin1) {
 361       UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length);
 362     } else {
 363       UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length);
 364     }
 365   }
 366 
 367 #ifdef ASSERT
 368   {
 369     ResourceMark rm;
 370     const char* expected = symbol->as_utf8();
 371     char* actual = as_utf8_string(h_obj());
 372     if (strncmp(expected, actual, utf8_len) != 0) {
 373       tty->print_cr("Symbol conversion failure: %s --> %s", expected, actual);
 374       ShouldNotReachHere();
 375     }
 376   }
 377 #endif
 378 
 379   return h_obj;
 380 }
 381 
 382 // Converts a C string to a Java String based on current encoding
 383 Handle java_lang_String::create_from_platform_dependent_str(const char* str, TRAPS) {
 384   assert(str != NULL, "bad arguments");
 385 
 386   typedef jstring (*to_java_string_fn_t)(JNIEnv*, const char *);
 387   static to_java_string_fn_t _to_java_string_fn = NULL;
 388 
 389   if (_to_java_string_fn == NULL) {
 390     void *lib_handle = os::native_java_library();
 391     _to_java_string_fn = CAST_TO_FN_PTR(to_java_string_fn_t, os::dll_lookup(lib_handle, "NewStringPlatform"));
 392     if (_to_java_string_fn == NULL) {
 393       fatal("NewStringPlatform missing");
 394     }
 395   }
 396 
 397   jstring js = NULL;
 398   { JavaThread* thread = (JavaThread*)THREAD;
 399     assert(thread->is_Java_thread(), "must be java thread");
 400     HandleMark hm(thread);
 401     ThreadToNativeFromVM ttn(thread);
 402     js = (_to_java_string_fn)(thread->jni_environment(), str);
 403   }
 404   return Handle(THREAD, JNIHandles::resolve(js));
 405 }
 406 
 407 // Converts a Java String to a native C string that can be used for
 408 // native OS calls.
 409 char* java_lang_String::as_platform_dependent_str(Handle java_string, TRAPS) {
 410   typedef char* (*to_platform_string_fn_t)(JNIEnv*, jstring, bool*);
 411   static to_platform_string_fn_t _to_platform_string_fn = NULL;
 412 
 413   if (_to_platform_string_fn == NULL) {
 414     void *lib_handle = os::native_java_library();
 415     _to_platform_string_fn = CAST_TO_FN_PTR(to_platform_string_fn_t, os::dll_lookup(lib_handle, "GetStringPlatformChars"));
 416     if (_to_platform_string_fn == NULL) {
 417       fatal("GetStringPlatformChars missing");
 418     }
 419   }
 420 
 421   char *native_platform_string;
 422   { JavaThread* thread = (JavaThread*)THREAD;
 423     assert(thread->is_Java_thread(), "must be java thread");
 424     JNIEnv *env = thread->jni_environment();
 425     jstring js = (jstring) JNIHandles::make_local(env, java_string());
 426     bool is_copy;
 427     HandleMark hm(thread);
 428     ThreadToNativeFromVM ttn(thread);
 429     native_platform_string = (_to_platform_string_fn)(env, js, &is_copy);
 430     assert(is_copy == JNI_TRUE, "is_copy value changed");
 431     JNIHandles::destroy_local(js);
 432   }
 433   return native_platform_string;
 434 }
 435 
 436 Handle java_lang_String::char_converter(Handle java_string, jchar from_char, jchar to_char, TRAPS) {
 437   oop          obj    = java_string();
 438   // Typical usage is to convert all '/' to '.' in string.
 439   typeArrayOop value  = java_lang_String::value(obj);
 440   int          length = java_lang_String::length(obj);
 441   bool      is_latin1 = java_lang_String::is_latin1(obj);
 442 
 443   // First check if any from_char exist
 444   int index; // Declared outside, used later
 445   for (index = 0; index < length; index++) {
 446     jchar c = !is_latin1 ? value->char_at(index) :
 447                   ((jchar) value->byte_at(index)) & 0xff;
 448     if (c == from_char) {
 449       break;
 450     }
 451   }
 452   if (index == length) {
 453     // No from_char, so do not copy.
 454     return java_string;
 455   }
 456 
 457   // Check if result string will be latin1
 458   bool to_is_latin1 = false;
 459 
 460   // Replacement char must be latin1
 461   if (CompactStrings && UNICODE::is_latin1(to_char)) {
 462     if (is_latin1) {
 463       // Source string is latin1 as well
 464       to_is_latin1 = true;
 465     } else if (!UNICODE::is_latin1(from_char)) {
 466       // We are replacing an UTF16 char. Scan string to
 467       // check if result can be latin1 encoded.
 468       to_is_latin1 = true;
 469       for (index = 0; index < length; index++) {
 470         jchar c = value->char_at(index);
 471         if (c != from_char && !UNICODE::is_latin1(c)) {
 472           to_is_latin1 = false;
 473           break;
 474         }
 475       }
 476     }
 477   }
 478 
 479   // Create new UNICODE (or byte) buffer. Must handlize value because GC
 480   // may happen during String and char array creation.
 481   typeArrayHandle h_value(THREAD, value);
 482   Handle string = basic_create(length, to_is_latin1, CHECK_NH);
 483   typeArrayOop from_buffer = h_value();
 484   typeArrayOop to_buffer = java_lang_String::value(string());
 485 
 486   // Copy contents
 487   for (index = 0; index < length; index++) {
 488     jchar c = (!is_latin1) ? from_buffer->char_at(index) :
 489                     ((jchar) from_buffer->byte_at(index)) & 0xff;
 490     if (c == from_char) {
 491       c = to_char;
 492     }
 493     if (!to_is_latin1) {
 494       to_buffer->char_at_put(index, c);
 495     } else {
 496       to_buffer->byte_at_put(index, (jbyte) c);
 497     }
 498   }
 499   return string;
 500 }
 501 
 502 jchar* java_lang_String::as_unicode_string(oop java_string, int& length, TRAPS) {
 503   typeArrayOop value  = java_lang_String::value(java_string);
 504                length = java_lang_String::length(java_string);
 505   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 506 
 507   jchar* result = NEW_RESOURCE_ARRAY_RETURN_NULL(jchar, length);
 508   if (result != NULL) {
 509     if (!is_latin1) {
 510       for (int index = 0; index < length; index++) {
 511         result[index] = value->char_at(index);
 512       }
 513     } else {
 514       for (int index = 0; index < length; index++) {
 515         result[index] = ((jchar) value->byte_at(index)) & 0xff;
 516       }
 517     }
 518   } else {
 519     THROW_MSG_0(vmSymbols::java_lang_OutOfMemoryError(), "could not allocate Unicode string");
 520   }
 521   return result;
 522 }
 523 
 524 unsigned int java_lang_String::hash_code(oop java_string) {
 525   int          length = java_lang_String::length(java_string);
 526   // Zero length string will hash to zero with String.hashCode() function.
 527   if (length == 0) return 0;
 528 
 529   typeArrayOop value  = java_lang_String::value(java_string);
 530   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 531 
 532   if (is_latin1) {
 533     return java_lang_String::hash_code(value->byte_at_addr(0), length);
 534   } else {
 535     return java_lang_String::hash_code(value->char_at_addr(0), length);
 536   }
 537 }
 538 
 539 char* java_lang_String::as_quoted_ascii(oop java_string) {
 540   typeArrayOop value  = java_lang_String::value(java_string);
 541   int          length = java_lang_String::length(java_string);
 542   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 543 
 544   if (length == 0) return NULL;
 545 
 546   char* result;
 547   int result_length;
 548   if (!is_latin1) {
 549     jchar* base = value->char_at_addr(0);
 550     result_length = UNICODE::quoted_ascii_length(base, length) + 1;
 551     result = NEW_RESOURCE_ARRAY(char, result_length);
 552     UNICODE::as_quoted_ascii(base, length, result, result_length);
 553   } else {
 554     jbyte* base = value->byte_at_addr(0);
 555     result_length = UNICODE::quoted_ascii_length(base, length) + 1;
 556     result = NEW_RESOURCE_ARRAY(char, result_length);
 557     UNICODE::as_quoted_ascii(base, length, result, result_length);
 558   }
 559   assert(result_length >= length + 1, "must not be shorter");
 560   assert(result_length == (int)strlen(result) + 1, "must match");
 561   return result;
 562 }
 563 
 564 Symbol* java_lang_String::as_symbol(oop java_string, TRAPS) {
 565   typeArrayOop value  = java_lang_String::value(java_string);
 566   int          length = java_lang_String::length(java_string);
 567   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 568   if (!is_latin1) {
 569     jchar* base = (length == 0) ? NULL : value->char_at_addr(0);
 570     Symbol* sym = SymbolTable::lookup_unicode(base, length, THREAD);
 571     return sym;
 572   } else {
 573     ResourceMark rm;
 574     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 575     const char* base = UNICODE::as_utf8(position, length);
 576     Symbol* sym = SymbolTable::lookup(base, length, THREAD);
 577     return sym;
 578   }
 579 }
 580 
 581 Symbol* java_lang_String::as_symbol_or_null(oop java_string) {
 582   typeArrayOop value  = java_lang_String::value(java_string);
 583   int          length = java_lang_String::length(java_string);
 584   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 585   if (!is_latin1) {
 586     jchar* base = (length == 0) ? NULL : value->char_at_addr(0);
 587     return SymbolTable::probe_unicode(base, length);
 588   } else {
 589     ResourceMark rm;
 590     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 591     const char* base = UNICODE::as_utf8(position, length);
 592     return SymbolTable::probe(base, length);
 593   }
 594 }
 595 
 596 int java_lang_String::utf8_length(oop java_string) {
 597   typeArrayOop value  = java_lang_String::value(java_string);
 598   int          length = java_lang_String::length(java_string);
 599   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 600   if (length == 0) {
 601     return 0;
 602   }
 603   if (!is_latin1) {
 604     return UNICODE::utf8_length(value->char_at_addr(0), length);
 605   } else {
 606     return UNICODE::utf8_length(value->byte_at_addr(0), length);
 607   }
 608 }
 609 
 610 char* java_lang_String::as_utf8_string(oop java_string) {
 611   typeArrayOop value  = java_lang_String::value(java_string);
 612   int          length = java_lang_String::length(java_string);
 613   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 614   if (!is_latin1) {
 615     jchar* position = (length == 0) ? NULL : value->char_at_addr(0);
 616     return UNICODE::as_utf8(position, length);
 617   } else {
 618     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 619     return UNICODE::as_utf8(position, length);
 620   }
 621 }
 622 
 623 char* java_lang_String::as_utf8_string(oop java_string, char* buf, int buflen) {
 624   typeArrayOop value  = java_lang_String::value(java_string);
 625   int          length = java_lang_String::length(java_string);
 626   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 627   if (!is_latin1) {
 628     jchar* position = (length == 0) ? NULL : value->char_at_addr(0);
 629     return UNICODE::as_utf8(position, length, buf, buflen);
 630   } else {
 631     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 632     return UNICODE::as_utf8(position, length, buf, buflen);
 633   }
 634 }
 635 
 636 char* java_lang_String::as_utf8_string(oop java_string, int start, int len) {
 637   typeArrayOop value  = java_lang_String::value(java_string);
 638   int          length = java_lang_String::length(java_string);
 639   assert(start + len <= length, "just checking");
 640   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 641   if (!is_latin1) {
 642     jchar* position = value->char_at_addr(start);
 643     return UNICODE::as_utf8(position, len);
 644   } else {
 645     jbyte* position = value->byte_at_addr(start);
 646     return UNICODE::as_utf8(position, len);
 647   }
 648 }
 649 
 650 char* java_lang_String::as_utf8_string(oop java_string, int start, int len, char* buf, int buflen) {
 651   typeArrayOop value  = java_lang_String::value(java_string);
 652   int          length = java_lang_String::length(java_string);
 653   assert(start + len <= length, "just checking");
 654   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 655   if (!is_latin1) {
 656     jchar* position = value->char_at_addr(start);
 657     return UNICODE::as_utf8(position, len, buf, buflen);
 658   } else {
 659     jbyte* position = value->byte_at_addr(start);
 660     return UNICODE::as_utf8(position, len, buf, buflen);
 661   }
 662 }
 663 
 664 bool java_lang_String::equals(oop java_string, jchar* chars, int len) {
 665   assert(java_string->klass() == SystemDictionary::String_klass(),
 666          "must be java_string");
 667   typeArrayOop value = java_lang_String::value_no_keepalive(java_string);
 668   int length = java_lang_String::length(java_string);
 669   if (length != len) {
 670     return false;
 671   }
 672   bool is_latin1 = java_lang_String::is_latin1(java_string);
 673   if (!is_latin1) {
 674     for (int i = 0; i < len; i++) {
 675       if (value->char_at(i) != chars[i]) {
 676         return false;
 677       }
 678     }
 679   } else {
 680     for (int i = 0; i < len; i++) {
 681       if ((((jchar) value->byte_at(i)) & 0xff) != chars[i]) {
 682         return false;
 683       }
 684     }
 685   }
 686   return true;
 687 }
 688 
 689 bool java_lang_String::equals(oop str1, oop str2) {
 690   assert(str1->klass() == SystemDictionary::String_klass(),
 691          "must be java String");
 692   assert(str2->klass() == SystemDictionary::String_klass(),
 693          "must be java String");
 694   typeArrayOop value1    = java_lang_String::value_no_keepalive(str1);
 695   int          length1   = java_lang_String::length(str1);
 696   bool         is_latin1 = java_lang_String::is_latin1(str1);
 697   typeArrayOop value2    = java_lang_String::value_no_keepalive(str2);
 698   int          length2   = java_lang_String::length(str2);
 699   bool         is_latin2 = java_lang_String::is_latin1(str2);
 700 
 701   if ((length1 != length2) || (is_latin1 != is_latin2)) {
 702     // Strings of different size or with different
 703     // coders are never equal.
 704     return false;
 705   }
 706   int blength1 = value1->length();
 707   for (int i = 0; i < blength1; i++) {
 708     if (value1->byte_at(i) != value2->byte_at(i)) {
 709       return false;
 710     }
 711   }
 712   return true;
 713 }
 714 
 715 void java_lang_String::print(oop java_string, outputStream* st) {
 716   assert(java_string->klass() == SystemDictionary::String_klass(), "must be java_string");
 717   typeArrayOop value  = java_lang_String::value_no_keepalive(java_string);
 718 
 719   if (value == NULL) {
 720     // This can happen if, e.g., printing a String
 721     // object before its initializer has been called
 722     st->print("NULL");
 723     return;
 724   }
 725 
 726   int length = java_lang_String::length(java_string);
 727   bool is_latin1 = java_lang_String::is_latin1(java_string);
 728 
 729   st->print("\"");
 730   for (int index = 0; index < length; index++) {
 731     st->print("%c", (!is_latin1) ?  value->char_at(index) :
 732                            ((jchar) value->byte_at(index)) & 0xff );
 733   }
 734   st->print("\"");
 735 }
 736 
 737 
 738 static void initialize_static_field(fieldDescriptor* fd, Handle mirror, TRAPS) {
 739   assert(mirror.not_null() && fd->is_static(), "just checking");
 740   if (fd->has_initial_value()) {
 741     BasicType t = fd->field_type();
 742     switch (t) {
 743       case T_BYTE:
 744         mirror()->byte_field_put(fd->offset(), fd->int_initial_value());
 745               break;
 746       case T_BOOLEAN:
 747         mirror()->bool_field_put(fd->offset(), fd->int_initial_value());
 748               break;
 749       case T_CHAR:
 750         mirror()->char_field_put(fd->offset(), fd->int_initial_value());
 751               break;
 752       case T_SHORT:
 753         mirror()->short_field_put(fd->offset(), fd->int_initial_value());
 754               break;
 755       case T_INT:
 756         mirror()->int_field_put(fd->offset(), fd->int_initial_value());
 757         break;
 758       case T_FLOAT:
 759         mirror()->float_field_put(fd->offset(), fd->float_initial_value());
 760         break;
 761       case T_DOUBLE:
 762         mirror()->double_field_put(fd->offset(), fd->double_initial_value());
 763         break;
 764       case T_LONG:
 765         mirror()->long_field_put(fd->offset(), fd->long_initial_value());
 766         break;
 767       case T_OBJECT:
 768         {
 769           assert(fd->signature() == vmSymbols::string_signature(),
 770                  "just checking");
 771           if (DumpSharedSpaces && MetaspaceShared::is_archive_object(mirror())) {
 772             // Archive the String field and update the pointer.
 773             oop s = mirror()->obj_field(fd->offset());
 774             oop archived_s = StringTable::create_archived_string(s, CHECK);
 775             mirror()->obj_field_put(fd->offset(), archived_s);
 776           } else {
 777             oop string = fd->string_initial_value(CHECK);
 778             mirror()->obj_field_put(fd->offset(), string);
 779           }
 780         }
 781         break;
 782       default:
 783         THROW_MSG(vmSymbols::java_lang_ClassFormatError(),
 784                   "Illegal ConstantValue attribute in class file");
 785     }
 786   }
 787 }
 788 
 789 
 790 void java_lang_Class::fixup_mirror(Klass* k, TRAPS) {
 791   assert(InstanceMirrorKlass::offset_of_static_fields() != 0, "must have been computed already");
 792 
 793   // If the offset was read from the shared archive, it was fixed up already
 794   if (!k->is_shared()) {
 795     if (k->is_instance_klass()) {
 796       // During bootstrap, java.lang.Class wasn't loaded so static field
 797       // offsets were computed without the size added it.  Go back and
 798       // update all the static field offsets to included the size.
 799         for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) {
 800         if (fs.access_flags().is_static()) {
 801           int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields();
 802           fs.set_offset(real_offset);
 803         }
 804       }
 805     }
 806   }
 807 
 808   if (k->is_shared() && k->has_raw_archived_mirror()) {
 809     if (MetaspaceShared::open_archive_heap_region_mapped()) {
 810       oop m = k->archived_java_mirror();
 811       assert(m != NULL, "archived mirror is NULL");
 812       assert(MetaspaceShared::is_archive_object(m), "must be archived mirror object");
 813       Handle m_h(THREAD, m);
 814       // restore_archived_mirror() clears the klass' _has_raw_archived_mirror flag
 815       restore_archived_mirror(k, m_h, Handle(), Handle(), Handle(), CHECK);
 816       return;
 817     } else {
 818       k->set_java_mirror_handle(NULL);
 819       k->clear_has_raw_archived_mirror();
 820     }
 821   }
 822   create_mirror(k, Handle(), Handle(), Handle(), CHECK);
 823 }
 824 
 825 void java_lang_Class::initialize_mirror_fields(Klass* k,
 826                                                Handle mirror,
 827                                                Handle protection_domain,
 828                                                TRAPS) {
 829   // Allocate a simple java object for a lock.
 830   // This needs to be a java object because during class initialization
 831   // it can be held across a java call.
 832   typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
 833   set_init_lock(mirror(), r);
 834 
 835   // Set protection domain also
 836   set_protection_domain(mirror(), protection_domain());
 837 
 838   // Initialize static fields
 839   InstanceKlass::cast(k)->do_local_static_fields(&initialize_static_field, mirror, CHECK);
 840 }
 841 
 842 // Set the java.lang.Module module field in the java_lang_Class mirror
 843 void java_lang_Class::set_mirror_module_field(Klass* k, Handle mirror, Handle module, TRAPS) {
 844   if (module.is_null()) {
 845     // During startup, the module may be NULL only if java.base has not been defined yet.
 846     // Put the class on the fixup_module_list to patch later when the java.lang.Module
 847     // for java.base is known.
 848     assert(!Universe::is_module_initialized(), "Incorrect java.lang.Module pre module system initialization");
 849 
 850     bool javabase_was_defined = false;
 851     {
 852       MutexLocker m1(Module_lock, THREAD);
 853       // Keep list of classes needing java.base module fixup
 854       if (!ModuleEntryTable::javabase_defined()) {
 855         assert(k->java_mirror() != NULL, "Class's mirror is null");
 856         k->class_loader_data()->inc_keep_alive();
 857         assert(fixup_module_field_list() != NULL, "fixup_module_field_list not initialized");
 858         fixup_module_field_list()->push(k);
 859       } else {
 860         javabase_was_defined = true;
 861       }
 862     }
 863 
 864     // If java.base was already defined then patch this particular class with java.base.
 865     if (javabase_was_defined) {
 866       ModuleEntry *javabase_entry = ModuleEntryTable::javabase_moduleEntry();
 867       assert(javabase_entry != NULL && javabase_entry->module() != NULL,
 868              "Setting class module field, " JAVA_BASE_NAME " should be defined");
 869       Handle javabase_handle(THREAD, javabase_entry->module());
 870       set_module(mirror(), javabase_handle());
 871     }
 872   } else {
 873     assert(Universe::is_module_initialized() ||
 874            (ModuleEntryTable::javabase_defined() &&
 875             (oopDesc::equals(module(), ModuleEntryTable::javabase_moduleEntry()->module()))),
 876            "Incorrect java.lang.Module specification while creating mirror");
 877     set_module(mirror(), module());
 878   }
 879 }
 880 
 881 // Statically allocate fixup lists because they always get created.
 882 void java_lang_Class::allocate_fixup_lists() {
 883   GrowableArray<Klass*>* mirror_list =
 884     new (ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(40, true);
 885   set_fixup_mirror_list(mirror_list);
 886 
 887   GrowableArray<Klass*>* module_list =
 888     new (ResourceObj::C_HEAP, mtModule) GrowableArray<Klass*>(500, true);
 889   set_fixup_module_field_list(module_list);
 890 }
 891 
 892 void java_lang_Class::create_mirror(Klass* k, Handle class_loader,
 893                                     Handle module, Handle protection_domain, TRAPS) {
 894   assert(k != NULL, "Use create_basic_type_mirror for primitive types");
 895   assert(k->java_mirror() == NULL, "should only assign mirror once");
 896 
 897   // Use this moment of initialization to cache modifier_flags also,
 898   // to support Class.getModifiers().  Instance classes recalculate
 899   // the cached flags after the class file is parsed, but before the
 900   // class is put into the system dictionary.
 901   int computed_modifiers = k->compute_modifier_flags(CHECK);
 902   k->set_modifier_flags(computed_modifiers);
 903   // Class_klass has to be loaded because it is used to allocate
 904   // the mirror.
 905   if (SystemDictionary::Class_klass_loaded()) {
 906     // Allocate mirror (java.lang.Class instance)
 907     oop mirror_oop = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK);
 908     Handle mirror(THREAD, mirror_oop);
 909     Handle comp_mirror;
 910 
 911     // Setup indirection from mirror->klass
 912     java_lang_Class::set_klass(mirror(), k);
 913 
 914     InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
 915     assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");
 916 
 917     java_lang_Class::set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));
 918 
 919     // It might also have a component mirror.  This mirror must already exist.
 920     if (k->is_array_klass()) {
 921       if (k->is_typeArray_klass()) {
 922         BasicType type = TypeArrayKlass::cast(k)->element_type();
 923         comp_mirror = Handle(THREAD, Universe::java_mirror(type));
 924       } else {
 925         assert(k->is_objArray_klass(), "Must be");
 926         Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
 927         assert(element_klass != NULL, "Must have an element klass");
 928         comp_mirror = Handle(THREAD, element_klass->java_mirror());
 929       }
 930       assert(comp_mirror() != NULL, "must have a mirror");
 931 
 932       // Two-way link between the array klass and its component mirror:
 933       // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
 934       set_component_mirror(mirror(), comp_mirror());
 935       // See below for ordering dependencies between field array_klass in component mirror
 936       // and java_mirror in this klass.
 937     } else {
 938       assert(k->is_instance_klass(), "Must be");
 939 
 940       initialize_mirror_fields(k, mirror, protection_domain, THREAD);
 941       if (HAS_PENDING_EXCEPTION) {
 942         // If any of the fields throws an exception like OOM remove the klass field
 943         // from the mirror so GC doesn't follow it after the klass has been deallocated.
 944         // This mirror looks like a primitive type, which logically it is because it
 945         // it represents no class.
 946         java_lang_Class::set_klass(mirror(), NULL);
 947         return;
 948       }
 949     }
 950 
 951     // set the classLoader field in the java_lang_Class instance
 952     assert(oopDesc::equals(class_loader(), k->class_loader()), "should be same");
 953     set_class_loader(mirror(), class_loader());
 954 
 955     // Setup indirection from klass->mirror
 956     // after any exceptions can happen during allocations.
 957     k->set_java_mirror(mirror);
 958 
 959     // Set the module field in the java_lang_Class instance.  This must be done
 960     // after the mirror is set.
 961     set_mirror_module_field(k, mirror, module, THREAD);
 962 
 963     if (comp_mirror() != NULL) {
 964       // Set after k->java_mirror() is published, because compiled code running
 965       // concurrently doesn't expect a k to have a null java_mirror.
 966       release_set_array_klass(comp_mirror(), k);
 967     }
 968   } else {
 969     assert(fixup_mirror_list() != NULL, "fixup_mirror_list not initialized");
 970     fixup_mirror_list()->push(k);
 971   }
 972 }
 973 
 974 #if INCLUDE_CDS_JAVA_HEAP
 975 // Clears mirror fields. Static final fields with initial values are reloaded
 976 // from constant pool. The object identity hash is in the object header and is
 977 // not affected.
 978 class ResetMirrorField: public FieldClosure {
 979  private:
 980   Handle _m;
 981 
 982  public:
 983   ResetMirrorField(Handle mirror) : _m(mirror) {}
 984 
 985   void do_field(fieldDescriptor* fd) {
 986     assert(DumpSharedSpaces, "dump time only");
 987     assert(_m.not_null(), "Mirror cannot be NULL");
 988 
 989     if (fd->is_static() && fd->has_initial_value()) {
 990       initialize_static_field(fd, _m, Thread::current());
 991       return;
 992     }
 993 
 994     BasicType ft = fd->field_type();
 995     switch (ft) {
 996       case T_BYTE:
 997         _m()->byte_field_put(fd->offset(), 0);
 998         break;
 999       case T_CHAR:
1000         _m()->char_field_put(fd->offset(), 0);
1001         break;
1002       case T_DOUBLE:
1003         _m()->double_field_put(fd->offset(), 0);
1004         break;
1005       case T_FLOAT:
1006         _m()->float_field_put(fd->offset(), 0);
1007         break;
1008       case T_INT:
1009         _m()->int_field_put(fd->offset(), 0);
1010         break;
1011       case T_LONG:
1012         _m()->long_field_put(fd->offset(), 0);
1013         break;
1014       case T_SHORT:
1015         _m()->short_field_put(fd->offset(), 0);
1016         break;
1017       case T_BOOLEAN:
1018         _m()->bool_field_put(fd->offset(), false);
1019         break;
1020       case T_ARRAY:
1021       case T_OBJECT: {
1022         // It might be useful to cache the String field, but
1023         // for now just clear out any reference field
1024         oop o = _m()->obj_field(fd->offset());
1025         _m()->obj_field_put(fd->offset(), NULL);
1026         break;
1027       }
1028       default:
1029         ShouldNotReachHere();
1030         break;
1031      }
1032   }
1033 };
1034 
1035 void java_lang_Class::archive_basic_type_mirrors(TRAPS) {
1036   assert(MetaspaceShared::is_heap_object_archiving_allowed(),
1037          "MetaspaceShared::is_heap_object_archiving_allowed() must be true");
1038 
1039   for (int t = 0; t <= T_VOID; t++) {
1040     oop m = Universe::_mirrors[t];
1041     if (m != NULL) {
1042       // Update the field at _array_klass_offset to point to the relocated array klass.
1043       oop archived_m = MetaspaceShared::archive_heap_object(m, THREAD);
1044       Klass *ak = (Klass*)(archived_m->metadata_field(_array_klass_offset));
1045       assert(ak != NULL || t == T_VOID, "should not be NULL");
1046       if (ak != NULL) {
1047         Klass *reloc_ak = MetaspaceShared::get_relocated_klass(ak);
1048         archived_m->metadata_field_put(_array_klass_offset, reloc_ak);
1049       }
1050 
1051       // Clear the fields. Just to be safe
1052       Klass *k = m->klass();
1053       Handle archived_mirror_h(THREAD, archived_m);
1054       ResetMirrorField reset(archived_mirror_h);
1055       InstanceKlass::cast(k)->do_nonstatic_fields(&reset);
1056 
1057       log_trace(cds, mirror)("Archived %s mirror object from " PTR_FORMAT " ==> " PTR_FORMAT,
1058                              type2name((BasicType)t), p2i(Universe::_mirrors[t]), p2i(archived_m));
1059 
1060       Universe::_mirrors[t] = archived_m;
1061     }
1062   }
1063 
1064   assert(Universe::_mirrors[T_INT] != NULL &&
1065          Universe::_mirrors[T_FLOAT] != NULL &&
1066          Universe::_mirrors[T_DOUBLE] != NULL &&
1067          Universe::_mirrors[T_BYTE] != NULL &&
1068          Universe::_mirrors[T_BOOLEAN] != NULL &&
1069          Universe::_mirrors[T_CHAR] != NULL &&
1070          Universe::_mirrors[T_LONG] != NULL &&
1071          Universe::_mirrors[T_SHORT] != NULL &&
1072          Universe::_mirrors[T_VOID] != NULL, "sanity");
1073 
1074   Universe::set_int_mirror(Universe::_mirrors[T_INT]);
1075   Universe::set_float_mirror(Universe::_mirrors[T_FLOAT]);
1076   Universe::set_double_mirror(Universe::_mirrors[T_DOUBLE]);
1077   Universe::set_byte_mirror(Universe::_mirrors[T_BYTE]);
1078   Universe::set_bool_mirror(Universe::_mirrors[T_BOOLEAN]);
1079   Universe::set_char_mirror(Universe::_mirrors[T_CHAR]);
1080   Universe::set_long_mirror(Universe::_mirrors[T_LONG]);
1081   Universe::set_short_mirror(Universe::_mirrors[T_SHORT]);
1082   Universe::set_void_mirror(Universe::_mirrors[T_VOID]);
1083 }
1084 
1085 //
1086 // After the mirror object is successfully archived, the archived
1087 // klass is set with _has_archived_raw_mirror flag.
1088 //
1089 // The _has_archived_raw_mirror flag is cleared at runtime when the
1090 // archived mirror is restored. If archived java heap data cannot
1091 // be used at runtime, new mirror object is created for the shared
1092 // class. The _has_archived_raw_mirror is cleared also during the process.
1093 oop java_lang_Class::archive_mirror(Klass* k, TRAPS) {
1094   assert(MetaspaceShared::is_heap_object_archiving_allowed(),
1095          "MetaspaceShared::is_heap_object_archiving_allowed() must be true");
1096 
1097   // Mirror is already archived
1098   if (k->has_raw_archived_mirror()) {
1099     assert(k->archived_java_mirror_raw() != NULL, "no archived mirror");
1100     return k->archived_java_mirror_raw();
1101   }
1102 
1103   // No mirror
1104   oop mirror = k->java_mirror();
1105   if (mirror == NULL) {
1106     return NULL;
1107   }
1108 
1109   if (k->is_instance_klass()) {
1110     InstanceKlass *ik = InstanceKlass::cast(k);
1111     assert(ik->signers() == NULL && !k->has_signer_and_not_archived(),
1112            "class with signer cannot be supported");
1113 
1114     if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
1115           ik->is_shared_app_class())) {
1116       // Archiving mirror for classes from non-builtin loaders is not
1117       // supported. Clear the _java_mirror within the archived class.
1118       k->set_java_mirror_handle(NULL);
1119       return NULL;
1120     }
1121   }
1122 
1123   // Now start archiving the mirror object
1124   oop archived_mirror = MetaspaceShared::archive_heap_object(mirror, THREAD);
1125   if (archived_mirror == NULL) {
1126     return NULL;
1127   }
1128 
1129   archived_mirror = process_archived_mirror(k, mirror, archived_mirror, THREAD);
1130   if (archived_mirror == NULL) {
1131     return NULL;
1132   }
1133 
1134   k->set_archived_java_mirror_raw(archived_mirror);
1135 
1136   k->set_has_raw_archived_mirror();
1137 
1138   ResourceMark rm;
1139   log_trace(cds, mirror)("Archived %s mirror object from " PTR_FORMAT " ==> " PTR_FORMAT,
1140                          k->external_name(), p2i(mirror), p2i(archived_mirror));
1141 
1142   return archived_mirror;
1143 }
1144 
1145 // The process is based on create_mirror().
1146 oop java_lang_Class::process_archived_mirror(Klass* k, oop mirror,
1147                                              oop archived_mirror,
1148                                              Thread *THREAD) {
1149   // Clear nonstatic fields in archived mirror. Some of the fields will be set
1150   // to archived metadata and objects below.
1151   Klass *c = archived_mirror->klass();
1152   Handle archived_mirror_h(THREAD, archived_mirror);
1153   ResetMirrorField reset(archived_mirror_h);
1154   InstanceKlass::cast(c)->do_nonstatic_fields(&reset);
1155 
1156   if (k->is_array_klass()) {
1157     oop archived_comp_mirror;
1158     if (k->is_typeArray_klass()) {
1159       // The primitive type mirrors are already archived. Get the archived mirror.
1160       oop comp_mirror = java_lang_Class::component_mirror(mirror);
1161       archived_comp_mirror = MetaspaceShared::find_archived_heap_object(comp_mirror);
1162       assert(archived_comp_mirror != NULL, "Must be");
1163     } else {
1164       assert(k->is_objArray_klass(), "Must be");
1165       Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
1166       assert(element_klass != NULL, "Must have an element klass");
1167       archived_comp_mirror = archive_mirror(element_klass, THREAD);
1168       if (archived_comp_mirror == NULL) {
1169         return NULL;
1170       }
1171     }
1172     java_lang_Class::set_component_mirror(archived_mirror, archived_comp_mirror);
1173   } else {
1174     assert(k->is_instance_klass(), "Must be");
1175 
1176     // Reset local static fields in the mirror
1177     InstanceKlass::cast(k)->do_local_static_fields(&reset);
1178 
1179     java_lang_Class:set_init_lock(archived_mirror, NULL);
1180 
1181     set_protection_domain(archived_mirror, NULL);
1182   }
1183 
1184   // clear class loader and mirror_module_field
1185   set_class_loader(archived_mirror, NULL);
1186   set_module(archived_mirror, NULL);
1187 
1188   // The archived mirror's field at _klass_offset is still pointing to the original
1189   // klass. Updated the field in the archived mirror to point to the relocated
1190   // klass in the archive.
1191   Klass *reloc_k = MetaspaceShared::get_relocated_klass(as_Klass(mirror));
1192   log_debug(cds, mirror)("Relocate mirror metadata field at _klass_offset from " PTR_FORMAT " ==> " PTR_FORMAT,
1193                          p2i(as_Klass(mirror)), p2i(reloc_k));
1194   archived_mirror->metadata_field_put(_klass_offset, reloc_k);
1195 
1196   // The field at _array_klass_offset is pointing to the original one dimension
1197   // higher array klass if exists. Relocate the pointer.
1198   Klass *arr = array_klass_acquire(mirror);
1199   if (arr != NULL) {
1200     Klass *reloc_arr = MetaspaceShared::get_relocated_klass(arr);
1201     log_debug(cds, mirror)("Relocate mirror metadata field at _array_klass_offset from " PTR_FORMAT " ==> " PTR_FORMAT,
1202                            p2i(arr), p2i(reloc_arr));
1203     archived_mirror->metadata_field_put(_array_klass_offset, reloc_arr);
1204   }
1205   return archived_mirror;
1206 }
1207 
1208 // After the archived mirror object is restored, the shared klass'
1209 // _has_raw_archived_mirror flag is cleared
1210 void java_lang_Class::restore_archived_mirror(Klass *k, Handle mirror,
1211                                               Handle class_loader, Handle module,
1212                                               Handle protection_domain, TRAPS) {
1213 
1214   // The java.lang.Class field offsets were archived and reloaded from archive.
1215   // No need to put classes on the fixup_mirror_list before java.lang.Class
1216   // is loaded.
1217 
1218   if (!k->is_array_klass()) {
1219     // - local static final fields with initial values were initialized at dump time
1220 
1221     // create the init_lock
1222     typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
1223     set_init_lock(mirror(), r);
1224 
1225     if (protection_domain.not_null()) {
1226       set_protection_domain(mirror(), protection_domain());
1227     }
1228   }
1229 
1230   assert(class_loader() == k->class_loader(), "should be same");
1231   if (class_loader.not_null()) {
1232     set_class_loader(mirror(), class_loader());
1233   }
1234 
1235   k->set_java_mirror(mirror);
1236   k->clear_has_raw_archived_mirror();
1237 
1238   set_mirror_module_field(k, mirror, module, THREAD);
1239 
1240   ResourceMark rm;
1241   log_trace(cds, mirror)("Restored %s archived mirror " PTR_FORMAT, k->external_name(), p2i(mirror()));
1242 }
1243 #endif // INCLUDE_CDS_JAVA_HEAP
1244 
1245 void java_lang_Class::fixup_module_field(Klass* k, Handle module) {
1246   assert(_module_offset != 0, "must have been computed already");
1247   java_lang_Class::set_module(k->java_mirror(), module());
1248 }
1249 
1250 int  java_lang_Class::oop_size(oop java_class) {
1251   assert(_oop_size_offset != 0, "must be set");
1252   int size = java_class->int_field(_oop_size_offset);
1253   assert(size > 0, "Oop size must be greater than zero, not %d", size);
1254   return size;
1255 }
1256 
1257 void java_lang_Class::set_oop_size(oop java_class, int size) {
1258   assert(_oop_size_offset != 0, "must be set");
1259   assert(size > 0, "Oop size must be greater than zero, not %d", size);
1260   java_class->int_field_put(_oop_size_offset, size);
1261 }
1262 
1263 int  java_lang_Class::static_oop_field_count(oop java_class) {
1264   assert(_static_oop_field_count_offset != 0, "must be set");
1265   return java_class->int_field(_static_oop_field_count_offset);
1266 }
1267 void java_lang_Class::set_static_oop_field_count(oop java_class, int size) {
1268   assert(_static_oop_field_count_offset != 0, "must be set");
1269   java_class->int_field_put(_static_oop_field_count_offset, size);
1270 }
1271 
1272 oop java_lang_Class::protection_domain(oop java_class) {
1273   assert(_protection_domain_offset != 0, "must be set");
1274   return java_class->obj_field(_protection_domain_offset);
1275 }
1276 void java_lang_Class::set_protection_domain(oop java_class, oop pd) {
1277   assert(_protection_domain_offset != 0, "must be set");
1278   java_class->obj_field_put(_protection_domain_offset, pd);
1279 }
1280 
1281 void java_lang_Class::set_component_mirror(oop java_class, oop comp_mirror) {
1282   assert(_component_mirror_offset != 0, "must be set");
1283     java_class->obj_field_put(_component_mirror_offset, comp_mirror);
1284   }
1285 oop java_lang_Class::component_mirror(oop java_class) {
1286   assert(_component_mirror_offset != 0, "must be set");
1287   return java_class->obj_field(_component_mirror_offset);
1288 }
1289 
1290 oop java_lang_Class::init_lock(oop java_class) {
1291   assert(_init_lock_offset != 0, "must be set");
1292   return java_class->obj_field(_init_lock_offset);
1293 }
1294 void java_lang_Class::set_init_lock(oop java_class, oop init_lock) {
1295   assert(_init_lock_offset != 0, "must be set");
1296   java_class->obj_field_put(_init_lock_offset, init_lock);
1297 }
1298 
1299 objArrayOop java_lang_Class::signers(oop java_class) {
1300   assert(_signers_offset != 0, "must be set");
1301   return (objArrayOop)java_class->obj_field(_signers_offset);
1302 }
1303 void java_lang_Class::set_signers(oop java_class, objArrayOop signers) {
1304   assert(_signers_offset != 0, "must be set");
1305   java_class->obj_field_put(_signers_offset, (oop)signers);
1306 }
1307 
1308 
1309 void java_lang_Class::set_class_loader(oop java_class, oop loader) {
1310   // jdk7 runs Queens in bootstrapping and jdk8-9 has no coordinated pushes yet.
1311   if (_class_loader_offset != 0) {
1312     java_class->obj_field_put(_class_loader_offset, loader);
1313   }
1314 }
1315 
1316 oop java_lang_Class::class_loader(oop java_class) {
1317   assert(_class_loader_offset != 0, "must be set");
1318   return java_class->obj_field(_class_loader_offset);
1319 }
1320 
1321 oop java_lang_Class::module(oop java_class) {
1322   assert(_module_offset != 0, "must be set");
1323   return java_class->obj_field(_module_offset);
1324 }
1325 
1326 void java_lang_Class::set_module(oop java_class, oop module) {
1327   assert(_module_offset != 0, "must be set");
1328   java_class->obj_field_put(_module_offset, module);
1329 }
1330 
1331 oop java_lang_Class::create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS) {
1332   // This should be improved by adding a field at the Java level or by
1333   // introducing a new VM klass (see comment in ClassFileParser)
1334   oop java_class = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(NULL, CHECK_0);
1335   if (type != T_VOID) {
1336     Klass* aklass = Universe::typeArrayKlassObj(type);
1337     assert(aklass != NULL, "correct bootstrap");
1338     release_set_array_klass(java_class, aklass);
1339   }
1340 #ifdef ASSERT
1341   InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(SystemDictionary::Class_klass());
1342   assert(java_lang_Class::static_oop_field_count(java_class) == 0, "should have been zeroed by allocation");
1343 #endif
1344   return java_class;
1345 }
1346 
1347 
1348 Klass* java_lang_Class::as_Klass(oop java_class) {
1349   //%note memory_2
1350   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1351   Klass* k = ((Klass*)java_class->metadata_field(_klass_offset));
1352   assert(k == NULL || k->is_klass(), "type check");
1353   return k;
1354 }
1355 
1356 
1357 void java_lang_Class::set_klass(oop java_class, Klass* klass) {
1358   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1359   java_class->metadata_field_put(_klass_offset, klass);
1360 }
1361 
1362 
1363 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1364   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1365   Symbol* name = NULL;
1366   bool is_instance = false;
1367   if (is_primitive(java_class)) {
1368     name = vmSymbols::type_signature(primitive_type(java_class));
1369   } else {
1370     Klass* k = as_Klass(java_class);
1371     is_instance = k->is_instance_klass();
1372     name = k->name();
1373   }
1374   if (name == NULL) {
1375     st->print("<null>");
1376     return;
1377   }
1378   if (is_instance)  st->print("L");
1379   st->write((char*) name->base(), (int) name->utf8_length());
1380   if (is_instance)  st->print(";");
1381 }
1382 
1383 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found, TRAPS) {
1384   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1385   Symbol* name;
1386   if (is_primitive(java_class)) {
1387     name = vmSymbols::type_signature(primitive_type(java_class));
1388     // Because this can create a new symbol, the caller has to decrement
1389     // the refcount, so make adjustment here and below for symbols returned
1390     // that are not created or incremented due to a successful lookup.
1391     name->increment_refcount();
1392   } else {
1393     Klass* k = as_Klass(java_class);
1394     if (!k->is_instance_klass()) {
1395       name = k->name();
1396       name->increment_refcount();
1397     } else {
1398       ResourceMark rm;
1399       const char* sigstr = k->signature_name();
1400       int         siglen = (int) strlen(sigstr);
1401       if (!intern_if_not_found) {
1402         name = SymbolTable::probe(sigstr, siglen);
1403       } else {
1404         name = SymbolTable::new_symbol(sigstr, siglen, THREAD);
1405       }
1406     }
1407   }
1408   return name;
1409 }
1410 
1411 // Returns the Java name for this Java mirror (Resource allocated)
1412 // See Klass::external_name().
1413 // For primitive type Java mirrors, its type name is returned.
1414 const char* java_lang_Class::as_external_name(oop java_class) {
1415   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1416   const char* name = NULL;
1417   if (is_primitive(java_class)) {
1418     name = type2name(primitive_type(java_class));
1419   } else {
1420     name = as_Klass(java_class)->external_name();
1421   }
1422   if (name == NULL) {
1423     name = "<null>";
1424   }
1425   return name;
1426 }
1427 
1428 Klass* java_lang_Class::array_klass_acquire(oop java_class) {
1429   Klass* k = ((Klass*)java_class->metadata_field_acquire(_array_klass_offset));
1430   assert(k == NULL || k->is_klass() && k->is_array_klass(), "should be array klass");
1431   return k;
1432 }
1433 
1434 
1435 void java_lang_Class::release_set_array_klass(oop java_class, Klass* klass) {
1436   assert(klass->is_klass() && klass->is_array_klass(), "should be array klass");
1437   java_class->release_metadata_field_put(_array_klass_offset, klass);
1438 }
1439 
1440 
1441 bool java_lang_Class::is_primitive(oop java_class) {
1442   // should assert:
1443   //assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1444   bool is_primitive = (java_class->metadata_field(_klass_offset) == NULL);
1445 
1446 #ifdef ASSERT
1447   if (is_primitive) {
1448     Klass* k = ((Klass*)java_class->metadata_field(_array_klass_offset));
1449     assert(k == NULL || is_java_primitive(ArrayKlass::cast(k)->element_type()),
1450         "Should be either the T_VOID primitive or a java primitive");
1451   }
1452 #endif
1453 
1454   return is_primitive;
1455 }
1456 
1457 
1458 BasicType java_lang_Class::primitive_type(oop java_class) {
1459   assert(java_lang_Class::is_primitive(java_class), "just checking");
1460   Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset));
1461   BasicType type = T_VOID;
1462   if (ak != NULL) {
1463     // Note: create_basic_type_mirror above initializes ak to a non-null value.
1464     type = ArrayKlass::cast(ak)->element_type();
1465   } else {
1466     assert(oopDesc::equals(java_class, Universe::void_mirror()), "only valid non-array primitive");
1467   }
1468   assert(oopDesc::equals(Universe::java_mirror(type), java_class), "must be consistent");
1469   return type;
1470 }
1471 
1472 BasicType java_lang_Class::as_BasicType(oop java_class, Klass** reference_klass) {
1473   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1474   if (is_primitive(java_class)) {
1475     if (reference_klass != NULL)
1476       (*reference_klass) = NULL;
1477     return primitive_type(java_class);
1478   } else {
1479     if (reference_klass != NULL)
1480       (*reference_klass) = as_Klass(java_class);
1481     return T_OBJECT;
1482   }
1483 }
1484 
1485 
1486 oop java_lang_Class::primitive_mirror(BasicType t) {
1487   oop mirror = Universe::java_mirror(t);
1488   assert(mirror != NULL && mirror->is_a(SystemDictionary::Class_klass()), "must be a Class");
1489   assert(java_lang_Class::is_primitive(mirror), "must be primitive");
1490   return mirror;
1491 }
1492 
1493 bool java_lang_Class::offsets_computed = false;
1494 int  java_lang_Class::classRedefinedCount_offset = -1;
1495 
1496 #define CLASS_FIELDS_DO(macro) \
1497   macro(classRedefinedCount_offset, k, "classRedefinedCount", int_signature,         false) ; \
1498   macro(_class_loader_offset,       k, "classLoader",         classloader_signature, false); \
1499   macro(_component_mirror_offset,   k, "componentType",       class_signature,       false); \
1500   macro(_module_offset,             k, "module",              module_signature,      false)
1501 
1502 void java_lang_Class::compute_offsets() {
1503   if (offsets_computed) {
1504     return;
1505   }
1506 
1507   offsets_computed = true;
1508 
1509   InstanceKlass* k = SystemDictionary::Class_klass();
1510   CLASS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1511 
1512   // Init lock is a C union with component_mirror.  Only instanceKlass mirrors have
1513   // init_lock and only ArrayKlass mirrors have component_mirror.  Since both are oops
1514   // GC treats them the same.
1515   _init_lock_offset = _component_mirror_offset;
1516 
1517   CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
1518 }
1519 
1520 #if INCLUDE_CDS
1521 void java_lang_Class::serialize(SerializeClosure* f) {
1522   f->do_u4((u4*)&offsets_computed);
1523   f->do_u4((u4*)&_init_lock_offset);
1524 
1525   CLASS_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1526 
1527   CLASS_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
1528 }
1529 #endif
1530 
1531 int java_lang_Class::classRedefinedCount(oop the_class_mirror) {
1532   if (classRedefinedCount_offset == -1) {
1533     // If we don't have an offset for it then just return -1 as a marker.
1534     return -1;
1535   }
1536 
1537   return the_class_mirror->int_field(classRedefinedCount_offset);
1538 }
1539 
1540 void java_lang_Class::set_classRedefinedCount(oop the_class_mirror, int value) {
1541   if (classRedefinedCount_offset == -1) {
1542     // If we don't have an offset for it then nothing to set.
1543     return;
1544   }
1545 
1546   the_class_mirror->int_field_put(classRedefinedCount_offset, value);
1547 }
1548 
1549 
1550 // Note: JDK1.1 and before had a privateInfo_offset field which was used for the
1551 //       platform thread structure, and a eetop offset which was used for thread
1552 //       local storage (and unused by the HotSpot VM). In JDK1.2 the two structures
1553 //       merged, so in the HotSpot VM we just use the eetop field for the thread
1554 //       instead of the privateInfo_offset.
1555 //
1556 // Note: The stackSize field is only present starting in 1.4.
1557 
1558 int java_lang_Thread::_name_offset = 0;
1559 int java_lang_Thread::_group_offset = 0;
1560 int java_lang_Thread::_contextClassLoader_offset = 0;
1561 int java_lang_Thread::_inheritedAccessControlContext_offset = 0;
1562 int java_lang_Thread::_priority_offset = 0;
1563 int java_lang_Thread::_eetop_offset = 0;
1564 int java_lang_Thread::_daemon_offset = 0;
1565 int java_lang_Thread::_stillborn_offset = 0;
1566 int java_lang_Thread::_stackSize_offset = 0;
1567 int java_lang_Thread::_tid_offset = 0;

1568 int java_lang_Thread::_thread_status_offset = 0;
1569 int java_lang_Thread::_park_blocker_offset = 0;
1570 int java_lang_Thread::_park_event_offset = 0 ;
1571 
1572 #define THREAD_FIELDS_DO(macro) \
1573   macro(_name_offset,          k, vmSymbols::name_name(), string_signature, false); \
1574   macro(_group_offset,         k, vmSymbols::group_name(), threadgroup_signature, false); \
1575   macro(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), classloader_signature, false); \
1576   macro(_inheritedAccessControlContext_offset, k, vmSymbols::inheritedAccessControlContext_name(), accesscontrolcontext_signature, false); \
1577   macro(_priority_offset,      k, vmSymbols::priority_name(), int_signature, false); \
1578   macro(_daemon_offset,        k, vmSymbols::daemon_name(), bool_signature, false); \
1579   macro(_eetop_offset,         k, "eetop", long_signature, false); \
1580   macro(_stillborn_offset,     k, "stillborn", bool_signature, false); \
1581   macro(_stackSize_offset,     k, "stackSize", long_signature, false); \
1582   macro(_tid_offset,           k, "tid", long_signature, false); \
1583   macro(_thread_status_offset, k, "threadStatus", int_signature, false); \
1584   macro(_park_blocker_offset,  k, "parkBlocker", object_signature, false); \
1585   macro(_park_event_offset,    k, "nativeParkEventPointer", long_signature, false)

1586 
1587 void java_lang_Thread::compute_offsets() {
1588   assert(_group_offset == 0, "offsets should be initialized only once");
1589 
1590   InstanceKlass* k = SystemDictionary::Thread_klass();
1591   THREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1592 }
1593 
1594 #if INCLUDE_CDS
1595 void java_lang_Thread::serialize(SerializeClosure* f) {
1596   THREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1597 }
1598 #endif
1599 
1600 JavaThread* java_lang_Thread::thread(oop java_thread) {
1601   return (JavaThread*)java_thread->address_field(_eetop_offset);
1602 }
1603 
1604 
1605 void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) {
1606   java_thread->address_field_put(_eetop_offset, (address)thread);
1607 }
1608 
1609 
1610 oop java_lang_Thread::name(oop java_thread) {
1611   return java_thread->obj_field(_name_offset);
1612 }
1613 
1614 
1615 void java_lang_Thread::set_name(oop java_thread, oop name) {
1616   java_thread->obj_field_put(_name_offset, name);
1617 }
1618 
1619 
1620 ThreadPriority java_lang_Thread::priority(oop java_thread) {
1621   return (ThreadPriority)java_thread->int_field(_priority_offset);
1622 }
1623 
1624 
1625 void java_lang_Thread::set_priority(oop java_thread, ThreadPriority priority) {
1626   java_thread->int_field_put(_priority_offset, priority);
1627 }
1628 
1629 
1630 oop java_lang_Thread::threadGroup(oop java_thread) {
1631   return java_thread->obj_field(_group_offset);
1632 }
1633 
1634 
1635 bool java_lang_Thread::is_stillborn(oop java_thread) {
1636   return java_thread->bool_field(_stillborn_offset) != 0;
1637 }
1638 
1639 
1640 // We never have reason to turn the stillborn bit off
1641 void java_lang_Thread::set_stillborn(oop java_thread) {
1642   java_thread->bool_field_put(_stillborn_offset, true);
1643 }
1644 
1645 
1646 bool java_lang_Thread::is_alive(oop java_thread) {
1647   JavaThread* thr = java_lang_Thread::thread(java_thread);
1648   return (thr != NULL);
1649 }
1650 
1651 
1652 bool java_lang_Thread::is_daemon(oop java_thread) {
1653   return java_thread->bool_field(_daemon_offset) != 0;
1654 }
1655 
1656 
1657 void java_lang_Thread::set_daemon(oop java_thread) {
1658   java_thread->bool_field_put(_daemon_offset, true);
1659 }
1660 
1661 oop java_lang_Thread::context_class_loader(oop java_thread) {
1662   return java_thread->obj_field(_contextClassLoader_offset);
1663 }
1664 
1665 oop java_lang_Thread::inherited_access_control_context(oop java_thread) {
1666   return java_thread->obj_field(_inheritedAccessControlContext_offset);
1667 }
1668 
1669 
1670 jlong java_lang_Thread::stackSize(oop java_thread) {
1671   if (_stackSize_offset > 0) {
1672     return java_thread->long_field(_stackSize_offset);
1673   } else {
1674     return 0;
1675   }
1676 }
1677 
1678 // Write the thread status value to threadStatus field in java.lang.Thread java class.
1679 void java_lang_Thread::set_thread_status(oop java_thread,
1680                                          java_lang_Thread::ThreadStatus status) {
1681   // The threadStatus is only present starting in 1.5
1682   if (_thread_status_offset > 0) {
1683     java_thread->int_field_put(_thread_status_offset, status);
1684   }
1685 }
1686 
1687 // Read thread status value from threadStatus field in java.lang.Thread java class.
1688 java_lang_Thread::ThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
1689   // Make sure the caller is operating on behalf of the VM or is
1690   // running VM code (state == _thread_in_vm).
1691   assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
1692          JavaThread::current()->thread_state() == _thread_in_vm,
1693          "Java Thread is not running in vm");
1694   // The threadStatus is only present starting in 1.5
1695   if (_thread_status_offset > 0) {
1696     return (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
1697   } else {
1698     // All we can easily figure out is if it is alive, but that is
1699     // enough info for a valid unknown status.
1700     // These aren't restricted to valid set ThreadStatus values, so
1701     // use JVMTI values and cast.
1702     JavaThread* thr = java_lang_Thread::thread(java_thread);
1703     if (thr == NULL) {
1704       // the thread hasn't run yet or is in the process of exiting
1705       return NEW;
1706     }
1707     return (java_lang_Thread::ThreadStatus)JVMTI_THREAD_STATE_ALIVE;
1708   }
1709 }
1710 
1711 
1712 jlong java_lang_Thread::thread_id(oop java_thread) {
1713   // The thread ID field is only present starting in 1.5
1714   if (_tid_offset > 0) {
1715     return java_thread->long_field(_tid_offset);
1716   } else {
1717     return 0;
1718   }
1719 }
1720 








1721 oop java_lang_Thread::park_blocker(oop java_thread) {
1722   assert(JDK_Version::current().supports_thread_park_blocker() &&
1723          _park_blocker_offset != 0, "Must support parkBlocker field");
1724 
1725   if (_park_blocker_offset > 0) {
1726     return java_thread->obj_field(_park_blocker_offset);
1727   }
1728 
1729   return NULL;
1730 }
1731 
1732 jlong java_lang_Thread::park_event(oop java_thread) {
1733   if (_park_event_offset > 0) {
1734     return java_thread->long_field(_park_event_offset);
1735   }
1736   return 0;
1737 }
1738 
1739 bool java_lang_Thread::set_park_event(oop java_thread, jlong ptr) {
1740   if (_park_event_offset > 0) {
1741     java_thread->long_field_put(_park_event_offset, ptr);
1742     return true;
1743   }
1744   return false;
1745 }
1746 
1747 
1748 const char* java_lang_Thread::thread_status_name(oop java_thread) {
1749   assert(_thread_status_offset != 0, "Must have thread status");
1750   ThreadStatus status = (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
1751   switch (status) {
1752     case NEW                      : return "NEW";
1753     case RUNNABLE                 : return "RUNNABLE";
1754     case SLEEPING                 : return "TIMED_WAITING (sleeping)";
1755     case IN_OBJECT_WAIT           : return "WAITING (on object monitor)";
1756     case IN_OBJECT_WAIT_TIMED     : return "TIMED_WAITING (on object monitor)";
1757     case PARKED                   : return "WAITING (parking)";
1758     case PARKED_TIMED             : return "TIMED_WAITING (parking)";
1759     case BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
1760     case TERMINATED               : return "TERMINATED";
1761     default                       : return "UNKNOWN";
1762   };
1763 }
1764 int java_lang_ThreadGroup::_parent_offset = 0;
1765 int java_lang_ThreadGroup::_name_offset = 0;
1766 int java_lang_ThreadGroup::_threads_offset = 0;
1767 int java_lang_ThreadGroup::_groups_offset = 0;
1768 int java_lang_ThreadGroup::_maxPriority_offset = 0;
1769 int java_lang_ThreadGroup::_destroyed_offset = 0;
1770 int java_lang_ThreadGroup::_daemon_offset = 0;
1771 int java_lang_ThreadGroup::_nthreads_offset = 0;
1772 int java_lang_ThreadGroup::_ngroups_offset = 0;
1773 
1774 oop  java_lang_ThreadGroup::parent(oop java_thread_group) {
1775   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1776   return java_thread_group->obj_field(_parent_offset);
1777 }
1778 
1779 // ("name as oop" accessor is not necessary)
1780 
1781 const char* java_lang_ThreadGroup::name(oop java_thread_group) {
1782   oop name = java_thread_group->obj_field(_name_offset);
1783   // ThreadGroup.name can be null
1784   if (name != NULL) {
1785     return java_lang_String::as_utf8_string(name);
1786   }
1787   return NULL;
1788 }
1789 
1790 int java_lang_ThreadGroup::nthreads(oop java_thread_group) {
1791   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1792   return java_thread_group->int_field(_nthreads_offset);
1793 }
1794 
1795 objArrayOop java_lang_ThreadGroup::threads(oop java_thread_group) {
1796   oop threads = java_thread_group->obj_field(_threads_offset);
1797   assert(threads != NULL, "threadgroups should have threads");
1798   assert(threads->is_objArray(), "just checking"); // Todo: Add better type checking code
1799   return objArrayOop(threads);
1800 }
1801 
1802 int java_lang_ThreadGroup::ngroups(oop java_thread_group) {
1803   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1804   return java_thread_group->int_field(_ngroups_offset);
1805 }
1806 
1807 objArrayOop java_lang_ThreadGroup::groups(oop java_thread_group) {
1808   oop groups = java_thread_group->obj_field(_groups_offset);
1809   assert(groups == NULL || groups->is_objArray(), "just checking"); // Todo: Add better type checking code
1810   return objArrayOop(groups);
1811 }
1812 
1813 ThreadPriority java_lang_ThreadGroup::maxPriority(oop java_thread_group) {
1814   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1815   return (ThreadPriority) java_thread_group->int_field(_maxPriority_offset);
1816 }
1817 
1818 bool java_lang_ThreadGroup::is_destroyed(oop java_thread_group) {
1819   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1820   return java_thread_group->bool_field(_destroyed_offset) != 0;
1821 }
1822 
1823 bool java_lang_ThreadGroup::is_daemon(oop java_thread_group) {
1824   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
1825   return java_thread_group->bool_field(_daemon_offset) != 0;
1826 }
1827 
1828 #define THREADGROUP_FIELDS_DO(macro) \
1829   macro(_parent_offset,      k, vmSymbols::parent_name(),      threadgroup_signature,       false); \
1830   macro(_name_offset,        k, vmSymbols::name_name(),        string_signature,            false); \
1831   macro(_threads_offset,     k, vmSymbols::threads_name(),     thread_array_signature,      false); \
1832   macro(_groups_offset,      k, vmSymbols::groups_name(),      threadgroup_array_signature, false); \
1833   macro(_maxPriority_offset, k, vmSymbols::maxPriority_name(), int_signature,               false); \
1834   macro(_destroyed_offset,   k, vmSymbols::destroyed_name(),   bool_signature,              false); \
1835   macro(_daemon_offset,      k, vmSymbols::daemon_name(),      bool_signature,              false); \
1836   macro(_nthreads_offset,    k, vmSymbols::nthreads_name(),    int_signature,               false); \
1837   macro(_ngroups_offset,     k, vmSymbols::ngroups_name(),     int_signature,               false)
1838 
1839 void java_lang_ThreadGroup::compute_offsets() {
1840   assert(_parent_offset == 0, "offsets should be initialized only once");
1841 
1842   InstanceKlass* k = SystemDictionary::ThreadGroup_klass();
1843   THREADGROUP_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1844 }
1845 
1846 #if INCLUDE_CDS
1847 void java_lang_ThreadGroup::serialize(SerializeClosure* f) {
1848   THREADGROUP_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1849 }
1850 #endif
1851 
1852 #define THROWABLE_FIELDS_DO(macro) \
1853   macro(backtrace_offset,     k, "backtrace",     object_signature,                  false); \
1854   macro(detailMessage_offset, k, "detailMessage", string_signature,                  false); \
1855   macro(stackTrace_offset,    k, "stackTrace",    java_lang_StackTraceElement_array, false); \
1856   macro(depth_offset,         k, "depth",         int_signature,                     false); \
1857   macro(static_unassigned_stacktrace_offset, k, "UNASSIGNED_STACK", java_lang_StackTraceElement_array, true)
1858 
1859 void java_lang_Throwable::compute_offsets() {
1860   InstanceKlass* k = SystemDictionary::Throwable_klass();
1861   THROWABLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1862 }
1863 
1864 #if INCLUDE_CDS
1865 void java_lang_Throwable::serialize(SerializeClosure* f) {
1866   THROWABLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1867 }
1868 #endif
1869 
1870 oop java_lang_Throwable::unassigned_stacktrace() {
1871   InstanceKlass* ik = SystemDictionary::Throwable_klass();
1872   oop base = ik->static_field_base_raw();
1873   return base->obj_field(static_unassigned_stacktrace_offset);
1874 }
1875 
1876 oop java_lang_Throwable::backtrace(oop throwable) {
1877   return throwable->obj_field_acquire(backtrace_offset);
1878 }
1879 
1880 
1881 void java_lang_Throwable::set_backtrace(oop throwable, oop value) {
1882   throwable->release_obj_field_put(backtrace_offset, value);
1883 }
1884 
1885 int java_lang_Throwable::depth(oop throwable) {
1886   return throwable->int_field(depth_offset);
1887 }
1888 
1889 void java_lang_Throwable::set_depth(oop throwable, int value) {
1890   throwable->int_field_put(depth_offset, value);
1891 }
1892 
1893 oop java_lang_Throwable::message(oop throwable) {
1894   return throwable->obj_field(detailMessage_offset);
1895 }
1896 
1897 
1898 // Return Symbol for detailed_message or NULL
1899 Symbol* java_lang_Throwable::detail_message(oop throwable) {
1900   PRESERVE_EXCEPTION_MARK;  // Keep original exception
1901   oop detailed_message = java_lang_Throwable::message(throwable);
1902   if (detailed_message != NULL) {
1903     return java_lang_String::as_symbol(detailed_message, THREAD);
1904   }
1905   return NULL;
1906 }
1907 
1908 void java_lang_Throwable::set_message(oop throwable, oop value) {
1909   throwable->obj_field_put(detailMessage_offset, value);
1910 }
1911 
1912 
1913 void java_lang_Throwable::set_stacktrace(oop throwable, oop st_element_array) {
1914   throwable->obj_field_put(stackTrace_offset, st_element_array);
1915 }
1916 
1917 void java_lang_Throwable::clear_stacktrace(oop throwable) {
1918   set_stacktrace(throwable, NULL);
1919 }
1920 
1921 
1922 void java_lang_Throwable::print(oop throwable, outputStream* st) {
1923   ResourceMark rm;
1924   Klass* k = throwable->klass();
1925   assert(k != NULL, "just checking");
1926   st->print("%s", k->external_name());
1927   oop msg = message(throwable);
1928   if (msg != NULL) {
1929     st->print(": %s", java_lang_String::as_utf8_string(msg));
1930   }
1931 }
1932 
1933 // After this many redefines, the stack trace is unreliable.
1934 const int MAX_VERSION = USHRT_MAX;
1935 
1936 static inline bool version_matches(Method* method, int version) {
1937   assert(version < MAX_VERSION, "version is too big");
1938   return method != NULL && (method->constants()->version() == version);
1939 }
1940 
1941 
1942 // This class provides a simple wrapper over the internal structure of
1943 // exception backtrace to insulate users of the backtrace from needing
1944 // to know what it looks like.
1945 class BacktraceBuilder: public StackObj {
1946  friend class BacktraceIterator;
1947  private:
1948   Handle          _backtrace;
1949   objArrayOop     _head;
1950   typeArrayOop    _methods;
1951   typeArrayOop    _bcis;
1952   objArrayOop     _mirrors;
1953   typeArrayOop    _names; // needed to insulate method name against redefinition
1954   int             _index;
1955   NoSafepointVerifier _nsv;
1956 
1957   enum {
1958     trace_methods_offset = java_lang_Throwable::trace_methods_offset,
1959     trace_bcis_offset    = java_lang_Throwable::trace_bcis_offset,
1960     trace_mirrors_offset = java_lang_Throwable::trace_mirrors_offset,
1961     trace_names_offset   = java_lang_Throwable::trace_names_offset,
1962     trace_next_offset    = java_lang_Throwable::trace_next_offset,
1963     trace_size           = java_lang_Throwable::trace_size,
1964     trace_chunk_size     = java_lang_Throwable::trace_chunk_size
1965   };
1966 
1967   // get info out of chunks
1968   static typeArrayOop get_methods(objArrayHandle chunk) {
1969     typeArrayOop methods = typeArrayOop(chunk->obj_at(trace_methods_offset));
1970     assert(methods != NULL, "method array should be initialized in backtrace");
1971     return methods;
1972   }
1973   static typeArrayOop get_bcis(objArrayHandle chunk) {
1974     typeArrayOop bcis = typeArrayOop(chunk->obj_at(trace_bcis_offset));
1975     assert(bcis != NULL, "bci array should be initialized in backtrace");
1976     return bcis;
1977   }
1978   static objArrayOop get_mirrors(objArrayHandle chunk) {
1979     objArrayOop mirrors = objArrayOop(chunk->obj_at(trace_mirrors_offset));
1980     assert(mirrors != NULL, "mirror array should be initialized in backtrace");
1981     return mirrors;
1982   }
1983   static typeArrayOop get_names(objArrayHandle chunk) {
1984     typeArrayOop names = typeArrayOop(chunk->obj_at(trace_names_offset));
1985     assert(names != NULL, "names array should be initialized in backtrace");
1986     return names;
1987   }
1988 
1989  public:
1990 
1991   // constructor for new backtrace
1992   BacktraceBuilder(TRAPS): _methods(NULL), _bcis(NULL), _head(NULL), _mirrors(NULL), _names(NULL) {
1993     expand(CHECK);
1994     _backtrace = Handle(THREAD, _head);
1995     _index = 0;
1996   }
1997 
1998   BacktraceBuilder(Thread* thread, objArrayHandle backtrace) {
1999     _methods = get_methods(backtrace);
2000     _bcis = get_bcis(backtrace);
2001     _mirrors = get_mirrors(backtrace);
2002     _names = get_names(backtrace);
2003     assert(_methods->length() == _bcis->length() &&
2004            _methods->length() == _mirrors->length() &&
2005            _mirrors->length() == _names->length(),
2006            "method and source information arrays should match");
2007 
2008     // head is the preallocated backtrace
2009     _head = backtrace();
2010     _backtrace = Handle(thread, _head);
2011     _index = 0;
2012   }
2013 
2014   void expand(TRAPS) {
2015     objArrayHandle old_head(THREAD, _head);
2016     PauseNoSafepointVerifier pnsv(&_nsv);
2017 
2018     objArrayOop head = oopFactory::new_objectArray(trace_size, CHECK);
2019     objArrayHandle new_head(THREAD, head);
2020 
2021     typeArrayOop methods = oopFactory::new_shortArray(trace_chunk_size, CHECK);
2022     typeArrayHandle new_methods(THREAD, methods);
2023 
2024     typeArrayOop bcis = oopFactory::new_intArray(trace_chunk_size, CHECK);
2025     typeArrayHandle new_bcis(THREAD, bcis);
2026 
2027     objArrayOop mirrors = oopFactory::new_objectArray(trace_chunk_size, CHECK);
2028     objArrayHandle new_mirrors(THREAD, mirrors);
2029 
2030     typeArrayOop names = oopFactory::new_symbolArray(trace_chunk_size, CHECK);
2031     typeArrayHandle new_names(THREAD, names);
2032 
2033     if (!old_head.is_null()) {
2034       old_head->obj_at_put(trace_next_offset, new_head());
2035     }
2036     new_head->obj_at_put(trace_methods_offset, new_methods());
2037     new_head->obj_at_put(trace_bcis_offset, new_bcis());
2038     new_head->obj_at_put(trace_mirrors_offset, new_mirrors());
2039     new_head->obj_at_put(trace_names_offset, new_names());
2040 
2041     _head    = new_head();
2042     _methods = new_methods();
2043     _bcis = new_bcis();
2044     _mirrors = new_mirrors();
2045     _names  = new_names();
2046     _index = 0;
2047   }
2048 
2049   oop backtrace() {
2050     return _backtrace();
2051   }
2052 
2053   inline void push(Method* method, int bci, TRAPS) {
2054     // Smear the -1 bci to 0 since the array only holds unsigned
2055     // shorts.  The later line number lookup would just smear the -1
2056     // to a 0 even if it could be recorded.
2057     if (bci == SynchronizationEntryBCI) bci = 0;
2058 
2059     if (_index >= trace_chunk_size) {
2060       methodHandle mhandle(THREAD, method);
2061       expand(CHECK);
2062       method = mhandle();
2063     }
2064 
2065     _methods->ushort_at_put(_index, method->orig_method_idnum());
2066     _bcis->int_at_put(_index, Backtrace::merge_bci_and_version(bci, method->constants()->version()));
2067 
2068     // Note:this doesn't leak symbols because the mirror in the backtrace keeps the
2069     // klass owning the symbols alive so their refcounts aren't decremented.
2070     Symbol* name = method->name();
2071     _names->symbol_at_put(_index, name);
2072 
2073     // We need to save the mirrors in the backtrace to keep the class
2074     // from being unloaded while we still have this stack trace.
2075     assert(method->method_holder()->java_mirror() != NULL, "never push null for mirror");
2076     _mirrors->obj_at_put(_index, method->method_holder()->java_mirror());
2077     _index++;
2078   }
2079 
2080 };
2081 
2082 struct BacktraceElement : public StackObj {
2083   int _method_id;
2084   int _bci;
2085   int _version;
2086   Symbol* _name;
2087   Handle _mirror;
2088   BacktraceElement(Handle mirror, int mid, int version, int bci, Symbol* name) :
2089                    _mirror(mirror), _method_id(mid), _version(version), _bci(bci), _name(name) {}
2090 };
2091 
2092 class BacktraceIterator : public StackObj {
2093   int _index;
2094   objArrayHandle  _result;
2095   objArrayHandle  _mirrors;
2096   typeArrayHandle _methods;
2097   typeArrayHandle _bcis;
2098   typeArrayHandle _names;
2099 
2100   void init(objArrayHandle result, Thread* thread) {
2101     // Get method id, bci, version and mirror from chunk
2102     _result = result;
2103     if (_result.not_null()) {
2104       _methods = typeArrayHandle(thread, BacktraceBuilder::get_methods(_result));
2105       _bcis = typeArrayHandle(thread, BacktraceBuilder::get_bcis(_result));
2106       _mirrors = objArrayHandle(thread, BacktraceBuilder::get_mirrors(_result));
2107       _names = typeArrayHandle(thread, BacktraceBuilder::get_names(_result));
2108       _index = 0;
2109     }
2110   }
2111  public:
2112   BacktraceIterator(objArrayHandle result, Thread* thread) {
2113     init(result, thread);
2114     assert(_methods.is_null() || _methods->length() == java_lang_Throwable::trace_chunk_size, "lengths don't match");
2115   }
2116 
2117   BacktraceElement next(Thread* thread) {
2118     BacktraceElement e (Handle(thread, _mirrors->obj_at(_index)),
2119                         _methods->ushort_at(_index),
2120                         Backtrace::version_at(_bcis->int_at(_index)),
2121                         Backtrace::bci_at(_bcis->int_at(_index)),
2122                         _names->symbol_at(_index));
2123     _index++;
2124 
2125     if (_index >= java_lang_Throwable::trace_chunk_size) {
2126       int next_offset = java_lang_Throwable::trace_next_offset;
2127       // Get next chunk
2128       objArrayHandle result (thread, objArrayOop(_result->obj_at(next_offset)));
2129       init(result, thread);
2130     }
2131     return e;
2132   }
2133 
2134   bool repeat() {
2135     return _result.not_null() && _mirrors->obj_at(_index) != NULL;
2136   }
2137 };
2138 
2139 
2140 // Print stack trace element to resource allocated buffer
2141 static void print_stack_element_to_stream(outputStream* st, Handle mirror, int method_id,
2142                                           int version, int bci, Symbol* name) {
2143   ResourceMark rm;
2144 
2145   // Get strings and string lengths
2146   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror()));
2147   const char* klass_name  = holder->external_name();
2148   int buf_len = (int)strlen(klass_name);
2149 
2150   char* method_name = name->as_C_string();
2151   buf_len += (int)strlen(method_name);
2152 
2153   char* source_file_name = NULL;
2154   Symbol* source = Backtrace::get_source_file_name(holder, version);
2155   if (source != NULL) {
2156     source_file_name = source->as_C_string();
2157     buf_len += (int)strlen(source_file_name);
2158   }
2159 
2160   char *module_name = NULL, *module_version = NULL;
2161   ModuleEntry* module = holder->module();
2162   if (module->is_named()) {
2163     module_name = module->name()->as_C_string();
2164     buf_len += (int)strlen(module_name);
2165     if (module->version() != NULL) {
2166       module_version = module->version()->as_C_string();
2167       buf_len += (int)strlen(module_version);
2168     }
2169   }
2170 
2171   // Allocate temporary buffer with extra space for formatting and line number
2172   char* buf = NEW_RESOURCE_ARRAY(char, buf_len + 64);
2173 
2174   // Print stack trace line in buffer
2175   sprintf(buf, "\tat %s.%s(", klass_name, method_name);
2176 
2177   // Print module information
2178   if (module_name != NULL) {
2179     if (module_version != NULL) {
2180       sprintf(buf + (int)strlen(buf), "%s@%s/", module_name, module_version);
2181     } else {
2182       sprintf(buf + (int)strlen(buf), "%s/", module_name);
2183     }
2184   }
2185 
2186   // The method can be NULL if the requested class version is gone
2187   Method* method = holder->method_with_orig_idnum(method_id, version);
2188   if (!version_matches(method, version)) {
2189     strcat(buf, "Redefined)");
2190   } else {
2191     int line_number = Backtrace::get_line_number(method, bci);
2192     if (line_number == -2) {
2193       strcat(buf, "Native Method)");
2194     } else {
2195       if (source_file_name != NULL && (line_number != -1)) {
2196         // Sourcename and linenumber
2197         sprintf(buf + (int)strlen(buf), "%s:%d)", source_file_name, line_number);
2198       } else if (source_file_name != NULL) {
2199         // Just sourcename
2200         sprintf(buf + (int)strlen(buf), "%s)", source_file_name);
2201       } else {
2202         // Neither sourcename nor linenumber
2203         sprintf(buf + (int)strlen(buf), "Unknown Source)");
2204       }
2205       CompiledMethod* nm = method->code();
2206       if (WizardMode && nm != NULL) {
2207         sprintf(buf + (int)strlen(buf), "(nmethod " INTPTR_FORMAT ")", (intptr_t)nm);
2208       }
2209     }
2210   }
2211 
2212   st->print_cr("%s", buf);
2213 }
2214 
2215 void java_lang_Throwable::print_stack_element(outputStream *st, const methodHandle& method, int bci) {
2216   Handle mirror (Thread::current(),  method->method_holder()->java_mirror());
2217   int method_id = method->orig_method_idnum();
2218   int version = method->constants()->version();
2219   print_stack_element_to_stream(st, mirror, method_id, version, bci, method->name());
2220 }
2221 
2222 /**
2223  * Print the throwable message and its stack trace plus all causes by walking the
2224  * cause chain.  The output looks the same as of Throwable.printStackTrace().
2225  */
2226 void java_lang_Throwable::print_stack_trace(Handle throwable, outputStream* st) {
2227   // First, print the message.
2228   print(throwable(), st);
2229   st->cr();
2230 
2231   // Now print the stack trace.
2232   Thread* THREAD = Thread::current();
2233   while (throwable.not_null()) {
2234     objArrayHandle result (THREAD, objArrayOop(backtrace(throwable())));
2235     if (result.is_null()) {
2236       st->print_raw_cr("\t<<no stack trace available>>");
2237       return;
2238     }
2239     BacktraceIterator iter(result, THREAD);
2240 
2241     while (iter.repeat()) {
2242       BacktraceElement bte = iter.next(THREAD);
2243       print_stack_element_to_stream(st, bte._mirror, bte._method_id, bte._version, bte._bci, bte._name);
2244     }
2245     {
2246       // Call getCause() which doesn't necessarily return the _cause field.
2247       EXCEPTION_MARK;
2248       JavaValue cause(T_OBJECT);
2249       JavaCalls::call_virtual(&cause,
2250                               throwable,
2251                               throwable->klass(),
2252                               vmSymbols::getCause_name(),
2253                               vmSymbols::void_throwable_signature(),
2254                               THREAD);
2255       // Ignore any exceptions. we are in the middle of exception handling. Same as classic VM.
2256       if (HAS_PENDING_EXCEPTION) {
2257         CLEAR_PENDING_EXCEPTION;
2258         throwable = Handle();
2259       } else {
2260         throwable = Handle(THREAD, (oop) cause.get_jobject());
2261         if (throwable.not_null()) {
2262           st->print("Caused by: ");
2263           print(throwable(), st);
2264           st->cr();
2265         }
2266       }
2267     }
2268   }
2269 }
2270 
2271 /**
2272  * Print the throwable stack trace by calling the Java method java.lang.Throwable.printStackTrace().
2273  */
2274 void java_lang_Throwable::java_printStackTrace(Handle throwable, TRAPS) {
2275   assert(throwable->is_a(SystemDictionary::Throwable_klass()), "Throwable instance expected");
2276   JavaValue result(T_VOID);
2277   JavaCalls::call_virtual(&result,
2278                           throwable,
2279                           SystemDictionary::Throwable_klass(),
2280                           vmSymbols::printStackTrace_name(),
2281                           vmSymbols::void_method_signature(),
2282                           THREAD);
2283 }
2284 
2285 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method, TRAPS) {
2286   if (!StackTraceInThrowable) return;
2287   ResourceMark rm(THREAD);
2288 
2289   // Start out by clearing the backtrace for this object, in case the VM
2290   // runs out of memory while allocating the stack trace
2291   set_backtrace(throwable(), NULL);
2292   // Clear lazily constructed Java level stacktrace if refilling occurs
2293   // This is unnecessary in 1.7+ but harmless
2294   clear_stacktrace(throwable());
2295 
2296   int max_depth = MaxJavaStackTraceDepth;
2297   JavaThread* thread = (JavaThread*)THREAD;
2298 
2299   BacktraceBuilder bt(CHECK);
2300 
2301   // If there is no Java frame just return the method that was being called
2302   // with bci 0
2303   if (!thread->has_last_Java_frame()) {
2304     if (max_depth >= 1 && method() != NULL) {
2305       bt.push(method(), 0, CHECK);
2306       log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), 1);
2307       set_depth(throwable(), 1);
2308       set_backtrace(throwable(), bt.backtrace());
2309     }
2310     return;
2311   }
2312 
2313   // Instead of using vframe directly, this version of fill_in_stack_trace
2314   // basically handles everything by hand. This significantly improved the
2315   // speed of this method call up to 28.5% on Solaris sparc. 27.1% on Windows.
2316   // See bug 6333838 for  more details.
2317   // The "ASSERT" here is to verify this method generates the exactly same stack
2318   // trace as utilizing vframe.
2319 #ifdef ASSERT
2320   vframeStream st(thread);
2321   methodHandle st_method(THREAD, st.method());
2322 #endif
2323   int total_count = 0;
2324   RegisterMap map(thread, false);
2325   int decode_offset = 0;
2326   CompiledMethod* nm = NULL;
2327   bool skip_fillInStackTrace_check = false;
2328   bool skip_throwableInit_check = false;
2329   bool skip_hidden = !ShowHiddenFrames;
2330 
2331   for (frame fr = thread->last_frame(); max_depth == 0 || max_depth != total_count;) {
2332     Method* method = NULL;
2333     int bci = 0;
2334 
2335     // Compiled java method case.
2336     if (decode_offset != 0) {
2337       DebugInfoReadStream stream(nm, decode_offset);
2338       decode_offset = stream.read_int();
2339       method = (Method*)nm->metadata_at(stream.read_int());
2340       bci = stream.read_bci();
2341     } else {
2342       if (fr.is_first_frame()) break;
2343       address pc = fr.pc();
2344       if (fr.is_interpreted_frame()) {
2345         address bcp = fr.interpreter_frame_bcp();
2346         method = fr.interpreter_frame_method();
2347         bci =  method->bci_from(bcp);
2348         fr = fr.sender(&map);
2349       } else {
2350         CodeBlob* cb = fr.cb();
2351         // HMMM QQQ might be nice to have frame return nm as NULL if cb is non-NULL
2352         // but non nmethod
2353         fr = fr.sender(&map);
2354         if (cb == NULL || !cb->is_compiled()) {
2355           continue;
2356         }
2357         nm = cb->as_compiled_method();
2358         if (nm->method()->is_native()) {
2359           method = nm->method();
2360           bci = 0;
2361         } else {
2362           PcDesc* pd = nm->pc_desc_at(pc);
2363           decode_offset = pd->scope_decode_offset();
2364           // if decode_offset is not equal to 0, it will execute the
2365           // "compiled java method case" at the beginning of the loop.
2366           continue;
2367         }
2368       }
2369     }
2370 #ifdef ASSERT
2371     assert(st_method() == method && st.bci() == bci,
2372            "Wrong stack trace");
2373     st.next();
2374     // vframeStream::method isn't GC-safe so store off a copy
2375     // of the Method* in case we GC.
2376     if (!st.at_end()) {
2377       st_method = st.method();
2378     }
2379 #endif
2380 
2381     // the format of the stacktrace will be:
2382     // - 1 or more fillInStackTrace frames for the exception class (skipped)
2383     // - 0 or more <init> methods for the exception class (skipped)
2384     // - rest of the stack
2385 
2386     if (!skip_fillInStackTrace_check) {
2387       if (method->name() == vmSymbols::fillInStackTrace_name() &&
2388           throwable->is_a(method->method_holder())) {
2389         continue;
2390       }
2391       else {
2392         skip_fillInStackTrace_check = true; // gone past them all
2393       }
2394     }
2395     if (!skip_throwableInit_check) {
2396       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2397 
2398       // skip <init> methods of the exception class and superclasses
2399       // This is simlar to classic VM.
2400       if (method->name() == vmSymbols::object_initializer_name() &&
2401           throwable->is_a(method->method_holder())) {
2402         continue;
2403       } else {
2404         // there are none or we've seen them all - either way stop checking
2405         skip_throwableInit_check = true;
2406       }
2407     }
2408     if (method->is_hidden()) {
2409       if (skip_hidden)  continue;
2410     }
2411     bt.push(method, bci, CHECK);
2412     total_count++;
2413   }
2414 
2415   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), total_count);
2416 
2417   // Put completed stack trace into throwable object
2418   set_backtrace(throwable(), bt.backtrace());
2419   set_depth(throwable(), total_count);
2420 }
2421 
2422 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method) {
2423   // No-op if stack trace is disabled
2424   if (!StackTraceInThrowable) {
2425     return;
2426   }
2427 
2428   // Disable stack traces for some preallocated out of memory errors
2429   if (!Universe::should_fill_in_stack_trace(throwable)) {
2430     return;
2431   }
2432 
2433   PRESERVE_EXCEPTION_MARK;
2434 
2435   JavaThread* thread = JavaThread::active();
2436   fill_in_stack_trace(throwable, method, thread);
2437   // ignore exceptions thrown during stack trace filling
2438   CLEAR_PENDING_EXCEPTION;
2439 }
2440 
2441 void java_lang_Throwable::allocate_backtrace(Handle throwable, TRAPS) {
2442   // Allocate stack trace - backtrace is created but not filled in
2443 
2444   // No-op if stack trace is disabled
2445   if (!StackTraceInThrowable) return;
2446   BacktraceBuilder bt(CHECK);   // creates a backtrace
2447   set_backtrace(throwable(), bt.backtrace());
2448 }
2449 
2450 
2451 void java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(Handle throwable) {
2452   // Fill in stack trace into preallocated backtrace (no GC)
2453 
2454   // No-op if stack trace is disabled
2455   if (!StackTraceInThrowable) return;
2456 
2457   assert(throwable->is_a(SystemDictionary::Throwable_klass()), "sanity check");
2458 
2459   JavaThread* THREAD = JavaThread::current();
2460 
2461   objArrayHandle backtrace (THREAD, (objArrayOop)java_lang_Throwable::backtrace(throwable()));
2462   assert(backtrace.not_null(), "backtrace should have been preallocated");
2463 
2464   ResourceMark rm(THREAD);
2465   vframeStream st(THREAD);
2466 
2467   BacktraceBuilder bt(THREAD, backtrace);
2468 
2469   // Unlike fill_in_stack_trace we do not skip fillInStackTrace or throwable init
2470   // methods as preallocated errors aren't created by "java" code.
2471 
2472   // fill in as much stack trace as possible
2473   int chunk_count = 0;
2474   for (;!st.at_end(); st.next()) {
2475     bt.push(st.method(), st.bci(), CHECK);
2476     chunk_count++;
2477 
2478     // Bail-out for deep stacks
2479     if (chunk_count >= trace_chunk_size) break;
2480   }
2481   set_depth(throwable(), chunk_count);
2482   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), chunk_count);
2483 
2484   // We support the Throwable immutability protocol defined for Java 7.
2485   java_lang_Throwable::set_stacktrace(throwable(), java_lang_Throwable::unassigned_stacktrace());
2486   assert(java_lang_Throwable::unassigned_stacktrace() != NULL, "not initialized");
2487 }
2488 
2489 void java_lang_Throwable::get_stack_trace_elements(Handle throwable,
2490                                                    objArrayHandle stack_trace_array_h, TRAPS) {
2491 
2492   if (throwable.is_null() || stack_trace_array_h.is_null()) {
2493     THROW(vmSymbols::java_lang_NullPointerException());
2494   }
2495 
2496   assert(stack_trace_array_h->is_objArray(), "Stack trace array should be an array of StackTraceElenent");
2497 
2498   if (stack_trace_array_h->length() != depth(throwable())) {
2499     THROW(vmSymbols::java_lang_IndexOutOfBoundsException());
2500   }
2501 
2502   objArrayHandle result(THREAD, objArrayOop(backtrace(throwable())));
2503   BacktraceIterator iter(result, THREAD);
2504 
2505   int index = 0;
2506   while (iter.repeat()) {
2507     BacktraceElement bte = iter.next(THREAD);
2508 
2509     Handle stack_trace_element(THREAD, stack_trace_array_h->obj_at(index++));
2510 
2511     if (stack_trace_element.is_null()) {
2512       THROW(vmSymbols::java_lang_NullPointerException());
2513     }
2514 
2515     InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror()));
2516     methodHandle method (THREAD, holder->method_with_orig_idnum(bte._method_id, bte._version));
2517 
2518     java_lang_StackTraceElement::fill_in(stack_trace_element, holder,
2519                                          method,
2520                                          bte._version,
2521                                          bte._bci,
2522                                          bte._name, CHECK);
2523   }
2524 }
2525 
2526 oop java_lang_StackTraceElement::create(const methodHandle& method, int bci, TRAPS) {
2527   // Allocate java.lang.StackTraceElement instance
2528   InstanceKlass* k = SystemDictionary::StackTraceElement_klass();
2529   assert(k != NULL, "must be loaded in 1.4+");
2530   if (k->should_be_initialized()) {
2531     k->initialize(CHECK_0);
2532   }
2533 
2534   Handle element = k->allocate_instance_handle(CHECK_0);
2535 
2536   int version = method->constants()->version();
2537   fill_in(element, method->method_holder(), method, version, bci, method->name(), CHECK_0);
2538   return element();
2539 }
2540 
2541 void java_lang_StackTraceElement::fill_in(Handle element,
2542                                           InstanceKlass* holder, const methodHandle& method,
2543                                           int version, int bci, Symbol* name, TRAPS) {
2544   assert(element->is_a(SystemDictionary::StackTraceElement_klass()), "sanity check");
2545 
2546   // Fill in class name
2547   ResourceMark rm(THREAD);
2548   const char* str = holder->external_name();
2549   oop classname = StringTable::intern((char*) str, CHECK);
2550   java_lang_StackTraceElement::set_declaringClass(element(), classname);
2551   java_lang_StackTraceElement::set_declaringClassObject(element(), holder->java_mirror());
2552 
2553   oop loader = holder->class_loader();
2554   if (loader != NULL) {
2555     oop loader_name = java_lang_ClassLoader::name(loader);
2556     if (loader_name != NULL)
2557       java_lang_StackTraceElement::set_classLoaderName(element(), loader_name);
2558   }
2559 
2560   // Fill in method name
2561   oop methodname = StringTable::intern(name, CHECK);
2562   java_lang_StackTraceElement::set_methodName(element(), methodname);
2563 
2564   // Fill in module name and version
2565   ModuleEntry* module = holder->module();
2566   if (module->is_named()) {
2567     oop module_name = StringTable::intern(module->name(), CHECK);
2568     java_lang_StackTraceElement::set_moduleName(element(), module_name);
2569     oop module_version;
2570     if (module->version() != NULL) {
2571       module_version = StringTable::intern(module->version(), CHECK);
2572     } else {
2573       module_version = NULL;
2574     }
2575     java_lang_StackTraceElement::set_moduleVersion(element(), module_version);
2576   }
2577 
2578   if (method() == NULL || !version_matches(method(), version)) {
2579     // The method was redefined, accurate line number information isn't available
2580     java_lang_StackTraceElement::set_fileName(element(), NULL);
2581     java_lang_StackTraceElement::set_lineNumber(element(), -1);
2582   } else {
2583     // Fill in source file name and line number.
2584     Symbol* source = Backtrace::get_source_file_name(holder, version);
2585     if (ShowHiddenFrames && source == NULL)
2586       source = vmSymbols::unknown_class_name();
2587     oop filename = StringTable::intern(source, CHECK);
2588     java_lang_StackTraceElement::set_fileName(element(), filename);
2589 
2590     int line_number = Backtrace::get_line_number(method, bci);
2591     java_lang_StackTraceElement::set_lineNumber(element(), line_number);
2592   }
2593 }
2594 
2595 Method* java_lang_StackFrameInfo::get_method(Handle stackFrame, InstanceKlass* holder, TRAPS) {
2596   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
2597   Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mname());
2598   // we should expand MemberName::name when Throwable uses StackTrace
2599   // MethodHandles::expand_MemberName(mname, MethodHandles::_suppress_defc|MethodHandles::_suppress_type, CHECK_NULL);
2600   return method;
2601 }
2602 
2603 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci, TRAPS) {
2604   // set Method* or mid/cpref
2605   Handle mname(Thread::current(), stackFrame->obj_field(_memberName_offset));
2606   InstanceKlass* ik = method->method_holder();
2607   CallInfo info(method(), ik, CHECK);
2608   MethodHandles::init_method_MemberName(mname, info);
2609   // set bci
2610   java_lang_StackFrameInfo::set_bci(stackFrame(), bci);
2611   // method may be redefined; store the version
2612   int version = method->constants()->version();
2613   assert((jushort)version == version, "version should be short");
2614   java_lang_StackFrameInfo::set_version(stackFrame(), (short)version);
2615 }
2616 
2617 void java_lang_StackFrameInfo::to_stack_trace_element(Handle stackFrame, Handle stack_trace_element, TRAPS) {
2618   ResourceMark rm(THREAD);
2619   Handle mname(THREAD, stackFrame->obj_field(java_lang_StackFrameInfo::_memberName_offset));
2620   Klass* clazz = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname()));
2621   InstanceKlass* holder = InstanceKlass::cast(clazz);
2622   Method* method = java_lang_StackFrameInfo::get_method(stackFrame, holder, CHECK);
2623 
2624   short version = stackFrame->short_field(_version_offset);
2625   short bci = stackFrame->short_field(_bci_offset);
2626   Symbol* name = method->name();
2627   java_lang_StackTraceElement::fill_in(stack_trace_element, holder, method, version, bci, name, CHECK);
2628 }
2629 
2630 #define STACKFRAMEINFO_FIELDS_DO(macro) \
2631   macro(_memberName_offset,     k, "memberName",  object_signature, false); \
2632   macro(_bci_offset,            k, "bci",         short_signature,  false)
2633 
2634 void java_lang_StackFrameInfo::compute_offsets() {
2635   InstanceKlass* k = SystemDictionary::StackFrameInfo_klass();
2636   STACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2637   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2638 }
2639 
2640 #if INCLUDE_CDS
2641 void java_lang_StackFrameInfo::serialize(SerializeClosure* f) {
2642   STACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2643   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
2644 }
2645 #endif
2646 
2647 #define LIVESTACKFRAMEINFO_FIELDS_DO(macro) \
2648   macro(_monitors_offset,   k, "monitors",    object_array_signature, false); \
2649   macro(_locals_offset,     k, "locals",      object_array_signature, false); \
2650   macro(_operands_offset,   k, "operands",    object_array_signature, false); \
2651   macro(_mode_offset,       k, "mode",        int_signature,          false)
2652 
2653 void java_lang_LiveStackFrameInfo::compute_offsets() {
2654   InstanceKlass* k = SystemDictionary::LiveStackFrameInfo_klass();
2655   LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2656 }
2657 
2658 #if INCLUDE_CDS
2659 void java_lang_LiveStackFrameInfo::serialize(SerializeClosure* f) {
2660   LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2661 }
2662 #endif
2663 
2664 #define ACCESSIBLEOBJECT_FIELDS_DO(macro) \
2665   macro(override_offset, k, "override", bool_signature, false)
2666 
2667 void java_lang_reflect_AccessibleObject::compute_offsets() {
2668   InstanceKlass* k = SystemDictionary::reflect_AccessibleObject_klass();
2669   ACCESSIBLEOBJECT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2670 }
2671 
2672 #if INCLUDE_CDS
2673 void java_lang_reflect_AccessibleObject::serialize(SerializeClosure* f) {
2674   ACCESSIBLEOBJECT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2675 }
2676 #endif
2677 
2678 jboolean java_lang_reflect_AccessibleObject::override(oop reflect) {
2679   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2680   return (jboolean) reflect->bool_field(override_offset);
2681 }
2682 
2683 void java_lang_reflect_AccessibleObject::set_override(oop reflect, jboolean value) {
2684   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2685   reflect->bool_field_put(override_offset, (int) value);
2686 }
2687 
2688 #define METHOD_FIELDS_DO(macro) \
2689   macro(clazz_offset,          k, vmSymbols::clazz_name(),          class_signature,       false); \
2690   macro(name_offset,           k, vmSymbols::name_name(),           string_signature,      false); \
2691   macro(returnType_offset,     k, vmSymbols::returnType_name(),     class_signature,       false); \
2692   macro(parameterTypes_offset, k, vmSymbols::parameterTypes_name(), class_array_signature, false); \
2693   macro(exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), class_array_signature, false); \
2694   macro(slot_offset,           k, vmSymbols::slot_name(),           int_signature,         false); \
2695   macro(modifiers_offset,      k, vmSymbols::modifiers_name(),      int_signature,         false); \
2696   macro##_OPTIONAL(signature_offset,             k, vmSymbols::signature_name(),             string_signature); \
2697   macro##_OPTIONAL(annotations_offset,           k, vmSymbols::annotations_name(),           byte_array_signature); \
2698   macro##_OPTIONAL(parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), byte_array_signature); \
2699   macro##_OPTIONAL(annotation_default_offset,    k, vmSymbols::annotation_default_name(),    byte_array_signature); \
2700   macro##_OPTIONAL(type_annotations_offset,      k, vmSymbols::type_annotations_name(),      byte_array_signature)
2701 
2702 void java_lang_reflect_Method::compute_offsets() {
2703   InstanceKlass* k = SystemDictionary::reflect_Method_klass();
2704   // The generic signature and annotations fields are only present in 1.5
2705   signature_offset = -1;
2706   annotations_offset = -1;
2707   parameter_annotations_offset = -1;
2708   annotation_default_offset = -1;
2709   type_annotations_offset = -1;
2710   METHOD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2711 }
2712 
2713 #if INCLUDE_CDS
2714 void java_lang_reflect_Method::serialize(SerializeClosure* f) {
2715   METHOD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2716 }
2717 #endif
2718 
2719 Handle java_lang_reflect_Method::create(TRAPS) {
2720   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2721   Klass* klass = SystemDictionary::reflect_Method_klass();
2722   // This class is eagerly initialized during VM initialization, since we keep a refence
2723   // to one of the methods
2724   assert(InstanceKlass::cast(klass)->is_initialized(), "must be initialized");
2725   return InstanceKlass::cast(klass)->allocate_instance_handle(THREAD);
2726 }
2727 
2728 oop java_lang_reflect_Method::clazz(oop reflect) {
2729   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2730   return reflect->obj_field(clazz_offset);
2731 }
2732 
2733 void java_lang_reflect_Method::set_clazz(oop reflect, oop value) {
2734   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2735    reflect->obj_field_put(clazz_offset, value);
2736 }
2737 
2738 int java_lang_reflect_Method::slot(oop reflect) {
2739   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2740   return reflect->int_field(slot_offset);
2741 }
2742 
2743 void java_lang_reflect_Method::set_slot(oop reflect, int value) {
2744   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2745   reflect->int_field_put(slot_offset, value);
2746 }
2747 
2748 oop java_lang_reflect_Method::name(oop method) {
2749   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2750   return method->obj_field(name_offset);
2751 }
2752 
2753 void java_lang_reflect_Method::set_name(oop method, oop value) {
2754   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2755   method->obj_field_put(name_offset, value);
2756 }
2757 
2758 oop java_lang_reflect_Method::return_type(oop method) {
2759   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2760   return method->obj_field(returnType_offset);
2761 }
2762 
2763 void java_lang_reflect_Method::set_return_type(oop method, oop value) {
2764   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2765   method->obj_field_put(returnType_offset, value);
2766 }
2767 
2768 oop java_lang_reflect_Method::parameter_types(oop method) {
2769   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2770   return method->obj_field(parameterTypes_offset);
2771 }
2772 
2773 void java_lang_reflect_Method::set_parameter_types(oop method, oop value) {
2774   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2775   method->obj_field_put(parameterTypes_offset, value);
2776 }
2777 
2778 oop java_lang_reflect_Method::exception_types(oop method) {
2779   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2780   return method->obj_field(exceptionTypes_offset);
2781 }
2782 
2783 void java_lang_reflect_Method::set_exception_types(oop method, oop value) {
2784   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2785   method->obj_field_put(exceptionTypes_offset, value);
2786 }
2787 
2788 int java_lang_reflect_Method::modifiers(oop method) {
2789   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2790   return method->int_field(modifiers_offset);
2791 }
2792 
2793 void java_lang_reflect_Method::set_modifiers(oop method, int value) {
2794   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2795   method->int_field_put(modifiers_offset, value);
2796 }
2797 
2798 bool java_lang_reflect_Method::has_signature_field() {
2799   return (signature_offset >= 0);
2800 }
2801 
2802 oop java_lang_reflect_Method::signature(oop method) {
2803   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2804   assert(has_signature_field(), "signature field must be present");
2805   return method->obj_field(signature_offset);
2806 }
2807 
2808 void java_lang_reflect_Method::set_signature(oop method, oop value) {
2809   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2810   assert(has_signature_field(), "signature field must be present");
2811   method->obj_field_put(signature_offset, value);
2812 }
2813 
2814 bool java_lang_reflect_Method::has_annotations_field() {
2815   return (annotations_offset >= 0);
2816 }
2817 
2818 oop java_lang_reflect_Method::annotations(oop method) {
2819   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2820   assert(has_annotations_field(), "annotations field must be present");
2821   return method->obj_field(annotations_offset);
2822 }
2823 
2824 void java_lang_reflect_Method::set_annotations(oop method, oop value) {
2825   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2826   assert(has_annotations_field(), "annotations field must be present");
2827   method->obj_field_put(annotations_offset, value);
2828 }
2829 
2830 bool java_lang_reflect_Method::has_parameter_annotations_field() {
2831   return (parameter_annotations_offset >= 0);
2832 }
2833 
2834 oop java_lang_reflect_Method::parameter_annotations(oop method) {
2835   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2836   assert(has_parameter_annotations_field(), "parameter annotations field must be present");
2837   return method->obj_field(parameter_annotations_offset);
2838 }
2839 
2840 void java_lang_reflect_Method::set_parameter_annotations(oop method, oop value) {
2841   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2842   assert(has_parameter_annotations_field(), "parameter annotations field must be present");
2843   method->obj_field_put(parameter_annotations_offset, value);
2844 }
2845 
2846 bool java_lang_reflect_Method::has_annotation_default_field() {
2847   return (annotation_default_offset >= 0);
2848 }
2849 
2850 oop java_lang_reflect_Method::annotation_default(oop method) {
2851   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2852   assert(has_annotation_default_field(), "annotation default field must be present");
2853   return method->obj_field(annotation_default_offset);
2854 }
2855 
2856 void java_lang_reflect_Method::set_annotation_default(oop method, oop value) {
2857   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2858   assert(has_annotation_default_field(), "annotation default field must be present");
2859   method->obj_field_put(annotation_default_offset, value);
2860 }
2861 
2862 bool java_lang_reflect_Method::has_type_annotations_field() {
2863   return (type_annotations_offset >= 0);
2864 }
2865 
2866 oop java_lang_reflect_Method::type_annotations(oop method) {
2867   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2868   assert(has_type_annotations_field(), "type_annotations field must be present");
2869   return method->obj_field(type_annotations_offset);
2870 }
2871 
2872 void java_lang_reflect_Method::set_type_annotations(oop method, oop value) {
2873   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2874   assert(has_type_annotations_field(), "type_annotations field must be present");
2875   method->obj_field_put(type_annotations_offset, value);
2876 }
2877 
2878 #define CONSTRUCTOR_FIELDS_DO(macro) \
2879   macro(clazz_offset,          k, vmSymbols::clazz_name(),          class_signature,       false); \
2880   macro(parameterTypes_offset, k, vmSymbols::parameterTypes_name(), class_array_signature, false); \
2881   macro(exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), class_array_signature, false); \
2882   macro(slot_offset,           k, vmSymbols::slot_name(),           int_signature,         false); \
2883   macro(modifiers_offset,      k, vmSymbols::modifiers_name(),      int_signature,         false); \
2884   macro##_OPTIONAL(signature_offset,             k, vmSymbols::signature_name(),             string_signature); \
2885   macro##_OPTIONAL(annotations_offset,           k, vmSymbols::annotations_name(),           byte_array_signature); \
2886   macro##_OPTIONAL(parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), byte_array_signature); \
2887   macro##_OPTIONAL(type_annotations_offset,      k, vmSymbols::type_annotations_name(),      byte_array_signature)
2888 
2889 
2890 void java_lang_reflect_Constructor::compute_offsets() {
2891   InstanceKlass* k = SystemDictionary::reflect_Constructor_klass();
2892   // The generic signature and annotations fields are only present in 1.5
2893   signature_offset = -1;
2894   annotations_offset = -1;
2895   parameter_annotations_offset = -1;
2896   type_annotations_offset = -1;
2897   CONSTRUCTOR_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2898 }
2899 
2900 #if INCLUDE_CDS
2901 void java_lang_reflect_Constructor::serialize(SerializeClosure* f) {
2902   CONSTRUCTOR_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2903 }
2904 #endif
2905 
2906 Handle java_lang_reflect_Constructor::create(TRAPS) {
2907   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2908   Symbol* name = vmSymbols::java_lang_reflect_Constructor();
2909   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
2910   InstanceKlass* ik = InstanceKlass::cast(k);
2911   // Ensure it is initialized
2912   ik->initialize(CHECK_NH);
2913   return ik->allocate_instance_handle(THREAD);
2914 }
2915 
2916 oop java_lang_reflect_Constructor::clazz(oop reflect) {
2917   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2918   return reflect->obj_field(clazz_offset);
2919 }
2920 
2921 void java_lang_reflect_Constructor::set_clazz(oop reflect, oop value) {
2922   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2923    reflect->obj_field_put(clazz_offset, value);
2924 }
2925 
2926 oop java_lang_reflect_Constructor::parameter_types(oop constructor) {
2927   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2928   return constructor->obj_field(parameterTypes_offset);
2929 }
2930 
2931 void java_lang_reflect_Constructor::set_parameter_types(oop constructor, oop value) {
2932   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2933   constructor->obj_field_put(parameterTypes_offset, value);
2934 }
2935 
2936 oop java_lang_reflect_Constructor::exception_types(oop constructor) {
2937   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2938   return constructor->obj_field(exceptionTypes_offset);
2939 }
2940 
2941 void java_lang_reflect_Constructor::set_exception_types(oop constructor, oop value) {
2942   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2943   constructor->obj_field_put(exceptionTypes_offset, value);
2944 }
2945 
2946 int java_lang_reflect_Constructor::slot(oop reflect) {
2947   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2948   return reflect->int_field(slot_offset);
2949 }
2950 
2951 void java_lang_reflect_Constructor::set_slot(oop reflect, int value) {
2952   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2953   reflect->int_field_put(slot_offset, value);
2954 }
2955 
2956 int java_lang_reflect_Constructor::modifiers(oop constructor) {
2957   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2958   return constructor->int_field(modifiers_offset);
2959 }
2960 
2961 void java_lang_reflect_Constructor::set_modifiers(oop constructor, int value) {
2962   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2963   constructor->int_field_put(modifiers_offset, value);
2964 }
2965 
2966 bool java_lang_reflect_Constructor::has_signature_field() {
2967   return (signature_offset >= 0);
2968 }
2969 
2970 oop java_lang_reflect_Constructor::signature(oop constructor) {
2971   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2972   assert(has_signature_field(), "signature field must be present");
2973   return constructor->obj_field(signature_offset);
2974 }
2975 
2976 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) {
2977   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2978   assert(has_signature_field(), "signature field must be present");
2979   constructor->obj_field_put(signature_offset, value);
2980 }
2981 
2982 bool java_lang_reflect_Constructor::has_annotations_field() {
2983   return (annotations_offset >= 0);
2984 }
2985 
2986 oop java_lang_reflect_Constructor::annotations(oop constructor) {
2987   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2988   assert(has_annotations_field(), "annotations field must be present");
2989   return constructor->obj_field(annotations_offset);
2990 }
2991 
2992 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) {
2993   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2994   assert(has_annotations_field(), "annotations field must be present");
2995   constructor->obj_field_put(annotations_offset, value);
2996 }
2997 
2998 bool java_lang_reflect_Constructor::has_parameter_annotations_field() {
2999   return (parameter_annotations_offset >= 0);
3000 }
3001 
3002 oop java_lang_reflect_Constructor::parameter_annotations(oop method) {
3003   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3004   assert(has_parameter_annotations_field(), "parameter annotations field must be present");
3005   return method->obj_field(parameter_annotations_offset);
3006 }
3007 
3008 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) {
3009   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3010   assert(has_parameter_annotations_field(), "parameter annotations field must be present");
3011   method->obj_field_put(parameter_annotations_offset, value);
3012 }
3013 
3014 bool java_lang_reflect_Constructor::has_type_annotations_field() {
3015   return (type_annotations_offset >= 0);
3016 }
3017 
3018 oop java_lang_reflect_Constructor::type_annotations(oop constructor) {
3019   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3020   assert(has_type_annotations_field(), "type_annotations field must be present");
3021   return constructor->obj_field(type_annotations_offset);
3022 }
3023 
3024 void java_lang_reflect_Constructor::set_type_annotations(oop constructor, oop value) {
3025   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3026   assert(has_type_annotations_field(), "type_annotations field must be present");
3027   constructor->obj_field_put(type_annotations_offset, value);
3028 }
3029 
3030 #define FIELD_FIELDS_DO(macro) \
3031   macro(clazz_offset,     k, vmSymbols::clazz_name(),     class_signature,  false); \
3032   macro(name_offset,      k, vmSymbols::name_name(),      string_signature, false); \
3033   macro(type_offset,      k, vmSymbols::type_name(),      class_signature,  false); \
3034   macro(slot_offset,      k, vmSymbols::slot_name(),      int_signature,    false); \
3035   macro(modifiers_offset, k, vmSymbols::modifiers_name(), int_signature,    false); \
3036   macro##_OPTIONAL(signature_offset,        k, vmSymbols::signature_name(), string_signature); \
3037   macro##_OPTIONAL(annotations_offset,      k, vmSymbols::annotations_name(),  byte_array_signature); \
3038   macro##_OPTIONAL(type_annotations_offset, k, vmSymbols::type_annotations_name(),  byte_array_signature)
3039 
3040 void java_lang_reflect_Field::compute_offsets() {
3041   InstanceKlass* k = SystemDictionary::reflect_Field_klass();
3042   // The generic signature and annotations fields are only present in 1.5
3043   signature_offset = -1;
3044   annotations_offset = -1;
3045   type_annotations_offset = -1;
3046   FIELD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3047 }
3048 
3049 #if INCLUDE_CDS
3050 void java_lang_reflect_Field::serialize(SerializeClosure* f) {
3051   FIELD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3052 }
3053 #endif
3054 
3055 Handle java_lang_reflect_Field::create(TRAPS) {
3056   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3057   Symbol* name = vmSymbols::java_lang_reflect_Field();
3058   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3059   InstanceKlass* ik = InstanceKlass::cast(k);
3060   // Ensure it is initialized
3061   ik->initialize(CHECK_NH);
3062   return ik->allocate_instance_handle(THREAD);
3063 }
3064 
3065 oop java_lang_reflect_Field::clazz(oop reflect) {
3066   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3067   return reflect->obj_field(clazz_offset);
3068 }
3069 
3070 void java_lang_reflect_Field::set_clazz(oop reflect, oop value) {
3071   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3072    reflect->obj_field_put(clazz_offset, value);
3073 }
3074 
3075 oop java_lang_reflect_Field::name(oop field) {
3076   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3077   return field->obj_field(name_offset);
3078 }
3079 
3080 void java_lang_reflect_Field::set_name(oop field, oop value) {
3081   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3082   field->obj_field_put(name_offset, value);
3083 }
3084 
3085 oop java_lang_reflect_Field::type(oop field) {
3086   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3087   return field->obj_field(type_offset);
3088 }
3089 
3090 void java_lang_reflect_Field::set_type(oop field, oop value) {
3091   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3092   field->obj_field_put(type_offset, value);
3093 }
3094 
3095 int java_lang_reflect_Field::slot(oop reflect) {
3096   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3097   return reflect->int_field(slot_offset);
3098 }
3099 
3100 void java_lang_reflect_Field::set_slot(oop reflect, int value) {
3101   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3102   reflect->int_field_put(slot_offset, value);
3103 }
3104 
3105 int java_lang_reflect_Field::modifiers(oop field) {
3106   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3107   return field->int_field(modifiers_offset);
3108 }
3109 
3110 void java_lang_reflect_Field::set_modifiers(oop field, int value) {
3111   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3112   field->int_field_put(modifiers_offset, value);
3113 }
3114 
3115 bool java_lang_reflect_Field::has_signature_field() {
3116   return (signature_offset >= 0);
3117 }
3118 
3119 oop java_lang_reflect_Field::signature(oop field) {
3120   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3121   assert(has_signature_field(), "signature field must be present");
3122   return field->obj_field(signature_offset);
3123 }
3124 
3125 void java_lang_reflect_Field::set_signature(oop field, oop value) {
3126   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3127   assert(has_signature_field(), "signature field must be present");
3128   field->obj_field_put(signature_offset, value);
3129 }
3130 
3131 bool java_lang_reflect_Field::has_annotations_field() {
3132   return (annotations_offset >= 0);
3133 }
3134 
3135 oop java_lang_reflect_Field::annotations(oop field) {
3136   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3137   assert(has_annotations_field(), "annotations field must be present");
3138   return field->obj_field(annotations_offset);
3139 }
3140 
3141 void java_lang_reflect_Field::set_annotations(oop field, oop value) {
3142   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3143   assert(has_annotations_field(), "annotations field must be present");
3144   field->obj_field_put(annotations_offset, value);
3145 }
3146 
3147 bool java_lang_reflect_Field::has_type_annotations_field() {
3148   return (type_annotations_offset >= 0);
3149 }
3150 
3151 oop java_lang_reflect_Field::type_annotations(oop field) {
3152   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3153   assert(has_type_annotations_field(), "type_annotations field must be present");
3154   return field->obj_field(type_annotations_offset);
3155 }
3156 
3157 void java_lang_reflect_Field::set_type_annotations(oop field, oop value) {
3158   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3159   assert(has_type_annotations_field(), "type_annotations field must be present");
3160   field->obj_field_put(type_annotations_offset, value);
3161 }
3162 
3163 #define CONSTANTPOOL_FIELDS_DO(macro) \
3164   macro(_oop_offset, k, "constantPoolOop", object_signature, false)
3165 
3166 void reflect_ConstantPool::compute_offsets() {
3167   InstanceKlass* k = SystemDictionary::reflect_ConstantPool_klass();
3168   // The field is called ConstantPool* in the sun.reflect.ConstantPool class.
3169   CONSTANTPOOL_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3170 }
3171 
3172 #if INCLUDE_CDS
3173 void reflect_ConstantPool::serialize(SerializeClosure* f) {
3174   CONSTANTPOOL_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3175 }
3176 #endif
3177 
3178 #define PARAMETER_FIELDS_DO(macro) \
3179   macro(name_offset,        k, vmSymbols::name_name(),        string_signature, false); \
3180   macro(modifiers_offset,   k, vmSymbols::modifiers_name(),   int_signature,    false); \
3181   macro(index_offset,       k, vmSymbols::index_name(),       int_signature,    false); \
3182   macro(executable_offset,  k, vmSymbols::executable_name(),  executable_signature, false)
3183 
3184 void java_lang_reflect_Parameter::compute_offsets() {
3185   InstanceKlass* k = SystemDictionary::reflect_Parameter_klass();
3186   PARAMETER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3187 }
3188 
3189 #if INCLUDE_CDS
3190 void java_lang_reflect_Parameter::serialize(SerializeClosure* f) {
3191   PARAMETER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3192 }
3193 #endif
3194 
3195 Handle java_lang_reflect_Parameter::create(TRAPS) {
3196   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3197   Symbol* name = vmSymbols::java_lang_reflect_Parameter();
3198   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3199   InstanceKlass* ik = InstanceKlass::cast(k);
3200   // Ensure it is initialized
3201   ik->initialize(CHECK_NH);
3202   return ik->allocate_instance_handle(THREAD);
3203 }
3204 
3205 oop java_lang_reflect_Parameter::name(oop param) {
3206   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3207   return param->obj_field(name_offset);
3208 }
3209 
3210 void java_lang_reflect_Parameter::set_name(oop param, oop value) {
3211   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3212   param->obj_field_put(name_offset, value);
3213 }
3214 
3215 int java_lang_reflect_Parameter::modifiers(oop param) {
3216   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3217   return param->int_field(modifiers_offset);
3218 }
3219 
3220 void java_lang_reflect_Parameter::set_modifiers(oop param, int value) {
3221   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3222   param->int_field_put(modifiers_offset, value);
3223 }
3224 
3225 int java_lang_reflect_Parameter::index(oop param) {
3226   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3227   return param->int_field(index_offset);
3228 }
3229 
3230 void java_lang_reflect_Parameter::set_index(oop param, int value) {
3231   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3232   param->int_field_put(index_offset, value);
3233 }
3234 
3235 oop java_lang_reflect_Parameter::executable(oop param) {
3236   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3237   return param->obj_field(executable_offset);
3238 }
3239 
3240 void java_lang_reflect_Parameter::set_executable(oop param, oop value) {
3241   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3242   param->obj_field_put(executable_offset, value);
3243 }
3244 
3245 
3246 int java_lang_Module::loader_offset;
3247 int java_lang_Module::name_offset;
3248 int java_lang_Module::_module_entry_offset = -1;
3249 
3250 Handle java_lang_Module::create(Handle loader, Handle module_name, TRAPS) {
3251   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3252 
3253   Symbol* name = vmSymbols::java_lang_Module();
3254   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3255   InstanceKlass* ik = InstanceKlass::cast(k);
3256   Handle jlmh = ik->allocate_instance_handle(CHECK_NH);
3257   JavaValue result(T_VOID);
3258   JavaCalls::call_special(&result, jlmh, ik,
3259                           vmSymbols::object_initializer_name(),
3260                           vmSymbols::java_lang_module_init_signature(),
3261                           loader, module_name, CHECK_NH);
3262   return jlmh;
3263 }
3264 
3265 #define MODULE_FIELDS_DO(macro) \
3266   macro(loader_offset,  k, vmSymbols::loader_name(),  classloader_signature, false); \
3267   macro(name_offset,    k, vmSymbols::name_name(),    string_signature,      false)
3268 
3269 void java_lang_Module::compute_offsets() {
3270   InstanceKlass* k = SystemDictionary::Module_klass();
3271   MODULE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3272   MODULE_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3273 }
3274 
3275 #if INCLUDE_CDS
3276 void java_lang_Module::serialize(SerializeClosure* f) {
3277   MODULE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3278   MODULE_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3279 }
3280 #endif
3281 
3282 oop java_lang_Module::loader(oop module) {
3283   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3284   return module->obj_field(loader_offset);
3285 }
3286 
3287 void java_lang_Module::set_loader(oop module, oop value) {
3288   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3289   module->obj_field_put(loader_offset, value);
3290 }
3291 
3292 oop java_lang_Module::name(oop module) {
3293   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3294   return module->obj_field(name_offset);
3295 }
3296 
3297 void java_lang_Module::set_name(oop module, oop value) {
3298   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3299   module->obj_field_put(name_offset, value);
3300 }
3301 
3302 ModuleEntry* java_lang_Module::module_entry(oop module) {
3303   assert(_module_entry_offset != -1, "Uninitialized module_entry_offset");
3304   assert(module != NULL, "module can't be null");
3305   assert(oopDesc::is_oop(module), "module must be oop");
3306 
3307   ModuleEntry* module_entry = (ModuleEntry*)module->address_field(_module_entry_offset);
3308   if (module_entry == NULL) {
3309     // If the inject field containing the ModuleEntry* is null then return the
3310     // class loader's unnamed module.
3311     oop loader = java_lang_Module::loader(module);
3312     Handle h_loader = Handle(Thread::current(), loader);
3313     ClassLoaderData* loader_cld = SystemDictionary::register_loader(h_loader);
3314     return loader_cld->unnamed_module();
3315   }
3316   return module_entry;
3317 }
3318 
3319 void java_lang_Module::set_module_entry(oop module, ModuleEntry* module_entry) {
3320   assert(_module_entry_offset != -1, "Uninitialized module_entry_offset");
3321   assert(module != NULL, "module can't be null");
3322   assert(oopDesc::is_oop(module), "module must be oop");
3323   module->address_field_put(_module_entry_offset, (address)module_entry);
3324 }
3325 
3326 Handle reflect_ConstantPool::create(TRAPS) {
3327   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3328   InstanceKlass* k = SystemDictionary::reflect_ConstantPool_klass();
3329   // Ensure it is initialized
3330   k->initialize(CHECK_NH);
3331   return k->allocate_instance_handle(THREAD);
3332 }
3333 
3334 
3335 void reflect_ConstantPool::set_cp(oop reflect, ConstantPool* value) {
3336   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3337   oop mirror = value->pool_holder()->java_mirror();
3338   // Save the mirror to get back the constant pool.
3339   reflect->obj_field_put(_oop_offset, mirror);
3340 }
3341 
3342 ConstantPool* reflect_ConstantPool::get_cp(oop reflect) {
3343   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3344 
3345   oop mirror = reflect->obj_field(_oop_offset);
3346   Klass* k = java_lang_Class::as_Klass(mirror);
3347   assert(k->is_instance_klass(), "Must be");
3348 
3349   // Get the constant pool back from the klass.  Since class redefinition
3350   // merges the new constant pool into the old, this is essentially the
3351   // same constant pool as the original.  If constant pool merging is
3352   // no longer done in the future, this will have to change to save
3353   // the original.
3354   return InstanceKlass::cast(k)->constants();
3355 }
3356 
3357 #define UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(macro) \
3358   macro(_base_offset, k, "base", object_signature, false)
3359 
3360 void reflect_UnsafeStaticFieldAccessorImpl::compute_offsets() {
3361   InstanceKlass* k = SystemDictionary::reflect_UnsafeStaticFieldAccessorImpl_klass();
3362   UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3363 }
3364 
3365 #if INCLUDE_CDS
3366 void reflect_UnsafeStaticFieldAccessorImpl::serialize(SerializeClosure* f) {
3367   UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3368 }
3369 #endif
3370 
3371 oop java_lang_boxing_object::initialize_and_allocate(BasicType type, TRAPS) {
3372   Klass* k = SystemDictionary::box_klass(type);
3373   if (k == NULL)  return NULL;
3374   InstanceKlass* ik = InstanceKlass::cast(k);
3375   if (!ik->is_initialized())  ik->initialize(CHECK_0);
3376   return ik->allocate_instance(THREAD);
3377 }
3378 
3379 
3380 oop java_lang_boxing_object::create(BasicType type, jvalue* value, TRAPS) {
3381   oop box = initialize_and_allocate(type, CHECK_0);
3382   if (box == NULL)  return NULL;
3383   switch (type) {
3384     case T_BOOLEAN:
3385       box->bool_field_put(value_offset, value->z);
3386       break;
3387     case T_CHAR:
3388       box->char_field_put(value_offset, value->c);
3389       break;
3390     case T_FLOAT:
3391       box->float_field_put(value_offset, value->f);
3392       break;
3393     case T_DOUBLE:
3394       box->double_field_put(long_value_offset, value->d);
3395       break;
3396     case T_BYTE:
3397       box->byte_field_put(value_offset, value->b);
3398       break;
3399     case T_SHORT:
3400       box->short_field_put(value_offset, value->s);
3401       break;
3402     case T_INT:
3403       box->int_field_put(value_offset, value->i);
3404       break;
3405     case T_LONG:
3406       box->long_field_put(long_value_offset, value->j);
3407       break;
3408     default:
3409       return NULL;
3410   }
3411   return box;
3412 }
3413 
3414 
3415 BasicType java_lang_boxing_object::basic_type(oop box) {
3416   if (box == NULL)  return T_ILLEGAL;
3417   BasicType type = SystemDictionary::box_klass_type(box->klass());
3418   if (type == T_OBJECT)         // 'unknown' value returned by SD::bkt
3419     return T_ILLEGAL;
3420   return type;
3421 }
3422 
3423 
3424 BasicType java_lang_boxing_object::get_value(oop box, jvalue* value) {
3425   BasicType type = SystemDictionary::box_klass_type(box->klass());
3426   switch (type) {
3427   case T_BOOLEAN:
3428     value->z = box->bool_field(value_offset);
3429     break;
3430   case T_CHAR:
3431     value->c = box->char_field(value_offset);
3432     break;
3433   case T_FLOAT:
3434     value->f = box->float_field(value_offset);
3435     break;
3436   case T_DOUBLE:
3437     value->d = box->double_field(long_value_offset);
3438     break;
3439   case T_BYTE:
3440     value->b = box->byte_field(value_offset);
3441     break;
3442   case T_SHORT:
3443     value->s = box->short_field(value_offset);
3444     break;
3445   case T_INT:
3446     value->i = box->int_field(value_offset);
3447     break;
3448   case T_LONG:
3449     value->j = box->long_field(long_value_offset);
3450     break;
3451   default:
3452     return T_ILLEGAL;
3453   } // end switch
3454   return type;
3455 }
3456 
3457 
3458 BasicType java_lang_boxing_object::set_value(oop box, jvalue* value) {
3459   BasicType type = SystemDictionary::box_klass_type(box->klass());
3460   switch (type) {
3461   case T_BOOLEAN:
3462     box->bool_field_put(value_offset, value->z);
3463     break;
3464   case T_CHAR:
3465     box->char_field_put(value_offset, value->c);
3466     break;
3467   case T_FLOAT:
3468     box->float_field_put(value_offset, value->f);
3469     break;
3470   case T_DOUBLE:
3471     box->double_field_put(long_value_offset, value->d);
3472     break;
3473   case T_BYTE:
3474     box->byte_field_put(value_offset, value->b);
3475     break;
3476   case T_SHORT:
3477     box->short_field_put(value_offset, value->s);
3478     break;
3479   case T_INT:
3480     box->int_field_put(value_offset, value->i);
3481     break;
3482   case T_LONG:
3483     box->long_field_put(long_value_offset, value->j);
3484     break;
3485   default:
3486     return T_ILLEGAL;
3487   } // end switch
3488   return type;
3489 }
3490 
3491 
3492 void java_lang_boxing_object::print(BasicType type, jvalue* value, outputStream* st) {
3493   switch (type) {
3494   case T_BOOLEAN:   st->print("%s", value->z ? "true" : "false");   break;
3495   case T_CHAR:      st->print("%d", value->c);                      break;
3496   case T_BYTE:      st->print("%d", value->b);                      break;
3497   case T_SHORT:     st->print("%d", value->s);                      break;
3498   case T_INT:       st->print("%d", value->i);                      break;
3499   case T_LONG:      st->print(JLONG_FORMAT, value->j);              break;
3500   case T_FLOAT:     st->print("%f", value->f);                      break;
3501   case T_DOUBLE:    st->print("%lf", value->d);                     break;
3502   default:          st->print("type %d?", type);                    break;
3503   }
3504 }
3505 
3506 // Support for java_lang_ref_Reference
3507 
3508 bool java_lang_ref_Reference::is_referent_field(oop obj, ptrdiff_t offset) {
3509   assert(obj != NULL, "sanity");
3510   if (offset != java_lang_ref_Reference::referent_offset) {
3511     return false;
3512   }
3513 
3514   Klass* k = obj->klass();
3515   if (!k->is_instance_klass()) {
3516     return false;
3517   }
3518 
3519   InstanceKlass* ik = InstanceKlass::cast(obj->klass());
3520   bool is_reference = ik->reference_type() != REF_NONE;
3521   assert(!is_reference || ik->is_subclass_of(SystemDictionary::Reference_klass()), "sanity");
3522   return is_reference;
3523 }
3524 
3525 // Support for java_lang_ref_SoftReference
3526 //
3527 
3528 #define SOFTREFERENCE_FIELDS_DO(macro) \
3529   macro(timestamp_offset,    k, "timestamp", long_signature, false); \
3530   macro(static_clock_offset, k, "clock",     long_signature, true)
3531 
3532 void java_lang_ref_SoftReference::compute_offsets() {
3533   InstanceKlass* k = SystemDictionary::SoftReference_klass();
3534   SOFTREFERENCE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3535 }
3536 
3537 #if INCLUDE_CDS
3538 void java_lang_ref_SoftReference::serialize(SerializeClosure* f) {
3539   SOFTREFERENCE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3540 }
3541 #endif
3542 
3543 jlong java_lang_ref_SoftReference::timestamp(oop ref) {
3544   return ref->long_field(timestamp_offset);
3545 }
3546 
3547 jlong java_lang_ref_SoftReference::clock() {
3548   InstanceKlass* ik = SystemDictionary::SoftReference_klass();
3549   oop base = ik->static_field_base_raw();
3550   return base->long_field(static_clock_offset);
3551 }
3552 
3553 void java_lang_ref_SoftReference::set_clock(jlong value) {
3554   InstanceKlass* ik = SystemDictionary::SoftReference_klass();
3555   oop base = ik->static_field_base_raw();
3556   base->long_field_put(static_clock_offset, value);
3557 }
3558 
3559 // Support for java_lang_invoke_DirectMethodHandle
3560 
3561 int java_lang_invoke_DirectMethodHandle::_member_offset;
3562 
3563 oop java_lang_invoke_DirectMethodHandle::member(oop dmh) {
3564   oop member_name = NULL;
3565   assert(oopDesc::is_oop(dmh) && java_lang_invoke_DirectMethodHandle::is_instance(dmh),
3566          "a DirectMethodHandle oop is expected");
3567   return dmh->obj_field(member_offset_in_bytes());
3568 }
3569 
3570 #define DIRECTMETHODHANDLE_FIELDS_DO(macro) \
3571   macro(_member_offset, k, "member", java_lang_invoke_MemberName_signature, false)
3572 
3573 void java_lang_invoke_DirectMethodHandle::compute_offsets() {
3574   InstanceKlass* k = SystemDictionary::DirectMethodHandle_klass();
3575   DIRECTMETHODHANDLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3576 }
3577 
3578 #if INCLUDE_CDS
3579 void java_lang_invoke_DirectMethodHandle::serialize(SerializeClosure* f) {
3580   DIRECTMETHODHANDLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3581 }
3582 #endif
3583 
3584 // Support for java_lang_invoke_MethodHandle
3585 
3586 int java_lang_invoke_MethodHandle::_type_offset;
3587 int java_lang_invoke_MethodHandle::_form_offset;
3588 
3589 int java_lang_invoke_MemberName::_clazz_offset;
3590 int java_lang_invoke_MemberName::_name_offset;
3591 int java_lang_invoke_MemberName::_type_offset;
3592 int java_lang_invoke_MemberName::_flags_offset;
3593 int java_lang_invoke_MemberName::_method_offset;
3594 int java_lang_invoke_MemberName::_vmindex_offset;
3595 
3596 int java_lang_invoke_ResolvedMethodName::_vmtarget_offset;
3597 int java_lang_invoke_ResolvedMethodName::_vmholder_offset;
3598 
3599 int java_lang_invoke_LambdaForm::_vmentry_offset;
3600 
3601 #define METHODHANDLE_FIELDS_DO(macro) \
3602   macro(_type_offset, k, vmSymbols::type_name(), java_lang_invoke_MethodType_signature, false); \
3603   macro(_form_offset, k, "form",                 java_lang_invoke_LambdaForm_signature, false)
3604 
3605 void java_lang_invoke_MethodHandle::compute_offsets() {
3606   InstanceKlass* k = SystemDictionary::MethodHandle_klass();
3607   METHODHANDLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3608 }
3609 
3610 #if INCLUDE_CDS
3611 void java_lang_invoke_MethodHandle::serialize(SerializeClosure* f) {
3612   METHODHANDLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3613 }
3614 #endif
3615 
3616 #define MEMBERNAME_FIELDS_DO(macro) \
3617   macro(_clazz_offset,   k, vmSymbols::clazz_name(),   class_signature,  false); \
3618   macro(_name_offset,    k, vmSymbols::name_name(),    string_signature, false); \
3619   macro(_type_offset,    k, vmSymbols::type_name(),    object_signature, false); \
3620   macro(_flags_offset,   k, vmSymbols::flags_name(),   int_signature,    false); \
3621   macro(_method_offset,  k, vmSymbols::method_name(),  java_lang_invoke_ResolvedMethodName_signature, false)
3622 
3623 void java_lang_invoke_MemberName::compute_offsets() {
3624   InstanceKlass* k = SystemDictionary::MemberName_klass();
3625   MEMBERNAME_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3626   MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3627 }
3628 
3629 #if INCLUDE_CDS
3630 void java_lang_invoke_MemberName::serialize(SerializeClosure* f) {
3631   MEMBERNAME_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3632   MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3633 }
3634 #endif
3635 
3636 void java_lang_invoke_ResolvedMethodName::compute_offsets() {
3637   InstanceKlass* k = SystemDictionary::ResolvedMethodName_klass();
3638   assert(k != NULL, "jdk mismatch");
3639   RESOLVEDMETHOD_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3640 }
3641 
3642 #if INCLUDE_CDS
3643 void java_lang_invoke_ResolvedMethodName::serialize(SerializeClosure* f) {
3644   RESOLVEDMETHOD_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3645 }
3646 #endif
3647 
3648 #define LAMBDAFORM_FIELDS_DO(macro) \
3649   macro(_vmentry_offset, k, "vmentry", java_lang_invoke_MemberName_signature, false)
3650 
3651 void java_lang_invoke_LambdaForm::compute_offsets() {
3652   InstanceKlass* k = SystemDictionary::LambdaForm_klass();
3653   assert (k != NULL, "jdk mismatch");
3654   LAMBDAFORM_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3655 }
3656 
3657 #if INCLUDE_CDS
3658 void java_lang_invoke_LambdaForm::serialize(SerializeClosure* f) {
3659   LAMBDAFORM_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3660 }
3661 #endif
3662 
3663 bool java_lang_invoke_LambdaForm::is_instance(oop obj) {
3664   return obj != NULL && is_subclass(obj->klass());
3665 }
3666 
3667 
3668 oop java_lang_invoke_MethodHandle::type(oop mh) {
3669   return mh->obj_field(_type_offset);
3670 }
3671 
3672 void java_lang_invoke_MethodHandle::set_type(oop mh, oop mtype) {
3673   mh->obj_field_put(_type_offset, mtype);
3674 }
3675 
3676 oop java_lang_invoke_MethodHandle::form(oop mh) {
3677   assert(_form_offset != 0, "");
3678   return mh->obj_field(_form_offset);
3679 }
3680 
3681 void java_lang_invoke_MethodHandle::set_form(oop mh, oop lform) {
3682   assert(_form_offset != 0, "");
3683   mh->obj_field_put(_form_offset, lform);
3684 }
3685 
3686 /// MemberName accessors
3687 
3688 oop java_lang_invoke_MemberName::clazz(oop mname) {
3689   assert(is_instance(mname), "wrong type");
3690   return mname->obj_field(_clazz_offset);
3691 }
3692 
3693 void java_lang_invoke_MemberName::set_clazz(oop mname, oop clazz) {
3694   assert(is_instance(mname), "wrong type");
3695   mname->obj_field_put(_clazz_offset, clazz);
3696 }
3697 
3698 oop java_lang_invoke_MemberName::name(oop mname) {
3699   assert(is_instance(mname), "wrong type");
3700   return mname->obj_field(_name_offset);
3701 }
3702 
3703 void java_lang_invoke_MemberName::set_name(oop mname, oop name) {
3704   assert(is_instance(mname), "wrong type");
3705   mname->obj_field_put(_name_offset, name);
3706 }
3707 
3708 oop java_lang_invoke_MemberName::type(oop mname) {
3709   assert(is_instance(mname), "wrong type");
3710   return mname->obj_field(_type_offset);
3711 }
3712 
3713 void java_lang_invoke_MemberName::set_type(oop mname, oop type) {
3714   assert(is_instance(mname), "wrong type");
3715   mname->obj_field_put(_type_offset, type);
3716 }
3717 
3718 int java_lang_invoke_MemberName::flags(oop mname) {
3719   assert(is_instance(mname), "wrong type");
3720   return mname->int_field(_flags_offset);
3721 }
3722 
3723 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) {
3724   assert(is_instance(mname), "wrong type");
3725   mname->int_field_put(_flags_offset, flags);
3726 }
3727 
3728 
3729 // Return vmtarget from ResolvedMethodName method field through indirection
3730 Method* java_lang_invoke_MemberName::vmtarget(oop mname) {
3731   assert(is_instance(mname), "wrong type");
3732   oop method = mname->obj_field(_method_offset);
3733   return method == NULL ? NULL : java_lang_invoke_ResolvedMethodName::vmtarget(method);
3734 }
3735 
3736 bool java_lang_invoke_MemberName::is_method(oop mname) {
3737   assert(is_instance(mname), "must be MemberName");
3738   return (flags(mname) & (MN_IS_METHOD | MN_IS_CONSTRUCTOR)) > 0;
3739 }
3740 
3741 void java_lang_invoke_MemberName::set_method(oop mname, oop resolved_method) {
3742   assert(is_instance(mname), "wrong type");
3743   mname->obj_field_put(_method_offset, resolved_method);
3744 }
3745 
3746 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) {
3747   assert(is_instance(mname), "wrong type");
3748   return (intptr_t) mname->address_field(_vmindex_offset);
3749 }
3750 
3751 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
3752   assert(is_instance(mname), "wrong type");
3753   mname->address_field_put(_vmindex_offset, (address) index);
3754 }
3755 
3756 
3757 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) {
3758   assert(is_instance(resolved_method), "wrong type");
3759   Method* m = (Method*)resolved_method->address_field(_vmtarget_offset);
3760   assert(m->is_method(), "must be");
3761   return m;
3762 }
3763 
3764 // Used by redefinition to change Method* to new Method* with same hash (name, signature)
3765 void java_lang_invoke_ResolvedMethodName::set_vmtarget(oop resolved_method, Method* m) {
3766   assert(is_instance(resolved_method), "wrong type");
3767   resolved_method->address_field_put(_vmtarget_offset, (address)m);
3768 }
3769 
3770 oop java_lang_invoke_ResolvedMethodName::find_resolved_method(const methodHandle& m, TRAPS) {
3771   // lookup ResolvedMethod oop in the table, or create a new one and intern it
3772   oop resolved_method = ResolvedMethodTable::find_method(m());
3773   if (resolved_method == NULL) {
3774     InstanceKlass* k = SystemDictionary::ResolvedMethodName_klass();
3775     if (!k->is_initialized()) {
3776       k->initialize(CHECK_NULL);
3777     }
3778     oop new_resolved_method = k->allocate_instance(CHECK_NULL);
3779     new_resolved_method->address_field_put(_vmtarget_offset, (address)m());
3780     // Add a reference to the loader (actually mirror because anonymous classes will not have
3781     // distinct loaders) to ensure the metadata is kept alive.
3782     // This mirror may be different than the one in clazz field.
3783     new_resolved_method->obj_field_put(_vmholder_offset, m->method_holder()->java_mirror());
3784     resolved_method = ResolvedMethodTable::add_method(Handle(THREAD, new_resolved_method));
3785   }
3786   return resolved_method;
3787 }
3788 
3789 oop java_lang_invoke_LambdaForm::vmentry(oop lform) {
3790   assert(is_instance(lform), "wrong type");
3791   return lform->obj_field(_vmentry_offset);
3792 }
3793 
3794 
3795 // Support for java_lang_invoke_MethodType
3796 
3797 int java_lang_invoke_MethodType::_rtype_offset;
3798 int java_lang_invoke_MethodType::_ptypes_offset;
3799 
3800 #define METHODTYPE_FIELDS_DO(macro) \
3801   macro(_rtype_offset,  k, "rtype",  class_signature,       false); \
3802   macro(_ptypes_offset, k, "ptypes", class_array_signature, false)
3803 
3804 void java_lang_invoke_MethodType::compute_offsets() {
3805   InstanceKlass* k = SystemDictionary::MethodType_klass();
3806   METHODTYPE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3807 }
3808 
3809 #if INCLUDE_CDS
3810 void java_lang_invoke_MethodType::serialize(SerializeClosure* f) {
3811   METHODTYPE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3812 }
3813 #endif
3814 
3815 void java_lang_invoke_MethodType::print_signature(oop mt, outputStream* st) {
3816   st->print("(");
3817   objArrayOop pts = ptypes(mt);
3818   for (int i = 0, limit = pts->length(); i < limit; i++) {
3819     java_lang_Class::print_signature(pts->obj_at(i), st);
3820   }
3821   st->print(")");
3822   java_lang_Class::print_signature(rtype(mt), st);
3823 }
3824 
3825 Symbol* java_lang_invoke_MethodType::as_signature(oop mt, bool intern_if_not_found, TRAPS) {
3826   ResourceMark rm;
3827   stringStream buffer(128);
3828   print_signature(mt, &buffer);
3829   const char* sigstr =       buffer.base();
3830   int         siglen = (int) buffer.size();
3831   Symbol *name;
3832   if (!intern_if_not_found) {
3833     name = SymbolTable::probe(sigstr, siglen);
3834   } else {
3835     name = SymbolTable::new_symbol(sigstr, siglen, THREAD);
3836   }
3837   return name;
3838 }
3839 
3840 bool java_lang_invoke_MethodType::equals(oop mt1, oop mt2) {
3841   if (oopDesc::equals(mt1, mt2))
3842     return true;
3843   if (!oopDesc::equals(rtype(mt1), rtype(mt2)))
3844     return false;
3845   if (ptype_count(mt1) != ptype_count(mt2))
3846     return false;
3847   for (int i = ptype_count(mt1) - 1; i >= 0; i--) {
3848     if (!oopDesc::equals(ptype(mt1, i), ptype(mt2, i)))
3849       return false;
3850   }
3851   return true;
3852 }
3853 
3854 oop java_lang_invoke_MethodType::rtype(oop mt) {
3855   assert(is_instance(mt), "must be a MethodType");
3856   return mt->obj_field(_rtype_offset);
3857 }
3858 
3859 objArrayOop java_lang_invoke_MethodType::ptypes(oop mt) {
3860   assert(is_instance(mt), "must be a MethodType");
3861   return (objArrayOop) mt->obj_field(_ptypes_offset);
3862 }
3863 
3864 oop java_lang_invoke_MethodType::ptype(oop mt, int idx) {
3865   return ptypes(mt)->obj_at(idx);
3866 }
3867 
3868 int java_lang_invoke_MethodType::ptype_count(oop mt) {
3869   return ptypes(mt)->length();
3870 }
3871 
3872 int java_lang_invoke_MethodType::ptype_slot_count(oop mt) {
3873   objArrayOop pts = ptypes(mt);
3874   int count = pts->length();
3875   int slots = 0;
3876   for (int i = 0; i < count; i++) {
3877     BasicType bt = java_lang_Class::as_BasicType(pts->obj_at(i));
3878     slots += type2size[bt];
3879   }
3880   return slots;
3881 }
3882 
3883 int java_lang_invoke_MethodType::rtype_slot_count(oop mt) {
3884   BasicType bt = java_lang_Class::as_BasicType(rtype(mt));
3885   return type2size[bt];
3886 }
3887 
3888 
3889 // Support for java_lang_invoke_CallSite
3890 
3891 int java_lang_invoke_CallSite::_target_offset;
3892 int java_lang_invoke_CallSite::_context_offset;
3893 
3894 #define CALLSITE_FIELDS_DO(macro) \
3895   macro(_target_offset,  k, "target", java_lang_invoke_MethodHandle_signature, false); \
3896   macro(_context_offset, k, "context", java_lang_invoke_MethodHandleNatives_CallSiteContext_signature, false)
3897 
3898 void java_lang_invoke_CallSite::compute_offsets() {
3899   InstanceKlass* k = SystemDictionary::CallSite_klass();
3900   CALLSITE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3901 }
3902 
3903 #if INCLUDE_CDS
3904 void java_lang_invoke_CallSite::serialize(SerializeClosure* f) {
3905   CALLSITE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3906 }
3907 #endif
3908 
3909 oop java_lang_invoke_CallSite::context(oop call_site) {
3910   assert(java_lang_invoke_CallSite::is_instance(call_site), "");
3911 
3912   oop dep_oop = call_site->obj_field(_context_offset);
3913   return dep_oop;
3914 }
3915 
3916 // Support for java_lang_invoke_MethodHandleNatives_CallSiteContext
3917 
3918 int java_lang_invoke_MethodHandleNatives_CallSiteContext::_vmdependencies_offset;
3919 
3920 void java_lang_invoke_MethodHandleNatives_CallSiteContext::compute_offsets() {
3921   InstanceKlass* k = SystemDictionary::Context_klass();
3922   CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3923 }
3924 
3925 #if INCLUDE_CDS
3926 void java_lang_invoke_MethodHandleNatives_CallSiteContext::serialize(SerializeClosure* f) {
3927   CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3928 }
3929 #endif
3930 
3931 DependencyContext java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(oop call_site) {
3932   assert(java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(call_site), "");
3933   intptr_t* vmdeps_addr = (intptr_t*)call_site->field_addr(_vmdependencies_offset);
3934   DependencyContext dep_ctx(vmdeps_addr);
3935   return dep_ctx;
3936 }
3937 
3938 // Support for java_security_AccessControlContext
3939 
3940 int java_security_AccessControlContext::_context_offset = 0;
3941 int java_security_AccessControlContext::_privilegedContext_offset = 0;
3942 int java_security_AccessControlContext::_isPrivileged_offset = 0;
3943 int java_security_AccessControlContext::_isAuthorized_offset = -1;
3944 
3945 #define ACCESSCONTROLCONTEXT_FIELDS_DO(macro) \
3946   macro(_context_offset,           k, "context",      protectiondomain_signature, false); \
3947   macro(_privilegedContext_offset, k, "privilegedContext", accesscontrolcontext_signature, false); \
3948   macro(_isPrivileged_offset,      k, "isPrivileged", bool_signature, false); \
3949   macro(_isAuthorized_offset,      k, "isAuthorized", bool_signature, false)
3950 
3951 void java_security_AccessControlContext::compute_offsets() {
3952   assert(_isPrivileged_offset == 0, "offsets should be initialized only once");
3953   InstanceKlass* k = SystemDictionary::AccessControlContext_klass();
3954   ACCESSCONTROLCONTEXT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3955 }
3956 
3957 #if INCLUDE_CDS
3958 void java_security_AccessControlContext::serialize(SerializeClosure* f) {
3959   ACCESSCONTROLCONTEXT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3960 }
3961 #endif
3962 
3963 bool java_security_AccessControlContext::is_authorized(Handle context) {
3964   assert(context.not_null() && context->klass() == SystemDictionary::AccessControlContext_klass(), "Invalid type");
3965   assert(_isAuthorized_offset != -1, "should be set");
3966   return context->bool_field(_isAuthorized_offset) != 0;
3967 }
3968 
3969 oop java_security_AccessControlContext::create(objArrayHandle context, bool isPrivileged, Handle privileged_context, TRAPS) {
3970   assert(_isPrivileged_offset != 0, "offsets should have been initialized");
3971   // Ensure klass is initialized
3972   SystemDictionary::AccessControlContext_klass()->initialize(CHECK_0);
3973   // Allocate result
3974   oop result = SystemDictionary::AccessControlContext_klass()->allocate_instance(CHECK_0);
3975   // Fill in values
3976   result->obj_field_put(_context_offset, context());
3977   result->obj_field_put(_privilegedContext_offset, privileged_context());
3978   result->bool_field_put(_isPrivileged_offset, isPrivileged);
3979   // whitelist AccessControlContexts created by the JVM if present
3980   if (_isAuthorized_offset != -1) {
3981     result->bool_field_put(_isAuthorized_offset, true);
3982   }
3983   return result;
3984 }
3985 
3986 
3987 // Support for java_lang_ClassLoader
3988 
3989 bool java_lang_ClassLoader::offsets_computed = false;
3990 int  java_lang_ClassLoader::_loader_data_offset = -1;
3991 int  java_lang_ClassLoader::parallelCapable_offset = -1;
3992 int  java_lang_ClassLoader::name_offset = -1;
3993 int  java_lang_ClassLoader::unnamedModule_offset = -1;
3994 
3995 ClassLoaderData* java_lang_ClassLoader::loader_data(oop loader) {
3996   assert(loader != NULL && oopDesc::is_oop(loader), "loader must be oop");
3997   return HeapAccess<>::load_at(loader, _loader_data_offset);
3998 }
3999 
4000 ClassLoaderData* java_lang_ClassLoader::cmpxchg_loader_data(ClassLoaderData* new_data, oop loader, ClassLoaderData* expected_data) {
4001   assert(loader != NULL && oopDesc::is_oop(loader), "loader must be oop");
4002   return HeapAccess<>::atomic_cmpxchg_at(new_data, loader, _loader_data_offset, expected_data);
4003 }
4004 
4005 #define CLASSLOADER_FIELDS_DO(macro) \
4006   macro(parallelCapable_offset, k1, "parallelLockMap",      concurrenthashmap_signature, false); \
4007   macro(name_offset,            k1, vmSymbols::name_name(), string_signature, false); \
4008   macro(unnamedModule_offset,   k1, "unnamedModule",        module_signature, false); \
4009   macro(parent_offset,          k1, "parent",               classloader_signature, false)
4010 
4011 void java_lang_ClassLoader::compute_offsets() {
4012   assert(!offsets_computed, "offsets should be initialized only once");
4013   offsets_computed = true;
4014 
4015   InstanceKlass* k1 = SystemDictionary::ClassLoader_klass();
4016   CLASSLOADER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4017 
4018   CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
4019 }
4020 
4021 #if INCLUDE_CDS
4022 void java_lang_ClassLoader::serialize(SerializeClosure* f) {
4023   CLASSLOADER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4024   CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
4025 }
4026 #endif
4027 
4028 oop java_lang_ClassLoader::parent(oop loader) {
4029   assert(is_instance(loader), "loader must be oop");
4030   return loader->obj_field(parent_offset);
4031 }
4032 
4033 oop java_lang_ClassLoader::name(oop loader) {
4034   assert(is_instance(loader), "loader must be oop");
4035   return loader->obj_field(name_offset);
4036 }
4037 
4038 bool java_lang_ClassLoader::isAncestor(oop loader, oop cl) {
4039   assert(is_instance(loader), "loader must be oop");
4040   assert(cl == NULL || is_instance(cl), "cl argument must be oop");
4041   oop acl = loader;
4042   debug_only(jint loop_count = 0);
4043   // This loop taken verbatim from ClassLoader.java:
4044   do {
4045     acl = parent(acl);
4046     if (oopDesc::equals(cl, acl)) {
4047       return true;
4048     }
4049     assert(++loop_count > 0, "loop_count overflow");
4050   } while (acl != NULL);
4051   return false;
4052 }
4053 
4054 bool java_lang_ClassLoader::is_instance(oop obj) {
4055   return obj != NULL && is_subclass(obj->klass());
4056 }
4057 
4058 
4059 // For class loader classes, parallelCapable defined
4060 // based on non-null field
4061 // Written to by java.lang.ClassLoader, vm only reads this field, doesn't set it
4062 bool java_lang_ClassLoader::parallelCapable(oop class_loader) {
4063   if (parallelCapable_offset == -1) {
4064      // Default for backward compatibility is false
4065      return false;
4066   }
4067   return (class_loader->obj_field(parallelCapable_offset) != NULL);
4068 }
4069 
4070 bool java_lang_ClassLoader::is_trusted_loader(oop loader) {
4071   // Fix for 4474172; see evaluation for more details
4072   loader = non_reflection_class_loader(loader);
4073 
4074   oop cl = SystemDictionary::java_system_loader();
4075   while(cl != NULL) {
4076     if (oopDesc::equals(cl, loader)) return true;
4077     cl = parent(cl);
4078   }
4079   return false;
4080 }
4081 
4082 // Return true if this is one of the class loaders associated with
4083 // the generated bytecodes for reflection.
4084 bool java_lang_ClassLoader::is_reflection_class_loader(oop loader) {
4085   if (loader != NULL) {
4086     Klass* delegating_cl_class = SystemDictionary::reflect_DelegatingClassLoader_klass();
4087     // This might be null in non-1.4 JDKs
4088     return (delegating_cl_class != NULL && loader->is_a(delegating_cl_class));
4089   }
4090   return false;
4091 }
4092 
4093 oop java_lang_ClassLoader::non_reflection_class_loader(oop loader) {
4094   // See whether this is one of the class loaders associated with
4095   // the generated bytecodes for reflection, and if so, "magically"
4096   // delegate to its parent to prevent class loading from occurring
4097   // in places where applications using reflection didn't expect it.
4098   if (is_reflection_class_loader(loader)) {
4099     return parent(loader);
4100   }
4101   return loader;
4102 }
4103 
4104 oop java_lang_ClassLoader::unnamedModule(oop loader) {
4105   assert(is_instance(loader), "loader must be oop");
4106   return loader->obj_field(unnamedModule_offset);
4107 }
4108 
4109 // Caller needs ResourceMark.
4110 const char* java_lang_ClassLoader::describe_external(const oop loader) {
4111   if (loader == NULL) {
4112     return "<bootstrap>";
4113   }
4114 
4115   bool well_known_loader = SystemDictionary::is_system_class_loader(loader) ||
4116                            SystemDictionary::is_platform_class_loader(loader);
4117 
4118   const char* name = NULL;
4119   oop nameOop = java_lang_ClassLoader::name(loader);
4120   if (nameOop != NULL) {
4121     name = java_lang_String::as_utf8_string(nameOop);
4122   }
4123   if (name == NULL) {
4124     // Use placeholder for missing name to have fixed message format.
4125     name = "<unnamed>";
4126   }
4127 
4128   stringStream ss;
4129   ss.print("\"%s\" (instance of %s", name, loader->klass()->external_name());
4130   if (!well_known_loader) {
4131     const char* parentName = NULL;
4132     oop pl = java_lang_ClassLoader::parent(loader);
4133     if (pl != NULL) {
4134       oop parentNameOop = java_lang_ClassLoader::name(pl);
4135       if (parentNameOop != NULL) {
4136         parentName = java_lang_String::as_utf8_string(parentNameOop);
4137         if (parentName == NULL) {
4138           parentName = "<unnamed>";
4139         }
4140       }
4141       ss.print(", child of \"%s\" %s", parentName, pl->klass()->external_name());
4142     } else {
4143       ss.print(", child of <bootstrap>");
4144     }
4145   }
4146   ss.print(")");
4147 
4148   return ss.as_string();
4149 }
4150 
4151 // Support for java_lang_System
4152 //
4153 #define SYSTEM_FIELDS_DO(macro) \
4154   macro(static_in_offset,  k, "in",  input_stream_signature, true); \
4155   macro(static_out_offset, k, "out", print_stream_signature, true); \
4156   macro(static_err_offset, k, "err", print_stream_signature, true); \
4157   macro(static_security_offset, k, "security", security_manager_signature, true)
4158 
4159 void java_lang_System::compute_offsets() {
4160   InstanceKlass* k = SystemDictionary::System_klass();
4161   SYSTEM_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4162 }
4163 
4164 #if INCLUDE_CDS
4165 void java_lang_System::serialize(SerializeClosure* f) {
4166    SYSTEM_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4167 }
4168 #endif
4169 
4170 int java_lang_System::in_offset_in_bytes() { return static_in_offset; }
4171 int java_lang_System::out_offset_in_bytes() { return static_out_offset; }
4172 int java_lang_System::err_offset_in_bytes() { return static_err_offset; }
4173 
4174 
4175 bool java_lang_System::has_security_manager() {
4176   InstanceKlass* ik = SystemDictionary::System_klass();
4177   oop base = ik->static_field_base_raw();
4178   return base->obj_field(static_security_offset) != NULL;
4179 }
4180 
4181 int java_lang_Class::_klass_offset;
4182 int java_lang_Class::_array_klass_offset;
4183 int java_lang_Class::_oop_size_offset;
4184 int java_lang_Class::_static_oop_field_count_offset;
4185 int java_lang_Class::_class_loader_offset;
4186 int java_lang_Class::_module_offset;
4187 int java_lang_Class::_protection_domain_offset;
4188 int java_lang_Class::_component_mirror_offset;
4189 int java_lang_Class::_init_lock_offset;
4190 int java_lang_Class::_signers_offset;
4191 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL;
4192 GrowableArray<Klass*>* java_lang_Class::_fixup_module_field_list = NULL;
4193 int java_lang_Throwable::backtrace_offset;
4194 int java_lang_Throwable::detailMessage_offset;
4195 int java_lang_Throwable::stackTrace_offset;
4196 int java_lang_Throwable::depth_offset;
4197 int java_lang_Throwable::static_unassigned_stacktrace_offset;
4198 int java_lang_reflect_AccessibleObject::override_offset;
4199 int java_lang_reflect_Method::clazz_offset;
4200 int java_lang_reflect_Method::name_offset;
4201 int java_lang_reflect_Method::returnType_offset;
4202 int java_lang_reflect_Method::parameterTypes_offset;
4203 int java_lang_reflect_Method::exceptionTypes_offset;
4204 int java_lang_reflect_Method::slot_offset;
4205 int java_lang_reflect_Method::modifiers_offset;
4206 int java_lang_reflect_Method::signature_offset;
4207 int java_lang_reflect_Method::annotations_offset;
4208 int java_lang_reflect_Method::parameter_annotations_offset;
4209 int java_lang_reflect_Method::annotation_default_offset;
4210 int java_lang_reflect_Method::type_annotations_offset;
4211 int java_lang_reflect_Constructor::clazz_offset;
4212 int java_lang_reflect_Constructor::parameterTypes_offset;
4213 int java_lang_reflect_Constructor::exceptionTypes_offset;
4214 int java_lang_reflect_Constructor::slot_offset;
4215 int java_lang_reflect_Constructor::modifiers_offset;
4216 int java_lang_reflect_Constructor::signature_offset;
4217 int java_lang_reflect_Constructor::annotations_offset;
4218 int java_lang_reflect_Constructor::parameter_annotations_offset;
4219 int java_lang_reflect_Constructor::type_annotations_offset;
4220 int java_lang_reflect_Field::clazz_offset;
4221 int java_lang_reflect_Field::name_offset;
4222 int java_lang_reflect_Field::type_offset;
4223 int java_lang_reflect_Field::slot_offset;
4224 int java_lang_reflect_Field::modifiers_offset;
4225 int java_lang_reflect_Field::signature_offset;
4226 int java_lang_reflect_Field::annotations_offset;
4227 int java_lang_reflect_Field::type_annotations_offset;
4228 int java_lang_reflect_Parameter::name_offset;
4229 int java_lang_reflect_Parameter::modifiers_offset;
4230 int java_lang_reflect_Parameter::index_offset;
4231 int java_lang_reflect_Parameter::executable_offset;
4232 int java_lang_boxing_object::value_offset;
4233 int java_lang_boxing_object::long_value_offset;
4234 int java_lang_ref_Reference::referent_offset;
4235 int java_lang_ref_Reference::queue_offset;
4236 int java_lang_ref_Reference::next_offset;
4237 int java_lang_ref_Reference::discovered_offset;
4238 int java_lang_ref_SoftReference::timestamp_offset;
4239 int java_lang_ref_SoftReference::static_clock_offset;













4240 int java_lang_ClassLoader::parent_offset;
4241 int java_lang_System::static_in_offset;
4242 int java_lang_System::static_out_offset;
4243 int java_lang_System::static_err_offset;
4244 int java_lang_System::static_security_offset;
4245 int java_lang_StackTraceElement::methodName_offset;
4246 int java_lang_StackTraceElement::fileName_offset;
4247 int java_lang_StackTraceElement::lineNumber_offset;
4248 int java_lang_StackTraceElement::moduleName_offset;
4249 int java_lang_StackTraceElement::moduleVersion_offset;
4250 int java_lang_StackTraceElement::classLoaderName_offset;
4251 int java_lang_StackTraceElement::declaringClass_offset;
4252 int java_lang_StackTraceElement::declaringClassObject_offset;
4253 int java_lang_StackFrameInfo::_memberName_offset;
4254 int java_lang_StackFrameInfo::_bci_offset;
4255 int java_lang_StackFrameInfo::_version_offset;
4256 int java_lang_LiveStackFrameInfo::_monitors_offset;
4257 int java_lang_LiveStackFrameInfo::_locals_offset;
4258 int java_lang_LiveStackFrameInfo::_operands_offset;
4259 int java_lang_LiveStackFrameInfo::_mode_offset;
4260 int java_lang_AssertionStatusDirectives::classes_offset;
4261 int java_lang_AssertionStatusDirectives::classEnabled_offset;
4262 int java_lang_AssertionStatusDirectives::packages_offset;
4263 int java_lang_AssertionStatusDirectives::packageEnabled_offset;
4264 int java_lang_AssertionStatusDirectives::deflt_offset;
4265 int java_nio_Buffer::_limit_offset;
4266 int java_util_concurrent_locks_AbstractOwnableSynchronizer::_owner_offset = 0;
4267 int reflect_ConstantPool::_oop_offset;
4268 int reflect_UnsafeStaticFieldAccessorImpl::_base_offset;
4269 
4270 #define STACKTRACEELEMENT_FIELDS_DO(macro) \
4271   macro(declaringClassObject_offset,  k, "declaringClassObject", class_signature, false); \
4272   macro(classLoaderName_offset, k, "classLoaderName", string_signature, false); \
4273   macro(moduleName_offset,      k, "moduleName",      string_signature, false); \
4274   macro(moduleVersion_offset,   k, "moduleVersion",   string_signature, false); \
4275   macro(declaringClass_offset,  k, "declaringClass",  string_signature, false); \
4276   macro(methodName_offset,      k, "methodName",      string_signature, false); \
4277   macro(fileName_offset,        k, "fileName",        string_signature, false); \
4278   macro(lineNumber_offset,      k, "lineNumber",      int_signature,    false)
4279 
4280 // Support for java_lang_StackTraceElement
4281 void java_lang_StackTraceElement::compute_offsets() {
4282   InstanceKlass* k = SystemDictionary::StackTraceElement_klass();
4283   STACKTRACEELEMENT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4284 }
4285 
4286 #if INCLUDE_CDS
4287 void java_lang_StackTraceElement::serialize(SerializeClosure* f) {
4288   STACKTRACEELEMENT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4289 }
4290 #endif
4291 
4292 void java_lang_StackTraceElement::set_fileName(oop element, oop value) {
4293   element->obj_field_put(fileName_offset, value);
4294 }
4295 
4296 void java_lang_StackTraceElement::set_declaringClass(oop element, oop value) {
4297   element->obj_field_put(declaringClass_offset, value);
4298 }
4299 
4300 void java_lang_StackTraceElement::set_methodName(oop element, oop value) {
4301   element->obj_field_put(methodName_offset, value);
4302 }
4303 
4304 void java_lang_StackTraceElement::set_lineNumber(oop element, int value) {
4305   element->int_field_put(lineNumber_offset, value);
4306 }
4307 
4308 void java_lang_StackTraceElement::set_moduleName(oop element, oop value) {
4309   element->obj_field_put(moduleName_offset, value);
4310 }
4311 
4312 void java_lang_StackTraceElement::set_moduleVersion(oop element, oop value) {
4313   element->obj_field_put(moduleVersion_offset, value);
4314 }
4315 
4316 void java_lang_StackTraceElement::set_classLoaderName(oop element, oop value) {
4317   element->obj_field_put(classLoaderName_offset, value);
4318 }
4319 
4320 void java_lang_StackTraceElement::set_declaringClassObject(oop element, oop value) {
4321   element->obj_field_put(declaringClassObject_offset, value);
4322 }
4323 
4324 void java_lang_StackFrameInfo::set_version(oop element, short value) {
4325   element->short_field_put(_version_offset, value);
4326 }
4327 
4328 void java_lang_StackFrameInfo::set_bci(oop element, int value) {
4329   element->int_field_put(_bci_offset, value);
4330 }
4331 
4332 void java_lang_LiveStackFrameInfo::set_monitors(oop element, oop value) {
4333   element->obj_field_put(_monitors_offset, value);
4334 }
4335 
4336 void java_lang_LiveStackFrameInfo::set_locals(oop element, oop value) {
4337   element->obj_field_put(_locals_offset, value);
4338 }
4339 
4340 void java_lang_LiveStackFrameInfo::set_operands(oop element, oop value) {
4341   element->obj_field_put(_operands_offset, value);
4342 }
4343 
4344 void java_lang_LiveStackFrameInfo::set_mode(oop element, int value) {
4345   element->int_field_put(_mode_offset, value);
4346 }
4347 
4348 // Support for java Assertions - java_lang_AssertionStatusDirectives.
4349 #define ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(macro) \
4350   macro(classes_offset,        k, "classes",        string_array_signature, false); \
4351   macro(classEnabled_offset,   k, "classEnabled",   bool_array_signature, false); \
4352   macro(packages_offset,       k, "packages",       string_array_signature, false); \
4353   macro(packageEnabled_offset, k, "packageEnabled", bool_array_signature,   false); \
4354   macro(deflt_offset,          k, "deflt",          bool_signature,         false)
4355 
4356 void java_lang_AssertionStatusDirectives::compute_offsets() {
4357   InstanceKlass* k = SystemDictionary::AssertionStatusDirectives_klass();
4358   ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4359 }
4360 
4361 #if INCLUDE_CDS
4362 void java_lang_AssertionStatusDirectives::serialize(SerializeClosure* f) {
4363   ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4364 }
4365 #endif
4366 
4367 void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) {
4368   o->obj_field_put(classes_offset, val);
4369 }
4370 
4371 void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) {
4372   o->obj_field_put(classEnabled_offset, val);
4373 }
4374 
4375 void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) {
4376   o->obj_field_put(packages_offset, val);
4377 }
4378 
4379 void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) {
4380   o->obj_field_put(packageEnabled_offset, val);
4381 }
4382 
4383 void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) {
4384   o->bool_field_put(deflt_offset, val);
4385 }
4386 
























4387 
4388 // Support for intrinsification of java.nio.Buffer.checkIndex
4389 int java_nio_Buffer::limit_offset() {
4390   return _limit_offset;
4391 }
4392 
4393 #define BUFFER_FIELDS_DO(macro) \
4394   macro(_limit_offset, k, "limit", int_signature, false)
4395 
4396 void java_nio_Buffer::compute_offsets() {
4397   InstanceKlass* k = SystemDictionary::nio_Buffer_klass();
4398   assert(k != NULL, "must be loaded in 1.4+");
4399   BUFFER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4400 }
4401 
4402 #if INCLUDE_CDS
4403 void java_nio_Buffer::serialize(SerializeClosure* f) {
4404   BUFFER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4405 }
4406 #endif
4407 
4408 void java_util_concurrent_locks_AbstractOwnableSynchronizer::initialize(TRAPS) {
4409   if (_owner_offset != 0) return;
4410 
4411   SystemDictionary::load_abstract_ownable_synchronizer_klass(CHECK);
4412   InstanceKlass* k = SystemDictionary::abstract_ownable_synchronizer_klass();
4413   compute_offset(_owner_offset, k,
4414                  "exclusiveOwnerThread", vmSymbols::thread_signature());
4415 }
4416 
4417 oop java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(oop obj) {
4418   assert(_owner_offset != 0, "Must be initialized");
4419   return obj->obj_field(_owner_offset);
4420 }
4421 
4422 static int member_offset(int hardcoded_offset) {
4423   return (hardcoded_offset * heapOopSize) + instanceOopDesc::base_offset_in_bytes();
4424 }
4425 
4426 // Compute hard-coded offsets
4427 // Invoked before SystemDictionary::initialize, so pre-loaded classes
4428 // are not available to determine the offset_of_static_fields.
4429 void JavaClasses::compute_hard_coded_offsets() {
4430 
4431   // java_lang_boxing_object
4432   java_lang_boxing_object::value_offset      = member_offset(java_lang_boxing_object::hc_value_offset);
4433   java_lang_boxing_object::long_value_offset = align_up(member_offset(java_lang_boxing_object::hc_value_offset), BytesPerLong);
4434 
4435   // java_lang_ref_Reference
4436   java_lang_ref_Reference::referent_offset    = member_offset(java_lang_ref_Reference::hc_referent_offset);
4437   java_lang_ref_Reference::queue_offset       = member_offset(java_lang_ref_Reference::hc_queue_offset);
4438   java_lang_ref_Reference::next_offset        = member_offset(java_lang_ref_Reference::hc_next_offset);
4439   java_lang_ref_Reference::discovered_offset  = member_offset(java_lang_ref_Reference::hc_discovered_offset);










4440 }
4441 
4442 
4443 // Compute non-hard-coded field offsets of all the classes in this file
4444 void JavaClasses::compute_offsets() {
4445   if (UseSharedSpaces) {
4446     return; // field offsets are loaded from archive
4447   }
4448 
4449   // java_lang_Class::compute_offsets was called earlier in bootstrap
4450   java_lang_System::compute_offsets();
4451   java_lang_ClassLoader::compute_offsets();
4452   java_lang_Throwable::compute_offsets();
4453   java_lang_Thread::compute_offsets();
4454   java_lang_ThreadGroup::compute_offsets();
4455   java_lang_AssertionStatusDirectives::compute_offsets();
4456   java_lang_ref_SoftReference::compute_offsets();
4457   java_lang_invoke_MethodHandle::compute_offsets();
4458   java_lang_invoke_DirectMethodHandle::compute_offsets();
4459   java_lang_invoke_MemberName::compute_offsets();
4460   java_lang_invoke_ResolvedMethodName::compute_offsets();
4461   java_lang_invoke_LambdaForm::compute_offsets();
4462   java_lang_invoke_MethodType::compute_offsets();
4463   java_lang_invoke_CallSite::compute_offsets();
4464   java_lang_invoke_MethodHandleNatives_CallSiteContext::compute_offsets();
4465   java_security_AccessControlContext::compute_offsets();
4466   // Initialize reflection classes. The layouts of these classes
4467   // changed with the new reflection implementation in JDK 1.4, and
4468   // since the Universe doesn't know what JDK version it is until this
4469   // point we defer computation of these offsets until now.
4470   java_lang_reflect_AccessibleObject::compute_offsets();
4471   java_lang_reflect_Method::compute_offsets();
4472   java_lang_reflect_Constructor::compute_offsets();
4473   java_lang_reflect_Field::compute_offsets();
4474   java_nio_Buffer::compute_offsets();
4475   reflect_ConstantPool::compute_offsets();
4476   reflect_UnsafeStaticFieldAccessorImpl::compute_offsets();
4477   java_lang_reflect_Parameter::compute_offsets();
4478   java_lang_Module::compute_offsets();
4479   java_lang_StackTraceElement::compute_offsets();
4480   java_lang_StackFrameInfo::compute_offsets();
4481   java_lang_LiveStackFrameInfo::compute_offsets();

4482 
4483   // generated interpreter code wants to know about the offsets we just computed:
4484   AbstractAssembler::update_delayed_values();
4485 }
4486 
4487 #ifndef PRODUCT
4488 
4489 // These functions exist to assert the validity of hard-coded field offsets to guard
4490 // against changes in the class files
4491 
4492 bool JavaClasses::check_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) {
4493   EXCEPTION_MARK;
4494   fieldDescriptor fd;
4495   TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
4496   Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
4497   InstanceKlass* ik = InstanceKlass::cast(k);
4498   TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
4499   TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
4500   if (!ik->find_local_field(f_name, f_sig, &fd)) {
4501     tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
4502     return false;
4503   }
4504   if (fd.is_static()) {
4505     tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name);
4506     return false;
4507   }
4508   if (fd.offset() == hardcoded_offset ) {
4509     return true;
4510   } else {
4511     tty->print_cr("Offset of nonstatic field %s.%s is hardcoded as %d but should really be %d.",
4512                   klass_name, field_name, hardcoded_offset, fd.offset());
4513     return false;
4514   }
4515 }
4516 
4517 // Check the hard-coded field offsets of all the classes in this file
4518 
4519 void JavaClasses::check_offsets() {
4520   bool valid = true;
4521 
4522 #define CHECK_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
4523   valid &= check_offset(klass_name, cpp_klass_name :: field_name ## _offset, #field_name, field_sig)
4524 
4525 #define CHECK_LONG_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
4526   valid &= check_offset(klass_name, cpp_klass_name :: long_ ## field_name ## _offset, #field_name, field_sig)
4527 
4528   // Boxed primitive objects (java_lang_boxing_object)
4529 
4530   CHECK_OFFSET("java/lang/Boolean",   java_lang_boxing_object, value, "Z");
4531   CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C");
4532   CHECK_OFFSET("java/lang/Float",     java_lang_boxing_object, value, "F");
4533   CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D");
4534   CHECK_OFFSET("java/lang/Byte",      java_lang_boxing_object, value, "B");
4535   CHECK_OFFSET("java/lang/Short",     java_lang_boxing_object, value, "S");
4536   CHECK_OFFSET("java/lang/Integer",   java_lang_boxing_object, value, "I");
4537   CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J");
4538 
4539   // java.lang.ref.Reference
4540 
4541   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, referent, "Ljava/lang/Object;");
4542   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, queue, "Ljava/lang/ref/ReferenceQueue;");
4543   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, next, "Ljava/lang/ref/Reference;");
4544   // Fake field
4545   //CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, discovered, "Ljava/lang/ref/Reference;");
4546 











4547   if (!valid) vm_exit_during_initialization("Hard-coded field offset verification failed");
4548 }
4549 
4550 #endif // PRODUCT
4551 
4552 int InjectedField::compute_offset() {
4553   InstanceKlass* ik = InstanceKlass::cast(klass());
4554   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
4555     if (!may_be_java && !fs.access_flags().is_internal()) {
4556       // Only look at injected fields
4557       continue;
4558     }
4559     if (fs.name() == name() && fs.signature() == signature()) {
4560       return fs.offset();
4561     }
4562   }
4563   ResourceMark rm;
4564   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)" : "");
4565 #ifndef PRODUCT
4566   ik->print();
4567   tty->print_cr("all fields:");
4568   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
4569     tty->print_cr("  name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int());
4570   }
4571 #endif //PRODUCT
4572   vm_exit_during_initialization("Invalid layout of preloaded class: use -Xlog:class+load=info to see the origin of the problem class");
4573   return -1;
4574 }
4575 
4576 void javaClasses_init() {
4577   JavaClasses::compute_offsets();
4578   JavaClasses::check_offsets();
4579   FilteredFieldsMap::initialize();  // must be done after computing offsets.
4580 }
--- EOF ---