Skip to content

Fix VM snapshot class loader initialization #20863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 32 additions & 36 deletions runtime/jcl/common/stdinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,35 +465,33 @@ internalInitializeJavaLangClassLoader(JNIEnv * env)

vmFuncs->internalEnterVMFromJNI(vmThread);

#if defined(J9VM_OPT_SNAPSHOT)
/* Always use the persisted applicationClassLoader in restore runs. */
if (!IS_RESTORE_RUN(vm)) {
if (IS_RESTORE_RUN(vm)) {
vmFuncs->initializeSnapshotClassLoaderObject(vm, vm->applicationClassLoader, J9_JNI_UNWRAP_REFERENCE(appClassLoader));
} else
#endif /* defined(J9VM_OPT_SNAPSHOT) */
{
vm->applicationClassLoader = J9VMJAVALANGCLASSLOADER_VMREF(vmThread, J9_JNI_UNWRAP_REFERENCE(appClassLoader));
}

if (NULL == vm->applicationClassLoader) {
/* CMVC 201518
* applicationClassLoader may be null due to lazy classloader initialization. Initialize
* the applicationClassLoader now or vm will start throwing NoClassDefFoundException.
*/
#if defined(J9VM_OPT_SNAPSHOTS)
if (IS_RESTORE_RUN(vm)) {
vmFuncs->initializeSnapshotClassLoaderObject(vm, vm->applicationClassLoader, J9_JNI_UNWRAP_REFERENCE(appClassLoader));
} else
#endif /* defined(J9VM_OPT_SNAPSHOTS) */
{
if (NULL == vm->applicationClassLoader) {
/* CMVC 201518
* applicationClassLoader may be null due to lazy classloader initialization. Initialize
* the applicationClassLoader now or vm will start throwing NoClassDefFoundException.
*/
vm->applicationClassLoader = (void *)(UDATA)(vmFuncs->internalAllocateClassLoader(vm, J9_JNI_UNWRAP_REFERENCE(appClassLoader)));
}

if (NULL != vmThread->currentException) {
/* while this exception check and return statement seem un-necessary, it is added to prevent
* oversights if anybody adds more code in the future.
*/
goto exitVM;
if (NULL != vmThread->currentException) {
/* While this exception check and return statement seem un-necessary, it is added to prevent
* oversights if anybody adds more code in the future.
*/
goto exitVM;
}
}
}

/* Set up extension class loader in VM */
if (NULL == vm->extensionClassLoader) {
if (NULL != vm->applicationClassLoader) {
j9object_t classLoaderObject = vm->applicationClassLoader->classLoaderObject;
j9object_t classLoaderParentObject = classLoaderObject;

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

/* Restore runs use the persisted extensionClassLoader. */
if (!IS_RESTORE_RUN(vm)) {
vm->extensionClassLoader = J9VMJAVALANGCLASSLOADER_VMREF(vmThread, classLoaderObject);
}

if (NULL == vm->extensionClassLoader) {
#if defined(J9VM_OPT_SNAPSHOTS)
if (IS_RESTORE_RUN(vm)) {
vmFuncs->initializeSnapshotClassLoaderObject(vm, vm->extensionClassLoader, classLoaderObject);
} else
/* Always use the persisted extensionClassLoader in restore runs. */
if (IS_RESTORE_RUN(vm)) {
vmFuncs->initializeSnapshotClassLoaderObject(vm, vm->extensionClassLoader, classLoaderObject);
} else
#endif /* defined(J9VM_OPT_SNAPSHOTS) */
{
if (NULL == vm->extensionClassLoader) {
vm->extensionClassLoader = J9VMJAVALANGCLASSLOADER_VMREF(vmThread, classLoaderObject);

if (NULL == vm->extensionClassLoader) {
vm->extensionClassLoader = (void *)(UDATA)(vmFuncs->internalAllocateClassLoader(vm, classLoaderObject));
}

if (NULL != vmThread->currentException) {
/* while this exception check and return statement seem un-necessary, it is added to prevent
* oversights if anybody adds more code in the future.
*/
goto exitVM;
if (NULL != vmThread->currentException) {
/* While this exception check and return statement seem un-necessary, it is added to prevent
* oversights if anybody adds more code in the future.
*/
goto exitVM;
}
}
}
}
Expand Down