< prev index next >

src/hotspot/share/classfile/javaClasses.cpp

Print this page




  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/altHashing.hpp"
  27 #include "classfile/classLoaderData.inline.hpp"
  28 #include "classfile/javaClasses.inline.hpp"
  29 #include "classfile/moduleEntry.hpp"
  30 #include "classfile/stringTable.hpp"
  31 #include "classfile/symbolTable.hpp"
  32 #include "classfile/vmSymbols.hpp"
  33 #include "code/debugInfo.hpp"
  34 #include "code/dependencyContext.hpp"
  35 #include "code/pcDesc.hpp"
  36 #include "interpreter/interpreter.hpp"
  37 #include "interpreter/linkResolver.hpp"
  38 #include "logging/log.hpp"
  39 #include "logging/logStream.hpp"
  40 #include "memory/heapShared.inline.hpp"
  41 #include "memory/metaspaceShared.hpp"
  42 #include "memory/oopFactory.hpp"
  43 #include "memory/resourceArea.hpp"
  44 #include "memory/universe.hpp"
  45 #include "oops/fieldStreams.hpp"
  46 #include "oops/instanceKlass.hpp"
  47 #include "oops/instanceMirrorKlass.hpp"
  48 #include "oops/klass.hpp"
  49 #include "oops/method.inline.hpp"
  50 #include "oops/objArrayOop.inline.hpp"
  51 #include "oops/oop.inline.hpp"


 144     vm_exit_during_initialization("Invalid layout of well-known class: use -Xlog:class+load=info to see the origin of the problem class");
 145   }
 146   dest_offset = fd.offset();
 147 }
 148 
 149 // Overloading to pass name as a string.
 150 static void compute_offset(int& dest_offset, InstanceKlass* ik,
 151                            const char* name_string, Symbol* signature_symbol,
 152                            bool is_static = false) {
 153   TempNewSymbol name = SymbolTable::probe(name_string, (int)strlen(name_string));
 154   if (name == NULL) {
 155     ResourceMark rm;
 156     log_error(class)("Name %s should be in the SymbolTable since its class is loaded", name_string);
 157     vm_exit_during_initialization("Invalid layout of well-known class", ik->external_name());
 158   }
 159   compute_offset(dest_offset, ik, name, signature_symbol, is_static);
 160 }
 161 
 162 int java_lang_String::value_offset  = 0;
 163 int java_lang_String::hash_offset   = 0;
 164 int java_lang_String::hashIsZero_offset = 0;
 165 int java_lang_String::coder_offset  = 0;
 166 
 167 bool java_lang_String::initialized  = false;
 168 
 169 bool java_lang_String::is_instance(oop obj) {
 170   return is_instance_inlined(obj);
 171 }
 172 
 173 #if INCLUDE_CDS
 174 #define FIELD_SERIALIZE_OFFSET(offset, klass, name, signature, is_static) \
 175   f->do_u4((u4*)&offset)
 176 #endif
 177 
 178 #define FIELD_COMPUTE_OFFSET(offset, klass, name, signature, is_static) \
 179   compute_offset(offset, klass, name, vmSymbols::signature(), is_static)
 180 
 181 #define STRING_FIELDS_DO(macro) \
 182   macro(value_offset, k, vmSymbols::value_name(), byte_array_signature, false); \
 183   macro(hash_offset,  k, "hash",                  int_signature,        false); \
 184   macro(hashIsZero_offset, k, "hashIsZero",       bool_signature,       false); \
 185   macro(coder_offset, k, "coder",                 byte_signature,       false);
 186 
 187 void java_lang_String::compute_offsets() {
 188   if (initialized) {
 189     return;
 190   }
 191 
 192   InstanceKlass* k = SystemDictionary::String_klass();
 193   STRING_FIELDS_DO(FIELD_COMPUTE_OFFSET);
 194 
 195   initialized = true;
 196 }
 197 
 198 #if INCLUDE_CDS
 199 void java_lang_String::serialize_offsets(SerializeClosure* f) {
 200   STRING_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
 201   f->do_u4((u4*)&initialized);
 202 }
 203 #endif
 204 
 205 class CompactStringsFixup : public FieldClosure {
 206 private:
 207   bool _value;
 208 
 209 public:
 210   CompactStringsFixup(bool value) : _value(value) {}
 211 
 212   void do_field(fieldDescriptor* fd) {
 213     if (fd->name() == vmSymbols::compact_strings_name()) {
 214       oop mirror = fd->field_holder()->java_mirror();
 215       assert(fd->field_holder() == SystemDictionary::String_klass(), "Should be String");
 216       assert(mirror != NULL, "String must have mirror already");
 217       mirror->bool_field_put(fd->offset(), _value);
 218     }
 219   }
 220 };
 221 
 222 void java_lang_String::set_compact_strings(bool value) {
 223   CompactStringsFixup fix(value);
 224   SystemDictionary::String_klass()->do_local_static_fields(&fix);
 225 }
 226 
 227 Handle java_lang_String::basic_create(int length, bool is_latin1, TRAPS) {
 228   assert(initialized, "Must be initialized");
 229   assert(CompactStrings || !is_latin1, "Must be UTF16 without CompactStrings");
 230 
 231   // Create the String object first, so there's a chance that the String
 232   // and the char array it points to end up in the same cache line.
 233   oop obj;
 234   obj = SystemDictionary::String_klass()->allocate_instance(CHECK_NH);
 235 
 236   // Create the char array.  The String object must be handlized here
 237   // because GC can happen as a result of the allocation attempt.
 238   Handle h_obj(THREAD, obj);
 239   int arr_length = is_latin1 ? length : length << 1; // 2 bytes per UTF16.
 240   typeArrayOop buffer = oopFactory::new_byteArray(arr_length, CHECK_NH);;
 241 
 242   // Point the String at the char array
 243   obj = h_obj();
 244   set_value(obj, buffer);


 493   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 494 
 495   jchar* result = NEW_RESOURCE_ARRAY_RETURN_NULL(jchar, length);
 496   if (result != NULL) {
 497     if (!is_latin1) {
 498       for (int index = 0; index < length; index++) {
 499         result[index] = value->char_at(index);
 500       }
 501     } else {
 502       for (int index = 0; index < length; index++) {
 503         result[index] = ((jchar) value->byte_at(index)) & 0xff;
 504       }
 505     }
 506   } else {
 507     THROW_MSG_0(vmSymbols::java_lang_OutOfMemoryError(), "could not allocate Unicode string");
 508   }
 509   return result;
 510 }
 511 
 512 unsigned int java_lang_String::hash_code(oop java_string) {
 513   // The hash and hashIsZero fields are subject to a benign data race,
 514   // making it crucial to ensure that any observable result of the
 515   // calculation in this method stays correct under any possible read of
 516   // these fields. Necessary restrictions to allow this to be correct
 517   // without explicit memory fences or similar concurrency primitives is
 518   // that we can ever only write to one of these two fields for a given
 519   // String instance, and that the computation is idempotent and derived
 520   // from immutable state
 521   assert(initialized && (hash_offset > 0) && (hashIsZero_offset > 0), "Must be initialized");
 522   if (java_lang_String::hash_is_set(java_string)) {
 523     return java_string->int_field(hash_offset);
 524   }
 525 
 526   typeArrayOop value = java_lang_String::value(java_string);
 527   int         length = java_lang_String::length(java_string, value);
 528   bool     is_latin1 = java_lang_String::is_latin1(java_string);
 529 
 530   unsigned int hash = 0;
 531   if (length > 0) {
 532     if (is_latin1) {
 533       hash = java_lang_String::hash_code(value->byte_at_addr(0), length);
 534     } else {
 535       hash = java_lang_String::hash_code(value->char_at_addr(0), length);
 536     }
 537   }
 538 
 539   if (hash != 0) {
 540     java_string->int_field_put(hash_offset, hash);
 541   } else {
 542     java_string->bool_field_put(hashIsZero_offset, true);
 543   }
 544   return hash;
 545 }
 546 
 547 char* java_lang_String::as_quoted_ascii(oop java_string) {
 548   typeArrayOop value  = java_lang_String::value(java_string);
 549   int          length = java_lang_String::length(java_string, value);
 550   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 551 
 552   if (length == 0) return NULL;
 553 
 554   char* result;
 555   int result_length;
 556   if (!is_latin1) {
 557     jchar* base = value->char_at_addr(0);
 558     result_length = UNICODE::quoted_ascii_length(base, length) + 1;
 559     result = NEW_RESOURCE_ARRAY(char, result_length);
 560     UNICODE::as_quoted_ascii(base, length, result, result_length);
 561   } else {
 562     jbyte* base = value->byte_at_addr(0);
 563     result_length = UNICODE::quoted_ascii_length(base, length) + 1;
 564     result = NEW_RESOURCE_ARRAY(char, result_length);
 565     UNICODE::as_quoted_ascii(base, length, result, result_length);
 566   }
 567   assert(result_length >= length + 1, "must not be shorter");
 568   assert(result_length == (int)strlen(result) + 1, "must match");
 569   return result;
 570 }
 571 
 572 Symbol* java_lang_String::as_symbol(oop java_string) {
 573   typeArrayOop value  = java_lang_String::value(java_string);
 574   int          length = java_lang_String::length(java_string, value);
 575   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 576   if (!is_latin1) {
 577     jchar* base = (length == 0) ? NULL : value->char_at_addr(0);
 578     Symbol* sym = SymbolTable::new_symbol(base, length);
 579     return sym;
 580   } else {
 581     ResourceMark rm;
 582     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 583     const char* base = UNICODE::as_utf8(position, length);
 584     Symbol* sym = SymbolTable::new_symbol(base, length);
 585     return sym;
 586   }
 587 }
 588 
 589 Symbol* java_lang_String::as_symbol_or_null(oop java_string) {
 590   typeArrayOop value  = java_lang_String::value(java_string);
 591   int          length = java_lang_String::length(java_string, value);
 592   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 593   if (!is_latin1) {
 594     jchar* base = (length == 0) ? NULL : value->char_at_addr(0);
 595     return SymbolTable::probe_unicode(base, length);
 596   } else {
 597     ResourceMark rm;
 598     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 599     const char* base = UNICODE::as_utf8(position, length);
 600     return SymbolTable::probe(base, length);
 601   }
 602 }
 603 
 604 int java_lang_String::utf8_length(oop java_string, typeArrayOop value) {


1426 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1427   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1428   Symbol* name = NULL;
1429   bool is_instance = false;
1430   if (is_primitive(java_class)) {
1431     name = vmSymbols::type_signature(primitive_type(java_class));
1432   } else {
1433     Klass* k = as_Klass(java_class);
1434     is_instance = k->is_instance_klass();
1435     name = k->name();
1436   }
1437   if (name == NULL) {
1438     st->print("<null>");
1439     return;
1440   }
1441   if (is_instance)  st->print("L");
1442   st->write((char*) name->base(), (int) name->utf8_length());
1443   if (is_instance)  st->print(";");
1444 }
1445 
1446 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found) {
1447   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1448   Symbol* name;
1449   if (is_primitive(java_class)) {
1450     name = vmSymbols::type_signature(primitive_type(java_class));
1451     // Because this can create a new symbol, the caller has to decrement
1452     // the refcount, so make adjustment here and below for symbols returned
1453     // that are not created or incremented due to a successful lookup.
1454     name->increment_refcount();
1455   } else {
1456     Klass* k = as_Klass(java_class);
1457     if (!k->is_instance_klass()) {
1458       name = k->name();
1459       name->increment_refcount();
1460     } else {
1461       ResourceMark rm;
1462       const char* sigstr = k->signature_name();
1463       int         siglen = (int) strlen(sigstr);
1464       if (!intern_if_not_found) {
1465         name = SymbolTable::probe(sigstr, siglen);
1466       } else {
1467         name = SymbolTable::new_symbol(sigstr, siglen);
1468       }
1469     }
1470   }
1471   return name;
1472 }
1473 
1474 // Returns the Java name for this Java mirror (Resource allocated)
1475 // See Klass::external_name().
1476 // For primitive type Java mirrors, its type name is returned.
1477 const char* java_lang_Class::as_external_name(oop java_class) {
1478   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1479   const char* name = NULL;
1480   if (is_primitive(java_class)) {
1481     name = type2name(primitive_type(java_class));
1482   } else {
1483     name = as_Klass(java_class)->external_name();
1484   }
1485   if (name == NULL) {
1486     name = "<null>";
1487   }


1597 // Note: JDK1.1 and before had a privateInfo_offset field which was used for the
1598 //       platform thread structure, and a eetop offset which was used for thread
1599 //       local storage (and unused by the HotSpot VM). In JDK1.2 the two structures
1600 //       merged, so in the HotSpot VM we just use the eetop field for the thread
1601 //       instead of the privateInfo_offset.
1602 //
1603 // Note: The stackSize field is only present starting in 1.4.
1604 
1605 int java_lang_Thread::_name_offset = 0;
1606 int java_lang_Thread::_group_offset = 0;
1607 int java_lang_Thread::_contextClassLoader_offset = 0;
1608 int java_lang_Thread::_inheritedAccessControlContext_offset = 0;
1609 int java_lang_Thread::_priority_offset = 0;
1610 int java_lang_Thread::_eetop_offset = 0;
1611 int java_lang_Thread::_daemon_offset = 0;
1612 int java_lang_Thread::_stillborn_offset = 0;
1613 int java_lang_Thread::_stackSize_offset = 0;
1614 int java_lang_Thread::_tid_offset = 0;
1615 int java_lang_Thread::_thread_status_offset = 0;
1616 int java_lang_Thread::_park_blocker_offset = 0;

1617 
1618 #define THREAD_FIELDS_DO(macro) \
1619   macro(_name_offset,          k, vmSymbols::name_name(), string_signature, false); \
1620   macro(_group_offset,         k, vmSymbols::group_name(), threadgroup_signature, false); \
1621   macro(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), classloader_signature, false); \
1622   macro(_inheritedAccessControlContext_offset, k, vmSymbols::inheritedAccessControlContext_name(), accesscontrolcontext_signature, false); \
1623   macro(_priority_offset,      k, vmSymbols::priority_name(), int_signature, false); \
1624   macro(_daemon_offset,        k, vmSymbols::daemon_name(), bool_signature, false); \
1625   macro(_eetop_offset,         k, "eetop", long_signature, false); \
1626   macro(_stillborn_offset,     k, "stillborn", bool_signature, false); \
1627   macro(_stackSize_offset,     k, "stackSize", long_signature, false); \
1628   macro(_tid_offset,           k, "tid", long_signature, false); \
1629   macro(_thread_status_offset, k, "threadStatus", int_signature, false); \
1630   macro(_park_blocker_offset,  k, "parkBlocker", object_signature, false)

