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

3669 int java_lang_ClassLoader::parent_offset;
3670 int java_lang_System::static_in_offset;
3671 int java_lang_System::static_out_offset;
3672 int java_lang_System::static_err_offset;
3673 int java_lang_System::static_security_offset;
3674 int java_lang_StackTraceElement::methodName_offset;
3675 int java_lang_StackTraceElement::fileName_offset;
3676 int java_lang_StackTraceElement::lineNumber_offset;
3677 int java_lang_StackTraceElement::moduleName_offset;
3678 int java_lang_StackTraceElement::moduleVersion_offset;
3679 int java_lang_StackTraceElement::classLoaderName_offset;
3680 int java_lang_StackTraceElement::declaringClass_offset;
3681 int java_lang_StackTraceElement::declaringClassObject_offset;
3682 int java_lang_StackFrameInfo::_declaringClass_offset;
3683 int java_lang_StackFrameInfo::_memberName_offset;
3684 int java_lang_StackFrameInfo::_bci_offset;
3685 int java_lang_StackFrameInfo::_version_offset;
3686 int java_lang_LiveStackFrameInfo::_monitors_offset;
3687 int java_lang_LiveStackFrameInfo::_locals_offset;
3688 int java_lang_LiveStackFrameInfo::_operands_offset;
3689 int java_lang_LiveStackFrameInfo::_mode_offset;
3690 int java_lang_AssertionStatusDirectives::classes_offset;
3691 int java_lang_AssertionStatusDirectives::classEnabled_offset;
3692 int java_lang_AssertionStatusDirectives::packages_offset;
3693 int java_lang_AssertionStatusDirectives::packageEnabled_offset;
3694 int java_lang_AssertionStatusDirectives::deflt_offset;
3695 int java_nio_Buffer::_limit_offset;
3696 int java_util_concurrent_locks_AbstractOwnableSynchronizer::_owner_offset = 0;
3697 int reflect_ConstantPool::_oop_offset;
3698 int reflect_UnsafeStaticFieldAccessorImpl::_base_offset;
3699 
3700 
3701 // Support for java_lang_StackTraceElement
3702 
3703 void java_lang_StackTraceElement::set_fileName(oop element, oop value) {
3704   element->obj_field_put(fileName_offset, value);
3705 }
3706 
3707 void java_lang_StackTraceElement::set_declaringClass(oop element, oop value) {
3708   element->obj_field_put(declaringClass_offset, value);
3709 }
3710 
3711 void java_lang_StackTraceElement::set_methodName(oop element, oop value) {
3712   element->obj_field_put(methodName_offset, value);
3713 }
3714 
3715 void java_lang_StackTraceElement::set_lineNumber(oop element, int value) {
3716   element->int_field_put(lineNumber_offset, value);
3717 }
3718 
3719 void java_lang_StackTraceElement::set_moduleName(oop element, oop value) {
3720   element->obj_field_put(moduleName_offset, value);
3721 }
3722 
3723 void java_lang_StackTraceElement::set_moduleVersion(oop element, oop value) {
3724   element->obj_field_put(moduleVersion_offset, value);
3725 }
3726 
3727 void java_lang_StackTraceElement::set_classLoaderName(oop element, oop value) {
3728   element->obj_field_put(classLoaderName_offset, value);
3729 }
3730 
3731 void java_lang_StackTraceElement::set_declaringClassObject(oop element, oop value) {
3732   element->obj_field_put(declaringClassObject_offset, value);
3733 }
3734 
3735 // Support for java_lang_StackFrameInfo
3736 void java_lang_StackFrameInfo::set_declaringClass(oop element, oop value) {
3737   element->obj_field_put(_declaringClass_offset, value);
3738 }
3739 
3740 void java_lang_StackFrameInfo::set_version(oop element, short value) {
3741   element->short_field_put(_version_offset, value);
3742 }
3743 
3744 void java_lang_StackFrameInfo::set_bci(oop element, int value) {
3745   element->int_field_put(_bci_offset, value);
3746 }
3747 
3748 void java_lang_LiveStackFrameInfo::set_monitors(oop element, oop value) {
3749   element->obj_field_put(_monitors_offset, value);
3750 }
3751 
3752 void java_lang_LiveStackFrameInfo::set_locals(oop element, oop value) {
3753   element->obj_field_put(_locals_offset, value);
3754 }
3755 
3756 void java_lang_LiveStackFrameInfo::set_operands(oop element, oop value) {
3757   element->obj_field_put(_operands_offset, value);
3758 }
3759 
3760 void java_lang_LiveStackFrameInfo::set_mode(oop element, int value) {
3761   element->int_field_put(_mode_offset, value);
3762 }
3763 
3764 // Support for java Assertions - java_lang_AssertionStatusDirectives.
3765 
3766 void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) {
3767   o->obj_field_put(classes_offset, val);
3768 }
3769 
3770 void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) {
3771   o->obj_field_put(classEnabled_offset, val);
3772 }
3773 
3774 void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) {
3775   o->obj_field_put(packages_offset, val);
3776 }
3777 
3778 void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) {
3779   o->obj_field_put(packageEnabled_offset, val);
3780 }
3781 
3782 void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) {
3783   o->bool_field_put(deflt_offset, val);
3784 }
3785 
3786 
3787 // Support for intrinsification of java.nio.Buffer.checkIndex
3788 int java_nio_Buffer::limit_offset() {
3789   return _limit_offset;
3790 }
3791 
3792 
3793 void java_nio_Buffer::compute_offsets() {
3794   Klass* k = SystemDictionary::nio_Buffer_klass();
3795   assert(k != NULL, "must be loaded in 1.4+");
3796   compute_offset(_limit_offset, k, vmSymbols::limit_name(), vmSymbols::int_signature());
3797 }
3798 
3799 void java_util_concurrent_locks_AbstractOwnableSynchronizer::initialize(TRAPS) {
3800   if (_owner_offset != 0) return;
3801 
3802   SystemDictionary::load_abstract_ownable_synchronizer_klass(CHECK);
3803   Klass* k = SystemDictionary::abstract_ownable_synchronizer_klass();
3804   compute_offset(_owner_offset, k,
3805                  vmSymbols::exclusive_owner_thread_name(), vmSymbols::thread_signature());
3806 }
3807 
3808 oop java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(oop obj) {
3809   assert(_owner_offset != 0, "Must be initialized");
3810   return obj->obj_field(_owner_offset);
3811 }
3812 
3813 // Compute hard-coded offsets
3814 // Invoked before SystemDictionary::initialize, so pre-loaded classes
3815 // are not available to determine the offset_of_static_fields.
3816 void JavaClasses::compute_hard_coded_offsets() {
3817   const int x = heapOopSize;
3818   const int header = instanceOopDesc::base_offset_in_bytes();
3819 
3820   // Throwable Class
3821   java_lang_Throwable::backtrace_offset  = java_lang_Throwable::hc_backtrace_offset  * x + header;
3822   java_lang_Throwable::detailMessage_offset = java_lang_Throwable::hc_detailMessage_offset * x + header;
3823   java_lang_Throwable::stackTrace_offset = java_lang_Throwable::hc_stackTrace_offset * x + header;
3824   java_lang_Throwable::static_unassigned_stacktrace_offset = java_lang_Throwable::hc_static_unassigned_stacktrace_offset *  x;
3825 
3826   // java_lang_boxing_object
3827   java_lang_boxing_object::value_offset = java_lang_boxing_object::hc_value_offset + header;
3828   java_lang_boxing_object::long_value_offset = align_up((java_lang_boxing_object::hc_value_offset + header), BytesPerLong);
3829 
3830   // java_lang_ref_Reference:
3831   java_lang_ref_Reference::referent_offset = java_lang_ref_Reference::hc_referent_offset * x + header;
3832   java_lang_ref_Reference::queue_offset = java_lang_ref_Reference::hc_queue_offset * x + header;
3833   java_lang_ref_Reference::next_offset  = java_lang_ref_Reference::hc_next_offset * x + header;
3834   java_lang_ref_Reference::discovered_offset  = java_lang_ref_Reference::hc_discovered_offset * x + header;
3835   // Artificial fields for java_lang_ref_Reference
3836   // The first field is for the discovered field added in 1.4
3837   java_lang_ref_Reference::number_of_fake_oop_fields = 1;
3838 
3839   // java_lang_ref_SoftReference Class
3840   java_lang_ref_SoftReference::timestamp_offset = align_up((java_lang_ref_SoftReference::hc_timestamp_offset * x + header), BytesPerLong);
3841   // Don't multiply static fields because they are always in wordSize units
3842   java_lang_ref_SoftReference::static_clock_offset = java_lang_ref_SoftReference::hc_static_clock_offset * x;
3843 



3844   // java_lang_ClassLoader
3845   java_lang_ClassLoader::parent_offset = java_lang_ClassLoader::hc_parent_offset * x + header;
3846 
3847   // java_lang_System
3848   java_lang_System::static_in_offset  = java_lang_System::hc_static_in_offset  * x;
3849   java_lang_System::static_out_offset = java_lang_System::hc_static_out_offset * x;
3850   java_lang_System::static_err_offset = java_lang_System::hc_static_err_offset * x;
3851   java_lang_System::static_security_offset = java_lang_System::hc_static_security_offset * x;
3852 
3853   // java_lang_StackTraceElement
3854   java_lang_StackTraceElement::declaringClassObject_offset = java_lang_StackTraceElement::hc_declaringClassObject_offset * x + header;
3855   java_lang_StackTraceElement::classLoaderName_offset = java_lang_StackTraceElement::hc_classLoaderName_offset * x + header;
3856   java_lang_StackTraceElement::moduleName_offset = java_lang_StackTraceElement::hc_moduleName_offset * x + header;
3857   java_lang_StackTraceElement::moduleVersion_offset = java_lang_StackTraceElement::hc_moduleVersion_offset * x + header;
3858   java_lang_StackTraceElement::declaringClass_offset = java_lang_StackTraceElement::hc_declaringClass_offset  * x + header;
3859   java_lang_StackTraceElement::methodName_offset = java_lang_StackTraceElement::hc_methodName_offset * x + header;
3860   java_lang_StackTraceElement::fileName_offset   = java_lang_StackTraceElement::hc_fileName_offset   * x + header;
3861   java_lang_StackTraceElement::lineNumber_offset = java_lang_StackTraceElement::hc_lineNumber_offset * x + header;
3862   java_lang_AssertionStatusDirectives::classes_offset = java_lang_AssertionStatusDirectives::hc_classes_offset * x + header;
3863   java_lang_AssertionStatusDirectives::classEnabled_offset = java_lang_AssertionStatusDirectives::hc_classEnabled_offset * x + header;
3864   java_lang_AssertionStatusDirectives::packages_offset = java_lang_AssertionStatusDirectives::hc_packages_offset * x + header;
3865   java_lang_AssertionStatusDirectives::packageEnabled_offset = java_lang_AssertionStatusDirectives::hc_packageEnabled_offset * x + header;
3866   java_lang_AssertionStatusDirectives::deflt_offset = java_lang_AssertionStatusDirectives::hc_deflt_offset * x + header;
3867 
3868 }
3869 
3870 
3871 // Compute non-hard-coded field offsets of all the classes in this file
3872 void JavaClasses::compute_offsets() {
3873   // java_lang_Class::compute_offsets was called earlier in bootstrap
3874   java_lang_ClassLoader::compute_offsets();
3875   java_lang_Throwable::compute_offsets();
3876   java_lang_Thread::compute_offsets();
3877   java_lang_ThreadGroup::compute_offsets();
3878   java_lang_invoke_MethodHandle::compute_offsets();
3879   java_lang_invoke_DirectMethodHandle::compute_offsets();
3880   java_lang_invoke_MemberName::compute_offsets();
3881   java_lang_invoke_ResolvedMethodName::compute_offsets();
3882   java_lang_invoke_LambdaForm::compute_offsets();
3883   java_lang_invoke_MethodType::compute_offsets();
3884   java_lang_invoke_CallSite::compute_offsets();
3885   java_lang_invoke_MethodHandleNatives_CallSiteContext::compute_offsets();
3886   java_security_AccessControlContext::compute_offsets();
3887   // Initialize reflection classes. The layouts of these classes
3888   // changed with the new reflection implementation in JDK 1.4, and
3889   // since the Universe doesn't know what JDK version it is until this
3890   // point we defer computation of these offsets until now.
3891   java_lang_reflect_AccessibleObject::compute_offsets();
3892   java_lang_reflect_Method::compute_offsets();
3893   java_lang_reflect_Constructor::compute_offsets();
3894   java_lang_reflect_Field::compute_offsets();
3895   java_nio_Buffer::compute_offsets();
3896   reflect_ConstantPool::compute_offsets();
3897   reflect_UnsafeStaticFieldAccessorImpl::compute_offsets();
3898   java_lang_reflect_Parameter::compute_offsets();
3899   java_lang_Module::compute_offsets();
3900   java_lang_StackFrameInfo::compute_offsets();
3901   java_lang_LiveStackFrameInfo::compute_offsets();
3902 
3903   // generated interpreter code wants to know about the offsets we just computed:
3904   AbstractAssembler::update_delayed_values();
3905 }
3906 
3907 #ifndef PRODUCT
3908 
3909 // These functions exist to assert the validity of hard-coded field offsets to guard
3910 // against changes in the class files
3911 
3912 bool JavaClasses::check_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) {
3913   EXCEPTION_MARK;
3914   fieldDescriptor fd;
3915   TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
3916   Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
3917   InstanceKlass* ik = InstanceKlass::cast(k);
3918   TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
3919   TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
3920   if (!ik->find_local_field(f_name, f_sig, &fd)) {
3921     tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
3922     return false;
3923   }
3924   if (fd.is_static()) {
3925     tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name);
3926     return false;
3927   }
3928   if (fd.offset() == hardcoded_offset ) {
3929     return true;
3930   } else {
3931     tty->print_cr("Offset of nonstatic field %s.%s is hardcoded as %d but should really be %d.",
3932                   klass_name, field_name, hardcoded_offset, fd.offset());
3933     return false;
3934   }
3935 }
3936 
3937 
3938 bool JavaClasses::check_static_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) {
3939   EXCEPTION_MARK;
3940   fieldDescriptor fd;
3941   TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
3942   Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
3943   InstanceKlass* ik = InstanceKlass::cast(k);
3944   TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
3945   TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
3946   if (!ik->find_local_field(f_name, f_sig, &fd)) {
3947     tty->print_cr("Static field %s.%s not found", klass_name, field_name);
3948     return false;
3949   }
3950   if (!fd.is_static()) {
3951     tty->print_cr("Static field %s.%s appears to be nonstatic", klass_name, field_name);
3952     return false;
3953   }
3954   if (fd.offset() == hardcoded_offset + InstanceMirrorKlass::offset_of_static_fields()) {
3955     return true;
3956   } else {
3957     tty->print_cr("Offset of static field %s.%s is hardcoded as %d but should really be %d.", klass_name, field_name, hardcoded_offset, fd.offset() - InstanceMirrorKlass::offset_of_static_fields());
3958     return false;
3959   }
3960 }
3961 
3962 
3963 bool JavaClasses::check_constant(const char *klass_name, int hardcoded_constant, const char *field_name, const char* field_sig) {
3964   EXCEPTION_MARK;
3965   fieldDescriptor fd;
3966   TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
3967   Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
3968   InstanceKlass* ik = InstanceKlass::cast(k);
3969   TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
3970   TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
3971   if (!ik->find_local_field(f_name, f_sig, &fd)) {
3972     tty->print_cr("Static field %s.%s not found", klass_name, field_name);
3973     return false;
3974   }
3975   if (!fd.is_static() || !fd.has_initial_value()) {
3976     tty->print_cr("Static field %s.%s appears to be non-constant", klass_name, field_name);
3977     return false;
3978   }
3979   if (!fd.initial_value_tag().is_int()) {
3980     tty->print_cr("Static field %s.%s is not an int", klass_name, field_name);
3981     return false;
3982   }
3983   jint field_value = fd.int_initial_value();
3984   if (field_value == hardcoded_constant) {
3985     return true;
3986   } else {
3987     tty->print_cr("Constant value of static field %s.%s is hardcoded as %d but should really be %d.", klass_name, field_name, hardcoded_constant, field_value);
3988     return false;
3989   }
3990 }
3991 
3992 
3993 // Check the hard-coded field offsets of all the classes in this file
3994 
3995 void JavaClasses::check_offsets() {
3996   bool valid = true;
3997 
3998 #define CHECK_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
3999   valid &= check_offset(klass_name, cpp_klass_name :: field_name ## _offset, #field_name, field_sig)
4000 
4001 #define CHECK_LONG_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
4002   valid &= check_offset(klass_name, cpp_klass_name :: long_ ## field_name ## _offset, #field_name, field_sig)
4003 
4004 #define CHECK_STATIC_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
4005   valid &= check_static_offset(klass_name, cpp_klass_name :: static_ ## field_name ## _offset, #field_name, field_sig)
4006 
4007 #define CHECK_CONSTANT(klass_name, cpp_klass_name, field_name, field_sig) \
4008   valid &= check_constant(klass_name, cpp_klass_name :: field_name, #field_name, field_sig)
4009 
4010   // java.lang.String
4011 
4012   CHECK_OFFSET("java/lang/String", java_lang_String, value, "[B");
4013   CHECK_OFFSET("java/lang/String", java_lang_String, hash, "I");
4014   CHECK_OFFSET("java/lang/String", java_lang_String, coder, "B");
4015 
4016   // java.lang.Class
4017 
4018   // Fake fields
4019   // CHECK_OFFSET("java/lang/Class", java_lang_Class, klass); // %%% this needs to be checked
4020   // CHECK_OFFSET("java/lang/Class", java_lang_Class, array_klass); // %%% this needs to be checked
4021 
4022   // java.lang.Throwable
4023 
4024   CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, backtrace, "Ljava/lang/Object;");
4025   CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, detailMessage, "Ljava/lang/String;");
4026   CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, stackTrace, "[Ljava/lang/StackTraceElement;");
4027   CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, depth, "I");
4028 
4029   // Boxed primitive objects (java_lang_boxing_object)
4030 
4031   CHECK_OFFSET("java/lang/Boolean",   java_lang_boxing_object, value, "Z");
4032   CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C");
4033   CHECK_OFFSET("java/lang/Float",     java_lang_boxing_object, value, "F");
4034   CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D");
4035   CHECK_OFFSET("java/lang/Byte",      java_lang_boxing_object, value, "B");
4036   CHECK_OFFSET("java/lang/Short",     java_lang_boxing_object, value, "S");
4037   CHECK_OFFSET("java/lang/Integer",   java_lang_boxing_object, value, "I");
4038   CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J");
4039 
4040   // java.lang.ClassLoader
4041 
4042   CHECK_OFFSET("java/lang/ClassLoader", java_lang_ClassLoader, parent,        "Ljava/lang/ClassLoader;");
4043 
4044   // java.lang.System
4045 
4046   CHECK_STATIC_OFFSET("java/lang/System", java_lang_System,  in, "Ljava/io/InputStream;");
4047   CHECK_STATIC_OFFSET("java/lang/System", java_lang_System, out, "Ljava/io/PrintStream;");
4048   CHECK_STATIC_OFFSET("java/lang/System", java_lang_System, err, "Ljava/io/PrintStream;");
4049   CHECK_STATIC_OFFSET("java/lang/System", java_lang_System, security, "Ljava/lang/SecurityManager;");
4050 
4051   // java.lang.StackTraceElement
4052 
4053   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, declaringClassObject, "Ljava/lang/Class;");
4054   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, classLoaderName, "Ljava/lang/String;");
4055   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, moduleName,      "Ljava/lang/String;");
4056   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, moduleVersion,   "Ljava/lang/String;");
4057   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, declaringClass,  "Ljava/lang/String;");
4058   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, methodName,      "Ljava/lang/String;");
4059   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, fileName,        "Ljava/lang/String;");
4060   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, lineNumber,      "I");
4061 
4062   // java.lang.ref.Reference
4063 
4064   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, referent, "Ljava/lang/Object;");
4065   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, queue, "Ljava/lang/ref/ReferenceQueue;");
4066   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, next, "Ljava/lang/ref/Reference;");
4067   // Fake field
4068   //CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, discovered, "Ljava/lang/ref/Reference;");
4069 




