Skip to content
Closed
Show file tree
Hide file tree
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
15 changes: 10 additions & 5 deletions src/hotspot/share/cds/classListWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ void ClassListWriter::write_to_stream(const InstanceKlass* k, outputStream* stre
ClassLoaderData* loader_data = k->class_loader_data();

if (!SystemDictionaryShared::is_builtin_loader(loader_data)) {
if (cfs == NULL || strncmp(cfs->source(), "file:", 5) != 0) {
if (cfs == NULL || (strncmp(cfs->source(), "file:", 5) != 0 &&
strncmp(cfs->source(), "jar:", 4) != 0)) {
return;
}
if (!SystemDictionaryShared::add_unregistered_class(Thread::current(), (InstanceKlass*)k)) {
Expand Down Expand Up @@ -155,13 +156,17 @@ void ClassListWriter::write_to_stream(const InstanceKlass* k, outputStream* stre
}
}

if (strncmp(cfs->source(), "file:", 5) == 0) {
#ifdef _WINDOWS
// "file:/C:/dir/foo.jar" -> "C:/dir/foo.jar"
stream->print(" source: %s", cfs->source() + 6);
// "file:/C:/dir/foo.jar" -> "C:/dir/foo.jar"
stream->print(" source: %s", cfs->source() + 6);
#else
// "file:/dir/foo.jar" -> "/dir/foo.jar"
stream->print(" source: %s", cfs->source() + 5);
// "file:/dir/foo.jar" -> "/dir/foo.jar"
stream->print(" source: %s", cfs->source() + 5);
#endif
} else {
stream->print(" source: %s", cfs->source());
}
}

stream->cr();
Expand Down
68 changes: 10 additions & 58 deletions src/hotspot/share/cds/unregisteredClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@

#include "precompiled.hpp"
#include "cds/unregisteredClasses.hpp"
#include "classfile/classFileStream.hpp"
#include "classfile/classLoader.inline.hpp"
#include "classfile/classLoaderExt.hpp"
#include "classfile/javaClasses.inline.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionaryShared.hpp"
#include "classfile/vmSymbols.hpp"
#include "memory/oopFactory.hpp"
#include "memory/resourceArea.hpp"
#include "oops/instanceKlass.hpp"
#include "runtime/handles.hpp"
#include "runtime/javaCalls.hpp"
#include "services/threadService.hpp"

Expand All @@ -51,65 +46,22 @@ InstanceKlass* UnregisteredClasses::load_class(Symbol* name, const char* path, T
PerfClassTraceTime::CLASS_LOAD);
}

Symbol* path_symbol = SymbolTable::new_symbol(path);
Handle url_classloader = get_url_classloader(path_symbol, CHECK_NULL);
Symbol* jar_util_name = vmSymbols::jdk_internal_misc_UberJarUtils();
Klass* jar_util_klass = SystemDictionary::resolve_or_null(jar_util_name, THREAD);
guarantee(jar_util_klass != NULL, "jdk/internal/misc/UberJarUtils must exist!");

Symbol* method = vmSymbols::loadClass_name();
Symbol* signature = vmSymbols::string_string_class_signature();
Handle path_string = java_lang_String::create_from_str(path, CHECK_NULL);
Handle ext_class_name = java_lang_String::externalize_classname(name, CHECK_NULL);

JavaValue result(T_OBJECT);
JavaCallArguments args(2);
args.set_receiver(url_classloader);
args.push_oop(path_string);
args.push_oop(ext_class_name);
args.push_int(JNI_FALSE);
JavaCalls::call_virtual(&result,
vmClasses::URLClassLoader_klass(),
vmSymbols::loadClass_name(),
vmSymbols::string_boolean_class_signature(),
&args,
CHECK_NULL);
JavaCalls::call_static(&result, jar_util_klass, method, signature, &args, CHECK_NULL);

assert(result.get_type() == T_OBJECT, "just checking");
oop obj = result.get_oop();
return InstanceKlass::cast(java_lang_Class::as_Klass(obj));
}

class URLClassLoaderTable : public ResourceHashtable<
Symbol*, Handle,
137, // prime number
ResourceObj::C_HEAP> {};

