Skip to content
Permalink
Browse files
8265605: Cannot call BootLoader::loadClassOrNull before initPhase2
Reviewed-by: alanb, mchung
  • Loading branch information
iklam committed May 14, 2021
1 parent 4086081 commit 1e0ecd6d56541c948e0d120295f5008d3248598f
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, Azul Systems, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
@@ -1134,7 +1134,7 @@ private void postDefineClass(Class<?> c, ProtectionDomain pd) {
Object classData);

// true if the name is null or has the potential to be a valid binary name
private boolean checkName(String name) {
private static boolean checkName(String name) {
if ((name == null) || (name.isEmpty()))
return true;
if ((name.indexOf('/') != -1) || (name.charAt(0) == '['))
@@ -1254,14 +1254,14 @@ protected final void resolveClass(Class<?> c) {
* Returns a class loaded by the bootstrap class loader;
* or return null if not found.
*/
Class<?> findBootstrapClassOrNull(String name) {
static Class<?> findBootstrapClassOrNull(String name) {
if (!checkName(name)) return null;

return findBootstrapClass(name);
}

// return null if not found
private native Class<?> findBootstrapClass(String name);
private static native Class<?> findBootstrapClass(String name);

/**
* Returns the class with the given <a href="#binary-name">binary name</a> if this
@@ -2218,8 +2218,8 @@ public void invokeFinalize(Object o) throws Throwable {
boolean initialize, int flags, Object classData) {
return ClassLoader.defineClass0(loader, lookup, name, b, 0, b.length, pd, initialize, flags, classData);
}
public Class<?> findBootstrapClassOrNull(ClassLoader cl, String name) {
return cl.findBootstrapClassOrNull(name);
public Class<?> findBootstrapClassOrNull(String name) {
return ClassLoader.findBootstrapClassOrNull(name);
}
public Package definePackage(ClassLoader cl, String name, Module module) {
return cl.definePackage(name, module);
@@ -161,7 +161,7 @@
/**
* Returns a class loaded by the bootstrap class loader.
*/
Class<?> findBootstrapClassOrNull(ClassLoader cl, String name);
Class<?> findBootstrapClassOrNull(String name);

/**
* Define a Package of the given name and module by the given class loader.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -47,7 +47,7 @@ private ArchivedClassLoaders() {
appLoader = ClassLoaders.appClassLoader();

servicesCatalogs = new ServicesCatalog[3];
servicesCatalogs[0] = BootLoader.getServicesCatalog();
servicesCatalogs[0] = ServicesCatalog.getServicesCatalog(bootLoader);
servicesCatalogs[1] = ServicesCatalog.getServicesCatalog(platformLoader);
servicesCatalogs[2] = ServicesCatalog.getServicesCatalog(appLoader);

@@ -67,7 +67,7 @@ ClassLoader appLoader() {
}

ServicesCatalog servicesCatalog(ClassLoader loader) {
if (loader == null) {
if (loader == bootLoader) {
return servicesCatalogs[0];
} else if (loader == platformLoader) {
return servicesCatalogs[1];
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -55,6 +55,8 @@
public class BootLoader {
private BootLoader() { }

private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();

// The unnamed module for the boot loader
private static final Module UNNAMED_MODULE;
private static final String JAVA_HOME = StaticProperty.javaHome();
@@ -64,17 +66,6 @@ private BootLoader() { }
setBootLoaderUnnamedModule0(UNNAMED_MODULE);
}

// ServiceCatalog for the boot class loader
private static final ServicesCatalog SERVICES_CATALOG;
static {
ArchivedClassLoaders archivedClassLoaders = ArchivedClassLoaders.get();
if (archivedClassLoaders != null) {
SERVICES_CATALOG = archivedClassLoaders.servicesCatalog(null);
} else {
SERVICES_CATALOG = ServicesCatalog.create();
}
}

// ClassLoaderValue map for the boot class loader
private static final ConcurrentHashMap<?, ?> CLASS_LOADER_VALUE_MAP
= new ConcurrentHashMap<>();
@@ -94,7 +85,7 @@ public static Module getUnnamedModule() {
* Returns the ServiceCatalog for modules defined to the boot class loader.
*/
public static ServicesCatalog getServicesCatalog() {
return SERVICES_CATALOG;
return ServicesCatalog.getServicesCatalog(ClassLoaders.bootLoader());
}

/**
@@ -131,7 +122,7 @@ public static void loadModule(ModuleReference mref) {
* Loads the Class object with the given name defined to the boot loader.
*/
public static Class<?> loadClassOrNull(String name) {
return ClassLoaders.bootLoader().loadClassOrNull(name);
return JLA.findBootstrapClassOrNull(name);
}

/**
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -55,15 +55,21 @@ private ClassLoaders() { }
private static final PlatformClassLoader PLATFORM_LOADER;
private static final AppClassLoader APP_LOADER;

// Sets the ServicesCatalog for the specified loader using archived objects.
private static void setArchivedServicesCatalog(ClassLoader loader) {
ServicesCatalog catalog = ArchivedClassLoaders.get().servicesCatalog(loader);
ServicesCatalog.putServicesCatalog(loader, catalog);
}

// Creates the built-in class loaders.
static {
ArchivedClassLoaders archivedClassLoaders = ArchivedClassLoaders.get();
if (archivedClassLoaders != null) {
// assert VM.getSavedProperty("jdk.boot.class.path.append") == null
BOOT_LOADER = (BootClassLoader) archivedClassLoaders.bootLoader();
setArchivedServicesCatalog(BOOT_LOADER);
PLATFORM_LOADER = (PlatformClassLoader) archivedClassLoaders.platformLoader();
ServicesCatalog catalog = archivedClassLoaders.servicesCatalog(PLATFORM_LOADER);
ServicesCatalog.putServicesCatalog(PLATFORM_LOADER, catalog);
setArchivedServicesCatalog(PLATFORM_LOADER);
} else {
// -Xbootclasspath/a or -javaagent with Boot-Class-Path attribute
String append = VM.getSavedProperty("jdk.boot.class.path.append");
@@ -86,8 +92,7 @@ private ClassLoaders() { }
URLClassPath ucp = new URLClassPath(cp, false);
if (archivedClassLoaders != null) {
APP_LOADER = (AppClassLoader) archivedClassLoaders.appLoader();
ServicesCatalog catalog = archivedClassLoaders.servicesCatalog(APP_LOADER);
ServicesCatalog.putServicesCatalog(APP_LOADER, catalog);
setArchivedServicesCatalog(APP_LOADER);
APP_LOADER.setClassPath(ucp);
} else {
APP_LOADER = new AppClassLoader(PLATFORM_LOADER, ucp);
@@ -132,7 +137,7 @@ public static ClassLoader appClassLoader() {

@Override
protected Class<?> loadClassOrNull(String cn, boolean resolve) {
return JLA.findBootstrapClassOrNull(this, cn);
return JLA.findBootstrapClassOrNull(cn);
}
};

@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -274,7 +274,7 @@ Java_java_lang_ClassLoader_defineClass0(JNIEnv *env,
* Returns NULL if class not found.
*/
JNIEXPORT jclass JNICALL
Java_java_lang_ClassLoader_findBootstrapClass(JNIEnv *env, jobject loader,
Java_java_lang_ClassLoader_findBootstrapClass(JNIEnv *env, jclass dummy,
jstring classname)
{
char *clname;
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -46,7 +46,7 @@ public static void main(String[] args) throws Exception {
System.out.println(ex.toString());
}
} else {
cls = jla.findBootstrapClassOrNull(appLoader, className);
cls = jla.findBootstrapClassOrNull(className);
System.out.println("bootLoader loaded class");
System.out.println("cls = " + cls);
try {

1 comment on commit 1e0ecd6

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 1e0ecd6 May 14, 2021

Please sign in to comment.