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