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  package org.apache.bcel.verifier.statics;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  import static org.mockito.Mockito.doReturn;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.spy;
26  import static org.mockito.Mockito.when;
27  
28  import java.util.stream.Stream;
29  
30  import org.apache.bcel.Const;
31  import org.apache.bcel.Repository;
32  import org.apache.bcel.classfile.Attribute;
33  import org.apache.bcel.classfile.Code;
34  import org.apache.bcel.classfile.CodeException;
35  import org.apache.bcel.classfile.Constant;
36  import org.apache.bcel.classfile.ConstantDouble;
37  import org.apache.bcel.classfile.ConstantFieldref;
38  import org.apache.bcel.classfile.ConstantInterfaceMethodref;
39  import org.apache.bcel.classfile.ConstantInvokeDynamic;
40  import org.apache.bcel.classfile.ConstantLong;
41  import org.apache.bcel.classfile.ConstantMethodHandle;
42  import org.apache.bcel.classfile.ConstantMethodType;
43  import org.apache.bcel.classfile.ConstantModule;
44  import org.apache.bcel.classfile.ConstantNameAndType;
45  import org.apache.bcel.classfile.ConstantPackage;
46  import org.apache.bcel.classfile.ConstantPool;
47  import org.apache.bcel.classfile.ConstantUtf8;
48  import org.apache.bcel.classfile.JavaClass;
49  import org.apache.bcel.classfile.Method;
50  import org.apache.bcel.util.SyntheticRepository;
51  import org.apache.bcel.verifier.VerificationResult;
52  import org.apache.bcel.verifier.Verifier;
53  import org.apache.bcel.verifier.VerifierFactory;
54  import org.junit.jupiter.api.AfterAll;
55  import org.junit.jupiter.api.BeforeEach;
56  import org.junit.jupiter.params.ParameterizedTest;
57  import org.junit.jupiter.params.provider.MethodSource;
58  
59  class Pass3aVerifierTest {
60      public static Stream<Constant> constantsNotSupportedByLdc() {
61          return Stream.of(
62                 new ConstantFieldref(0, 0),
63                 new ConstantInterfaceMethodref(0, 0),
64                 new ConstantInvokeDynamic(0, 0),
65                 new ConstantMethodHandle(0, 0),
66                 new ConstantDouble(0D),
67                 new ConstantLong(0L),
68                 new ConstantMethodHandle(0, 0),
69                 new ConstantMethodType(0),
70                 new ConstantModule(0),
71                 new ConstantNameAndType(0, 0),
72                 new ConstantPackage(0),
73                 new ConstantUtf8("constant")
74                  );
75      }
76      @AfterAll
77      public static void restoreRepository() {
78          // We have set our mock repository, revert the change
79          Repository.setRepository(SyntheticRepository.getInstance());
80      }
81      private Verifier verifier;
82      private org.apache.bcel.util.Repository repository;
83  
84      private ConstantPool cp;
85  
86      private JavaClass javaClass;
87  
88      @ParameterizedTest
89      @MethodSource("constantsNotSupportedByLdc")
90      public void rejectLdcConstant(final Constant constant) {
91          // LDC the constant 0 and then return
92          final byte[] methodCode = {
93                  Const.LDC,
94                  0,
95                  0,
96                  (byte) Const.RETURN,
97          };
98  
99          final Code code = new Code(0, 0, 0, 0, methodCode, new CodeException[0], new Attribute[0], cp);
100 
101         when(cp.getConstantPool()).thenReturn(new Constant[] {constant});
102 
103         final Attribute[] attributes = {code};
104         final Method method = new Method(0, 0, 0, attributes, cp);
105 
106         when(javaClass.getMethods()).thenReturn(new Method[] {method});
107 
108         final Pass3aVerifier pass3aVerifier = new Pass3aVerifier(verifier, 0);
109         final VerificationResult verificationResult = pass3aVerifier.do_verify();
110 
111         assertEquals(VerificationResult.VERIFIED_REJECTED, verificationResult.getStatus());
112         assertTrue(verificationResult.getMessage().startsWith("Instruction ldc[18](2) 0 constraint violated: Operand of LDC"));
113     }
114 
115     @BeforeEach
116     void setup() throws ClassNotFoundException {
117         final String className = "org.apache.bcel.verifier.statics.Pass3aVerifierTest.foo";
118 
119         verifier = spy(VerifierFactory.getVerifier(className));
120         repository = mock(org.apache.bcel.util.Repository.class);
121         cp = mock(ConstantPool.class);
122         javaClass = mock(JavaClass.class);
123 
124         // Mock the verifier
125         doReturn(VerificationResult.VR_OK).when(verifier).doPass2();
126 
127         // Mock the repository
128         Repository.setRepository(repository);
129         when(repository.loadClass(className)).thenReturn(javaClass);
130 
131         // Mock the constant pool
132         when(cp.getConstantPool()).thenReturn(new Constant[] {new ConstantModule(0)});
133 
134         // Mock the java class
135         when(javaClass.getConstantPool()).thenReturn(cp);
136     }
137 }
138