Skip to content

Add test for BSM resolution on error throw #21730

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions test/functional/Jsr292/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<include name="**/indyn/Helper.java" />
<include name="**/indyn/SimpleIndyGenerator.java" />
<include name="**/CrossPackageHelper.java"/>
<include name="**/indy/IndyOSRTest.java"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct the path and keep the elements sorted by name.

<classpath>
<pathelement location="${LIB_DIR}/asm-all.jar" />
<pathelement location="${LIB_DIR}/junit4.jar" />
Expand Down
25 changes: 24 additions & 1 deletion test/functional/Jsr292/playlist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,29 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex
<impl>ibm</impl>
</impls>
</test>
<test>
<testCaseName>jsr292_InDyn_OSRTest</testCaseName>
<variations>
<variation>-Xjit:"dontInline={*dummy*},limit={*dummy*},count=1,disableAsyncCompilation,initialOptLevel=warm,disableGuardedCountingRecompilation"</variation>
</variations>
<command>TR_EnableStartupNextGenHCRAtAllOpts=1 $(JAVA_COMMAND) $(JVM_OPTIONS) \
-cp $(Q)$(TEST_RESROOT)$(D)jsr292test.jar$(P)$(RESOURCES_DIR)$(P)$(TESTNG)$(P)$(LIB_DIR)$(D)asm-all.jar$(Q) \
org.testng.TestNG -d $(REPORTDIR) $(Q)$(TEST_RESROOT)$(D)testng$(XMLSUFFIX).xml$(Q) \
-testnames jsr292_InDyn_OSRTest \
-groups $(TEST_GROUP) \
-excludegroups $(DEFAULT_EXCLUDE); \
$(TEST_STATUS)</command>
<levels>
<level>extended</level>
</levels>
<groups>
<group>functional</group>
</groups>
<impls>
<impl>openj9</impl>
<impl>ibm</impl>
</impls>
</test>
<!--
TODO: The following test cases specific to Lookup are temporarily excluded in Java 14
as there is no backward compatibility of these APIs since Java 14 due to the new changes
Expand Down Expand Up @@ -512,4 +535,4 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-ex
<impl>ibm</impl>
</impls>
</test>
</playlist>
</playlist>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please restore the final newline (every non-empty text file should end with one).

150 changes: 150 additions & 0 deletions test/functional/Jsr292/src/com/ibm/j9/jsr292/indyn/IndyOSRTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright IBM Corp. and others 2001
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The year should be 2025 for a new file.

*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*/
package com.ibm.j9.jsr292.indyn;

import org.openj9.test.util.VersionCheck;

import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;

import org.testng.Assert;
import org.testng.AssertJUnit;
import org.objectweb.asm.*;
import static org.objectweb.asm.Opcodes.*;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.ConstantCallSite;
import java.lang.invoke.CallSite;
import java.lang.invoke.MethodType;
Comment on lines +34 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sort imports:

import java.lang.invoke.CallSite;
import java.lang.invoke.ConstantCallSite;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

import java.lang.reflect.Method;

public class IndyOSRTest {

private static Throwable thrower = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the redundant initializer.

private static String dummyClassPathName = "com/ibm/j9/jsr292/indyn/TestBSMError";
private static String dummyClassFullName = "com.ibm.j9.jsr292.indyn.TestBSMError";
Comment on lines +44 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These fields should be final.


// generate method with invokedynamic bytecode that uses the BSM "bootstrap" below
private static byte[] generate(int version) {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
MethodVisitor mv;
cw.visit(VersionCheck.major() + V1_8 - 8, ACC_PUBLIC, dummyClassPathName + version, null, "java/lang/Object", null);
Comment on lines +49 to +51
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work in Java 25+ where org.objectweb.asm is not available.


mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "dummy", "()V", null, null);
mv.visitCode();

Handle bsm = new Handle(
H_INVOKESTATIC,
"com/ibm/j9/jsr292/indyn/IndyOSRTest",
"bootstrap",
"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;",
false
);

mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn(1L);
mv.visitLdcInsn(2L);
//mv.visitVarInsn(ILOAD, 0);
mv.visitLdcInsn(3);
mv.visitLdcInsn(4);
mv.visitInvokeDynamicInsn("someName", "(JJII)Ljava/lang/String;", bsm);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(6, 4);
mv.visitEnd();
cw.visitEnd();
return cw.toByteArray();
}



