1 /*
   2  * Copyright (c) 2012, 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 /*
  26  * @test Test7116786
  27  * @summary verify that VerifyError messages are as expected
  28  * @library testcases.jar
  29  * @run main/othervm -Xverify:all Test7116786
  30  */
  31 
  32 
  33 /**
  34  * This class contains information regarding when a VerifyError is thrown
  35  * in the verifier.  Most of the data is informational-only, and can be
  36  * used to track down where and why VerifyErrors are thrown.  As such it
  37  * is possible the information may go out-of-date.
  38  *
  39  * The only fields used for the purpose of testing is the 'caseName' and
  40  * the 'message'.  The 'caseName' corresponds to a classfile which exhibits
  41  * the VerifyError, and the 'message' is a regular expression which we expect
  42  * to match the verify error message.  If the 'message' doesn't match what
  43  * we expect, it warrents investigation to see if we are still triggering
  44  * the VerifyError that we expect.  It could simply just be that the message
  45  * changed, which is fine.
  46  *
  47  * Some cases are not testable, either because the code is probably unreachable
  48  * or the test classfile would be too onerous to create.  These cases are
  49  * marked with 'testable' == false, and the test runner will skip them.
  50  */
  51 class Case {
  52     private String caseName;    // Name of the case
  53     private String file;        // Source file where VerifyError is thrown
  54     private String location;    // enclosing function or switch case
  55     private String description; // What causes this VerifyError
  56     private String message;     // The VerifyError message used.
  57 
  58     private boolean testable;   // Whether this case is testable or not.
  59 
  60     public Case(String caseName, String file, boolean testable,
  61                 String location, String description, String message) {
  62         this.caseName = caseName;
  63         this.file = file;
  64         this.testable = testable;
  65         this.location = location;
  66         this.description = description;
  67         this.message = message;
  68     }
  69 
  70     String getCaseName() { return this.caseName; }
  71     String getFile() { return this.file; }
  72     String getLocation() { return this.location; }
  73     String getDescription() { return this.description; }
  74     String getMessage() { return this.message; }
  75 
  76     boolean isTestable() { return this.testable; }
  77 }
  78 
  79 /**
  80  * These are the locations in the source code where VerifyErrors are thrown
  81  * as of today, 2012/07/18.  These may change as the verification code is
  82  * modified, which is ok.  This test is trying to provide coverage for all
  83  * VerifyErrors (just to make sure there are no crashes) and it's probably
  84  * not necessary to update it every time the VM changes.
  85  */
  86 class VerifyErrorCases {
  87     public static final Case[] cases = {
  88 
  89         new Case("case00", "stackMapFrame.cpp", true, "pop_stack_ex",
  90                  "stack underflow",
  91                  "Operand stack underflow"),
  92 
  93         new Case("case01", "stackMapFrame.cpp", true, "pop_stack_ex",
  94                  "stack pop not assignable to expected",
  95                  "Bad type on operand stack"),
  96 
  97         new Case("case02", "stackMapFrame.cpp", true, "get_local",
  98                  "local index out-of-bounds",
  99                  "Local variable table overflow"),
 100 
 101         new Case("case03", "stackMapFrame.cpp", true, "get_local",
 102                  "local not assignable to expected",
 103                  "Bad local variable type"),
 104 
 105         new Case("case04", "stackMapFrame.cpp", true, "get_local_2",
 106                  "local index out-of-bounds [type2]",
 107                  "get long/double overflows locals"),
 108 
 109         new Case("case05", "stackMapFrame.cpp", true, "get_local_2",
 110                  "local not assignabled to expected [type2]",
 111                  "Bad local variable type"),
 112 
 113         /* Unreachable: Can't split long/double on stack */
 114         new Case("case06", "stackMapFrame.cpp", false, "get_local_2",
 115                  "local second-word not assignabled to expected",
 116                  "Bad local variable type"),
 117 
 118         new Case("case07", "stackMapFrame.cpp", true, "set_local",
 119                  "local index out-of-bounds",
 120                  "Local variable table overflow"),
 121 
 122         new Case("case08", "stackMapFrame.cpp", true, "set_local_2",
 123                  "local index out-of-bounds [type2]",
 124                  "Local variable table overflow"),
 125 
 126         new Case("case09", "stackMapFrame.hpp", true, "push_stack",
 127                  "stack overflow",
 128                  "Operand stack overflow"),
 129 
 130         new Case("case10", "stackMapFrame.hpp", true, "push_stack_2",
 131                  "stack overflow [type2]",
 132                  "Operand stack overflow"),
 133 
 134         new Case("case11", "stackMapFrame.hpp", true, "pop_stack",
 135                  "stack underflow",
 136                  "Operand stack underflow"),
 137 
 138         new Case("case12", "stackMapTable.cpp", true, "StackMapTable ctor",
 139                  "stackmap offset beyond code size",
 140                  "StackMapTable error: bad offset"),
 141 
 142         new Case("case13", "stackMapTable.cpp", true, "match_stackmap",
 143                  "no stackmap frame at expected location",
 144                  "Expecting a stackmap frame at branch target "),
 145 
 146         new Case("case14", "stackMapTable.cpp", true, "check_jump_target",
 147                  "no stackmap frame at jump location or bad jump",
 148                  "Inconsistent stackmap frames at branch target "),
 149 
 150         new Case("case15", "stackMapTable.cpp", true, "check_new_object",
 151                  "backward jump with uninit",
 152                  "Uninitialized object exists on backward branch "),
 153 
 154         /* Unreachable: wide instructions verified during bytecode analysis */
 155         new Case("case16", "verifier.cpp", false, "loop header",
 156                  "bad op in wide instruction",
 157                  "Bad wide instruction"),
 158 
 159         new Case("case17", "verifier.cpp", true, "case iaload",
 160                  "TOS not X array",
 161                  "Bad type on operand stack in iaload"),
 162 
 163         new Case("case18", "verifier.cpp", true, "case baload",
 164                  "TOS not X array",
 165                  "Bad type on operand stack in baload"),
 166 
 167         new Case("case19", "verifier.cpp", true, "case caload",
 168                  "TOS not X array",
 169                  "Bad type on operand stack in caload"),
 170 
 171         new Case("case20", "verifier.cpp", true, "case saload",
 172                  "TOS not X array",
 173                  "Bad type on operand stack in saload"),
 174 
 175         new Case("case21", "verifier.cpp", true, "case laload",
 176                  "TOS not X array",
 177                  "Bad type on operand stack in laload"),
 178 
 179         new Case("case22", "verifier.cpp", true, "case faload",
 180                  "TOS not X array",
 181                  "Bad type on operand stack in faload"),
 182 
 183         new Case("case23", "verifier.cpp", true, "case daload",
 184                  "TOS not X array",
 185                  "Bad type on operand stack in daload"),
 186 
 187         new Case("case24", "verifier.cpp", true, "case aaload",
 188                  "TOS not X array",
 189                  "Bad type on operand stack in aaload"),
 190 
 191         new Case("case25", "verifier.cpp", true, "case iastore",
 192                  "TOS not int array",
 193                  "Bad type on operand stack in iastore"),
 194 
 195         new Case("case26", "verifier.cpp", true, "case bastore",
 196                  "TOS not byte array",
 197                  "Bad type on operand stack in bastore"),
 198 
 199         new Case("case27", "verifier.cpp", true, "case castore",
 200                  "TOS not char array",
 201                  "Bad type on operand stack in castore"),
 202 
 203         new Case("case28", "verifier.cpp", true, "case sastore",
 204                  "TOS not short array",
 205                  "Bad type on operand stack in sastore"),
 206 
 207         new Case("case29", "verifier.cpp", true, "case lastore",
 208                  "TOS not long array",
 209                  "Bad type on operand stack in lastore"),
 210 
 211         new Case("case30", "verifier.cpp", true, "case fastore",
 212                  "TOS not float array",
 213                  "Bad type on operand stack in fastore"),
 214 
 215         new Case("case31", "verifier.cpp", true, "case dastore",
 216                  "TOS not double array",
 217                  "Bad type on operand stack in dastore"),
 218 
 219         new Case("case32", "verifier.cpp", true, "case aastore",
 220                  "TOS not object array",
 221                  "Bad type on operand stack in aastore"),
 222 
 223         /* Unreachable: In order to hit this case, we would need a
 224          * category2_1st at TOS which is not possible. */
 225         new Case("case33", "verifier.cpp", false, "case pop2",
 226                  "TOS is category2_1st (would split)",
 227                  "Bad type on operand stack in pop2"),
 228 
 229         /* Unreachable: In order to hit this case, we would need a
 230          * category2_1st at stack depth 2 with category_1 on TOS which is not
 231          * possible. */
 232         new Case("case34", "verifier.cpp", false, "case dup_x2",
 233                  "TOS-1 is category2_1st (would split)",
 234                  "Bad type on operand stack in dup_x2"),
 235 
 236         /* Unreachable: In order to hit this case, we would need a
 237          * category2_1st at TOS which is not possible. */
 238         new Case("case35", "verifier.cpp", false, "case dup2",
 239                  "TOS-1 is category2_1st (would split)",
 240                  "Bad type on operand stack in dup2"),
 241 
 242         /* Unreachable: In order to hit this case, we would need a
 243          * category2_1st at TOS which is not possible. */
 244         new Case("case36", "verifier.cpp", false, "case dup2_x1",
 245                  "TOS-1 is category2_1st (would split)",
 246                  "Bad type on operand stack in dup2_x1"),
 247 
 248         /* Unreachable: In order to hit this case, we would need a
 249          * category2_1st at TOS which is not possible. */
 250         new Case("case37", "verifier.cpp", false, "case dup2_x2",
 251                  "TOS-1 is category2_1st (would split)",
 252                  "Bad type on operand stack in dup2_x2"),
 253 
 254         /* Unreachable: In order to hit this case, we would need a
 255          * category2_1st at stack depth 3 with either 2 category_1 or 1
 256          * category_2 on TOS, which is not possible. */
 257         new Case("case38", "verifier.cpp", false, "case dup2_x2",
 258                  "TOS-3 is category2_1st (would split)",
 259                  "Bad type on operand stack in dup2_x2"),
 260 
 261         new Case("case39", "verifier.cpp", true, "case return",
 262                  "return type of method is not void",
 263                  "Method expects a return value"),
 264 
 265         new Case("case40", "verifier.cpp", true, "case return",
 266                  "return with uninitialized this ",
 267                  "Constructor must call super() or this() before return"),
 268 
 269         new Case("case41", "verifier.cpp", true, "case new",
 270                  "cp index not a class type",
 271                  "Illegal new instruction"),
 272 
 273         new Case("case42", "verifier.cpp", true, "case arraylength",
 274                  "TOS is not an array",
 275                  "Bad type on operand stack in arraylength"),
 276 
 277         new Case("case43", "verifier.cpp", true, "case multianewarray",
 278                  "CP index does not refer to array type",
 279                  "Illegal constant pool index in multianewarray instruction"),
 280 
 281         new Case("case44", "verifier.cpp", true, "case multianewarray",
 282                  "Bad dimension (<1) or does not match CP signature",
 283                  "Illegal dimension in multianewarray instruction: "),
 284 
 285         new Case("case45", "verifier.cpp", true, "case default",
 286                  "Unrecognized bytecode",
 287                  "Bad instruction: "),
 288 
 289         new Case("case46", "verifier.cpp", true, "loop end",
 290                  "control flow falls off method",
 291                  "Control flow falls through code end"),
 292 
 293         new Case("case47", "verifier.cpp", true, "generate_code_data",
 294                  "illegal bytecode via RawBytecodeStream (breakpoint)",
 295                  "Bad instruction"),
 296 
 297         new Case("case48", "verifier.cpp", true, "generate_code_data",
 298                  "illegal bytecode via RawBytecodeStream (other illegal)",
 299                  "Bad instruction"),
 300 
 301         new Case("case49", "verifier.cpp", true,
 302                  "verify_exception_handler_table",
 303                  "catch_type is not throwable",
 304                  "Catch type is not a subclass of Throwable in " +
 305                  "exception handler "),
 306 
 307         new Case("case50", "verifier.cpp", true, "verify_stackmap_table",
 308                  "missing a stack map frame @ target location (mid table)",
 309                  "Expecting a stack map frame"),
 310 
 311         new Case("case51", "verifier.cpp", true, "verify_stackmap_table",
 312                  "stack map does not match?",
 313                  "Instruction type does not match stack map"),
 314 
 315         new Case("case52", "verifier.cpp", true, "verify_stackmap_table",
 316                  "missing a stack map frame @ target location (end of table)",
 317                  "Expecting a stack map frame"),
 318 
 319         new Case("case53", "verifier.cpp", true,
 320                  "verify_exception_handler_targets",
 321                  "stackmap mismatch at exception handler",
 322                  "Stack map does not match the one at exception handler "),
 323 
 324         new Case("case54", "verifier.cpp", true, "verify_cp_index",
 325                  "constant pool index is out-of-bounds",
 326                  "Illegal constant pool index "),
 327 
 328         new Case("case55", "verifier.cpp", true, "verify_cp_type",
 329                  "constant pool entry is not expected type",
 330                  "Illegal type at constant pool entry "),
 331 
 332         new Case("case56", "verifier.cpp", true, "verify_cp_class_type",
 333                  "constant pool entry is not an object type",
 334                  "Illegal type at constant pool entry "),
 335 
 336         /* Unreachable: verify_cp_type gates this case */
 337         new Case("case57", "verifier.cpp", false, "verify_ldc",
 338                  "invalid constant pool index in ldc",
 339                  "Invalid index in ldc"),
 340 
 341         new Case("case58", "verifier.cpp", true, "verify_switch",
 342                  "bad switch padding",
 343                  "Nonzero padding byte in lookswitch or tableswitch"),
 344 
 345         new Case("case59", "verifier.cpp", true, "verify_switch",
 346                  "tableswitch low is greater than high",
 347                  "low must be less than or equal to high in tableswitch"),
 348 
 349         /* Unreachable on 64-bit?  Only way to get here is to overflow
 350          * the 'keys' variable which can't happen on 64-bit since we're dealing
 351          * with 32-bit values.  Perhaps reachable on 32-bit but the
 352          * triggering class would be quite large */
 353         new Case("case60", "verifier.cpp", false, "verify_switch",
 354                  "high - low + 1 < 0 (overflow?)",
 355                  "too many keys in tableswitch"),
 356 
 357         /* Would have to create a 16G classfile to trip this.  Possible but
 358          * not reasonable to do in a test.  */
 359         new Case("case61", "verifier.cpp", false, "verify_switch",
 360                  "lookupswitch keys < 0",
 361                  "number of keys in lookupswitch less than 0"),
 362 
 363         new Case("case62", "verifier.cpp", true, "verify_switch",
 364                  "lookupswitch keys out-of-order",
 365                  "Bad lookupswitch instruction"),
 366 
 367         /* Unreachable: Class file parser verifies Fieldref contents */
 368         new Case("case63", "verifier.cpp", false, "verify_field_instructions",
 369                  "referenced class is not an CP object",
 370                  "Expecting reference to class in class "),
 371 
 372         new Case("case64", "verifier.cpp", true, "verify_field_instructions",
 373                  "TOS not assignable to field type in putfield",
 374                  "Bad type on operand stack in putfield"),
 375 
 376         new Case("case65", "verifier.cpp", true, "verify_field_instructions",
 377                  "TOS not assignable to class when accessing protected field",
 378                  "Bad access to protected data in getfield"),
 379 
 380         new Case("case66", "verifier.cpp", true, "verify_invoke_init",
 381                  "Uninit_this is not of the current type or it's supertype",
 382                  "Bad <init> method call"),
 383 
 384         /* Unreachable:  Stack map parsing ensures valid type and new
 385          * instructions have a valid BCI. */
 386         new Case("case67", "verifier.cpp", false, "verify_invoke_init",
 387                  "Uninit type with bad new instruction index",
 388                  "Expecting new instruction"),
 389 
 390         new Case("case68", "verifier.cpp", true, "verify_invoke_init",
 391                  "calling other class's <init> method",
 392                  "Call to wrong <init> method"),
 393 
 394         new Case("case69", "verifier.cpp", true, "verify_invoke_init",
 395                  "Calling protected <init> and type unassignable from current",
 396                  "Bad access to protected <init> method"),
 397 
 398         new Case("case70", "verifier.cpp", true, "verify_invoke_init",
 399                  "TOS is not an uninitialized (or Uninit_this) type",
 400                  "Bad operand type when invoking <init>"),
 401 
 402         new Case("case71", "verifier.cpp", true, "verify_invoke_instructions",
 403                  "Arg count in instruction doesn't match signature",
 404                  "Inconsistent args count operand in invokeinterface"),
 405 
 406         new Case("case72", "verifier.cpp", true, "verify_invoke_instructions",
 407                  "Non-zero pad in invokeinterface",
 408                  "Fourth operand byte of invokeinterface must be zero"),
 409 
 410         new Case("case73", "verifier.cpp", true, "verify_invoke_instructions",
 411                  "Non-zero pad in invokedynamic",
 412                  "Third and fourth operand bytes of " +
 413                  "invokedynamic must be zero"),
 414 
 415         new Case("case74", "verifier.cpp", true, "verify_invoke_instructions",
 416                  "Non-invokespecial trying to invoke a '<' method",
 417                  "Illegal call to internal method"),
 418 
 419         new Case("case75", "verifier.cpp", true, "verify_invoke_instructions",
 420                  "invokespecial and current unassignable from referenced type",
 421                  "Bad invokespecial instruction: current class isn't " +
 422                  "assignable to reference class."),
 423 
 424         new Case("case76", "verifier.cpp", true, "verify_invoke_instructions",
 425                  "TOS not assignable to current when calling protected method",
 426                  "Bad access to protected data in invokevirtual"),
 427 
 428         /* Unreachable:  class file parser enforces void signature */
 429         new Case("case77", "verifier.cpp", false, "verify_invoke_instructions",
 430                  "<init> method is not void return",
 431                  "Return type must be void in <init> method"),
 432 
 433         new Case("case78", "verifier.cpp", true, "get_newarray_type",
 434                  "newarray type invalid",
 435                  "Illegal newarray instruction"),
 436 
 437         new Case("case79", "verifier.cpp", true, "verify_return_value",
 438                  "void return from method which has a return value",
 439                  "Method expects a return value"),
 440 
 441         new Case("case80", "verifier.cpp", true, "verify_return_value",
 442                  "TOS type does not match signature",
 443                  "Bad return type")
 444     };
 445 }
 446 
 447 public class Test7116786 {
 448     public static void main(String argv[]) throws Exception {
 449         for (Case c : VerifyErrorCases.cases) {
 450             System.out.println("******** " + c.getCaseName() + " ********");
 451             if (c.isTestable()) {
 452                 try {
 453                     ClassLoader cl = Test7116786.class.getClassLoader();
 454                     Class<?> cls = Class.forName(c.getCaseName(), true, cl);
 455                     throw new RuntimeException(
 456                         "++ FAIL: No verify error encountered");
 457                 } catch (VerifyError ve) {
 458                     String message = c.getMessage();
 459                     String veMessage = ve.getMessage();
 460                     System.out.print(veMessage);
 461                     if (!veMessage.startsWith(message)) {
 462                         // We're not seeing the message we expect.  Could be
 463                         // that we've gotten the wrong VerifyError case, or
 464                         // maybe the message changed.
 465                         System.out.println("++ FAIL? " +
 466                             "Message does not match what was expected: " +
 467                             message);
 468                         continue;
 469                     }
 470                     if (!veMessage.contains("Exception Details:") &&
 471                         !veMessage.contains("Reason:")) {
 472                         System.out.println("++ FAIL: No details found");
 473                         throw new RuntimeException("FAIL: No details found");
 474                     }
 475                     System.out.println("++ PASS");
 476                 }
 477             } else {
 478                System.out.println("++ SKIPPED");
 479             }
 480         }
 481     }
 482 }