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     java_lang_Class::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     java_lang_Class::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         java_lang_Class::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 = java_lang_Class::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     java_lang_Class::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     java_lang_Class: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   java_lang_Class::set_module(k->java_mirror(), module());
1620 }
1621 
1622 void java_lang_Class::set_oop_size(HeapWord* java_class, int size) {
1623   assert(_oop_size_offset != 0, "must be set");
1624   assert(size > 0, "Oop size must be greater than zero, not %d", size);
1625   *(int*)(((char*)java_class) + _oop_size_offset) = size;
1626 }
1627 
1628 int  java_lang_Class::static_oop_field_count(oop java_class) {
1629   assert(_static_oop_field_count_offset != 0, "must be set");
1630   return java_class->int_field(_static_oop_field_count_offset);
1631 }
1632 
1633 void java_lang_Class::set_static_oop_field_count(oop java_class, int size) {
1634   assert(_static_oop_field_count_offset != 0, "must be set");
1635   java_class->int_field_put(_static_oop_field_count_offset, size);
1636 }
1637 
1638 oop java_lang_Class::protection_domain(oop java_class) {
1639   assert(_protection_domain_offset != 0, "must be set");
1640   return java_class->obj_field(_protection_domain_offset);
1641 }
1642 void java_lang_Class::set_protection_domain(oop java_class, oop pd) {
1643   assert(_protection_domain_offset != 0, "must be set");
1644   java_class->obj_field_put(_protection_domain_offset, pd);
1645 }
1646 
1647 void java_lang_Class::set_component_mirror(oop java_class, oop comp_mirror) {
1648   assert(_component_mirror_offset != 0, "must be set");
1649     java_class->obj_field_put(_component_mirror_offset, comp_mirror);
1650   }
1651 oop java_lang_Class::component_mirror(oop java_class) {
1652   assert(_component_mirror_offset != 0, "must be set");
1653   return java_class->obj_field(_component_mirror_offset);
1654 }
1655 
1656 oop java_lang_Class::init_lock(oop java_class) {
1657   assert(_init_lock_offset != 0, "must be set");
1658   return java_class->obj_field(_init_lock_offset);
1659 }
1660 void java_lang_Class::set_init_lock(oop java_class, oop init_lock) {
1661   assert(_init_lock_offset != 0, "must be set");
1662   java_class->obj_field_put(_init_lock_offset, init_lock);
1663 }
1664 
1665 objArrayOop java_lang_Class::signers(oop java_class) {
1666   assert(_signers_offset != 0, "must be set");
1667   return (objArrayOop)java_class->obj_field(_signers_offset);
1668 }
1669 void java_lang_Class::set_signers(oop java_class, objArrayOop signers) {
1670   assert(_signers_offset != 0, "must be set");
1671   java_class->obj_field_put(_signers_offset, signers);
1672 }
1673 
1674 oop java_lang_Class::class_data(oop java_class) {
1675   assert(_classData_offset != 0, "must be set");
1676   return java_class->obj_field(_classData_offset);
1677 }
1678 void java_lang_Class::set_class_data(oop java_class, oop class_data) {
1679   assert(_classData_offset != 0, "must be set");
1680   java_class->obj_field_put(_classData_offset, class_data);
1681 }
1682 
1683 void java_lang_Class::set_class_loader(oop java_class, oop loader) {
1684   assert(_class_loader_offset != 0, "offsets should have been initialized");
1685   java_class->obj_field_put(_class_loader_offset, loader);
1686 }
1687 
1688 oop java_lang_Class::class_loader(oop java_class) {
1689   assert(_class_loader_offset != 0, "must be set");
1690   return java_class->obj_field(_class_loader_offset);
1691 }
1692 
1693 oop java_lang_Class::module(oop java_class) {
1694   assert(_module_offset != 0, "must be set");
1695   return java_class->obj_field(_module_offset);
1696 }
1697 
1698 void java_lang_Class::set_module(oop java_class, oop module) {
1699   assert(_module_offset != 0, "must be set");
1700   java_class->obj_field_put(_module_offset, module);
1701 }
1702 
1703 oop java_lang_Class::name(Handle java_class, TRAPS) {
1704   assert(_name_offset != 0, "must be set");
1705   oop o = java_class->obj_field(_name_offset);
1706   if (o == NULL) {
1707     o = StringTable::intern(java_lang_Class::as_external_name(java_class()), THREAD);
1708     java_class->obj_field_put(_name_offset, o);
1709   }
1710   return o;
1711 }
1712 
1713 oop java_lang_Class::source_file(oop java_class) {
1714   assert(_source_file_offset != 0, "must be set");
1715   return java_class->obj_field(_source_file_offset);
1716 }
1717 
1718 void java_lang_Class::set_source_file(oop java_class, oop source_file) {
1719   assert(_source_file_offset != 0, "must be set");
1720   java_class->obj_field_put(_source_file_offset, source_file);
1721 }
1722 
1723 oop java_lang_Class::create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS) {
1724   // This should be improved by adding a field at the Java level or by
1725   // introducing a new VM klass (see comment in ClassFileParser)
1726   oop java_class = InstanceMirrorKlass::cast(vmClasses::Class_klass())->allocate_instance(NULL, CHECK_NULL);
1727   if (type != T_VOID) {
1728     Klass* aklass = Universe::typeArrayKlassObj(type);
1729     assert(aklass != NULL, "correct bootstrap");
1730     release_set_array_klass(java_class, aklass);
1731   }
1732 #ifdef ASSERT
1733   InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(vmClasses::Class_klass());
1734   assert(java_lang_Class::static_oop_field_count(java_class) == 0, "should have been zeroed by allocation");
1735 #endif
1736   return java_class;
1737 }
1738 
1739 void java_lang_Class::set_klass(oop java_class, Klass* klass) {
1740   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1741   java_class->metadata_field_put(_klass_offset, klass);
1742 }
1743 
1744 
1745 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1746   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1747   Symbol* name = NULL;
1748   bool is_instance = false;
1749   if (is_primitive(java_class)) {
1750     name = vmSymbols::type_signature(primitive_type(java_class));
1751   } else {
1752     Klass* k = as_Klass(java_class);
1753     is_instance = k->is_instance_klass();
1754     name = k->name();
1755   }
1756   if (name == NULL) {
1757     st->print("<null>");
1758     return;
1759   }
1760   if (is_instance)  st->print("L");
1761   st->write((char*) name->base(), (int) name->utf8_length());
1762   if (is_instance)  st->print(";");
1763 }
1764 
1765 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found) {
1766   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1767   Symbol* name;
1768   if (is_primitive(java_class)) {
1769     name = vmSymbols::type_signature(primitive_type(java_class));
1770     // Because this can create a new symbol, the caller has to decrement
1771     // the refcount, so make adjustment here and below for symbols returned
1772     // that are not created or incremented due to a successful lookup.
1773     name->increment_refcount();
1774   } else {
1775     Klass* k = as_Klass(java_class);
1776     if (!k->is_instance_klass()) {
1777       name = k->name();
1778       name->increment_refcount();
1779     } else {
1780       ResourceMark rm;
1781       const char* sigstr = k->signature_name();
1782       int         siglen = (int) strlen(sigstr);
1783       if (!intern_if_not_found) {
1784         name = SymbolTable::probe(sigstr, siglen);
1785       } else {
1786         name = SymbolTable::new_symbol(sigstr, siglen);
1787       }
1788     }
1789   }
1790   return name;
1791 }
1792 
1793 // Returns the Java name for this Java mirror (Resource allocated)
1794 // See Klass::external_name().
1795 // For primitive type Java mirrors, its type name is returned.
1796 const char* java_lang_Class::as_external_name(oop java_class) {
1797   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1798   const char* name = NULL;
1799   if (is_primitive(java_class)) {
1800     name = type2name(primitive_type(java_class));
1801   } else {
1802     name = as_Klass(java_class)->external_name();
1803   }
1804   if (name == NULL) {
1805     name = "<null>";
1806   }
1807   return name;
1808 }
1809 
1810 Klass* java_lang_Class::array_klass_acquire(oop java_class) {
1811   Klass* k = ((Klass*)java_class->metadata_field_acquire(_array_klass_offset));
1812   assert(k == NULL || k->is_klass() && k->is_array_klass(), "should be array klass");
1813   return k;
1814 }
1815 
1816 
1817 void java_lang_Class::release_set_array_klass(oop java_class, Klass* klass) {
1818   assert(klass->is_klass() && klass->is_array_klass(), "should be array klass");
1819   java_class->release_metadata_field_put(_array_klass_offset, klass);
1820 }
1821 
1822 
1823 BasicType java_lang_Class::primitive_type(oop java_class) {
1824   assert(java_lang_Class::is_primitive(java_class), "just checking");
1825   Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset));
1826   BasicType type = T_VOID;
1827   if (ak != NULL) {
1828     // Note: create_basic_type_mirror above initializes ak to a non-null value.
1829     type = ArrayKlass::cast(ak)->element_type();
1830   } else {
1831     assert(java_class == Universe::void_mirror(), "only valid non-array primitive");
1832   }
1833   assert(Universe::java_mirror(type) == java_class, "must be consistent");
1834   return type;
1835 }
1836 
1837 BasicType java_lang_Class::as_BasicType(oop java_class, Klass** reference_klass) {
1838   assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1839   if (is_primitive(java_class)) {
1840     if (reference_klass != NULL)
1841       (*reference_klass) = NULL;
1842     return primitive_type(java_class);
1843   } else {
1844     if (reference_klass != NULL)
1845       (*reference_klass) = as_Klass(java_class);
1846     return T_OBJECT;
1847   }
1848 }
1849 
1850 
1851 oop java_lang_Class::primitive_mirror(BasicType t) {
1852   oop mirror = Universe::java_mirror(t);
1853   assert(mirror != NULL && mirror->is_a(vmClasses::Class_klass()), "must be a Class");
1854   assert(java_lang_Class::is_primitive(mirror), "must be primitive");
1855   return mirror;
1856 }
1857 
1858 #define CLASS_FIELDS_DO(macro) \
1859   macro(_classRedefinedCount_offset, k, "classRedefinedCount", int_signature,         false); \
1860   macro(_class_loader_offset,        k, "classLoader",         classloader_signature, false); \
1861   macro(_component_mirror_offset,    k, "componentType",       class_signature,       false); \
1862   macro(_module_offset,              k, "module",              module_signature,      false); \
1863   macro(_name_offset,                k, "name",                string_signature,      false); \
1864   macro(_classData_offset,           k, "classData",           object_signature,      false);
1865 
1866 void java_lang_Class::compute_offsets() {
1867   if (_offsets_computed) {
1868     return;
1869   }
1870 
1871   _offsets_computed = true;
1872 
1873   InstanceKlass* k = vmClasses::Class_klass();
1874   CLASS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1875 
1876   // Init lock is a C union with component_mirror.  Only instanceKlass mirrors have
1877   // init_lock and only ArrayKlass mirrors have component_mirror.  Since both are oops
1878   // GC treats them the same.
1879   _init_lock_offset = _component_mirror_offset;
1880 
1881   CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
1882 }
1883 
1884 #if INCLUDE_CDS
1885 void java_lang_Class::serialize_offsets(SerializeClosure* f) {
1886   f->do_bool(&_offsets_computed);
1887   f->do_u4((u4*)&_init_lock_offset);
1888 
1889   CLASS_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1890 
1891   CLASS_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
1892 }
1893 #endif
1894 
1895 int java_lang_Class::classRedefinedCount(oop the_class_mirror) {
1896   assert(_classRedefinedCount_offset != 0, "offsets should have been initialized");
1897   return the_class_mirror->int_field(_classRedefinedCount_offset);
1898 }
1899 
1900 void java_lang_Class::set_classRedefinedCount(oop the_class_mirror, int value) {
1901   assert(_classRedefinedCount_offset != 0, "offsets should have been initialized");
1902   the_class_mirror->int_field_put(_classRedefinedCount_offset, value);
1903 }
1904 
1905 
1906 // Note: JDK1.1 and before had a privateInfo_offset field which was used for the
1907 //       platform thread structure, and a eetop offset which was used for thread
1908 //       local storage (and unused by the HotSpot VM). In JDK1.2 the two structures
1909 //       merged, so in the HotSpot VM we just use the eetop field for the thread
1910 //       instead of the privateInfo_offset.
1911 //
1912 // Note: The stackSize field is only present starting in 1.4.
1913 
1914 int java_lang_Thread_FieldHolder::_group_offset;
1915 int java_lang_Thread_FieldHolder::_priority_offset;
1916 int java_lang_Thread_FieldHolder::_stackSize_offset;
1917 int java_lang_Thread_FieldHolder::_stillborn_offset;
1918 int java_lang_Thread_FieldHolder::_daemon_offset;
1919 int java_lang_Thread_FieldHolder::_thread_status_offset;
1920 
1921 #define THREAD_FIELD_HOLDER_FIELDS_DO(macro) \
1922   macro(_group_offset,         k, vmSymbols::group_name(), threadgroup_signature, false); \
1923   macro(_priority_offset,      k, vmSymbols::priority_name(), int_signature, false); \
1924   macro(_stackSize_offset,     k, "stackSize", long_signature, false); \
1925   macro(_stillborn_offset,     k, "stillborn", bool_signature, false); \
1926   macro(_daemon_offset,        k, vmSymbols::daemon_name(), bool_signature, false); \
1927   macro(_thread_status_offset, k, "threadStatus", int_signature, false)
1928 
1929 void java_lang_Thread_FieldHolder::compute_offsets() {
1930   assert(_group_offset == 0, "offsets should be initialized only once");
1931 
1932   InstanceKlass* k = vmClasses::Thread_FieldHolder_klass();
1933   THREAD_FIELD_HOLDER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1934 }
1935 
1936 #if INCLUDE_CDS
1937 void java_lang_Thread_FieldHolder::serialize_offsets(SerializeClosure* f) {
1938   THREAD_FIELD_HOLDER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1939 }
1940 #endif
1941 
1942 oop java_lang_Thread_FieldHolder::threadGroup(oop holder) {
1943   return holder->obj_field(_group_offset);
1944 }
1945 
1946 ThreadPriority java_lang_Thread_FieldHolder::priority(oop holder) {
1947   return (ThreadPriority)holder->int_field(_priority_offset);
1948 }
1949 
1950 void java_lang_Thread_FieldHolder::set_priority(oop holder, ThreadPriority priority) {
1951   holder->int_field_put(_priority_offset, priority);
1952 }
1953 
1954 jlong java_lang_Thread_FieldHolder::stackSize(oop holder) {
1955   return holder->long_field(_stackSize_offset);
1956 }
1957 
1958 bool java_lang_Thread_FieldHolder::is_stillborn(oop holder) {
1959   return holder->bool_field(_stillborn_offset) != 0;
1960 }
1961 
1962 void java_lang_Thread_FieldHolder::set_stillborn(oop holder) {
1963   holder->bool_field_put(_stillborn_offset, true);
1964 }
1965 
1966 bool java_lang_Thread_FieldHolder::is_daemon(oop holder) {
1967   return holder->bool_field(_daemon_offset) != 0;
1968 }
1969 
1970 void java_lang_Thread_FieldHolder::set_daemon(oop holder) {
1971   holder->bool_field_put(_daemon_offset, true);
1972 }
1973 
1974 void java_lang_Thread_FieldHolder::set_thread_status(oop holder, JavaThreadStatus status) {
1975   holder->int_field_put(_thread_status_offset, static_cast<int>(status));
1976 }
1977 
1978 JavaThreadStatus java_lang_Thread_FieldHolder::get_thread_status(oop holder) {
1979   return static_cast<JavaThreadStatus>(holder->int_field(_thread_status_offset));
1980 }
1981 
1982 
1983 int java_lang_Thread_VirtualThreads::_static_THREAD_GROUP_offset = 0;
1984 
1985 #define THREAD_VIRTUAL_THREADS_STATIC_FIELDS_DO(macro) \
1986   macro(_static_THREAD_GROUP_offset, k, "THREAD_GROUP", threadgroup_signature, true);
1987 
1988 void java_lang_Thread_VirtualThreads::compute_offsets() {
1989   assert(_static_THREAD_GROUP_offset == 0, "offsets should be initialized only once");
1990 
1991   InstanceKlass* k = vmClasses::Thread_VirtualThreads_klass();
1992   THREAD_VIRTUAL_THREADS_STATIC_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1993 }
1994 
1995 #if INCLUDE_CDS
1996 void java_lang_Thread_VirtualThreads::serialize_offsets(SerializeClosure* f) {
1997   THREAD_VIRTUAL_THREADS_STATIC_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
1998 }
1999 #endif
2000 
2001 oop java_lang_Thread_VirtualThreads::get_THREAD_GROUP() {
2002   InstanceKlass* k = vmClasses::Thread_VirtualThreads_klass();
2003   oop base = k->static_field_base_raw();
2004   return base->obj_field(_static_THREAD_GROUP_offset);
2005 }
2006 
2007 
2008 int java_lang_Thread_ClassLoaders::_static_NOT_SUPPORTED_offset = 0;
2009 
2010 #define THREAD_CLASS_LOADERS_STATIC_FIELDS_DO(macro) \
2011   macro(_static_NOT_SUPPORTED_offset, k, "NOT_SUPPORTED", classloader_signature, true);
2012 
2013 void java_lang_Thread_ClassLoaders::compute_offsets() {
2014   assert(_static_NOT_SUPPORTED_offset == 0, "offsets should be initialized only once");
2015 
2016   InstanceKlass* k = vmClasses::Thread_ClassLoaders_klass();
2017   THREAD_CLASS_LOADERS_STATIC_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2018 }
2019 
2020 #if INCLUDE_CDS
2021 void java_lang_Thread_ClassLoaders::serialize_offsets(SerializeClosure* f) {
2022   THREAD_CLASS_LOADERS_STATIC_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2023 }
2024 #endif
2025 
2026 oop java_lang_Thread_ClassLoaders::get_NOT_SUPPORTED() {
2027   InstanceKlass* k = vmClasses::Thread_ClassLoaders_klass();
2028   oop base = k->static_field_base_raw();
2029   return base->obj_field(_static_NOT_SUPPORTED_offset);
2030 }
2031 
2032 int java_lang_Thread::_holder_offset;
2033 int java_lang_Thread::_name_offset;
2034 int java_lang_Thread::_contextClassLoader_offset;
2035 int java_lang_Thread::_inheritedAccessControlContext_offset;
2036 int java_lang_Thread::_eetop_offset;
2037 int java_lang_Thread::_jvmti_thread_state_offset;
2038 int java_lang_Thread::_interrupted_offset;
2039 int java_lang_Thread::_tid_offset;
2040 int java_lang_Thread::_continuation_offset;
2041 int java_lang_Thread::_park_blocker_offset;
2042 int java_lang_Thread::_scopeLocalBindings_offset;
2043 
2044 #define THREAD_FIELDS_DO(macro) \
2045   macro(_holder_offset,        k, "holder", thread_fieldholder_signature, false); \
2046   macro(_name_offset,          k, vmSymbols::name_name(), string_signature, false); \
2047   macro(_contextClassLoader_offset, k, vmSymbols::contextClassLoader_name(), classloader_signature, false); \
2048   macro(_inheritedAccessControlContext_offset, k, vmSymbols::inheritedAccessControlContext_name(), accesscontrolcontext_signature, false); \
2049   macro(_eetop_offset,         k, "eetop", long_signature, false); \
2050   macro(_interrupted_offset,   k, "interrupted", bool_signature, false); \
2051   macro(_tid_offset,           k, "tid", long_signature, false); \
2052   macro(_park_blocker_offset,  k, "parkBlocker", object_signature, false); \
2053   macro(_continuation_offset,  k, "cont", continuation_signature, false); \
2054   macro(_scopeLocalBindings_offset, k, "scopeLocalBindings", scopeLocalSnapshot_name, false);
2055 
2056 void java_lang_Thread::compute_offsets() {
2057   assert(_holder_offset == 0, "offsets should be initialized only once");
2058 
2059   InstanceKlass* k = vmClasses::Thread_klass();
2060   THREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2061   THREAD_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
2062 }
2063 
2064 #if INCLUDE_CDS
2065 void java_lang_Thread::serialize_offsets(SerializeClosure* f) {
2066   THREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2067   THREAD_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
2068 }
2069 #endif
2070 
2071 JavaThread* java_lang_Thread::thread(oop java_thread) {
2072   return (JavaThread*)java_thread->address_field(_eetop_offset);
2073 }
2074 
2075 void java_lang_Thread::set_thread(oop java_thread, JavaThread* thread) {
2076   java_thread->address_field_put(_eetop_offset, (address)thread);
2077 }
2078 
2079 JvmtiThreadState* java_lang_Thread::jvmti_thread_state(oop java_thread) {
2080   return (JvmtiThreadState*)java_thread->address_field(_jvmti_thread_state_offset);
2081 }
2082 
2083 void java_lang_Thread::set_jvmti_thread_state(oop java_thread, JvmtiThreadState* state) {
2084   java_thread->address_field_put(_jvmti_thread_state_offset, (address)state);
2085 }
2086 
2087 void java_lang_Thread::clear_scopeLocalBindings(oop java_thread) {
2088   java_thread->obj_field_put(_scopeLocalBindings_offset, NULL);
2089 }
2090 
2091 oop java_lang_Thread::holder(oop java_thread) {
2092     return java_thread->obj_field(_holder_offset);
2093 }
2094 
2095 bool java_lang_Thread::interrupted(oop java_thread) {
2096   // Make sure the caller can safely access oops.
2097   assert(Thread::current()->is_VM_thread() ||
2098          (JavaThread::current()->thread_state() != _thread_blocked &&
2099           JavaThread::current()->thread_state() != _thread_in_native),
2100          "Unsafe access to oop");
2101   return java_thread->bool_field_volatile(_interrupted_offset);
2102 }
2103 
2104 void java_lang_Thread::set_interrupted(oop java_thread, bool val) {
2105   // Make sure the caller can safely access oops.
2106   assert(Thread::current()->is_VM_thread() ||
2107          (JavaThread::current()->thread_state() != _thread_blocked &&
2108           JavaThread::current()->thread_state() != _thread_in_native),
2109          "Unsafe access to oop");
2110   java_thread->bool_field_put_volatile(_interrupted_offset, val);
2111 }
2112 
2113 
2114 oop java_lang_Thread::name(oop java_thread) {
2115   return java_thread->obj_field(_name_offset);
2116 }
2117 
2118 
2119 void java_lang_Thread::set_name(oop java_thread, oop name) {
2120   java_thread->obj_field_put(_name_offset, name);
2121 }
2122 
2123 
2124 ThreadPriority java_lang_Thread::priority(oop java_thread) {
2125   oop holder = java_lang_Thread::holder(java_thread);
2126   assert(holder != NULL, "Java Thread not initialized");
2127   return java_lang_Thread_FieldHolder::priority(holder);
2128 }
2129 
2130 
2131 void java_lang_Thread::set_priority(oop java_thread, ThreadPriority priority) {
2132   oop holder = java_lang_Thread::holder(java_thread);
2133   assert(holder != NULL, "Java Thread not initialized");
2134   java_lang_Thread_FieldHolder::set_priority(holder, priority);
2135 }
2136 
2137 
2138 oop java_lang_Thread::threadGroup(oop java_thread) {
2139   oop holder = java_lang_Thread::holder(java_thread);
2140   assert(holder != NULL, "Java Thread not initialized");
2141   return java_lang_Thread_FieldHolder::threadGroup(holder);
2142 }
2143 
2144 
2145 bool java_lang_Thread::is_stillborn(oop java_thread) {
2146   oop holder = java_lang_Thread::holder(java_thread);
2147   assert(holder != NULL, "Java Thread not initialized");
2148   return java_lang_Thread_FieldHolder::is_stillborn(holder);
2149 }
2150 
2151 
2152 // We never have reason to turn the stillborn bit off
2153 void java_lang_Thread::set_stillborn(oop java_thread) {
2154   oop holder = java_lang_Thread::holder(java_thread);
2155   assert(holder != NULL, "Java Thread not initialized");
2156   java_lang_Thread_FieldHolder::set_stillborn(holder);
2157 }
2158 
2159 
2160 bool java_lang_Thread::is_alive(oop java_thread) {
2161   JavaThread* thr = java_lang_Thread::thread(java_thread);
2162   return (thr != NULL);
2163 }
2164 
2165 
2166 bool java_lang_Thread::is_daemon(oop java_thread) {
2167   oop holder = java_lang_Thread::holder(java_thread);
2168   assert(holder != NULL, "Java Thread not initialized");
2169   return java_lang_Thread_FieldHolder::is_daemon(holder);
2170 }
2171 
2172 void java_lang_Thread::set_daemon(oop java_thread) {
2173   oop holder = java_lang_Thread::holder(java_thread);
2174   assert(holder != NULL, "Java Thread not initialized");
2175   java_lang_Thread_FieldHolder::set_daemon(holder);
2176 }
2177 
2178 oop java_lang_Thread::context_class_loader(oop java_thread) {
2179   return java_thread->obj_field(_contextClassLoader_offset);
2180 }
2181 
2182 oop java_lang_Thread::inherited_access_control_context(oop java_thread) {
2183   return java_thread->obj_field(_inheritedAccessControlContext_offset);
2184 }
2185 
2186 
2187 jlong java_lang_Thread::stackSize(oop java_thread) {
2188   oop holder = java_lang_Thread::holder(java_thread);
2189   assert(holder != NULL, "Java Thread not initialized");
2190   return java_lang_Thread_FieldHolder::stackSize(holder);
2191 }
2192 
2193 // Write the thread status value to threadStatus field in java.lang.Thread java class.
2194 void java_lang_Thread::set_thread_status(oop java_thread, JavaThreadStatus status) {
2195   oop holder = java_lang_Thread::holder(java_thread);
2196   assert(holder != NULL, "Java Thread not initialized");
2197   java_lang_Thread_FieldHolder::set_thread_status(holder, status);
2198 }
2199 
2200 // Read thread status value from threadStatus field in java.lang.Thread java class.
2201 JavaThreadStatus java_lang_Thread::get_thread_status(oop java_thread) {
2202   // Make sure the caller is operating on behalf of the VM or is
2203   // running VM code (state == _thread_in_vm).
2204   assert(Threads_lock->owned_by_self() || Thread::current()->is_VM_thread() ||
2205          JavaThread::current()->thread_state() == _thread_in_vm,
2206          "Java Thread is not running in vm");
2207   oop holder = java_lang_Thread::holder(java_thread);
2208   if (holder == NULL) {
2209     return JavaThreadStatus::NEW;  // Java Thread not initialized
2210   } else {
2211     return java_lang_Thread_FieldHolder::get_thread_status(holder);
2212   }
2213 }
2214 
2215 jlong java_lang_Thread::thread_id(oop java_thread) {
2216   // The 16 most significant bits can be used for tracing
2217   // so these bits are excluded using a mask.
2218   static const jlong tid_mask = (((jlong)1) << 48) - 1;
2219   return java_thread->long_field(_tid_offset) & tid_mask;
2220 }
2221 
2222 ByteSize java_lang_Thread::thread_id_offset() {
2223   return in_ByteSize(_tid_offset);
2224 }
2225 
2226 oop java_lang_Thread::park_blocker(oop java_thread) {
2227   return java_thread->obj_field(_park_blocker_offset);
2228 }
2229 
2230 oop java_lang_Thread::async_get_stack_trace(oop java_thread, TRAPS) {
2231   JavaThread* thread;
2232   bool is_virtual = java_lang_VirtualThread::is_instance(java_thread);
2233   if (is_virtual) {
2234     oop carrier_thread = java_lang_VirtualThread::carrier_thread(java_thread);
2235     if (carrier_thread == NULL) {
2236       return NULL;
2237     }
2238     thread = java_lang_Thread::thread(carrier_thread);
2239   } else {
2240     thread = java_lang_Thread::thread(java_thread);
2241   }
2242 
2243   class GetStackTraceClosure : public HandshakeClosure {
2244   public:
2245     const Handle _java_thread;
2246     int _depth;
2247     GrowableArray<Method*>* _methods;
2248     GrowableArray<int>*     _bcis;
2249     GrowableArray<oop>*     _continuations;
2250 
2251     GetStackTraceClosure(Handle java_thread) :
2252       HandshakeClosure("GetStackTraceClosure"), _java_thread(java_thread), _depth(0) {
2253       // Pick some initial length
2254       int init_length = MaxJavaStackTraceDepth/2;
2255       _methods = new GrowableArray<Method*>(init_length);
2256       _bcis = new GrowableArray<int>(init_length);
2257       _continuations = new GrowableArray<oop>(init_length);
2258     }
2259 
2260     bool can_be_processed_by(Thread* thread) {
2261       return thread->is_Java_thread();
2262     }
2263 
2264     void do_thread(Thread* th) {
2265       JavaThread* thread = JavaThread::cast(th);
2266 
2267       if (!thread->has_last_Java_frame()) {
2268         return;
2269       }
2270 
2271       bool carrier = false;
2272       oop vthread_scope = java_lang_VirtualThread::vthread_scope();
2273       if (java_lang_VirtualThread::is_instance(_java_thread())) {
2274         // if (thread->vthread() != _java_thread()) // We might be inside a System.executeOnCarrierThread
2275         if (thread->last_continuation(vthread_scope)->cont_oop() !=
2276               java_lang_VirtualThread::continuation(_java_thread())) {
2277           return; // not mounted
2278         }
2279       } else {
2280         carrier = (thread->last_continuation(vthread_scope) != NULL);
2281       }
2282 
2283       const int max_depth = MaxJavaStackTraceDepth;
2284       const bool skip_hidden = !ShowHiddenFrames;
2285 
2286       int total_count = 0;
2287       for (vframeStream vfst(thread, false, false, carrier);
2288            !vfst.at_end() && (max_depth == 0 || max_depth != total_count);
2289            vfst.next()) {
2290 
2291         if (skip_hidden && (vfst.method()->is_hidden() ||
2292                             vfst.method()->is_continuation_enter_intrinsic())) continue;
2293         _methods->push(vfst.method());
2294         _bcis->push(vfst.bci());
2295         _continuations->push(contScopeName(vfst.continuation()));
2296         total_count++;
2297       }
2298 
2299       _depth = total_count;
2300     }
2301 
2302     oop contScopeName(oop cont) {
2303       return cont == NULL ? NULL : jdk_internal_vm_ContinuationScope::name(Continuation::continuation_scope(cont));
2304     }
2305   };
2306 
2307   // Handshake with target
2308   ResourceMark rm(THREAD);
2309   HandleMark   hm(THREAD);
2310   GetStackTraceClosure gstc(Handle(THREAD, java_thread));
2311   Handshake::execute(&gstc, thread);
2312 
2313   // Stop if no stack trace is found.
2314   if (gstc._depth == 0) {
2315     return NULL;
2316   }
2317 
2318   // Convert the continuations into handles before allocation.
2319   assert(gstc._depth == gstc._continuations->length(), "should be the same");
2320   GrowableArray<Handle>* cont_handles = new GrowableArray<Handle>(gstc._depth);
2321   for (int i = 0; i < gstc._depth; i++) {
2322     cont_handles->push(Handle(THREAD, gstc._continuations->at(i)));
2323   }
2324 
2325   // Convert to StackTraceElement array
2326   InstanceKlass* k = vmClasses::StackTraceElement_klass();
2327   assert(k != NULL, "must be loaded in 1.4+");
2328   if (k->should_be_initialized()) k->initialize(CHECK_NULL);
2329   objArrayHandle trace = oopFactory::new_objArray_handle(k, gstc._depth, CHECK_NULL);
2330 
2331   for (int i = 0; i < gstc._depth; i++) {
2332     methodHandle method(THREAD, gstc._methods->at(i));
2333     oop element = java_lang_StackTraceElement::create(method,
2334                                                       gstc._bcis->at(i),
2335                                                       cont_handles->at(i),
2336                                                       CHECK_NULL);
2337     trace->obj_at_put(i, element);
2338   }
2339 
2340   return trace();
2341 }
2342 
2343 const char* java_lang_Thread::thread_status_name(oop java_thread) {
2344   oop holder = java_lang_Thread::holder(java_thread);
2345   assert(holder != NULL, "Java Thread not initialized");
2346   JavaThreadStatus status = java_lang_Thread_FieldHolder::get_thread_status(holder);
2347   switch (status) {
2348     case JavaThreadStatus::NEW                      : return "NEW";
2349     case JavaThreadStatus::RUNNABLE                 : return "RUNNABLE";
2350     case JavaThreadStatus::SLEEPING                 : return "TIMED_WAITING (sleeping)";
2351     case JavaThreadStatus::IN_OBJECT_WAIT           : return "WAITING (on object monitor)";
2352     case JavaThreadStatus::IN_OBJECT_WAIT_TIMED     : return "TIMED_WAITING (on object monitor)";
2353     case JavaThreadStatus::PARKED                   : return "WAITING (parking)";
2354     case JavaThreadStatus::PARKED_TIMED             : return "TIMED_WAITING (parking)";
2355     case JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER : return "BLOCKED (on object monitor)";
2356     case JavaThreadStatus::TERMINATED               : return "TERMINATED";
2357     default                       : return "UNKNOWN";
2358   };
2359 }
2360 int java_lang_ThreadGroup::_parent_offset;
2361 int java_lang_ThreadGroup::_name_offset;
2362 int java_lang_ThreadGroup::_maxPriority_offset;
2363 int java_lang_ThreadGroup::_ngroups_offset;
2364 int java_lang_ThreadGroup::_groups_offset;
2365 int java_lang_ThreadGroup::_nweaks_offset;
2366 int java_lang_ThreadGroup::_weaks_offset;
2367 
2368 oop  java_lang_ThreadGroup::parent(oop java_thread_group) {
2369   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
2370   return java_thread_group->obj_field(_parent_offset);
2371 }
2372 
2373 // ("name as oop" accessor is not necessary)
2374 
2375 const char* java_lang_ThreadGroup::name(oop java_thread_group) {
2376   oop name = java_thread_group->obj_field(_name_offset);
2377   // ThreadGroup.name can be null
2378   if (name != NULL) {
2379     return java_lang_String::as_utf8_string(name);
2380   }
2381   return NULL;
2382 }
2383 
2384 ThreadPriority java_lang_ThreadGroup::maxPriority(oop java_thread_group) {
2385   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
2386   return (ThreadPriority) java_thread_group->int_field(_maxPriority_offset);
2387 }
2388 
2389 int java_lang_ThreadGroup::ngroups(oop java_thread_group) {
2390   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
2391   return java_thread_group->int_field(_ngroups_offset);
2392 }
2393 
2394 objArrayOop java_lang_ThreadGroup::groups(oop java_thread_group) {
2395   oop groups = java_thread_group->obj_field(_groups_offset);
2396   assert(groups == NULL || groups->is_objArray(), "just checking"); // Todo: Add better type checking code
2397   return objArrayOop(groups);
2398 }
2399 
2400 int java_lang_ThreadGroup::nweaks(oop java_thread_group) {
2401   assert(oopDesc::is_oop(java_thread_group), "thread group must be oop");
2402   return java_thread_group->int_field(_nweaks_offset);
2403 }
2404 
2405 objArrayOop java_lang_ThreadGroup::weaks(oop java_thread_group) {
2406   oop weaks = java_thread_group->obj_field(_weaks_offset);
2407   assert(weaks == NULL || weaks->is_objArray(), "just checking");
2408   return objArrayOop(weaks);
2409 }
2410 
2411 #define THREADGROUP_FIELDS_DO(macro) \
2412   macro(_parent_offset,      k, vmSymbols::parent_name(),      threadgroup_signature,       false); \
2413   macro(_name_offset,        k, vmSymbols::name_name(),        string_signature,            false); \
2414   macro(_maxPriority_offset, k, vmSymbols::maxPriority_name(), int_signature,               false); \
2415   macro(_ngroups_offset,     k, vmSymbols::ngroups_name(),     int_signature,               false); \
2416   macro(_groups_offset,      k, vmSymbols::groups_name(),      threadgroup_array_signature, false); \
2417   macro(_nweaks_offset,      k, vmSymbols::nweaks_name(),      int_signature,               false); \
2418   macro(_weaks_offset,       k, vmSymbols::weaks_name(),       weakreference_array_signature, false);
2419 
2420 void java_lang_ThreadGroup::compute_offsets() {
2421   assert(_parent_offset == 0, "offsets should be initialized only once");
2422 
2423   InstanceKlass* k = vmClasses::ThreadGroup_klass();
2424   THREADGROUP_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2425 }
2426 
2427 #if INCLUDE_CDS
2428 void java_lang_ThreadGroup::serialize_offsets(SerializeClosure* f) {
2429   THREADGROUP_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2430 }
2431 #endif
2432 
2433 
2434 // java_lang_VirtualThread
2435 
2436 int java_lang_VirtualThread::static_notify_jvmti_events_offset;
2437 int java_lang_VirtualThread::static_vthread_scope_offset;
2438 int java_lang_VirtualThread::_carrierThread_offset;
2439 int java_lang_VirtualThread::_continuation_offset;
2440 int java_lang_VirtualThread::_state_offset;
2441 
2442 #define VTHREAD_FIELDS_DO(macro) \
2443   macro(static_notify_jvmti_events_offset,  k, "notifyJvmtiEvents",  bool_signature, true); \
2444   macro(static_vthread_scope_offset,  k, "VTHREAD_SCOPE",  continuationscope_signature, true); \
2445   macro(_carrierThread_offset,  k, "carrierThread",  thread_signature, false); \
2446   macro(_continuation_offset,  k, "cont",  continuation_signature, false); \
2447   macro(_state_offset,  k, "state",  int_signature, false)
2448 
2449 static jboolean vthread_notify_jvmti_events = JNI_FALSE;
2450 
2451 void java_lang_VirtualThread::compute_offsets() {
2452   InstanceKlass* k = vmClasses::VirtualThread_klass();
2453   VTHREAD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2454 }
2455 
2456 void java_lang_VirtualThread::init_static_notify_jvmti_events() {
2457   if (vthread_notify_jvmti_events) {
2458     InstanceKlass* ik = vmClasses::VirtualThread_klass();
2459     oop base = ik->static_field_base_raw();
2460     base->release_bool_field_put(static_notify_jvmti_events_offset, vthread_notify_jvmti_events);
2461   }
2462 }
2463 
2464 bool java_lang_VirtualThread::is_instance(oop obj) {
2465   return obj != NULL && is_subclass(obj->klass());
2466 }
2467 
2468 oop java_lang_VirtualThread::carrier_thread(oop vthread) {
2469   oop thread = vthread->obj_field(_carrierThread_offset);
2470   return thread;
2471 }
2472 
2473 oop java_lang_VirtualThread::continuation(oop vthread) {
2474   oop cont = vthread->obj_field(_continuation_offset);
2475   return cont;
2476 }
2477 
2478 jshort java_lang_VirtualThread::state(oop vthread) {
2479   return vthread->short_field_acquire(_state_offset);
2480 }
2481 
2482 JavaThreadStatus java_lang_VirtualThread::map_state_to_thread_status(jint state) {
2483   JavaThreadStatus status = JavaThreadStatus::NEW;
2484   switch (state) {
2485     case NEW :
2486       status = JavaThreadStatus::NEW;
2487       break;
2488     case STARTED :
2489     case RUNNABLE :
2490     case RUNNING :
2491     case PARKING :
2492     case YIELDING :
2493       status = JavaThreadStatus::RUNNABLE;
2494       break;
2495     case PARKED :
2496     case PARKED_SUSPENDED :
2497     case PINNED :
2498       status = JavaThreadStatus::PARKED;
2499       break;
2500     case TERMINATED :
2501       status = JavaThreadStatus::TERMINATED;
2502       break;
2503     default:
2504       ShouldNotReachHere();
2505   }
2506   return status;
2507 }
2508 
2509 #if INCLUDE_CDS
2510 void java_lang_VirtualThread::serialize_offsets(SerializeClosure* f) {
2511    VTHREAD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2512 }
2513 #endif
2514 
2515 bool java_lang_VirtualThread::notify_jvmti_events() {
2516   return vthread_notify_jvmti_events == JNI_TRUE;
2517 }
2518 
2519 void java_lang_VirtualThread::set_notify_jvmti_events(jboolean enable) {
2520   vthread_notify_jvmti_events = enable;
2521 }
2522 
2523 
2524 // java_lang_Throwable
2525 
2526 int java_lang_Throwable::_backtrace_offset;
2527 int java_lang_Throwable::_detailMessage_offset;
2528 int java_lang_Throwable::_stackTrace_offset;
2529 int java_lang_Throwable::_depth_offset;
2530 int java_lang_Throwable::_cause_offset;
2531 int java_lang_Throwable::_static_unassigned_stacktrace_offset;
2532 
2533 #define THROWABLE_FIELDS_DO(macro) \
2534   macro(_backtrace_offset,     k, "backtrace",     object_signature,                  false); \
2535   macro(_detailMessage_offset, k, "detailMessage", string_signature,                  false); \
2536   macro(_stackTrace_offset,    k, "stackTrace",    java_lang_StackTraceElement_array, false); \
2537   macro(_depth_offset,         k, "depth",         int_signature,                     false); \
2538   macro(_cause_offset,         k, "cause",         throwable_signature,               false); \
2539   macro(_static_unassigned_stacktrace_offset, k, "UNASSIGNED_STACK", java_lang_StackTraceElement_array, true)
2540 
2541 void java_lang_Throwable::compute_offsets() {
2542   InstanceKlass* k = vmClasses::Throwable_klass();
2543   THROWABLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
2544 }
2545 
2546 #if INCLUDE_CDS
2547 void java_lang_Throwable::serialize_offsets(SerializeClosure* f) {
2548   THROWABLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
2549 }
2550 #endif
2551 
2552 oop java_lang_Throwable::unassigned_stacktrace() {
2553   InstanceKlass* ik = vmClasses::Throwable_klass();
2554   oop base = ik->static_field_base_raw();
2555   return base->obj_field(_static_unassigned_stacktrace_offset);
2556 }
2557 
2558 oop java_lang_Throwable::backtrace(oop throwable) {
2559   return throwable->obj_field_acquire(_backtrace_offset);
2560 }
2561 
2562 
2563 void java_lang_Throwable::set_backtrace(oop throwable, oop value) {
2564   throwable->release_obj_field_put(_backtrace_offset, value);
2565 }
2566 
2567 int java_lang_Throwable::depth(oop throwable) {
2568   return throwable->int_field(_depth_offset);
2569 }
2570 
2571 void java_lang_Throwable::set_depth(oop throwable, int value) {
2572   throwable->int_field_put(_depth_offset, value);
2573 }
2574 
2575 oop java_lang_Throwable::message(oop throwable) {
2576   return throwable->obj_field(_detailMessage_offset);
2577 }
2578 
2579 oop java_lang_Throwable::cause(oop throwable) {
2580   return throwable->obj_field(_cause_offset);
2581 }
2582 
2583 // Return Symbol for detailed_message or NULL
2584 Symbol* java_lang_Throwable::detail_message(oop throwable) {
2585   PreserveExceptionMark pm(Thread::current());
2586   oop detailed_message = java_lang_Throwable::message(throwable);
2587   if (detailed_message != NULL) {
2588     return java_lang_String::as_symbol(detailed_message);
2589   }
2590   return NULL;
2591 }
2592 
2593 void java_lang_Throwable::set_message(oop throwable, oop value) {
2594   throwable->obj_field_put(_detailMessage_offset, value);
2595 }
2596 
2597 
2598 void java_lang_Throwable::set_stacktrace(oop throwable, oop st_element_array) {
2599   throwable->obj_field_put(_stackTrace_offset, st_element_array);
2600 }
2601 
2602 void java_lang_Throwable::clear_stacktrace(oop throwable) {
2603   set_stacktrace(throwable, NULL);
2604 }
2605 
2606 
2607 void java_lang_Throwable::print(oop throwable, outputStream* st) {
2608   ResourceMark rm;
2609   Klass* k = throwable->klass();
2610   assert(k != NULL, "just checking");
2611   st->print("%s", k->external_name());
2612   oop msg = message(throwable);
2613   if (msg != NULL) {
2614     st->print(": %s", java_lang_String::as_utf8_string(msg));
2615   }
2616 }
2617 
2618 // After this many redefines, the stack trace is unreliable.
2619 const int MAX_VERSION = USHRT_MAX;
2620 
2621 static inline bool version_matches(Method* method, int version) {
2622   assert(version < MAX_VERSION, "version is too big");
2623   return method != NULL && (method->constants()->version() == version);
2624 }
2625 
2626 // Print stack trace element to resource allocated buffer
2627 static void print_stack_element_to_stream(outputStream* st, Handle mirror, int method_id,
2628                                           int version, int bci, Symbol* name) {
2629   ResourceMark rm;
2630 
2631   // Get strings and string lengths
2632   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(mirror()));
2633   const char* klass_name  = holder->external_name();
2634   int buf_len = (int)strlen(klass_name);
2635 
2636   char* method_name = name->as_C_string();
2637   buf_len += (int)strlen(method_name);
2638 
2639   char* source_file_name = NULL;
2640   Symbol* source = Backtrace::get_source_file_name(holder, version);
2641   if (source != NULL) {
2642     source_file_name = source->as_C_string();
2643     buf_len += (int)strlen(source_file_name);
2644   }
2645 
2646   char *module_name = NULL, *module_version = NULL;
2647   ModuleEntry* module = holder->module();
2648   if (module->is_named()) {
2649     module_name = module->name()->as_C_string();
2650     buf_len += (int)strlen(module_name);
2651     if (module->version() != NULL) {
2652       module_version = module->version()->as_C_string();
2653       buf_len += (int)strlen(module_version);
2654     }
2655   }
2656 
2657   // Allocate temporary buffer with extra space for formatting and line number
2658   char* buf = NEW_RESOURCE_ARRAY(char, buf_len + 64);
2659 
2660   // Print stack trace line in buffer
2661   sprintf(buf, "\tat %s.%s(", klass_name, method_name);
2662 
2663   // Print module information
2664   if (module_name != NULL) {
2665     if (module_version != NULL) {
2666       sprintf(buf + (int)strlen(buf), "%s@%s/", module_name, module_version);
2667     } else {
2668       sprintf(buf + (int)strlen(buf), "%s/", module_name);
2669     }
2670   }
2671 
2672   // The method can be NULL if the requested class version is gone
2673   Method* method = holder->method_with_orig_idnum(method_id, version);
2674   if (!version_matches(method, version)) {
2675     strcat(buf, "Redefined)");
2676   } else {
2677     int line_number = Backtrace::get_line_number(method, bci);
2678     if (line_number == -2) {
2679       strcat(buf, "Native Method)");
2680     } else {
2681       if (source_file_name != NULL && (line_number != -1)) {
2682         // Sourcename and linenumber
2683         sprintf(buf + (int)strlen(buf), "%s:%d)", source_file_name, line_number);
2684       } else if (source_file_name != NULL) {
2685         // Just sourcename
2686         sprintf(buf + (int)strlen(buf), "%s)", source_file_name);
2687       } else {
2688         // Neither sourcename nor linenumber
2689         sprintf(buf + (int)strlen(buf), "Unknown Source)");
2690       }
2691       CompiledMethod* nm = method->code();
2692       if (WizardMode && nm != NULL) {
2693         sprintf(buf + (int)strlen(buf), "(nmethod " INTPTR_FORMAT ")", (intptr_t)nm);
2694       }
2695     }
2696   }
2697 
2698   st->print_cr("%s", buf);
2699 }
2700 
2701 void java_lang_Throwable::print_stack_element(outputStream *st, Method* method, int bci) {
2702   Handle mirror (Thread::current(),  method->method_holder()->java_mirror());
2703   int method_id = method->orig_method_idnum();
2704   int version = method->constants()->version();
2705   print_stack_element_to_stream(st, mirror, method_id, version, bci, method->name());
2706 }
2707 
2708 /**
2709  * Print the throwable message and its stack trace plus all causes by walking the
2710  * cause chain.  The output looks the same as of Throwable.printStackTrace().
2711  */
2712 void java_lang_Throwable::print_stack_trace(Handle throwable, outputStream* st) {
2713   // First, print the message.
2714   print(throwable(), st);
2715   st->cr();
2716 
2717   // Now print the stack trace.
2718   JavaThread* THREAD = JavaThread::current(); // For exception macros.
2719   while (throwable.not_null()) {
2720     objArrayHandle result (THREAD, objArrayOop(backtrace(throwable())));
2721     if (result.is_null()) {
2722       st->print_raw_cr("\t<<no stack trace available>>");
2723       return;
2724     }
2725     BacktraceIterator iter(result, THREAD);
2726 
2727     while (iter.repeat()) {
2728       BacktraceElement bte = iter.next(THREAD);
2729       print_stack_element_to_stream(st, bte._mirror, bte._method_id, bte._version, bte._bci, bte._name);
2730     }
2731     {
2732       // Call getCause() which doesn't necessarily return the _cause field.
2733       ExceptionMark em(THREAD);
2734       JavaValue cause(T_OBJECT);
2735       JavaCalls::call_virtual(&cause,
2736                               throwable,
2737                               throwable->klass(),
2738                               vmSymbols::getCause_name(),
2739                               vmSymbols::void_throwable_signature(),
2740                               THREAD);
2741       // Ignore any exceptions. we are in the middle of exception handling. Same as classic VM.
2742       if (HAS_PENDING_EXCEPTION) {
2743         CLEAR_PENDING_EXCEPTION;
2744         throwable = Handle();
2745       } else {
2746         throwable = Handle(THREAD, cause.get_oop());
2747         if (throwable.not_null()) {
2748           st->print("Caused by: ");
2749           print(throwable(), st);
2750           st->cr();
2751         }
2752       }
2753     }
2754   }
2755 }
2756 
2757 /**
2758  * Print the throwable stack trace by calling the Java method java.lang.Throwable.printStackTrace().
2759  */
2760 void java_lang_Throwable::java_printStackTrace(Handle throwable, TRAPS) {
2761   assert(throwable->is_a(vmClasses::Throwable_klass()), "Throwable instance expected");
2762   JavaValue result(T_VOID);
2763   JavaCalls::call_virtual(&result,
2764                           throwable,
2765                           vmClasses::Throwable_klass(),
2766                           vmSymbols::printStackTrace_name(),
2767                           vmSymbols::void_method_signature(),
2768                           THREAD);
2769 }
2770 
2771 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method, TRAPS) {
2772   if (!StackTraceInThrowable) return;
2773   ResourceMark rm(THREAD);
2774   HandleMark hm(THREAD);
2775 
2776   // Start out by clearing the backtrace for this object, in case the VM
2777   // runs out of memory while allocating the stack trace
2778   set_backtrace(throwable(), NULL);
2779   // Clear lazily constructed Java level stacktrace if refilling occurs
2780   // This is unnecessary in 1.7+ but harmless
2781   clear_stacktrace(throwable());
2782 
2783   int max_depth = MaxJavaStackTraceDepth;
2784   JavaThread* thread = THREAD;
2785 
2786   BacktraceBuilder bt(CHECK);
2787 
2788   // If there is no Java frame just return the method that was being called
2789   // with bci 0
2790   if (!thread->has_last_Java_frame()) {
2791     if (max_depth >= 1 && method() != NULL) {
2792       bt.push(method(), 0, NULL, CHECK);
2793       log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), 1);
2794       set_depth(throwable(), 1);
2795       set_backtrace(throwable(), bt.backtrace());
2796     }
2797     return;
2798   }
2799 
2800   // Instead of using vframe directly, this version of fill_in_stack_trace
2801   // basically handles everything by hand. This significantly improved the
2802   // speed of this method call up to 28.5% on Solaris sparc. 27.1% on Windows.
2803   // See bug 6333838 for  more details.
2804   // The "ASSERT" here is to verify this method generates the exactly same stack
2805   // trace as utilizing vframe.
2806 #ifdef ASSERT
2807   vframeStream st(thread, false /* stop_at_java_call_stub */, false /* process_frames */);
2808 #endif
2809   int total_count = 0;
2810   RegisterMap map(thread, false /* update */, false /* process_frames */, true /* walk_cont */);
2811   int decode_offset = 0;
2812   CompiledMethod* nm = NULL;
2813   bool skip_fillInStackTrace_check = false;
2814   bool skip_throwableInit_check = false;
2815   bool skip_hidden = !ShowHiddenFrames;
2816   bool show_carrier = ShowCarrierFrames;
2817   Handle cont_h(THREAD, thread->last_continuation()->cont_oop());
2818   for (frame fr = thread->last_frame(); max_depth == 0 || max_depth != total_count;) {
2819     Method* method = NULL;
2820     int bci = 0;
2821     oop contScopeName = (cont_h() != NULL) ? jdk_internal_vm_ContinuationScope::name(jdk_internal_vm_Continuation::scope(cont_h())) : (oop)NULL;
2822 
2823     // Compiled java method case.
2824     if (decode_offset != 0) {
2825       DebugInfoReadStream stream(nm, decode_offset);
2826       decode_offset = stream.read_int();
2827       method = (Method*)nm->metadata_at(stream.read_int());
2828       bci = stream.read_bci();
2829     } else {
2830       if (fr.is_first_frame()) break;
2831 
2832       if (cont_h() != NULL && Continuation::is_continuation_enterSpecial(fr)) {
2833         oop scope = jdk_internal_vm_Continuation::scope(cont_h());
2834         if (!show_carrier && scope == java_lang_VirtualThread::vthread_scope()) break;
2835 
2836         Handle parent_h(THREAD, jdk_internal_vm_Continuation::parent(cont_h()));
2837         cont_h =  parent_h;
2838       }
2839 
2840       address pc = fr.pc();
2841       if (fr.is_interpreted_frame()) {
2842         address bcp;
2843         if (!map.in_cont()) {
2844           bcp = fr.interpreter_frame_bcp();
2845           method = fr.interpreter_frame_method();
2846         } else {
2847           bcp = map.stack_chunk()->interpreter_frame_bcp(fr);
2848           method = map.stack_chunk()->interpreter_frame_method(fr);
2849         }
2850         bci =  method->bci_from(bcp);
2851         fr = fr.sender(&map);
2852       } else {
2853         CodeBlob* cb = fr.cb();
2854         // HMMM QQQ might be nice to have frame return nm as NULL if cb is non-NULL
2855         // but non nmethod
2856         fr = fr.sender(&map);
2857         if (cb == NULL || !cb->is_compiled()) {
2858           continue;
2859         }
2860         nm = cb->as_compiled_method();
2861         assert (nm->method() != NULL, "must be");
2862         if (nm->method()->is_native()) {
2863           method = nm->method();
2864           bci = 0;
2865         } else {
2866           PcDesc* pd = nm->pc_desc_at(pc);
2867           decode_offset = pd->scope_decode_offset();
2868           // if decode_offset is not equal to 0, it will execute the
2869           // "compiled java method case" at the beginning of the loop.
2870           continue;
2871         }
2872       }
2873     }
2874 #ifdef ASSERT
2875     if (!st.at_end()) { // TODO LOOM remove once we show only vthread trace
2876       assert(st.method() == method && st.bci() == bci, "Wrong stack trace");
2877       st.next();
2878     }
2879 #endif
2880 
2881     // the format of the stacktrace will be:
2882     // - 1 or more fillInStackTrace frames for the exception class (skipped)
2883     // - 0 or more <init> methods for the exception class (skipped)
2884     // - rest of the stack
2885 
2886     if (!skip_fillInStackTrace_check) {
2887       if (method->name() == vmSymbols::fillInStackTrace_name() &&
2888           throwable->is_a(method->method_holder())) {
2889         continue;
2890       }
2891       else {
2892         skip_fillInStackTrace_check = true; // gone past them all
2893       }
2894     }
2895     if (!skip_throwableInit_check) {
2896       assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2897 
2898       // skip <init> methods of the exception class and superclasses
2899       // This is simlar to classic VM.
2900       if (method->name() == vmSymbols::object_initializer_name() &&
2901           throwable->is_a(method->method_holder())) {
2902         continue;
2903       } else {
2904         // there are none or we've seen them all - either way stop checking
2905         skip_throwableInit_check = true;
2906       }
2907     }
2908     if (method->is_hidden() || method->is_continuation_enter_intrinsic()) {
2909       if (skip_hidden) {
2910         if (total_count == 0) {
2911           // The top frame will be hidden from the stack trace.
2912           bt.set_has_hidden_top_frame(CHECK);
2913         }
2914         continue;
2915       }
2916     }
2917 
2918     bt.push(method, bci, contScopeName, CHECK);
2919     total_count++;
2920   }
2921 
2922   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), total_count);
2923 
2924   // Put completed stack trace into throwable object
2925   set_backtrace(throwable(), bt.backtrace());
2926   set_depth(throwable(), total_count);
2927 }
2928 
2929 void java_lang_Throwable::fill_in_stack_trace(Handle throwable, const methodHandle& method) {
2930   // No-op if stack trace is disabled
2931   if (!StackTraceInThrowable) {
2932     return;
2933   }
2934 
2935   // Disable stack traces for some preallocated out of memory errors
2936   if (!Universe::should_fill_in_stack_trace(throwable)) {
2937     return;
2938   }
2939 
2940   JavaThread* THREAD = JavaThread::current(); // For exception macros.
2941   PreserveExceptionMark pm(THREAD);
2942 
2943   fill_in_stack_trace(throwable, method, THREAD);
2944   // Ignore exceptions thrown during stack trace filling (OOM) and reinstall the
2945   // original exception via the PreserveExceptionMark destructor.
2946   CLEAR_PENDING_EXCEPTION;
2947 }
2948 
2949 void java_lang_Throwable::allocate_backtrace(Handle throwable, TRAPS) {
2950   // Allocate stack trace - backtrace is created but not filled in
2951 
2952   // No-op if stack trace is disabled
2953   if (!StackTraceInThrowable) return;
2954   BacktraceBuilder bt(CHECK);   // creates a backtrace
2955   set_backtrace(throwable(), bt.backtrace());
2956 }
2957 
2958 
2959 void java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(Handle throwable) {
2960   // Fill in stack trace into preallocated backtrace (no GC)
2961 
2962   // No-op if stack trace is disabled
2963   if (!StackTraceInThrowable) return;
2964 
2965   assert(throwable->is_a(vmClasses::Throwable_klass()), "sanity check");
2966 
2967   JavaThread* THREAD = JavaThread::current(); // For exception macros.
2968 
2969   objArrayHandle backtrace (THREAD, (objArrayOop)java_lang_Throwable::backtrace(throwable()));
2970   assert(backtrace.not_null(), "backtrace should have been preallocated");
2971 
2972   ResourceMark rm(THREAD);
2973   vframeStream st(THREAD, false /* stop_at_java_call_stub */, false /* process_frames */);
2974 
2975   BacktraceBuilder bt(THREAD, backtrace);
2976 
2977   // Unlike fill_in_stack_trace we do not skip fillInStackTrace or throwable init
2978   // methods as preallocated errors aren't created by "java" code.
2979 
2980   // fill in as much stack trace as possible
2981   int chunk_count = 0;
2982   for (;!st.at_end(); st.next()) {
2983     bt.push(st.method(), st.bci(), NULL, CHECK);
2984     chunk_count++;
2985 
2986     // Bail-out for deep stacks
2987     if (chunk_count >= trace_chunk_size) break;
2988   }
2989   set_depth(throwable(), chunk_count);
2990   log_info(stacktrace)("%s, %d", throwable->klass()->external_name(), chunk_count);
2991 
2992   // We support the Throwable immutability protocol defined for Java 7.
2993   java_lang_Throwable::set_stacktrace(throwable(), java_lang_Throwable::unassigned_stacktrace());
2994   assert(java_lang_Throwable::unassigned_stacktrace() != NULL, "not initialized");
2995 }
2996 
2997 void java_lang_Throwable::get_stack_trace_elements(int depth, Handle backtrace,
2998                                                    objArrayHandle stack_trace_array_h, TRAPS) {
2999 
3000   if (backtrace.is_null() || stack_trace_array_h.is_null()) {
3001     THROW(vmSymbols::java_lang_NullPointerException());
3002   }
3003 
3004   assert(stack_trace_array_h->is_objArray(), "Stack trace array should be an array of StackTraceElenent");
3005 
3006   if (stack_trace_array_h->length() != depth) {
3007     THROW(vmSymbols::java_lang_IndexOutOfBoundsException());
3008   }
3009 
3010   objArrayHandle result(THREAD, objArrayOop(backtrace()));
3011   BacktraceIterator iter(result, THREAD);
3012 
3013   int index = 0;
3014   while (iter.repeat()) {
3015     BacktraceElement bte = iter.next(THREAD);
3016 
3017     Handle stack_trace_element(THREAD, stack_trace_array_h->obj_at(index++));
3018 
3019     if (stack_trace_element.is_null()) {
3020       THROW(vmSymbols::java_lang_NullPointerException());
3021     }
3022 
3023     InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror()));
3024     methodHandle method (THREAD, holder->method_with_orig_idnum(bte._method_id, bte._version));
3025 
3026     java_lang_StackTraceElement::fill_in(stack_trace_element, holder,
3027                                          method,
3028                                          bte._version,
3029                                          bte._bci,
3030                                          bte._name,
3031                                          bte._cont,
3032                                          CHECK);
3033   }
3034 }
3035 
3036 Handle java_lang_Throwable::get_cause_with_stack_trace(Handle throwable, TRAPS) {
3037   // Call to JVM to fill in the stack trace and clear declaringClassObject to
3038   // not keep classes alive in the stack trace.
3039   // call this:  public StackTraceElement[] getStackTrace()
3040   assert(throwable.not_null(), "shouldn't be");
3041 
3042   JavaValue result(T_ARRAY);
3043   JavaCalls::call_virtual(&result, throwable,
3044                           vmClasses::Throwable_klass(),
3045                           vmSymbols::getStackTrace_name(),
3046                           vmSymbols::getStackTrace_signature(),
3047                           CHECK_NH);
3048   Handle stack_trace(THREAD, result.get_oop());
3049   assert(stack_trace->is_objArray(), "Should be an array");
3050 
3051   // Throw ExceptionInInitializerError as the cause with this exception in
3052   // the message and stack trace.
3053 
3054   // Now create the message with the original exception and thread name.
3055   Symbol* message = java_lang_Throwable::detail_message(throwable());
3056   ResourceMark rm(THREAD);
3057   stringStream st;
3058   st.print("Exception %s%s ", throwable()->klass()->name()->as_klass_external_name(),
3059              message == nullptr ? "" : ":");
3060   if (message == NULL) {
3061     st.print("[in thread \"%s\"]", THREAD->name());
3062   } else {
3063     st.print("%s [in thread \"%s\"]", message->as_C_string(), THREAD->name());
3064   }
3065 
3066   Symbol* exception_name = vmSymbols::java_lang_ExceptionInInitializerError();
3067   Handle h_cause = Exceptions::new_exception(THREAD, exception_name, st.as_string());
3068 
3069   // If new_exception returns a different exception while creating the exception, return null.
3070   if (h_cause->klass()->name() != exception_name) {
3071     log_info(class, init)("Exception thrown while saving initialization exception %s",
3072                           h_cause->klass()->external_name());
3073     return Handle();
3074   }
3075   java_lang_Throwable::set_stacktrace(h_cause(), stack_trace());
3076   // Clear backtrace because the stacktrace should be used instead.
3077   set_backtrace(h_cause(), NULL);
3078   return h_cause;
3079 }
3080 
3081 bool java_lang_Throwable::get_top_method_and_bci(oop throwable, Method** method, int* bci) {
3082   JavaThread* current = JavaThread::current();
3083   objArrayHandle result(current, objArrayOop(backtrace(throwable)));
3084   BacktraceIterator iter(result, current);
3085   // No backtrace available.
3086   if (!iter.repeat()) return false;
3087 
3088   // If the exception happened in a frame that has been hidden, i.e.,
3089   // omitted from the back trace, we can not compute the message.
3090   oop hidden = ((objArrayOop)backtrace(throwable))->obj_at(trace_hidden_offset);
3091   if (hidden != NULL) {
3092     return false;
3093   }
3094 
3095   // Get first backtrace element.
3096   BacktraceElement bte = iter.next(current);
3097 
3098   InstanceKlass* holder = InstanceKlass::cast(java_lang_Class::as_Klass(bte._mirror()));
3099   assert(holder != NULL, "first element should be non-null");
3100   Method* m = holder->method_with_orig_idnum(bte._method_id, bte._version);
3101 
3102   // Original version is no longer available.
3103   if (m == NULL || !version_matches(m, bte._version)) {
3104     return false;
3105   }
3106 
3107   *method = m;
3108   *bci = bte._bci;
3109   return true;
3110 }
3111 
3112 oop java_lang_StackTraceElement::create(const methodHandle& method, int bci, Handle contScope, TRAPS) {
3113   // Allocate java.lang.StackTraceElement instance
3114   InstanceKlass* k = vmClasses::StackTraceElement_klass();
3115   assert(k != NULL, "must be loaded in 1.4+");
3116   if (k->should_be_initialized()) {
3117     k->initialize(CHECK_NULL);
3118   }
3119 
3120   Handle element = k->allocate_instance_handle(CHECK_NULL);
3121 
3122   int version = method->constants()->version();
3123   fill_in(element, method->method_holder(), method, version, bci, method->name(), contScope, CHECK_NULL);
3124   return element();
3125 }
3126 
3127 void java_lang_StackTraceElement::fill_in(Handle element,
3128                                           InstanceKlass* holder, const methodHandle& method,
3129                                           int version, int bci, Symbol* name, Handle contScopeName, TRAPS) {
3130   assert(element->is_a(vmClasses::StackTraceElement_klass()), "sanity check");
3131 
3132   ResourceMark rm(THREAD);
3133   HandleMark hm(THREAD);
3134 
3135   // Fill in class name
3136   Handle java_class(THREAD, holder->java_mirror());
3137   oop classname = java_lang_Class::name(java_class, CHECK);
3138   java_lang_StackTraceElement::set_declaringClass(element(), classname);
3139   java_lang_StackTraceElement::set_declaringClassObject(element(), java_class());
3140 
3141   oop loader = holder->class_loader();
3142   if (loader != NULL) {
3143     oop loader_name = java_lang_ClassLoader::name(loader);
3144     if (loader_name != NULL)
3145       java_lang_StackTraceElement::set_classLoaderName(element(), loader_name);
3146   }
3147 
3148   // Fill in method name
3149   oop methodname = StringTable::intern(name, CHECK);
3150   java_lang_StackTraceElement::set_methodName(element(), methodname);
3151 
3152   // Fill in module name and version
3153   ModuleEntry* module = holder->module();
3154   if (module->is_named()) {
3155     oop module_name = StringTable::intern(module->name(), CHECK);
3156     java_lang_StackTraceElement::set_moduleName(element(), module_name);
3157     oop module_version;
3158     if (module->version() != NULL) {
3159       module_version = StringTable::intern(module->version(), CHECK);
3160     } else {
3161       module_version = NULL;
3162     }
3163     java_lang_StackTraceElement::set_moduleVersion(element(), module_version);
3164   }
3165 
3166   if (method() == NULL || !version_matches(method(), version)) {
3167     // The method was redefined, accurate line number information isn't available
3168     java_lang_StackTraceElement::set_fileName(element(), NULL);
3169     java_lang_StackTraceElement::set_lineNumber(element(), -1);
3170   } else {
3171     Symbol* source;
3172     oop source_file;
3173     int line_number;
3174     decode_file_and_line(java_class, holder, version, method, bci, source, source_file, line_number, CHECK);
3175 
3176     java_lang_StackTraceElement::set_fileName(element(), source_file);
3177     java_lang_StackTraceElement::set_lineNumber(element(), line_number);
3178   }
3179 
3180   // Fill in continuation scope
3181   java_lang_StackTraceElement::set_contScopeName(element(), contScopeName());
3182 }
3183 
3184 void java_lang_StackTraceElement::decode_file_and_line(Handle java_class,
3185                                                        InstanceKlass* holder,
3186                                                        int version,
3187                                                        const methodHandle& method,
3188                                                        int bci,
3189                                                        Symbol*& source,
3190                                                        oop& source_file,
3191                                                        int& line_number, TRAPS) {
3192   // Fill in source file name and line number.
3193   source = Backtrace::get_source_file_name(holder, version);
3194   source_file = java_lang_Class::source_file(java_class());
3195   if (source != NULL) {
3196     // Class was not redefined. We can trust its cache if set,
3197     // else we have to initialize it.
3198     if (source_file == NULL) {
3199       source_file = StringTable::intern(source, CHECK);
3200       java_lang_Class::set_source_file(java_class(), source_file);
3201     }
3202   } else {
3203     // Class was redefined. Dump the cache if it was set.
3204     if (source_file != NULL) {
3205       source_file = NULL;
3206       java_lang_Class::set_source_file(java_class(), source_file);
3207     }
3208   }
3209   line_number = Backtrace::get_line_number(method(), bci);
3210 }
3211 
3212 #if INCLUDE_JVMCI
3213 void java_lang_StackTraceElement::decode(const methodHandle& method, int bci,
3214                                          Symbol*& filename, int& line_number, TRAPS) {
3215   ResourceMark rm(THREAD);
3216   HandleMark hm(THREAD);
3217 
3218   filename = NULL;
3219   line_number = -1;
3220 
3221   oop source_file;
3222   int version = method->constants()->version();
3223   InstanceKlass* holder = method->method_holder();
3224   Handle java_class(THREAD, holder->java_mirror());
3225   decode_file_and_line(java_class, holder, version, method, bci, filename, source_file, line_number, CHECK);
3226 }
3227 #endif // INCLUDE_JVMCI
3228 
3229 // java_lang_StackFrameInfo
3230 
3231 int java_lang_StackFrameInfo::_memberName_offset;
3232 int java_lang_StackFrameInfo::_bci_offset;
3233 int java_lang_StackFrameInfo::_version_offset;
3234 int java_lang_StackFrameInfo::_contScope_offset;
3235 
3236 #define STACKFRAMEINFO_FIELDS_DO(macro) \
3237   macro(_memberName_offset,     k, "memberName",  object_signature, false); \
3238   macro(_bci_offset,            k, "bci",         int_signature,    false); \
3239   macro(_contScope_offset,      k, "contScope",   continuationscope_signature, false)
3240 
3241 void java_lang_StackFrameInfo::compute_offsets() {
3242   InstanceKlass* k = vmClasses::StackFrameInfo_klass();
3243   STACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3244   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3245 }
3246 
3247 #if INCLUDE_CDS
3248 void java_lang_StackFrameInfo::serialize_offsets(SerializeClosure* f) {
3249   STACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3250   STACKFRAMEINFO_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3251 }
3252 #endif
3253 
3254 Method* java_lang_StackFrameInfo::get_method(Handle stackFrame, InstanceKlass* holder, TRAPS) {
3255   HandleMark hm(THREAD);
3256   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
3257   Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(mname());
3258   // we should expand MemberName::name when Throwable uses StackTrace
3259   // MethodHandles::expand_MemberName(mname, MethodHandles::_suppress_defc|MethodHandles::_suppress_type, CHECK_NULL);
3260   return method;
3261 }
3262 
3263 void java_lang_StackFrameInfo::set_method_and_bci(Handle stackFrame, const methodHandle& method, int bci, oop cont, TRAPS) {
3264   // set Method* or mid/cpref
3265   HandleMark hm(THREAD);
3266   Handle mname(THREAD, stackFrame->obj_field(_memberName_offset));
3267   Handle cont_h (THREAD, cont);
3268   InstanceKlass* ik = method->method_holder();
3269   CallInfo info(method(), ik, CHECK);
3270   MethodHandles::init_method_MemberName(mname, info);
3271   // set bci
3272   java_lang_StackFrameInfo::set_bci(stackFrame(), bci);
3273   // method may be redefined; store the version
3274   int version = method->constants()->version();
3275   assert((jushort)version == version, "version should be short");
3276   java_lang_StackFrameInfo::set_version(stackFrame(), (short)version);
3277 
3278   oop contScope = cont_h() != NULL ? jdk_internal_vm_Continuation::scope(cont_h()) : (oop)NULL;
3279   java_lang_StackFrameInfo::set_contScope(stackFrame(), contScope);
3280 }
3281 
3282 void java_lang_StackFrameInfo::to_stack_trace_element(Handle stackFrame, Handle stack_trace_element, TRAPS) {
3283   ResourceMark rm(THREAD);
3284   HandleMark hm(THREAD);
3285   Handle mname(THREAD, stackFrame->obj_field(java_lang_StackFrameInfo::_memberName_offset));
3286   Klass* clazz = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname()));
3287   InstanceKlass* holder = InstanceKlass::cast(clazz);
3288   Method* method = java_lang_StackFrameInfo::get_method(stackFrame, holder, CHECK);
3289   oop contScope = stackFrame->obj_field(java_lang_StackFrameInfo::_contScope_offset);
3290   Handle contScopeName(THREAD, contScope != (oop)NULL ? jdk_internal_vm_ContinuationScope::name(contScope) : (oop)NULL);
3291 
3292   short version = stackFrame->short_field(_version_offset);
3293   int bci = stackFrame->int_field(_bci_offset);
3294   Symbol* name = method->name();
3295   java_lang_StackTraceElement::fill_in(stack_trace_element, holder, methodHandle(THREAD, method), version, bci, name, contScopeName, CHECK);
3296 }
3297 
3298 void java_lang_StackTraceElement::set_contScopeName(oop element, oop value) {
3299   element->obj_field_put(_contScopeName_offset, value);
3300 }
3301 
3302 void java_lang_StackFrameInfo::set_version(oop element, short value) {
3303   element->short_field_put(_version_offset, value);
3304 }
3305 
3306 void java_lang_StackFrameInfo::set_bci(oop element, int value) {
3307   assert(value >= 0 && value < max_jushort, "must be a valid bci value");
3308   element->int_field_put(_bci_offset, value);
3309 }
3310 
3311 void java_lang_StackFrameInfo::set_contScope(oop element, oop value) {
3312   element->obj_field_put(_contScope_offset, value);
3313 }
3314 
3315 int java_lang_LiveStackFrameInfo::_monitors_offset;
3316 int java_lang_LiveStackFrameInfo::_locals_offset;
3317 int java_lang_LiveStackFrameInfo::_operands_offset;
3318 int java_lang_LiveStackFrameInfo::_mode_offset;
3319 
3320 #define LIVESTACKFRAMEINFO_FIELDS_DO(macro) \
3321   macro(_monitors_offset,   k, "monitors",    object_array_signature, false); \
3322   macro(_locals_offset,     k, "locals",      object_array_signature, false); \
3323   macro(_operands_offset,   k, "operands",    object_array_signature, false); \
3324   macro(_mode_offset,       k, "mode",        int_signature,          false)
3325 
3326 void java_lang_LiveStackFrameInfo::compute_offsets() {
3327   InstanceKlass* k = vmClasses::LiveStackFrameInfo_klass();
3328   LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3329 }
3330 
3331 #if INCLUDE_CDS
3332 void java_lang_LiveStackFrameInfo::serialize_offsets(SerializeClosure* f) {
3333   LIVESTACKFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3334 }
3335 #endif
3336 
3337 void java_lang_LiveStackFrameInfo::set_monitors(oop element, oop value) {
3338   element->obj_field_put(_monitors_offset, value);
3339 }
3340 
3341 void java_lang_LiveStackFrameInfo::set_locals(oop element, oop value) {
3342   element->obj_field_put(_locals_offset, value);
3343 }
3344 
3345 void java_lang_LiveStackFrameInfo::set_operands(oop element, oop value) {
3346   element->obj_field_put(_operands_offset, value);
3347 }
3348 
3349 void java_lang_LiveStackFrameInfo::set_mode(oop element, int value) {
3350   element->int_field_put(_mode_offset, value);
3351 }
3352 
3353 
3354 // java_lang_AccessibleObject
3355 
3356 int java_lang_reflect_AccessibleObject::_override_offset;
3357 
3358 #define ACCESSIBLEOBJECT_FIELDS_DO(macro) \
3359   macro(_override_offset, k, "override", bool_signature, false)
3360 
3361 void java_lang_reflect_AccessibleObject::compute_offsets() {
3362   InstanceKlass* k = vmClasses::reflect_AccessibleObject_klass();
3363   ACCESSIBLEOBJECT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3364 }
3365 
3366 #if INCLUDE_CDS
3367 void java_lang_reflect_AccessibleObject::serialize_offsets(SerializeClosure* f) {
3368   ACCESSIBLEOBJECT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3369 }
3370 #endif
3371 
3372 jboolean java_lang_reflect_AccessibleObject::override(oop reflect) {
3373   return (jboolean) reflect->bool_field(_override_offset);
3374 }
3375 
3376 void java_lang_reflect_AccessibleObject::set_override(oop reflect, jboolean value) {
3377   reflect->bool_field_put(_override_offset, (int) value);
3378 }
3379 
3380 // java_lang_reflect_Method
3381 
3382 int java_lang_reflect_Method::_clazz_offset;
3383 int java_lang_reflect_Method::_name_offset;
3384 int java_lang_reflect_Method::_returnType_offset;
3385 int java_lang_reflect_Method::_parameterTypes_offset;
3386 int java_lang_reflect_Method::_exceptionTypes_offset;
3387 int java_lang_reflect_Method::_slot_offset;
3388 int java_lang_reflect_Method::_modifiers_offset;
3389 int java_lang_reflect_Method::_signature_offset;
3390 int java_lang_reflect_Method::_annotations_offset;
3391 int java_lang_reflect_Method::_parameter_annotations_offset;
3392 int java_lang_reflect_Method::_annotation_default_offset;
3393 
3394 #define METHOD_FIELDS_DO(macro) \
3395   macro(_clazz_offset,          k, vmSymbols::clazz_name(),          class_signature,       false); \
3396   macro(_name_offset,           k, vmSymbols::name_name(),           string_signature,      false); \
3397   macro(_returnType_offset,     k, vmSymbols::returnType_name(),     class_signature,       false); \
3398   macro(_parameterTypes_offset, k, vmSymbols::parameterTypes_name(), class_array_signature, false); \
3399   macro(_exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), class_array_signature, false); \
3400   macro(_slot_offset,           k, vmSymbols::slot_name(),           int_signature,         false); \
3401   macro(_modifiers_offset,      k, vmSymbols::modifiers_name(),      int_signature,         false); \
3402   macro(_signature_offset,             k, vmSymbols::signature_name(),             string_signature,     false); \
3403   macro(_annotations_offset,           k, vmSymbols::annotations_name(),           byte_array_signature, false); \
3404   macro(_parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), byte_array_signature, false); \
3405   macro(_annotation_default_offset,    k, vmSymbols::annotation_default_name(),    byte_array_signature, false);
3406 
3407 void java_lang_reflect_Method::compute_offsets() {
3408   InstanceKlass* k = vmClasses::reflect_Method_klass();
3409   METHOD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3410 }
3411 
3412 #if INCLUDE_CDS
3413 void java_lang_reflect_Method::serialize_offsets(SerializeClosure* f) {
3414   METHOD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3415 }
3416 #endif
3417 
3418 Handle java_lang_reflect_Method::create(TRAPS) {
3419   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3420   Klass* klass = vmClasses::reflect_Method_klass();
3421   // This class is eagerly initialized during VM initialization, since we keep a refence
3422   // to one of the methods
3423   assert(InstanceKlass::cast(klass)->is_initialized(), "must be initialized");
3424   return InstanceKlass::cast(klass)->allocate_instance_handle(THREAD);
3425 }
3426 
3427 oop java_lang_reflect_Method::clazz(oop reflect) {
3428   return reflect->obj_field(_clazz_offset);
3429 }
3430 
3431 void java_lang_reflect_Method::set_clazz(oop reflect, oop value) {
3432    reflect->obj_field_put(_clazz_offset, value);
3433 }
3434 
3435 int java_lang_reflect_Method::slot(oop reflect) {
3436   return reflect->int_field(_slot_offset);
3437 }
3438 
3439 void java_lang_reflect_Method::set_slot(oop reflect, int value) {
3440   reflect->int_field_put(_slot_offset, value);
3441 }
3442 
3443 void java_lang_reflect_Method::set_name(oop method, oop value) {
3444   method->obj_field_put(_name_offset, value);
3445 }
3446 
3447 oop java_lang_reflect_Method::return_type(oop method) {
3448   return method->obj_field(_returnType_offset);
3449 }
3450 
3451 void java_lang_reflect_Method::set_return_type(oop method, oop value) {
3452   method->obj_field_put(_returnType_offset, value);
3453 }
3454 
3455 oop java_lang_reflect_Method::parameter_types(oop method) {
3456   return method->obj_field(_parameterTypes_offset);
3457 }
3458 
3459 void java_lang_reflect_Method::set_parameter_types(oop method, oop value) {
3460   method->obj_field_put(_parameterTypes_offset, value);
3461 }
3462 
3463 void java_lang_reflect_Method::set_exception_types(oop method, oop value) {
3464   method->obj_field_put(_exceptionTypes_offset, value);
3465 }
3466 
3467 void java_lang_reflect_Method::set_modifiers(oop method, int value) {
3468   method->int_field_put(_modifiers_offset, value);
3469 }
3470 
3471 void java_lang_reflect_Method::set_signature(oop method, oop value) {
3472   method->obj_field_put(_signature_offset, value);
3473 }
3474 
3475 void java_lang_reflect_Method::set_annotations(oop method, oop value) {
3476   method->obj_field_put(_annotations_offset, value);
3477 }
3478 
3479 void java_lang_reflect_Method::set_parameter_annotations(oop method, oop value) {
3480   method->obj_field_put(_parameter_annotations_offset, value);
3481 }
3482 
3483 void java_lang_reflect_Method::set_annotation_default(oop method, oop value) {
3484   method->obj_field_put(_annotation_default_offset, value);
3485 }
3486 
3487 int java_lang_reflect_Constructor::_clazz_offset;
3488 int java_lang_reflect_Constructor::_parameterTypes_offset;
3489 int java_lang_reflect_Constructor::_exceptionTypes_offset;
3490 int java_lang_reflect_Constructor::_slot_offset;
3491 int java_lang_reflect_Constructor::_modifiers_offset;
3492 int java_lang_reflect_Constructor::_signature_offset;
3493 int java_lang_reflect_Constructor::_annotations_offset;
3494 int java_lang_reflect_Constructor::_parameter_annotations_offset;
3495 
3496 #define CONSTRUCTOR_FIELDS_DO(macro) \
3497   macro(_clazz_offset,          k, vmSymbols::clazz_name(),          class_signature,       false); \
3498   macro(_parameterTypes_offset, k, vmSymbols::parameterTypes_name(), class_array_signature, false); \
3499   macro(_exceptionTypes_offset, k, vmSymbols::exceptionTypes_name(), class_array_signature, false); \
3500   macro(_slot_offset,           k, vmSymbols::slot_name(),           int_signature,         false); \
3501   macro(_modifiers_offset,      k, vmSymbols::modifiers_name(),      int_signature,         false); \
3502   macro(_signature_offset,             k, vmSymbols::signature_name(),             string_signature,     false); \
3503   macro(_annotations_offset,           k, vmSymbols::annotations_name(),           byte_array_signature, false); \
3504   macro(_parameter_annotations_offset, k, vmSymbols::parameter_annotations_name(), byte_array_signature, false);
3505 
3506 void java_lang_reflect_Constructor::compute_offsets() {
3507   InstanceKlass* k = vmClasses::reflect_Constructor_klass();
3508   CONSTRUCTOR_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3509 }
3510 
3511 #if INCLUDE_CDS
3512 void java_lang_reflect_Constructor::serialize_offsets(SerializeClosure* f) {
3513   CONSTRUCTOR_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3514 }
3515 #endif
3516 
3517 Handle java_lang_reflect_Constructor::create(TRAPS) {
3518   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3519   Symbol* name = vmSymbols::java_lang_reflect_Constructor();
3520   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3521   InstanceKlass* ik = InstanceKlass::cast(k);
3522   // Ensure it is initialized
3523   ik->initialize(CHECK_NH);
3524   return ik->allocate_instance_handle(THREAD);
3525 }
3526 
3527 oop java_lang_reflect_Constructor::clazz(oop reflect) {
3528   return reflect->obj_field(_clazz_offset);
3529 }
3530 
3531 void java_lang_reflect_Constructor::set_clazz(oop reflect, oop value) {
3532    reflect->obj_field_put(_clazz_offset, value);
3533 }
3534 
3535 oop java_lang_reflect_Constructor::parameter_types(oop constructor) {
3536   return constructor->obj_field(_parameterTypes_offset);
3537 }
3538 
3539 void java_lang_reflect_Constructor::set_parameter_types(oop constructor, oop value) {
3540   constructor->obj_field_put(_parameterTypes_offset, value);
3541 }
3542 
3543 void java_lang_reflect_Constructor::set_exception_types(oop constructor, oop value) {
3544   constructor->obj_field_put(_exceptionTypes_offset, value);
3545 }
3546 
3547 int java_lang_reflect_Constructor::slot(oop reflect) {
3548   return reflect->int_field(_slot_offset);
3549 }
3550 
3551 void java_lang_reflect_Constructor::set_slot(oop reflect, int value) {
3552   reflect->int_field_put(_slot_offset, value);
3553 }
3554 
3555 void java_lang_reflect_Constructor::set_modifiers(oop constructor, int value) {
3556   constructor->int_field_put(_modifiers_offset, value);
3557 }
3558 
3559 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) {
3560   constructor->obj_field_put(_signature_offset, value);
3561 }
3562 
3563 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) {
3564   constructor->obj_field_put(_annotations_offset, value);
3565 }
3566 
3567 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) {
3568   method->obj_field_put(_parameter_annotations_offset, value);
3569 }
3570 
3571 int java_lang_reflect_Field::_clazz_offset;
3572 int java_lang_reflect_Field::_name_offset;
3573 int java_lang_reflect_Field::_type_offset;
3574 int java_lang_reflect_Field::_slot_offset;
3575 int java_lang_reflect_Field::_modifiers_offset;
3576 int java_lang_reflect_Field::_trusted_final_offset;
3577 int java_lang_reflect_Field::_signature_offset;
3578 int java_lang_reflect_Field::_annotations_offset;
3579 
3580 #define FIELD_FIELDS_DO(macro) \
3581   macro(_clazz_offset,     k, vmSymbols::clazz_name(),     class_signature,  false); \
3582   macro(_name_offset,      k, vmSymbols::name_name(),      string_signature, false); \
3583   macro(_type_offset,      k, vmSymbols::type_name(),      class_signature,  false); \
3584   macro(_slot_offset,      k, vmSymbols::slot_name(),      int_signature,    false); \
3585   macro(_modifiers_offset, k, vmSymbols::modifiers_name(), int_signature,    false); \
3586   macro(_trusted_final_offset,    k, vmSymbols::trusted_final_name(),    bool_signature,       false); \
3587   macro(_signature_offset,        k, vmSymbols::signature_name(),        string_signature,     false); \
3588   macro(_annotations_offset,      k, vmSymbols::annotations_name(),      byte_array_signature, false);
3589 
3590 void java_lang_reflect_Field::compute_offsets() {
3591   InstanceKlass* k = vmClasses::reflect_Field_klass();
3592   FIELD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3593 }
3594 
3595 #if INCLUDE_CDS
3596 void java_lang_reflect_Field::serialize_offsets(SerializeClosure* f) {
3597   FIELD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3598 }
3599 #endif
3600 
3601 Handle java_lang_reflect_Field::create(TRAPS) {
3602   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3603   Symbol* name = vmSymbols::java_lang_reflect_Field();
3604   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3605   InstanceKlass* ik = InstanceKlass::cast(k);
3606   // Ensure it is initialized
3607   ik->initialize(CHECK_NH);
3608   return ik->allocate_instance_handle(THREAD);
3609 }
3610 
3611 oop java_lang_reflect_Field::clazz(oop reflect) {
3612   return reflect->obj_field(_clazz_offset);
3613 }
3614 
3615 void java_lang_reflect_Field::set_clazz(oop reflect, oop value) {
3616   reflect->obj_field_put(_clazz_offset, value);
3617 }
3618 
3619 oop java_lang_reflect_Field::name(oop field) {
3620   return field->obj_field(_name_offset);
3621 }
3622 
3623 void java_lang_reflect_Field::set_name(oop field, oop value) {
3624   field->obj_field_put(_name_offset, value);
3625 }
3626 
3627 oop java_lang_reflect_Field::type(oop field) {
3628   return field->obj_field(_type_offset);
3629 }
3630 
3631 void java_lang_reflect_Field::set_type(oop field, oop value) {
3632   field->obj_field_put(_type_offset, value);
3633 }
3634 
3635 int java_lang_reflect_Field::slot(oop reflect) {
3636   return reflect->int_field(_slot_offset);
3637 }
3638 
3639 void java_lang_reflect_Field::set_slot(oop reflect, int value) {
3640   reflect->int_field_put(_slot_offset, value);
3641 }
3642 
3643 int java_lang_reflect_Field::modifiers(oop field) {
3644   return field->int_field(_modifiers_offset);
3645 }
3646 
3647 void java_lang_reflect_Field::set_modifiers(oop field, int value) {
3648   field->int_field_put(_modifiers_offset, value);
3649 }
3650 
3651 void java_lang_reflect_Field::set_trusted_final(oop field) {
3652   field->bool_field_put(_trusted_final_offset, true);
3653 }
3654 
3655 void java_lang_reflect_Field::set_signature(oop field, oop value) {
3656   field->obj_field_put(_signature_offset, value);
3657 }
3658 
3659 void java_lang_reflect_Field::set_annotations(oop field, oop value) {
3660   field->obj_field_put(_annotations_offset, value);
3661 }
3662 
3663 oop java_lang_reflect_RecordComponent::create(InstanceKlass* holder, RecordComponent* component, TRAPS) {
3664   // Allocate java.lang.reflect.RecordComponent instance
3665   HandleMark hm(THREAD);
3666   InstanceKlass* ik = vmClasses::RecordComponent_klass();
3667   assert(ik != NULL, "must be loaded");
3668   ik->initialize(CHECK_NULL);
3669 
3670   Handle element = ik->allocate_instance_handle(CHECK_NULL);
3671 
3672   Handle decl_class(THREAD, holder->java_mirror());
3673   java_lang_reflect_RecordComponent::set_clazz(element(), decl_class());
3674 
3675   Symbol* name = holder->constants()->symbol_at(component->name_index()); // name_index is a utf8
3676   oop component_name = StringTable::intern(name, CHECK_NULL);
3677   java_lang_reflect_RecordComponent::set_name(element(), component_name);
3678 
3679   Symbol* type = holder->constants()->symbol_at(component->descriptor_index());
3680   Handle component_type_h =
3681     SystemDictionary::find_java_mirror_for_type(type, holder, SignatureStream::NCDFError, CHECK_NULL);
3682   java_lang_reflect_RecordComponent::set_type(element(), component_type_h());
3683 
3684   Method* accessor_method = NULL;
3685   {
3686     // Prepend "()" to type to create the full method signature.
3687     ResourceMark rm(THREAD);
3688     int sig_len = type->utf8_length() + 3; // "()" and null char
3689     char* sig = NEW_RESOURCE_ARRAY(char, sig_len);
3690     jio_snprintf(sig, sig_len, "%c%c%s", JVM_SIGNATURE_FUNC, JVM_SIGNATURE_ENDFUNC, type->as_C_string());
3691     TempNewSymbol full_sig = SymbolTable::new_symbol(sig);
3692     accessor_method = holder->find_instance_method(name, full_sig, Klass::PrivateLookupMode::find);
3693   }
3694 
3695   if (accessor_method != NULL) {
3696     methodHandle method(THREAD, accessor_method);
3697     oop m = Reflection::new_method(method, false, CHECK_NULL);
3698     java_lang_reflect_RecordComponent::set_accessor(element(), m);
3699   } else {
3700     java_lang_reflect_RecordComponent::set_accessor(element(), NULL);
3701   }
3702 
3703   int sig_index = component->generic_signature_index();
3704   if (sig_index > 0) {
3705     Symbol* sig = holder->constants()->symbol_at(sig_index); // sig_index is a utf8
3706     oop component_sig = StringTable::intern(sig, CHECK_NULL);
3707     java_lang_reflect_RecordComponent::set_signature(element(), component_sig);
3708   } else {
3709     java_lang_reflect_RecordComponent::set_signature(element(), NULL);
3710   }
3711 
3712   typeArrayOop annotation_oop = Annotations::make_java_array(component->annotations(), CHECK_NULL);
3713   java_lang_reflect_RecordComponent::set_annotations(element(), annotation_oop);
3714 
3715   typeArrayOop type_annotation_oop = Annotations::make_java_array(component->type_annotations(), CHECK_NULL);
3716   java_lang_reflect_RecordComponent::set_typeAnnotations(element(), type_annotation_oop);
3717 
3718   return element();
3719 }
3720 
3721 int reflect_ConstantPool::_oop_offset;
3722 
3723 #define CONSTANTPOOL_FIELDS_DO(macro) \
3724   macro(_oop_offset, k, "constantPoolOop", object_signature, false)
3725 
3726 void reflect_ConstantPool::compute_offsets() {
3727   InstanceKlass* k = vmClasses::reflect_ConstantPool_klass();
3728   // The field is called ConstantPool* in the sun.reflect.ConstantPool class.
3729   CONSTANTPOOL_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3730 }
3731 
3732 #if INCLUDE_CDS
3733 void reflect_ConstantPool::serialize_offsets(SerializeClosure* f) {
3734   CONSTANTPOOL_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3735 }
3736 #endif
3737 
3738 int java_lang_reflect_Parameter::_name_offset;
3739 int java_lang_reflect_Parameter::_modifiers_offset;
3740 int java_lang_reflect_Parameter::_index_offset;
3741 int java_lang_reflect_Parameter::_executable_offset;
3742 
3743 #define PARAMETER_FIELDS_DO(macro) \
3744   macro(_name_offset,        k, vmSymbols::name_name(),        string_signature, false); \
3745   macro(_modifiers_offset,   k, vmSymbols::modifiers_name(),   int_signature,    false); \
3746   macro(_index_offset,       k, vmSymbols::index_name(),       int_signature,    false); \
3747   macro(_executable_offset,  k, vmSymbols::executable_name(),  executable_signature, false)
3748 
3749 void java_lang_reflect_Parameter::compute_offsets() {
3750   InstanceKlass* k = vmClasses::reflect_Parameter_klass();
3751   PARAMETER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3752 }
3753 
3754 #if INCLUDE_CDS
3755 void java_lang_reflect_Parameter::serialize_offsets(SerializeClosure* f) {
3756   PARAMETER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3757 }
3758 #endif
3759 
3760 Handle java_lang_reflect_Parameter::create(TRAPS) {
3761   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3762   Symbol* name = vmSymbols::java_lang_reflect_Parameter();
3763   Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3764   InstanceKlass* ik = InstanceKlass::cast(k);
3765   // Ensure it is initialized
3766   ik->initialize(CHECK_NH);
3767   return ik->allocate_instance_handle(THREAD);
3768 }
3769 
3770 oop java_lang_reflect_Parameter::name(oop param) {
3771   return param->obj_field(_name_offset);
3772 }
3773 
3774 void java_lang_reflect_Parameter::set_name(oop param, oop value) {
3775   param->obj_field_put(_name_offset, value);
3776 }
3777 
3778 int java_lang_reflect_Parameter::modifiers(oop param) {
3779   return param->int_field(_modifiers_offset);
3780 }
3781 
3782 void java_lang_reflect_Parameter::set_modifiers(oop param, int value) {
3783   param->int_field_put(_modifiers_offset, value);
3784 }
3785 
3786 int java_lang_reflect_Parameter::index(oop param) {
3787   return param->int_field(_index_offset);
3788 }
3789 
3790 void java_lang_reflect_Parameter::set_index(oop param, int value) {
3791   param->int_field_put(_index_offset, value);
3792 }
3793 
3794 oop java_lang_reflect_Parameter::executable(oop param) {
3795   return param->obj_field(_executable_offset);
3796 }
3797 
3798 void java_lang_reflect_Parameter::set_executable(oop param, oop value) {
3799   param->obj_field_put(_executable_offset, value);
3800 }
3801 
3802 // java_lang_Module
3803 
3804 int java_lang_Module::_loader_offset;
3805 int java_lang_Module::_name_offset;
3806 int java_lang_Module::_module_entry_offset;
3807 
3808 Handle java_lang_Module::create(Handle loader, Handle module_name, TRAPS) {
3809   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3810   return JavaCalls::construct_new_instance(vmClasses::Module_klass(),
3811                           vmSymbols::java_lang_module_init_signature(),
3812                           loader, module_name, CHECK_NH);
3813 }
3814 
3815 #define MODULE_FIELDS_DO(macro) \
3816   macro(_loader_offset,  k, vmSymbols::loader_name(),  classloader_signature, false); \
3817   macro(_name_offset,    k, vmSymbols::name_name(),    string_signature,      false)
3818 
3819 void java_lang_Module::compute_offsets() {
3820   InstanceKlass* k = vmClasses::Module_klass();
3821   MODULE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3822   MODULE_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
3823 }
3824 
3825 #if INCLUDE_CDS
3826 void java_lang_Module::serialize_offsets(SerializeClosure* f) {
3827   MODULE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3828   MODULE_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
3829 }
3830 #endif
3831 
3832 oop java_lang_Module::loader(oop module) {
3833   return module->obj_field(_loader_offset);
3834 }
3835 
3836 void java_lang_Module::set_loader(oop module, oop value) {
3837   module->obj_field_put(_loader_offset, value);
3838 }
3839 
3840 oop java_lang_Module::name(oop module) {
3841   return module->obj_field(_name_offset);
3842 }
3843 
3844 void java_lang_Module::set_name(oop module, oop value) {
3845   module->obj_field_put(_name_offset, value);
3846 }
3847 
3848 ModuleEntry* java_lang_Module::module_entry_raw(oop module) {
3849   assert(_module_entry_offset != 0, "Uninitialized module_entry_offset");
3850   assert(module != NULL, "module can't be null");
3851   assert(oopDesc::is_oop(module), "module must be oop");
3852 
3853   ModuleEntry* module_entry = (ModuleEntry*)module->address_field(_module_entry_offset);
3854   return module_entry;
3855 }
3856 
3857 ModuleEntry* java_lang_Module::module_entry(oop module) {
3858   ModuleEntry* module_entry = module_entry_raw(module);
3859   if (module_entry == NULL) {
3860     // If the inject field containing the ModuleEntry* is null then return the
3861     // class loader's unnamed module.
3862     oop loader = java_lang_Module::loader(module);
3863     Handle h_loader = Handle(Thread::current(), loader);
3864     ClassLoaderData* loader_cld = SystemDictionary::register_loader(h_loader);
3865     return loader_cld->unnamed_module();
3866   }
3867   return module_entry;
3868 }
3869 
3870 void java_lang_Module::set_module_entry(oop module, ModuleEntry* module_entry) {
3871   assert(_module_entry_offset != 0, "Uninitialized module_entry_offset");
3872   assert(module != NULL, "module can't be null");
3873   assert(oopDesc::is_oop(module), "module must be oop");
3874   module->address_field_put(_module_entry_offset, (address)module_entry);
3875 }
3876 
3877 Handle reflect_ConstantPool::create(TRAPS) {
3878   assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3879   InstanceKlass* k = vmClasses::reflect_ConstantPool_klass();
3880   // Ensure it is initialized
3881   k->initialize(CHECK_NH);
3882   return k->allocate_instance_handle(THREAD);
3883 }
3884 
3885 
3886 void reflect_ConstantPool::set_cp(oop reflect, ConstantPool* value) {
3887   oop mirror = value->pool_holder()->java_mirror();
3888   // Save the mirror to get back the constant pool.
3889   reflect->obj_field_put(_oop_offset, mirror);
3890 }
3891 
3892 ConstantPool* reflect_ConstantPool::get_cp(oop reflect) {
3893 
3894   oop mirror = reflect->obj_field(_oop_offset);
3895   Klass* k = java_lang_Class::as_Klass(mirror);
3896   assert(k->is_instance_klass(), "Must be");
3897 
3898   // Get the constant pool back from the klass.  Since class redefinition
3899   // merges the new constant pool into the old, this is essentially the
3900   // same constant pool as the original.  If constant pool merging is
3901   // no longer done in the future, this will have to change to save
3902   // the original.
3903   return InstanceKlass::cast(k)->constants();
3904 }
3905 
3906 int reflect_UnsafeStaticFieldAccessorImpl::_base_offset;
3907 
3908 #define UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(macro) \
3909   macro(_base_offset, k, "base", object_signature, false)
3910 
3911 void reflect_UnsafeStaticFieldAccessorImpl::compute_offsets() {
3912   InstanceKlass* k = vmClasses::reflect_UnsafeStaticFieldAccessorImpl_klass();
3913   UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3914 }
3915 
3916 #if INCLUDE_CDS
3917 void reflect_UnsafeStaticFieldAccessorImpl::serialize_offsets(SerializeClosure* f) {
3918   UNSAFESTATICFIELDACCESSORIMPL_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3919 }
3920 #endif
3921 
3922 // Support for java_lang_ref_Reference
3923 
3924 bool java_lang_ref_Reference::_offsets_initialized;
3925 
3926 int java_lang_ref_Reference::_referent_offset;
3927 int java_lang_ref_Reference::_queue_offset;
3928 int java_lang_ref_Reference::_next_offset;
3929 int java_lang_ref_Reference::_discovered_offset;
3930 
3931 #define REFERENCE_FIELDS_DO(macro) \
3932   macro(_referent_offset,   k, "referent", object_signature, false); \
3933   macro(_queue_offset,      k, "queue", referencequeue_signature, false); \
3934   macro(_next_offset,       k, "next", reference_signature, false); \
3935   macro(_discovered_offset, k, "discovered", reference_signature, false);
3936 
3937 void java_lang_ref_Reference::compute_offsets() {
3938   if (_offsets_initialized) {
3939     return;
3940   }
3941   _offsets_initialized = true;
3942   InstanceKlass* k = vmClasses::Reference_klass();
3943   REFERENCE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3944 }
3945 
3946 #if INCLUDE_CDS
3947 void java_lang_ref_Reference::serialize_offsets(SerializeClosure* f) {
3948   f->do_bool(&_offsets_initialized);
3949   REFERENCE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3950 }
3951 #endif
3952 
3953 bool java_lang_ref_Reference::is_referent_field(oop obj, ptrdiff_t offset) {
3954   assert(obj != NULL, "sanity");
3955   if (offset != _referent_offset) {
3956     return false;
3957   }
3958 
3959   Klass* k = obj->klass();
3960   if (!k->is_instance_klass()) {
3961     return false;
3962   }
3963 
3964   InstanceKlass* ik = InstanceKlass::cast(obj->klass());
3965   bool is_reference = ik->reference_type() != REF_NONE;
3966   assert(!is_reference || ik->is_subclass_of(vmClasses::Reference_klass()), "sanity");
3967   return is_reference;
3968 }
3969 
3970 int java_lang_boxing_object::_value_offset;
3971 int java_lang_boxing_object::_long_value_offset;
3972 
3973 #define BOXING_FIELDS_DO(macro) \
3974   macro(_value_offset,      integerKlass, "value", int_signature, false); \
3975   macro(_long_value_offset, longKlass, "value", long_signature, false);
3976 
3977 void java_lang_boxing_object::compute_offsets() {
3978   InstanceKlass* integerKlass = vmClasses::Integer_klass();
3979   InstanceKlass* longKlass = vmClasses::Long_klass();
3980   BOXING_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3981 }
3982 
3983 #if INCLUDE_CDS
3984 void java_lang_boxing_object::serialize_offsets(SerializeClosure* f) {
3985   BOXING_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3986 }
3987 #endif
3988 
3989 oop java_lang_boxing_object::initialize_and_allocate(BasicType type, TRAPS) {
3990   Klass* k = vmClasses::box_klass(type);
3991   if (k == NULL)  return NULL;
3992   InstanceKlass* ik = InstanceKlass::cast(k);
3993   if (!ik->is_initialized())  ik->initialize(CHECK_NULL);
3994   return ik->allocate_instance(THREAD);
3995 }
3996 
3997 
3998 oop java_lang_boxing_object::create(BasicType type, jvalue* value, TRAPS) {
3999   oop box = initialize_and_allocate(type, CHECK_NULL);
4000   if (box == NULL)  return NULL;
4001   switch (type) {
4002     case T_BOOLEAN:
4003       box->bool_field_put(_value_offset, value->z);
4004       break;
4005     case T_CHAR:
4006       box->char_field_put(_value_offset, value->c);
4007       break;
4008     case T_FLOAT:
4009       box->float_field_put(_value_offset, value->f);
4010       break;
4011     case T_DOUBLE:
4012       box->double_field_put(_long_value_offset, value->d);
4013       break;
4014     case T_BYTE:
4015       box->byte_field_put(_value_offset, value->b);
4016       break;
4017     case T_SHORT:
4018       box->short_field_put(_value_offset, value->s);
4019       break;
4020     case T_INT:
4021       box->int_field_put(_value_offset, value->i);
4022       break;
4023     case T_LONG:
4024       box->long_field_put(_long_value_offset, value->j);
4025       break;
4026     default:
4027       return NULL;
4028   }
4029   return box;
4030 }
4031 
4032 
4033 BasicType java_lang_boxing_object::basic_type(oop box) {
4034   if (box == NULL)  return T_ILLEGAL;
4035   BasicType type = vmClasses::box_klass_type(box->klass());
4036   if (type == T_OBJECT)         // 'unknown' value returned by SD::bkt
4037     return T_ILLEGAL;
4038   return type;
4039 }
4040 
4041 
4042 BasicType java_lang_boxing_object::get_value(oop box, jvalue* value) {
4043   BasicType type = vmClasses::box_klass_type(box->klass());
4044   switch (type) {
4045   case T_BOOLEAN:
4046     value->z = box->bool_field(_value_offset);
4047     break;
4048   case T_CHAR:
4049     value->c = box->char_field(_value_offset);
4050     break;
4051   case T_FLOAT:
4052     value->f = box->float_field(_value_offset);
4053     break;
4054   case T_DOUBLE:
4055     value->d = box->double_field(_long_value_offset);
4056     break;
4057   case T_BYTE:
4058     value->b = box->byte_field(_value_offset);
4059     break;
4060   case T_SHORT:
4061     value->s = box->short_field(_value_offset);
4062     break;
4063   case T_INT:
4064     value->i = box->int_field(_value_offset);
4065     break;
4066   case T_LONG:
4067     value->j = box->long_field(_long_value_offset);
4068     break;
4069   default:
4070     return T_ILLEGAL;
4071   } // end switch
4072   return type;
4073 }
4074 
4075 
4076 BasicType java_lang_boxing_object::set_value(oop box, jvalue* value) {
4077   BasicType type = vmClasses::box_klass_type(box->klass());
4078   switch (type) {
4079   case T_BOOLEAN:
4080     box->bool_field_put(_value_offset, value->z);
4081     break;
4082   case T_CHAR:
4083     box->char_field_put(_value_offset, value->c);
4084     break;
4085   case T_FLOAT:
4086     box->float_field_put(_value_offset, value->f);
4087     break;
4088   case T_DOUBLE:
4089     box->double_field_put(_long_value_offset, value->d);
4090     break;
4091   case T_BYTE:
4092     box->byte_field_put(_value_offset, value->b);
4093     break;
4094   case T_SHORT:
4095     box->short_field_put(_value_offset, value->s);
4096     break;
4097   case T_INT:
4098     box->int_field_put(_value_offset, value->i);
4099     break;
4100   case T_LONG:
4101     box->long_field_put(_long_value_offset, value->j);
4102     break;
4103   default:
4104     return T_ILLEGAL;
4105   } // end switch
4106   return type;
4107 }
4108 
4109 
4110 void java_lang_boxing_object::print(BasicType type, jvalue* value, outputStream* st) {
4111   switch (type) {
4112   case T_BOOLEAN:   st->print("%s", value->z ? "true" : "false");   break;
4113   case T_CHAR:      st->print("%d", value->c);                      break;
4114   case T_BYTE:      st->print("%d", value->b);                      break;
4115   case T_SHORT:     st->print("%d", value->s);                      break;
4116   case T_INT:       st->print("%d", value->i);                      break;
4117   case T_LONG:      st->print(JLONG_FORMAT, value->j);              break;
4118   case T_FLOAT:     st->print("%f", value->f);                      break;
4119   case T_DOUBLE:    st->print("%lf", value->d);                     break;
4120   default:          st->print("type %d?", type);                    break;
4121   }
4122 }
4123 
4124 
4125 // Support for java_lang_ref_SoftReference
4126 //
4127 
4128 int java_lang_ref_SoftReference::_timestamp_offset;
4129 int java_lang_ref_SoftReference::_static_clock_offset;
4130 
4131 #define SOFTREFERENCE_FIELDS_DO(macro) \
4132   macro(_timestamp_offset,    k, "timestamp", long_signature, false); \
4133   macro(_static_clock_offset, k, "clock",     long_signature, true)
4134 
4135 void java_lang_ref_SoftReference::compute_offsets() {
4136   InstanceKlass* k = vmClasses::SoftReference_klass();
4137   SOFTREFERENCE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4138 }
4139 
4140 #if INCLUDE_CDS
4141 void java_lang_ref_SoftReference::serialize_offsets(SerializeClosure* f) {
4142   SOFTREFERENCE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4143 }
4144 #endif
4145 
4146 jlong java_lang_ref_SoftReference::timestamp(oop ref) {
4147   return ref->long_field(_timestamp_offset);
4148 }
4149 
4150 jlong java_lang_ref_SoftReference::clock() {
4151   InstanceKlass* ik = vmClasses::SoftReference_klass();
4152   oop base = ik->static_field_base_raw();
4153   return base->long_field(_static_clock_offset);
4154 }
4155 
4156 void java_lang_ref_SoftReference::set_clock(jlong value) {
4157   InstanceKlass* ik = vmClasses::SoftReference_klass();
4158   oop base = ik->static_field_base_raw();
4159   base->long_field_put(_static_clock_offset, value);
4160 }
4161 
4162 // Support for java_lang_invoke_DirectMethodHandle
4163 
4164 int java_lang_invoke_DirectMethodHandle::_member_offset;
4165 
4166 oop java_lang_invoke_DirectMethodHandle::member(oop dmh) {
4167   oop member_name = NULL;
4168   assert(oopDesc::is_oop(dmh) && java_lang_invoke_DirectMethodHandle::is_instance(dmh),
4169          "a DirectMethodHandle oop is expected");
4170   return dmh->obj_field(_member_offset);
4171 }
4172 
4173 #define DIRECTMETHODHANDLE_FIELDS_DO(macro) \
4174   macro(_member_offset, k, "member", java_lang_invoke_MemberName_signature, false)
4175 
4176 void java_lang_invoke_DirectMethodHandle::compute_offsets() {
4177   InstanceKlass* k = vmClasses::DirectMethodHandle_klass();
4178   DIRECTMETHODHANDLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4179 }
4180 
4181 #if INCLUDE_CDS
4182 void java_lang_invoke_DirectMethodHandle::serialize_offsets(SerializeClosure* f) {
4183   DIRECTMETHODHANDLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4184 }
4185 #endif
4186 
4187 // Support for java_lang_invoke_MethodHandle
4188 
4189 int java_lang_invoke_MethodHandle::_type_offset;
4190 int java_lang_invoke_MethodHandle::_form_offset;
4191 
4192 int java_lang_invoke_MemberName::_clazz_offset;
4193 int java_lang_invoke_MemberName::_name_offset;
4194 int java_lang_invoke_MemberName::_type_offset;
4195 int java_lang_invoke_MemberName::_flags_offset;
4196 int java_lang_invoke_MemberName::_method_offset;
4197 int java_lang_invoke_MemberName::_vmindex_offset;
4198 
4199 int java_lang_invoke_ResolvedMethodName::_vmtarget_offset;
4200 int java_lang_invoke_ResolvedMethodName::_vmholder_offset;
4201 
4202 int java_lang_invoke_LambdaForm::_vmentry_offset;
4203 
4204 #define METHODHANDLE_FIELDS_DO(macro) \
4205   macro(_type_offset, k, vmSymbols::type_name(), java_lang_invoke_MethodType_signature, false); \
4206   macro(_form_offset, k, "form",                 java_lang_invoke_LambdaForm_signature, false)
4207 
4208 void java_lang_invoke_MethodHandle::compute_offsets() {
4209   InstanceKlass* k = vmClasses::MethodHandle_klass();
4210   METHODHANDLE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4211 }
4212 
4213 #if INCLUDE_CDS
4214 void java_lang_invoke_MethodHandle::serialize_offsets(SerializeClosure* f) {
4215   METHODHANDLE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4216 }
4217 #endif
4218 
4219 #define MEMBERNAME_FIELDS_DO(macro) \
4220   macro(_clazz_offset,   k, vmSymbols::clazz_name(),   class_signature,  false); \
4221   macro(_name_offset,    k, vmSymbols::name_name(),    string_signature, false); \
4222   macro(_type_offset,    k, vmSymbols::type_name(),    object_signature, false); \
4223   macro(_flags_offset,   k, vmSymbols::flags_name(),   int_signature,    false); \
4224   macro(_method_offset,  k, vmSymbols::method_name(),  java_lang_invoke_ResolvedMethodName_signature, false)
4225 
4226 void java_lang_invoke_MemberName::compute_offsets() {
4227   InstanceKlass* k = vmClasses::MemberName_klass();
4228   MEMBERNAME_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4229   MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
4230 }
4231 
4232 #if INCLUDE_CDS
4233 void java_lang_invoke_MemberName::serialize_offsets(SerializeClosure* f) {
4234   MEMBERNAME_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4235   MEMBERNAME_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
4236 }
4237 #endif
4238 
4239 void java_lang_invoke_ResolvedMethodName::compute_offsets() {
4240   InstanceKlass* k = vmClasses::ResolvedMethodName_klass();
4241   assert(k != NULL, "jdk mismatch");
4242   RESOLVEDMETHOD_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
4243 }
4244 
4245 #if INCLUDE_CDS
4246 void java_lang_invoke_ResolvedMethodName::serialize_offsets(SerializeClosure* f) {
4247   RESOLVEDMETHOD_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
4248 }
4249 #endif
4250 
4251 #define LAMBDAFORM_FIELDS_DO(macro) \
4252   macro(_vmentry_offset, k, "vmentry", java_lang_invoke_MemberName_signature, false)
4253 
4254 void java_lang_invoke_LambdaForm::compute_offsets() {
4255   InstanceKlass* k = vmClasses::LambdaForm_klass();
4256   assert (k != NULL, "jdk mismatch");
4257   LAMBDAFORM_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4258 }
4259 
4260 #if INCLUDE_CDS
4261 void java_lang_invoke_LambdaForm::serialize_offsets(SerializeClosure* f) {
4262   LAMBDAFORM_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4263 }
4264 #endif
4265 
4266 bool java_lang_invoke_LambdaForm::is_instance(oop obj) {
4267   return obj != NULL && is_subclass(obj->klass());
4268 }
4269 
4270 int jdk_internal_invoke_NativeEntryPoint::_shadow_space_offset;
4271 int jdk_internal_invoke_NativeEntryPoint::_argMoves_offset;
4272 int jdk_internal_invoke_NativeEntryPoint::_returnMoves_offset;
4273 int jdk_internal_invoke_NativeEntryPoint::_need_transition_offset;
4274 int jdk_internal_invoke_NativeEntryPoint::_method_type_offset;
4275 int jdk_internal_invoke_NativeEntryPoint::_name_offset;
4276 
4277 #define NEP_FIELDS_DO(macro) \
4278   macro(_shadow_space_offset,    k, "shadowSpace",    int_signature, false); \
4279   macro(_argMoves_offset,        k, "argMoves",       long_array_signature, false); \
4280   macro(_returnMoves_offset,     k, "returnMoves",    long_array_signature, false); \
4281   macro(_need_transition_offset, k, "needTransition", bool_signature, false); \
4282   macro(_method_type_offset,     k, "methodType",     java_lang_invoke_MethodType_signature, false); \
4283   macro(_name_offset,            k, "name",           string_signature, false);
4284 
4285 bool jdk_internal_invoke_NativeEntryPoint::is_instance(oop obj) {
4286   return obj != NULL && is_subclass(obj->klass());
4287 }
4288 
4289 void jdk_internal_invoke_NativeEntryPoint::compute_offsets() {
4290   InstanceKlass* k = vmClasses::NativeEntryPoint_klass();
4291   NEP_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4292 }
4293 
4294 #if INCLUDE_CDS
4295 void jdk_internal_invoke_NativeEntryPoint::serialize_offsets(SerializeClosure* f) {
4296   NEP_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4297 }
4298 #endif
4299 
4300 jint jdk_internal_invoke_NativeEntryPoint::shadow_space(oop entry) {
4301   return entry->int_field(_shadow_space_offset);
4302 }
4303 
4304 oop jdk_internal_invoke_NativeEntryPoint::argMoves(oop entry) {
4305   return entry->obj_field(_argMoves_offset);
4306 }
4307 
4308 oop jdk_internal_invoke_NativeEntryPoint::returnMoves(oop entry) {
4309   return entry->obj_field(_returnMoves_offset);
4310 }
4311 
4312 jboolean jdk_internal_invoke_NativeEntryPoint::need_transition(oop entry) {
4313   return entry->bool_field(_need_transition_offset);
4314 }
4315 
4316 oop jdk_internal_invoke_NativeEntryPoint::method_type(oop entry) {
4317   return entry->obj_field(_method_type_offset);
4318 }
4319 
4320 oop jdk_internal_invoke_NativeEntryPoint::name(oop entry) {
4321   return entry->obj_field(_name_offset);
4322 }
4323 
4324 oop java_lang_invoke_MethodHandle::type(oop mh) {
4325   return mh->obj_field(_type_offset);
4326 }
4327 
4328 void java_lang_invoke_MethodHandle::set_type(oop mh, oop mtype) {
4329   mh->obj_field_put(_type_offset, mtype);
4330 }
4331 
4332 oop java_lang_invoke_MethodHandle::form(oop mh) {
4333   assert(_form_offset != 0, "");
4334   return mh->obj_field(_form_offset);
4335 }
4336 
4337 void java_lang_invoke_MethodHandle::set_form(oop mh, oop lform) {
4338   assert(_form_offset != 0, "");
4339   mh->obj_field_put(_form_offset, lform);
4340 }
4341 
4342 /// MemberName accessors
4343 
4344 oop java_lang_invoke_MemberName::clazz(oop mname) {
4345   assert(is_instance(mname), "wrong type");
4346   return mname->obj_field(_clazz_offset);
4347 }
4348 
4349 void java_lang_invoke_MemberName::set_clazz(oop mname, oop clazz) {
4350   assert(is_instance(mname), "wrong type");
4351   mname->obj_field_put(_clazz_offset, clazz);
4352 }
4353 
4354 oop java_lang_invoke_MemberName::name(oop mname) {
4355   assert(is_instance(mname), "wrong type");
4356   return mname->obj_field(_name_offset);
4357 }
4358 
4359 void java_lang_invoke_MemberName::set_name(oop mname, oop name) {
4360   assert(is_instance(mname), "wrong type");
4361   mname->obj_field_put(_name_offset, name);
4362 }
4363 
4364 oop java_lang_invoke_MemberName::type(oop mname) {
4365   assert(is_instance(mname), "wrong type");
4366   return mname->obj_field(_type_offset);
4367 }
4368 
4369 void java_lang_invoke_MemberName::set_type(oop mname, oop type) {
4370   assert(is_instance(mname), "wrong type");
4371   mname->obj_field_put(_type_offset, type);
4372 }
4373 
4374 int java_lang_invoke_MemberName::flags(oop mname) {
4375   assert(is_instance(mname), "wrong type");
4376   return mname->int_field(_flags_offset);
4377 }
4378 
4379 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) {
4380   assert(is_instance(mname), "wrong type");
4381   mname->int_field_put(_flags_offset, flags);
4382 }
4383 
4384 
4385 // Return vmtarget from ResolvedMethodName method field through indirection
4386 Method* java_lang_invoke_MemberName::vmtarget(oop mname) {
4387   assert(is_instance(mname), "wrong type");
4388   oop method = mname->obj_field(_method_offset);
4389   return method == NULL ? NULL : java_lang_invoke_ResolvedMethodName::vmtarget(method);
4390 }
4391 
4392 bool java_lang_invoke_MemberName::is_method(oop mname) {
4393   assert(is_instance(mname), "must be MemberName");
4394   return (flags(mname) & (MN_IS_METHOD | MN_IS_CONSTRUCTOR)) > 0;
4395 }
4396 
4397 void java_lang_invoke_MemberName::set_method(oop mname, oop resolved_method) {
4398   assert(is_instance(mname), "wrong type");
4399   mname->obj_field_put(_method_offset, resolved_method);
4400 }
4401 
4402 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) {
4403   assert(is_instance(mname), "wrong type");
4404   return (intptr_t) mname->address_field(_vmindex_offset);
4405 }
4406 
4407 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
4408   assert(is_instance(mname), "wrong type");
4409   mname->address_field_put(_vmindex_offset, (address) index);
4410 }
4411 
4412 
4413 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) {
4414   assert(is_instance(resolved_method), "wrong type");
4415   Method* m = (Method*)resolved_method->address_field(_vmtarget_offset);
4416   assert(m->is_method(), "must be");
4417   return m;
4418 }
4419 
4420 // Used by redefinition to change Method* to new Method* with same hash (name, signature)
4421 void java_lang_invoke_ResolvedMethodName::set_vmtarget(oop resolved_method, Method* m) {
4422   assert(is_instance(resolved_method), "wrong type");
4423   resolved_method->address_field_put(_vmtarget_offset, (address)m);
4424 }
4425 
4426 void java_lang_invoke_ResolvedMethodName::set_vmholder(oop resolved_method, oop holder) {
4427   assert(is_instance(resolved_method), "wrong type");
4428   resolved_method->obj_field_put(_vmholder_offset, holder);
4429 }
4430 
4431 oop java_lang_invoke_ResolvedMethodName::find_resolved_method(const methodHandle& m, TRAPS) {
4432   const Method* method = m();
4433 
4434   // lookup ResolvedMethod oop in the table, or create a new one and intern it
4435   oop resolved_method = ResolvedMethodTable::find_method(method);
4436   if (resolved_method != NULL) {
4437     return resolved_method;
4438   }
4439 
4440   InstanceKlass* k = vmClasses::ResolvedMethodName_klass();
4441   if (!k->is_initialized()) {
4442     k->initialize(CHECK_NULL);
4443   }
4444 
4445   oop new_resolved_method = k->allocate_instance(CHECK_NULL);
4446 
4447   NoSafepointVerifier nsv;
4448 
4449   if (method->is_old()) {
4450     method = (method->is_deleted()) ? Universe::throw_no_such_method_error() :
4451                                       method->get_new_method();
4452   }
4453 
4454   InstanceKlass* holder = method->method_holder();
4455 
4456   set_vmtarget(new_resolved_method, const_cast<Method*>(method));
4457   // Add a reference to the loader (actually mirror because hidden classes may not have
4458   // distinct loaders) to ensure the metadata is kept alive.
4459   // This mirror may be different than the one in clazz field.
4460   set_vmholder(new_resolved_method, holder->java_mirror());
4461 
4462   // Set flag in class to indicate this InstanceKlass has entries in the table
4463   // to avoid walking table during redefinition if none of the redefined classes
4464   // have any membernames in the table.
4465   holder->set_has_resolved_methods();
4466 
4467   return ResolvedMethodTable::add_method(method, Handle(THREAD, new_resolved_method));
4468 }
4469 
4470 oop java_lang_invoke_LambdaForm::vmentry(oop lform) {
4471   assert(is_instance(lform), "wrong type");
4472   return lform->obj_field(_vmentry_offset);
4473 }
4474 
4475 
4476 // Support for java_lang_invoke_MethodType
4477 
4478 int java_lang_invoke_MethodType::_rtype_offset;
4479 int java_lang_invoke_MethodType::_ptypes_offset;
4480 
4481 #define METHODTYPE_FIELDS_DO(macro) \
4482   macro(_rtype_offset,  k, "rtype",  class_signature,       false); \
4483   macro(_ptypes_offset, k, "ptypes", class_array_signature, false)
4484 
4485 void java_lang_invoke_MethodType::compute_offsets() {
4486   InstanceKlass* k = vmClasses::MethodType_klass();
4487   METHODTYPE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4488 }
4489 
4490 #if INCLUDE_CDS
4491 void java_lang_invoke_MethodType::serialize_offsets(SerializeClosure* f) {
4492   METHODTYPE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4493 }
4494 #endif
4495 
4496 void java_lang_invoke_MethodType::print_signature(oop mt, outputStream* st) {
4497   st->print("(");
4498   objArrayOop pts = ptypes(mt);
4499   for (int i = 0, limit = pts->length(); i < limit; i++) {
4500     java_lang_Class::print_signature(pts->obj_at(i), st);
4501   }
4502   st->print(")");
4503   java_lang_Class::print_signature(rtype(mt), st);
4504 }
4505 
4506 Symbol* java_lang_invoke_MethodType::as_signature(oop mt, bool intern_if_not_found) {
4507   ResourceMark rm;
4508   stringStream buffer(128);
4509   print_signature(mt, &buffer);
4510   const char* sigstr =       buffer.base();
4511   int         siglen = (int) buffer.size();
4512   Symbol *name;
4513   if (!intern_if_not_found) {
4514     name = SymbolTable::probe(sigstr, siglen);
4515   } else {
4516     name = SymbolTable::new_symbol(sigstr, siglen);
4517   }
4518   return name;
4519 }
4520 
4521 bool java_lang_invoke_MethodType::equals(oop mt1, oop mt2) {
4522   if (mt1 == mt2)
4523     return true;
4524   if (rtype(mt1) != rtype(mt2))
4525     return false;
4526   if (ptype_count(mt1) != ptype_count(mt2))
4527     return false;
4528   for (int i = ptype_count(mt1) - 1; i >= 0; i--) {
4529     if (ptype(mt1, i) != ptype(mt2, i))
4530       return false;
4531   }
4532   return true;
4533 }
4534 
4535 oop java_lang_invoke_MethodType::rtype(oop mt) {
4536   assert(is_instance(mt), "must be a MethodType");
4537   return mt->obj_field(_rtype_offset);
4538 }
4539 
4540 objArrayOop java_lang_invoke_MethodType::ptypes(oop mt) {
4541   assert(is_instance(mt), "must be a MethodType");
4542   return (objArrayOop) mt->obj_field(_ptypes_offset);
4543 }
4544 
4545 oop java_lang_invoke_MethodType::ptype(oop mt, int idx) {
4546   return ptypes(mt)->obj_at(idx);
4547 }
4548 
4549 int java_lang_invoke_MethodType::ptype_count(oop mt) {
4550   return ptypes(mt)->length();
4551 }
4552 
4553 int java_lang_invoke_MethodType::ptype_slot_count(oop mt) {
4554   objArrayOop pts = ptypes(mt);
4555   int count = pts->length();
4556   int slots = 0;
4557   for (int i = 0; i < count; i++) {
4558     BasicType bt = java_lang_Class::as_BasicType(pts->obj_at(i));
4559     slots += type2size[bt];
4560   }
4561   return slots;
4562 }
4563 
4564 int java_lang_invoke_MethodType::rtype_slot_count(oop mt) {
4565   BasicType bt = java_lang_Class::as_BasicType(rtype(mt));
4566   return type2size[bt];
4567 }
4568 
4569 
4570 // Support for java_lang_invoke_CallSite
4571 
4572 int java_lang_invoke_CallSite::_target_offset;
4573 int java_lang_invoke_CallSite::_context_offset;
4574 
4575 #define CALLSITE_FIELDS_DO(macro) \
4576   macro(_target_offset,  k, "target", java_lang_invoke_MethodHandle_signature, false); \
4577   macro(_context_offset, k, "context", java_lang_invoke_MethodHandleNatives_CallSiteContext_signature, false)
4578 
4579 void java_lang_invoke_CallSite::compute_offsets() {
4580   InstanceKlass* k = vmClasses::CallSite_klass();
4581   CALLSITE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4582 }
4583 
4584 #if INCLUDE_CDS
4585 void java_lang_invoke_CallSite::serialize_offsets(SerializeClosure* f) {
4586   CALLSITE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4587 }
4588 #endif
4589 
4590 oop java_lang_invoke_CallSite::context_no_keepalive(oop call_site) {
4591   assert(java_lang_invoke_CallSite::is_instance(call_site), "");
4592 
4593   oop dep_oop = call_site->obj_field_access<AS_NO_KEEPALIVE>(_context_offset);
4594   return dep_oop;
4595 }
4596 
4597 // Support for java_lang_invoke_ConstantCallSite
4598 
4599 int java_lang_invoke_ConstantCallSite::_is_frozen_offset;
4600 
4601 #define CONSTANTCALLSITE_FIELDS_DO(macro) \
4602   macro(_is_frozen_offset, k, "isFrozen", bool_signature, false)
4603 
4604 void java_lang_invoke_ConstantCallSite::compute_offsets() {
4605   InstanceKlass* k = vmClasses::ConstantCallSite_klass();
4606   CONSTANTCALLSITE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4607 }
4608 
4609 #if INCLUDE_CDS
4610 void java_lang_invoke_ConstantCallSite::serialize_offsets(SerializeClosure* f) {
4611   CONSTANTCALLSITE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4612 }
4613 #endif
4614 
4615 // Support for java_lang_invoke_MethodHandleNatives_CallSiteContext
4616 
4617 int java_lang_invoke_MethodHandleNatives_CallSiteContext::_vmdependencies_offset;
4618 int java_lang_invoke_MethodHandleNatives_CallSiteContext::_last_cleanup_offset;
4619 
4620 void java_lang_invoke_MethodHandleNatives_CallSiteContext::compute_offsets() {
4621   InstanceKlass* k = vmClasses::Context_klass();
4622   CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
4623 }
4624 
4625 #if INCLUDE_CDS
4626 void java_lang_invoke_MethodHandleNatives_CallSiteContext::serialize_offsets(SerializeClosure* f) {
4627   CALLSITECONTEXT_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
4628 }
4629 #endif
4630 
4631 DependencyContext java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(oop call_site) {
4632   assert(java_lang_invoke_MethodHandleNatives_CallSiteContext::is_instance(call_site), "");
4633   nmethodBucket* volatile* vmdeps_addr = call_site->field_addr<nmethodBucket* volatile>(_vmdependencies_offset);
4634   volatile uint64_t* last_cleanup_addr = call_site->field_addr<volatile uint64_t>(_last_cleanup_offset);
4635   DependencyContext dep_ctx(vmdeps_addr, last_cleanup_addr);
4636   return dep_ctx;
4637 }
4638 
4639 // Support for java_security_AccessControlContext
4640 
4641 int java_security_AccessControlContext::_context_offset;
4642 int java_security_AccessControlContext::_privilegedContext_offset;
4643 int java_security_AccessControlContext::_isPrivileged_offset;
4644 int java_security_AccessControlContext::_isAuthorized_offset;
4645 
4646 #define ACCESSCONTROLCONTEXT_FIELDS_DO(macro) \
4647   macro(_context_offset,           k, "context",      protectiondomain_signature, false); \
4648   macro(_privilegedContext_offset, k, "privilegedContext", accesscontrolcontext_signature, false); \
4649   macro(_isPrivileged_offset,      k, "isPrivileged", bool_signature, false); \
4650   macro(_isAuthorized_offset,      k, "isAuthorized", bool_signature, false)
4651 
4652 void java_security_AccessControlContext::compute_offsets() {
4653   assert(_isPrivileged_offset == 0, "offsets should be initialized only once");
4654   InstanceKlass* k = vmClasses::AccessControlContext_klass();
4655   ACCESSCONTROLCONTEXT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4656 }
4657 
4658 #if INCLUDE_CDS
4659 void java_security_AccessControlContext::serialize_offsets(SerializeClosure* f) {
4660   ACCESSCONTROLCONTEXT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4661 }
4662 #endif
4663 
4664 oop java_security_AccessControlContext::create(objArrayHandle context, bool isPrivileged, Handle privileged_context, TRAPS) {
4665   assert(_isPrivileged_offset != 0, "offsets should have been initialized");
4666   assert(_isAuthorized_offset != 0, "offsets should have been initialized");
4667   // Ensure klass is initialized
4668   vmClasses::AccessControlContext_klass()->initialize(CHECK_NULL);
4669   // Allocate result
4670   oop result = vmClasses::AccessControlContext_klass()->allocate_instance(CHECK_NULL);
4671   // Fill in values
4672   result->obj_field_put(_context_offset, context());
4673   result->obj_field_put(_privilegedContext_offset, privileged_context());
4674   result->bool_field_put(_isPrivileged_offset, isPrivileged);
4675   result->bool_field_put(_isAuthorized_offset, true);
4676   return result;
4677 }
4678 
4679 
4680 // Support for java_lang_ClassLoader
4681 
4682 int  java_lang_ClassLoader::_loader_data_offset;
4683 int  java_lang_ClassLoader::_parallelCapable_offset;
4684 int  java_lang_ClassLoader::_name_offset;
4685 int  java_lang_ClassLoader::_nameAndId_offset;
4686 int  java_lang_ClassLoader::_unnamedModule_offset;
4687 int  java_lang_ClassLoader::_parent_offset;
4688 
4689 ClassLoaderData* java_lang_ClassLoader::loader_data_acquire(oop loader) {
4690   assert(loader != NULL, "loader must not be NULL");
4691   assert(oopDesc::is_oop(loader), "loader must be oop");
4692   return Atomic::load_acquire(loader->field_addr<ClassLoaderData*>(_loader_data_offset));
4693 }
4694 
4695 ClassLoaderData* java_lang_ClassLoader::loader_data(oop loader) {
4696   assert(loader != NULL, "loader must not be NULL");
4697   assert(oopDesc::is_oop(loader), "loader must be oop");
4698   return *loader->field_addr<ClassLoaderData*>(_loader_data_offset);
4699 }
4700 
4701 void java_lang_ClassLoader::release_set_loader_data(oop loader, ClassLoaderData* new_data) {
4702   assert(loader != NULL, "loader must not be NULL");
4703   assert(oopDesc::is_oop(loader), "loader must be oop");
4704   Atomic::release_store(loader->field_addr<ClassLoaderData*>(_loader_data_offset), new_data);
4705 }
4706 
4707 #define CLASSLOADER_FIELDS_DO(macro) \
4708   macro(_parallelCapable_offset, k1, "parallelLockMap",      concurrenthashmap_signature, false); \
4709   macro(_name_offset,            k1, vmSymbols::name_name(), string_signature, false); \
4710   macro(_nameAndId_offset,       k1, "nameAndId",            string_signature, false); \
4711   macro(_unnamedModule_offset,   k1, "unnamedModule",        module_signature, false); \
4712   macro(_parent_offset,          k1, "parent",               classloader_signature, false)
4713 
4714 void java_lang_ClassLoader::compute_offsets() {
4715   InstanceKlass* k1 = vmClasses::ClassLoader_klass();
4716   CLASSLOADER_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4717 
4718   CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
4719 }
4720 
4721 #if INCLUDE_CDS
4722 void java_lang_ClassLoader::serialize_offsets(SerializeClosure* f) {
4723   CLASSLOADER_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4724   CLASSLOADER_INJECTED_FIELDS(INJECTED_FIELD_SERIALIZE_OFFSET);
4725 }
4726 #endif
4727 
4728 oop java_lang_ClassLoader::parent(oop loader) {
4729   assert(is_instance(loader), "loader must be oop");
4730   return loader->obj_field(_parent_offset);
4731 }
4732 
4733 // Returns the name field of this class loader.  If the name field has not
4734 // been set, null will be returned.
4735 oop java_lang_ClassLoader::name(oop loader) {
4736   assert(is_instance(loader), "loader must be oop");
4737   return loader->obj_field(_name_offset);
4738 }
4739 
4740 // Returns the nameAndId field of this class loader. The format is
4741 // as follows:
4742 //   If the defining loader has a name explicitly set then '<loader-name>' @<id>
4743 //   If the defining loader has no name then <qualified-class-name> @<id>
4744 //   If built-in loader, then omit '@<id>' as there is only one instance.
4745 // Use ClassLoader::loader_name_id() to obtain this String as a char*.
4746 oop java_lang_ClassLoader::nameAndId(oop loader) {
4747   assert(is_instance(loader), "loader must be oop");
4748   return loader->obj_field(_nameAndId_offset);
4749 }
4750 
4751 bool java_lang_ClassLoader::isAncestor(oop loader, oop cl) {
4752   assert(is_instance(loader), "loader must be oop");
4753   assert(cl == NULL || is_instance(cl), "cl argument must be oop");
4754   oop acl = loader;
4755   debug_only(jint loop_count = 0);
4756   // This loop taken verbatim from ClassLoader.java:
4757   do {
4758     acl = parent(acl);
4759     if (cl == acl) {
4760       return true;
4761     }
4762     assert(++loop_count > 0, "loop_count overflow");
4763   } while (acl != NULL);
4764   return false;
4765 }
4766 
4767 bool java_lang_ClassLoader::is_instance(oop obj) {
4768   return obj != NULL && is_subclass(obj->klass());
4769 }
4770 
4771 
4772 // For class loader classes, parallelCapable defined
4773 // based on non-null field
4774 // Written to by java.lang.ClassLoader, vm only reads this field, doesn't set it
4775 bool java_lang_ClassLoader::parallelCapable(oop class_loader) {
4776   assert(_parallelCapable_offset != 0, "offsets should have been initialized");
4777   return (class_loader->obj_field(_parallelCapable_offset) != NULL);
4778 }
4779 
4780 bool java_lang_ClassLoader::is_trusted_loader(oop loader) {
4781   // Fix for 4474172; see evaluation for more details
4782   loader = non_reflection_class_loader(loader);
4783 
4784   oop cl = SystemDictionary::java_system_loader();
4785   while(cl != NULL) {
4786     if (cl == loader) return true;
4787     cl = parent(cl);
4788   }
4789   return false;
4790 }
4791 
4792 // Return true if this is one of the class loaders associated with
4793 // the generated bytecodes for reflection.
4794 bool java_lang_ClassLoader::is_reflection_class_loader(oop loader) {
4795   if (loader != NULL) {
4796     Klass* delegating_cl_class = vmClasses::reflect_DelegatingClassLoader_klass();
4797     // This might be null in non-1.4 JDKs
4798     return (delegating_cl_class != NULL && loader->is_a(delegating_cl_class));
4799   }
4800   return false;
4801 }
4802 
4803 oop java_lang_ClassLoader::non_reflection_class_loader(oop loader) {
4804   // See whether this is one of the class loaders associated with
4805   // the generated bytecodes for reflection, and if so, "magically"
4806   // delegate to its parent to prevent class loading from occurring
4807   // in places where applications using reflection didn't expect it.
4808   if (is_reflection_class_loader(loader)) {
4809     return parent(loader);
4810   }
4811   return loader;
4812 }
4813 
4814 oop java_lang_ClassLoader::unnamedModule(oop loader) {
4815   assert(is_instance(loader), "loader must be oop");
4816   return loader->obj_field(_unnamedModule_offset);
4817 }
4818 
4819 // Support for java_lang_System
4820 //
4821 
4822 int java_lang_System::_static_in_offset;
4823 int java_lang_System::_static_out_offset;
4824 int java_lang_System::_static_err_offset;
4825 int java_lang_System::_static_security_offset;
4826 int java_lang_System::_static_allow_security_offset;
4827 int java_lang_System::_static_never_offset;
4828 
4829 #define SYSTEM_FIELDS_DO(macro) \
4830   macro(_static_in_offset,  k, "in",  input_stream_signature, true); \
4831   macro(_static_out_offset, k, "out", print_stream_signature, true); \
4832   macro(_static_err_offset, k, "err", print_stream_signature, true); \
4833   macro(_static_security_offset, k, "security", security_manager_signature, true); \
4834   macro(_static_allow_security_offset, k, "allowSecurityManager", int_signature, true); \
4835   macro(_static_never_offset, k, "NEVER", int_signature, true)
4836 
4837 void java_lang_System::compute_offsets() {
4838   InstanceKlass* k = vmClasses::System_klass();
4839   SYSTEM_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4840 }
4841 
4842 // This field tells us that a security manager can never be installed so we
4843 // can completely skip populating the ProtectionDomainCacheTable.
4844 bool java_lang_System::allow_security_manager() {
4845   static int initialized = false;
4846   static bool allowed = true; // default
4847   if (!initialized) {
4848     oop base = vmClasses::System_klass()->static_field_base_raw();
4849     int never = base->int_field(_static_never_offset);
4850     allowed = (base->int_field(_static_allow_security_offset) != never);
4851     initialized = true;
4852   }
4853   return allowed;
4854 }
4855 
4856 // This field tells us that a security manager is installed.
4857 bool java_lang_System::has_security_manager() {
4858   oop base = vmClasses::System_klass()->static_field_base_raw();
4859   return base->obj_field(_static_security_offset) != NULL;
4860 }
4861 
4862 #if INCLUDE_CDS
4863 void java_lang_System::serialize_offsets(SerializeClosure* f) {
4864    SYSTEM_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4865 }
4866 #endif
4867 
4868 // Support for jdk_internal_misc_UnsafeConstants
4869 //
4870 class UnsafeConstantsFixup : public FieldClosure {
4871 private:
4872   int _address_size;
4873   int _page_size;
4874   bool _big_endian;
4875   bool _use_unaligned_access;
4876   int _data_cache_line_flush_size;
4877 public:
4878   UnsafeConstantsFixup() {
4879     // round up values for all static final fields
4880     _address_size = sizeof(void*);
4881     _page_size = os::vm_page_size();
4882     _big_endian = LITTLE_ENDIAN_ONLY(false) BIG_ENDIAN_ONLY(true);
4883     _use_unaligned_access = UseUnalignedAccesses;
4884     _data_cache_line_flush_size = (int)VM_Version::data_cache_line_flush_size();
4885   }
4886 
4887   void do_field(fieldDescriptor* fd) {
4888     oop mirror = fd->field_holder()->java_mirror();
4889     assert(mirror != NULL, "UnsafeConstants must have mirror already");
4890     assert(fd->field_holder() == vmClasses::UnsafeConstants_klass(), "Should be UnsafeConstants");
4891     assert(fd->is_final(), "fields of UnsafeConstants must be final");
4892     assert(fd->is_static(), "fields of UnsafeConstants must be static");
4893     if (fd->name() == vmSymbols::address_size_name()) {
4894       mirror->int_field_put(fd->offset(), _address_size);
4895     } else if (fd->name() == vmSymbols::page_size_name()) {
4896       mirror->int_field_put(fd->offset(), _page_size);
4897     } else if (fd->name() == vmSymbols::big_endian_name()) {
4898       mirror->bool_field_put(fd->offset(), _big_endian);
4899     } else if (fd->name() == vmSymbols::use_unaligned_access_name()) {
4900       mirror->bool_field_put(fd->offset(), _use_unaligned_access);
4901     } else if (fd->name() == vmSymbols::data_cache_line_flush_size_name()) {
4902       mirror->int_field_put(fd->offset(), _data_cache_line_flush_size);
4903     } else if (fd->name() == vmSymbols::scoped_cache_shift_name()) {
4904       mirror->int_field_put(fd->offset(), ScopeLocalCacheSize ? exact_log2(ScopeLocalCacheSize) : -1);
4905     } else {
4906       assert(false, "unexpected UnsafeConstants field");
4907     }
4908   }
4909 };
4910 
4911 void jdk_internal_misc_UnsafeConstants::set_unsafe_constants() {
4912   UnsafeConstantsFixup fixup;
4913   vmClasses::UnsafeConstants_klass()->do_local_static_fields(&fixup);
4914 }
4915 
4916 
4917 // java_lang_StackTraceElement
4918 
4919 int java_lang_StackTraceElement::_methodName_offset;
4920 int java_lang_StackTraceElement::_fileName_offset;
4921 int java_lang_StackTraceElement::_lineNumber_offset;
4922 int java_lang_StackTraceElement::_moduleName_offset;
4923 int java_lang_StackTraceElement::_moduleVersion_offset;
4924 int java_lang_StackTraceElement::_classLoaderName_offset;
4925 int java_lang_StackTraceElement::_declaringClass_offset;
4926 int java_lang_StackTraceElement::_declaringClassObject_offset;
4927 int java_lang_StackTraceElement::_contScopeName_offset;
4928 
4929 #define STACKTRACEELEMENT_FIELDS_DO(macro) \
4930   macro(_declaringClassObject_offset,  k, "declaringClassObject", class_signature, false); \
4931   macro(_classLoaderName_offset, k, "classLoaderName", string_signature, false); \
4932   macro(_moduleName_offset,      k, "moduleName",      string_signature, false); \
4933   macro(_moduleVersion_offset,   k, "moduleVersion",   string_signature, false); \
4934   macro(_declaringClass_offset,  k, "declaringClass",  string_signature, false); \
4935   macro(_methodName_offset,      k, "methodName",      string_signature, false); \
4936   macro(_fileName_offset,        k, "fileName",        string_signature, false); \
4937   macro(_lineNumber_offset,      k, "lineNumber",      int_signature,    false); \
4938   macro(_contScopeName_offset,   k, "contScopeName",   string_signature, false)
4939 
4940 // Support for java_lang_StackTraceElement
4941 void java_lang_StackTraceElement::compute_offsets() {
4942   InstanceKlass* k = vmClasses::StackTraceElement_klass();
4943   STACKTRACEELEMENT_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4944 }
4945 
4946 #if INCLUDE_CDS
4947 void java_lang_StackTraceElement::serialize_offsets(SerializeClosure* f) {
4948   STACKTRACEELEMENT_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4949 }
4950 #endif
4951 
4952 void java_lang_StackTraceElement::set_fileName(oop element, oop value) {
4953   element->obj_field_put(_fileName_offset, value);
4954 }
4955 
4956 void java_lang_StackTraceElement::set_declaringClass(oop element, oop value) {
4957   element->obj_field_put(_declaringClass_offset, value);
4958 }
4959 
4960 void java_lang_StackTraceElement::set_methodName(oop element, oop value) {
4961   element->obj_field_put(_methodName_offset, value);
4962 }
4963 
4964 void java_lang_StackTraceElement::set_lineNumber(oop element, int value) {
4965   element->int_field_put(_lineNumber_offset, value);
4966 }
4967 
4968 void java_lang_StackTraceElement::set_moduleName(oop element, oop value) {
4969   element->obj_field_put(_moduleName_offset, value);
4970 }
4971 
4972 void java_lang_StackTraceElement::set_moduleVersion(oop element, oop value) {
4973   element->obj_field_put(_moduleVersion_offset, value);
4974 }
4975 
4976 void java_lang_StackTraceElement::set_classLoaderName(oop element, oop value) {
4977   element->obj_field_put(_classLoaderName_offset, value);
4978 }
4979 
4980 void java_lang_StackTraceElement::set_declaringClassObject(oop element, oop value) {
4981   element->obj_field_put(_declaringClassObject_offset, value);
4982 }
4983 
4984 
4985 // java_lang_AssertionStatusDirectives
4986 
4987 int java_lang_AssertionStatusDirectives::_classes_offset;
4988 int java_lang_AssertionStatusDirectives::_classEnabled_offset;
4989 int java_lang_AssertionStatusDirectives::_packages_offset;
4990 int java_lang_AssertionStatusDirectives::_packageEnabled_offset;
4991 int java_lang_AssertionStatusDirectives::_deflt_offset;
4992 
4993 // Support for java Assertions - java_lang_AssertionStatusDirectives.
4994 #define ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(macro) \
4995   macro(_classes_offset,        k, "classes",        string_array_signature, false); \
4996   macro(_classEnabled_offset,   k, "classEnabled",   bool_array_signature, false); \
4997   macro(_packages_offset,       k, "packages",       string_array_signature, false); \
4998   macro(_packageEnabled_offset, k, "packageEnabled", bool_array_signature,   false); \
4999   macro(_deflt_offset,          k, "deflt",          bool_signature,         false)
5000 
5001 void java_lang_AssertionStatusDirectives::compute_offsets() {
5002   InstanceKlass* k = vmClasses::AssertionStatusDirectives_klass();
5003   ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5004 }
5005 
5006 #if INCLUDE_CDS
5007 void java_lang_AssertionStatusDirectives::serialize_offsets(SerializeClosure* f) {
5008   ASSERTIONSTATUSDIRECTIVES_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5009 }
5010 #endif
5011 
5012 void java_lang_AssertionStatusDirectives::set_classes(oop o, oop val) {
5013   o->obj_field_put(_classes_offset, val);
5014 }
5015 
5016 void java_lang_AssertionStatusDirectives::set_classEnabled(oop o, oop val) {
5017   o->obj_field_put(_classEnabled_offset, val);
5018 }
5019 
5020 void java_lang_AssertionStatusDirectives::set_packages(oop o, oop val) {
5021   o->obj_field_put(_packages_offset, val);
5022 }
5023 
5024 void java_lang_AssertionStatusDirectives::set_packageEnabled(oop o, oop val) {
5025   o->obj_field_put(_packageEnabled_offset, val);
5026 }
5027 
5028 void java_lang_AssertionStatusDirectives::set_deflt(oop o, bool val) {
5029   o->bool_field_put(_deflt_offset, val);
5030 }
5031 
5032 // Support for jdk.internal.vm.Continuation
5033 
5034 int jdk_internal_vm_ContinuationScope::_name_offset;
5035 int jdk_internal_vm_Continuation::_scope_offset;
5036 int jdk_internal_vm_Continuation::_target_offset;
5037 int jdk_internal_vm_Continuation::_tail_offset;
5038 int jdk_internal_vm_Continuation::_parent_offset;
5039 int jdk_internal_vm_Continuation::_yieldInfo_offset;
5040 int jdk_internal_vm_Continuation::_cs_offset;
5041 int jdk_internal_vm_Continuation::_reset_offset;
5042 int jdk_internal_vm_Continuation::_mounted_offset;
5043 int jdk_internal_vm_Continuation::_done_offset;
5044 int jdk_internal_vm_Continuation::_preempted_offset;
5045 
5046 #define CONTINUATIONSCOPE_FIELDS_DO(macro) \
5047   macro(_name_offset, k, vmSymbols::name_name(), string_signature, false);
5048 
5049 void jdk_internal_vm_ContinuationScope::compute_offsets() {
5050   InstanceKlass* k = vmClasses::ContinuationScope_klass();
5051   CONTINUATIONSCOPE_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5052 }
5053 
5054 #if INCLUDE_CDS
5055 void jdk_internal_vm_ContinuationScope::serialize_offsets(SerializeClosure* f) {
5056   CONTINUATIONSCOPE_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5057 }
5058 #endif
5059 
5060 // Support for jdk.internal.vm.Continuation
5061 
5062 #define CONTINUATION_FIELDS_DO(macro) \
5063   macro(_scope_offset,     k, vmSymbols::scope_name(),     continuationscope_signature, false); \
5064   macro(_target_offset,    k, vmSymbols::target_name(),    runnable_signature,          false); \
5065   macro(_parent_offset,    k, vmSymbols::parent_name(),    continuation_signature,      false); \
5066   macro(_yieldInfo_offset, k, vmSymbols::yieldInfo_name(), object_signature,            false); \
5067   macro(_tail_offset,      k, vmSymbols::tail_name(),      stackchunk_signature,        false); \
5068   macro(_cs_offset,        k, vmSymbols::cs_name(),        short_signature,             false); \
5069   macro(_reset_offset,     k, vmSymbols::reset_name(),     bool_signature,              false); \
5070   macro(_mounted_offset,   k, vmSymbols::mounted_name(),   bool_signature,              false); \
5071   macro(_done_offset,      k, vmSymbols::done_name(),      bool_signature,              false); \
5072   macro(_preempted_offset, k, "preempted",                 bool_signature,              false);
5073 
5074 void jdk_internal_vm_Continuation::compute_offsets() {
5075   InstanceKlass* k = vmClasses::Continuation_klass();
5076   CONTINUATION_FIELDS_DO(FIELD_COMPUTE_OFFSET);
5077 }
5078 
5079 #if INCLUDE_CDS
5080 void jdk_internal_vm_Continuation::serialize_offsets(SerializeClosure* f) {
5081   CONTINUATION_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
5082 }
5083 #endif
5084 
5085 // Support for jdk.internal.vm.StackChunk
5086 
5087 int jdk_internal_vm_StackChunk::_parent_offset;
5088 int jdk_internal_vm_StackChunk::_size_offset;
5089 int jdk_internal_vm_StackChunk::_sp_offset;
5090 int jdk_internal_vm_StackChunk::_pc_offset;
5091 int jdk_internal_vm_StackChunk::_argsize_offset;
5092 int jdk_internal_vm_StackChunk::_flags_offset;
5093 int jdk_internal_vm_StackChunk::_gcSP_offset;
5094 int jdk_internal_vm_StackChunk::_markCycle_offset;
5095 int jdk_internal_vm_StackChunk::_maxSize_offset;
5096 int jdk_internal_vm_StackChunk::_numFrames_offset;
5097 int jdk_internal_vm_StackChunk::_numOops_offset;
5098 int jdk_internal_vm_StackChunk::_cont_offset;
5099 
5100 #define STACKCHUNK_FIELDS_DO(macro) \
5101   macro(_parent_offset,    k, vmSymbols::parent_name(),    stackchunk_signature, false); \
5102   macro(_size_offset,      k, vmSymbols::size_name(),      int_signature,        false); \
5103   macro(_sp_offset,        k, vmSymbols::sp_name(),        int_signature,        false); \
5104   macro(_pc_offset,        k, vmSymbols::pc_name(),        long_signature,       false); \
5105   macro(_argsize_offset,   k, vmSymbols::argsize_name(),   int_signature,        false); \
5106   macro(_flags_offset,     k, "flags",                     byte_signature,       false); \
5107   macro(_gcSP_offset,      k, "gcSP",                      int_signature,        false); \
5108   macro(_markCycle_offset, k, "markCycle",                 long_signature,       false); \
5109   macro(_maxSize_offset,   k, vmSymbols::maxSize_name(),   int_signature,        false); \
5110   macro(_numFrames_offset, k, vmSymbols::numFrames_name(), int_signature,        false); \
5111   macro(_numOops_offset,   k, vmSymbols::numOops_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 }