1631 
1632 void java_lang_Thread::compute_offsets() {
1633   assert(_group_offset == 0, "offsets should be initialized only once");
1634 
1635   InstanceKlass* k = SystemDictionary::Thread_klass();
1636   THREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1637 }
1638 
1639 #if INCLUDE_CDS
1640 void java_lang_Thread::serialize_offsets(SerializeClosure* f) {
1641   THREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1642 }
1643 #endif
1644 
1645 JavaThread* java_lang_Thread::thread(oop java_thread) {
1646   return (JavaThread*)java_thread->address_field(_eetop_offset);
1647 }
1648 
1649 
1650 void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) {


1696 
1697 bool java_lang_Thread::is_daemon(oop java_thread) {
1698   return java_thread->bool_field(_daemon_offset) != 0;
1699 }
1700 
1701 
1702 void java_lang_Thread::set_daemon(oop java_thread) {
1703   java_thread->bool_field_put(_daemon_offset, true);
1704 }
1705 
1706 oop java_lang_Thread::context_class_loader(oop java_thread) {
1707   return java_thread->obj_field(_contextClassLoader_offset);
1708 }
1709 
1710 oop java_lang_Thread::inherited_access_control_context(oop java_thread) {
1711   return java_thread->obj_field(_inheritedAccessControlContext_offset);
1712 }
1713 
1714 
1715 jlong java_lang_Thread::stackSize(oop java_thread) {
1716   return java_thread->long_field(_stackSize_offset);




1717 }
1718 
1719 // Write the thread status value to threadStatus field in java.lang.Thread java class.
1720 void java_lang_Thread::set_thread_status(oop java_thread,
1721                                          java_lang_Thread::ThreadStatus status) {
1722   java_thread->int_field_put(_thread_status_offset, status);



1723 }
1724 
1725 // Read thread status value from threadStatus field in java.lang.Thread java class.
1726 java_lang_Thread::ThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
1727   // Make sure the caller is operating on behalf of the VM or is
1728   // running VM code (state == _thread_in_vm).
1729   assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
1730          JavaThread::current()->thread_state() == _thread_in_vm,
1731          "Java Thread is not running in vm");
1732   return (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);














