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