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