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