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