1 /*
   2  * Copyright (c) 2011, 2024, 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 #include "precompiled.hpp"
  25 #include "classfile/javaClasses.inline.hpp"
  26 #include "code/compiledIC.hpp"
  27 #include "compiler/compileBroker.hpp"
  28 #include "compiler/compilerThread.hpp"
  29 #include "compiler/oopMap.hpp"
  30 #include "gc/shared/barrierSetNMethod.hpp"
  31 #include "jvmci/jvmciCodeInstaller.hpp"
  32 #include "jvmci/jvmciCompilerToVM.hpp"
  33 #include "jvmci/jvmciRuntime.hpp"
  34 #include "memory/universe.hpp"
  35 #include "oops/compressedKlass.inline.hpp"
  36 #include "oops/klass.inline.hpp"
  37 #include "prims/jvmtiExport.hpp"
  38 #include "prims/methodHandles.hpp"
  39 #include "runtime/arguments.hpp"
  40 #include "runtime/interfaceSupport.inline.hpp"
  41 #include "runtime/jniHandles.inline.hpp"
  42 #include "runtime/os.hpp"
  43 #include "runtime/sharedRuntime.hpp"
  44 #include "utilities/align.hpp"
  45 
  46 // frequently used constants
  47 // Allocate them with new so they are never destroyed (otherwise, a
  48 // forced exit could destroy these objects while they are still in
  49 // use).
  50 ConstantOopWriteValue* CodeInstaller::_oop_null_scope_value = new (mtJVMCI) ConstantOopWriteValue(nullptr);
  51 ConstantIntValue*      CodeInstaller::_int_m1_scope_value = new (mtJVMCI) ConstantIntValue(-1);
  52 ConstantIntValue*      CodeInstaller::_int_0_scope_value =  new (mtJVMCI) ConstantIntValue((jint)0);
  53 ConstantIntValue*      CodeInstaller::_int_1_scope_value =  new (mtJVMCI) ConstantIntValue(1);
  54 ConstantIntValue*      CodeInstaller::_int_2_scope_value =  new (mtJVMCI) ConstantIntValue(2);
  55 LocationValue*         CodeInstaller::_illegal_value = new (mtJVMCI) LocationValue(Location());
  56 MarkerValue*           CodeInstaller::_virtual_byte_array_marker = new (mtJVMCI) MarkerValue();
  57 
  58 static bool is_set(u1 flags, u1 bit) {
  59   return flags & bit;
  60 }
  61 
  62 oop HotSpotCompiledCodeStream::get_oop(int id, JVMCI_TRAPS) const {
  63   if (_object_pool.is_null()) {
  64     JVMCI_ERROR_NULL("object pool is null%s", context());
  65   }
  66   if (!_object_pool.is_null() && 0 <= id && id < _object_pool->length()) {
  67     return _object_pool->obj_at(id);
  68   }
  69   JVMCI_ERROR_NULL("unknown direct object id %d%s", id, context());
  70 }
  71 
  72 u4 HotSpotCompiledCodeStream::offset() const {
  73   u4 res = 0;
  74   for (Chunk* c = _head; c != nullptr; c = c->next()) {
  75     if (c == _chunk) {
  76       res += _pos - c->data();
  77       break;
  78     } else {
  79       res += c->size();
  80     }
  81   }
  82   return res;
  83 }
  84 
  85 bool HotSpotCompiledCodeStream::available() const {
  86   u4 rem = _chunk->data_end() - _pos;
  87   for (Chunk* c = _chunk->next(); c != nullptr; c = c->next()) {
  88     rem += c->size();
  89   }
  90   return rem;
  91 }
  92 
  93 void HotSpotCompiledCodeStream::dump_buffer(outputStream* st) const {
  94   st->print_cr("HotSpotCompiledCode stream for %s:", code_desc());
  95   int chunk_index = 0;
  96   for (Chunk* c = _head; c != nullptr; c = c->next()) {
  97     const u1* data     = c->data();
  98     const u1* data_end = c->data_end();
  99 
 100     int to_dump = data_end - data;
 101     st->print_cr("# chunk %d, %d bytes", chunk_index, to_dump);
 102     st->print_data((void*) data, to_dump, true, false);
 103     chunk_index++;
 104   }
 105 }
 106 
 107 void HotSpotCompiledCodeStream::dump_buffer_tail(int len, outputStream* st) const {
 108   const u1* start;
 109   int avail = _pos - _chunk->data();
 110   if (len >= avail) {
 111     len = avail;
 112     start = _chunk->data();
 113   } else {
 114     start = _pos - len;
 115 
 116     // Ensure start is 16-byte aligned wrt chunk start
 117     int start_offset = start - _chunk->data();
 118     start -= (start_offset % 16);
 119     len = _pos - start;
 120   }
 121 
 122   st->print_cr("Last %d bytes up to current read position " INTPTR_FORMAT " in HotSpotCompiledCode stream for %s:", len, p2i(_pos), code_desc());
 123   st->print_data((void*) start, len, true, false);
 124 }
 125 
 126 const char* HotSpotCompiledCodeStream::context() const {
 127   stringStream st;
 128   st.cr();
 129   st.print_cr("at " INTPTR_FORMAT " in HotSpotCompiledCode stream", p2i(_pos));
 130   dump_buffer_tail(100, &st);
 131   return st.as_string();
 132 }
 133 
 134 void HotSpotCompiledCodeStream::before_read(u1 size) {
 135   if (_pos + size > _chunk->data_end()) {
 136     Chunk* next = _chunk->next();
 137     if (next == nullptr || size > next->size()) {
 138       dump_buffer();
 139       fatal("%s: reading %d bytes overflows buffer at " INTPTR_FORMAT, code_desc(), size, p2i(_pos));
 140     }
 141     _chunk = next;
 142     _pos = _chunk->data();
 143   }
 144 }
 145 
 146 // Reads a size followed by an ascii string from the stream and
 147 // checks that they match `expect_size` and `expect_name` respectively. This
 148 // implements a rudimentary type checking of the stream between the stream producer
 149 // (Java) and the consumer (C++).
 150 void HotSpotCompiledCodeStream::check_data(u2 expect_size, const char* expect_name) {
 151   u2 actual_size = get_u1();
 152   u2 ascii_len = get_u1();
 153   const char* actual_name = (const char*) _pos;
 154   char* end = (char*) _pos + ascii_len;
 155   _pos = (const u1*) end;
 156   if (strlen(expect_name) != ascii_len || strncmp(expect_name, actual_name, ascii_len) != 0) {
 157     dump_buffer();
 158     fatal("%s: expected \"%s\" at " INTPTR_FORMAT ", got \"%.*s\" (len: %d)",
 159         code_desc(), expect_name, p2i(actual_name), ascii_len, actual_name, ascii_len);
 160   }
 161   if (actual_size != expect_size) {
 162     dump_buffer();
 163     fatal("%s: expected \"%s\" at " INTPTR_FORMAT " to have size %u, got %u",
 164         code_desc(), expect_name, p2i(actual_name), expect_size, actual_size);
 165   }
 166 }
 167 
 168 const char* HotSpotCompiledCodeStream::read_utf8(const char* name, JVMCI_TRAPS) {
 169   jint utf_len = read_s4(name);
 170   if (utf_len == -1) {
 171     return nullptr;
 172   }
 173   guarantee(utf_len >= 0, "bad utf_len: %d", utf_len);
 174 
 175   const char* utf = (const char*) _pos;
 176   char* end = (char*) _pos + utf_len;
 177   _pos = (const u1*) (end + 1);
 178   if (*end != '\0') {
 179     JVMCI_ERROR_NULL("UTF8 string at " INTPTR_FORMAT " of length %d missing 0 terminator: \"%.*s\"%s",
 180         p2i(utf), utf_len, utf_len, utf, context());
 181   }
 182   return utf;
 183 }
 184 
 185 Method* HotSpotCompiledCodeStream::read_method(const char* name) {
 186   return (Method*) read_u8(name);
 187 }
 188 
 189 Klass* HotSpotCompiledCodeStream::read_klass(const char* name) {
 190   return (Klass*) read_u8(name);
 191 }
 192 
 193 ScopeValue* HotSpotCompiledCodeStream::virtual_object_at(int id, JVMCI_TRAPS) const {
 194   if (_virtual_objects == nullptr) {
 195     JVMCI_ERROR_NULL("virtual object id %d read outside scope of decoding DebugInfo%s", id, context());
 196   }
 197   if (id < 0 || id >= _virtual_objects->length()) {
 198     JVMCI_ERROR_NULL("invalid virtual object id %d%s", id, context());
 199   }
 200   return _virtual_objects->at(id);
 201 }
 202 
 203 #ifndef PRODUCT
 204 void CodeInstaller::verify_bci_constants(JVMCIEnv* env) {
 205 #define CHECK_IN_SYNC(name) do { \
 206   int expect = env->get_BytecodeFrame_ ## name ##_BCI(); \
 207   int actual = name##_BCI; \
 208   if (expect != actual) fatal("CodeInstaller::" #name "_BCI(%d) != BytecodeFrame." #name "_BCI(%d)", expect, actual); \
 209 } while(0)
 210 
 211   CHECK_IN_SYNC(UNWIND);
 212   CHECK_IN_SYNC(BEFORE);
 213   CHECK_IN_SYNC(AFTER);
 214   CHECK_IN_SYNC(AFTER_EXCEPTION);
 215   CHECK_IN_SYNC(UNKNOWN);
 216   CHECK_IN_SYNC(INVALID_FRAMESTATE);
 217 #undef CHECK_IN_SYNC
 218 }
 219 #endif
 220 
 221 VMReg CodeInstaller::getVMRegFromLocation(HotSpotCompiledCodeStream* stream, int total_frame_size, JVMCI_TRAPS) {
 222   u2 reg = stream->read_u2("register");
 223   u2 offset = stream->read_u2("offset");
 224 
 225   if (reg != NO_REGISTER) {
 226     VMReg vmReg = CodeInstaller::get_hotspot_reg(reg, JVMCI_CHECK_NULL);
 227     if (offset % 4 == 0) {
 228       return vmReg->next(offset / 4);
 229     } else {
 230       JVMCI_ERROR_NULL("unaligned subregister offset %d in oop map%s", offset, stream->context());
 231     }
 232   } else {
 233     if (offset % 4 == 0) {
 234       VMReg vmReg = VMRegImpl::stack2reg(offset / 4);
 235       if (!OopMapValue::legal_vm_reg_name(vmReg)) {
 236         // This restriction only applies to VMRegs that are used in OopMap but
 237         // since that's the only use of VMRegs it's simplest to put this test
 238         // here.  This test should also be equivalent legal_vm_reg_name but JVMCI
 239         // clients can use max_oop_map_stack_stack_offset to detect this problem
 240         // directly.  The asserts just ensure that the tests are in agreement.
 241         assert(offset > CompilerToVM::Data::max_oop_map_stack_offset(), "illegal VMReg");
 242         JVMCI_ERROR_NULL("stack offset %d is too large to be encoded in OopMap (max %d)%s",
 243                          offset, CompilerToVM::Data::max_oop_map_stack_offset(), stream->context());
 244       }
 245       assert(OopMapValue::legal_vm_reg_name(vmReg), "illegal VMReg");
 246       return vmReg;
 247     } else {
 248       JVMCI_ERROR_NULL("unaligned stack offset %d in oop map%s", offset, stream->context());
 249     }
 250   }
 251 }
 252 
 253 OopMap* CodeInstaller::create_oop_map(HotSpotCompiledCodeStream* stream, u1 debug_info_flags, JVMCI_TRAPS) {
 254   assert(is_set(debug_info_flags, DI_HAS_REFERENCE_MAP), "must be");
 255   u2 max_register_size = stream->read_u2("maxRegisterSize");
 256   if (!_has_wide_vector && SharedRuntime::is_wide_vector(max_register_size)) {
 257     if (SharedRuntime::polling_page_vectors_safepoint_handler_blob() == nullptr) {
 258       JVMCI_ERROR_NULL("JVMCI is producing code using vectors larger than the runtime supports%s", stream->context());
 259     }
 260     _has_wide_vector = true;
 261   }
 262   u2 length = stream->read_u2("referenceMap:length");
 263 
 264   OopMap* map = new OopMap(_total_frame_size, _parameter_count);
 265   for (int i = 0; i < length; i++) {
 266     bool has_derived = stream->read_bool("hasDerived");
 267     u2 bytes = stream->read_u2("sizeInBytes");
 268     VMReg vmReg = getVMRegFromLocation(stream, _total_frame_size, JVMCI_CHECK_NULL);
 269     if (has_derived) {
 270       // derived oop
 271       if (bytes == LP64_ONLY(8) NOT_LP64(4)) {
 272         VMReg baseReg = getVMRegFromLocation(stream, _total_frame_size, JVMCI_CHECK_NULL);
 273         map->set_derived_oop(vmReg, baseReg);
 274       } else {
 275         JVMCI_ERROR_NULL("invalid derived oop size in ReferenceMap: %d%s", bytes, stream->context());
 276       }
 277 #ifdef _LP64
 278     } else if (bytes == 8) {
 279       // wide oop
 280       map->set_oop(vmReg);
 281     } else if (bytes == 4) {
 282       // narrow oop
 283       map->set_narrowoop(vmReg);
 284 #else
 285     } else if (bytes == 4) {
 286       map->set_oop(vmReg);
 287 #endif
 288     } else {
 289       JVMCI_ERROR_NULL("invalid oop size in ReferenceMap: %d%s", bytes, stream->context());
 290     }
 291   }
 292 
 293   if (is_set(debug_info_flags, DI_HAS_CALLEE_SAVE_INFO)) {
 294     length = stream->read_u2("calleeSaveInfo:length");
 295     for (jint i = 0; i < length; i++) {
 296       u2 jvmci_reg_number = stream->read_u2("register");
 297       VMReg hotspot_reg = CodeInstaller::get_hotspot_reg(jvmci_reg_number, JVMCI_CHECK_NULL);
 298       // HotSpot stack slots are 4 bytes
 299       u2 jvmci_slot = stream->read_u2("slot");
 300       jint hotspot_slot = jvmci_slot * VMRegImpl::slots_per_word;
 301       VMReg hotspot_slot_as_reg = VMRegImpl::stack2reg(hotspot_slot);
 302       map->set_callee_saved(hotspot_slot_as_reg, hotspot_reg);
 303 #ifdef _LP64
 304       // (copied from generate_oop_map() in c1_Runtime1_x86.cpp)
 305       VMReg hotspot_slot_hi_as_reg = VMRegImpl::stack2reg(hotspot_slot + 1);
 306       map->set_callee_saved(hotspot_slot_hi_as_reg, hotspot_reg->next());
 307 #endif
 308     }
 309   }
 310   return map;
 311 }
 312 
 313 void* CodeInstaller::record_metadata_reference(CodeSection* section, address dest, HotSpotCompiledCodeStream* stream, u1 tag, JVMCI_TRAPS) {
 314   /*
 315    * This method needs to return a raw (untyped) pointer, since the value of a pointer to the base
 316    * class is in general not equal to the pointer of the subclass. When patching metaspace pointers,
 317    * the compiler expects a direct pointer to the subclass (Klass* or Method*), not a pointer to the
 318    * base class (Metadata* or MetaspaceObj*).
 319    */
 320   if (tag == PATCH_KLASS) {
 321     Klass* klass = stream->read_klass("patch:klass");
 322     int index = _oop_recorder->find_index(klass);
 323     section->relocate(dest, metadata_Relocation::spec(index));
 324     JVMCI_event_3("metadata[%d of %d] = %s", index, _oop_recorder->metadata_count(), klass->name()->as_C_string());
 325     return klass;
 326   } else if (tag == PATCH_METHOD) {
 327     Method* method = stream->read_method("patch:method");
 328     int index = _oop_recorder->find_index(method);
 329     section->relocate(dest, metadata_Relocation::spec(index));
 330     JVMCI_event_3("metadata[%d of %d] = %s", index, _oop_recorder->metadata_count(), method->name()->as_C_string());
 331     return method;
 332   } else {
 333     JVMCI_ERROR_NULL("unexpected metadata reference tag: %d%s", tag, stream->context());
 334   }
 335 }
 336 
 337 #ifdef _LP64
 338 narrowKlass CodeInstaller::record_narrow_metadata_reference(CodeSection* section, address dest, HotSpotCompiledCodeStream* stream, u1 tag, JVMCI_TRAPS) {
 339   if (tag != PATCH_NARROW_KLASS) {
 340     JVMCI_ERROR_0("unexpected compressed pointer tag %d%s", tag, stream->context());
 341   }
 342   Klass* klass = stream->read_klass("patch:klass");
 343   int index = _oop_recorder->find_index(klass);
 344   section->relocate(dest, metadata_Relocation::spec(index));
 345   JVMCI_event_3("narrowKlass[%d of %d] = %s", index, _oop_recorder->metadata_count(), klass->name()->as_C_string());
 346   guarantee(CompressedKlassPointers::is_encodable(klass), "klass cannot be compressed: %s", klass->external_name());
 347   return CompressedKlassPointers::encode(klass);
 348 }
 349 #endif
 350 
 351 ScopeValue* CodeInstaller::to_primitive_value(HotSpotCompiledCodeStream* stream, jlong raw, BasicType type, ScopeValue* &second, JVMCI_TRAPS) {
 352   if (type == T_INT || type == T_FLOAT) {
 353     jint prim = (jint) raw;
 354     switch (prim) {
 355       case -1: return _int_m1_scope_value;
 356       case  0: return _int_0_scope_value;
 357       case  1: return _int_1_scope_value;
 358       case  2: return _int_2_scope_value;
 359       default: return new ConstantIntValue(prim);
 360     }
 361   } else if (type == T_LONG || type == T_DOUBLE) {
 362     jlong prim = raw;
 363     second = _int_1_scope_value;
 364     return new ConstantLongValue(prim);
 365   } else {
 366     JVMCI_ERROR_NULL("unexpected primitive constant type %s%s", basictype_to_str(type), stream->context());
 367   }
 368 }
 369 
 370 Handle CodeInstaller::read_oop(HotSpotCompiledCodeStream* stream, u1 tag, JVMCI_TRAPS) {
 371   oop obj;
 372   if (tag == OBJECT_ID) {
 373     obj = stream->get_oop(stream->read_u1("id"), JVMCI_CHECK_(Handle()));
 374   } else if (tag == OBJECT_ID2) {
 375     obj = stream->get_oop(stream->read_u2("id:2"), JVMCI_CHECK_(Handle()));
 376   } else if (tag == JOBJECT) {
 377     jlong object_handle = stream->read_u8("jobject");
 378     obj = jvmci_env()->resolve_oop_handle(object_handle);
 379   } else {
 380     JVMCI_ERROR_(Handle(), "unexpected oop tag: %d", tag)
 381   }
 382   if (obj == nullptr) {
 383     JVMCI_THROW_MSG_(InternalError, "Constant was unexpectedly null", Handle());
 384   } else {
 385     guarantee(oopDesc::is_oop_or_null(obj), "invalid oop: " INTPTR_FORMAT, p2i((oopDesc*) obj));
 386   }
 387   return Handle(stream->thread(), obj);
 388 }
 389 
 390 ScopeValue* CodeInstaller::get_scope_value(HotSpotCompiledCodeStream* stream, u1 tag, BasicType type, ScopeValue* &second, JVMCI_TRAPS) {
 391   second = nullptr;
 392   switch (tag) {
 393     case ILLEGAL: {
 394       if (type != T_ILLEGAL) {
 395         JVMCI_ERROR_NULL("unexpected illegal value, expected %s%s", basictype_to_str(type), stream->context());
 396       }
 397       return _illegal_value;
 398     }
 399     case REGISTER_PRIMITIVE:
 400     case REGISTER_NARROW_OOP:
 401     case REGISTER_OOP:
 402     case REGISTER_VECTOR: {
 403       u2 number = stream->read_u2("register");
 404       VMReg hotspotRegister = get_hotspot_reg(number, JVMCI_CHECK_NULL);
 405       if (is_general_purpose_reg(hotspotRegister)) {
 406         Location::Type locationType;
 407         if (type == T_OBJECT) {
 408           locationType = tag == REGISTER_NARROW_OOP ? Location::narrowoop : Location::oop;
 409         } else if (type == T_LONG) {
 410           locationType = Location::lng;
 411         } else if (type == T_INT || type == T_FLOAT || type == T_SHORT || type == T_CHAR || type == T_BYTE || type == T_BOOLEAN) {
 412           locationType = Location::int_in_long;
 413         } else {
 414           JVMCI_ERROR_NULL("unexpected type %s in CPU register%s", basictype_to_str(type), stream->context());
 415         }
 416         ScopeValue* value = new LocationValue(Location::new_reg_loc(locationType, hotspotRegister));
 417         if (type == T_LONG) {
 418           second = value;
 419         }
 420         return value;
 421       } else {
 422         Location::Type locationType;
 423         if (type == T_FLOAT) {
 424           // this seems weird, but the same value is used in c1_LinearScan
 425           locationType = Location::normal;
 426         } else if (type == T_DOUBLE) {
 427           locationType = Location::dbl;
 428         } else if (type == T_OBJECT && tag == REGISTER_VECTOR) {
 429           locationType = Location::vector;
 430         } else {
 431           JVMCI_ERROR_NULL("unexpected type %s in floating point register%s", basictype_to_str(type), stream->context());
 432         }
 433         ScopeValue* value = new LocationValue(Location::new_reg_loc(locationType, hotspotRegister));
 434         if (type == T_DOUBLE) {
 435           second = value;
 436         }
 437         return value;
 438       }
 439     }
 440     case STACK_SLOT_PRIMITIVE:
 441     case STACK_SLOT_NARROW_OOP:
 442     case STACK_SLOT_OOP:
 443     case STACK_SLOT_VECTOR: {
 444       jint offset = (jshort) stream->read_s2("offset");
 445       if (stream->read_bool("addRawFrameSize")) {
 446         offset += _total_frame_size;
 447       }
 448       Location::Type locationType;
 449       if (type == T_OBJECT) {
 450         locationType = tag == STACK_SLOT_VECTOR ? Location::vector : tag == STACK_SLOT_NARROW_OOP ? Location::narrowoop : Location::oop;
 451       } else if (type == T_LONG) {
 452         locationType = Location::lng;
 453       } else if (type == T_DOUBLE) {
 454         locationType = Location::dbl;
 455       } else if (type == T_INT || type == T_FLOAT || type == T_SHORT || type == T_CHAR || type == T_BYTE || type == T_BOOLEAN) {
 456         locationType = Location::normal;
 457       } else {
 458         JVMCI_ERROR_NULL("unexpected type %s in stack slot%s", basictype_to_str(type), stream->context());
 459       }
 460       ScopeValue* value = new LocationValue(Location::new_stk_loc(locationType, offset));
 461       if (type == T_DOUBLE || type == T_LONG) {
 462         second = value;
 463       }
 464       return value;
 465     }
 466     case NULL_CONSTANT:      { return _oop_null_scope_value; }
 467     case RAW_CONSTANT:       { return new ConstantLongValue(stream->read_u8("primitive")); }
 468     case PRIMITIVE_0:        { ScopeValue* v = to_primitive_value(stream, 0, type, second, JVMCI_CHECK_NULL); return v; }
 469     case PRIMITIVE4:         { ScopeValue* v = to_primitive_value(stream, stream->read_s4("primitive4"), type, second, JVMCI_CHECK_NULL); return v; }
 470     case PRIMITIVE8:         { ScopeValue* v = to_primitive_value(stream, stream->read_s8("primitive8"), type, second, JVMCI_CHECK_NULL); return v; }
 471     case VIRTUAL_OBJECT_ID:  { ScopeValue* v = stream->virtual_object_at(stream->read_u1("id"),   JVMCI_CHECK_NULL); return v; }
 472     case VIRTUAL_OBJECT_ID2: { ScopeValue* v = stream->virtual_object_at(stream->read_u2("id:2"), JVMCI_CHECK_NULL); return v; }
 473 
 474     case OBJECT_ID:
 475     case OBJECT_ID2:
 476     case JOBJECT: {
 477       Handle obj = read_oop(stream, tag, JVMCI_CHECK_NULL);
 478       return new ConstantOopWriteValue(JNIHandles::make_local(obj()));
 479     }
 480     default: {
 481       JVMCI_ERROR_NULL("unexpected tag in scope: %d%s", tag, stream->context())
 482     }
 483   }
 484 }
 485 
 486 void CodeInstaller::record_object_value(ObjectValue* sv, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
 487   oop javaMirror = JNIHandles::resolve(sv->klass()->as_ConstantOopWriteValue()->value());
 488   Klass* klass = java_lang_Class::as_Klass(javaMirror);
 489   bool isLongArray = klass == Universe::longArrayKlass();
 490   bool isByteArray = klass == Universe::byteArrayKlass();
 491 
 492   u2 length = stream->read_u2("values:length");
 493   for (jint i = 0; i < length; i++) {
 494     ScopeValue* cur_second = nullptr;
 495     BasicType type = (BasicType) stream->read_u1("basicType");
 496     ScopeValue* value;
 497     u1 tag = stream->read_u1("tag");
 498     if (tag == ILLEGAL) {
 499       if (isByteArray && type == T_ILLEGAL) {
 500         /*
 501          * The difference between a virtualized large access and a deferred write is the kind stored in the slotKinds
 502          * of the virtual object: in the virtualization case, the kind is illegal, in the deferred write case, the kind
 503          * is access stack kind (an int).
 504          */
 505         value = _virtual_byte_array_marker;
 506       } else {
 507         value = _illegal_value;
 508         if (type == T_DOUBLE || type == T_LONG) {
 509             cur_second = _illegal_value;
 510         }
 511       }
 512     } else {
 513       value = get_scope_value(stream, tag, type, cur_second, JVMCI_CHECK);
 514     }
 515 
 516     if (isLongArray && cur_second == nullptr) {
 517       // we're trying to put ints into a long array... this isn't really valid, but it's used for some optimizations.
 518       // add an int 0 constant
 519       cur_second = _int_0_scope_value;
 520     }
 521 
 522     if (isByteArray && cur_second != nullptr && (type == T_DOUBLE || type == T_LONG)) {
 523       // we are trying to write a long in a byte Array. We will need to count the illegals to restore the type of
 524       // the thing we put inside.
 525       cur_second = nullptr;
 526     }
 527 
 528     if (cur_second != nullptr) {
 529       sv->field_values()->append(cur_second);
 530     }
 531     assert(value != nullptr, "missing value");
 532     sv->field_values()->append(value);
 533   }
 534 }
 535 
 536 GrowableArray<ScopeValue*>* CodeInstaller::read_local_or_stack_values(HotSpotCompiledCodeStream* stream, u1 frame_flags, bool is_locals, JVMCI_TRAPS) {
 537   u2 length;
 538   if (is_locals) {
 539     if (!is_set(frame_flags, DIF_HAS_LOCALS)) {
 540       return nullptr;
 541     }
 542     length = stream->read_u2("numLocals");
 543   } else {
 544     if (!is_set(frame_flags, DIF_HAS_STACK)) {
 545       return nullptr;
 546     }
 547     length = stream->read_u2("numStack");
 548   }
 549   GrowableArray<ScopeValue*>* values = new GrowableArray<ScopeValue*> (length);
 550   for (int i = 0; i < length; i++) {
 551     ScopeValue* second = nullptr;
 552     BasicType type = (BasicType) stream->read_u1("basicType");
 553     u1 tag = stream->read_u1("tag");
 554     ScopeValue* first = get_scope_value(stream, tag, type, second, JVMCI_CHECK_NULL);
 555     if (second != nullptr) {
 556       if (i == length) {
 557         JVMCI_ERROR_NULL("double-slot value not followed by Value.ILLEGAL%s", stream->context());
 558       }
 559       i++;
 560       stream->read_u1("basicType");
 561       tag = stream->read_u1("tag");
 562       if (tag != ILLEGAL) {
 563         JVMCI_ERROR_NULL("double-slot value not followed by Value.ILLEGAL%s", stream->context());
 564       }
 565       values->append(second);
 566     }
 567     values->append(first);
 568   }
 569   return values;
 570 }
 571 
 572 GrowableArray<MonitorValue*>* CodeInstaller::read_monitor_values(HotSpotCompiledCodeStream* stream, u1 frame_flags, JVMCI_TRAPS) {
 573   if (!is_set(frame_flags, DIF_HAS_LOCKS)) {
 574     return nullptr;
 575   }
 576   if (!_has_monitors) {
 577     _has_monitors = true;
 578   }
 579   u2 length = stream->read_u2("numLocks");
 580   GrowableArray<MonitorValue*>* monitors = new GrowableArray<MonitorValue*>(length);
 581   for (int i = 0; i < length; i++) {
 582     bool eliminated = stream->read_bool("isEliminated");
 583     ScopeValue* second = nullptr;
 584     ScopeValue* owner_value = get_scope_value(stream, stream->read_u1("tag"), T_OBJECT, second, JVMCI_CHECK_NULL);
 585     assert(second == nullptr, "monitor cannot occupy two stack slots");
 586 
 587     ScopeValue* lock_data_value = get_scope_value(stream, stream->read_u1("tag"), T_LONG, second, JVMCI_CHECK_NULL);
 588     assert(second == lock_data_value, "monitor is LONG value that occupies two stack slots");
 589     assert(lock_data_value->is_location(), "invalid monitor location");
 590     Location lock_data_loc = ((LocationValue*) lock_data_value)->location();
 591 
 592     monitors->append(new MonitorValue(owner_value, lock_data_loc, eliminated));
 593   }
 594   return monitors;
 595 }
 596 
 597 void CodeInstaller::initialize_dependencies(HotSpotCompiledCodeStream* stream, u1 code_flags, OopRecorder* oop_recorder, JVMCI_TRAPS) {
 598   JavaThread* thread = stream->thread();
 599   CompilerThread* compilerThread = thread->is_Compiler_thread() ? CompilerThread::cast(thread) : nullptr;
 600   _oop_recorder = oop_recorder;
 601   _dependencies = new Dependencies(&_arena, _oop_recorder, compilerThread != nullptr ? compilerThread->log() : nullptr);
 602   if (is_set(code_flags, HCC_HAS_ASSUMPTIONS)) {
 603     u2 length = stream->read_u2("assumptions:length");
 604     for (int i = 0; i < length; ++i) {
 605       u1 tag = stream->read_u1("tag");
 606       switch (tag) {
 607         case NO_FINALIZABLE_SUBCLASS: {
 608           Klass* receiver_type = stream->read_klass("receiverType");
 609           _dependencies->assert_has_no_finalizable_subclasses(receiver_type);
 610           break;
 611         }
 612         case CONCRETE_SUBTYPE: {
 613           Klass* context = stream->read_klass("context");
 614           Klass* subtype = stream->read_klass("subtype");
 615           assert(context->is_abstract(), "must be");
 616           _dependencies->assert_abstract_with_unique_concrete_subtype(context, subtype);
 617           break;
 618         }
 619         case LEAF_TYPE: {
 620           Klass* context = stream->read_klass("context");
 621           _dependencies->assert_leaf_type(context);
 622           break;
 623         }
 624         case CONCRETE_METHOD: {
 625           Klass* context = stream->read_klass("context");
 626           Method* impl = stream->read_method("impl");
 627           _dependencies->assert_unique_concrete_method(context, impl);
 628           break;
 629         }
 630         case CALLSITE_TARGET_VALUE: {
 631           u1 obj_tag = stream->read_u1("tag");
 632           Handle callSite = read_oop(stream, obj_tag, JVMCI_CHECK);
 633           obj_tag = stream->read_u1("tag");
 634           Handle methodHandle = read_oop(stream, obj_tag, JVMCI_CHECK);
 635           _dependencies->assert_call_site_target_value(callSite(), methodHandle());
 636           break;
 637         }
 638         default: {
 639           JVMCI_ERROR("unexpected assumption tag %d%s", tag, stream->context());
 640         }
 641       }
 642     }
 643   }
 644   if (is_set(code_flags, HCC_HAS_METHODS)) {
 645     u2 length = stream->read_u2("methods:length");
 646     for (int i = 0; i < length; ++i) {
 647       Method* method = stream->read_method("method");
 648       if (JvmtiExport::can_hotswap_or_post_breakpoint()) {
 649         _dependencies->assert_evol_method(method);
 650       }
 651     }
 652   }
 653 }
 654 
 655 JVMCI::CodeInstallResult CodeInstaller::install_runtime_stub(CodeBlob*& cb,
 656                                                              const char* name,
 657                                                              CodeBuffer* buffer,
 658                                                              int stack_slots,
 659                                                              JVMCI_TRAPS) {
 660   if (name == nullptr) {
 661     JVMCI_ERROR_OK("stub should have a name");
 662   }
 663 
 664   name = os::strdup(name);
 665   GrowableArray<RuntimeStub*> *stubs_to_free = nullptr;
 666 #ifdef ASSERT
 667   const char* val = Arguments::PropertyList_get_value(Arguments::system_properties(), "test.jvmci.forceRuntimeStubAllocFail");
 668   if (val != nullptr && strstr(name , val) != nullptr) {
 669     stubs_to_free = new GrowableArray<RuntimeStub*>();
 670     JVMCI_event_1("forcing allocation of %s in code cache to fail", name);
 671   }
 672 #endif
 673 
 674   do {
 675     RuntimeStub* stub = RuntimeStub::new_runtime_stub(name,
 676                                        buffer,
 677                                        _offsets.value(CodeOffsets::Frame_Complete),
 678                                        stack_slots,
 679                                        _debug_recorder->_oopmaps,
 680                                        /* caller_must_gc_arguments */ false,
 681                                        /* alloc_fail_is_fatal */ false);
 682     cb = stub;
 683     if (stub == nullptr) {
 684       // Allocation failed
 685 #ifdef ASSERT
 686       if (stubs_to_free != nullptr) {
 687         JVMCI_event_1("allocation of %s in code cache failed, freeing %d stubs", name, stubs_to_free->length());
 688         for (GrowableArrayIterator<RuntimeStub*> iter = stubs_to_free->begin(); iter != stubs_to_free->end(); ++iter) {
 689           RuntimeStub::free(*iter);
 690         }
 691       }
 692 #endif
 693       return JVMCI::cache_full;
 694     }
 695     if (stubs_to_free == nullptr) {
 696       return JVMCI::ok;
 697     }
 698     stubs_to_free->append(stub);
 699   } while (true);
 700 }
 701 
 702 JVMCI::CodeInstallResult CodeInstaller::install(JVMCICompiler* compiler,
 703     jlong compiled_code_buffer,
 704     bool with_type_info,
 705     JVMCIObject compiled_code,
 706     objArrayHandle object_pool,
 707     CodeBlob*& cb,
 708     JVMCINMethodHandle& nmethod_handle,
 709     JVMCIObject installed_code,
 710     FailedSpeculation** failed_speculations,
 711     char* speculations,
 712     int speculations_len,
 713     JVMCI_TRAPS) {
 714 
 715   JavaThread* thread = JavaThread::current();
 716   HotSpotCompiledCodeStream* stream = new HotSpotCompiledCodeStream(thread, (const u1*) compiled_code_buffer, with_type_info, object_pool);
 717 
 718   u1 code_flags = stream->read_u1("code:flags");
 719   bool is_nmethod = is_set(code_flags, HCC_IS_NMETHOD);
 720   const char* name = stream->read_utf8("name", JVMCI_CHECK_OK);
 721 
 722   methodHandle method;
 723   jint entry_bci = -1;
 724   JVMCICompileState* compile_state = nullptr;
 725   bool has_unsafe_access = false;
 726   bool has_scoped_access = false;
 727   jint id = -1;
 728 
 729   if (is_nmethod) {
 730     method = methodHandle(thread, stream->read_method("method"));
 731     entry_bci = is_nmethod ? stream->read_s4("entryBCI") : -1;
 732     compile_state = (JVMCICompileState*) stream->read_u8("compileState");
 733     has_unsafe_access = stream->read_bool("hasUnsafeAccess");
 734     has_scoped_access = stream->read_bool("hasScopedAccess");
 735     id = stream->read_s4("id");
 736   }
 737   stream->set_code_desc(name, method);
 738 
 739   CodeBuffer buffer("JVMCI Compiler CodeBuffer");
 740   OopRecorder* recorder = new OopRecorder(&_arena, true);
 741   initialize_dependencies(stream, code_flags, recorder, JVMCI_CHECK_OK);
 742 
 743   // Get instructions and constants CodeSections early because we need it.
 744   _instructions = buffer.insts();
 745   _constants = buffer.consts();
 746 
 747   initialize_fields(stream, code_flags, method, buffer, JVMCI_CHECK_OK);
 748   JVMCI::CodeInstallResult result = initialize_buffer(compiled_code, buffer, stream, code_flags, JVMCI_CHECK_OK);
 749 
 750   u4 available = stream->available();
 751   if (result == JVMCI::ok && available != 0) {
 752     JVMCI_ERROR_OK("%d bytes remaining in stream%s", available, stream->context());
 753   }
 754 
 755   if (result != JVMCI::ok) {
 756     return result;
 757   }
 758 
 759   int stack_slots = _total_frame_size / HeapWordSize; // conversion to words
 760 
 761   if (!is_nmethod) {
 762     return install_runtime_stub(cb, name, &buffer, stack_slots, JVMCI_CHECK_OK);
 763   } else {
 764     if (compile_state != nullptr) {
 765       jvmci_env()->set_compile_state(compile_state);
 766     }
 767 
 768     if (id == -1) {
 769       // Make sure a valid compile_id is associated with every compile
 770       id = CompileBroker::assign_compile_id_unlocked(thread, method, entry_bci);
 771       jvmci_env()->set_HotSpotCompiledNmethod_id(compiled_code, id);
 772     }
 773     if (!jvmci_env()->isa_HotSpotNmethod(installed_code)) {
 774       JVMCI_THROW_MSG_(IllegalArgumentException, "InstalledCode object must be a HotSpotNmethod when installing a HotSpotCompiledNmethod", JVMCI::ok);
 775     }
 776 
 777     // Enforce that compiled methods have an nmethod barrier.
 778     if (_nmethod_entry_patch_offset == -1) {
 779       JVMCI_THROW_MSG_(IllegalArgumentException, "nmethod entry barrier is missing", JVMCI::ok);
 780     }
 781 
 782     JVMCIObject mirror = installed_code;
 783     nmethod* nm = nullptr; // nm is an out parameter of register_method
 784     result = runtime()->register_method(jvmci_env(),
 785                                         method,
 786                                         nm,
 787                                         entry_bci,
 788                                         &_offsets,
 789                                         _orig_pc_offset,
 790                                         &buffer,
 791                                         stack_slots,
 792                                         _debug_recorder->_oopmaps,
 793                                         &_exception_handler_table,
 794                                         &_implicit_exception_table,
 795                                         compiler,
 796                                         _debug_recorder,
 797                                         _dependencies,
 798                                         id,
 799                                         _has_monitors,
 800                                         has_unsafe_access,
 801                                         has_scoped_access,
 802                                         _has_wide_vector,
 803                                         compiled_code,
 804                                         mirror,
 805                                         failed_speculations,
 806                                         speculations,
 807                                         speculations_len,
 808                                         _nmethod_entry_patch_offset);
 809     if (result == JVMCI::ok) {
 810       guarantee(nm != nullptr, "successful compile must produce an nmethod");
 811       nmethod_handle.set_nmethod(nm);
 812       cb = nm;
 813       if (compile_state == nullptr) {
 814         // This compile didn't come through the CompileBroker so perform the printing here
 815         DirectiveSet* directive = DirectivesStack::getMatchingDirective(method, compiler);
 816         nm->maybe_print_nmethod(directive);
 817         DirectivesStack::release(directive);
 818       }
 819 
 820       BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
 821 
 822       // an empty error buffer for use by the verify_barrier code
 823       err_msg msg("");
 824       if (!bs_nm->verify_barrier(nm, msg)) {
 825         JVMCI_THROW_MSG_(IllegalArgumentException, err_msg("nmethod entry barrier is malformed: %s", msg.buffer()), JVMCI::ok);
 826       }
 827     }
 828   }
 829 
 830   if (cb != nullptr) {
 831     // Make sure the pre-calculated constants section size was correct.
 832     guarantee((cb->code_begin() - cb->content_begin()) >= _constants_size, "%d < %d", (int)(cb->code_begin() - cb->content_begin()), _constants_size);
 833   }
 834   return result;
 835 }
 836 
 837 void CodeInstaller::initialize_fields(HotSpotCompiledCodeStream* stream, u1 code_flags, methodHandle& method, CodeBuffer& buffer, JVMCI_TRAPS) {
 838   if (!method.is_null()) {
 839     _parameter_count = method->size_of_parameters();
 840     JVMCI_event_2("installing code for %s", method->name_and_sig_as_C_string());
 841   } else {
 842     // Must be a HotSpotCompiledCode for a stub.
 843     // Only used in OopMap constructor for non-product builds
 844     _parameter_count = 0;
 845   }
 846   _sites_count = stream->read_s4("sites:length");
 847   _code_size = stream->read_s4("targetCodeSize");
 848   _total_frame_size = stream->read_s4("totalFrameSize");
 849   if (!is_set(code_flags, HCC_HAS_DEOPT_RESCUE_SLOT)) {
 850     _orig_pc_offset = -1;
 851   } else {
 852     _orig_pc_offset = stream->read_s2("offset");
 853     if (stream->read_bool("addRawFrameSize")) {
 854       _orig_pc_offset += _total_frame_size;
 855     }
 856     if (_orig_pc_offset < 0) {
 857       JVMCI_ERROR("invalid deopt rescue slot: %d%s", _orig_pc_offset, stream->context());
 858     }
 859   }
 860 
 861   // Pre-calculate the constants section size.  This is required for PC-relative addressing.
 862   u4 data_section_size = stream->read_u4("dataSectionSize");
 863   u1 data_section_alignment = stream->read_u1("dataSectionAlignment");
 864   buffer.set_const_section_alignment(data_section_alignment);
 865   if ((_constants->alignment() % data_section_alignment) != 0) {
 866     JVMCI_ERROR("invalid data section alignment: %d [constants alignment: %d]%s",
 867         data_section_alignment, _constants->alignment(), stream->context());
 868   }
 869   _constants_size = data_section_size;
 870   _next_call_type = INVOKE_INVALID;
 871   _has_monitors = false;
 872   _has_wide_vector = false;
 873   _nmethod_entry_patch_offset = -1;
 874 }
 875 
 876 u1 CodeInstaller::as_read_oop_tag(HotSpotCompiledCodeStream* stream, u1 patch_object_tag, JVMCI_TRAPS) {
 877   switch (patch_object_tag) {
 878     case PATCH_OBJECT_ID:
 879     case PATCH_NARROW_OBJECT_ID: {
 880       return OBJECT_ID;
 881     }
 882     case PATCH_OBJECT_ID2:
 883     case PATCH_NARROW_OBJECT_ID2: {
 884       return OBJECT_ID2;
 885     }
 886     case PATCH_NARROW_JOBJECT:
 887     case PATCH_JOBJECT: {
 888       return JOBJECT;
 889     }
 890     default: {
 891       JVMCI_ERROR_0("unknown object patch tag: %d%s", patch_object_tag, stream->context());
 892     }
 893   }
 894 }
 895 
 896 int CodeInstaller::estimate_stubs_size(HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
 897   // Estimate the number of static call stubs that might be emitted.
 898   u2 static_call_stubs = stream->read_u2("numStaticCallStubs");
 899   u2 trampoline_stubs = stream->read_u2("numTrampolineStubs");
 900   int size = static_call_stubs * CompiledDirectCall::to_interp_stub_size();
 901   size += trampoline_stubs * CompiledDirectCall::to_trampoline_stub_size();
 902   return size;
 903 }
 904 
 905 // perform data and call relocation on the CodeBuffer
 906 JVMCI::CodeInstallResult CodeInstaller::initialize_buffer(JVMCIObject compiled_code, CodeBuffer& buffer, HotSpotCompiledCodeStream* stream, u1 code_flags, JVMCI_TRAPS) {
 907   JavaThread* thread = stream->thread();
 908   HandleMark hm(thread);
 909   int locs_buffer_size = _sites_count * (relocInfo::length_limit + sizeof(relocInfo));
 910 
 911 
 912   // Allocate enough space in the stub section for the static call
 913   // stubs.  Stubs have extra relocs but they are managed by the stub
 914   // section itself so they don't need to be accounted for in the
 915   // locs_buffer above.
 916   int stubs_size = estimate_stubs_size(stream, JVMCI_CHECK_OK);
 917 
 918   assert((CodeBuffer::SECT_INSTS == CodeBuffer::SECT_STUBS - 1) &&
 919          (CodeBuffer::SECT_CONSTS == CodeBuffer::SECT_INSTS - 1), "sections order: consts, insts, stubs");
 920   // buffer content: [constants + code_align] + [code + stubs_align] + [stubs]
 921   int total_size = align_up(_constants_size, buffer.insts()->alignment()) +
 922                    align_up(_code_size, buffer.stubs()->alignment()) +
 923                    stubs_size;
 924 
 925   if (total_size > JVMCINMethodSizeLimit) {
 926     return JVMCI::code_too_large;
 927   }
 928 
 929   buffer.initialize(total_size, locs_buffer_size);
 930   if (buffer.blob() == nullptr) {
 931     return JVMCI::cache_full;
 932   }
 933   buffer.initialize_stubs_size(stubs_size);
 934   buffer.initialize_consts_size(_constants_size);
 935 
 936   _debug_recorder = new DebugInformationRecorder(_oop_recorder);
 937   _debug_recorder->set_oopmaps(new OopMapSet());
 938 
 939   buffer.initialize_oop_recorder(_oop_recorder);
 940 
 941   // copy the constant data into the newly created CodeBuffer
 942   address end_data = _constants->start() + _constants_size;
 943   JVMCIObject data_section = jvmci_env()->get_HotSpotCompiledCode_dataSection(compiled_code);
 944   JVMCIENV->copy_bytes_to(data_section, (jbyte*) _constants->start(), 0, _constants_size);
 945   _constants->set_end(end_data);
 946 
 947   // copy the code into the newly created CodeBuffer
 948   address end_pc = _instructions->start() + _code_size;
 949   guarantee(_instructions->allocates2(end_pc), "initialize should have reserved enough space for all the code");
 950 
 951   JVMCIPrimitiveArray code = jvmci_env()->get_HotSpotCompiledCode_targetCode(compiled_code);
 952   JVMCIENV->copy_bytes_to(code, (jbyte*) _instructions->start(), 0, _code_size);
 953   _instructions->set_end(end_pc);
 954 
 955 
 956   u2 length = stream->read_u2("dataSectionPatches:length");
 957   for (int i = 0; i < length; i++) {
 958     address dest = _constants->start() + stream->read_u4("patch:pcOffset");
 959     u1 tag = stream->read_u1("tag");
 960 
 961     switch (tag) {
 962       case PATCH_METHOD:
 963       case PATCH_KLASS: {
 964         *((void**) dest) = record_metadata_reference(_constants, dest, stream, tag, JVMCI_CHECK_OK);
 965         break;
 966       }
 967       case PATCH_NARROW_KLASS: {
 968 #ifdef _LP64
 969         *((narrowKlass*) dest) = record_narrow_metadata_reference(_constants, dest, stream, tag, JVMCI_CHECK_OK);
 970 #else
 971         JVMCI_ERROR_OK("unexpected compressed Klass* in 32-bit mode");
 972 #endif
 973         break;
 974       }
 975       case PATCH_OBJECT_ID:
 976       case PATCH_OBJECT_ID2:
 977       case PATCH_NARROW_OBJECT_ID:
 978       case PATCH_NARROW_OBJECT_ID2:
 979       case PATCH_JOBJECT:
 980       case PATCH_NARROW_JOBJECT: {
 981         bool narrow = tag == PATCH_NARROW_OBJECT_ID || tag == PATCH_NARROW_OBJECT_ID2  || tag == PATCH_NARROW_JOBJECT;
 982         u1 read_tag = as_read_oop_tag(stream, tag, JVMCI_CHECK_OK);
 983         record_oop_patch(stream, dest, read_tag, narrow, JVMCI_CHECK_OK);
 984         break;
 985       }
 986       default: {
 987         JVMCI_ERROR_OK("invalid constant tag: %d%s", tag, stream->context());
 988         break;
 989       }
 990     }
 991   }
 992 
 993   jint last_pc_offset = -1;
 994   for (int i = 0; i < _sites_count; i++) {
 995     u4 pc_offset = stream->read_s4("site:pcOffset");
 996     u1 tag = stream->read_u1("tag");
 997     switch (tag) {
 998       case SITE_FOREIGN_CALL:
 999       case SITE_FOREIGN_CALL_NO_DEBUG_INFO:
1000       case SITE_CALL: {
1001         site_Call(buffer, tag, pc_offset, stream, JVMCI_CHECK_OK);
1002         break;
1003       }
1004       case SITE_SAFEPOINT:
1005       case SITE_IMPLICIT_EXCEPTION:
1006       case SITE_IMPLICIT_EXCEPTION_DISPATCH: {
1007         site_Safepoint(buffer, pc_offset, stream, tag, JVMCI_CHECK_OK);
1008         break;
1009       }
1010       case SITE_INFOPOINT: {
1011         site_Infopoint(buffer, pc_offset, stream, JVMCI_CHECK_OK);
1012         break;
1013       }
1014       case SITE_MARK: {
1015         site_Mark(buffer, pc_offset, stream, JVMCI_CHECK_OK);
1016         break;
1017       }
1018       case SITE_DATA_PATCH: {
1019         site_DataPatch(buffer, pc_offset, stream, JVMCI_CHECK_OK);
1020         break;
1021       }
1022       case SITE_EXCEPTION_HANDLER: {
1023         site_ExceptionHandler(pc_offset, stream);
1024         break;
1025       }
1026       default: {
1027         JVMCI_ERROR_OK("unexpected site tag at " INTPTR_FORMAT ": %d", p2i(stream->pos() - 1), tag);
1028       }
1029     }
1030 
1031     last_pc_offset = pc_offset;
1032 
1033     if ((i % 32 == 0) && SafepointMechanism::should_process(thread)) {
1034       // Force a safepoint to mitigate pause time installing large code
1035       ThreadToNativeFromVM ttnfv(thread);
1036     }
1037   }
1038 
1039   if (is_set(code_flags, HCC_HAS_COMMENTS)) {
1040     u2 length = stream->read_u2("comments:length");
1041     for (int i = 0; i < length; i++) {
1042       u4 pc_offset = stream->read_u4("comment:pcOffset");
1043       const char* text = stream->read_utf8("comment:text", JVMCI_CHECK_OK);
1044 #ifndef PRODUCT
1045       buffer.block_comment(pc_offset, text);
1046 #endif
1047     }
1048   }
1049   if (_has_auto_box) {
1050     JavaThread* THREAD = thread; // For exception macros.
1051     JVMCI::ensure_box_caches_initialized(CHECK_(JVMCI::ok));
1052   }
1053   return JVMCI::ok;
1054 }
1055 
1056 void CodeInstaller::record_oop_patch(HotSpotCompiledCodeStream* stream, address dest, u1 read_tag, bool narrow, JVMCI_TRAPS) {
1057   Handle obj = read_oop(stream, read_tag, JVMCI_CHECK);
1058   jobject value = JNIHandles::make_local(obj());
1059   int oop_index = _oop_recorder->find_index(value);
1060   if (narrow) {
1061 #ifdef _LP64
1062     _constants->relocate(dest, oop_Relocation::spec(oop_index), relocInfo::narrow_oop_in_const);
1063 #else
1064     JVMCI_ERROR("unexpected compressed oop in 32-bit mode");
1065 #endif
1066   } else {
1067     _constants->relocate(dest, oop_Relocation::spec(oop_index));
1068   }
1069 }
1070 
1071 void CodeInstaller::site_ExceptionHandler(jint pc_offset, HotSpotCompiledCodeStream* stream) {
1072   u4 handler_offset = stream->read_u4("site:handlerPos");
1073 
1074   // Subtable header
1075   _exception_handler_table.add_entry(HandlerTableEntry(1, pc_offset, 0));
1076 
1077   // Subtable entry
1078   _exception_handler_table.add_entry(HandlerTableEntry(-1, handler_offset, 0));
1079 }
1080 
1081 void CodeInstaller::read_virtual_objects(HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1082   u2 length = stream->read_u2("virtualObjects:length");
1083   if (length == 0) {
1084     return;
1085   }
1086   GrowableArray<ScopeValue*> *objects = new GrowableArray<ScopeValue*>(length, length, nullptr);
1087   stream->set_virtual_objects(objects);
1088   // Create the unique ObjectValues
1089   JavaThread* thread = stream->thread();
1090   for (int id = 0; id < length; id++) {
1091     Klass* klass = stream->read_klass("type");
1092     bool is_auto_box = stream->read_bool("isAutoBox");
1093     if (is_auto_box) {
1094       _has_auto_box = true;
1095     }
1096     oop javaMirror = klass->java_mirror();
1097     ScopeValue *klass_sv = new ConstantOopWriteValue(JNIHandles::make_local(javaMirror));
1098     ObjectValue* sv = is_auto_box ? new AutoBoxObjectValue(id, klass_sv) : new ObjectValue(id, klass_sv);
1099     objects->at_put(id, sv);
1100   }
1101   // All the values which could be referenced by the VirtualObjects
1102   // exist, so now describe all the VirtualObjects themselves.
1103   for (int id = 0; id < length; id++) {
1104     record_object_value(objects->at(id)->as_ObjectValue(), stream, JVMCI_CHECK);
1105   }
1106   _debug_recorder->dump_object_pool(objects);
1107 
1108   stream->set_virtual_objects(objects);
1109 }
1110 
1111 int CodeInstaller::map_jvmci_bci(int bci) {
1112   if (bci < 0) {
1113     switch (bci) {
1114       case BEFORE_BCI: return BeforeBci;
1115       case AFTER_BCI: return AfterBci;
1116       case UNWIND_BCI: return UnwindBci;
1117       case AFTER_EXCEPTION_BCI: return AfterExceptionBci;
1118       case UNKNOWN_BCI: return UnknownBci;
1119       case INVALID_FRAMESTATE_BCI: return InvalidFrameStateBci;
1120     }
1121     ShouldNotReachHere();
1122   }
1123   return bci;
1124 }
1125 
1126 void CodeInstaller::record_scope(jint pc_offset, HotSpotCompiledCodeStream* stream, u1 debug_info_flags, bool full_info, bool is_mh_invoke, bool return_oop, JVMCI_TRAPS) {
1127   if (full_info) {
1128     read_virtual_objects(stream, JVMCI_CHECK);
1129   }
1130   if (is_set(debug_info_flags, DI_HAS_FRAMES)) {
1131     u2 depth = stream->read_u2("depth");
1132     for (int i = 0; i < depth; i++) {
1133       Thread* thread = Thread::current();
1134       methodHandle method(thread, stream->read_method("method"));
1135       jint bci = map_jvmci_bci(stream->read_s4("bci"));
1136       if (bci == BEFORE_BCI) {
1137         bci = SynchronizationEntryBCI;
1138       }
1139 
1140       JVMCI_event_2("Recording scope pc_offset=%d bci=%d method=%s", pc_offset, bci, method->name_and_sig_as_C_string());
1141 
1142       bool reexecute = false;
1143       bool rethrow_exception = false;
1144 
1145       DebugToken* locals_token = nullptr;
1146       DebugToken* stack_token = nullptr;
1147       DebugToken* monitors_token = nullptr;
1148 
1149       if (full_info) {
1150         u1 frame_flags = stream->read_u1("flags");
1151         rethrow_exception = is_set(frame_flags, DIF_RETHROW_EXCEPTION);
1152 
1153         if (bci >= 0) {
1154           reexecute = !is_set(frame_flags, DIF_DURING_CALL);
1155         }
1156 
1157         GrowableArray<ScopeValue*>* locals = read_local_or_stack_values(stream, frame_flags, true, JVMCI_CHECK);
1158         GrowableArray<ScopeValue*>* stack = read_local_or_stack_values(stream, frame_flags, false, JVMCI_CHECK);
1159         GrowableArray<MonitorValue*>* monitors = read_monitor_values(stream, frame_flags, JVMCI_CHECK);
1160 
1161         locals_token = _debug_recorder->create_scope_values(locals);
1162         stack_token = _debug_recorder->create_scope_values(stack);
1163         monitors_token = _debug_recorder->create_monitor_values(monitors);
1164       }
1165 
1166       // has_ea_local_in_scope and arg_escape should be added to JVMCI
1167       const bool return_scalarized     = false;
1168       const bool has_ea_local_in_scope = false;
1169       const bool arg_escape            = false;
1170       _debug_recorder->describe_scope(pc_offset, method, nullptr, bci, reexecute, rethrow_exception, is_mh_invoke, return_oop,
1171                                       return_scalarized, has_ea_local_in_scope, arg_escape,
1172                                       locals_token, stack_token, monitors_token);
1173     }
1174   }
1175   if (full_info) {
1176     // Clear the virtual objects as they are specific to one DebugInfo
1177     stream->set_virtual_objects(nullptr);
1178   }
1179 }
1180 
1181 void CodeInstaller::site_Safepoint(CodeBuffer& buffer, jint pc_offset, HotSpotCompiledCodeStream* stream, u1 tag, JVMCI_TRAPS) {
1182   u1 flags = stream->read_u1("debugInfo:flags");
1183   OopMap *map = create_oop_map(stream, flags, JVMCI_CHECK);
1184   _debug_recorder->add_safepoint(pc_offset, map);
1185   record_scope(pc_offset, stream, flags, true, JVMCI_CHECK);
1186   _debug_recorder->end_safepoint(pc_offset);
1187   if (_orig_pc_offset < 0) {
1188     JVMCI_ERROR("method contains safepoint, but has no deopt rescue slot");
1189   }
1190   if (tag == SITE_IMPLICIT_EXCEPTION_DISPATCH) {
1191     jint dispatch_offset = stream->read_s4("dispatchOffset");
1192     _implicit_exception_table.append(pc_offset, dispatch_offset);
1193   } else if (tag == SITE_IMPLICIT_EXCEPTION) {
1194     _implicit_exception_table.add_deoptimize(pc_offset);
1195   }
1196 }
1197 
1198 void CodeInstaller::site_Infopoint(CodeBuffer& buffer, jint pc_offset, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1199   u1 flags = stream->read_u1("debugInfo:flags");
1200   _debug_recorder->add_non_safepoint(pc_offset);
1201   record_scope(pc_offset, stream, flags, false, JVMCI_CHECK);
1202   _debug_recorder->end_non_safepoint(pc_offset);
1203 }
1204 
1205 void CodeInstaller::site_Call(CodeBuffer& buffer, u1 tag, jint pc_offset, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1206   JavaThread* thread = stream->thread();
1207   jlong target = stream->read_u8("target");
1208   methodHandle method;
1209   bool direct_call = false;
1210   if (tag == SITE_CALL) {
1211     method = methodHandle(thread, (Method*) target);
1212     assert(Method::is_valid_method(method()), "invalid method");
1213     direct_call = stream->read_bool("direct");
1214     if (method.is_null()) {
1215       JVMCI_THROW(NullPointerException);
1216     }
1217   }
1218 
1219   NativeInstruction* inst = nativeInstruction_at(_instructions->start() + pc_offset);
1220   jint next_pc_offset = CodeInstaller::pd_next_offset(inst, pc_offset, JVMCI_CHECK);
1221 
1222   if (tag != SITE_FOREIGN_CALL_NO_DEBUG_INFO) {
1223     u1 flags = stream->read_u1("debugInfo:flags");
1224     OopMap *map = create_oop_map(stream, flags, JVMCI_CHECK);
1225     _debug_recorder->add_safepoint(next_pc_offset, map);
1226 
1227     if (!method.is_null()) {
1228       vmIntrinsics::ID iid = method->intrinsic_id();
1229       bool is_mh_invoke = false;
1230       if (direct_call) {
1231         is_mh_invoke = !method->is_static() && (iid == vmIntrinsics::_compiledLambdaForm ||
1232                 (MethodHandles::is_signature_polymorphic(iid) && MethodHandles::is_signature_polymorphic_intrinsic(iid)));
1233       }
1234       bool return_oop = method->is_returning_oop();
1235       record_scope(next_pc_offset, stream, flags, true, is_mh_invoke, return_oop, JVMCI_CHECK);
1236     } else {
1237       record_scope(next_pc_offset, stream, flags, true, JVMCI_CHECK);
1238     }
1239   }
1240 
1241   if (tag != SITE_CALL) {
1242     jlong foreign_call_destination = target;
1243     CodeInstaller::pd_relocate_ForeignCall(inst, foreign_call_destination, JVMCI_CHECK);
1244   } else {
1245     CodeInstaller::pd_relocate_JavaMethod(buffer, method, pc_offset, JVMCI_CHECK);
1246     if (_next_call_type == INVOKESTATIC || _next_call_type == INVOKESPECIAL) {
1247       // Need a static call stub for transitions from compiled to interpreted.
1248       MacroAssembler masm(&buffer);
1249       if (CompiledDirectCall::emit_to_interp_stub(&masm, _instructions->start() + pc_offset) == nullptr) {
1250         JVMCI_ERROR("could not emit to_interp stub - code cache is full");
1251       }
1252     }
1253   }
1254 
1255   _next_call_type = INVOKE_INVALID;
1256 
1257   if (tag != SITE_FOREIGN_CALL_NO_DEBUG_INFO) {
1258     _debug_recorder->end_safepoint(next_pc_offset);
1259   }
1260 }
1261 
1262 void CodeInstaller::site_DataPatch(CodeBuffer& buffer, jint pc_offset, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1263   u1 tag = stream->read_u1("tag");
1264   switch (tag) {
1265     case PATCH_OBJECT_ID:
1266     case PATCH_OBJECT_ID2:
1267     case PATCH_NARROW_OBJECT_ID:
1268     case PATCH_NARROW_OBJECT_ID2:
1269     case PATCH_JOBJECT:
1270     case PATCH_NARROW_JOBJECT: {
1271       bool narrow = tag == PATCH_NARROW_OBJECT_ID || tag == PATCH_NARROW_OBJECT_ID2  || tag == PATCH_NARROW_JOBJECT;
1272       u1 read_tag = as_read_oop_tag(stream, tag, JVMCI_CHECK);
1273       Handle obj = read_oop(stream, read_tag, JVMCI_CHECK);
1274       pd_patch_OopConstant(pc_offset, obj, narrow, JVMCI_CHECK);
1275       break;
1276     }
1277     case PATCH_METHOD:
1278     case PATCH_KLASS:
1279     case PATCH_NARROW_KLASS: {
1280       pd_patch_MetaspaceConstant(pc_offset, stream, tag, JVMCI_CHECK);
1281       break;
1282     }
1283     case PATCH_DATA_SECTION_REFERENCE: {
1284       int data_offset = stream->read_u4("data:offset");
1285       if (0 <= data_offset && data_offset < _constants_size) {
1286         if (!is_aligned(data_offset, CompilerToVM::Data::get_data_section_item_alignment())) {
1287           JVMCI_ERROR("data offset 0x%x is not %d-byte aligned%s", data_offset, relocInfo::addr_unit(), stream->context());
1288         }
1289         pd_patch_DataSectionReference(pc_offset, data_offset, JVMCI_CHECK);
1290       } else {
1291         JVMCI_ERROR("data offset 0x%x points outside data section (size 0x%x)%s", data_offset, _constants_size, stream->context());
1292       }
1293       break;
1294     }
1295     default: {
1296       JVMCI_ERROR("unknown data patch tag: %d%s", tag, stream->context());
1297     }
1298   }
1299 }
1300 
1301 void CodeInstaller::site_Mark(CodeBuffer& buffer, jint pc_offset, HotSpotCompiledCodeStream* stream, JVMCI_TRAPS) {
1302   u1 id = stream->read_u1("mark:id");
1303   address pc = _instructions->start() + pc_offset;
1304 
1305   if (pd_relocate(pc, id)) {
1306     return;
1307   }
1308 
1309   switch (id) {
1310     case UNVERIFIED_ENTRY:
1311       _offsets.set_value(CodeOffsets::Entry, pc_offset);
1312       break;
1313     case VERIFIED_ENTRY:
1314       _offsets.set_value(CodeOffsets::Verified_Entry, pc_offset);
1315       _offsets.set_value(CodeOffsets::Verified_Inline_Entry, pc_offset);
1316       _offsets.set_value(CodeOffsets::Verified_Inline_Entry_RO, pc_offset);
1317       break;
1318     case OSR_ENTRY:
1319       _offsets.set_value(CodeOffsets::OSR_Entry, pc_offset);
1320       break;
1321     case EXCEPTION_HANDLER_ENTRY:
1322       _offsets.set_value(CodeOffsets::Exceptions, pc_offset);
1323       break;
1324     case DEOPT_HANDLER_ENTRY:
1325       _offsets.set_value(CodeOffsets::Deopt, pc_offset);
1326       break;
1327     case DEOPT_MH_HANDLER_ENTRY:
1328       _offsets.set_value(CodeOffsets::DeoptMH, pc_offset);
1329       break;
1330     case FRAME_COMPLETE:
1331       _offsets.set_value(CodeOffsets::Frame_Complete, pc_offset);
1332       break;
1333     case ENTRY_BARRIER_PATCH:
1334       _nmethod_entry_patch_offset = pc_offset;
1335       break;
1336     case INVOKEVIRTUAL:
1337     case INVOKEINTERFACE:
1338     case INLINE_INVOKE:
1339     case INVOKESTATIC:
1340     case INVOKESPECIAL:
1341       _next_call_type = (MarkId) id;
1342       _invoke_mark_pc = pc;
1343       break;
1344     case CARD_TABLE_SHIFT:
1345     case CARD_TABLE_ADDRESS:
1346     case HEAP_TOP_ADDRESS:
1347     case HEAP_END_ADDRESS:
1348     case NARROW_KLASS_BASE_ADDRESS:
1349     case NARROW_OOP_BASE_ADDRESS:
1350     case CRC_TABLE_ADDRESS:
1351     case LOG_OF_HEAP_REGION_GRAIN_BYTES:
1352     case INLINE_CONTIGUOUS_ALLOCATION_SUPPORTED:
1353     case VERIFY_OOPS:
1354     case VERIFY_OOP_BITS:
1355     case VERIFY_OOP_MASK:
1356     case VERIFY_OOP_COUNT_ADDRESS:
1357       break;
1358 
1359     default:
1360       JVMCI_ERROR("invalid mark id: %d%s", id, stream->context());
1361       break;
1362   }
1363 }