8313638: Add test for dump of resolved references

Reviewed-by: ccheung, iklam
This commit is contained in:
Matias Saavedra Silva 2023-09-14 14:30:14 +00:00
parent cfa89012ab
commit 83dca6296e
5 changed files with 202 additions and 0 deletions

View file

@ -0,0 +1,75 @@
/*
* Copyright (c) 2023, 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 ResolvedReferencesNotNullTest
* @bug 8313638
* @summary Testing resolved references array to ensure elements are non-null
* @requires vm.cds.write.archived.java.heap
* @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
* @build jdk.test.whitebox.WhiteBox ResolvedReferencesWb ResolvedReferencesTestApp
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
* @run driver ResolvedReferencesNotNullTest
*/
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import jdk.test.whitebox.WhiteBox;
public class ResolvedReferencesNotNullTest {
public static void main(String[] args) throws Exception {
SharedStringsUtils.buildJarAndWhiteBox("ResolvedReferencesWb", "ResolvedReferencesTestApp");
String appJar = TestCommon.getTestJar(SharedStringsUtils.TEST_JAR_NAME_FULL);
String whiteboxParam = SharedStringsUtils.getWbParam();
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-cp",
appJar,
whiteboxParam,
"-XX:+UnlockDiagnosticVMOptions",
"-XX:+WhiteBoxAPI",
"ResolvedReferencesWb",
"false" // ResolvedReferencesTestApp is not archived
);
OutputAnalyzer output = new OutputAnalyzer(pb.start());
output.shouldHaveExitValue(0);
TestCommon.dump(appJar,
TestCommon.list("ResolvedReferencesWb", "ResolvedReferencesTestApp"),
TestCommon.concat("-XX:SharedArchiveFile=ResolvedRef.jsa",
"-XX:+UnlockDiagnosticVMOptions",
"-XX:+WhiteBoxAPI",
whiteboxParam));
// Since ResolvedReferencesTestApp is now archived, all of the strings should be in the resolved
// references array
TestCommon.run("-cp",
appJar,
whiteboxParam,
"-XX:SharedArchiveFile=ResolvedRef.jsa",
"-XX:+UnlockDiagnosticVMOptions",
"-XX:+WhiteBoxAPI",
"ResolvedReferencesWb",
"true" // ResolvedReferencesTestApp is archived
).assertNormalExit();
}
}

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2023, 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.
*/
public class ResolvedReferencesTestApp {
// These strings must be in the resolved references array
static String foo = "fooString";
static String bar = "barString";
// This method is never called so the string should not be added to the resolved references array
String qux() { return "quxString"; }
}

View file

@ -0,0 +1,83 @@
/*
* Copyright (c) 2023, 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 jdk.test.whitebox.WhiteBox;
public class ResolvedReferencesWb {
public static void main(String[] args) throws Exception {
WhiteBox wb = WhiteBox.getWhiteBox();
if (args.length < 1) {
throw new RuntimeException("Test requires arg: [true|false]");
}
if (!args[0].equals("true") && !args[0].equals("false")) {
throw new RuntimeException("Invalid argument: Test requires arg: [true|false]");
}
ResolvedReferencesTestApp t = new ResolvedReferencesTestApp();
Object[] resolvedReferences = wb.getResolvedReferences(ResolvedReferencesTestApp.class);
boolean isArchived = (args[0].equals("true"));
if (resolvedReferences.length <= 0) {
throw new RuntimeException("Resolved reference should not be null");
}
boolean foundFoo = false;
boolean foundBar = false;
boolean foundQux = false;
for (Object o : resolvedReferences) {
if (o != null) {
foundFoo |= (o.equals("fooString"));
foundBar |= (o.equals("barString"));
foundQux |= (o.equals("quxString"));
}
}
if (isArchived) {
// CDS eagerly resolves all the string literals in the ConstantPool. At this point, all
// three strings should be in the resolvedReferences array.
if (!foundFoo || !foundBar || !foundQux) {
throwException(resolvedReferences, "Incorrect resolved references array, all strings should be present");
}
} else {
// If the class is not archived, the string literals in the ConstantPool are resolved
// on-demand. At this point, ResolvedReferencesTestApp::<clinit> has been executed
// so the two strings used there should be in the resolvedReferences array.
// ResolvedReferencesTestApp::qux() is not executed so "quxString"
// should not yet be resolved.
if (!foundFoo || !foundBar || foundQux) {
throwException(resolvedReferences, "Incorrect resolved references array, quxString should not be archived");
}
}
}
static void throwException(Object[] resolvedRefs, String errMsg) throws RuntimeException {
System.out.printf("Resolved References Array Length: %d\n", resolvedRefs.length);
for (Object o : resolvedRefs) {
System.out.println(o);
}
throw new RuntimeException(errMsg);
}
}

View file

@ -140,6 +140,12 @@ public class WhiteBox {
return getConstantPoolCacheLength0(aClass);
}
private native Object[] getResolvedReferences0(Class<?> aClass);
public Object[] getResolvedReferences(Class<?> aClass) {
Objects.requireNonNull(aClass);
return getResolvedReferences0(aClass);
}
private native int remapInstructionOperandFromCPCache0(Class<?> aClass, int index);
public int remapInstructionOperandFromCPCache(Class<?> aClass, int index) {
Objects.requireNonNull(aClass);