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/classListParser.hpp"
 28 #include "cds/lambdaFormInvokers.hpp"
 29 #include "cds/metaspaceShared.hpp"
 30 #include "cds/unregisteredClasses.hpp"
 31 #include "classfile/classLoaderExt.hpp"
 32 #include "classfile/javaClasses.inline.hpp"
 33 #include "classfile/symbolTable.hpp"
 34 #include "classfile/systemDictionary.hpp"
 35 #include "classfile/systemDictionaryShared.hpp"
 36 #include "classfile/vmClasses.hpp"
 37 #include "classfile/vmSymbols.hpp"
 38 #include "interpreter/bytecode.hpp"
 39 #include "interpreter/bytecodeStream.hpp"
 40 #include "interpreter/linkResolver.hpp"
 41 #include "jimage.hpp"
 42 #include "jvm.h"
 43 #include "logging/log.hpp"
 44 #include "logging/logTag.hpp"
 45 #include "memory/resourceArea.hpp"
 46 #include "oops/constantPool.hpp"
 47 #include "runtime/atomic.hpp"
 48 #include "runtime/handles.inline.hpp"
 49 #include "runtime/java.hpp"
 50 #include "runtime/javaCalls.hpp"
 51 #include "utilities/defaultStream.hpp"
 52 #include "utilities/macros.hpp"
 53 
 54 volatile Thread* ClassListParser::_parsing_thread = nullptr;
 55 ClassListParser* ClassListParser::_instance = nullptr;
 56 
 57 ClassListParser::ClassListParser(const char* file, ParseMode parse_mode) : _id2klass_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE) {
 58   log_info(cds)("Parsing %s%s", file,
 59                 (parse_mode == _parse_lambda_forms_invokers_only) ? " (lambda form invokers only)" : "");
 60   _classlist_file = file;
 61   _file = nullptr;
 62   // Use os::open() because neither fopen() nor os::fopen()
 63   // can handle long path name on Windows.
 64   int fd = os::open(file, O_RDONLY, S_IREAD);
 65   if (fd != -1) {
 66     // Obtain a File* from the file descriptor so that fgets()
 67     // can be used in parse_one_line()
 68     _file = os::fdopen(fd, "r");
 69   }
 70   if (_file == nullptr) {
 71     char errmsg[JVM_MAXPATHLEN];
 72     os::lasterror(errmsg, JVM_MAXPATHLEN);
 73     vm_exit_during_initialization("Loading classlist failed", errmsg);
 74   }
 75   _line_no = 0;
 76   _interfaces = new (mtClass) GrowableArray<int>(10, mtClass);
 77   _indy_items = new (mtClass) GrowableArray<const char*>(9, mtClass);
 78   _parse_mode = parse_mode;
 79 
 80   // _instance should only be accessed by the thread that created _instance.
 81   assert(_instance == nullptr, "must be singleton");
 82   _instance = this;
 83   Atomic::store(&_parsing_thread, Thread::current());
 84 }
 85 
 86 bool ClassListParser::is_parsing_thread() {
 87   return Atomic::load(&_parsing_thread) == Thread::current();
 88 }
 89 
 90 ClassListParser::~ClassListParser() {
 91   if (_file != nullptr) {
 92     fclose(_file);
 93   }
 94   Atomic::store(&_parsing_thread, (Thread*)nullptr);
 95   delete _indy_items;
 96   delete _interfaces;
 97   _instance = nullptr;
 98 }
 99 
