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.generic;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  
25  import java.io.ByteArrayOutputStream;
26  import java.io.DataOutputStream;
27  import java.io.IOException;
28  
29  import org.apache.bcel.Const;
30  import org.apache.bcel.classfile.ClassFormatException;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests that {@link TABLESWITCH} and {@link LOOKUPSWITCH} validate their match table counts against the remaining code bytes instead of allocating whatever
35   * amount of memory the raw count fields request.
36   */
37  class SwitchAllocationTest {
38  
39      private static byte[] switchBytes(final short opcode, final int... operands) throws IOException {
40          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
41          try (DataOutputStream dos = new DataOutputStream(baos)) {
42              dos.writeByte(opcode);
43              for (int i = 0; i < 3; i++) {
44                  dos.writeByte(0); // padding up to the next 4-byte boundary
45              }
46              for (final int operand : operands) {
47                  dos.writeInt(operand);
48              }
49          }
50          return baos.toByteArray();
51      }
52  
53      @Test
54      void testLookupswitchHugeNpairsRejected() throws IOException {
55          // default = 0, npairs = Integer.MAX_VALUE, no pairs present.
56          final byte[] code = switchBytes(Const.LOOKUPSWITCH, 0, Integer.MAX_VALUE);
57          assertThrows(ClassFormatException.class, () -> new InstructionList(code));
58      }
59  
60      @Test
61      void testLookupswitchNegativeNpairsRejected() throws IOException {
62          final byte[] code = switchBytes(Const.LOOKUPSWITCH, 0, -1);
63          assertThrows(ClassFormatException.class, () -> new InstructionList(code));
64      }
65  
66      @Test
67      void testLookupswitchValidAccepted() throws IOException {
68          // default = 0, npairs = 1, one (match, offset) pair branching back to the instruction itself.
69          final byte[] code = switchBytes(Const.LOOKUPSWITCH, 0, 1, 5, 0);
70          assertEquals(1, new InstructionList(code).getLength());
71      }
72  
73      @Test
74      void testTableswitchHugeRangeRejected() throws IOException {
75          // default = 0, low = 0, high = Integer.MAX_VALUE - 1: requests roughly 8 GB across the three tables.
76          final byte[] code = switchBytes(Const.TABLESWITCH, 0, 0, Integer.MAX_VALUE - 1);
77          assertThrows(ClassFormatException.class, () -> new InstructionList(code));
78      }
79  
80      @Test
81      void testTableswitchOverflowingRangeRejected() throws IOException {
82          // high - low + 1 overflows int arithmetic to a negative value.
83          final byte[] code = switchBytes(Const.TABLESWITCH, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
84          assertThrows(ClassFormatException.class, () -> new InstructionList(code));
85      }
86  
87      @Test
88      void testTableswitchValidAccepted() throws IOException {
89          // default = 0, low = 0, high = 1, two jump offsets branching back to the instruction itself.
90          final byte[] code = switchBytes(Const.TABLESWITCH, 0, 0, 1, 0, 0);
91          assertEquals(1, new InstructionList(code).getLength());
92      }
93  }