1733 }
1734 
1735 
1736 jlong java_lang_Thread::thread_id(oop java_thread) {
1737   return java_thread->long_field(_tid_offset);





1738 }
1739 
1740 oop java_lang_Thread::park_blocker(oop java_thread) {
1741   return java_thread->obj_field(_park_blocker_offset);






















1742 }
1743 

1744 const char* java_lang_Thread::thread_status_name(oop java_thread) {

1745   ThreadStatus status = (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
1746   switch (status) {
1747     case NEW                      : return "NEW";
1748     case RUNNABLE                 : return "RUNNABLE";
1749     case SLEEPING                 : return "TIMED_WAITING (sleeping)";
1750     case IN_OBJECT_WAIT           : return "WAITING (on object monitor)";
1751     case IN_OBJECT_WAIT_TIMED     : return "TIMED_WAITING (on object monitor)";
1752     case PARKED                   : return "WAITING (parking)";
1753     case PARKED_TIMED             : return "TIMED_WAITING (parking)";
1754     case BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
1755     case TERMINATED               : return "TERMINATED";
1756     default                       : return "UNKNOWN";
1757   };
1758 }
1759 int java_lang_ThreadGroup::_parent_offset = 0;
1760 int java_lang_ThreadGroup::_name_offset = 0;
1761 int java_lang_ThreadGroup::_threads_offset = 0;
1762 int java_lang_ThreadGroup::_groups_offset = 0;
1763 int java_lang_ThreadGroup::_maxPriority_offset = 0;
1764 int java_lang_ThreadGroup::_destroyed_offset = 0;


1878 }
1879 
1880 int java_lang_Throwable::depth(oop throwable) {
1881   return throwable->int_field(depth_offset);
1882 }
1883 
1884 void java_lang_Throwable::set_depth(oop throwable, int value) {
1885   throwable->int_field_put(depth_offset, value);
1886 }
1887 
1888 oop java_lang_Throwable::message(oop throwable) {
1889   return throwable->obj_field(detailMessage_offset);
1890 }
1891 
1892 
1893 // Return Symbol for detailed_message or NULL
1894 Symbol* java_lang_Throwable::detail_message(oop throwable) {
1895   PRESERVE_EXCEPTION_MARK;  // Keep original exception
1896   oop detailed_message = java_lang_Throwable::message(throwable);
1897   if (detailed_message != NULL) {
1898     return java_lang_String::as_symbol(detailed_message);
1899   }
1900   return NULL;
1901 }
1902 
1903 void java_lang_Throwable::set_message(oop throwable, oop value) {
1904   throwable->obj_field_put(detailMessage_offset, value);
1905 }
1906 
1907 
1908 void java_lang_Throwable::set_stacktrace(oop throwable, oop st_element_array) {
1909   throwable->obj_field_put(stackTrace_offset, st_element_array);
1910 }
1911 
1912 void java_lang_Throwable::clear_stacktrace(oop throwable) {
1913   set_stacktrace(throwable, NULL);
1914 }
1915 
1916 
1917 void java_lang_Throwable::print(oop throwable, outputStream* st) {
1918   ResourceMark rm;


2588         java_lang_Class::set_source_file(java_class(), source_file);
2589       }
2590     } else {
2591       // Class was redefined. Dump the cache if it was set.
2592       if (source_file != NULL) {
2593         source_file = NULL;
2594         java_lang_Class::set_source_file(java_class(), source_file);
2595       }
2596       if (ShowHiddenFrames) {
2597         source = vmSymbols::unknown_class_name();
2598         source_file = StringTable::intern(source, CHECK);
2599       }
2600     }
2601     java_lang_StackTraceElement::set_fileName(element(), source_file);
2602 
2603     int line_number = Backtrace::get_line_number(method, bci);
2604     java_lang_StackTraceElement::set_lineNumber(element(), line_number);
2605   }
2606 }
2607 
2608 #if INCLUDE_JVMCI
2609 void java_lang_StackTraceElement::decode(Handle mirror, methodHandle method, int bci, Symbol*& methodname, Symbol*& filename, int& line_number) {
2610   int method_id = method->orig_method_idnum();
2611   int cpref = method->name_index();
2612   decode(mirror, method_id, method->constants()->version(), bci, cpref, methodname, filename, line_number);
2613 }
2614 
2615 void java_lang_StackTraceElement::decode(Handle mirror, int method_id, int version, int bci, int cpref, Symbol*& methodname, Symbol*& filename, int& line_number) {
2616   // Fill in class name
2617   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror()));
2618   Method* method = holder->method_with_orig_idnum(method_id, version);
2619 
2620   // The method can be NULL if the requested class version is gone
2621   Symbol* sym = (method != NULL) ? method->name() : holder->constants()->symbol_at(cpref);
2622 
2623   // Fill in method name
2624   methodname = sym;
2625 
2626   if (!version_matches(method, version)) {
2627     // If the method was redefined, accurate line number information isn't available
2628     filename = NULL;
2629     line_number = -1;
2630   } else {
2631     // Fill in source file name and line number.
2632     // Use a specific ik version as a holder since the mirror might
2633     // refer to a version that is now obsolete and no longer accessible
2634     // via the previous versions list.
2635     holder = holder->get_klass_version(version);
2636     assert(holder != NULL, "sanity check");
2637     Symbol* source = holder->source_file_name();
2638     if (ShowHiddenFrames && source == NULL) {
2639       source = vmSymbols::unknown_class_name();
2640     }
2641     filename = source;
2642     line_number = Backtrace::get_line_number(method, bci);
2643   }
2644 }
2645 #endif // INCLUDE_JVMCI
2646 
2647 Method* java_lang_StackFrameInfo::get_method(Handle stackFrame, InstanceKlass* holder, TRAPS) {
2648   HandleMark hm(THREAD);
2649   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
2650   Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mname());
2651   // we should expand MemberName::name when Throwable uses StackTrace
2652   // MethodHandles::expand_MemberName(mname, MethodHandles::_suppress_defc|MethodHandles::_suppress_type, CHECK_NULL);
2653   return method;
2654 }
2655 
2656 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci, TRAPS) {
2657   // set Method* or mid/cpref
2658   HandleMark hm(THREAD);
2659   Handle mname(Thread::current(), stackFrame->obj_field(_memberName_offset));
2660   InstanceKlass* ik = method->method_holder();
2661   CallInfo info(method(), ik, CHECK);
2662   MethodHandles::init_method_MemberName(mname, info);
2663   // set bci
2664   java_lang_StackFrameInfo::set_bci(stackFrame(), bci);
2665   // method may be redefined; store the version
2666   int version = method->constants()->version();


3606 
3607 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
3608   assert(is_instance(mname), "wrong type");
3609   mname->address_field_put(_vmindex_offset, (address) index);
3610 }
3611 
3612 
3613 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) {
3614   assert(is_instance(resolved_method), "wrong type");
3615   Method* m = (Method*)resolved_method->address_field(_vmtarget_offset);
3616   assert(m->is_method(), "must be");
3617   return m;
3618 }
3619 
3620 // Used by redefinition to change Method* to new Method* with same hash (name, signature)
3621 void java_lang_invoke_ResolvedMethodName::set_vmtarget(oop resolved_method, Method* m) {
3622   assert(is_instance(resolved_method), "wrong type");
3623   resolved_method->address_field_put(_vmtarget_offset, (address)m);
3624 }
3625 
3626 void java_lang_invoke_ResolvedMethodName::set_vmholder(oop resolved_method, oop holder) {
3627   assert(is_instance(resolved_method), "wrong type");
3628   resolved_method->obj_field_put(_vmholder_offset, holder);
3629 }
3630 
3631 oop java_lang_invoke_ResolvedMethodName::find_resolved_method(const methodHandle& m, TRAPS) {
3632   const Method* method = m();
3633 
3634   // lookup ResolvedMethod oop in the table, or create a new one and intern it
3635   oop resolved_method = ResolvedMethodTable::find_method(method);
3636   if (resolved_method != NULL) {
3637     return resolved_method;










3638   }
3639 
3640   InstanceKlass* k = SystemDictionary::ResolvedMethodName_klass();
3641   if (!k->is_initialized()) {
3642     k->initialize(CHECK_NULL);
3643   }
3644 
3645   oop new_resolved_method = k->allocate_instance(CHECK_NULL);
3646 
3647   NoSafepointVerifier nsv;
3648 
3649   if (method->is_old()) {
3650     method = (method->is_deleted()) ? Universe::throw_no_such_method_error() :
3651                                       method->get_new_method();
3652   }
3653 
3654   InstanceKlass* holder = method->method_holder();
3655 
3656   set_vmtarget(new_resolved_method, const_cast<Method*>(method));
3657   // Add a reference to the loader (actually mirror because unsafe anonymous classes will not have
3658   // distinct loaders) to ensure the metadata is kept alive.
3659   // This mirror may be different than the one in clazz field.
3660   set_vmholder(new_resolved_method, holder->java_mirror());
3661 
3662   // Set flag in class to indicate this InstanceKlass has entries in the table
3663   // to avoid walking table during redefinition if none of the redefined classes
3664   // have any membernames in the table.
3665   holder->set_has_resolved_methods();
3666 
3667   return ResolvedMethodTable::add_method(method, Handle(THREAD, new_resolved_method));
3668 }
3669 
3670 oop java_lang_invoke_LambdaForm::vmentry(oop lform) {
3671   assert(is_instance(lform), "wrong type");
3672   return lform->obj_field(_vmentry_offset);
3673 }
3674 
3675 
3676 // Support for java_lang_invoke_MethodType
3677 
3678 int java_lang_invoke_MethodType::_rtype_offset;
3679 int java_lang_invoke_MethodType::_ptypes_offset;
3680 
3681 #define METHODTYPE_FIELDS_DO(macro) \
3682   macro(_rtype_offset,  k, "rtype",  class_signature,       false); \
3683   macro(_ptypes_offset, k, "ptypes", class_array_signature, false)
3684 
3685 void java_lang_invoke_MethodType::compute_offsets() {
3686   InstanceKlass* k = SystemDictionary::MethodType_klass();
3687   METHODTYPE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3688 }
3689 
3690 #if INCLUDE_CDS
3691 void java_lang_invoke_MethodType::serialize_offsets(SerializeClosure* f) {
3692   METHODTYPE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3693 }
3694 #endif
3695 
3696 void java_lang_invoke_MethodType::print_signature(oop mt, outputStream* st) {
3697   st->print("(");
3698   objArrayOop pts = ptypes(mt);
3699   for (int i = 0, limit = pts->length(); i < limit; i++) {
3700     java_lang_Class::print_signature(pts->obj_at(i), st);
3701   }
3702   st->print(")");
3703   java_lang_Class::print_signature(rtype(mt), st);
3704 }
3705 
3706 Symbol* java_lang_invoke_MethodType::as_signature(oop mt, bool intern_if_not_found) {
3707   ResourceMark rm;
3708   stringStream buffer(128);
3709   print_signature(mt, &buffer);
3710   const char* sigstr =       buffer.base();
3711   int         siglen = (int) buffer.size();
3712   Symbol *name;
3713   if (!intern_if_not_found) {
3714     name = SymbolTable::probe(sigstr, siglen);
3715   } else {
3716     name = SymbolTable::new_symbol(sigstr, siglen);
3717   }
3718   return name;
3719 }
3720 
3721 bool java_lang_invoke_MethodType::equals(oop mt1, oop mt2) {
3722   if (oopDesc::equals(mt1, mt2))
3723     return true;
3724   if (!oopDesc::equals(rtype(mt1), rtype(mt2)))
3725     return false;
3726   if (ptype_count(mt1) != ptype_count(mt2))
3727     return false;
3728   for (int i = ptype_count(mt1) - 1; i >= 0; i--) {
3729     if (!oopDesc::equals(ptype(mt1, i), ptype(mt2, i)))
3730       return false;
3731   }
3732   return true;
3733 }
3734 
3735 oop java_lang_invoke_MethodType::rtype(oop mt) {
3736   assert(is_instance(mt), "must be a MethodType");


4009   macro(static_in_offset,  k, "in",  input_stream_signature, true); \
4010   macro(static_out_offset, k, "out", print_stream_signature, true); \
4011   macro(static_err_offset, k, "err", print_stream_signature, true); \
4012   macro(static_security_offset, k, "security", security_manager_signature, true)
4013 
4014 void java_lang_System::compute_offsets() {
4015   InstanceKlass* k = SystemDictionary::System_klass();
4016   SYSTEM_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4017 }
4018 
4019 #if INCLUDE_CDS
4020 void java_lang_System::serialize_offsets(SerializeClosure* f) {
4021    SYSTEM_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4022 }
4023 #endif
4024 
4025 int java_lang_System::in_offset_in_bytes() { return static_in_offset; }
4026 int java_lang_System::out_offset_in_bytes() { return static_out_offset; }
4027 int java_lang_System::err_offset_in_bytes() { return static_err_offset; }
4028 
4029 // Support for jdk_internal_misc_UnsafeConstants
4030 //
4031 class UnsafeConstantsFixup : public FieldClosure {
4032 private:
4033   int _address_size;
4034   int _page_size;
4035   bool _big_endian;
4036   bool _use_unaligned_access;
4037 public:
4038   UnsafeConstantsFixup() {
4039     // round up values for all static final fields
4040     _address_size = sizeof(void*);
4041     _page_size = os::vm_page_size();
4042     _big_endian = LITTLE_ENDIAN_ONLY(false) BIG_ENDIAN_ONLY(true);
4043     _use_unaligned_access = UseUnalignedAccesses;
4044   }
4045 
4046   void do_field(fieldDescriptor* fd) {
4047     oop mirror = fd->field_holder()->java_mirror();
4048     assert(mirror != NULL, "UnsafeConstants must have mirror already");
4049     assert(fd->field_holder() == SystemDictionary::UnsafeConstants_klass(), "Should be UnsafeConstants");
4050     assert(fd->is_final(), "fields of UnsafeConstants must be final");
4051     assert(fd->is_static(), "fields of UnsafeConstants must be static");
4052     if (fd->name() == vmSymbols::address_size_name()) {
4053       mirror->int_field_put(fd->offset(), _address_size);
4054     } else if (fd->name() == vmSymbols::page_size_name()) {
4055       mirror->int_field_put(fd->offset(), _page_size);
4056     } else if (fd->name() == vmSymbols::big_endian_name()) {
4057       mirror->bool_field_put(fd->offset(), _big_endian);
4058     } else if (fd->name() == vmSymbols::use_unaligned_access_name()) {
4059       mirror->bool_field_put(fd->offset(), _use_unaligned_access);
4060     } else {
4061       assert(false, "unexpected UnsafeConstants field");
4062     }
4063   }
4064 };
4065 
4066 void jdk_internal_misc_UnsafeConstants::set_unsafe_constants() {
4067   UnsafeConstantsFixup fixup;
4068   SystemDictionary::UnsafeConstants_klass()->do_local_static_fields(&fixup);
4069 }
4070 
4071 int java_lang_Class::_klass_offset;
4072 int java_lang_Class::_array_klass_offset;
4073 int java_lang_Class::_oop_size_offset;
4074 int java_lang_Class::_static_oop_field_count_offset;
4075 int java_lang_Class::_class_loader_offset;
4076 int java_lang_Class::_module_offset;
4077 int java_lang_Class::_protection_domain_offset;
4078 int java_lang_Class::_component_mirror_offset;
4079 int java_lang_Class::_init_lock_offset;
4080 int java_lang_Class::_signers_offset;
4081 int java_lang_Class::_name_offset;
4082 int java_lang_Class::_source_file_offset;
4083 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL;
4084 GrowableArray<Klass*>* java_lang_Class::_fixup_module_field_list = NULL;
4085 int java_lang_Throwable::backtrace_offset;
4086 int java_lang_Throwable::detailMessage_offset;
4087 int java_lang_Throwable::stackTrace_offset;
4088 int java_lang_Throwable::depth_offset;
4089 int java_lang_Throwable::static_unassigned_stacktrace_offset;
4090 int java_lang_reflect_AccessibleObject::override_offset;


4358   AbstractAssembler::update_delayed_values();
4359 }
4360 
4361 #if INCLUDE_CDS
4362 #define DO_SERIALIZE_OFFSETS(k) k::serialize_offsets(soc);
4363 
4364 void JavaClasses::serialize_offsets(SerializeClosure* soc) {
4365   BASIC_JAVA_CLASSES_DO(DO_SERIALIZE_OFFSETS);
4366 }
4367 #endif
4368 
4369 
4370 #ifndef PRODUCT
4371 
4372 // These functions exist to assert the validity of hard-coded field offsets to guard
4373 // against changes in the class files
4374 
4375 bool JavaClasses::check_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) {
4376   EXCEPTION_MARK;
4377   fieldDescriptor fd;
4378   TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name);
4379   Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
4380   InstanceKlass* ik = InstanceKlass::cast(k);
4381   TempNewSymbol f_name = SymbolTable::new_symbol(field_name);
4382   TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig);
4383   if (!ik->find_local_field(f_name, f_sig, &fd)) {
4384     tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
4385     return false;
4386   }
4387   if (fd.is_static()) {
4388     tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name);
4389     return false;
4390   }
4391   if (fd.offset() == hardcoded_offset ) {
4392     return true;
4393   } else {
4394     tty->print_cr("Offset of nonstatic field %s.%s is hardcoded as %d but should really be %d.",
4395                   klass_name, field_name, hardcoded_offset, fd.offset());
4396     return false;
4397   }
4398 }
4399 
4400 // Check the hard-coded field offsets of all the classes in this file
4401 
4402 void JavaClasses::check_offsets() {




  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/altHashing.hpp"
  27 #include "classfile/classLoaderData.inline.hpp"
  28 #include "classfile/javaClasses.inline.hpp"
  29 #include "classfile/moduleEntry.hpp"
  30 #include "classfile/stringTable.hpp"

  31 #include "classfile/vmSymbols.hpp"
  32 #include "code/debugInfo.hpp"
  33 #include "code/dependencyContext.hpp"
  34 #include "code/pcDesc.hpp"
  35 #include "interpreter/interpreter.hpp"
  36 #include "interpreter/linkResolver.hpp"
  37 #include "logging/log.hpp"
  38 #include "logging/logStream.hpp"
  39 #include "memory/heapShared.inline.hpp"
  40 #include "memory/metaspaceShared.hpp"
  41 #include "memory/oopFactory.hpp"
  42 #include "memory/resourceArea.hpp"
  43 #include "memory/universe.hpp"
  44 #include "oops/fieldStreams.hpp"
  45 #include "oops/instanceKlass.hpp"
  46 #include "oops/instanceMirrorKlass.hpp"
  47 #include "oops/klass.hpp"
  48 #include "oops/method.inline.hpp"
  49 #include "oops/objArrayOop.inline.hpp"
  50 #include "oops/oop.inline.hpp"


 143     vm_exit_during_initialization("Invalid layout of well-known class: use -Xlog:class+load=info to see the origin of the problem class");
 144   }
 145   dest_offset = fd.offset();
 146 }
 147 
 148 // Overloading to pass name as a string.
 149 static void compute_offset(int& dest_offset, InstanceKlass* ik,
 150                            const char* name_string, Symbol* signature_symbol,
 151                            bool is_static = false) {
 152   TempNewSymbol name = SymbolTable::probe(name_string, (int)strlen(name_string));
 153   if (name == NULL) {
 154     ResourceMark rm;
 155     log_error(class)("Name %s should be in the SymbolTable since its class is loaded", name_string);
 156     vm_exit_during_initialization("Invalid layout of well-known class", ik->external_name());
 157   }
 158   compute_offset(dest_offset, ik, name, signature_symbol, is_static);
 159 }
 160 
 161 int java_lang_String::value_offset  = 0;
 162 int java_lang_String::hash_offset   = 0;

 163 int java_lang_String::coder_offset  = 0;
 164 
 165 bool java_lang_String::initialized  = false;
 166 
 167 bool java_lang_String::is_instance(oop obj) {
 168   return is_instance_inlined(obj);
 169 }
 170 
 171 #if INCLUDE_CDS
 172 #define FIELD_SERIALIZE_OFFSET(offset, klass, name, signature, is_static) \
 173   f->do_u4((u4*)&offset)
 174 #endif
 175 
 176 #define FIELD_COMPUTE_OFFSET(offset, klass, name, signature, is_static) \
 177   compute_offset(offset, klass, name, vmSymbols::signature(), is_static)
 178 
 179 #define STRING_FIELDS_DO(macro) \
 180   macro(value_offset, k, vmSymbols::value_name(), byte_array_signature, false); \
 181   macro(hash_offset,  k, "hash",                  int_signature,        false); \
 182   macro(coder_offset, k, "coder",                 byte_signature,       false)

 183 
 184 void java_lang_String::compute_offsets() {
 185   if (initialized) {
 186     return;
 187   }
 188 
 189   InstanceKlass* k = SystemDictionary::String_klass();
 190   STRING_FIELDS_DO(FIELD_COMPUTE_OFFSET);
 191 
 192   initialized = true;
 193 }
 194 
 195 #if INCLUDE_CDS
 196 void java_lang_String::serialize_offsets(SerializeClosure* f) {
 197   STRING_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
 198   f->do_u4((u4*)&initialized);
 199 }
 200 #endif
 201 
 202 class CompactStringsFixup : public FieldClosure {
 203 private:
 204   bool _value;
 205 
 206 public:
 207   CompactStringsFixup(bool value) : _value(value) {}
 208 
 209   void do_field(fieldDescriptor* fd) {
 210     if (fd->name() == vmSymbols::compact_strings_name()) {
 211       oop mirror = fd->field_holder()->java_mirror();
 212       assert(fd->field_holder() == SystemDictionary::String_klass(), "Should be String");
 213       assert(mirror != NULL, "String must have mirror already");
 214       mirror->bool_field_put(fd->offset(), _value);
 215     }
 216   }
 217 };
 218 
 219 void java_lang_String::set_compact_strings(bool value) {
 220   CompactStringsFixup fix(value);
 221   InstanceKlass::cast(SystemDictionary::String_klass())->do_local_static_fields(&fix);
 222 }
 223 
 224 Handle java_lang_String::basic_create(int length, bool is_latin1, TRAPS) {
 225   assert(initialized, "Must be initialized");
 226   assert(CompactStrings || !is_latin1, "Must be UTF16 without CompactStrings");
 227 
 228   // Create the String object first, so there's a chance that the String
 229   // and the char array it points to end up in the same cache line.
 230   oop obj;
 231   obj = SystemDictionary::String_klass()->allocate_instance(CHECK_NH);
 232 
 233   // Create the char array.  The String object must be handlized here
 234   // because GC can happen as a result of the allocation attempt.
 235   Handle h_obj(THREAD, obj);
 236   int arr_length = is_latin1 ? length : length << 1; // 2 bytes per UTF16.
 237   typeArrayOop buffer = oopFactory::new_byteArray(arr_length, CHECK_NH);;
 238 
 239   // Point the String at the char array
 240   obj = h_obj();
 241   set_value(obj, buffer);


 490   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 491 
 492   jchar* result = NEW_RESOURCE_ARRAY_RETURN_NULL(jchar, length);
 493   if (result != NULL) {
 494     if (!is_latin1) {
 495       for (int index = 0; index < length; index++) {
 496         result[index] = value->char_at(index);
 497       }
 498     } else {
 499       for (int index = 0; index < length; index++) {
 500         result[index] = ((jchar) value->byte_at(index)) & 0xff;
 501       }
 502     }
 503   } else {
 504     THROW_MSG_0(vmSymbols::java_lang_OutOfMemoryError(), "could not allocate Unicode string");
 505   }
 506   return result;
 507 }
 508 
 509 unsigned int java_lang_String::hash_code(oop java_string) {
 510   typeArrayOop value  = java_lang_String::value(java_string);
 511   int          length = java_lang_String::length(java_string, value);
 512   // Zero length string will hash to zero with String.hashCode() function.
 513   if (length == 0) return 0;












 514 
 515   bool      is_latin1 = java_lang_String::is_latin1(java_string);







 516 
 517   if (is_latin1) {
 518     return java_lang_String::hash_code(value->byte_at_addr(0), length);
 519   } else {
 520     return java_lang_String::hash_code(value->char_at_addr(0), length);
 521   }

 522 }
 523 
 524 char* java_lang_String::as_quoted_ascii(oop java_string) {
 525   typeArrayOop value  = java_lang_String::value(java_string);
 526   int          length = java_lang_String::length(java_string, value);
 527   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 528 
 529   if (length == 0) return NULL;
 530 
 531   char* result;
 532   int result_length;
 533   if (!is_latin1) {
 534     jchar* base = value->char_at_addr(0);
 535     result_length = UNICODE::quoted_ascii_length(base, length) + 1;
 536     result = NEW_RESOURCE_ARRAY(char, result_length);
 537     UNICODE::as_quoted_ascii(base, length, result, result_length);
 538   } else {
 539     jbyte* base = value->byte_at_addr(0);
 540     result_length = UNICODE::quoted_ascii_length(base, length) + 1;
 541     result = NEW_RESOURCE_ARRAY(char, result_length);
 542     UNICODE::as_quoted_ascii(base, length, result, result_length);
 543   }
 544   assert(result_length >= length + 1, "must not be shorter");
 545   assert(result_length == (int)strlen(result) + 1, "must match");
 546   return result;
 547 }
 548 
 549 Symbol* java_lang_String::as_symbol(oop java_string, TRAPS) {
 550   typeArrayOop value  = java_lang_String::value(java_string);
 551   int          length = java_lang_String::length(java_string, value);
 552   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 553   if (!is_latin1) {
 554     jchar* base = (length == 0) ? NULL : value->char_at_addr(0);
 555     Symbol* sym = SymbolTable::lookup_unicode(base, length, THREAD);
 556     return sym;
 557   } else {
 558     ResourceMark rm;
 559     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 560     const char* base = UNICODE::as_utf8(position, length);
 561     Symbol* sym = SymbolTable::lookup(base, length, THREAD);
 562     return sym;
 563   }
 564 }
 565 
 566 Symbol* java_lang_String::as_symbol_or_null(oop java_string) {
 567   typeArrayOop value  = java_lang_String::value(java_string);
 568   int          length = java_lang_String::length(java_string, value);
 569   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 570   if (!is_latin1) {
 571     jchar* base = (length == 0) ? NULL : value->char_at_addr(0);
 572     return SymbolTable::probe_unicode(base, length);
 573   } else {
 574     ResourceMark rm;
 575     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 576     const char* base = UNICODE::as_utf8(position, length);
 577     return SymbolTable::probe(base, length);
 578   }
 579 }
 580 
 581 int java_lang_String::utf8_length(oop java_string, typeArrayOop value) {


1403 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1404   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1405   Symbol* name = NULL;
1406   bool is_instance = false;
1407   if (is_primitive(java_class)) {
1408     name = vmSymbols::type_signature(primitive_type(java_class));
1409   } else {
1410     Klass* k = as_Klass(java_class);
1411     is_instance = k->is_instance_klass();
1412     name = k->name();
1413   }
1414   if (name == NULL) {
1415     st->print("<null>");
1416     return;
1417   }
1418   if (is_instance)  st->print("L");
1419   st->write((char*) name->base(), (int) name->utf8_length());
1420   if (is_instance)  st->print(";");
1421 }
1422 
1423 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found, TRAPS) {
1424   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1425   Symbol* name;
1426   if (is_primitive(java_class)) {
1427     name = vmSymbols::type_signature(primitive_type(java_class));
1428     // Because this can create a new symbol, the caller has to decrement
1429     // the refcount, so make adjustment here and below for symbols returned
1430     // that are not created or incremented due to a successful lookup.
1431     name->increment_refcount();
1432   } else {
1433     Klass* k = as_Klass(java_class);
1434     if (!k->is_instance_klass()) {
1435       name = k->name();
1436       name->increment_refcount();
1437     } else {
1438       ResourceMark rm;
1439       const char* sigstr = k->signature_name();
1440       int         siglen = (int) strlen(sigstr);
1441       if (!intern_if_not_found) {
1442         name = SymbolTable::probe(sigstr, siglen);
1443       } else {
1444         name = SymbolTable::new_symbol(sigstr, siglen, THREAD);
1445       }
1446     }
1447   }
1448   return name;
1449 }
1450 
1451 // Returns the Java name for this Java mirror (Resource allocated)
1452 // See Klass::external_name().
1453 // For primitive type Java mirrors, its type name is returned.
1454 const char* java_lang_Class::as_external_name(oop java_class) {
1455   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1456   const char* name = NULL;
1457   if (is_primitive(java_class)) {
1458     name = type2name(primitive_type(java_class));
1459   } else {
1460     name = as_Klass(java_class)->external_name();
1461   }
1462   if (name == NULL) {
1463     name = "<null>";
1464   }


1574 // Note: JDK1.1 and before had a privateInfo_offset field which was used for the
1575 //       platform thread structure, and a eetop offset which was used for thread
1576 //       local storage (and unused by the HotSpot VM). In JDK1.2 the two structures
1577 //       merged, so in the HotSpot VM we just use the eetop field for the thread
1578 //       instead of the privateInfo_offset.
1579 //
1580 // Note: The stackSize field is only present starting in 1.4.
1581 
1582 int java_lang_Thread::_name_offset = 0;
1583 int java_lang_Thread::_group_offset = 0;
1584 int java_lang_Thread::_contextClassLoader_offset = 0;
1585 int java_lang_Thread::_inheritedAccessControlContext_offset = 0;
1586 int java_lang_Thread::_priority_offset = 0;
1587 int java_lang_Thread::_eetop_offset = 0;
1588 int java_lang_Thread::_daemon_offset = 0;
1589 int java_lang_Thread::_stillborn_offset = 0;
1590 int java_lang_Thread::_stackSize_offset = 0;
1591 int java_lang_Thread::_tid_offset = 0;
1592 int java_lang_Thread::_thread_status_offset = 0;
1593 int java_lang_Thread::_park_blocker_offset = 0;
1594 int java_lang_Thread::_park_event_offset = 0 ;
1595 
1596 #define THREAD_FIELDS_DO(macro) \
1597   macro(_name_offset,          k, vmSymbols::name_name(), string_signature, false); \
1598   macro(_group_offset,         k, vmSymbols::group_name(), threadgroup_signature, false); \
1599   macro(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), classloader_signature, false); \
1600   macro(_inheritedAccessControlContext_offset, k, vmSymbols::inheritedAccessControlContext_name(), accesscontrolcontext_signature, false); \
1601   macro(_priority_offset,      k, vmSymbols::priority_name(), int_signature, false); \
1602   macro(_daemon_offset,        k, vmSymbols::daemon_name(), bool_signature, false); \
1603   macro(_eetop_offset,         k, "eetop", long_signature, false); \
1604   macro(_stillborn_offset,     k, "stillborn", bool_signature, false); \
1605   macro(_stackSize_offset,     k, "stackSize", long_signature, false); \
1606   macro(_tid_offset,           k, "tid", long_signature, false); \
1607   macro(_thread_status_offset, k, "threadStatus", int_signature, false); \
1608   macro(_park_blocker_offset,  k, "parkBlocker", object_signature, false); \
1609   macro(_park_event_offset,    k, "nativeParkEventPointer", long_signature, false)
1610 
1611 void java_lang_Thread::compute_offsets() {
1612   assert(_group_offset == 0, "offsets should be initialized only once");
1613 
1614   InstanceKlass* k = SystemDictionary::Thread_klass();
1615   THREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1616 }
1617 
1618 #if INCLUDE_CDS
1619 void java_lang_Thread::serialize_offsets(SerializeClosure* f) {
1620   THREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1621 }
1622 #endif
1623 
1624 JavaThread* java_lang_Thread::thread(oop java_thread) {
1625   return (JavaThread*)java_thread->address_field(_eetop_offset);
1626 }
1627 
1628 
1629 void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) {


1675 
1676 bool java_lang_Thread::is_daemon(oop java_thread) {
1677   return java_thread->bool_field(_daemon_offset) != 0;
1678 }
1679 
1680 
1681 void java_lang_Thread::set_daemon(oop java_thread) {
1682   java_thread->bool_field_put(_daemon_offset, true);
1683 }
1684 
1685 oop java_lang_Thread::context_class_loader(oop java_thread) {
1686   return java_thread->obj_field(_contextClassLoader_offset);
1687 }
1688 
1689 oop java_lang_Thread::inherited_access_control_context(oop java_thread) {
1690   return java_thread->obj_field(_inheritedAccessControlContext_offset);
1691 }
1692 
1693 
1694 jlong java_lang_Thread::stackSize(oop java_thread) {
1695   if (_stackSize_offset > 0) {
1696     return java_thread->long_field(_stackSize_offset);
1697   } else {
1698     return 0;
1699   }
1700 }
1701 
1702 // Write the thread status value to threadStatus field in java.lang.Thread java class.
1703 void java_lang_Thread::set_thread_status(oop java_thread,
1704                                          java_lang_Thread::ThreadStatus status) {
1705   // The threadStatus is only present starting in 1.5
1706   if (_thread_status_offset > 0) {
1707     java_thread->int_field_put(_thread_status_offset, status);
1708   }
1709 }
1710 
1711 // Read thread status value from threadStatus field in java.lang.Thread java class.
1712 java_lang_Thread::ThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
1713   // Make sure the caller is operating on behalf of the VM or is
1714   // running VM code (state == _thread_in_vm).
1715   assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
1716          JavaThread::current()->thread_state() == _thread_in_vm,
1717          "Java Thread is not running in vm");
1718   // The threadStatus is only present starting in 1.5
1719   if (_thread_status_offset > 0) {
1720     return (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
1721   } else {
1722     // All we can easily figure out is if it is alive, but that is
1723     // enough info for a valid unknown status.
1724     // These aren't restricted to valid set ThreadStatus values, so
1725     // use JVMTI values and cast.
1726     JavaThread* thr = java_lang_Thread::thread(java_thread);
1727     if (thr == NULL) {
1728       // the thread hasn't run yet or is in the process of exiting
1729       return NEW;
1730     }
1731     return (java_lang_Thread::ThreadStatus)JVMTI_THREAD_STATE_ALIVE;
1732   }
1733 }
1734 
1735 
1736 jlong java_lang_Thread::thread_id(oop java_thread) {
1737   // The thread ID field is only present starting in 1.5
1738   if (_tid_offset > 0) {
1739     return java_thread->long_field(_tid_offset);
1740   } else {
1741     return 0;
1742   }
1743 }
1744 
1745 oop java_lang_Thread::park_blocker(oop java_thread) {
1746   assert(JDK_Version::current().supports_thread_park_blocker() &&
1747          _park_blocker_offset != 0, "Must support parkBlocker field");
1748 
1749   if (_park_blocker_offset > 0) {
1750     return java_thread->obj_field(_park_blocker_offset);
1751   }
1752 
1753   return NULL;
1754 }
1755 
1756 jlong java_lang_Thread::park_event(oop java_thread) {
1757   if (_park_event_offset > 0) {
1758     return java_thread->long_field(_park_event_offset);
1759   }
1760   return 0;
1761 }
1762 
1763 bool java_lang_Thread::set_park_event(oop java_thread, jlong ptr) {
1764   if (_park_event_offset > 0) {
1765     java_thread->long_field_put(_park_event_offset, ptr);
1766     return true;
1767   }
1768   return false;
1769 }
1770 
1771 
1772 const char* java_lang_Thread::thread_status_name(oop java_thread) {
1773   assert(_thread_status_offset != 0, "Must have thread status");
1774   ThreadStatus status = (java_lang_Thread::ThreadStatus)java_thread->int_field(_thread_status_offset);
1775   switch (status) {
1776     case NEW                      : return "NEW";
1777     case RUNNABLE                 : return "RUNNABLE";
1778     case SLEEPING                 : return "TIMED_WAITING (sleeping)";
1779     case IN_OBJECT_WAIT           : return "WAITING (on object monitor)";
1780     case IN_OBJECT_WAIT_TIMED     : return "TIMED_WAITING (on object monitor)";
1781     case PARKED                   : return "WAITING (parking)";
1782     case PARKED_TIMED             : return "TIMED_WAITING (parking)";
1783     case BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
1784     case TERMINATED               : return "TERMINATED";
1785     default                       : return "UNKNOWN";
1786   };
1787 }
1788 int java_lang_ThreadGroup::_parent_offset = 0;
1789 int java_lang_ThreadGroup::_name_offset = 0;
1790 int java_lang_ThreadGroup::_threads_offset = 0;
1791 int java_lang_ThreadGroup::_groups_offset = 0;
1792 int java_lang_ThreadGroup::_maxPriority_offset = 0;
1793 int java_lang_ThreadGroup::_destroyed_offset = 0;