4070   // java.lang.ref.SoftReference
4071 
4072   CHECK_OFFSET("java/lang/ref/SoftReference", java_lang_ref_SoftReference, timestamp, "J");
4073   CHECK_STATIC_OFFSET("java/lang/ref/SoftReference", java_lang_ref_SoftReference, clock, "J");
4074 
4075   // java.lang.AssertionStatusDirectives
4076   //
4077   // The CheckAssertionStatusDirectives boolean can be removed from here and
4078   // globals.hpp after the AssertionStatusDirectives class has been integrated
4079   // into merlin "for some time."  Without it, the vm will fail with early
4080   // merlin builds.
4081 
4082   if (CheckAssertionStatusDirectives) {
4083     const char* nm = "java/lang/AssertionStatusDirectives";
4084     const char* sig = "[Ljava/lang/String;";
4085     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, classes, sig);
4086     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, classEnabled, "[Z");
4087     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, packages, sig);
4088     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, packageEnabled, "[Z");
4089     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, deflt, "Z");
4090   }
4091 
4092   if (!valid) vm_exit_during_initialization("Hard-coded field offset verification failed");
4093 }
4094 
4095 #endif // PRODUCT
4096 
4097 int InjectedField::compute_offset() {
4098   InstanceKlass* ik = InstanceKlass::cast(klass());
4099   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
4100     if (!may_be_java && !fs.access_flags().is_internal()) {
4101       // Only look at injected fields
4102       continue;
4103     }
4104     if (fs.name() == name() && fs.signature() == signature()) {
4105       return fs.offset();
4106     }
4107   }
4108   ResourceMark rm;
4109   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)" : "");
4110 #ifndef PRODUCT
4111   ik->print();
4112   tty->print_cr("all fields:");
4113   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
4114     tty->print_cr("  name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int());
4115   }
4116 #endif //PRODUCT
4117   vm_exit_during_initialization("Invalid layout of preloaded class: use -Xlog:class+load=info to see the origin of the problem class");
4118   return -1;
4119 }
4120 
4121 void javaClasses_init() {
4122   JavaClasses::compute_offsets();
4123   JavaClasses::check_offsets();
4124   FilteredFieldsMap::initialize();  // must be done after computing offsets.
4125 }
--- EOF ---