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