1907 }
1908 
1909 int java_lang_Throwable::depth(oop throwable) {
1910   return throwable->int_field(depth_offset);
1911 }
1912 
1913 void java_lang_Throwable::set_depth(oop throwable, int value) {
1914   throwable->int_field_put(depth_offset, value);
1915 }
1916 
1917 oop java_lang_Throwable::message(oop throwable) {
1918   return throwable->obj_field(detailMessage_offset);
1919 }
1920 
1921 
1922 // Return Symbol for detailed_message or NULL
1923 Symbol* java_lang_Throwable::detail_message(oop throwable) {
1924   PRESERVE_EXCEPTION_MARK;  // Keep original exception
1925   oop detailed_message = java_lang_Throwable::message(throwable);
1926   if (detailed_message != NULL) {
1927     return java_lang_String::as_symbol(detailed_message, THREAD);
1928   }
1929   return NULL;
1930 }
1931 
1932 void java_lang_Throwable::set_message(oop throwable, oop value) {
1933   throwable->obj_field_put(detailMessage_offset, value);
1934 }
1935 
1936 
1937 void java_lang_Throwable::set_stacktrace(oop throwable, oop st_element_array) {
1938   throwable->obj_field_put(stackTrace_offset, st_element_array);
1939 }
1940 
1941 void java_lang_Throwable::clear_stacktrace(oop throwable) {
1942   set_stacktrace(throwable, NULL);
1943 }
1944 
1945 
1946 void java_lang_Throwable::print(oop throwable, outputStream* st) {
1947   ResourceMark rm;


2617         java_lang_Class::set_source_file(java_class(), source_file);
2618       }
2619     } else {
2620       // Class was redefined. Dump the cache if it was set.
2621       if (source_file != NULL) {
2622         source_file = NULL;
2623         java_lang_Class::set_source_file(java_class(), source_file);
2624       }
2625       if (ShowHiddenFrames) {
2626         source = vmSymbols::unknown_class_name();
2627         source_file = StringTable::intern(source, CHECK);
2628       }
2629     }
2630     java_lang_StackTraceElement::set_fileName(element(), source_file);
2631 
2632     int line_number = Backtrace::get_line_number(method, bci);
2633     java_lang_StackTraceElement::set_lineNumber(element(), line_number);
2634   }
2635 }
2636 







































