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