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 java.io.IOException;
23  import java.io.OutputStream;
24  
25  import org.apache.bcel.Const;
26  import org.apache.bcel.classfile.JavaClass;
27  import org.apache.bcel.generic.ArrayInstruction;
28  import org.apache.bcel.generic.ClassGen;
29  import org.apache.bcel.generic.ConstantPoolGen;
30  import org.apache.bcel.generic.Instruction;
31  import org.apache.bcel.generic.InstructionConst;
32  import org.apache.bcel.generic.InstructionFactory;
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 the given array load or store instruction to an array whose component type does not
40   * match the instruction, for example {@code castore} on an {@code int[]} or {@code laload} on an {@code int[]}.
41   * Structural verification must reject every such access.
42   */
43  public class TestArrayAccess07Creator extends TestCreator {
44      private final InstructionFactory factory;
45      private final ConstantPoolGen cp;
46      private final ClassGen cg;
47      private final ArrayInstruction arrayInstruction;
48      private final Type arrayElementType;
49      private final String simpleClassName;
50  
51      public TestArrayAccess07Creator(final ArrayInstruction arrayInstruction, final Type arrayElementType) {
52          this.arrayInstruction = arrayInstruction;
53          this.arrayElementType = arrayElementType;
54          final String name = arrayInstruction.getName();
55          simpleClassName = "TestArrayAccess07" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
56          cg = new ClassGen(TEST_PACKAGE + "." + simpleClassName, "java.lang.Object", simpleClassName + ".java", Const.ACC_PUBLIC | Const.ACC_SUPER,
57              new String[] {});
58          cp = cg.getConstantPool();
59          factory = new InstructionFactory(cg, cp);
60      }
61  
62      @Override
63      public void create(final OutputStream out) throws IOException {
64          createMethod_0();
65          createMethod_1();
66          cg.getJavaClass().dump(out);
67      }
68  
69      private void createMethod_0() {
70          final InstructionList il = new InstructionList();
71          final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, "<init>", TEST_PACKAGE + "." + simpleClassName, il,
72              cp);
73          il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
74          il.append(factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
75          il.append(InstructionFactory.createReturn(Type.VOID));
76          method.setMaxStack();
77          method.setMaxLocals();
78          cg.addMethod(method.getMethod());
79          il.dispose();
80      }
81  
82      private void createMethod_1() {
83          final InstructionList il = new InstructionList();
84          final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, new String[] {}, "test",
85              TEST_PACKAGE + "." + simpleClassName, il, cp);
86  
87          il.append(new PUSH(cp, 1));
88          il.append(factory.createNewArray(arrayElementType, (short) 1));
89          il.append(new PUSH(cp, 0));
90          if (arrayInstruction.produceStack(cp) == 0) {
91              // store: push a value of the type the instruction stores, then access the mismatched array
92              il.append(storedValue());
93              il.append(arrayInstruction);
94          } else {
95              // load: access the mismatched array, then pop the loaded value
96              il.append(arrayInstruction);
97              il.append(arrayInstruction.produceStack(cp) == 2 ? InstructionConst.POP2 : InstructionConst.POP);
98          }
99          il.append(InstructionFactory.createReturn(Type.VOID));
100         method.setMaxStack();
101         method.setMaxLocals();
102         cg.addMethod(method.getMethod());
103         il.dispose();
104     }
105 
106     @Override
107     protected String getClassName() {
108         return simpleClassName + JavaClass.EXTENSION;
109     }
110 
111     public String getSimpleClassName() {
112         return simpleClassName;
113     }
114 
115     private Instruction storedValue() {
116         switch (arrayInstruction.getOpcode()) {
117         case Const.AASTORE:
118             return InstructionConst.ACONST_NULL;
119         case Const.DASTORE:
120             return InstructionConst.DCONST_1;
121         case Const.FASTORE:
122             return InstructionConst.FCONST_1;
123         case Const.LASTORE:
124             return InstructionConst.LCONST_1;
125         default:
126             return InstructionConst.ICONST_1;
127         }
128     }
129 }