302 FREE_RESOURCE_ARRAY(char, path, JVM_MAXPATHLEN); 303 return NULL; 304 } 305 306 ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name, bool is_boot_append) : ClassPathEntry() { 307 _zip = zip; 308 char *copy = NEW_C_HEAP_ARRAY(char, strlen(zip_name)+1, mtClass); 309 strcpy(copy, zip_name); 310 _zip_name = copy; 311 _is_boot_append = is_boot_append; 312 _multi_versioned = _unknown; 313 } 314 315 ClassPathZipEntry::~ClassPathZipEntry() { 316 if (ZipClose != NULL) { 317 (*ZipClose)(_zip); 318 } 319 FREE_C_HEAP_ARRAY(char, _zip_name); 320 } 321 322 u1* ClassPathZipEntry::open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS) { 323 // enable call to C land 324 JavaThread* thread = JavaThread::current(); 325 ThreadToNativeFromVM ttn(thread); 326 // check whether zip archive contains name 327 jint name_len; 328 jzentry* entry = (*FindEntry)(_zip, name, filesize, &name_len); 329 if (entry == NULL) return NULL; 330 u1* buffer; 331 char name_buf[128]; 332 char* filename; 333 if (name_len < 128) { 334 filename = name_buf; 335 } else { 336 filename = NEW_RESOURCE_ARRAY(char, name_len + 1); 337 } 338 339 // file found, get pointer to the entry in mmapped jar file. 340 if (ReadMappedEntry == NULL || 341 !(*ReadMappedEntry)(_zip, entry, &buffer, filename)) { 676 void* ClassLoader::get_shared_paths_misc_info() { 677 return _shared_paths_misc_info->buffer(); 678 } 679 680 bool ClassLoader::check_shared_paths_misc_info(void *buf, int size) { 681 SharedPathsMiscInfo* checker = SharedClassUtil::allocate_shared_paths_misc_info((char*)buf, size); 682 bool result = checker->check(); 683 delete checker; 684 return result; 685 } 686 #endif 687 688 // Construct the array of module/path pairs as specified to --patch-module 689 // for the boot loader to search ahead of the jimage, if the class being 690 // loaded is defined to a module that has been specified to --patch-module. 691 void ClassLoader::setup_patch_mod_entries() { 692 Thread* THREAD = Thread::current(); 693 GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix(); 694 int num_of_entries = patch_mod_args->length(); 695 696 assert(!DumpSharedSpaces, "DumpSharedSpaces not supported with --patch-module"); 697 assert(!UseSharedSpaces, "UseSharedSpaces not supported with --patch-module"); 698 699 // Set up the boot loader's _patch_mod_entries list 700 _patch_mod_entries = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, true); 701 702 for (int i = 0; i < num_of_entries; i++) { 703 const char* module_name = (patch_mod_args->at(i))->module_name(); 704 Symbol* const module_sym = SymbolTable::lookup(module_name, (int)strlen(module_name), CHECK); 705 assert(module_sym != NULL, "Failed to obtain Symbol for module name"); 706 ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym); 707 708 char* class_path = (patch_mod_args->at(i))->path_string(); 709 int len = (int)strlen(class_path); 710 int end = 0; 711 // Iterate over the module's class path entries 712 for (int start = 0; start < len; start = end) { 713 while (class_path[end] && class_path[end] != os::path_separator()[0]) { 714 end++; 715 } 716 EXCEPTION_MARK; 717 ResourceMark rm(THREAD); 1378 while (e != NULL) { 1379 stream = e->open_stream(file_name, CHECK_NULL); 1380 // No context.check is required since CDS is not supported 1381 // for an exploded modules build or if --patch-module is specified. 1382 if (NULL != stream) { 1383 return stream; 1384 } 1385 e = e->next(); 1386 } 1387 // If the module was located, break out even if the class was not 1388 // located successfully from that module's ClassPathEntry list. 1389 // There will not be another valid entry for that module. 1390 return NULL; 1391 } 1392 } 1393 } 1394 1395 return NULL; 1396 } 1397 1398 instanceKlassHandle ClassLoader::load_class(Symbol* name, bool search_append_only, TRAPS) { 1399 assert(name != NULL, "invariant"); 1400 assert(THREAD->is_Java_thread(), "must be a JavaThread"); 1401 1402 ResourceMark rm(THREAD); 1403 HandleMark hm(THREAD); 1404 1405 const char* const class_name = name->as_C_string(); 1406 1407 EventMark m("loading class %s", class_name); 1408 ThreadProfilerMark tpm(ThreadProfilerMark::classLoaderRegion); 1409 1410 const char* const file_name = file_name_for_class_name(class_name, 1411 name->utf8_length()); 1412 assert(file_name != NULL, "invariant"); 1413 1414 ClassLoaderExt::Context context(class_name, file_name, THREAD); 1415 1416 // Lookup stream for parsing .class file 1417 ClassFileStream* stream = NULL; 1418 s2 classpath_index = 0; 1419 ClassPathEntry* e = NULL; 1420 1421 // If DumpSharedSpaces is true boot loader visibility boundaries are set to: 1422 // - [jimage] + [_first_append_entry to _last_append_entry] (all path entries). 1423 // No --patch-module entries or exploded module builds are included since CDS 1424 // is not supported if --patch-module or exploded module builds are used. 1425 // 1426 // If search_append_only is true, boot loader visibility boundaries are 1427 // set to be _first_append_entry to the end. This includes: 1428 // [-Xbootclasspath/a]; [jvmti appended entries] 1429 // 1430 // If both DumpSharedSpaces and search_append_only are false, boot loader 1431 // visibility boundaries are set to be the --patch-module entries plus the base piece. 1432 // This would include: 1433 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build] 1434 // 1435 // DumpSharedSpaces and search_append_only are mutually exclusive and cannot 1436 // be true at the same time. 1437 assert(!(DumpSharedSpaces && search_append_only), "DumpSharedSpaces and search_append_only are both true"); 1438 1439 // Load Attempt #1: --patch-module 1440 // Determine the class' defining module. If it appears in the _patch_mod_entries, 1441 // attempt to load the class from those locations specific to the module. 1442 // Specifications to --patch-module can contain a partial number of classes 1443 // that are part of the overall module definition. So if a particular class is not 1444 // found within its module specification, the search should continue to Load Attempt #2. 1445 // Note: The --patch-module entries are never searched if the boot loader's 1446 // visibility boundary is limited to only searching the append entries. 1447 if (_patch_mod_entries != NULL && !search_append_only && !DumpSharedSpaces) { 1448 stream = search_module_entries(_patch_mod_entries, class_name, file_name, CHECK_NULL); 1449 } 1450 1451 // Load Attempt #2: [jimage | exploded build] 1452 if (!search_append_only && (NULL == stream)) { 1453 if (has_jrt_entry()) { 1454 e = _jrt_entry; 1455 stream = _jrt_entry->open_stream(file_name, CHECK_NULL); 1456 if (!context.check(stream, classpath_index)) { 1457 return NULL; 1458 } 1459 } else { 1460 // Exploded build - attempt to locate class in its defining module's location. 1461 assert(_exploded_entries != NULL, "No exploded build entries present"); 1462 stream = search_module_entries(_exploded_entries, class_name, file_name, CHECK_NULL); 1463 } 1464 } 1465 1466 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries] 1467 if ((search_append_only || DumpSharedSpaces) && (NULL == stream)) { 1468 // For the boot loader append path search, the starting classpath_index | 302 FREE_RESOURCE_ARRAY(char, path, JVM_MAXPATHLEN); 303 return NULL; 304 } 305 306 ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name, bool is_boot_append) : ClassPathEntry() { 307 _zip = zip; 308 char *copy = NEW_C_HEAP_ARRAY(char, strlen(zip_name)+1, mtClass); 309 strcpy(copy, zip_name); 310 _zip_name = copy; 311 _is_boot_append = is_boot_append; 312 _multi_versioned = _unknown; 313 } 314 315 ClassPathZipEntry::~ClassPathZipEntry() { 316 if (ZipClose != NULL) { 317 (*ZipClose)(_zip); 318 } 319 FREE_C_HEAP_ARRAY(char, _zip_name); 320 } 321 322 bool ClassPathZipEntry::stream_exists(const char* name) { 323 // enable call to C land 324 JavaThread* thread = JavaThread::current(); 325 ThreadToNativeFromVM ttn(thread); 326 // check whether zip archive contains name 327 jint name_len, filesize; 328 jzentry* entry = (*FindEntry)(_zip, name, &filesize, &name_len); 329 return (entry != NULL) ? true : false; 330 } 331 332 u1* ClassPathZipEntry::open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS) { 333 // enable call to C land 334 JavaThread* thread = JavaThread::current(); 335 ThreadToNativeFromVM ttn(thread); 336 // check whether zip archive contains name 337 jint name_len; 338 jzentry* entry = (*FindEntry)(_zip, name, filesize, &name_len); 339 if (entry == NULL) return NULL; 340 u1* buffer; 341 char name_buf[128]; 342 char* filename; 343 if (name_len < 128) { 344 filename = name_buf; 345 } else { 346 filename = NEW_RESOURCE_ARRAY(char, name_len + 1); 347 } 348 349 // file found, get pointer to the entry in mmapped jar file. 350 if (ReadMappedEntry == NULL || 351 !(*ReadMappedEntry)(_zip, entry, &buffer, filename)) { 686 void* ClassLoader::get_shared_paths_misc_info() { 687 return _shared_paths_misc_info->buffer(); 688 } 689 690 bool ClassLoader::check_shared_paths_misc_info(void *buf, int size) { 691 SharedPathsMiscInfo* checker = SharedClassUtil::allocate_shared_paths_misc_info((char*)buf, size); 692 bool result = checker->check(); 693 delete checker; 694 return result; 695 } 696 #endif 697 698 // Construct the array of module/path pairs as specified to --patch-module 699 // for the boot loader to search ahead of the jimage, if the class being 700 // loaded is defined to a module that has been specified to --patch-module. 701 void ClassLoader::setup_patch_mod_entries() { 702 Thread* THREAD = Thread::current(); 703 GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix(); 704 int num_of_entries = patch_mod_args->length(); 705 706 707 // Set up the boot loader's _patch_mod_entries list 708 _patch_mod_entries = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, true); 709 710 for (int i = 0; i < num_of_entries; i++) { 711 const char* module_name = (patch_mod_args->at(i))->module_name(); 712 Symbol* const module_sym = SymbolTable::lookup(module_name, (int)strlen(module_name), CHECK); 713 assert(module_sym != NULL, "Failed to obtain Symbol for module name"); 714 ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym); 715 716 char* class_path = (patch_mod_args->at(i))->path_string(); 717 int len = (int)strlen(class_path); 718 int end = 0; 719 // Iterate over the module's class path entries 720 for (int start = 0; start < len; start = end) { 721 while (class_path[end] && class_path[end] != os::path_separator()[0]) { 722 end++; 723 } 724 EXCEPTION_MARK; 725 ResourceMark rm(THREAD); 1386 while (e != NULL) { 1387 stream = e->open_stream(file_name, CHECK_NULL); 1388 // No context.check is required since CDS is not supported 1389 // for an exploded modules build or if --patch-module is specified. 1390 if (NULL != stream) { 1391 return stream; 1392 } 1393 e = e->next(); 1394 } 1395 // If the module was located, break out even if the class was not 1396 // located successfully from that module's ClassPathEntry list. 1397 // There will not be another valid entry for that module. 1398 return NULL; 1399 } 1400 } 1401 } 1402 1403 return NULL; 1404 } 1405 1406 #if INCLUDE_CDS 1407 // The following function is only used during CDS dump time. 1408 // It checks if a class can be found in the jar entries of the _patch_mod_entries. 1409 // It does not support non-jar entries. 1410 bool ClassLoader::is_in_patch_module(const char* const file_name) { 1411 assert(DumpSharedSpaces, "dump time only"); 1412 if (_patch_mod_entries == NULL) { 1413 return false; 1414 } 1415 1416 bool found; 1417 int num_of_entries = _patch_mod_entries->length(); 1418 char* class_module_name = NULL; 1419 ResourceMark rm; 1420 const char *pkg_name = package_from_name(file_name); 1421 // Using the jimage to obtain the class' module name. 1422 // The ModuleEntryTable cannot be used at this point during dump time 1423 // because the module system hasn't been initialized yet. 1424 if (pkg_name != NULL) { 1425 JImageFile *jimage = _jrt_entry->jimage(); 1426 class_module_name = (char*)(*JImagePackageToModule)(jimage, pkg_name); 1427 } 1428 1429 if (class_module_name == NULL) { 1430 return false; 1431 } 1432 1433 // Loop through all the patch module entries looking for module 1434 for (int i = 0; i < num_of_entries; i++) { 1435 ModuleClassPathList* module_cpl = _patch_mod_entries->at(i); 1436 Symbol* module_cpl_name = module_cpl->module_name(); 1437 1438 if (strcmp(module_cpl_name->as_C_string(), class_module_name) == 0) { 1439 // Class' module has been located, attempt to locate 1440 // the class from the module's ClassPathEntry list. 1441 ClassPathEntry* e = module_cpl->module_first_entry(); 1442 while (e != NULL) { 1443 if (e->is_jar_file()) { 1444 found = e->stream_exists(file_name); 1445 if (found) { 1446 return true; 1447 } else { 1448 e = e->next(); 1449 } 1450 } else { 1451 if (DumpSharedSpaces) { 1452 vm_exit_during_initialization("Only jar file in --patch-module is supported for CDS dumping ", e->name()); 1453 } 1454 } 1455 } 1456 } 1457 } 1458 1459 return false; 1460 } 1461 #endif // INCLUDE_CDS 1462 1463 instanceKlassHandle ClassLoader::load_class(Symbol* name, bool search_append_only, TRAPS) { 1464 assert(name != NULL, "invariant"); 1465 assert(THREAD->is_Java_thread(), "must be a JavaThread"); 1466 1467 ResourceMark rm(THREAD); 1468 HandleMark hm(THREAD); 1469 1470 const char* const class_name = name->as_C_string(); 1471 1472 EventMark m("loading class %s", class_name); 1473 ThreadProfilerMark tpm(ThreadProfilerMark::classLoaderRegion); 1474 1475 const char* const file_name = file_name_for_class_name(class_name, 1476 name->utf8_length()); 1477 assert(file_name != NULL, "invariant"); 1478 1479 ClassLoaderExt::Context context(class_name, file_name, THREAD); 1480 1481 // Lookup stream for parsing .class file 1482 ClassFileStream* stream = NULL; 1483 s2 classpath_index = 0; 1484 ClassPathEntry* e = NULL; 1485 1486 // If DumpSharedSpaces is true boot loader visibility boundaries are set to: 1487 // - [jimage] + [_first_append_entry to _last_append_entry] (all path entries). 1488 // If a class is found in the --patch-module entries, the class will not be included in the 1489 // CDS archive. Also, CDS is not supported if exploeded module builds are used. 1490 // 1491 // If search_append_only is true, boot loader visibility boundaries are 1492 // set to be _first_append_entry to the end. This includes: 1493 // [-Xbootclasspath/a]; [jvmti appended entries] 1494 // 1495 // If both DumpSharedSpaces and search_append_only are false, boot loader 1496 // visibility boundaries are set to be the --patch-module entries plus the base piece. 1497 // This would include: 1498 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build] 1499 // 1500 // DumpSharedSpaces and search_append_only are mutually exclusive and cannot 1501 // be true at the same time. 1502 assert(!(DumpSharedSpaces && search_append_only), "DumpSharedSpaces and search_append_only are both true"); 1503 1504 bool found_patch_mod_entry = false; 1505 // Load Attempt #1: --patch-module 1506 // Determine the class' defining module. If it appears in the _patch_mod_entries, 1507 // attempt to load the class from those locations specific to the module. 1508 // Specifications to --patch-module can contain a partial number of classes 1509 // that are part of the overall module definition. So if a particular class is not 1510 // found within its module specification, the search should continue to Load Attempt #2. 1511 // Note: The --patch-module entries are never searched if the boot loader's 1512 // visibility boundary is limited to only searching the append entries. 1513 if (_patch_mod_entries != NULL && !search_append_only) { 1514 if (!DumpSharedSpaces) { 1515 stream = search_module_entries(_patch_mod_entries, class_name, file_name, CHECK_NULL); 1516 } else { 1517 #if INCLUDE_CDS 1518 found_patch_mod_entry = is_in_patch_module(file_name); 1519 #endif 1520 } 1521 } 1522 1523 if (DumpSharedSpaces && found_patch_mod_entry) { 1524 tty->print_cr("Preload Warning: Skip archiving class %s found in --patch-module entry", class_name); 1525 return NULL; 1526 } 1527 1528 // Load Attempt #2: [jimage | exploded build] 1529 if (!search_append_only && (NULL == stream)) { 1530 if (has_jrt_entry()) { 1531 e = _jrt_entry; 1532 stream = _jrt_entry->open_stream(file_name, CHECK_NULL); 1533 if (!context.check(stream, classpath_index)) { 1534 return NULL; 1535 } 1536 } else { 1537 // Exploded build - attempt to locate class in its defining module's location. 1538 assert(_exploded_entries != NULL, "No exploded build entries present"); 1539 stream = search_module_entries(_exploded_entries, class_name, file_name, CHECK_NULL); 1540 } 1541 } 1542 1543 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries] 1544 if ((search_append_only || DumpSharedSpaces) && (NULL == stream)) { 1545 // For the boot loader append path search, the starting classpath_index |