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.classfile;
21  
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.ByteArrayOutputStream;
27  import java.io.DataInputStream;
28  import java.io.DataOutputStream;
29  import java.io.IOException;
30  
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests that {@link ElementValue#readElementValue(java.io.DataInput, ConstantPool)} bounds the combined annotation/array nesting depth instead of recursing
35   * until a {@link StackOverflowError}.
36   */
37  class ElementValueNestingTest {
38  
39      /**
40       * Wraps the given element value bytes into an annotation element value ('@'): one annotation with a single element value pair.
41       */
42      private static byte[] annotationOf(final byte[] inner) throws IOException {
43          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
44          try (DataOutputStream dos = new DataOutputStream(baos)) {
45              dos.writeByte(ElementValue.ANNOTATION);
46              dos.writeShort(0); // type_index
47              dos.writeShort(1); // num_element_value_pairs
48              dos.writeShort(0); // element_name_index
49              dos.write(inner);
50          }
51          return baos.toByteArray();
52      }
53  
54      /**
55       * Wraps the given element value bytes into an array element value ('[') with a single element.
56       */
57      private static byte[] arrayOf(final byte[] inner) throws IOException {
58          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
59          try (DataOutputStream dos = new DataOutputStream(baos)) {
60              dos.writeByte(ElementValue.ARRAY);
61              dos.writeShort(1); // num_values
62              dos.write(inner);
63          }
64          return baos.toByteArray();
65      }
66  
67      private static byte[] stringValue() {
68          return new byte[] {ElementValue.STRING, 0, 0};
69      }
70  
71      @Test
72      void testAlternatingArrayAnnotationNestingRejected() throws IOException {
73          byte[] bytes = stringValue();
74          for (int i = 0; i < 300; i++) {
75              bytes = arrayOf(annotationOf(bytes));
76          }
77          try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes))) {
78              assertThrows(ClassFormatException.class, () -> ElementValue.readElementValue(in, new ConstantPool(new ConstantUtf8("Test"))));
79          }
80      }
81  
82      @Test
83      void testDeeplyNestedAnnotationsRejected() throws IOException {
84          byte[] bytes = stringValue();
85          for (int i = 0; i < 300; i++) {
86              bytes = annotationOf(bytes);
87          }
88          try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes))) {
89              assertThrows(ClassFormatException.class, () -> ElementValue.readElementValue(in, new ConstantPool(new ConstantUtf8("Test"))));
90          }
91      }
92  
93      @Test
94      void testModeratelyNestedAnnotationsAccepted() throws IOException {
95          byte[] bytes = stringValue();
96          for (int i = 0; i < 3; i++) {
97              bytes = annotationOf(arrayOf(bytes));
98          }
99          try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes))) {
100             assertTrue(ElementValue.readElementValue(in, new ConstantPool(new ConstantUtf8("Test"))) instanceof AnnotationElementValue);
101         }
102     }
103 }