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.assertThrowsExactly;
23  
24  import java.io.IOException;
25  import java.util.stream.Stream;
26  
27  import org.apache.bcel.generic.ArrayInstruction;
28  import org.apache.bcel.generic.InstructionConst;
29  import org.apache.bcel.generic.Type;
30  import org.apache.bcel.verifier.tests.TestArrayAccess02Creator;
31  import org.apache.bcel.verifier.tests.TestArrayAccess03Creator;
32  import org.apache.bcel.verifier.tests.TestArrayAccess04DoubleCreator;
33  import org.apache.bcel.verifier.tests.TestArrayAccess04FloatCreator;
34  import org.apache.bcel.verifier.tests.TestArrayAccess04IntCreator;
35  import org.apache.bcel.verifier.tests.TestArrayAccess04LongCreator;
36  import org.apache.bcel.verifier.tests.TestArrayAccess04ShortCreator;
37  import org.apache.bcel.verifier.tests.TestArrayAccess04UnknownCreator;
38  import org.apache.bcel.verifier.tests.TestArrayAccess05Creator;
39  import org.apache.bcel.verifier.tests.TestArrayAccess06Creator;
40  import org.apache.bcel.verifier.tests.TestArrayAccess07Creator;
41  import org.junit.jupiter.api.Test;
42  import org.junit.jupiter.params.ParameterizedTest;
43  import org.junit.jupiter.params.provider.Arguments;
44  import org.junit.jupiter.params.provider.MethodSource;
45  
46  class VerifierArrayAccessTest extends AbstractVerifierTest {
47  
48      /**
49       * Array load and store instructions applied to an array whose component type does not match the instruction.
50       * {@code caload} and {@code iaload} have their own tests, {@link org.apache.bcel.verifier.tests.TestArrayAccess06Creator} and
51       * {@link org.apache.bcel.verifier.tests.TestArrayAccess05Creator}.
52       */
53      static Stream<Arguments> invalidComponentTypeArrayAccess() {
54          return Stream.of(
55          // @formatter:off
56              Arguments.of(InstructionConst.AALOAD, Type.INT),
57              Arguments.of(InstructionConst.AASTORE, Type.INT),
58              Arguments.of(InstructionConst.BALOAD, Type.INT),
59              Arguments.of(InstructionConst.BASTORE, Type.INT),
60              Arguments.of(InstructionConst.CASTORE, Type.INT),
61              Arguments.of(InstructionConst.DALOAD, Type.INT),
62              Arguments.of(InstructionConst.DASTORE, Type.INT),
63              Arguments.of(InstructionConst.FALOAD, Type.INT),
64              Arguments.of(InstructionConst.FASTORE, Type.INT),
65              Arguments.of(InstructionConst.IASTORE, Type.CHAR),
66              Arguments.of(InstructionConst.LALOAD, Type.INT),
67              Arguments.of(InstructionConst.LASTORE, Type.INT),
68              Arguments.of(InstructionConst.SALOAD, Type.INT),
69              Arguments.of(InstructionConst.SASTORE, Type.INT));
70          // @formatter:on
71      }
72  
73      @Test
74      void testInvalidArrayAccess() throws IOException, ClassNotFoundException {
75          new TestArrayAccess03Creator().create();
76          assertVerifyRejected("TestArrayAccess03", "Verification of an arraystore instruction on an object must fail.");
77          new TestArrayAccess04IntCreator().create();
78          assertVerifyRejected("TestArrayAccess04Int", "Verification of an arraystore instruction of an int on an array of references must fail.");
79          new TestArrayAccess04FloatCreator().create();
80          assertVerifyRejected("TestArrayAccess04Float", "Verification of an arraystore instruction of a float on an array of references must fail.");
81          new TestArrayAccess04DoubleCreator().create();
82          assertVerifyRejected("TestArrayAccess04Double", "Verification of an arraystore instruction of a double on an array of references must fail.");
83          new TestArrayAccess04LongCreator().create();
84          assertVerifyRejected("TestArrayAccess04Long", "Verification of an arraystore instruction of a long on an array of references must fail.");
85          new TestArrayAccess04ShortCreator().create();
86          assertVerifyRejected("TestArrayAccess04Short", "Verification of an arraystore instruction of a short on an array of references must fail.");
87          final TestArrayAccess04UnknownCreator testArrayAccess04UnknownCreator = new TestArrayAccess04UnknownCreator();
88          assertThrowsExactly(IllegalArgumentException.class, testArrayAccess04UnknownCreator::create, "Invalid type <unknown object>");
89          new TestArrayAccess05Creator().create();
90          assertVerifyRejected("TestArrayAccess05", "Verification of iaload applied to a multidimensional int[][] must fail.");
91          new TestArrayAccess06Creator().create();
92          assertVerifyRejected("TestArrayAccess06", "Verification of caload applied to an int[] must fail.");
93      }
94  
95      @ParameterizedTest
96      @MethodSource("invalidComponentTypeArrayAccess")
97      void testInvalidComponentTypeArrayAccess(final ArrayInstruction arrayInstruction, final Type arrayElementType)
98          throws IOException, ClassNotFoundException {
99          final TestArrayAccess07Creator creator = new TestArrayAccess07Creator(arrayInstruction, arrayElementType);
100         creator.create();
101         assertVerifyRejected(creator.getSimpleClassName(),
102             "Verification of " + arrayInstruction.getName() + " applied to a " + arrayElementType + "[] must fail.");
103     }
104 
105     @Test
106     void testValidArrayAccess() throws IOException, ClassNotFoundException {
107         assertVerifyOK("TestArrayAccess01", "Verification of an arraystore instruction on an array that is not compatible with the stored element must pass.");
108         new TestArrayAccess02Creator().create();
109         assertVerifyOK("TestArrayAccess02", "Verification of an arraystore instruction on an array that is not compatible with the stored element must pass.");
110     }
111 
112 }