1 /*
   2  * Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "cds/archiveUtils.hpp"
  27 #include "cds/cdsConfig.hpp"
  28 #include "cds/classListParser.hpp"
  29 #include "cds/classPrelinker.hpp"
  30 #include "cds/lambdaFormInvokers.hpp"
  31 #include "cds/metaspaceShared.hpp"
  32 #include "cds/unregisteredClasses.hpp"
  33 #include "classfile/javaClasses.inline.hpp"
  34 #include "classfile/symbolTable.hpp"
  35 #include "classfile/systemDictionary.hpp"
  36 #include "classfile/systemDictionaryShared.hpp"
  37 #include "classfile/vmClasses.hpp"
  38 #include "classfile/vmSymbols.hpp"
  39 #include "interpreter/bytecode.hpp"
  40 #include "interpreter/interpreterRuntime.hpp"
  41 #include "interpreter/linkResolver.hpp"
  42 #include "jimage.hpp"
  43 #include "jvm.h"
  44 #include "logging/log.hpp"
  45 #include "logging/logTag.hpp"
  46 #include "memory/oopFactory.hpp"
  47 #include "memory/resourceArea.hpp"
  48 #include "oops/constantPool.inline.hpp"
  49 #include "oops/cpCache.inline.hpp"
  50 #include "runtime/atomic.hpp"
  51 #include "runtime/handles.inline.hpp"
  52 #include "runtime/java.hpp"
  53 #include "runtime/javaCalls.hpp"
  54 #include "utilities/defaultStream.hpp"
  55 #include "utilities/macros.hpp"
  56 
  57 const char* ClassListParser::CLASS_REFLECTION_DATA_TAG = "@class-reflection-data";
  58 const char* ClassListParser::CONSTANT_POOL_TAG = "@cp";
  59 const char* ClassListParser::DYNAMIC_PROXY_TAG = "@dynamic-proxy";
  60 const char* ClassListParser::LAMBDA_FORM_TAG = "@lambda-form-invoker";
  61 const char* ClassListParser::LAMBDA_PROXY_TAG = "@lambda-proxy";
  62 const char* ClassListParser::LOADER_NEGATIVE_CACHE_TAG = "@loader-negative-cache";
  63 
  64 volatile Thread* ClassListParser::_parsing_thread = nullptr;
  65 ClassListParser* ClassListParser::_instance = nullptr;
  66 
  67 ClassListParser::ClassListParser(const char* file, ParseMode parse_mode) : _id2klass_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE) {
  68   log_info(cds)("Parsing %s%s", file,
  69                 (parse_mode == _parse_lambda_forms_invokers_only) ? " (lambda form invokers only)" : "");
  70   _classlist_file = file;
  71   _file = nullptr;
  72   // Use os::open() because neither fopen() nor os::fopen()
  73   // can handle long path name on Windows.
  74   int fd = os::open(file, O_RDONLY, S_IREAD);
  75   if (fd != -1) {
  76     // Obtain a File* from the file descriptor so that fgets()
  77     // can be used in parse_one_line()
  78     _file = os::fdopen(fd, "r");
  79   }
  80   if (_file == nullptr) {
  81     char errmsg[JVM_MAXPATHLEN];
  82     os::lasterror(errmsg, JVM_MAXPATHLEN);
  83     vm_exit_during_initialization("Loading classlist failed", errmsg);
  84   }
  85   _line_no = 0;
  86   _token = _line;
  87   _interfaces = new (mtClass) GrowableArray<int>(10, mtClass);
  88   _indy_items = new (mtClass) GrowableArray<const char*>(9, mtClass);
  89   _parse_mode = parse_mode;
  90 
  91   // _instance should only be accessed by the thread that created _instance.
  92   assert(_instance == nullptr, "must be singleton");
  93   _instance = this;
  94   Atomic::store(&_parsing_thread, Thread::current());
  95 }
  96 
  97 bool ClassListParser::is_parsing_thread() {
  98   return Atomic::load(&_parsing_thread) == Thread::current();
  99 }
 100 
 101 ClassListParser::~ClassListParser() {
 102   if (_file != nullptr) {
 103     fclose(_file);
 104   }
 105   Atomic::store(&_parsing_thread, (Thread*)nullptr);
 106   delete _indy_items;
 107   delete _interfaces;
 108   _instance = nullptr;
 109 }
 110 
 111 int ClassListParser::parse(TRAPS) {
 112   int class_count = 0;
 113 
 114   while (parse_one_line()) {
 115     if (lambda_form_line()) {
 116       // The current line is "@lambda-form-invoker ...". It has been recorded in LambdaFormInvokers,
 117       // and will be processed later.
 118       continue;
 119     }
 120     if (_constant_pool_line) {
 121       continue;
 122     }
 123     if (_class_reflection_data_line) {
 124       continue;
 125     }
 126     if (_loader_negative_cache_line) {
 127       continue;
 128     }
 129     if (_parse_mode == _parse_lambda_forms_invokers_only) {
 130       continue;
 131     }
 132 
 133     TempNewSymbol class_name_symbol = SymbolTable::new_symbol(_class_name);
 134     if (_indy_items->length() > 0) {
 135       // The current line is "@lambda-proxy class_name". Load the proxy class.
 136       resolve_indy(THREAD, class_name_symbol);
 137       class_count++;
 138       continue;
 139     }
 140 
 141     Klass* klass = load_current_class(class_name_symbol, THREAD);
 142     if (HAS_PENDING_EXCEPTION) {
 143       if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
 144         // If we have run out of memory, don't try to load the rest of the classes in
 145         // the classlist. Throw an exception, which will terminate the dumping process.
 146         return 0; // THROW
 147       }
 148 
 149       ResourceMark rm(THREAD);
 150       char* ex_msg = (char*)"";
 151       oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
 152       if (message != nullptr) {
 153         ex_msg = java_lang_String::as_utf8_string(message);
 154       }
 155       log_warning(cds)("%s: %s", PENDING_EXCEPTION->klass()->external_name(), ex_msg);
 156       // We might have an invalid class name or an bad class. Warn about it
 157       // and keep going to the next line.
 158       CLEAR_PENDING_EXCEPTION;
 159       log_warning(cds)("Preload Warning: Cannot find %s", _class_name);
 160       continue;
 161     }
 162 
 163     assert(klass != nullptr, "sanity");
 164     if (log_is_enabled(Trace, cds)) {
 165       ResourceMark rm(THREAD);
 166       log_trace(cds)("Shared spaces preloaded: %s", klass->external_name());
 167     }
 168 
 169     if (klass->is_instance_klass()) {
 170       InstanceKlass* ik = InstanceKlass::cast(klass);
 171 
 172       // Link the class to cause the bytecodes to be rewritten and the
 173       // cpcache to be created. The linking is done as soon as classes
 174       // are loaded in order that the related data structures (klass and
 175       // cpCache) are located together.
 176       MetaspaceShared::try_link_class(THREAD, ik);
 177     }
 178 
 179     class_count++;
 180   }
 181 
 182   return class_count;
 183 }
 184 
 185 bool ClassListParser::parse_one_line() {
 186   for (;;) {
 187     if (fgets(_line, sizeof(_line), _file) == nullptr) {
 188       return false;
 189     }
 190     ++ _line_no;
 191     _line_len = (int)strlen(_line);
 192     if (_line_len > _max_allowed_line_len) {
 193       error("input line too long (must be no longer than %d chars)", _max_allowed_line_len);
 194     }
 195     if (*_line == '#') { // comment
 196       continue;
 197     }
 198 
 199     {
 200       int len = (int)strlen(_line);
 201       int i;
 202       // Replace \t\r\n\f with ' '
 203       for (i=0; i<len; i++) {
 204         if (_line[i] == '\t' || _line[i] == '\r' || _line[i] == '\n' || _line[i] == '\f') {
 205           _line[i] = ' ';
 206         }
 207       }
 208 
 209       // Remove trailing newline/space
 210       while (len > 0) {
 211         if (_line[len-1] == ' ') {
 212           _line[len-1] = '\0';
 213           len --;
 214         } else {
 215           break;
 216         }
 217       }
 218       _line_len = len;
 219     }
 220 
 221     // valid line
 222     break;
 223   }
 224 
 225   _class_name = _line;
 226   _id = _unspecified;
 227   _super = _unspecified;
 228   _interfaces->clear();
 229   _source = nullptr;
 230   _interfaces_specified = false;
 231   _indy_items->clear();
 232   _lambda_form_line = false;
 233   _constant_pool_line = false;
 234   _class_reflection_data_line = false;
 235   _loader_negative_cache_line = false;
 236 
 237   if (_line[0] == '@') {
 238     return parse_at_tags();
 239   }
 240 
 241   if ((_token = strchr(_line, ' ')) == nullptr) {
 242     // No optional arguments are specified.
 243     return true;
 244   }
 245 
 246   // Mark the end of the name, and go to the next input char
 247   *_token++ = '\0';
 248 
 249   while (*_token) {
 250     skip_whitespaces();
 251 
 252     if (parse_uint_option("id:", &_id)) {
 253       continue;
 254     } else if (parse_uint_option("super:", &_super)) {
 255       check_already_loaded("Super class", _super);
 256       continue;
 257     } else if (skip_token("interfaces:")) {
 258       int i;
 259       while (try_parse_uint(&i)) {
 260         check_already_loaded("Interface", i);
 261         _interfaces->append(i);
 262       }
 263     } else if (skip_token("source:")) {
 264       skip_whitespaces();
 265       _source = _token;
 266       char* s = strchr(_token, ' ');
 267       if (s == nullptr) {
 268         break; // end of input line
 269       } else {
 270         *s = '\0'; // mark the end of _source
 271         _token = s+1;
 272       }
 273     } else {
 274       error("Unknown input");
 275     }
 276   }
 277 
 278   // if src is specified
 279   //     id super interfaces must all be specified
 280   //     loader may be specified
 281   // else
 282   //     # the class is loaded from classpath
 283   //     id may be specified
 284   //     super, interfaces, loader must not be specified
 285   return true;
 286 }
 287 
 288 void ClassListParser::split_tokens_by_whitespace(int offset) {
 289   int start = offset;
 290   int end;
 291   bool done = false;
 292   while (!done) {
 293     while (_line[start] == ' ' || _line[start] == '\t') start++;
 294     end = start;
 295     while (_line[end] && _line[end] != ' ' && _line[end] != '\t') end++;
 296     if (_line[end] == '\0') {
 297       done = true;
 298     } else {
 299       _line[end] = '\0';
 300     }
 301     _indy_items->append(_line + start);
 302     start = ++end;
 303   }
 304 }
 305 
 306 int ClassListParser::split_at_tag_from_line() {
 307   _token = _line;
 308   char* ptr;
 309   if ((ptr = strchr(_line, ' ')) == nullptr) {
 310     error("Too few items following the @ tag \"%s\" line #%d", _line, _line_no);
 311     return 0;
 312   }
 313   *ptr++ = '\0';
 314   while (*ptr == ' ' || *ptr == '\t') ptr++;
 315   return (int)(ptr - _line);
 316 }
 317 
 318 bool ClassListParser::parse_at_tags() {
 319   assert(_line[0] == '@', "must be");
 320   int offset;
 321   if ((offset = split_at_tag_from_line()) == 0) {
 322     return false;
 323   }
 324 
 325   if (strcmp(_token, LAMBDA_PROXY_TAG) == 0) {
 326     split_tokens_by_whitespace(offset);
 327     if (_indy_items->length() < 2) {
 328       error("Line with @ tag has too few items \"%s\" line #%d", _token, _line_no);
 329       return false;
 330     }
 331     // set the class name
 332     _class_name = _indy_items->at(0);
 333     return true;
 334   } else if (strcmp(_token, LAMBDA_FORM_TAG) == 0) {
 335     LambdaFormInvokers::append(os::strdup((const char*)(_line + offset), mtInternal));
 336     _lambda_form_line = true;
 337     return true;
 338   } else if (strcmp(_token, CONSTANT_POOL_TAG) == 0) {
 339     _token = _line + offset;
 340     _constant_pool_line = true;
 341     parse_constant_pool_tag();
 342     return true;
 343   } else if (strcmp(_token, CLASS_REFLECTION_DATA_TAG) == 0) {
 344     _token = _line + offset;
 345     _class_reflection_data_line = true;
 346     parse_class_reflection_data_tag();
 347     return true;
 348   } else if (strcmp(_token, DYNAMIC_PROXY_TAG) == 0) {
 349     _token = _line + offset;
 350     _constant_pool_line = true;
 351     parse_dynamic_proxy_tag();
 352     return true;
 353   } else if (strcmp(_token, LOADER_NEGATIVE_CACHE_TAG) == 0) {
 354     _token = _line + offset;
 355     _loader_negative_cache_line = true;
 356     parse_loader_negative_cache_tag();
 357     return true;
 358   } else {
 359     error("Invalid @ tag at the beginning of line \"%s\" line #%d", _token, _line_no);
 360     return false;
 361   }
 362 }
 363 
 364 void ClassListParser::skip_whitespaces() {
 365   while (*_token == ' ' || *_token == '\t') {
 366     _token ++;
 367   }
 368 }
 369 
 370 void ClassListParser::skip_non_whitespaces() {
 371   while (*_token && *_token != ' ' && *_token != '\t') {
 372     _token ++;
 373   }
 374 }
 375 
 376 void ClassListParser::parse_int(int* value) {
 377   skip_whitespaces();
 378   if (sscanf(_token, "%i", value) == 1) {
 379     skip_non_whitespaces();
 380   } else {
 381     error("Error: expected integer");
 382   }
 383 }
 384 
 385 void ClassListParser::parse_uint(int* value) {
 386   parse_int(value);
 387   if (*value < 0) {
 388     error("Error: negative integers not allowed (%d)", *value);
 389   }
 390 }
 391 
 392 bool ClassListParser::try_parse_uint(int* value) {
 393   skip_whitespaces();
 394   if (sscanf(_token, "%i", value) == 1) {
 395     skip_non_whitespaces();
 396     return true;
 397   }
 398   return false;
 399 }
 400 
 401 bool ClassListParser::skip_token(const char* option_name) {
 402   size_t len = strlen(option_name);
 403   if (strncmp(_token, option_name, len) == 0) {
 404     _token += len;
 405     return true;
 406   } else {
 407     return false;
 408   }
 409 }
 410 
 411 bool ClassListParser::parse_int_option(const char* option_name, int* value) {
 412   if (skip_token(option_name)) {
 413     if (*value != _unspecified) {
 414       error("%s specified twice", option_name);
 415     } else {
 416       parse_int(value);
 417       return true;
 418     }
 419   }
 420   return false;
 421 }
 422 
 423 bool ClassListParser::parse_uint_option(const char* option_name, int* value) {
 424   if (skip_token(option_name)) {
 425     if (*value != _unspecified) {
 426       error("%s specified twice", option_name);
 427     } else {
 428       parse_uint(value);
 429       return true;
 430     }
 431   }
 432   return false;
 433 }
 434 
 435 void ClassListParser::print_specified_interfaces() {
 436   const int n = _interfaces->length();
 437   jio_fprintf(defaultStream::error_stream(), "Currently specified interfaces[%d] = {\n", n);
 438   for (int i=0; i<n; i++) {
 439     InstanceKlass* k = lookup_class_by_id(_interfaces->at(i));
 440     jio_fprintf(defaultStream::error_stream(), "  %4d = %s\n", _interfaces->at(i), k->name()->as_klass_external_name());
 441   }
 442   jio_fprintf(defaultStream::error_stream(), "}\n");
 443 }
 444 
 445 void ClassListParser::print_actual_interfaces(InstanceKlass* ik) {
 446   int n = ik->local_interfaces()->length();
 447   jio_fprintf(defaultStream::error_stream(), "Actual interfaces[%d] = {\n", n);
 448   for (int i = 0; i < n; i++) {
 449     InstanceKlass* e = ik->local_interfaces()->at(i);
 450     jio_fprintf(defaultStream::error_stream(), "  %s\n", e->name()->as_klass_external_name());
 451   }
 452   jio_fprintf(defaultStream::error_stream(), "}\n");
 453 }
 454 
 455 void ClassListParser::print_diagnostic_info(outputStream* st, const char* msg, ...) {
 456   va_list ap;
 457   va_start(ap, msg);
 458   print_diagnostic_info(st, msg, ap);
 459   va_end(ap);
 460 }
 461 
 462 void ClassListParser::print_diagnostic_info(outputStream* st, const char* msg, va_list ap) {
 463   int error_index = pointer_delta_as_int(_token, _line);
 464   if (error_index >= _line_len) {
 465     error_index = _line_len - 1;
 466   }
 467   if (error_index < 0) {
 468     error_index = 0;
 469   }
 470 
 471   st->print("An error has occurred while processing class list file %s %d:%d.\n",
 472             _classlist_file, _line_no, (error_index + 1));
 473   st->vprint(msg, ap);
 474 
 475   if (_line_len <= 0) {
 476     st->print("\n");
 477   } else {
 478     st->print(":\n");
 479     for (int i=0; i<_line_len; i++) {
 480       char c = _line[i];
 481       if (c == '\0') {
 482         st->print("%s", " ");
 483       } else {
 484         st->print("%c", c);
 485       }
 486     }
 487     st->print("\n");
 488     for (int i=0; i<error_index; i++) {
 489       st->print("%s", " ");
 490     }
 491     st->print("^\n");
 492   }
 493 }
 494 
 495 void ClassListParser::error(const char* msg, ...) {
 496   va_list ap;
 497   va_start(ap, msg);
 498   LogTarget(Error, cds) lt;
 499   LogStream ls(lt);
 500   print_diagnostic_info(&ls, msg, ap);
 501   vm_exit_during_initialization("class list format error.", nullptr);
 502   va_end(ap);
 503 }
 504 
 505 void ClassListParser::constant_pool_resolution_warning(const char* msg, ...) {
 506   va_list ap;
 507   va_start(ap, msg);
 508   LogTarget(Warning, cds, resolve) lt;
 509   LogStream ls(lt);
 510   print_diagnostic_info(&ls, msg, ap);
 511   ls.print("Your classlist may be out of sync with the JDK or the application.");
 512   va_end(ap);
 513 }
 514 
 515 // This function is used for loading classes for customized class loaders
 516 // during archive dumping.
 517 InstanceKlass* ClassListParser::load_class_from_source(Symbol* class_name, TRAPS) {
 518 #if !(defined(_LP64) && (defined(LINUX) || defined(__APPLE__) || defined(_WINDOWS)))
 519   // The only supported platforms are: (1) Linux/64-bit and (2) Solaris/64-bit and
 520   // (3) MacOSX/64-bit and (4) Windowss/64-bit
 521   // This #if condition should be in sync with the areCustomLoadersSupportedForCDS
 522   // method in test/lib/jdk/test/lib/Platform.java.
 523   error("AppCDS custom class loaders not supported on this platform");
 524 #endif
 525 
 526   if (!is_super_specified()) {
 527     error("If source location is specified, super class must be also specified");
 528   }
 529   if (!is_id_specified()) {
 530     error("If source location is specified, id must be also specified");
 531   }
 532   if (strncmp(_class_name, "java/", 5) == 0) {
 533     log_info(cds)("Prohibited package for non-bootstrap classes: %s.class from %s",
 534           _class_name, _source);
 535     THROW_NULL(vmSymbols::java_lang_ClassNotFoundException());
 536   }
 537 
 538   InstanceKlass* k = UnregisteredClasses::load_class(class_name, _source, CHECK_NULL);
 539   if (k->local_interfaces()->length() != _interfaces->length()) {
 540     print_specified_interfaces();
 541     print_actual_interfaces(k);
 542     error("The number of interfaces (%d) specified in class list does not match the class file (%d)",
 543           _interfaces->length(), k->local_interfaces()->length());
 544   }
 545 
 546   assert(k->is_shared_unregistered_class(), "must be");
 547 
 548   bool added = SystemDictionaryShared::add_unregistered_class(THREAD, k);
 549   if (!added) {
 550     // We allow only a single unregistered class for each unique name.
 551     error("Duplicated class %s", _class_name);
 552   }
 553 
 554   return k;
 555 }
 556 
 557 void ClassListParser::populate_cds_indy_info(const constantPoolHandle &pool, int cp_index, CDSIndyInfo* cii, TRAPS) {
 558   // Caller needs to allocate ResourceMark.
 559   int type_index = pool->bootstrap_name_and_type_ref_index_at(cp_index);
 560   int name_index = pool->name_ref_index_at(type_index);
 561   cii->add_item(pool->symbol_at(name_index)->as_C_string());
 562   int sig_index = pool->signature_ref_index_at(type_index);
 563   cii->add_item(pool->symbol_at(sig_index)->as_C_string());
 564   int argc = pool->bootstrap_argument_count_at(cp_index);
 565   if (argc > 0) {
 566     for (int arg_i = 0; arg_i < argc; arg_i++) {
 567       int arg = pool->bootstrap_argument_index_at(cp_index, arg_i);
 568       jbyte tag = pool->tag_at(arg).value();
 569       if (tag == JVM_CONSTANT_MethodType) {
 570         cii->add_item(pool->method_type_signature_at(arg)->as_C_string());
 571       } else if (tag == JVM_CONSTANT_MethodHandle) {
 572         cii->add_ref_kind(pool->method_handle_ref_kind_at(arg));
 573         int callee_index = pool->method_handle_klass_index_at(arg);
 574         Klass* callee = pool->klass_at(callee_index, CHECK);
 575         cii->add_item(callee->name()->as_C_string());
 576         cii->add_item(pool->method_handle_name_ref_at(arg)->as_C_string());
 577         cii->add_item(pool->method_handle_signature_ref_at(arg)->as_C_string());
 578       } else {
 579         ShouldNotReachHere();
 580       }
 581     }
 582   }
 583 }
 584 
 585 bool ClassListParser::is_matching_cp_entry(const constantPoolHandle &pool, int cp_index, TRAPS) {
 586   ResourceMark rm(THREAD);
 587   CDSIndyInfo cii;
 588   populate_cds_indy_info(pool, cp_index, &cii, CHECK_0);
 589   GrowableArray<const char*>* items = cii.items();
 590   int indy_info_offset = 1;
 591   if (_indy_items->length() - indy_info_offset != items->length()) {
 592     return false;
 593   }
 594   for (int i = 0; i < items->length(); i++) {
 595     if (strcmp(_indy_items->at(i + indy_info_offset), items->at(i)) != 0) {
 596       return false;
 597     }
 598   }
 599   return true;
 600 }
 601 
 602 void ClassListParser::resolve_indy(JavaThread* current, Symbol* class_name_symbol) {
 603   ExceptionMark em(current);
 604   JavaThread* THREAD = current; // For exception macros.
 605   ClassListParser::resolve_indy_impl(class_name_symbol, THREAD);
 606   if (HAS_PENDING_EXCEPTION) {
 607     ResourceMark rm(current);
 608     char* ex_msg = (char*)"";
 609     oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
 610     if (message != nullptr) {
 611       ex_msg = java_lang_String::as_utf8_string(message);
 612     }
 613     log_warning(cds)("resolve_indy for class %s has encountered exception: %s %s",
 614                      class_name_symbol->as_C_string(),
 615                      PENDING_EXCEPTION->klass()->external_name(),
 616                      ex_msg);
 617     CLEAR_PENDING_EXCEPTION;
 618   }
 619 }
 620 
 621 void ClassListParser::resolve_indy_impl(Symbol* class_name_symbol, TRAPS) {
 622   Handle class_loader(THREAD, SystemDictionary::java_system_loader());
 623   Handle protection_domain;
 624   Klass* klass = SystemDictionary::resolve_or_fail(class_name_symbol, class_loader, protection_domain, true, CHECK);
 625   if (klass->is_instance_klass()) {
 626     InstanceKlass* ik = InstanceKlass::cast(klass);
 627     MetaspaceShared::try_link_class(THREAD, ik);
 628     if (!ik->is_linked()) {
 629       // Verification of ik has failed
 630       return;
 631     }
 632 
 633     ConstantPool* cp = ik->constants();
 634     ConstantPoolCache* cpcache = cp->cache();
 635     bool found = false;
 636     for (int indy_index = 0; indy_index < cpcache->resolved_indy_entries_length(); indy_index++) {
 637       int pool_index = cpcache->resolved_indy_entry_at(indy_index)->constant_pool_index();
 638       constantPoolHandle pool(THREAD, cp);
 639       BootstrapInfo bootstrap_specifier(pool, pool_index, indy_index);
 640       Handle bsm = bootstrap_specifier.resolve_bsm(CHECK);
 641       if (!SystemDictionaryShared::is_supported_invokedynamic(&bootstrap_specifier)) {
 642         log_debug(cds, lambda)("is_supported_invokedynamic check failed for cp_index %d", pool_index);
 643         continue;
 644       }
 645       bool matched = is_matching_cp_entry(pool, pool_index, CHECK);
 646       if (matched) {
 647         found = true;
 648         CallInfo info;
 649         bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(info, CHECK);
 650         if (!is_done) {
 651           // resolve it
 652           Handle recv;
 653           LinkResolver::resolve_invoke(info,
 654                                        recv,
 655                                        pool,
 656                                        ConstantPool::encode_invokedynamic_index(indy_index),
 657                                        Bytecodes::_invokedynamic, CHECK);
 658           break;
 659         }
 660         cpcache->set_dynamic_call(info, indy_index);
 661       }
 662     }
 663     if (!found) {
 664       ResourceMark rm(THREAD);
 665       log_warning(cds)("No invoke dynamic constant pool entry can be found for class %s. The classlist is probably out-of-date.",
 666                      class_name_symbol->as_C_string());
 667     }
 668   }
 669 }
 670 
 671 Klass* ClassListParser::load_current_class(Symbol* class_name_symbol, TRAPS) {
 672   Klass* klass;
 673   if (!is_loading_from_source()) {
 674     // Load classes for the boot/platform/app loaders only.
 675     if (is_super_specified()) {
 676       error("If source location is not specified, super class must not be specified");
 677     }
 678     if (are_interfaces_specified()) {
 679       error("If source location is not specified, interface(s) must not be specified");
 680     }
 681 
 682     if (Signature::is_array(class_name_symbol)) {
 683       // array classes are not supported in class list.
 684       THROW_NULL(vmSymbols::java_lang_ClassNotFoundException());
 685     }
 686 
 687     JavaValue result(T_OBJECT);
 688     // Call java_system_loader().loadClass() directly, which will
 689     // delegate to the correct loader (boot, platform or app) depending on
 690     // the package name.
 691 
 692     // ClassLoader.loadClass() wants external class name format, i.e., convert '/' chars to '.'
 693     Handle ext_class_name = java_lang_String::externalize_classname(class_name_symbol, CHECK_NULL);
 694     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 695 
 696     JavaCalls::call_virtual(&result,
 697                             loader, //SystemDictionary::java_system_loader(),
 698                             vmClasses::ClassLoader_klass(),
 699                             vmSymbols::loadClass_name(),
 700                             vmSymbols::string_class_signature(),
 701                             ext_class_name,
 702                             CHECK_NULL);
 703 
 704     assert(result.get_type() == T_OBJECT, "just checking");
 705     oop obj = result.get_oop();
 706     assert(obj != nullptr, "jdk.internal.loader.BuiltinClassLoader::loadClass never returns null");
 707     klass = java_lang_Class::as_Klass(obj);
 708   } else {
 709     // If "source:" tag is specified, all super class and super interfaces must be specified in the
 710     // class list file.
 711     klass = load_class_from_source(class_name_symbol, CHECK_NULL);
 712   }
 713 
 714   assert(klass != nullptr, "exception should have been thrown");
 715   assert(klass->is_instance_klass(), "array classes should have been filtered out");
 716 
 717   if (is_id_specified()) {
 718     InstanceKlass* ik = InstanceKlass::cast(klass);
 719     int id = this->id();
 720     SystemDictionaryShared::update_shared_entry(ik, id);
 721     bool created;
 722     id2klass_table()->put_if_absent(id, ik, &created);
 723     if (!created) {
 724       error("Duplicated ID %d for class %s", id, _class_name);
 725     }
 726     if (id2klass_table()->maybe_grow()) {
 727       log_info(cds, hashtables)("Expanded id2klass_table() to %d", id2klass_table()->table_size());
 728     }
 729   }
 730 
 731   return klass;
 732 }
 733 
 734 bool ClassListParser::is_loading_from_source() {
 735   return (_source != nullptr);
 736 }
 737 
 738 InstanceKlass* ClassListParser::lookup_class_by_id(int id) {
 739   InstanceKlass** klass_ptr = id2klass_table()->get(id);
 740   if (klass_ptr == nullptr) {
 741     error("Class ID %d has not been defined", id);
 742   }
 743   assert(*klass_ptr != nullptr, "must be");
 744   return *klass_ptr;
 745 }
 746 
 747 
 748 InstanceKlass* ClassListParser::lookup_super_for_current_class(Symbol* super_name) {
 749   if (!is_loading_from_source()) {
 750     return nullptr;
 751   }
 752 
 753   InstanceKlass* k = lookup_class_by_id(super());
 754   if (super_name != k->name()) {
 755     error("The specified super class %s (id %d) does not match actual super class %s",
 756           k->name()->as_klass_external_name(), super(),
 757           super_name->as_klass_external_name());
 758   }
 759   return k;
 760 }
 761 
 762 InstanceKlass* ClassListParser::lookup_interface_for_current_class(Symbol* interface_name) {
 763   if (!is_loading_from_source()) {
 764     return nullptr;
 765   }
 766 
 767   const int n = _interfaces->length();
 768   if (n == 0) {
 769     error("Class %s implements the interface %s, but no interface has been specified in the input line",
 770           _class_name, interface_name->as_klass_external_name());
 771     ShouldNotReachHere();
 772   }
 773 
 774   int i;
 775   for (i=0; i<n; i++) {
 776     InstanceKlass* k = lookup_class_by_id(_interfaces->at(i));
 777     if (interface_name == k->name()) {
 778       return k;
 779     }
 780   }
 781 
 782   // interface_name is not specified by the "interfaces:" keyword.
 783   print_specified_interfaces();
 784   error("The interface %s implemented by class %s does not match any of the specified interface IDs",
 785         interface_name->as_klass_external_name(), _class_name);
 786   ShouldNotReachHere();
 787   return nullptr;
 788 }
 789 
 790 InstanceKlass* ClassListParser::find_builtin_class_helper(JavaThread* current, Symbol* class_name_symbol, oop class_loader_oop) {
 791   Handle class_loader(current, class_loader_oop);
 792   Handle protection_domain;
 793   return SystemDictionary::find_instance_klass(current, class_name_symbol, class_loader, protection_domain);
 794 }
 795 
 796 InstanceKlass* ClassListParser::find_builtin_class(JavaThread* current, const char* class_name) {
 797   TempNewSymbol class_name_symbol = SymbolTable::new_symbol(class_name);
 798   InstanceKlass* ik;
 799 
 800   if ( (ik = find_builtin_class_helper(current, class_name_symbol, nullptr)) != nullptr
 801     || (ik = find_builtin_class_helper(current, class_name_symbol, SystemDictionary::java_platform_loader())) != nullptr
 802     || (ik = find_builtin_class_helper(current, class_name_symbol, SystemDictionary::java_system_loader())) != nullptr) {
 803     return ik;
 804   } else {
 805     return nullptr;
 806   }
 807 }
 808 
 809 void ClassListParser::parse_constant_pool_tag() {
 810   if (_parse_mode == _parse_lambda_forms_invokers_only) {
 811     return;
 812   }
 813 
 814   JavaThread* THREAD = JavaThread::current();
 815   skip_whitespaces();
 816   char* class_name = _token;
 817   skip_non_whitespaces();
 818   *_token = '\0';
 819   _token ++;
 820 
 821   InstanceKlass* ik = find_builtin_class(THREAD, class_name);
 822   if (ik == nullptr) {
 823     _token = class_name;
 824     if (strstr(class_name, "/$Proxy") != nullptr ||
 825         strstr(class_name, "MethodHandle$Species_") != nullptr) {
 826       // ignore -- TODO: we should filter these out in classListWriter.cpp
 827     } else {
 828       constant_pool_resolution_warning("class %s is not (yet) loaded by one of the built-in loaders", class_name);
 829     }
 830     return;
 831   }
 832 
 833   ResourceMark rm(THREAD);
 834   constantPoolHandle cp(THREAD, ik->constants());
 835   GrowableArray<bool> preresolve_list(cp->length(), cp->length(), false);
 836   bool preresolve_class = false;
 837   bool preresolve_fmi = false;
 838   bool preresolve_indy = false;
 839   
 840   while (*_token) {
 841     int cp_index;
 842     skip_whitespaces();
 843     parse_uint(&cp_index);
 844     if (cp_index < 1 || cp_index >= cp->length()) {
 845       constant_pool_resolution_warning("Invalid constant pool index %d", cp_index);
 846       return;
 847     } else {
 848       preresolve_list.at_put(cp_index, true);
 849     }
 850     constantTag cp_tag = cp->tag_at(cp_index);
 851     switch (cp_tag.value()) {
 852     case JVM_CONSTANT_UnresolvedClass:
 853       preresolve_class = true;
 854       break;
 855     case JVM_CONSTANT_UnresolvedClassInError:
 856     case JVM_CONSTANT_Class:
 857       // ignore
 858     case JVM_CONSTANT_Fieldref:
 859     case JVM_CONSTANT_Methodref:
 860     case JVM_CONSTANT_InterfaceMethodref:
 861       preresolve_fmi = true;
 862       break;
 863     case JVM_CONSTANT_InvokeDynamic:
 864       preresolve_indy = true;
 865       break;
 866     default:
 867       constant_pool_resolution_warning("Unsupported constant pool index %d: %s (type=%d)",
 868                                        cp_index, cp_tag.internal_name(), cp_tag.value());
 869       return;
 870     }
 871   }
 872 
 873   if (preresolve_class) {
 874     ClassPrelinker::preresolve_class_cp_entries(THREAD, ik, &preresolve_list);
 875   }
 876   if (preresolve_fmi) {
 877 // FIXME: too coarse; doesn't cover resolution of Class entries
 878 //    JavaThread::NoJavaCodeMark no_java_code(THREAD); // ensure no clinits are exectued
 879     ClassPrelinker::preresolve_field_and_method_cp_entries(THREAD, ik, &preresolve_list);
 880   }
 881   if (preresolve_indy) {
 882     ClassPrelinker::preresolve_indy_cp_entries(THREAD, ik, &preresolve_list);
 883   }
 884 }
 885 
 886 void ClassListParser::parse_class_reflection_data_tag() {
 887   if (_parse_mode == _parse_lambda_forms_invokers_only) {
 888     return;
 889   }
 890 
 891   JavaThread* THREAD = JavaThread::current();
 892   skip_whitespaces();
 893   char* class_name = _token;
 894   skip_non_whitespaces();
 895   *_token = '\0';
 896   _token ++;
 897 
 898   InstanceKlass* ik = find_builtin_class(THREAD, class_name);
 899   if (ik == nullptr) {
 900     _token = class_name;
 901     if (strstr(class_name, "/$Proxy") != nullptr ||
 902         strstr(class_name, "MethodHandle$Species_") != nullptr) {
 903       // ignore -- TODO: we should filter these out in classListWriter.cpp
 904     } else {
 905       warning("%s: class not found: %s", CLASS_REFLECTION_DATA_TAG, class_name);
 906     }
 907     return;
 908   }
 909 
 910   ResourceMark rm(THREAD);
 911 
 912   int rd_flags = _unspecified;
 913   while (*_token) {
 914     skip_whitespaces();
 915     if (rd_flags != _unspecified) {
 916       error("rd_flags specified twice");
 917       return;
 918     }
 919     parse_uint(&rd_flags);
 920   }
 921   if (rd_flags == _unspecified) {
 922     error("no rd_flags specified");
 923     return;
 924   }
 925 
 926   if (ArchiveReflectionData) {
 927     ClassPrelinker::generate_reflection_data(THREAD, ik, rd_flags);
 928   }
 929 }
 930 
 931 oop ClassListParser::loader_from_type(const char* loader_type) {
 932   oop loader;
 933   if (!ArchiveUtils::builtin_loader_from_type(loader_type, &loader)) {
 934     error("Unknown loader %s", loader_type);
 935   }
 936   return loader;
 937 }
 938 
 939 void ClassListParser::parse_dynamic_proxy_tag() {
 940   if (_parse_mode == _parse_lambda_forms_invokers_only) {
 941     return;
 942   }
 943 
 944   skip_whitespaces();
 945   char* loader_type = _token;
 946   skip_non_whitespaces();
 947   *_token = '\0';
 948   _token ++;
 949 
 950   skip_whitespaces();
 951   char* proxy_name_str = _token;
 952   skip_non_whitespaces();
 953   *_token = '\0';
 954   _token ++;
 955 
 956   skip_whitespaces();
 957   int access_flags;
 958   parse_uint(&access_flags);
 959 
 960   skip_whitespaces();
 961   int num_intfs;
 962   parse_uint(&num_intfs);
 963 
 964   JavaThread* THREAD = JavaThread::current();
 965   Handle loader(THREAD, loader_from_type(loader_type));
 966   Handle proxy_name(THREAD, java_lang_String::create_oop_from_str(proxy_name_str, THREAD));
 967   if (HAS_PENDING_EXCEPTION) {
 968     error("Out of memory");
 969   }
 970 
 971   objArrayHandle interfaces(THREAD, oopFactory::new_objArray(vmClasses::Class_klass(), num_intfs, THREAD));
 972   if (HAS_PENDING_EXCEPTION) {
 973     error("Out of memory");
 974   }
 975 
 976   for (int i = 0; i < num_intfs; i++) {
 977     skip_whitespaces();
 978     char* intf_name = _token;
 979     skip_non_whitespaces();
 980     *_token = '\0';
 981     _token ++;
 982 
 983     InstanceKlass* ik = find_builtin_class(THREAD, intf_name);
 984     if (ik != nullptr) {
 985       interfaces()->obj_at_put(i, ik->java_mirror());
 986     } else {
 987       error("Unknown class %s", intf_name);
 988     }
 989   }
 990 
 991   if (strncmp("jdk.proxy", proxy_name_str, 9) != 0) {
 992     return;
 993   }
 994 
 995   ClassPrelinker::define_dynamic_proxy_class(loader, proxy_name, interfaces, access_flags, THREAD);
 996   if (HAS_PENDING_EXCEPTION) {
 997     PENDING_EXCEPTION->print_on(tty);
 998     error("defineProxyClassForCDS failed");
 999   }
1000 }
1001 
1002 void ClassListParser::parse_loader_negative_cache_tag() {
1003   skip_whitespaces();
1004   char* loader_type = _token;
1005   skip_non_whitespaces();
1006   *_token = '\0';
1007   _token ++;
1008 
1009   oop loader;
1010   Klass* loader_klass;
1011   if (!strcmp(loader_type, "app")) {
1012     loader = SystemDictionary::java_system_loader();
1013     loader_klass = vmClasses::jdk_internal_loader_ClassLoaders_AppClassLoader_klass();
1014   } else if (!strcmp(loader_type, "platform")) {
1015     loader = SystemDictionary::java_platform_loader();
1016     loader_klass = vmClasses::jdk_internal_loader_ClassLoaders_PlatformClassLoader_klass();
1017   } else {
1018     warning("%s: unrecognized loader type %s is ignored", LOADER_NEGATIVE_CACHE_TAG, loader_type);
1019     return;
1020   }
1021 
1022   char* contents = _token;
1023   skip_non_whitespaces();
1024   *_token = '\0';
1025   _token ++;
1026 
1027   if (ArchiveLoaderLookupCache) {
1028     TempNewSymbol method = SymbolTable::new_symbol("generateNegativeLookupCache");
1029     TempNewSymbol signature = SymbolTable::new_symbol("(Ljava/lang/String;)V");
1030 
1031     EXCEPTION_MARK;
1032     HandleMark hm(THREAD);
1033     JavaCallArguments args(Handle(THREAD, loader));
1034     Handle contents_h = java_lang_String::create_from_str(contents, THREAD);
1035     args.push_oop(contents_h);
1036     JavaValue result(T_VOID);
1037     JavaCalls::call_virtual(&result,
1038                             loader_klass,
1039                             method,
1040                             signature,
1041                             &args, THREAD);
1042     if (HAS_PENDING_EXCEPTION) {
1043       Handle exc_handle(THREAD, PENDING_EXCEPTION);
1044       CLEAR_PENDING_EXCEPTION;
1045 
1046       log_warning(cds)("Exception during BuiltinClassLoader::generateNegativeLookupCache() call for %s loader", loader_type);
1047       LogStreamHandle(Debug, cds) log;
1048       if (log.is_enabled()) {
1049         java_lang_Throwable::print_stack_trace(exc_handle, &log);
1050       }
1051     }
1052   }
1053 }