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