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