1 /*
   2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "cds/archiveBuilder.hpp"
  28 #include "cds/heapShared.inline.hpp"
  29 #include "cds/metaspaceShared.hpp"
  30 #include "classfile/altHashing.hpp"
  31 #include "classfile/classLoaderData.inline.hpp"
  32 #include "classfile/javaClasses.inline.hpp"
  33 #include "classfile/javaThreadStatus.hpp"
  34 #include "classfile/moduleEntry.hpp"
  35 #include "classfile/stringTable.hpp"
  36 #include "classfile/symbolTable.hpp"
  37 #include "classfile/systemDictionary.hpp"
  38 #include "classfile/vmClasses.hpp"
  39 #include "classfile/vmSymbols.hpp"
  40 #include "code/debugInfo.hpp"
  41 #include "code/dependencyContext.hpp"
  42 #include "code/pcDesc.hpp"
  43 #include "gc/shared/collectedHeap.inline.hpp"
  44 #include "interpreter/interpreter.hpp"
  45 #include "interpreter/linkResolver.hpp"
  46 #include "logging/log.hpp"
  47 #include "logging/logStream.hpp"
  48 #include "memory/oopFactory.hpp"
  49 #include "memory/resourceArea.hpp"
  50 #include "memory/universe.hpp"
  51 #include "oops/fieldStreams.inline.hpp"
  52 #include "oops/instanceKlass.inline.hpp"
  53 #include "oops/instanceMirrorKlass.hpp"
  54 #include "oops/klass.hpp"
  55 #include "oops/klass.inline.hpp"
  56 #include "oops/method.inline.hpp"
  57 #include "oops/objArrayOop.inline.hpp"
  58 #include "oops/oop.inline.hpp"
  59 #include "oops/symbol.hpp"
  60 #include "oops/recordComponent.hpp"
  61 #include "oops/typeArrayOop.inline.hpp"
  62 #include "prims/jvmtiExport.hpp"
  63 #include "prims/methodHandles.hpp"
  64 #include "prims/resolvedMethodTable.hpp"
  65 #include "runtime/fieldDescriptor.inline.hpp"
  66 #include "runtime/frame.inline.hpp"
  67 #include "runtime/handles.inline.hpp"
  68 #include "runtime/init.hpp"
  69 #include "runtime/interfaceSupport.inline.hpp"
  70 #include "runtime/java.hpp"
  71 #include "runtime/javaCalls.hpp"
  72 #include "runtime/jniHandles.inline.hpp"
  73 #include "runtime/reflectionUtils.hpp"
  74 #include "runtime/safepoint.hpp"
  75 #include "runtime/safepointVerifiers.hpp"
  76 #include "runtime/thread.inline.hpp"
  77 #include "runtime/vframe.inline.hpp"
  78 #include "runtime/vm_version.hpp"
  79 #include "utilities/align.hpp"
  80 #include "utilities/preserveException.hpp"
  81 #include "utilities/utf8.hpp"
  82 #if INCLUDE_JVMCI
  83 #include "jvmci/jvmciJavaClasses.hpp"
  84 #endif
  85 
  86 #define INJECTED_FIELD_COMPUTE_OFFSET(klass, name, signature, may_be_java)    \
  87   klass::_##name##_offset = JavaClasses::compute_injected_offset(JavaClasses::klass##_##name##_enum);
  88 
  89 #if INCLUDE_CDS
  90 #define INJECTED_FIELD_SERIALIZE_OFFSET(klass, name, signature, may_be_java) \
  91   f->do_u4((u4*)&_##name##_offset);
  92 #endif
  93 
  94 #define DECLARE_INJECTED_FIELD(klass, name, signature, may_be_java)           \
  95   { VM_CLASS_ID(klass), VM_SYMBOL_ENUM_NAME(name##_name), VM_SYMBOL_ENUM_NAME(signature), may_be_java },
  96 
  97 InjectedField JavaClasses::_injected_fields[] = {
  98   ALL_INJECTED_FIELDS(DECLARE_INJECTED_FIELD)
  99 };
 100 
 101 // Register native methods of Object
 102 void java_lang_Object::register_natives(TRAPS) {
 103   InstanceKlass* obj = vmClasses::Object_klass();
 104   Method::register_native(obj, vmSymbols::hashCode_name(),
 105                           vmSymbols::void_int_signature(), (address) &JVM_IHashCode, CHECK);
 106   Method::register_native(obj, vmSymbols::wait_name(),
 107                           vmSymbols::long_void_signature(), (address) &JVM_MonitorWait, CHECK);
 108   Method::register_native(obj, vmSymbols::notify_name(),
 109                           vmSymbols::void_method_signature(), (address) &JVM_MonitorNotify, CHECK);
 110   Method::register_native(obj, vmSymbols::notifyAll_name(),
 111                           vmSymbols::void_method_signature(), (address) &JVM_MonitorNotifyAll, CHECK);
 112   Method::register_native(obj, vmSymbols::clone_name(),
 113                           vmSymbols::void_object_signature(), (address) &JVM_Clone, THREAD);
 114 }
 115 
 116 int JavaClasses::compute_injected_offset(InjectedFieldID id) {
 117   return _injected_fields[id].compute_offset();
 118 }
 119 
 120 InjectedField* JavaClasses::get_injected(Symbol* class_name, int* field_count) {
 121   *field_count = 0;
 122 
 123   vmSymbolID sid = vmSymbols::find_sid(class_name);
 124   if (sid == vmSymbolID::NO_SID) {
 125     // Only well known classes can inject fields
 126     return NULL;
 127   }
 128 
 129   int count = 0;
 130   int start = -1;
 131 
 132 #define LOOKUP_INJECTED_FIELD(klass, name, signature, may_be_java) \
 133   if (sid == VM_SYMBOL_ENUM_NAME(klass)) {                         \
 134     count++;                                                       \
 135     if (start == -1) start = klass##_##name##_enum;                \
 136   }
 137   ALL_INJECTED_FIELDS(LOOKUP_INJECTED_FIELD);
 138 #undef LOOKUP_INJECTED_FIELD
 139 
 140   if (start != -1) {
 141     *field_count = count;
 142     return _injected_fields + start;
 143   }
 144   return NULL;
 145 }
 146 
 147 
 148 // Helpful routine for computing field offsets at run time rather than hardcoding them
 149 // Finds local fields only, including static fields.  Static field offsets are from the
 150 // beginning of the mirror.
 151 static void compute_offset(int &dest_offset,
 152                            InstanceKlass* ik, Symbol* name_symbol, Symbol* signature_symbol,
 153                            bool is_static = false) {
 154   fieldDescriptor fd;
 155   if (ik == NULL) {
 156     ResourceMark rm;
 157     log_error(class)("Mismatch JDK version for field: %s type: %s", name_symbol->as_C_string(), signature_symbol->as_C_string());
 158     vm_exit_during_initialization("Invalid layout of well-known class");
 159   }
 160 
 161   if (!ik->find_local_field(name_symbol, signature_symbol, &fd) || fd.is_static() != is_static) {
 162     ResourceMark rm;
 163     log_error(class)("Invalid layout of %s field: %s type: %s", ik->external_name(),
 164                      name_symbol->as_C_string(), signature_symbol->as_C_string());
 165 #ifndef PRODUCT
 166     // Prints all fields and offsets
 167     Log(class) lt;
 168     LogStream ls(lt.error());
 169     ik->print_on(&ls);
 170 #endif //PRODUCT
 171     vm_exit_during_initialization("Invalid layout of well-known class: use -Xlog:class+load=info to see the origin of the problem class");
 172   }
 173   dest_offset = fd.offset();
 174 }
 175 
 176 // Overloading to pass name as a string.
 177 static void compute_offset(int& dest_offset, InstanceKlass* ik,
 178                            const char* name_string, Symbol* signature_symbol,
 179                            bool is_static = false) {
 180   TempNewSymbol name = SymbolTable::probe(name_string, (int)strlen(name_string));
 181   if (name == NULL) {
 182     ResourceMark rm;
 183     log_error(class)("Name %s should be in the SymbolTable since its class is loaded", name_string);
 184     vm_exit_during_initialization("Invalid layout of well-known class", ik->external_name());
 185   }
 186   compute_offset(dest_offset, ik, name, signature_symbol, is_static);
 187 }
 188 
 189 
 190 #if INCLUDE_CDS
 191 #define FIELD_SERIALIZE_OFFSET(offset, klass, name, signature, is_static) \
 192   f->do_u4((u4*)&offset)
 193 #endif
 194 
 195 #define FIELD_COMPUTE_OFFSET(offset, klass, name, signature, is_static) \
 196   compute_offset(offset, klass, name, vmSymbols::signature(), is_static)
 197 
 198 // This class provides a simple wrapper over the internal structure of
 199 // exception backtrace to insulate users of the backtrace from needing
 200 // to know what it looks like.
 201 // The code of this class is not GC safe. Allocations can only happen
 202 // in expand().
 203 class BacktraceBuilder: public StackObj {
 204  friend class BacktraceIterator;
 205  private:
 206   Handle          _backtrace;
 207   objArrayOop     _head;
 208   typeArrayOop    _methods;
 209   typeArrayOop    _bcis;
 210   objArrayOop     _mirrors;
 211   typeArrayOop    _names; // Needed to insulate method name against redefinition.
 212   objArrayOop     _conts;
 213   // True if the top frame of the backtrace is omitted because it shall be hidden.
 214   bool            _has_hidden_top_frame;
 215   int             _index;
 216   NoSafepointVerifier _nsv;
 217 
 218   enum {
 219     trace_methods_offset = java_lang_Throwable::trace_methods_offset,
 220     trace_bcis_offset    = java_lang_Throwable::trace_bcis_offset,
 221     trace_mirrors_offset = java_lang_Throwable::trace_mirrors_offset,
 222     trace_names_offset   = java_lang_Throwable::trace_names_offset,
 223     trace_conts_offset   = java_lang_Throwable::trace_conts_offset,
 224     trace_next_offset    = java_lang_Throwable::trace_next_offset,
 225     trace_hidden_offset  = java_lang_Throwable::trace_hidden_offset,
 226     trace_size           = java_lang_Throwable::trace_size,
 227     trace_chunk_size     = java_lang_Throwable::trace_chunk_size
 228   };
 229 
 230   // get info out of chunks
 231   static typeArrayOop get_methods(objArrayHandle chunk) {
 232     typeArrayOop methods = typeArrayOop(chunk->obj_at(trace_methods_offset));
 233     assert(methods != NULL, "method array should be initialized in backtrace");
 234     return methods;
 235   }
 236   static typeArrayOop get_bcis(objArrayHandle chunk) {
 237     typeArrayOop bcis = typeArrayOop(chunk->obj_at(trace_bcis_offset));
 238     assert(bcis != NULL, "bci array should be initialized in backtrace");
 239     return bcis;
 240   }
 241   static objArrayOop get_mirrors(objArrayHandle chunk) {
 242     objArrayOop mirrors = objArrayOop(chunk->obj_at(trace_mirrors_offset));
 243     assert(mirrors != NULL, "mirror array should be initialized in backtrace");
 244     return mirrors;
 245   }
 246   static typeArrayOop get_names(objArrayHandle chunk) {
 247     typeArrayOop names = typeArrayOop(chunk->obj_at(trace_names_offset));
 248     assert(names != NULL, "names array should be initialized in backtrace");
 249     return names;
 250   }
 251   static objArrayOop get_conts(objArrayHandle chunk) {
 252     objArrayOop conts = objArrayOop(chunk->obj_at(trace_conts_offset));
 253     assert(conts != NULL, "conts array should be initialized in backtrace");
 254     return conts;
 255   }
 256   static bool has_hidden_top_frame(objArrayHandle chunk) {
 257     oop hidden = chunk->obj_at(trace_hidden_offset);
 258     return hidden != NULL;
 259   }
 260 
 261  public:
 262 
 263   // constructor for new backtrace
 264   BacktraceBuilder(TRAPS): _head(NULL), _methods(NULL), _bcis(NULL), _mirrors(NULL), _names(NULL), _conts(NULL), _has_hidden_top_frame(false) {
 265     expand(CHECK);
 266     _backtrace = Handle(THREAD, _head);
 267     _index = 0;
 268   }
 269 
 270   BacktraceBuilder(Thread* thread, objArrayHandle backtrace) {
 271     _methods = get_methods(backtrace);
 272     _bcis = get_bcis(backtrace);
 273     _mirrors = get_mirrors(backtrace);
 274     _names = get_names(backtrace);
 275     _conts = get_conts(backtrace);
 276     _has_hidden_top_frame = has_hidden_top_frame(backtrace);
 277     assert(_methods->length() == _bcis->length() &&
 278            _methods->length() == _mirrors->length() &&
 279            _mirrors->length() == _names->length() &&
 280            _names->length() == _conts->length(),
 281            "method and source information arrays should match");
 282 
 283     // head is the preallocated backtrace
 284     _head = backtrace();
 285     _backtrace = Handle(thread, _head);
 286     _index = 0;
 287   }
 288 
 289   void expand(TRAPS) {
 290     objArrayHandle old_head(THREAD, _head);
 291     PauseNoSafepointVerifier pnsv(&_nsv);
 292 
 293     objArrayOop head = oopFactory::new_objectArray(trace_size, CHECK);
 294     objArrayHandle new_head(THREAD, head);
 295 
 296     typeArrayOop methods = oopFactory::new_shortArray(trace_chunk_size, CHECK);
 297     typeArrayHandle new_methods(THREAD, methods);
 298 
 299     typeArrayOop bcis = oopFactory::new_intArray(trace_chunk_size, CHECK);
 300     typeArrayHandle new_bcis(THREAD, bcis);
 301 
 302     objArrayOop mirrors = oopFactory::new_objectArray(trace_chunk_size, CHECK);
 303     objArrayHandle new_mirrors(THREAD, mirrors);
 304 
 305     typeArrayOop names = oopFactory::new_symbolArray(trace_chunk_size, CHECK);
 306     typeArrayHandle new_names(THREAD, names);
 307 
 308     objArrayOop conts = oopFactory::new_objectArray(trace_chunk_size, CHECK);
 309     objArrayHandle new_conts(THREAD, conts);
 310 
 311     if (!old_head.is_null()) {
 312       old_head->obj_at_put(trace_next_offset, new_head());
 313     }
 314     new_head->obj_at_put(trace_methods_offset, new_methods());
 315     new_head->obj_at_put(trace_bcis_offset, new_bcis());
 316     new_head->obj_at_put(trace_mirrors_offset, new_mirrors());
 317     new_head->obj_at_put(trace_names_offset, new_names());
 318     new_head->obj_at_put(trace_conts_offset, new_conts());
 319     new_head->obj_at_put(trace_hidden_offset, NULL);
 320 
 321     _head    = new_head();
 322     _methods = new_methods();
 323     _bcis = new_bcis();
 324     _mirrors = new_mirrors();
 325     _names  = new_names();
 326     _conts  = new_conts();
 327     _index = 0;
 328   }
 329 
 330   oop backtrace() {
 331     return _backtrace();
 332   }
 333 
 334   inline void push(Method* method, int bci, oop contScopeName, TRAPS) {
 335     // Smear the -1 bci to 0 since the array only holds unsigned
 336     // shorts.  The later line number lookup would just smear the -1
 337     // to a 0 even if it could be recorded.
 338     if (bci == SynchronizationEntryBCI) bci = 0;
 339 
 340     if (_index >= trace_chunk_size) {
 341       methodHandle mhandle(THREAD, method);
 342       Handle chandle(THREAD, contScopeName);
 343       expand(CHECK);
 344       method = mhandle();
 345       contScopeName = chandle();
 346     }
 347 
 348     _methods->ushort_at_put(_index, method->orig_method_idnum());
 349     _bcis->int_at_put(_index, Backtrace::merge_bci_and_version(bci, method->constants()->version()));
 350 
 351     // Note:this doesn't leak symbols because the mirror in the backtrace keeps the
 352     // klass owning the symbols alive so their refcounts aren't decremented.
 353     Symbol* name = method->name();
 354     _names->symbol_at_put(_index, name);
 355 
 356     // We need to save the mirrors in the backtrace to keep the class
 357     // from being unloaded while we still have this stack trace.
 358     assert(method->method_holder()->java_mirror() != NULL, "never push null for mirror");
 359     _mirrors->obj_at_put(_index, method->method_holder()->java_mirror());
 360 
 361     _conts->obj_at_put(_index, contScopeName);
 362 
 363     _index++;
 364   }
 365 
 366   void set_has_hidden_top_frame(TRAPS) {
 367     if (!_has_hidden_top_frame) {
 368       // It would be nice to add java/lang/Boolean::TRUE here
 369       // to indicate that this backtrace has a hidden top frame.
 370       // But this code is used before TRUE is allocated.
 371       // Therefore let's just use an arbitrary legal oop
 372       // available right here. _methods is a short[].
 373       assert(_methods != NULL, "we need a legal oop");
 374       _has_hidden_top_frame = true;
 375       _head->obj_at_put(trace_hidden_offset, _methods);
 376     }
 377   }
 378 };
 379 
 380 struct BacktraceElement : public StackObj {
 381   int _method_id;
 382   int _bci;
 383   int _version;
 384   Symbol* _name;
 385   Handle _mirror;
 386   Handle _cont; // the continuation scope name (String)
 387   BacktraceElement(Handle mirror, int mid, int version, int bci, Symbol* name, Handle cont) :
 388                    _method_id(mid), _bci(bci), _version(version), _name(name), _mirror(mirror), _cont(cont) {}
 389 };
 390 
 391 class BacktraceIterator : public StackObj {
 392   int _index;
 393   objArrayHandle  _result;
 394   objArrayHandle  _mirrors;
 395   typeArrayHandle _methods;
 396   typeArrayHandle _bcis;
 397   typeArrayHandle _names;
 398   objArrayHandle  _conts;
 399 
 400   void init(objArrayHandle result, Thread* thread) {
 401     // Get method id, bci, version and mirror from chunk
 402     _result = result;
 403     if (_result.not_null()) {
 404       _methods = typeArrayHandle(thread, BacktraceBuilder::get_methods(_result));
 405       _bcis = typeArrayHandle(thread, BacktraceBuilder::get_bcis(_result));
 406       _mirrors = objArrayHandle(thread, BacktraceBuilder::get_mirrors(_result));
 407       _names = typeArrayHandle(thread, BacktraceBuilder::get_names(_result));
 408       _conts = objArrayHandle(thread, BacktraceBuilder::get_conts(_result));
 409       _index = 0;
 410     }
 411   }
 412  public:
 413   BacktraceIterator(objArrayHandle result, Thread* thread) {
 414     init(result, thread);
 415     assert(_methods.is_null() || _methods->length() == java_lang_Throwable::trace_chunk_size, "lengths don't match");
 416   }
 417 
 418   BacktraceElement next(Thread* thread) {
 419     BacktraceElement e (Handle(thread, _mirrors->obj_at(_index)),
 420                         _methods->ushort_at(_index),
 421                         Backtrace::version_at(_bcis->int_at(_index)),
 422                         Backtrace::bci_at(_bcis->int_at(_index)),
 423                         _names->symbol_at(_index),
 424                         Handle(thread, _conts->obj_at(_index)));
 425     _index++;
 426 
 427     if (_index >= java_lang_Throwable::trace_chunk_size) {
 428       int next_offset = java_lang_Throwable::trace_next_offset;
 429       // Get next chunk
 430       objArrayHandle result (thread, objArrayOop(_result->obj_at(next_offset)));
 431       init(result, thread);
 432     }
 433     return e;
 434   }
 435 
 436   bool repeat() {
 437     return _result.not_null() && _mirrors->obj_at(_index) != NULL;
 438   }
 439 };
 440 
 441 // java_lang_String
 442 
 443 int java_lang_String::_value_offset;
 444 int java_lang_String::_hash_offset;
 445 int java_lang_String::_hashIsZero_offset;
 446 int java_lang_String::_coder_offset;
 447 int java_lang_String::_flags_offset;
 448 
 449 bool java_lang_String::_initialized;
 450 
 451 
 452 bool java_lang_String::test_and_set_flag(oop java_string, uint8_t flag_mask) {
 453   uint8_t* addr = flags_addr(java_string);
 454   uint8_t value = Atomic::load(addr);
 455   while ((value & flag_mask) == 0) {
 456     uint8_t old_value = value;
 457     value |= flag_mask;
 458     value = Atomic::cmpxchg(addr, old_value, value);
 459     if (value == old_value) return false; // Flag bit changed from 0 to 1.
 460   }
 461   return true;                  // Flag bit is already 1.
 462 }
 463 
 464 #define STRING_FIELDS_DO(macro) \
 465   macro(_value_offset, k, vmSymbols::value_name(), byte_array_signature, false); \
 466   macro(_hash_offset,  k, "hash",                  int_signature,        false); \
 467   macro(_hashIsZero_offset, k, "hashIsZero",       bool_signature,       false); \
 468   macro(_coder_offset, k, "coder",                 byte_signature,       false);
 469 
 470 void java_lang_String::compute_offsets() {
 471   if (_initialized) {
 472     return;
 473   }
 474 
 475   InstanceKlass* k = vmClasses::String_klass();
 476   STRING_FIELDS_DO(FIELD_COMPUTE_OFFSET);
 477   STRING_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
 478 
 479   _initialized = true;
 480 }
 481 
 482 #if INCLUDE_CDS
 483 void java_lang_String::serialize_offsets(SerializeClosure* f) {
 484   STRING_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
 485   STRING_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
 486   f->do_bool(&_initialized);
 487 }
 488 #endif
 489 
 490 class CompactStringsFixup : public FieldClosure {
 491 private:
 492   bool _value;
 493 
 494 public:
 495   CompactStringsFixup(bool value) : _value(value) {}
 496 
 497   void do_field(fieldDescriptor* fd) {
 498     if (fd->name() == vmSymbols::compact_strings_name()) {
 499       oop mirror = fd->field_holder()->java_mirror();
 500       assert(fd->field_holder() == vmClasses::String_klass(), "Should be String");
 501       assert(mirror != NULL, "String must have mirror already");
 502       mirror->bool_field_put(fd->offset(), _value);
 503     }
 504   }
 505 };
 506 
 507 void java_lang_String::set_compact_strings(bool value) {
 508   CompactStringsFixup fix(value);
 509   vmClasses::String_klass()->do_local_static_fields(&fix);
 510 }
 511 
 512 Handle java_lang_String::basic_create(int length, bool is_latin1, TRAPS) {
 513   assert(_initialized, "Must be initialized");
 514   assert(CompactStrings || !is_latin1, "Must be UTF16 without CompactStrings");
 515 
 516   // Create the String object first, so there's a chance that the String
 517   // and the char array it points to end up in the same cache line.
 518   oop obj;
 519   obj = vmClasses::String_klass()->allocate_instance(CHECK_NH);
 520 
 521   // Create the char array.  The String object must be handlized here
 522   // because GC can happen as a result of the allocation attempt.
 523   Handle h_obj(THREAD, obj);
 524   int arr_length = is_latin1 ? length : length << 1; // 2 bytes per UTF16.
 525   typeArrayOop buffer = oopFactory::new_byteArray(arr_length, CHECK_NH);;
 526 
 527   // Point the String at the char array
 528   obj = h_obj();
 529   set_value(obj, buffer);
 530   // No need to zero the offset, allocation zero'ed the entire String object
 531   set_coder(obj, is_latin1 ? CODER_LATIN1 : CODER_UTF16);
 532   return h_obj;
 533 }
 534 
 535 Handle java_lang_String::create_from_unicode(const jchar* unicode, int length, TRAPS) {
 536   bool is_latin1 = CompactStrings && UNICODE::is_latin1(unicode, length);
 537   Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
 538   typeArrayOop buffer = value(h_obj());
 539   assert(TypeArrayKlass::cast(buffer->klass())->element_type() == T_BYTE, "only byte[]");
 540   if (is_latin1) {
 541     for (int index = 0; index < length; index++) {
 542       buffer->byte_at_put(index, (jbyte)unicode[index]);
 543     }
 544   } else {
 545     for (int index = 0; index < length; index++) {
 546       buffer->char_at_put(index, unicode[index]);
 547     }
 548   }
 549 
 550 #ifdef ASSERT
 551   {
 552     ResourceMark rm;
 553     char* expected = UNICODE::as_utf8(unicode, length);
 554     char* actual = as_utf8_string(h_obj());
 555     if (strcmp(expected, actual) != 0) {
 556       fatal("Unicode conversion failure: %s --> %s", expected, actual);
 557     }
 558   }
 559 #endif
 560 
 561   return h_obj;
 562 }
 563 
 564 oop java_lang_String::create_oop_from_unicode(const jchar* unicode, int length, TRAPS) {
 565   Handle h_obj = create_from_unicode(unicode, length, CHECK_NULL);
 566   return h_obj();
 567 }
 568 
 569 Handle java_lang_String::create_from_str(const char* utf8_str, TRAPS) {
 570   if (utf8_str == NULL) {
 571     return Handle();
 572   }
 573   bool has_multibyte, is_latin1;
 574   int length = UTF8::unicode_length(utf8_str, is_latin1, has_multibyte);
 575   if (!CompactStrings) {
 576     has_multibyte = true;
 577     is_latin1 = false;
 578   }
 579 
 580   Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
 581   if (length > 0) {
 582     if (!has_multibyte) {
 583       const jbyte* src = reinterpret_cast<const jbyte*>(utf8_str);
 584       ArrayAccess<>::arraycopy_from_native(src, value(h_obj()), typeArrayOopDesc::element_offset<jbyte>(0), length);
 585     } else if (is_latin1) {
 586       UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length);
 587     } else {
 588       UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length);
 589     }
 590   }
 591 
 592 #ifdef ASSERT
 593   // This check is too strict when the input string is not a valid UTF8.
 594   // For example, it may be created with arbitrary content via jni_NewStringUTF.
 595   if (UTF8::is_legal_utf8((const unsigned char*)utf8_str, (int)strlen(utf8_str), false)) {
 596     ResourceMark rm;
 597     const char* expected = utf8_str;
 598     char* actual = as_utf8_string(h_obj());
 599     if (strcmp(expected, actual) != 0) {
 600       fatal("String conversion failure: %s --> %s", expected, actual);
 601     }
 602   }
 603 #endif
 604 
 605   return h_obj;
 606 }
 607 
 608 oop java_lang_String::create_oop_from_str(const char* utf8_str, TRAPS) {
 609   Handle h_obj = create_from_str(utf8_str, CHECK_NULL);
 610   return h_obj();
 611 }
 612 
 613 Handle java_lang_String::create_from_symbol(Symbol* symbol, TRAPS) {
 614   const char* utf8_str = (char*)symbol->bytes();
 615   int utf8_len = symbol->utf8_length();
 616 
 617   bool has_multibyte, is_latin1;
 618   int length = UTF8::unicode_length(utf8_str, utf8_len, is_latin1, has_multibyte);
 619   if (!CompactStrings) {
 620     has_multibyte = true;
 621     is_latin1 = false;
 622   }
 623 
 624   Handle h_obj = basic_create(length, is_latin1, CHECK_NH);
 625   if (length > 0) {
 626     if (!has_multibyte) {
 627       const jbyte* src = reinterpret_cast<const jbyte*>(utf8_str);
 628       ArrayAccess<>::arraycopy_from_native(src, value(h_obj()), typeArrayOopDesc::element_offset<jbyte>(0), length);
 629     } else if (is_latin1) {
 630       UTF8::convert_to_unicode(utf8_str, value(h_obj())->byte_at_addr(0), length);
 631     } else {
 632       UTF8::convert_to_unicode(utf8_str, value(h_obj())->char_at_addr(0), length);
 633     }
 634   }
 635 
 636 #ifdef ASSERT
 637   {
 638     ResourceMark rm;
 639     const char* expected = symbol->as_utf8();
 640     char* actual = as_utf8_string(h_obj());
 641     if (strncmp(expected, actual, utf8_len) != 0) {
 642       fatal("Symbol conversion failure: %s --> %s", expected, actual);
 643     }
 644   }
 645 #endif
 646 
 647   return h_obj;
 648 }
 649 
 650 // Converts a C string to a Java String based on current encoding
 651 Handle java_lang_String::create_from_platform_dependent_str(const char* str, TRAPS) {
 652   assert(str != NULL, "bad arguments");
 653 
 654   typedef jstring (JNICALL *to_java_string_fn_t)(JNIEnv*, const char *);
 655   static to_java_string_fn_t _to_java_string_fn = NULL;
 656 
 657   if (_to_java_string_fn == NULL) {
 658     void *lib_handle = os::native_java_library();
 659     _to_java_string_fn = CAST_TO_FN_PTR(to_java_string_fn_t, os::dll_lookup(lib_handle, "JNU_NewStringPlatform"));
 660 #if defined(_WIN32) && !defined(_WIN64)
 661     if (_to_java_string_fn == NULL) {
 662       // On 32 bit Windows, also try __stdcall decorated name
 663       _to_java_string_fn = CAST_TO_FN_PTR(to_java_string_fn_t, os::dll_lookup(lib_handle, "_JNU_NewStringPlatform@8"));
 664     }
 665 #endif
 666     if (_to_java_string_fn == NULL) {
 667       fatal("JNU_NewStringPlatform missing");
 668     }
 669   }
 670 
 671   jstring js = NULL;
 672   {
 673     JavaThread* thread = THREAD;
 674     HandleMark hm(thread);
 675     ThreadToNativeFromVM ttn(thread);
 676     js = (_to_java_string_fn)(thread->jni_environment(), str);
 677   }
 678 
 679   Handle native_platform_string(THREAD, JNIHandles::resolve(js));
 680   JNIHandles::destroy_local(js);  // destroy local JNIHandle.
 681   return native_platform_string;
 682 }
 683 
 684 // Converts a Java String to a native C string that can be used for
 685 // native OS calls.
 686 char* java_lang_String::as_platform_dependent_str(Handle java_string, TRAPS) {
 687   typedef char* (*to_platform_string_fn_t)(JNIEnv*, jstring, bool*);
 688   static to_platform_string_fn_t _to_platform_string_fn = NULL;
 689 
 690   if (_to_platform_string_fn == NULL) {
 691     void *lib_handle = os::native_java_library();
 692     _to_platform_string_fn = CAST_TO_FN_PTR(to_platform_string_fn_t, os::dll_lookup(lib_handle, "GetStringPlatformChars"));
 693     if (_to_platform_string_fn == NULL) {
 694       fatal("GetStringPlatformChars missing");
 695     }
 696   }
 697 
 698   char *native_platform_string;
 699   { JavaThread* thread = THREAD;
 700     jstring js = (jstring) JNIHandles::make_local(thread, java_string());
 701     bool is_copy;
 702     HandleMark hm(thread);
 703     ThreadToNativeFromVM ttn(thread);
 704     JNIEnv *env = thread->jni_environment();
 705     native_platform_string = (_to_platform_string_fn)(env, js, &is_copy);
 706     assert(is_copy == JNI_TRUE, "is_copy value changed");
 707     JNIHandles::destroy_local(js);
 708   }
 709   return native_platform_string;
 710 }
 711 
 712 Handle java_lang_String::externalize_classname(Symbol* java_name, TRAPS) {
 713   ResourceMark rm(THREAD);
 714   return create_from_str(java_name->as_klass_external_name(), THREAD);
 715 }
 716 
 717 jchar* java_lang_String::as_unicode_string(oop java_string, int& length, TRAPS) {
 718   jchar* result = as_unicode_string_or_null(java_string, length);
 719   if (result == NULL) {
 720     THROW_MSG_0(vmSymbols::java_lang_OutOfMemoryError(), "could not allocate Unicode string");
 721   }
 722   return result;
 723 }
 724 
 725 jchar* java_lang_String::as_unicode_string_or_null(oop java_string, int& length) {
 726   typeArrayOop value  = java_lang_String::value(java_string);
 727                length = java_lang_String::length(java_string, value);
 728   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 729 
 730   jchar* result = NEW_RESOURCE_ARRAY_RETURN_NULL(jchar, length);
 731   if (result != NULL) {
 732     if (!is_latin1) {
 733       for (int index = 0; index < length; index++) {
 734         result[index] = value->char_at(index);
 735       }
 736     } else {
 737       for (int index = 0; index < length; index++) {
 738         result[index] = ((jchar) value->byte_at(index)) & 0xff;
 739       }
 740     }
 741   }
 742   return result;
 743 }
 744 
 745 inline unsigned int java_lang_String::hash_code_impl(oop java_string, bool update) {
 746   // The hash and hashIsZero fields are subject to a benign data race,
 747   // making it crucial to ensure that any observable result of the
 748   // calculation in this method stays correct under any possible read of
 749   // these fields. Necessary restrictions to allow this to be correct
 750   // without explicit memory fences or similar concurrency primitives is
 751   // that we can ever only write to one of these two fields for a given
 752   // String instance, and that the computation is idempotent and derived
 753   // from immutable state
 754   assert(_initialized && (_hash_offset > 0) && (_hashIsZero_offset > 0), "Must be initialized");
 755   if (java_lang_String::hash_is_set(java_string)) {
 756     return java_string->int_field(_hash_offset);
 757   }
 758 
 759   typeArrayOop value = java_lang_String::value(java_string);
 760   int         length = java_lang_String::length(java_string, value);
 761   bool     is_latin1 = java_lang_String::is_latin1(java_string);
 762 
 763   unsigned int hash = 0;
 764   if (length > 0) {
 765     if (is_latin1) {
 766       hash = java_lang_String::hash_code(value->byte_at_addr(0), length);
 767     } else {
 768       hash = java_lang_String::hash_code(value->char_at_addr(0), length);
 769     }
 770   }
 771 
 772   if (update) {
 773     if (hash != 0) {
 774       java_string->int_field_put(_hash_offset, hash);
 775     } else {
 776       java_string->bool_field_put(_hashIsZero_offset, true);
 777     }
 778   }
 779   return hash;
 780 }
 781 
 782 unsigned int java_lang_String::hash_code(oop java_string) {
 783   return hash_code_impl(java_string, /*update=*/true);
 784 }
 785 
 786 unsigned int java_lang_String::hash_code_noupdate(oop java_string) {
 787   return hash_code_impl(java_string, /*update=*/false);
 788 }
 789 
 790 
 791 char* java_lang_String::as_quoted_ascii(oop java_string) {
 792   typeArrayOop value  = java_lang_String::value(java_string);
 793   int          length = java_lang_String::length(java_string, value);
 794   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 795 
 796   if (length == 0) return NULL;
 797 
 798   char* result;
 799   int result_length;
 800   if (!is_latin1) {
 801     jchar* base = value->char_at_addr(0);
 802     result_length = UNICODE::quoted_ascii_length(base, length) + 1;
 803     result = NEW_RESOURCE_ARRAY(char, result_length);
 804     UNICODE::as_quoted_ascii(base, length, result, result_length);
 805   } else {
 806     jbyte* base = value->byte_at_addr(0);
 807     result_length = UNICODE::quoted_ascii_length(base, length) + 1;
 808     result = NEW_RESOURCE_ARRAY(char, result_length);
 809     UNICODE::as_quoted_ascii(base, length, result, result_length);
 810   }
 811   assert(result_length >= length + 1, "must not be shorter");
 812   assert(result_length == (int)strlen(result) + 1, "must match");
 813   return result;
 814 }
 815 
 816 Symbol* java_lang_String::as_symbol(oop java_string) {
 817   typeArrayOop value  = java_lang_String::value(java_string);
 818   int          length = java_lang_String::length(java_string, value);
 819   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 820   if (!is_latin1) {
 821     jchar* base = (length == 0) ? NULL : value->char_at_addr(0);
 822     Symbol* sym = SymbolTable::new_symbol(base, length);
 823     return sym;
 824   } else {
 825     ResourceMark rm;
 826     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 827     const char* base = UNICODE::as_utf8(position, length);
 828     Symbol* sym = SymbolTable::new_symbol(base, length);
 829     return sym;
 830   }
 831 }
 832 
 833 Symbol* java_lang_String::as_symbol_or_null(oop java_string) {
 834   typeArrayOop value  = java_lang_String::value(java_string);
 835   int          length = java_lang_String::length(java_string, value);
 836   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 837   if (!is_latin1) {
 838     jchar* base = (length == 0) ? NULL : value->char_at_addr(0);
 839     return SymbolTable::probe_unicode(base, length);
 840   } else {
 841     ResourceMark rm;
 842     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 843     const char* base = UNICODE::as_utf8(position, length);
 844     return SymbolTable::probe(base, length);
 845   }
 846 }
 847 
 848 int java_lang_String::utf8_length(oop java_string, typeArrayOop value) {
 849   assert(value_equals(value, java_lang_String::value(java_string)),
 850          "value must be same as java_lang_String::value(java_string)");
 851   int length = java_lang_String::length(java_string, value);
 852   if (length == 0) {
 853     return 0;
 854   }
 855   if (!java_lang_String::is_latin1(java_string)) {
 856     return UNICODE::utf8_length(value->char_at_addr(0), length);
 857   } else {
 858     return UNICODE::utf8_length(value->byte_at_addr(0), length);
 859   }
 860 }
 861 
 862 int java_lang_String::utf8_length(oop java_string) {
 863   typeArrayOop value = java_lang_String::value(java_string);
 864   return utf8_length(java_string, value);
 865 }
 866 
 867 char* java_lang_String::as_utf8_string(oop java_string) {
 868   int length;
 869   return as_utf8_string(java_string, length);
 870 }
 871 
 872 char* java_lang_String::as_utf8_string(oop java_string, int& length) {
 873   typeArrayOop value = java_lang_String::value(java_string);
 874   length             = java_lang_String::length(java_string, value);
 875   bool     is_latin1 = java_lang_String::is_latin1(java_string);
 876   if (!is_latin1) {
 877     jchar* position = (length == 0) ? NULL : value->char_at_addr(0);
 878     return UNICODE::as_utf8(position, length);
 879   } else {
 880     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 881     return UNICODE::as_utf8(position, length);
 882   }
 883 }
 884 
 885 // Uses a provided buffer if it's sufficiently large, otherwise allocates
 886 // a resource array to fit
 887 char* java_lang_String::as_utf8_string_full(oop java_string, char* buf, int buflen, int& utf8_len) {
 888   typeArrayOop value = java_lang_String::value(java_string);
 889   int            len = java_lang_String::length(java_string, value);
 890   bool     is_latin1 = java_lang_String::is_latin1(java_string);
 891   if (!is_latin1) {
 892     jchar *position = (len == 0) ? NULL : value->char_at_addr(0);
 893     utf8_len = UNICODE::utf8_length(position, len);
 894     if (utf8_len >= buflen) {
 895       buf = NEW_RESOURCE_ARRAY(char, utf8_len + 1);
 896     }
 897     return UNICODE::as_utf8(position, len, buf, utf8_len + 1);
 898   } else {
 899     jbyte *position = (len == 0) ? NULL : value->byte_at_addr(0);
 900     utf8_len = UNICODE::utf8_length(position, len);
 901     if (utf8_len >= buflen) {
 902       buf = NEW_RESOURCE_ARRAY(char, utf8_len + 1);
 903     }
 904     return UNICODE::as_utf8(position, len, buf, utf8_len + 1);
 905   }
 906 }
 907 
 908 char* java_lang_String::as_utf8_string(oop java_string, typeArrayOop value, char* buf, int buflen) {
 909   assert(value_equals(value, java_lang_String::value(java_string)),
 910          "value must be same as java_lang_String::value(java_string)");
 911   int     length = java_lang_String::length(java_string, value);
 912   bool is_latin1 = java_lang_String::is_latin1(java_string);
 913   if (!is_latin1) {
 914     jchar* position = (length == 0) ? NULL : value->char_at_addr(0);
 915     return UNICODE::as_utf8(position, length, buf, buflen);
 916   } else {
 917     jbyte* position = (length == 0) ? NULL : value->byte_at_addr(0);
 918     return UNICODE::as_utf8(position, length, buf, buflen);
 919   }
 920 }
 921 
 922 char* java_lang_String::as_utf8_string(oop java_string, char* buf, int buflen) {
 923   typeArrayOop value = java_lang_String::value(java_string);
 924   return as_utf8_string(java_string, value, buf, buflen);
 925 }
 926 
 927 char* java_lang_String::as_utf8_string(oop java_string, int start, int len) {
 928   typeArrayOop value  = java_lang_String::value(java_string);
 929   bool      is_latin1 = java_lang_String::is_latin1(java_string);
 930   assert(start + len <= java_lang_String::length(java_string), "just checking");
 931   if (!is_latin1) {
 932     jchar* position = value->char_at_addr(start);
 933     return UNICODE::as_utf8(position, len);
 934   } else {
 935     jbyte* position = value->byte_at_addr(start);
 936     return UNICODE::as_utf8(position, len);
 937   }
 938 }
 939 
 940 char* java_lang_String::as_utf8_string(oop java_string, typeArrayOop value, int start, int len, char* buf, int buflen) {
 941   assert(value_equals(value, java_lang_String::value(java_string)),
 942          "value must be same as java_lang_String::value(java_string)");
 943   assert(start + len <= java_lang_String::length(java_string), "just checking");
 944   bool is_latin1 = java_lang_String::is_latin1(java_string);
 945   if (!is_latin1) {
 946     jchar* position = value->char_at_addr(start);
 947     return UNICODE::as_utf8(position, len, buf, buflen);
 948   } else {
 949     jbyte* position = value->byte_at_addr(start);
 950     return UNICODE::as_utf8(position, len, buf, buflen);
 951   }
 952 }
 953 
 954 bool java_lang_String::equals(oop java_string, const jchar* chars, int len) {
 955   assert(java_string->klass() == vmClasses::String_klass(),
 956          "must be java_string");
 957   typeArrayOop value = java_lang_String::value_no_keepalive(java_string);
 958   int length = java_lang_String::length(java_string, value);
 959   if (length != len) {
 960     return false;
 961   }
 962   bool is_latin1 = java_lang_String::is_latin1(java_string);
 963   if (!is_latin1) {
 964     for (int i = 0; i < len; i++) {
 965       if (value->char_at(i) != chars[i]) {
 966         return false;
 967       }
 968     }
 969   } else {
 970     for (int i = 0; i < len; i++) {
 971       if ((((jchar) value->byte_at(i)) & 0xff) != chars[i]) {
 972         return false;
 973       }
 974     }
 975   }
 976   return true;
 977 }
 978 
 979 bool java_lang_String::equals(oop str1, oop str2) {
 980   assert(str1->klass() == vmClasses::String_klass(),
 981          "must be java String");
 982   assert(str2->klass() == vmClasses::String_klass(),
 983          "must be java String");
 984   typeArrayOop value1    = java_lang_String::value_no_keepalive(str1);
 985   bool         is_latin1 = java_lang_String::is_latin1(str1);
 986   typeArrayOop value2    = java_lang_String::value_no_keepalive(str2);
 987   bool         is_latin2 = java_lang_String::is_latin1(str2);
 988 
 989   if (is_latin1 != is_latin2) {
 990     // Strings with different coders are never equal.
 991     return false;
 992   }
 993   return value_equals(value1, value2);
 994 }
 995 
 996 void java_lang_String::print(oop java_string, outputStream* st) {
 997   assert(java_string->klass() == vmClasses::String_klass(), "must be java_string");
 998   typeArrayOop value  = java_lang_String::value_no_keepalive(java_string);
 999 
1000   if (value == NULL) {
1001     // This can happen if, e.g., printing a String
1002     // object before its initializer has been called
1003     st->print("NULL");
1004     return;
1005   }
1006 
1007   int length = java_lang_String::length(java_string, value);
1008   bool is_latin1 = java_lang_String::is_latin1(java_string);
1009 
1010   st->print("\"");
1011   for (int index = 0; index < length; index++) {
1012     st->print("%c", (!is_latin1) ?  value->char_at(index) :
1013                            ((jchar) value->byte_at(index)) & 0xff );
1014   }
1015   st->print("\"");
1016 }
1017 
1018 // java_lang_Class
1019 
1020 int java_lang_Class::_klass_offset;
1021 int java_lang_Class::_array_klass_offset;
1022 int java_lang_Class::_oop_size_offset;
1023 int java_lang_Class::_static_oop_field_count_offset;
1024 int java_lang_Class::_class_loader_offset;
1025 int java_lang_Class::_module_offset;
1026 int java_lang_Class::_protection_domain_offset;
1027 int java_lang_Class::_component_mirror_offset;
1028 int java_lang_Class::_init_lock_offset;
1029 int java_lang_Class::_signers_offset;
1030 int java_lang_Class::_name_offset;
1031 int java_lang_Class::_source_file_offset;
1032 int java_lang_Class::_classData_offset;
1033 int java_lang_Class::_classRedefinedCount_offset;
1034 
1035 bool java_lang_Class::_offsets_computed = false;
1036 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL;
1037 GrowableArray<Klass*>* java_lang_Class::_fixup_module_field_list = NULL;
1038 
1039 #ifdef ASSERT
1040 inline static void assert_valid_static_string_field(fieldDescriptor* fd) {
1041   assert(fd->has_initial_value(), "caller should have checked this");
1042   assert(fd->field_type() == T_OBJECT, "caller should have checked this");
1043   // Can't use vmSymbols::string_signature() as fd->signature() may have been relocated
1044   // during DumpSharedSpaces
1045   assert(fd->signature()->equals("Ljava/lang/String;"), "just checking");
1046 }
1047 #endif
1048 
1049 static void initialize_static_string_field(fieldDescriptor* fd, Handle mirror, TRAPS) {
1050   DEBUG_ONLY(assert_valid_static_string_field(fd);)
1051   oop string = fd->string_initial_value(CHECK);
1052   mirror()->obj_field_put(fd->offset(), string);
1053 }
1054 
1055 #if INCLUDE_CDS_JAVA_HEAP
1056 static void initialize_static_string_field_for_dump(fieldDescriptor* fd, Handle mirror) {
1057   DEBUG_ONLY(assert_valid_static_string_field(fd);)
1058   assert(DumpSharedSpaces, "must be");
1059   assert(HeapShared::is_archived_object_during_dumptime(mirror()), "must be");
1060   // Archive the String field and update the pointer.
1061   oop s = mirror()->obj_field(fd->offset());
1062   oop archived_s = StringTable::create_archived_string(s);
1063   mirror()->obj_field_put(fd->offset(), archived_s);
1064 }
1065 #endif
1066 
1067 static void initialize_static_primitive_field(fieldDescriptor* fd, Handle mirror) {
1068   assert(fd->has_initial_value(), "caller should have checked this");
1069   BasicType t = fd->field_type();
1070   switch (t) {
1071   case T_BYTE:
1072     mirror()->byte_field_put(fd->offset(), fd->int_initial_value());
1073     break;
1074   case T_BOOLEAN:
1075     mirror()->bool_field_put(fd->offset(), fd->int_initial_value());
1076     break;
1077   case T_CHAR:
1078     mirror()->char_field_put(fd->offset(), fd->int_initial_value());
1079     break;
1080   case T_SHORT:
1081     mirror()->short_field_put(fd->offset(), fd->int_initial_value());
1082     break;
1083   case T_INT:
1084     mirror()->int_field_put(fd->offset(), fd->int_initial_value());
1085     break;
1086   case T_FLOAT:
1087     mirror()->float_field_put(fd->offset(), fd->float_initial_value());
1088     break;
1089   case T_DOUBLE:
1090     mirror()->double_field_put(fd->offset(), fd->double_initial_value());
1091     break;
1092   case T_LONG:
1093     mirror()->long_field_put(fd->offset(), fd->long_initial_value());
1094     break;
1095   default:
1096     // Illegal ConstantValue attribute in class file should have been
1097     // caught during classfile parsing.
1098     ShouldNotReachHere();
1099   }
1100 }
1101 
1102 static void initialize_static_field(fieldDescriptor* fd, Handle mirror, TRAPS) {
1103   assert(mirror.not_null() && fd->is_static(), "just checking");
1104   if (fd->has_initial_value()) {
1105     if (fd->field_type() != T_OBJECT) {
1106       initialize_static_primitive_field(fd, mirror);
1107     } else {
1108       initialize_static_string_field(fd, mirror, CHECK);
1109     }
1110   }
1111 }
1112 
1113 #if INCLUDE_CDS_JAVA_HEAP
1114 static void initialize_static_field_for_dump(fieldDescriptor* fd, Handle mirror) {
1115   assert(mirror.not_null() && fd->is_static(), "just checking");
1116   if (fd->has_initial_value()) {
1117     if (fd->field_type() != T_OBJECT) {
1118       initialize_static_primitive_field(fd, mirror);
1119     } else {
1120       initialize_static_string_field_for_dump(fd, mirror);
1121     }
1122   }
1123 }
1124 #endif
1125 
1126 void java_lang_Class::fixup_mirror(Klass* k, TRAPS) {
1127   assert(InstanceMirrorKlass::offset_of_static_fields() != 0, "must have been computed already");
1128 
1129   // If the offset was read from the shared archive, it was fixed up already
1130   if (!k->is_shared()) {
1131     if (k->is_instance_klass()) {
1132       // During bootstrap, java.lang.Class wasn't loaded so static field
1133       // offsets were computed without the size added it.  Go back and
1134       // update all the static field offsets to included the size.
1135       for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) {
1136         if (fs.access_flags().is_static()) {
1137           int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields();
1138           fs.set_offset(real_offset);
1139         }
1140       }
1141     }
1142   }
1143 
1144   if (k->is_shared() && k->has_archived_mirror_index()) {
1145     if (HeapShared::are_archived_mirrors_available()) {
1146       bool present = restore_archived_mirror(k, Handle(), Handle(), Handle(), CHECK);
1147       assert(present, "Missing archived mirror for %s", k->external_name());
1148       return;
1149     } else {
1150       k->clear_java_mirror_handle();
1151       k->clear_archived_mirror_index();
1152     }
1153   }
1154   create_mirror(k, Handle(), Handle(), Handle(), Handle(), CHECK);
1155 }
1156 
1157 void java_lang_Class::initialize_mirror_fields(Klass* k,
1158                                                Handle mirror,
1159                                                Handle protection_domain,
1160                                                Handle classData,
1161                                                TRAPS) {
1162   // Allocate a simple java object for a lock.
1163   // This needs to be a java object because during class initialization
1164   // it can be held across a java call.
1165   typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
1166   set_init_lock(mirror(), r);
1167 
1168   // Set protection domain also
1169   set_protection_domain(mirror(), protection_domain());
1170 
1171   // Initialize static fields
1172   InstanceKlass::cast(k)->do_local_static_fields(&initialize_static_field, mirror, CHECK);
1173 
1174  // Set classData
1175   set_class_data(mirror(), classData());
1176 }
1177 
1178 // Set the java.lang.Module module field in the java_lang_Class mirror
1179 void java_lang_Class::set_mirror_module_field(JavaThread* current, Klass* k, Handle mirror, Handle module) {
1180   if (module.is_null()) {
1181     // During startup, the module may be NULL only if java.base has not been defined yet.
1182     // Put the class on the fixup_module_list to patch later when the java.lang.Module
1183     // for java.base is known. But note that since we captured the NULL module another
1184     // thread may have completed that initialization.
1185 
1186     bool javabase_was_defined = false;
1187     {
1188       MutexLocker m1(current, Module_lock);
1189       // Keep list of classes needing java.base module fixup
1190       if (!ModuleEntryTable::javabase_defined()) {
1191         assert(k->java_mirror() != NULL, "Class's mirror is null");
1192         k->class_loader_data()->inc_keep_alive();
1193         assert(fixup_module_field_list() != NULL, "fixup_module_field_list not initialized");
1194         fixup_module_field_list()->push(k);
1195       } else {
1196         javabase_was_defined = true;
1197       }
1198     }
1199 
1200     // If java.base was already defined then patch this particular class with java.base.
1201     if (javabase_was_defined) {
1202       ModuleEntry *javabase_entry = ModuleEntryTable::javabase_moduleEntry();
1203       assert(javabase_entry != NULL && javabase_entry->module() != NULL,
1204              "Setting class module field, " JAVA_BASE_NAME " should be defined");
1205       Handle javabase_handle(current, javabase_entry->module());
1206       set_module(mirror(), javabase_handle());
1207     }
1208   } else {
1209     assert(Universe::is_module_initialized() ||
1210            (ModuleEntryTable::javabase_defined() &&
1211             (module() == ModuleEntryTable::javabase_moduleEntry()->module())),
1212            "Incorrect java.lang.Module specification while creating mirror");
1213     set_module(mirror(), module());
1214   }
1215 }
1216 
1217 // Statically allocate fixup lists because they always get created.
1218 void java_lang_Class::allocate_fixup_lists() {
1219   GrowableArray<Klass*>* mirror_list =
1220     new (ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(40, mtClass);
1221   set_fixup_mirror_list(mirror_list);
1222 
1223   GrowableArray<Klass*>* module_list =
1224     new (ResourceObj::C_HEAP, mtModule) GrowableArray<Klass*>(500, mtModule);
1225   set_fixup_module_field_list(module_list);
1226 }
1227 
1228 void java_lang_Class::create_mirror(Klass* k, Handle class_loader,
1229                                     Handle module, Handle protection_domain,
1230                                     Handle classData, TRAPS) {
1231   assert(k != NULL, "Use create_basic_type_mirror for primitive types");
1232   assert(k->java_mirror() == NULL, "should only assign mirror once");
1233 
1234   // Use this moment of initialization to cache modifier_flags also,
1235   // to support Class.getModifiers().  Instance classes recalculate
1236   // the cached flags after the class file is parsed, but before the
1237   // class is put into the system dictionary.
1238   int computed_modifiers = k->compute_modifier_flags();
1239   k->set_modifier_flags(computed_modifiers);
1240   // Class_klass has to be loaded because it is used to allocate
1241   // the mirror.
1242   if (vmClasses::Class_klass_loaded()) {
1243     // Allocate mirror (java.lang.Class instance)
1244     oop mirror_oop = InstanceMirrorKlass::cast(vmClasses::Class_klass())->allocate_instance(k, CHECK);
1245     Handle mirror(THREAD, mirror_oop);
1246     Handle comp_mirror;
1247 
1248     // Setup indirection from mirror->klass
1249     set_klass(mirror(), k);
1250 
1251     InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
1252     assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");
1253 
1254     set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));
1255 
1256     // It might also have a component mirror.  This mirror must already exist.
1257     if (k->is_array_klass()) {
1258       if (k->is_typeArray_klass()) {
1259         BasicType type = TypeArrayKlass::cast(k)->element_type();
1260         comp_mirror = Handle(THREAD, Universe::java_mirror(type));
1261       } else {
1262         assert(k->is_objArray_klass(), "Must be");
1263         Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
1264         assert(element_klass != NULL, "Must have an element klass");
1265         comp_mirror = Handle(THREAD, element_klass->java_mirror());
1266       }
1267       assert(comp_mirror() != NULL, "must have a mirror");
1268 
1269       // Two-way link between the array klass and its component mirror:
1270       // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
1271       set_component_mirror(mirror(), comp_mirror());
1272       // See below for ordering dependencies between field array_klass in component mirror
1273       // and java_mirror in this klass.
1274     } else {
1275       assert(k->is_instance_klass(), "Must be");
1276 
1277       initialize_mirror_fields(k, mirror, protection_domain, classData, THREAD);
1278       if (HAS_PENDING_EXCEPTION) {
1279         // If any of the fields throws an exception like OOM remove the klass field
1280         // from the mirror so GC doesn't follow it after the klass has been deallocated.
1281         // This mirror looks like a primitive type, which logically it is because it
1282         // it represents no class.
1283         set_klass(mirror(), NULL);
1284         return;
1285       }
1286     }
1287 
1288     // set the classLoader field in the java_lang_Class instance
1289     assert(class_loader() == k->class_loader(), "should be same");
1290     set_class_loader(mirror(), class_loader());
1291 
1292     // Setup indirection from klass->mirror
1293     // after any exceptions can happen during allocations.
1294     k->set_java_mirror(mirror);
1295 
1296     // Set the module field in the java_lang_Class instance.  This must be done
1297     // after the mirror is set.
1298     set_mirror_module_field(THREAD, k, mirror, module);
1299 
1300     if (comp_mirror() != NULL) {
1301       // Set after k->java_mirror() is published, because compiled code running
1302       // concurrently doesn't expect a k to have a null java_mirror.
1303       release_set_array_klass(comp_mirror(), k);
1304     }
1305   } else {
1306     assert(fixup_mirror_list() != NULL, "fixup_mirror_list not initialized");
1307     fixup_mirror_list()->push(k);
1308   }
1309 }
1310 
1311 #if INCLUDE_CDS_JAVA_HEAP
1312 // Clears mirror fields. Static final fields with initial values are reloaded
1313 // from constant pool. The object identity hash is in the object header and is
1314 // not affected.
1315 class ResetMirrorField: public FieldClosure {
1316  private:
1317   Handle _m;
1318 
1319  public:
1320   ResetMirrorField(Handle mirror) : _m(mirror) {}
1321 
1322   void do_field(fieldDescriptor* fd) {
1323     assert(DumpSharedSpaces, "dump time only");
1324     assert(_m.not_null(), "Mirror cannot be NULL");
1325 
1326     if (fd->is_static() && fd->has_initial_value()) {
1327       initialize_static_field_for_dump(fd, _m);
1328       return;
1329     }
1330 
1331     BasicType ft = fd->field_type();
1332     switch (ft) {
1333       case T_BYTE:
1334         _m()->byte_field_put(fd->offset(), 0);
1335         break;
1336       case T_CHAR:
1337         _m()->char_field_put(fd->offset(), 0);
1338         break;
1339       case T_DOUBLE:
1340         _m()->double_field_put(fd->offset(), 0);
1341         break;
1342       case T_FLOAT:
1343         _m()->float_field_put(fd->offset(), 0);
1344         break;
1345       case T_INT:
1346         _m()->int_field_put(fd->offset(), 0);
1347         break;
1348       case T_LONG:
1349         _m()->long_field_put(fd->offset(), 0);
1350         break;
1351       case T_SHORT:
1352         _m()->short_field_put(fd->offset(), 0);
1353         break;
1354       case T_BOOLEAN:
1355         _m()->bool_field_put(fd->offset(), false);
1356         break;
1357       case T_ARRAY:
1358       case T_OBJECT: {
1359         // It might be useful to cache the String field, but
1360         // for now just clear out any reference field
1361         oop o = _m()->obj_field(fd->offset());
1362         _m()->obj_field_put(fd->offset(), NULL);
1363         break;
1364       }
1365       default:
1366         ShouldNotReachHere();
1367         break;
1368      }
1369   }
1370 };
1371 
1372 static void set_klass_field_in_archived_mirror(oop mirror_obj, int offset, Klass* k) {
1373   assert(java_lang_Class::is_instance(mirror_obj), "must be");
1374   // this is the copy of k in the output buffer
1375   Klass* copy = ArchiveBuilder::get_relocated_klass(k);
1376 
1377   // This is the address of k, if the archive is loaded at the requested location
1378   Klass* def = ArchiveBuilder::current()->to_requested(copy);
1379 
1380   log_debug(cds, heap, mirror)(
1381       "Relocate mirror metadata field at %d from " PTR_FORMAT " ==> " PTR_FORMAT,
1382       offset, p2i(k), p2i(def));
1383 
1384   mirror_obj->metadata_field_put(offset, def);
1385 }
1386 
1387 void java_lang_Class::archive_basic_type_mirrors() {
1388   assert(HeapShared::can_write(), "must be");
1389 
1390   for (int t = T_BOOLEAN; t < T_VOID+1; t++) {
1391     BasicType bt = (BasicType)t;
1392     oop m = Universe::_mirrors[t].resolve();
1393     if (m != NULL) {
1394       // Update the field at _array_klass_offset to point to the relocated array klass.
1395       oop archived_m = HeapShared::archive_object(m);
1396       assert(archived_m != NULL, "sanity");
1397       Klass *ak = (Klass*)(archived_m->metadata_field(_array_klass_offset));
1398       assert(ak != NULL || t == T_VOID, "should not be NULL");
1399       if (ak != NULL) {
1400         set_klass_field_in_archived_mirror(archived_m, _array_klass_offset, ak);
1401       }
1402 
1403       // Clear the fields. Just to be safe
1404       Klass *k = m->klass();
1405       Handle archived_mirror_h(Thread::current(), archived_m);
1406       ResetMirrorField reset(archived_mirror_h);
1407       InstanceKlass::cast(k)->do_nonstatic_fields(&reset);
1408 
1409       log_trace(cds, heap, mirror)(
1410         "Archived %s mirror object from " PTR_FORMAT " ==> " PTR_FORMAT,
1411         type2name(bt), p2i(m), p2i(archived_m));
1412 
1413       Universe::replace_mirror(bt, archived_m);
1414     }
1415   }
1416 }
1417 //
1418 // After the mirror object is successfully archived, the archived
1419 // klass is set with _has_archived_raw_mirror flag.
1420 //
1421 // The _has_archived_raw_mirror flag is cleared at runtime when the
1422 // archived mirror is restored. If archived java heap data cannot
1423 // be used at runtime, new mirror object is created for the shared
1424 // class. The _has_archived_raw_mirror is cleared also during the process.
1425 oop java_lang_Class::archive_mirror(Klass* k) {
1426   assert(HeapShared::can_write(), "must be");
1427 
1428   // Mirror is already archived
1429   if (k->has_archived_mirror_index()) {
1430     assert(k->archived_java_mirror() != NULL, "no archived mirror");
1431     return k->archived_java_mirror();
1432   }
1433 
1434   // No mirror
1435   oop mirror = k->java_mirror();
1436   if (mirror == NULL) {
1437     return NULL;
1438   }
1439 
1440   if (k->is_instance_klass()) {
1441     InstanceKlass *ik = InstanceKlass::cast(k);
1442     assert(ik->signers() == NULL, "class with signer should have been excluded");
1443 
1444     if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
1445           ik->is_shared_app_class())) {
1446       // Archiving mirror for classes from non-builtin loaders is not
1447       // supported.
1448       return NULL;
1449     }
1450   }
1451 
1452   // Now start archiving the mirror object
1453   oop archived_mirror = HeapShared::archive_object(mirror);
1454   if (archived_mirror == NULL) {
1455     return NULL;
1456   }
1457 
1458   archived_mirror = process_archived_mirror(k, mirror, archived_mirror);
1459   if (archived_mirror == NULL) {
1460     return NULL;
1461   }
1462 
1463   k->set_archived_java_mirror(archived_mirror);
1464 
1465   ResourceMark rm;
1466   log_trace(cds, heap, mirror)(
1467     "Archived %s mirror object from " PTR_FORMAT " ==> " PTR_FORMAT,
1468     k->external_name(), p2i(mirror), p2i(archived_mirror));
1469 
1470   return archived_mirror;
1471 }
1472 
1473 // The process is based on create_mirror().
1474 oop java_lang_Class::process_archived_mirror(Klass* k, oop mirror,
1475                                              oop archived_mirror) {
1476   // Clear nonstatic fields in archived mirror. Some of the fields will be set
1477   // to archived metadata and objects below.
1478   Klass *c = archived_mirror->klass();
1479   Handle archived_mirror_h(Thread::current(), archived_mirror);
1480   ResetMirrorField reset(archived_mirror_h);
1481   InstanceKlass::cast(c)->do_nonstatic_fields(&reset);
1482 
1483   if (k->is_array_klass()) {
1484     oop archived_comp_mirror;
1485     if (k->is_typeArray_klass()) {
1486       // The primitive type mirrors are already archived. Get the archived mirror.
1487       oop comp_mirror = component_mirror(mirror);
1488       archived_comp_mirror = HeapShared::find_archived_heap_object(comp_mirror);
1489       assert(archived_comp_mirror != NULL, "Must be");
1490     } else {
1491       assert(k->is_objArray_klass(), "Must be");
1492       Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
1493       assert(element_klass != NULL, "Must have an element klass");
1494       archived_comp_mirror = archive_mirror(element_klass);
1495       if (archived_comp_mirror == NULL) {
1496         return NULL;
1497       }
1498     }
1499     set_component_mirror(archived_mirror, archived_comp_mirror);
1500   } else {
1501     assert(k->is_instance_klass(), "Must be");
1502 
1503     // Reset local static fields in the mirror
1504     InstanceKlass::cast(k)->do_local_static_fields(&reset);
1505 
1506     set_init_lock(archived_mirror, NULL);
1507 
1508     set_protection_domain(archived_mirror, NULL);
1509     set_signers(archived_mirror, NULL);
1510     set_source_file(archived_mirror, NULL);
1511   }
1512 
1513   // clear class loader and mirror_module_field
1514   set_class_loader(archived_mirror, NULL);
1515   set_module(archived_mirror, NULL);
1516 
1517   // The archived mirror's field at _klass_offset is still pointing to the original
1518   // klass. Updated the field in the archived mirror to point to the relocated
1519   // klass in the archive.
1520   set_klass_field_in_archived_mirror(archived_mirror, _klass_offset, as_Klass(mirror));
1521 
1522   // The field at _array_klass_offset is pointing to the original one dimension
1523   // higher array klass if exists. Relocate the pointer.
1524   Klass *arr = array_klass_acquire(mirror);
1525   if (arr != NULL) {
1526     set_klass_field_in_archived_mirror(archived_mirror, _array_klass_offset, arr);
1527   }
1528   return archived_mirror;
1529 }
1530 
1531 void java_lang_Class::update_archived_primitive_mirror_native_pointers(oop archived_mirror) {
1532   if (MetaspaceShared::relocation_delta() != 0) {
1533     assert(archived_mirror->metadata_field(_klass_offset) == NULL, "must be for primitive class");
1534 
1535     Klass* ak = ((Klass*)archived_mirror->metadata_field(_array_klass_offset));
1536     if (ak != NULL) {
1537       archived_mirror->metadata_field_put(_array_klass_offset,
1538           (Klass*)(address(ak) + MetaspaceShared::relocation_delta()));
1539     }
1540   }
1541 }
1542 
1543 void java_lang_Class::update_archived_mirror_native_pointers(oop archived_mirror) {
1544   assert(MetaspaceShared::relocation_delta() != 0, "must be");
1545 
1546   Klass* k = ((Klass*)archived_mirror->metadata_field(_klass_offset));
1547   archived_mirror->metadata_field_put(_klass_offset,
1548       (Klass*)(address(k) + MetaspaceShared::relocation_delta()));
1549 
1550   Klass* ak = ((Klass*)archived_mirror->metadata_field(_array_klass_offset));
1551   if (ak != NULL) {
1552     archived_mirror->metadata_field_put(_array_klass_offset,
1553         (Klass*)(address(ak) + MetaspaceShared::relocation_delta()));
1554   }
1555 }
1556 
1557 
1558 // Returns true if the mirror is updated, false if no archived mirror
1559 // data is present. After the archived mirror object is restored, the
1560 // shared klass' _has_raw_archived_mirror flag is cleared.
1561 bool java_lang_Class::restore_archived_mirror(Klass *k,
1562                                               Handle class_loader, Handle module,
1563                                               Handle protection_domain, TRAPS) {
1564   // Postpone restoring archived mirror until java.lang.Class is loaded. Please
1565   // see more details in vmClasses::resolve_all().
1566   if (!vmClasses::Class_klass_loaded()) {
1567     assert(fixup_mirror_list() != NULL, "fixup_mirror_list not initialized");
1568     fixup_mirror_list()->push(k);
1569     return true;
1570   }
1571 
1572   oop m = k->archived_java_mirror();
1573   assert(m != NULL, "must have stored non-null archived mirror");
1574 
1575   // Sanity: clear it now to prevent re-initialization if any of the following fails
1576   k->clear_archived_mirror_index();
1577 
1578   // mirror is archived, restore
1579   log_debug(cds, mirror)("Archived mirror is: " PTR_FORMAT, p2i(m));
1580   if (HeapShared::is_mapped()) {
1581     assert(Universe::heap()->is_archived_object(m), "must be archived mirror object");
1582   }
1583   assert(as_Klass(m) == k, "must be");
1584   Handle mirror(THREAD, m);
1585 
1586   if (!k->is_array_klass()) {
1587     // - local static final fields with initial values were initialized at dump time
1588 
1589     // create the init_lock
1590     typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK_(false));
1591     set_init_lock(mirror(), r);
1592 
1593     if (protection_domain.not_null()) {
1594       set_protection_domain(mirror(), protection_domain());
1595     }
1596   }
1597 
1598   assert(class_loader() == k->class_loader(), "should be same");
1599   if (class_loader.not_null()) {
1600     set_class_loader(mirror(), class_loader());
1601   }
1602 
1603   k->set_java_mirror(mirror);
1604 
1605   set_mirror_module_field(THREAD, k, mirror, module);
1606 
1607   if (log_is_enabled(Trace, cds, heap, mirror)) {
1608     ResourceMark rm(THREAD);
1609     log_trace(cds, heap, mirror)(
1610         "Restored %s archived mirror " PTR_FORMAT, k->external_name(), p2i(mirror()));
1611   }
1612 
1613   return true;
1614 }
1615 #endif // INCLUDE_CDS_JAVA_HEAP
1616 
1617 void java_lang_Class::fixup_module_field(Klass* k, Handle module) {
1618   assert(_module_offset != 0, "must have been computed already");
1619   set_module(k->java_mirror(), module());
1620 }
1621 
1622 void java_lang_Class::set_oop_size(HeapWord* java_class, size_t size) {
1623   assert(_oop_size_offset != 0, "must be set");
1624   assert(size > 0, "Oop size must be greater than zero, not " SIZE_FORMAT, size);
1625   assert(size <= INT_MAX, "Lossy conversion: " SIZE_FORMAT, size);
1626   *(int*)(((char*)java_class) + _oop_size_offset) = (int)size;
1627 }
1628 
1629 int  java_lang_Class::static_oop_field_count(oop java_class) {
1630   assert(_static_oop_field_count_offset != 0, "must be set");
1631   return java_class->int_field(_static_oop_field_count_offset);
1632 }
1633 
1634 void java_lang_Class::set_static_oop_field_count(oop java_class, int size) {
1635   assert(_static_oop_field_count_offset != 0, "must be set");
1636   java_class->int_field_put(_static_oop_field_count_offset, size);
1637 }
1638 
1639 oop java_lang_Class::protection_domain(oop java_class) {
1640   assert(_protection_domain_offset != 0, "must be set");
1641   return java_class->obj_field(_protection_domain_offset);
1642 }
1643 void java_lang_Class::set_protection_domain(oop java_class, oop pd) {
1644   assert(_protection_domain_offset != 0, "must be set");
1645   java_class->obj_field_put(_protection_domain_offset, pd);
1646 }
1647 
1648 void java_lang_Class::set_component_mirror(oop java_class, oop comp_mirror) {
1649   assert(_component_mirror_offset != 0, "must be set");
1650     java_class->obj_field_put(_component_mirror_offset, comp_mirror);
1651   }
1652 oop java_lang_Class::component_mirror(oop java_class) {
1653   assert(_component_mirror_offset != 0, "must be set");
1654   return java_class->obj_field(_component_mirror_offset);
1655 }
1656 
1657 oop java_lang_Class::init_lock(oop java_class) {
1658   assert(_init_lock_offset != 0, "must be set");
1659   return java_class->obj_field(_init_lock_offset);
1660 }
1661 void java_lang_Class::set_init_lock(oop java_class, oop init_lock) {
1662   assert(_init_lock_offset != 0, "must be set");
1663   java_class->obj_field_put(_init_lock_offset, init_lock);
1664 }
1665 
1666 objArrayOop java_lang_Class::signers(oop java_class) {
1667   assert(_signers_offset != 0, "must be set");
1668   return (objArrayOop)java_class->obj_field(_signers_offset);
1669 }
1670 void java_lang_Class::set_signers(oop java_class, objArrayOop signers) {
1671   assert(_signers_offset != 0, "must be set");
1672   java_class->obj_field_put(_signers_offset, signers);
1673 }
1674 
1675 oop java_lang_Class::class_data(oop java_class) {
1676   assert(_classData_offset != 0, "must be set");
1677   return java_class->obj_field(_classData_offset);
1678 }
1679 void java_lang_Class::set_class_data(oop java_class, oop class_data) {
1680   assert(_classData_offset != 0, "must be set");
1681   java_class->obj_field_put(_classData_offset, class_data);
1682 }
1683 
1684 void java_lang_Class::set_class_loader(oop java_class, oop loader) {
1685   assert(_class_loader_offset != 0, "offsets should have been initialized");
1686   java_class->obj_field_put(_class_loader_offset, loader);
1687 }
1688 
1689 oop java_lang_Class::class_loader(oop java_class) {
1690   assert(_class_loader_offset != 0, "must be set");
1691   return java_class->obj_field(_class_loader_offset);
1692 }
1693 
1694 oop java_lang_Class::module(oop java_class) {
1695   assert(_module_offset != 0, "must be set");
1696   return java_class->obj_field(_module_offset);
1697 }
1698 
1699 void java_lang_Class::set_module(oop java_class, oop module) {
1700   assert(_module_offset != 0, "must be set");
1701   java_class->obj_field_put(_module_offset, module);
1702 }
1703 
1704 oop java_lang_Class::name(Handle java_class, TRAPS) {
1705   assert(_name_offset != 0, "must be set");
1706   oop o = java_class->obj_field(_name_offset);
1707   if (o == NULL) {
1708     o = StringTable::intern(as_external_name(java_class()), THREAD);
1709     java_class->obj_field_put(_name_offset, o);
1710   }
1711   return o;
1712 }
1713 
1714 oop java_lang_Class::source_file(oop java_class) {
1715   assert(_source_file_offset != 0, "must be set");
1716   return java_class->obj_field(_source_file_offset);
1717 }
1718 
1719 void java_lang_Class::set_source_file(oop java_class, oop source_file) {
1720   assert(_source_file_offset != 0, "must be set");
1721   java_class->obj_field_put(_source_file_offset, source_file);
1722 }
1723 
1724 oop java_lang_Class::create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS) {
1725   // This should be improved by adding a field at the Java level or by
1726   // introducing a new VM klass (see comment in ClassFileParser)
1727   oop java_class = InstanceMirrorKlass::cast(vmClasses::Class_klass())->allocate_instance(NULL, CHECK_NULL);
1728   if (type != T_VOID) {
1729     Klass* aklass = Universe::typeArrayKlassObj(type);
1730     assert(aklass != NULL, "correct bootstrap");
1731     release_set_array_klass(java_class, aklass);
1732   }
1733 #ifdef ASSERT
1734   InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(vmClasses::Class_klass());
1735   assert(static_oop_field_count(java_class) == 0, "should have been zeroed by allocation");
1736 #endif
1737   return java_class;
1738 }
1739 
1740 void java_lang_Class::set_klass(oop java_class, Klass* klass) {
1741   assert(is_instance(java_class), "must be a Class object");
1742   java_class->metadata_field_put(_klass_offset, klass);
1743 }
1744 
1745 
1746 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1747   assert(is_instance(java_class), "must be a Class object");
1748   Symbol* name = NULL;
1749   bool is_instance = false;
1750   if (is_primitive(java_class)) {
1751     name = vmSymbols::type_signature(primitive_type(java_class));
1752   } else {
1753     Klass* k = as_Klass(java_class);
1754     is_instance = k->is_instance_klass();
1755     name = k->name();
1756   }
1757   if (name == NULL) {
1758     st->print("<null>");
1759     return;
1760   }
1761   if (is_instance)  st->print("L");
1762   st->write((char*) name->base(), (int) name->utf8_length());
1763   if (is_instance)  st->print(";");
1764 }
1765 
1766 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found) {
1767   assert(is_instance(java_class), "must be a Class object");
1768   Symbol* name;
1769   if (is_primitive(java_class)) {
1770     name = vmSymbols::type_signature(primitive_type(java_class));
1771     // Because this can create a new symbol, the caller has to decrement
1772     // the refcount, so make adjustment here and below for symbols returned
1773     // that are not created or incremented due to a successful lookup.
1774     name->increment_refcount();
1775   } else {
1776     Klass* k = as_Klass(java_class);
1777     if (!k->is_instance_klass()) {
1778       name = k->name();
1779       name->increment_refcount();
1780     } else {
1781       ResourceMark rm;
1782       const char* sigstr = k->signature_name();
1783       int         siglen = (int) strlen(sigstr);
1784       if (!intern_if_not_found) {
1785         name = SymbolTable::probe(sigstr, siglen);
1786       } else {
1787         name = SymbolTable::new_symbol(sigstr, siglen);
1788       }
1789     }
1790   }
1791   return name;
1792 }
1793 
1794 // Returns the Java name for this Java mirror (Resource allocated)
1795 // See Klass::external_name().
1796 // For primitive type Java mirrors, its type name is returned.
1797 const char* java_lang_Class::as_external_name(oop java_class) {
1798   assert(is_instance(java_class), "must be a Class object");
1799   const char* name = NULL;
1800   if (is_primitive(java_class)) {
1801     name = type2name(primitive_type(java_class));
1802   } else {
1803     name = as_Klass(java_class)->external_name();
1804   }
1805   if (name == NULL) {
1806     name = "<null>";
1807   }
1808   return name;
1809 }
1810 
1811 Klass* java_lang_Class::array_klass_acquire(oop java_class) {
1812   Klass* k = ((Klass*)java_class->metadata_field_acquire(_array_klass_offset));
1813   assert(k == NULL || k->is_klass() && k->is_array_klass(), "should be array klass");
1814   return k;
1815 }
1816 
1817 
1818 void java_lang_Class::release_set_array_klass(oop java_class, Klass* klass) {
1819   assert(klass->is_klass() && klass->is_array_klass(), "should be array klass");
1820   java_class->release_metadata_field_put(_array_klass_offset, klass);
1821 }
1822 
1823 
1824 BasicType java_lang_Class::primitive_type(oop java_class) {
1825   assert(is_primitive(java_class), "just checking");
1826   Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset));
1827   BasicType type = T_VOID;
1828   if (ak != NULL) {
1829     // Note: create_basic_type_mirror above initializes ak to a non-null value.
1830     type = ArrayKlass::cast(ak)->element_type();
1831   } else {
1832     assert(java_class == Universe::void_mirror(), "only valid non-array primitive");
1833   }
1834   assert(Universe::java_mirror(type) == java_class, "must be consistent");
1835   return type;
1836 }
1837 
1838 BasicType java_lang_Class::as_BasicType(oop java_class, Klass** reference_klass) {
1839   assert(is_instance(java_class), "must be a Class object");
1840   if (is_primitive(java_class)) {
1841     if (reference_klass != NULL)
1842       (*reference_klass) = NULL;
1843     return primitive_type(java_class);
1844   } else {
1845     if (reference_klass != NULL)
1846       (*reference_klass) = as_Klass(java_class);
1847     return T_OBJECT;
1848   }
1849 }
1850 
1851 
1852 oop java_lang_Class::primitive_mirror(BasicType t) {
1853   oop mirror = Universe::java_mirror(t);
1854   assert(mirror != NULL && mirror->is_a(vmClasses::Class_klass()), "must be a Class");
1855   assert(is_primitive(mirror), "must be primitive");
1856   return mirror;
1857 }
1858 
1859 #define CLASS_FIELDS_DO(macro) \
1860   macro(_classRedefinedCount_offset, k, "classRedefinedCount", int_signature,         false); \
1861   macro(_class_loader_offset,        k, "classLoader",         classloader_signature, false); \
1862   macro(_component_mirror_offset,    k, "componentType",       class_signature,       false); \
1863   macro(_module_offset,              k, "module",              module_signature,      false); \
1864   macro(_name_offset,                k, "name",                string_signature,      false); \
1865   macro(_classData_offset,           k, "classData",           object_signature,      false);
1866 
1867 void java_lang_Class::compute_offsets() {
1868   if (_offsets_computed) {
1869     return;
1870   }
1871 
1872   _offsets_computed = true;
1873 
1874   InstanceKlass* k = vmClasses::Class_klass();
1875   CLASS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1876 
1877   // Init lock is a C union with component_mirror.  Only instanceKlass mirrors have
1878   // init_lock and only ArrayKlass mirrors have component_mirror.  Since both are oops
1879   // GC treats them the same.
1880   _init_lock_offset = _component_mirror_offset;
1881 
1882   CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
1883 }
1884 
1885 #if INCLUDE_CDS
1886 void java_lang_Class::serialize_offsets(SerializeClosure* f) {
1887   f->do_bool(&_offsets_computed);
1888   f->do_u4((u4*)&_init_lock_offset);
1889 
1890   CLASS_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1891 
1892   CLASS_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
1893 }
1894 #endif
1895 
1896 int java_lang_Class::classRedefinedCount(oop the_class_mirror) {
1897   assert(_classRedefinedCount_offset != 0, "offsets should have been initialized");
1898   return the_class_mirror->int_field(_classRedefinedCount_offset);
1899 }
1900 
1901 void java_lang_Class::set_classRedefinedCount(oop the_class_mirror, int value) {
1902   assert(_classRedefinedCount_offset != 0, "offsets should have been initialized");
1903   the_class_mirror->int_field_put(_classRedefinedCount_offset, value);
1904 }
1905 
1906 
1907 // Note: JDK1.1 and before had a privateInfo_offset field which was used for the
1908 //       platform thread structure, and a eetop offset which was used for thread
1909 //       local storage (and unused by the HotSpot VM). In JDK1.2 the two structures
1910 //       merged, so in the HotSpot VM we just use the eetop field for the thread
1911 //       instead of the privateInfo_offset.
1912 //
1913 // Note: The stackSize field is only present starting in 1.4.
1914 
1915 int java_lang_Thread_FieldHolder::_group_offset;
1916 int java_lang_Thread_FieldHolder::_priority_offset;
1917 int java_lang_Thread_FieldHolder::_stackSize_offset;
1918 int java_lang_Thread_FieldHolder::_stillborn_offset;
1919 int java_lang_Thread_FieldHolder::_daemon_offset;
1920 int java_lang_Thread_FieldHolder::_thread_status_offset;
1921 
1922 #define THREAD_FIELD_HOLDER_FIELDS_DO(macro) \
1923   macro(_group_offset,         k, vmSymbols::group_name(), threadgroup_signature, false); \
1924   macro(_priority_offset,      k, vmSymbols::priority_name(), int_signature, false); \
1925   macro(_stackSize_offset,     k, "stackSize", long_signature, false); \
1926   macro(_stillborn_offset,     k, "stillborn", bool_signature, false); \
1927   macro(_daemon_offset,        k, vmSymbols::daemon_name(), bool_signature, false); \
1928   macro(_thread_status_offset, k, "threadStatus", int_signature, false)
1929 
1930 void java_lang_Thread_FieldHolder::compute_offsets() {
1931   assert(_group_offset == 0, "offsets should be initialized only once");
1932 
1933   InstanceKlass* k = vmClasses::Thread_FieldHolder_klass();
1934   THREAD_FIELD_HOLDER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1935 }
1936 
1937 #if INCLUDE_CDS
1938 void java_lang_Thread_FieldHolder::serialize_offsets(SerializeClosure* f) {
1939   THREAD_FIELD_HOLDER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1940 }
1941 #endif
1942 
1943 oop java_lang_Thread_FieldHolder::threadGroup(oop holder) {
1944   return holder->obj_field(_group_offset);
1945 }
1946 
1947 ThreadPriority java_lang_Thread_FieldHolder::priority(oop holder) {
1948   return (ThreadPriority)holder->int_field(_priority_offset);
1949 }
1950 
1951 void java_lang_Thread_FieldHolder::set_priority(oop holder, ThreadPriority priority) {
1952   holder->int_field_put(_priority_offset, priority);
1953 }
1954 
1955 jlong java_lang_Thread_FieldHolder::stackSize(oop holder) {
1956   return holder->long_field(_stackSize_offset);
1957 }
1958 
1959 bool java_lang_Thread_FieldHolder::is_stillborn(oop holder) {
1960   return holder->bool_field(_stillborn_offset) != 0;
1961 }
1962 
1963 void java_lang_Thread_FieldHolder::set_stillborn(oop holder) {
1964   holder->bool_field_put(_stillborn_offset, true);
1965 }
1966 
1967 bool java_lang_Thread_FieldHolder::is_daemon(oop holder) {
1968   return holder->bool_field(_daemon_offset) != 0;
1969 }
1970 
1971 void java_lang_Thread_FieldHolder::set_daemon(oop holder) {
1972   holder->bool_field_put(_daemon_offset, true);
1973 }
1974 
1975 void java_lang_Thread_FieldHolder::set_thread_status(oop holder, JavaThreadStatus status) {
1976   holder->int_field_put(_thread_status_offset, static_cast<int>(status));
1977 }
1978 
1979 JavaThreadStatus java_lang_Thread_FieldHolder::get_thread_status(oop holder) {
1980   return static_cast<JavaThreadStatus>(holder->int_field(_thread_status_offset));
1981 }
1982 
1983 
1984 int java_lang_Thread_VirtualThreads::_static_THREAD_GROUP_offset = 0;
1985 
1986 #define THREAD_VIRTUAL_THREADS_STATIC_FIELDS_DO(macro) \
1987   macro(_static_THREAD_GROUP_offset, k, "THREAD_GROUP", threadgroup_signature, true);
1988 
1989 void java_lang_Thread_VirtualThreads::compute_offsets() {
1990   assert(_static_THREAD_GROUP_offset == 0, "offsets should be initialized only once");
1991 
1992   InstanceKlass* k = vmClasses::Thread_VirtualThreads_klass();
1993   THREAD_VIRTUAL_THREADS_STATIC_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1994 }
1995 
1996 #if INCLUDE_CDS
1997 void java_lang_Thread_VirtualThreads::serialize_offsets(SerializeClosure* f) {
1998   THREAD_VIRTUAL_THREADS_STATIC_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1999 }
2000 #endif
2001 
2002 oop java_lang_Thread_VirtualThreads::get_THREAD_GROUP() {
2003   InstanceKlass* k = vmClasses::Thread_VirtualThreads_klass();
2004   oop base = k->static_field_base_raw();
2005   return base->obj_field(_static_THREAD_GROUP_offset);
2006 }
2007 
2008 
2009 int java_lang_Thread_ClassLoaders::_static_NOT_SUPPORTED_offset = 0;
2010 
2011 #define THREAD_CLASS_LOADERS_STATIC_FIELDS_DO(macro) \
2012   macro(_static_NOT_SUPPORTED_offset, k, "NOT_SUPPORTED", classloader_signature, true);
2013 
2014 void java_lang_Thread_ClassLoaders::compute_offsets() {
2015   assert(_static_NOT_SUPPORTED_offset == 0, "offsets should be initialized only once");
2016 
2017   InstanceKlass* k = vmClasses::Thread_ClassLoaders_klass();
2018   THREAD_CLASS_LOADERS_STATIC_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2019 }
2020 
2021 #if INCLUDE_CDS
2022 void java_lang_Thread_ClassLoaders::serialize_offsets(SerializeClosure* f) {
2023   THREAD_CLASS_LOADERS_STATIC_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2024 }
2025 #endif
2026 
2027 oop java_lang_Thread_ClassLoaders::get_NOT_SUPPORTED() {
2028   InstanceKlass* k = vmClasses::Thread_ClassLoaders_klass();
2029   oop base = k->static_field_base_raw();
2030   return base->obj_field(_static_NOT_SUPPORTED_offset);
2031 }
2032 
2033 int java_lang_Thread::_holder_offset;
2034 int java_lang_Thread::_name_offset;
2035 int java_lang_Thread::_contextClassLoader_offset;
2036 int java_lang_Thread::_inheritedAccessControlContext_offset;
2037 int java_lang_Thread::_eetop_offset;
2038 int java_lang_Thread::_jvmti_thread_state_offset;
2039 int java_lang_Thread::_interrupted_offset;
2040 int java_lang_Thread::_tid_offset;
2041 int java_lang_Thread::_continuation_offset;
2042 int java_lang_Thread::_park_blocker_offset;
2043 int java_lang_Thread::_scopeLocalBindings_offset;
2044 
2045 #define THREAD_FIELDS_DO(macro) \
2046   macro(_holder_offset,        k, "holder", thread_fieldholder_signature, false); \
2047   macro(_name_offset,          k, vmSymbols::name_name(), string_signature, false); \
2048   macro(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), classloader_signature, false); \
2049   macro(_inheritedAccessControlContext_offset, k, vmSymbols::inheritedAccessControlContext_name(), accesscontrolcontext_signature, false); \
2050   macro(_eetop_offset,         k, "eetop", long_signature, false); \
2051   macro(_interrupted_offset,   k, "interrupted", bool_signature, false); \
2052   macro(_tid_offset,           k, "tid", long_signature, false); \
2053   macro(_park_blocker_offset,  k, "parkBlocker", object_signature, false); \
2054   macro(_continuation_offset,  k, "cont", continuation_signature, false); \
2055   macro(_scopeLocalBindings_offset, k, "scopeLocalBindings", scopeLocalSnapshot_name, false);
2056 
2057 void java_lang_Thread::compute_offsets() {
2058   assert(_holder_offset == 0, "offsets should be initialized only once");
2059 
2060   InstanceKlass* k = vmClasses::Thread_klass();
2061   THREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2062   THREAD_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2063 }
2064 
2065 #if INCLUDE_CDS
2066 void java_lang_Thread::serialize_offsets(SerializeClosure* f) {
2067   THREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2068   THREAD_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
2069 }
2070 #endif
2071 
2072 JavaThread* java_lang_Thread::thread(oop java_thread) {
2073   return (JavaThread*)java_thread->address_field(_eetop_offset);
2074 }
2075 
2076 void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) {
2077   java_thread->address_field_put(_eetop_offset, (address)thread);
2078 }
2079 
2080 JvmtiThreadState* java_lang_Thread::jvmti_thread_state(oop java_thread) {
2081   return (JvmtiThreadState*)java_thread->address_field(_jvmti_thread_state_offset);
2082 }
2083 
2084 void java_lang_Thread::set_jvmti_thread_state(oop java_thread, JvmtiThreadState* state) {
2085   java_thread->address_field_put(_jvmti_thread_state_offset, (address)state);
2086 }
2087 
2088 void java_lang_Thread::clear_scopeLocalBindings(oop java_thread) {
2089   java_thread->obj_field_put(_scopeLocalBindings_offset, NULL);
2090 }
2091 
2092 oop java_lang_Thread::holder(oop java_thread) {
2093     return java_thread->obj_field(_holder_offset);
2094 }
2095 
2096 bool java_lang_Thread::interrupted(oop java_thread) {
2097   // Make sure the caller can safely access oops.
2098   assert(Thread::current()->is_VM_thread() ||
2099          (JavaThread::current()->thread_state() != _thread_blocked &&
2100           JavaThread::current()->thread_state() != _thread_in_native),
2101          "Unsafe access to oop");
2102   return java_thread->bool_field_volatile(_interrupted_offset);
2103 }
2104 
2105 void java_lang_Thread::set_interrupted(oop java_thread, bool val) {
2106   // Make sure the caller can safely access oops.
2107   assert(Thread::current()->is_VM_thread() ||
2108          (JavaThread::current()->thread_state() != _thread_blocked &&
2109           JavaThread::current()->thread_state() != _thread_in_native),
2110          "Unsafe access to oop");
2111   java_thread->bool_field_put_volatile(_interrupted_offset, val);
2112 }
2113 
2114 
2115 oop java_lang_Thread::name(oop java_thread) {
2116   return java_thread->obj_field(_name_offset);
2117 }
2118 
2119 
2120 void java_lang_Thread::set_name(oop java_thread, oop name) {
2121   java_thread->obj_field_put(_name_offset, name);
2122 }
2123 
2124 
2125 ThreadPriority java_lang_Thread::priority(oop java_thread) {
2126   oop holder = java_lang_Thread::holder(java_thread);
2127   assert(holder != NULL, "Java Thread not initialized");
2128   return java_lang_Thread_FieldHolder::priority(holder);
2129 }
2130 
2131 
2132 void java_lang_Thread::set_priority(oop java_thread, ThreadPriority priority) {
2133   oop holder = java_lang_Thread::holder(java_thread);
2134   assert(holder != NULL, "Java Thread not initialized");
2135   java_lang_Thread_FieldHolder::set_priority(holder, priority);
2136 }
2137 
2138 
2139 oop java_lang_Thread::threadGroup(oop java_thread) {
2140   oop holder = java_lang_Thread::holder(java_thread);
2141   assert(holder != NULL, "Java Thread not initialized");
2142   return java_lang_Thread_FieldHolder::threadGroup(holder);
2143 }
2144 
2145 
2146 bool java_lang_Thread::is_stillborn(oop java_thread) {
2147   oop holder = java_lang_Thread::holder(java_thread);
2148   assert(holder != NULL, "Java Thread not initialized");
2149   return java_lang_Thread_FieldHolder::is_stillborn(holder);
2150 }
2151 
2152 
2153 // We never have reason to turn the stillborn bit off
2154 void java_lang_Thread::set_stillborn(oop java_thread) {
2155   oop holder = java_lang_Thread::holder(java_thread);
2156   assert(holder != NULL, "Java Thread not initialized");
2157   java_lang_Thread_FieldHolder::set_stillborn(holder);
2158 }
2159 
2160 
2161 bool java_lang_Thread::is_alive(oop java_thread) {
2162   JavaThread* thr = java_lang_Thread::thread(java_thread);
2163   return (thr != NULL);
2164 }
2165 
2166 
2167 bool java_lang_Thread::is_daemon(oop java_thread) {
2168   oop holder = java_lang_Thread::holder(java_thread);
2169   assert(holder != NULL, "Java Thread not initialized");
2170   return java_lang_Thread_FieldHolder::is_daemon(holder);
2171 }
2172 
2173 void java_lang_Thread::set_daemon(oop java_thread) {
2174   oop holder = java_lang_Thread::holder(java_thread);
2175   assert(holder != NULL, "Java Thread not initialized");
2176   java_lang_Thread_FieldHolder::set_daemon(holder);
2177 }
2178 
2179 oop java_lang_Thread::context_class_loader(oop java_thread) {
2180   return java_thread->obj_field(_contextClassLoader_offset);
2181 }
2182 
2183 oop java_lang_Thread::inherited_access_control_context(oop java_thread) {
2184   return java_thread->obj_field(_inheritedAccessControlContext_offset);
2185 }
2186 
2187 
2188 jlong java_lang_Thread::stackSize(oop java_thread) {
2189   oop holder = java_lang_Thread::holder(java_thread);
2190   assert(holder != NULL, "Java Thread not initialized");
2191   return java_lang_Thread_FieldHolder::stackSize(holder);
2192 }
2193 
2194 // Write the thread status value to threadStatus field in java.lang.Thread java class.
2195 void java_lang_Thread::set_thread_status(oop java_thread, JavaThreadStatus status) {
2196   oop holder = java_lang_Thread::holder(java_thread);
2197   assert(holder != NULL, "Java Thread not initialized");
2198   java_lang_Thread_FieldHolder::set_thread_status(holder, status);
2199 }
2200 
2201 // Read thread status value from threadStatus field in java.lang.Thread java class.
2202 JavaThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
2203   // Make sure the caller is operating on behalf of the VM or is
2204   // running VM code (state == _thread_in_vm).
2205   assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
2206          JavaThread::current()->thread_state() == _thread_in_vm,
2207          "Java Thread is not running in vm");
2208   oop holder = java_lang_Thread::holder(java_thread);
2209   if (holder == NULL) {
2210     return JavaThreadStatus::NEW;  // Java Thread not initialized
2211   } else {
2212     return java_lang_Thread_FieldHolder::get_thread_status(holder);
2213   }
2214 }
2215 
2216 jlong java_lang_Thread::thread_id(oop java_thread) {
2217   // The 16 most significant bits can be used for tracing
2218   // so these bits are excluded using a mask.
2219   static const jlong tid_mask = (((jlong)1) << 48) - 1;
2220   return java_thread->long_field(_tid_offset) & tid_mask;
2221 }
2222 
2223 ByteSize java_lang_Thread::thread_id_offset() {
2224   return in_ByteSize(_tid_offset);
2225 }
2226 
2227 oop java_lang_Thread::park_blocker(oop java_thread) {
2228   return java_thread->obj_field(_park_blocker_offset);
2229 }
2230 
2231 oop java_lang_Thread::async_get_stack_trace(oop java_thread, TRAPS) {
2232   ThreadsListHandle tlh(JavaThread::current());
2233   JavaThread* thread;
2234   bool is_virtual = java_lang_VirtualThread::is_instance(java_thread);
2235   if (is_virtual) {
2236     oop carrier_thread = java_lang_VirtualThread::carrier_thread(java_thread);
2237     if (carrier_thread == NULL) {
2238       return NULL;
2239     }
2240     thread = java_lang_Thread::thread(carrier_thread);
2241   } else {
2242     thread = java_lang_Thread::thread(java_thread);
2243   }
2244   if (thread == NULL) {
2245     return NULL;
2246   }
2247 
2248   class GetStackTraceClosure : public HandshakeClosure {
2249   public:
2250     const Handle _java_thread;
2251     int _depth;
2252     GrowableArray<Method*>* _methods;
2253     GrowableArray<int>*     _bcis;
2254     GrowableArray<oop>*     _continuations;
2255 
2256     GetStackTraceClosure(Handle java_thread) :
2257       HandshakeClosure("GetStackTraceClosure"), _java_thread(java_thread), _depth(0) {
2258       // Pick some initial length
2259       int init_length = MaxJavaStackTraceDepth/2;
2260       _methods = new GrowableArray<Method*>(init_length);
2261       _bcis = new GrowableArray<int>(init_length);
2262       _continuations = new GrowableArray<oop>(init_length);
2263     }
2264 
2265     bool can_be_processed_by(Thread* thread) {
2266       return thread->is_Java_thread();
2267     }
2268 
2269     void do_thread(Thread* th) {
2270       JavaThread* thread = JavaThread::cast(th);
2271 
2272       if (!thread->has_last_Java_frame()) {
2273         return;
2274       }
2275 
2276       bool carrier = false;
2277       oop vthread_scope = java_lang_VirtualThread::vthread_scope();
2278       if (java_lang_VirtualThread::is_instance(_java_thread())) {
2279         // if (thread->vthread() != _java_thread()) // We might be inside a System.executeOnCarrierThread
2280         if (thread->last_continuation(vthread_scope)->cont_oop() !=
2281               java_lang_VirtualThread::continuation(_java_thread())) {
2282           return; // not mounted
2283         }
2284       } else {
2285         carrier = (thread->last_continuation(vthread_scope) != NULL);
2286       }
2287 
2288       const int max_depth = MaxJavaStackTraceDepth;
2289       const bool skip_hidden = !ShowHiddenFrames;
2290 
2291       int total_count = 0;
2292       for (vframeStream vfst(thread, false, false, carrier);
2293            !vfst.at_end() && (max_depth == 0 || max_depth != total_count);
2294            vfst.next()) {
2295 
2296         if (skip_hidden && (vfst.method()->is_hidden() ||
2297                             vfst.method()->is_continuation_enter_intrinsic())) continue;
2298         _methods->push(vfst.method());
2299         _bcis->push(vfst.bci());
2300         _continuations->push(contScopeName(vfst.continuation()));
2301         total_count++;
2302       }
2303 
2304       _depth = total_count;
2305     }
2306 
2307     oop contScopeName(oop cont) {
2308       return cont == NULL ? NULL : jdk_internal_vm_ContinuationScope::name(Continuation::continuation_scope(cont));
2309     }
2310   };
2311 
2312   // Handshake with target
2313   ResourceMark rm(THREAD);
2314   HandleMark   hm(THREAD);
2315   GetStackTraceClosure gstc(Handle(THREAD, java_thread));
2316   Handshake::execute(&gstc, &tlh, thread);
2317 
2318   // Stop if no stack trace is found.
2319   if (gstc._depth == 0) {
2320     return NULL;
2321   }
2322 
2323   // Convert the continuations into handles before allocation.
2324   assert(gstc._depth == gstc._continuations->length(), "should be the same");
2325   GrowableArray<Handle>* cont_handles = new GrowableArray<Handle>(gstc._depth);
2326   for (int i = 0; i < gstc._depth; i++) {
2327     cont_handles->push(Handle(THREAD, gstc._continuations->at(i)));
2328   }
2329 
2330   // Convert to StackTraceElement array
2331   InstanceKlass* k = vmClasses::StackTraceElement_klass();
2332   assert(k != NULL, "must be loaded in 1.4+");
2333   if (k->should_be_initialized()) k->initialize(CHECK_NULL);
2334   objArrayHandle trace = oopFactory::new_objArray_handle(k, gstc._depth, CHECK_NULL);
2335 
2336   for (int i = 0; i < gstc._depth; i++) {
2337     methodHandle method(THREAD, gstc._methods->at(i));
2338     oop element = java_lang_StackTraceElement::create(method,
2339                                                       gstc._bcis->at(i),
2340                                                       cont_handles->at(i),
2341                                                       CHECK_NULL);
2342     trace->obj_at_put(i, element);
2343   }
2344 
2345   return trace();
2346 }
2347 
2348 const char* java_lang_Thread::thread_status_name(oop java_thread) {
2349   oop holder = java_lang_Thread::holder(java_thread);
2350   assert(holder != NULL, "Java Thread not initialized");
2351   JavaThreadStatus status = java_lang_Thread_FieldHolder::get_thread_status(holder);
2352   switch (status) {
2353     case JavaThreadStatus::NEW                      : return "NEW";
2354     case JavaThreadStatus::RUNNABLE                 : return "RUNNABLE";
2355     case JavaThreadStatus::SLEEPING                 : return "TIMED_WAITING (sleeping)";
2356     case JavaThreadStatus::IN_OBJECT_WAIT           : return "WAITING (on object monitor)";
2357     case JavaThreadStatus::IN_OBJECT_WAIT_TIMED     : return "TIMED_WAITING (on object monitor)";
2358     case JavaThreadStatus::PARKED                   : return "WAITING (parking)";
2359     case JavaThreadStatus::PARKED_TIMED             : return "TIMED_WAITING (parking)";
2360     case JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
2361     case JavaThreadStatus::TERMINATED               : return "TERMINATED";
2362     default                       : return "UNKNOWN";
2363   };
2364 }
2365 int java_lang_ThreadGroup::_parent_offset;
2366 int java_lang_ThreadGroup::_name_offset;
2367 int java_lang_ThreadGroup::_maxPriority_offset;
2368 int java_lang_ThreadGroup::_ngroups_offset;
2369 int java_lang_ThreadGroup::_groups_offset;
2370 int java_lang_ThreadGroup::_nweaks_offset;
2371 int java_lang_ThreadGroup::_weaks_offset;
2372 
2373 oop  java_lang_ThreadGroup::parent(oop java_thread_group) {
2374   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
2375   return java_thread_group->obj_field(_parent_offset);
2376 }
2377 
2378 // ("name as oop" accessor is not necessary)
2379 
2380 const char* java_lang_ThreadGroup::name(oop java_thread_group) {
2381   oop name = java_thread_group->obj_field(_name_offset);
2382   // ThreadGroup.name can be null
2383   if (name != NULL) {
2384     return java_lang_String::as_utf8_string(name);
2385   }
2386   return NULL;
2387 }
2388 
2389 ThreadPriority java_lang_ThreadGroup::maxPriority(oop java_thread_group) {
2390   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
2391   return (ThreadPriority) java_thread_group->int_field(_maxPriority_offset);
2392 }
2393 
2394 int java_lang_ThreadGroup::ngroups(oop java_thread_group) {
2395   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
2396   return java_thread_group->int_field(_ngroups_offset);
2397 }
2398 
2399 objArrayOop java_lang_ThreadGroup::groups(oop java_thread_group) {
2400   oop groups = java_thread_group->obj_field(_groups_offset);
2401   assert(groups == NULL || groups->is_objArray(), "just checking"); // Todo: Add better type checking code
2402   return objArrayOop(groups);
2403 }
2404 
2405 int java_lang_ThreadGroup::nweaks(oop java_thread_group) {
2406   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
2407   return java_thread_group->int_field(_nweaks_offset);
2408 }
2409 
2410 objArrayOop java_lang_ThreadGroup::weaks(oop java_thread_group) {
2411   oop weaks = java_thread_group->obj_field(_weaks_offset);
2412   assert(weaks == NULL || weaks->is_objArray(), "just checking");
2413   return objArrayOop(weaks);
2414 }
2415 
2416 #define THREADGROUP_FIELDS_DO(macro) \
2417   macro(_parent_offset,      k, vmSymbols::parent_name(),      threadgroup_signature,       false); \
2418   macro(_name_offset,        k, vmSymbols::name_name(),        string_signature,            false); \
2419   macro(_maxPriority_offset, k, vmSymbols::maxPriority_name(), int_signature,               false); \
2420   macro(_ngroups_offset,     k, vmSymbols::ngroups_name(),     int_signature,               false); \
2421   macro(_groups_offset,      k, vmSymbols::groups_name(),      threadgroup_array_signature, false); \
2422   macro(_nweaks_offset,      k, vmSymbols::nweaks_name(),      int_signature,               false); \
2423   macro(_weaks_offset,       k, vmSymbols::weaks_name(),       weakreference_array_signature, false);
2424 
2425 void java_lang_ThreadGroup::compute_offsets() {
2426   assert(_parent_offset == 0, "offsets should be initialized only once");
2427 
2428   InstanceKlass* k = vmClasses::ThreadGroup_klass();
2429   THREADGROUP_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2430 }
2431 
2432 #if INCLUDE_CDS
2433 void java_lang_ThreadGroup::serialize_offsets(SerializeClosure* f) {
2434   THREADGROUP_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2435 }
2436 #endif
2437 
2438 
2439 // java_lang_VirtualThread
2440 
2441 int java_lang_VirtualThread::static_notify_jvmti_events_offset;
2442 int java_lang_VirtualThread::static_vthread_scope_offset;
2443 int java_lang_VirtualThread::_carrierThread_offset;
2444 int java_lang_VirtualThread::_continuation_offset;
2445 int java_lang_VirtualThread::_state_offset;
2446 
2447 #define VTHREAD_FIELDS_DO(macro) \
2448   macro(static_notify_jvmti_events_offset,  k, "notifyJvmtiEvents",  bool_signature, true); \
2449   macro(static_vthread_scope_offset,  k, "VTHREAD_SCOPE",  continuationscope_signature, true); \
2450   macro(_carrierThread_offset,  k, "carrierThread",  thread_signature, false); \
2451   macro(_continuation_offset,  k, "cont",  continuation_signature, false); \
2452   macro(_state_offset,  k, "state",  int_signature, false)
2453 
2454 static jboolean vthread_notify_jvmti_events = JNI_FALSE;
2455 
2456 void java_lang_VirtualThread::compute_offsets() {
2457   InstanceKlass* k = vmClasses::VirtualThread_klass();
2458   VTHREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2459 }
2460 
2461 void java_lang_VirtualThread::init_static_notify_jvmti_events() {
2462   if (vthread_notify_jvmti_events) {
2463     InstanceKlass* ik = vmClasses::VirtualThread_klass();
2464     oop base = ik->static_field_base_raw();
2465     base->release_bool_field_put(static_notify_jvmti_events_offset, vthread_notify_jvmti_events);
2466   }
2467 }
2468 
2469 bool java_lang_VirtualThread::is_instance(oop obj) {
2470   return obj != NULL && is_subclass(obj->klass());
2471 }
2472 
2473 oop java_lang_VirtualThread::carrier_thread(oop vthread) {
2474   oop thread = vthread->obj_field(_carrierThread_offset);
2475   return thread;
2476 }
2477 
2478 oop java_lang_VirtualThread::continuation(oop vthread) {
2479   oop cont = vthread->obj_field(_continuation_offset);
2480   return cont;
2481 }
2482 
2483 jshort java_lang_VirtualThread::state(oop vthread) {
2484   return vthread->short_field_acquire(_state_offset);
2485 }
2486 
2487 JavaThreadStatus java_lang_VirtualThread::map_state_to_thread_status(jint state) {
2488   JavaThreadStatus status = JavaThreadStatus::NEW;
2489   switch (state) {
2490     case NEW :
2491       status = JavaThreadStatus::NEW;
2492       break;
2493     case STARTED :
2494     case RUNNABLE :
2495     case RUNNABLE_SUSPENDED :
2496     case RUNNING :
2497     case PARKING :
2498     case YIELDING :
2499       status = JavaThreadStatus::RUNNABLE;
2500       break;
2501     case PARKED :
2502     case PARKED_SUSPENDED :
2503     case PINNED :
2504       status = JavaThreadStatus::PARKED;
2505       break;
2506     case TERMINATED :
2507       status = JavaThreadStatus::TERMINATED;
2508       break;
2509     default:
2510       ShouldNotReachHere();
2511   }
2512   return status;
2513 }
2514 
2515 #if INCLUDE_CDS
2516 void java_lang_VirtualThread::serialize_offsets(SerializeClosure* f) {
2517    VTHREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2518 }
2519 #endif
2520 
2521 bool java_lang_VirtualThread::notify_jvmti_events() {
2522   return vthread_notify_jvmti_events == JNI_TRUE;
2523 }
2524 
2525 void java_lang_VirtualThread::set_notify_jvmti_events(jboolean enable) {
2526   vthread_notify_jvmti_events = enable;
2527 }
2528 
2529 
2530 // java_lang_Throwable
2531 
2532 int java_lang_Throwable::_backtrace_offset;
2533 int java_lang_Throwable::_detailMessage_offset;
2534 int java_lang_Throwable::_stackTrace_offset;
2535 int java_lang_Throwable::_depth_offset;
2536 int java_lang_Throwable::_cause_offset;
2537 int java_lang_Throwable::_static_unassigned_stacktrace_offset;
2538 
2539 #define THROWABLE_FIELDS_DO(macro) \
2540   macro(_backtrace_offset,     k, "backtrace",     object_signature,                  false); \
2541   macro(_detailMessage_offset, k, "detailMessage", string_signature,                  false); \
2542   macro(_stackTrace_offset,    k, "stackTrace",    java_lang_StackTraceElement_array, false); \
2543   macro(_depth_offset,         k, "depth",         int_signature,                     false); \
2544   macro(_cause_offset,         k, "cause",         throwable_signature,               false); \
2545   macro(_static_unassigned_stacktrace_offset, k, "UNASSIGNED_STACK", java_lang_StackTraceElement_array, true)
2546 
2547 void java_lang_Throwable::compute_offsets() {
2548   InstanceKlass* k = vmClasses::Throwable_klass();
2549   THROWABLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2550 }
2551 
2552 #if INCLUDE_CDS
2553 void java_lang_Throwable::serialize_offsets(SerializeClosure* f) {
2554   THROWABLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2555 }
2556 #endif
2557 
2558 oop java_lang_Throwable::unassigned_stacktrace() {
2559   InstanceKlass* ik = vmClasses::Throwable_klass();
2560   oop base = ik->static_field_base_raw();
2561   return base->obj_field(_static_unassigned_stacktrace_offset);
2562 }
2563 
2564 oop java_lang_Throwable::backtrace(oop throwable) {
2565   return throwable->obj_field_acquire(_backtrace_offset);
2566 }
2567 
2568 
2569 void java_lang_Throwable::set_backtrace(oop throwable, oop value) {
2570   throwable->release_obj_field_put(_backtrace_offset, value);
2571 }
2572 
2573 int java_lang_Throwable::depth(oop throwable) {
2574   return throwable->int_field(_depth_offset);
2575 }
2576 
2577 void java_lang_Throwable::set_depth(oop throwable, int value) {
2578   throwable->int_field_put(_depth_offset, value);
2579 }
2580 
2581 oop java_lang_Throwable::message(oop throwable) {
2582   return throwable->obj_field(_detailMessage_offset);
2583 }
2584 
2585 oop java_lang_Throwable::cause(oop throwable) {
2586   return throwable->obj_field(_cause_offset);
2587 }
2588 
2589 // Return Symbol for detailed_message or NULL
2590 Symbol* java_lang_Throwable::detail_message(oop throwable) {
2591   PreserveExceptionMark pm(Thread::current());
2592   oop detailed_message = java_lang_Throwable::message(throwable);
2593   if (detailed_message != NULL) {
2594     return java_lang_String::as_symbol(detailed_message);
2595   }
2596   return NULL;
2597 }
2598 
2599 void java_lang_Throwable::set_message(oop throwable, oop value) {
2600   throwable->obj_field_put(_detailMessage_offset, value);
2601 }
2602 
2603 
2604 void java_lang_Throwable::set_stacktrace(oop throwable, oop st_element_array) {
2605   throwable->obj_field_put(_stackTrace_offset, st_element_array);
2606 }
2607 
2608 void java_lang_Throwable::clear_stacktrace(oop throwable) {
2609   set_stacktrace(throwable, NULL);
2610 }
2611 
2612 
2613 void java_lang_Throwable::print(oop throwable, outputStream* st) {
2614   ResourceMark rm;
2615   Klass* k = throwable->klass();
2616   assert(k != NULL, "just checking");
2617   st->print("%s", k->external_name());
2618   oop msg = message(throwable);
2619   if (msg != NULL) {
2620     st->print(": %s", java_lang_String::as_utf8_string(msg));
2621   }
2622 }
2623 
2624 // After this many redefines, the stack trace is unreliable.
2625 const int MAX_VERSION = USHRT_MAX;
2626 
2627 static inline bool version_matches(Method* method, int version) {
2628   assert(version < MAX_VERSION, "version is too big");
2629   return method != NULL && (method->constants()->version() == version);
2630 }
2631 
2632 // Print stack trace element to resource allocated buffer
2633 static void print_stack_element_to_stream(outputStream* st, Handle mirror, int method_id,
2634                                           int version, int bci, Symbol* name) {
2635   ResourceMark rm;
2636 
2637   // Get strings and string lengths
2638   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror()));
2639   const char* klass_name  = holder->external_name();
2640   int buf_len = (int)strlen(klass_name);
2641 
2642   char* method_name = name->as_C_string();
2643   buf_len += (int)strlen(method_name);
2644 
2645   char* source_file_name = NULL;
2646   Symbol* source = Backtrace::get_source_file_name(holder, version);
2647   if (source != NULL) {
2648     source_file_name = source->as_C_string();
2649     buf_len += (int)strlen(source_file_name);
2650   }
2651 
2652   char *module_name = NULL, *module_version = NULL;
2653   ModuleEntry* module = holder->module();
2654   if (module->is_named()) {
2655     module_name = module->name()->as_C_string();
2656     buf_len += (int)strlen(module_name);
2657     if (module->version() != NULL) {
2658       module_version = module->version()->as_C_string();
2659       buf_len += (int)strlen(module_version);
2660     }
2661   }
2662 
2663   // Allocate temporary buffer with extra space for formatting and line number
2664   char* buf = NEW_RESOURCE_ARRAY(char, buf_len + 64);
2665 
2666   // Print stack trace line in buffer
2667   sprintf(buf, "\tat %s.%s(", klass_name, method_name);
2668 
2669   // Print module information
2670   if (module_name != NULL) {
2671     if (module_version != NULL) {
2672       sprintf(buf + (int)strlen(buf), "%s@%s/", module_name, module_version);
2673     } else {
2674       sprintf(buf + (int)strlen(buf), "%s/", module_name);
2675     }
2676   }
2677 
2678   // The method can be NULL if the requested class version is gone
2679   Method* method = holder->method_with_orig_idnum(method_id, version);
2680   if (!version_matches(method, version)) {
2681     strcat(buf, "Redefined)");
2682   } else {
2683     int line_number = Backtrace::get_line_number(method, bci);
2684     if (line_number == -2) {
2685       strcat(buf, "Native Method)");
2686     } else {
2687       if (source_file_name != NULL && (line_number != -1)) {
2688         // Sourcename and linenumber
2689         sprintf(buf + (int)strlen(buf), "%s:%d)", source_file_name, line_number);
2690       } else if (source_file_name != NULL) {
2691         // Just sourcename
2692         sprintf(buf + (int)strlen(buf), "%s)", source_file_name);
2693       } else {
2694         // Neither sourcename nor linenumber
2695         sprintf(buf + (int)strlen(buf), "Unknown Source)");
2696       }
2697       CompiledMethod* nm = method->code();
2698       if (WizardMode && nm != NULL) {
2699         sprintf(buf + (int)strlen(buf), "(nmethod " INTPTR_FORMAT ")", (intptr_t)nm);
2700       }
2701     }
2702   }
2703 
2704   st->print_cr("%s", buf);
2705 }
2706 
2707 void java_lang_Throwable::print_stack_element(outputStream *st, Method* method, int bci) {
2708   Handle mirror (Thread::current(),  method->method_holder()->java_mirror());
2709   int method_id = method->orig_method_idnum();
2710   int version = method->constants()->version();
2711   print_stack_element_to_stream(st, mirror, method_id, version, bci, method->name());
2712 }
2713 
2714 /**
2715  * Print the throwable message and its stack trace plus all causes by walking the
2716  * cause chain.  The output looks the same as of Throwable.printStackTrace().
2717  */
2718 void java_lang_Throwable::print_stack_trace(Handle throwable, outputStream* st) {
2719   // First, print the message.
2720   print(throwable(), st);
2721   st->cr();
2722 
2723   // Now print the stack trace.
2724   JavaThread* THREAD = JavaThread::current(); // For exception macros.
2725   while (throwable.not_null()) {
2726     objArrayHandle result (THREAD, objArrayOop(backtrace(throwable())));
2727     if (result.is_null()) {
2728       st->print_raw_cr("\t<<no stack trace available>>");
2729       return;
2730     }
2731     BacktraceIterator iter(result, THREAD);
2732 
2733     while (iter.repeat()) {
2734       BacktraceElement bte = iter.next(THREAD);
2735       print_stack_element_to_stream(st, bte._mirror, bte._method_id, bte._version, bte._bci, bte._name);
2736     }
2737     {
2738       // Call getCause() which doesn't necessarily return the _cause field.
2739       ExceptionMark em(THREAD);
2740       JavaValue cause(T_OBJECT);
2741       JavaCalls::call_virtual(&cause,
2742                               throwable,
2743                               throwable->klass(),
2744                               vmSymbols::getCause_name(),
2745                               vmSymbols::void_throwable_signature(),
2746                               THREAD);
2747       // Ignore any exceptions. we are in the middle of exception handling. Same as classic VM.
2748       if (HAS_PENDING_EXCEPTION) {
2749         CLEAR_PENDING_EXCEPTION;
2750         throwable = Handle();
2751       } else {
2752         throwable = Handle(THREAD, cause.get_oop());
2753         if (throwable.not_null()) {
2754           st->print("Caused by: ");
2755           print(throwable(), st);
2756           st->cr();
2757         }
2758       }
2759     }
2760   }
2761 }
2762 
2763 /**
2764  * Print the throwable stack trace by calling the Java method java.lang.Throwable.printStackTrace().
2765  */
2766 void java_lang_Throwable::java_printStackTrace(Handle throwable, TRAPS) {
2767   assert(throwable->is_a(vmClasses::Throwable_klass()), "Throwable instance expected");
2768   JavaValue result(T_VOID);
2769   JavaCalls::call_virtual(&result,
2770                           throwable,
2771                           vmClasses::Throwable_klass(),
2772                           vmSymbols::printStackTrace_name(),
2773                           vmSymbols::void_method_signature(),
2774                           THREAD);
2775 }
2776 
2777 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method, TRAPS) {
2778   if (!StackTraceInThrowable) return;
2779   ResourceMark rm(THREAD);
2780   HandleMark hm(THREAD);
2781 
2782   // Start out by clearing the backtrace for this object, in case the VM
2783   // runs out of memory while allocating the stack trace
2784   set_backtrace(throwable(), NULL);
2785   // Clear lazily constructed Java level stacktrace if refilling occurs
2786   // This is unnecessary in 1.7+ but harmless
2787   clear_stacktrace(throwable());
2788 
2789   int max_depth = MaxJavaStackTraceDepth;
2790   JavaThread* thread = THREAD;
2791 
2792   BacktraceBuilder bt(CHECK);
2793 
2794   // If there is no Java frame just return the method that was being called
2795   // with bci 0
2796   if (!thread->has_last_Java_frame()) {
2797     if (max_depth >= 1 && method() != NULL) {
2798       bt.push(method(), 0, NULL, CHECK);
2799       log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), 1);
2800       set_depth(throwable(), 1);
2801       set_backtrace(throwable(), bt.backtrace());
2802     }
2803     return;
2804   }
2805 
2806   // Instead of using vframe directly, this version of fill_in_stack_trace
2807   // basically handles everything by hand. This significantly improved the
2808   // speed of this method call up to 28.5% on Solaris sparc. 27.1% on Windows.
2809   // See bug 6333838 for  more details.
2810   // The "ASSERT" here is to verify this method generates the exactly same stack
2811   // trace as utilizing vframe.
2812 #ifdef ASSERT
2813   vframeStream st(thread, false /* stop_at_java_call_stub */, false /* process_frames */);
2814 #endif
2815   int total_count = 0;
2816   RegisterMap map(thread, false /* update */, false /* process_frames */, true /* walk_cont */);
2817   int decode_offset = 0;
2818   CompiledMethod* nm = NULL;
2819   bool skip_fillInStackTrace_check = false;
2820   bool skip_throwableInit_check = false;
2821   bool skip_hidden = !ShowHiddenFrames;
2822   bool show_carrier = ShowCarrierFrames;
2823   Handle cont_h(THREAD, thread->last_continuation()->cont_oop());
2824   for (frame fr = thread->last_frame(); max_depth == 0 || max_depth != total_count;) {
2825     Method* method = NULL;
2826     int bci = 0;
2827     oop contScopeName = (cont_h() != NULL) ? jdk_internal_vm_ContinuationScope::name(jdk_internal_vm_Continuation::scope(cont_h())) : (oop)NULL;
2828 
2829     // Compiled java method case.
2830     if (decode_offset != 0) {
2831       DebugInfoReadStream stream(nm, decode_offset);
2832       decode_offset = stream.read_int();
2833       method = (Method*)nm->metadata_at(stream.read_int());
2834       bci = stream.read_bci();
2835     } else {
2836       if (fr.is_first_frame()) break;
2837 
2838       if (cont_h() != NULL && Continuation::is_continuation_enterSpecial(fr)) {
2839         oop scope = jdk_internal_vm_Continuation::scope(cont_h());
2840         if (!show_carrier && scope == java_lang_VirtualThread::vthread_scope()) break;
2841 
2842         Handle parent_h(THREAD, jdk_internal_vm_Continuation::parent(cont_h()));
2843         cont_h =  parent_h;
2844       }
2845 
2846       address pc = fr.pc();
2847       if (fr.is_interpreted_frame()) {
2848         address bcp;
2849         if (!map.in_cont()) {
2850           bcp = fr.interpreter_frame_bcp();
2851           method = fr.interpreter_frame_method();
2852         } else {
2853           bcp = map.stack_chunk()->interpreter_frame_bcp(fr);
2854           method = map.stack_chunk()->interpreter_frame_method(fr);
2855         }
2856         bci =  method->bci_from(bcp);
2857         fr = fr.sender(&map);
2858       } else {
2859         CodeBlob* cb = fr.cb();
2860         // HMMM QQQ might be nice to have frame return nm as NULL if cb is non-NULL
2861         // but non nmethod
2862         fr = fr.sender(&map);
2863         if (cb == NULL || !cb->is_compiled()) {
2864           continue;
2865         }
2866         nm = cb->as_compiled_method();
2867         assert (nm->method() != NULL, "must be");
2868         if (nm->method()->is_native()) {
2869           method = nm->method();
2870           bci = 0;
2871         } else {
2872           PcDesc* pd = nm->pc_desc_at(pc);
2873           decode_offset = pd->scope_decode_offset();
2874           // if decode_offset is not equal to 0, it will execute the
2875           // "compiled java method case" at the beginning of the loop.
2876           continue;
2877         }
2878       }
2879     }
2880 #ifdef ASSERT
2881     if (!st.at_end()) { // TODO LOOM remove once we show only vthread trace
2882       assert(st.method() == method && st.bci() == bci, "Wrong stack trace");
2883       st.next();
2884     }
2885 #endif
2886 
2887     // the format of the stacktrace will be:
2888     // - 1 or more fillInStackTrace frames for the exception class (skipped)
2889     // - 0 or more <init> methods for the exception class (skipped)
2890     // - rest of the stack
2891 
2892     if (!skip_fillInStackTrace_check) {
2893       if (method->name() == vmSymbols::fillInStackTrace_name() &&
2894           throwable->is_a(method->method_holder())) {
2895         continue;
2896       }
2897       else {
2898         skip_fillInStackTrace_check = true; // gone past them all
2899       }
2900     }
2901     if (!skip_throwableInit_check) {
2902       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2903 
2904       // skip <init> methods of the exception class and superclasses
2905       // This is simlar to classic VM.
2906       if (method->name() == vmSymbols::object_initializer_name() &&
2907           throwable->is_a(method->method_holder())) {
2908         continue;
2909       } else {
2910         // there are none or we've seen them all - either way stop checking
2911         skip_throwableInit_check = true;
2912       }
2913     }
2914     if (method->is_hidden() || method->is_continuation_enter_intrinsic()) {
2915       if (skip_hidden) {
2916         if (total_count == 0) {
2917           // The top frame will be hidden from the stack trace.
2918           bt.set_has_hidden_top_frame(CHECK);
2919         }
2920         continue;
2921       }
2922     }
2923 
2924     bt.push(method, bci, contScopeName, CHECK);
2925     total_count++;
2926   }
2927 
2928   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), total_count);
2929 
2930   // Put completed stack trace into throwable object
2931   set_backtrace(throwable(), bt.backtrace());
2932   set_depth(throwable(), total_count);
2933 }
2934 
2935 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method) {
2936   // No-op if stack trace is disabled
2937   if (!StackTraceInThrowable) {
2938     return;
2939   }
2940 
2941   // Disable stack traces for some preallocated out of memory errors
2942   if (!Universe::should_fill_in_stack_trace(throwable)) {
2943     return;
2944   }
2945 
2946   JavaThread* THREAD = JavaThread::current(); // For exception macros.
2947   PreserveExceptionMark pm(THREAD);
2948 
2949   fill_in_stack_trace(throwable, method, THREAD);
2950   // Ignore exceptions thrown during stack trace filling (OOM) and reinstall the
2951   // original exception via the PreserveExceptionMark destructor.
2952   CLEAR_PENDING_EXCEPTION;
2953 }
2954 
2955 void java_lang_Throwable::allocate_backtrace(Handle throwable, TRAPS) {
2956   // Allocate stack trace - backtrace is created but not filled in
2957 
2958   // No-op if stack trace is disabled
2959   if (!StackTraceInThrowable) return;
2960   BacktraceBuilder bt(CHECK);   // creates a backtrace
2961   set_backtrace(throwable(), bt.backtrace());
2962 }
2963 
2964 
2965 void java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(Handle throwable) {
2966   // Fill in stack trace into preallocated backtrace (no GC)
2967 
2968   // No-op if stack trace is disabled
2969   if (!StackTraceInThrowable) return;
2970 
2971   assert(throwable->is_a(vmClasses::Throwable_klass()), "sanity check");
2972 
2973   JavaThread* THREAD = JavaThread::current(); // For exception macros.
2974 
2975   objArrayHandle backtrace (THREAD, (objArrayOop)java_lang_Throwable::backtrace(throwable()));
2976   assert(backtrace.not_null(), "backtrace should have been preallocated");
2977 
2978   ResourceMark rm(THREAD);
2979   vframeStream st(THREAD, false /* stop_at_java_call_stub */, false /* process_frames */);
2980 
2981   BacktraceBuilder bt(THREAD, backtrace);
2982 
2983   // Unlike fill_in_stack_trace we do not skip fillInStackTrace or throwable init
2984   // methods as preallocated errors aren't created by "java" code.
2985 
2986   // fill in as much stack trace as possible
2987   int chunk_count = 0;
2988   for (;!st.at_end(); st.next()) {
2989     bt.push(st.method(), st.bci(), NULL, CHECK);
2990     chunk_count++;
2991 
2992     // Bail-out for deep stacks
2993     if (chunk_count >= trace_chunk_size) break;
2994   }
2995   set_depth(throwable(), chunk_count);
2996   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), chunk_count);
2997 
2998   // We support the Throwable immutability protocol defined for Java 7.
2999   java_lang_Throwable::set_stacktrace(throwable(), java_lang_Throwable::unassigned_stacktrace());
3000   assert(java_lang_Throwable::unassigned_stacktrace() != NULL, "not initialized");
3001 }
3002 
3003 void java_lang_Throwable::get_stack_trace_elements(int depth, Handle backtrace,
3004                                                    objArrayHandle stack_trace_array_h, TRAPS) {
3005 
3006   if (backtrace.is_null() || stack_trace_array_h.is_null()) {
3007     THROW(vmSymbols::java_lang_NullPointerException());
3008   }
3009 
3010   assert(stack_trace_array_h->is_objArray(), "Stack trace array should be an array of StackTraceElenent");
3011 
3012   if (stack_trace_array_h->length() != depth) {
3013     THROW(vmSymbols::java_lang_IndexOutOfBoundsException());
3014   }
3015 
3016   objArrayHandle result(THREAD, objArrayOop(backtrace()));
3017   BacktraceIterator iter(result, THREAD);
3018 
3019   int index = 0;
3020   while (iter.repeat()) {
3021     BacktraceElement bte = iter.next(THREAD);
3022 
3023     Handle stack_trace_element(THREAD, stack_trace_array_h->obj_at(index++));
3024 
3025     if (stack_trace_element.is_null()) {
3026       THROW(vmSymbols::java_lang_NullPointerException());
3027     }
3028 
3029     InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror()));
3030     methodHandle method (THREAD, holder->method_with_orig_idnum(bte._method_id, bte._version));
3031 
3032     java_lang_StackTraceElement::fill_in(stack_trace_element, holder,
3033                                          method,
3034                                          bte._version,
3035                                          bte._bci,
3036                                          bte._name,
3037                                          bte._cont,
3038                                          CHECK);
3039   }
3040 }
3041 
3042 Handle java_lang_Throwable::get_cause_with_stack_trace(Handle throwable, TRAPS) {
3043   // Call to JVM to fill in the stack trace and clear declaringClassObject to
3044   // not keep classes alive in the stack trace.
3045   // call this:  public StackTraceElement[] getStackTrace()
3046   assert(throwable.not_null(), "shouldn't be");
3047 
3048   JavaValue result(T_ARRAY);
3049   JavaCalls::call_virtual(&result, throwable,
3050                           vmClasses::Throwable_klass(),
3051                           vmSymbols::getStackTrace_name(),
3052                           vmSymbols::getStackTrace_signature(),
3053                           CHECK_NH);
3054   Handle stack_trace(THREAD, result.get_oop());
3055   assert(stack_trace->is_objArray(), "Should be an array");
3056 
3057   // Throw ExceptionInInitializerError as the cause with this exception in
3058   // the message and stack trace.
3059 
3060   // Now create the message with the original exception and thread name.
3061   Symbol* message = java_lang_Throwable::detail_message(throwable());
3062   ResourceMark rm(THREAD);
3063   stringStream st;
3064   st.print("Exception %s%s ", throwable()->klass()->name()->as_klass_external_name(),
3065              message == nullptr ? "" : ":");
3066   if (message == NULL) {
3067     st.print("[in thread \"%s\"]", THREAD->name());
3068   } else {
3069     st.print("%s [in thread \"%s\"]", message->as_C_string(), THREAD->name());
3070   }
3071 
3072   Symbol* exception_name = vmSymbols::java_lang_ExceptionInInitializerError();
3073   Handle h_cause = Exceptions::new_exception(THREAD, exception_name, st.as_string());
3074 
3075   // If new_exception returns a different exception while creating the exception, return null.
3076   if (h_cause->klass()->name() != exception_name) {
3077     log_info(class, init)("Exception thrown while saving initialization exception %s",
3078                           h_cause->klass()->external_name());
3079     return Handle();
3080   }
3081   java_lang_Throwable::set_stacktrace(h_cause(), stack_trace());
3082   // Clear backtrace because the stacktrace should be used instead.
3083   set_backtrace(h_cause(), NULL);
3084   return h_cause;
3085 }
3086 
3087 bool java_lang_Throwable::get_top_method_and_bci(oop throwable, Method** method, int* bci) {
3088   JavaThread* current = JavaThread::current();
3089   objArrayHandle result(current, objArrayOop(backtrace(throwable)));
3090   BacktraceIterator iter(result, current);
3091   // No backtrace available.
3092   if (!iter.repeat()) return false;
3093 
3094   // If the exception happened in a frame that has been hidden, i.e.,
3095   // omitted from the back trace, we can not compute the message.
3096   oop hidden = ((objArrayOop)backtrace(throwable))->obj_at(trace_hidden_offset);
3097   if (hidden != NULL) {
3098     return false;
3099   }
3100 
3101   // Get first backtrace element.
3102   BacktraceElement bte = iter.next(current);
3103 
3104   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror()));
3105   assert(holder != NULL, "first element should be non-null");
3106   Method* m = holder->method_with_orig_idnum(bte._method_id, bte._version);
3107 
3108   // Original version is no longer available.
3109   if (m == NULL || !version_matches(m, bte._version)) {
3110     return false;
3111   }
3112 
3113   *method = m;
3114   *bci = bte._bci;
3115   return true;
3116 }
3117 
3118 oop java_lang_StackTraceElement::create(const methodHandle& method, int bci, Handle contScope, TRAPS) {
3119   // Allocate java.lang.StackTraceElement instance
3120   InstanceKlass* k = vmClasses::StackTraceElement_klass();
3121   assert(k != NULL, "must be loaded in 1.4+");
3122   if (k->should_be_initialized()) {
3123     k->initialize(CHECK_NULL);
3124   }
3125 
3126   Handle element = k->allocate_instance_handle(CHECK_NULL);
3127 
3128   int version = method->constants()->version();
3129   fill_in(element, method->method_holder(), method, version, bci, method->name(), contScope, CHECK_NULL);
3130   return element();
3131 }
3132 
3133 void java_lang_StackTraceElement::fill_in(Handle element,
3134                                           InstanceKlass* holder, const methodHandle& method,
3135                                           int version, int bci, Symbol* name, Handle contScopeName, TRAPS) {
3136   assert(element->is_a(vmClasses::StackTraceElement_klass()), "sanity check");
3137 
3138   ResourceMark rm(THREAD);
3139   HandleMark hm(THREAD);
3140 
3141   // Fill in class name
3142   Handle java_class(THREAD, holder->java_mirror());
3143   oop classname = java_lang_Class::name(java_class, CHECK);
3144   java_lang_StackTraceElement::set_declaringClass(element(), classname);
3145   java_lang_StackTraceElement::set_declaringClassObject(element(), java_class());
3146 
3147   oop loader = holder->class_loader();
3148   if (loader != NULL) {
3149     oop loader_name = java_lang_ClassLoader::name(loader);
3150     if (loader_name != NULL)
3151       java_lang_StackTraceElement::set_classLoaderName(element(), loader_name);
3152   }
3153 
3154   // Fill in method name
3155   oop methodname = StringTable::intern(name, CHECK);
3156   java_lang_StackTraceElement::set_methodName(element(), methodname);
3157 
3158   // Fill in module name and version
3159   ModuleEntry* module = holder->module();
3160   if (module->is_named()) {
3161     oop module_name = StringTable::intern(module->name(), CHECK);
3162     java_lang_StackTraceElement::set_moduleName(element(), module_name);
3163     oop module_version;
3164     if (module->version() != NULL) {
3165       module_version = StringTable::intern(module->version(), CHECK);
3166     } else {
3167       module_version = NULL;
3168     }
3169     java_lang_StackTraceElement::set_moduleVersion(element(), module_version);
3170   }
3171 
3172   if (method() == NULL || !version_matches(method(), version)) {
3173     // The method was redefined, accurate line number information isn't available
3174     java_lang_StackTraceElement::set_fileName(element(), NULL);
3175     java_lang_StackTraceElement::set_lineNumber(element(), -1);
3176   } else {
3177     Symbol* source;
3178     oop source_file;
3179     int line_number;
3180     decode_file_and_line(java_class, holder, version, method, bci, source, source_file, line_number, CHECK);
3181 
3182     java_lang_StackTraceElement::set_fileName(element(), source_file);
3183     java_lang_StackTraceElement::set_lineNumber(element(), line_number);
3184   }
3185 
3186   // Fill in continuation scope
3187   java_lang_StackTraceElement::set_contScopeName(element(), contScopeName());
3188 }
3189 
3190 void java_lang_StackTraceElement::decode_file_and_line(Handle java_class,
3191                                                        InstanceKlass* holder,
3192                                                        int version,
3193                                                        const methodHandle& method,
3194                                                        int bci,
3195                                                        Symbol*& source,
3196                                                        oop& source_file,
3197                                                        int& line_number, TRAPS) {
3198   // Fill in source file name and line number.
3199   source = Backtrace::get_source_file_name(holder, version);
3200   source_file = java_lang_Class::source_file(java_class());
3201   if (source != NULL) {
3202     // Class was not redefined. We can trust its cache if set,
3203     // else we have to initialize it.
3204     if (source_file == NULL) {
3205       source_file = StringTable::intern(source, CHECK);
3206       java_lang_Class::set_source_file(java_class(), source_file);
3207     }
3208   } else {
3209     // Class was redefined. Dump the cache if it was set.
3210     if (source_file != NULL) {
3211       source_file = NULL;
3212       java_lang_Class::set_source_file(java_class(), source_file);
3213     }
3214   }
3215   line_number = Backtrace::get_line_number(method(), bci);
3216 }
3217 
3218 #if INCLUDE_JVMCI
3219 void java_lang_StackTraceElement::decode(const methodHandle& method, int bci,
3220                                          Symbol*& filename, int& line_number, TRAPS) {
3221   ResourceMark rm(THREAD);
3222   HandleMark hm(THREAD);
3223 
3224   filename = NULL;
3225   line_number = -1;
3226 
3227   oop source_file;
3228   int version = method->constants()->version();
3229   InstanceKlass* holder = method->method_holder();
3230   Handle java_class(THREAD, holder->java_mirror());
3231   decode_file_and_line(java_class, holder, version, method, bci, filename, source_file, line_number, CHECK);
3232 }
3233 #endif // INCLUDE_JVMCI
3234 
3235 // java_lang_StackFrameInfo
3236 
3237 int java_lang_StackFrameInfo::_memberName_offset;
3238 int java_lang_StackFrameInfo::_bci_offset;
3239 int java_lang_StackFrameInfo::_version_offset;
3240 int java_lang_StackFrameInfo::_contScope_offset;
3241 
3242 #define STACKFRAMEINFO_FIELDS_DO(macro) \
3243   macro(_memberName_offset,     k, "memberName",  object_signature, false); \
3244   macro(_bci_offset,            k, "bci",         int_signature,    false); \
3245   macro(_contScope_offset,      k, "contScope",   continuationscope_signature, false)
3246 
3247 void java_lang_StackFrameInfo::compute_offsets() {
3248   InstanceKlass* k = vmClasses::StackFrameInfo_klass();
3249   STACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3250   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3251 }
3252 
3253 #if INCLUDE_CDS
3254 void java_lang_StackFrameInfo::serialize_offsets(SerializeClosure* f) {
3255   STACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3256   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3257 }
3258 #endif
3259 
3260 Method* java_lang_StackFrameInfo::get_method(Handle stackFrame, InstanceKlass* holder, TRAPS) {
3261   HandleMark hm(THREAD);
3262   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
3263   Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mname());
3264   // we should expand MemberName::name when Throwable uses StackTrace
3265   // MethodHandles::expand_MemberName(mname, MethodHandles::_suppress_defc|MethodHandles::_suppress_type, CHECK_NULL);
3266   return method;
3267 }
3268 
3269 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci, oop cont, TRAPS) {
3270   // set Method* or mid/cpref
3271   HandleMark hm(THREAD);
3272   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
3273   Handle cont_h (THREAD, cont);
3274   InstanceKlass* ik = method->method_holder();
3275   CallInfo info(method(), ik, CHECK);
3276   MethodHandles::init_method_MemberName(mname, info);
3277   // set bci
3278   java_lang_StackFrameInfo::set_bci(stackFrame(), bci);
3279   // method may be redefined; store the version
3280   int version = method->constants()->version();
3281   assert((jushort)version == version, "version should be short");
3282   java_lang_StackFrameInfo::set_version(stackFrame(), (short)version);
3283 
3284   oop contScope = cont_h() != NULL ? jdk_internal_vm_Continuation::scope(cont_h()) : (oop)NULL;
3285   java_lang_StackFrameInfo::set_contScope(stackFrame(), contScope);
3286 }
3287 
3288 void java_lang_StackFrameInfo::to_stack_trace_element(Handle stackFrame, Handle stack_trace_element, TRAPS) {
3289   ResourceMark rm(THREAD);
3290   HandleMark hm(THREAD);
3291   Handle mname(THREAD, stackFrame->obj_field(java_lang_StackFrameInfo::_memberName_offset));
3292   Klass* clazz = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname()));
3293   InstanceKlass* holder = InstanceKlass::cast(clazz);
3294   Method* method = java_lang_StackFrameInfo::get_method(stackFrame, holder, CHECK);
3295   oop contScope = stackFrame->obj_field(java_lang_StackFrameInfo::_contScope_offset);
3296   Handle contScopeName(THREAD, contScope != (oop)NULL ? jdk_internal_vm_ContinuationScope::name(contScope) : (oop)NULL);
3297 
3298   short version = stackFrame->short_field(_version_offset);
3299   int bci = stackFrame->int_field(_bci_offset);
3300   Symbol* name = method->name();
3301   java_lang_StackTraceElement::fill_in(stack_trace_element, holder, methodHandle(THREAD, method), version, bci, name, contScopeName, CHECK);
3302 }
3303 
3304 void java_lang_StackTraceElement::set_contScopeName(oop element, oop value) {
3305   element->obj_field_put(_contScopeName_offset, value);
3306 }
3307 
3308 void java_lang_StackFrameInfo::set_version(oop element, short value) {
3309   element->short_field_put(_version_offset, value);
3310 }
3311 
3312 void java_lang_StackFrameInfo::set_bci(oop element, int value) {
3313   assert(value >= 0 && value < max_jushort, "must be a valid bci value");
3314   element->int_field_put(_bci_offset, value);
3315 }
3316 
3317 void java_lang_StackFrameInfo::set_contScope(oop element, oop value) {
3318   element->obj_field_put(_contScope_offset, value);
3319 }
3320 
3321 int java_lang_LiveStackFrameInfo::_monitors_offset;
3322 int java_lang_LiveStackFrameInfo::_locals_offset;
3323 int java_lang_LiveStackFrameInfo::_operands_offset;
3324 int java_lang_LiveStackFrameInfo::_mode_offset;
3325 
3326 #define LIVESTACKFRAMEINFO_FIELDS_DO(macro) \
3327   macro(_monitors_offset,   k, "monitors",    object_array_signature, false); \
3328   macro(_locals_offset,     k, "locals",      object_array_signature, false); \
3329   macro(_operands_offset,   k, "operands",    object_array_signature, false); \
3330   macro(_mode_offset,       k, "mode",        int_signature,          false)
3331 
3332 void java_lang_LiveStackFrameInfo::compute_offsets() {
3333   InstanceKlass* k = vmClasses::LiveStackFrameInfo_klass();
3334   LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3335 }
3336 
3337 #if INCLUDE_CDS
3338 void java_lang_LiveStackFrameInfo::serialize_offsets(SerializeClosure* f) {
3339   LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3340 }
3341 #endif
3342 
3343 void java_lang_LiveStackFrameInfo::set_monitors(oop element, oop value) {
3344   element->obj_field_put(_monitors_offset, value);
3345 }
3346 
3347 void java_lang_LiveStackFrameInfo::set_locals(oop element, oop value) {
3348   element->obj_field_put(_locals_offset, value);
3349 }
3350 
3351 void java_lang_LiveStackFrameInfo::set_operands(oop element, oop value) {
3352   element->obj_field_put(_operands_offset, value);
3353 }
3354 
3355 void java_lang_LiveStackFrameInfo::set_mode(oop element, int value) {
3356   element->int_field_put(_mode_offset, value);
3357 }
3358 
3359 
3360 // java_lang_AccessibleObject
3361 
3362 int java_lang_reflect_AccessibleObject::_override_offset;
3363 
3364 #define ACCESSIBLEOBJECT_FIELDS_DO(macro) \
3365   macro(_override_offset, k, "override", bool_signature, false)
3366 
3367 void java_lang_reflect_AccessibleObject::compute_offsets() {
3368   InstanceKlass* k = vmClasses::reflect_AccessibleObject_klass();
3369   ACCESSIBLEOBJECT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3370 }
3371 
3372 #if INCLUDE_CDS
3373 void java_lang_reflect_AccessibleObject::serialize_offsets(SerializeClosure* f) {
3374   ACCESSIBLEOBJECT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3375 }
3376 #endif
3377 
3378 jboolean java_lang_reflect_AccessibleObject::override(oop reflect) {
3379   return (jboolean) reflect->bool_field(_override_offset);
3380 }
3381 
3382 void java_lang_reflect_AccessibleObject::set_override(oop reflect, jboolean value) {
3383   reflect->bool_field_put(_override_offset, (int) value);
3384 }
3385 
3386 // java_lang_reflect_Method
3387 
3388 int java_lang_reflect_Method::_clazz_offset;
3389 int java_lang_reflect_Method::_name_offset;
3390 int java_lang_reflect_Method::_returnType_offset;
3391 int java_lang_reflect_Method::_parameterTypes_offset;
3392 int java_lang_reflect_Method::_exceptionTypes_offset;
3393 int java_lang_reflect_Method::_slot_offset;
3394 int java_lang_reflect_Method::_modifiers_offset;
3395 int java_lang_reflect_Method::_signature_offset;
3396 int java_lang_reflect_Method::_annotations_offset;
3397 int java_lang_reflect_Method::_parameter_annotations_offset;
3398 int java_lang_reflect_Method::_annotation_default_offset;
3399 
3400 #define METHOD_FIELDS_DO(macro) \
3401   macro(_clazz_offset,          k, vmSymbols::clazz_name(),          class_signature,       false); \
3402   macro(_name_offset,           k, vmSymbols::name_name(),           string_signature,      false); \
3403   macro(_returnType_offset,     k, vmSymbols::returnType_name(),     class_signature,       false); \
3404   macro(_parameterTypes_offset, k, vmSymbols::parameterTypes_name(), class_array_signature, false); \
3405   macro(_exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), class_array_signature, false); \
3406   macro(_slot_offset,           k, vmSymbols::slot_name(),           int_signature,         false); \
3407   macro(_modifiers_offset,      k, vmSymbols::modifiers_name(),      int_signature,         false); \
3408   macro(_signature_offset,             k, vmSymbols::signature_name(),             string_signature,     false); \
3409   macro(_annotations_offset,           k, vmSymbols::annotations_name(),           byte_array_signature, false); \
3410   macro(_parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), byte_array_signature, false); \
3411   macro(_annotation_default_offset,    k, vmSymbols::annotation_default_name(),    byte_array_signature, false);
3412 
3413 void java_lang_reflect_Method::compute_offsets() {
3414   InstanceKlass* k = vmClasses::reflect_Method_klass();
3415   METHOD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3416 }
3417 
3418 #if INCLUDE_CDS
3419 void java_lang_reflect_Method::serialize_offsets(SerializeClosure* f) {
3420   METHOD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3421 }
3422 #endif
3423 
3424 Handle java_lang_reflect_Method::create(TRAPS) {
3425   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3426   Klass* klass = vmClasses::reflect_Method_klass();
3427   // This class is eagerly initialized during VM initialization, since we keep a refence
3428   // to one of the methods
3429   assert(InstanceKlass::cast(klass)->is_initialized(), "must be initialized");
3430   return InstanceKlass::cast(klass)->allocate_instance_handle(THREAD);
3431 }
3432 
3433 oop java_lang_reflect_Method::clazz(oop reflect) {
3434   return reflect->obj_field(_clazz_offset);
3435 }
3436 
3437 void java_lang_reflect_Method::set_clazz(oop reflect, oop value) {
3438    reflect->obj_field_put(_clazz_offset, value);
3439 }
3440 
3441 int java_lang_reflect_Method::slot(oop reflect) {
3442   return reflect->int_field(_slot_offset);
3443 }
3444 
3445 void java_lang_reflect_Method::set_slot(oop reflect, int value) {
3446   reflect->int_field_put(_slot_offset, value);
3447 }
3448 
3449 void java_lang_reflect_Method::set_name(oop method, oop value) {
3450   method->obj_field_put(_name_offset, value);
3451 }
3452 
3453 oop java_lang_reflect_Method::return_type(oop method) {
3454   return method->obj_field(_returnType_offset);
3455 }
3456 
3457 void java_lang_reflect_Method::set_return_type(oop method, oop value) {
3458   method->obj_field_put(_returnType_offset, value);
3459 }
3460 
3461 oop java_lang_reflect_Method::parameter_types(oop method) {
3462   return method->obj_field(_parameterTypes_offset);
3463 }
3464 
3465 void java_lang_reflect_Method::set_parameter_types(oop method, oop value) {
3466   method->obj_field_put(_parameterTypes_offset, value);
3467 }
3468 
3469 void java_lang_reflect_Method::set_exception_types(oop method, oop value) {
3470   method->obj_field_put(_exceptionTypes_offset, value);
3471 }
3472 
3473 void java_lang_reflect_Method::set_modifiers(oop method, int value) {
3474   method->int_field_put(_modifiers_offset, value);
3475 }
3476 
3477 void java_lang_reflect_Method::set_signature(oop method, oop value) {
3478   method->obj_field_put(_signature_offset, value);
3479 }
3480 
3481 void java_lang_reflect_Method::set_annotations(oop method, oop value) {
3482   method->obj_field_put(_annotations_offset, value);
3483 }
3484 
3485 void java_lang_reflect_Method::set_parameter_annotations(oop method, oop value) {
3486   method->obj_field_put(_parameter_annotations_offset, value);
3487 }
3488 
3489 void java_lang_reflect_Method::set_annotation_default(oop method, oop value) {
3490   method->obj_field_put(_annotation_default_offset, value);
3491 }
3492 
3493 int java_lang_reflect_Constructor::_clazz_offset;
3494 int java_lang_reflect_Constructor::_parameterTypes_offset;
3495 int java_lang_reflect_Constructor::_exceptionTypes_offset;
3496 int java_lang_reflect_Constructor::_slot_offset;
3497 int java_lang_reflect_Constructor::_modifiers_offset;
3498 int java_lang_reflect_Constructor::_signature_offset;
3499 int java_lang_reflect_Constructor::_annotations_offset;
3500 int java_lang_reflect_Constructor::_parameter_annotations_offset;
3501 
3502 #define CONSTRUCTOR_FIELDS_DO(macro) \
3503   macro(_clazz_offset,          k, vmSymbols::clazz_name(),          class_signature,       false); \
3504   macro(_parameterTypes_offset, k, vmSymbols::parameterTypes_name(), class_array_signature, false); \
3505   macro(_exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), class_array_signature, false); \
3506   macro(_slot_offset,           k, vmSymbols::slot_name(),           int_signature,         false); \
3507   macro(_modifiers_offset,      k, vmSymbols::modifiers_name(),      int_signature,         false); \
3508   macro(_signature_offset,             k, vmSymbols::signature_name(),             string_signature,     false); \
3509   macro(_annotations_offset,           k, vmSymbols::annotations_name(),           byte_array_signature, false); \
3510   macro(_parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), byte_array_signature, false);
3511 
3512 void java_lang_reflect_Constructor::compute_offsets() {
3513   InstanceKlass* k = vmClasses::reflect_Constructor_klass();
3514   CONSTRUCTOR_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3515 }
3516 
3517 #if INCLUDE_CDS
3518 void java_lang_reflect_Constructor::serialize_offsets(SerializeClosure* f) {
3519   CONSTRUCTOR_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3520 }
3521 #endif
3522 
3523 Handle java_lang_reflect_Constructor::create(TRAPS) {
3524   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3525   Symbol* name = vmSymbols::java_lang_reflect_Constructor();
3526   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3527   InstanceKlass* ik = InstanceKlass::cast(k);
3528   // Ensure it is initialized
3529   ik->initialize(CHECK_NH);
3530   return ik->allocate_instance_handle(THREAD);
3531 }
3532 
3533 oop java_lang_reflect_Constructor::clazz(oop reflect) {
3534   return reflect->obj_field(_clazz_offset);
3535 }
3536 
3537 void java_lang_reflect_Constructor::set_clazz(oop reflect, oop value) {
3538    reflect->obj_field_put(_clazz_offset, value);
3539 }
3540 
3541 oop java_lang_reflect_Constructor::parameter_types(oop constructor) {
3542   return constructor->obj_field(_parameterTypes_offset);
3543 }
3544 
3545 void java_lang_reflect_Constructor::set_parameter_types(oop constructor, oop value) {
3546   constructor->obj_field_put(_parameterTypes_offset, value);
3547 }
3548 
3549 void java_lang_reflect_Constructor::set_exception_types(oop constructor, oop value) {
3550   constructor->obj_field_put(_exceptionTypes_offset, value);
3551 }
3552 
3553 int java_lang_reflect_Constructor::slot(oop reflect) {
3554   return reflect->int_field(_slot_offset);
3555 }
3556 
3557 void java_lang_reflect_Constructor::set_slot(oop reflect, int value) {
3558   reflect->int_field_put(_slot_offset, value);
3559 }
3560 
3561 void java_lang_reflect_Constructor::set_modifiers(oop constructor, int value) {
3562   constructor->int_field_put(_modifiers_offset, value);
3563 }
3564 
3565 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) {
3566   constructor->obj_field_put(_signature_offset, value);
3567 }
3568 
3569 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) {
3570   constructor->obj_field_put(_annotations_offset, value);
3571 }
3572 
3573 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) {
3574   method->obj_field_put(_parameter_annotations_offset, value);
3575 }
3576 
3577 int java_lang_reflect_Field::_clazz_offset;
3578 int java_lang_reflect_Field::_name_offset;
3579 int java_lang_reflect_Field::_type_offset;
3580 int java_lang_reflect_Field::_slot_offset;
3581 int java_lang_reflect_Field::_modifiers_offset;
3582 int java_lang_reflect_Field::_trusted_final_offset;
3583 int java_lang_reflect_Field::_signature_offset;
3584 int java_lang_reflect_Field::_annotations_offset;
3585 
3586 #define FIELD_FIELDS_DO(macro) \
3587   macro(_clazz_offset,     k, vmSymbols::clazz_name(),     class_signature,  false); \
3588   macro(_name_offset,      k, vmSymbols::name_name(),      string_signature, false); \
3589   macro(_type_offset,      k, vmSymbols::type_name(),      class_signature,  false); \
3590   macro(_slot_offset,      k, vmSymbols::slot_name(),      int_signature,    false); \
3591   macro(_modifiers_offset, k, vmSymbols::modifiers_name(), int_signature,    false); \
3592   macro(_trusted_final_offset,    k, vmSymbols::trusted_final_name(),    bool_signature,       false); \
3593   macro(_signature_offset,        k, vmSymbols::signature_name(),        string_signature,     false); \
3594   macro(_annotations_offset,      k, vmSymbols::annotations_name(),      byte_array_signature, false);
3595 
3596 void java_lang_reflect_Field::compute_offsets() {
3597   InstanceKlass* k = vmClasses::reflect_Field_klass();
3598   FIELD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3599 }
3600 
3601 #if INCLUDE_CDS
3602 void java_lang_reflect_Field::serialize_offsets(SerializeClosure* f) {
3603   FIELD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3604 }
3605 #endif
3606 
3607 Handle java_lang_reflect_Field::create(TRAPS) {
3608   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3609   Symbol* name = vmSymbols::java_lang_reflect_Field();
3610   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3611   InstanceKlass* ik = InstanceKlass::cast(k);
3612   // Ensure it is initialized
3613   ik->initialize(CHECK_NH);
3614   return ik->allocate_instance_handle(THREAD);
3615 }
3616 
3617 oop java_lang_reflect_Field::clazz(oop reflect) {
3618   return reflect->obj_field(_clazz_offset);
3619 }
3620 
3621 void java_lang_reflect_Field::set_clazz(oop reflect, oop value) {
3622   reflect->obj_field_put(_clazz_offset, value);
3623 }
3624 
3625 oop java_lang_reflect_Field::name(oop field) {
3626   return field->obj_field(_name_offset);
3627 }
3628 
3629 void java_lang_reflect_Field::set_name(oop field, oop value) {
3630   field->obj_field_put(_name_offset, value);
3631 }
3632 
3633 oop java_lang_reflect_Field::type(oop field) {
3634   return field->obj_field(_type_offset);
3635 }
3636 
3637 void java_lang_reflect_Field::set_type(oop field, oop value) {
3638   field->obj_field_put(_type_offset, value);
3639 }
3640 
3641 int java_lang_reflect_Field::slot(oop reflect) {
3642   return reflect->int_field(_slot_offset);
3643 }
3644 
3645 void java_lang_reflect_Field::set_slot(oop reflect, int value) {
3646   reflect->int_field_put(_slot_offset, value);
3647 }
3648 
3649 int java_lang_reflect_Field::modifiers(oop field) {
3650   return field->int_field(_modifiers_offset);
3651 }
3652 
3653 void java_lang_reflect_Field::set_modifiers(oop field, int value) {
3654   field->int_field_put(_modifiers_offset, value);
3655 }
3656 
3657 void java_lang_reflect_Field::set_trusted_final(oop field) {
3658   field->bool_field_put(_trusted_final_offset, true);
3659 }
3660 
3661 void java_lang_reflect_Field::set_signature(oop field, oop value) {
3662   field->obj_field_put(_signature_offset, value);
3663 }
3664 
3665 void java_lang_reflect_Field::set_annotations(oop field, oop value) {
3666   field->obj_field_put(_annotations_offset, value);
3667 }
3668 
3669 oop java_lang_reflect_RecordComponent::create(InstanceKlass* holder, RecordComponent* component, TRAPS) {
3670   // Allocate java.lang.reflect.RecordComponent instance
3671   HandleMark hm(THREAD);
3672   InstanceKlass* ik = vmClasses::RecordComponent_klass();
3673   assert(ik != NULL, "must be loaded");
3674   ik->initialize(CHECK_NULL);
3675 
3676   Handle element = ik->allocate_instance_handle(CHECK_NULL);
3677 
3678   Handle decl_class(THREAD, holder->java_mirror());
3679   java_lang_reflect_RecordComponent::set_clazz(element(), decl_class());
3680 
3681   Symbol* name = holder->constants()->symbol_at(component->name_index()); // name_index is a utf8
3682   oop component_name = StringTable::intern(name, CHECK_NULL);
3683   java_lang_reflect_RecordComponent::set_name(element(), component_name);
3684 
3685   Symbol* type = holder->constants()->symbol_at(component->descriptor_index());
3686   Handle component_type_h =
3687     SystemDictionary::find_java_mirror_for_type(type, holder, SignatureStream::NCDFError, CHECK_NULL);
3688   java_lang_reflect_RecordComponent::set_type(element(), component_type_h());
3689 
3690   Method* accessor_method = NULL;
3691   {
3692     // Prepend "()" to type to create the full method signature.
3693     ResourceMark rm(THREAD);
3694     int sig_len = type->utf8_length() + 3; // "()" and null char
3695     char* sig = NEW_RESOURCE_ARRAY(char, sig_len);
3696     jio_snprintf(sig, sig_len, "%c%c%s", JVM_SIGNATURE_FUNC, JVM_SIGNATURE_ENDFUNC, type->as_C_string());
3697     TempNewSymbol full_sig = SymbolTable::new_symbol(sig);
3698     accessor_method = holder->find_instance_method(name, full_sig, Klass::PrivateLookupMode::find);
3699   }
3700 
3701   if (accessor_method != NULL) {
3702     methodHandle method(THREAD, accessor_method);
3703     oop m = Reflection::new_method(method, false, CHECK_NULL);
3704     java_lang_reflect_RecordComponent::set_accessor(element(), m);
3705   } else {
3706     java_lang_reflect_RecordComponent::set_accessor(element(), NULL);
3707   }
3708 
3709   int sig_index = component->generic_signature_index();
3710   if (sig_index > 0) {
3711     Symbol* sig = holder->constants()->symbol_at(sig_index); // sig_index is a utf8
3712     oop component_sig = StringTable::intern(sig, CHECK_NULL);
3713     java_lang_reflect_RecordComponent::set_signature(element(), component_sig);
3714   } else {
3715     java_lang_reflect_RecordComponent::set_signature(element(), NULL);
3716   }
3717 
3718   typeArrayOop annotation_oop = Annotations::make_java_array(component->annotations(), CHECK_NULL);
3719   java_lang_reflect_RecordComponent::set_annotations(element(), annotation_oop);
3720 
3721   typeArrayOop type_annotation_oop = Annotations::make_java_array(component->type_annotations(), CHECK_NULL);
3722   java_lang_reflect_RecordComponent::set_typeAnnotations(element(), type_annotation_oop);
3723 
3724   return element();
3725 }
3726 
3727 int reflect_ConstantPool::_oop_offset;
3728 
3729 #define CONSTANTPOOL_FIELDS_DO(macro) \
3730   macro(_oop_offset, k, "constantPoolOop", object_signature, false)
3731 
3732 void reflect_ConstantPool::compute_offsets() {
3733   InstanceKlass* k = vmClasses::reflect_ConstantPool_klass();
3734   // The field is called ConstantPool* in the sun.reflect.ConstantPool class.
3735   CONSTANTPOOL_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3736 }
3737 
3738 #if INCLUDE_CDS
3739 void reflect_ConstantPool::serialize_offsets(SerializeClosure* f) {
3740   CONSTANTPOOL_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3741 }
3742 #endif
3743 
3744 int java_lang_reflect_Parameter::_name_offset;
3745 int java_lang_reflect_Parameter::_modifiers_offset;
3746 int java_lang_reflect_Parameter::_index_offset;
3747 int java_lang_reflect_Parameter::_executable_offset;
3748 
3749 #define PARAMETER_FIELDS_DO(macro) \
3750   macro(_name_offset,        k, vmSymbols::name_name(),        string_signature, false); \
3751   macro(_modifiers_offset,   k, vmSymbols::modifiers_name(),   int_signature,    false); \
3752   macro(_index_offset,       k, vmSymbols::index_name(),       int_signature,    false); \
3753   macro(_executable_offset,  k, vmSymbols::executable_name(),  executable_signature, false)
3754 
3755 void java_lang_reflect_Parameter::compute_offsets() {
3756   InstanceKlass* k = vmClasses::reflect_Parameter_klass();
3757   PARAMETER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3758 }
3759 
3760 #if INCLUDE_CDS
3761 void java_lang_reflect_Parameter::serialize_offsets(SerializeClosure* f) {
3762   PARAMETER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3763 }
3764 #endif
3765 
3766 Handle java_lang_reflect_Parameter::create(TRAPS) {
3767   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3768   Symbol* name = vmSymbols::java_lang_reflect_Parameter();
3769   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3770   InstanceKlass* ik = InstanceKlass::cast(k);
3771   // Ensure it is initialized
3772   ik->initialize(CHECK_NH);
3773   return ik->allocate_instance_handle(THREAD);
3774 }
3775 
3776 oop java_lang_reflect_Parameter::name(oop param) {
3777   return param->obj_field(_name_offset);
3778 }
3779 
3780 void java_lang_reflect_Parameter::set_name(oop param, oop value) {
3781   param->obj_field_put(_name_offset, value);
3782 }
3783 
3784 int java_lang_reflect_Parameter::modifiers(oop param) {
3785   return param->int_field(_modifiers_offset);
3786 }
3787 
3788 void java_lang_reflect_Parameter::set_modifiers(oop param, int value) {
3789   param->int_field_put(_modifiers_offset, value);
3790 }
3791 
3792 int java_lang_reflect_Parameter::index(oop param) {
3793   return param->int_field(_index_offset);
3794 }
3795 
3796 void java_lang_reflect_Parameter::set_index(oop param, int value) {
3797   param->int_field_put(_index_offset, value);
3798 }
3799 
3800 oop java_lang_reflect_Parameter::executable(oop param) {
3801   return param->obj_field(_executable_offset);
3802 }
3803 
3804 void java_lang_reflect_Parameter::set_executable(oop param, oop value) {
3805   param->obj_field_put(_executable_offset, value);
3806 }
3807 
3808 // java_lang_Module
3809 
3810 int java_lang_Module::_loader_offset;
3811 int java_lang_Module::_name_offset;
3812 int java_lang_Module::_module_entry_offset;
3813 
3814 Handle java_lang_Module::create(Handle loader, Handle module_name, TRAPS) {
3815   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3816   return JavaCalls::construct_new_instance(vmClasses::Module_klass(),
3817                           vmSymbols::java_lang_module_init_signature(),
3818                           loader, module_name, CHECK_NH);
3819 }
3820 
3821 #define MODULE_FIELDS_DO(macro) \
3822   macro(_loader_offset,  k, vmSymbols::loader_name(),  classloader_signature, false); \
3823   macro(_name_offset,    k, vmSymbols::name_name(),    string_signature,      false)
3824 
3825 void java_lang_Module::compute_offsets() {
3826   InstanceKlass* k = vmClasses::Module_klass();
3827   MODULE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3828   MODULE_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3829 }
3830 
3831 #if INCLUDE_CDS
3832 void java_lang_Module::serialize_offsets(SerializeClosure* f) {
3833   MODULE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3834   MODULE_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3835 }
3836 #endif
3837 
3838 oop java_lang_Module::loader(oop module) {
3839   return module->obj_field(_loader_offset);
3840 }
3841 
3842 void java_lang_Module::set_loader(oop module, oop value) {
3843   module->obj_field_put(_loader_offset, value);
3844 }
3845 
3846 oop java_lang_Module::name(oop module) {
3847   return module->obj_field(_name_offset);
3848 }
3849 
3850 void java_lang_Module::set_name(oop module, oop value) {
3851   module->obj_field_put(_name_offset, value);
3852 }
3853 
3854 ModuleEntry* java_lang_Module::module_entry_raw(oop module) {
3855   assert(_module_entry_offset != 0, "Uninitialized module_entry_offset");
3856   assert(module != NULL, "module can't be null");
3857   assert(oopDesc::is_oop(module), "module must be oop");
3858 
3859   ModuleEntry* module_entry = (ModuleEntry*)module->address_field(_module_entry_offset);
3860   return module_entry;
3861 }
3862 
3863 ModuleEntry* java_lang_Module::module_entry(oop module) {
3864   ModuleEntry* module_entry = module_entry_raw(module);
3865   if (module_entry == NULL) {
3866     // If the inject field containing the ModuleEntry* is null then return the
3867     // class loader's unnamed module.
3868     oop loader = java_lang_Module::loader(module);
3869     Handle h_loader = Handle(Thread::current(), loader);
3870     ClassLoaderData* loader_cld = SystemDictionary::register_loader(h_loader);
3871     return loader_cld->unnamed_module();
3872   }
3873   return module_entry;
3874 }
3875 
3876 void java_lang_Module::set_module_entry(oop module, ModuleEntry* module_entry) {
3877   assert(_module_entry_offset != 0, "Uninitialized module_entry_offset");
3878   assert(module != NULL, "module can't be null");
3879   assert(oopDesc::is_oop(module), "module must be oop");
3880   module->address_field_put(_module_entry_offset, (address)module_entry);
3881 }
3882 
3883 Handle reflect_ConstantPool::create(TRAPS) {
3884   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3885   InstanceKlass* k = vmClasses::reflect_ConstantPool_klass();
3886   // Ensure it is initialized
3887   k->initialize(CHECK_NH);
3888   return k->allocate_instance_handle(THREAD);
3889 }
3890 
3891 
3892 void reflect_ConstantPool::set_cp(oop reflect, ConstantPool* value) {
3893   oop mirror = value->pool_holder()->java_mirror();
3894   // Save the mirror to get back the constant pool.
3895   reflect->obj_field_put(_oop_offset, mirror);
3896 }
3897 
3898 ConstantPool* reflect_ConstantPool::get_cp(oop reflect) {
3899 
3900   oop mirror = reflect->obj_field(_oop_offset);
3901   Klass* k = java_lang_Class::as_Klass(mirror);
3902   assert(k->is_instance_klass(), "Must be");
3903 
3904   // Get the constant pool back from the klass.  Since class redefinition
3905   // merges the new constant pool into the old, this is essentially the
3906   // same constant pool as the original.  If constant pool merging is
3907   // no longer done in the future, this will have to change to save
3908   // the original.
3909   return InstanceKlass::cast(k)->constants();
3910 }
3911 
3912 int reflect_UnsafeStaticFieldAccessorImpl::_base_offset;
3913 
3914 #define UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(macro) \
3915   macro(_base_offset, k, "base", object_signature, false)
3916 
3917 void reflect_UnsafeStaticFieldAccessorImpl::compute_offsets() {
3918   InstanceKlass* k = vmClasses::reflect_UnsafeStaticFieldAccessorImpl_klass();
3919   UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3920 }
3921 
3922 #if INCLUDE_CDS
3923 void reflect_UnsafeStaticFieldAccessorImpl::serialize_offsets(SerializeClosure* f) {
3924   UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3925 }
3926 #endif
3927 
3928 // Support for java_lang_ref_Reference
3929 
3930 bool java_lang_ref_Reference::_offsets_initialized;
3931 
3932 int java_lang_ref_Reference::_referent_offset;
3933 int java_lang_ref_Reference::_queue_offset;
3934 int java_lang_ref_Reference::_next_offset;
3935 int java_lang_ref_Reference::_discovered_offset;
3936 
3937 #define REFERENCE_FIELDS_DO(macro) \
3938   macro(_referent_offset,   k, "referent", object_signature, false); \
3939   macro(_queue_offset,      k, "queue", referencequeue_signature, false); \
3940   macro(_next_offset,       k, "next", reference_signature, false); \
3941   macro(_discovered_offset, k, "discovered", reference_signature, false);
3942 
3943 void java_lang_ref_Reference::compute_offsets() {
3944   if (_offsets_initialized) {
3945     return;
3946   }
3947   _offsets_initialized = true;
3948   InstanceKlass* k = vmClasses::Reference_klass();
3949   REFERENCE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3950 }
3951 
3952 #if INCLUDE_CDS
3953 void java_lang_ref_Reference::serialize_offsets(SerializeClosure* f) {
3954   f->do_bool(&_offsets_initialized);
3955   REFERENCE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3956 }
3957 #endif
3958 
3959 bool java_lang_ref_Reference::is_referent_field(oop obj, ptrdiff_t offset) {
3960   assert(obj != NULL, "sanity");
3961   if (offset != _referent_offset) {
3962     return false;
3963   }
3964 
3965   Klass* k = obj->klass();
3966   if (!k->is_instance_klass()) {
3967     return false;
3968   }
3969 
3970   InstanceKlass* ik = InstanceKlass::cast(obj->klass());
3971   bool is_reference = ik->reference_type() != REF_NONE;
3972   assert(!is_reference || ik->is_subclass_of(vmClasses::Reference_klass()), "sanity");
3973   return is_reference;
3974 }
3975 
3976 int java_lang_boxing_object::_value_offset;
3977 int java_lang_boxing_object::_long_value_offset;
3978 
3979 #define BOXING_FIELDS_DO(macro) \
3980   macro(_value_offset,      integerKlass, "value", int_signature, false); \
3981   macro(_long_value_offset, longKlass, "value", long_signature, false);
3982 
3983 void java_lang_boxing_object::compute_offsets() {
3984   InstanceKlass* integerKlass = vmClasses::Integer_klass();
3985   InstanceKlass* longKlass = vmClasses::Long_klass();
3986   BOXING_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3987 }
3988 
3989 #if INCLUDE_CDS
3990 void java_lang_boxing_object::serialize_offsets(SerializeClosure* f) {
3991   BOXING_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3992 }
3993 #endif
3994 
3995 oop java_lang_boxing_object::initialize_and_allocate(BasicType type, TRAPS) {
3996   Klass* k = vmClasses::box_klass(type);
3997   if (k == NULL)  return NULL;
3998   InstanceKlass* ik = InstanceKlass::cast(k);
3999   if (!ik->is_initialized())  ik->initialize(CHECK_NULL);
4000   return ik->allocate_instance(THREAD);
4001 }
4002 
4003 
4004 oop java_lang_boxing_object::create(BasicType type, jvalue* value, TRAPS) {
4005   oop box = initialize_and_allocate(type, CHECK_NULL);
4006   if (box == NULL)  return NULL;
4007   switch (type) {
4008     case T_BOOLEAN:
4009       box->bool_field_put(_value_offset, value->z);
4010       break;
4011     case T_CHAR:
4012       box->char_field_put(_value_offset, value->c);
4013       break;
4014     case T_FLOAT:
4015       box->float_field_put(_value_offset, value->f);
4016       break;
4017     case T_DOUBLE:
4018       box->double_field_put(_long_value_offset, value->d);
4019       break;
4020     case T_BYTE:
4021       box->byte_field_put(_value_offset, value->b);
4022       break;
4023     case T_SHORT:
4024       box->short_field_put(_value_offset, value->s);
4025       break;
4026     case T_INT:
4027       box->int_field_put(_value_offset, value->i);
4028       break;
4029     case T_LONG:
4030       box->long_field_put(_long_value_offset, value->j);
4031       break;
4032     default:
4033       return NULL;
4034   }
4035   return box;
4036 }
4037 
4038 
4039 BasicType java_lang_boxing_object::basic_type(oop box) {
4040   if (box == NULL)  return T_ILLEGAL;
4041   BasicType type = vmClasses::box_klass_type(box->klass());
4042   if (type == T_OBJECT)         // 'unknown' value returned by SD::bkt
4043     return T_ILLEGAL;
4044   return type;
4045 }
4046 
4047 
4048 BasicType java_lang_boxing_object::get_value(oop box, jvalue* value) {
4049   BasicType type = vmClasses::box_klass_type(box->klass());
4050   switch (type) {
4051   case T_BOOLEAN:
4052     value->z = box->bool_field(_value_offset);
4053     break;
4054   case T_CHAR:
4055     value->c = box->char_field(_value_offset);
4056     break;
4057   case T_FLOAT:
4058     value->f = box->float_field(_value_offset);
4059     break;
4060   case T_DOUBLE:
4061     value->d = box->double_field(_long_value_offset);
4062     break;
4063   case T_BYTE:
4064     value->b = box->byte_field(_value_offset);
4065     break;
4066   case T_SHORT:
4067     value->s = box->short_field(_value_offset);
4068     break;
4069   case T_INT:
4070     value->i = box->int_field(_value_offset);
4071     break;
4072   case T_LONG:
4073     value->j = box->long_field(_long_value_offset);
4074     break;
4075   default:
4076     return T_ILLEGAL;
4077   } // end switch
4078   return type;
4079 }
4080 
4081 
4082 BasicType java_lang_boxing_object::set_value(oop box, jvalue* value) {
4083   BasicType type = vmClasses::box_klass_type(box->klass());
4084   switch (type) {
4085   case T_BOOLEAN:
4086     box->bool_field_put(_value_offset, value->z);
4087     break;
4088   case T_CHAR:
4089     box->char_field_put(_value_offset, value->c);
4090     break;
4091   case T_FLOAT:
4092     box->float_field_put(_value_offset, value->f);
4093     break;
4094   case T_DOUBLE:
4095     box->double_field_put(_long_value_offset, value->d);
4096     break;
4097   case T_BYTE:
4098     box->byte_field_put(_value_offset, value->b);
4099     break;
4100   case T_SHORT:
4101     box->short_field_put(_value_offset, value->s);
4102     break;
4103   case T_INT:
4104     box->int_field_put(_value_offset, value->i);
4105     break;
4106   case T_LONG:
4107     box->long_field_put(_long_value_offset, value->j);
4108     break;
4109   default:
4110     return T_ILLEGAL;
4111   } // end switch
4112   return type;
4113 }
4114 
4115 
4116 void java_lang_boxing_object::print(BasicType type, jvalue* value, outputStream* st) {
4117   switch (type) {
4118   case T_BOOLEAN:   st->print("%s", value->z ? "true" : "false");   break;
4119   case T_CHAR:      st->print("%d", value->c);                      break;
4120   case T_BYTE:      st->print("%d", value->b);                      break;
4121   case T_SHORT:     st->print("%d", value->s);                      break;
4122   case T_INT:       st->print("%d", value->i);                      break;
4123   case T_LONG:      st->print(JLONG_FORMAT, value->j);              break;
4124   case T_FLOAT:     st->print("%f", value->f);                      break;
4125   case T_DOUBLE:    st->print("%lf", value->d);                     break;
4126   default:          st->print("type %d?", type);                    break;
4127   }
4128 }
4129 
4130 
4131 // Support for java_lang_ref_SoftReference
4132 //
4133 
4134 int java_lang_ref_SoftReference::_timestamp_offset;
4135 int java_lang_ref_SoftReference::_static_clock_offset;
4136 
4137 #define SOFTREFERENCE_FIELDS_DO(macro) \
4138   macro(_timestamp_offset,    k, "timestamp", long_signature, false); \
4139   macro(_static_clock_offset, k, "clock",     long_signature, true)
4140 
4141 void java_lang_ref_SoftReference::compute_offsets() {
4142   InstanceKlass* k = vmClasses::SoftReference_klass();
4143   SOFTREFERENCE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4144 }
4145 
4146 #if INCLUDE_CDS
4147 void java_lang_ref_SoftReference::serialize_offsets(SerializeClosure* f) {
4148   SOFTREFERENCE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4149 }
4150 #endif
4151 
4152 jlong java_lang_ref_SoftReference::timestamp(oop ref) {
4153   return ref->long_field(_timestamp_offset);
4154 }
4155 
4156 jlong java_lang_ref_SoftReference::clock() {
4157   InstanceKlass* ik = vmClasses::SoftReference_klass();
4158   oop base = ik->static_field_base_raw();
4159   return base->long_field(_static_clock_offset);
4160 }
4161 
4162 void java_lang_ref_SoftReference::set_clock(jlong value) {
4163   InstanceKlass* ik = vmClasses::SoftReference_klass();
4164   oop base = ik->static_field_base_raw();
4165   base->long_field_put(_static_clock_offset, value);
4166 }
4167 
4168 // Support for java_lang_invoke_DirectMethodHandle
4169 
4170 int java_lang_invoke_DirectMethodHandle::_member_offset;
4171 
4172 oop java_lang_invoke_DirectMethodHandle::member(oop dmh) {
4173   oop member_name = NULL;
4174   assert(oopDesc::is_oop(dmh) && java_lang_invoke_DirectMethodHandle::is_instance(dmh),
4175          "a DirectMethodHandle oop is expected");
4176   return dmh->obj_field(_member_offset);
4177 }
4178 
4179 #define DIRECTMETHODHANDLE_FIELDS_DO(macro) \
4180   macro(_member_offset, k, "member", java_lang_invoke_MemberName_signature, false)
4181 
4182 void java_lang_invoke_DirectMethodHandle::compute_offsets() {
4183   InstanceKlass* k = vmClasses::DirectMethodHandle_klass();
4184   DIRECTMETHODHANDLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4185 }
4186 
4187 #if INCLUDE_CDS
4188 void java_lang_invoke_DirectMethodHandle::serialize_offsets(SerializeClosure* f) {
4189   DIRECTMETHODHANDLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4190 }
4191 #endif
4192 
4193 // Support for java_lang_invoke_MethodHandle
4194 
4195 int java_lang_invoke_MethodHandle::_type_offset;
4196 int java_lang_invoke_MethodHandle::_form_offset;
4197 
4198 int java_lang_invoke_MemberName::_clazz_offset;
4199 int java_lang_invoke_MemberName::_name_offset;
4200 int java_lang_invoke_MemberName::_type_offset;
4201 int java_lang_invoke_MemberName::_flags_offset;
4202 int java_lang_invoke_MemberName::_method_offset;
4203 int java_lang_invoke_MemberName::_vmindex_offset;
4204 
4205 int java_lang_invoke_ResolvedMethodName::_vmtarget_offset;
4206 int java_lang_invoke_ResolvedMethodName::_vmholder_offset;
4207 
4208 int java_lang_invoke_LambdaForm::_vmentry_offset;
4209 
4210 #define METHODHANDLE_FIELDS_DO(macro) \
4211   macro(_type_offset, k, vmSymbols::type_name(), java_lang_invoke_MethodType_signature, false); \
4212   macro(_form_offset, k, "form",                 java_lang_invoke_LambdaForm_signature, false)
4213 
4214 void java_lang_invoke_MethodHandle::compute_offsets() {
4215   InstanceKlass* k = vmClasses::MethodHandle_klass();
4216   METHODHANDLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4217 }
4218 
4219 #if INCLUDE_CDS
4220 void java_lang_invoke_MethodHandle::serialize_offsets(SerializeClosure* f) {
4221   METHODHANDLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4222 }
4223 #endif
4224 
4225 #define MEMBERNAME_FIELDS_DO(macro) \
4226   macro(_clazz_offset,   k, vmSymbols::clazz_name(),   class_signature,  false); \
4227   macro(_name_offset,    k, vmSymbols::name_name(),    string_signature, false); \
4228   macro(_type_offset,    k, vmSymbols::type_name(),    object_signature, false); \
4229   macro(_flags_offset,   k, vmSymbols::flags_name(),   int_signature,    false); \
4230   macro(_method_offset,  k, vmSymbols::method_name(),  java_lang_invoke_ResolvedMethodName_signature, false)
4231 
4232 void java_lang_invoke_MemberName::compute_offsets() {
4233   InstanceKlass* k = vmClasses::MemberName_klass();
4234   MEMBERNAME_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4235   MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
4236 }
4237 
4238 #if INCLUDE_CDS
4239 void java_lang_invoke_MemberName::serialize_offsets(SerializeClosure* f) {
4240   MEMBERNAME_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4241   MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
4242 }
4243 #endif
4244 
4245 void java_lang_invoke_ResolvedMethodName::compute_offsets() {
4246   InstanceKlass* k = vmClasses::ResolvedMethodName_klass();
4247   assert(k != NULL, "jdk mismatch");
4248   RESOLVEDMETHOD_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
4249 }
4250 
4251 #if INCLUDE_CDS
4252 void java_lang_invoke_ResolvedMethodName::serialize_offsets(SerializeClosure* f) {
4253   RESOLVEDMETHOD_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
4254 }
4255 #endif
4256 
4257 #define LAMBDAFORM_FIELDS_DO(macro) \
4258   macro(_vmentry_offset, k, "vmentry", java_lang_invoke_MemberName_signature, false)
4259 
4260 void java_lang_invoke_LambdaForm::compute_offsets() {
4261   InstanceKlass* k = vmClasses::LambdaForm_klass();
4262   assert (k != NULL, "jdk mismatch");
4263   LAMBDAFORM_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4264 }
4265 
4266 #if INCLUDE_CDS
4267 void java_lang_invoke_LambdaForm::serialize_offsets(SerializeClosure* f) {
4268   LAMBDAFORM_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4269 }
4270 #endif
4271 
4272 bool java_lang_invoke_LambdaForm::is_instance(oop obj) {
4273   return obj != NULL && is_subclass(obj->klass());
4274 }
4275 
4276 int jdk_internal_invoke_NativeEntryPoint::_shadow_space_offset;
4277 int jdk_internal_invoke_NativeEntryPoint::_argMoves_offset;
4278 int jdk_internal_invoke_NativeEntryPoint::_returnMoves_offset;
4279 int jdk_internal_invoke_NativeEntryPoint::_need_transition_offset;
4280 int jdk_internal_invoke_NativeEntryPoint::_method_type_offset;
4281 int jdk_internal_invoke_NativeEntryPoint::_name_offset;
4282 
4283 #define NEP_FIELDS_DO(macro) \
4284   macro(_shadow_space_offset,    k, "shadowSpace",    int_signature, false); \
4285   macro(_argMoves_offset,        k, "argMoves",       long_array_signature, false); \
4286   macro(_returnMoves_offset,     k, "returnMoves",    long_array_signature, false); \
4287   macro(_need_transition_offset, k, "needTransition", bool_signature, false); \
4288   macro(_method_type_offset,     k, "methodType",     java_lang_invoke_MethodType_signature, false); \
4289   macro(_name_offset,            k, "name",           string_signature, false);
4290 
4291 bool jdk_internal_invoke_NativeEntryPoint::is_instance(oop obj) {
4292   return obj != NULL && is_subclass(obj->klass());
4293 }
4294 
4295 void jdk_internal_invoke_NativeEntryPoint::compute_offsets() {
4296   InstanceKlass* k = vmClasses::NativeEntryPoint_klass();
4297   NEP_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4298 }
4299 
4300 #if INCLUDE_CDS
4301 void jdk_internal_invoke_NativeEntryPoint::serialize_offsets(SerializeClosure* f) {
4302   NEP_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4303 }
4304 #endif
4305 
4306 jint jdk_internal_invoke_NativeEntryPoint::shadow_space(oop entry) {
4307   return entry->int_field(_shadow_space_offset);
4308 }
4309 
4310 oop jdk_internal_invoke_NativeEntryPoint::argMoves(oop entry) {
4311   return entry->obj_field(_argMoves_offset);
4312 }
4313 
4314 oop jdk_internal_invoke_NativeEntryPoint::returnMoves(oop entry) {
4315   return entry->obj_field(_returnMoves_offset);
4316 }
4317 
4318 jboolean jdk_internal_invoke_NativeEntryPoint::need_transition(oop entry) {
4319   return entry->bool_field(_need_transition_offset);
4320 }
4321 
4322 oop jdk_internal_invoke_NativeEntryPoint::method_type(oop entry) {
4323   return entry->obj_field(_method_type_offset);
4324 }
4325 
4326 oop jdk_internal_invoke_NativeEntryPoint::name(oop entry) {
4327   return entry->obj_field(_name_offset);
4328 }
4329 
4330 oop java_lang_invoke_MethodHandle::type(oop mh) {
4331   return mh->obj_field(_type_offset);
4332 }
4333 
4334 void java_lang_invoke_MethodHandle::set_type(oop mh, oop mtype) {
4335   mh->obj_field_put(_type_offset, mtype);
4336 }
4337 
4338 oop java_lang_invoke_MethodHandle::form(oop mh) {
4339   assert(_form_offset != 0, "");
4340   return mh->obj_field(_form_offset);
4341 }
4342 
4343 void java_lang_invoke_MethodHandle::set_form(oop mh, oop lform) {
4344   assert(_form_offset != 0, "");
4345   mh->obj_field_put(_form_offset, lform);
4346 }
4347 
4348 /// MemberName accessors
4349 
4350 oop java_lang_invoke_MemberName::clazz(oop mname) {
4351   assert(is_instance(mname), "wrong type");
4352   return mname->obj_field(_clazz_offset);
4353 }
4354 
4355 void java_lang_invoke_MemberName::set_clazz(oop mname, oop clazz) {
4356   assert(is_instance(mname), "wrong type");
4357   mname->obj_field_put(_clazz_offset, clazz);
4358 }
4359 
4360 oop java_lang_invoke_MemberName::name(oop mname) {
4361   assert(is_instance(mname), "wrong type");
4362   return mname->obj_field(_name_offset);
4363 }
4364 
4365 void java_lang_invoke_MemberName::set_name(oop mname, oop name) {
4366   assert(is_instance(mname), "wrong type");
4367   mname->obj_field_put(_name_offset, name);
4368 }
4369 
4370 oop java_lang_invoke_MemberName::type(oop mname) {
4371   assert(is_instance(mname), "wrong type");
4372   return mname->obj_field(_type_offset);
4373 }
4374 
4375 void java_lang_invoke_MemberName::set_type(oop mname, oop type) {
4376   assert(is_instance(mname), "wrong type");
4377   mname->obj_field_put(_type_offset, type);
4378 }
4379 
4380 int java_lang_invoke_MemberName::flags(oop mname) {
4381   assert(is_instance(mname), "wrong type");
4382   return mname->int_field(_flags_offset);
4383 }
4384 
4385 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) {
4386   assert(is_instance(mname), "wrong type");
4387   mname->int_field_put(_flags_offset, flags);
4388 }
4389 
4390 
4391 // Return vmtarget from ResolvedMethodName method field through indirection
4392 Method* java_lang_invoke_MemberName::vmtarget(oop mname) {
4393   assert(is_instance(mname), "wrong type");
4394   oop method = mname->obj_field(_method_offset);
4395   return method == NULL ? NULL : java_lang_invoke_ResolvedMethodName::vmtarget(method);
4396 }
4397 
4398 bool java_lang_invoke_MemberName::is_method(oop mname) {
4399   assert(is_instance(mname), "must be MemberName");
4400   return (flags(mname) & (MN_IS_METHOD | MN_IS_CONSTRUCTOR)) > 0;
4401 }
4402 
4403 void java_lang_invoke_MemberName::set_method(oop mname, oop resolved_method) {
4404   assert(is_instance(mname), "wrong type");
4405   mname->obj_field_put(_method_offset, resolved_method);
4406 }
4407 
4408 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) {
4409   assert(is_instance(mname), "wrong type");
4410   return (intptr_t) mname->address_field(_vmindex_offset);
4411 }
4412 
4413 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
4414   assert(is_instance(mname), "wrong type");
4415   mname->address_field_put(_vmindex_offset, (address) index);
4416 }
4417 
4418 
4419 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) {
4420   assert(is_instance(resolved_method), "wrong type");
4421   Method* m = (Method*)resolved_method->address_field(_vmtarget_offset);
4422   assert(m->is_method(), "must be");
4423   return m;
4424 }
4425 
4426 // Used by redefinition to change Method* to new Method* with same hash (name, signature)
4427 void java_lang_invoke_ResolvedMethodName::set_vmtarget(oop resolved_method, Method* m) {
4428   assert(is_instance(resolved_method), "wrong type");
4429   resolved_method->address_field_put(_vmtarget_offset, (address)m);
4430 }
4431 
4432 void java_lang_invoke_ResolvedMethodName::set_vmholder(oop resolved_method, oop holder) {
4433   assert(is_instance(resolved_method), "wrong type");
4434   resolved_method->obj_field_put(_vmholder_offset, holder);
4435 }
4436 
4437 oop java_lang_invoke_ResolvedMethodName::find_resolved_method(const methodHandle& m, TRAPS) {
4438   const Method* method = m();
4439 
4440   // lookup ResolvedMethod oop in the table, or create a new one and intern it
4441   oop resolved_method = ResolvedMethodTable::find_method(method);
4442   if (resolved_method != NULL) {
4443     return resolved_method;
4444   }
4445 
4446   InstanceKlass* k = vmClasses::ResolvedMethodName_klass();
4447   if (!k->is_initialized()) {
4448     k->initialize(CHECK_NULL);
4449   }
4450 
4451   oop new_resolved_method = k->allocate_instance(CHECK_NULL);
4452 
4453   NoSafepointVerifier nsv;
4454 
4455   if (method->is_old()) {
4456     method = (method->is_deleted()) ? Universe::throw_no_such_method_error() :
4457                                       method->get_new_method();
4458   }
4459 
4460   InstanceKlass* holder = method->method_holder();
4461 
4462   set_vmtarget(new_resolved_method, const_cast<Method*>(method));
4463   // Add a reference to the loader (actually mirror because hidden classes may not have
4464   // distinct loaders) to ensure the metadata is kept alive.
4465   // This mirror may be different than the one in clazz field.
4466   set_vmholder(new_resolved_method, holder->java_mirror());
4467 
4468   // Set flag in class to indicate this InstanceKlass has entries in the table
4469   // to avoid walking table during redefinition if none of the redefined classes
4470   // have any membernames in the table.
4471   holder->set_has_resolved_methods();
4472 
4473   return ResolvedMethodTable::add_method(method, Handle(THREAD, new_resolved_method));
4474 }
4475 
4476 oop java_lang_invoke_LambdaForm::vmentry(oop lform) {
4477   assert(is_instance(lform), "wrong type");
4478   return lform->obj_field(_vmentry_offset);
4479 }
4480 
4481 
4482 // Support for java_lang_invoke_MethodType
4483 
4484 int java_lang_invoke_MethodType::_rtype_offset;
4485 int java_lang_invoke_MethodType::_ptypes_offset;
4486 
4487 #define METHODTYPE_FIELDS_DO(macro) \
4488   macro(_rtype_offset,  k, "rtype",  class_signature,       false); \
4489   macro(_ptypes_offset, k, "ptypes", class_array_signature, false)
4490 
4491 void java_lang_invoke_MethodType::compute_offsets() {
4492   InstanceKlass* k = vmClasses::MethodType_klass();
4493   METHODTYPE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4494 }
4495 
4496 #if INCLUDE_CDS
4497 void java_lang_invoke_MethodType::serialize_offsets(SerializeClosure* f) {
4498   METHODTYPE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4499 }
4500 #endif
4501 
4502 void java_lang_invoke_MethodType::print_signature(oop mt, outputStream* st) {
4503   st->print("(");
4504   objArrayOop pts = ptypes(mt);
4505   for (int i = 0, limit = pts->length(); i < limit; i++) {
4506     java_lang_Class::print_signature(pts->obj_at(i), st);
4507   }
4508   st->print(")");
4509   java_lang_Class::print_signature(rtype(mt), st);
4510 }
4511 
4512 Symbol* java_lang_invoke_MethodType::as_signature(oop mt, bool intern_if_not_found) {
4513   ResourceMark rm;
4514   stringStream buffer(128);
4515   print_signature(mt, &buffer);
4516   const char* sigstr =       buffer.base();
4517   int         siglen = (int) buffer.size();
4518   Symbol *name;
4519   if (!intern_if_not_found) {
4520     name = SymbolTable::probe(sigstr, siglen);
4521   } else {
4522     name = SymbolTable::new_symbol(sigstr, siglen);
4523   }
4524   return name;
4525 }
4526 
4527 bool java_lang_invoke_MethodType::equals(oop mt1, oop mt2) {
4528   if (mt1 == mt2)
4529     return true;
4530   if (rtype(mt1) != rtype(mt2))
4531     return false;
4532   if (ptype_count(mt1) != ptype_count(mt2))
4533     return false;
4534   for (int i = ptype_count(mt1) - 1; i >= 0; i--) {
4535     if (ptype(mt1, i) != ptype(mt2, i))
4536       return false;
4537   }
4538   return true;
4539 }
4540 
4541 oop java_lang_invoke_MethodType::rtype(oop mt) {
4542   assert(is_instance(mt), "must be a MethodType");
4543   return mt->obj_field(_rtype_offset);
4544 }
4545 
4546 objArrayOop java_lang_invoke_MethodType::ptypes(oop mt) {
4547   assert(is_instance(mt), "must be a MethodType");
4548   return (objArrayOop) mt->obj_field(_ptypes_offset);
4549 }
4550 
4551 oop java_lang_invoke_MethodType::ptype(oop mt, int idx) {
4552   return ptypes(mt)->obj_at(idx);
4553 }
4554 
4555 int java_lang_invoke_MethodType::ptype_count(oop mt) {
4556   return ptypes(mt)->length();
4557 }
4558 
4559 int java_lang_invoke_MethodType::ptype_slot_count(oop mt) {
4560   objArrayOop pts = ptypes(mt);
4561   int count = pts->length();
4562   int slots = 0;
4563   for (int i = 0; i < count; i++) {
4564     BasicType bt = java_lang_Class::as_BasicType(pts->obj_at(i));
4565     slots += type2size[bt];
4566   }
4567   return slots;
4568 }
4569 
4570 int java_lang_invoke_MethodType::rtype_slot_count(oop mt) {
4571   BasicType bt = java_lang_Class::as_BasicType(rtype(mt));
4572   return type2size[bt];
4573 }
4574 
4575 
4576 // Support for java_lang_invoke_CallSite
4577 
4578 int java_lang_invoke_CallSite::_target_offset;
4579 int java_lang_invoke_CallSite::_context_offset;
4580 
4581 #define CALLSITE_FIELDS_DO(macro) \
4582   macro(_target_offset,  k, "target", java_lang_invoke_MethodHandle_signature, false); \
4583   macro(_context_offset, k, "context", java_lang_invoke_MethodHandleNatives_CallSiteContext_signature, false)
4584 
4585 void java_lang_invoke_CallSite::compute_offsets() {
4586   InstanceKlass* k = vmClasses::CallSite_klass();
4587   CALLSITE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4588 }
4589 
4590 #if INCLUDE_CDS
4591 void java_lang_invoke_CallSite::serialize_offsets(SerializeClosure* f) {
4592   CALLSITE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4593 }
4594 #endif
4595 
4596 oop java_lang_invoke_CallSite::context_no_keepalive(oop call_site) {
4597   assert(java_lang_invoke_CallSite::is_instance(call_site), "");
4598 
4599   oop dep_oop = call_site->obj_field_access<AS_NO_KEEPALIVE>(_context_offset);
4600   return dep_oop;
4601 }
4602 
4603 // Support for java_lang_invoke_ConstantCallSite
4604 
4605 int java_lang_invoke_ConstantCallSite::_is_frozen_offset;
4606 
4607 #define CONSTANTCALLSITE_FIELDS_DO(macro) \
4608   macro(_is_frozen_offset, k, "isFrozen", bool_signature, false)
4609 
4610 void java_lang_invoke_ConstantCallSite::compute_offsets() {
4611   InstanceKlass* k = vmClasses::ConstantCallSite_klass();
4612   CONSTANTCALLSITE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4613 }
4614 
4615 #if INCLUDE_CDS
4616 void java_lang_invoke_ConstantCallSite::serialize_offsets(SerializeClosure* f) {
4617   CONSTANTCALLSITE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4618 }
4619 #endif
4620 
4621 // Support for java_lang_invoke_MethodHandleNatives_CallSiteContext
4622 
4623 int java_lang_invoke_MethodHandleNatives_CallSiteContext::_vmdependencies_offset;
4624 int java_lang_invoke_MethodHandleNatives_CallSiteContext::_last_cleanup_offset;
4625 
4626 void java_lang_invoke_MethodHandleNatives_CallSiteContext::compute_offsets() {
4627   InstanceKlass* k = vmClasses::Context_klass();
4628   CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
4629 }
4630 
4631 #if INCLUDE_CDS
4632 void java_lang_invoke_MethodHandleNatives_CallSiteContext::serialize_offsets(SerializeClosure* f) {
4633   CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
4634 }
4635 #endif
4636 
4637 DependencyContext java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(oop call_site) {
4638   assert(java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(call_site), "");
4639   nmethodBucket* volatile* vmdeps_addr = call_site->field_addr<nmethodBucket* volatile>(_vmdependencies_offset);
4640   volatile uint64_t* last_cleanup_addr = call_site->field_addr<volatile uint64_t>(_last_cleanup_offset);
4641   DependencyContext dep_ctx(vmdeps_addr, last_cleanup_addr);
4642   return dep_ctx;
4643 }
4644 
4645 // Support for java_security_AccessControlContext
4646 
4647 int java_security_AccessControlContext::_context_offset;
4648 int java_security_AccessControlContext::_privilegedContext_offset;
4649 int java_security_AccessControlContext::_isPrivileged_offset;
4650 int java_security_AccessControlContext::_isAuthorized_offset;
4651 
4652 #define ACCESSCONTROLCONTEXT_FIELDS_DO(macro) \
4653   macro(_context_offset,           k, "context",      protectiondomain_signature, false); \
4654   macro(_privilegedContext_offset, k, "privilegedContext", accesscontrolcontext_signature, false); \
4655   macro(_isPrivileged_offset,      k, "isPrivileged", bool_signature, false); \
4656   macro(_isAuthorized_offset,      k, "isAuthorized", bool_signature, false)
4657 
4658 void java_security_AccessControlContext::compute_offsets() {
4659   assert(_isPrivileged_offset == 0, "offsets should be initialized only once");
4660   InstanceKlass* k = vmClasses::AccessControlContext_klass();
4661   ACCESSCONTROLCONTEXT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4662 }
4663 
4664 #if INCLUDE_CDS
4665 void java_security_AccessControlContext::serialize_offsets(SerializeClosure* f) {
4666   ACCESSCONTROLCONTEXT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4667 }
4668 #endif
4669 
4670 oop java_security_AccessControlContext::create(objArrayHandle context, bool isPrivileged, Handle privileged_context, TRAPS) {
4671   assert(_isPrivileged_offset != 0, "offsets should have been initialized");
4672   assert(_isAuthorized_offset != 0, "offsets should have been initialized");
4673   // Ensure klass is initialized
4674   vmClasses::AccessControlContext_klass()->initialize(CHECK_NULL);
4675   // Allocate result
4676   oop result = vmClasses::AccessControlContext_klass()->allocate_instance(CHECK_NULL);
4677   // Fill in values
4678   result->obj_field_put(_context_offset, context());
4679   result->obj_field_put(_privilegedContext_offset, privileged_context());
4680   result->bool_field_put(_isPrivileged_offset, isPrivileged);
4681   result->bool_field_put(_isAuthorized_offset, true);
4682   return result;
4683 }
4684 
4685 
4686 // Support for java_lang_ClassLoader
4687 
4688 int  java_lang_ClassLoader::_loader_data_offset;
4689 int  java_lang_ClassLoader::_parallelCapable_offset;
4690 int  java_lang_ClassLoader::_name_offset;
4691 int  java_lang_ClassLoader::_nameAndId_offset;
4692 int  java_lang_ClassLoader::_unnamedModule_offset;
4693 int  java_lang_ClassLoader::_parent_offset;
4694 
4695 ClassLoaderData* java_lang_ClassLoader::loader_data_acquire(oop loader) {
4696   assert(loader != NULL, "loader must not be NULL");
4697   assert(oopDesc::is_oop(loader), "loader must be oop");
4698   return Atomic::load_acquire(loader->field_addr<ClassLoaderData*>(_loader_data_offset));
4699 }
4700 
4701 ClassLoaderData* java_lang_ClassLoader::loader_data(oop loader) {
4702   assert(loader != NULL, "loader must not be NULL");
4703   assert(oopDesc::is_oop(loader), "loader must be oop");
4704   return *loader->field_addr<ClassLoaderData*>(_loader_data_offset);
4705 }
4706 
4707 void java_lang_ClassLoader::release_set_loader_data(oop loader, ClassLoaderData* new_data) {
4708   assert(loader != NULL, "loader must not be NULL");
4709   assert(oopDesc::is_oop(loader), "loader must be oop");
4710   Atomic::release_store(loader->field_addr<ClassLoaderData*>(_loader_data_offset), new_data);
4711 }
4712 
4713 #define CLASSLOADER_FIELDS_DO(macro) \
4714   macro(_parallelCapable_offset, k1, "parallelLockMap",      concurrenthashmap_signature, false); \
4715   macro(_name_offset,            k1, vmSymbols::name_name(), string_signature, false); \
4716   macro(_nameAndId_offset,       k1, "nameAndId",            string_signature, false); \
4717   macro(_unnamedModule_offset,   k1, "unnamedModule",        module_signature, false); \
4718   macro(_parent_offset,          k1, "parent",               classloader_signature, false)
4719 
4720 void java_lang_ClassLoader::compute_offsets() {
4721   InstanceKlass* k1 = vmClasses::ClassLoader_klass();
4722   CLASSLOADER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4723 
4724   CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
4725 }
4726 
4727 #if INCLUDE_CDS
4728 void java_lang_ClassLoader::serialize_offsets(SerializeClosure* f) {
4729   CLASSLOADER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4730   CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
4731 }
4732 #endif
4733 
4734 oop java_lang_ClassLoader::parent(oop loader) {
4735   assert(is_instance(loader), "loader must be oop");
4736   return loader->obj_field(_parent_offset);
4737 }
4738 
4739 // Returns the name field of this class loader.  If the name field has not
4740 // been set, null will be returned.
4741 oop java_lang_ClassLoader::name(oop loader) {
4742   assert(is_instance(loader), "loader must be oop");
4743   return loader->obj_field(_name_offset);
4744 }
4745 
4746 // Returns the nameAndId field of this class loader. The format is
4747 // as follows:
4748 //   If the defining loader has a name explicitly set then '<loader-name>' @<id>
4749 //   If the defining loader has no name then <qualified-class-name> @<id>
4750 //   If built-in loader, then omit '@<id>' as there is only one instance.
4751 // Use ClassLoader::loader_name_id() to obtain this String as a char*.
4752 oop java_lang_ClassLoader::nameAndId(oop loader) {
4753   assert(is_instance(loader), "loader must be oop");
4754   return loader->obj_field(_nameAndId_offset);
4755 }
4756 
4757 bool java_lang_ClassLoader::isAncestor(oop loader, oop cl) {
4758   assert(is_instance(loader), "loader must be oop");
4759   assert(cl == NULL || is_instance(cl), "cl argument must be oop");
4760   oop acl = loader;
4761   debug_only(jint loop_count = 0);
4762   // This loop taken verbatim from ClassLoader.java:
4763   do {
4764     acl = parent(acl);
4765     if (cl == acl) {
4766       return true;
4767     }
4768     assert(++loop_count > 0, "loop_count overflow");
4769   } while (acl != NULL);
4770   return false;
4771 }
4772 
4773 bool java_lang_ClassLoader::is_instance(oop obj) {
4774   return obj != NULL && is_subclass(obj->klass());
4775 }
4776 
4777 
4778 // For class loader classes, parallelCapable defined
4779 // based on non-null field
4780 // Written to by java.lang.ClassLoader, vm only reads this field, doesn't set it
4781 bool java_lang_ClassLoader::parallelCapable(oop class_loader) {
4782   assert(_parallelCapable_offset != 0, "offsets should have been initialized");
4783   return (class_loader->obj_field(_parallelCapable_offset) != NULL);
4784 }
4785 
4786 bool java_lang_ClassLoader::is_trusted_loader(oop loader) {
4787   // Fix for 4474172; see evaluation for more details
4788   loader = non_reflection_class_loader(loader);
4789 
4790   oop cl = SystemDictionary::java_system_loader();
4791   while(cl != NULL) {
4792     if (cl == loader) return true;
4793     cl = parent(cl);
4794   }
4795   return false;
4796 }
4797 
4798 // Return true if this is one of the class loaders associated with
4799 // the generated bytecodes for reflection.
4800 bool java_lang_ClassLoader::is_reflection_class_loader(oop loader) {
4801   if (loader != NULL) {
4802     Klass* delegating_cl_class = vmClasses::reflect_DelegatingClassLoader_klass();
4803     // This might be null in non-1.4 JDKs
4804     return (delegating_cl_class != NULL && loader->is_a(delegating_cl_class));
4805   }
4806   return false;
4807 }
4808 
4809 oop java_lang_ClassLoader::non_reflection_class_loader(oop loader) {
4810   // See whether this is one of the class loaders associated with
4811   // the generated bytecodes for reflection, and if so, "magically"
4812   // delegate to its parent to prevent class loading from occurring
4813   // in places where applications using reflection didn't expect it.
4814   if (is_reflection_class_loader(loader)) {
4815     return parent(loader);
4816   }
4817   return loader;
4818 }
4819 
4820 oop java_lang_ClassLoader::unnamedModule(oop loader) {
4821   assert(is_instance(loader), "loader must be oop");
4822   return loader->obj_field(_unnamedModule_offset);
4823 }
4824 
4825 // Support for java_lang_System
4826 //
4827 
4828 int java_lang_System::_static_in_offset;
4829 int java_lang_System::_static_out_offset;
4830 int java_lang_System::_static_err_offset;
4831 int java_lang_System::_static_security_offset;
4832 int java_lang_System::_static_allow_security_offset;
4833 int java_lang_System::_static_never_offset;
4834 
4835 #define SYSTEM_FIELDS_DO(macro) \
4836   macro(_static_in_offset,  k, "in",  input_stream_signature, true); \
4837   macro(_static_out_offset, k, "out", print_stream_signature, true); \
4838   macro(_static_err_offset, k, "err", print_stream_signature, true); \
4839   macro(_static_security_offset, k, "security", security_manager_signature, true); \
4840   macro(_static_allow_security_offset, k, "allowSecurityManager", int_signature, true); \
4841   macro(_static_never_offset, k, "NEVER", int_signature, true)
4842 
4843 void java_lang_System::compute_offsets() {
4844   InstanceKlass* k = vmClasses::System_klass();
4845   SYSTEM_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4846 }
4847 
4848 // This field tells us that a security manager can never be installed so we
4849 // can completely skip populating the ProtectionDomainCacheTable.
4850 bool java_lang_System::allow_security_manager() {
4851   static int initialized = false;
4852   static bool allowed = true; // default
4853   if (!initialized) {
4854     oop base = vmClasses::System_klass()->static_field_base_raw();
4855     int never = base->int_field(_static_never_offset);
4856     allowed = (base->int_field(_static_allow_security_offset) != never);
4857     initialized = true;
4858   }
4859   return allowed;
4860 }
4861 
4862 // This field tells us that a security manager is installed.
4863 bool java_lang_System::has_security_manager() {
4864   oop base = vmClasses::System_klass()->static_field_base_raw();
4865   return base->obj_field(_static_security_offset) != NULL;
4866 }
4867 
4868 #if INCLUDE_CDS
4869 void java_lang_System::serialize_offsets(SerializeClosure* f) {
4870    SYSTEM_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4871 }
4872 #endif
4873 
4874 // Support for jdk_internal_misc_UnsafeConstants
4875 //
4876 class UnsafeConstantsFixup : public FieldClosure {
4877 private:
4878   int _address_size;
4879   int _page_size;
4880   bool _big_endian;
4881   bool _use_unaligned_access;
4882   int _data_cache_line_flush_size;
4883 public:
4884   UnsafeConstantsFixup() {
4885     // round up values for all static final fields
4886     _address_size = sizeof(void*);
4887     _page_size = os::vm_page_size();
4888     _big_endian = LITTLE_ENDIAN_ONLY(false) BIG_ENDIAN_ONLY(true);
4889     _use_unaligned_access = UseUnalignedAccesses;
4890     _data_cache_line_flush_size = (int)VM_Version::data_cache_line_flush_size();
4891   }
4892 
4893   void do_field(fieldDescriptor* fd) {
4894     oop mirror = fd->field_holder()->java_mirror();
4895     assert(mirror != NULL, "UnsafeConstants must have mirror already");
4896     assert(fd->field_holder() == vmClasses::UnsafeConstants_klass(), "Should be UnsafeConstants");
4897     assert(fd->is_final(), "fields of UnsafeConstants must be final");
4898     assert(fd->is_static(), "fields of UnsafeConstants must be static");
4899     if (fd->name() == vmSymbols::address_size_name()) {
4900       mirror->int_field_put(fd->offset(), _address_size);
4901     } else if (fd->name() == vmSymbols::page_size_name()) {
4902       mirror->int_field_put(fd->offset(), _page_size);
4903     } else if (fd->name() == vmSymbols::big_endian_name()) {
4904       mirror->bool_field_put(fd->offset(), _big_endian);
4905     } else if (fd->name() == vmSymbols::use_unaligned_access_name()) {
4906       mirror->bool_field_put(fd->offset(), _use_unaligned_access);
4907     } else if (fd->name() == vmSymbols::data_cache_line_flush_size_name()) {
4908       mirror->int_field_put(fd->offset(), _data_cache_line_flush_size);
4909     } else if (fd->name() == vmSymbols::scoped_cache_shift_name()) {
4910       mirror->int_field_put(fd->offset(), ScopeLocalCacheSize ? exact_log2(ScopeLocalCacheSize) : -1);
4911     } else {
4912       assert(false, "unexpected UnsafeConstants field");
4913     }
4914   }
4915 };
4916 
4917 void jdk_internal_misc_UnsafeConstants::set_unsafe_constants() {
4918   UnsafeConstantsFixup fixup;
4919   vmClasses::UnsafeConstants_klass()->do_local_static_fields(&fixup);
4920 }
4921 
4922 
4923 // java_lang_StackTraceElement
4924 
4925 int java_lang_StackTraceElement::_methodName_offset;
4926 int java_lang_StackTraceElement::_fileName_offset;
4927 int java_lang_StackTraceElement::_lineNumber_offset;
4928 int java_lang_StackTraceElement::_moduleName_offset;
4929 int java_lang_StackTraceElement::_moduleVersion_offset;
4930 int java_lang_StackTraceElement::_classLoaderName_offset;
4931 int java_lang_StackTraceElement::_declaringClass_offset;
4932 int java_lang_StackTraceElement::_declaringClassObject_offset;
4933 int java_lang_StackTraceElement::_contScopeName_offset;
4934 
4935 #define STACKTRACEELEMENT_FIELDS_DO(macro) \
4936   macro(_declaringClassObject_offset,  k, "declaringClassObject", class_signature, false); \
4937   macro(_classLoaderName_offset, k, "classLoaderName", string_signature, false); \
4938   macro(_moduleName_offset,      k, "moduleName",      string_signature, false); \
4939   macro(_moduleVersion_offset,   k, "moduleVersion",   string_signature, false); \
4940   macro(_declaringClass_offset,  k, "declaringClass",  string_signature, false); \
4941   macro(_methodName_offset,      k, "methodName",      string_signature, false); \
4942   macro(_fileName_offset,        k, "fileName",        string_signature, false); \
4943   macro(_lineNumber_offset,      k, "lineNumber",      int_signature,    false); \
4944   macro(_contScopeName_offset,   k, "contScopeName",   string_signature, false)
4945 
4946 // Support for java_lang_StackTraceElement
4947 void java_lang_StackTraceElement::compute_offsets() {
4948   InstanceKlass* k = vmClasses::StackTraceElement_klass();
4949   STACKTRACEELEMENT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4950 }
4951 
4952 #if INCLUDE_CDS
4953 void java_lang_StackTraceElement::serialize_offsets(SerializeClosure* f) {
4954   STACKTRACEELEMENT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4955 }
4956 #endif
4957 
4958 void java_lang_StackTraceElement::set_fileName(oop element, oop value) {
4959   element->obj_field_put(_fileName_offset, value);
4960 }
4961 
4962 void java_lang_StackTraceElement::set_declaringClass(oop element, oop value) {
4963   element->obj_field_put(_declaringClass_offset, value);
4964 }
4965 
4966 void java_lang_StackTraceElement::set_methodName(oop element, oop value) {
4967   element->obj_field_put(_methodName_offset, value);
4968 }
4969 
4970 void java_lang_StackTraceElement::set_lineNumber(oop element, int value) {
4971   element->int_field_put(_lineNumber_offset, value);
4972 }
4973 
4974 void java_lang_StackTraceElement::set_moduleName(oop element, oop value) {
4975   element->obj_field_put(_moduleName_offset, value);
4976 }
4977 
4978 void java_lang_StackTraceElement::set_moduleVersion(oop element, oop value) {
4979   element->obj_field_put(_moduleVersion_offset, value);
4980 }
4981 
4982 void java_lang_StackTraceElement::set_classLoaderName(oop element, oop value) {
4983   element->obj_field_put(_classLoaderName_offset, value);
4984 }
4985 
4986 void java_lang_StackTraceElement::set_declaringClassObject(oop element, oop value) {
4987   element->obj_field_put(_declaringClassObject_offset, value);
4988 }
4989 
4990 
4991 // java_lang_AssertionStatusDirectives
4992 
4993 int java_lang_AssertionStatusDirectives::_classes_offset;
4994 int java_lang_AssertionStatusDirectives::_classEnabled_offset;
4995 int java_lang_AssertionStatusDirectives::_packages_offset;
4996 int java_lang_AssertionStatusDirectives::_packageEnabled_offset;
4997 int java_lang_AssertionStatusDirectives::_deflt_offset;
4998 
4999 // Support for java Assertions - java_lang_AssertionStatusDirectives.
5000 #define ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(macro) \
5001   macro(_classes_offset,        k, "classes",        string_array_signature, false); \
5002   macro(_classEnabled_offset,   k, "classEnabled",   bool_array_signature, false); \
5003   macro(_packages_offset,       k, "packages",       string_array_signature, false); \
5004   macro(_packageEnabled_offset, k, "packageEnabled", bool_array_signature,   false); \
5005   macro(_deflt_offset,          k, "deflt",          bool_signature,         false)
5006 
5007 void java_lang_AssertionStatusDirectives::compute_offsets() {
5008   InstanceKlass* k = vmClasses::AssertionStatusDirectives_klass();
5009   ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5010 }
5011 
5012 #if INCLUDE_CDS
5013 void java_lang_AssertionStatusDirectives::serialize_offsets(SerializeClosure* f) {
5014   ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5015 }
5016 #endif
5017 
5018 void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) {
5019   o->obj_field_put(_classes_offset, val);
5020 }
5021 
5022 void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) {
5023   o->obj_field_put(_classEnabled_offset, val);
5024 }
5025 
5026 void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) {
5027   o->obj_field_put(_packages_offset, val);
5028 }
5029 
5030 void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) {
5031   o->obj_field_put(_packageEnabled_offset, val);
5032 }
5033 
5034 void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) {
5035   o->bool_field_put(_deflt_offset, val);
5036 }
5037 
5038 // Support for jdk.internal.vm.Continuation
5039 
5040 int jdk_internal_vm_ContinuationScope::_name_offset;
5041 int jdk_internal_vm_Continuation::_scope_offset;
5042 int jdk_internal_vm_Continuation::_target_offset;
5043 int jdk_internal_vm_Continuation::_tail_offset;
5044 int jdk_internal_vm_Continuation::_parent_offset;
5045 int jdk_internal_vm_Continuation::_yieldInfo_offset;
5046 int jdk_internal_vm_Continuation::_cs_offset;
5047 int jdk_internal_vm_Continuation::_mounted_offset;
5048 int jdk_internal_vm_Continuation::_done_offset;
5049 int jdk_internal_vm_Continuation::_preempted_offset;
5050 
5051 #define CONTINUATIONSCOPE_FIELDS_DO(macro) \
5052   macro(_name_offset, k, vmSymbols::name_name(), string_signature, false);
5053 
5054 void jdk_internal_vm_ContinuationScope::compute_offsets() {
5055   InstanceKlass* k = vmClasses::ContinuationScope_klass();
5056   CONTINUATIONSCOPE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5057 }
5058 
5059 #if INCLUDE_CDS
5060 void jdk_internal_vm_ContinuationScope::serialize_offsets(SerializeClosure* f) {
5061   CONTINUATIONSCOPE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5062 }
5063 #endif
5064 
5065 // Support for jdk.internal.vm.Continuation
5066 
5067 #define CONTINUATION_FIELDS_DO(macro) \
5068   macro(_scope_offset,     k, vmSymbols::scope_name(),     continuationscope_signature, false); \
5069   macro(_target_offset,    k, vmSymbols::target_name(),    runnable_signature,          false); \
5070   macro(_parent_offset,    k, vmSymbols::parent_name(),    continuation_signature,      false); \
5071   macro(_yieldInfo_offset, k, vmSymbols::yieldInfo_name(), object_signature,            false); \
5072   macro(_tail_offset,      k, vmSymbols::tail_name(),      stackchunk_signature,        false); \
5073   macro(_cs_offset,        k, vmSymbols::cs_name(),        short_signature,             false); \
5074   macro(_mounted_offset,   k, vmSymbols::mounted_name(),   bool_signature,              false); \
5075   macro(_done_offset,      k, vmSymbols::done_name(),      bool_signature,              false); \
5076   macro(_preempted_offset, k, "preempted",                 bool_signature,              false);
5077 
5078 void jdk_internal_vm_Continuation::compute_offsets() {
5079   InstanceKlass* k = vmClasses::Continuation_klass();
5080   CONTINUATION_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5081 }
5082 
5083 #if INCLUDE_CDS
5084 void jdk_internal_vm_Continuation::serialize_offsets(SerializeClosure* f) {
5085   CONTINUATION_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5086 }
5087 #endif
5088 
5089 // Support for jdk.internal.vm.StackChunk
5090 
5091 int jdk_internal_vm_StackChunk::_parent_offset;
5092 int jdk_internal_vm_StackChunk::_size_offset;
5093 int jdk_internal_vm_StackChunk::_sp_offset;
5094 int jdk_internal_vm_StackChunk::_pc_offset;
5095 int jdk_internal_vm_StackChunk::_argsize_offset;
5096 int jdk_internal_vm_StackChunk::_flags_offset;
5097 int jdk_internal_vm_StackChunk::_gcSP_offset;
5098 int jdk_internal_vm_StackChunk::_markCycle_offset;
5099 int jdk_internal_vm_StackChunk::_maxSize_offset;
5100 int jdk_internal_vm_StackChunk::_cont_offset;
5101 
5102 #define STACKCHUNK_FIELDS_DO(macro) \
5103   macro(_parent_offset,    k, vmSymbols::parent_name(),    stackchunk_signature, false); \
5104   macro(_size_offset,      k, vmSymbols::size_name(),      int_signature,        false); \
5105   macro(_sp_offset,        k, vmSymbols::sp_name(),        int_signature,        false); \
5106   macro(_pc_offset,        k, vmSymbols::pc_name(),        long_signature,       false); \
5107   macro(_argsize_offset,   k, vmSymbols::argsize_name(),   int_signature,        false); \
5108   macro(_flags_offset,     k, "flags",                     byte_signature,       false); \
5109   macro(_gcSP_offset,      k, "gcSP",                      int_signature,        false); \
5110   macro(_markCycle_offset, k, "markCycle",                 long_signature,       false); \
5111   macro(_maxSize_offset,   k, vmSymbols::maxSize_name(),   int_signature,        false); \
5112   macro(_cont_offset,      k, "cont",                      continuation_signature, false);
5113 
5114 void jdk_internal_vm_StackChunk::compute_offsets() {
5115   InstanceKlass* k = vmClasses::StackChunk_klass();
5116   STACKCHUNK_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5117 }
5118 
5119 #if INCLUDE_CDS
5120 void jdk_internal_vm_StackChunk::serialize_offsets(SerializeClosure* f) {
5121   STACKCHUNK_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5122 }
5123 #endif
5124 
5125 
5126 int java_util_concurrent_locks_AbstractOwnableSynchronizer::_owner_offset;
5127 
5128 #define AOS_FIELDS_DO(macro) \
5129   macro(_owner_offset, k, "exclusiveOwnerThread", thread_signature, false)
5130 
5131 void java_util_concurrent_locks_AbstractOwnableSynchronizer::compute_offsets() {
5132   InstanceKlass* k = vmClasses::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass();
5133   AOS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5134 }
5135 
5136 oop java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(oop obj) {
5137   assert(_owner_offset != 0, "Must be initialized");
5138   return obj->obj_field(_owner_offset);
5139 }
5140 
5141 #if INCLUDE_CDS
5142 void java_util_concurrent_locks_AbstractOwnableSynchronizer::serialize_offsets(SerializeClosure* f) {
5143   AOS_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5144 }
5145 #endif
5146 
5147 int vector_VectorPayload::_payload_offset;
5148 
5149 #define VECTORPAYLOAD_FIELDS_DO(macro) \
5150   macro(_payload_offset, k, "payload", object_signature, false)
5151 
5152 void vector_VectorPayload::compute_offsets() {
5153   InstanceKlass* k = vmClasses::vector_VectorPayload_klass();
5154   VECTORPAYLOAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5155 }
5156 
5157 #if INCLUDE_CDS
5158 void vector_VectorPayload::serialize_offsets(SerializeClosure* f) {
5159   VECTORPAYLOAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5160 }
5161 #endif
5162 
5163 void vector_VectorPayload::set_payload(oop o, oop val) {
5164   o->obj_field_put(_payload_offset, val);
5165 }
5166 
5167 bool vector_VectorPayload::is_instance(oop obj) {
5168   return obj != NULL && is_subclass(obj->klass());
5169 }
5170 
5171 int java_lang_Integer_IntegerCache::_static_cache_offset;
5172 int java_lang_Long_LongCache::_static_cache_offset;
5173 int java_lang_Character_CharacterCache::_static_cache_offset;
5174 int java_lang_Short_ShortCache::_static_cache_offset;
5175 int java_lang_Byte_ByteCache::_static_cache_offset;
5176 
5177 #define INTEGER_CACHE_FIELDS_DO(macro) \
5178   macro(_static_cache_offset, k, "cache", java_lang_Integer_array_signature, true)
5179 
5180 void java_lang_Integer_IntegerCache::compute_offsets(InstanceKlass *k) {
5181   guarantee(k != NULL && k->is_initialized(), "must be loaded and initialized");
5182   INTEGER_CACHE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5183 }
5184 
5185 objArrayOop java_lang_Integer_IntegerCache::cache(InstanceKlass *ik) {
5186   oop base = ik->static_field_base_raw();
5187   return objArrayOop(base->obj_field(_static_cache_offset));
5188 }
5189 
5190 Symbol* java_lang_Integer_IntegerCache::symbol() {
5191   return vmSymbols::java_lang_Integer_IntegerCache();
5192 }
5193 
5194 #if INCLUDE_CDS
5195 void java_lang_Integer_IntegerCache::serialize_offsets(SerializeClosure* f) {
5196   INTEGER_CACHE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5197 }
5198 #endif
5199 #undef INTEGER_CACHE_FIELDS_DO
5200 
5201 jint java_lang_Integer::value(oop obj) {
5202    jvalue v;
5203    java_lang_boxing_object::get_value(obj, &v);
5204    return v.i;
5205 }
5206 
5207 #define LONG_CACHE_FIELDS_DO(macro) \
5208   macro(_static_cache_offset, k, "cache", java_lang_Long_array_signature, true)
5209 
5210 void java_lang_Long_LongCache::compute_offsets(InstanceKlass *k) {
5211   guarantee(k != NULL && k->is_initialized(), "must be loaded and initialized");
5212   LONG_CACHE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5213 }
5214 
5215 objArrayOop java_lang_Long_LongCache::cache(InstanceKlass *ik) {
5216   oop base = ik->static_field_base_raw();
5217   return objArrayOop(base->obj_field(_static_cache_offset));
5218 }
5219 
5220 Symbol* java_lang_Long_LongCache::symbol() {
5221   return vmSymbols::java_lang_Long_LongCache();
5222 }
5223 
5224 #if INCLUDE_CDS
5225 void java_lang_Long_LongCache::serialize_offsets(SerializeClosure* f) {
5226   LONG_CACHE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5227 }
5228 #endif
5229 #undef LONG_CACHE_FIELDS_DO
5230 
5231 jlong java_lang_Long::value(oop obj) {
5232    jvalue v;
5233    java_lang_boxing_object::get_value(obj, &v);
5234    return v.j;
5235 }
5236 
5237 #define CHARACTER_CACHE_FIELDS_DO(macro) \
5238   macro(_static_cache_offset, k, "cache", java_lang_Character_array_signature, true)
5239 
5240 void java_lang_Character_CharacterCache::compute_offsets(InstanceKlass *k) {
5241   guarantee(k != NULL && k->is_initialized(), "must be loaded and initialized");
5242   CHARACTER_CACHE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5243 }
5244 
5245 objArrayOop java_lang_Character_CharacterCache::cache(InstanceKlass *ik) {
5246   oop base = ik->static_field_base_raw();
5247   return objArrayOop(base->obj_field(_static_cache_offset));
5248 }
5249 
5250 Symbol* java_lang_Character_CharacterCache::symbol() {
5251   return vmSymbols::java_lang_Character_CharacterCache();
5252 }
5253 
5254 #if INCLUDE_CDS
5255 void java_lang_Character_CharacterCache::serialize_offsets(SerializeClosure* f) {
5256   CHARACTER_CACHE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5257 }
5258 #endif
5259 #undef CHARACTER_CACHE_FIELDS_DO
5260 
5261 jchar java_lang_Character::value(oop obj) {
5262    jvalue v;
5263    java_lang_boxing_object::get_value(obj, &v);
5264    return v.c;
5265 }
5266 
5267 #define SHORT_CACHE_FIELDS_DO(macro) \
5268   macro(_static_cache_offset, k, "cache", java_lang_Short_array_signature, true)
5269 
5270 void java_lang_Short_ShortCache::compute_offsets(InstanceKlass *k) {
5271   guarantee(k != NULL && k->is_initialized(), "must be loaded and initialized");
5272   SHORT_CACHE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5273 }
5274 
5275 objArrayOop java_lang_Short_ShortCache::cache(InstanceKlass *ik) {
5276   oop base = ik->static_field_base_raw();
5277   return objArrayOop(base->obj_field(_static_cache_offset));
5278 }
5279 
5280 Symbol* java_lang_Short_ShortCache::symbol() {
5281   return vmSymbols::java_lang_Short_ShortCache();
5282 }
5283 
5284 #if INCLUDE_CDS
5285 void java_lang_Short_ShortCache::serialize_offsets(SerializeClosure* f) {
5286   SHORT_CACHE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5287 }
5288 #endif
5289 #undef SHORT_CACHE_FIELDS_DO
5290 
5291 jshort java_lang_Short::value(oop obj) {
5292    jvalue v;
5293    java_lang_boxing_object::get_value(obj, &v);
5294    return v.s;
5295 }
5296 
5297 #define BYTE_CACHE_FIELDS_DO(macro) \
5298   macro(_static_cache_offset, k, "cache", java_lang_Byte_array_signature, true)
5299 
5300 void java_lang_Byte_ByteCache::compute_offsets(InstanceKlass *k) {
5301   guarantee(k != NULL && k->is_initialized(), "must be loaded and initialized");
5302   BYTE_CACHE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5303 }
5304 
5305 objArrayOop java_lang_Byte_ByteCache::cache(InstanceKlass *ik) {
5306   oop base = ik->static_field_base_raw();
5307   return objArrayOop(base->obj_field(_static_cache_offset));
5308 }
5309 
5310 Symbol* java_lang_Byte_ByteCache::symbol() {
5311   return vmSymbols::java_lang_Byte_ByteCache();
5312 }
5313 
5314 #if INCLUDE_CDS
5315 void java_lang_Byte_ByteCache::serialize_offsets(SerializeClosure* f) {
5316   BYTE_CACHE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5317 }
5318 #endif
5319 #undef BYTE_CACHE_FIELDS_DO
5320 
5321 jbyte java_lang_Byte::value(oop obj) {
5322    jvalue v;
5323    java_lang_boxing_object::get_value(obj, &v);
5324    return v.b;
5325 }
5326 
5327 int java_lang_Boolean::_static_TRUE_offset;
5328 int java_lang_Boolean::_static_FALSE_offset;
5329 
5330 #define BOOLEAN_FIELDS_DO(macro) \
5331   macro(_static_TRUE_offset, k, "TRUE", java_lang_Boolean_signature, true); \
5332   macro(_static_FALSE_offset, k, "FALSE", java_lang_Boolean_signature, true)
5333 
5334 
5335 void java_lang_Boolean::compute_offsets(InstanceKlass *k) {
5336   guarantee(k != NULL && k->is_initialized(), "must be loaded and initialized");
5337   BOOLEAN_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5338 }
5339 
5340 oop java_lang_Boolean::get_TRUE(InstanceKlass *ik) {
5341   oop base = ik->static_field_base_raw();
5342   return base->obj_field(_static_TRUE_offset);
5343 }
5344 
5345 oop java_lang_Boolean::get_FALSE(InstanceKlass *ik) {
5346   oop base = ik->static_field_base_raw();
5347   return base->obj_field(_static_FALSE_offset);
5348 }
5349 
5350 Symbol* java_lang_Boolean::symbol() {
5351   return vmSymbols::java_lang_Boolean();
5352 }
5353 
5354 #if INCLUDE_CDS
5355 void java_lang_Boolean::serialize_offsets(SerializeClosure* f) {
5356   BOOLEAN_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5357 }
5358 #endif
5359 #undef BOOLEAN_CACHE_FIELDS_DO
5360 
5361 jboolean java_lang_Boolean::value(oop obj) {
5362    jvalue v;
5363    java_lang_boxing_object::get_value(obj, &v);
5364    return v.z;
5365 }
5366 
5367 // java_lang_reflect_RecordComponent
5368 
5369 int java_lang_reflect_RecordComponent::_clazz_offset;
5370 int java_lang_reflect_RecordComponent::_name_offset;
5371 int java_lang_reflect_RecordComponent::_type_offset;
5372 int java_lang_reflect_RecordComponent::_accessor_offset;
5373 int java_lang_reflect_RecordComponent::_signature_offset;
5374 int java_lang_reflect_RecordComponent::_annotations_offset;
5375 int java_lang_reflect_RecordComponent::_typeAnnotations_offset;
5376 
5377 #define RECORDCOMPONENT_FIELDS_DO(macro) \
5378   macro(_clazz_offset,       k, "clazz",       class_signature,  false); \
5379   macro(_name_offset,        k, "name",        string_signature, false); \
5380   macro(_type_offset,        k, "type",        class_signature,  false); \
5381   macro(_accessor_offset,    k, "accessor",    reflect_method_signature, false); \
5382   macro(_signature_offset,   k, "signature",   string_signature, false); \
5383   macro(_annotations_offset, k, "annotations", byte_array_signature,     false); \
5384   macro(_typeAnnotations_offset, k, "typeAnnotations", byte_array_signature, false);
5385 
5386 // Support for java_lang_reflect_RecordComponent
5387 void java_lang_reflect_RecordComponent::compute_offsets() {
5388   InstanceKlass* k = vmClasses::RecordComponent_klass();
5389   RECORDCOMPONENT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5390 }
5391 
5392 #if INCLUDE_CDS
5393 void java_lang_reflect_RecordComponent::serialize_offsets(SerializeClosure* f) {
5394   RECORDCOMPONENT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5395 }
5396 #endif
5397 
5398 void java_lang_reflect_RecordComponent::set_clazz(oop element, oop value) {
5399   element->obj_field_put(_clazz_offset, value);
5400 }
5401 
5402 void java_lang_reflect_RecordComponent::set_name(oop element, oop value) {
5403   element->obj_field_put(_name_offset, value);
5404 }
5405 
5406 void java_lang_reflect_RecordComponent::set_type(oop element, oop value) {
5407   element->obj_field_put(_type_offset, value);
5408 }
5409 
5410 void java_lang_reflect_RecordComponent::set_accessor(oop element, oop value) {
5411   element->obj_field_put(_accessor_offset, value);
5412 }
5413 
5414 void java_lang_reflect_RecordComponent::set_signature(oop element, oop value) {
5415   element->obj_field_put(_signature_offset, value);
5416 }
5417 
5418 void java_lang_reflect_RecordComponent::set_annotations(oop element, oop value) {
5419   element->obj_field_put(_annotations_offset, value);
5420 }
5421 
5422 void java_lang_reflect_RecordComponent::set_typeAnnotations(oop element, oop value) {
5423   element->obj_field_put(_typeAnnotations_offset, value);
5424 }
5425 
5426 // java_lang_InternalError
5427 int java_lang_InternalError::_during_unsafe_access_offset;
5428 
5429 void java_lang_InternalError::set_during_unsafe_access(oop internal_error) {
5430   internal_error->bool_field_put(_during_unsafe_access_offset, true);
5431 }
5432 
5433 jboolean java_lang_InternalError::during_unsafe_access(oop internal_error) {
5434   return internal_error->bool_field(_during_unsafe_access_offset);
5435 }
5436 
5437 void java_lang_InternalError::compute_offsets() {
5438   INTERNALERROR_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
5439 }
5440 
5441 #if INCLUDE_CDS
5442 void java_lang_InternalError::serialize_offsets(SerializeClosure* f) {
5443   INTERNALERROR_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
5444 }
5445 #endif
5446 
5447 #define DO_COMPUTE_OFFSETS(k) k::compute_offsets();
5448 
5449 // Compute field offsets of all the classes in this file
5450 void JavaClasses::compute_offsets() {
5451   if (UseSharedSpaces) {
5452     JVMTI_ONLY(assert(JvmtiExport::is_early_phase() && !(JvmtiExport::should_post_class_file_load_hook() &&
5453                                                          JvmtiExport::has_early_class_hook_env()),
5454                       "JavaClasses::compute_offsets() must be called in early JVMTI phase."));
5455     // None of the classes used by the rest of this function can be replaced by
5456     // JVMTI ClassFileLoadHook.
5457     // We are safe to use the archived offsets, which have already been restored
5458     // by JavaClasses::serialize_offsets, without computing the offsets again.
5459     return;
5460   }
5461 
5462   // We have already called the compute_offsets() of the
5463   // BASIC_JAVA_CLASSES_DO_PART1 classes (java_lang_String, java_lang_Class and
5464   // java_lang_ref_Reference) earlier inside vmClasses::resolve_all()
5465   BASIC_JAVA_CLASSES_DO_PART2(DO_COMPUTE_OFFSETS);
5466 }
5467 
5468 #if INCLUDE_CDS
5469 #define DO_SERIALIZE_OFFSETS(k) k::serialize_offsets(soc);
5470 
5471 void JavaClasses::serialize_offsets(SerializeClosure* soc) {
5472   BASIC_JAVA_CLASSES_DO(DO_SERIALIZE_OFFSETS);
5473 }
5474 #endif
5475 
5476 #if INCLUDE_CDS_JAVA_HEAP
5477 bool JavaClasses::is_supported_for_archiving(oop obj) {
5478   Klass* klass = obj->klass();
5479 
5480   if (klass == vmClasses::ClassLoader_klass() ||  // ClassLoader::loader_data is malloc'ed.
5481       // The next 3 classes are used to implement java.lang.invoke, and are not used directly in
5482       // regular Java code. The implementation of java.lang.invoke uses generated hidden classes
5483       // (e.g., as referenced by ResolvedMethodName::vmholder) that are not yet supported by CDS.
5484       // So for now we cannot not support these classes for archiving.
5485       //
5486       // These objects typically are not referenced by static fields, but rather by resolved
5487       // constant pool entries, so excluding them shouldn't affect the archiving of static fields.
5488       klass == vmClasses::ResolvedMethodName_klass() ||
5489       klass == vmClasses::MemberName_klass() ||
5490       klass == vmClasses::Context_klass()) {
5491     return false;
5492   }
5493 
5494   return true;
5495 }
5496 #endif
5497 
5498 #ifndef PRODUCT
5499 
5500 // These functions exist to assert the validity of de-serialized offsets in boxing object as a sanity check.
5501 
5502 bool JavaClasses::check_offset(const char *klass_name, int deserialized_offset, const char *field_name,
5503                                const char* field_sig) {
5504   EXCEPTION_MARK;
5505   fieldDescriptor fd;
5506   TempNewSymbol klass_sym = SymbolTable::new_symbol(klass_name);
5507   Klass* k = SystemDictionary::resolve_or_fail(klass_sym, true, CATCH);
5508   InstanceKlass* ik = InstanceKlass::cast(k);
5509   TempNewSymbol f_name = SymbolTable::new_symbol(field_name);
5510   TempNewSymbol f_sig  = SymbolTable::new_symbol(field_sig);
5511   if (!ik->find_local_field(f_name, f_sig, &fd)) {
5512     tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
5513     return false;
5514   }
5515   if (fd.is_static()) {
5516     tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name);
5517     return false;
5518   }
5519   if (fd.offset() == deserialized_offset ) {
5520     return true;
5521   } else {
5522     tty->print_cr("Offset of nonstatic field %s.%s is deserialized as %d but should really be %d.",
5523                   klass_name, field_name, deserialized_offset, fd.offset());
5524     return false;
5525   }
5526 }
5527 
5528 void JavaClasses::check_offsets() {
5529   bool valid = true;
5530 
5531 #define CHECK_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
5532   valid &= check_offset(klass_name, cpp_klass_name :: _##field_name ## _offset, #field_name, field_sig)
5533 
5534 #define CHECK_LONG_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
5535   valid &= check_offset(klass_name, cpp_klass_name :: _##long_ ## field_name ## _offset, #field_name, field_sig)
5536 
5537   // Boxed primitive objects (java_lang_boxing_object)
5538 
5539   CHECK_OFFSET("java/lang/Boolean",   java_lang_boxing_object, value, "Z");
5540   CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C");
5541   CHECK_OFFSET("java/lang/Float",     java_lang_boxing_object, value, "F");
5542   CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D");
5543   CHECK_OFFSET("java/lang/Byte",      java_lang_boxing_object, value, "B");
5544   CHECK_OFFSET("java/lang/Short",     java_lang_boxing_object, value, "S");
5545   CHECK_OFFSET("java/lang/Integer",   java_lang_boxing_object, value, "I");
5546   CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J");
5547 
5548   // jdk.internal.vm.Continuation
5549 
5550   // CHECK_OFFSET("jdk/internal/vm/Continuation", jdk_internal_vm_Continuation, target,   "Ljava/lang/Runnable;");
5551   // CHECK_OFFSET("jdk/internal/vm/Continuation", jdk_internal_vm_Continuation, stack,    "[I");
5552   // CHECK_OFFSET("jdk/internal/vm/Continuation", jdk_internal_vm_Continuation, parent,   "Ljdk/internal/vm/Continuation;");
5553   // CHECK_OFFSET("jdk/internal/vm/Continuation", jdk_internal_vm_Continuation, lastFP,   "I");
5554   // CHECK_OFFSET("jdk/internal/vm/Continuation", jdk_internal_vm_Continuation, lastSP,   "I");
5555 
5556   if (!valid) vm_exit_during_initialization("Field offset verification failed");
5557 }
5558 
5559 #endif // PRODUCT
5560 
5561 int InjectedField::compute_offset() {
5562   InstanceKlass* ik = InstanceKlass::cast(klass());
5563   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
5564     if (!may_be_java && !fs.access_flags().is_internal()) {
5565       // Only look at injected fields
5566       continue;
5567     }
5568     if (fs.name() == name() && fs.signature() == signature()) {
5569       return fs.offset();
5570     }
5571   }
5572   ResourceMark rm;
5573   tty->print_cr("Invalid layout of %s at %s/%s%s", ik->external_name(), name()->as_C_string(), signature()->as_C_string(), may_be_java ? " (may_be_java)" : "");
5574 #ifndef PRODUCT
5575   ik->print();
5576   tty->print_cr("all fields:");
5577   for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
5578     tty->print_cr("  name: %s, sig: %s, flags: %08x", fs.name()->as_C_string(), fs.signature()->as_C_string(), fs.access_flags().as_int());
5579   }
5580 #endif //PRODUCT
5581   vm_exit_during_initialization("Invalid layout of well-known class: use -Xlog:class+load=info to see the origin of the problem class");
5582   return -1;
5583 }
5584 
5585 void javaClasses_init() {
5586   JavaClasses::compute_offsets();
5587   JavaClasses::check_offsets();
5588   java_lang_VirtualThread::init_static_notify_jvmti_events();
5589   FilteredFieldsMap::initialize();  // must be done after computing offsets.
5590 }