1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.bcel.verifier;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23
24 import org.apache.bcel.Const;
25 import org.apache.bcel.Repository;
26 import org.apache.bcel.generic.ArrayType;
27 import org.apache.bcel.generic.ClassGen;
28 import org.apache.bcel.generic.ConstantPoolGen;
29 import org.apache.bcel.generic.InstructionFactory;
30 import org.apache.bcel.generic.InstructionList;
31 import org.apache.bcel.generic.MethodGen;
32 import org.apache.bcel.generic.ObjectType;
33 import org.apache.bcel.generic.Type;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.condition.DisabledForJreRange;
36 import org.junit.jupiter.api.condition.JRE;
37
38 class VerifierInvokeTest extends AbstractVerifierTest {
39
40 private void assertInvokeVirtual(final String owner, final String name, final Type returnType, final Type[] argumentTypes,
41 final boolean accepted) throws ClassNotFoundException {
42 final String className = TEST_PACKAGE + "SignaturePolymorphicCaller";
43 final ClassGen classGen = new ClassGen(className, "java.lang.Object", "SignaturePolymorphicCaller.java", Const.ACC_PUBLIC | Const.ACC_SUPER, null);
44 final ConstantPoolGen constantPool = classGen.getConstantPool();
45 final InstructionFactory factory = new InstructionFactory(classGen);
46 final InstructionList instructions = new InstructionList();
47 final Type[] parameters = new Type[argumentTypes.length + 1];
48 parameters[0] = new ObjectType(owner);
49 System.arraycopy(argumentTypes, 0, parameters, 1, argumentTypes.length);
50 int slot = 0;
51 for (final Type parameter : parameters) {
52 instructions.append(InstructionFactory.createLoad(parameter, slot));
53 slot += parameter.getSize();
54 }
55 instructions.append(factory.createInvoke(owner, name, returnType, argumentTypes, Const.INVOKEVIRTUAL));
56 instructions.append(InstructionFactory.createReturn(returnType));
57 final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, returnType, parameters, null, "call", className,
58 instructions, constantPool);
59 method.setMaxStack();
60 method.setMaxLocals();
61 classGen.addMethod(method.getMethod());
62 Repository.addClass(classGen.getJavaClass());
63 VerifierFactory.clear();
64 try {
65 final Verifier verifier = VerifierFactory.getVerifier(className);
66 assertEquals(VerificationResult.VR_OK, verifier.doPass1());
67 assertEquals(VerificationResult.VR_OK, verifier.doPass2());
68 final VerificationResult result = verifier.doPass3a(0);
69 assertEquals(accepted ? VerificationResult.VERIFIED_OK : VerificationResult.VERIFIED_REJECTED, result.getStatus(), result::getMessage);
70 if (accepted) {
71 assertEquals(VerificationResult.VR_OK, verifier.doPass3b(0));
72 }
73 } finally {
74 instructions.dispose();
75 Repository.removeClass(className);
76 VerifierFactory.clear();
77 }
78 }
79
80 @Test
81 void testMethodHandleSignatures() throws ClassNotFoundException {
82 for (final String name : new String[] { "invoke", "invokeExact" }) {
83 assertInvokeVirtual("java.lang.invoke.MethodHandle", name, Type.OBJECT,
84 new Type[] { new ArrayType(Type.BYTE, 1), new ArrayType(Type.OBJECT, 1) }, true);
85 assertInvokeVirtual("java.lang.invoke.MethodHandle", name, Type.LONG, new Type[] { Type.LONG, Type.DOUBLE }, true);
86 assertInvokeVirtual("java.lang.invoke.MethodHandle", name, Type.VOID, Type.NO_ARGS, true);
87 }
88 }
89
90 @Test
91 void testOrdinaryMethodSignatures() throws ClassNotFoundException {
92 assertInvokeVirtual("java.lang.invoke.MethodHandle", "invokeWithArguments", Type.OBJECT, new Type[] { Type.INT }, false);
93 assertInvokeVirtual("java.lang.invoke.MethodHandle", "missingMethod", Type.OBJECT, Type.NO_ARGS, false);
94 assertInvokeVirtual("java.lang.Object", "toString", Type.INT, Type.NO_ARGS, false);
95 }
96
97 @Test
98 @DisabledForJreRange(max = JRE.JAVA_8)
99 void testVarHandleSignatures() throws ClassNotFoundException {
100 assertInvokeVirtual("java.lang.invoke.VarHandle", "get", Type.INT, new Type[] { Type.OBJECT }, true);
101 assertInvokeVirtual("java.lang.invoke.VarHandle", "set", Type.VOID, new Type[] { Type.OBJECT, Type.INT }, true);
102 assertInvokeVirtual("java.lang.invoke.VarHandle", "compareAndSet", Type.BOOLEAN, new Type[] { Type.OBJECT, Type.INT, Type.INT }, true);
103 assertInvokeVirtual("java.lang.invoke.VarHandle", "toString", Type.INT, Type.NO_ARGS, false);
104 }
105
106 @Test
107 void testLegalInvokeInterface() throws ClassNotFoundException {
108 assertVerifyOK("TestLegalInvokeInterface01", "Verification of invokeinterface on a method defined in a superinterface must pass.");
109 }
110
111 @Test
112 void testLegalInvokeSpecial() throws ClassNotFoundException {
113 assertVerifyOK("TestLegalInvokeSpecial01", "Verification of invokespecial on a method defined in a superclass must pass.");
114 assertVerifyOK("TestLegalInvokeSpecial02", "Verification of invokespecial on a method defined in a superclass must pass.");
115 }
116
117 @Test
118 void testLegalInvokeStatic() throws ClassNotFoundException {
119 assertVerifyOK("TestLegalInvokeStatic01", "Verification of invokestatic on a method defined in a superclass must pass.");
120 }
121
122 @Test
123 void testLegalInvokeVirtual() throws ClassNotFoundException {
124 assertVerifyOK("TestLegalInvokeVirtual01", "Verification of invokevirtual on a method defined in a superclass must pass.");
125 assertVerifyOK("TestLegalInvokeVirtual02", "Verification of invokevirtual on a method defined in a superinterface must pass.");
126 }
127 }