2637 Method* java_lang_StackFrameInfo::get_method(Handle stackFrame, InstanceKlass* holder, TRAPS) {
2638   HandleMark hm(THREAD);
2639   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
2640   Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mname());
2641   // we should expand MemberName::name when Throwable uses StackTrace
2642   // MethodHandles::expand_MemberName(mname, MethodHandles::_suppress_defc|MethodHandles::_suppress_type, CHECK_NULL);
2643   return method;
2644 }
2645 
2646 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci, TRAPS) {
2647   // set Method* or mid/cpref
2648   HandleMark hm(THREAD);
2649   Handle mname(Thread::current(), stackFrame->obj_field(_memberName_offset));
2650   InstanceKlass* ik = method->method_holder();
2651   CallInfo info(method(), ik, CHECK);
2652   MethodHandles::init_method_MemberName(mname, info);
2653   // set bci
2654   java_lang_StackFrameInfo::set_bci(stackFrame(), bci);
2655   // method may be redefined; store the version
2656   int version = method->constants()->version();


3596 
3597 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
3598   assert(is_instance(mname), "wrong type");
3599   mname->address_field_put(_vmindex_offset, (address) index);
3600 }
3601 
3602 
3603 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) {
3604   assert(is_instance(resolved_method), "wrong type");
3605   Method* m = (Method*)resolved_method->address_field(_vmtarget_offset);
3606   assert(m->is_method(), "must be");
3607   return m;
3608 }
3609 
3610 // Used by redefinition to change Method* to new Method* with same hash (name, signature)
3611 void java_lang_invoke_ResolvedMethodName::set_vmtarget(oop resolved_method, Method* m) {
3612   assert(is_instance(resolved_method), "wrong type");
3613   resolved_method->address_field_put(_vmtarget_offset, (address)m);
3614 }
3615 





