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