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