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
8276126: Dump time class transformation causes heap objects of non-boot classes to be archived #6484
8276126: Dump time class transformation causes heap objects of non-boot classes to be archived #6484
Changes from 3 commits
c033dd4
58b1d9f
381f26d
f3eee00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,9 @@ | |
|
||
#include "precompiled.hpp" | ||
#include "cds/filemap.hpp" | ||
#include "cds/heapShared.hpp" | ||
#include "classfile/classFileParser.hpp" | ||
#include "classfile/classFileStream.hpp" | ||
#include "classfile/classLoader.inline.hpp" | ||
#include "classfile/classLoaderExt.hpp" | ||
#include "classfile/classLoaderData.inline.hpp" | ||
|
@@ -227,7 +229,8 @@ void ClassLoaderExt::setup_search_paths(JavaThread* current) { | |
ClassLoaderExt::setup_app_search_path(current); | ||
} | ||
|
||
void ClassLoaderExt::record_result(const s2 classpath_index, InstanceKlass* result) { | ||
void ClassLoaderExt::record_result(const s2 classpath_index, InstanceKlass* result, | ||
const ClassFileStream* stream, bool redefined) { | ||
Arguments::assert_is_dumping_archive(); | ||
|
||
// We need to remember where the class comes from during dumping. | ||
|
@@ -245,4 +248,21 @@ void ClassLoaderExt::record_result(const s2 classpath_index, InstanceKlass* resu | |
} | ||
result->set_shared_classpath_index(classpath_index); | ||
result->set_shared_class_loader_type(classloader_type); | ||
#if INCLUDE_CDS_JAVA_HEAP | ||
if (DumpSharedSpaces && AllowArchivingWithJavaAgent && classloader_type == ClassLoader::BOOT_LOADER && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the function is call during dump time (the assert at first line of this function check that) so DumpSharedSpace can be removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the explanation. |
||
classpath_index < 0 && HeapShared::can_write() && redefined) { | ||
// During static dump, classes for the built-in loaders are always loaded from | ||
// known locations (jimage, classpath or modulepath), so classpath_index should | ||
// always be >= 0. | ||
// The only exception is when a java agent is used during dump time (for testing | ||
// purposes only). If a class is transformed by the agent, the CodeSource of | ||
// this class may point to an unknown location. This may break heap object archiving, | ||
// which requires all the boot classes to be from known locations. This is an | ||
// uncommon scenario (even in test cases). Let's simply disable heap object archiving. | ||
ResourceMark rm; | ||
log_warning(cds)("CDS heap objects cannot be written because class %s maybe modified by ClassFileLoadHook.", | ||
result->external_name()); | ||
HeapShared::disable_writing(); | ||
} | ||
#endif // INCLUDE_CDS_JAVA_HEAP | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* 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. | ||
* | ||
*/ | ||
|
||
/* | ||
* @test | ||
* @bug 8276126 | ||
* @summary Test static dumping with java agent transforming a class loaded | ||
* by the boot class loader. | ||
* @requires vm.cds.write.archived.java.heap | ||
* @requires vm.jvmti | ||
* @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds/test-classes | ||
* @compile test-classes/Hello.java | ||
* @compile test-classes/TransformBootClass.java | ||
* @run driver LambdaWithJavaAgent | ||
*/ | ||
|
||
import jdk.test.lib.cds.CDSOptions; | ||
import jdk.test.lib.cds.CDSTestUtils; | ||
import jdk.test.lib.process.OutputAnalyzer; | ||
import jdk.test.lib.process.ProcessTools; | ||
import jdk.test.lib.helpers.ClassFileInstaller; | ||
|
||
public class LambdaWithJavaAgent { | ||
|
||
public static String agentClasses[] = { | ||
TransformBootClass.class.getName(), | ||
}; | ||
|
||
public static void main(String[] args) throws Exception { | ||
String mainClass = Hello.class.getName(); | ||
String namePrefix = "lambda-with-java-agent"; | ||
JarBuilder.build(namePrefix, mainClass); | ||
|
||
String appJar = TestCommon.getTestJar(namePrefix + ".jar"); | ||
String classList = namePrefix + ".list"; | ||
String archiveName = namePrefix + ".jsa"; | ||
|
||
String agentJar = | ||
ClassFileInstaller.writeJar("TransformBootClass.jar", | ||
ClassFileInstaller.Manifest.fromSourceFile("test-classes/TransformBootClass.mf"), | ||
agentClasses); | ||
String useJavaAgent = "-javaagent:" + agentJar + "=jdk/internal/math/FDBigInteger"; | ||
|
||
// dump class list | ||
CDSTestUtils.dumpClassList(classList, "-cp", appJar, mainClass); | ||
|
||
// create archive with the class list | ||
CDSOptions opts = (new CDSOptions()) | ||
.addPrefix("-XX:ExtraSharedClassListFile=" + classList, | ||
"-cp", appJar, | ||
"-XX:+AllowArchivingWithJavaAgent", | ||
useJavaAgent, | ||
"-Xlog:class+load,cds+class=debug,cds") | ||
.setArchiveName(archiveName); | ||
OutputAnalyzer output = CDSTestUtils.createArchiveAndCheck(opts); | ||
output.shouldContain("CDS heap objects cannot be written because class jdk.internal.math.FDBigInteger maybe modified by ClassFileLoadHook") | ||
.shouldContain("Skipping jdk/internal/math/FDBigInteger: Unsupported location") | ||
.shouldMatch(".class.load.*jdk.internal.math.FDBigInteger.*source.*modules"); | ||
|
||
// run with archive | ||
CDSOptions runOpts = (new CDSOptions()) | ||
.addPrefix("-cp", appJar, "-Xlog:class+load,cds=debug", | ||
"-XX:+AllowArchivingWithJavaAgent", | ||
useJavaAgent) | ||
.setArchiveName(archiveName) | ||
.setUseVersion(false) | ||
.addSuffix(mainClass); | ||
output = CDSTestUtils.runWithArchive(runOpts); | ||
TestCommon.checkExecReturn(output, 0, true, | ||
"Hello source: shared objects file"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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. | ||
* | ||
*/ | ||
|
||
import java.lang.instrument.ClassFileTransformer; | ||
import java.lang.instrument.Instrumentation; | ||
import java.lang.instrument.IllegalClassFormatException; | ||
import java.security.ProtectionDomain; | ||
|
||
public class TransformBootClass implements ClassFileTransformer { | ||
|
||
static String targetClass = null; | ||
|
||
public byte[] transform(ClassLoader loader, String name, Class<?> classBeingRedefined, | ||
ProtectionDomain pd, byte[] buffer) throws IllegalClassFormatException { | ||
|
||
if (name.equals(targetClass)) { | ||
System.out.println("Transforming class " + name); | ||
return buffer; | ||
} | ||
return null; | ||
} | ||
|
||
private static Instrumentation savedInstrumentation; | ||
|
||
public static void premain(String agentArguments, Instrumentation instrumentation) { | ||
System.out.println("TransformBootClass.premain() is called"); | ||
instrumentation.addTransformer(new TransformBootClass(), /*canRetransform=*/true); | ||
savedInstrumentation = instrumentation; | ||
if (agentArguments != null) { | ||
targetClass = agentArguments; | ||
} | ||
} | ||
|
||
public static Instrumentation getInstrumentation() { | ||
return savedInstrumentation; | ||
} | ||
|
||
public static void agentmain(String args, Instrumentation inst) throws Exception { | ||
premain(args, inst); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Manifest-Version: 1.0 | ||
Premain-Class: TransformBootClass | ||
Agent-Class: TransformBootClass | ||
Can-Retransform-Classes: true | ||
Can-Redefine-Classes: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
stream
argument is not used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for spotting this. I've fixed it.