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