Comment on lines +78 to +80
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One blank at a time, please.

public static CallSite bootstrap(MethodHandles.Lookup lookup, String name, MethodType mt) throws Throwable {
if (thrower == null) {
MethodHandle target = MethodHandles.lookup().findStatic(IndyTest.class, "sanity", MethodType.methodType(String.class, long.class, long.class, int.class, int.class));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mean to refer to this class (IndyOSRTest).
Please fold this long line:

		MethodHandle target = MethodHandles.lookup().findStatic(
				IndyOSRTest.class,
				"sanity",
				MethodType.methodType(String.class, long.class, long.class, int.class, int.class));

return new ConstantCallSite(target);
}
System.out.println("Throwing");
throw thrower;
}

public static String sanity(long a, long b, int c, int d) {
return "bootstrap" + a + "," + b + "," + c + "," + d;
}

private class ByteArrayClassLoader extends ClassLoader {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class should be static final.

public Class<?> getc(String name, byte[] b) {
return defineClass(name,b,0,b.length);
}
}

@DataProvider(name="throwableProvider")
public static Object[][] throwableProvider() {
return new Object[][] {{new NullPointerException(), 1}, {new StackOverflowError(), 2}, {new IllegalArgumentException(), 3},
{new ClassCastException(), 4}};
Comment on lines +102 to +103
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please split:

		return new Object[][] {
			{new NullPointerException(), 1},
			{new StackOverflowError(), 2},
			{new IllegalArgumentException(), 3},
			{new ClassCastException(), 4}};

}

@Test(groups = {"level.extended"}, dataProvider="throwableProvider", invocationCount=1)
public void testBSMErrorThrow(Throwable t, int version) {
thrower = t;
ByteArrayClassLoader c = new ByteArrayClassLoader();
byte[] b = IndyOSRTest.generate(version);
System.out.println(b.length);
Class<?> cls = c.getc(dummyClassFullName + version, b);

// attempt once in the interpreter
try {
if (t == null){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space before {.

Assert.assertTrue(cls.getMethod("dummy").invoke(null).equals("bootstrap1,2,3,4"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use assertEquals() (also line 136).

} else {
cls.getMethod("dummy").invoke(null);
}
} catch(IllegalAccessException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space after each catch.

Assert.fail("Cannot access dummy method");
} catch(NoSuchMethodException e) {
Assert.fail("Cannot find dummy method");
} catch (java.lang.reflect.InvocationTargetException e) {
if (t == null) {
Assert.fail("Bootsrap method should not throw error when parameter t is null");
}
Comment on lines +126 to +128
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use assertNotNull()

			Assert.assertNotNull(t, "Bootsrap method should not throw error when parameter t is null");

Also line 146.

} catch (Throwable t2) {
System.out.println("Caught something");
}

// run again after compilation
try {
if (t == null){
Assert.assertTrue(cls.getMethod("dummy").invoke(null).equals("bootstrap1,2,3,4"));
} else {
cls.getMethod("dummy").invoke(null);
}
} catch(IllegalAccessException e) {
Assert.fail("Cannot access dummy method");
} catch(NoSuchMethodException e) {
Assert.fail("Cannot find dummy method");
} catch (java.lang.reflect.InvocationTargetException e) {
if (t == null) {
Assert.fail("Bootsrap method should not throw error when parameter t is null");
}
}
}
Comment on lines +148 to +149
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please indent only with tabs.

}
15 changes: 14 additions & 1 deletion test/functional/Jsr292/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,17 @@
<class name="com.ibm.j9.jsr292.indyn.ComplexIndyTest"/>
</classes>
</test>
</suite>

<test name="jsr292_InDyn_OSRTest">
<classes>
<class name="com.ibm.j9.jsr292.indyn.IndyOSRTest"/>
</classes>
</test>

<test name="jsr292_InDyn_OSRTest">
<classes>
<class name="com.ibm.j9.jsr292.indyn.IndyOSRTest"/>
</classes>
</test>
Comment on lines +126 to +130
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this repeated?


</suite>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please restore the final newline.