1 /*
  2  * Copyright (c) 2018, 2024, 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  * @ignore Fix JDK-8328423
 26  * @test
 27  * @bug 8223028
 28  * @summary test that the right exceptions get thrown for bad inline type
 29  *          class files.
 30  * @enablePreview
 31  * @compile verifierTests.jcod
 32  * @run main/othervm -verify VerifierInlineTypes
 33  */
 34 
 35 public class VerifierInlineTypes {
 36 
 37     public static void runTestVerifyError(String test_name, String message) throws Exception {
 38         System.out.println("Testing: " + test_name);
 39         try {
 40             Class newClass = Class.forName(test_name);
 41             throw new RuntimeException("Expected VerifyError exception not thrown");
 42         } catch (java.lang.VerifyError e) {
 43             if (!e.getMessage().contains(message)) {
 44                 throw new RuntimeException("Wrong VerifyError: " + e.getMessage());
 45             }
 46         }
 47     }
 48 
 49     public static void runTestFormatError(String test_name, String message) throws Exception {
 50         System.out.println("Testing: " + test_name);
 51         try {
 52             Class newClass = Class.forName(test_name);
 53             throw new RuntimeException("Expected ClassFormatError exception not thrown");
 54         } catch (java.lang.ClassFormatError e) {
 55             if (!e.getMessage().contains(message)) {
 56                 throw new RuntimeException("Wrong ClassFormatError: " + e.getMessage());
 57             }
 58         }
 59     }
 60 
 61     public static void runTestNoError(String test_name) throws Exception {
 62         System.out.println("Testing: " + test_name);
 63         Class newClass = Class.forName(test_name);
 64     }
 65 
 66     public static void main(String[] args) throws Exception {
 67 
 68         // Test that a aconst_init opcode with an out of bounds cp index causes a VerifyError.
 69         runTestVerifyError("defValBadCP", "Illegal constant pool index");
 70 
 71         // Test that ClassFormatError is thrown for a class file, with major version 54, that
 72         // contains a aconst_init opcode.
 73         runTestFormatError("defValBadMajorVersion", "aconst_init not supported by this class file version");
 74 
 75         // Test VerifyError is thrown if a aconst_init's cp entry is not a class.
 76         runTestVerifyError("defValWrongCPType", "Illegal type at constant pool entry");
 77 
 78         // Test that a withfield opcode with an out of bounds cp index causes a VerifyError.
 79         runTestVerifyError("wthFldBadCP", "Illegal constant pool index");
 80 
 81         // Test that VerifyError is thrown if the first operand on the stack is not assignable
 82         // to withfield's field.
 83         runTestVerifyError("wthFldBadFldVal", "Bad type on operand stack");
 84 
 85         // Test that VerifyError is thrown if the second operand on the stack is a primitive.
 86         runTestVerifyError("wthFldBadFldRef", "Bad type on operand stack");
 87 
 88         // Test that ClassFormatError is thrown for a class file, with major version 54, that
 89         // contains a withfield opcode.
 90         runTestFormatError("wthFldBadMajorVersion", "withfield not supported by this class file version");
 91 
 92         // Test VerifyError is thrown if a withfields's cp entry is not a field.
 93         runTestVerifyError("wthFldWrongCPType", "Illegal type at constant pool entry");
 94 
 95         // Test VerifyError is thrown if a aconst_init's cp entry is not an inline type.
 96         runTestVerifyError("defValueObj", "Illegal type at constant pool entry 4");
 97 
 98         // Test that the verifier doesn't require that a withfield bytecode has a Q type operand.
 99         Class newClass = Class.forName("withfieldL");
100 
101         // Test that null is not assignable to an inline type.
102         runTestVerifyError("NoNullVT",
103             "Type null (current frame, stack[1]) is not assignable to 'QNoNullVT;'");
104     }
105 }