3616 oop java_lang_invoke_ResolvedMethodName::find_resolved_method(const methodHandle& m, TRAPS) {


3617   // lookup ResolvedMethod oop in the table, or create a new one and intern it
3618   oop resolved_method = ResolvedMethodTable::find_method(m());
3619   if (resolved_method == NULL) {
3620     InstanceKlass* k = SystemDictionary::ResolvedMethodName_klass();
3621     if (!k->is_initialized()) {
3622       k->initialize(CHECK_NULL);
3623     }
3624     oop new_resolved_method = k->allocate_instance(CHECK_NULL);
3625     new_resolved_method->address_field_put(_vmtarget_offset, (address)m());
3626     // Add a reference to the loader (actually mirror because unsafe anonymous classes will not have
3627     // distinct loaders) to ensure the metadata is kept alive.
3628     // This mirror may be different than the one in clazz field.
3629     new_resolved_method->obj_field_put(_vmholder_offset, m->method_holder()->java_mirror());
3630     resolved_method = ResolvedMethodTable::add_method(m, Handle(THREAD, new_resolved_method));
3631   }
3632   return resolved_method;




























3633 }
3634 
3635 oop java_lang_invoke_LambdaForm::vmentry(oop lform) {
3636   assert(is_instance(lform), "wrong type");
3637   return lform->obj_field(_vmentry_offset);
3638 }
3639 
3640 
3641 // Support for java_lang_invoke_MethodType
3642 
3643 int java_lang_invoke_MethodType::_rtype_offset;
3644 int java_lang_invoke_MethodType::_ptypes_offset;
3645 
3646 #define METHODTYPE_FIELDS_DO(macro) \
3647   macro(_rtype_offset,  k, "rtype",  class_signature,       false); \
3648   macro(_ptypes_offset, k, "ptypes", class_array_signature, false)
3649 
3650 void java_lang_invoke_MethodType::compute_offsets() {
3651   InstanceKlass* k = SystemDictionary::MethodType_klass();
3652   METHODTYPE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3653 }
3654 
3655 #if INCLUDE_CDS
3656 void java_lang_invoke_MethodType::serialize_offsets(SerializeClosure* f) {
3657   METHODTYPE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3658 }
3659 #endif
3660 
3661 void java_lang_invoke_MethodType::print_signature(oop mt, outputStream* st) {
3662   st->print("(");
3663   objArrayOop pts = ptypes(mt);
3664   for (int i = 0, limit = pts->length(); i < limit; i++) {
3665     java_lang_Class::print_signature(pts->obj_at(i), st);
3666   }
3667   st->print(")");
3668   java_lang_Class::print_signature(rtype(mt), st);
3669 }
3670 
3671 Symbol* java_lang_invoke_MethodType::as_signature(oop mt, bool intern_if_not_found, TRAPS) {
3672   ResourceMark rm;
3673   stringStream buffer(128);
3674   print_signature(mt, &buffer);
3675   const char* sigstr =       buffer.base();
3676   int         siglen = (int) buffer.size();
3677   Symbol *name;
3678   if (!intern_if_not_found) {
3679     name = SymbolTable::probe(sigstr, siglen);
3680   } else {
3681     name = SymbolTable::new_symbol(sigstr, siglen, THREAD);
3682   }
3683   return name;
3684 }
3685 
3686 bool java_lang_invoke_MethodType::equals(oop mt1, oop mt2) {
3687   if (oopDesc::equals(mt1, mt2))
3688     return true;
3689   if (!oopDesc::equals(rtype(mt1), rtype(mt2)))
3690     return false;
3691   if (ptype_count(mt1) != ptype_count(mt2))
3692     return false;
3693   for (int i = ptype_count(mt1) - 1; i >= 0; i--) {
3694     if (!oopDesc::equals(ptype(mt1, i), ptype(mt2, i)))
3695       return false;
3696   }
3697   return true;
3698 }
3699 
3700 oop java_lang_invoke_MethodType::rtype(oop mt) {
3701   assert(is_instance(mt), "must be a MethodType");


3974   macro(static_in_offset,  k, "in",  input_stream_signature, true); \
3975   macro(static_out_offset, k, "out", print_stream_signature, true); \
3976   macro(static_err_offset, k, "err", print_stream_signature, true); \
3977   macro(static_security_offset, k, "security", security_manager_signature, true)
3978 
3979 void java_lang_System::compute_offsets() {
3980   InstanceKlass* k = SystemDictionary::System_klass();
3981   SYSTEM_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3982 }
3983 
3984 #if INCLUDE_CDS
3985 void java_lang_System::serialize_offsets(SerializeClosure* f) {
3986    SYSTEM_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3987 }
3988 #endif
3989 
3990 int java_lang_System::in_offset_in_bytes() { return static_in_offset; }
3991 int java_lang_System::out_offset_in_bytes() { return static_out_offset; }
3992 int java_lang_System::err_offset_in_bytes() { return static_err_offset; }
3993 










































3994 int java_lang_Class::_klass_offset;
3995 int java_lang_Class::_array_klass_offset;
3996 int java_lang_Class::_oop_size_offset;
3997 int java_lang_Class::_static_oop_field_count_offset;
3998 int java_lang_Class::_class_loader_offset;
3999 int java_lang_Class::_module_offset;
4000 int java_lang_Class::_protection_domain_offset;
4001 int java_lang_Class::_component_mirror_offset;
4002 int java_lang_Class::_init_lock_offset;
4003 int java_lang_Class::_signers_offset;
4004 int java_lang_Class::_name_offset;
4005 int java_lang_Class::_source_file_offset;
4006 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL;
4007 GrowableArray<Klass*>* java_lang_Class::_fixup_module_field_list = NULL;
4008 int java_lang_Throwable::backtrace_offset;
4009 int java_lang_Throwable::detailMessage_offset;
4010 int java_lang_Throwable::stackTrace_offset;
4011 int java_lang_Throwable::depth_offset;
4012 int java_lang_Throwable::static_unassigned_stacktrace_offset;
4013 int java_lang_reflect_AccessibleObject::override_offset;


4281   AbstractAssembler::update_delayed_values();
4282 }
4283 
4284 #if INCLUDE_CDS
4285 #define DO_SERIALIZE_OFFSETS(k) k::serialize_offsets(soc);
4286 
4287 void JavaClasses::serialize_offsets(SerializeClosure* soc) {
4288   BASIC_JAVA_CLASSES_DO(DO_SERIALIZE_OFFSETS);
4289 }
4290 #endif
4291 
4292 
4293 #ifndef PRODUCT
4294 
4295 // These functions exist to assert the validity of hard-coded field offsets to guard
4296 // against changes in the class files
4297 
4298 bool JavaClasses::check_offset(const char *klass_name, int hardcoded_offset, const char *field_name, const char* field_sig) {
4299   EXCEPTION_MARK;
4300   fieldDescriptor fd;
4301   TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name, CATCH);
4302   Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
4303   InstanceKlass* ik = InstanceKlass::cast(k);
4304   TempNewSymbol f_name = SymbolTable::new_symbol(field_name, CATCH);
4305   TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig, CATCH);
4306   if (!ik->find_local_field(f_name, f_sig, &fd)) {
4307     tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
4308     return false;
4309   }
4310   if (fd.is_static()) {
4311     tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name);
4312     return false;
4313   }
4314   if (fd.offset() == hardcoded_offset ) {
4315     return true;
4316   } else {
4317     tty->print_cr("Offset of nonstatic field %s.%s is hardcoded as %d but should really be %d.",
4318                   klass_name, field_name, hardcoded_offset, fd.offset());
4319     return false;
4320   }
4321 }
4322 
4323 // Check the hard-coded field offsets of all the classes in this file
4324 
4325 void JavaClasses::check_offsets() {


< prev index next >