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