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