static URLClassLoaderTable* _url_classloader_table = NULL;

Handle UnregisteredClasses::create_url_classloader(Symbol* path, TRAPS) {
ResourceMark rm(THREAD);
JavaValue result(T_OBJECT);
Handle path_string = java_lang_String::create_from_str(path->as_C_string(), CHECK_NH);
JavaCalls::call_static(&result,
vmClasses::jdk_internal_loader_ClassLoaders_klass(),
vmSymbols::toFileURL_name(),
vmSymbols::toFileURL_signature(),
path_string, CHECK_NH);
assert(result.get_type() == T_OBJECT, "just checking");
oop url_h = result.get_oop();
objArrayHandle urls = oopFactory::new_objArray_handle(vmClasses::URL_klass(), 1, CHECK_NH);
urls->obj_at_put(0, url_h);

Handle url_classloader = JavaCalls::construct_new_instance(
vmClasses::URLClassLoader_klass(),
vmSymbols::url_array_classloader_void_signature(),
urls, Handle(), CHECK_NH);
return url_classloader;
}

Handle UnregisteredClasses::get_url_classloader(Symbol* path, TRAPS) {
if (_url_classloader_table == NULL) {
_url_classloader_table = new (ResourceObj::C_HEAP, mtClass)URLClassLoaderTable();
}
Handle* url_classloader_ptr = _url_classloader_table->get(path);
if (url_classloader_ptr != NULL) {
return *url_classloader_ptr;
} else {
Handle url_classloader = create_url_classloader(path, CHECK_NH);
_url_classloader_table->put(path, url_classloader);
path->increment_refcount();
return url_classloader;
}
}
8 changes: 2 additions & 6 deletions src/hotspot/share/cds/unregisteredClasses.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@
#ifndef SHARE_CDS_UNREGISTEREDCLASSES_HPP
#define SHARE_CDS_UNREGISTEREDCLASSES_HPP

#include "runtime/handles.hpp"
#include "oops/instanceKlass.hpp"

class UnregisteredClasses: AllStatic {
public:
static InstanceKlass* load_class(Symbol* h_name, const char* path, TRAPS);

private:
static Handle create_url_classloader(Symbol* path, TRAPS);
static Handle get_url_classloader(Symbol* path, TRAPS);
static InstanceKlass* load_class(Symbol* name, const char* path, TRAPS);
};

