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.DataInput;
28  import java.io.DataInputStream;
29  import java.io.DataOutputStream;
30  import java.io.IOException;
31  
32  import org.junit.jupiter.params.ParameterizedTest;
33  import org.junit.jupiter.params.provider.ValueSource;
34  
35  /**
36   * Tests that {@link Attribute#readAttribute(DataInput, ConstantPool)} bounds the attribute nesting depth instead of recursing until a
37   * {@link StackOverflowError}.
38   */
39  class AttributeNestingTest {
40  
41      /**
42       * Builds a Code attribute nested {@code depth} times inside itself: each level declares one sub-attribute, which is again a Code attribute.
43       */
44      private static byte[] nestedCodeAttribute(final int depth, final boolean compactCodeHeader) throws IOException {
45          byte[] inner = {};
46          int attributeCount = 0;
47          for (int i = 0; i < depth; i++) {
48              final ByteArrayOutputStream baos = new ByteArrayOutputStream();
49              try (DataOutputStream dos = new DataOutputStream(baos)) {
50                  dos.writeShort(1); // attribute_name_index -> "Code"
51                  dos.writeInt((compactCodeHeader ? 8 : 12) + inner.length); // attribute_length
52                  if (compactCodeHeader) {
53                      dos.writeByte(0); // max_stack
54                      dos.writeByte(0); // max_locals
55                      dos.writeShort(0); // code_length
56                  } else {
57                      dos.writeShort(0); // max_stack
58                      dos.writeShort(0); // max_locals
59                      dos.writeInt(0); // code_length
60                  }
61                  dos.writeShort(0); // exception_table_length
62                  dos.writeShort(attributeCount); // attributes_count
63                  dos.write(inner);
64              }
65              inner = baos.toByteArray();
66              attributeCount = 1;
67          }
68          return inner;
69      }
70  
71      @ParameterizedTest
72      @ValueSource(booleans = {false, true})
73      void testDeeplyNestedCodeAttributeRejected(final boolean compactCodeHeader) throws IOException {
74          final ConstantPool cp = new ConstantPool(new ConstantUtf8("unused index 0"), new ConstantUtf8("Code"));
75          final byte[] bytes = nestedCodeAttribute(1_000, compactCodeHeader);
76          try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes))) {
77              assertThrows(ClassFormatException.class, () -> Attribute.readAttribute(in, cp, compactCodeHeader));
78          }
79      }
80  
81      @ParameterizedTest
82      @ValueSource(booleans = {false, true})
83      void testModeratelyNestedCodeAttributeAccepted(final boolean compactCodeHeader) throws IOException {
84          final ConstantPool cp = new ConstantPool(new ConstantUtf8("unused index 0"), new ConstantUtf8("Code"));
85          final byte[] bytes = nestedCodeAttribute(3, compactCodeHeader);
86          try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes))) {
87              assertTrue(Attribute.readAttribute(in, cp, compactCodeHeader) instanceof Code);
88          }
89      }
90  }