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