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  * Print the throwable message and its stack trace plus all causes by walking the
1725  * cause chain.  The output looks the same as of Throwable.printStackTrace().
1726  */
1727 void java_lang_Throwable::print_stack_trace(Handle throwable, outputStream* st) {
1728   // First, print the message.
1729   print(throwable, st);
1730   st->cr();
1731 
1732   // Now print the stack trace.
1733   Thread* THREAD = Thread::current();
1734   while (throwable.not_null()) {
1735     objArrayHandle result (THREAD, objArrayOop(backtrace(throwable())));
1736     if (result.is_null()) {
1737       st->print_raw_cr(no_stack_trace_message());
1738       return;
1739     }
1740 
1741     while (result.not_null()) {
1742       // Get method id, bci, version and mirror from chunk
1743       typeArrayHandle methods (THREAD, BacktraceBuilder::get_methods(result));
1744       typeArrayHandle bcis (THREAD, BacktraceBuilder::get_bcis(result));
1745       objArrayHandle mirrors (THREAD, BacktraceBuilder::get_mirrors(result));
1746       typeArrayHandle cprefs (THREAD, BacktraceBuilder::get_cprefs(result));
1747 
1748       int length = methods()->length();
1749       for (int index = 0; index < length; index++) {
1750         Handle mirror(THREAD, mirrors->obj_at(index));
1751         // NULL mirror means end of stack trace
1752         if (mirror.is_null()) goto handle_cause;
1753         int method = methods->short_at(index);
1754         int version = Backtrace::version_at(bcis->int_at(index));
1755         int bci = Backtrace::bci_at(bcis->int_at(index));
1756         int cpref = cprefs->short_at(index);
1757         print_stack_element(st, mirror, method, version, bci, cpref);
1758       }
1759       result = objArrayHandle(THREAD, objArrayOop(result->obj_at(trace_next_offset)));
1760     }
1761   handle_cause:
1762     {
1763       EXCEPTION_MARK;
1764       JavaValue cause(T_OBJECT);
1765       JavaCalls::call_virtual(&cause,
1766                               throwable,
1767                               KlassHandle(THREAD, throwable->klass()),
1768                               vmSymbols::getCause_name(),
1769                               vmSymbols::void_throwable_signature(),
1770                               THREAD);
1771       // Ignore any exceptions. we are in the middle of exception handling. Same as classic VM.
1772       if (HAS_PENDING_EXCEPTION) {
1773         CLEAR_PENDING_EXCEPTION;
1774         throwable = Handle();
1775       } else {
1776         throwable = Handle(THREAD, (oop) cause.get_jobject());
1777         if (throwable.not_null()) {
1778           st->print("Caused by: ");
1779           print(throwable, st);
1780           st->cr();
1781         }
1782       }
1783     }
1784   }
1785 }
1786 
1787 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method, TRAPS) {
1788   if (!StackTraceInThrowable) return;
1789   ResourceMark rm(THREAD);
1790 
1791   // Start out by clearing the backtrace for this object, in case the VM
1792   // runs out of memory while allocating the stack trace
1793   set_backtrace(throwable(), NULL);
1794   // Clear lazily constructed Java level stacktrace if refilling occurs
1795   // This is unnecessary in 1.7+ but harmless
1796   clear_stacktrace(throwable());
1797 
1798   int max_depth = MaxJavaStackTraceDepth;
1799   JavaThread* thread = (JavaThread*)THREAD;
1800   BacktraceBuilder bt(CHECK);
1801 
1802   // If there is no Java frame just return the method that was being called
1803   // with bci 0
1804   if (!thread->has_last_Java_frame()) {
1805     if (max_depth >= 1 && method() != NULL) {
1806       bt.push(method(), 0, CHECK);
1807       set_backtrace(throwable(), bt.backtrace());
1808     }
1809     return;
1810   }
1811 
1812   // Instead of using vframe directly, this version of fill_in_stack_trace
1813   // basically handles everything by hand. This significantly improved the
1814   // speed of this method call up to 28.5% on Solaris sparc. 27.1% on Windows.
1815   // See bug 6333838 for  more details.
1816   // The "ASSERT" here is to verify this method generates the exactly same stack
1817   // trace as utilizing vframe.
1818 #ifdef ASSERT
1819   vframeStream st(thread);
1820   methodHandle st_method(THREAD, st.method());
1821 #endif
1822   int total_count = 0;
1823   RegisterMap map(thread, false);
1824   int decode_offset = 0;
1825   nmethod* nm = NULL;
1826   bool skip_fillInStackTrace_check = false;
1827   bool skip_throwableInit_check = false;
1828   bool skip_hidden = !ShowHiddenFrames;
1829 
1830   for (frame fr = thread->last_frame(); max_depth != total_count;) {
1831     Method* method = NULL;
1832     int bci = 0;
1833 
1834     // Compiled java method case.
1835     if (decode_offset != 0) {
1836       DebugInfoReadStream stream(nm, decode_offset);
1837       decode_offset = stream.read_int();
1838       method = (Method*)nm->metadata_at(stream.read_int());
1839       bci = stream.read_bci();
1840     } else {
1841       if (fr.is_first_frame()) break;
1842       address pc = fr.pc();
1843       if (fr.is_interpreted_frame()) {
1844         address bcp = fr.interpreter_frame_bcp();
1845         method = fr.interpreter_frame_method();
1846         bci =  method->bci_from(bcp);
1847         fr = fr.sender(&map);
1848       } else {
1849         CodeBlob* cb = fr.cb();
1850         // HMMM QQQ might be nice to have frame return nm as NULL if cb is non-NULL
1851         // but non nmethod
1852         fr = fr.sender(&map);
1853         if (cb == NULL || !cb->is_nmethod()) {
1854           continue;
1855         }
1856         nm = (nmethod*)cb;
1857         if (nm->method()->is_native()) {
1858           method = nm->method();
1859           bci = 0;
1860         } else {
1861           PcDesc* pd = nm->pc_desc_at(pc);
1862           decode_offset = pd->scope_decode_offset();
1863           // if decode_offset is not equal to 0, it will execute the
1864           // "compiled java method case" at the beginning of the loop.
1865           continue;
1866         }
1867       }
1868     }
1869 #ifdef ASSERT
1870     assert(st_method() == method && st.bci() == bci,
1871            "Wrong stack trace");
1872     st.next();
1873     // vframeStream::method isn't GC-safe so store off a copy
1874     // of the Method* in case we GC.
1875     if (!st.at_end()) {
1876       st_method = st.method();
1877     }
1878 #endif
1879 
1880     // the format of the stacktrace will be:
1881     // - 1 or more fillInStackTrace frames for the exception class (skipped)
1882     // - 0 or more <init> methods for the exception class (skipped)
1883     // - rest of the stack
1884 
1885     if (!skip_fillInStackTrace_check) {
1886       if (method->name() == vmSymbols::fillInStackTrace_name() &&
1887           throwable->is_a(method->method_holder())) {
1888         continue;
1889       }
1890       else {
1891         skip_fillInStackTrace_check = true; // gone past them all
1892       }
1893     }
1894     if (!skip_throwableInit_check) {
1895       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
1896 
1897       // skip <init> methods of the exception class and superclasses
1898       // This is simlar to classic VM.
1899       if (method->name() == vmSymbols::object_initializer_name() &&
1900           throwable->is_a(method->method_holder())) {
1901         continue;
1902       } else {
1903         // there are none or we've seen them all - either way stop checking
1904         skip_throwableInit_check = true;
1905       }
1906     }
1907     if (method->is_hidden()) {
1908       if (skip_hidden)  continue;
1909     }
1910     bt.push(method, bci, CHECK);
1911     total_count++;
1912   }
1913 
1914   // Put completed stack trace into throwable object
1915   set_backtrace(throwable(), bt.backtrace());
1916 }
1917 
1918 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method) {
1919   // No-op if stack trace is disabled
1920   if (!StackTraceInThrowable) {
1921     return;
1922   }
1923 
1924   // Disable stack traces for some preallocated out of memory errors
1925   if (!Universe::should_fill_in_stack_trace(throwable)) {
1926     return;
1927   }
1928 
1929   PRESERVE_EXCEPTION_MARK;
1930 
1931   JavaThread* thread = JavaThread::active();
1932   fill_in_stack_trace(throwable, method, thread);
1933   // ignore exceptions thrown during stack trace filling
1934   CLEAR_PENDING_EXCEPTION;
1935 }
1936 
1937 void java_lang_Throwable::allocate_backtrace(Handle throwable, TRAPS) {
1938   // Allocate stack trace - backtrace is created but not filled in
1939 
1940   // No-op if stack trace is disabled
1941   if (!StackTraceInThrowable) return;
1942   BacktraceBuilder bt(CHECK);   // creates a backtrace
1943   set_backtrace(throwable(), bt.backtrace());
1944 }
1945 
1946 
1947 void java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(Handle throwable) {
1948   // Fill in stack trace into preallocated backtrace (no GC)
1949 
1950   // No-op if stack trace is disabled
1951   if (!StackTraceInThrowable) return;
1952 
1953   assert(throwable->is_a(SystemDictionary::Throwable_klass()), "sanity check");
1954 
1955   JavaThread* THREAD = JavaThread::current();
1956 
1957   objArrayHandle backtrace (THREAD, (objArrayOop)java_lang_Throwable::backtrace(throwable()));
1958   assert(backtrace.not_null(), "backtrace should have been preallocated");
1959 
1960   ResourceMark rm(THREAD);
1961   vframeStream st(THREAD);
1962 
1963   BacktraceBuilder bt(backtrace);
1964 
1965   // Unlike fill_in_stack_trace we do not skip fillInStackTrace or throwable init
1966   // methods as preallocated errors aren't created by "java" code.
1967 
1968   // fill in as much stack trace as possible
1969   typeArrayOop methods = BacktraceBuilder::get_methods(backtrace);
1970   int max_chunks = MIN2(methods->length(), (int)MaxJavaStackTraceDepth);
1971   int chunk_count = 0;
1972 
1973   for (;!st.at_end(); st.next()) {
1974     bt.push(st.method(), st.bci(), CHECK);
1975     chunk_count++;
1976 
1977     // Bail-out for deep stacks
1978     if (chunk_count >= max_chunks) break;
1979   }
1980 
1981   // We support the Throwable immutability protocol defined for Java 7.
1982   java_lang_Throwable::set_stacktrace(throwable(), java_lang_Throwable::unassigned_stacktrace());
1983   assert(java_lang_Throwable::unassigned_stacktrace() != NULL, "not initialized");
1984 }
1985 
1986 
1987 int java_lang_Throwable::get_stack_trace_depth(oop throwable, TRAPS) {
1988   if (throwable == NULL) {
1989     THROW_0(vmSymbols::java_lang_NullPointerException());
1990   }
1991   objArrayOop chunk = objArrayOop(backtrace(throwable));
1992   int depth = 0;
1993   if (chunk != NULL) {
1994     // Iterate over chunks and count full ones
1995     while (true) {
1996       objArrayOop next = objArrayOop(chunk->obj_at(trace_next_offset));
1997       if (next == NULL) break;
1998       depth += trace_chunk_size;
1999       chunk = next;
2000     }
2001     assert(chunk != NULL && chunk->obj_at(trace_next_offset) == NULL, "sanity check");
2002     // Count element in remaining partial chunk.  NULL value for mirror
2003     // marks the end of the stack trace elements that are saved.
2004     objArrayOop mirrors = BacktraceBuilder::get_mirrors(chunk);
2005     assert(mirrors != NULL, "sanity check");
2006     for (int i = 0; i < mirrors->length(); i++) {
2007       if (mirrors->obj_at(i) == NULL) break;
2008       depth++;
2009     }
2010   }
2011   return depth;
2012 }
2013 
2014 
2015 oop java_lang_Throwable::get_stack_trace_element(oop throwable, int index, TRAPS) {
2016   if (throwable == NULL) {
2017     THROW_0(vmSymbols::java_lang_NullPointerException());
2018   }
2019   if (index < 0) {
2020     THROW_(vmSymbols::java_lang_IndexOutOfBoundsException(), NULL);
2021   }
2022   // Compute how many chunks to skip and index into actual chunk
2023   objArrayOop chunk = objArrayOop(backtrace(throwable));
2024   int skip_chunks = index / trace_chunk_size;
2025   int chunk_index = index % trace_chunk_size;
2026   while (chunk != NULL && skip_chunks > 0) {
2027     chunk = objArrayOop(chunk->obj_at(trace_next_offset));
2028         skip_chunks--;
2029   }
2030   if (chunk == NULL) {
2031     THROW_(vmSymbols::java_lang_IndexOutOfBoundsException(), NULL);
2032   }
2033   // Get method id, bci, version, mirror and cpref from chunk
2034   typeArrayOop methods = BacktraceBuilder::get_methods(chunk);
2035   typeArrayOop bcis = BacktraceBuilder::get_bcis(chunk);
2036   objArrayOop mirrors = BacktraceBuilder::get_mirrors(chunk);
2037   typeArrayOop cprefs = BacktraceBuilder::get_cprefs(chunk);
2038 
2039   assert(methods != NULL && bcis != NULL && mirrors != NULL, "sanity check");
2040 
2041   int method = methods->short_at(chunk_index);
2042   int version = Backtrace::version_at(bcis->int_at(chunk_index));
2043   int bci = Backtrace::bci_at(bcis->int_at(chunk_index));
2044   int cpref = cprefs->short_at(chunk_index);
2045   Handle mirror(THREAD, mirrors->obj_at(chunk_index));
2046 
2047   // Chunk can be partial full
2048   if (mirror.is_null()) {
2049     THROW_(vmSymbols::java_lang_IndexOutOfBoundsException(), NULL);
2050   }
2051   oop element = java_lang_StackTraceElement::create(mirror, method, version, bci, cpref, CHECK_0);
2052   return element;
2053 }
2054 
2055 oop java_lang_StackTraceElement::create(Handle mirror, int method_id,
2056                                         int version, int bci, int cpref, TRAPS) {
2057   // Allocate java.lang.StackTraceElement instance
2058   Klass* k = SystemDictionary::StackTraceElement_klass();
2059   assert(k != NULL, "must be loaded in 1.4+");
2060   instanceKlassHandle ik (THREAD, k);
2061   if (ik->should_be_initialized()) {
2062     ik->initialize(CHECK_0);
2063   }
2064 
2065   Handle element = ik->allocate_instance_handle(CHECK_0);
2066 
2067   // Fill in class name
2068   ResourceMark rm(THREAD);
2069   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror()));
2070   const char* str = holder->external_name();
2071   oop classname = StringTable::intern((char*) str, CHECK_0);
2072   java_lang_StackTraceElement::set_declaringClass(element(), classname);
2073 
2074   Method* method = holder->method_with_orig_idnum(method_id, version);
2075 
2076   // The method can be NULL if the requested class version is gone
2077   Symbol* sym = (method != NULL) ? method->name() : holder->constants()->symbol_at(cpref);
2078 
2079   // Fill in method name
2080   oop methodname = StringTable::intern(sym, CHECK_0);
2081   java_lang_StackTraceElement::set_methodName(element(), methodname);
2082 
2083   if (!version_matches(method, version)) {
2084     // The method was redefined, accurate line number information isn't available
2085     java_lang_StackTraceElement::set_fileName(element(), NULL);
2086     java_lang_StackTraceElement::set_lineNumber(element(), -1);
2087   } else {
2088     // Fill in source file name and line number.
2089     Symbol* source = Backtrace::get_source_file_name(holder, version);
2090     if (ShowHiddenFrames && source == NULL)
2091       source = vmSymbols::unknown_class_name();
2092     oop filename = StringTable::intern(source, CHECK_0);
2093     java_lang_StackTraceElement::set_fileName(element(), filename);
2094 
2095     int line_number = Backtrace::get_line_number(method, bci);
2096     java_lang_StackTraceElement::set_lineNumber(element(), line_number);
2097   }
2098   return element();
2099 }
2100 
2101 oop java_lang_StackTraceElement::create(const methodHandle& method, int bci, TRAPS) {
2102   Handle mirror (THREAD, method->method_holder()->java_mirror());
2103   int method_id = method->orig_method_idnum();
2104   int cpref = method->name_index();
2105   return create(mirror, method_id, method->constants()->version(), bci, cpref, THREAD);
2106 }
2107 
2108 Method* java_lang_StackFrameInfo::get_method(Handle stackFrame, InstanceKlass* holder, TRAPS) {
2109   if (MemberNameInStackFrame) {
2110     Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
2111     Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mname());
2112     // we should expand MemberName::name when Throwable uses StackTrace
2113     // MethodHandles::expand_MemberName(mname, MethodHandles::_suppress_defc|MethodHandles::_suppress_type, CHECK_NULL);
2114     return method;
2115   } else {
2116     short mid       = stackFrame->short_field(_mid_offset);
2117     short version   = stackFrame->short_field(_version_offset);
2118     return holder->method_with_orig_idnum(mid, version);
2119   }
2120 }
2121 
2122 Symbol* java_lang_StackFrameInfo::get_file_name(Handle stackFrame, InstanceKlass* holder) {
2123   if (MemberNameInStackFrame) {
2124     return holder->source_file_name();
2125   } else {
2126     short version = stackFrame->short_field(_version_offset);
2127     return Backtrace::get_source_file_name(holder, version);
2128   }
2129 }
2130 
2131 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci) {
2132   // set Method* or mid/cpref
2133   if (MemberNameInStackFrame) {
2134     oop mname = stackFrame->obj_field(_memberName_offset);
2135     InstanceKlass* ik = method->method_holder();
2136     CallInfo info(method(), ik);
2137     MethodHandles::init_method_MemberName(mname, info);
2138   } else {
2139     int mid = method->orig_method_idnum();
2140     int cpref = method->name_index();
2141     assert((jushort)mid == mid,        "mid should be short");
2142     assert((jushort)cpref == cpref,    "cpref should be short");
2143     java_lang_StackFrameInfo::set_mid(stackFrame(),     (short)mid);
2144     java_lang_StackFrameInfo::set_cpref(stackFrame(),   (short)cpref);
2145   }
2146   // set bci
2147   java_lang_StackFrameInfo::set_bci(stackFrame(), bci);
2148   // method may be redefined; store the version
2149   int version = method->constants()->version();
2150   assert((jushort)version == version, "version should be short");
2151   java_lang_StackFrameInfo::set_version(stackFrame(), (short)version);
2152 }
2153 
2154 void java_lang_StackFrameInfo::fill_methodInfo(Handle stackFrame, TRAPS) {
2155   ResourceMark rm(THREAD);
2156   oop k = stackFrame->obj_field(_declaringClass_offset);
2157   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(k));
2158   Method* method = java_lang_StackFrameInfo::get_method(stackFrame, holder, CHECK);
2159   int bci = stackFrame->int_field(_bci_offset);
2160 
2161   // The method can be NULL if the requested class version is gone
2162   Symbol* sym = (method != NULL) ? method->name() : NULL;
2163   if (MemberNameInStackFrame) {
2164     assert(sym != NULL, "MemberName must have method name");
2165   } else {
2166       // The method can be NULL if the requested class version is gone
2167     if (sym == NULL) {
2168       short cpref   = stackFrame->short_field(_cpref_offset);
2169       sym = holder->constants()->symbol_at(cpref);
2170     }
2171   }
2172 
2173   // set method name
2174   oop methodname = StringTable::intern(sym, CHECK);
2175   java_lang_StackFrameInfo::set_methodName(stackFrame(), methodname);
2176 
2177   // set file name and line number
2178   Symbol* source = get_file_name(stackFrame, holder);
2179   if (source != NULL) {
2180     oop filename = StringTable::intern(source, CHECK);
2181     java_lang_StackFrameInfo::set_fileName(stackFrame(), filename);
2182   }
2183 
2184   // if the method has been redefined, the bci is no longer applicable
2185   short version = stackFrame->short_field(_version_offset);
2186   if (version_matches(method, version)) {
2187     int line_number = Backtrace::get_line_number(method, bci);
2188     java_lang_StackFrameInfo::set_lineNumber(stackFrame(), line_number);
2189   }
2190 }
2191 
2192 void java_lang_StackFrameInfo::compute_offsets() {
2193   Klass* k = SystemDictionary::StackFrameInfo_klass();
2194   compute_offset(_declaringClass_offset, k, vmSymbols::declaringClass_name(),  vmSymbols::class_signature());
2195   compute_offset(_memberName_offset,     k, vmSymbols::memberName_name(),  vmSymbols::object_signature());
2196   compute_offset(_bci_offset,            k, vmSymbols::bci_name(),         vmSymbols::int_signature());
2197   compute_offset(_methodName_offset,     k, vmSymbols::methodName_name(),  vmSymbols::string_signature());
2198   compute_offset(_fileName_offset,       k, vmSymbols::fileName_name(),    vmSymbols::string_signature());
2199   compute_offset(_lineNumber_offset,     k, vmSymbols::lineNumber_name(),  vmSymbols::int_signature());
2200   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2201 }
2202 
2203 void java_lang_LiveStackFrameInfo::compute_offsets() {
2204   Klass* k = SystemDictionary::LiveStackFrameInfo_klass();
2205   compute_offset(_monitors_offset,   k, vmSymbols::monitors_name(),    vmSymbols::object_array_signature());
2206   compute_offset(_locals_offset,     k, vmSymbols::locals_name(),      vmSymbols::object_array_signature());
2207   compute_offset(_operands_offset,   k, vmSymbols::operands_name(),    vmSymbols::object_array_signature());
2208 }
2209 
2210 void java_lang_reflect_AccessibleObject::compute_offsets() {
2211   Klass* k = SystemDictionary::reflect_AccessibleObject_klass();
2212   compute_offset(override_offset, k, vmSymbols::override_name(), vmSymbols::bool_signature());
2213 }
2214 
2215 jboolean java_lang_reflect_AccessibleObject::override(oop reflect) {
2216   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2217   return (jboolean) reflect->bool_field(override_offset);
2218 }
2219 
2220 void java_lang_reflect_AccessibleObject::set_override(oop reflect, jboolean value) {
2221   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2222   reflect->bool_field_put(override_offset, (int) value);
2223 }
2224 
2225 void java_lang_reflect_Method::compute_offsets() {
2226   Klass* k = SystemDictionary::reflect_Method_klass();
2227   compute_offset(clazz_offset,          k, vmSymbols::clazz_name(),          vmSymbols::class_signature());
2228   compute_offset(name_offset,           k, vmSymbols::name_name(),           vmSymbols::string_signature());
2229   compute_offset(returnType_offset,     k, vmSymbols::returnType_name(),     vmSymbols::class_signature());
2230   compute_offset(parameterTypes_offset, k, vmSymbols::parameterTypes_name(), vmSymbols::class_array_signature());
2231   compute_offset(exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), vmSymbols::class_array_signature());
2232   compute_offset(slot_offset,           k, vmSymbols::slot_name(),           vmSymbols::int_signature());
2233   compute_offset(modifiers_offset,      k, vmSymbols::modifiers_name(),      vmSymbols::int_signature());
2234   // The generic signature and annotations fields are only present in 1.5
2235   signature_offset = -1;
2236   annotations_offset = -1;
2237   parameter_annotations_offset = -1;
2238   annotation_default_offset = -1;
2239   type_annotations_offset = -1;
2240   compute_optional_offset(signature_offset,             k, vmSymbols::signature_name(),             vmSymbols::string_signature());
2241   compute_optional_offset(annotations_offset,           k, vmSymbols::annotations_name(),           vmSymbols::byte_array_signature());
2242   compute_optional_offset(parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), vmSymbols::byte_array_signature());
2243   compute_optional_offset(annotation_default_offset,    k, vmSymbols::annotation_default_name(),    vmSymbols::byte_array_signature());
2244   compute_optional_offset(type_annotations_offset,      k, vmSymbols::type_annotations_name(),      vmSymbols::byte_array_signature());
2245 }
2246 
2247 Handle java_lang_reflect_Method::create(TRAPS) {
2248   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2249   Klass* klass = SystemDictionary::reflect_Method_klass();
2250   // This class is eagerly initialized during VM initialization, since we keep a refence
2251   // to one of the methods
2252   assert(InstanceKlass::cast(klass)->is_initialized(), "must be initialized");
2253   return InstanceKlass::cast(klass)->allocate_instance_handle(THREAD);
2254 }
2255 
2256 oop java_lang_reflect_Method::clazz(oop reflect) {
2257   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2258   return reflect->obj_field(clazz_offset);
2259 }
2260 
2261 void java_lang_reflect_Method::set_clazz(oop reflect, oop value) {
2262   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2263    reflect->obj_field_put(clazz_offset, value);
2264 }
2265 
2266 int java_lang_reflect_Method::slot(oop reflect) {
2267   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2268   return reflect->int_field(slot_offset);
2269 }
2270 
2271 void java_lang_reflect_Method::set_slot(oop reflect, int value) {
2272   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2273   reflect->int_field_put(slot_offset, value);
2274 }
2275 
2276 oop java_lang_reflect_Method::name(oop method) {
2277   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2278   return method->obj_field(name_offset);
2279 }
2280 
2281 void java_lang_reflect_Method::set_name(oop method, oop value) {
2282   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2283   method->obj_field_put(name_offset, value);
2284 }
2285 
2286 oop java_lang_reflect_Method::return_type(oop method) {
2287   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2288   return method->obj_field(returnType_offset);
2289 }
2290 
2291 void java_lang_reflect_Method::set_return_type(oop method, oop value) {
2292   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2293   method->obj_field_put(returnType_offset, value);
2294 }
2295 
2296 oop java_lang_reflect_Method::parameter_types(oop method) {
2297   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2298   return method->obj_field(parameterTypes_offset);
2299 }
2300 
2301 void java_lang_reflect_Method::set_parameter_types(oop method, oop value) {
2302   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2303   method->obj_field_put(parameterTypes_offset, value);
2304 }
2305 
2306 oop java_lang_reflect_Method::exception_types(oop method) {
2307   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2308   return method->obj_field(exceptionTypes_offset);
2309 }
2310 
2311 void java_lang_reflect_Method::set_exception_types(oop method, oop value) {
2312   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2313   method->obj_field_put(exceptionTypes_offset, value);
2314 }
2315 
2316 int java_lang_reflect_Method::modifiers(oop method) {
2317   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2318   return method->int_field(modifiers_offset);
2319 }
2320 
2321 void java_lang_reflect_Method::set_modifiers(oop method, int value) {
2322   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2323   method->int_field_put(modifiers_offset, value);
2324 }
2325 
2326 bool java_lang_reflect_Method::has_signature_field() {
2327   return (signature_offset >= 0);
2328 }
2329 
2330 oop java_lang_reflect_Method::signature(oop method) {
2331   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2332   assert(has_signature_field(), "signature field must be present");
2333   return method->obj_field(signature_offset);
2334 }
2335 
2336 void java_lang_reflect_Method::set_signature(oop method, oop value) {
2337   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2338   assert(has_signature_field(), "signature field must be present");
2339   method->obj_field_put(signature_offset, value);
2340 }
2341 
2342 bool java_lang_reflect_Method::has_annotations_field() {
2343   return (annotations_offset >= 0);
2344 }
2345 
2346 oop java_lang_reflect_Method::annotations(oop method) {
2347   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2348   assert(has_annotations_field(), "annotations field must be present");
2349   return method->obj_field(annotations_offset);
2350 }
2351 
2352 void java_lang_reflect_Method::set_annotations(oop method, oop value) {
2353   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2354   assert(has_annotations_field(), "annotations field must be present");
2355   method->obj_field_put(annotations_offset, value);
2356 }
2357 
2358 bool java_lang_reflect_Method::has_parameter_annotations_field() {
2359   return (parameter_annotations_offset >= 0);
2360 }
2361 
2362 oop java_lang_reflect_Method::parameter_annotations(oop method) {
2363   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2364   assert(has_parameter_annotations_field(), "parameter annotations field must be present");
2365   return method->obj_field(parameter_annotations_offset);
2366 }
2367 
2368 void java_lang_reflect_Method::set_parameter_annotations(oop method, oop value) {
2369   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2370   assert(has_parameter_annotations_field(), "parameter annotations field must be present");
2371   method->obj_field_put(parameter_annotations_offset, value);
2372 }
2373 
2374 bool java_lang_reflect_Method::has_annotation_default_field() {
2375   return (annotation_default_offset >= 0);
2376 }
2377 
2378 oop java_lang_reflect_Method::annotation_default(oop method) {
2379   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2380   assert(has_annotation_default_field(), "annotation default field must be present");
2381   return method->obj_field(annotation_default_offset);
2382 }
2383 
2384 void java_lang_reflect_Method::set_annotation_default(oop method, oop value) {
2385   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2386   assert(has_annotation_default_field(), "annotation default field must be present");
2387   method->obj_field_put(annotation_default_offset, value);
2388 }
2389 
2390 bool java_lang_reflect_Method::has_type_annotations_field() {
2391   return (type_annotations_offset >= 0);
2392 }
2393 
2394 oop java_lang_reflect_Method::type_annotations(oop method) {
2395   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2396   assert(has_type_annotations_field(), "type_annotations field must be present");
2397   return method->obj_field(type_annotations_offset);
2398 }
2399 
2400 void java_lang_reflect_Method::set_type_annotations(oop method, oop value) {
2401   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2402   assert(has_type_annotations_field(), "type_annotations field must be present");
2403   method->obj_field_put(type_annotations_offset, value);
2404 }
2405 
2406 void java_lang_reflect_Constructor::compute_offsets() {
2407   Klass* k = SystemDictionary::reflect_Constructor_klass();
2408   compute_offset(clazz_offset,          k, vmSymbols::clazz_name(),          vmSymbols::class_signature());
2409   compute_offset(parameterTypes_offset, k, vmSymbols::parameterTypes_name(), vmSymbols::class_array_signature());
2410   compute_offset(exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), vmSymbols::class_array_signature());
2411   compute_offset(slot_offset,           k, vmSymbols::slot_name(),           vmSymbols::int_signature());
2412   compute_offset(modifiers_offset,      k, vmSymbols::modifiers_name(),      vmSymbols::int_signature());
2413   // The generic signature and annotations fields are only present in 1.5
2414   signature_offset = -1;
2415   annotations_offset = -1;
2416   parameter_annotations_offset = -1;
2417   type_annotations_offset = -1;
2418   compute_optional_offset(signature_offset,             k, vmSymbols::signature_name(),             vmSymbols::string_signature());
2419   compute_optional_offset(annotations_offset,           k, vmSymbols::annotations_name(),           vmSymbols::byte_array_signature());
2420   compute_optional_offset(parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), vmSymbols::byte_array_signature());
2421   compute_optional_offset(type_annotations_offset,      k, vmSymbols::type_annotations_name(),      vmSymbols::byte_array_signature());
2422 }
2423 
2424 Handle java_lang_reflect_Constructor::create(TRAPS) {
2425   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2426   Symbol* name = vmSymbols::java_lang_reflect_Constructor();
2427   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
2428   instanceKlassHandle klass (THREAD, k);
2429   // Ensure it is initialized
2430   klass->initialize(CHECK_NH);
2431   return klass->allocate_instance_handle(THREAD);
2432 }
2433 
2434 oop java_lang_reflect_Constructor::clazz(oop reflect) {
2435   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2436   return reflect->obj_field(clazz_offset);
2437 }
2438 
2439 void java_lang_reflect_Constructor::set_clazz(oop reflect, oop value) {
2440   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2441    reflect->obj_field_put(clazz_offset, value);
2442 }
2443 
2444 oop java_lang_reflect_Constructor::parameter_types(oop constructor) {
2445   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2446   return constructor->obj_field(parameterTypes_offset);
2447 }
2448 
2449 void java_lang_reflect_Constructor::set_parameter_types(oop constructor, oop value) {
2450   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2451   constructor->obj_field_put(parameterTypes_offset, value);
2452 }
2453 
2454 oop java_lang_reflect_Constructor::exception_types(oop constructor) {
2455   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2456   return constructor->obj_field(exceptionTypes_offset);
2457 }
2458 
2459 void java_lang_reflect_Constructor::set_exception_types(oop constructor, oop value) {
2460   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2461   constructor->obj_field_put(exceptionTypes_offset, value);
2462 }
2463 
2464 int java_lang_reflect_Constructor::slot(oop reflect) {
2465   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2466   return reflect->int_field(slot_offset);
2467 }
2468 
2469 void java_lang_reflect_Constructor::set_slot(oop reflect, int value) {
2470   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2471   reflect->int_field_put(slot_offset, value);
2472 }
2473 
2474 int java_lang_reflect_Constructor::modifiers(oop constructor) {
2475   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2476   return constructor->int_field(modifiers_offset);
2477 }
2478 
2479 void java_lang_reflect_Constructor::set_modifiers(oop constructor, int value) {
2480   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2481   constructor->int_field_put(modifiers_offset, value);
2482 }
2483 
2484 bool java_lang_reflect_Constructor::has_signature_field() {
2485   return (signature_offset >= 0);
2486 }
2487 
2488 oop java_lang_reflect_Constructor::signature(oop constructor) {
2489   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2490   assert(has_signature_field(), "signature field must be present");
2491   return constructor->obj_field(signature_offset);
2492 }
2493 
2494 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) {
2495   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2496   assert(has_signature_field(), "signature field must be present");
2497   constructor->obj_field_put(signature_offset, value);
2498 }
2499 
2500 bool java_lang_reflect_Constructor::has_annotations_field() {
2501   return (annotations_offset >= 0);
2502 }
2503 
2504 oop java_lang_reflect_Constructor::annotations(oop constructor) {
2505   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2506   assert(has_annotations_field(), "annotations field must be present");
2507   return constructor->obj_field(annotations_offset);
2508 }
2509 
2510 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) {
2511   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2512   assert(has_annotations_field(), "annotations field must be present");
2513   constructor->obj_field_put(annotations_offset, value);
2514 }
2515 
2516 bool java_lang_reflect_Constructor::has_parameter_annotations_field() {
2517   return (parameter_annotations_offset >= 0);
2518 }
2519 
2520 oop java_lang_reflect_Constructor::parameter_annotations(oop method) {
2521   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2522   assert(has_parameter_annotations_field(), "parameter annotations field must be present");
2523   return method->obj_field(parameter_annotations_offset);
2524 }
2525 
2526 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) {
2527   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2528   assert(has_parameter_annotations_field(), "parameter annotations field must be present");
2529   method->obj_field_put(parameter_annotations_offset, value);
2530 }
2531 
2532 bool java_lang_reflect_Constructor::has_type_annotations_field() {
2533   return (type_annotations_offset >= 0);
2534 }
2535 
2536 oop java_lang_reflect_Constructor::type_annotations(oop constructor) {
2537   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2538   assert(has_type_annotations_field(), "type_annotations field must be present");
2539   return constructor->obj_field(type_annotations_offset);
2540 }
2541 
2542 void java_lang_reflect_Constructor::set_type_annotations(oop constructor, oop value) {
2543   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2544   assert(has_type_annotations_field(), "type_annotations field must be present");
2545   constructor->obj_field_put(type_annotations_offset, value);
2546 }
2547 
2548 void java_lang_reflect_Field::compute_offsets() {
2549   Klass* k = SystemDictionary::reflect_Field_klass();
2550   compute_offset(clazz_offset,     k, vmSymbols::clazz_name(),     vmSymbols::class_signature());
2551   compute_offset(name_offset,      k, vmSymbols::name_name(),      vmSymbols::string_signature());
2552   compute_offset(type_offset,      k, vmSymbols::type_name(),      vmSymbols::class_signature());
2553   compute_offset(slot_offset,      k, vmSymbols::slot_name(),      vmSymbols::int_signature());
2554   compute_offset(modifiers_offset, k, vmSymbols::modifiers_name(), vmSymbols::int_signature());
2555   // The generic signature and annotations fields are only present in 1.5
2556   signature_offset = -1;
2557   annotations_offset = -1;
2558   type_annotations_offset = -1;
2559   compute_optional_offset(signature_offset, k, vmSymbols::signature_name(), vmSymbols::string_signature());
2560   compute_optional_offset(annotations_offset,  k, vmSymbols::annotations_name(),  vmSymbols::byte_array_signature());
2561   compute_optional_offset(type_annotations_offset,  k, vmSymbols::type_annotations_name(),  vmSymbols::byte_array_signature());
2562 }
2563 
2564 Handle java_lang_reflect_Field::create(TRAPS) {
2565   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2566   Symbol* name = vmSymbols::java_lang_reflect_Field();
2567   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
2568   instanceKlassHandle klass (THREAD, k);
2569   // Ensure it is initialized
2570   klass->initialize(CHECK_NH);
2571   return klass->allocate_instance_handle(THREAD);
2572 }
2573 
2574 oop java_lang_reflect_Field::clazz(oop reflect) {
2575   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2576   return reflect->obj_field(clazz_offset);
2577 }
2578 
2579 void java_lang_reflect_Field::set_clazz(oop reflect, oop value) {
2580   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2581    reflect->obj_field_put(clazz_offset, value);
2582 }
2583 
2584 oop java_lang_reflect_Field::name(oop field) {
2585   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2586   return field->obj_field(name_offset);
2587 }
2588 
2589 void java_lang_reflect_Field::set_name(oop field, oop value) {
2590   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2591   field->obj_field_put(name_offset, value);
2592 }
2593 
2594 oop java_lang_reflect_Field::type(oop field) {
2595   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2596   return field->obj_field(type_offset);
2597 }
2598 
2599 void java_lang_reflect_Field::set_type(oop field, oop value) {
2600   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2601   field->obj_field_put(type_offset, value);
2602 }
2603 
2604 int java_lang_reflect_Field::slot(oop reflect) {
2605   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2606   return reflect->int_field(slot_offset);
2607 }
2608 
2609 void java_lang_reflect_Field::set_slot(oop reflect, int value) {
2610   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2611   reflect->int_field_put(slot_offset, value);
2612 }
2613 
2614 int java_lang_reflect_Field::modifiers(oop field) {
2615   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2616   return field->int_field(modifiers_offset);
2617 }
2618 
2619 void java_lang_reflect_Field::set_modifiers(oop field, int value) {
2620   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2621   field->int_field_put(modifiers_offset, value);
2622 }
2623 
2624 bool java_lang_reflect_Field::has_signature_field() {
2625   return (signature_offset >= 0);
2626 }
2627 
2628 oop java_lang_reflect_Field::signature(oop field) {
2629   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2630   assert(has_signature_field(), "signature field must be present");
2631   return field->obj_field(signature_offset);
2632 }
2633 
2634 void java_lang_reflect_Field::set_signature(oop field, oop value) {
2635   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2636   assert(has_signature_field(), "signature field must be present");
2637   field->obj_field_put(signature_offset, value);
2638 }
2639 
2640 bool java_lang_reflect_Field::has_annotations_field() {
2641   return (annotations_offset >= 0);
2642 }
2643 
2644 oop java_lang_reflect_Field::annotations(oop field) {
2645   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2646   assert(has_annotations_field(), "annotations field must be present");
2647   return field->obj_field(annotations_offset);
2648 }
2649 
2650 void java_lang_reflect_Field::set_annotations(oop field, oop value) {
2651   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2652   assert(has_annotations_field(), "annotations field must be present");
2653   field->obj_field_put(annotations_offset, value);
2654 }
2655 
2656 bool java_lang_reflect_Field::has_type_annotations_field() {
2657   return (type_annotations_offset >= 0);
2658 }
2659 
2660 oop java_lang_reflect_Field::type_annotations(oop field) {
2661   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2662   assert(has_type_annotations_field(), "type_annotations field must be present");
2663   return field->obj_field(type_annotations_offset);
2664 }
2665 
2666 void java_lang_reflect_Field::set_type_annotations(oop field, oop value) {
2667   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2668   assert(has_type_annotations_field(), "type_annotations field must be present");
2669   field->obj_field_put(type_annotations_offset, value);
2670 }
2671 
2672 void sun_reflect_ConstantPool::compute_offsets() {
2673   Klass* k = SystemDictionary::reflect_ConstantPool_klass();
2674   // This null test can be removed post beta
2675   if (k != NULL) {
2676     // The field is called ConstantPool* in the sun.reflect.ConstantPool class.
2677     compute_offset(_oop_offset, k, vmSymbols::ConstantPool_name(), vmSymbols::object_signature());
2678   }
2679 }
2680 
2681 void java_lang_reflect_Parameter::compute_offsets() {
2682   Klass* k = SystemDictionary::reflect_Parameter_klass();
2683   if(NULL != k) {
2684     compute_offset(name_offset,        k, vmSymbols::name_name(),        vmSymbols::string_signature());
2685     compute_offset(modifiers_offset,   k, vmSymbols::modifiers_name(),   vmSymbols::int_signature());
2686     compute_offset(index_offset,       k, vmSymbols::index_name(),       vmSymbols::int_signature());
2687     compute_offset(executable_offset,  k, vmSymbols::executable_name(),  vmSymbols::executable_signature());
2688   }
2689 }
2690 
2691 Handle java_lang_reflect_Parameter::create(TRAPS) {
2692   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2693   Symbol* name = vmSymbols::java_lang_reflect_Parameter();
2694   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
2695   instanceKlassHandle klass (THREAD, k);
2696   // Ensure it is initialized
2697   klass->initialize(CHECK_NH);
2698   return klass->allocate_instance_handle(THREAD);
2699 }
2700 
2701 oop java_lang_reflect_Parameter::name(oop param) {
2702   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2703   return param->obj_field(name_offset);
2704 }
2705 
2706 void java_lang_reflect_Parameter::set_name(oop param, oop value) {
2707   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2708   param->obj_field_put(name_offset, value);
2709 }
2710 
2711 int java_lang_reflect_Parameter::modifiers(oop param) {
2712   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2713   return param->int_field(modifiers_offset);
2714 }
2715 
2716 void java_lang_reflect_Parameter::set_modifiers(oop param, int value) {
2717   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2718   param->int_field_put(modifiers_offset, value);
2719 }
2720 
2721 int java_lang_reflect_Parameter::index(oop param) {
2722   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2723   return param->int_field(index_offset);
2724 }
2725 
2726 void java_lang_reflect_Parameter::set_index(oop param, int value) {
2727   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2728   param->int_field_put(index_offset, value);
2729 }
2730 
2731 oop java_lang_reflect_Parameter::executable(oop param) {
2732   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2733   return param->obj_field(executable_offset);
2734 }
2735 
2736 void java_lang_reflect_Parameter::set_executable(oop param, oop value) {
2737   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2738   param->obj_field_put(executable_offset, value);
2739 }
2740 
2741 
2742 Handle sun_reflect_ConstantPool::create(TRAPS) {
2743   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2744   Klass* k = SystemDictionary::reflect_ConstantPool_klass();
2745   instanceKlassHandle klass (THREAD, k);
2746   // Ensure it is initialized
2747   klass->initialize(CHECK_NH);
2748   return klass->allocate_instance_handle(THREAD);
2749 }
2750 
2751 
2752 void sun_reflect_ConstantPool::set_cp(oop reflect, ConstantPool* value) {
2753   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2754   oop mirror = value->pool_holder()->java_mirror();
2755   // Save the mirror to get back the constant pool.
2756   reflect->obj_field_put(_oop_offset, mirror);
2757 }
2758 
2759 ConstantPool* sun_reflect_ConstantPool::get_cp(oop reflect) {
2760   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
2761 
2762   oop mirror = reflect->obj_field(_oop_offset);
2763   Klass* k = java_lang_Class::as_Klass(mirror);
2764   assert(k->is_instance_klass(), "Must be");
2765 
2766   // Get the constant pool back from the klass.  Since class redefinition
2767   // merges the new constant pool into the old, this is essentially the
2768   // same constant pool as the original.  If constant pool merging is
2769   // no longer done in the future, this will have to change to save
2770   // the original.
2771   return InstanceKlass::cast(k)->constants();
2772 }
2773 
2774 void sun_reflect_UnsafeStaticFieldAccessorImpl::compute_offsets() {
2775   Klass* k = SystemDictionary::reflect_UnsafeStaticFieldAccessorImpl_klass();
2776   // This null test can be removed post beta
2777   if (k != NULL) {
2778     compute_offset(_base_offset, k,
2779                    vmSymbols::base_name(), vmSymbols::object_signature());
2780   }
2781 }
2782 
2783 oop java_lang_boxing_object::initialize_and_allocate(BasicType type, TRAPS) {
2784   Klass* k = SystemDictionary::box_klass(type);
2785   if (k == NULL)  return NULL;
2786   instanceKlassHandle h (THREAD, k);
2787   if (!h->is_initialized())  h->initialize(CHECK_0);
2788   return h->allocate_instance(THREAD);
2789 }
2790 
2791 
2792 oop java_lang_boxing_object::create(BasicType type, jvalue* value, TRAPS) {
2793   oop box = initialize_and_allocate(type, CHECK_0);
2794   if (box == NULL)  return NULL;
2795   switch (type) {
2796     case T_BOOLEAN:
2797       box->bool_field_put(value_offset, value->z);
2798       break;
2799     case T_CHAR:
2800       box->char_field_put(value_offset, value->c);
2801       break;
2802     case T_FLOAT:
2803       box->float_field_put(value_offset, value->f);
2804       break;
2805     case T_DOUBLE:
2806       box->double_field_put(long_value_offset, value->d);
2807       break;
2808     case T_BYTE:
2809       box->byte_field_put(value_offset, value->b);
2810       break;
2811     case T_SHORT:
2812       box->short_field_put(value_offset, value->s);
2813       break;
2814     case T_INT:
2815       box->int_field_put(value_offset, value->i);
2816       break;
2817     case T_LONG:
2818       box->long_field_put(long_value_offset, value->j);
2819       break;
2820     default:
2821       return NULL;
2822   }
2823   return box;
2824 }
2825 
2826 
2827 BasicType java_lang_boxing_object::basic_type(oop box) {
2828   if (box == NULL)  return T_ILLEGAL;
2829   BasicType type = SystemDictionary::box_klass_type(box->klass());
2830   if (type == T_OBJECT)         // 'unknown' value returned by SD::bkt
2831     return T_ILLEGAL;
2832   return type;
2833 }
2834 
2835 
2836 BasicType java_lang_boxing_object::get_value(oop box, jvalue* value) {
2837   BasicType type = SystemDictionary::box_klass_type(box->klass());
2838   switch (type) {
2839   case T_BOOLEAN:
2840     value->z = box->bool_field(value_offset);
2841     break;
2842   case T_CHAR:
2843     value->c = box->char_field(value_offset);
2844     break;
2845   case T_FLOAT:
2846     value->f = box->float_field(value_offset);
2847     break;
2848   case T_DOUBLE:
2849     value->d = box->double_field(long_value_offset);
2850     break;
2851   case T_BYTE:
2852     value->b = box->byte_field(value_offset);
2853     break;
2854   case T_SHORT:
2855     value->s = box->short_field(value_offset);
2856     break;
2857   case T_INT:
2858     value->i = box->int_field(value_offset);
2859     break;
2860   case T_LONG:
2861     value->j = box->long_field(long_value_offset);
2862     break;
2863   default:
2864     return T_ILLEGAL;
2865   } // end switch
2866   return type;
2867 }
2868 
2869 
2870 BasicType java_lang_boxing_object::set_value(oop box, jvalue* value) {
2871   BasicType type = SystemDictionary::box_klass_type(box->klass());
2872   switch (type) {
2873   case T_BOOLEAN:
2874     box->bool_field_put(value_offset, value->z);
2875     break;
2876   case T_CHAR:
2877     box->char_field_put(value_offset, value->c);
2878     break;
2879   case T_FLOAT:
2880     box->float_field_put(value_offset, value->f);
2881     break;
2882   case T_DOUBLE:
2883     box->double_field_put(long_value_offset, value->d);
2884     break;
2885   case T_BYTE:
2886     box->byte_field_put(value_offset, value->b);
2887     break;
2888   case T_SHORT:
2889     box->short_field_put(value_offset, value->s);
2890     break;
2891   case T_INT:
2892     box->int_field_put(value_offset, value->i);
2893     break;
2894   case T_LONG:
2895     box->long_field_put(long_value_offset, value->j);
2896     break;
2897   default:
2898     return T_ILLEGAL;
2899   } // end switch
2900   return type;
2901 }
2902 
2903 
2904 void java_lang_boxing_object::print(BasicType type, jvalue* value, outputStream* st) {
2905   switch (type) {
2906   case T_BOOLEAN:   st->print("%s", value->z ? "true" : "false");   break;
2907   case T_CHAR:      st->print("%d", value->c);                      break;
2908   case T_BYTE:      st->print("%d", value->b);                      break;
2909   case T_SHORT:     st->print("%d", value->s);                      break;
2910   case T_INT:       st->print("%d", value->i);                      break;
2911   case T_LONG:      st->print(INT64_FORMAT, value->j);              break;
2912   case T_FLOAT:     st->print("%f", value->f);                      break;
2913   case T_DOUBLE:    st->print("%lf", value->d);                     break;
2914   default:          st->print("type %d?", type);                    break;
2915   }
2916 }
2917 
2918 
2919 // Support for java_lang_ref_Reference
2920 HeapWord *java_lang_ref_Reference::pending_list_lock_addr() {
2921   InstanceKlass* ik = SystemDictionary::Reference_klass();
2922   address addr = ik->static_field_addr(static_lock_offset);
2923   return (HeapWord*) addr;
2924 }
2925 
2926 oop java_lang_ref_Reference::pending_list_lock() {
2927   InstanceKlass* ik = SystemDictionary::Reference_klass();
2928   address addr = ik->static_field_addr(static_lock_offset);
2929   if (UseCompressedOops) {
2930     return oopDesc::load_decode_heap_oop((narrowOop *)addr);
2931   } else {
2932     return oopDesc::load_decode_heap_oop((oop*)addr);
2933   }
2934 }
2935 
2936 HeapWord *java_lang_ref_Reference::pending_list_addr() {
2937   InstanceKlass* ik = SystemDictionary::Reference_klass();
2938   address addr = ik->static_field_addr(static_pending_offset);
2939   // XXX This might not be HeapWord aligned, almost rather be char *.
2940   return (HeapWord*)addr;
2941 }
2942 
2943 oop java_lang_ref_Reference::pending_list() {
2944   char *addr = (char *)pending_list_addr();
2945   if (UseCompressedOops) {
2946     return oopDesc::load_decode_heap_oop((narrowOop *)addr);
2947   } else {
2948     return oopDesc::load_decode_heap_oop((oop*)addr);
2949   }
2950 }
2951 
2952 
2953 // Support for java_lang_ref_SoftReference
2954 
2955 jlong java_lang_ref_SoftReference::timestamp(oop ref) {
2956   return ref->long_field(timestamp_offset);
2957 }
2958 
2959 jlong java_lang_ref_SoftReference::clock() {
2960   InstanceKlass* ik = SystemDictionary::SoftReference_klass();
2961   jlong* offset = (jlong*)ik->static_field_addr(static_clock_offset);
2962   return *offset;
2963 }
2964 
2965 void java_lang_ref_SoftReference::set_clock(jlong value) {
2966   InstanceKlass* ik = SystemDictionary::SoftReference_klass();
2967   jlong* offset = (jlong*)ik->static_field_addr(static_clock_offset);
2968   *offset = value;
2969 }
2970 
2971 // Support for java_lang_invoke_DirectMethodHandle
2972 
2973 int java_lang_invoke_DirectMethodHandle::_member_offset;
2974 
2975 oop java_lang_invoke_DirectMethodHandle::member(oop dmh) {
2976   oop member_name = NULL;
2977   bool is_dmh = dmh->is_oop() && java_lang_invoke_DirectMethodHandle::is_instance(dmh);
2978   assert(is_dmh, "a DirectMethodHandle oop is expected");
2979   if (is_dmh) {
2980     member_name = dmh->obj_field(member_offset_in_bytes());
2981   }
2982   return member_name;
2983 }
2984 
2985 void java_lang_invoke_DirectMethodHandle::compute_offsets() {
2986   Klass* klass_oop = SystemDictionary::DirectMethodHandle_klass();
2987   if (klass_oop != NULL) {
2988     compute_offset(_member_offset, klass_oop, vmSymbols::member_name(), vmSymbols::java_lang_invoke_MemberName_signature());
2989   }
2990 }
2991 
2992 // Support for java_lang_invoke_MethodHandle
2993 
2994 int java_lang_invoke_MethodHandle::_type_offset;
2995 int java_lang_invoke_MethodHandle::_form_offset;
2996 
2997 int java_lang_invoke_MemberName::_clazz_offset;
2998 int java_lang_invoke_MemberName::_name_offset;
2999 int java_lang_invoke_MemberName::_type_offset;
3000 int java_lang_invoke_MemberName::_flags_offset;
3001 int java_lang_invoke_MemberName::_vmtarget_offset;
3002 int java_lang_invoke_MemberName::_vmloader_offset;
3003 int java_lang_invoke_MemberName::_vmindex_offset;
3004 
3005 int java_lang_invoke_LambdaForm::_vmentry_offset;
3006 
3007 void java_lang_invoke_MethodHandle::compute_offsets() {
3008   Klass* klass_oop = SystemDictionary::MethodHandle_klass();
3009   if (klass_oop != NULL) {
3010     compute_offset(_type_offset, klass_oop, vmSymbols::type_name(), vmSymbols::java_lang_invoke_MethodType_signature());
3011     compute_offset(_form_offset, klass_oop, vmSymbols::form_name(), vmSymbols::java_lang_invoke_LambdaForm_signature());
3012   }
3013 }
3014 
3015 void java_lang_invoke_MemberName::compute_offsets() {
3016   Klass* klass_oop = SystemDictionary::MemberName_klass();
3017   if (klass_oop != NULL) {
3018     compute_offset(_clazz_offset,     klass_oop, vmSymbols::clazz_name(),     vmSymbols::class_signature());
3019     compute_offset(_name_offset,      klass_oop, vmSymbols::name_name(),      vmSymbols::string_signature());
3020     compute_offset(_type_offset,      klass_oop, vmSymbols::type_name(),      vmSymbols::object_signature());
3021     compute_offset(_flags_offset,     klass_oop, vmSymbols::flags_name(),     vmSymbols::int_signature());
3022     MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3023   }
3024 }
3025 
3026 void java_lang_invoke_LambdaForm::compute_offsets() {
3027   Klass* klass_oop = SystemDictionary::LambdaForm_klass();
3028   if (klass_oop != NULL) {
3029     compute_offset(_vmentry_offset, klass_oop, vmSymbols::vmentry_name(), vmSymbols::java_lang_invoke_MemberName_signature());
3030   }
3031 }
3032 
3033 bool java_lang_invoke_LambdaForm::is_instance(oop obj) {
3034   return obj != NULL && is_subclass(obj->klass());
3035 }
3036 
3037 
3038 oop java_lang_invoke_MethodHandle::type(oop mh) {
3039   return mh->obj_field(_type_offset);
3040 }
3041 
3042 void java_lang_invoke_MethodHandle::set_type(oop mh, oop mtype) {
3043   mh->obj_field_put(_type_offset, mtype);
3044 }
3045 
3046 oop java_lang_invoke_MethodHandle::form(oop mh) {
3047   assert(_form_offset != 0, "");
3048   return mh->obj_field(_form_offset);
3049 }
3050 
3051 void java_lang_invoke_MethodHandle::set_form(oop mh, oop lform) {
3052   assert(_form_offset != 0, "");
3053   mh->obj_field_put(_form_offset, lform);
3054 }
3055 
3056 /// MemberName accessors
3057 
3058 oop java_lang_invoke_MemberName::clazz(oop mname) {
3059   assert(is_instance(mname), "wrong type");
3060   return mname->obj_field(_clazz_offset);
3061 }
3062 
3063 void java_lang_invoke_MemberName::set_clazz(oop mname, oop clazz) {
3064   assert(is_instance(mname), "wrong type");
3065   mname->obj_field_put(_clazz_offset, clazz);
3066 }
3067 
3068 oop java_lang_invoke_MemberName::name(oop mname) {
3069   assert(is_instance(mname), "wrong type");
3070   return mname->obj_field(_name_offset);
3071 }
3072 
3073 void java_lang_invoke_MemberName::set_name(oop mname, oop name) {
3074   assert(is_instance(mname), "wrong type");
3075   mname->obj_field_put(_name_offset, name);
3076 }
3077 
3078 oop java_lang_invoke_MemberName::type(oop mname) {
3079   assert(is_instance(mname), "wrong type");
3080   return mname->obj_field(_type_offset);
3081 }
3082 
3083 void java_lang_invoke_MemberName::set_type(oop mname, oop type) {
3084   assert(is_instance(mname), "wrong type");
3085   mname->obj_field_put(_type_offset, type);
3086 }
3087 
3088 int java_lang_invoke_MemberName::flags(oop mname) {
3089   assert(is_instance(mname), "wrong type");
3090   return mname->int_field(_flags_offset);
3091 }
3092 
3093 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) {
3094   assert(is_instance(mname), "wrong type");
3095   mname->int_field_put(_flags_offset, flags);
3096 }
3097 
3098 Metadata* java_lang_invoke_MemberName::vmtarget(oop mname) {
3099   assert(is_instance(mname), "wrong type");
3100   return (Metadata*)mname->address_field(_vmtarget_offset);
3101 }
3102 
3103 bool java_lang_invoke_MemberName::is_method(oop mname) {
3104   assert(is_instance(mname), "must be MemberName");
3105   return (flags(mname) & (MN_IS_METHOD | MN_IS_CONSTRUCTOR)) > 0;
3106 }
3107 
3108 void java_lang_invoke_MemberName::set_vmtarget(oop mname, Metadata* ref) {
3109   assert(is_instance(mname), "wrong type");
3110   // check the type of the vmtarget
3111   oop dependency = NULL;
3112   if (ref != NULL) {
3113     switch (flags(mname) & (MN_IS_METHOD |
3114                             MN_IS_CONSTRUCTOR |
3115                             MN_IS_FIELD)) {
3116     case MN_IS_METHOD:
3117     case MN_IS_CONSTRUCTOR:
3118       assert(ref->is_method(), "should be a method");
3119       dependency = ((Method*)ref)->method_holder()->java_mirror();
3120       break;
3121     case MN_IS_FIELD:
3122       assert(ref->is_klass(), "should be a class");
3123       dependency = ((Klass*)ref)->java_mirror();
3124       break;
3125     default:
3126       ShouldNotReachHere();
3127     }
3128   }
3129   mname->address_field_put(_vmtarget_offset, (address)ref);
3130   // Add a reference to the loader (actually mirror because anonymous classes will not have
3131   // distinct loaders) to ensure the metadata is kept alive
3132   // This mirror may be different than the one in clazz field.
3133   mname->obj_field_put(_vmloader_offset, dependency);
3134 }
3135 
3136 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) {
3137   assert(is_instance(mname), "wrong type");
3138   return (intptr_t) mname->address_field(_vmindex_offset);
3139 }
3140 
3141 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
3142   assert(is_instance(mname), "wrong type");
3143   mname->address_field_put(_vmindex_offset, (address) index);
3144 }
3145 
3146 oop java_lang_invoke_LambdaForm::vmentry(oop lform) {
3147   assert(is_instance(lform), "wrong type");
3148   return lform->obj_field(_vmentry_offset);
3149 }
3150 
3151 
3152 // Support for java_lang_invoke_MethodType
3153 
3154 int java_lang_invoke_MethodType::_rtype_offset;
3155 int java_lang_invoke_MethodType::_ptypes_offset;
3156 
3157 void java_lang_invoke_MethodType::compute_offsets() {
3158   Klass* k = SystemDictionary::MethodType_klass();
3159   if (k != NULL) {
3160     compute_offset(_rtype_offset,  k, vmSymbols::rtype_name(),  vmSymbols::class_signature());
3161     compute_offset(_ptypes_offset, k, vmSymbols::ptypes_name(), vmSymbols::class_array_signature());
3162   }
3163 }
3164 
3165 void java_lang_invoke_MethodType::print_signature(oop mt, outputStream* st) {
3166   st->print("(");
3167   objArrayOop pts = ptypes(mt);
3168   for (int i = 0, limit = pts->length(); i < limit; i++) {
3169     java_lang_Class::print_signature(pts->obj_at(i), st);
3170   }
3171   st->print(")");
3172   java_lang_Class::print_signature(rtype(mt), st);
3173 }
3174 
3175 Symbol* java_lang_invoke_MethodType::as_signature(oop mt, bool intern_if_not_found, TRAPS) {
3176   ResourceMark rm;
3177   stringStream buffer(128);
3178   print_signature(mt, &buffer);
3179   const char* sigstr =       buffer.base();
3180   int         siglen = (int) buffer.size();
3181   Symbol *name;
3182   if (!intern_if_not_found) {
3183     name = SymbolTable::probe(sigstr, siglen);
3184   } else {
3185     name = SymbolTable::new_symbol(sigstr, siglen, THREAD);
3186   }
3187   return name;
3188 }
3189 
3190 bool java_lang_invoke_MethodType::equals(oop mt1, oop mt2) {
3191   if (mt1 == mt2)
3192     return true;
3193   if (rtype(mt1) != rtype(mt2))
3194     return false;
3195   if (ptype_count(mt1) != ptype_count(mt2))
3196     return false;
3197   for (int i = ptype_count(mt1) - 1; i >= 0; i--) {
3198     if (ptype(mt1, i) != ptype(mt2, i))
3199       return false;
3200   }
3201   return true;
3202 }
3203 
3204 oop java_lang_invoke_MethodType::rtype(oop mt) {
3205   assert(is_instance(mt), "must be a MethodType");
3206   return mt->obj_field(_rtype_offset);
3207 }
3208 
3209 objArrayOop java_lang_invoke_MethodType::ptypes(oop mt) {
3210   assert(is_instance(mt), "must be a MethodType");
3211   return (objArrayOop) mt->obj_field(_ptypes_offset);
3212 }
3213 
3214 oop java_lang_invoke_MethodType::ptype(oop mt, int idx) {
3215   return ptypes(mt)->obj_at(idx);
3216 }
3217 
3218 int java_lang_invoke_MethodType::ptype_count(oop mt) {
3219   return ptypes(mt)->length();
3220 }
3221 
3222 int java_lang_invoke_MethodType::ptype_slot_count(oop mt) {
3223   objArrayOop pts = ptypes(mt);
3224   int count = pts->length();
3225   int slots = 0;
3226   for (int i = 0; i < count; i++) {
3227     BasicType bt = java_lang_Class::as_BasicType(pts->obj_at(i));
3228     slots += type2size[bt];
3229   }
3230   return slots;
3231 }
3232 
3233 int java_lang_invoke_MethodType::rtype_slot_count(oop mt) {
3234   BasicType bt = java_lang_Class::as_BasicType(rtype(mt));
3235   return type2size[bt];
3236 }
3237 
3238 
3239 // Support for java_lang_invoke_CallSite
3240 
3241 int java_lang_invoke_CallSite::_target_offset;
3242 int java_lang_invoke_CallSite::_context_offset;
3243 
3244 void java_lang_invoke_CallSite::compute_offsets() {
3245   Klass* k = SystemDictionary::CallSite_klass();
3246   if (k != NULL) {
3247     compute_offset(_target_offset, k, vmSymbols::target_name(), vmSymbols::java_lang_invoke_MethodHandle_signature());
3248     compute_offset(_context_offset, k, vmSymbols::context_name(),
3249                    vmSymbols::java_lang_invoke_MethodHandleNatives_CallSiteContext_signature());
3250   }
3251 }
3252 
3253 oop java_lang_invoke_CallSite::context(oop call_site) {
3254   assert(java_lang_invoke_CallSite::is_instance(call_site), "");
3255 
3256   oop dep_oop = call_site->obj_field(_context_offset);
3257   return dep_oop;
3258 }
3259 
3260 // Support for java_lang_invoke_MethodHandleNatives_CallSiteContext
3261 
3262 int java_lang_invoke_MethodHandleNatives_CallSiteContext::_vmdependencies_offset;
3263 
3264 void java_lang_invoke_MethodHandleNatives_CallSiteContext::compute_offsets() {
3265   Klass* k = SystemDictionary::Context_klass();
3266   if (k != NULL) {
3267     CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3268   }
3269 }
3270 
3271 DependencyContext java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(oop call_site) {
3272   assert(java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(call_site), "");
3273   intptr_t* vmdeps_addr = (intptr_t*)call_site->address_field_addr(_vmdependencies_offset);
3274   DependencyContext dep_ctx(vmdeps_addr);
3275   return dep_ctx;
3276 }
3277 
3278 // Support for java_security_AccessControlContext
3279 
3280 int java_security_AccessControlContext::_context_offset = 0;
3281 int java_security_AccessControlContext::_privilegedContext_offset = 0;
3282 int java_security_AccessControlContext::_isPrivileged_offset = 0;
3283 int java_security_AccessControlContext::_isAuthorized_offset = -1;
3284 
3285 void java_security_AccessControlContext::compute_offsets() {
3286   assert(_isPrivileged_offset == 0, "offsets should be initialized only once");
3287   fieldDescriptor fd;
3288   InstanceKlass* ik = SystemDictionary::AccessControlContext_klass();
3289 
3290   if (!ik->find_local_field(vmSymbols::context_name(), vmSymbols::protectiondomain_signature(), &fd)) {
3291     fatal("Invalid layout of java.security.AccessControlContext");
3292   }
3293   _context_offset = fd.offset();
3294 
3295   if (!ik->find_local_field(vmSymbols::privilegedContext_name(), vmSymbols::accesscontrolcontext_signature(), &fd)) {
3296     fatal("Invalid layout of java.security.AccessControlContext");
3297   }
3298   _privilegedContext_offset = fd.offset();
3299 
3300   if (!ik->find_local_field(vmSymbols::isPrivileged_name(), vmSymbols::bool_signature(), &fd)) {
3301     fatal("Invalid layout of java.security.AccessControlContext");
3302   }
3303   _isPrivileged_offset = fd.offset();
3304 
3305   // The offset may not be present for bootstrapping with older JDK.
3306   if (ik->find_local_field(vmSymbols::isAuthorized_name(), vmSymbols::bool_signature(), &fd)) {
3307     _isAuthorized_offset = fd.offset();
3308   }
3309 }
3310 
3311 
3312 bool java_security_AccessControlContext::is_authorized(Handle context) {
3313   assert(context.not_null() && context->klass() == SystemDictionary::AccessControlContext_klass(), "Invalid type");
3314   assert(_isAuthorized_offset != -1, "should be set");
3315   return context->bool_field(_isAuthorized_offset) != 0;
3316 }
3317 
3318 oop java_security_AccessControlContext::create(objArrayHandle context, bool isPrivileged, Handle privileged_context, TRAPS) {
3319   assert(_isPrivileged_offset != 0, "offsets should have been initialized");
3320   // Ensure klass is initialized
3321   SystemDictionary::AccessControlContext_klass()->initialize(CHECK_0);
3322   // Allocate result
3323   oop result = SystemDictionary::AccessControlContext_klass()->allocate_instance(CHECK_0);
3324   // Fill in values
3325   result->obj_field_put(_context_offset, context());
3326   result->obj_field_put(_privilegedContext_offset, privileged_context());
3327   result->bool_field_put(_isPrivileged_offset, isPrivileged);
3328   // whitelist AccessControlContexts created by the JVM if present
3329   if (_isAuthorized_offset != -1) {
3330     result->bool_field_put(_isAuthorized_offset, true);
3331   }
3332   return result;
3333 }
3334 
3335 
3336 // Support for java_lang_ClassLoader
3337 
3338 bool java_lang_ClassLoader::offsets_computed = false;
3339 int  java_lang_ClassLoader::_loader_data_offset = -1;
3340 int  java_lang_ClassLoader::parallelCapable_offset = -1;
3341 
3342 ClassLoaderData** java_lang_ClassLoader::loader_data_addr(oop loader) {
3343     assert(loader != NULL && loader->is_oop(), "loader must be oop");
3344     return (ClassLoaderData**) loader->address_field_addr(_loader_data_offset);
3345 }
3346 
3347 ClassLoaderData* java_lang_ClassLoader::loader_data(oop loader) {
3348   return *java_lang_ClassLoader::loader_data_addr(loader);
3349 }
3350 
3351 void java_lang_ClassLoader::compute_offsets() {
3352   assert(!offsets_computed, "offsets should be initialized only once");
3353   offsets_computed = true;
3354 
3355   // The field indicating parallelCapable (parallelLockMap) is only present starting in 7,
3356   Klass* k1 = SystemDictionary::ClassLoader_klass();
3357   compute_optional_offset(parallelCapable_offset,
3358     k1, vmSymbols::parallelCapable_name(), vmSymbols::concurrenthashmap_signature());
3359 
3360   CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3361 }
3362 
3363 oop java_lang_ClassLoader::parent(oop loader) {
3364   assert(is_instance(loader), "loader must be oop");
3365   return loader->obj_field(parent_offset);
3366 }
3367 
3368 bool java_lang_ClassLoader::isAncestor(oop loader, oop cl) {
3369   assert(is_instance(loader), "loader must be oop");
3370   assert(cl == NULL || is_instance(cl), "cl argument must be oop");
3371   oop acl = loader;
3372   debug_only(jint loop_count = 0);
3373   // This loop taken verbatim from ClassLoader.java:
3374   do {
3375     acl = parent(acl);
3376     if (cl == acl) {
3377       return true;
3378     }
3379     assert(++loop_count > 0, "loop_count overflow");
3380   } while (acl != NULL);
3381   return false;
3382 }
3383 
3384 bool java_lang_ClassLoader::is_instance(oop obj) {
3385   return obj != NULL && is_subclass(obj->klass());
3386 }
3387 
3388 
3389 // For class loader classes, parallelCapable defined
3390 // based on non-null field
3391 // Written to by java.lang.ClassLoader, vm only reads this field, doesn't set it
3392 bool java_lang_ClassLoader::parallelCapable(oop class_loader) {
3393   if (parallelCapable_offset == -1) {
3394      // Default for backward compatibility is false
3395      return false;
3396   }
3397   return (class_loader->obj_field(parallelCapable_offset) != NULL);
3398 }
3399 
3400 bool java_lang_ClassLoader::is_trusted_loader(oop loader) {
3401   // Fix for 4474172; see evaluation for more details
3402   loader = non_reflection_class_loader(loader);
3403 
3404   oop cl = SystemDictionary::java_system_loader();
3405   while(cl != NULL) {
3406     if (cl == loader) return true;
3407     cl = parent(cl);
3408   }
3409   return false;
3410 }
3411 
3412 oop java_lang_ClassLoader::non_reflection_class_loader(oop loader) {
3413   if (loader != NULL) {
3414     // See whether this is one of the class loaders associated with
3415     // the generated bytecodes for reflection, and if so, "magically"
3416     // delegate to its parent to prevent class loading from occurring
3417     // in places where applications using reflection didn't expect it.
3418     Klass* delegating_cl_class = SystemDictionary::reflect_DelegatingClassLoader_klass();
3419     // This might be null in non-1.4 JDKs
3420     if (delegating_cl_class != NULL && loader->is_a(delegating_cl_class)) {
3421       return parent(loader);
3422     }
3423   }
3424   return loader;
3425 }
3426 
3427 
3428 // Support for java_lang_System
3429 int java_lang_System::in_offset_in_bytes() {
3430   return (InstanceMirrorKlass::offset_of_static_fields() + static_in_offset);
3431 }
3432 
3433 
3434 int java_lang_System::out_offset_in_bytes() {
3435   return (InstanceMirrorKlass::offset_of_static_fields() + static_out_offset);
3436 }
3437 
3438 
3439 int java_lang_System::err_offset_in_bytes() {
3440   return (InstanceMirrorKlass::offset_of_static_fields() + static_err_offset);
3441 }
3442 
3443 
3444 bool java_lang_System::has_security_manager() {
3445   InstanceKlass* ik = SystemDictionary::System_klass();
3446   address addr = ik->static_field_addr(static_security_offset);
3447   if (UseCompressedOops) {
3448     return oopDesc::load_decode_heap_oop((narrowOop *)addr) != NULL;
3449   } else {
3450     return oopDesc::load_decode_heap_oop((oop*)addr) != NULL;
3451   }
3452 }
3453 
3454 int java_lang_Class::_klass_offset;
3455 int java_lang_Class::_array_klass_offset;
3456 int java_lang_Class::_oop_size_offset;
3457 int java_lang_Class::_static_oop_field_count_offset;
3458 int java_lang_Class::_class_loader_offset;
3459 int java_lang_Class::_protection_domain_offset;
3460 int java_lang_Class::_component_mirror_offset;
3461 int java_lang_Class::_init_lock_offset;
3462 int java_lang_Class::_signers_offset;
3463 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL;
3464 int java_lang_Throwable::backtrace_offset;
3465 int java_lang_Throwable::detailMessage_offset;
3466 int java_lang_Throwable::cause_offset;
3467 int java_lang_Throwable::stackTrace_offset;
3468 int java_lang_Throwable::static_unassigned_stacktrace_offset;
3469 int java_lang_reflect_AccessibleObject::override_offset;
3470 int java_lang_reflect_Method::clazz_offset;
3471 int java_lang_reflect_Method::name_offset;
3472 int java_lang_reflect_Method::returnType_offset;
3473 int java_lang_reflect_Method::parameterTypes_offset;
3474 int java_lang_reflect_Method::exceptionTypes_offset;
3475 int java_lang_reflect_Method::slot_offset;
3476 int java_lang_reflect_Method::modifiers_offset;
3477 int java_lang_reflect_Method::signature_offset;
3478 int java_lang_reflect_Method::annotations_offset;
3479 int java_lang_reflect_Method::parameter_annotations_offset;
3480 int java_lang_reflect_Method::annotation_default_offset;
3481 int java_lang_reflect_Method::type_annotations_offset;
3482 int java_lang_reflect_Constructor::clazz_offset;
3483 int java_lang_reflect_Constructor::parameterTypes_offset;
3484 int java_lang_reflect_Constructor::exceptionTypes_offset;
3485 int java_lang_reflect_Constructor::slot_offset;
3486 int java_lang_reflect_Constructor::modifiers_offset;
3487 int java_lang_reflect_Constructor::signature_offset;
3488 int java_lang_reflect_Constructor::annotations_offset;
3489 int java_lang_reflect_Constructor::parameter_annotations_offset;
3490 int java_lang_reflect_Constructor::type_annotations_offset;
3491 int java_lang_reflect_Field::clazz_offset;
3492 int java_lang_reflect_Field::name_offset;
3493 int java_lang_reflect_Field::type_offset;
3494 int java_lang_reflect_Field::slot_offset;
3495 int java_lang_reflect_Field::modifiers_offset;
3496 int java_lang_reflect_Field::signature_offset;
3497 int java_lang_reflect_Field::annotations_offset;
3498 int java_lang_reflect_Field::type_annotations_offset;
3499 int java_lang_reflect_Parameter::name_offset;
3500 int java_lang_reflect_Parameter::modifiers_offset;
3501 int java_lang_reflect_Parameter::index_offset;
3502 int java_lang_reflect_Parameter::executable_offset;
3503 int java_lang_boxing_object::value_offset;
3504 int java_lang_boxing_object::long_value_offset;
3505 int java_lang_ref_Reference::referent_offset;
3506 int java_lang_ref_Reference::queue_offset;
3507 int java_lang_ref_Reference::next_offset;
3508 int java_lang_ref_Reference::discovered_offset;
3509 int java_lang_ref_Reference::static_lock_offset;
3510 int java_lang_ref_Reference::static_pending_offset;
3511 int java_lang_ref_Reference::number_of_fake_oop_fields;
3512 int java_lang_ref_SoftReference::timestamp_offset;
3513 int java_lang_ref_SoftReference::static_clock_offset;
3514 int java_lang_ClassLoader::parent_offset;
3515 int java_lang_System::static_in_offset;
3516 int java_lang_System::static_out_offset;
3517 int java_lang_System::static_err_offset;
3518 int java_lang_System::static_security_offset;
3519 int java_lang_StackTraceElement::declaringClass_offset;
3520 int java_lang_StackTraceElement::methodName_offset;
3521 int java_lang_StackTraceElement::fileName_offset;
3522 int java_lang_StackTraceElement::lineNumber_offset;
3523 int java_lang_StackFrameInfo::_declaringClass_offset;
3524 int java_lang_StackFrameInfo::_memberName_offset;
3525 int java_lang_StackFrameInfo::_bci_offset;
3526 int java_lang_StackFrameInfo::_methodName_offset;
3527 int java_lang_StackFrameInfo::_fileName_offset;
3528 int java_lang_StackFrameInfo::_lineNumber_offset;
3529 int java_lang_StackFrameInfo::_mid_offset;
3530 int java_lang_StackFrameInfo::_version_offset;
3531 int java_lang_StackFrameInfo::_cpref_offset;
3532 int java_lang_LiveStackFrameInfo::_monitors_offset;
3533 int java_lang_LiveStackFrameInfo::_locals_offset;
3534 int java_lang_LiveStackFrameInfo::_operands_offset;
3535 int java_lang_AssertionStatusDirectives::classes_offset;
3536 int java_lang_AssertionStatusDirectives::classEnabled_offset;
3537 int java_lang_AssertionStatusDirectives::packages_offset;
3538 int java_lang_AssertionStatusDirectives::packageEnabled_offset;
3539 int java_lang_AssertionStatusDirectives::deflt_offset;
3540 int java_nio_Buffer::_limit_offset;
3541 int java_util_concurrent_locks_AbstractOwnableSynchronizer::_owner_offset = 0;
3542 int sun_reflect_ConstantPool::_oop_offset;
3543 int sun_reflect_UnsafeStaticFieldAccessorImpl::_base_offset;
3544 
3545 
3546 // Support for java_lang_StackTraceElement
3547 
3548 void java_lang_StackTraceElement::set_fileName(oop element, oop value) {
3549   element->obj_field_put(fileName_offset, value);
3550 }
3551 
3552 void java_lang_StackTraceElement::set_declaringClass(oop element, oop value) {
3553   element->obj_field_put(declaringClass_offset, value);
3554 }
3555 
3556 void java_lang_StackTraceElement::set_methodName(oop element, oop value) {
3557   element->obj_field_put(methodName_offset, value);
3558 }
3559 
3560 void java_lang_StackTraceElement::set_lineNumber(oop element, int value) {
3561   element->int_field_put(lineNumber_offset, value);
3562 }
3563 
3564 // Support for java_lang_StackFrameInfo
3565 void java_lang_StackFrameInfo::set_declaringClass(oop element, oop value) {
3566   element->obj_field_put(_declaringClass_offset, value);
3567 }
3568 
3569 void java_lang_StackFrameInfo::set_mid(oop element, short value) {
3570   element->short_field_put(_mid_offset, value);
3571 }
3572 
3573 void java_lang_StackFrameInfo::set_version(oop element, short value) {
3574   element->short_field_put(_version_offset, value);
3575 }
3576 
3577 void java_lang_StackFrameInfo::set_cpref(oop element, short value) {
3578   element->short_field_put(_cpref_offset, value);
3579 }
3580 
3581 void java_lang_StackFrameInfo::set_bci(oop element, int value) {
3582   element->int_field_put(_bci_offset, value);
3583 }
3584 
3585 void java_lang_StackFrameInfo::set_fileName(oop element, oop value) {
3586   element->obj_field_put(_fileName_offset, value);
3587 }
3588 
3589 void java_lang_StackFrameInfo::set_methodName(oop element, oop value) {
3590   element->obj_field_put(_methodName_offset, value);
3591 }
3592 
3593 void java_lang_StackFrameInfo::set_lineNumber(oop element, int value) {
3594   element->int_field_put(_lineNumber_offset, value);
3595 }
3596 
3597 void java_lang_LiveStackFrameInfo::set_monitors(oop element, oop value) {
3598   element->obj_field_put(_monitors_offset, value);
3599 }
3600 
3601 void java_lang_LiveStackFrameInfo::set_locals(oop element, oop value) {
3602   element->obj_field_put(_locals_offset, value);
3603 }
3604 
3605 void java_lang_LiveStackFrameInfo::set_operands(oop element, oop value) {
3606   element->obj_field_put(_operands_offset, value);
3607 }
3608 
3609 // Support for java Assertions - java_lang_AssertionStatusDirectives.
3610 
3611 void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) {
3612   o->obj_field_put(classes_offset, val);
3613 }
3614 
3615 void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) {
3616   o->obj_field_put(classEnabled_offset, val);
3617 }
3618 
3619 void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) {
3620   o->obj_field_put(packages_offset, val);
3621 }
3622 
3623 void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) {
3624   o->obj_field_put(packageEnabled_offset, val);
3625 }
3626 
3627 void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) {
3628   o->bool_field_put(deflt_offset, val);
3629 }
3630 
3631 
3632 // Support for intrinsification of java.nio.Buffer.checkIndex
3633 int java_nio_Buffer::limit_offset() {
3634   return _limit_offset;
3635 }
3636 
3637 
3638 void java_nio_Buffer::compute_offsets() {
3639   Klass* k = SystemDictionary::nio_Buffer_klass();
3640   assert(k != NULL, "must be loaded in 1.4+");
3641   compute_offset(_limit_offset, k, vmSymbols::limit_name(), vmSymbols::int_signature());
3642 }
3643 
3644 void java_util_concurrent_locks_AbstractOwnableSynchronizer::initialize(TRAPS) {
3645   if (_owner_offset != 0) return;
3646 
3647   SystemDictionary::load_abstract_ownable_synchronizer_klass(CHECK);
3648   Klass* k = SystemDictionary::abstract_ownable_synchronizer_klass();
3649   compute_offset(_owner_offset, k,
3650                  vmSymbols::exclusive_owner_thread_name(), vmSymbols::thread_signature());
3651 }
3652 
3653 oop java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(oop obj) {
3654   assert(_owner_offset != 0, "Must be initialized");
3655   return obj->obj_field(_owner_offset);
3656 }
3657 
3658 // Compute hard-coded offsets
3659 // Invoked before SystemDictionary::initialize, so pre-loaded classes
3660 // are not available to determine the offset_of_static_fields.
3661 void JavaClasses::compute_hard_coded_offsets() {
3662   const int x = heapOopSize;
3663   const int header = instanceOopDesc::base_offset_in_bytes();
3664 
3665   // Throwable Class
3666   java_lang_Throwable::backtrace_offset  = java_lang_Throwable::hc_backtrace_offset  * x + header;
3667   java_lang_Throwable::detailMessage_offset = java_lang_Throwable::hc_detailMessage_offset * x + header;
3668   java_lang_Throwable::cause_offset      = java_lang_Throwable::hc_cause_offset      * x + header;
3669   java_lang_Throwable::stackTrace_offset = java_lang_Throwable::hc_stackTrace_offset * x + header;
3670   java_lang_Throwable::static_unassigned_stacktrace_offset = java_lang_Throwable::hc_static_unassigned_stacktrace_offset *  x;
3671 
3672   // java_lang_boxing_object
3673   java_lang_boxing_object::value_offset = java_lang_boxing_object::hc_value_offset + header;
3674   java_lang_boxing_object::long_value_offset = align_size_up((java_lang_boxing_object::hc_value_offset + header), BytesPerLong);
3675 
3676   // java_lang_ref_Reference:
3677   java_lang_ref_Reference::referent_offset = java_lang_ref_Reference::hc_referent_offset * x + header;
3678   java_lang_ref_Reference::queue_offset = java_lang_ref_Reference::hc_queue_offset * x + header;
3679   java_lang_ref_Reference::next_offset  = java_lang_ref_Reference::hc_next_offset * x + header;
3680   java_lang_ref_Reference::discovered_offset  = java_lang_ref_Reference::hc_discovered_offset * x + header;
3681   java_lang_ref_Reference::static_lock_offset = java_lang_ref_Reference::hc_static_lock_offset *  x;
3682   java_lang_ref_Reference::static_pending_offset = java_lang_ref_Reference::hc_static_pending_offset * x;
3683   // Artificial fields for java_lang_ref_Reference
3684   // The first field is for the discovered field added in 1.4
3685   java_lang_ref_Reference::number_of_fake_oop_fields = 1;
3686 
3687   // java_lang_ref_SoftReference Class
3688   java_lang_ref_SoftReference::timestamp_offset = align_size_up((java_lang_ref_SoftReference::hc_timestamp_offset * x + header), BytesPerLong);
3689   // Don't multiply static fields because they are always in wordSize units
3690   java_lang_ref_SoftReference::static_clock_offset = java_lang_ref_SoftReference::hc_static_clock_offset * x;
3691 
3692   // java_lang_ClassLoader
3693   java_lang_ClassLoader::parent_offset = java_lang_ClassLoader::hc_parent_offset * x + header;
3694 
3695   // java_lang_System
3696   java_lang_System::static_in_offset  = java_lang_System::hc_static_in_offset  * x;
3697   java_lang_System::static_out_offset = java_lang_System::hc_static_out_offset * x;
3698   java_lang_System::static_err_offset = java_lang_System::hc_static_err_offset * x;
3699   java_lang_System::static_security_offset = java_lang_System::hc_static_security_offset * x;
3700 
3701   // java_lang_StackTraceElement
3702   java_lang_StackTraceElement::declaringClass_offset = java_lang_StackTraceElement::hc_declaringClass_offset  * x + header;
3703   java_lang_StackTraceElement::methodName_offset = java_lang_StackTraceElement::hc_methodName_offset * x + header;
3704   java_lang_StackTraceElement::fileName_offset   = java_lang_StackTraceElement::hc_fileName_offset   * x + header;
3705   java_lang_StackTraceElement::lineNumber_offset = java_lang_StackTraceElement::hc_lineNumber_offset * x + header;
3706   java_lang_AssertionStatusDirectives::classes_offset = java_lang_AssertionStatusDirectives::hc_classes_offset * x + header;
3707   java_lang_AssertionStatusDirectives::classEnabled_offset = java_lang_AssertionStatusDirectives::hc_classEnabled_offset * x + header;
3708   java_lang_AssertionStatusDirectives::packages_offset = java_lang_AssertionStatusDirectives::hc_packages_offset * x + header;
3709   java_lang_AssertionStatusDirectives::packageEnabled_offset = java_lang_AssertionStatusDirectives::hc_packageEnabled_offset * x + header;
3710   java_lang_AssertionStatusDirectives::deflt_offset = java_lang_AssertionStatusDirectives::hc_deflt_offset * x + header;
3711 
3712 }
3713 
3714 
3715 // Compute non-hard-coded field offsets of all the classes in this file
3716 void JavaClasses::compute_offsets() {
3717   // java_lang_Class::compute_offsets was called earlier in bootstrap
3718   java_lang_ClassLoader::compute_offsets();
3719   java_lang_Thread::compute_offsets();
3720   java_lang_ThreadGroup::compute_offsets();
3721   java_lang_invoke_MethodHandle::compute_offsets();
3722   java_lang_invoke_DirectMethodHandle::compute_offsets();
3723   java_lang_invoke_MemberName::compute_offsets();
3724   java_lang_invoke_LambdaForm::compute_offsets();
3725   java_lang_invoke_MethodType::compute_offsets();
3726   java_lang_invoke_CallSite::compute_offsets();
3727   java_lang_invoke_MethodHandleNatives_CallSiteContext::compute_offsets();
3728   java_security_AccessControlContext::compute_offsets();
3729   // Initialize reflection classes. The layouts of these classes
3730   // changed with the new reflection implementation in JDK 1.4, and
3731   // since the Universe doesn't know what JDK version it is until this
3732   // point we defer computation of these offsets until now.
3733   java_lang_reflect_AccessibleObject::compute_offsets();
3734   java_lang_reflect_Method::compute_offsets();
3735   java_lang_reflect_Constructor::compute_offsets();
3736   java_lang_reflect_Field::compute_offsets();
3737   java_nio_Buffer::compute_offsets();
3738   sun_reflect_ConstantPool::compute_offsets();
3739   sun_reflect_UnsafeStaticFieldAccessorImpl::compute_offsets();
3740   java_lang_reflect_Parameter::compute_offsets();
3741   java_lang_StackFrameInfo::compute_offsets();
3742   java_lang_LiveStackFrameInfo::compute_offsets();
3743 
3744   // generated interpreter code wants to know about the offsets we just computed:
3745   AbstractAssembler::update_delayed_values();
3746 }
3747 
3748 #ifndef PRODUCT
3749 
3750 // These functions exist to assert the validity of hard-coded field offsets to guard
3751 // against changes in the class files
3752 
3753 bool JavaClasses::check_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) {
3754   EXCEPTION_MARK;
3755   fieldDescriptor fd;
3756   TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
3757   Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
3758   instanceKlassHandle h_klass (THREAD, k);
3759   TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
3760   TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
3761   if (!h_klass->find_local_field(f_name, f_sig, &fd)) {
3762     tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
3763     return false;
3764   }
3765   if (fd.is_static()) {
3766     tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name);
3767     return false;
3768   }
3769   if (fd.offset() == hardcoded_offset ) {
3770     return true;
3771   } else {
3772     tty->print_cr("Offset of nonstatic field %s.%s is hardcoded as %d but should really be %d.",
3773                   klass_name, field_name, hardcoded_offset, fd.offset());
3774     return false;
3775   }
3776 }
3777 
3778 
3779 bool JavaClasses::check_static_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) {
3780   EXCEPTION_MARK;
3781   fieldDescriptor fd;
3782   TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
3783   Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
3784   instanceKlassHandle h_klass (THREAD, k);
3785   TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
3786   TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
3787   if (!h_klass->find_local_field(f_name, f_sig, &fd)) {
3788     tty->print_cr("Static field %s.%s not found", klass_name, field_name);
3789     return false;
3790   }
3791   if (!fd.is_static()) {
3792     tty->print_cr("Static field %s.%s appears to be nonstatic", klass_name, field_name);
3793     return false;
3794   }
3795   if (fd.offset() == hardcoded_offset + InstanceMirrorKlass::offset_of_static_fields()) {
3796     return true;
3797   } else {
3798     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());
3799     return false;
3800   }
3801 }
3802 
3803 
3804 bool JavaClasses::check_constant(const char *klass_name, int hardcoded_constant, const char *field_name, const char* field_sig) {
3805   EXCEPTION_MARK;
3806   fieldDescriptor fd;
3807   TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
3808   Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
3809   instanceKlassHandle h_klass (THREAD, k);
3810   TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
3811   TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
3812   if (!h_klass->find_local_field(f_name, f_sig, &fd)) {
3813     tty->print_cr("Static field %s.%s not found", klass_name, field_name);
3814     return false;
3815   }
3816   if (!fd.is_static() || !fd.has_initial_value()) {
3817     tty->print_cr("Static field %s.%s appears to be non-constant", klass_name, field_name);
3818     return false;
3819   }
3820   if (!fd.initial_value_tag().is_int()) {
3821     tty->print_cr("Static field %s.%s is not an int", klass_name, field_name);
3822     return false;
3823   }
3824   jint field_value = fd.int_initial_value();
3825   if (field_value == hardcoded_constant) {
3826     return true;
3827   } else {
3828     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);
3829     return false;
3830   }
3831 }
3832 
3833 
3834 // Check the hard-coded field offsets of all the classes in this file
3835 
3836 void JavaClasses::check_offsets() {
3837   bool valid = true;
3838   HandleMark hm;
3839 
3840 #define CHECK_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
3841   valid &= check_offset(klass_name, cpp_klass_name :: field_name ## _offset, #field_name, field_sig)
3842 
3843 #define CHECK_LONG_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
3844   valid &= check_offset(klass_name, cpp_klass_name :: long_ ## field_name ## _offset, #field_name, field_sig)
3845 
3846 #define CHECK_STATIC_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
3847   valid &= check_static_offset(klass_name, cpp_klass_name :: static_ ## field_name ## _offset, #field_name, field_sig)
3848 
3849 #define CHECK_CONSTANT(klass_name, cpp_klass_name, field_name, field_sig) \
3850   valid &= check_constant(klass_name, cpp_klass_name :: field_name, #field_name, field_sig)
3851 
3852   // java.lang.String
3853 
3854   CHECK_OFFSET("java/lang/String", java_lang_String, value, "[B");
3855   if (java_lang_String::has_hash_field()) {
3856     CHECK_OFFSET("java/lang/String", java_lang_String, hash, "I");
3857   }
3858   if (java_lang_String::has_coder_field()) {
3859     CHECK_OFFSET("java/lang/String", java_lang_String, coder, "B");
3860   }
3861 
3862   // java.lang.Class
3863 
3864   // Fake fields
3865   // CHECK_OFFSET("java/lang/Class", java_lang_Class, klass); // %%% this needs to be checked
3866   // CHECK_OFFSET("java/lang/Class", java_lang_Class, array_klass); // %%% this needs to be checked
3867 
3868   // java.lang.Throwable
3869 
3870   CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, backtrace, "Ljava/lang/Object;");
3871   CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, detailMessage, "Ljava/lang/String;");
3872   CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, cause, "Ljava/lang/Throwable;");
3873   CHECK_OFFSET("java/lang/Throwable", java_lang_Throwable, stackTrace, "[Ljava/lang/StackTraceElement;");
3874 
3875   // Boxed primitive objects (java_lang_boxing_object)
3876 
3877   CHECK_OFFSET("java/lang/Boolean",   java_lang_boxing_object, value, "Z");
3878   CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C");
3879   CHECK_OFFSET("java/lang/Float",     java_lang_boxing_object, value, "F");
3880   CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D");
3881   CHECK_OFFSET("java/lang/Byte",      java_lang_boxing_object, value, "B");
3882   CHECK_OFFSET("java/lang/Short",     java_lang_boxing_object, value, "S");
3883   CHECK_OFFSET("java/lang/Integer",   java_lang_boxing_object, value, "I");
3884   CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J");
3885 
3886   // java.lang.ClassLoader
3887 
3888   CHECK_OFFSET("java/lang/ClassLoader", java_lang_ClassLoader, parent,      "Ljava/lang/ClassLoader;");
3889 
3890   // java.lang.System
3891 
3892   CHECK_STATIC_OFFSET("java/lang/System", java_lang_System,  in, "Ljava/io/InputStream;");
3893   CHECK_STATIC_OFFSET("java/lang/System", java_lang_System, out, "Ljava/io/PrintStream;");
3894   CHECK_STATIC_OFFSET("java/lang/System", java_lang_System, err, "Ljava/io/PrintStream;");
3895   CHECK_STATIC_OFFSET("java/lang/System", java_lang_System, security, "Ljava/lang/SecurityManager;");
3896 
3897   // java.lang.StackTraceElement
3898 
3899   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, declaringClass, "Ljava/lang/String;");
3900   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, methodName, "Ljava/lang/String;");
3901   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement,   fileName, "Ljava/lang/String;");
3902   CHECK_OFFSET("java/lang/StackTraceElement", java_lang_StackTraceElement, lineNumber, "I");
3903 
3904   // java.lang.ref.Reference
3905 
3906   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, referent, "Ljava/lang/Object;");
3907   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, queue, "Ljava/lang/ref/ReferenceQueue;");
3908   CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, next, "Ljava/lang/ref/Reference;");
3909   // Fake field
3910   //CHECK_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, discovered, "Ljava/lang/ref/Reference;");
3911   CHECK_STATIC_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, lock, "Ljava/lang/ref/Reference$Lock;");
3912   CHECK_STATIC_OFFSET("java/lang/ref/Reference", java_lang_ref_Reference, pending, "Ljava/lang/ref/Reference;");
3913 
3914   // java.lang.ref.SoftReference
3915 
3916   CHECK_OFFSET("java/lang/ref/SoftReference", java_lang_ref_SoftReference, timestamp, "J");
3917   CHECK_STATIC_OFFSET("java/lang/ref/SoftReference", java_lang_ref_SoftReference, clock, "J");
3918 
3919   // java.lang.AssertionStatusDirectives
3920   //
3921   // The CheckAssertionStatusDirectives boolean can be removed from here and
3922   // globals.hpp after the AssertionStatusDirectives class has been integrated
3923   // into merlin "for some time."  Without it, the vm will fail with early
3924   // merlin builds.
3925 
3926   if (CheckAssertionStatusDirectives) {
3927     const char* nm = "java/lang/AssertionStatusDirectives";
3928     const char* sig = "[Ljava/lang/String;";
3929     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, classes, sig);
3930     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, classEnabled, "[Z");
3931     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, packages, sig);
3932     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, packageEnabled, "[Z");
3933     CHECK_OFFSET(nm, java_lang_AssertionStatusDirectives, deflt, "Z");
3934   }
3935 
3936   if (!valid) vm_exit_during_initialization("Hard-coded field offset verification failed");
3937 }
3938 
3939 #endif // PRODUCT
3940 
3941 int InjectedField::compute_offset() {
3942   InstanceKlass* ik = InstanceKlass::cast(klass());
3943   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
3944     if (!may_be_java && !fs.access_flags().is_internal()) {
3945       // Only look at injected fields
3946       continue;
3947     }
3948     if (fs.name() == name() && fs.signature() == signature()) {
3949       return fs.offset();
3950     }
3951   }
3952   ResourceMark rm;
3953   tty->print_cr("Invalid layout of %s at %s/%s%s", ik->external_name(), name()->as_C_string(), signature()->as_C_string(), may_be_java ? " (may_be_java)" : "");
3954 #ifndef PRODUCT
3955   ik->print();
3956   tty->print_cr("all fields:");
3957   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
3958     tty->print_cr("  name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int());
3959   }
3960 #endif //PRODUCT
3961   vm_exit_during_initialization("Invalid layout of preloaded class: use -XX:+TraceClassLoading to see the origin of the problem class");
3962   return -1;
3963 }
3964 
3965 void javaClasses_init() {
3966   JavaClasses::compute_offsets();
3967   JavaClasses::check_offsets();
3968   FilteredFieldsMap::initialize();  // must be done after computing offsets.
3969 }