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.tests;
21  
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  
24  import java.io.IOException;
25  import java.io.OutputStream;
26  
27  import org.apache.bcel.Const;
28  import org.apache.bcel.generic.ClassGen;
29  import org.apache.bcel.generic.ConstantPoolGen;
30  import org.apache.bcel.generic.InstructionConst;
31  import org.apache.bcel.generic.InstructionFactory;
32  import org.apache.bcel.generic.InstructionHandle;
33  import org.apache.bcel.generic.InstructionList;
34  import org.apache.bcel.generic.MethodGen;
35  import org.apache.bcel.generic.PUSH;
36  import org.apache.bcel.generic.Type;
37  
38  /**
39   * Emits a method that applies {@code caload} to an {@code int[]} reference. The {@code caload} instruction requires a
40   * {@code char[]}, so applying it to an array with a different component type is illegal and structural verification must
41   * reject it.
42   */
43  public class TestArrayAccess06Creator extends TestCreator {
44      private final InstructionFactory factory;
45      private final ConstantPoolGen cp;
46      private final ClassGen cg;
47  
48      public TestArrayAccess06Creator() {
49          cg = new ClassGen(TEST_PACKAGE + ".TestArrayAccess06", "java.lang.Object", "TestArrayAccess06.java", Const.ACC_PUBLIC | Const.ACC_SUPER,
50              new String[] {});
51          cp = cg.getConstantPool();
52          factory = new InstructionFactory(cg, cp);
53      }
54  
55      @Override
56      public void create(final OutputStream out) throws IOException {
57          createMethod_0();
58          createMethod_1();
59          cg.getJavaClass().dump(out);
60      }
61  
62      private void createMethod_0() {
63          final InstructionList il = new InstructionList();
64          final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, "<init>", TEST_PACKAGE + ".TestArrayAccess06", il, cp);
65          il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
66          il.append(factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
67          il.append(InstructionFactory.createReturn(Type.VOID));
68          method.setMaxStack();
69          method.setMaxLocals();
70          cg.addMethod(method.getMethod());
71          il.dispose();
72      }
73  
74      private void createMethod_1() {
75          final InstructionList il = new InstructionList();
76          final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, new String[] {}, "test",
77              TEST_PACKAGE + ".TestArrayAccess06", il, cp);
78  
79          il.append(new PUSH(cp, 1));
80          il.append(factory.createNewArray(Type.INT, (short) 1)); // int[]
81          il.append(new PUSH(cp, 0));
82          il.append(InstructionConst.CALOAD); // caload on int[] is illegal
83          il.append(InstructionConst.POP);
84          final InstructionHandle ihReturn = il.append(InstructionFactory.createReturn(Type.VOID));
85          assertNotNull(ihReturn);
86          method.setMaxStack();
87          method.setMaxLocals();
88          cg.addMethod(method.getMethod());
89          il.dispose();
90      }
91  }