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