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