View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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 }