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