Skip to content

Commit 63a354b

Browse files
authored
Merge pull request #20863 from ThanHenderson/fix-init-check-order
Fix VM snapshot class loader initialization
2 parents 9a1177f + bdb91fe commit 63a354b

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

runtime/jcl/common/stdinit.c

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -465,35 +465,33 @@ internalInitializeJavaLangClassLoader(JNIEnv * env)
465465

466466
vmFuncs->internalEnterVMFromJNI(vmThread);
467467

468+
#if defined(J9VM_OPT_SNAPSHOT)
468469
/* Always use the persisted applicationClassLoader in restore runs. */
469-
if (!IS_RESTORE_RUN(vm)) {
470+
if (IS_RESTORE_RUN(vm)) {
471+
vmFuncs->initializeSnapshotClassLoaderObject(vm, vm->applicationClassLoader, J9_JNI_UNWRAP_REFERENCE(appClassLoader));
472+
} else
473+
#endif /* defined(J9VM_OPT_SNAPSHOT) */
474+
{
470475
vm->applicationClassLoader = J9VMJAVALANGCLASSLOADER_VMREF(vmThread, J9_JNI_UNWRAP_REFERENCE(appClassLoader));
471-
}
472476

473-
if (NULL == vm->applicationClassLoader) {
474-
/* CMVC 201518
475-
* applicationClassLoader may be null due to lazy classloader initialization. Initialize
476-
* the applicationClassLoader now or vm will start throwing NoClassDefFoundException.
477-
*/
478-
#if defined(J9VM_OPT_SNAPSHOTS)
479-
if (IS_RESTORE_RUN(vm)) {
480-
vmFuncs->initializeSnapshotClassLoaderObject(vm, vm->applicationClassLoader, J9_JNI_UNWRAP_REFERENCE(appClassLoader));
481-
} else
482-
#endif /* defined(J9VM_OPT_SNAPSHOTS) */
483-
{
477+
if (NULL == vm->applicationClassLoader) {
478+
/* CMVC 201518
479+
* applicationClassLoader may be null due to lazy classloader initialization. Initialize
480+
* the applicationClassLoader now or vm will start throwing NoClassDefFoundException.
481+
*/
484482
vm->applicationClassLoader = (void *)(UDATA)(vmFuncs->internalAllocateClassLoader(vm, J9_JNI_UNWRAP_REFERENCE(appClassLoader)));
485-
}
486483

487-
if (NULL != vmThread->currentException) {
488-
/* while this exception check and return statement seem un-necessary, it is added to prevent
489-
* oversights if anybody adds more code in the future.
490-
*/
491-
goto exitVM;
484+
if (NULL != vmThread->currentException) {
485+
/* While this exception check and return statement seem un-necessary, it is added to prevent
486+
* oversights if anybody adds more code in the future.
487+
*/
488+
goto exitVM;
489+
}
492490
}
493491
}
494492

495493
/* Set up extension class loader in VM */
496-
if (NULL == vm->extensionClassLoader) {
494+
if (NULL != vm->applicationClassLoader) {
497495
j9object_t classLoaderObject = vm->applicationClassLoader->classLoaderObject;
498496
j9object_t classLoaderParentObject = classLoaderObject;
499497

@@ -502,26 +500,24 @@ internalInitializeJavaLangClassLoader(JNIEnv * env)
502500
classLoaderParentObject = J9VMJAVALANGCLASSLOADER_PARENT(vmThread, classLoaderObject);
503501
}
504502

505-
/* Restore runs use the persisted extensionClassLoader. */
506-
if (!IS_RESTORE_RUN(vm)) {
507-
vm->extensionClassLoader = J9VMJAVALANGCLASSLOADER_VMREF(vmThread, classLoaderObject);
508-
}
509-
510-
if (NULL == vm->extensionClassLoader) {
511503
#if defined(J9VM_OPT_SNAPSHOTS)
512-
if (IS_RESTORE_RUN(vm)) {
513-
vmFuncs->initializeSnapshotClassLoaderObject(vm, vm->extensionClassLoader, classLoaderObject);
514-
} else
504+
/* Always use the persisted extensionClassLoader in restore runs. */
505+
if (IS_RESTORE_RUN(vm)) {
506+
vmFuncs->initializeSnapshotClassLoaderObject(vm, vm->extensionClassLoader, classLoaderObject);
507+
} else
515508
#endif /* defined(J9VM_OPT_SNAPSHOTS) */
516-
{
509+
if (NULL == vm->extensionClassLoader) {
510+
vm->extensionClassLoader = J9VMJAVALANGCLASSLOADER_VMREF(vmThread, classLoaderObject);
511+
512+
if (NULL == vm->extensionClassLoader) {
517513
vm->extensionClassLoader = (void *)(UDATA)(vmFuncs->internalAllocateClassLoader(vm, classLoaderObject));
518-
}
519514

520-
if (NULL != vmThread->currentException) {
521-
/* while this exception check and return statement seem un-necessary, it is added to prevent
522-
* oversights if anybody adds more code in the future.
523-
*/
524-
goto exitVM;
515+
if (NULL != vmThread->currentException) {
516+
/* While this exception check and return statement seem un-necessary, it is added to prevent
517+
* oversights if anybody adds more code in the future.
518+
*/
519+
goto exitVM;
520+
}
525521
}
526522
}
527523
}

0 commit comments

Comments
 (0)