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