1 /*
   2  * Copyright (c) 2013, 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 
  25 #include "precompiled.hpp"
  26 #include "ci/ciMethodData.hpp"
  27 #include "ci/ciReplay.hpp"
  28 #include "ci/ciSymbol.hpp"
  29 #include "ci/ciKlass.hpp"
  30 #include "ci/ciUtilities.inline.hpp"
  31 #include "classfile/javaClasses.hpp"
  32 #include "classfile/symbolTable.hpp"
  33 #include "classfile/systemDictionary.hpp"
  34 #include "compiler/compilationPolicy.hpp"
  35 #include "compiler/compileBroker.hpp"
  36 #include "compiler/compilerDefinitions.inline.hpp"
  37 #include "interpreter/linkResolver.hpp"
  38 #include "jvm.h"
  39 #include "memory/allocation.inline.hpp"
  40 #include "memory/oopFactory.hpp"
  41 #include "memory/resourceArea.hpp"
  42 #include "oops/constantPool.inline.hpp"
  43 #include "oops/cpCache.inline.hpp"
  44 #include "oops/fieldStreams.inline.hpp"
  45 #include "oops/inlineKlass.inline.hpp"
  46 #include "oops/klass.inline.hpp"
  47 #include "oops/method.inline.hpp"
  48 #include "oops/oop.inline.hpp"
  49 #include "oops/resolvedIndyEntry.hpp"
  50 #include "prims/jvmtiExport.hpp"
  51 #include "prims/methodHandles.hpp"
  52 #include "runtime/fieldDescriptor.inline.hpp"
  53 #include "runtime/globals_extension.hpp"
  54 #include "runtime/handles.inline.hpp"
  55 #include "runtime/java.hpp"
  56 #include "runtime/jniHandles.inline.hpp"
  57 #include "runtime/threads.hpp"
  58 #include "utilities/copy.hpp"
  59 #include "utilities/macros.hpp"
  60 #include "utilities/utf8.hpp"
  61 
  62 // ciReplay
  63 
  64 typedef struct _ciMethodDataRecord {
  65   const char* _klass_name;
  66   const char* _method_name;
  67   const char* _signature;
  68 
  69   int _state;
  70   int _invocation_counter;
  71 
  72   intptr_t* _data;
  73   char*     _orig_data;
  74   Klass**   _classes;
  75   Method**  _methods;
  76   int*      _classes_offsets;
  77   int*      _methods_offsets;
  78   int       _data_length;
  79   int       _orig_data_length;
  80   int       _classes_length;
  81   int       _methods_length;
  82 } ciMethodDataRecord;
  83 
  84 typedef struct _ciMethodRecord {
  85   const char* _klass_name;
  86   const char* _method_name;
  87   const char* _signature;
  88 
  89   int _instructions_size;
  90   int _interpreter_invocation_count;
  91   int _interpreter_throwout_count;
  92   int _invocation_counter;
  93   int _backedge_counter;
  94 } ciMethodRecord;
  95 
  96 typedef struct _ciInstanceKlassRecord {
  97   const InstanceKlass* _klass;
  98   jobject _java_mirror; // Global handle to java mirror to prevent unloading
  99 } ciInstanceKlassRecord;
 100 
 101 typedef struct _ciInlineRecord {
 102   const char* _klass_name;
 103   const char* _method_name;
 104   const char* _signature;
 105 
 106   int _inline_depth;
 107   int _inline_bci;
 108   bool _inline_late;
 109 } ciInlineRecord;
 110 
 111 class  CompileReplay;
 112 static CompileReplay* replay_state;
 113 
 114 class CompileReplay : public StackObj {
 115  private:
 116   FILE*   _stream;
 117   Thread* _thread;
 118   Handle  _protection_domain;
 119   bool    _protection_domain_initialized;
 120   Handle  _loader;
 121   int     _version;
 122 
 123   GrowableArray<ciMethodRecord*>     _ci_method_records;
 124   GrowableArray<ciMethodDataRecord*> _ci_method_data_records;
 125   GrowableArray<ciInstanceKlassRecord*> _ci_instance_klass_records;
 126 
 127   // Use pointer because we may need to return inline records
 128   // without destroying them.
 129   GrowableArray<ciInlineRecord*>*    _ci_inline_records;
 130 
 131   const char* _error_message;
 132 
 133   char* _bufptr;
 134   char* _buffer;
 135   int   _buffer_length;
 136 
 137   // "compile" data
 138   ciKlass* _iklass;
 139   Method*  _imethod;
 140   int      _entry_bci;
 141   int      _comp_level;
 142 
 143  public:
 144   CompileReplay(const char* filename, TRAPS) {
 145     _thread = THREAD;
 146     _loader = Handle(_thread, SystemDictionary::java_system_loader());
 147     _protection_domain = Handle();
 148     _protection_domain_initialized = false;
 149 
 150     _stream = os::fopen(filename, "rt");
 151     if (_stream == nullptr) {
 152       fprintf(stderr, "ERROR: Can't open replay file %s\n", filename);
 153     }
 154 
 155     _ci_inline_records = nullptr;
 156     _error_message = nullptr;
 157 
 158     _buffer_length = 32;
 159     _buffer = NEW_RESOURCE_ARRAY(char, _buffer_length);
 160     _bufptr = _buffer;
 161 
 162     _imethod = nullptr;
 163     _iklass  = nullptr;
 164     _entry_bci  = 0;
 165     _comp_level = 0;
 166     _version = 0;
 167 
 168     test();
 169   }
 170 
 171   ~CompileReplay() {
 172     if (_stream != nullptr) fclose(_stream);
 173   }
 174 
 175   void test() {
 176     strcpy(_buffer, "1 2 foo 4 bar 0x9 \"this is it\"");
 177     _bufptr = _buffer;
 178     assert(parse_int("test") == 1, "what");
 179     assert(parse_int("test") == 2, "what");
 180     assert(strcmp(parse_string(), "foo") == 0, "what");
 181     assert(parse_int("test") == 4, "what");
 182     assert(strcmp(parse_string(), "bar") == 0, "what");
 183     assert(parse_intptr_t("test") == 9, "what");
 184     assert(strcmp(parse_quoted_string(), "this is it") == 0, "what");
 185   }
 186 
 187   bool had_error() {
 188     return _error_message != nullptr || _thread->has_pending_exception();
 189   }
 190 
 191   bool can_replay() {
 192     return !(_stream == nullptr || had_error());
 193   }
 194 
 195   void report_error(const char* msg) {
 196     _error_message = msg;
 197   }
 198 
 199   int parse_int(const char* label) {
 200     if (had_error()) {
 201       return 0;
 202     }
 203 
 204     int v = 0;
 205     int read;
 206     if (sscanf(_bufptr, "%i%n", &v, &read) != 1) {
 207       report_error(label);
 208     } else {
 209       _bufptr += read;
 210     }
 211     return v;
 212   }
 213 
 214   intptr_t parse_intptr_t(const char* label) {
 215     if (had_error()) {
 216       return 0;
 217     }
 218 
 219     intptr_t v = 0;
 220     int read;
 221     if (sscanf(_bufptr, INTPTR_FORMAT "%n", &v, &read) != 1) {
 222       report_error(label);
 223     } else {
 224       _bufptr += read;
 225     }
 226     return v;
 227   }
 228 
 229   void skip_ws() {
 230     // Skip any leading whitespace
 231     while (*_bufptr == ' ' || *_bufptr == '\t') {
 232       _bufptr++;
 233     }
 234   }
 235 
 236   // Ignore the rest of the line
 237   void skip_remaining() {
 238     _bufptr = &_bufptr[strlen(_bufptr)]; // skip ahead to terminator
 239   }
 240 
 241   char* scan_and_terminate(char delim) {
 242     char* str = _bufptr;
 243     while (*_bufptr != delim && *_bufptr != '\0') {
 244       _bufptr++;
 245     }
 246     if (*_bufptr != '\0') {
 247       *_bufptr++ = '\0';
 248     }
 249     if (_bufptr == str) {
 250       // nothing here
 251       return nullptr;
 252     }
 253     return str;
 254   }
 255 
 256   char* parse_string() {
 257     if (had_error()) return nullptr;
 258 
 259     skip_ws();
 260     return scan_and_terminate(' ');
 261   }
 262 
 263   char* parse_quoted_string() {
 264     if (had_error()) return nullptr;
 265 
 266     skip_ws();
 267 
 268     if (*_bufptr == '"') {
 269       _bufptr++;
 270       return scan_and_terminate('"');
 271     } else {
 272       return scan_and_terminate(' ');
 273     }
 274   }
 275 
 276   char* parse_escaped_string() {
 277     char* result = parse_quoted_string();
 278     if (result != nullptr) {
 279       unescape_string(result);
 280     }
 281     return result;
 282   }
 283 
 284   // Look for the tag 'tag' followed by an
 285   bool parse_tag_and_count(const char* tag, int& length) {
 286     const char* t = parse_string();
 287     if (t == nullptr) {
 288       return false;
 289     }
 290 
 291     if (strcmp(tag, t) != 0) {
 292       report_error(tag);
 293       return false;
 294     }
 295     length = parse_int("parse_tag_and_count");
 296     return !had_error();
 297   }
 298 
 299   // Parse a sequence of raw data encoded as bytes and return the
 300   // resulting data.
 301   char* parse_data(const char* tag, int& length) {
 302     int read_size = 0;
 303     if (!parse_tag_and_count(tag, read_size)) {
 304       return nullptr;
 305     }
 306 
 307     int actual_size = sizeof(MethodData::CompilerCounters);
 308     char *result = NEW_RESOURCE_ARRAY(char, actual_size);
 309     int i = 0;
 310     if (read_size != actual_size) {
 311       tty->print_cr("Warning: ciMethodData parsing sees MethodData size %i in file, current is %i", read_size,
 312                     actual_size);
 313       // Replay serializes the entire MethodData, but the data is at the end.
 314       // If the MethodData instance size has changed, we can pad or truncate in the beginning
 315       int padding = actual_size - read_size;
 316       if (padding > 0) {
 317         // pad missing data with zeros
 318         tty->print_cr("- Padding MethodData");
 319         for (; i < padding; i++) {
 320           result[i] = 0;
 321         }
 322       } else if (padding < 0) {
 323         // drop some data
 324         tty->print_cr("- Truncating MethodData");
 325         for (int j = 0; j < -padding; j++) {
 326           int val = parse_int("data");
 327           // discard val
 328         }
 329       }
 330     }
 331 
 332     assert(i < actual_size, "At least some data must remain to be copied");
 333     for (; i < actual_size; i++) {
 334       int val = parse_int("data");
 335       result[i] = val;
 336     }
 337     length = actual_size;
 338     return result;
 339   }
 340 
 341   // Parse a standard chunk of data emitted as:
 342   //   'tag' <length> # # ...
 343   // Where each # is an intptr_t item
 344   intptr_t* parse_intptr_data(const char* tag, int& length) {
 345     if (!parse_tag_and_count(tag, length)) {
 346       return nullptr;
 347     }
 348 
 349     intptr_t* result = NEW_RESOURCE_ARRAY(intptr_t, length);
 350     for (int i = 0; i < length; i++) {
 351       skip_ws();
 352       intptr_t val = parse_intptr_t("data");
 353       result[i] = val;
 354     }
 355     return result;
 356   }
 357 
 358   // Parse a possibly quoted version of a symbol into a symbolOop
 359   Symbol* parse_symbol() {
 360     const char* str = parse_escaped_string();
 361     if (str != nullptr) {
 362       Symbol* sym = SymbolTable::new_symbol(str);
 363       return sym;
 364     }
 365     return nullptr;
 366   }
 367 
 368   bool parse_terminator() {
 369     char* terminator = parse_string();
 370     if (terminator != nullptr && strcmp(terminator, ";") == 0) {
 371       return true;
 372     }
 373     return false;
 374   }
 375 
 376   // Parse a special hidden klass location syntax
 377   // syntax: @bci <klass> <name> <signature> <bci> <location>* ;
 378   // syntax: @cpi <klass> <cpi> <location>* ;
 379   Klass* parse_cp_ref(TRAPS) {
 380     JavaThread* thread = THREAD;
 381     oop obj = nullptr;
 382     char* ref = parse_string();
 383     if (strcmp(ref, "bci") == 0) {
 384       Method* m = parse_method(CHECK_NULL);
 385       if (m == nullptr) {
 386         return nullptr;
 387       }
 388 
 389       InstanceKlass* ik = m->method_holder();
 390       const constantPoolHandle cp(Thread::current(), ik->constants());
 391 
 392       // invokedynamic or invokehandle
 393 
 394       methodHandle caller(Thread::current(), m);
 395       int bci = parse_int("bci");
 396       if (m->validate_bci(bci) != bci) {
 397         report_error("bad bci");
 398         return nullptr;
 399       }
 400 
 401       ik->link_class(CHECK_NULL);
 402 
 403       Bytecode_invoke bytecode = Bytecode_invoke_check(caller, bci);
 404       if (!Bytecodes::is_defined(bytecode.code()) || !bytecode.is_valid()) {
 405         report_error("no invoke found at bci");
 406         return nullptr;
 407       }
 408       bytecode.verify();
 409       int index = bytecode.index();
 410 
 411       ConstantPoolCacheEntry* cp_cache_entry = nullptr;
 412       CallInfo callInfo;
 413       Bytecodes::Code bc = bytecode.invoke_code();
 414       LinkResolver::resolve_invoke(callInfo, Handle(), cp, index, bc, CHECK_NULL);
 415 
 416       // ResolvedIndyEntry and ConstantPoolCacheEntry must currently coexist.
 417       // To address this, the variables below contain the values that *might*
 418       // be used to avoid multiple blocks of similar code. When CPCE is obsoleted
 419       // these can be removed
 420       oop appendix = nullptr;
 421       Method* adapter_method = nullptr;
 422       int pool_index = 0;
 423 
 424       if (bytecode.is_invokedynamic()) {
 425         index = cp->decode_invokedynamic_index(index);
 426         cp->cache()->set_dynamic_call(callInfo, index);
 427 
 428         appendix = cp->resolved_reference_from_indy(index);
 429         adapter_method = cp->resolved_indy_entry_at(index)->method();
 430         pool_index = cp->resolved_indy_entry_at(index)->constant_pool_index();
 431       } else if (bytecode.is_invokehandle()) {
 432 #ifdef ASSERT
 433         Klass* holder = cp->klass_ref_at(index, bytecode.code(), CHECK_NULL);
 434         Symbol* name = cp->name_ref_at(index, bytecode.code());
 435         assert(MethodHandles::is_signature_polymorphic_name(holder, name), "");
 436 #endif
 437         cp_cache_entry = cp->cache()->entry_at(cp->decode_cpcache_index(index));
 438         cp_cache_entry->set_method_handle(cp, callInfo);
 439 
 440         appendix = cp_cache_entry->appendix_if_resolved(cp);
 441         adapter_method = cp_cache_entry->f1_as_method();
 442         pool_index = cp_cache_entry->constant_pool_index();
 443       } else {
 444         report_error("no dynamic invoke found");
 445         return nullptr;
 446       }
 447       char* dyno_ref = parse_string();
 448       if (strcmp(dyno_ref, "<appendix>") == 0) {
 449         obj = appendix;
 450       } else if (strcmp(dyno_ref, "<adapter>") == 0) {
 451         if (!parse_terminator()) {
 452           report_error("no dynamic invoke found");
 453           return nullptr;
 454         }
 455         Method* adapter = adapter_method;
 456         if (adapter == nullptr) {
 457           report_error("no adapter found");
 458           return nullptr;
 459         }
 460         return adapter->method_holder();
 461       } else if (strcmp(dyno_ref, "<bsm>") == 0) {
 462         BootstrapInfo bootstrap_specifier(cp, pool_index, index);
 463         obj = cp->resolve_possibly_cached_constant_at(bootstrap_specifier.bsm_index(), CHECK_NULL);
 464       } else {
 465         report_error("unrecognized token");
 466         return nullptr;
 467       }
 468     } else {
 469       // constant pool ref (MethodHandle)
 470       if (strcmp(ref, "cpi") != 0) {
 471         report_error("unexpected token");
 472         return nullptr;
 473       }
 474 
 475       Klass* k = parse_klass(CHECK_NULL);
 476       if (k == nullptr) {
 477         return nullptr;
 478       }
 479       InstanceKlass* ik = InstanceKlass::cast(k);
 480       const constantPoolHandle cp(Thread::current(), ik->constants());
 481 
 482       int cpi = parse_int("cpi");
 483 
 484       if (cpi >= cp->length()) {
 485         report_error("bad cpi");
 486         return nullptr;
 487       }
 488       if (!cp->tag_at(cpi).is_method_handle()) {
 489         report_error("no method handle found at cpi");
 490         return nullptr;
 491       }
 492       ik->link_class(CHECK_NULL);
 493       obj = cp->resolve_possibly_cached_constant_at(cpi, CHECK_NULL);
 494     }
 495     if (obj == nullptr) {
 496       report_error("null cp object found");
 497       return nullptr;
 498     }
 499     Klass* k = nullptr;
 500     skip_ws();
 501     // loop: read fields
 502     char* field = nullptr;
 503     do {
 504       field = parse_string();
 505       if (field == nullptr) {
 506         report_error("no field found");
 507         return nullptr;
 508       }
 509       if (strcmp(field, ";") == 0) {
 510         break;
 511       }
 512       // raw Method*
 513       if (strcmp(field, "<vmtarget>") == 0) {
 514         Method* vmtarget = java_lang_invoke_MemberName::vmtarget(obj);
 515         k = (vmtarget == nullptr) ? nullptr : vmtarget->method_holder();
 516         if (k == nullptr) {
 517           report_error("null vmtarget found");
 518           return nullptr;
 519         }
 520         if (!parse_terminator()) {
 521           report_error("missing terminator");
 522           return nullptr;
 523         }
 524         return k;
 525       }
 526       obj = ciReplay::obj_field(obj, field);
 527       // array
 528       if (obj != nullptr && obj->is_objArray()) {
 529         objArrayOop arr = (objArrayOop)obj;
 530         int index = parse_int("index");
 531         if (index >= arr->length()) {
 532           report_error("bad array index");
 533           return nullptr;
 534         }
 535         obj = arr->obj_at(index);
 536       }
 537     } while (obj != nullptr);
 538     if (obj == nullptr) {
 539       report_error("null field found");
 540       return nullptr;
 541     }
 542     k = obj->klass();
 543     return k;
 544   }
 545 
 546   // Parse a valid klass name and look it up
 547   // syntax: <name>
 548   // syntax: <constant pool ref>
 549   Klass* parse_klass(TRAPS) {
 550     skip_ws();
 551     // check for constant pool object reference (for a dynamic/hidden class)
 552     bool cp_ref = (*_bufptr == '@');
 553     if (cp_ref) {
 554       ++_bufptr;
 555       Klass* k = parse_cp_ref(CHECK_NULL);
 556       if (k != nullptr && !k->is_hidden()) {
 557         report_error("expected hidden class");
 558         return nullptr;
 559       }
 560       return k;
 561     }
 562     char* str = parse_escaped_string();
 563     Symbol* klass_name = SymbolTable::new_symbol(str);
 564     if (klass_name != nullptr) {
 565       Klass* k = nullptr;
 566       if (_iklass != nullptr) {
 567         k = (Klass*)_iklass->find_klass(ciSymbol::make(klass_name->as_C_string()))->constant_encoding();
 568       } else {
 569         k = SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD);
 570       }
 571       if (HAS_PENDING_EXCEPTION) {
 572         oop throwable = PENDING_EXCEPTION;
 573         java_lang_Throwable::print(throwable, tty);
 574         tty->cr();
 575         report_error(str);
 576         if (ReplayIgnoreInitErrors) {
 577           CLEAR_PENDING_EXCEPTION;
 578           _error_message = nullptr;
 579         }
 580         return nullptr;
 581       }
 582       return k;
 583     }
 584     return nullptr;
 585   }
 586 
 587   // Lookup a klass
 588   Klass* resolve_klass(const char* klass, TRAPS) {
 589     Symbol* klass_name = SymbolTable::new_symbol(klass);
 590     return SystemDictionary::resolve_or_fail(klass_name, _loader, _protection_domain, true, THREAD);
 591   }
 592 
 593   // Parse the standard tuple of <klass> <name> <signature>
 594   Method* parse_method(TRAPS) {
 595     InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK_NULL);
 596     if (k == nullptr) {
 597       report_error("Can't find holder klass");
 598       return nullptr;
 599     }
 600     Symbol* method_name = parse_symbol();
 601     Symbol* method_signature = parse_symbol();
 602     Method* m = k->find_method(method_name, method_signature);
 603     if (m == nullptr) {
 604       report_error("Can't find method");
 605     }
 606     return m;
 607   }
 608 
 609   int get_line(int c) {
 610     int buffer_pos = 0;
 611     while(c != EOF) {
 612       if (buffer_pos + 1 >= _buffer_length) {
 613         int new_length = _buffer_length * 2;
 614         // Next call will throw error in case of OOM.
 615         _buffer = REALLOC_RESOURCE_ARRAY(char, _buffer, _buffer_length, new_length);
 616         _buffer_length = new_length;
 617       }
 618       if (c == '\n') {
 619         c = getc(_stream); // get next char
 620         break;
 621       } else if (c == '\r') {
 622         // skip LF
 623       } else {
 624         _buffer[buffer_pos++] = c;
 625       }
 626       c = getc(_stream);
 627     }
 628     // null terminate it, reset the pointer
 629     _buffer[buffer_pos] = '\0'; // NL or EOF
 630     _bufptr = _buffer;
 631     return c;
 632   }
 633 
 634   // Process each line of the replay file executing each command until
 635   // the file ends.
 636   void process(TRAPS) {
 637     int line_no = 1;
 638     int c = getc(_stream);
 639     while(c != EOF) {
 640       c = get_line(c);
 641       process_command(false, THREAD);
 642       if (had_error()) {
 643         int pos = _bufptr - _buffer + 1;
 644         tty->print_cr("Error while parsing line %d at position %d: %s\n", line_no, pos, _error_message);
 645         if (ReplayIgnoreInitErrors) {
 646           CLEAR_PENDING_EXCEPTION;
 647           _error_message = nullptr;
 648         } else {
 649           return;
 650         }
 651       }
 652       line_no++;
 653     }
 654     reset();
 655   }
 656 
 657   void process_command(bool is_replay_inline, TRAPS) {
 658     char* cmd = parse_string();
 659     if (cmd == nullptr) {
 660       return;
 661     }
 662     if (strcmp("#", cmd) == 0) {
 663       // comment line, print or ignore
 664       if (Verbose) {
 665         tty->print_cr("# %s", _bufptr);
 666       }
 667       skip_remaining();
 668     } else if (strcmp("version", cmd) == 0) {
 669       _version = parse_int("version");
 670       if (_version < 0 || _version > REPLAY_VERSION) {
 671         tty->print_cr("# unrecognized version %d, expected 0 <= version <= %d", _version, REPLAY_VERSION);
 672       }
 673     } else if (strcmp("compile", cmd) == 0) {
 674       process_compile(CHECK);
 675     } else if (!is_replay_inline) {
 676       if (strcmp("ciMethod", cmd) == 0) {
 677         process_ciMethod(CHECK);
 678       } else if (strcmp("ciMethodData", cmd) == 0) {
 679         process_ciMethodData(CHECK);
 680       } else if (strcmp("staticfield", cmd) == 0) {
 681         process_staticfield(CHECK);
 682       } else if (strcmp("ciInstanceKlass", cmd) == 0) {
 683         process_ciInstanceKlass(CHECK);
 684       } else if (strcmp("instanceKlass", cmd) == 0) {
 685         process_instanceKlass(CHECK);
 686 #if INCLUDE_JVMTI
 687       } else if (strcmp("JvmtiExport", cmd) == 0) {
 688         process_JvmtiExport(CHECK);
 689 #endif // INCLUDE_JVMTI
 690       } else {
 691         report_error("unknown command");
 692       }
 693     } else {
 694       report_error("unknown command");
 695     }
 696     if (!had_error() && *_bufptr != '\0') {
 697       report_error("line not properly terminated");
 698     }
 699   }
 700 
 701   // validation of comp_level
 702   bool is_valid_comp_level(int comp_level) {
 703     const int msg_len = 256;
 704     char* msg = nullptr;
 705     if (!is_compile(comp_level)) {
 706       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 707       jio_snprintf(msg, msg_len, "%d isn't compilation level", comp_level);
 708     } else if (is_c1_compile(comp_level) && !CompilerConfig::is_c1_enabled()) {
 709       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 710       jio_snprintf(msg, msg_len, "compilation level %d requires C1", comp_level);
 711     } else if (is_c2_compile(comp_level) && !CompilerConfig::is_c2_enabled()) {
 712       msg = NEW_RESOURCE_ARRAY(char, msg_len);
 713       jio_snprintf(msg, msg_len, "compilation level %d requires C2", comp_level);
 714     }
 715     if (msg != nullptr) {
 716       report_error(msg);
 717       return false;
 718     }
 719     return true;
 720   }
 721 
 722   // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> (<depth> <bci> <klass> <name> <signature>)*
 723   void* process_inline(ciMethod* imethod, Method* m, int entry_bci, int comp_level, TRAPS) {
 724     _imethod    = m;
 725     _iklass     = imethod->holder();
 726     _entry_bci  = entry_bci;
 727     _comp_level = comp_level;
 728     int line_no = 1;
 729     int c = getc(_stream);
 730     while(c != EOF) {
 731       c = get_line(c);
 732       process_command(true, CHECK_NULL);
 733       if (had_error()) {
 734         tty->print_cr("Error while parsing line %d: %s\n", line_no, _error_message);
 735         tty->print_cr("%s", _buffer);
 736         return nullptr;
 737       }
 738       if (_ci_inline_records != nullptr && _ci_inline_records->length() > 0) {
 739         // Found inlining record for the requested method.
 740         return _ci_inline_records;
 741       }
 742       line_no++;
 743     }
 744     return nullptr;
 745   }
 746 
 747   // compile <klass> <name> <signature> <entry_bci> <comp_level> inline <count> (<depth> <bci> <inline_late> <klass> <name> <signature>)*
 748   void process_compile(TRAPS) {
 749     Method* method = parse_method(CHECK);
 750     if (had_error()) return;
 751     int entry_bci = parse_int("entry_bci");
 752     int comp_level = parse_int("comp_level");
 753     if (!is_valid_comp_level(comp_level)) {
 754       return;
 755     }
 756     if (_imethod != nullptr) {
 757       // Replay Inlining
 758       if (entry_bci != _entry_bci || comp_level != _comp_level) {
 759         return;
 760       }
 761       const char* iklass_name  = _imethod->method_holder()->name()->as_utf8();
 762       const char* imethod_name = _imethod->name()->as_utf8();
 763       const char* isignature   = _imethod->signature()->as_utf8();
 764       const char* klass_name   = method->method_holder()->name()->as_utf8();
 765       const char* method_name  = method->name()->as_utf8();
 766       const char* signature    = method->signature()->as_utf8();
 767       if (strcmp(iklass_name,  klass_name)  != 0 ||
 768           strcmp(imethod_name, method_name) != 0 ||
 769           strcmp(isignature,   signature)   != 0) {
 770         return;
 771       }
 772     }
 773     int inline_count = 0;
 774     if (parse_tag_and_count("inline", inline_count)) {
 775       // Record inlining data
 776       _ci_inline_records = new GrowableArray<ciInlineRecord*>();
 777       for (int i = 0; i < inline_count; i++) {
 778         int depth = parse_int("inline_depth");
 779         int bci = parse_int("inline_bci");
 780         if (had_error()) {
 781           break;
 782         }
 783         int inline_late = 0;
 784         if (_version >= 2) {
 785           inline_late = parse_int("inline_late");
 786           if (had_error()) {
 787               break;
 788           }
 789         }
 790 
 791         Method* inl_method = parse_method(CHECK);
 792         if (had_error()) {
 793           break;
 794         }
 795         new_ciInlineRecord(inl_method, bci, depth, inline_late);
 796       }
 797     }
 798     if (_imethod != nullptr) {
 799       return; // Replay Inlining
 800     }
 801     InstanceKlass* ik = method->method_holder();
 802     ik->initialize(THREAD);
 803     if (HAS_PENDING_EXCEPTION) {
 804       oop throwable = PENDING_EXCEPTION;
 805       java_lang_Throwable::print(throwable, tty);
 806       tty->cr();
 807       if (ReplayIgnoreInitErrors) {
 808         CLEAR_PENDING_EXCEPTION;
 809         ik->set_init_state(InstanceKlass::fully_initialized);
 810       } else {
 811         return;
 812       }
 813     }
 814     // Make sure the existence of a prior compile doesn't stop this one
 815     CompiledMethod* nm = (entry_bci != InvocationEntryBci) ? method->lookup_osr_nmethod_for(entry_bci, comp_level, true) : method->code();
 816     if (nm != nullptr) {
 817       nm->make_not_entrant();
 818     }
 819     replay_state = this;
 820     CompileBroker::compile_method(methodHandle(THREAD, method), entry_bci, comp_level,
 821                                   methodHandle(), 0, CompileTask::Reason_Replay, THREAD);
 822     replay_state = nullptr;
 823   }
 824 
 825   // ciMethod <klass> <name> <signature> <invocation_counter> <backedge_counter> <interpreter_invocation_count> <interpreter_throwout_count> <instructions_size>
 826   void process_ciMethod(TRAPS) {
 827     Method* method = parse_method(CHECK);
 828     if (had_error()) return;
 829     ciMethodRecord* rec = new_ciMethod(method);
 830     rec->_invocation_counter = parse_int("invocation_counter");
 831     rec->_backedge_counter = parse_int("backedge_counter");
 832     rec->_interpreter_invocation_count = parse_int("interpreter_invocation_count");
 833     rec->_interpreter_throwout_count = parse_int("interpreter_throwout_count");
 834     rec->_instructions_size = parse_int("instructions_size");
 835   }
 836 
 837   // ciMethodData <klass> <name> <signature> <state> <invocation_counter> orig <length> <byte>* data <length> <ptr>* oops <length> (<offset> <klass>)* methods <length> (<offset> <klass> <name> <signature>)*
 838   void process_ciMethodData(TRAPS) {
 839     Method* method = parse_method(CHECK);
 840     if (had_error()) return;
 841     /* just copied from Method, to build interpret data*/
 842 
 843     // To be properly initialized, some profiling in the MDO needs the
 844     // method to be rewritten (number of arguments at a call for instance)
 845     method->method_holder()->link_class(CHECK);
 846     assert(method->method_data() == nullptr, "Should only be initialized once");
 847     ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
 848     MethodData* method_data = MethodData::allocate(loader_data, methodHandle(THREAD, method), CHECK);
 849     method->set_method_data(method_data);
 850 
 851     // collect and record all the needed information for later
 852     ciMethodDataRecord* rec = new_ciMethodData(method);
 853     rec->_state = parse_int("state");
 854     if (_version < 1) {
 855       parse_int("current_mileage");
 856     } else {
 857       rec->_invocation_counter = parse_int("invocation_counter");
 858     }
 859 
 860     rec->_orig_data = parse_data("orig", rec->_orig_data_length);
 861     if (rec->_orig_data == nullptr) {
 862       return;
 863     }
 864     rec->_data = parse_intptr_data("data", rec->_data_length);
 865     if (rec->_data == nullptr) {
 866       return;
 867     }
 868     if (!parse_tag_and_count("oops", rec->_classes_length)) {
 869       return;
 870     }
 871     rec->_classes = NEW_RESOURCE_ARRAY(Klass*, rec->_classes_length);
 872     rec->_classes_offsets = NEW_RESOURCE_ARRAY(int, rec->_classes_length);
 873     for (int i = 0; i < rec->_classes_length; i++) {
 874       int offset = parse_int("offset");
 875       if (had_error()) {
 876         return;
 877       }
 878       Klass* k = parse_klass(CHECK);
 879       rec->_classes_offsets[i] = offset;
 880       rec->_classes[i] = k;
 881     }
 882 
 883     if (!parse_tag_and_count("methods", rec->_methods_length)) {
 884       return;
 885     }
 886     rec->_methods = NEW_RESOURCE_ARRAY(Method*, rec->_methods_length);
 887     rec->_methods_offsets = NEW_RESOURCE_ARRAY(int, rec->_methods_length);
 888     for (int i = 0; i < rec->_methods_length; i++) {
 889       int offset = parse_int("offset");
 890       if (had_error()) {
 891         return;
 892       }
 893       Method* m = parse_method(CHECK);
 894       rec->_methods_offsets[i] = offset;
 895       rec->_methods[i] = m;
 896     }
 897   }
 898 
 899   // instanceKlass <name>
 900   // instanceKlass <constant pool ref> # <original hidden class name>
 901   //
 902   // Loads and initializes the klass 'name'.  This can be used to
 903   // create particular class loading environments
 904   void process_instanceKlass(TRAPS) {
 905     // just load the referenced class
 906     Klass* k = parse_klass(CHECK);
 907 
 908     if (_version >= 1) {
 909       if (!_protection_domain_initialized && k != nullptr) {
 910         assert(_protection_domain() == nullptr, "must be uninitialized");
 911         // The first entry is the holder class of the method for which a replay compilation is requested.
 912         // Use the same protection domain to load all subsequent classes in order to resolve all classes
 913         // in signatures of inlinees. This ensures that inlining can be done as stated in the replay file.
 914         _protection_domain = Handle(_thread, k->protection_domain());
 915       }
 916 
 917       _protection_domain_initialized = true;
 918     }
 919 
 920     if (k == nullptr) {
 921       return;
 922     }
 923     const char* comment = parse_string();
 924     bool is_comment = comment != nullptr && strcmp(comment, "#") == 0;
 925     if (k->is_hidden() != is_comment) {
 926       report_error("hidden class with comment expected");
 927       return;
 928     }
 929     // comment, print or ignore
 930     if (is_comment) {
 931       if (Verbose) {
 932         const char* hidden = parse_string();
 933         tty->print_cr("Found %s for %s", k->name()->as_quoted_ascii(), hidden);
 934       }
 935       skip_remaining();
 936     }
 937   }
 938 
 939   // ciInstanceKlass <name> <is_linked> <is_initialized> <length> tag*
 940   //
 941   // Load the klass 'name' and link or initialize it.  Verify that the
 942   // constant pool is the same length as 'length' and make sure the
 943   // constant pool tags are in the same state.
 944   void process_ciInstanceKlass(TRAPS) {
 945     InstanceKlass* k = (InstanceKlass*)parse_klass(CHECK);
 946     if (k == nullptr) {
 947       skip_remaining();
 948       return;
 949     }
 950     int is_linked = parse_int("is_linked");
 951     int is_initialized = parse_int("is_initialized");
 952     int length = parse_int("length");
 953     if (is_initialized) {
 954       k->initialize(THREAD);
 955       if (HAS_PENDING_EXCEPTION) {
 956         oop throwable = PENDING_EXCEPTION;
 957         java_lang_Throwable::print(throwable, tty);
 958         tty->cr();
 959         if (ReplayIgnoreInitErrors) {
 960           CLEAR_PENDING_EXCEPTION;
 961           k->set_init_state(InstanceKlass::fully_initialized);
 962         } else {
 963           return;
 964         }
 965       }
 966     } else if (is_linked) {
 967       k->link_class(CHECK);
 968     }
 969     new_ciInstanceKlass(k);
 970     ConstantPool* cp = k->constants();
 971     if (length != cp->length()) {
 972       report_error("constant pool length mismatch: wrong class files?");
 973       return;
 974     }
 975 
 976     int parsed_two_word = 0;
 977     for (int i = 1; i < length; i++) {
 978       int tag = parse_int("tag");
 979       if (had_error()) {
 980         return;
 981       }
 982       switch (cp->tag_at(i).value()) {
 983         case JVM_CONSTANT_UnresolvedClass: {
 984           if (tag == JVM_CONSTANT_Class) {
 985             tty->print_cr("Resolving klass %s at %d", cp->klass_name_at(i)->as_utf8(), i);
 986             Klass* k = cp->klass_at(i, CHECK);
 987           }
 988           break;
 989         }
 990 
 991         case JVM_CONSTANT_Long:
 992         case JVM_CONSTANT_Double:
 993           parsed_two_word = i + 1;
 994 
 995         case JVM_CONSTANT_ClassIndex:
 996         case JVM_CONSTANT_StringIndex:
 997         case JVM_CONSTANT_String:
 998         case JVM_CONSTANT_UnresolvedClassInError:
 999         case JVM_CONSTANT_Fieldref:
1000         case JVM_CONSTANT_Methodref:
1001         case JVM_CONSTANT_InterfaceMethodref:
1002         case JVM_CONSTANT_NameAndType:
1003         case JVM_CONSTANT_Utf8:
1004         case JVM_CONSTANT_Integer:
1005         case JVM_CONSTANT_Float:
1006         case JVM_CONSTANT_MethodHandle:
1007         case JVM_CONSTANT_MethodType:
1008         case JVM_CONSTANT_Dynamic:
1009         case JVM_CONSTANT_InvokeDynamic:
1010           if (tag != cp->tag_at(i).value()) {
1011             report_error("tag mismatch: wrong class files?");
1012             return;
1013           }
1014           break;
1015 
1016         case JVM_CONSTANT_Class:
1017           if (tag == JVM_CONSTANT_UnresolvedClass) {
1018             Klass* k = cp->klass_at(i, CHECK);
1019             tty->print_cr("Warning: entry was unresolved in the replay data: %s", k->name()->as_utf8());
1020           } else if (tag != JVM_CONSTANT_Class) {
1021             report_error("Unexpected tag");
1022             return;
1023           }
1024           break;
1025 
1026         case 0:
1027           if (parsed_two_word == i) continue;
1028 
1029         default:
1030           fatal("Unexpected tag: %d", cp->tag_at(i).value());
1031           break;
1032       }
1033 
1034     }
1035   }
1036 
1037   class InlineTypeFieldInitializer : public FieldClosure {
1038     oop _vt;
1039     CompileReplay* _replay;
1040   public:
1041     InlineTypeFieldInitializer(oop vt, CompileReplay* replay)
1042   : _vt(vt), _replay(replay) {}
1043 
1044     void do_field(fieldDescriptor* fd) {
1045       BasicType bt = fd->field_type();
1046       const char* string_value = fd->is_null_free_inline_type() ? nullptr : _replay->parse_escaped_string();
1047       switch (bt) {
1048       case T_BYTE: {
1049         int value = atoi(string_value);
1050         _vt->byte_field_put(fd->offset(), value);
1051         break;
1052       }
1053       case T_BOOLEAN: {
1054         int value = atoi(string_value);
1055         _vt->bool_field_put(fd->offset(), value);
1056         break;
1057       }
1058       case T_SHORT: {
1059         int value = atoi(string_value);
1060         _vt->short_field_put(fd->offset(), value);
1061         break;
1062       }
1063       case T_CHAR: {
1064         int value = atoi(string_value);
1065         _vt->char_field_put(fd->offset(), value);
1066         break;
1067       }
1068       case T_INT: {
1069         int value = atoi(string_value);
1070         _vt->int_field_put(fd->offset(), value);
1071         break;
1072       }
1073       case T_LONG: {
1074         jlong value;
1075         if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
1076           fprintf(stderr, "Error parsing long: %s\n", string_value);
1077           break;
1078         }
1079         _vt->long_field_put(fd->offset(), value);
1080         break;
1081       }
1082       case T_FLOAT: {
1083         float value = atof(string_value);
1084         _vt->float_field_put(fd->offset(), value);
1085         break;
1086       }
1087       case T_DOUBLE: {
1088         double value = atof(string_value);
1089         _vt->double_field_put(fd->offset(), value);
1090         break;
1091       }
1092       case T_ARRAY:
1093       case T_OBJECT:
1094       case T_PRIMITIVE_OBJECT:
1095         if (!fd->is_null_free_inline_type()) {
1096           JavaThread* THREAD = JavaThread::current();
1097           bool res = _replay->process_staticfield_reference(string_value, _vt, fd, THREAD);
1098           assert(res, "should succeed for arrays & objects");
1099           break;
1100         } else {
1101           InlineKlass* vk = InlineKlass::cast(fd->field_holder()->get_inline_type_field_klass(fd->index()));
1102           if (fd->is_flat()) {
1103             int field_offset = fd->offset() - vk->first_field_offset();
1104             oop obj = cast_to_oop(cast_from_oop<address>(_vt) + field_offset);
1105             InlineTypeFieldInitializer init_fields(obj, _replay);
1106             vk->do_nonstatic_fields(&init_fields);
1107           } else {
1108             oop value = vk->allocate_instance(JavaThread::current());
1109             _vt->obj_field_put(fd->offset(), value);
1110           }
1111           break;
1112         }
1113       default: {
1114         fatal("Unhandled type: %s", type2name(bt));
1115       }
1116       }
1117     }
1118   };
1119 
1120   bool process_staticfield_reference(const char* field_signature, oop java_mirror, fieldDescriptor* fd, TRAPS) {
1121     if (field_signature[0] == JVM_SIGNATURE_ARRAY) {
1122       int length = parse_int("array length");
1123       oop value = nullptr;
1124 
1125       if (field_signature[1] == JVM_SIGNATURE_ARRAY) {
1126         // multi dimensional array
1127         Klass* k = resolve_klass(field_signature, CHECK_(true));
1128         ArrayKlass* kelem = (ArrayKlass *)k;
1129         int rank = 0;
1130         while (field_signature[rank] == JVM_SIGNATURE_ARRAY) {
1131           rank++;
1132         }
1133         jint* dims = NEW_RESOURCE_ARRAY(jint, rank);
1134         dims[0] = length;
1135         for (int i = 1; i < rank; i++) {
1136           dims[i] = 1; // These aren't relevant to the compiler
1137         }
1138         value = kelem->multi_allocate(rank, dims, CHECK_(true));
1139       } else {
1140         if (strcmp(field_signature, "[B") == 0) {
1141           value = oopFactory::new_byteArray(length, CHECK_(true));
1142         } else if (strcmp(field_signature, "[Z") == 0) {
1143           value = oopFactory::new_boolArray(length, CHECK_(true));
1144         } else if (strcmp(field_signature, "[C") == 0) {
1145           value = oopFactory::new_charArray(length, CHECK_(true));
1146         } else if (strcmp(field_signature, "[S") == 0) {
1147           value = oopFactory::new_shortArray(length, CHECK_(true));
1148         } else if (strcmp(field_signature, "[F") == 0) {
1149           value = oopFactory::new_floatArray(length, CHECK_(true));
1150         } else if (strcmp(field_signature, "[D") == 0) {
1151           value = oopFactory::new_doubleArray(length, CHECK_(true));
1152         } else if (strcmp(field_signature, "[I") == 0) {
1153           value = oopFactory::new_intArray(length, CHECK_(true));
1154         } else if (strcmp(field_signature, "[J") == 0) {
1155           value = oopFactory::new_longArray(length, CHECK_(true));
1156         } else if (field_signature[0] == JVM_SIGNATURE_ARRAY &&
1157                    field_signature[1] == JVM_SIGNATURE_CLASS) {
1158           Klass* kelem = resolve_klass(field_signature + 1, CHECK_(true));
1159           parse_klass(CHECK_(true)); // eat up the array class name
1160           value = oopFactory::new_objArray(kelem, length, CHECK_(true));
1161         } else if (field_signature[0] == JVM_SIGNATURE_ARRAY &&
1162                    field_signature[1] == JVM_SIGNATURE_PRIMITIVE_OBJECT) {
1163           Klass* kelem = resolve_klass(field_signature + 1, CHECK_(true));
1164           parse_klass(CHECK_(true)); // eat up the array class name
1165           value = oopFactory::new_valueArray(kelem, length, CHECK_(true));
1166         } else {
1167           report_error("unhandled array staticfield");
1168         }
1169       }
1170       java_mirror->obj_field_put(fd->offset(), value);
1171       return true;
1172     } else if (strcmp(field_signature, "Ljava/lang/String;") == 0) {
1173       const char* string_value = parse_escaped_string();
1174       Handle value = java_lang_String::create_from_str(string_value, CHECK_(true));
1175       java_mirror->obj_field_put(fd->offset(), value());
1176       return true;
1177     } else if (field_signature[0] == 'L') {
1178       const char* instance = parse_escaped_string();
1179       Klass* k = resolve_klass(instance, CHECK_(true));
1180       oop value = InstanceKlass::cast(k)->allocate_instance(CHECK_(true));
1181       java_mirror->obj_field_put(fd->offset(), value);
1182       return true;
1183     }
1184     return false;
1185   }
1186 
1187   // Initialize a class and fill in the value for a static field.
1188   // This is useful when the compile was dependent on the value of
1189   // static fields but it's impossible to properly rerun the static
1190   // initializer.
1191   void process_staticfield(TRAPS) {
1192     InstanceKlass* k = (InstanceKlass *)parse_klass(CHECK);
1193 
1194     if (k == nullptr || ReplaySuppressInitializers == 0 ||
1195         (ReplaySuppressInitializers == 2 && k->class_loader() == nullptr)) {
1196         skip_remaining();
1197       return;
1198     }
1199 
1200     assert(k->is_initialized(), "must be");
1201 
1202     const char* field_name = parse_escaped_string();
1203     const char* field_signature = parse_string();
1204     fieldDescriptor fd;
1205     Symbol* name = SymbolTable::new_symbol(field_name);
1206     Symbol* sig = SymbolTable::new_symbol(field_signature);
1207     if (!k->find_local_field(name, sig, &fd) ||
1208         !fd.is_static() ||
1209         fd.has_initial_value()) {
1210       report_error(field_name);
1211       return;
1212     }
1213 
1214     oop java_mirror = k->java_mirror();
1215     if (strcmp(field_signature, "I") == 0) {
1216       const char* string_value = parse_escaped_string();
1217       int value = atoi(string_value);
1218       java_mirror->int_field_put(fd.offset(), value);
1219     } else if (strcmp(field_signature, "B") == 0) {
1220       const char* string_value = parse_escaped_string();
1221       int value = atoi(string_value);
1222       java_mirror->byte_field_put(fd.offset(), value);
1223     } else if (strcmp(field_signature, "C") == 0) {
1224       const char* string_value = parse_escaped_string();
1225       int value = atoi(string_value);
1226       java_mirror->char_field_put(fd.offset(), value);
1227     } else if (strcmp(field_signature, "S") == 0) {
1228       const char* string_value = parse_escaped_string();
1229       int value = atoi(string_value);
1230       java_mirror->short_field_put(fd.offset(), value);
1231     } else if (strcmp(field_signature, "Z") == 0) {
1232       const char* string_value = parse_escaped_string();
1233       int value = atoi(string_value);
1234       java_mirror->bool_field_put(fd.offset(), value);
1235     } else if (strcmp(field_signature, "J") == 0) {
1236       const char* string_value = parse_escaped_string();
1237       jlong value;
1238       if (sscanf(string_value, JLONG_FORMAT, &value) != 1) {
1239         fprintf(stderr, "Error parsing long: %s\n", string_value);
1240         return;
1241       }
1242       java_mirror->long_field_put(fd.offset(), value);
1243     } else if (strcmp(field_signature, "F") == 0) {
1244       const char* string_value = parse_escaped_string();
1245       float value = atof(string_value);
1246       java_mirror->float_field_put(fd.offset(), value);
1247     } else if (strcmp(field_signature, "D") == 0) {
1248       const char* string_value = parse_escaped_string();
1249       double value = atof(string_value);
1250       java_mirror->double_field_put(fd.offset(), value);
1251     } else if (field_signature[0] == JVM_SIGNATURE_PRIMITIVE_OBJECT) {
1252       Klass* kelem = resolve_klass(field_signature, CHECK);
1253       InlineKlass* vk = InlineKlass::cast(kelem);
1254       oop value = vk->allocate_instance(CHECK);
1255       InlineTypeFieldInitializer init_fields(value, this);
1256       vk->do_nonstatic_fields(&init_fields);
1257       java_mirror->obj_field_put(fd.offset(), value);
1258     } else {
1259       bool res = process_staticfield_reference(field_signature, java_mirror, &fd, CHECK);
1260       if (!res)  {
1261         report_error("unhandled staticfield");
1262       }
1263     }
1264   }
1265 
1266 #if INCLUDE_JVMTI
1267   // JvmtiExport <field> <value>
1268   void process_JvmtiExport(TRAPS) {
1269     const char* field = parse_string();
1270     bool value = parse_int("JvmtiExport flag") != 0;
1271     if (strcmp(field, "can_access_local_variables") == 0) {
1272       JvmtiExport::set_can_access_local_variables(value);
1273     } else if (strcmp(field, "can_hotswap_or_post_breakpoint") == 0) {
1274       JvmtiExport::set_can_hotswap_or_post_breakpoint(value);
1275     } else if (strcmp(field, "can_post_on_exceptions") == 0) {
1276       JvmtiExport::set_can_post_on_exceptions(value);
1277     } else {
1278       report_error("Unrecognized JvmtiExport directive");
1279     }
1280   }
1281 #endif // INCLUDE_JVMTI
1282 
1283   // Create and initialize a record for a ciMethod
1284   ciMethodRecord* new_ciMethod(Method* method) {
1285     ciMethodRecord* rec = NEW_RESOURCE_OBJ(ciMethodRecord);
1286     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1287     rec->_method_name = method->name()->as_utf8();
1288     rec->_signature = method->signature()->as_utf8();
1289     _ci_method_records.append(rec);
1290     return rec;
1291   }
1292 
1293   // Lookup data for a ciMethod
1294   ciMethodRecord* find_ciMethodRecord(Method* method) {
1295     const char* klass_name =  method->method_holder()->name()->as_utf8();
1296     const char* method_name = method->name()->as_utf8();
1297     const char* signature = method->signature()->as_utf8();
1298     for (int i = 0; i < _ci_method_records.length(); i++) {
1299       ciMethodRecord* rec = _ci_method_records.at(i);
1300       if (strcmp(rec->_klass_name, klass_name) == 0 &&
1301           strcmp(rec->_method_name, method_name) == 0 &&
1302           strcmp(rec->_signature, signature) == 0) {
1303         return rec;
1304       }
1305     }
1306     return nullptr;
1307   }
1308 
1309   // Create and initialize a record for a ciInstanceKlass which was present at replay dump time.
1310   void new_ciInstanceKlass(const InstanceKlass* klass) {
1311     ciInstanceKlassRecord* rec = NEW_RESOURCE_OBJ(ciInstanceKlassRecord);
1312     rec->_klass = klass;
1313     oop java_mirror = klass->java_mirror();
1314     Handle h_java_mirror(_thread, java_mirror);
1315     rec->_java_mirror = JNIHandles::make_global(h_java_mirror);
1316     _ci_instance_klass_records.append(rec);
1317   }
1318 
1319   // Check if a ciInstanceKlass was present at replay dump time for a klass.
1320   ciInstanceKlassRecord* find_ciInstanceKlass(const InstanceKlass* klass) {
1321     for (int i = 0; i < _ci_instance_klass_records.length(); i++) {
1322       ciInstanceKlassRecord* rec = _ci_instance_klass_records.at(i);
1323       if (klass == rec->_klass) {
1324         // ciInstanceKlass for this klass was resolved.
1325         return rec;
1326       }
1327     }
1328     return nullptr;
1329   }
1330 
1331   // Create and initialize a record for a ciMethodData
1332   ciMethodDataRecord* new_ciMethodData(Method* method) {
1333     ciMethodDataRecord* rec = NEW_RESOURCE_OBJ(ciMethodDataRecord);
1334     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1335     rec->_method_name = method->name()->as_utf8();
1336     rec->_signature = method->signature()->as_utf8();
1337     _ci_method_data_records.append(rec);
1338     return rec;
1339   }
1340 
1341   // Lookup data for a ciMethodData
1342   ciMethodDataRecord* find_ciMethodDataRecord(Method* method) {
1343     const char* klass_name =  method->method_holder()->name()->as_utf8();
1344     const char* method_name = method->name()->as_utf8();
1345     const char* signature = method->signature()->as_utf8();
1346     for (int i = 0; i < _ci_method_data_records.length(); i++) {
1347       ciMethodDataRecord* rec = _ci_method_data_records.at(i);
1348       if (strcmp(rec->_klass_name, klass_name) == 0 &&
1349           strcmp(rec->_method_name, method_name) == 0 &&
1350           strcmp(rec->_signature, signature) == 0) {
1351         return rec;
1352       }
1353     }
1354     return nullptr;
1355   }
1356 
1357   // Create and initialize a record for a ciInlineRecord
1358   ciInlineRecord* new_ciInlineRecord(Method* method, int bci, int depth, int inline_late) {
1359     ciInlineRecord* rec = NEW_RESOURCE_OBJ(ciInlineRecord);
1360     rec->_klass_name =  method->method_holder()->name()->as_utf8();
1361     rec->_method_name = method->name()->as_utf8();
1362     rec->_signature = method->signature()->as_utf8();
1363     rec->_inline_bci = bci;
1364     rec->_inline_depth = depth;
1365     rec->_inline_late = inline_late;
1366     _ci_inline_records->append(rec);
1367     return rec;
1368   }
1369 
1370   // Lookup inlining data for a ciMethod
1371   ciInlineRecord* find_ciInlineRecord(Method* method, int bci, int depth) {
1372     if (_ci_inline_records != nullptr) {
1373       return find_ciInlineRecord(_ci_inline_records, method, bci, depth);
1374     }
1375     return nullptr;
1376   }
1377 
1378   static ciInlineRecord* find_ciInlineRecord(GrowableArray<ciInlineRecord*>*  records,
1379                                       Method* method, int bci, int depth) {
1380     if (records != nullptr) {
1381       const char* klass_name  = method->method_holder()->name()->as_utf8();
1382       const char* method_name = method->name()->as_utf8();
1383       const char* signature   = method->signature()->as_utf8();
1384       for (int i = 0; i < records->length(); i++) {
1385         ciInlineRecord* rec = records->at(i);
1386         if ((rec->_inline_bci == bci) &&
1387             (rec->_inline_depth == depth) &&
1388             (strcmp(rec->_klass_name, klass_name) == 0) &&
1389             (strcmp(rec->_method_name, method_name) == 0) &&
1390             (strcmp(rec->_signature, signature) == 0)) {
1391           return rec;
1392         }
1393       }
1394     }
1395     return nullptr;
1396   }
1397 
1398   const char* error_message() {
1399     return _error_message;
1400   }
1401 
1402   void reset() {
1403     _error_message = nullptr;
1404     _ci_method_records.clear();
1405     _ci_method_data_records.clear();
1406   }
1407 
1408   // Take an ascii string contain \u#### escapes and convert it to utf8
1409   // in place.
1410   static void unescape_string(char* value) {
1411     char* from = value;
1412     char* to = value;
1413     while (*from != '\0') {
1414       if (*from != '\\') {
1415         *from++ = *to++;
1416       } else {
1417         switch (from[1]) {
1418           case 'u': {
1419             from += 2;
1420             jchar value=0;
1421             for (int i=0; i<4; i++) {
1422               char c = *from++;
1423               switch (c) {
1424                 case '0': case '1': case '2': case '3': case '4':
1425                 case '5': case '6': case '7': case '8': case '9':
1426                   value = (value << 4) + c - '0';
1427                   break;
1428                 case 'a': case 'b': case 'c':
1429                 case 'd': case 'e': case 'f':
1430                   value = (value << 4) + 10 + c - 'a';
1431                   break;
1432                 case 'A': case 'B': case 'C':
1433                 case 'D': case 'E': case 'F':
1434                   value = (value << 4) + 10 + c - 'A';
1435                   break;
1436                 default:
1437                   ShouldNotReachHere();
1438               }
1439             }
1440             UNICODE::convert_to_utf8(&value, 1, to);
1441             to++;
1442             break;
1443           }
1444           case 't': *to++ = '\t'; from += 2; break;
1445           case 'n': *to++ = '\n'; from += 2; break;
1446           case 'r': *to++ = '\r'; from += 2; break;
1447           case 'f': *to++ = '\f'; from += 2; break;
1448           default:
1449             ShouldNotReachHere();
1450         }
1451       }
1452     }
1453     *from = *to;
1454   }
1455 };
1456 
1457 void ciReplay::replay(TRAPS) {
1458   int exit_code = replay_impl(THREAD);
1459 
1460   Threads::destroy_vm();
1461 
1462   vm_exit(exit_code);
1463 }
1464 
1465 bool ciReplay::no_replay_state() {
1466   return replay_state == nullptr;
1467 }
1468 
1469 void* ciReplay::load_inline_data(ciMethod* method, int entry_bci, int comp_level) {
1470   if (FLAG_IS_DEFAULT(InlineDataFile)) {
1471     tty->print_cr("ERROR: no inline replay data file specified (use -XX:InlineDataFile=inline_pid12345.txt).");
1472     return nullptr;
1473   }
1474 
1475   VM_ENTRY_MARK;
1476   // Load and parse the replay data
1477   CompileReplay rp(InlineDataFile, THREAD);
1478   if (!rp.can_replay()) {
1479     tty->print_cr("ciReplay: !rp.can_replay()");
1480     return nullptr;
1481   }
1482   void* data = rp.process_inline(method, method->get_Method(), entry_bci, comp_level, THREAD);
1483   if (HAS_PENDING_EXCEPTION) {
1484     Handle throwable(THREAD, PENDING_EXCEPTION);
1485     CLEAR_PENDING_EXCEPTION;
1486     java_lang_Throwable::print_stack_trace(throwable, tty);
1487     tty->cr();
1488     return nullptr;
1489   }
1490 
1491   if (rp.had_error()) {
1492     tty->print_cr("ciReplay: Failed on %s", rp.error_message());
1493     return nullptr;
1494   }
1495   return data;
1496 }
1497 
1498 int ciReplay::replay_impl(TRAPS) {
1499   HandleMark hm(THREAD);
1500   ResourceMark rm(THREAD);
1501 
1502   if (ReplaySuppressInitializers > 2) {
1503     // ReplaySuppressInitializers > 2 means that we want to allow
1504     // normal VM bootstrap but once we get into the replay itself
1505     // don't allow any initializers to be run.
1506     ReplaySuppressInitializers = 1;
1507   }
1508 
1509   if (FLAG_IS_DEFAULT(ReplayDataFile)) {
1510     tty->print_cr("ERROR: no compiler replay data file specified (use -XX:ReplayDataFile=replay_pid12345.txt).");
1511     return 1;
1512   }
1513 
1514   // Load and parse the replay data
1515   CompileReplay rp(ReplayDataFile, THREAD);
1516   int exit_code = 0;
1517   if (rp.can_replay()) {
1518     rp.process(THREAD);
1519   } else {
1520     exit_code = 1;
1521     return exit_code;
1522   }
1523 
1524   if (HAS_PENDING_EXCEPTION) {
1525     Handle throwable(THREAD, PENDING_EXCEPTION);
1526     CLEAR_PENDING_EXCEPTION;
1527     java_lang_Throwable::print_stack_trace(throwable, tty);
1528     tty->cr();
1529     exit_code = 2;
1530   }
1531 
1532   if (rp.had_error()) {
1533     tty->print_cr("Failed on %s", rp.error_message());
1534     exit_code = 1;
1535   }
1536   return exit_code;
1537 }
1538 
1539 void ciReplay::initialize(ciMethodData* m) {
1540   if (no_replay_state()) {
1541     return;
1542   }
1543 
1544   ASSERT_IN_VM;
1545   ResourceMark rm;
1546 
1547   Method* method = m->get_MethodData()->method();
1548   ciMethodDataRecord* rec = replay_state->find_ciMethodDataRecord(method);
1549   if (rec == nullptr) {
1550     // This indicates some mismatch with the original environment and
1551     // the replay environment though it's not always enough to
1552     // interfere with reproducing a bug
1553     tty->print_cr("Warning: requesting ciMethodData record for method with no data: ");
1554     method->print_name(tty);
1555     tty->cr();
1556   } else {
1557     m->_state = rec->_state;
1558     m->_invocation_counter = rec->_invocation_counter;
1559     if (rec->_data_length != 0) {
1560       assert(m->_data_size + m->_extra_data_size == rec->_data_length * (int)sizeof(rec->_data[0]) ||
1561              m->_data_size == rec->_data_length * (int)sizeof(rec->_data[0]), "must agree");
1562 
1563       // Write the correct ciObjects back into the profile data
1564       ciEnv* env = ciEnv::current();
1565       for (int i = 0; i < rec->_classes_length; i++) {
1566         Klass *k = rec->_classes[i];
1567         // In case this class pointer is is tagged, preserve the tag bits
1568         intptr_t status = 0;
1569         if (k != nullptr) {
1570           status = ciTypeEntries::with_status(env->get_metadata(k)->as_klass(), rec->_data[rec->_classes_offsets[i]]);
1571         }
1572         rec->_data[rec->_classes_offsets[i]] = status;
1573       }
1574       for (int i = 0; i < rec->_methods_length; i++) {
1575         Method *m = rec->_methods[i];
1576         *(ciMetadata**)(rec->_data + rec->_methods_offsets[i]) =
1577           env->get_metadata(m);
1578       }
1579       // Copy the updated profile data into place as intptr_ts
1580 #ifdef _LP64
1581       Copy::conjoint_jlongs_atomic((jlong *)rec->_data, (jlong *)m->_data, rec->_data_length);
1582 #else
1583       Copy::conjoint_jints_atomic((jint *)rec->_data, (jint *)m->_data, rec->_data_length);
1584 #endif
1585     }
1586 
1587     // copy in the original header
1588     Copy::conjoint_jbytes(rec->_orig_data, (char*)&m->_orig, rec->_orig_data_length);
1589   }
1590 }
1591 
1592 
1593 bool ciReplay::should_not_inline(ciMethod* method) {
1594   if (no_replay_state()) {
1595     return false;
1596   }
1597   VM_ENTRY_MARK;
1598   // ciMethod without a record shouldn't be inlined.
1599   return replay_state->find_ciMethodRecord(method->get_Method()) == nullptr;
1600 }
1601 
1602 bool ciReplay::should_inline(void* data, ciMethod* method, int bci, int inline_depth, bool& should_delay) {
1603   if (data != nullptr) {
1604     GrowableArray<ciInlineRecord*>* records = (GrowableArray<ciInlineRecord*>*)data;
1605     VM_ENTRY_MARK;
1606     // Inline record are ordered by bci and depth.
1607     ciInlineRecord* record = CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth);
1608     if (record == nullptr) {
1609       return false;
1610     }
1611     should_delay = record->_inline_late;
1612     return true;
1613   } else if (replay_state != nullptr) {
1614     VM_ENTRY_MARK;
1615     // Inline record are ordered by bci and depth.
1616     ciInlineRecord* record = replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth);
1617     if (record == nullptr) {
1618       return false;
1619     }
1620     should_delay = record->_inline_late;
1621     return true;
1622   }
1623   return false;
1624 }
1625 
1626 bool ciReplay::should_not_inline(void* data, ciMethod* method, int bci, int inline_depth) {
1627   if (data != nullptr) {
1628     GrowableArray<ciInlineRecord*>* records = (GrowableArray<ciInlineRecord*>*)data;
1629     VM_ENTRY_MARK;
1630     // Inline record are ordered by bci and depth.
1631     return CompileReplay::find_ciInlineRecord(records, method->get_Method(), bci, inline_depth) == nullptr;
1632   } else if (replay_state != nullptr) {
1633     VM_ENTRY_MARK;
1634     // Inline record are ordered by bci and depth.
1635     return replay_state->find_ciInlineRecord(method->get_Method(), bci, inline_depth) == nullptr;
1636   }
1637   return false;
1638 }
1639 
1640 void ciReplay::initialize(ciMethod* m) {
1641   if (no_replay_state()) {
1642     return;
1643   }
1644 
1645   ASSERT_IN_VM;
1646   ResourceMark rm;
1647 
1648   Method* method = m->get_Method();
1649   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1650   if (rec == nullptr) {
1651     // This indicates some mismatch with the original environment and
1652     // the replay environment though it's not always enough to
1653     // interfere with reproducing a bug
1654     tty->print_cr("Warning: requesting ciMethod record for method with no data: ");
1655     method->print_name(tty);
1656     tty->cr();
1657   } else {
1658     EXCEPTION_CONTEXT;
1659     // m->_instructions_size = rec->_instructions_size;
1660     m->_inline_instructions_size = -1;
1661     m->_interpreter_invocation_count = rec->_interpreter_invocation_count;
1662     m->_interpreter_throwout_count = rec->_interpreter_throwout_count;
1663     MethodCounters* mcs = method->get_method_counters(CHECK_AND_CLEAR);
1664     guarantee(mcs != nullptr, "method counters allocation failed");
1665     mcs->invocation_counter()->_counter = rec->_invocation_counter;
1666     mcs->backedge_counter()->_counter = rec->_backedge_counter;
1667   }
1668 }
1669 
1670 void ciReplay::initialize(ciInstanceKlass* ci_ik, InstanceKlass* ik) {
1671   assert(!no_replay_state(), "must have replay state");
1672 
1673   ASSERT_IN_VM;
1674   ciInstanceKlassRecord* rec = replay_state->find_ciInstanceKlass(ik);
1675   assert(rec != nullptr, "ciInstanceKlass must be whitelisted");
1676   ci_ik->_java_mirror = CURRENT_ENV->get_instance(JNIHandles::resolve(rec->_java_mirror));
1677 }
1678 
1679 bool ciReplay::is_loaded(Method* method) {
1680   if (no_replay_state()) {
1681     return true;
1682   }
1683 
1684   ASSERT_IN_VM;
1685   ResourceMark rm;
1686 
1687   ciMethodRecord* rec = replay_state->find_ciMethodRecord(method);
1688   return rec != nullptr;
1689 }
1690 
1691 bool ciReplay::is_klass_unresolved(const InstanceKlass* klass) {
1692   if (no_replay_state()) {
1693     return false;
1694   }
1695 
1696   // Check if klass is found on whitelist.
1697   ciInstanceKlassRecord* rec = replay_state->find_ciInstanceKlass(klass);
1698   return rec == nullptr;
1699 }
1700 
1701 oop ciReplay::obj_field(oop obj, Symbol* name) {
1702   InstanceKlass* ik = InstanceKlass::cast(obj->klass());
1703 
1704   do {
1705     if (!ik->has_nonstatic_fields()) {
1706       ik = ik->java_super();
1707       continue;
1708     }
1709 
1710     for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
1711       if (fs.access_flags().is_static()) {
1712         continue;
1713       }
1714       if (fs.name() == name) {
1715         int offset = fs.offset();
1716 #ifdef ASSERT
1717         fieldDescriptor fd = fs.field_descriptor();
1718         assert(fd.offset() == ik->field_offset(fd.index()), "!");
1719 #endif
1720         oop f = obj->obj_field(offset);
1721         return f;
1722       }
1723     }
1724 
1725     ik = ik->java_super();
1726   } while (ik != nullptr);
1727   return nullptr;
1728 }
1729 
1730 oop ciReplay::obj_field(oop obj, const char *name) {
1731   Symbol* fname = SymbolTable::probe(name, (int)strlen(name));
1732   if (fname == nullptr) {
1733     return nullptr;
1734   }
1735   return obj_field(obj, fname);
1736 }