100 int ClassListParser::parse(TRAPS) {
101   int class_count = 0;
102 
103   while (parse_one_line()) {
104     if (lambda_form_line()) {
105       // The current line is "@lambda-form-invoker ...". It has been recorded in LambdaFormInvokers,
106       // and will be processed later.
107       continue;
108     }
109 
110     if (_parse_mode == _parse_lambda_forms_invokers_only) {
111       continue;
112     }
113 
114     TempNewSymbol class_name_symbol = SymbolTable::new_symbol(_class_name);
115     if (_indy_items->length() > 0) {
116       // The current line is "@lambda-proxy class_name". Load the proxy class.
117       resolve_indy(THREAD, class_name_symbol);
118       class_count++;
119       continue;
120     }
121 
122     Klass* klass = load_current_class(class_name_symbol, THREAD);
123     if (HAS_PENDING_EXCEPTION) {
124       if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
125         // If we have run out of memory, don't try to load the rest of the classes in
126         // the classlist. Throw an exception, which will terminate the dumping process.
127         return 0; // THROW
128       }
129 
130       ResourceMark rm(THREAD);
131       char* ex_msg = (char*)"";
132       oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
133       if (message != nullptr) {
134         ex_msg = java_lang_String::as_utf8_string(message);
135       }
136       log_warning(cds)("%s: %s", PENDING_EXCEPTION->klass()->external_name(), ex_msg);
137       // We might have an invalid class name or an bad class. Warn about it
138       // and keep going to the next line.
139       CLEAR_PENDING_EXCEPTION;
140       log_warning(cds)("Preload Warning: Cannot find %s", _class_name);
141       continue;
142     }
143 
144     assert(klass != nullptr, "sanity");
145     if (log_is_enabled(Trace, cds)) {
146       ResourceMark rm(THREAD);
147       log_trace(cds)("Shared spaces preloaded: %s", klass->external_name());
148     }
149 
150     if (klass->is_instance_klass()) {
151       InstanceKlass* ik = InstanceKlass::cast(klass);
152 
153       // Link the class to cause the bytecodes to be rewritten and the
154       // cpcache to be created. The linking is done as soon as classes
155       // are loaded in order that the related data structures (klass and
156       // cpCache) are located together.
157       MetaspaceShared::try_link_class(THREAD, ik);
158     }
159 
160     class_count++;
161   }
162 
163   return class_count;
164 }
165 
166 bool ClassListParser::parse_one_line() {
167   for (;;) {
168     if (fgets(_line, sizeof(_line), _file) == nullptr) {
169       return false;
170     }
171     ++ _line_no;
172     _line_len = (int)strlen(_line);
173     if (_line_len > _max_allowed_line_len) {
174       error("input line too long (must be no longer than %d chars)", _max_allowed_line_len);
175     }
176     if (*_line == '#') { // comment
177       continue;
178     }
179 
180     {
181       int len = (int)strlen(_line);
182       int i;
183       // Replace \t\r\n\f with ' '
184       for (i=0; i<len; i++) {
185         if (_line[i] == '\t' || _line[i] == '\r' || _line[i] == '\n' || _line[i] == '\f') {
186           _line[i] = ' ';
187         }
188       }
189 
190       // Remove trailing newline/space
191       while (len > 0) {
192         if (_line[len-1] == ' ') {
193           _line[len-1] = '\0';
194           len --;
195         } else {
196           break;
197         }
198       }
199       _line_len = len;
200     }
201 
202     // valid line
203     break;
204   }
205 
206   _class_name = _line;
207   _id = _unspecified;
208   _super = _unspecified;
209   _interfaces->clear();
210   _source = nullptr;
211   _interfaces_specified = false;
212   _indy_items->clear();
213   _lambda_form_line = false;
214 
215   if (_line[0] == '@') {
216     return parse_at_tags();
217   }
218 
219   if ((_token = strchr(_line, ' ')) == nullptr) {
220     // No optional arguments are specified.
221     return true;
222   }
223 
224   // Mark the end of the name, and go to the next input char
225   *_token++ = '\0';
226 
227   while (*_token) {
228     skip_whitespaces();
229 
230     if (parse_uint_option("id:", &_id)) {
231       continue;
232     } else if (parse_uint_option("super:", &_super)) {
233       check_already_loaded("Super class", _super);
234       continue;
235     } else if (skip_token("interfaces:")) {
236       int i;
237       while (try_parse_uint(&i)) {
238         check_already_loaded("Interface", i);
239         _interfaces->append(i);
240       }
241     } else if (skip_token("source:")) {
242       skip_whitespaces();
243       _source = _token;
244       char* s = strchr(_token, ' ');
245       if (s == nullptr) {
246         break; // end of input line
247       } else {
248         *s = '\0'; // mark the end of _source
249         _token = s+1;
250       }
251     } else {
252       error("Unknown input");
253     }
254   }
255 
256   // if src is specified
257   //     id super interfaces must all be specified
258   //     loader may be specified
259   // else
260   //     # the class is loaded from classpath
261   //     id may be specified
262   //     super, interfaces, loader must not be specified
263   return true;
264 }
265 
266 void ClassListParser::split_tokens_by_whitespace(int offset) {
267   int start = offset;
268   int end;
269   bool done = false;
270   while (!done) {
271     while (_line[start] == ' ' || _line[start] == '\t') start++;
272     end = start;
273     while (_line[end] && _line[end] != ' ' && _line[end] != '\t') end++;
274     if (_line[end] == '\0') {
275       done = true;
276     } else {
277       _line[end] = '\0';
278     }
279     _indy_items->append(_line + start);
280     start = ++end;
281   }
282 }
283 
284 int ClassListParser::split_at_tag_from_line() {
285   _token = _line;
286   char* ptr;
287   if ((ptr = strchr(_line, ' ')) == nullptr) {
288     error("Too few items following the @ tag \"%s\" line #%d", _line, _line_no);
289     return 0;
290   }
291   *ptr++ = '\0';
292   while (*ptr == ' ' || *ptr == '\t') ptr++;
293   return (int)(ptr - _line);
294 }
295 
296 bool ClassListParser::parse_at_tags() {
297   assert(_line[0] == '@', "must be");
298   int offset;
299   if ((offset = split_at_tag_from_line()) == 0) {
300     return false;
301   }
302 
303   if (strcmp(_token, LAMBDA_PROXY_TAG) == 0) {
304     split_tokens_by_whitespace(offset);
305     if (_indy_items->length() < 2) {
306       error("Line with @ tag has too few items \"%s\" line #%d", _token, _line_no);
307       return false;
308     }
309     // set the class name
310     _class_name = _indy_items->at(0);
311     return true;
312   } else if (strcmp(_token, LAMBDA_FORM_TAG) == 0) {
313     LambdaFormInvokers::append(os::strdup((const char*)(_line + offset), mtInternal));
314     _lambda_form_line = true;
315     return true;
316   } else {
317     error("Invalid @ tag at the beginning of line \"%s\" line #%d", _token, _line_no);
318     return false;
319   }
320 }
321 
322 void ClassListParser::skip_whitespaces() {
323   while (*_token == ' ' || *_token == '\t') {
324     _token ++;
325   }
326 }
327 
328 void ClassListParser::skip_non_whitespaces() {
329   while (*_token && *_token != ' ' && *_token != '\t') {
330     _token ++;
331   }
332 }
333 
334 void ClassListParser::parse_int(int* value) {
335   skip_whitespaces();
336   if (sscanf(_token, "%i", value) == 1) {
337     skip_non_whitespaces();
338   } else {
339     error("Error: expected integer");
340   }
341 }
342 
343 void ClassListParser::parse_uint(int* value) {
344   parse_int(value);
345   if (*value < 0) {
346     error("Error: negative integers not allowed (%d)", *value);
347   }
348 }
349 
350 bool ClassListParser::try_parse_uint(int* value) {
351   skip_whitespaces();
352   if (sscanf(_token, "%i", value) == 1) {
353     skip_non_whitespaces();
354     return true;
355   }
356   return false;
357 }
358 
359 bool ClassListParser::skip_token(const char* option_name) {
360   size_t len = strlen(option_name);
361   if (strncmp(_token, option_name, len) == 0) {
362     _token += len;
363     return true;
364   } else {
365     return false;
366   }
367 }
368 
369 bool ClassListParser::parse_int_option(const char* option_name, int* value) {
370   if (skip_token(option_name)) {
371     if (*value != _unspecified) {
372       error("%s specified twice", option_name);
373     } else {
374       parse_int(value);
375       return true;
376     }
377   }
378   return false;
379 }
380 
381 bool ClassListParser::parse_uint_option(const char* option_name, int* value) {
382   if (skip_token(option_name)) {
383     if (*value != _unspecified) {
384       error("%s specified twice", option_name);
385     } else {
386       parse_uint(value);
387       return true;
388     }
389   }
390   return false;
391 }
392 
393 void ClassListParser::print_specified_interfaces() {
394   const int n = _interfaces->length();
395   jio_fprintf(defaultStream::error_stream(), "Currently specified interfaces[%d] = {\n", n);
396   for (int i=0; i<n; i++) {
397     InstanceKlass* k = lookup_class_by_id(_interfaces->at(i));
398     jio_fprintf(defaultStream::error_stream(), "  %4d = %s\n", _interfaces->at(i), k->name()->as_klass_external_name());
399   }
400   jio_fprintf(defaultStream::error_stream(), "}\n");
401 }
402 
403 void ClassListParser::print_actual_interfaces(InstanceKlass* ik) {
404   int n = ik->local_interfaces()->length();
405   jio_fprintf(defaultStream::error_stream(), "Actual interfaces[%d] = {\n", n);
406   for (int i = 0; i < n; i++) {
407     InstanceKlass* e = ik->local_interfaces()->at(i);
408     jio_fprintf(defaultStream::error_stream(), "  %s\n", e->name()->as_klass_external_name());
409   }
410   jio_fprintf(defaultStream::error_stream(), "}\n");
411 }
412 
413 void ClassListParser::error(const char* msg, ...) {
414   va_list ap;
415   va_start(ap, msg);
416   int error_index = _token - _line;
417   if (error_index >= _line_len) {
418     error_index = _line_len - 1;
419   }
420   if (error_index < 0) {
421     error_index = 0;
422   }
423 
424   jio_fprintf(defaultStream::error_stream(),
425               "An error has occurred while processing class list file %s %d:%d.\n",
426               _classlist_file, _line_no, (error_index + 1));
427   jio_vfprintf(defaultStream::error_stream(), msg, ap);
428 
429   if (_line_len <= 0) {
430     jio_fprintf(defaultStream::error_stream(), "\n");
431   } else {
432     jio_fprintf(defaultStream::error_stream(), ":\n");
433     for (int i=0; i<_line_len; i++) {
434       char c = _line[i];
435       if (c == '\0') {
436         jio_fprintf(defaultStream::error_stream(), "%s", " ");
437       } else {
438         jio_fprintf(defaultStream::error_stream(), "%c", c);
439       }
440     }
441     jio_fprintf(defaultStream::error_stream(), "\n");
442     for (int i=0; i<error_index; i++) {
443       jio_fprintf(defaultStream::error_stream(), "%s", " ");
444     }
445     jio_fprintf(defaultStream::error_stream(), "^\n");
446   }
447 
448   vm_exit_during_initialization("class list format error.", nullptr);
449   va_end(ap);
450 }
451 
452 // This function is used for loading classes for customized class loaders
453 // during archive dumping.
454 InstanceKlass* ClassListParser::load_class_from_source(Symbol* class_name, TRAPS) {
455 #if !(defined(_LP64) && (defined(LINUX) || defined(__APPLE__) || defined(_WINDOWS)))
456   // The only supported platforms are: (1) Linux/64-bit and (2) Solaris/64-bit and
457   // (3) MacOSX/64-bit and (4) Windowss/64-bit
458   // This #if condition should be in sync with the areCustomLoadersSupportedForCDS
459   // method in test/lib/jdk/test/lib/Platform.java.
460   error("AppCDS custom class loaders not supported on this platform");
461 #endif
462 
463   if (!is_super_specified()) {
464     error("If source location is specified, super class must be also specified");
465   }
466   if (!is_id_specified()) {
467     error("If source location is specified, id must be also specified");
468   }
469   if (strncmp(_class_name, "java/", 5) == 0) {
470     log_info(cds)("Prohibited package for non-bootstrap classes: %s.class from %s",
471           _class_name, _source);
472     THROW_NULL(vmSymbols::java_lang_ClassNotFoundException());
473   }
474 
475   InstanceKlass* k = UnregisteredClasses::load_class(class_name, _source, CHECK_NULL);
476   if (k->local_interfaces()->length() != _interfaces->length()) {
477     print_specified_interfaces();
478     print_actual_interfaces(k);
479     error("The number of interfaces (%d) specified in class list does not match the class file (%d)",
480           _interfaces->length(), k->local_interfaces()->length());
481   }
482 
483   assert(k->is_shared_unregistered_class(), "must be");
484 
485   bool added = SystemDictionaryShared::add_unregistered_class(THREAD, k);
486   if (!added) {
487     // We allow only a single unregistered class for each unique name.
488     error("Duplicated class %s", _class_name);
489   }
490 
491   return k;
492 }
493 
494 void ClassListParser::populate_cds_indy_info(const constantPoolHandle &pool, int cp_index, CDSIndyInfo* cii, TRAPS) {
495   // Caller needs to allocate ResourceMark.
496   int type_index = pool->bootstrap_name_and_type_ref_index_at(cp_index);
497   int name_index = pool->name_ref_index_at(type_index);
498   cii->add_item(pool->symbol_at(name_index)->as_C_string());
499   int sig_index = pool->signature_ref_index_at(type_index);
500   cii->add_item(pool->symbol_at(sig_index)->as_C_string());
501   int argc = pool->bootstrap_argument_count_at(cp_index);
502   if (argc > 0) {
503     for (int arg_i = 0; arg_i < argc; arg_i++) {
504       int arg = pool->bootstrap_argument_index_at(cp_index, arg_i);
505       jbyte tag = pool->tag_at(arg).value();
506       if (tag == JVM_CONSTANT_MethodType) {
507         cii->add_item(pool->method_type_signature_at(arg)->as_C_string());
508       } else if (tag == JVM_CONSTANT_MethodHandle) {
509         cii->add_ref_kind(pool->method_handle_ref_kind_at(arg));
510         int callee_index = pool->method_handle_klass_index_at(arg);
511         Klass* callee = pool->klass_at(callee_index, CHECK);
512         cii->add_item(callee->name()->as_C_string());
513         cii->add_item(pool->method_handle_name_ref_at(arg)->as_C_string());
514         cii->add_item(pool->method_handle_signature_ref_at(arg)->as_C_string());
515       } else {
516         ShouldNotReachHere();
517       }
518     }
519   }
520 }
521 
522 bool ClassListParser::is_matching_cp_entry(const constantPoolHandle &pool, int cp_index, TRAPS) {
523   ResourceMark rm(THREAD);
524   CDSIndyInfo cii;
525   populate_cds_indy_info(pool, cp_index, &cii, CHECK_0);
526   GrowableArray<const char*>* items = cii.items();
527   int indy_info_offset = 1;
528   if (_indy_items->length() - indy_info_offset != items->length()) {
529     return false;
530   }
531   for (int i = 0; i < items->length(); i++) {
532     if (strcmp(_indy_items->at(i + indy_info_offset), items->at(i)) != 0) {
533       return false;
534     }
535   }
536   return true;
537 }
538 
539 void ClassListParser::resolve_indy(JavaThread* current, Symbol* class_name_symbol) {
540   ExceptionMark em(current);
541   JavaThread* THREAD = current; // For exception macros.
542   ClassListParser::resolve_indy_impl(class_name_symbol, THREAD);
543   if (HAS_PENDING_EXCEPTION) {
544     ResourceMark rm(current);
545     char* ex_msg = (char*)"";
546     oop message = java_lang_Throwable::message(PENDING_EXCEPTION);
547     if (message != nullptr) {
548       ex_msg = java_lang_String::as_utf8_string(message);
549     }
550     log_warning(cds)("resolve_indy for class %s has encountered exception: %s %s",
551                      class_name_symbol->as_C_string(),
552                      PENDING_EXCEPTION->klass()->external_name(),
553                      ex_msg);
554     CLEAR_PENDING_EXCEPTION;
555   }
556 }
557 
558 void ClassListParser::resolve_indy_impl(Symbol* class_name_symbol, TRAPS) {
559   Handle class_loader(THREAD, SystemDictionary::java_system_loader());
560   Handle protection_domain;
561   Klass* klass = SystemDictionary::resolve_or_fail(class_name_symbol, class_loader, protection_domain, true, CHECK);
562   if (klass->is_instance_klass()) {
563     InstanceKlass* ik = InstanceKlass::cast(klass);
564     MetaspaceShared::try_link_class(THREAD, ik);
565     if (!ik->is_linked()) {
566       // Verification of ik has failed
567       return;
568     }
569 
570     ConstantPool* cp = ik->constants();
571     ConstantPoolCache* cpcache = cp->cache();
572     bool found = false;
573     for (int indy_index = 0; indy_index < cpcache->resolved_indy_entries_length(); indy_index++) {
574       int pool_index = cpcache->resolved_indy_entry_at(indy_index)->constant_pool_index();
575       constantPoolHandle pool(THREAD, cp);
576       BootstrapInfo bootstrap_specifier(pool, pool_index, indy_index);
577       Handle bsm = bootstrap_specifier.resolve_bsm(CHECK);
578       if (!SystemDictionaryShared::is_supported_invokedynamic(&bootstrap_specifier)) {
579         log_debug(cds, lambda)("is_supported_invokedynamic check failed for cp_index %d", pool_index);
580         continue;
581       }
582       bool matched = is_matching_cp_entry(pool, pool_index, CHECK);
583       if (matched) {
584         found = true;
585         CallInfo info;
586         bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(info, CHECK);
587         if (!is_done) {
588           // resolve it
589           Handle recv;
590           LinkResolver::resolve_invoke(info,
591                                        recv,
592                                        pool,
593                                        ConstantPool::encode_invokedynamic_index(indy_index),
594                                        Bytecodes::_invokedynamic, CHECK);
595           break;
596         }
597         cpcache->set_dynamic_call(info, indy_index);
598       }
599     }
600     if (!found) {
601       ResourceMark rm(THREAD);
602       log_warning(cds)("No invoke dynamic constant pool entry can be found for class %s. The classlist is probably out-of-date.",
603                      class_name_symbol->as_C_string());
604     }
605   }
606 }
607 
608 Klass* ClassListParser::load_current_class(Symbol* class_name_symbol, TRAPS) {
609   Klass* klass;
610   if (!is_loading_from_source()) {
611     // Load classes for the boot/platform/app loaders only.
612     if (is_super_specified()) {
613       error("If source location is not specified, super class must not be specified");
614     }
615     if (are_interfaces_specified()) {
616       error("If source location is not specified, interface(s) must not be specified");
617     }
618 
619     if (Signature::is_array(class_name_symbol)) {
620       // array classes are not supported in class list.
621       THROW_NULL(vmSymbols::java_lang_ClassNotFoundException());
622     }
623 
624     JavaValue result(T_OBJECT);
625     // Call java_system_loader().loadClass() directly, which will
626     // delegate to the correct loader (boot, platform or app) depending on
627     // the package name.
628 
629     // ClassLoader.loadClass() wants external class name format, i.e., convert '/' chars to '.'
630     Handle ext_class_name = java_lang_String::externalize_classname(class_name_symbol, CHECK_NULL);
631     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
632 
633     JavaCalls::call_virtual(&result,
634                             loader, //SystemDictionary::java_system_loader(),
635                             vmClasses::ClassLoader_klass(),
636                             vmSymbols::loadClass_name(),
637                             vmSymbols::string_class_signature(),
638                             ext_class_name,
639                             CHECK_NULL);
640 
641     assert(result.get_type() == T_OBJECT, "just checking");
642     oop obj = result.get_oop();
643     assert(obj != nullptr, "jdk.internal.loader.BuiltinClassLoader::loadClass never returns null");
644     klass = java_lang_Class::as_Klass(obj);
645   } else {
646     // If "source:" tag is specified, all super class and super interfaces must be specified in the
647     // class list file.
648     klass = load_class_from_source(class_name_symbol, CHECK_NULL);
649   }
650 
651   assert(klass != nullptr, "exception should have been thrown");
652   assert(klass->is_instance_klass(), "array classes should have been filtered out");
653 
654   if (is_id_specified()) {
655     InstanceKlass* ik = InstanceKlass::cast(klass);
656     int id = this->id();
657     SystemDictionaryShared::update_shared_entry(ik, id);
658     bool created;
659     id2klass_table()->put_if_absent(id, ik, &created);
660     if (!created) {
661       error("Duplicated ID %d for class %s", id, _class_name);
662     }
663     if (id2klass_table()->maybe_grow()) {
664       log_info(cds, hashtables)("Expanded id2klass_table() to %d", id2klass_table()->table_size());
665     }
666   }
667 
668   return klass;
669 }
670 
671 bool ClassListParser::is_loading_from_source() {
672   return (_source != nullptr);
673 }
674 
675 InstanceKlass* ClassListParser::lookup_class_by_id(int id) {
676   InstanceKlass** klass_ptr = id2klass_table()->get(id);
677   if (klass_ptr == nullptr) {
678     error("Class ID %d has not been defined", id);
679   }
680   assert(*klass_ptr != nullptr, "must be");
681   return *klass_ptr;
682 }
683 
684 
685 InstanceKlass* ClassListParser::lookup_super_for_current_class(Symbol* super_name) {
686   if (!is_loading_from_source()) {
687     return nullptr;
688   }
689 
690   InstanceKlass* k = lookup_class_by_id(super());
691   if (super_name != k->name()) {
692     error("The specified super class %s (id %d) does not match actual super class %s",
693           k->name()->as_klass_external_name(), super(),
694           super_name->as_klass_external_name());
695   }
696   return k;
697 }
698 
699 InstanceKlass* ClassListParser::lookup_interface_for_current_class(Symbol* interface_name) {
700   if (!is_loading_from_source()) {
701     return nullptr;
702   }
703 
704   const int n = _interfaces->length();
705   if (n == 0) {
706     error("Class %s implements the interface %s, but no interface has been specified in the input line",
707           _class_name, interface_name->as_klass_external_name());
708     ShouldNotReachHere();
709   }
710 
711   int i;
712   for (i=0; i<n; i++) {
713     InstanceKlass* k = lookup_class_by_id(_interfaces->at(i));
714     if (interface_name == k->name()) {
715       return k;
716     }
717   }
718 
719   // interface_name is not specified by the "interfaces:" keyword.
720   print_specified_interfaces();
721   error("The interface %s implemented by class %s does not match any of the specified interface IDs",
722         interface_name->as_klass_external_name(), _class_name);
723   ShouldNotReachHere();
724   return nullptr;
725 }