#endif // SHARE_CDS_UNREGISTEREDCLASSES_HPP
1 change: 0 additions & 1 deletion src/hotspot/share/classfile/vmClassMacros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@
/* support for CDS */ \
do_klass(ByteArrayInputStream_klass, java_io_ByteArrayInputStream ) \
do_klass(URL_klass, java_net_URL ) \
do_klass(URLClassLoader_klass, java_net_URLClassLoader ) \
do_klass(Jar_Manifest_klass, java_util_jar_Manifest ) \
do_klass(jdk_internal_loader_BuiltinClassLoader_klass,jdk_internal_loader_BuiltinClassLoader ) \
do_klass(jdk_internal_loader_ClassLoaders_klass, jdk_internal_loader_ClassLoaders ) \
Expand Down
3 changes: 2 additions & 1 deletion src/hotspot/share/classfile/vmSymbols.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@
template(threadgroup_string_void_signature, "(Ljava/lang/ThreadGroup;Ljava/lang/String;)V") \
template(string_class_signature, "(Ljava/lang/String;)Ljava/lang/Class;") \
template(string_boolean_class_signature, "(Ljava/lang/String;Z)Ljava/lang/Class;") \
template(string_string_class_signature, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Class;") \
template(object_object_object_signature, "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;") \
template(string_string_string_signature, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;") \
template(string_string_signature, "(Ljava/lang/String;)Ljava/lang/String;") \
Expand Down Expand Up @@ -706,12 +707,12 @@
template(java_lang_invoke_DelegatingMethodHandle_Holder, "java/lang/invoke/DelegatingMethodHandle$Holder") \
template(jdk_internal_loader_ClassLoaders, "jdk/internal/loader/ClassLoaders") \
template(jdk_internal_misc_CDS, "jdk/internal/misc/CDS") \
template(jdk_internal_misc_UberJarUtils, "jdk/internal/misc/UberJarUtils") \
template(java_util_concurrent_ConcurrentHashMap, "java/util/concurrent/ConcurrentHashMap") \
template(java_util_ArrayList, "java/util/ArrayList") \
template(toFileURL_name, "toFileURL") \
template(toFileURL_signature, "(Ljava/lang/String;)Ljava/net/URL;") \
template(url_void_signature, "(Ljava/net/URL;)V") \
template(url_array_classloader_void_signature, "([Ljava/net/URL;Ljava/lang/ClassLoader;)V") \
\
/*end*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ private void resetArchivedStates() {
*
* @apiNote This is called by the VM
*/
@Deprecated
private static URL toFileURL(String s) {
public static URL toFileURL(String s) {
try {
// Use an intermediate File object to construct a URI/URL without
// authority component as URLClassPath can't handle URLs with a UNC
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

package jdk.internal.misc;

import java.io.IOException;
import java.security.ProtectionDomain;

/**
* This abstract class is a super class of EmbeddedJarReader and SimpleJarReader.
*/
abstract class AbstractJarReader extends ClassLoader {
Class<?> loadClass(String name, ProtectionDomain pd) throws ClassNotFoundException {
Class<?> clazz = findLoadedClass(name);
if (clazz == null) {
try {
String entryName = name.replace('.', '/') + ".class";
byte[] bytes = getEntry(entryName);
clazz = super.defineClass(name, bytes, 0, bytes.length, pd);
} catch (Exception e) {
clazz = super.loadClass(name, false);
}
}
return clazz;
}

abstract byte[] getEntry(String name) throws IOException, ClassNotFoundException;
}
107 changes: 107 additions & 0 deletions src/java.base/share/classes/jdk/internal/misc/EmbeddedJarReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
package jdk.internal.misc;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.security.CodeSigner;
import java.security.CodeSource;
import java.security.ProtectionDomain;

/**
* This class is for retrieving the class bytes from the bytes of a jar file.
* It delegates to AbstractJarReader to load the class using the class bytes.
*/
class EmbeddedJarReader extends AbstractJarReader {
final static int BUFFER_SIZE = 4096;
String jarPath;
ZipInputStream zins;
HashMap<String, byte[]> entryCache;

protected EmbeddedJarReader(String jarPath, byte[] jarBytes) {
this.jarPath = jarPath;
this.zins = new ZipInputStream(new ByteArrayInputStream(jarBytes));
this.entryCache = new HashMap<String, byte[]>();
}

public Class<?> loadClass(String name) throws ClassNotFoundException {
ProtectionDomain pd = null;
try {
URL u = new URL("file:" + jarPath);
CodeSource cs = new CodeSource(u, (CodeSigner[])null);
pd = new ProtectionDomain(cs, null);
} catch (MalformedURLException mue) {
// ignore MalformedURLException. The class can be loaded with null pd.
// The pd is for showing the "source:" in the class loading trace.
}
return super.loadClass(name, pd);
}

@Override
byte[] getEntry(String name) throws IOException, ClassNotFoundException {
boolean found = false;
byte[] bytes = entryCache.get(name);
if (bytes != null) {
return bytes;
} else {
ZipEntry ze = null;
while ((ze = zins.getNextEntry()) != null) {
if (!ze.isDirectory()) {
bytes = readEntry(zins, ze);
String entryName = ze.getName();
entryCache.put(entryName, bytes);
if (entryName.equals(name)) {
found = true;
break;
}
}
}
}

if (found) {
return bytes;
} else {
throw new ClassNotFoundException(name);
}
}

private static byte[] readEntry(InputStream in, ZipEntry entry) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
long size = entry.getSize();
int nRead;
byte[] data = new byte[BUFFER_SIZE];
while ((nRead = in.read(data, 0, data.length)) != -1) {
baos.write(data, 0, nRead);
}
baos.close();
return baos.toByteArray();
}
}
Loading