1 /*
 2  * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 8186211
27  * @summary CONSTANT_Dynamic_info structure present with various bad BSM index, BSM array attribute checks.
28  * @compile CondyBadBSMIndex.jcod
29  * @compile CondyEmptyBSMArray1.jcod
30  * @compile CondyNoBSMArray.jcod
31  * @run main/othervm -Xverify:all CondyBadBSMArrayTest
32  */
33 
34 // Test that a CONSTANT_Dynamic_info structure present with the following issues:
35 // 1. The CONSTANT_Dynamic_info structure's bootstrap_method_attr_index value is
36 //    an index outside of the array size.
37 // 2. An empty BootstrapMethods Attribute array
38 // 3. No BootstrapMethods Attribute array present.
39 public class CondyBadBSMArrayTest {
40     public static void main(String args[]) throws Throwable {
41         // 1. The CONSTANT_Dynamic_info structure's bootstrap_method_attr_index is outside the array size
42         try {
43             Class newClass = Class.forName("CondyBadBSMIndex");
44             throw new RuntimeException("Expected ClassFormatError exception not thrown");
45         } catch (java.lang.ClassFormatError e) {
46             if (!e.getMessage().contains("Short length on BootstrapMethods in class file")) {
47                 throw new RuntimeException("ClassFormatError thrown, incorrect message");
48             }
49             System.out.println("Test CondyBadBSMIndex passed: " + e.getMessage());
50         } catch (Throwable e) {
51             throw new RuntimeException("Expected ClassFormatError exception not thrown");
52         }
53 
54         // 2. An empty BootstrapMethods Attribute array - contains zero elements
55         try {
56             Class newClass = Class.forName("CondyEmptyBSMArray1");
57             throw new RuntimeException("Expected ClassFormatError exception not thrown");
58         } catch (java.lang.ClassFormatError e) {
59             if (!e.getMessage().contains("Short length on BootstrapMethods in class file")) {
60                 throw new RuntimeException("ClassFormatError thrown, incorrect message: " + e.getMessage());
61             }
62             System.out.println("Test CondyEmptyBSMArray1 passed: " + e.getMessage());
63         } catch (Throwable e) {
64             throw new RuntimeException("Expected ClassFormatError exception not thrown");
65         }
66 
67         // 3. No BootstrapMethods Attribute array present`
68         try {
69             Class newClass = Class.forName("CondyNoBSMArray");
70             throw new RuntimeException("Expected ClassFormatError exception not thrown");
71         } catch (java.lang.ClassFormatError e) {
72             if (!e.getMessage().contains("Missing BootstrapMethods attribute in class file")) {
73                 throw new RuntimeException("ClassFormatError thrown, incorrect message");
74             }
75             System.out.println("Test CondyNoBSMArray passed: " + e.getMessage());
76         } catch (Throwable e) {
77             throw new RuntimeException("Expected ClassFormatError exception not thrown");
78         }
79     }
80 }